Browse Source

fix: workaround for hang when preventDefault-ing nativeWindowOpen (7-1-x) (#21497)

* fix: enable workaround for nativeWindowOpen hang

* add test

* test: ensure window doesn't leak into other test
loc 5 years ago
parent
commit
1edfffae25

+ 19 - 0
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<content::WebContents> new_contents,

+ 13 - 0
shell/browser/api/atom_api_web_contents.h

@@ -346,6 +346,19 @@ class WebContents : public mate::TrackableObject<WebContents>,
                               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,

+ 22 - 0
spec-main/api-web-contents-spec.ts

@@ -333,4 +333,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()
+  })
 })