feat: add webContents.close() (#35509)
* feat: add webContents.close() * update docs, add test for beforeunload override * Update web-contents.md
This commit is contained in:
parent
994834d25a
commit
eebf34cc6c
5 changed files with 110 additions and 1 deletions
|
@ -134,6 +134,7 @@ BrowserWindow::~BrowserWindow() {
|
|||
api_web_contents_->RemoveObserver(this);
|
||||
// Destroy the WebContents.
|
||||
OnCloseContents();
|
||||
api_web_contents_->Destroy();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -181,7 +182,6 @@ void BrowserWindow::WebContentsDestroyed() {
|
|||
|
||||
void BrowserWindow::OnCloseContents() {
|
||||
BaseWindow::ResetBrowserViews();
|
||||
api_web_contents_->Destroy();
|
||||
}
|
||||
|
||||
void BrowserWindow::OnRendererResponsive(content::RenderProcessHost*) {
|
||||
|
|
|
@ -1009,6 +1009,19 @@ void WebContents::Destroy() {
|
|||
}
|
||||
}
|
||||
|
||||
void WebContents::Close(absl::optional<gin_helper::Dictionary> options) {
|
||||
bool dispatch_beforeunload = false;
|
||||
if (options)
|
||||
options->Get("waitForBeforeUnload", &dispatch_beforeunload);
|
||||
if (dispatch_beforeunload &&
|
||||
web_contents()->NeedToFireBeforeUnloadOrUnloadEvents()) {
|
||||
NotifyUserActivation();
|
||||
web_contents()->DispatchBeforeUnload(false /* auto_cancel */);
|
||||
} else {
|
||||
web_contents()->Close();
|
||||
}
|
||||
}
|
||||
|
||||
bool WebContents::DidAddMessageToConsole(
|
||||
content::WebContents* source,
|
||||
blink::mojom::ConsoleMessageLevel level,
|
||||
|
@ -1199,6 +1212,8 @@ void WebContents::CloseContents(content::WebContents* source) {
|
|||
|
||||
for (ExtendedWebContentsObserver& observer : observers_)
|
||||
observer.OnCloseContents();
|
||||
|
||||
Destroy();
|
||||
}
|
||||
|
||||
void WebContents::ActivateContents(content::WebContents* source) {
|
||||
|
@ -3921,6 +3936,7 @@ v8::Local<v8::ObjectTemplate> WebContents::FillObjectTemplate(
|
|||
// destroyable.
|
||||
return gin_helper::ObjectTemplateBuilder(isolate, templ)
|
||||
.SetMethod("destroy", &WebContents::Destroy)
|
||||
.SetMethod("close", &WebContents::Close)
|
||||
.SetMethod("getBackgroundThrottling",
|
||||
&WebContents::GetBackgroundThrottling)
|
||||
.SetMethod("setBackgroundThrottling",
|
||||
|
|
|
@ -152,6 +152,7 @@ class WebContents : public ExclusiveAccessContext,
|
|||
const char* GetTypeName() override;
|
||||
|
||||
void Destroy();
|
||||
void Close(absl::optional<gin_helper::Dictionary> options);
|
||||
base::WeakPtr<WebContents> GetWeakPtr() { return weak_factory_.GetWeakPtr(); }
|
||||
|
||||
bool GetBackgroundThrottling() const;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue