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

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

Issue 1086733002: Ensure tests have an active task runner (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased. Created 5 years, 8 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"
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
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; 84 int g_keep_alive_count = 0;
85 bool g_disable_shutdown_for_testing = false;
85 86
86 #if defined(OS_CHROMEOS) 87 #if defined(OS_CHROMEOS)
87 // Whether chrome should send stop request to a session manager. 88 // Whether chrome should send stop request to a session manager.
88 bool g_send_stop_request_to_session_manager = false; 89 bool g_send_stop_request_to_session_manager = false;
89 #endif 90 #endif
90 91
91 } // namespace 92 } // namespace
92 93
93 void MarkAsCleanShutdown() { 94 void MarkAsCleanShutdown() {
94 // TODO(beng): Can this use ProfileManager::GetLoadedProfiles() instead? 95 // TODO(beng): Can this use ProfileManager::GetLoadedProfiles() instead?
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 } 313 }
313 314
314 void IncrementKeepAliveCount() { 315 void IncrementKeepAliveCount() {
315 // Increment the browser process refcount as long as we're keeping the 316 // Increment the browser process refcount as long as we're keeping the
316 // application alive. 317 // application alive.
317 if (!WillKeepAlive()) 318 if (!WillKeepAlive())
318 g_browser_process->AddRefModule(); 319 g_browser_process->AddRefModule();
319 ++g_keep_alive_count; 320 ++g_keep_alive_count;
320 } 321 }
321 322
323 void CloseAllBrowsersIfNeeded() {
324 // If there are no browsers open and we aren't already shutting down,
325 // initiate a shutdown. Also skips shutdown if this is a unit test.
326 // (MessageLoop::current() == null or explicitly disabled).
327 if (chrome::GetTotalBrowserCount() == 0 &&
328 !browser_shutdown::IsTryingToQuit() && base::MessageLoop::current() &&
329 !g_disable_shutdown_for_testing) {
330 CloseAllBrowsers();
331 }
332 }
333
322 void DecrementKeepAliveCount() { 334 void DecrementKeepAliveCount() {
323 DCHECK_GT(g_keep_alive_count, 0); 335 DCHECK_GT(g_keep_alive_count, 0);
324 --g_keep_alive_count; 336 --g_keep_alive_count;
325
326 DCHECK(g_browser_process);
327 // Although we should have a browser process, if there is none, 337 // Although we should have a browser process, if there is none,
328 // there is nothing to do. 338 // there is nothing to do.
329 if (!g_browser_process) return; 339 if (!g_browser_process) return;
330 340
331 // Allow the app to shutdown again. 341 // Allow the app to shutdown again.
332 if (!WillKeepAlive()) { 342 if (!WillKeepAlive()) {
333 g_browser_process->ReleaseModule(); 343 g_browser_process->ReleaseModule();
334 // If there are no browsers open and we aren't already shutting down, 344 CloseAllBrowsersIfNeeded();
335 // initiate a shutdown. Also skips shutdown if this is a unit test
336 // (MessageLoop::current() == null).
337 if (chrome::GetTotalBrowserCount() == 0 &&
338 !browser_shutdown::IsTryingToQuit() &&
339 base::MessageLoop::current()) {
340 CloseAllBrowsers();
341 }
342 } 345 }
343 } 346 }
344 347
345 bool WillKeepAlive() { 348 bool WillKeepAlive() {
346 return g_keep_alive_count > 0; 349 return g_keep_alive_count > 0;
347 } 350 }
348 351
349 void NotifyAppTerminating() { 352 void NotifyAppTerminating() {
350 static bool notified = false; 353 static bool notified = false;
351 if (notified) 354 if (notified)
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 // 2. If the desktop type of the browser going away is desktop and the ASH 418 // 2. If the desktop type of the browser going away is desktop and the ASH
416 // environment is still active. 419 // environment is still active.
417 if (browser->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_NATIVE) 420 if (browser->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_NATIVE)
418 return !ash::Shell::HasInstance(); 421 return !ash::Shell::HasInstance();
419 else if (browser->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH) 422 else if (browser->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH)
420 return BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE)->empty(); 423 return BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE)->empty();
421 #endif 424 #endif
422 return true; 425 return true;
423 } 426 }
424 427
428 void DisableShutdownForTesting(bool disable_shutdown_for_testing) {
429 g_disable_shutdown_for_testing = disable_shutdown_for_testing;
430 if (!g_disable_shutdown_for_testing && !WillKeepAlive())
431 CloseAllBrowsersIfNeeded();
432 }
433
425 } // namespace chrome 434 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/lifetime/application_lifetime.h ('k') | chrome/browser/media/webrtc_log_uploader_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698