Correctly handle window's lifetime when it's closed.
authorCheng Zhao <zcbenz@gmail.com>
Wed, 1 May 2013 15:28:01 +0000 (23:28 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 1 May 2013 15:28:01 +0000 (23:28 +0800)
browser/api/atom_api_window.cc
browser/api/atom_api_window.h
browser/native_window.cc
browser/native_window.h
browser/native_window_mac.h
browser/native_window_mac.mm
vendor/brightray

index 079648b..65343e6 100644 (file)
@@ -44,13 +44,24 @@ Window::Window(v8::Handle<v8::Object> wrapper, base::DictionaryValue* options)
 }
 
 Window::~Window() {
+  window_->RemoveObserver(this);
 }
 
 void Window::OnPageTitleUpdated(bool* prevent_default,
                                 const std::string& title) {
-  scoped_ptr<base::ListValue> 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
index 25f16b3..f25b07d 100644 (file)
@@ -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<v8::Value> New(const v8::Arguments &args);
index e6593d9..9569eb7 100644 (file)
@@ -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());
index c2ebf49..e3c2af1 100644 (file)
@@ -157,8 +157,6 @@ class NativeWindow : public content::WebContentsDelegate,
 
   scoped_ptr<brightray::InspectableWebContents> inspectable_web_contents_;
 
-  bool window_going_to_destroy_;
-
   DISALLOW_COPY_AND_ASSIGN(NativeWindow);
 };
 
index 99f7c29..5f58380 100644 (file)
@@ -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);
index ea3a640..48d324f 100644 (file)
@@ -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];
 }
 
index 65af413..a519990 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 65af4131fc8d5b3450f89950dddc08be8a720860
+Subproject commit a51999007ff325562a7c5a73a76c198a080b37a8