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..569bfa28864024ad06578046bc109ded2e8432c5 |
--- /dev/null |
+++ b/chrome/browser/lifetime/keep_alive_registry.cc |
@@ -0,0 +1,56 @@ |
+// 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(); |
+ |
+ auto count_per_origin_it = registered_keep_alives_.find(origin); |
sky
2016/02/25 18:43:50
Is there a reason you converted away from the simp
dgn
2016/02/25 20:08:49
I wanted to be sure about the initial value in the
dgn
2016/02/26 11:51:59
Done.
|
+ if (count_per_origin_it == registered_keep_alives_.end()) |
+ registered_keep_alives_.insert(std::make_pair(origin, 1)); |
+ else |
+ ++count_per_origin_it->second; |
+ |
+ ++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(); |
+} |