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/lifetime/application_lifetime.h" | 7 #include "chrome/browser/lifetime/application_lifetime.h" |
| 8 #include "chrome/browser/lifetime/keep_alive_state_observer.h" |
8 #include "chrome/browser/lifetime/keep_alive_types.h" | 9 #include "chrome/browser/lifetime/keep_alive_types.h" |
9 | 10 |
10 //////////////////////////////////////////////////////////////////////////////// | 11 //////////////////////////////////////////////////////////////////////////////// |
11 // Public methods | 12 // Public methods |
12 | 13 |
13 // static | 14 // static |
14 KeepAliveRegistry* KeepAliveRegistry::GetInstance() { | 15 KeepAliveRegistry* KeepAliveRegistry::GetInstance() { |
15 return base::Singleton<KeepAliveRegistry>::get(); | 16 return base::Singleton<KeepAliveRegistry>::get(); |
16 } | 17 } |
17 | 18 |
18 bool KeepAliveRegistry::WillKeepAlive() const { | 19 bool KeepAliveRegistry::IsKeepingAlive() const { |
19 return registered_count_ > 0; | 20 return registered_count_ > 0; |
20 } | 21 } |
21 | 22 |
| 23 bool KeepAliveRegistry::IsRestartAllowed() const { |
| 24 return registered_count_ == restart_allowed_count_; |
| 25 } |
| 26 |
| 27 void KeepAliveRegistry::AddObserver(KeepAliveStateObserver* observer) { |
| 28 observers_.AddObserver(observer); |
| 29 } |
| 30 |
| 31 void KeepAliveRegistry::RemoveObserver(KeepAliveStateObserver* observer) { |
| 32 observers_.RemoveObserver(observer); |
| 33 } |
| 34 |
22 //////////////////////////////////////////////////////////////////////////////// | 35 //////////////////////////////////////////////////////////////////////////////// |
23 // Private methods | 36 // Private methods |
24 | 37 |
25 KeepAliveRegistry::KeepAliveRegistry() : registered_count_(0) {} | 38 KeepAliveRegistry::KeepAliveRegistry() |
| 39 : registered_count_(0), restart_allowed_count_(0) {} |
26 | 40 |
27 KeepAliveRegistry::~KeepAliveRegistry() { | 41 KeepAliveRegistry::~KeepAliveRegistry() { |
28 DCHECK_EQ(0, registered_count_); | 42 DCHECK_EQ(0, registered_count_); |
29 DCHECK_EQ(0u, registered_keep_alives_.size()); | 43 DCHECK_EQ(0u, registered_keep_alives_.size()); |
| 44 DCHECK_EQ(0, restart_allowed_count_); |
30 } | 45 } |
31 | 46 |
32 void KeepAliveRegistry::Register(KeepAliveOrigin origin) { | 47 void KeepAliveRegistry::Register(KeepAliveOrigin origin, |
33 if (!WillKeepAlive()) | 48 KeepAliveRestartOption restart) { |
34 chrome::IncrementKeepAliveCount(); | 49 bool old_keeping_alive = IsKeepingAlive(); |
| 50 bool old_restart_allowed = IsRestartAllowed(); |
35 | 51 |
36 ++registered_keep_alives_[origin]; | 52 ++registered_keep_alives_[origin]; |
37 ++registered_count_; | 53 ++registered_count_; |
| 54 |
| 55 if (restart == KeepAliveRestartOption::ENABLED) |
| 56 ++restart_allowed_count_; |
| 57 |
| 58 bool new_keeping_alive = IsKeepingAlive(); |
| 59 bool new_restart_allowed = IsRestartAllowed(); |
| 60 |
| 61 if (new_keeping_alive != old_keeping_alive) |
| 62 OnKeepingAliveChanged(new_keeping_alive); |
| 63 |
| 64 if (new_restart_allowed != old_restart_allowed) |
| 65 OnRestartAllowedChanged(new_restart_allowed); |
| 66 |
| 67 DVLOG(1) << "New state of the KeepAliveRegistry: " << *this; |
38 } | 68 } |
39 | 69 |
40 void KeepAliveRegistry::Unregister(KeepAliveOrigin origin) { | 70 void KeepAliveRegistry::Unregister(KeepAliveOrigin origin, |
| 71 KeepAliveRestartOption restart) { |
| 72 bool old_keeping_alive = IsKeepingAlive(); |
| 73 bool old_restart_allowed = IsRestartAllowed(); |
| 74 |
41 --registered_count_; | 75 --registered_count_; |
42 DCHECK_GE(registered_count_, 0); | 76 DCHECK_GE(registered_count_, 0); |
43 | 77 |
44 int new_count = --registered_keep_alives_[origin]; | 78 int new_count = --registered_keep_alives_[origin]; |
45 DCHECK_GE(registered_keep_alives_[origin], 0); | 79 DCHECK_GE(registered_keep_alives_[origin], 0); |
46 if (new_count == 0) | 80 if (new_count == 0) |
47 registered_keep_alives_.erase(origin); | 81 registered_keep_alives_.erase(origin); |
48 | 82 |
49 if (!WillKeepAlive()) | 83 if (restart == KeepAliveRestartOption::ENABLED) |
| 84 --restart_allowed_count_; |
| 85 |
| 86 bool new_keeping_alive = IsKeepingAlive(); |
| 87 bool new_restart_allowed = IsRestartAllowed(); |
| 88 |
| 89 if (new_restart_allowed != old_restart_allowed) |
| 90 OnRestartAllowedChanged(new_restart_allowed); |
| 91 |
| 92 if (new_keeping_alive != old_keeping_alive) |
| 93 OnKeepingAliveChanged(new_keeping_alive); |
| 94 |
| 95 DVLOG(1) << "New state of the KeepAliveRegistry: " << *this; |
| 96 } |
| 97 |
| 98 void KeepAliveRegistry::OnKeepingAliveChanged(bool new_keeping_alive) { |
| 99 if (new_keeping_alive) { |
| 100 DVLOG(1) << "KeepAliveRegistry is now keeping the browser alive."; |
| 101 chrome::IncrementKeepAliveCount(); |
| 102 } else { |
| 103 DVLOG(1) << "KeepAliveRegistry stopped keeping the browser alive."; |
50 chrome::DecrementKeepAliveCount(); | 104 chrome::DecrementKeepAliveCount(); |
| 105 } |
51 } | 106 } |
| 107 |
| 108 void KeepAliveRegistry::OnRestartAllowedChanged(bool new_restart_allowed) { |
| 109 DVLOG(1) << "Notifying KeepAliveStateObservers: Restart changed to: " |
| 110 << new_restart_allowed; |
| 111 FOR_EACH_OBSERVER(KeepAliveStateObserver, observers_, |
| 112 OnKeepAliveRestartStateChanged(new_restart_allowed)); |
| 113 } |
| 114 |
| 115 #ifndef NDEBUG |
| 116 std::ostream& operator<<(std::ostream& out, const KeepAliveRegistry& registry) { |
| 117 out << "{KeepingAlive=" << registry.IsKeepingAlive() |
| 118 << ", RestartAllowed=" << registry.IsRestartAllowed() << ", KeepAlives=["; |
| 119 for (auto counts_per_origin_it : registry.registered_keep_alives_) { |
| 120 if (counts_per_origin_it != *registry.registered_keep_alives_.begin()) |
| 121 out << ", "; |
| 122 out << counts_per_origin_it.first << " (" << counts_per_origin_it.second |
| 123 << ")"; |
| 124 } |
| 125 out << "]}"; |
| 126 return out; |
| 127 } |
| 128 #endif // ndef NDEBUG |
OLD | NEW |