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

Side by Side Diff: chrome/browser/ui/browser_list.cc

Issue 471763008: Nicely handle OnBeforeUnloads with guest and lock mode. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleanup browser close; callback reset Created 6 years, 3 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 (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/logging.h" 9 #include "base/logging.h"
10 #include "chrome/browser/browser_process.h" 10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/browser_shutdown.h" 11 #include "chrome/browser/browser_shutdown.h"
12 #include "chrome/browser/chrome_notification_types.h" 12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/lifetime/application_lifetime.h" 13 #include "chrome/browser/lifetime/application_lifetime.h"
14 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_info_cache.h"
16 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/ui/browser.h" 17 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_finder.h" 18 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/browser/ui/browser_iterator.h" 19 #include "chrome/browser/ui/browser_iterator.h"
18 #include "chrome/browser/ui/browser_list_observer.h" 20 #include "chrome/browser/ui/browser_list_observer.h"
19 #include "chrome/browser/ui/browser_window.h" 21 #include "chrome/browser/ui/browser_window.h"
20 #include "chrome/browser/ui/host_desktop.h" 22 #include "chrome/browser/ui/host_desktop.h"
21 #include "content/public/browser/notification_service.h" 23 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/user_metrics.h" 24 #include "content/public/browser/user_metrics.h"
23 25
24 using base::UserMetricsAction; 26 using base::UserMetricsAction;
25 using content::WebContents; 27 using content::WebContents;
26 28
27 // static 29 // static
28 base::LazyInstance<ObserverList<chrome::BrowserListObserver> >::Leaky 30 base::LazyInstance<ObserverList<chrome::BrowserListObserver> >::Leaky
29 BrowserList::observers_ = LAZY_INSTANCE_INITIALIZER; 31 BrowserList::observers_ = LAZY_INSTANCE_INITIALIZER;
30 32
31 // static 33 // static
32 BrowserList* BrowserList::native_instance_ = NULL; 34 BrowserList* BrowserList::native_instance_ = NULL;
33 BrowserList* BrowserList::ash_instance_ = NULL; 35 BrowserList* BrowserList::ash_instance_ = NULL;
34 36
37 // static
38 // The current browser handling its OnBeforeUnload event before closing.
39 Browser* current_closing_browser_;
Peter Kasting 2014/08/28 18:46:12 You don't care about the actual pointer value here
Mike Lerman 2014/08/29 18:02:21 Done.
40
35 //////////////////////////////////////////////////////////////////////////////// 41 ////////////////////////////////////////////////////////////////////////////////
36 // BrowserList, public: 42 // BrowserList, public:
37 43
38 Browser* BrowserList::GetLastActive() const { 44 Browser* BrowserList::GetLastActive() const {
39 if (!last_active_browsers_.empty()) 45 if (!last_active_browsers_.empty())
40 return *(last_active_browsers_.rbegin()); 46 return *(last_active_browsers_.rbegin());
41 return NULL; 47 return NULL;
42 } 48 }
43 49
44 // static 50 // static
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 // static 115 // static
110 void BrowserList::AddObserver(chrome::BrowserListObserver* observer) { 116 void BrowserList::AddObserver(chrome::BrowserListObserver* observer) {
111 observers_.Get().AddObserver(observer); 117 observers_.Get().AddObserver(observer);
112 } 118 }
113 119
114 // static 120 // static
115 void BrowserList::RemoveObserver(chrome::BrowserListObserver* observer) { 121 void BrowserList::RemoveObserver(chrome::BrowserListObserver* observer) {
116 observers_.Get().RemoveObserver(observer); 122 observers_.Get().RemoveObserver(observer);
117 } 123 }
118 124
125 // static
119 void BrowserList::CloseAllBrowsersWithProfile(Profile* profile) { 126 void BrowserList::CloseAllBrowsersWithProfile(Profile* profile) {
120 BrowserVector browsers_to_close; 127 BrowserVector browsers_to_close;
121 for (chrome::BrowserIterator it; !it.done(); it.Next()) { 128 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
122 if (it->profile()->GetOriginalProfile() == profile->GetOriginalProfile()) 129 if (it->profile()->GetOriginalProfile() == profile->GetOriginalProfile())
123 browsers_to_close.push_back(*it); 130 browsers_to_close.push_back(*it);
124 } 131 }
125 132
126 for (BrowserVector::const_iterator it = browsers_to_close.begin(); 133 for (BrowserVector::const_iterator it = browsers_to_close.begin();
127 it != browsers_to_close.end(); ++it) { 134 it != browsers_to_close.end(); ++it) {
128 (*it)->window()->Close(); 135 (*it)->window()->Close();
129 } 136 }
130 } 137 }
131 138
132 // static 139 // static
140 void BrowserList::CloseAllBrowsersWithProfile(Profile* profile,
141 const base::Callback<void(size_t)>& on_close_success) {
142 current_closing_browser_ = NULL;
143 BrowserVector browsers_to_close;
144 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
145 if (it->profile()->GetOriginalProfile() == profile->GetOriginalProfile())
146 browsers_to_close.push_back(*it);
147 }
148
149 size_t profile_index = g_browser_process->profile_manager()->
150 GetProfileInfoCache().GetIndexOfProfileWithPath(profile->GetPath());
151 TryToCloseBrowserList(browsers_to_close, on_close_success, profile_index);
152 }
153
154 // static
155 void BrowserList::TryToCloseBrowserList(const BrowserVector& browsers_to_close,
156 const base::Callback<void(size_t)>& on_close_success,
157 const size_t profile_index) {
158 for (BrowserVector::const_iterator it = browsers_to_close.begin();
159 it != browsers_to_close.end(); ++it) {
160 if ((*it)->CallBeforeUnloadHandlers(
161 base::Bind(&BrowserList::PostBeforeUnloadHandlers,
162 browsers_to_close,
163 on_close_success,
164 profile_index))) {
165 current_closing_browser_ = *it;
166 return;
167 }
168 }
169
170 on_close_success.Run(profile_index);
Peter Kasting 2014/08/28 18:46:12 It surprises me that we run this before we've actu
Mike Lerman 2014/08/29 18:02:21 Not important. Moved.
171
172 for (BrowserVector::const_iterator it = browsers_to_close.begin();
173 it != browsers_to_close.end(); ++it) {
Peter Kasting 2014/08/28 18:46:12 Nit: {} not necessary
Mike Lerman 2014/08/29 18:02:21 Done.
174 (*it)->window()->Close();
175 }
176 }
177
178 // static
179 void BrowserList::PostBeforeUnloadHandlers(
180 const BrowserVector& browsers_to_close,
181 const base::Callback<void(size_t)>& on_close_success,
182 const size_t profile_index,
183 bool proceed) {
184 // Process only one call if the latter of multiple browsers clicks abort.
Peter Kasting 2014/08/28 18:46:12 It's still unclear to me why this code has this ef
Mike Lerman 2014/08/29 18:02:21 I've improved the documentation in browser_list.h
185 if (!current_closing_browser_)
186 return;
187
188 current_closing_browser_ = NULL;
189
190 if (proceed) {
191 TryToCloseBrowserList(browsers_to_close, on_close_success, profile_index);
192 } else {
193 for (BrowserVector::const_iterator it = browsers_to_close.begin();
194 it != browsers_to_close.end(); ++it) {
195 (*it)->ResetBeforeUnloadHandlers();
196 }
197 }
198 }
199
200 // static
133 void BrowserList::SetLastActive(Browser* browser) { 201 void BrowserList::SetLastActive(Browser* browser) {
134 content::RecordAction(UserMetricsAction("ActiveBrowserChanged")); 202 content::RecordAction(UserMetricsAction("ActiveBrowserChanged"));
135 BrowserList* browser_list = GetInstance(browser->host_desktop_type()); 203 BrowserList* browser_list = GetInstance(browser->host_desktop_type());
136 204
137 RemoveBrowserFrom(browser, &browser_list->last_active_browsers_); 205 RemoveBrowserFrom(browser, &browser_list->last_active_browsers_);
138 browser_list->last_active_browsers_.push_back(browser); 206 browser_list->last_active_browsers_.push_back(browser);
139 207
140 FOR_EACH_OBSERVER(chrome::BrowserListObserver, observers_.Get(), 208 FOR_EACH_OBSERVER(chrome::BrowserListObserver, observers_.Get(),
141 OnBrowserSetLastActive(browser)); 209 OnBrowserSetLastActive(browser));
142 } 210 }
(...skipping 30 matching lines...) Expand all
173 } 241 }
174 242
175 // static 243 // static
176 void BrowserList::RemoveBrowserFrom(Browser* browser, 244 void BrowserList::RemoveBrowserFrom(Browser* browser,
177 BrowserVector* browser_list) { 245 BrowserVector* browser_list) {
178 BrowserVector::iterator remove_browser = 246 BrowserVector::iterator remove_browser =
179 std::find(browser_list->begin(), browser_list->end(), browser); 247 std::find(browser_list->begin(), browser_list->end(), browser);
180 if (remove_browser != browser_list->end()) 248 if (remove_browser != browser_list->end())
181 browser_list->erase(remove_browser); 249 browser_list->erase(remove_browser);
182 } 250 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698