Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Side by Side Diff: chrome/browser/lifetime/application_lifetime.cc

Issue 1376063005: Cleanup: Pull some browser keep alive functions into its own file. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more fixes Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 74
75 // Check TabsNeedBeforeUnloadFired(). 75 // Check TabsNeedBeforeUnloadFired().
76 for (; !browser_it.done(); browser_it.Next()) { 76 for (; !browser_it.done(); browser_it.Next()) {
77 if (browser_it->TabsNeedBeforeUnloadFired()) 77 if (browser_it->TabsNeedBeforeUnloadFired())
78 return false; 78 return false;
79 } 79 }
80 return true; 80 return true;
81 } 81 }
82 #endif // !defined(OS_ANDROID) 82 #endif // !defined(OS_ANDROID)
83 83
84 int g_keep_alive_count = 0;
85 bool g_disable_shutdown_for_testing = false;
86
87 #if defined(OS_CHROMEOS) 84 #if defined(OS_CHROMEOS)
88 // Whether chrome should send stop request to a session manager. 85 // Whether chrome should send stop request to a session manager.
89 bool g_send_stop_request_to_session_manager = false; 86 bool g_send_stop_request_to_session_manager = false;
90 #endif 87 #endif
91 88
92 } // namespace 89 } // namespace
93 90
94 void MarkAsCleanShutdown() { 91 void MarkAsCleanShutdown() {
95 // TODO(beng): Can this use ProfileManager::GetLoadedProfiles() instead? 92 // TODO(beng): Can this use ProfileManager::GetLoadedProfiles() instead?
96 for (chrome::BrowserIterator it; !it.done(); it.Next()) 93 for (chrome::BrowserIterator it; !it.done(); it.Next())
(...skipping 18 matching lines...) Expand all
115 void CloseAllBrowsersAndQuit() { 112 void CloseAllBrowsersAndQuit() {
116 browser_shutdown::SetTryingToQuit(true); 113 browser_shutdown::SetTryingToQuit(true);
117 CloseAllBrowsers(); 114 CloseAllBrowsers();
118 } 115 }
119 116
120 void CloseAllBrowsers() { 117 void CloseAllBrowsers() {
121 // If there are no browsers and closing the last browser would quit the 118 // If there are no browsers and closing the last browser would quit the
122 // application, send the APP_TERMINATING action here. Otherwise, it will be 119 // application, send the APP_TERMINATING action here. Otherwise, it will be
123 // sent by RemoveBrowser() when the last browser has closed. 120 // sent by RemoveBrowser() when the last browser has closed.
124 if (chrome::GetTotalBrowserCount() == 0 && 121 if (chrome::GetTotalBrowserCount() == 0 &&
125 (browser_shutdown::IsTryingToQuit() || !chrome::WillKeepAlive())) { 122 (browser_shutdown::IsTryingToQuit() ||
123 !browser_lifetime::WillKeepAlive())) {
126 // Tell everyone that we are shutting down. 124 // Tell everyone that we are shutting down.
127 browser_shutdown::SetTryingToQuit(true); 125 browser_shutdown::SetTryingToQuit(true);
128 126
129 #if defined(ENABLE_SESSION_SERVICE) 127 #if defined(ENABLE_SESSION_SERVICE)
130 // If ShuttingDownWithoutClosingBrowsers() returns true, the session 128 // If ShuttingDownWithoutClosingBrowsers() returns true, the session
131 // services may not get a chance to shut down normally, so explicitly shut 129 // services may not get a chance to shut down normally, so explicitly shut
132 // them down here to ensure they have a chance to persist their data. 130 // them down here to ensure they have a chance to persist their data.
133 ProfileManager::ShutdownSessionServices(); 131 ProfileManager::ShutdownSessionServices();
134 #endif 132 #endif
135 133
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 funnel.RecordEvent(L"KillProcess"); 302 funnel.RecordEvent(L"KillProcess");
305 #endif 303 #endif
306 304
307 // On Windows 7 and later, the system will consider the process ripe for 305 // On Windows 7 and later, the system will consider the process ripe for
308 // termination as soon as it hides or destroys its windows. Since any 306 // termination as soon as it hides or destroys its windows. Since any
309 // execution past that point will be non-deterministically cut short, we 307 // execution past that point will be non-deterministically cut short, we
310 // might as well put ourselves out of that misery deterministically. 308 // might as well put ourselves out of that misery deterministically.
311 base::Process::Current().Terminate(0, false); 309 base::Process::Current().Terminate(0, false);
312 } 310 }
313 311
314 void IncrementKeepAliveCount() {
315 // Increment the browser process refcount as long as we're keeping the
316 // application alive.
317 if (!WillKeepAlive())
318 g_browser_process->AddRefModule();
319 ++g_keep_alive_count;
320 }
321
322 void CloseAllBrowsersIfNeeded() {
323 // If there are no browsers open and we aren't already shutting down,
324 // initiate a shutdown. Also skips shutdown if this is a unit test.
325 // (MessageLoop::current() == null or explicitly disabled).
326 if (chrome::GetTotalBrowserCount() == 0 &&
327 !browser_shutdown::IsTryingToQuit() && base::MessageLoop::current() &&
328 !g_disable_shutdown_for_testing) {
329 CloseAllBrowsers();
330 }
331 }
332
333 void DecrementKeepAliveCount() {
334 DCHECK_GT(g_keep_alive_count, 0);
335 --g_keep_alive_count;
336 // Although we should have a browser process, if there is none,
337 // there is nothing to do.
338 if (!g_browser_process) return;
339
340 // Allow the app to shutdown again.
341 if (!WillKeepAlive()) {
342 g_browser_process->ReleaseModule();
343 CloseAllBrowsersIfNeeded();
344 }
345 }
346
347 bool WillKeepAlive() {
348 return g_keep_alive_count > 0;
349 }
350
351 void NotifyAppTerminating() { 312 void NotifyAppTerminating() {
352 static bool notified = false; 313 static bool notified = false;
353 if (notified) 314 if (notified)
354 return; 315 return;
355 notified = true; 316 notified = true;
356 content::NotificationService::current()->Notify( 317 content::NotificationService::current()->Notify(
357 chrome::NOTIFICATION_APP_TERMINATING, 318 chrome::NOTIFICATION_APP_TERMINATING,
358 content::NotificationService::AllSources(), 319 content::NotificationService::AllSources(),
359 content::NotificationService::NoDetails()); 320 content::NotificationService::NoDetails());
360 } 321 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 // 2. If the desktop type of the browser going away is desktop and the ASH 378 // 2. If the desktop type of the browser going away is desktop and the ASH
418 // environment is still active. 379 // environment is still active.
419 if (browser->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_NATIVE) 380 if (browser->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_NATIVE)
420 return !ash::Shell::HasInstance(); 381 return !ash::Shell::HasInstance();
421 else if (browser->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH) 382 else if (browser->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH)
422 return BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE)->empty(); 383 return BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE)->empty();
423 #endif 384 #endif
424 return true; 385 return true;
425 } 386 }
426 387
427 void DisableShutdownForTesting(bool disable_shutdown_for_testing) {
428 g_disable_shutdown_for_testing = disable_shutdown_for_testing;
429 if (!g_disable_shutdown_for_testing && !WillKeepAlive())
430 CloseAllBrowsersIfNeeded();
431 }
432
433 } // namespace chrome 388 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698