| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_ | 5 #ifndef CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_ |
| 6 #define CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_ | 6 #define CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" | 12 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" |
| 12 #include "content/public/browser/notification_observer.h" | 13 #include "content/public/browser/notification_observer.h" |
| 13 #include "content/public/browser/notification_registrar.h" | 14 #include "content/public/browser/notification_registrar.h" |
| 14 | 15 |
| 15 class Browser; | 16 class Browser; |
| 16 class TabContents; | 17 class TabContents; |
| 17 class TabStripModel; | 18 class TabStripModel; |
| 18 | 19 |
| 19 namespace content { | 20 namespace content { |
| 20 class NotificationSource; | 21 class NotificationSource; |
| 21 class NotifictaionDetails; | 22 class NotifictaionDetails; |
| 22 class WebContents; | 23 class WebContents; |
| 23 } | 24 } |
| 24 | 25 |
| 25 namespace chrome { | 26 namespace chrome { |
| 27 class UnloadDetachedHandler; |
| 26 | 28 |
| 29 // UnloadController manages closing tabs and windows -- especially in |
| 30 // regards to beforeunload handlers (proceed/cancel dialogs) and |
| 31 // unload handlers (no user interaction). |
| 32 // |
| 33 // Typical flow of closing a tab: |
| 34 // 1. Browser calls |CanCloseContents()|. |
| 35 // If true, browser calls contents::CloseWebContents(). |
| 36 // 2. WebContents notifies us via its delegate and BeforeUnloadFired() |
| 37 // that the beforeunload handler was run. If the user allowed the |
| 38 // close to continue, we hand-off running the unload handler to |
| 39 // UnloadDetachedHandler which decides if the tab can be unloaded |
| 40 // in the background. If it can, the tab is removed from the tab |
| 41 // strip at this point. |
| 42 // |
| 43 // Typical flow of closing a window: |
| 44 // 1. Browser calls |ShouldCloseWindow()|. |
| 45 // If beforeunload/unload handlers need to run, |UnloadController| returns |
| 46 // false and calls |ProcessPendingTabs()| (private method). |
| 47 // 2. For each tab with a beforeunload/unload handler, |ProcessPendingTabs()| |
| 48 // calls |web_contents->OnCloseStarted()| |
| 49 // and |web_contents->GetRenderViewHost()->FirePageBeforeUnload()|. |
| 50 // 3. |UnloadController| waits for any tabs from the "closing a tab" flow above |
| 51 // to finish detached unloads. |
| 52 // 4. For each tab with a beforeunload/unload handler, |ProcessPendingTabs()| |
| 53 // calls |web_contents->GetRenderViewHost()->ClosePage()|. |
| 54 // 5. |ProcessPendingTabs()| calls |browser_->OnWindowClosing()|. |
| 55 // |
| 27 class UnloadController : public content::NotificationObserver, | 56 class UnloadController : public content::NotificationObserver, |
| 28 public TabStripModelObserver { | 57 public TabStripModelObserver { |
| 29 public: | 58 public: |
| 30 explicit UnloadController(Browser* browser); | 59 explicit UnloadController(Browser* browser); |
| 31 virtual ~UnloadController(); | 60 virtual ~UnloadController(); |
| 32 | 61 |
| 33 // Returns true if |contents| can be cleanly closed. When |browser_| is being | 62 // Returns true if |contents| can be cleanly closed. When |browser_| is being |
| 34 // closed, this function will return false to indicate |contents| should not | 63 // closed, this function will return false to indicate |contents| should not |
| 35 // be cleanly closed, since the fast shutdown path will just kill its | 64 // be cleanly closed, since the fast shutdown path will just kill its |
| 36 // renderer. | 65 // renderer. |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 // | 136 // |
| 108 // Typically you'll want to pass in true for |process_now|. Passing in true | 137 // Typically you'll want to pass in true for |process_now|. Passing in true |
| 109 // may result in deleting |tab|. If you know that shouldn't happen (because of | 138 // may result in deleting |tab|. If you know that shouldn't happen (because of |
| 110 // the state of the stack), pass in false. | 139 // the state of the stack), pass in false. |
| 111 void ClearUnloadState(content::WebContents* web_contents, bool process_now); | 140 void ClearUnloadState(content::WebContents* web_contents, bool process_now); |
| 112 | 141 |
| 113 Browser* browser_; | 142 Browser* browser_; |
| 114 | 143 |
| 115 content::NotificationRegistrar registrar_; | 144 content::NotificationRegistrar registrar_; |
| 116 | 145 |
| 117 // Tracks tabs that need there beforeunload event fired before we can | 146 // Tracks tabs that need their beforeunload event fired before we can |
| 118 // close the browser. Only gets populated when we try to close the browser. | 147 // close the browser. Only gets populated when we try to close the browser. |
| 119 UnloadListenerSet tabs_needing_before_unload_fired_; | 148 UnloadListenerSet tabs_needing_before_unload_fired_; |
| 120 | 149 |
| 121 // Tracks tabs that need there unload event fired before we can | 150 // Tracks tabs that need their unload event fired before we can |
| 122 // close the browser. Only gets populated when we try to close the browser. | 151 // close the browser. Only gets populated when we try to close the browser. |
| 123 UnloadListenerSet tabs_needing_unload_fired_; | 152 UnloadListenerSet tabs_needing_unload_fired_; |
| 124 | 153 |
| 125 // Whether we are processing the beforeunload and unload events of each tab | 154 // Whether we are processing the beforeunload and unload events of each tab |
| 126 // in preparation for closing the browser. UnloadController owns this state | 155 // in preparation for closing the browser. UnloadController owns this state |
| 127 // rather than Browser because unload handlers are the only reason that a | 156 // rather than Browser because unload handlers are the only reason that a |
| 128 // Browser window isn't just immediately closed. | 157 // Browser window isn't just immediately closed. |
| 129 bool is_attempting_to_close_browser_; | 158 bool is_attempting_to_close_browser_; |
| 130 | 159 |
| 160 // Allow unload handlers to run without holding up the UI. |
| 161 scoped_ptr<UnloadDetachedHandler> unload_detached_handler_; |
| 162 |
| 131 base::WeakPtrFactory<UnloadController> weak_factory_; | 163 base::WeakPtrFactory<UnloadController> weak_factory_; |
| 132 | 164 |
| 133 DISALLOW_COPY_AND_ASSIGN(UnloadController); | 165 DISALLOW_COPY_AND_ASSIGN(UnloadController); |
| 134 }; | 166 }; |
| 135 | 167 |
| 136 } // namespace chrome | 168 } // namespace chrome |
| 137 | 169 |
| 138 #endif // CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_ | 170 #endif // CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_ |
| OLD | NEW |