Index: chrome/browser/background/background_mode_optimizer.cc |
diff --git a/chrome/browser/background/background_mode_optimizer.cc b/chrome/browser/background/background_mode_optimizer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bcb552e256b480f14087e6bcba7ac6f23f98ef37 |
--- /dev/null |
+++ b/chrome/browser/background/background_mode_optimizer.cc |
@@ -0,0 +1,68 @@ |
+// Copyright 2016 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/background/background_mode_optimizer.h" |
+ |
+#include "base/command_line.h" |
+#include "base/feature_list.h" |
+#include "base/memory/ptr_util.h" |
+#include "chrome/browser/background/background_mode_manager.h" |
+#include "chrome/browser/browser_process.h" |
+#include "chrome/browser/browser_shutdown.h" |
+#include "chrome/browser/lifetime/application_lifetime.h" |
+#include "chrome/browser/lifetime/keep_alive_registry.h" |
+#include "chrome/common/chrome_features.h" |
+#include "chrome/common/chrome_switches.h" |
+ |
+// static |
+std::unique_ptr<BackgroundModeOptimizer> BackgroundModeOptimizer::Create() { |
+ // If the -keep-alive-for-test flag is passed, then always keep chrome running |
+ // in the background until the user explicitly terminates it. |
+ if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
+ switches::kKeepAliveForTest)) |
+ 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.
|
+ |
+#if defined(OS_WIN) || defined(OS_LINUX) |
+ if (base::FeatureList::IsEnabled(features::kBackgroundModeAllowRestart)) |
+ return base::WrapUnique(new BackgroundModeOptimizer()); |
+#endif // defined(OS_WIN) || defined(OS_LINUX) |
+ |
+ return std::unique_ptr<BackgroundModeOptimizer>(); |
+} |
+ |
+BackgroundModeOptimizer::BackgroundModeOptimizer() { |
+ KeepAliveRegistry::GetInstance()->AddObserver(this); |
+} |
+ |
+BackgroundModeOptimizer::~BackgroundModeOptimizer() { |
+ KeepAliveRegistry::GetInstance()->RemoveObserver(this); |
+} |
+ |
+/////////////////////////////////////////////////////////////////////////////// |
+// KeepAliveRegistry implementation |
+ |
+void BackgroundModeOptimizer::OnKeepAliveStateChanged(bool is_keeping_alive) { |
+ // Nothing to do |
+} |
+ |
+void BackgroundModeOptimizer::OnKeepAliveRestartStateChanged(bool can_restart) { |
+ DVLOG(1) << "KeepAliveRestartStateChanged: " |
+ << (can_restart ? "Attempting to restart..." : "Nope"); |
+ if (can_restart) |
+ TryBrowserRestart(); |
+} |
+ |
+/////////////////////////////////////////////////////////////////////////////// |
+// private methods |
+ |
+void BackgroundModeOptimizer::TryBrowserRestart() { |
+ // If the application is already shutting down, do not turn it into a restart. |
+ if (browser_shutdown::IsTryingToQuit() || |
+ browser_shutdown::GetShutdownType() != browser_shutdown::NOT_VALID) |
+ return; |
+ |
+ browser_shutdown::SetShouldRestartInBackground(true); |
+ chrome::AttemptRestart(); |
+ 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
|
+} |