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

Unified Diff: chrome/browser/lifetime/keep_alive_registry.cc

Issue 1725883002: Add KeepAliveStateObserver, add the Restart option (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@KeepAlive
Patch Set: 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
index 08c6c78e146c5b980e94bb2e7299252a6985fbd4..061f074db4a5a05a7d02e51f017be59236b296df 100644
--- a/chrome/browser/lifetime/keep_alive_registry.cc
+++ b/chrome/browser/lifetime/keep_alive_registry.cc
@@ -5,6 +5,7 @@
#include "chrome/browser/lifetime/keep_alive_registry.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"
////////////////////////////////////////////////////////////////////////////////
@@ -15,29 +16,62 @@ KeepAliveRegistry* KeepAliveRegistry::GetInstance() {
return base::Singleton<KeepAliveRegistry>::get();
}
-bool KeepAliveRegistry::WillKeepAlive() const {
+bool KeepAliveRegistry::IsKeepingAlive() const {
return registered_count_ > 0;
}
+bool KeepAliveRegistry::IsRestartAllowed() const {
+ return registered_count_ == restart_allowed_count_;
+}
+
+void KeepAliveRegistry::AddObserver(KeepAliveStateObserver* observer) {
+ observers_.AddObserver(observer);
+}
+
+void KeepAliveRegistry::RemoveObserver(KeepAliveStateObserver* observer) {
+ observers_.RemoveObserver(observer);
+}
+
////////////////////////////////////////////////////////////////////////////////
// Private methods
-KeepAliveRegistry::KeepAliveRegistry() : registered_count_(0) {}
+KeepAliveRegistry::KeepAliveRegistry()
+ : registered_count_(0), restart_allowed_count_(0) {}
KeepAliveRegistry::~KeepAliveRegistry() {
DCHECK_EQ(0, registered_count_);
DCHECK_EQ(0u, registered_keep_alives_.size());
+ DCHECK_EQ(0, restart_allowed_count_);
}
-void KeepAliveRegistry::Register(KeepAliveOrigin origin) {
- if (!WillKeepAlive())
- chrome::IncrementKeepAliveCount();
+void KeepAliveRegistry::Register(KeepAliveOrigin origin,
+ KeepAliveRestartOption restart) {
+ bool old_keeping_alive = IsKeepingAlive();
sky 2016/02/26 22:13:25 49-50 and 58-65 are duplicated in UnRegister. How
dgn 2016/03/07 17:17:14 I considered something similar before (see up to p
sky 2016/03/09 18:29:35 Ok. Keep what you have here.
dgn 2016/03/09 18:37:04 Acknowledged.
+ bool old_restart_allowed = IsRestartAllowed();
++registered_keep_alives_[origin];
++registered_count_;
+
+ if (restart == KeepAliveRestartOption::ENABLED)
+ ++restart_allowed_count_;
+
+ bool new_keeping_alive = IsKeepingAlive();
+ bool new_restart_allowed = IsRestartAllowed();
+
+ if (new_keeping_alive != old_keeping_alive)
+ OnKeepingAliveChanged(new_keeping_alive);
+
+ if (new_restart_allowed != old_restart_allowed)
+ OnRestartAllowedChanged(new_restart_allowed);
+
+ DVLOG(1) << "New state of the KeepAliveRegistry: " << *this;
}
-void KeepAliveRegistry::Unregister(KeepAliveOrigin origin) {
+void KeepAliveRegistry::Unregister(KeepAliveOrigin origin,
+ KeepAliveRestartOption restart) {
+ bool old_keeping_alive = IsKeepingAlive();
+ bool old_restart_allowed = IsRestartAllowed();
+
--registered_count_;
DCHECK_GE(registered_count_, 0);
@@ -46,6 +80,52 @@ void KeepAliveRegistry::Unregister(KeepAliveOrigin origin) {
if (new_count == 0)
registered_keep_alives_.erase(origin);
- if (!WillKeepAlive())
+ if (restart == KeepAliveRestartOption::ENABLED)
+ --restart_allowed_count_;
+
+ bool new_keeping_alive = IsKeepingAlive();
+ bool new_restart_allowed = IsRestartAllowed();
+
+ if (new_restart_allowed != old_restart_allowed)
+ OnRestartAllowedChanged(new_restart_allowed);
+
+ if (new_keeping_alive != old_keeping_alive)
+ OnKeepingAliveChanged(new_keeping_alive);
+
+ DVLOG(1) << "New state of the KeepAliveRegistry: " << *this;
+}
+
+
+void KeepAliveRegistry::OnKeepingAliveChanged(bool new_keeping_alive) {
+ if (new_keeping_alive) {
+ DVLOG(1) << "KeepAliveRegistry is now keeping the browser alive.";
+ chrome::IncrementKeepAliveCount();
+ } else {
+ DVLOG(1) << "KeepAliveRegistry stopped keeping the browser alive.";
chrome::DecrementKeepAliveCount();
+ }
+}
+void KeepAliveRegistry::OnRestartAllowedChanged(bool new_restart_allowed) {
+ if (new_restart_allowed) {
+ DVLOG(1) << "Notifying KeepAliveStateObservers: Restart Allowed";
+ FOR_EACH_OBSERVER(KeepAliveStateObserver, observers_, OnRestartAllowed());
+ } else {
+ DVLOG(1) << "Notifying KeepAliveStateObservers: Restart Forbidden";
+ FOR_EACH_OBSERVER(KeepAliveStateObserver, observers_, OnRestartForbidden());
+ }
+}
+
+#ifndef NDEBUG
+std::ostream& operator<<(std::ostream& out, const KeepAliveRegistry& registry) {
+ out << "{KeepingAlive=" << registry.IsKeepingAlive() << ", RestartAllowed="
+ << registry.IsRestartAllowed() << ", KeepAlives=[";
+ for (auto counts_per_origin_it : registry.registered_keep_alives_) {
+ if (counts_per_origin_it != *registry.registered_keep_alives_.begin())
+ out << ", ";
+ out << counts_per_origin_it.first << " (" << counts_per_origin_it.second
+ << ")";
+ }
+ out << "]}";
+ return out;
}
+#endif // ndef NDEBUG

Powered by Google App Engine
This is Rietveld 408576698