Skip to content

Commit 5154e8f

Browse files
authored
fix: enable workaround for nativeWindowOpen hang (electron#22825)
1 parent 21c8395 commit 5154e8f

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

shell/browser/api/electron_api_web_contents.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,30 @@ void WebContents::WebContentsCreatedWithFullParams(
663663
tracker->body = params.body;
664664
}
665665

666+
bool WebContents::IsWebContentsCreationOverridden(
667+
content::SiteInstance* source_site_instance,
668+
content::mojom::WindowContainerType window_container_type,
669+
const GURL& opener_url,
670+
const std::string& frame_name,
671+
const GURL& target_url) {
672+
if (Emit("-will-add-new-contents", target_url, frame_name)) {
673+
return true;
674+
}
675+
return false;
676+
}
677+
678+
content::WebContents* WebContents::CreateCustomWebContents(
679+
content::RenderFrameHost* opener,
680+
content::SiteInstance* source_site_instance,
681+
bool is_new_browsing_instance,
682+
const GURL& opener_url,
683+
const std::string& frame_name,
684+
const GURL& target_url,
685+
const std::string& partition_id,
686+
content::SessionStorageNamespace* session_storage_namespace) {
687+
return nullptr;
688+
}
689+
666690
void WebContents::AddNewContents(
667691
content::WebContents* source,
668692
std::unique_ptr<content::WebContents> new_contents,

shell/browser/api/electron_api_web_contents.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,21 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
384384
const base::string16& message,
385385
int32_t line_no,
386386
const base::string16& source_id) override;
387+
bool IsWebContentsCreationOverridden(
388+
content::SiteInstance* source_site_instance,
389+
content::mojom::WindowContainerType window_container_type,
390+
const GURL& opener_url,
391+
const std::string& frame_name,
392+
const GURL& target_url) override;
393+
content::WebContents* CreateCustomWebContents(
394+
content::RenderFrameHost* opener,
395+
content::SiteInstance* source_site_instance,
396+
bool is_new_browsing_instance,
397+
const GURL& opener_url,
398+
const std::string& frame_name,
399+
const GURL& target_url,
400+
const std::string& partition_id,
401+
content::SessionStorageNamespace* session_storage_namespace) override;
387402
void WebContentsCreatedWithFullParams(
388403
content::WebContents* source_contents,
389404
int opener_render_process_id,

spec-main/api-web-contents-spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,4 +1829,26 @@ describe('webContents module', () => {
18291829
expect(body).to.equal('401');
18301830
});
18311831
});
1832+
1833+
it('emits a cancelable event before creating a child webcontents', async () => {
1834+
const w = new BrowserWindow({
1835+
show: false,
1836+
webPreferences: {
1837+
sandbox: true
1838+
}
1839+
});
1840+
w.webContents.on('-will-add-new-contents' as any, (event: any, url: any) => {
1841+
expect(url).to.equal('about:blank');
1842+
event.preventDefault();
1843+
});
1844+
let wasCalled = false;
1845+
w.webContents.on('new-window' as any, () => {
1846+
wasCalled = true;
1847+
});
1848+
await w.loadURL('about:blank');
1849+
await w.webContents.executeJavaScript(`window.open('about:blank')`);
1850+
await new Promise((resolve) => { process.nextTick(resolve); });
1851+
expect(wasCalled).to.equal(false);
1852+
await closeAllWindows();
1853+
});
18321854
});

0 commit comments

Comments
 (0)