diff --git a/docs/api/structures/extension.md b/docs/api/structures/extension.md index 824e03a937ca0..df9e77c202699 100644 --- a/docs/api/structures/extension.md +++ b/docs/api/structures/extension.md @@ -5,4 +5,4 @@ * `name` String * `path` String - The extension's file path. * `version` String -* `url` String - The extension's `chrome-extension://` URL. \ No newline at end of file +* `url` String - The extension's `chrome-extension://` URL. diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 512204c5f14c8..7eee3504b624f 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -654,6 +654,30 @@ void WebContents::WebContentsCreated(content::WebContents* source_contents, tracker->frame_name = frame_name; } +bool WebContents::IsWebContentsCreationOverridden( + content::SiteInstance* source_site_instance, + content::mojom::WindowContainerType window_container_type, + const GURL& opener_url, + const std::string& frame_name, + const GURL& target_url) { + if (Emit("-will-add-new-contents", target_url, frame_name)) { + return true; + } + return false; +} + +content::WebContents* WebContents::CreateCustomWebContents( + content::RenderFrameHost* opener, + content::SiteInstance* source_site_instance, + bool is_new_browsing_instance, + const GURL& opener_url, + const std::string& frame_name, + const GURL& target_url, + const std::string& partition_id, + content::SessionStorageNamespace* session_storage_namespace) { + return nullptr; +} + void WebContents::AddNewContents( content::WebContents* source, std::unique_ptr new_contents, diff --git a/shell/browser/api/electron_api_web_contents.h b/shell/browser/api/electron_api_web_contents.h index 5566b43b3c3d8..af54186a3c303 100644 --- a/shell/browser/api/electron_api_web_contents.h +++ b/shell/browser/api/electron_api_web_contents.h @@ -376,6 +376,21 @@ class WebContents : public gin_helper::TrackableObject, const base::string16& message, int32_t line_no, const base::string16& source_id) override; + bool IsWebContentsCreationOverridden( + content::SiteInstance* source_site_instance, + content::mojom::WindowContainerType window_container_type, + const GURL& opener_url, + const std::string& frame_name, + const GURL& target_url) override; + content::WebContents* CreateCustomWebContents( + content::RenderFrameHost* opener, + content::SiteInstance* source_site_instance, + bool is_new_browsing_instance, + const GURL& opener_url, + const std::string& frame_name, + const GURL& target_url, + const std::string& partition_id, + content::SessionStorageNamespace* session_storage_namespace) override; void WebContentsCreated(content::WebContents* source_contents, int opener_render_process_id, int opener_render_frame_id, diff --git a/spec-main/api-web-contents-spec.ts b/spec-main/api-web-contents-spec.ts index 20a23d044381e..47c5f92eae45b 100644 --- a/spec-main/api-web-contents-spec.ts +++ b/spec-main/api-web-contents-spec.ts @@ -1828,4 +1828,26 @@ describe('webContents module', () => { expect(body).to.equal('401'); }); }); + + it('emits a cancelable event before creating a child webcontents', async () => { + const w = new BrowserWindow({ + show: false, + webPreferences: { + sandbox: true + } + }); + w.webContents.on('-will-add-new-contents' as any, (event: any, url: any) => { + expect(url).to.equal('about:blank'); + event.preventDefault(); + }); + let wasCalled = false; + w.webContents.on('new-window' as any, () => { + wasCalled = true; + }); + await w.loadURL('about:blank'); + await w.webContents.executeJavaScript(`window.open('about:blank')`); + await new Promise((resolve) => { process.nextTick(resolve); }); + expect(wasCalled).to.equal(false); + await closeAllWindows(); + }); });