From 44b8343585ff321bd6f5d88ac82ba55d5d40736d Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 13 Jan 2016 14:47:39 +0800 Subject: [PATCH] Fix crash when closing page with webview --- atom/browser/api/atom_api_web_contents.cc | 20 ++++++++++++-------- atom/browser/lib/guest-view-manager.js | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index c4de932..77e445e 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -280,15 +280,11 @@ WebContents::WebContents(v8::Isolate* isolate, } WebContents::~WebContents() { - if (type_ == WEB_VIEW && managed_web_contents()) { - // When force destroying the "destroyed" event is not emitted. + // The webview's lifetime is completely controlled by GuestViewManager, so + // it is always destroyed by calling webview.destroy(), we need to make + // sure the "destroyed" event is emitted manually. + if (type_ == WEB_VIEW && managed_web_contents()) WebContentsDestroyed(); - - guest_delegate_->Destroy(); - - Observe(nullptr); - DestroyWebContents(); - } } bool WebContents::AddMessageToConsole(content::WebContents* source, @@ -624,7 +620,15 @@ bool WebContents::OnMessageReceived(const IPC::Message& message) { void WebContents::WebContentsDestroyed() { // The RenderViewDeleted was not called when the WebContents is destroyed. RenderViewDeleted(web_contents()->GetRenderViewHost()); + + // This event is only for internal use, which is emitted when WebContents is + // being destroyed. + Emit("will-destroy"); + + // Cleanup relationships with other parts. RemoveFromWeakMap(); + if (type_ == WEB_VIEW) + guest_delegate_->Destroy(); // We can not call Destroy here because we need to call Emit first, but we // also do not want any method to be used, so just mark as destroyed here. diff --git a/atom/browser/lib/guest-view-manager.js b/atom/browser/lib/guest-view-manager.js index a1d8248..83f1f6a 100644 --- a/atom/browser/lib/guest-view-manager.js +++ b/atom/browser/lib/guest-view-manager.js @@ -52,7 +52,7 @@ createGuest = function(embedder, params) { }; /* Destroy guest when the embedder is gone or navigated. */ - destroyEvents = ['destroyed', 'crashed', 'did-navigate']; + destroyEvents = ['will-destroy', 'crashed', 'did-navigate']; destroy = function() { if (guestInstances[id] != null) { return destroyGuest(embedder, id); -- 2.7.4