From 5154e8ff7537713e82292c468aaeeeb708c56bfb Mon Sep 17 00:00:00 2001 From: loc Date: Mon, 13 Apr 2020 08:37:41 -0700 Subject: [PATCH] fix: enable workaround for nativeWindowOpen hang (#22825) --- .../browser/api/electron_api_web_contents.cc | 24 +++++++++++++++++++ shell/browser/api/electron_api_web_contents.h | 15 ++++++++++++ spec-main/api-web-contents-spec.ts | 22 +++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 5bd2e3be4d06..970af86312ec 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -663,6 +663,30 @@ void WebContents::WebContentsCreatedWithFullParams( tracker->body = params.body; } +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 c13f59e40622..980ff85b0630 100644 --- a/shell/browser/api/electron_api_web_contents.h +++ b/shell/browser/api/electron_api_web_contents.h @@ -384,6 +384,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 WebContentsCreatedWithFullParams( content::WebContents* source_contents, int opener_render_process_id, diff --git a/spec-main/api-web-contents-spec.ts b/spec-main/api-web-contents-spec.ts index 9cabad454ed9..016213231d10 100644 --- a/spec-main/api-web-contents-spec.ts +++ b/spec-main/api-web-contents-spec.ts @@ -1829,4 +1829,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(); + }); });