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 |
| index 72628b32fe6c0e6759023bd957c3ae0c87e933df..7f540f6c46d67f75d82f3e3ed0355916883d06a3 100644 |
| --- a/chrome/browser/lifetime/keep_alive_registry.cc |
| +++ b/chrome/browser/lifetime/keep_alive_registry.cc |
| @@ -4,7 +4,9 @@ |
| #include "chrome/browser/lifetime/keep_alive_registry.h" |
| +#include "chrome/browser/browser_process.h" |
| #include "chrome/browser/lifetime/application_lifetime.h" |
| +#include "chrome/browser/lifetime/keep_alive_state_observer.h" |
| #include "chrome/browser/lifetime/keep_alive_types.h" |
| namespace { |
| @@ -17,6 +19,18 @@ void RemoveOne(keep_alive::Origin key, |
| container.erase(it); |
| } |
| +void HugBrowser() { |
|
Bernhard Bauer
2016/02/24 16:54:34
Awww... 😃
dgn
2016/02/26 14:34:29
Aaaand.. It's gone.
|
| + // TODO(dgn): We currently use the plain KeepAliveCount. We will integrate |
| + // that in this class progressively as mechanisms are merged. |
| + chrome::IncrementKeepAliveCount(); |
| +} |
| + |
| +void ReleaseBrowser() { |
| + // TODO(dgn): We currently use the plain KeepAliveCount. We will integrate |
| + // that in this class progressively as mechanisms are merged. |
| + chrome::DecrementKeepAliveCount(); |
| +} |
| + |
| } // namespace |
| //////////////////////////////////////////////////////////////////////////////// |
| @@ -27,29 +41,96 @@ KeepAliveRegistry* KeepAliveRegistry::GetInstance() { |
| return base::Singleton<KeepAliveRegistry>::get(); |
| } |
| +void KeepAliveRegistry::AddObserver(KeepAliveStateObserver* observer) { |
| + observers_.AddObserver(observer); |
| +} |
| + |
| +void KeepAliveRegistry::RemoveObserver(KeepAliveStateObserver* observer) { |
| + observers_.RemoveObserver(observer); |
| +} |
| + |
| //////////////////////////////////////////////////////////////////////////////// |
| // Private methods |
| -KeepAliveRegistry::KeepAliveRegistry() {} |
| +KeepAliveRegistry::KeepAliveRegistry() { |
| + state_ = {false, // is_keeping_alive |
| + keep_alive::RestartOption::DISABLED}; |
| +} |
| KeepAliveRegistry::~KeepAliveRegistry() { |
| DCHECK_EQ(0u, registered_keep_alives_.size()); |
| + DCHECK_EQ(0u, restart_allowed_keep_alives_.size()); |
| } |
| -void KeepAliveRegistry::Register(keep_alive::Origin origin) { |
| +KeepAliveRegistry::KeepAliveState KeepAliveRegistry::ComputeCurrentState() |
| + const { |
| + bool keeping_alive = registered_keep_alives_.size() > 0; |
| + bool restart_allowed = |
| + registered_keep_alives_.size() == restart_allowed_keep_alives_.size() && |
| + keeping_alive; |
|
Bernhard Bauer
2016/02/24 16:54:33
Hm... is restart really disallowed if nothing is k
dgn
2016/02/24 19:08:32
Hum true... The observer can always check WillKeep
|
| + |
| + return {keeping_alive, restart_allowed ? keep_alive::RestartOption::ENABLED |
| + : keep_alive::RestartOption::DISABLED}; |
| +} |
| + |
| +void KeepAliveRegistry::NotifyOfStateDifferences( |
| + const KeepAliveState& new_state) { |
| + if (state_.restart != new_state.restart) { |
| + if (new_state.restart == keep_alive::RestartOption::ENABLED) { |
| + VLOG(1) << "Notifying observers: Restart Allowed"; |
| + FOR_EACH_OBSERVER(KeepAliveStateObserver, observers_, OnRestartAllowed()); |
| + } else { |
| + VLOG(1) << "Notifying observers: Restart Forbidden"; |
| + FOR_EACH_OBSERVER(KeepAliveStateObserver, observers_, |
| + OnRestartForbidden()); |
| + } |
| + } |
| +} |
| + |
| +void KeepAliveRegistry::Register(keep_alive::Origin origin, |
| + keep_alive::RestartOption restart) { |
| registered_keep_alives_.insert(origin); |
| + if (restart == keep_alive::RestartOption::ENABLED) { |
|
Bernhard Bauer
2016/02/24 16:54:34
Nit: No braces.
dgn
2016/02/26 14:34:29
Removed.
|
| + restart_allowed_keep_alives_.insert(origin); |
| + } |
| - // TODO(dgn): We currently use the plain KeepAliveCount. We will integrate |
| - // that in this class progressively as mechanisms are merged. |
| - if (registered_keep_alives_.size() == 1) |
| - chrome::IncrementKeepAliveCount(); |
| + if (!state_.is_keeping_alive) |
| + HugBrowser(); |
| + |
| + KeepAliveState new_state = ComputeCurrentState(); |
| + NotifyOfStateDifferences(new_state); |
| + state_ = new_state; |
| + DumpRegistryIfDebug(); |
| } |
| -void KeepAliveRegistry::Unregister(keep_alive::Origin origin) { |
| +void KeepAliveRegistry::Unregister(keep_alive::Origin origin, |
| + keep_alive::RestartOption restart) { |
| RemoveOne(origin, registered_keep_alives_); |
| + if (restart == keep_alive::RestartOption::ENABLED) { |
|
Bernhard Bauer
2016/02/24 16:54:34
Nit: Braces are unnecessary here.
dgn
2016/02/26 14:34:29
Removed.
|
| + RemoveOne(origin, restart_allowed_keep_alives_); |
| + } |
| - // TODO(dgn): We currently use the plain KeepAliveCount. We will integrate |
| - // that in this class progressively as mechanisms are merged. |
| - if (registered_keep_alives_.size() == 0) |
| - chrome::DecrementKeepAliveCount(); |
| + KeepAliveState new_state = ComputeCurrentState(); |
| + NotifyOfStateDifferences(new_state); |
| + state_ = new_state; |
| + DumpRegistryIfDebug(); |
| + |
| + if (!state_.is_keeping_alive) |
| + ReleaseBrowser(); |
| +} |
| + |
| +void KeepAliveRegistry::DumpRegistryIfDebug() const { |
| +#ifndef NDEBUG |
| + VLOG(1) << "KeepAliveRegistry dump - total: {registered=" |
| + << registered_keep_alives_.size() |
| + << ", restart=" << restart_allowed_keep_alives_.size() << "}"; |
| + for (auto origin_it = registered_keep_alives_.begin(); |
| + origin_it != registered_keep_alives_.end(); |
| + origin_it = registered_keep_alives_.upper_bound(*origin_it)) { |
| + VLOG(1) << "- " << *origin_it |
| + << " {registered=" << registered_keep_alives_.count(*origin_it) |
| + << ", restart=" << restart_allowed_keep_alives_.count(*origin_it) |
| + << "}"; |
| + } |
| +#endif // ndef NDEBUG |
| } |