| 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 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 DevToolsWindow::NeedsToInterceptBeforeUnload(contents); | 173 DevToolsWindow::NeedsToInterceptBeforeUnload(contents); |
| 174 if (!ContainsKey(tabs_needing_unload_fired_, contents) && | 174 if (!ContainsKey(tabs_needing_unload_fired_, contents) && |
| 175 should_fire_beforeunload) { | 175 should_fire_beforeunload) { |
| 176 tabs_needing_before_unload_fired_.insert(contents); | 176 tabs_needing_before_unload_fired_.insert(contents); |
| 177 } | 177 } |
| 178 } | 178 } |
| 179 } | 179 } |
| 180 return !tabs_needing_before_unload_fired_.empty(); | 180 return !tabs_needing_before_unload_fired_.empty(); |
| 181 } | 181 } |
| 182 | 182 |
| 183 void UnloadController::CancelWindowClose() { |
| 184 // Closing of window can be canceled from a beforeunload handler. |
| 185 DCHECK(is_attempting_to_close_browser_); |
| 186 tabs_needing_before_unload_fired_.clear(); |
| 187 for (UnloadListenerSet::iterator it = tabs_needing_unload_fired_.begin(); |
| 188 it != tabs_needing_unload_fired_.end(); ++it) { |
| 189 DevToolsWindow::OnPageCloseCanceled(*it); |
| 190 } |
| 191 tabs_needing_unload_fired_.clear(); |
| 192 if (is_calling_before_unload_handlers()) { |
| 193 base::Callback<void(bool)> on_close_confirmed = on_close_confirmed_; |
| 194 on_close_confirmed_.Reset(); |
| 195 on_close_confirmed.Run(false); |
| 196 } |
| 197 is_attempting_to_close_browser_ = false; |
| 198 |
| 199 content::NotificationService::current()->Notify( |
| 200 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, |
| 201 content::Source<Browser>(browser_), |
| 202 content::NotificationService::NoDetails()); |
| 203 } |
| 204 |
| 183 //////////////////////////////////////////////////////////////////////////////// | 205 //////////////////////////////////////////////////////////////////////////////// |
| 184 // UnloadController, content::NotificationObserver implementation: | 206 // UnloadController, content::NotificationObserver implementation: |
| 185 | 207 |
| 186 void UnloadController::Observe(int type, | 208 void UnloadController::Observe(int type, |
| 187 const content::NotificationSource& source, | 209 const content::NotificationSource& source, |
| 188 const content::NotificationDetails& details) { | 210 const content::NotificationDetails& details) { |
| 189 switch (type) { | 211 switch (type) { |
| 190 case content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED: | 212 case content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED: |
| 191 if (is_attempting_to_close_browser_) { | 213 if (is_attempting_to_close_browser_) { |
| 192 ClearUnloadState(content::Source<content::WebContents>(source).ptr(), | 214 ClearUnloadState(content::Source<content::WebContents>(source).ptr(), |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 NOTREACHED(); | 323 NOTREACHED(); |
| 302 } | 324 } |
| 303 } | 325 } |
| 304 | 326 |
| 305 bool UnloadController::HasCompletedUnloadProcessing() const { | 327 bool UnloadController::HasCompletedUnloadProcessing() const { |
| 306 return is_attempting_to_close_browser_ && | 328 return is_attempting_to_close_browser_ && |
| 307 tabs_needing_before_unload_fired_.empty() && | 329 tabs_needing_before_unload_fired_.empty() && |
| 308 tabs_needing_unload_fired_.empty(); | 330 tabs_needing_unload_fired_.empty(); |
| 309 } | 331 } |
| 310 | 332 |
| 311 void UnloadController::CancelWindowClose() { | |
| 312 // Closing of window can be canceled from a beforeunload handler. | |
| 313 DCHECK(is_attempting_to_close_browser_); | |
| 314 tabs_needing_before_unload_fired_.clear(); | |
| 315 for (UnloadListenerSet::iterator it = tabs_needing_unload_fired_.begin(); | |
| 316 it != tabs_needing_unload_fired_.end(); ++it) { | |
| 317 DevToolsWindow::OnPageCloseCanceled(*it); | |
| 318 } | |
| 319 tabs_needing_unload_fired_.clear(); | |
| 320 if (is_calling_before_unload_handlers()) { | |
| 321 base::Callback<void(bool)> on_close_confirmed = on_close_confirmed_; | |
| 322 on_close_confirmed_.Reset(); | |
| 323 on_close_confirmed.Run(false); | |
| 324 } | |
| 325 is_attempting_to_close_browser_ = false; | |
| 326 | |
| 327 content::NotificationService::current()->Notify( | |
| 328 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, | |
| 329 content::Source<Browser>(browser_), | |
| 330 content::NotificationService::NoDetails()); | |
| 331 } | |
| 332 | |
| 333 bool UnloadController::RemoveFromSet(UnloadListenerSet* set, | 333 bool UnloadController::RemoveFromSet(UnloadListenerSet* set, |
| 334 content::WebContents* web_contents) { | 334 content::WebContents* web_contents) { |
| 335 DCHECK(is_attempting_to_close_browser_); | 335 DCHECK(is_attempting_to_close_browser_); |
| 336 | 336 |
| 337 UnloadListenerSet::iterator iter = | 337 UnloadListenerSet::iterator iter = |
| 338 std::find(set->begin(), set->end(), web_contents); | 338 std::find(set->begin(), set->end(), web_contents); |
| 339 if (iter != set->end()) { | 339 if (iter != set->end()) { |
| 340 set->erase(iter); | 340 set->erase(iter); |
| 341 return true; | 341 return true; |
| 342 } | 342 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 353 } else { | 353 } else { |
| 354 base::MessageLoop::current()->PostTask( | 354 base::MessageLoop::current()->PostTask( |
| 355 FROM_HERE, | 355 FROM_HERE, |
| 356 base::Bind(&UnloadController::ProcessPendingTabs, | 356 base::Bind(&UnloadController::ProcessPendingTabs, |
| 357 weak_factory_.GetWeakPtr())); | 357 weak_factory_.GetWeakPtr())); |
| 358 } | 358 } |
| 359 } | 359 } |
| 360 } | 360 } |
| 361 | 361 |
| 362 } // namespace chrome | 362 } // namespace chrome |
| OLD | NEW |