Chromium Code Reviews| 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(); |
| +} |