From 09b17c86d54b351c5a85eb8118eee63816ab6d1e Mon Sep 17 00:00:00 2001 From: Andy Locascio Date: Thu, 12 Dec 2019 15:01:31 -0800 Subject: [PATCH 1/3] fix: enable workaround for nativeWindowOpen hang --- shell/browser/api/atom_api_web_contents.cc | 19 +++++++++++++++++++ shell/browser/api/atom_api_web_contents.h | 13 +++++++++++++ 2 files changed, 32 insertions(+) diff --git a/shell/browser/api/atom_api_web_contents.cc b/shell/browser/api/atom_api_web_contents.cc index c33747612cd2b..2e792dc4a47ed 100644 --- a/shell/browser/api/atom_api_web_contents.cc +++ b/shell/browser/api/atom_api_web_contents.cc @@ -598,6 +598,25 @@ void WebContents::WebContentsCreated(content::WebContents* source_contents, tracker->frame_name = frame_name; } +bool WebContents::ShouldCreateWebContents( + content::WebContents* web_contents, + content::RenderFrameHost* opener, + content::SiteInstance* source_site_instance, + int32_t route_id, + int32_t main_frame_route_id, + int32_t main_frame_widget_route_id, + content::mojom::WindowContainerType window_container_type, + const GURL& opener_url, + const std::string& frame_name, + const GURL& target_url, + const std::string& partition_id, + content::SessionStorageNamespace* session_storage_namespace) { + if (Emit("-will-add-new-contents", target_url, frame_name)) { + return false; + } + return true; +} + void WebContents::AddNewContents( content::WebContents* source, std::unique_ptr new_contents, diff --git a/shell/browser/api/atom_api_web_contents.h b/shell/browser/api/atom_api_web_contents.h index 9de5b327e0760..3047549a60337 100644 --- a/shell/browser/api/atom_api_web_contents.h +++ b/shell/browser/api/atom_api_web_contents.h @@ -346,6 +346,19 @@ class WebContents : public mate::TrackableObject, const base::string16& message, int32_t line_no, const base::string16& source_id) override; + bool ShouldCreateWebContents( + content::WebContents* web_contents, + content::RenderFrameHost* opener, + content::SiteInstance* source_site_instance, + int32_t route_id, + int32_t main_frame_route_id, + int32_t main_frame_widget_route_id, + content::mojom::WindowContainerType window_container_type, + 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, From bf6762d5db67da43f29d6fa3b29b1d838b24f984 Mon Sep 17 00:00:00 2001 From: Andy Locascio Date: Thu, 12 Dec 2019 15:30:16 -0800 Subject: [PATCH 2/3] add test --- spec-main/api-web-contents-spec.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/spec-main/api-web-contents-spec.ts b/spec-main/api-web-contents-spec.ts index e7d90c7077af7..606075408ded7 100644 --- a/spec-main/api-web-contents-spec.ts +++ b/spec-main/api-web-contents-spec.ts @@ -333,4 +333,25 @@ 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) + }) }) From 525ce7926585df7a5dc7895bcf9e426317e4c31d Mon Sep 17 00:00:00 2001 From: Andy Locascio Date: Thu, 12 Dec 2019 21:22:14 -0800 Subject: [PATCH 3/3] test: ensure window doesn't leak into other test --- spec-main/api-web-contents-spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/spec-main/api-web-contents-spec.ts b/spec-main/api-web-contents-spec.ts index 606075408ded7..6574b5331420c 100644 --- a/spec-main/api-web-contents-spec.ts +++ b/spec-main/api-web-contents-spec.ts @@ -353,5 +353,6 @@ describe('webContents module', () => { await w.webContents.executeJavaScript(`window.open('about:blank')`) await new Promise((resolve) => { process.nextTick(resolve) }) expect(wasCalled).to.equal(false) + await closeAllWindows() }) })