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

Unified Diff: chrome/browser/lifetime/keep_alive_registry.cc

Issue 1708343002: Add ScopedKeepAlive to c/b/lifetime (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use a map and counts Created 4 years, 10 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 side-by-side diff with in-line comments
Download patch
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();
+}

Powered by Google App Engine
This is Rietveld 408576698