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(); |
sky
2016/02/26 22:13:25
49-50 and 58-65 are duplicated in UnRegister. How
dgn
2016/03/07 17:17:14
I considered something similar before (see up to p
sky
2016/03/09 18:29:35
Ok. Keep what you have here.
dgn
2016/03/09 18:37:04
Acknowledged.
| |
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 | |
99 void KeepAliveRegistry::OnKeepingAliveChanged(bool new_keeping_alive) { | |
100 if (new_keeping_alive) { | |
101 DVLOG(1) << "KeepAliveRegistry is now keeping the browser alive."; | |
102 chrome::IncrementKeepAliveCount(); | |
103 } else { | |
104 DVLOG(1) << "KeepAliveRegistry stopped keeping the browser alive."; | |
50 chrome::DecrementKeepAliveCount(); | 105 chrome::DecrementKeepAliveCount(); |
106 } | |
51 } | 107 } |
108 void KeepAliveRegistry::OnRestartAllowedChanged(bool new_restart_allowed) { | |
109 if (new_restart_allowed) { | |
110 DVLOG(1) << "Notifying KeepAliveStateObservers: Restart Allowed"; | |
111 FOR_EACH_OBSERVER(KeepAliveStateObserver, observers_, OnRestartAllowed()); | |
112 } else { | |
113 DVLOG(1) << "Notifying KeepAliveStateObservers: Restart Forbidden"; | |
114 FOR_EACH_OBSERVER(KeepAliveStateObserver, observers_, OnRestartForbidden()); | |
115 } | |
116 } | |
117 | |
118 #ifndef NDEBUG | |
119 std::ostream& operator<<(std::ostream& out, const KeepAliveRegistry& registry) { | |
120 out << "{KeepingAlive=" << registry.IsKeepingAlive() << ", RestartAllowed=" | |
121 << registry.IsRestartAllowed() << ", KeepAlives=["; | |
122 for (auto counts_per_origin_it : registry.registered_keep_alives_) { | |
123 if (counts_per_origin_it != *registry.registered_keep_alives_.begin()) | |
124 out << ", "; | |
125 out << counts_per_origin_it.first << " (" << counts_per_origin_it.second | |
126 << ")"; | |
127 } | |
128 out << "]}"; | |
129 return out; | |
130 } | |
131 #endif // ndef NDEBUG | |
OLD | NEW |