Make sure WindowClosed message is sent when window is destroyed.
authorCheng Zhao <zcbenz@gmail.com>
Thu, 2 May 2013 12:08:23 +0000 (20:08 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 2 May 2013 12:08:23 +0000 (20:08 +0800)
browser/native_window.cc
browser/native_window.h

index 9569eb7..13af679 100644 (file)
@@ -38,6 +38,7 @@ std::vector<NativeWindow*> NativeWindow::windows_;
 NativeWindow::NativeWindow(content::WebContents* web_contents,
                            base::DictionaryValue* options)
     : content::WebContentsObserver(web_contents),
+      is_closed_(false),
       inspectable_web_contents_(
           brightray::InspectableWebContents::Create(web_contents)) {
   web_contents->SetDelegate(this);
@@ -50,8 +51,9 @@ NativeWindow::NativeWindow(content::WebContents* web_contents,
 }
 
 NativeWindow::~NativeWindow() {
-  windows_.erase(std::remove(windows_.begin(), windows_.end(), this),
-                 windows_.end());
+  // It's possible that the windows gets destroyed before it's closed, in that
+  // case we need to ensure the OnWindowClosed message is still notified.
+  NotifyWindowClosed();
 }
 
 // static
@@ -153,6 +155,17 @@ content::WebContents* NativeWindow::GetWebContents() const {
   return inspectable_web_contents_->GetWebContents();
 }
 
+void NativeWindow::NotifyWindowClosed() {
+  if (is_closed_)
+    return;
+
+  is_closed_ = true;
+  windows_.erase(std::remove(windows_.begin(), windows_.end(), this),
+                 windows_.end());
+
+  FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowClosed());
+}
+
 // Window opened by window.open.
 void NativeWindow::WebContentsCreated(
     content::WebContents* source_contents,
@@ -186,7 +199,7 @@ void NativeWindow::CloseContents(content::WebContents* source) {
   // overriding the WillCloseWindow method in the observer.
   CloseImmediately();
 
-  FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowClosed());
+  NotifyWindowClosed();
 }
 
 bool NativeWindow::OnMessageReceived(const IPC::Message& message) {
index e3c2af1..da971a9 100644 (file)
@@ -59,6 +59,9 @@ class NativeWindow : public content::WebContentsDelegate,
   static NativeWindow* FromProcessIDAndRoutingID(int process_id,
                                                  int routing_id);
 
+  // Return all opened windows.
+  static std::vector<NativeWindow*> windows() { return windows_; }
+
   void InitFromOptions(base::DictionaryValue* options);
 
   virtual void Close() = 0;
@@ -92,6 +95,7 @@ class NativeWindow : public content::WebContentsDelegate,
   virtual void SetKiosk(bool kiosk) = 0;
   virtual bool IsKiosk() = 0;
 
+  virtual bool IsClosed() const { return is_closed_; }
   virtual void ShowDevTools();
   virtual void CloseDevTools();
 
@@ -118,6 +122,8 @@ class NativeWindow : public content::WebContentsDelegate,
     return inspectable_web_contents_.get();
   }
 
+  void NotifyWindowClosed();
+
   // Implementations of content::WebContentsDelegate.
   virtual void WebContentsCreated(content::WebContents* source_contents,
                                   int64 source_frame_id,
@@ -153,8 +159,9 @@ class NativeWindow : public content::WebContentsDelegate,
   // Stores all windows.
   static std::vector<NativeWindow*> windows_;
 
-  scoped_ptr<AtomJavaScriptDialogManager> dialog_manager_;
+  bool is_closed_;
 
+  scoped_ptr<AtomJavaScriptDialogManager> dialog_manager_;
   scoped_ptr<brightray::InspectableWebContents> inspectable_web_contents_;
 
   DISALLOW_COPY_AND_ASSIGN(NativeWindow);