Index: chrome/browser/lifetime/keep_alive_registry.cc |
diff --git a/chrome/browser/lifetime/keep_alive_registry.cc b/chrome/browser/lifetime/keep_alive_registry.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..69256d76ad6eddcb839d39fb6cf80cb2b11ddbb3 |
--- /dev/null |
+++ b/chrome/browser/lifetime/keep_alive_registry.cc |
@@ -0,0 +1,73 @@ |
+// 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/lifetime/keep_alive_registry.h" |
+ |
+#include "chrome/browser/lifetime/application_lifetime.h" |
+#include "chrome/browser/lifetime/keep_alive_types.h" |
+ |
+namespace { |
+ |
+KeepAliveRegistry* g_test_instance = nullptr; |
+ |
+} // namespace |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// Public methods |
+ |
+// static |
+KeepAliveRegistry* KeepAliveRegistry::GetInstance() { |
+ if (g_test_instance) |
+ return g_test_instance; |
+ |
+ return base::Singleton<KeepAliveRegistry>::get(); |
+} |
+ |
+bool KeepAliveRegistry::WillKeepAlive() const { |
+ return registered_count_ > 0; |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// Private methods |
+ |
+KeepAliveRegistry::KeepAliveRegistry() {} |
+ |
+KeepAliveRegistry::~KeepAliveRegistry() { |
+ DCHECK_EQ(0, registered_count_); |
+} |
+ |
+// static |
+void KeepAliveRegistry::SetInstanceForTesting(KeepAliveRegistry* new_instance) { |
+ g_test_instance = new_instance; |
+} |
+ |
+void KeepAliveRegistry::Register(KeepAliveOrigin origin) { |
+ if (!WillKeepAlive()) |
+ HugBrowser(); |
+ |
+ ++registered_keep_alives_[origin]; |
+ ++registered_count_; |
+} |
+ |
+void KeepAliveRegistry::Unregister(KeepAliveOrigin origin) { |
+ --registered_count_; |
+ --registered_keep_alives_[origin]; |
sky
2016/02/25 00:25:18
remove from rendered_keep_alives if count == 0
dgn
2016/02/25 17:19:46
Done.
|
+ DCHECK_GE(registered_count_, 0); |
+ DCHECK_GE(registered_keep_alives_[origin], 0); |
+ |
+ if (!WillKeepAlive()) |
+ ReleaseBrowser(); |
+} |
+ |
+void KeepAliveRegistry::HugBrowser() { |
+ // TODO(dgn): We currently use the plain KeepAliveCount. We will integrate |
+ // that in this class progressively as mechanisms are merged. |
+ chrome::IncrementKeepAliveCount(); |
+} |
+ |
+void KeepAliveRegistry::ReleaseBrowser() { |
+ // TODO(dgn): We currently use the plain KeepAliveCount. We will integrate |
+ // that in this class progressively as mechanisms are merged. |
+ chrome::DecrementKeepAliveCount(); |
+} |