Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(918)

Side by Side Diff: chrome/browser/lifetime/keep_alive_registry.cc

Issue 1803143002: Replace BrowserProces::AddRefModule/RemoveModule by ScopedKeepAlive (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 } 100 }
97 101
98 void KeepAliveRegistry::OnKeepingAliveChanged(bool new_keeping_alive) { 102 void KeepAliveRegistry::OnKeepingAliveChanged(bool new_keeping_alive) {
99 // Although we should have a browser process, if there is none, 103 // Although we should have a browser process, if there is none,
100 // there is nothing to do. 104 // there is nothing to do.
101 if (!g_browser_process) 105 if (!g_browser_process)
102 return; 106 return;
103 107
104 if (new_keeping_alive) { 108 if (new_keeping_alive) {
105 DVLOG(1) << "KeepAliveRegistry is now keeping the browser alive."; 109 DVLOG(1) << "KeepAliveRegistry is now keeping the browser alive.";
106 g_browser_process->AddRefModule(); 110 g_browser_process->Pin();
107 } else { 111 } else {
108 DVLOG(1) << "KeepAliveRegistry stopped keeping the browser alive."; 112 DVLOG(1) << "KeepAliveRegistry stopped keeping the browser alive.";
109 g_browser_process->ReleaseModule(); 113 g_browser_process->Unpin();
110 chrome::CloseAllBrowsersIfNeeded(); 114 chrome::ShutdownIfNeeded();
111 } 115 }
112 } 116 }
113 117
114 void KeepAliveRegistry::OnRestartAllowedChanged(bool new_restart_allowed) { 118 void KeepAliveRegistry::OnRestartAllowedChanged(bool new_restart_allowed) {
115 DVLOG(1) << "Notifying KeepAliveStateObservers: Restart changed to: " 119 DVLOG(1) << "Notifying KeepAliveStateObservers: Restart changed to: "
116 << new_restart_allowed; 120 << new_restart_allowed;
117 FOR_EACH_OBSERVER(KeepAliveStateObserver, observers_, 121 FOR_EACH_OBSERVER(KeepAliveStateObserver, observers_,
118 OnKeepAliveRestartStateChanged(new_restart_allowed)); 122 OnKeepAliveRestartStateChanged(new_restart_allowed));
119 } 123 }
120 124
121 #ifndef NDEBUG 125 #ifndef NDEBUG
122 std::ostream& operator<<(std::ostream& out, const KeepAliveRegistry& registry) { 126 std::ostream& operator<<(std::ostream& out, const KeepAliveRegistry& registry) {
123 out << "{registered_count_=" << registry.registered_count_ 127 out << "{registered_count_=" << registry.registered_count_
124 << ", restart_allowed_count_=" << registry.restart_allowed_count_ 128 << ", restart_allowed_count_=" << registry.restart_allowed_count_
125 << ", KeepAlives=["; 129 << ", KeepAlives=[";
126 for (auto counts_per_origin_it : registry.registered_keep_alives_) { 130 for (auto counts_per_origin_it : registry.registered_keep_alives_) {
127 if (counts_per_origin_it != *registry.registered_keep_alives_.begin()) 131 if (counts_per_origin_it != *registry.registered_keep_alives_.begin())
128 out << ", "; 132 out << ", ";
129 out << counts_per_origin_it.first << " (" << counts_per_origin_it.second 133 out << counts_per_origin_it.first << " (" << counts_per_origin_it.second
130 << ")"; 134 << ")";
131 } 135 }
132 out << "]}"; 136 out << "]}";
133 return out; 137 return out;
134 } 138 }
135 #endif // ndef NDEBUG 139 #endif // ndef NDEBUG
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698