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/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/26 20:26:15
Why do you actually need this? You only ever use
Mike Lerman
2014/08/27 14:11:38
This is done in BrowserCloseManager where I got th
Peter Kasting
2014/08/27 19:31:34
I'm never OK with landing code the author and revi
Mike Lerman
2014/08/28 15:06:52
Answer: If I have multiple browsers open, and acce
| |
| 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 Loading... | |
| 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, | |
|
noms (inactive)
2014/08/26 20:17:32
I'm a little confused because CodeSearch still sho
Peter Kasting
2014/08/26 20:26:15
I think you're confused; that method is defined ju
Mike Lerman
2014/08/27 14:11:38
There is one place (Profile Deletion flow) that ca
Peter Kasting
2014/08/27 19:31:34
Can you do that in this CL? If not, don't worry a
Mike Lerman
2014/08/28 15:06:52
I'm going to see if the TPM's are okay with mergin
| |
| 141 const base::Callback<void(size_t)>& on_close_success, | |
| 142 const base::Callback<void(size_t)>& on_close_aborted) { | |
| 143 current_closing_browser_ = NULL; | |
| 144 BrowserVector browsers_to_close; | |
| 145 for (chrome::BrowserIterator it; !it.done(); it.Next()) { | |
| 146 if (it->profile()->GetOriginalProfile() == profile->GetOriginalProfile()) | |
| 147 browsers_to_close.push_back(*it); | |
| 148 } | |
| 149 | |
| 150 ProfileInfoCache& cache = | |
|
Peter Kasting
2014/08/26 20:26:15
Nit: Prefer pointers or const refs to non-const re
Mike Lerman
2014/08/27 14:11:38
Inlined, since the ProfileInfoCache is only used o
| |
| 151 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 152 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath()); | |
| 153 | |
| 154 TryToCloseBrowserList(browsers_to_close, on_close_success, | |
|
noms (inactive)
2014/08/26 20:17:32
Hmm, can this and CloseAllBrowsers just get merged
Mike Lerman
2014/08/27 14:11:38
TryToCloseBrowserList is called in PostBeforeUnloa
| |
| 155 on_close_aborted, profile_index); | |
|
Peter Kasting
2014/08/26 20:26:15
Nit: All lines of args must be aligned
Mike Lerman
2014/08/27 14:11:38
No longer an issue.
| |
| 156 } | |
| 157 | |
| 158 // static | |
| 159 void BrowserList::TryToCloseBrowserList(const BrowserVector& browsers_to_close, | |
| 160 const base::Callback<void(size_t)>& on_close_success, | |
| 161 const base::Callback<void(size_t)>& on_close_aborted, | |
| 162 const size_t profile_index) { | |
| 163 for (BrowserVector::const_iterator it = browsers_to_close.begin(); | |
| 164 it != browsers_to_close.end(); ++it) { | |
| 165 if ((*it)->CallBeforeUnloadHandlers( | |
| 166 base::Bind(&BrowserList::PostBeforeUnloadHandlers, | |
| 167 browsers_to_close, | |
| 168 on_close_success, | |
| 169 on_close_aborted, | |
| 170 profile_index))) { | |
| 171 current_closing_browser_ = *it; | |
| 172 return; | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 on_close_success.Run(profile_index); | |
| 177 | |
| 178 // Close() can invalidate the std::vector::iterator, so use array indexing. | |
| 179 Browser*const * browsers_ready_to_close = &browsers_to_close[0]; | |
| 180 for (size_t i = 0; i < browsers_to_close.size(); i++) | |
| 181 browsers_ready_to_close[i]->window()->Close(); | |
|
noms (inactive)
2014/08/26 20:17:32
The original function in CodeSearch shows that an
Peter Kasting
2014/08/26 20:26:15
Yeah, the difference between this and the above sc
Mike Lerman
2014/08/27 14:11:38
Okay, I shall dig further into why the iterator ge
Mike Lerman
2014/08/28 15:06:53
The reason there's a difference between here and a
| |
| 182 } | |
| 183 | |
| 184 // static | |
| 185 void BrowserList::PostBeforeUnloadHandlers( | |
| 186 const BrowserVector& browsers_to_close, | |
| 187 const base::Callback<void(size_t)>& on_close_success, | |
| 188 const base::Callback<void(size_t)>& on_close_aborted, | |
| 189 const size_t profile_index, | |
| 190 bool proceed) { | |
| 191 if (!current_closing_browser_) | |
| 192 return; | |
| 193 | |
| 194 current_closing_browser_ = NULL; | |
| 195 | |
| 196 if (proceed) { | |
| 197 TryToCloseBrowserList(browsers_to_close, on_close_success, on_close_aborted, | |
| 198 profile_index); | |
| 199 } else { | |
| 200 for (BrowserVector::const_iterator it = browsers_to_close.begin(); | |
| 201 it != browsers_to_close.end(); ++it) { | |
| 202 (*it)->ResetBeforeUnloadHandlers(); | |
| 203 } | |
| 204 on_close_aborted.Run(profile_index); | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 // static | |
| 133 void BrowserList::SetLastActive(Browser* browser) { | 209 void BrowserList::SetLastActive(Browser* browser) { |
| 134 content::RecordAction(UserMetricsAction("ActiveBrowserChanged")); | 210 content::RecordAction(UserMetricsAction("ActiveBrowserChanged")); |
| 135 BrowserList* browser_list = GetInstance(browser->host_desktop_type()); | 211 BrowserList* browser_list = GetInstance(browser->host_desktop_type()); |
| 136 | 212 |
| 137 RemoveBrowserFrom(browser, &browser_list->last_active_browsers_); | 213 RemoveBrowserFrom(browser, &browser_list->last_active_browsers_); |
| 138 browser_list->last_active_browsers_.push_back(browser); | 214 browser_list->last_active_browsers_.push_back(browser); |
| 139 | 215 |
| 140 FOR_EACH_OBSERVER(chrome::BrowserListObserver, observers_.Get(), | 216 FOR_EACH_OBSERVER(chrome::BrowserListObserver, observers_.Get(), |
| 141 OnBrowserSetLastActive(browser)); | 217 OnBrowserSetLastActive(browser)); |
| 142 } | 218 } |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 173 } | 249 } |
| 174 | 250 |
| 175 // static | 251 // static |
| 176 void BrowserList::RemoveBrowserFrom(Browser* browser, | 252 void BrowserList::RemoveBrowserFrom(Browser* browser, |
| 177 BrowserVector* browser_list) { | 253 BrowserVector* browser_list) { |
| 178 BrowserVector::iterator remove_browser = | 254 BrowserVector::iterator remove_browser = |
| 179 std::find(browser_list->begin(), browser_list->end(), browser); | 255 std::find(browser_list->begin(), browser_list->end(), browser); |
| 180 if (remove_browser != browser_list->end()) | 256 if (remove_browser != browser_list->end()) |
| 181 browser_list->erase(remove_browser); | 257 browser_list->erase(remove_browser); |
| 182 } | 258 } |
| OLD | NEW |