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

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: Remove KeepAliveOrigin::TEST 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..08c6c78e146c5b980e94bb2e7299252a6985fbd4
--- /dev/null
+++ b/chrome/browser/lifetime/keep_alive_registry.cc
@@ -0,0 +1,51 @@
+// 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"
+
+////////////////////////////////////////////////////////////////////////////////
+// Public methods
+
+// static
+KeepAliveRegistry* KeepAliveRegistry::GetInstance() {
+ return base::Singleton<KeepAliveRegistry>::get();
+}
+
+bool KeepAliveRegistry::WillKeepAlive() const {
+ return registered_count_ > 0;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// Private methods
+
+KeepAliveRegistry::KeepAliveRegistry() : registered_count_(0) {}
+
+KeepAliveRegistry::~KeepAliveRegistry() {
+ DCHECK_EQ(0, registered_count_);
+ DCHECK_EQ(0u, registered_keep_alives_.size());
+}
+
+void KeepAliveRegistry::Register(KeepAliveOrigin origin) {
+ if (!WillKeepAlive())
+ chrome::IncrementKeepAliveCount();
+
+ ++registered_keep_alives_[origin];
+ ++registered_count_;
+}
+
+void KeepAliveRegistry::Unregister(KeepAliveOrigin origin) {
+ --registered_count_;
+ DCHECK_GE(registered_count_, 0);
+
+ int new_count = --registered_keep_alives_[origin];
+ DCHECK_GE(registered_keep_alives_[origin], 0);
+ if (new_count == 0)
+ registered_keep_alives_.erase(origin);
+
+ if (!WillKeepAlive())
+ chrome::DecrementKeepAliveCount();
+}
« no previous file with comments | « chrome/browser/lifetime/keep_alive_registry.h ('k') | chrome/browser/lifetime/keep_alive_registry_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698