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/lifetime/application_lifetime.h" | 8 #include "chrome/browser/lifetime/application_lifetime.h" |
| 9 #include "chrome/browser/lifetime/keep_alive_state_observer.h" | |
| 8 #include "chrome/browser/lifetime/keep_alive_types.h" | 10 #include "chrome/browser/lifetime/keep_alive_types.h" |
| 9 | 11 |
| 10 namespace { | 12 namespace { |
| 11 | 13 |
| 12 // Helper function to remove one item from the multisets. | 14 // Helper function to remove one item from the multisets. |
| 13 void RemoveOne(keep_alive::Origin key, | 15 void RemoveOne(keep_alive::Origin key, |
| 14 std::multiset<keep_alive::Origin>& container) { | 16 std::multiset<keep_alive::Origin>& container) { |
| 15 auto it = container.find(key); | 17 auto it = container.find(key); |
| 16 DCHECK(it != container.end()); | 18 DCHECK(it != container.end()); |
| 17 container.erase(it); | 19 container.erase(it); |
| 18 } | 20 } |
| 19 | 21 |
| 22 void HugBrowser() { | |
|
Bernhard Bauer
2016/02/24 16:54:34
Awww... 😃
dgn
2016/02/26 14:34:29
Aaaand.. It's gone.
| |
| 23 // TODO(dgn): We currently use the plain KeepAliveCount. We will integrate | |
| 24 // that in this class progressively as mechanisms are merged. | |
| 25 chrome::IncrementKeepAliveCount(); | |
| 26 } | |
| 27 | |
| 28 void ReleaseBrowser() { | |
| 29 // TODO(dgn): We currently use the plain KeepAliveCount. We will integrate | |
| 30 // that in this class progressively as mechanisms are merged. | |
| 31 chrome::DecrementKeepAliveCount(); | |
| 32 } | |
| 33 | |
| 20 } // namespace | 34 } // namespace |
| 21 | 35 |
| 22 //////////////////////////////////////////////////////////////////////////////// | 36 //////////////////////////////////////////////////////////////////////////////// |
| 23 // Public methods | 37 // Public methods |
| 24 | 38 |
| 25 // static | 39 // static |
| 26 KeepAliveRegistry* KeepAliveRegistry::GetInstance() { | 40 KeepAliveRegistry* KeepAliveRegistry::GetInstance() { |
| 27 return base::Singleton<KeepAliveRegistry>::get(); | 41 return base::Singleton<KeepAliveRegistry>::get(); |
| 28 } | 42 } |
| 29 | 43 |
| 44 void KeepAliveRegistry::AddObserver(KeepAliveStateObserver* observer) { | |
| 45 observers_.AddObserver(observer); | |
| 46 } | |
| 47 | |
| 48 void KeepAliveRegistry::RemoveObserver(KeepAliveStateObserver* observer) { | |
| 49 observers_.RemoveObserver(observer); | |
| 50 } | |
| 51 | |
| 30 //////////////////////////////////////////////////////////////////////////////// | 52 //////////////////////////////////////////////////////////////////////////////// |
| 31 // Private methods | 53 // Private methods |
| 32 | 54 |
| 33 KeepAliveRegistry::KeepAliveRegistry() {} | 55 KeepAliveRegistry::KeepAliveRegistry() { |
| 56 state_ = {false, // is_keeping_alive | |
| 57 keep_alive::RestartOption::DISABLED}; | |
| 58 } | |
| 34 | 59 |
| 35 KeepAliveRegistry::~KeepAliveRegistry() { | 60 KeepAliveRegistry::~KeepAliveRegistry() { |
| 36 DCHECK_EQ(0u, registered_keep_alives_.size()); | 61 DCHECK_EQ(0u, registered_keep_alives_.size()); |
| 62 DCHECK_EQ(0u, restart_allowed_keep_alives_.size()); | |
| 37 } | 63 } |
| 38 | 64 |
| 39 void KeepAliveRegistry::Register(keep_alive::Origin origin) { | 65 KeepAliveRegistry::KeepAliveState KeepAliveRegistry::ComputeCurrentState() |
| 40 registered_keep_alives_.insert(origin); | 66 const { |
| 67 bool keeping_alive = registered_keep_alives_.size() > 0; | |
| 68 bool restart_allowed = | |
| 69 registered_keep_alives_.size() == restart_allowed_keep_alives_.size() && | |
| 70 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
| |
| 41 | 71 |
| 42 // TODO(dgn): We currently use the plain KeepAliveCount. We will integrate | 72 return {keeping_alive, restart_allowed ? keep_alive::RestartOption::ENABLED |
| 43 // that in this class progressively as mechanisms are merged. | 73 : keep_alive::RestartOption::DISABLED}; |
| 44 if (registered_keep_alives_.size() == 1) | |
| 45 chrome::IncrementKeepAliveCount(); | |
| 46 } | 74 } |
| 47 | 75 |
| 48 void KeepAliveRegistry::Unregister(keep_alive::Origin origin) { | 76 void KeepAliveRegistry::NotifyOfStateDifferences( |
| 77 const KeepAliveState& new_state) { | |
| 78 if (state_.restart != new_state.restart) { | |
| 79 if (new_state.restart == keep_alive::RestartOption::ENABLED) { | |
| 80 VLOG(1) << "Notifying observers: Restart Allowed"; | |
| 81 FOR_EACH_OBSERVER(KeepAliveStateObserver, observers_, OnRestartAllowed()); | |
| 82 } else { | |
| 83 VLOG(1) << "Notifying observers: Restart Forbidden"; | |
| 84 FOR_EACH_OBSERVER(KeepAliveStateObserver, observers_, | |
| 85 OnRestartForbidden()); | |
| 86 } | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 void KeepAliveRegistry::Register(keep_alive::Origin origin, | |
| 91 keep_alive::RestartOption restart) { | |
| 92 registered_keep_alives_.insert(origin); | |
| 93 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.
| |
| 94 restart_allowed_keep_alives_.insert(origin); | |
| 95 } | |
| 96 | |
| 97 if (!state_.is_keeping_alive) | |
| 98 HugBrowser(); | |
| 99 | |
| 100 KeepAliveState new_state = ComputeCurrentState(); | |
| 101 NotifyOfStateDifferences(new_state); | |
| 102 state_ = new_state; | |
| 103 DumpRegistryIfDebug(); | |
| 104 } | |
| 105 | |
| 106 void KeepAliveRegistry::Unregister(keep_alive::Origin origin, | |
| 107 keep_alive::RestartOption restart) { | |
| 49 RemoveOne(origin, registered_keep_alives_); | 108 RemoveOne(origin, registered_keep_alives_); |
| 109 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.
| |
| 110 RemoveOne(origin, restart_allowed_keep_alives_); | |
| 111 } | |
| 50 | 112 |
| 51 // TODO(dgn): We currently use the plain KeepAliveCount. We will integrate | 113 KeepAliveState new_state = ComputeCurrentState(); |
| 52 // that in this class progressively as mechanisms are merged. | 114 NotifyOfStateDifferences(new_state); |
| 53 if (registered_keep_alives_.size() == 0) | 115 state_ = new_state; |
| 54 chrome::DecrementKeepAliveCount(); | 116 DumpRegistryIfDebug(); |
| 117 | |
| 118 if (!state_.is_keeping_alive) | |
| 119 ReleaseBrowser(); | |
| 55 } | 120 } |
| 121 | |
| 122 void KeepAliveRegistry::DumpRegistryIfDebug() const { | |
| 123 #ifndef NDEBUG | |
| 124 VLOG(1) << "KeepAliveRegistry dump - total: {registered=" | |
| 125 << registered_keep_alives_.size() | |
| 126 << ", restart=" << restart_allowed_keep_alives_.size() << "}"; | |
| 127 for (auto origin_it = registered_keep_alives_.begin(); | |
| 128 origin_it != registered_keep_alives_.end(); | |
| 129 origin_it = registered_keep_alives_.upper_bound(*origin_it)) { | |
| 130 VLOG(1) << "- " << *origin_it | |
| 131 << " {registered=" << registered_keep_alives_.count(*origin_it) | |
| 132 << ", restart=" << restart_allowed_keep_alives_.count(*origin_it) | |
| 133 << "}"; | |
| 134 } | |
| 135 #endif // ndef NDEBUG | |
| 136 } | |
| OLD | NEW |