OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/ui/browser_list.h" | 5 #include "chrome/browser/ui/browser_list.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
12 #include "chrome/browser/browser_shutdown.h" | 12 #include "chrome/browser/browser_shutdown.h" |
13 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
14 #include "chrome/browser/lifetime/application_lifetime.h" | 14 #include "chrome/browser/lifetime/application_lifetime.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
17 #include "chrome/browser/ui/browser_finder.h" | 17 #include "chrome/browser/ui/browser_finder.h" |
18 #include "chrome/browser/ui/browser_list.h" | |
19 #include "chrome/browser/ui/browser_list_observer.h" | 18 #include "chrome/browser/ui/browser_list_observer.h" |
20 #include "chrome/browser/ui/browser_window.h" | 19 #include "chrome/browser/ui/browser_window.h" |
21 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
22 #include "content/public/browser/user_metrics.h" | 21 #include "content/public/browser/user_metrics.h" |
23 | 22 |
24 using base::UserMetricsAction; | 23 using base::UserMetricsAction; |
25 using content::WebContents; | 24 using content::WebContents; |
26 | 25 |
| 26 namespace { |
| 27 |
| 28 BrowserList::BrowserVector GetBrowserToClose(Profile* profile) { |
| 29 BrowserList::BrowserVector browsers_to_close; |
| 30 for (auto* browser : *BrowserList::GetInstance()) { |
| 31 if (browser->profile()->GetOriginalProfile() == |
| 32 profile->GetOriginalProfile()) |
| 33 browsers_to_close.push_back(browser); |
| 34 } |
| 35 return browsers_to_close; |
| 36 } |
| 37 |
| 38 } // namespace |
| 39 |
27 // static | 40 // static |
28 base::LazyInstance<base::ObserverList<chrome::BrowserListObserver>>::Leaky | 41 base::LazyInstance<base::ObserverList<chrome::BrowserListObserver>>::Leaky |
29 BrowserList::observers_ = LAZY_INSTANCE_INITIALIZER; | 42 BrowserList::observers_ = LAZY_INSTANCE_INITIALIZER; |
30 | 43 |
31 // static | 44 // static |
32 BrowserList* BrowserList::instance_ = NULL; | 45 BrowserList* BrowserList::instance_ = NULL; |
33 | 46 |
34 //////////////////////////////////////////////////////////////////////////////// | 47 //////////////////////////////////////////////////////////////////////////////// |
35 // BrowserList, public: | 48 // BrowserList, public: |
36 | 49 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 browsers_to_close.push_back(browser); | 130 browsers_to_close.push_back(browser); |
118 } | 131 } |
119 | 132 |
120 for (BrowserVector::const_iterator it = browsers_to_close.begin(); | 133 for (BrowserVector::const_iterator it = browsers_to_close.begin(); |
121 it != browsers_to_close.end(); ++it) { | 134 it != browsers_to_close.end(); ++it) { |
122 (*it)->window()->Close(); | 135 (*it)->window()->Close(); |
123 } | 136 } |
124 } | 137 } |
125 | 138 |
126 // static | 139 // static |
127 void BrowserList::CloseAllBrowsersWithProfile(Profile* profile, | 140 void BrowserList::CloseAllBrowsersWithProfile( |
| 141 Profile* profile, |
128 const CloseCallback& on_close_success, | 142 const CloseCallback& on_close_success, |
129 const CloseCallback& on_close_aborted) { | 143 const CloseCallback& on_close_aborted, |
130 BrowserVector browsers_to_close; | 144 bool is_force) { |
131 for (auto* browser : *BrowserList::GetInstance()) { | 145 TryToCloseBrowserList(GetBrowserToClose(profile), on_close_success, |
132 if (browser->profile()->GetOriginalProfile() == | 146 on_close_aborted, profile->GetPath(), is_force); |
133 profile->GetOriginalProfile()) | |
134 browsers_to_close.push_back(browser); | |
135 } | |
136 | |
137 TryToCloseBrowserList(browsers_to_close, on_close_success, on_close_aborted, | |
138 profile->GetPath()); | |
139 } | 147 } |
140 | 148 |
141 // static | 149 // static |
142 void BrowserList::TryToCloseBrowserList(const BrowserVector& browsers_to_close, | 150 void BrowserList::TryToCloseBrowserList(const BrowserVector& browsers_to_close, |
143 const CloseCallback& on_close_success, | 151 const CloseCallback& on_close_success, |
144 const CloseCallback& on_close_aborted, | 152 const CloseCallback& on_close_aborted, |
145 const base::FilePath& profile_path) { | 153 const base::FilePath& profile_path, |
| 154 const bool is_force) { |
146 for (BrowserVector::const_iterator it = browsers_to_close.begin(); | 155 for (BrowserVector::const_iterator it = browsers_to_close.begin(); |
147 it != browsers_to_close.end(); ++it) { | 156 it != browsers_to_close.end(); ++it) { |
148 if ((*it)->CallBeforeUnloadHandlers( | 157 if ((*it)->TryToCloseWindow( |
149 base::Bind(&BrowserList::PostBeforeUnloadHandlers, | 158 is_force, base::Bind(&BrowserList::PostTryToCloseBrowserWindow, |
150 browsers_to_close, | 159 browsers_to_close, on_close_success, |
151 on_close_success, | 160 on_close_aborted, profile_path, is_force))) { |
152 on_close_aborted, | |
153 profile_path))) { | |
154 return; | 161 return; |
155 } | 162 } |
156 } | 163 } |
157 | 164 |
158 on_close_success.Run(profile_path); | 165 on_close_success.Run(profile_path); |
159 | 166 |
160 for (Browser* b : browsers_to_close) { | 167 for (Browser* b : browsers_to_close) { |
161 // BeforeUnload handlers may close browser windows, so we need to explicitly | 168 // BeforeUnload handlers may close browser windows, so we need to explicitly |
162 // check whether they still exist. | 169 // check whether they still exist. |
163 if (b->window()) | 170 if (b->window()) |
164 b->window()->Close(); | 171 b->window()->Close(); |
165 } | 172 } |
166 } | 173 } |
167 | 174 |
168 // static | 175 // static |
169 void BrowserList::PostBeforeUnloadHandlers( | 176 void BrowserList::PostTryToCloseBrowserWindow( |
170 const BrowserVector& browsers_to_close, | 177 const BrowserVector& browsers_to_close, |
171 const CloseCallback& on_close_success, | 178 const CloseCallback& on_close_success, |
172 const CloseCallback& on_close_aborted, | 179 const CloseCallback& on_close_aborted, |
173 const base::FilePath& profile_path, | 180 const base::FilePath& profile_path, |
| 181 const bool is_force, |
174 bool tab_close_confirmed) { | 182 bool tab_close_confirmed) { |
175 // We need this bool to avoid infinite recursion when resetting the | 183 // We need this bool to avoid infinite recursion when resetting the |
176 // BeforeUnload handlers, since doing that will trigger calls back to this | 184 // BeforeUnload handlers, since doing that will trigger calls back to this |
177 // method for each affected window. | 185 // method for each affected window. |
178 static bool resetting_handlers = false; | 186 static bool resetting_handlers = false; |
179 | 187 |
180 if (tab_close_confirmed) { | 188 if (tab_close_confirmed) { |
181 TryToCloseBrowserList(browsers_to_close, on_close_success, on_close_aborted, | 189 TryToCloseBrowserList(browsers_to_close, on_close_success, on_close_aborted, |
182 profile_path); | 190 profile_path, is_force); |
183 } else if (!resetting_handlers) { | 191 } else if (!resetting_handlers) { |
184 base::AutoReset<bool> resetting_handlers_scoper(&resetting_handlers, true); | 192 base::AutoReset<bool> resetting_handlers_scoper(&resetting_handlers, true); |
185 for (BrowserVector::const_iterator it = browsers_to_close.begin(); | 193 for (BrowserVector::const_iterator it = browsers_to_close.begin(); |
186 it != browsers_to_close.end(); ++it) { | 194 it != browsers_to_close.end(); ++it) { |
187 (*it)->ResetBeforeUnloadHandlers(); | 195 (*it)->ResetCloseWindow(); |
188 } | 196 } |
189 on_close_aborted.Run(profile_path); | 197 on_close_aborted.Run(profile_path); |
190 } | 198 } |
191 } | 199 } |
192 | 200 |
193 // static | 201 // static |
194 void BrowserList::MoveBrowsersInWorkspaceToFront( | 202 void BrowserList::MoveBrowsersInWorkspaceToFront( |
195 const std::string& new_workspace) { | 203 const std::string& new_workspace) { |
196 DCHECK(!new_workspace.empty()); | 204 DCHECK(!new_workspace.empty()); |
197 | 205 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 } | 276 } |
269 | 277 |
270 // static | 278 // static |
271 void BrowserList::RemoveBrowserFrom(Browser* browser, | 279 void BrowserList::RemoveBrowserFrom(Browser* browser, |
272 BrowserVector* browser_list) { | 280 BrowserVector* browser_list) { |
273 BrowserVector::iterator remove_browser = | 281 BrowserVector::iterator remove_browser = |
274 std::find(browser_list->begin(), browser_list->end(), browser); | 282 std::find(browser_list->begin(), browser_list->end(), browser); |
275 if (remove_browser != browser_list->end()) | 283 if (remove_browser != browser_list->end()) |
276 browser_list->erase(remove_browser); | 284 browser_list->erase(remove_browser); |
277 } | 285 } |
OLD | NEW |