Add window closing related methods for window observer.
authorCheng Zhao <zcbenz@gmail.com>
Wed, 1 May 2013 08:33:19 +0000 (16:33 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 1 May 2013 08:33:19 +0000 (16:33 +0800)
browser/native_window.cc
browser/native_window_mac.mm
browser/native_window_observer.h

index 99f5b53..e6593d9 100644 (file)
@@ -134,6 +134,13 @@ void NativeWindow::CloseDevTools() {
 }
 
 void NativeWindow::CloseWebContents() {
+  bool prevent_default = false;
+  FOR_EACH_OBSERVER(NativeWindowObserver,
+                    observers_,
+                    WillCloseWindow(&prevent_default));
+  if (prevent_default)
+    return;
+
   content::WebContents* web_contents(GetWebContents());
 
   if (web_contents->NeedToFireBeforeUnload())
@@ -174,8 +181,12 @@ content::JavaScriptDialogManager* NativeWindow::GetJavaScriptDialogManager() {
 void NativeWindow::CloseContents(content::WebContents* source) {
   // When the web contents is gone, close the window immediately, but the
   // memory will not be freed until you call delete.
-  // In this way, it would be safe to manage windows via smart pointers.
+  // 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.
   CloseImmediately();
+
+  FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowClosed());
 }
 
 bool NativeWindow::OnMessageReceived(const IPC::Message& message) {
index 0d490a6..ea3a640 100644 (file)
@@ -99,7 +99,6 @@ NativeWindowMac::NativeWindowMac(content::WebContents* web_contents,
   [atom_window setShell:this];
 
   window_ = atom_window;
-  [window() setReleasedWhenClosed:NO];
   [window() setDelegate:[[AtomNSWindowDelegate alloc] initWithShell:this]];
 
   // Disable fullscreen button when 'fullscreen' is specified to false.
@@ -115,7 +114,6 @@ NativeWindowMac::NativeWindowMac(content::WebContents* web_contents,
 }
 
 NativeWindowMac::~NativeWindowMac() {
-  [window() release];
 }
 
 void NativeWindowMac::Close() {
index 335fd30..f21ca0d 100644 (file)
@@ -15,7 +15,13 @@ class NativeWindowObserver {
 
   // Called when the web page of the window has updated it's document title.
   virtual void OnPageTitleUpdated(bool* prevent_default,
-                                  const std::string& title) = 0;
+                                  const std::string& title) {}
+
+  // Called when the window is gonna closed.
+  virtual void WillCloseWindow(bool* prevent_default) {}
+
+  // Called when the window is closed.
+  virtual void OnWindowClosed() {}
 };
 
 }  // namespace atom