dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
Michael Giuffrida | 2dbce0d1 | 2017-09-02 03:30:59 | [diff] [blame] | 5 | #include "components/keep_alive_registry/keep_alive_registry.h" |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 6 | |
Michael Giuffrida | 2dbce0d1 | 2017-09-02 03:30:59 | [diff] [blame] | 7 | #include "base/logging.h" |
manzagop | ba8991e | 2017-05-08 21:05:21 | [diff] [blame] | 8 | #include "build/build_config.h" |
Michael Giuffrida | 2dbce0d1 | 2017-09-02 03:30:59 | [diff] [blame] | 9 | #include "components/keep_alive_registry/keep_alive_state_observer.h" |
| 10 | #include "components/keep_alive_registry/keep_alive_types.h" |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 11 | |
manzagop | ba8991e | 2017-05-08 21:05:21 | [diff] [blame] | 12 | #if defined(OS_WIN) |
Sigurdur Asgeirsson | a0ecc77 | 2020-02-06 18:02:27 | [diff] [blame^] | 13 | #include "components/browser_watcher/activity_data_names.h" |
| 14 | #include "components/browser_watcher/extended_crash_reporting.h" |
manzagop | ba8991e | 2017-05-08 21:05:21 | [diff] [blame] | 15 | #endif |
| 16 | |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 17 | //////////////////////////////////////////////////////////////////////////////// |
| 18 | // Public methods |
| 19 | |
| 20 | // static |
| 21 | KeepAliveRegistry* KeepAliveRegistry::GetInstance() { |
| 22 | return base::Singleton<KeepAliveRegistry>::get(); |
| 23 | } |
| 24 | |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 25 | bool KeepAliveRegistry::IsKeepingAlive() const { |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 26 | return registered_count_ > 0; |
| 27 | } |
| 28 | |
Aleksei Seren | 121533e | 2017-11-28 20:11:18 | [diff] [blame] | 29 | bool KeepAliveRegistry::IsKeepingAliveOnlyByBrowserOrigin() const { |
| 30 | for (const auto& value : registered_keep_alives_) { |
| 31 | if (value.first != KeepAliveOrigin::BROWSER) |
| 32 | return false; |
| 33 | } |
| 34 | return true; |
| 35 | } |
| 36 | |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 37 | bool KeepAliveRegistry::IsRestartAllowed() const { |
| 38 | return registered_count_ == restart_allowed_count_; |
| 39 | } |
| 40 | |
dgn | fe075c8 | 2016-03-18 11:25:35 | [diff] [blame] | 41 | bool KeepAliveRegistry::IsOriginRegistered(KeepAliveOrigin origin) const { |
| 42 | return registered_keep_alives_.find(origin) != registered_keep_alives_.end(); |
| 43 | } |
| 44 | |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 45 | void KeepAliveRegistry::AddObserver(KeepAliveStateObserver* observer) { |
| 46 | observers_.AddObserver(observer); |
| 47 | } |
| 48 | |
| 49 | void KeepAliveRegistry::RemoveObserver(KeepAliveStateObserver* observer) { |
| 50 | observers_.RemoveObserver(observer); |
| 51 | } |
| 52 | |
dgn | af6e861 | 2016-08-12 12:24:09 | [diff] [blame] | 53 | bool KeepAliveRegistry::WouldRestartWithout( |
| 54 | const std::vector<KeepAliveOrigin>& origins) const { |
| 55 | int registered_count = 0; |
| 56 | int restart_allowed_count = 0; |
| 57 | |
| 58 | for (auto origin : origins) { |
| 59 | auto counts_it = registered_keep_alives_.find(origin); |
| 60 | if (counts_it != registered_keep_alives_.end()) { |
| 61 | registered_count += counts_it->second; |
| 62 | |
| 63 | counts_it = restart_allowed_keep_alives_.find(origin); |
| 64 | if (counts_it != restart_allowed_keep_alives_.end()) |
| 65 | restart_allowed_count += counts_it->second; |
| 66 | } else { |
| 67 | // |registered_keep_alives_| is supposed to be a superset of |
| 68 | // |restart_allowed_keep_alives_| |
| 69 | DCHECK(restart_allowed_keep_alives_.find(origin) == |
| 70 | restart_allowed_keep_alives_.end()); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | registered_count = registered_count_ - registered_count; |
| 75 | restart_allowed_count = restart_allowed_count_ - restart_allowed_count; |
| 76 | |
| 77 | DCHECK_GE(registered_count, 0); |
| 78 | DCHECK_GE(restart_allowed_count, 0); |
| 79 | |
| 80 | return registered_count == restart_allowed_count; |
| 81 | } |
| 82 | |
Vladislav Kaznacheev | eb65ea1 | 2019-05-14 00:30:05 | [diff] [blame] | 83 | bool KeepAliveRegistry::IsShuttingDown() const { |
| 84 | return is_shutting_down_; |
| 85 | } |
| 86 | |
Michael Giuffrida | 2dbce0d1 | 2017-09-02 03:30:59 | [diff] [blame] | 87 | void KeepAliveRegistry::SetIsShuttingDown(bool value) { |
Michael Giuffrida | 2dbce0d1 | 2017-09-02 03:30:59 | [diff] [blame] | 88 | is_shutting_down_ = value; |
Michael Giuffrida | 2dbce0d1 | 2017-09-02 03:30:59 | [diff] [blame] | 89 | } |
| 90 | |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 91 | //////////////////////////////////////////////////////////////////////////////// |
| 92 | // Private methods |
| 93 | |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 94 | KeepAliveRegistry::KeepAliveRegistry() |
| 95 | : registered_count_(0), restart_allowed_count_(0) {} |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 96 | |
| 97 | KeepAliveRegistry::~KeepAliveRegistry() { |
Wez | 2f88261b | 2018-04-16 18:03:09 | [diff] [blame] | 98 | DCHECK_LE(registered_count_, 0) << "KeepAliveRegistry state:" << *this; |
| 99 | DCHECK_EQ(registered_keep_alives_.size(), 0u) |
| 100 | << "KeepAliveRegistry state:" << *this; |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 101 | } |
| 102 | |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 103 | void KeepAliveRegistry::Register(KeepAliveOrigin origin, |
| 104 | KeepAliveRestartOption restart) { |
Wez | 2f88261b | 2018-04-16 18:03:09 | [diff] [blame] | 105 | CHECK(!is_shutting_down_); |
dgn | 1e50dd2 | 2016-04-22 22:16:56 | [diff] [blame] | 106 | |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 107 | bool old_keeping_alive = IsKeepingAlive(); |
| 108 | bool old_restart_allowed = IsRestartAllowed(); |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 109 | |
| 110 | ++registered_keep_alives_[origin]; |
| 111 | ++registered_count_; |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 112 | |
dgn | af6e861 | 2016-08-12 12:24:09 | [diff] [blame] | 113 | if (restart == KeepAliveRestartOption::ENABLED) { |
| 114 | ++restart_allowed_keep_alives_[origin]; |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 115 | ++restart_allowed_count_; |
dgn | af6e861 | 2016-08-12 12:24:09 | [diff] [blame] | 116 | } |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 117 | |
| 118 | bool new_keeping_alive = IsKeepingAlive(); |
| 119 | bool new_restart_allowed = IsRestartAllowed(); |
| 120 | |
| 121 | if (new_keeping_alive != old_keeping_alive) |
dgn | fe075c8 | 2016-03-18 11:25:35 | [diff] [blame] | 122 | OnKeepAliveStateChanged(new_keeping_alive); |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 123 | |
| 124 | if (new_restart_allowed != old_restart_allowed) |
| 125 | OnRestartAllowedChanged(new_restart_allowed); |
| 126 | |
dgn | 1e50dd2 | 2016-04-22 22:16:56 | [diff] [blame] | 127 | DVLOG(1) << "New state of the KeepAliveRegistry: " << *this; |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 128 | } |
| 129 | |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 130 | void KeepAliveRegistry::Unregister(KeepAliveOrigin origin, |
| 131 | KeepAliveRestartOption restart) { |
| 132 | bool old_keeping_alive = IsKeepingAlive(); |
| 133 | bool old_restart_allowed = IsRestartAllowed(); |
| 134 | |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 135 | --registered_count_; |
| 136 | DCHECK_GE(registered_count_, 0); |
dgn | af6e861 | 2016-08-12 12:24:09 | [diff] [blame] | 137 | DecrementCount(origin, ®istered_keep_alives_); |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 138 | |
dgn | af6e861 | 2016-08-12 12:24:09 | [diff] [blame] | 139 | if (restart == KeepAliveRestartOption::ENABLED) { |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 140 | --restart_allowed_count_; |
dgn | af6e861 | 2016-08-12 12:24:09 | [diff] [blame] | 141 | DecrementCount(origin, &restart_allowed_keep_alives_); |
| 142 | } |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 143 | |
| 144 | bool new_keeping_alive = IsKeepingAlive(); |
| 145 | bool new_restart_allowed = IsRestartAllowed(); |
| 146 | |
dgn | 3eaf67e | 2016-07-26 09:01:12 | [diff] [blame] | 147 | // Update the KeepAlive state first, so that listeners can check if we are |
| 148 | // trying to shutdown. |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 149 | if (new_keeping_alive != old_keeping_alive) |
dgn | fe075c8 | 2016-03-18 11:25:35 | [diff] [blame] | 150 | OnKeepAliveStateChanged(new_keeping_alive); |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 151 | |
dgn | 3eaf67e | 2016-07-26 09:01:12 | [diff] [blame] | 152 | if (new_restart_allowed != old_restart_allowed) |
| 153 | OnRestartAllowedChanged(new_restart_allowed); |
| 154 | |
dgn | 1e50dd2 | 2016-04-22 22:16:56 | [diff] [blame] | 155 | DVLOG(1) << "New state of the KeepAliveRegistry:" << *this; |
dgn | 3d351ad1 | 2016-02-26 17:36:45 | [diff] [blame] | 156 | } |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 157 | |
dgn | fe075c8 | 2016-03-18 11:25:35 | [diff] [blame] | 158 | void KeepAliveRegistry::OnKeepAliveStateChanged(bool new_keeping_alive) { |
| 159 | DVLOG(1) << "Notifying KeepAliveStateObservers: KeepingAlive changed to: " |
| 160 | << new_keeping_alive; |
manzagop | ba8991e | 2017-05-08 21:05:21 | [diff] [blame] | 161 | #if defined(OS_WIN) |
Sigurdur Asgeirsson | a0ecc77 | 2020-02-06 18:02:27 | [diff] [blame^] | 162 | browser_watcher::ExtendedCrashReporting::SetDataBool( |
| 163 | browser_watcher::kActivityKeepAlive, new_keeping_alive); |
manzagop | ba8991e | 2017-05-08 21:05:21 | [diff] [blame] | 164 | #endif |
ericwilligers | 58b0e16 | 2016-10-21 07:15:56 | [diff] [blame] | 165 | for (KeepAliveStateObserver& observer : observers_) |
| 166 | observer.OnKeepAliveStateChanged(new_keeping_alive); |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void KeepAliveRegistry::OnRestartAllowedChanged(bool new_restart_allowed) { |
| 170 | DVLOG(1) << "Notifying KeepAliveStateObservers: Restart changed to: " |
| 171 | << new_restart_allowed; |
manzagop | ba8991e | 2017-05-08 21:05:21 | [diff] [blame] | 172 | #if defined(OS_WIN) |
Sigurdur Asgeirsson | a0ecc77 | 2020-02-06 18:02:27 | [diff] [blame^] | 173 | browser_watcher::ExtendedCrashReporting::SetDataBool( |
| 174 | browser_watcher::kActivityRestartAllowed, new_restart_allowed); |
manzagop | ba8991e | 2017-05-08 21:05:21 | [diff] [blame] | 175 | #endif |
ericwilligers | 58b0e16 | 2016-10-21 07:15:56 | [diff] [blame] | 176 | for (KeepAliveStateObserver& observer : observers_) |
| 177 | observer.OnKeepAliveRestartStateChanged(new_restart_allowed); |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 178 | } |
| 179 | |
dgn | af6e861 | 2016-08-12 12:24:09 | [diff] [blame] | 180 | void KeepAliveRegistry::DecrementCount(KeepAliveOrigin origin, |
| 181 | OriginMap* keep_alive_map) { |
| 182 | int new_count = --keep_alive_map->at(origin); |
| 183 | DCHECK_GE(keep_alive_map->at(origin), 0); |
| 184 | if (new_count == 0) |
| 185 | keep_alive_map->erase(origin); |
| 186 | } |
| 187 | |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 188 | std::ostream& operator<<(std::ostream& out, const KeepAliveRegistry& registry) { |
dgn | 0237778 | 2016-03-12 00:58:38 | [diff] [blame] | 189 | out << "{registered_count_=" << registry.registered_count_ |
| 190 | << ", restart_allowed_count_=" << registry.restart_allowed_count_ |
| 191 | << ", KeepAlives=["; |
dgn | 3563ecaf | 2016-03-09 19:28:26 | [diff] [blame] | 192 | for (auto counts_per_origin_it : registry.registered_keep_alives_) { |
| 193 | if (counts_per_origin_it != *registry.registered_keep_alives_.begin()) |
| 194 | out << ", "; |
| 195 | out << counts_per_origin_it.first << " (" << counts_per_origin_it.second |
| 196 | << ")"; |
| 197 | } |
| 198 | out << "]}"; |
| 199 | return out; |
| 200 | } |