Chromium Code Reviews| 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/logging.h" | 10 #include "base/logging.h" |
| 10 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/browser_shutdown.h" | 12 #include "chrome/browser/browser_shutdown.h" |
| 12 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
| 13 #include "chrome/browser/lifetime/application_lifetime.h" | 14 #include "chrome/browser/lifetime/application_lifetime.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/browser_finder.h" | 17 #include "chrome/browser/ui/browser_finder.h" |
| 17 #include "chrome/browser/ui/browser_iterator.h" | 18 #include "chrome/browser/ui/browser_iterator.h" |
| 18 #include "chrome/browser/ui/browser_list_observer.h" | 19 #include "chrome/browser/ui/browser_list_observer.h" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 // static | 110 // static |
| 110 void BrowserList::AddObserver(chrome::BrowserListObserver* observer) { | 111 void BrowserList::AddObserver(chrome::BrowserListObserver* observer) { |
| 111 observers_.Get().AddObserver(observer); | 112 observers_.Get().AddObserver(observer); |
| 112 } | 113 } |
| 113 | 114 |
| 114 // static | 115 // static |
| 115 void BrowserList::RemoveObserver(chrome::BrowserListObserver* observer) { | 116 void BrowserList::RemoveObserver(chrome::BrowserListObserver* observer) { |
| 116 observers_.Get().RemoveObserver(observer); | 117 observers_.Get().RemoveObserver(observer); |
| 117 } | 118 } |
| 118 | 119 |
| 120 // static | |
| 119 void BrowserList::CloseAllBrowsersWithProfile(Profile* profile) { | 121 void BrowserList::CloseAllBrowsersWithProfile(Profile* profile) { |
| 120 BrowserVector browsers_to_close; | 122 BrowserVector browsers_to_close; |
| 121 for (chrome::BrowserIterator it; !it.done(); it.Next()) { | 123 for (chrome::BrowserIterator it; !it.done(); it.Next()) { |
| 122 if (it->profile()->GetOriginalProfile() == profile->GetOriginalProfile()) | 124 if (it->profile()->GetOriginalProfile() == profile->GetOriginalProfile()) |
| 123 browsers_to_close.push_back(*it); | 125 browsers_to_close.push_back(*it); |
| 124 } | 126 } |
| 125 | 127 |
| 126 for (BrowserVector::const_iterator it = browsers_to_close.begin(); | 128 for (BrowserVector::const_iterator it = browsers_to_close.begin(); |
| 127 it != browsers_to_close.end(); ++it) { | 129 it != browsers_to_close.end(); ++it) { |
| 128 (*it)->window()->Close(); | 130 (*it)->window()->Close(); |
| 129 } | 131 } |
| 130 } | 132 } |
| 131 | 133 |
| 132 // static | 134 // static |
| 135 void BrowserList::CloseAllBrowsersWithProfile(Profile* profile, | |
| 136 const base::Callback<void(const base::FilePath&)>& on_close_success) { | |
| 137 BrowserVector browsers_to_close; | |
| 138 for (chrome::BrowserIterator it; !it.done(); it.Next()) { | |
| 139 if (it->profile()->GetOriginalProfile() == profile->GetOriginalProfile()) | |
| 140 browsers_to_close.push_back(*it); | |
| 141 } | |
| 142 | |
| 143 TryToCloseBrowserList(browsers_to_close, | |
| 144 on_close_success, | |
| 145 profile->GetPath()); | |
| 146 } | |
| 147 | |
| 148 // static | |
| 149 void BrowserList::TryToCloseBrowserList(const BrowserVector& browsers_to_close, | |
| 150 const base::Callback<void(const base::FilePath&)>& on_close_success, | |
| 151 const base::FilePath& profile_path) { | |
| 152 for (BrowserVector::const_iterator it = browsers_to_close.begin(); | |
| 153 it != browsers_to_close.end(); ++it) { | |
| 154 if ((*it)->CallBeforeUnloadHandlers( | |
| 155 base::Bind(&BrowserList::PostBeforeUnloadHandlers, | |
| 156 browsers_to_close, | |
| 157 on_close_success, | |
| 158 profile_path))) { | |
| 159 return; | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 for (BrowserVector::const_iterator it = browsers_to_close.begin(); | |
| 164 it != browsers_to_close.end(); ++it) | |
| 165 (*it)->window()->Close(); | |
| 166 | |
| 167 on_close_success.Run(profile_path); | |
| 168 } | |
| 169 | |
| 170 // static | |
| 171 void BrowserList::PostBeforeUnloadHandlers( | |
| 172 const BrowserVector& browsers_to_close, | |
| 173 const base::Callback<void(const base::FilePath&)>& on_close_success, | |
| 174 const base::FilePath& profile_path, | |
| 175 bool tab_close_confirmed) { | |
| 176 // We need this bool to avoid infinite recursion when resetting the | |
| 177 // BeforeUnload handlers, since doing that will trigger calls back to this | |
| 178 // method for each affected window. | |
| 179 static bool resetting_handlers = false; | |
| 180 | |
| 181 if (tab_close_confirmed) { | |
| 182 TryToCloseBrowserList(browsers_to_close, on_close_success, profile_path); | |
| 183 } else if (!resetting_handlers) { | |
| 184 base::AutoReset<bool> resetting_handlers_scoper(&resetting_handlers, true); | |
| 185 for (BrowserVector::const_iterator it = browsers_to_close.begin(); | |
| 186 it != browsers_to_close.end(); ++it) { | |
|
Peter Kasting
2014/09/02 19:40:34
Nit: Indent one more
Mike Lerman
2014/09/03 15:28:34
Done.
| |
| 187 (*it)->ResetBeforeUnloadHandlers(); | |
| 188 } | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 // static | |
| 133 void BrowserList::SetLastActive(Browser* browser) { | 193 void BrowserList::SetLastActive(Browser* browser) { |
| 134 content::RecordAction(UserMetricsAction("ActiveBrowserChanged")); | 194 content::RecordAction(UserMetricsAction("ActiveBrowserChanged")); |
| 135 BrowserList* browser_list = GetInstance(browser->host_desktop_type()); | 195 BrowserList* browser_list = GetInstance(browser->host_desktop_type()); |
| 136 | 196 |
| 137 RemoveBrowserFrom(browser, &browser_list->last_active_browsers_); | 197 RemoveBrowserFrom(browser, &browser_list->last_active_browsers_); |
| 138 browser_list->last_active_browsers_.push_back(browser); | 198 browser_list->last_active_browsers_.push_back(browser); |
| 139 | 199 |
| 140 FOR_EACH_OBSERVER(chrome::BrowserListObserver, observers_.Get(), | 200 FOR_EACH_OBSERVER(chrome::BrowserListObserver, observers_.Get(), |
| 141 OnBrowserSetLastActive(browser)); | 201 OnBrowserSetLastActive(browser)); |
| 142 } | 202 } |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 173 } | 233 } |
| 174 | 234 |
| 175 // static | 235 // static |
| 176 void BrowserList::RemoveBrowserFrom(Browser* browser, | 236 void BrowserList::RemoveBrowserFrom(Browser* browser, |
| 177 BrowserVector* browser_list) { | 237 BrowserVector* browser_list) { |
| 178 BrowserVector::iterator remove_browser = | 238 BrowserVector::iterator remove_browser = |
| 179 std::find(browser_list->begin(), browser_list->end(), browser); | 239 std::find(browser_list->begin(), browser_list->end(), browser); |
| 180 if (remove_browser != browser_list->end()) | 240 if (remove_browser != browser_list->end()) |
| 181 browser_list->erase(remove_browser); | 241 browser_list->erase(remove_browser); |
| 182 } | 242 } |
| OLD | NEW |