| 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/lifetime/application_lifetime.h" | 5 #include "chrome/browser/lifetime/application_lifetime.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 13 #include "base/process/process.h" | 12 #include "base/process/process.h" |
| 14 #include "base/process/process_handle.h" | 13 #include "base/process/process_handle.h" |
| 15 #include "base/trace_event/trace_event.h" | 14 #include "base/trace_event/trace_event.h" |
| 16 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 17 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 18 #include "chrome/browser/browser_process_platform_part.h" | 17 #include "chrome/browser/browser_process_platform_part.h" |
| 19 #include "chrome/browser/browser_shutdown.h" | 18 #include "chrome/browser/browser_shutdown.h" |
| 20 #include "chrome/browser/chrome_notification_types.h" | 19 #include "chrome/browser/chrome_notification_types.h" |
| 21 #include "chrome/browser/download/download_service.h" | 20 #include "chrome/browser/download/download_service.h" |
| 22 #include "chrome/browser/lifetime/browser_close_manager.h" | 21 #include "chrome/browser/lifetime/browser_close_manager.h" |
| 22 #include "chrome/browser/lifetime/browser_keep_alive.h" |
| 23 #include "chrome/browser/metrics/thread_watcher.h" | 23 #include "chrome/browser/metrics/thread_watcher.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/browser/profiles/profile_manager.h" | 25 #include "chrome/browser/profiles/profile_manager.h" |
| 26 #include "chrome/browser/ui/browser.h" | 26 #include "chrome/browser/ui/browser.h" |
| 27 #include "chrome/browser/ui/browser_finder.h" | 27 #include "chrome/browser/ui/browser_finder.h" |
| 28 #include "chrome/browser/ui/browser_iterator.h" | 28 #include "chrome/browser/ui/browser_iterator.h" |
| 29 #include "chrome/browser/ui/browser_tabstrip.h" | 29 #include "chrome/browser/ui/browser_tabstrip.h" |
| 30 #include "chrome/browser/ui/browser_window.h" | 30 #include "chrome/browser/ui/browser_window.h" |
| 31 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 31 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 32 #include "chrome/browser/ui/user_manager.h" | 32 #include "chrome/browser/ui/user_manager.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 | 73 |
| 74 // Check TabsNeedBeforeUnloadFired(). | 74 // Check TabsNeedBeforeUnloadFired(). |
| 75 for (; !browser_it.done(); browser_it.Next()) { | 75 for (; !browser_it.done(); browser_it.Next()) { |
| 76 if (browser_it->TabsNeedBeforeUnloadFired()) | 76 if (browser_it->TabsNeedBeforeUnloadFired()) |
| 77 return false; | 77 return false; |
| 78 } | 78 } |
| 79 return true; | 79 return true; |
| 80 } | 80 } |
| 81 #endif // !defined(OS_ANDROID) | 81 #endif // !defined(OS_ANDROID) |
| 82 | 82 |
| 83 int g_keep_alive_count = 0; | |
| 84 bool g_disable_shutdown_for_testing = false; | |
| 85 | |
| 86 #if defined(OS_CHROMEOS) | 83 #if defined(OS_CHROMEOS) |
| 87 // Whether chrome should send stop request to a session manager. | 84 // Whether chrome should send stop request to a session manager. |
| 88 bool g_send_stop_request_to_session_manager = false; | 85 bool g_send_stop_request_to_session_manager = false; |
| 89 #endif | 86 #endif |
| 90 | 87 |
| 91 } // namespace | 88 } // namespace |
| 92 | 89 |
| 93 void MarkAsCleanShutdown() { | 90 void MarkAsCleanShutdown() { |
| 94 // TODO(beng): Can this use ProfileManager::GetLoadedProfiles() instead? | 91 // TODO(beng): Can this use ProfileManager::GetLoadedProfiles() instead? |
| 95 for (chrome::BrowserIterator it; !it.done(); it.Next()) | 92 for (chrome::BrowserIterator it; !it.done(); it.Next()) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 114 void CloseAllBrowsersAndQuit() { | 111 void CloseAllBrowsersAndQuit() { |
| 115 browser_shutdown::SetTryingToQuit(true); | 112 browser_shutdown::SetTryingToQuit(true); |
| 116 CloseAllBrowsers(); | 113 CloseAllBrowsers(); |
| 117 } | 114 } |
| 118 | 115 |
| 119 void CloseAllBrowsers() { | 116 void CloseAllBrowsers() { |
| 120 // If there are no browsers and closing the last browser would quit the | 117 // If there are no browsers and closing the last browser would quit the |
| 121 // application, send the APP_TERMINATING action here. Otherwise, it will be | 118 // application, send the APP_TERMINATING action here. Otherwise, it will be |
| 122 // sent by RemoveBrowser() when the last browser has closed. | 119 // sent by RemoveBrowser() when the last browser has closed. |
| 123 if (chrome::GetTotalBrowserCount() == 0 && | 120 if (chrome::GetTotalBrowserCount() == 0 && |
| 124 (browser_shutdown::IsTryingToQuit() || !chrome::WillKeepAlive())) { | 121 (browser_shutdown::IsTryingToQuit() || |
| 122 !browser_lifetime::WillKeepAlive())) { |
| 125 // Tell everyone that we are shutting down. | 123 // Tell everyone that we are shutting down. |
| 126 browser_shutdown::SetTryingToQuit(true); | 124 browser_shutdown::SetTryingToQuit(true); |
| 127 | 125 |
| 128 #if defined(ENABLE_SESSION_SERVICE) | 126 #if defined(ENABLE_SESSION_SERVICE) |
| 129 // If ShuttingDownWithoutClosingBrowsers() returns true, the session | 127 // If ShuttingDownWithoutClosingBrowsers() returns true, the session |
| 130 // services may not get a chance to shut down normally, so explicitly shut | 128 // services may not get a chance to shut down normally, so explicitly shut |
| 131 // them down here to ensure they have a chance to persist their data. | 129 // them down here to ensure they have a chance to persist their data. |
| 132 ProfileManager::ShutdownSessionServices(); | 130 ProfileManager::ShutdownSessionServices(); |
| 133 #endif | 131 #endif |
| 134 | 132 |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 #if defined(OS_WIN) | 285 #if defined(OS_WIN) |
| 288 base::win::SetShouldCrashOnProcessDetach(false); | 286 base::win::SetShouldCrashOnProcessDetach(false); |
| 289 #endif | 287 #endif |
| 290 // On Windows 7 and later, the system will consider the process ripe for | 288 // On Windows 7 and later, the system will consider the process ripe for |
| 291 // termination as soon as it hides or destroys its windows. Since any | 289 // termination as soon as it hides or destroys its windows. Since any |
| 292 // execution past that point will be non-deterministically cut short, we | 290 // execution past that point will be non-deterministically cut short, we |
| 293 // might as well put ourselves out of that misery deterministically. | 291 // might as well put ourselves out of that misery deterministically. |
| 294 base::Process::Current().Terminate(0, false); | 292 base::Process::Current().Terminate(0, false); |
| 295 } | 293 } |
| 296 | 294 |
| 297 void IncrementKeepAliveCount() { | |
| 298 // Increment the browser process refcount as long as we're keeping the | |
| 299 // application alive. | |
| 300 if (!WillKeepAlive()) | |
| 301 g_browser_process->AddRefModule(); | |
| 302 ++g_keep_alive_count; | |
| 303 } | |
| 304 | |
| 305 void CloseAllBrowsersIfNeeded() { | |
| 306 // If there are no browsers open and we aren't already shutting down, | |
| 307 // initiate a shutdown. Also skips shutdown if this is a unit test. | |
| 308 // (MessageLoop::current() == null or explicitly disabled). | |
| 309 if (chrome::GetTotalBrowserCount() == 0 && | |
| 310 !browser_shutdown::IsTryingToQuit() && base::MessageLoop::current() && | |
| 311 !g_disable_shutdown_for_testing) { | |
| 312 CloseAllBrowsers(); | |
| 313 } | |
| 314 } | |
| 315 | |
| 316 void DecrementKeepAliveCount() { | |
| 317 DCHECK_GT(g_keep_alive_count, 0); | |
| 318 --g_keep_alive_count; | |
| 319 // Although we should have a browser process, if there is none, | |
| 320 // there is nothing to do. | |
| 321 if (!g_browser_process) return; | |
| 322 | |
| 323 // Allow the app to shutdown again. | |
| 324 if (!WillKeepAlive()) { | |
| 325 g_browser_process->ReleaseModule(); | |
| 326 CloseAllBrowsersIfNeeded(); | |
| 327 } | |
| 328 } | |
| 329 | |
| 330 bool WillKeepAlive() { | |
| 331 return g_keep_alive_count > 0; | |
| 332 } | |
| 333 | |
| 334 void NotifyAppTerminating() { | 295 void NotifyAppTerminating() { |
| 335 static bool notified = false; | 296 static bool notified = false; |
| 336 if (notified) | 297 if (notified) |
| 337 return; | 298 return; |
| 338 notified = true; | 299 notified = true; |
| 339 content::NotificationService::current()->Notify( | 300 content::NotificationService::current()->Notify( |
| 340 chrome::NOTIFICATION_APP_TERMINATING, | 301 chrome::NOTIFICATION_APP_TERMINATING, |
| 341 content::NotificationService::AllSources(), | 302 content::NotificationService::AllSources(), |
| 342 content::NotificationService::NoDetails()); | 303 content::NotificationService::NoDetails()); |
| 343 } | 304 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 // 2. If the desktop type of the browser going away is desktop and the ASH | 361 // 2. If the desktop type of the browser going away is desktop and the ASH |
| 401 // environment is still active. | 362 // environment is still active. |
| 402 if (browser->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_NATIVE) | 363 if (browser->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_NATIVE) |
| 403 return !ash::Shell::HasInstance(); | 364 return !ash::Shell::HasInstance(); |
| 404 else if (browser->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH) | 365 else if (browser->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH) |
| 405 return BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE)->empty(); | 366 return BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE)->empty(); |
| 406 #endif | 367 #endif |
| 407 return true; | 368 return true; |
| 408 } | 369 } |
| 409 | 370 |
| 410 void DisableShutdownForTesting(bool disable_shutdown_for_testing) { | |
| 411 g_disable_shutdown_for_testing = disable_shutdown_for_testing; | |
| 412 if (!g_disable_shutdown_for_testing && !WillKeepAlive()) | |
| 413 CloseAllBrowsersIfNeeded(); | |
| 414 } | |
| 415 | |
| 416 } // namespace chrome | 371 } // namespace chrome |
| OLD | NEW |