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 #include "chrome/browser/ui/unload_controller.h" | 5 #include "chrome/browser/ui/unload_controller.h" |
6 | 6 |
| 7 #include "base/callback.h" |
7 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
8 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
9 #include "chrome/browser/ui/browser_tabstrip.h" | 10 #include "chrome/browser/ui/browser_tabstrip.h" |
10 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 11 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 13 #include "chrome/browser/ui/unload_detached_handler.h" |
12 #include "chrome/common/chrome_notification_types.h" | 14 #include "chrome/common/chrome_notification_types.h" |
13 #include "content/public/browser/notification_service.h" | 15 #include "content/public/browser/notification_service.h" |
14 #include "content/public/browser/notification_source.h" | 16 #include "content/public/browser/notification_source.h" |
15 #include "content/public/browser/notification_types.h" | 17 #include "content/public/browser/notification_types.h" |
16 #include "content/public/browser/render_view_host.h" | 18 #include "content/public/browser/render_view_host.h" |
17 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
18 | 20 |
19 namespace chrome { | 21 namespace chrome { |
20 | 22 |
21 //////////////////////////////////////////////////////////////////////////////// | 23 //////////////////////////////////////////////////////////////////////////////// |
22 // UnloadController, public: | 24 // UnloadController, public: |
23 | 25 |
24 UnloadController::UnloadController(Browser* browser) | 26 UnloadController::UnloadController(Browser* browser) |
25 : browser_(browser), | 27 : browser_(browser), |
26 is_attempting_to_close_browser_(false), | 28 is_attempting_to_close_browser_(false), |
| 29 unload_detached_handler_(new UnloadDetachedHandler( |
| 30 base::Bind(&UnloadController::ProcessPendingTabs, |
| 31 base::Unretained(this)))), |
27 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 32 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
28 browser_->tab_strip_model()->AddObserver(this); | 33 browser_->tab_strip_model()->AddObserver(this); |
29 } | 34 } |
30 | 35 |
31 UnloadController::~UnloadController() { | 36 UnloadController::~UnloadController() { |
32 browser_->tab_strip_model()->RemoveObserver(this); | 37 browser_->tab_strip_model()->RemoveObserver(this); |
| 38 delete unload_detached_handler_; |
33 } | 39 } |
34 | 40 |
35 bool UnloadController::CanCloseContents(content::WebContents* contents) { | 41 bool UnloadController::CanCloseContents(content::WebContents* contents) { |
36 // Don't try to close the tab when the whole browser is being closed, since | 42 // Don't try to close the tab when the whole browser is being closed, since |
37 // that avoids the fast shutdown path where we just kill all the renderers. | 43 // that avoids the fast shutdown path where we just kill all the renderers. |
38 if (is_attempting_to_close_browser_) | 44 if (is_attempting_to_close_browser_) |
39 ClearUnloadState(contents, true); | 45 ClearUnloadState(contents, true); |
40 return !is_attempting_to_close_browser_; | 46 return !is_attempting_to_close_browser_; |
41 } | 47 } |
42 | 48 |
43 bool UnloadController::BeforeUnloadFired(content::WebContents* contents, | 49 bool UnloadController::BeforeUnloadFired(content::WebContents* contents, |
44 bool proceed) { | 50 bool proceed) { |
45 if (!is_attempting_to_close_browser_) { | 51 if (!is_attempting_to_close_browser_) { |
46 if (!proceed) | 52 if (proceed) { |
| 53 // No more dialogs are possible, so remove the tab and finish |
| 54 // running unload listeners asynchrounously. |
| 55 unload_detached_handler_->DetachWebContents(browser_->tab_strip_model(), |
| 56 contents); |
| 57 } else { |
47 contents->SetClosedByUserGesture(false); | 58 contents->SetClosedByUserGesture(false); |
| 59 } |
48 return proceed; | 60 return proceed; |
49 } | 61 } |
50 | 62 |
51 if (!proceed) { | 63 if (!proceed) { |
52 CancelWindowClose(); | 64 CancelWindowClose(); |
53 contents->SetClosedByUserGesture(false); | 65 contents->SetClosedByUserGesture(false); |
54 return false; | 66 return false; |
55 } | 67 } |
56 | 68 |
57 if (RemoveFromSet(&tabs_needing_before_unload_fired_, contents)) { | 69 if (RemoveFromSet(&tabs_needing_before_unload_fired_, contents)) { |
(...skipping 10 matching lines...) Expand all Loading... |
68 return true; | 80 return true; |
69 } | 81 } |
70 | 82 |
71 bool UnloadController::ShouldCloseWindow() { | 83 bool UnloadController::ShouldCloseWindow() { |
72 if (HasCompletedUnloadProcessing()) | 84 if (HasCompletedUnloadProcessing()) |
73 return true; | 85 return true; |
74 | 86 |
75 is_attempting_to_close_browser_ = true; | 87 is_attempting_to_close_browser_ = true; |
76 | 88 |
77 if (!TabsNeedBeforeUnloadFired()) | 89 if (!TabsNeedBeforeUnloadFired()) |
78 return true; | 90 return !unload_detached_handler_->HasTabs(); |
79 | 91 |
80 ProcessPendingTabs(); | 92 ProcessPendingTabs(); |
81 return false; | 93 return false; |
82 } | 94 } |
83 | 95 |
84 bool UnloadController::TabsNeedBeforeUnloadFired() { | 96 bool UnloadController::TabsNeedBeforeUnloadFired() { |
85 if (tabs_needing_before_unload_fired_.empty()) { | 97 if (tabs_needing_before_unload_fired_.empty()) { |
86 for (int i = 0; i < browser_->tab_count(); ++i) { | 98 for (int i = 0; i < browser_->tab_count(); ++i) { |
87 content::WebContents* contents = | 99 content::WebContents* contents = |
88 chrome::GetTabContentsAt(browser_, i)->web_contents(); | 100 chrome::GetTabContentsAt(browser_, i)->web_contents(); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 } | 187 } |
176 | 188 |
177 // Process beforeunload tabs first. When that queue is empty, process | 189 // Process beforeunload tabs first. When that queue is empty, process |
178 // unload tabs. | 190 // unload tabs. |
179 if (!tabs_needing_before_unload_fired_.empty()) { | 191 if (!tabs_needing_before_unload_fired_.empty()) { |
180 content::WebContents* web_contents = | 192 content::WebContents* web_contents = |
181 *(tabs_needing_before_unload_fired_.begin()); | 193 *(tabs_needing_before_unload_fired_.begin()); |
182 // Null check render_view_host here as this gets called on a PostTask and | 194 // Null check render_view_host here as this gets called on a PostTask and |
183 // the tab's render_view_host may have been nulled out. | 195 // the tab's render_view_host may have been nulled out. |
184 if (web_contents->GetRenderViewHost()) { | 196 if (web_contents->GetRenderViewHost()) { |
| 197 web_contents->OnCloseStarted(); |
185 web_contents->GetRenderViewHost()->FirePageBeforeUnload(false); | 198 web_contents->GetRenderViewHost()->FirePageBeforeUnload(false); |
186 } else { | 199 } else { |
187 ClearUnloadState(web_contents, true); | 200 ClearUnloadState(web_contents, true); |
188 } | 201 } |
189 } else if (!tabs_needing_unload_fired_.empty()) { | 202 } else if (!tabs_needing_unload_fired_.empty()) { |
190 // We've finished firing all beforeunload events and can proceed with unload | 203 // We've finished firing all beforeunload events and can proceed with unload |
191 // events. | 204 // events. |
192 // TODO(ojan): We should add a call to browser_shutdown::OnShutdownStarting | 205 // TODO(ojan): We should add a call to browser_shutdown::OnShutdownStarting |
193 // somewhere around here so that we have accurate measurements of shutdown | 206 // somewhere around here so that we have accurate measurements of shutdown |
194 // time. | 207 // time. |
195 // TODO(ojan): We can probably fire all the unload events in parallel and | 208 // TODO(ojan): We can probably fire all the unload events in parallel and |
196 // get a perf benefit from that in the cases where the tab hangs in it's | 209 // get a perf benefit from that in the cases where the tab hangs in it's |
197 // unload handler or takes a long time to page in. | 210 // unload handler or takes a long time to page in. |
198 content::WebContents* web_contents = *(tabs_needing_unload_fired_.begin()); | 211 content::WebContents* web_contents = *(tabs_needing_unload_fired_.begin()); |
199 // Null check render_view_host here as this gets called on a PostTask and | 212 // Null check render_view_host here as this gets called on a PostTask and |
200 // the tab's render_view_host may have been nulled out. | 213 // the tab's render_view_host may have been nulled out. |
201 if (web_contents->GetRenderViewHost()) { | 214 if (web_contents->GetRenderViewHost()) { |
202 web_contents->GetRenderViewHost()->ClosePage(); | 215 if (!unload_detached_handler_->HasTabs()) { |
| 216 web_contents->GetRenderViewHost()->ClosePage(); |
| 217 } |
203 } else { | 218 } else { |
204 ClearUnloadState(web_contents, true); | 219 ClearUnloadState(web_contents, true); |
205 } | 220 } |
206 } else { | 221 } else { |
207 NOTREACHED(); | 222 NOTREACHED(); |
208 } | 223 } |
209 } | 224 } |
210 | 225 |
211 bool UnloadController::HasCompletedUnloadProcessing() const { | 226 bool UnloadController::HasCompletedUnloadProcessing() const { |
212 return is_attempting_to_close_browser_ && | 227 return is_attempting_to_close_browser_ && |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 } else { | 265 } else { |
251 MessageLoop::current()->PostTask( | 266 MessageLoop::current()->PostTask( |
252 FROM_HERE, | 267 FROM_HERE, |
253 base::Bind(&UnloadController::ProcessPendingTabs, | 268 base::Bind(&UnloadController::ProcessPendingTabs, |
254 weak_factory_.GetWeakPtr())); | 269 weak_factory_.GetWeakPtr())); |
255 } | 270 } |
256 } | 271 } |
257 } | 272 } |
258 | 273 |
259 } // namespace chrome | 274 } // namespace chrome |
OLD | NEW |