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

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 singleton instead of browserprocess 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..29849f633e4f93fcd1bcf519e3fb3247a47fb1c6
--- /dev/null
+++ b/chrome/browser/lifetime/keep_alive_registry.cc
@@ -0,0 +1,61 @@
+// 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/browser_process.h"
+#include "chrome/browser/lifetime/application_lifetime.h"
+
+namespace {
+// Helper function to remove one token from the multisets.
Bernhard Bauer 2016/02/19 17:39:28 Empty line after opening the namespace.
dgn 2016/02/19 18:04:28 Done.
+void RemoveOne(const std::string& key, std::multiset<std::string>& container) {
+ auto it = container.find(key);
+ if (it != container.end())
Bernhard Bauer 2016/02/19 17:39:28 I would DCHECK this -- it shouldn't be possible to
dgn 2016/02/19 18:04:28 Done.
+ container.erase(it);
+}
+}
Bernhard Bauer 2016/02/19 17:39:28 // namespace
dgn 2016/02/19 18:04:28 Done.
+
+// static
+KeepAliveRegistry* KeepAliveRegistry::GetInstance() {
+ return base::Singleton<KeepAliveRegistry>::get();
+}
+
+KeepAliveRegistry::KeepAliveRegistry() {}
+
+KeepAliveRegistry::~KeepAliveRegistry() {
+ DCHECK_EQ(registered_tokens_.size(), 0u);
Bernhard Bauer 2016/02/19 17:39:28 Put the expected value first for nicer error messa
dgn 2016/02/19 18:04:28 Done.
+ DCHECK_EQ(reset_allowed_tokens_.size(), 0u);
+}
+
+void KeepAliveRegistry::RegisterToken(const std::string& token,
+ OptimizationOptions opt) {
+ DCHECK(!token.empty());
+
+ registered_tokens_.insert(token);
+
+ if (opt == OptimizationOptions::RESTART_ALLOWED)
+ reset_allowed_tokens_.insert(token);
+
+ // TODO(dgn): If this is the first non RESTART_ALLOWED token, we are now busy.
+
+ // TODO(dgn): We currently use the plain KeepAliveCount. We will integrate
+ // that in this class progressively as mechanisms are merged.
+ if (registered_tokens_.size() == 1)
+ chrome::IncrementKeepAliveCount();
+}
+
+void KeepAliveRegistry::UnregisterToken(const std::string& token,
+ OptimizationOptions opt) {
+ RemoveOne(token, registered_tokens_);
+
+ if (opt == OptimizationOptions::RESTART_ALLOWED)
+ RemoveOne(token, reset_allowed_tokens_);
+
+ // TODO(dgn): If this is the last non RESTART_ALLOWED token, we are now idle.
+
+ // TODO(dgn): We currently use the plain KeepAliveCount. We will integrate
+ // that in this class progressively as mechanisms are merged.
+ if (registered_tokens_.size() == 0)
+ chrome::DecrementKeepAliveCount();
+}

Powered by Google App Engine
This is Rietveld 408576698