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/lifetime/application_lifetime.h" | 8 #include "chrome/browser/lifetime/application_lifetime.h" |
8 #include "chrome/browser/lifetime/keep_alive_state_observer.h" | 9 #include "chrome/browser/lifetime/keep_alive_state_observer.h" |
9 #include "chrome/browser/lifetime/keep_alive_types.h" | 10 #include "chrome/browser/lifetime/keep_alive_types.h" |
10 | 11 |
11 //////////////////////////////////////////////////////////////////////////////// | 12 //////////////////////////////////////////////////////////////////////////////// |
12 // Public methods | 13 // Public methods |
13 | 14 |
14 // static | 15 // static |
15 KeepAliveRegistry* KeepAliveRegistry::GetInstance() { | 16 KeepAliveRegistry* KeepAliveRegistry::GetInstance() { |
16 return base::Singleton<KeepAliveRegistry>::get(); | 17 return base::Singleton<KeepAliveRegistry>::get(); |
(...skipping 15 matching lines...) Expand all Loading... |
32 observers_.RemoveObserver(observer); | 33 observers_.RemoveObserver(observer); |
33 } | 34 } |
34 | 35 |
35 //////////////////////////////////////////////////////////////////////////////// | 36 //////////////////////////////////////////////////////////////////////////////// |
36 // Private methods | 37 // Private methods |
37 | 38 |
38 KeepAliveRegistry::KeepAliveRegistry() | 39 KeepAliveRegistry::KeepAliveRegistry() |
39 : registered_count_(0), restart_allowed_count_(0) {} | 40 : registered_count_(0), restart_allowed_count_(0) {} |
40 | 41 |
41 KeepAliveRegistry::~KeepAliveRegistry() { | 42 KeepAliveRegistry::~KeepAliveRegistry() { |
42 DCHECK_EQ(0, registered_count_); | 43 DLOG_IF(ERROR, registered_count_ > 0 || registered_keep_alives_.size() > 0) |
43 DCHECK_EQ(0u, registered_keep_alives_.size()); | 44 << "KeepAliveRegistry not empty at destruction time. State: " << *this; |
44 DCHECK_EQ(0, restart_allowed_count_); | |
45 } | 45 } |
46 | 46 |
47 void KeepAliveRegistry::Register(KeepAliveOrigin origin, | 47 void KeepAliveRegistry::Register(KeepAliveOrigin origin, |
48 KeepAliveRestartOption restart) { | 48 KeepAliveRestartOption restart) { |
49 bool old_keeping_alive = IsKeepingAlive(); | 49 bool old_keeping_alive = IsKeepingAlive(); |
50 bool old_restart_allowed = IsRestartAllowed(); | 50 bool old_restart_allowed = IsRestartAllowed(); |
51 | 51 |
52 ++registered_keep_alives_[origin]; | 52 ++registered_keep_alives_[origin]; |
53 ++registered_count_; | 53 ++registered_count_; |
54 | 54 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 if (new_restart_allowed != old_restart_allowed) | 89 if (new_restart_allowed != old_restart_allowed) |
90 OnRestartAllowedChanged(new_restart_allowed); | 90 OnRestartAllowedChanged(new_restart_allowed); |
91 | 91 |
92 if (new_keeping_alive != old_keeping_alive) | 92 if (new_keeping_alive != old_keeping_alive) |
93 OnKeepingAliveChanged(new_keeping_alive); | 93 OnKeepingAliveChanged(new_keeping_alive); |
94 | 94 |
95 DVLOG(1) << "New state of the KeepAliveRegistry: " << *this; | 95 DVLOG(1) << "New state of the KeepAliveRegistry: " << *this; |
96 } | 96 } |
97 | 97 |
98 void KeepAliveRegistry::OnKeepingAliveChanged(bool new_keeping_alive) { | 98 void KeepAliveRegistry::OnKeepingAliveChanged(bool new_keeping_alive) { |
| 99 // Although we should have a browser process, if there is none, |
| 100 // there is nothing to do. |
| 101 if (!g_browser_process) |
| 102 return; |
| 103 |
99 if (new_keeping_alive) { | 104 if (new_keeping_alive) { |
100 DVLOG(1) << "KeepAliveRegistry is now keeping the browser alive."; | 105 DVLOG(1) << "KeepAliveRegistry is now keeping the browser alive."; |
101 chrome::IncrementKeepAliveCount(); | 106 g_browser_process->AddRefModule(); |
102 } else { | 107 } else { |
103 DVLOG(1) << "KeepAliveRegistry stopped keeping the browser alive."; | 108 DVLOG(1) << "KeepAliveRegistry stopped keeping the browser alive."; |
104 chrome::DecrementKeepAliveCount(); | 109 g_browser_process->ReleaseModule(); |
| 110 chrome::CloseAllBrowsersIfNeeded(); |
105 } | 111 } |
106 } | 112 } |
107 | 113 |
108 void KeepAliveRegistry::OnRestartAllowedChanged(bool new_restart_allowed) { | 114 void KeepAliveRegistry::OnRestartAllowedChanged(bool new_restart_allowed) { |
109 DVLOG(1) << "Notifying KeepAliveStateObservers: Restart changed to: " | 115 DVLOG(1) << "Notifying KeepAliveStateObservers: Restart changed to: " |
110 << new_restart_allowed; | 116 << new_restart_allowed; |
111 FOR_EACH_OBSERVER(KeepAliveStateObserver, observers_, | 117 FOR_EACH_OBSERVER(KeepAliveStateObserver, observers_, |
112 OnKeepAliveRestartStateChanged(new_restart_allowed)); | 118 OnKeepAliveRestartStateChanged(new_restart_allowed)); |
113 } | 119 } |
114 | 120 |
115 #ifndef NDEBUG | 121 #ifndef NDEBUG |
116 std::ostream& operator<<(std::ostream& out, const KeepAliveRegistry& registry) { | 122 std::ostream& operator<<(std::ostream& out, const KeepAliveRegistry& registry) { |
117 out << "{KeepingAlive=" << registry.IsKeepingAlive() | 123 out << "{registered_count_=" << registry.registered_count_ |
118 << ", RestartAllowed=" << registry.IsRestartAllowed() << ", KeepAlives=["; | 124 << ", restart_allowed_count_=" << registry.restart_allowed_count_ |
| 125 << ", KeepAlives=["; |
119 for (auto counts_per_origin_it : registry.registered_keep_alives_) { | 126 for (auto counts_per_origin_it : registry.registered_keep_alives_) { |
120 if (counts_per_origin_it != *registry.registered_keep_alives_.begin()) | 127 if (counts_per_origin_it != *registry.registered_keep_alives_.begin()) |
121 out << ", "; | 128 out << ", "; |
122 out << counts_per_origin_it.first << " (" << counts_per_origin_it.second | 129 out << counts_per_origin_it.first << " (" << counts_per_origin_it.second |
123 << ")"; | 130 << ")"; |
124 } | 131 } |
125 out << "]}"; | 132 out << "]}"; |
126 return out; | 133 return out; |
127 } | 134 } |
128 #endif // ndef NDEBUG | 135 #endif // ndef NDEBUG |
OLD | NEW |