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 GetBrowsersToClose(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 skip_beforeunload) { |
131 for (auto* browser : *BrowserList::GetInstance()) { | 145 TryToCloseBrowserList(GetBrowsersToClose(profile), on_close_success, |
132 if (browser->profile()->GetOriginalProfile() == | 146 on_close_aborted, profile->GetPath(), |
133 profile->GetOriginalProfile()) | 147 skip_beforeunload); |
134 browsers_to_close.push_back(browser); | |
135 } | |
136 | |
137 TryToCloseBrowserList(browsers_to_close, on_close_success, on_close_aborted, | |
138 profile->GetPath()); | |
139 } | 148 } |
140 | 149 |
141 // static | 150 // static |
142 void BrowserList::TryToCloseBrowserList(const BrowserVector& browsers_to_close, | 151 void BrowserList::TryToCloseBrowserList(const BrowserVector& browsers_to_close, |
143 const CloseCallback& on_close_success, | 152 const CloseCallback& on_close_success, |
144 const CloseCallback& on_close_aborted, | 153 const CloseCallback& on_close_aborted, |
145 const base::FilePath& profile_path) { | 154 const base::FilePath& profile_path, |
| 155 const bool skip_beforeunload) { |
146 for (BrowserVector::const_iterator it = browsers_to_close.begin(); | 156 for (BrowserVector::const_iterator it = browsers_to_close.begin(); |
147 it != browsers_to_close.end(); ++it) { | 157 it != browsers_to_close.end(); ++it) { |
148 if ((*it)->CallBeforeUnloadHandlers( | 158 if ((*it)->TryToCloseWindow( |
149 base::Bind(&BrowserList::PostBeforeUnloadHandlers, | 159 skip_beforeunload, |
150 browsers_to_close, | 160 base::Bind(&BrowserList::PostTryToCloseBrowserWindow, |
151 on_close_success, | 161 browsers_to_close, on_close_success, on_close_aborted, |
152 on_close_aborted, | 162 profile_path, skip_beforeunload))) { |
153 profile_path))) { | |
154 return; | 163 return; |
155 } | 164 } |
156 } | 165 } |
157 | 166 |
158 on_close_success.Run(profile_path); | 167 on_close_success.Run(profile_path); |
159 | 168 |
160 for (Browser* b : browsers_to_close) { | 169 for (Browser* b : browsers_to_close) { |
161 // BeforeUnload handlers may close browser windows, so we need to explicitly | 170 // BeforeUnload handlers may close browser windows, so we need to explicitly |
162 // check whether they still exist. | 171 // check whether they still exist. |
163 if (b->window()) | 172 if (b->window()) |
164 b->window()->Close(); | 173 b->window()->Close(); |
165 } | 174 } |
166 } | 175 } |
167 | 176 |
168 // static | 177 // static |
169 void BrowserList::PostBeforeUnloadHandlers( | 178 void BrowserList::PostTryToCloseBrowserWindow( |
170 const BrowserVector& browsers_to_close, | 179 const BrowserVector& browsers_to_close, |
171 const CloseCallback& on_close_success, | 180 const CloseCallback& on_close_success, |
172 const CloseCallback& on_close_aborted, | 181 const CloseCallback& on_close_aborted, |
173 const base::FilePath& profile_path, | 182 const base::FilePath& profile_path, |
| 183 const bool skip_beforeunload, |
174 bool tab_close_confirmed) { | 184 bool tab_close_confirmed) { |
175 // We need this bool to avoid infinite recursion when resetting the | 185 // We need this bool to avoid infinite recursion when resetting the |
176 // BeforeUnload handlers, since doing that will trigger calls back to this | 186 // BeforeUnload handlers, since doing that will trigger calls back to this |
177 // method for each affected window. | 187 // method for each affected window. |
178 static bool resetting_handlers = false; | 188 static bool resetting_handlers = false; |
179 | 189 |
180 if (tab_close_confirmed) { | 190 if (tab_close_confirmed) { |
181 TryToCloseBrowserList(browsers_to_close, on_close_success, on_close_aborted, | 191 TryToCloseBrowserList(browsers_to_close, on_close_success, on_close_aborted, |
182 profile_path); | 192 profile_path, skip_beforeunload); |
183 } else if (!resetting_handlers) { | 193 } else if (!resetting_handlers) { |
184 base::AutoReset<bool> resetting_handlers_scoper(&resetting_handlers, true); | 194 base::AutoReset<bool> resetting_handlers_scoper(&resetting_handlers, true); |
185 for (BrowserVector::const_iterator it = browsers_to_close.begin(); | 195 for (BrowserVector::const_iterator it = browsers_to_close.begin(); |
186 it != browsers_to_close.end(); ++it) { | 196 it != browsers_to_close.end(); ++it) { |
187 (*it)->ResetBeforeUnloadHandlers(); | 197 (*it)->ResetTryToCloseWindow(); |
188 } | 198 } |
189 on_close_aborted.Run(profile_path); | 199 on_close_aborted.Run(profile_path); |
190 } | 200 } |
191 } | 201 } |
192 | 202 |
193 // static | 203 // static |
194 void BrowserList::MoveBrowsersInWorkspaceToFront( | 204 void BrowserList::MoveBrowsersInWorkspaceToFront( |
195 const std::string& new_workspace) { | 205 const std::string& new_workspace) { |
196 DCHECK(!new_workspace.empty()); | 206 DCHECK(!new_workspace.empty()); |
197 | 207 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 } | 278 } |
269 | 279 |
270 // static | 280 // static |
271 void BrowserList::RemoveBrowserFrom(Browser* browser, | 281 void BrowserList::RemoveBrowserFrom(Browser* browser, |
272 BrowserVector* browser_list) { | 282 BrowserVector* browser_list) { |
273 BrowserVector::iterator remove_browser = | 283 BrowserVector::iterator remove_browser = |
274 std::find(browser_list->begin(), browser_list->end(), browser); | 284 std::find(browser_list->begin(), browser_list->end(), browser); |
275 if (remove_browser != browser_list->end()) | 285 if (remove_browser != browser_list->end()) |
276 browser_list->erase(remove_browser); | 286 browser_list->erase(remove_browser); |
277 } | 287 } |
OLD | NEW |