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

Side by Side 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, 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
(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 void IncrementKeepAliveCount() {
35 // Increment the browser process refcount as long as we're keeping the
36 // application alive.
37 if (!WillKeepAlive())
38 g_browser_process->AddRefModule();
39 ++g_keep_alive_count;
40 }
41
42 void DecrementKeepAliveCount() {
43 DCHECK_GT(g_keep_alive_count, 0);
44 --g_keep_alive_count;
45 // Although we should have a browser process, if there is none,
46 // there is nothing to do.
47 if (!g_browser_process)
48 return;
49
50 // Allow the app to shutdown again.
51 if (!WillKeepAlive()) {
52 g_browser_process->ReleaseModule();
53 CloseAllBrowsersIfNeeded();
54 }
55 }
56
57 bool WillKeepAlive() {
58 return g_keep_alive_count > 0;
59 }
60
61 void DisableShutdownForTesting(bool disable_shutdown_for_testing) {
62 g_disable_shutdown_for_testing = disable_shutdown_for_testing;
63 if (!g_disable_shutdown_for_testing && !WillKeepAlive())
64 CloseAllBrowsersIfNeeded();
65 }
66
67 } // namespace browser_lifetime
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698