OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/lifetime/browser_keep_alive.h" |
| 6 |
| 7 #include "base/message_loop/message_loop.h" |
| 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/browser_shutdown.h" |
| 10 #include "chrome/browser/lifetime/application_lifetime.h" |
| 11 #include "chrome/browser/ui/browser_finder.h" |
| 12 |
| 13 namespace browser_lifetime { |
| 14 |
| 15 namespace { |
| 16 |
| 17 int g_keep_alive_count = 0; |
| 18 bool g_disable_shutdown_for_testing = false; |
| 19 |
| 20 void CloseAllBrowsersIfNeeded() { |
| 21 // If there are no browsers open and we aren't already shutting down, |
| 22 // initiate a shutdown. Also skips shutdown if this is a unit test. |
| 23 // (MessageLoop::current() == null or explicitly disabled). |
| 24 if (chrome::GetTotalBrowserCount() == 0 && |
| 25 !browser_shutdown::IsTryingToQuit() && |
| 26 base::MessageLoop::current() && |
| 27 !g_disable_shutdown_for_testing) { |
| 28 chrome::CloseAllBrowsers(); |
| 29 } |
| 30 } |
| 31 |
| 32 } // namespace |
| 33 |
| 34 ScopedKeepAlive::ScopedKeepAlive() { |
| 35 // Allow ScopedKeepAlive to be used in unit tests. |
| 36 if (g_browser_process) |
| 37 browser_lifetime::IncrementKeepAliveCount(); |
| 38 } |
| 39 |
| 40 ScopedKeepAlive::~ScopedKeepAlive() { |
| 41 if (g_browser_process) |
| 42 browser_lifetime::DecrementKeepAliveCount(); |
| 43 } |
| 44 |
| 45 void IncrementKeepAliveCount() { |
| 46 // Increment the browser process refcount as long as we're keeping the |
| 47 // application alive. |
| 48 if (!WillKeepAlive()) |
| 49 g_browser_process->AddRefModule(); |
| 50 ++g_keep_alive_count; |
| 51 } |
| 52 |
| 53 void DecrementKeepAliveCount() { |
| 54 DCHECK_GT(g_keep_alive_count, 0); |
| 55 --g_keep_alive_count; |
| 56 // Although we should have a browser process, if there is none, |
| 57 // there is nothing to do. |
| 58 if (!g_browser_process) |
| 59 return; |
| 60 |
| 61 // Allow the app to shutdown again. |
| 62 if (!WillKeepAlive()) { |
| 63 g_browser_process->ReleaseModule(); |
| 64 CloseAllBrowsersIfNeeded(); |
| 65 } |
| 66 } |
| 67 |
| 68 bool WillKeepAlive() { |
| 69 return g_keep_alive_count > 0; |
| 70 } |
| 71 |
| 72 void DisableShutdownForTesting(bool disable_shutdown_for_testing) { |
| 73 g_disable_shutdown_for_testing = disable_shutdown_for_testing; |
| 74 if (!g_disable_shutdown_for_testing && !WillKeepAlive()) |
| 75 CloseAllBrowsersIfNeeded(); |
| 76 } |
| 77 |
| 78 } // namespace browser_lifetime |
OLD | NEW |