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/browser_process.h" |
8 #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" | 9 #include "chrome/browser/lifetime/keep_alive_state_observer.h" |
10 #include "chrome/browser/lifetime/keep_alive_types.h" | 10 #include "chrome/browser/lifetime/keep_alive_types.h" |
11 | 11 |
12 //////////////////////////////////////////////////////////////////////////////// | 12 //////////////////////////////////////////////////////////////////////////////// |
13 // Public methods | 13 // Public methods |
14 | 14 |
15 // static | 15 // static |
16 KeepAliveRegistry* KeepAliveRegistry::GetInstance() { | 16 KeepAliveRegistry* KeepAliveRegistry::GetInstance() { |
17 return base::Singleton<KeepAliveRegistry>::get(); | 17 return base::Singleton<KeepAliveRegistry>::get(); |
18 } | 18 } |
19 | 19 |
20 bool KeepAliveRegistry::IsKeepingAlive() const { | 20 bool KeepAliveRegistry::IsKeepingAlive() const { |
21 return registered_count_ > 0; | 21 return registered_count_ > 0; |
22 } | 22 } |
23 | 23 |
24 bool KeepAliveRegistry::IsRestartAllowed() const { | 24 bool KeepAliveRegistry::IsRestartAllowed() const { |
25 return registered_count_ == restart_allowed_count_; | 25 return registered_count_ == restart_allowed_count_; |
26 } | 26 } |
27 | 27 |
| 28 bool KeepAliveRegistry::IsOriginRegistered(KeepAliveOrigin origin) const { |
| 29 return registered_keep_alives_.find(origin) != registered_keep_alives_.end(); |
| 30 } |
| 31 |
28 void KeepAliveRegistry::AddObserver(KeepAliveStateObserver* observer) { | 32 void KeepAliveRegistry::AddObserver(KeepAliveStateObserver* observer) { |
29 observers_.AddObserver(observer); | 33 observers_.AddObserver(observer); |
30 } | 34 } |
31 | 35 |
32 void KeepAliveRegistry::RemoveObserver(KeepAliveStateObserver* observer) { | 36 void KeepAliveRegistry::RemoveObserver(KeepAliveStateObserver* observer) { |
33 observers_.RemoveObserver(observer); | 37 observers_.RemoveObserver(observer); |
34 } | 38 } |
35 | 39 |
36 //////////////////////////////////////////////////////////////////////////////// | 40 //////////////////////////////////////////////////////////////////////////////// |
37 // Private methods | 41 // Private methods |
(...skipping 14 matching lines...) Expand all Loading... |
52 ++registered_keep_alives_[origin]; | 56 ++registered_keep_alives_[origin]; |
53 ++registered_count_; | 57 ++registered_count_; |
54 | 58 |
55 if (restart == KeepAliveRestartOption::ENABLED) | 59 if (restart == KeepAliveRestartOption::ENABLED) |
56 ++restart_allowed_count_; | 60 ++restart_allowed_count_; |
57 | 61 |
58 bool new_keeping_alive = IsKeepingAlive(); | 62 bool new_keeping_alive = IsKeepingAlive(); |
59 bool new_restart_allowed = IsRestartAllowed(); | 63 bool new_restart_allowed = IsRestartAllowed(); |
60 | 64 |
61 if (new_keeping_alive != old_keeping_alive) | 65 if (new_keeping_alive != old_keeping_alive) |
62 OnKeepingAliveChanged(new_keeping_alive); | 66 OnKeepAliveStateChanged(new_keeping_alive); |
63 | 67 |
64 if (new_restart_allowed != old_restart_allowed) | 68 if (new_restart_allowed != old_restart_allowed) |
65 OnRestartAllowedChanged(new_restart_allowed); | 69 OnRestartAllowedChanged(new_restart_allowed); |
66 | 70 |
67 DVLOG(1) << "New state of the KeepAliveRegistry: " << *this; | 71 DVLOG(1) << "New state of the KeepAliveRegistry: " << *this; |
68 } | 72 } |
69 | 73 |
70 void KeepAliveRegistry::Unregister(KeepAliveOrigin origin, | 74 void KeepAliveRegistry::Unregister(KeepAliveOrigin origin, |
71 KeepAliveRestartOption restart) { | 75 KeepAliveRestartOption restart) { |
72 bool old_keeping_alive = IsKeepingAlive(); | 76 bool old_keeping_alive = IsKeepingAlive(); |
(...skipping 10 matching lines...) Expand all Loading... |
83 if (restart == KeepAliveRestartOption::ENABLED) | 87 if (restart == KeepAliveRestartOption::ENABLED) |
84 --restart_allowed_count_; | 88 --restart_allowed_count_; |
85 | 89 |
86 bool new_keeping_alive = IsKeepingAlive(); | 90 bool new_keeping_alive = IsKeepingAlive(); |
87 bool new_restart_allowed = IsRestartAllowed(); | 91 bool new_restart_allowed = IsRestartAllowed(); |
88 | 92 |
89 if (new_restart_allowed != old_restart_allowed) | 93 if (new_restart_allowed != old_restart_allowed) |
90 OnRestartAllowedChanged(new_restart_allowed); | 94 OnRestartAllowedChanged(new_restart_allowed); |
91 | 95 |
92 if (new_keeping_alive != old_keeping_alive) | 96 if (new_keeping_alive != old_keeping_alive) |
93 OnKeepingAliveChanged(new_keeping_alive); | 97 OnKeepAliveStateChanged(new_keeping_alive); |
94 | 98 |
95 DVLOG(1) << "New state of the KeepAliveRegistry: " << *this; | 99 DVLOG(1) << "New state of the KeepAliveRegistry: " << *this; |
96 } | 100 } |
97 | 101 |
98 void KeepAliveRegistry::OnKeepingAliveChanged(bool new_keeping_alive) { | 102 void KeepAliveRegistry::OnKeepAliveStateChanged(bool new_keeping_alive) { |
99 // Although we should have a browser process, if there is none, | 103 DVLOG(1) << "Notifying KeepAliveStateObservers: KeepingAlive changed to: " |
100 // there is nothing to do. | 104 << new_keeping_alive; |
101 if (!g_browser_process) | 105 FOR_EACH_OBSERVER(KeepAliveStateObserver, observers_, |
102 return; | 106 OnKeepAliveStateChanged(new_keeping_alive)); |
103 | |
104 if (new_keeping_alive) { | |
105 DVLOG(1) << "KeepAliveRegistry is now keeping the browser alive."; | |
106 g_browser_process->AddRefModule(); | |
107 } else { | |
108 DVLOG(1) << "KeepAliveRegistry stopped keeping the browser alive."; | |
109 g_browser_process->ReleaseModule(); | |
110 chrome::CloseAllBrowsersIfNeeded(); | |
111 } | |
112 } | 107 } |
113 | 108 |
114 void KeepAliveRegistry::OnRestartAllowedChanged(bool new_restart_allowed) { | 109 void KeepAliveRegistry::OnRestartAllowedChanged(bool new_restart_allowed) { |
115 DVLOG(1) << "Notifying KeepAliveStateObservers: Restart changed to: " | 110 DVLOG(1) << "Notifying KeepAliveStateObservers: Restart changed to: " |
116 << new_restart_allowed; | 111 << new_restart_allowed; |
117 FOR_EACH_OBSERVER(KeepAliveStateObserver, observers_, | 112 FOR_EACH_OBSERVER(KeepAliveStateObserver, observers_, |
118 OnKeepAliveRestartStateChanged(new_restart_allowed)); | 113 OnKeepAliveRestartStateChanged(new_restart_allowed)); |
119 } | 114 } |
120 | 115 |
121 #ifndef NDEBUG | 116 #ifndef NDEBUG |
122 std::ostream& operator<<(std::ostream& out, const KeepAliveRegistry& registry) { | 117 std::ostream& operator<<(std::ostream& out, const KeepAliveRegistry& registry) { |
123 out << "{registered_count_=" << registry.registered_count_ | 118 out << "{registered_count_=" << registry.registered_count_ |
124 << ", restart_allowed_count_=" << registry.restart_allowed_count_ | 119 << ", restart_allowed_count_=" << registry.restart_allowed_count_ |
125 << ", KeepAlives=["; | 120 << ", KeepAlives=["; |
126 for (auto counts_per_origin_it : registry.registered_keep_alives_) { | 121 for (auto counts_per_origin_it : registry.registered_keep_alives_) { |
127 if (counts_per_origin_it != *registry.registered_keep_alives_.begin()) | 122 if (counts_per_origin_it != *registry.registered_keep_alives_.begin()) |
128 out << ", "; | 123 out << ", "; |
129 out << counts_per_origin_it.first << " (" << counts_per_origin_it.second | 124 out << counts_per_origin_it.first << " (" << counts_per_origin_it.second |
130 << ")"; | 125 << ")"; |
131 } | 126 } |
132 out << "]}"; | 127 out << "]}"; |
133 return out; | 128 return out; |
134 } | 129 } |
135 #endif // ndef NDEBUG | 130 #endif // ndef NDEBUG |
OLD | NEW |