1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/browser_list.h"
#include <algorithm>
#include "base/auto_reset.h"
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/user_metrics.h"
#include "base/observer_list.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/buildflags.h"
#include "chrome/browser/lifetime/application_lifetime_desktop.h"
#include "chrome/browser/lifetime/browser_shutdown.h"
#include "chrome/browser/lifetime/termination_notification.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sessions/app_session_service_factory.h"
#include "chrome/browser/sessions/session_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list_enumerator.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/browser_window.h"
using base::UserMetricsAction;
using content::WebContents;
namespace {
BrowserList::BrowserWeakVector GetBrowsersToClose(Profile* profile) {
BrowserList::BrowserWeakVector browsers_to_close;
for (Browser* browser : *BrowserList::GetInstance()) {
if (browser->profile()->GetOriginalProfile() ==
profile->GetOriginalProfile()) {
browsers_to_close.push_back(browser->AsWeakPtr());
}
}
return browsers_to_close;
}
BrowserList::BrowserWeakVector GetIncognitoBrowsersToClose(Profile* profile) {
BrowserList::BrowserWeakVector browsers_to_close;
for (Browser* browser : *BrowserList::GetInstance()) {
if (browser->profile() == profile) {
browsers_to_close.push_back(browser->AsWeakPtr());
}
}
return browsers_to_close;
}
} // namespace
// static
base::LazyInstance<base::ObserverList<BrowserListObserver>,
BrowserList::ObserverListTraits>
BrowserList::observers_ = LAZY_INSTANCE_INITIALIZER;
// static
BrowserList* BrowserList::instance_ = nullptr;
////////////////////////////////////////////////////////////////////////////////
// BrowserList, public:
Browser* BrowserList::GetLastActive() const {
if (!browsers_ordered_by_activation_.empty()) {
return *(browsers_ordered_by_activation_.rbegin());
}
return nullptr;
}
void BrowserList::ForEachCurrentBrowser(
base::FunctionRef<void(Browser*)> on_browser) {
// Make a copy of the BrowserList to simplify the case where we need to
// add or remove a Browser during the loop.
constexpr bool kEnumerateNewBrowser = false;
BrowserListEnumerator browser_list_copy(kEnumerateNewBrowser);
while (!browser_list_copy.empty()) {
on_browser(browser_list_copy.Next());
}
}
void BrowserList::ForEachCurrentAndNewBrowser(
base::FunctionRef<void(Browser*)> on_browser) {
// Make a copy of the BrowserList to simplify the case where we need to
// add or remove a Browser during the loop.
constexpr bool kEnumerateNewBrowser = true;
BrowserListEnumerator browser_list_copy(kEnumerateNewBrowser);
while (!browser_list_copy.empty()) {
on_browser(browser_list_copy.Next());
}
}
// static
BrowserList* BrowserList::GetInstance() {
BrowserList** list = &instance_;
if (!*list) {
*list = new BrowserList;
}
return *list;
}
// static
void BrowserList::AddBrowser(Browser* browser) {
DCHECK(browser);
DCHECK(browser->window()) << "Browser should not be added to BrowserList "
"until it is fully constructed.";
GetInstance()->browsers_.push_back(browser);
browser->RegisterKeepAlive();
for (BrowserListObserver& observer : observers_.Get()) {
observer.OnBrowserAdded(browser);
}
AddBrowserToActiveList(browser);
if (browser->profile()->IsGuestSession()) {
base::UmaHistogramCounts100("Browser.WindowCount.Guest",
GetGuestBrowserCount());
} else if (browser->profile()->IsIncognitoProfile()) {
base::UmaHistogramCounts100(
"Browser.WindowCount.Incognito",
GetOffTheRecordBrowsersActiveForProfile(browser->profile()));
}
}
// static
void BrowserList::RemoveBrowser(Browser* browser) {
// Remove |browser| from the appropriate list instance.
BrowserList* browser_list = GetInstance();
RemoveBrowserFrom(browser, &browser_list->browsers_ordered_by_activation_);
browser_list->currently_closing_browsers_.erase(browser);
RemoveBrowserFrom(browser, &browser_list->browsers_);
for (BrowserListObserver& observer : observers_.Get()) {
observer.OnBrowserRemoved(browser);
}
browser->UnregisterKeepAlive();
// If we're exiting, send out the APP_TERMINATING notification to allow other
// modules to shut themselves down.
if (chrome::GetTotalBrowserCount() == 0 &&
(browser_shutdown::IsTryingToQuit() ||
g_browser_process->IsShuttingDown())) {
// Last browser has just closed, and this is a user-initiated quit or there
// is no module keeping the app alive, so send out our notification. No need
// to call ProfileManager::ShutdownSessionServices() as part of the
// shutdown, because Browser::WindowClosing() already makes sure that the
// SessionService is created and notified.
browser_shutdown::NotifyAppTerminating();
chrome::OnAppExiting();
}
}
// static
void BrowserList::AddBrowserToActiveList(Browser* browser) {
if (browser->IsActive()) {
SetLastActive(browser);
return;
}
// |BrowserList::browsers_ordered_by_activation_| should contain every
// browser, so prepend any inactive browsers to it.
BrowserVector* active_browsers =
&GetInstance()->browsers_ordered_by_activation_;
RemoveBrowserFrom(browser, active_browsers);
active_browsers->insert(active_browsers->begin(), browser);
}
// static
void BrowserList::AddObserver(BrowserListObserver* observer) {
observers_.Get().AddObserver(observer);
}
// static
void BrowserList::RemoveObserver(BrowserListObserver* observer) {
observers_.Get().RemoveObserver(observer);
}
// static
void BrowserList::CloseAllBrowsersWithProfile(Profile* profile) {
BrowserVector browsers_to_close;
for (Browser* browser : *BrowserList::GetInstance()) {
if (browser->profile()->GetOriginalProfile() ==
profile->GetOriginalProfile()) {
browsers_to_close.push_back(browser);
}
}
for (const auto& it : browsers_to_close) {
it->window()->Close();
}
}
// static
void BrowserList::CloseAllBrowsersWithProfile(
Profile* profile,
const CloseCallback& on_close_success,
const CloseCallback& on_close_aborted,
bool skip_beforeunload) {
SessionServiceFactory::ShutdownForProfile(profile);
AppSessionServiceFactory::ShutdownForProfile(profile);
TryToCloseBrowserList(GetBrowsersToClose(profile), on_close_success,
on_close_aborted, profile->GetPath(),
skip_beforeunload);
}
// static
void BrowserList::CloseAllBrowsersWithIncognitoProfile(
Profile* profile,
const CloseCallback& on_close_success,
const CloseCallback& on_close_aborted,
bool skip_beforeunload) {
DCHECK(profile->IsOffTheRecord());
auto browsers_to_close = GetIncognitoBrowsersToClose(profile);
// When closing devtools browser related to incognito browser, do not skip
// calling before unload handlers.
skip_beforeunload =
skip_beforeunload &&
std::ranges::none_of(browsers_to_close, &Browser::is_type_devtools);
TryToCloseBrowserList(browsers_to_close, on_close_success, on_close_aborted,
profile->GetPath(), skip_beforeunload);
}
// static
void BrowserList::TryToCloseBrowserList(
const BrowserWeakVector& browsers_to_close,
const CloseCallback& on_close_success,
const CloseCallback& on_close_aborted,
const base::FilePath& profile_path,
const bool skip_beforeunload) {
for (auto& weak_browser : browsers_to_close) {
if (weak_browser &&
weak_browser->TryToCloseWindow(
skip_beforeunload,
base::BindRepeating(&BrowserList::PostTryToCloseBrowserWindow,
browsers_to_close, on_close_success,
on_close_aborted, profile_path,
skip_beforeunload))) {
return;
}
}
if (on_close_success) {
on_close_success.Run(profile_path);
}
for (auto& weak_b : browsers_to_close) {
// BeforeUnload handlers may close browser windows, so we need to explicitly
// check whether they still exist.
if (weak_b && weak_b->window()) {
weak_b->window()->Close();
}
}
}
// static
void BrowserList::PostTryToCloseBrowserWindow(
const BrowserWeakVector& browsers_to_close,
const CloseCallback& on_close_success,
const CloseCallback& on_close_aborted,
const base::FilePath& profile_path,
const bool skip_beforeunload,
bool tab_close_confirmed) {
// We need this bool to avoid infinite recursion when resetting the
// BeforeUnload handlers, since doing that will trigger calls back to this
// method for each affected window.
static bool resetting_handlers = false;
if (tab_close_confirmed) {
TryToCloseBrowserList(browsers_to_close, on_close_success, on_close_aborted,
profile_path, skip_beforeunload);
} else if (!resetting_handlers) {
base::AutoReset<bool> resetting_handlers_scoper(&resetting_handlers, true);
for (auto& weak_browser : browsers_to_close) {
// This function is called asynchronously, so that the Browser may have
// been destroyed by the time we get here.
if (weak_browser) {
weak_browser->ResetTryToCloseWindow();
}
}
if (on_close_aborted) {
on_close_aborted.Run(profile_path);
}
}
}
// static
void BrowserList::MoveBrowsersInWorkspaceToFront(
const std::string& new_workspace) {
DCHECK(!new_workspace.empty());
BrowserList* instance = GetInstance();
Browser* old_last_active = instance->GetLastActive();
BrowserVector& last_active_browsers =
instance->browsers_ordered_by_activation_;
// Perform a stable partition on the browsers in the list so that the browsers
// in the new workspace appear after the browsers in the other workspaces.
//
// For example, if we have a list of browser-workspace pairs
// [{b1, 0}, {b2, 1}, {b3, 0}, {b4, 1}]
// and we switch to workspace 1, we want the resulting browser list to look
// like [{b1, 0}, {b3, 0}, {b2, 1}, {b4, 1}].
std::stable_partition(
last_active_browsers.begin(), last_active_browsers.end(),
[&new_workspace](Browser* browser) {
return !browser->window()->IsVisibleOnAllWorkspaces() &&
browser->window()->GetWorkspace() != new_workspace;
});
Browser* new_last_active = instance->GetLastActive();
if (old_last_active != new_last_active) {
for (BrowserListObserver& observer : observers_.Get()) {
observer.OnBrowserSetLastActive(new_last_active);
}
}
}
// static
void BrowserList::SetLastActive(Browser* browser) {
BrowserList* instance = GetInstance();
DCHECK(base::Contains(*instance, browser))
<< "SetLastActive called for a browser before the browser was added to "
"the BrowserList.";
DCHECK(browser->window())
<< "SetLastActive called for a browser with no window set.";
base::RecordAction(UserMetricsAction("ActiveBrowserChanged"));
RemoveBrowserFrom(browser, &instance->browsers_ordered_by_activation_);
instance->browsers_ordered_by_activation_.push_back(browser);
for (BrowserListObserver& observer : observers_.Get()) {
observer.OnBrowserSetLastActive(browser);
}
}
// static
void BrowserList::NotifyBrowserNoLongerActive(Browser* browser) {
BrowserList* instance = GetInstance();
DCHECK(base::Contains(*instance, browser))
<< "NotifyBrowserNoLongerActive called for a browser before the browser "
"was added to the BrowserList.";
DCHECK(browser->window())
<< "NotifyBrowserNoLongerActive called for a browser with no window set.";
for (BrowserListObserver& observer : observers_.Get()) {
observer.OnBrowserNoLongerActive(browser);
}
}
// static
void BrowserList::NotifyBrowserCloseCancelled(Browser* browser,
BrowserClosingStatus reason) {
for (BrowserListObserver& observer : observers_.Get()) {
observer.OnBrowserCloseCancelled(browser, reason);
}
}
// static
void BrowserList::NotifyBrowserCloseStarted(Browser* browser) {
GetInstance()->currently_closing_browsers_.insert(browser);
for (BrowserListObserver& observer : observers_.Get()) {
observer.OnBrowserClosing(browser);
}
}
// static
bool BrowserList::IsOffTheRecordBrowserActive() {
for (Browser* browser : *BrowserList::GetInstance()) {
if (browser->profile()->IsOffTheRecord()) {
return true;
}
}
return false;
}
// static
int BrowserList::GetOffTheRecordBrowsersActiveForProfile(Profile* profile) {
BrowserList* list = BrowserList::GetInstance();
return std::ranges::count_if(*list, [profile](Browser* browser) {
return browser->profile()->IsSameOrParent(profile) &&
browser->profile()->IsOffTheRecord() && !browser->is_type_devtools();
});
}
// static
size_t BrowserList::GetIncognitoBrowserCount() {
BrowserList* list = BrowserList::GetInstance();
return std::ranges::count_if(*list, [](Browser* browser) {
return browser->profile()->IsIncognitoProfile() &&
!browser->is_type_devtools();
});
}
// static
size_t BrowserList::GetGuestBrowserCount() {
BrowserList* list = BrowserList::GetInstance();
return std::ranges::count_if(*list, [](Browser* browser) {
return browser->profile()->IsGuestSession() && !browser->is_type_devtools();
});
}
// static
bool BrowserList::IsOffTheRecordBrowserInUse(Profile* profile) {
BrowserList* list = BrowserList::GetInstance();
return std::ranges::any_of(*list, [profile](Browser* browser) {
return browser->profile()->IsSameOrParent(profile) &&
browser->profile()->IsOffTheRecord();
});
}
////////////////////////////////////////////////////////////////////////////////
// BrowserList, private:
BrowserList::BrowserList() = default;
BrowserList::~BrowserList() = default;
// static
void BrowserList::RemoveBrowserFrom(Browser* browser,
BrowserVector* browser_list) {
auto remove_browser = std::ranges::find(*browser_list, browser);
if (remove_browser != browser_list->end()) {
browser_list->erase(remove_browser);
}
}
|