Fix a crash when destroying window.
authorCheng Zhao <zcbenz@gmail.com>
Fri, 11 Apr 2014 10:43:01 +0000 (18:43 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 11 Apr 2014 10:43:01 +0000 (18:43 +0800)
atom/browser/native_window.cc
atom/browser/native_window.h
atom/browser/native_window_mac.mm

index 95fa029..088f142 100644 (file)
@@ -57,12 +57,12 @@ NativeWindow::NativeWindow(content::WebContents* web_contents,
                            base::DictionaryValue* options)
     : content::WebContentsObserver(web_contents),
       has_frame_(true),
-      inspectable_web_contents_(
-          brightray::InspectableWebContents::Create(web_contents)),
       is_closed_(false),
       node_integration_("except-iframe"),
       has_dialog_attached_(false),
-      weak_factory_(this) {
+      weak_factory_(this),
+      inspectable_web_contents_(
+          brightray::InspectableWebContents::Create(web_contents)) {
   options->GetBoolean(switches::kFrame, &has_frame_);
 
 #if defined(OS_MACOSX)
@@ -365,6 +365,10 @@ void NativeWindow::NotifyWindowBlur() {
   FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowBlur());
 }
 
+void NativeWindow::DestroyWebContents() {
+  inspectable_web_contents_.reset();
+}
+
 // In atom-shell all reloads and navigations started by renderer process would
 // be redirected to this method, so we can have precise control of how we
 // would open the url (in our case, is to restart the renderer process). See
index 17b06b4..47ce314 100644 (file)
@@ -196,6 +196,9 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
   void NotifyWindowClosed();
   void NotifyWindowBlur();
 
+  // Destroy the inspectable_web_contents.
+  void DestroyWebContents();
+
   // Called when the window needs to update its draggable region.
   virtual void UpdateDraggableRegions(
       const std::vector<DraggableRegion>& regions) = 0;
@@ -251,8 +254,6 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
   // Window icon.
   gfx::Image icon_;
 
-  scoped_ptr<brightray::InspectableWebContents> inspectable_web_contents_;
-
  private:
   // Schedule a notification unresponsive event.
   void ScheduleUnresponsiveEvent(int ms);
@@ -307,6 +308,11 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
 
   scoped_ptr<AtomJavaScriptDialogManager> dialog_manager_;
 
+  // Notice that inspectable_web_contents_ must be placed after dialog_manager_,
+  // so we can make sure inspectable_web_contents_ is destroyed before
+  // dialog_manager_, otherwise a crash would happen.
+  scoped_ptr<brightray::InspectableWebContents> inspectable_web_contents_;
+
   // Maps url to file path, used by the file requests sent from devtools.
   typedef std::map<std::string, base::FilePath> PathsMap;
   PathsMap saved_files_;
index d0ea3ef..673da67 100644 (file)
@@ -201,7 +201,7 @@ NativeWindowMac::NativeWindowMac(content::WebContents* web_contents,
 NativeWindowMac::~NativeWindowMac() {
   // Force InspectableWebContents to be destroyed before we destroy window,
   // because it may still be observing the window at this time.
-  inspectable_web_contents_.reset();
+  DestroyWebContents();
 }
 
 void NativeWindowMac::Close() {