OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/browser_list.h" | 5 #include "chrome/browser/ui/browser_list.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
12 #include "chrome/browser/browser_shutdown.h" | 12 #include "chrome/browser/browser_shutdown.h" |
13 #include "chrome/browser/metrics/thread_watcher.h" | 13 #include "chrome/browser/metrics/thread_watcher.h" |
14 #include "chrome/browser/prefs/pref_service.h" | 14 #include "chrome/browser/prefs/pref_service.h" |
15 #include "chrome/browser/printing/background_printing_manager.h" | 15 #include "chrome/browser/printing/background_printing_manager.h" |
16 #include "chrome/browser/profiles/profile_manager.h" | 16 #include "chrome/browser/profiles/profile_manager.h" |
17 #include "chrome/browser/ui/browser_window.h" | 17 #include "chrome/browser/ui/browser_window.h" |
18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
19 #include "chrome/common/chrome_notification_types.h" | 19 #include "chrome/common/chrome_notification_types.h" |
20 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
21 #include "content/browser/renderer_host/render_process_host.h" | 21 #include "content/browser/renderer_host/render_process_host.h" |
22 #include "content/browser/tab_contents/navigation_details.h" | 22 #include "content/browser/tab_contents/navigation_details.h" |
23 #include "content/common/result_codes.h" | 23 #include "content/common/result_codes.h" |
24 #include "content/public/browser/notification_registrar.h" | 24 #include "content/public/browser/notification_registrar.h" |
25 #include "content/common/notification_service.h" | 25 #include "content/public/browser/notification_service.h" |
26 | 26 |
27 #if defined(OS_MACOSX) | 27 #if defined(OS_MACOSX) |
28 #include "chrome/browser/chrome_browser_application_mac.h" | 28 #include "chrome/browser/chrome_browser_application_mac.h" |
29 #endif | 29 #endif |
30 | 30 |
31 #if defined(OS_CHROMEOS) | 31 #if defined(OS_CHROMEOS) |
32 #include "chrome/browser/chromeos/boot_times_loader.h" | 32 #include "chrome/browser/chromeos/boot_times_loader.h" |
33 #include "chrome/browser/chromeos/cros/cros_library.h" | 33 #include "chrome/browser/chromeos/cros/cros_library.h" |
34 #include "chrome/browser/chromeos/cros/update_library.h" | 34 #include "chrome/browser/chromeos/cros/update_library.h" |
35 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" | 35 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" |
36 #include "chrome/browser/chromeos/dbus/session_manager_client.h" | 36 #include "chrome/browser/chromeos/dbus/session_manager_client.h" |
37 #if defined(TOOLKIT_USES_GTK) | 37 #if defined(TOOLKIT_USES_GTK) |
38 #include "chrome/browser/chromeos/wm_ipc.h" | 38 #include "chrome/browser/chromeos/wm_ipc.h" |
39 #endif | 39 #endif |
40 #endif | 40 #endif |
41 | 41 |
42 namespace { | 42 namespace { |
43 | 43 |
44 // This object is instantiated when the first Browser object is added to the | 44 // This object is instantiated when the first Browser object is added to the |
45 // list and delete when the last one is removed. It watches for loads and | 45 // list and delete when the last one is removed. It watches for loads and |
46 // creates histograms of some global object counts. | 46 // creates histograms of some global object counts. |
47 class BrowserActivityObserver : public content::NotificationObserver { | 47 class BrowserActivityObserver : public content::NotificationObserver { |
48 public: | 48 public: |
49 BrowserActivityObserver() { | 49 BrowserActivityObserver() { |
50 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 50 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
51 NotificationService::AllSources()); | 51 content::NotificationService::AllSources()); |
52 } | 52 } |
53 ~BrowserActivityObserver() {} | 53 ~BrowserActivityObserver() {} |
54 | 54 |
55 private: | 55 private: |
56 // content::NotificationObserver implementation. | 56 // content::NotificationObserver implementation. |
57 virtual void Observe(int type, | 57 virtual void Observe(int type, |
58 const content::NotificationSource& source, | 58 const content::NotificationSource& source, |
59 const content::NotificationDetails& details) { | 59 const content::NotificationDetails& details) { |
60 DCHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED); | 60 DCHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED); |
61 const content::LoadCommittedDetails& load = | 61 const content::LoadCommittedDetails& load = |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 // static | 241 // static |
242 void BrowserList::AddBrowser(Browser* browser) { | 242 void BrowserList::AddBrowser(Browser* browser) { |
243 DCHECK(browser); | 243 DCHECK(browser); |
244 browsers_.push_back(browser); | 244 browsers_.push_back(browser); |
245 | 245 |
246 g_browser_process->AddRefModule(); | 246 g_browser_process->AddRefModule(); |
247 | 247 |
248 if (!activity_observer) | 248 if (!activity_observer) |
249 activity_observer = new BrowserActivityObserver; | 249 activity_observer = new BrowserActivityObserver; |
250 | 250 |
251 NotificationService::current()->Notify( | 251 content::NotificationService::current()->Notify( |
252 chrome::NOTIFICATION_BROWSER_OPENED, | 252 chrome::NOTIFICATION_BROWSER_OPENED, |
253 content::Source<Browser>(browser), | 253 content::Source<Browser>(browser), |
254 NotificationService::NoDetails()); | 254 content::NotificationService::NoDetails()); |
255 | 255 |
256 // Send out notifications after add has occurred. Do some basic checking to | 256 // Send out notifications after add has occurred. Do some basic checking to |
257 // try to catch evil observers that change the list from under us. | 257 // try to catch evil observers that change the list from under us. |
258 size_t original_count = observers_.size(); | 258 size_t original_count = observers_.size(); |
259 FOR_EACH_OBSERVER(Observer, observers_, OnBrowserAdded(browser)); | 259 FOR_EACH_OBSERVER(Observer, observers_, OnBrowserAdded(browser)); |
260 DCHECK_EQ(original_count, observers_.size()) | 260 DCHECK_EQ(original_count, observers_.size()) |
261 << "observer list modified during notification"; | 261 << "observer list modified during notification"; |
262 } | 262 } |
263 | 263 |
264 // static | 264 // static |
265 void BrowserList::MarkAsCleanShutdown() { | 265 void BrowserList::MarkAsCleanShutdown() { |
266 for (const_iterator i = begin(); i != end(); ++i) { | 266 for (const_iterator i = begin(); i != end(); ++i) { |
267 (*i)->profile()->MarkAsCleanShutdown(); | 267 (*i)->profile()->MarkAsCleanShutdown(); |
268 } | 268 } |
269 } | 269 } |
270 | 270 |
271 void BrowserList::AttemptExitInternal() { | 271 void BrowserList::AttemptExitInternal() { |
272 NotificationService::current()->Notify( | 272 content::NotificationService::current()->Notify( |
273 content::NOTIFICATION_APP_EXITING, | 273 content::NOTIFICATION_APP_EXITING, |
274 NotificationService::AllSources(), | 274 content::NotificationService::AllSources(), |
275 NotificationService::NoDetails()); | 275 content::NotificationService::NoDetails()); |
276 | 276 |
277 #if !defined(OS_MACOSX) | 277 #if !defined(OS_MACOSX) |
278 // On most platforms, closing all windows causes the application to exit. | 278 // On most platforms, closing all windows causes the application to exit. |
279 CloseAllBrowsers(); | 279 CloseAllBrowsers(); |
280 #else | 280 #else |
281 // On the Mac, the application continues to run once all windows are closed. | 281 // On the Mac, the application continues to run once all windows are closed. |
282 // Terminate will result in a CloseAllBrowsers() call, and once (and if) | 282 // Terminate will result in a CloseAllBrowsers() call, and once (and if) |
283 // that is done, will cause the application to exit cleanly. | 283 // that is done, will cause the application to exit cleanly. |
284 chrome_browser_application_mac::Terminate(); | 284 chrome_browser_application_mac::Terminate(); |
285 #endif | 285 #endif |
286 } | 286 } |
287 | 287 |
288 // static | 288 // static |
289 void BrowserList::NotifyAndTerminate(bool fast_path) { | 289 void BrowserList::NotifyAndTerminate(bool fast_path) { |
290 #if defined(OS_CHROMEOS) | 290 #if defined(OS_CHROMEOS) |
291 if (!signout) | 291 if (!signout) |
292 return; | 292 return; |
293 #endif | 293 #endif |
294 | 294 |
295 if (fast_path) { | 295 if (fast_path) { |
296 NotificationService::current()->Notify( | 296 content::NotificationService::current()->Notify( |
297 content::NOTIFICATION_APP_TERMINATING, | 297 content::NOTIFICATION_APP_TERMINATING, |
298 NotificationService::AllSources(), | 298 content::NotificationService::AllSources(), |
299 NotificationService::NoDetails()); | 299 content::NotificationService::NoDetails()); |
300 } | 300 } |
301 | 301 |
302 #if defined(OS_CHROMEOS) | 302 #if defined(OS_CHROMEOS) |
303 NotifyWindowManagerAboutSignout(); | 303 NotifyWindowManagerAboutSignout(); |
304 chromeos::CrosLibrary* cros_library = chromeos::CrosLibrary::Get(); | 304 chromeos::CrosLibrary* cros_library = chromeos::CrosLibrary::Get(); |
305 if (cros_library->EnsureLoaded()) { | 305 if (cros_library->EnsureLoaded()) { |
306 // If update has been installed, reboot, otherwise, sign out. | 306 // If update has been installed, reboot, otherwise, sign out. |
307 if (cros_library->GetUpdateLibrary()->status().status == | 307 if (cros_library->GetUpdateLibrary()->status().status == |
308 chromeos::UPDATE_STATUS_UPDATED_NEED_REBOOT) { | 308 chromeos::UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
309 cros_library->GetUpdateLibrary()->RebootAfterUpdate(); | 309 cros_library->GetUpdateLibrary()->RebootAfterUpdate(); |
(...skipping 10 matching lines...) Expand all Loading... |
320 | 320 |
321 // static | 321 // static |
322 void BrowserList::RemoveBrowser(Browser* browser) { | 322 void BrowserList::RemoveBrowser(Browser* browser) { |
323 RemoveBrowserFrom(browser, &last_active_browsers_); | 323 RemoveBrowserFrom(browser, &last_active_browsers_); |
324 | 324 |
325 // Closing all windows does not indicate quitting the application on the Mac, | 325 // Closing all windows does not indicate quitting the application on the Mac, |
326 // however, many UI tests rely on this behavior so leave it be for now and | 326 // however, many UI tests rely on this behavior so leave it be for now and |
327 // simply ignore the behavior on the Mac outside of unit tests. | 327 // simply ignore the behavior on the Mac outside of unit tests. |
328 // TODO(andybons): Fix the UI tests to Do The Right Thing. | 328 // TODO(andybons): Fix the UI tests to Do The Right Thing. |
329 bool closing_last_browser = (browsers_.size() == 1); | 329 bool closing_last_browser = (browsers_.size() == 1); |
330 NotificationService::current()->Notify( | 330 content::NotificationService::current()->Notify( |
331 chrome::NOTIFICATION_BROWSER_CLOSED, | 331 chrome::NOTIFICATION_BROWSER_CLOSED, |
332 content::Source<Browser>(browser), | 332 content::Source<Browser>(browser), |
333 content::Details<bool>(&closing_last_browser)); | 333 content::Details<bool>(&closing_last_browser)); |
334 | 334 |
335 RemoveBrowserFrom(browser, &browsers_); | 335 RemoveBrowserFrom(browser, &browsers_); |
336 | 336 |
337 // Do some basic checking to try to catch evil observers | 337 // Do some basic checking to try to catch evil observers |
338 // that change the list from under us. | 338 // that change the list from under us. |
339 size_t original_count = observers_.size(); | 339 size_t original_count = observers_.size(); |
340 FOR_EACH_OBSERVER(Observer, observers_, OnBrowserRemoved(browser)); | 340 FOR_EACH_OBSERVER(Observer, observers_, OnBrowserRemoved(browser)); |
(...skipping 12 matching lines...) Expand all Loading... |
353 // If we're exiting, send out the APP_TERMINATING notification to allow other | 353 // If we're exiting, send out the APP_TERMINATING notification to allow other |
354 // modules to shut themselves down. | 354 // modules to shut themselves down. |
355 if (browsers_.empty() && | 355 if (browsers_.empty() && |
356 (browser_shutdown::IsTryingToQuit() || | 356 (browser_shutdown::IsTryingToQuit() || |
357 g_browser_process->IsShuttingDown())) { | 357 g_browser_process->IsShuttingDown())) { |
358 // Last browser has just closed, and this is a user-initiated quit or there | 358 // Last browser has just closed, and this is a user-initiated quit or there |
359 // is no module keeping the app alive, so send out our notification. No need | 359 // is no module keeping the app alive, so send out our notification. No need |
360 // to call ProfileManager::ShutdownSessionServices() as part of the | 360 // to call ProfileManager::ShutdownSessionServices() as part of the |
361 // shutdown, because Browser::WindowClosing() already makes sure that the | 361 // shutdown, because Browser::WindowClosing() already makes sure that the |
362 // SessionService is created and notified. | 362 // SessionService is created and notified. |
363 NotificationService::current()->Notify( | 363 content::NotificationService::current()->Notify( |
364 content::NOTIFICATION_APP_TERMINATING, | 364 content::NOTIFICATION_APP_TERMINATING, |
365 NotificationService::AllSources(), | 365 content::NotificationService::AllSources(), |
366 NotificationService::NoDetails()); | 366 content::NotificationService::NoDetails()); |
367 AllBrowsersClosedAndAppExiting(); | 367 AllBrowsersClosedAndAppExiting(); |
368 } | 368 } |
369 } | 369 } |
370 | 370 |
371 // static | 371 // static |
372 void BrowserList::AddObserver(BrowserList::Observer* observer) { | 372 void BrowserList::AddObserver(BrowserList::Observer* observer) { |
373 observers_.AddObserver(observer); | 373 observers_.AddObserver(observer); |
374 } | 374 } |
375 | 375 |
376 // static | 376 // static |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 // Start watching for hang during shutdown, and crash it if takes too long. | 517 // Start watching for hang during shutdown, and crash it if takes too long. |
518 // We disarm when |shutdown_watcher| object is destroyed, which is when we | 518 // We disarm when |shutdown_watcher| object is destroyed, which is when we |
519 // exit this function. | 519 // exit this function. |
520 ShutdownWatcherHelper shutdown_watcher; | 520 ShutdownWatcherHelper shutdown_watcher; |
521 shutdown_watcher.Arm(base::TimeDelta::FromSeconds(90)); | 521 shutdown_watcher.Arm(base::TimeDelta::FromSeconds(90)); |
522 | 522 |
523 // EndSession is invoked once per frame. Only do something the first time. | 523 // EndSession is invoked once per frame. Only do something the first time. |
524 static bool already_ended = false; | 524 static bool already_ended = false; |
525 // We may get called in the middle of shutdown, e.g. http://crbug.com/70852 | 525 // We may get called in the middle of shutdown, e.g. http://crbug.com/70852 |
526 // In this case, do nothing. | 526 // In this case, do nothing. |
527 if (already_ended || !NotificationService::current()) | 527 if (already_ended || !content::NotificationService::current()) |
528 return; | 528 return; |
529 already_ended = true; | 529 already_ended = true; |
530 | 530 |
531 browser_shutdown::OnShutdownStarting(browser_shutdown::END_SESSION); | 531 browser_shutdown::OnShutdownStarting(browser_shutdown::END_SESSION); |
532 | 532 |
533 NotificationService::current()->Notify( | 533 content::NotificationService::current()->Notify( |
534 content::NOTIFICATION_APP_EXITING, | 534 content::NOTIFICATION_APP_EXITING, |
535 NotificationService::AllSources(), | 535 content::NotificationService::AllSources(), |
536 NotificationService::NoDetails()); | 536 content::NotificationService::NoDetails()); |
537 | 537 |
538 // Write important data first. | 538 // Write important data first. |
539 g_browser_process->EndSession(); | 539 g_browser_process->EndSession(); |
540 | 540 |
541 BrowserList::CloseAllBrowsers(); | 541 BrowserList::CloseAllBrowsers(); |
542 | 542 |
543 // Send out notification. This is used during testing so that the test harness | 543 // Send out notification. This is used during testing so that the test harness |
544 // can properly shutdown before we exit. | 544 // can properly shutdown before we exit. |
545 NotificationService::current()->Notify( | 545 content::NotificationService::current()->Notify( |
546 chrome::NOTIFICATION_SESSION_END, | 546 chrome::NOTIFICATION_SESSION_END, |
547 NotificationService::AllSources(), | 547 content::NotificationService::AllSources(), |
548 NotificationService::NoDetails()); | 548 content::NotificationService::NoDetails()); |
549 | 549 |
550 // And shutdown. | 550 // And shutdown. |
551 browser_shutdown::Shutdown(); | 551 browser_shutdown::Shutdown(); |
552 } | 552 } |
553 | 553 |
554 // static | 554 // static |
555 void BrowserList::SessionEnding() { | 555 void BrowserList::SessionEnding() { |
556 TimeLimitedSessionEnding(); | 556 TimeLimitedSessionEnding(); |
557 | 557 |
558 #if defined(OS_WIN) | 558 #if defined(OS_WIN) |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
790 // If no more TabContents from Browsers, check the BackgroundPrintingManager. | 790 // If no more TabContents from Browsers, check the BackgroundPrintingManager. |
791 while (bg_printing_iterator_ != GetBackgroundPrintingManager()->end()) { | 791 while (bg_printing_iterator_ != GetBackgroundPrintingManager()->end()) { |
792 cur_ = *bg_printing_iterator_; | 792 cur_ = *bg_printing_iterator_; |
793 CHECK(cur_); | 793 CHECK(cur_); |
794 ++bg_printing_iterator_; | 794 ++bg_printing_iterator_; |
795 return; | 795 return; |
796 } | 796 } |
797 // Reached the end - no more TabContents. | 797 // Reached the end - no more TabContents. |
798 cur_ = NULL; | 798 cur_ = NULL; |
799 } | 799 } |
OLD | NEW |