From e7aab096e7f1acb7bb27dfef8914b7d0168dc6b1 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 2 May 2013 20:08:23 +0800 Subject: [PATCH] Make sure WindowClosed message is sent when window is destroyed. --- browser/native_window.cc | 19 ++++++++++++++++--- browser/native_window.h | 9 ++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/browser/native_window.cc b/browser/native_window.cc index 9569eb7a2146..13af679beeb1 100644 --- a/browser/native_window.cc +++ b/browser/native_window.cc @@ -38,6 +38,7 @@ std::vector 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) { diff --git a/browser/native_window.h b/browser/native_window.h index e3c2af1ecf82..da971a979b9a 100644 --- a/browser/native_window.h +++ b/browser/native_window.h @@ -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 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 windows_; - scoped_ptr dialog_manager_; + bool is_closed_; + scoped_ptr dialog_manager_; scoped_ptr inspectable_web_contents_; DISALLOW_COPY_AND_ASSIGN(NativeWindow);