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/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
9 #include "chrome/browser/devtools/devtools_window.h" | 9 #include "chrome/browser/devtools/devtools_window.h" |
10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 bool UnloadController::CanCloseContents(content::WebContents* contents) { | 35 bool UnloadController::CanCloseContents(content::WebContents* contents) { |
36 // Don't try to close the tab when the whole browser is being closed, since | 36 // 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. | 37 // that avoids the fast shutdown path where we just kill all the renderers. |
38 if (is_attempting_to_close_browser_) | 38 if (is_attempting_to_close_browser_) |
39 ClearUnloadState(contents, true); | 39 ClearUnloadState(contents, true); |
40 return !is_attempting_to_close_browser_ || | 40 return !is_attempting_to_close_browser_ || |
41 is_calling_before_unload_handlers(); | 41 is_calling_before_unload_handlers(); |
42 } | 42 } |
43 | 43 |
44 // static | 44 // static |
| 45 bool UnloadController::CanFastShutdownWebContents( |
| 46 content::WebContents* contents) { |
| 47 return DevToolsWindow::GetInstanceForInspectedRenderViewHost( |
| 48 contents->GetRenderViewHost()) == NULL; |
| 49 } |
| 50 |
| 51 // static |
45 bool UnloadController::RunUnloadEventsHelper(content::WebContents* contents) { | 52 bool UnloadController::RunUnloadEventsHelper(content::WebContents* contents) { |
| 53 // If there's a devtools window attached to the web contents, |
| 54 // then we would like to run its beforeunload handlers first. |
| 55 if (DevToolsWindow::InterceptPageBeforeUnload(contents)) { |
| 56 return true; |
| 57 } |
46 // If the WebContents is not connected yet, then there's no unload | 58 // If the WebContents is not connected yet, then there's no unload |
47 // handler we can fire even if the WebContents has an unload listener. | 59 // handler we can fire even if the WebContents has an unload listener. |
48 // One case where we hit this is in a tab that has an infinite loop | 60 // One case where we hit this is in a tab that has an infinite loop |
49 // before load. | 61 // before load. |
50 if (contents->NeedToFireBeforeUnload()) { | 62 if (contents->NeedToFireBeforeUnload()) { |
51 // If the page has unload listeners, then we tell the renderer to fire | 63 // If the page has unload listeners, then we tell the renderer to fire |
52 // them. Once they have fired, we'll get a message back saying whether | 64 // them. Once they have fired, we'll get a message back saying whether |
53 // to proceed closing the page or not, which sends us back to this method | 65 // to proceed closing the page or not, which sends us back to this method |
54 // with the NeedToFireBeforeUnload bit cleared. | 66 // with the NeedToFireBeforeUnload bit cleared. |
55 contents->GetRenderViewHost()->FirePageBeforeUnload(false); | 67 contents->GetRenderViewHost()->FirePageBeforeUnload(false); |
56 return true; | 68 return true; |
57 } | 69 } |
58 return false; | 70 return false; |
59 } | 71 } |
60 | 72 |
61 bool UnloadController::BeforeUnloadFired(content::WebContents* contents, | 73 bool UnloadController::BeforeUnloadFired(content::WebContents* contents, |
62 bool proceed) { | 74 bool proceed) { |
| 75 if (!proceed) |
| 76 DevToolsWindow::PageClosingCanceled(contents); |
| 77 |
63 if (!is_attempting_to_close_browser_) { | 78 if (!is_attempting_to_close_browser_) { |
64 if (!proceed) | 79 if (!proceed) |
65 contents->SetClosedByUserGesture(false); | 80 contents->SetClosedByUserGesture(false); |
66 return proceed; | 81 return proceed; |
67 } | 82 } |
68 | 83 |
69 if (!proceed) { | 84 if (!proceed) { |
70 CancelWindowClose(); | 85 CancelWindowClose(); |
71 contents->SetClosedByUserGesture(false); | 86 contents->SetClosedByUserGesture(false); |
72 return false; | 87 return false; |
(...skipping 10 matching lines...) Expand all Loading... |
83 return false; | 98 return false; |
84 } | 99 } |
85 | 100 |
86 return true; | 101 return true; |
87 } | 102 } |
88 | 103 |
89 bool UnloadController::ShouldCloseWindow() { | 104 bool UnloadController::ShouldCloseWindow() { |
90 if (HasCompletedUnloadProcessing()) | 105 if (HasCompletedUnloadProcessing()) |
91 return true; | 106 return true; |
92 | 107 |
| 108 if (browser_->is_devtools() && |
| 109 DevToolsWindow::ShouldCloseDevToolsBrowser(browser_)) { |
| 110 return true; |
| 111 } |
| 112 |
93 // The behavior followed here varies based on the current phase of the | 113 // The behavior followed here varies based on the current phase of the |
94 // operation and whether a batched shutdown is in progress. | 114 // operation and whether a batched shutdown is in progress. |
95 // | 115 // |
96 // If there are tabs with outstanding beforeunload handlers: | 116 // If there are tabs with outstanding beforeunload handlers: |
97 // 1. If a batched shutdown is in progress: return false. | 117 // 1. If a batched shutdown is in progress: return false. |
98 // This is to prevent interference with batched shutdown already in | 118 // This is to prevent interference with batched shutdown already in |
99 // progress. | 119 // progress. |
100 // 2. Otherwise: start sending beforeunload events and return false. | 120 // 2. Otherwise: start sending beforeunload events and return false. |
101 // | 121 // |
102 // Otherwise, If there are no tabs with outstanding beforeunload handlers: | 122 // Otherwise, If there are no tabs with outstanding beforeunload handlers: |
103 // 3. If a batched shutdown is in progress: start sending unload events and | 123 // 3. If a batched shutdown is in progress: start sending unload events and |
104 // return false. | 124 // return false. |
105 // 4. Otherwise: return true. | 125 // 4. Otherwise: return true. |
106 is_attempting_to_close_browser_ = true; | 126 is_attempting_to_close_browser_ = true; |
107 // Cases 1 and 4. | 127 // Cases 1 and 4. |
108 bool need_beforeunload_fired = TabsNeedBeforeUnloadFired(); | 128 bool need_beforeunload_fired = TabsNeedBeforeUnloadFired(); |
109 if (need_beforeunload_fired == is_calling_before_unload_handlers()) | 129 if (need_beforeunload_fired == is_calling_before_unload_handlers()) |
110 return !need_beforeunload_fired; | 130 return !need_beforeunload_fired; |
111 | 131 |
112 // Cases 2 and 3. | 132 // Cases 2 and 3. |
113 on_close_confirmed_.Reset(); | 133 on_close_confirmed_.Reset(); |
114 ProcessPendingTabs(); | 134 ProcessPendingTabs(); |
115 return false; | 135 return false; |
116 } | 136 } |
117 | 137 |
118 bool UnloadController::CallBeforeUnloadHandlers( | 138 bool UnloadController::CallBeforeUnloadHandlers( |
119 const base::Callback<void(bool)>& on_close_confirmed) { | 139 const base::Callback<void(bool)>& on_close_confirmed) { |
120 if (HasCompletedUnloadProcessing() || !TabsNeedBeforeUnloadFired()) | 140 // DevTools browser will get its beforeunload events triggered on |
| 141 // inspected tab closing |
| 142 if (browser_->is_devtools() || HasCompletedUnloadProcessing() || |
| 143 !TabsNeedBeforeUnloadFired()) |
121 return false; | 144 return false; |
122 | 145 |
123 is_attempting_to_close_browser_ = true; | 146 is_attempting_to_close_browser_ = true; |
124 on_close_confirmed_ = on_close_confirmed; | 147 on_close_confirmed_ = on_close_confirmed; |
125 | 148 |
126 ProcessPendingTabs(); | 149 ProcessPendingTabs(); |
127 return true; | 150 return true; |
128 } | 151 } |
129 | 152 |
130 void UnloadController::ResetBeforeUnloadHandlers() { | 153 void UnloadController::ResetBeforeUnloadHandlers() { |
131 if (!is_calling_before_unload_handlers()) | 154 if (!is_calling_before_unload_handlers()) |
132 return; | 155 return; |
133 CancelWindowClose(); | 156 CancelWindowClose(); |
134 } | 157 } |
135 | 158 |
136 bool UnloadController::TabsNeedBeforeUnloadFired() { | 159 bool UnloadController::TabsNeedBeforeUnloadFired() { |
137 if (tabs_needing_before_unload_fired_.empty()) { | 160 if (tabs_needing_before_unload_fired_.empty()) { |
138 for (int i = 0; i < browser_->tab_strip_model()->count(); ++i) { | 161 for (int i = 0; i < browser_->tab_strip_model()->count(); ++i) { |
139 content::WebContents* contents = | 162 content::WebContents* contents = |
140 browser_->tab_strip_model()->GetWebContentsAt(i); | 163 browser_->tab_strip_model()->GetWebContentsAt(i); |
141 if (!ContainsKey(tabs_needing_unload_fired_, contents) && | 164 if (!ContainsKey(tabs_needing_unload_fired_, contents) && |
142 contents->NeedToFireBeforeUnload()) { | 165 (contents->NeedToFireBeforeUnload() || |
| 166 DevToolsWindow::NeedToFireBeforeUnload(contents))) { |
143 tabs_needing_before_unload_fired_.insert(contents); | 167 tabs_needing_before_unload_fired_.insert(contents); |
144 } | 168 } |
145 } | 169 } |
146 } | 170 } |
147 return !tabs_needing_before_unload_fired_.empty(); | 171 return !tabs_needing_before_unload_fired_.empty(); |
148 } | 172 } |
149 | 173 |
150 //////////////////////////////////////////////////////////////////////////////// | 174 //////////////////////////////////////////////////////////////////////////////// |
151 // UnloadController, content::NotificationObserver implementation: | 175 // UnloadController, content::NotificationObserver implementation: |
152 | 176 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 } | 253 } |
230 | 254 |
231 // Process beforeunload tabs first. When that queue is empty, process | 255 // Process beforeunload tabs first. When that queue is empty, process |
232 // unload tabs. | 256 // unload tabs. |
233 if (!tabs_needing_before_unload_fired_.empty()) { | 257 if (!tabs_needing_before_unload_fired_.empty()) { |
234 content::WebContents* web_contents = | 258 content::WebContents* web_contents = |
235 *(tabs_needing_before_unload_fired_.begin()); | 259 *(tabs_needing_before_unload_fired_.begin()); |
236 // Null check render_view_host here as this gets called on a PostTask and | 260 // Null check render_view_host here as this gets called on a PostTask and |
237 // the tab's render_view_host may have been nulled out. | 261 // the tab's render_view_host may have been nulled out. |
238 if (web_contents->GetRenderViewHost()) { | 262 if (web_contents->GetRenderViewHost()) { |
239 web_contents->GetRenderViewHost()->FirePageBeforeUnload(false); | 263 if (!DevToolsWindow::InterceptPageBeforeUnload(web_contents)) |
| 264 web_contents->GetRenderViewHost()->FirePageBeforeUnload(false); |
240 } else { | 265 } else { |
241 ClearUnloadState(web_contents, true); | 266 ClearUnloadState(web_contents, true); |
242 } | 267 } |
243 } else if (is_calling_before_unload_handlers()) { | 268 } else if (is_calling_before_unload_handlers()) { |
244 on_close_confirmed_.Run(true); | 269 on_close_confirmed_.Run(true); |
245 } else if (!tabs_needing_unload_fired_.empty()) { | 270 } else if (!tabs_needing_unload_fired_.empty()) { |
246 // We've finished firing all beforeunload events and can proceed with unload | 271 // We've finished firing all beforeunload events and can proceed with unload |
247 // events. | 272 // events. |
248 // TODO(ojan): We should add a call to browser_shutdown::OnShutdownStarting | 273 // TODO(ojan): We should add a call to browser_shutdown::OnShutdownStarting |
249 // somewhere around here so that we have accurate measurements of shutdown | 274 // somewhere around here so that we have accurate measurements of shutdown |
(...skipping 17 matching lines...) Expand all Loading... |
267 bool UnloadController::HasCompletedUnloadProcessing() const { | 292 bool UnloadController::HasCompletedUnloadProcessing() const { |
268 return is_attempting_to_close_browser_ && | 293 return is_attempting_to_close_browser_ && |
269 tabs_needing_before_unload_fired_.empty() && | 294 tabs_needing_before_unload_fired_.empty() && |
270 tabs_needing_unload_fired_.empty(); | 295 tabs_needing_unload_fired_.empty(); |
271 } | 296 } |
272 | 297 |
273 void UnloadController::CancelWindowClose() { | 298 void UnloadController::CancelWindowClose() { |
274 // Closing of window can be canceled from a beforeunload handler. | 299 // Closing of window can be canceled from a beforeunload handler. |
275 DCHECK(is_attempting_to_close_browser_); | 300 DCHECK(is_attempting_to_close_browser_); |
276 tabs_needing_before_unload_fired_.clear(); | 301 tabs_needing_before_unload_fired_.clear(); |
| 302 for (UnloadListenerSet::iterator it = tabs_needing_unload_fired_.begin(); |
| 303 it != tabs_needing_unload_fired_.end(); ++it) { |
| 304 DevToolsWindow::PageClosingCanceled(*it); |
| 305 } |
277 tabs_needing_unload_fired_.clear(); | 306 tabs_needing_unload_fired_.clear(); |
278 if (is_calling_before_unload_handlers()) { | 307 if (is_calling_before_unload_handlers()) { |
279 base::Callback<void(bool)> on_close_confirmed = on_close_confirmed_; | 308 base::Callback<void(bool)> on_close_confirmed = on_close_confirmed_; |
280 on_close_confirmed_.Reset(); | 309 on_close_confirmed_.Reset(); |
281 on_close_confirmed.Run(false); | 310 on_close_confirmed.Run(false); |
282 } | 311 } |
283 is_attempting_to_close_browser_ = false; | 312 is_attempting_to_close_browser_ = false; |
284 | 313 |
285 content::NotificationService::current()->Notify( | 314 content::NotificationService::current()->Notify( |
286 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, | 315 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, |
(...skipping 24 matching lines...) Expand all Loading... |
311 } else { | 340 } else { |
312 base::MessageLoop::current()->PostTask( | 341 base::MessageLoop::current()->PostTask( |
313 FROM_HERE, | 342 FROM_HERE, |
314 base::Bind(&UnloadController::ProcessPendingTabs, | 343 base::Bind(&UnloadController::ProcessPendingTabs, |
315 weak_factory_.GetWeakPtr())); | 344 weak_factory_.GetWeakPtr())); |
316 } | 345 } |
317 } | 346 } |
318 } | 347 } |
319 | 348 |
320 } // namespace chrome | 349 } // namespace chrome |
OLD | NEW |