Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/lifetime/keep_alive_registry.h" | 5 #include "chrome/browser/lifetime/keep_alive_registry.h" |
| 6 | 6 |
| 7 #include "chrome/browser/browser_process.h" | 7 #include "chrome/browser/browser_process.h" |
| 8 #include "chrome/browser/lifetime/application_lifetime.h" | 8 #include "chrome/browser/lifetime/application_lifetime.h" |
| 9 #include "chrome/browser/lifetime/keep_alive_options.h" | 9 #include "chrome/browser/lifetime/keep_alive_options.h" |
| 10 #include "chrome/browser/lifetime/keep_alive_state_observer.h" | |
| 10 | 11 |
| 11 namespace { | 12 namespace { |
| 12 | 13 |
| 13 // Helper function to remove one item from the multisets. | 14 // Helper function to remove one item from the multisets. |
| 14 void RemoveOne(const KeepAliveOptions* key, | 15 void RemoveOne(const KeepAliveOptions* key, |
| 15 std::multiset<const KeepAliveOptions*>& container) { | 16 std::multiset<const KeepAliveOptions*>& container) { |
| 16 auto it = container.find(key); | 17 auto it = container.find(key); |
| 17 DCHECK(it != container.end()); | 18 DCHECK(it != container.end()); |
| 18 container.erase(it); | 19 container.erase(it); |
| 19 } | 20 } |
| 20 | 21 |
| 21 } // namespace | 22 } // namespace |
| 22 | 23 |
| 23 //////////////////////////////////////////////////////////////////////////////// | 24 //////////////////////////////////////////////////////////////////////////////// |
| 24 // Public methods | 25 // Public methods |
| 25 | 26 |
| 26 // static | 27 // static |
| 27 KeepAliveRegistry* KeepAliveRegistry::GetInstance() { | 28 KeepAliveRegistry* KeepAliveRegistry::GetInstance() { |
| 28 return base::Singleton<KeepAliveRegistry>::get(); | 29 return base::Singleton<KeepAliveRegistry>::get(); |
| 29 } | 30 } |
| 30 | 31 |
| 32 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.
| |
| 33 DCHECK(!observer_ || !observer); | |
| 34 observer_ = observer; | |
| 35 | |
| 36 KeepAliveOptions all_disabled_state = {nullptr, | |
| 37 KeepAliveOptionRestart::DISABLED}; | |
| 38 | |
| 39 NotifyOfStateDifferences(all_disabled_state); | |
| 40 } | |
| 41 | |
| 31 //////////////////////////////////////////////////////////////////////////////// | 42 //////////////////////////////////////////////////////////////////////////////// |
| 32 // Private methods | 43 // Private methods |
| 33 | 44 |
| 34 KeepAliveRegistry::KeepAliveRegistry() {} | 45 KeepAliveRegistry::KeepAliveRegistry() : observer_(nullptr) {} |
| 35 | 46 |
| 36 KeepAliveRegistry::~KeepAliveRegistry() { | 47 KeepAliveRegistry::~KeepAliveRegistry() { |
| 37 DCHECK_EQ(0u, registered_keep_alives_.size()); | 48 DCHECK_EQ(0u, registered_keep_alives_.size()); |
| 49 DCHECK_EQ(0u, restart_allowed_keep_alives_.size()); | |
| 50 } | |
| 51 | |
| 52 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.
| |
| 53 bool keeping_alive = registered_keep_alives_.size() > 0; | |
| 54 bool restart_allowed = | |
| 55 registered_keep_alives_.size() == restart_allowed_keep_alives_.size() && | |
| 56 keeping_alive; | |
| 57 KeepAliveOptions current_state = { | |
| 58 nullptr, restart_allowed ? KeepAliveOptionRestart::ENABLED | |
| 59 : KeepAliveOptionRestart::DISABLED}; | |
| 60 return current_state; | |
| 61 } | |
| 62 | |
| 63 void KeepAliveRegistry::NotifyOfStateDifferences( | |
| 64 const KeepAliveOptions& previous_state) const { | |
| 65 if (!observer_) | |
| 66 return; | |
| 67 | |
| 68 KeepAliveOptions current_state = ComputeCurrentState(); | |
| 69 | |
| 70 if (previous_state.restart != current_state.restart) { | |
| 71 if (current_state.restart == KeepAliveOptionRestart::ENABLED) | |
| 72 observer_->OnRestartAllowed(); | |
| 73 else | |
| 74 observer_->OnRestartForbidden(); | |
| 75 } | |
| 38 } | 76 } |
| 39 | 77 |
| 40 void KeepAliveRegistry::Register(const KeepAliveOptions* option) { | 78 void KeepAliveRegistry::Register(const KeepAliveOptions* option) { |
| 79 // Check the state before adding the new KeepAlive | |
| 80 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.
| |
| 81 | |
| 41 registered_keep_alives_.insert(option); | 82 registered_keep_alives_.insert(option); |
| 42 | 83 |
| 43 // TODO(dgn): We currently use the plain KeepAliveCount. We will integrate | 84 // TODO(dgn): We currently use the plain KeepAliveCount. We will integrate |
| 44 // that in this class progressively as mechanisms are merged. | 85 // that in this class progressively as mechanisms are merged. |
| 45 if (registered_keep_alives_.size() == 1) | 86 if (registered_keep_alives_.size() == 1) |
| 46 chrome::IncrementKeepAliveCount(); | 87 chrome::IncrementKeepAliveCount(); |
| 88 | |
| 89 if (option->restart == KeepAliveOptionRestart::ENABLED) { | |
| 90 restart_allowed_keep_alives_.insert(option); | |
| 91 } | |
| 92 | |
| 93 NotifyOfStateDifferences(before_state); | |
| 94 DumpCurrentState(); | |
| 47 } | 95 } |
| 48 | 96 |
| 49 void KeepAliveRegistry::Unregister(const KeepAliveOptions* option) { | 97 void KeepAliveRegistry::Unregister(const KeepAliveOptions* option) { |
| 98 DCHECK(option); | |
| 99 | |
| 100 // Check the state before adding the new KeepAlive | |
| 101 KeepAliveOptions before_state = ComputeCurrentState(); | |
| 102 | |
| 50 RemoveOne(option, registered_keep_alives_); | 103 RemoveOne(option, registered_keep_alives_); |
| 51 | 104 |
| 52 | |
| 53 // TODO(dgn): We currently use the plain KeepAliveCount. We will integrate | 105 // TODO(dgn): We currently use the plain KeepAliveCount. We will integrate |
| 54 // that in this class progressively as mechanisms are merged. | 106 // that in this class progressively as mechanisms are merged. |
| 55 if (registered_keep_alives_.size() == 0) | 107 if (registered_keep_alives_.size() == 0) |
| 56 chrome::DecrementKeepAliveCount(); | 108 chrome::DecrementKeepAliveCount(); |
| 109 | |
| 110 if (option->restart == KeepAliveOptionRestart::ENABLED) { | |
| 111 RemoveOne(option, restart_allowed_keep_alives_); | |
| 112 } | |
| 113 | |
| 114 NotifyOfStateDifferences(before_state); | |
| 115 DumpCurrentState(); | |
| 57 } | 116 } |
| 117 | |
| 118 void KeepAliveRegistry::DumpCurrentState() const { | |
| 119 #if !defined(NDEBUG) | |
| 120 std::string msg = " - Registered: "; | |
| 121 for (const KeepAliveOptions* registered : registered_keep_alives_) | |
| 122 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
| |
| 123 msg += "\n - RestartAllowed: "; | |
| 124 for (const KeepAliveOptions* registered : registered_keep_alives_) | |
| 125 msg += std::string(registered->label) + " "; | |
| 126 DLOG(INFO) << "Dumping current KeepAlive state:\n" << msg; | |
| 127 #endif // !defined(NDEBUG) | |
| 128 } | |
| OLD | NEW |