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. The tab is removed from the tab strip at |
| 40 // this point. |
| 41 // |
| 42 // Typical flow of closing a window: |
| 43 // 1. BrowserView::CanClose() calls TabsNeedBeforeUnloadFired(). |
| 44 // If beforeunload/unload handlers need to run, UnloadController returns |
| 45 // true and calls ProcessPendingTabs() (private method). |
| 46 // 2. For each tab with a beforeunload/unload handler, ProcessPendingTabs() |
| 47 // calls |web_contents->OnCloseStarted()| |
| 48 // and |web_contents->GetRenderViewHost()->FirePageBeforeUnload()|. |
| 49 // 3. If the user allowed the close to continue, we hand-off all the tabs with |
| 50 // unload handlers to UnloadDetachedHandler. All the tabs are removed |
| 51 // from the tab strip. |
| 52 // 4. The browser gets notified that the tab strip is empty and calls |
| 53 // CloseFrame where the empty tab strip causes the window to hide. |
| 54 // Once the detached tabs finish, the browser calls CloseFrame again and |
| 55 // the window is finally closed. |
| 56 // |
27 class UnloadController : public content::NotificationObserver, | 57 class UnloadController : public content::NotificationObserver, |
28 public TabStripModelObserver { | 58 public TabStripModelObserver { |
29 public: | 59 public: |
30 explicit UnloadController(Browser* browser); | 60 explicit UnloadController(Browser* browser); |
31 virtual ~UnloadController(); | 61 virtual ~UnloadController(); |
32 | 62 |
33 // Returns true if |contents| can be cleanly closed. When |browser_| is being | 63 // Returns true if |contents| can be cleanly closed. When |browser_| is being |
34 // closed, this function will return false to indicate |contents| should not | 64 // closed, this function will return false to indicate |contents| should not |
35 // be cleanly closed, since the fast shutdown path will just kill its | 65 // be cleanly closed, since the fast shutdown path will just kill its |
36 // renderer. | 66 // renderer. |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 // | 138 // |
109 // Typically you'll want to pass in true for |process_now|. Passing in true | 139 // Typically you'll want to pass in true for |process_now|. Passing in true |
110 // may result in deleting |tab|. If you know that shouldn't happen (because of | 140 // may result in deleting |tab|. If you know that shouldn't happen (because of |
111 // the state of the stack), pass in false. | 141 // the state of the stack), pass in false. |
112 void ClearUnloadState(content::WebContents* web_contents, bool process_now); | 142 void ClearUnloadState(content::WebContents* web_contents, bool process_now); |
113 | 143 |
114 Browser* browser_; | 144 Browser* browser_; |
115 | 145 |
116 content::NotificationRegistrar registrar_; | 146 content::NotificationRegistrar registrar_; |
117 | 147 |
118 // Tracks tabs that need there beforeunload event fired before we can | 148 // Tracks tabs that need their beforeunload event fired before we can |
119 // close the browser. Only gets populated when we try to close the browser. | 149 // close the browser. Only gets populated when we try to close the browser. |
120 UnloadListenerSet tabs_needing_before_unload_fired_; | 150 UnloadListenerSet tabs_needing_before_unload_fired_; |
121 | 151 |
122 // Tracks tabs that need there unload event fired before we can | 152 // Tracks tabs that need their unload event fired before we can |
123 // close the browser. Only gets populated when we try to close the browser. | 153 // close the browser. Only gets populated when we try to close the browser. |
124 UnloadListenerSet tabs_needing_unload_fired_; | 154 UnloadListenerSet tabs_needing_unload_fired_; |
125 | 155 |
126 // Whether we are processing the beforeunload and unload events of each tab | 156 // Whether we are processing the beforeunload and unload events of each tab |
127 // in preparation for closing the browser. UnloadController owns this state | 157 // in preparation for closing the browser. UnloadController owns this state |
128 // rather than Browser because unload handlers are the only reason that a | 158 // rather than Browser because unload handlers are the only reason that a |
129 // Browser window isn't just immediately closed. | 159 // Browser window isn't just immediately closed. |
130 bool is_attempting_to_close_browser_; | 160 bool is_attempting_to_close_browser_; |
131 | 161 |
| 162 // Allow unload handlers to run without holding up the UI. |
| 163 scoped_ptr<UnloadDetachedHandler> unload_detached_handler_; |
| 164 |
132 base::WeakPtrFactory<UnloadController> weak_factory_; | 165 base::WeakPtrFactory<UnloadController> weak_factory_; |
133 | 166 |
134 DISALLOW_COPY_AND_ASSIGN(UnloadController); | 167 DISALLOW_COPY_AND_ASSIGN(UnloadController); |
135 }; | 168 }; |
136 | 169 |
137 } // namespace chrome | 170 } // namespace chrome |
138 | 171 |
139 #endif // CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_ | 172 #endif // CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_ |
OLD | NEW |