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

Unified Diff: chrome/browser/lifetime/browser_keep_alive.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: fix build Created 5 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/lifetime/browser_keep_alive.cc
diff --git a/chrome/browser/lifetime/browser_keep_alive.cc b/chrome/browser/lifetime/browser_keep_alive.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6810f10e9f5d508d54f9d2ab9dea752c35c36a24
--- /dev/null
+++ b/chrome/browser/lifetime/browser_keep_alive.cc
@@ -0,0 +1,67 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/lifetime/browser_keep_alive.h"
+
+#include "base/message_loop/message_loop.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/browser_shutdown.h"
+#include "chrome/browser/lifetime/application_lifetime.h"
+#include "chrome/browser/ui/browser_finder.h"
+
+namespace browser_lifetime {
+
+namespace {
+
+int g_keep_alive_count = 0;
+bool g_disable_shutdown_for_testing = false;
+
+void CloseAllBrowsersIfNeeded() {
+ // If there are no browsers open and we aren't already shutting down,
+ // initiate a shutdown. Also skips shutdown if this is a unit test.
+ // (MessageLoop::current() == null or explicitly disabled).
+ if (chrome::GetTotalBrowserCount() == 0 &&
+ !browser_shutdown::IsTryingToQuit() &&
+ base::MessageLoop::current() &&
+ !g_disable_shutdown_for_testing) {
+ chrome::CloseAllBrowsers();
+ }
+}
+
+} // namespace
+
+void IncrementKeepAliveCount() {
+ // Increment the browser process refcount as long as we're keeping the
+ // application alive.
+ if (!WillKeepAlive())
+ g_browser_process->AddRefModule();
+ ++g_keep_alive_count;
+}
+
+void DecrementKeepAliveCount() {
+ DCHECK_GT(g_keep_alive_count, 0);
+ --g_keep_alive_count;
+ // Although we should have a browser process, if there is none,
+ // there is nothing to do.
+ if (!g_browser_process)
+ return;
+
+ // Allow the app to shutdown again.
+ if (!WillKeepAlive()) {
+ g_browser_process->ReleaseModule();
+ CloseAllBrowsersIfNeeded();
+ }
+}
+
+bool WillKeepAlive() {
+ return g_keep_alive_count > 0;
+}
+
+void DisableShutdownForTesting(bool disable_shutdown_for_testing) {
+ g_disable_shutdown_for_testing = disable_shutdown_for_testing;
+ if (!g_disable_shutdown_for_testing && !WillKeepAlive())
+ CloseAllBrowsersIfNeeded();
+}
+
+} // namespace browser_lifetime

Powered by Google App Engine
This is Rietveld 408576698