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

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 670954535468a866105cdd12abcee8a44d2188f8..e0c07cccf076d4d4fab6858b6fe2b9ac2552c5e6 100644
--- a/chrome/browser/lifetime/keep_alive_registry.cc
+++ b/chrome/browser/lifetime/keep_alive_registry.cc
@@ -7,6 +7,7 @@
#include "chrome/browser/browser_process.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/lifetime/keep_alive_options.h"
+#include "chrome/browser/lifetime/keep_alive_state_observer.h"
namespace {
@@ -28,30 +29,100 @@ KeepAliveRegistry* KeepAliveRegistry::GetInstance() {
return base::Singleton<KeepAliveRegistry>::get();
}
+void KeepAliveRegistry::SetObserver(KeepAliveStateObserver* observer) {
dgn 2016/02/23 16:58:46 having add/remove observer and an ObserverList wou
dgn 2016/02/24 13:03:55 Done.
+ DCHECK(!observer_ || !observer);
+ observer_ = observer;
+
+ KeepAliveOptions all_disabled_state = {nullptr,
+ KeepAliveOptionRestart::DISABLED};
+
+ NotifyOfStateDifferences(all_disabled_state);
+}
+
////////////////////////////////////////////////////////////////////////////////
// Private methods
-KeepAliveRegistry::KeepAliveRegistry() {}
+KeepAliveRegistry::KeepAliveRegistry() : observer_(nullptr) {}
KeepAliveRegistry::~KeepAliveRegistry() {
DCHECK_EQ(0u, registered_keep_alives_.size());
+ DCHECK_EQ(0u, restart_allowed_keep_alives_.size());
+}
+
+KeepAliveOptions KeepAliveRegistry::ComputeCurrentState() const {
dgn 2016/02/23 16:58:46 Might be better to have a different struct instead
dgn 2016/02/24 13:03:55 Done.
+ bool keeping_alive = registered_keep_alives_.size() > 0;
+ bool restart_allowed =
+ registered_keep_alives_.size() == restart_allowed_keep_alives_.size() &&
+ keeping_alive;
+ KeepAliveOptions current_state = {
+ nullptr, restart_allowed ? KeepAliveOptionRestart::ENABLED
+ : KeepAliveOptionRestart::DISABLED};
+ return current_state;
+}
+
+void KeepAliveRegistry::NotifyOfStateDifferences(
+ const KeepAliveOptions& previous_state) const {
+ if (!observer_)
+ return;
+
+ KeepAliveOptions current_state = ComputeCurrentState();
+
+ if (previous_state.restart != current_state.restart) {
+ if (current_state.restart == KeepAliveOptionRestart::ENABLED)
+ observer_->OnRestartAllowed();
+ else
+ observer_->OnRestartForbidden();
+ }
}
void KeepAliveRegistry::Register(const KeepAliveOptions* option) {
+ // Check the state before adding the new KeepAlive
+ KeepAliveOptions before_state = ComputeCurrentState();
dgn 2016/02/23 16:58:46 Should I store it in a member instead of recomputi
dgn 2016/02/24 13:03:55 Done.
+
registered_keep_alives_.insert(option);
// 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 (option->restart == KeepAliveOptionRestart::ENABLED) {
+ restart_allowed_keep_alives_.insert(option);
+ }
+
+ NotifyOfStateDifferences(before_state);
+ DumpCurrentState();
}
void KeepAliveRegistry::Unregister(const KeepAliveOptions* option) {
- RemoveOne(option, registered_keep_alives_);
+ DCHECK(option);
+
+ // Check the state before adding the new KeepAlive
+ KeepAliveOptions before_state = ComputeCurrentState();
+ RemoveOne(option, registered_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();
+
+ if (option->restart == KeepAliveOptionRestart::ENABLED) {
+ RemoveOne(option, restart_allowed_keep_alives_);
+ }
+
+ NotifyOfStateDifferences(before_state);
+ DumpCurrentState();
+}
+
+void KeepAliveRegistry::DumpCurrentState() const {
+#if !defined(NDEBUG)
+ std::string msg = " - Registered: ";
+ for (const KeepAliveOptions* registered : registered_keep_alives_)
+ msg += std::string(registered->label) + " ";
dgn 2016/02/23 16:58:46 seems that we use std::string::append and base::St
dgn 2016/02/24 13:03:55 Just logged directly with streams
+ msg += "\n - RestartAllowed: ";
+ for (const KeepAliveOptions* registered : registered_keep_alives_)
+ msg += std::string(registered->label) + " ";
+ DLOG(INFO) << "Dumping current KeepAlive state:\n" << msg;
+#endif // !defined(NDEBUG)
}

Powered by Google App Engine
This is Rietveld 408576698