Browse Source

browser: make destruction of webContents async

deepak1556 8 years ago
parent
commit
f974a6bda9

+ 11 - 4
atom/browser/api/atom_api_web_contents.cc

@@ -417,15 +417,22 @@ WebContents::~WebContents() {
       guest_delegate_->Destroy();
 
     RenderViewDeleted(web_contents()->GetRenderViewHost());
-    DestroyWebContents();
+
+    if (type_ == BROWSER_WINDOW && owner_window()) {
+      owner_window()->CloseContents(nullptr);
+    } else if (type_ == WEB_VIEW) {
+      DestroyWebContents(false /* async */);
+    } else {
+      DestroyWebContents(true /* async */);
+    }
   }
 }
 
-void WebContents::DestroyWebContents() {
+void WebContents::DestroyWebContents(bool async) {
   // This event is only for internal use, which is emitted when WebContents is
   // being destroyed.
   Emit("will-destroy");
-  ResetManagedWebContents();
+  ResetManagedWebContents(async);
 }
 
 bool WebContents::DidAddMessageToConsole(content::WebContents* source,
@@ -477,7 +484,7 @@ void WebContents::AddNewContents(content::WebContents* source,
   if (Emit("-add-new-contents", api_web_contents, disposition, user_gesture,
       initial_rect.x(), initial_rect.y(), initial_rect.width(),
       initial_rect.height())) {
-    api_web_contents->DestroyWebContents();
+    api_web_contents->DestroyWebContents(false /* async */);
   }
 }
 

+ 1 - 1
atom/browser/api/atom_api_web_contents.h

@@ -78,7 +78,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
                              v8::Local<v8::FunctionTemplate> prototype);
 
   // Notifies to destroy any guest web contents before destroying self.
-  void DestroyWebContents();
+  void DestroyWebContents(bool async);
 
   int64_t GetID() const;
   int GetProcessID() const;

+ 1 - 1
atom/browser/api/atom_api_window.cc

@@ -173,7 +173,7 @@ void Window::WillDestroyNativeObject() {
 }
 
 void Window::OnWindowClosed() {
-  api_web_contents_->DestroyWebContents();
+  api_web_contents_->DestroyWebContents(true /* async */);
 
   RemoveFromWeakMap();
   window_->RemoveObserver(this);

+ 7 - 2
atom/browser/common_web_contents_delegate.cc

@@ -188,8 +188,13 @@ void CommonWebContentsDelegate::SetOwnerWindow(
   }
 }
 
-void CommonWebContentsDelegate::ResetManagedWebContents() {
-  web_contents_.reset();
+void CommonWebContentsDelegate::ResetManagedWebContents(bool async) {
+  if (async) {
+    base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE,
+                                                    web_contents_.release());
+  } else {
+    web_contents_.reset();
+  }
 }
 
 content::WebContents* CommonWebContentsDelegate::GetWebContents() const {

+ 1 - 1
atom/browser/common_web_contents_delegate.h

@@ -112,7 +112,7 @@ class CommonWebContentsDelegate
 #endif
 
   // Destroy the managed InspectableWebContents object.
-  void ResetManagedWebContents();
+  void ResetManagedWebContents(bool async);
 
  private:
   // Callback for when DevToolsSaveToFile has completed.