OLD | NEW |
---|---|
(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/common/chrome_features.h" | |
16 #include "chrome/common/chrome_switches.h" | |
17 | |
18 // static | |
19 std::unique_ptr<BackgroundModeOptimizer> BackgroundModeOptimizer::Create() { | |
20 // If the -keep-alive-for-test flag is passed, then always keep chrome running | |
21 // in the background until the user explicitly terminates it. | |
22 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
23 switches::kKeepAliveForTest)) | |
24 return std::unique_ptr<BackgroundModeOptimizer>(); | |
sky
2016/05/18 19:23:06
nit: return nullptr here and 31.
dgn
2016/06/20 17:45:18
Done.
| |
25 | |
26 #if defined(OS_WIN) || defined(OS_LINUX) | |
27 if (base::FeatureList::IsEnabled(features::kBackgroundModeAllowRestart)) | |
28 return base::WrapUnique(new BackgroundModeOptimizer()); | |
29 #endif // defined(OS_WIN) || defined(OS_LINUX) | |
30 | |
31 return std::unique_ptr<BackgroundModeOptimizer>(); | |
32 } | |
33 | |
34 BackgroundModeOptimizer::BackgroundModeOptimizer() { | |
35 KeepAliveRegistry::GetInstance()->AddObserver(this); | |
36 } | |
37 | |
38 BackgroundModeOptimizer::~BackgroundModeOptimizer() { | |
39 KeepAliveRegistry::GetInstance()->RemoveObserver(this); | |
40 } | |
41 | |
42 /////////////////////////////////////////////////////////////////////////////// | |
43 // KeepAliveRegistry implementation | |
44 | |
45 void BackgroundModeOptimizer::OnKeepAliveStateChanged(bool is_keeping_alive) { | |
46 // Nothing to do | |
47 } | |
48 | |
49 void BackgroundModeOptimizer::OnKeepAliveRestartStateChanged(bool can_restart) { | |
50 DVLOG(1) << "KeepAliveRestartStateChanged: " | |
51 << (can_restart ? "Attempting to restart..." : "Nope"); | |
52 if (can_restart) | |
53 TryBrowserRestart(); | |
54 } | |
55 | |
56 /////////////////////////////////////////////////////////////////////////////// | |
57 // private methods | |
58 | |
59 void BackgroundModeOptimizer::TryBrowserRestart() { | |
60 // If the application is already shutting down, do not turn it into a restart. | |
61 if (browser_shutdown::IsTryingToQuit() || | |
62 browser_shutdown::GetShutdownType() != browser_shutdown::NOT_VALID) | |
63 return; | |
64 | |
65 browser_shutdown::SetShouldRestartInBackground(true); | |
66 chrome::AttemptRestart(); | |
67 g_browser_process->background_mode_manager()->EndBackgroundMode(); | |
sky
2016/05/18 19:23:06
If the restart attempt fails, is the background mo
dgn
2016/06/20 17:45:18
The restart specific flags are set unconditionally
sky
2016/06/20 19:31:55
I think you should make AttemptRestart return a st
| |
68 } | |
OLD | NEW |