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 <sstream> | |
| 8 | |
| 7 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 8 #include "chrome/browser/lifetime/application_lifetime.h" | 10 #include "chrome/browser/lifetime/application_lifetime.h" |
| 9 #include "chrome/browser/lifetime/keep_alive_state_observer.h" | 11 #include "chrome/browser/lifetime/keep_alive_state_observer.h" |
| 10 #include "chrome/browser/lifetime/keep_alive_types.h" | 12 #include "chrome/browser/lifetime/keep_alive_types.h" |
| 11 | 13 |
| 12 //////////////////////////////////////////////////////////////////////////////// | 14 //////////////////////////////////////////////////////////////////////////////// |
| 13 // Public methods | 15 // Public methods |
| 14 | 16 |
| 15 // static | 17 // static |
| 16 KeepAliveRegistry* KeepAliveRegistry::GetInstance() { | 18 KeepAliveRegistry* KeepAliveRegistry::GetInstance() { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 | 62 |
| 61 registered_count = registered_count_ - registered_count; | 63 registered_count = registered_count_ - registered_count; |
| 62 restart_allowed_count = restart_allowed_count_ - restart_allowed_count; | 64 restart_allowed_count = restart_allowed_count_ - restart_allowed_count; |
| 63 | 65 |
| 64 DCHECK_GE(registered_count, 0); | 66 DCHECK_GE(registered_count, 0); |
| 65 DCHECK_GE(restart_allowed_count, 0); | 67 DCHECK_GE(restart_allowed_count, 0); |
| 66 | 68 |
| 67 return registered_count == restart_allowed_count; | 69 return registered_count == restart_allowed_count; |
| 68 } | 70 } |
| 69 | 71 |
| 72 std::string KeepAliveRegistry::ToString() const { | |
| 73 std::stringstream stream; | |
| 74 stream << *this; | |
|
sky
2017/03/22 03:56:16
Are you sure this returns something helpful?
| |
| 75 return stream.str(); | |
| 76 } | |
| 77 | |
| 70 //////////////////////////////////////////////////////////////////////////////// | 78 //////////////////////////////////////////////////////////////////////////////// |
| 71 // Private methods | 79 // Private methods |
| 72 | 80 |
| 73 KeepAliveRegistry::KeepAliveRegistry() | 81 KeepAliveRegistry::KeepAliveRegistry() |
| 74 : registered_count_(0), restart_allowed_count_(0) {} | 82 : registered_count_(0), restart_allowed_count_(0) {} |
| 75 | 83 |
| 76 KeepAliveRegistry::~KeepAliveRegistry() { | 84 KeepAliveRegistry::~KeepAliveRegistry() { |
| 77 DLOG_IF(ERROR, registered_count_ > 0 || registered_keep_alives_.size() > 0) | 85 // TODO(sammiequon): Change all VLOGS back to DVLOGS in this file once |
| 86 // crbug.com/660962 is resolved. | |
| 87 VLOG_IF(1, registered_count_ > 0 || registered_keep_alives_.size() > 0) | |
|
sky
2017/03/22 03:56:16
!empty()
| |
| 78 << "KeepAliveRegistry not empty at destruction time. State:" << *this; | 88 << "KeepAliveRegistry not empty at destruction time. State:" << *this; |
| 79 } | 89 } |
| 80 | 90 |
| 81 void KeepAliveRegistry::Register(KeepAliveOrigin origin, | 91 void KeepAliveRegistry::Register(KeepAliveOrigin origin, |
| 82 KeepAliveRestartOption restart) { | 92 KeepAliveRestartOption restart) { |
| 83 DCHECK(!g_browser_process->IsShuttingDown()); | 93 DCHECK(!g_browser_process->IsShuttingDown()); |
| 84 | 94 |
| 85 bool old_keeping_alive = IsKeepingAlive(); | 95 bool old_keeping_alive = IsKeepingAlive(); |
| 86 bool old_restart_allowed = IsRestartAllowed(); | 96 bool old_restart_allowed = IsRestartAllowed(); |
| 87 | 97 |
| 88 ++registered_keep_alives_[origin]; | 98 ++registered_keep_alives_[origin]; |
| 89 ++registered_count_; | 99 ++registered_count_; |
| 90 | 100 |
| 91 if (restart == KeepAliveRestartOption::ENABLED) { | 101 if (restart == KeepAliveRestartOption::ENABLED) { |
| 92 ++restart_allowed_keep_alives_[origin]; | 102 ++restart_allowed_keep_alives_[origin]; |
| 93 ++restart_allowed_count_; | 103 ++restart_allowed_count_; |
| 94 } | 104 } |
| 95 | 105 |
| 96 bool new_keeping_alive = IsKeepingAlive(); | 106 bool new_keeping_alive = IsKeepingAlive(); |
| 97 bool new_restart_allowed = IsRestartAllowed(); | 107 bool new_restart_allowed = IsRestartAllowed(); |
| 98 | 108 |
| 99 if (new_keeping_alive != old_keeping_alive) | 109 if (new_keeping_alive != old_keeping_alive) |
| 100 OnKeepAliveStateChanged(new_keeping_alive); | 110 OnKeepAliveStateChanged(new_keeping_alive); |
| 101 | 111 |
| 102 if (new_restart_allowed != old_restart_allowed) | 112 if (new_restart_allowed != old_restart_allowed) |
| 103 OnRestartAllowedChanged(new_restart_allowed); | 113 OnRestartAllowedChanged(new_restart_allowed); |
| 104 | 114 |
| 105 DVLOG(1) << "New state of the KeepAliveRegistry: " << *this; | 115 VLOG(1) << "New state of the KeepAliveRegistry: " << *this; |
| 106 } | 116 } |
| 107 | 117 |
| 108 void KeepAliveRegistry::Unregister(KeepAliveOrigin origin, | 118 void KeepAliveRegistry::Unregister(KeepAliveOrigin origin, |
| 109 KeepAliveRestartOption restart) { | 119 KeepAliveRestartOption restart) { |
| 110 bool old_keeping_alive = IsKeepingAlive(); | 120 bool old_keeping_alive = IsKeepingAlive(); |
| 111 bool old_restart_allowed = IsRestartAllowed(); | 121 bool old_restart_allowed = IsRestartAllowed(); |
| 112 | 122 |
| 113 --registered_count_; | 123 --registered_count_; |
| 114 DCHECK_GE(registered_count_, 0); | 124 DCHECK_GE(registered_count_, 0); |
| 115 DecrementCount(origin, ®istered_keep_alives_); | 125 DecrementCount(origin, ®istered_keep_alives_); |
| 116 | 126 |
| 117 if (restart == KeepAliveRestartOption::ENABLED) { | 127 if (restart == KeepAliveRestartOption::ENABLED) { |
| 118 --restart_allowed_count_; | 128 --restart_allowed_count_; |
| 119 DecrementCount(origin, &restart_allowed_keep_alives_); | 129 DecrementCount(origin, &restart_allowed_keep_alives_); |
| 120 } | 130 } |
| 121 | 131 |
| 122 bool new_keeping_alive = IsKeepingAlive(); | 132 bool new_keeping_alive = IsKeepingAlive(); |
| 123 bool new_restart_allowed = IsRestartAllowed(); | 133 bool new_restart_allowed = IsRestartAllowed(); |
| 124 | 134 |
| 125 // Update the KeepAlive state first, so that listeners can check if we are | 135 // Update the KeepAlive state first, so that listeners can check if we are |
| 126 // trying to shutdown. | 136 // trying to shutdown. |
| 127 if (new_keeping_alive != old_keeping_alive) | 137 if (new_keeping_alive != old_keeping_alive) |
| 128 OnKeepAliveStateChanged(new_keeping_alive); | 138 OnKeepAliveStateChanged(new_keeping_alive); |
| 129 | 139 |
| 130 if (new_restart_allowed != old_restart_allowed) | 140 if (new_restart_allowed != old_restart_allowed) |
| 131 OnRestartAllowedChanged(new_restart_allowed); | 141 OnRestartAllowedChanged(new_restart_allowed); |
| 132 | 142 |
| 133 DVLOG(1) << "New state of the KeepAliveRegistry:" << *this; | 143 VLOG(1) << "New state of the KeepAliveRegistry:" << *this; |
| 134 } | 144 } |
| 135 | 145 |
| 136 void KeepAliveRegistry::OnKeepAliveStateChanged(bool new_keeping_alive) { | 146 void KeepAliveRegistry::OnKeepAliveStateChanged(bool new_keeping_alive) { |
| 137 DVLOG(1) << "Notifying KeepAliveStateObservers: KeepingAlive changed to: " | 147 VLOG(1) << "Notifying KeepAliveStateObservers: KeepingAlive changed to: " |
| 138 << new_keeping_alive; | 148 << new_keeping_alive; |
| 139 for (KeepAliveStateObserver& observer : observers_) | 149 for (KeepAliveStateObserver& observer : observers_) |
| 140 observer.OnKeepAliveStateChanged(new_keeping_alive); | 150 observer.OnKeepAliveStateChanged(new_keeping_alive); |
| 141 } | 151 } |
| 142 | 152 |
| 143 void KeepAliveRegistry::OnRestartAllowedChanged(bool new_restart_allowed) { | 153 void KeepAliveRegistry::OnRestartAllowedChanged(bool new_restart_allowed) { |
| 144 DVLOG(1) << "Notifying KeepAliveStateObservers: Restart changed to: " | 154 VLOG(1) << "Notifying KeepAliveStateObservers: Restart changed to: " |
| 145 << new_restart_allowed; | 155 << new_restart_allowed; |
| 146 for (KeepAliveStateObserver& observer : observers_) | 156 for (KeepAliveStateObserver& observer : observers_) |
| 147 observer.OnKeepAliveRestartStateChanged(new_restart_allowed); | 157 observer.OnKeepAliveRestartStateChanged(new_restart_allowed); |
| 148 } | 158 } |
| 149 | 159 |
| 150 void KeepAliveRegistry::DecrementCount(KeepAliveOrigin origin, | 160 void KeepAliveRegistry::DecrementCount(KeepAliveOrigin origin, |
| 151 OriginMap* keep_alive_map) { | 161 OriginMap* keep_alive_map) { |
| 152 int new_count = --keep_alive_map->at(origin); | 162 int new_count = --keep_alive_map->at(origin); |
| 153 DCHECK_GE(keep_alive_map->at(origin), 0); | 163 DCHECK_GE(keep_alive_map->at(origin), 0); |
| 154 if (new_count == 0) | 164 if (new_count == 0) |
| 155 keep_alive_map->erase(origin); | 165 keep_alive_map->erase(origin); |
| 156 } | 166 } |
| 157 | 167 |
| 158 std::ostream& operator<<(std::ostream& out, const KeepAliveRegistry& registry) { | 168 std::ostream& operator<<(std::ostream& out, const KeepAliveRegistry& registry) { |
| 159 out << "{registered_count_=" << registry.registered_count_ | 169 out << "{registered_count_=" << registry.registered_count_ |
| 160 << ", restart_allowed_count_=" << registry.restart_allowed_count_ | 170 << ", restart_allowed_count_=" << registry.restart_allowed_count_ |
| 161 << ", KeepAlives=["; | 171 << ", KeepAlives=["; |
| 162 for (auto counts_per_origin_it : registry.registered_keep_alives_) { | 172 for (auto counts_per_origin_it : registry.registered_keep_alives_) { |
| 163 if (counts_per_origin_it != *registry.registered_keep_alives_.begin()) | 173 if (counts_per_origin_it != *registry.registered_keep_alives_.begin()) |
| 164 out << ", "; | 174 out << ", "; |
| 165 out << counts_per_origin_it.first << " (" << counts_per_origin_it.second | 175 out << counts_per_origin_it.first << " (" << counts_per_origin_it.second |
| 166 << ")"; | 176 << ")"; |
| 167 } | 177 } |
| 168 out << "]}"; | 178 out << "]}"; |
| 169 return out; | 179 return out; |
| 170 } | 180 } |
| OLD | NEW |