From afe07521c591e622fb1e7bd8e212e58412db2570 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 1 May 2013 23:28:01 +0800 Subject: [PATCH] Correctly handle window's lifetime when it's closed. --- browser/api/atom_api_window.cc | 17 ++++++++++++++--- browser/api/atom_api_window.h | 2 ++ browser/native_window.cc | 2 +- browser/native_window.h | 2 -- browser/native_window_mac.h | 2 +- browser/native_window_mac.mm | 4 ++++ vendor/brightray | 2 +- 7 files changed, 23 insertions(+), 8 deletions(-) diff --git a/browser/api/atom_api_window.cc b/browser/api/atom_api_window.cc index 079648bcb345..65343e62b3d0 100644 --- a/browser/api/atom_api_window.cc +++ b/browser/api/atom_api_window.cc @@ -44,13 +44,24 @@ Window::Window(v8::Handle wrapper, base::DictionaryValue* options) } Window::~Window() { + window_->RemoveObserver(this); } void Window::OnPageTitleUpdated(bool* prevent_default, const std::string& title) { - scoped_ptr args(new base::ListValue); - args->AppendString(title); - *prevent_default = Emit("page-title-updated", args.get()); + base::ListValue args; + args.AppendString(title); + *prevent_default = Emit("page-title-updated", &args); +} + +void Window::WillCloseWindow(bool* prevent_default) { + base::ListValue args; + *prevent_default = Emit("close", &args); +} + +void Window::OnWindowClosed() { + // Free memory immediately when window is closed. + delete this; } // static diff --git a/browser/api/atom_api_window.h b/browser/api/atom_api_window.h index 25f16b358f03..f25b07df77f2 100644 --- a/browser/api/atom_api_window.h +++ b/browser/api/atom_api_window.h @@ -35,6 +35,8 @@ class Window : public EventEmitter, // Implementations of NativeWindowObserver. virtual void OnPageTitleUpdated(bool* prevent_default, const std::string& title) OVERRIDE; + virtual void WillCloseWindow(bool* prevent_default) OVERRIDE; + virtual void OnWindowClosed() OVERRIDE; private: static v8::Handle New(const v8::Arguments &args); diff --git a/browser/native_window.cc b/browser/native_window.cc index e6593d9f0e71..9569eb7a2146 100644 --- a/browser/native_window.cc +++ b/browser/native_window.cc @@ -183,7 +183,7 @@ void NativeWindow::CloseContents(content::WebContents* source) { // memory will not be freed until you call delete. // In this way, it would be safe to manage windows via smart pointers. If you // want to free memory when the window is closed, you can do deleting by - // overriding WillCloseWindow method in the observer. + // overriding the WillCloseWindow method in the observer. CloseImmediately(); FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowClosed()); diff --git a/browser/native_window.h b/browser/native_window.h index c2ebf495f7fa..e3c2af1ecf82 100644 --- a/browser/native_window.h +++ b/browser/native_window.h @@ -157,8 +157,6 @@ class NativeWindow : public content::WebContentsDelegate, scoped_ptr inspectable_web_contents_; - bool window_going_to_destroy_; - DISALLOW_COPY_AND_ASSIGN(NativeWindow); }; diff --git a/browser/native_window_mac.h b/browser/native_window_mac.h index 99f7c29cc43f..5f583802b915 100644 --- a/browser/native_window_mac.h +++ b/browser/native_window_mac.h @@ -50,7 +50,7 @@ class NativeWindowMac : public NativeWindow { virtual void SetKiosk(bool kiosk) OVERRIDE; virtual bool IsKiosk() OVERRIDE; - NSWindow* window() const { return window_; } + NSWindow*& window() { return window_; } protected: void SetNonLionFullscreen(bool fullscreen); diff --git a/browser/native_window_mac.mm b/browser/native_window_mac.mm index ea3a6408823d..48d324fb4edf 100644 --- a/browser/native_window_mac.mm +++ b/browser/native_window_mac.mm @@ -37,6 +37,7 @@ } - (void)windowWillClose:(NSNotification *)notification { + shell_->window() = nil; [self autorelease]; } @@ -114,6 +115,8 @@ NativeWindowMac::NativeWindowMac(content::WebContents* web_contents, } NativeWindowMac::~NativeWindowMac() { + if (window()) + [window() release]; } void NativeWindowMac::Close() { @@ -121,6 +124,7 @@ void NativeWindowMac::Close() { } void NativeWindowMac::CloseImmediately() { + [window() orderOut:nil]; [window() close]; } diff --git a/vendor/brightray b/vendor/brightray index 65af4131fc8d..a51999007ff3 160000 --- a/vendor/brightray +++ b/vendor/brightray @@ -1 +1 @@ -Subproject commit 65af4131fc8d5b3450f89950dddc08be8a720860 +Subproject commit a51999007ff325562a7c5a73a76c198a080b37a8