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

Side by Side Diff: chrome/browser/background/background_mode_optimizer.cc

Issue 1931503002: Add BackgroundModeOptimizer that can restart the browser (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@PushKeepAlive
Patch Set: cleanup, enable by default (that's just to verify the tests) Created 4 years, 5 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 2016 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/background/background_mode_optimizer.h"
6
7 #include "base/command_line.h"
8 #include "base/feature_list.h"
9 #include "base/memory/ptr_util.h"
10 #include "chrome/browser/background/background_mode_manager.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/browser_shutdown.h"
13 #include "chrome/browser/lifetime/application_lifetime.h"
14 #include "chrome/browser/lifetime/keep_alive_registry.h"
15 #include "chrome/browser/ui/browser_list.h"
16 #include "chrome/common/chrome_features.h"
17 #include "chrome/common/chrome_switches.h"
18
19 // static
sky 2016/07/06 16:55:33 nit: make order match header.
dgn 2016/07/22 15:18:09 Done.
20 std::unique_ptr<BackgroundModeOptimizer> BackgroundModeOptimizer::Create() {
21 // If the -keep-alive-for-test flag is passed, then always keep chrome running
22 // in the background until the user explicitly terminates it.
23 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
24 switches::kKeepAliveForTest))
25 return nullptr;
26
27 #if defined(OS_WIN) || defined(OS_LINUX)
28 if (base::FeatureList::IsEnabled(features::kBackgroundModeAllowRestart))
29 return base::WrapUnique(new BackgroundModeOptimizer());
30 #endif // defined(OS_WIN) || defined(OS_LINUX)
31
32 return nullptr;
33 }
34
35 BackgroundModeOptimizer::BackgroundModeOptimizer() : browser_was_added_(false) {
36 KeepAliveRegistry::GetInstance()->AddObserver(this);
37 BrowserList::AddObserver(this);
38 }
39
40 BackgroundModeOptimizer::~BackgroundModeOptimizer() {
41 KeepAliveRegistry::GetInstance()->RemoveObserver(this);
42 BrowserList::RemoveObserver(this);
43 }
44
45 ///////////////////////////////////////////////////////////////////////////////
46 // KeepAliveRegistry implementation
47
48 void BackgroundModeOptimizer::OnKeepAliveStateChanged(bool is_keeping_alive) {
49 // Nothing to do
50 }
51
52 void BackgroundModeOptimizer::OnKeepAliveRestartStateChanged(bool can_restart) {
53 if (can_restart)
54 TryBrowserRestart();
55 }
56 ///////////////////////////////////////////////////////////////////////////////
57 // BrowserListObserver implementation
58
59 void BackgroundModeOptimizer::OnBrowserAdded(Browser* browser) {
60 browser_was_added_ = true;
61 }
62
63 ///////////////////////////////////////////////////////////////////////////////
64 // private methods
65
66 void BackgroundModeOptimizer::TryBrowserRestart() {
67 // Avoid unecessary restarts. Whether a browser window has been shown is
68 // our current heuristic to determine if it's worth it.
69 if (!browser_was_added_) {
70 DVLOG(1) << "TryBrowserRestart: Cancelled because no browser was added "
71 << "since the last restart";
72 return;
73 }
74
75 // If the application is already shutting down, do not turn it into a restart.
76 if (browser_shutdown::IsTryingToQuit() ||
77 browser_shutdown::GetShutdownType() != browser_shutdown::NOT_VALID) {
78 DVLOG(1) << "TryBrowserRestart: Cancelled because we are shutting down.";
79 return;
80 }
81
82 DVLOG(1) << "TryBrowserRestart: Making restart attempt.";
83
84 BackgroundModeManager::SetShouldRestartInBackground(true);
85 chrome::AttemptRestart();
86 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698