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

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: address comments. disable feature by default 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 BackgroundModeOptimizer::~BackgroundModeOptimizer() {
20 KeepAliveRegistry::GetInstance()->RemoveObserver(this);
21 BrowserList::RemoveObserver(this);
22 }
23
24 // static
25 std::unique_ptr<BackgroundModeOptimizer> BackgroundModeOptimizer::Create() {
26 // If the -keep-alive-for-test flag is passed, then always keep chrome running
27 // in the background until the user explicitly terminates it.
28 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
29 switches::kKeepAliveForTest))
30 return nullptr;
31
32 #if defined(OS_WIN) || defined(OS_LINUX)
33 if (base::FeatureList::IsEnabled(features::kBackgroundModeAllowRestart))
34 return base::WrapUnique(new BackgroundModeOptimizer());
35 #endif // defined(OS_WIN) || defined(OS_LINUX)
36
37 return nullptr;
38 }
39
40 ///////////////////////////////////////////////////////////////////////////////
41 // KeepAliveRegistry implementation
42
43 void BackgroundModeOptimizer::OnKeepAliveStateChanged(bool is_keeping_alive) {
44 // Nothing to do
45 }
46
47 void BackgroundModeOptimizer::OnKeepAliveRestartStateChanged(bool can_restart) {
48 if (can_restart)
49 TryBrowserRestart();
50 }
51 ///////////////////////////////////////////////////////////////////////////////
52 // BrowserListObserver implementation
53
54 void BackgroundModeOptimizer::OnBrowserAdded(Browser* browser) {
55 browser_was_added_ = true;
56 }
57
58 ///////////////////////////////////////////////////////////////////////////////
59 // private methods
60
61 BackgroundModeOptimizer::BackgroundModeOptimizer() : browser_was_added_(false) {
62 KeepAliveRegistry::GetInstance()->AddObserver(this);
63 BrowserList::AddObserver(this);
64 }
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: Restarting.";
83 DoRestart();
84 }
85
86 void BackgroundModeOptimizer::DoRestart() {
87 BackgroundModeManager::set_should_restart_in_background(true);
88 chrome::AttemptRestart();
89 }
OLDNEW
« no previous file with comments | « chrome/browser/background/background_mode_optimizer.h ('k') | chrome/browser/background/background_mode_optimizer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698