Skip to content

Commit

Permalink
update to use new webcontents delegate methods
Browse files Browse the repository at this point in the history
  • Loading branch information
loc committed Mar 24, 2020
1 parent e6a8fa2 commit 789a927
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
25 changes: 15 additions & 10 deletions shell/browser/api/electron_api_web_contents.cc
Expand Up @@ -662,23 +662,28 @@ void WebContents::WebContentsCreated(content::WebContents* source_contents,
tracker->frame_name = frame_name;
}

bool WebContents::ShouldCreateWebContents(
content::WebContents* web_contents,
content::RenderFrameHost* opener,
bool WebContents::IsWebContentsCreationOverridden(
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) {
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) {
if (Emit("-will-add-new-contents", target_url, frame_name)) {
return false;
}
return true;
return nullptr;
}

void WebContents::AddNewContents(
Expand Down
14 changes: 8 additions & 6 deletions shell/browser/api/electron_api_web_contents.h
Expand Up @@ -383,16 +383,18 @@ class WebContents : public gin_helper::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,
bool IsWebContentsCreationOverridden(
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) 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;
Expand Down
44 changes: 22 additions & 22 deletions spec-main/api-web-contents-spec.ts
Expand Up @@ -1820,34 +1820,34 @@ describe('webContents module', () => {
it('cancels authentication when callback is called with no arguments', async () => {
const w = new BrowserWindow({ show: false });
w.webContents.on('login', (event, request, authInfo, cb) => {
event.preventDefault()
cb()
})
await w.loadURL(serverUrl)
const body = await w.webContents.executeJavaScript(`document.documentElement.textContent`)
expect(body).to.equal('401')
})
})
event.preventDefault();
cb();
});
await w.loadURL(serverUrl);
const body = await w.webContents.executeJavaScript('document.documentElement.textContent');
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
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()
})
})
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();
});
});

0 comments on commit 789a927

Please sign in to comment.