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 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
|
// Copyright 2013 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/ash/shelf/browser_status_monitor.h"
#include <memory>
#include "ash/public/cpp/shelf_types.h"
#include "base/containers/contains.h"
#include "base/debug/dump_without_crashing.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
#include "chrome/browser/ui/ash/shelf/app_service/app_service_app_window_shelf_controller.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_controller.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_controller_util.h"
#include "chrome/browser/ui/ash/shelf/shelf_spinner_controller.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_utils.h"
#include "chrome/common/chrome_features.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
namespace {
// Checks if a given browser is running a windowed app. It will return true for
// web apps, hosted apps, and packaged V1 apps.
bool IsAppBrowser(const Browser* browser) {
return (browser->is_type_app() || browser->is_type_app_popup()) &&
!web_app::GetAppIdFromApplicationName(browser->app_name()).empty();
}
Browser* GetBrowserWithTabStripModel(TabStripModel* tab_strip_model) {
for (Browser* browser : *BrowserList::GetInstance()) {
if (browser->tab_strip_model() == tab_strip_model) {
return browser;
}
}
return nullptr;
}
} // namespace
// This class monitors the WebContent of the all tab and notifies a navigation
// to the BrowserStatusMonitor.
class BrowserStatusMonitor::LocalWebContentsObserver
: public content::WebContentsObserver {
public:
LocalWebContentsObserver(content::WebContents* contents,
BrowserStatusMonitor* monitor)
: content::WebContentsObserver(contents), monitor_(monitor) {}
LocalWebContentsObserver(const LocalWebContentsObserver&) = delete;
LocalWebContentsObserver& operator=(const LocalWebContentsObserver&) = delete;
~LocalWebContentsObserver() override = default;
// content::WebContentsObserver
void PrimaryPageChanged(content::Page& page) override {
monitor_->OnTabNavigationFinished(web_contents());
}
private:
raw_ptr<BrowserStatusMonitor> monitor_;
};
BrowserStatusMonitor::BrowserStatusMonitor(
ChromeShelfController* shelf_controller)
: shelf_controller_(shelf_controller),
browser_tab_strip_tracker_(this, nullptr) {
DCHECK(shelf_controller_);
app_service_instance_helper_ =
shelf_controller->app_service_app_window_controller()
->app_service_instance_helper();
DCHECK(app_service_instance_helper_);
}
BrowserStatusMonitor::~BrowserStatusMonitor() {
DCHECK(initialized_);
BrowserList::RemoveObserver(this);
// Simulate OnBrowserRemoved() for all Browsers.
for (Browser* browser : *BrowserList::GetInstance()) {
OnBrowserRemoved(browser);
}
}
void BrowserStatusMonitor::Initialize() {
DCHECK(!initialized_);
initialized_ = true;
// Simulate OnBrowserAdded() for all existing Browsers.
for (Browser* browser : *BrowserList::GetInstance()) {
OnBrowserAdded(browser);
}
// BrowserList::AddObserver() comes before BrowserTabStripTracker::Init() to
// ensure that OnBrowserAdded() is always invoked before
// OnTabStripModelChanged() is invoked to describe the initial state of the
// Browser.
BrowserList::AddObserver(this);
browser_tab_strip_tracker_.Init();
}
void BrowserStatusMonitor::ActiveUserChanged(const std::string& user_email) {
// When the active profile changes, all windowed and tabbed apps owned by the
// newly selected profile are added to the shelf, and the ones owned by other
// profiles are removed.
for (Browser* browser : *BrowserList::GetInstance()) {
bool owned = multi_user_util::IsProfileFromActiveUser(browser->profile());
TabStripModel* tab_strip_model = browser->tab_strip_model();
if (browser->is_type_app() || browser->is_type_app_popup()) {
// Add windowed apps owned by the current profile, and remove the one
// owned by other profiles.
bool app_in_shelf = IsAppBrowserInShelf(browser);
content::WebContents* active_web_contents =
tab_strip_model->GetActiveWebContents();
if (owned && !app_in_shelf) {
// Adding an app to the shelf consists of two actions: add the browser
// (shelf item) and add the content (shelf item status).
AddAppBrowserToShelf(browser);
if (active_web_contents) {
shelf_controller_->UpdateAppState(active_web_contents,
false /*remove*/);
}
} else if (!owned && app_in_shelf) {
// Removing an app from the shelf requires to remove the content and
// the shelf item (reverse order of addition).
if (active_web_contents) {
shelf_controller_->UpdateAppState(active_web_contents,
true /*remove*/);
}
RemoveAppBrowserFromShelf(browser);
}
} else if (browser->is_type_normal()) {
// Add tabbed apps owned by the current profile, and remove the ones owned
// by other profiles.
for (int i = 0; i < tab_strip_model->count(); ++i) {
shelf_controller_->UpdateAppState(tab_strip_model->GetWebContentsAt(i),
!owned /*remove*/);
}
}
}
// Update the browser state since some of the additions/removals above might
// have had an impact on the browser item state.
UpdateBrowserItemState();
}
void BrowserStatusMonitor::UpdateAppItemState(content::WebContents* contents,
bool remove) {
DCHECK(contents);
DCHECK(initialized_);
// It is possible to come here from Browser::SwapTabContent where the contents
// cannot be associated with a browser. A removal however should be properly
// processed.
Browser* browser = chrome::FindBrowserWithTab(contents);
if (remove || (browser && multi_user_util::IsProfileFromActiveUser(
browser->profile()))) {
shelf_controller_->UpdateAppState(contents, remove);
}
}
void BrowserStatusMonitor::UpdateBrowserItemState() {
DCHECK(initialized_);
shelf_controller_->UpdateBrowserItemState();
}
void BrowserStatusMonitor::OnBrowserAdded(Browser* browser) {
DCHECK(initialized_);
#if DCHECK_IS_ON()
auto insert_result = known_browsers_.insert(browser);
DCHECK(insert_result.second);
#endif
if (IsAppBrowser(browser) &&
multi_user_util::IsProfileFromActiveUser(browser->profile())) {
AddAppBrowserToShelf(browser);
}
}
void BrowserStatusMonitor::OnBrowserRemoved(Browser* browser) {
DCHECK(initialized_);
#if DCHECK_IS_ON()
size_t num_removed = known_browsers_.erase(browser);
DCHECK_EQ(num_removed, 1U);
#endif
if (IsAppBrowser(browser) &&
multi_user_util::IsProfileFromActiveUser(browser->profile())) {
RemoveAppBrowserFromShelf(browser);
}
UpdateBrowserItemState();
if (app_service_instance_helper_) {
app_service_instance_helper_->OnBrowserRemoved();
}
}
void BrowserStatusMonitor::OnTabStripModelChanged(
TabStripModel* tab_strip_model,
const TabStripModelChange& change,
const TabStripSelectionChange& selection) {
// OnBrowserAdded() must be invoked before OnTabStripModelChanged(). See
// comment in constructor.
Browser* browser = GetBrowserWithTabStripModel(tab_strip_model);
#if DCHECK_IS_ON()
DCHECK(base::Contains(known_browsers_, browser));
#endif
if (change.type() == TabStripModelChange::kInserted) {
for (const auto& contents : change.GetInsert()->contents) {
if (base::Contains(webcontents_to_observer_map_, contents.contents)) {
#if DCHECK_IS_ON()
{
// The tab must be in the set of tabs in transit.
size_t num_removed = tabs_in_transit_.erase(contents.contents.get());
DCHECK_EQ(num_removed, 1u);
}
#endif
OnTabMoved(tab_strip_model, contents.contents);
} else {
#if DCHECK_IS_ON()
DCHECK(!base::Contains(tabs_in_transit_, contents.contents));
#endif
OnTabInserted(tab_strip_model, contents.contents);
}
}
UpdateBrowserItemState();
} else if (change.type() == TabStripModelChange::kRemoved) {
auto* remove = change.GetRemove();
for (const auto& contents : remove->contents) {
switch (contents.remove_reason) {
case TabStripModelChange::RemoveReason::kDeleted:
#if DCHECK_IS_ON()
DCHECK(!base::Contains(tabs_in_transit_, contents.contents));
#endif
OnTabClosing(contents.contents);
break;
case TabStripModelChange::RemoveReason::kInsertedIntoOtherTabStrip:
// The tab will be reinserted immediately into another browser, so
// this event is ignored.
if (browser->is_type_devtools()) {
// TODO(crbug.com/40773744): when a dev tools window is docked, and
// its WebContents is removed, it will not be reinserted into
// another tab strip, so it should be treated as closed.
OnTabClosing(contents.contents);
} else {
#if DCHECK_IS_ON()
// The tab must not be already in the set of tabs in transit.
DCHECK(tabs_in_transit_.insert(contents.contents).second);
#endif
}
break;
}
}
} else if (change.type() == TabStripModelChange::kReplaced) {
auto* replace = change.GetReplace();
OnTabReplaced(tab_strip_model, replace->old_contents,
replace->new_contents);
}
if (tab_strip_model->empty()) {
return;
}
if (selection.active_tab_changed()) {
OnActiveTabChanged(selection.old_contents, selection.new_contents);
}
}
void BrowserStatusMonitor::AddAppBrowserToShelf(Browser* browser) {
DCHECK(IsAppBrowser(browser));
DCHECK(initialized_);
std::string app_id =
web_app::GetAppIdFromApplicationName(browser->app_name());
DCHECK(!app_id.empty());
if (!IsAppBrowserInShelfWithAppId(app_id)) {
if (auto* chrome_controller = ChromeShelfController::instance()) {
chrome_controller->GetShelfSpinnerController()->CloseSpinner(app_id);
}
shelf_controller_->SetAppStatus(app_id, ash::STATUS_RUNNING);
}
browser_to_app_id_map_[browser] = app_id;
}
void BrowserStatusMonitor::RemoveAppBrowserFromShelf(Browser* browser) {
DCHECK(IsAppBrowser(browser));
DCHECK(initialized_);
auto iter = browser_to_app_id_map_.find(browser);
if (iter != browser_to_app_id_map_.end()) {
std::string app_id = iter->second;
browser_to_app_id_map_.erase(iter);
if (!IsAppBrowserInShelfWithAppId(app_id)) {
shelf_controller_->SetAppStatus(app_id, ash::STATUS_CLOSED);
}
}
}
bool BrowserStatusMonitor::IsAppBrowserInShelf(Browser* browser) {
return browser_to_app_id_map_.find(browser) != browser_to_app_id_map_.end();
}
bool BrowserStatusMonitor::IsAppBrowserInShelfWithAppId(
const std::string& app_id) {
for (const auto& iter : browser_to_app_id_map_) {
if (iter.second == app_id) {
return true;
}
}
return false;
}
void BrowserStatusMonitor::OnActiveTabChanged(
content::WebContents* old_contents,
content::WebContents* new_contents) {
Browser* browser = nullptr;
// Use |new_contents|. |old_contents| could be nullptr.
DCHECK(new_contents);
browser = chrome::FindBrowserWithTab(new_contents);
// Update immediately on a tab change.
if (old_contents &&
(TabStripModel::kNoTab !=
browser->tab_strip_model()->GetIndexOfWebContents(old_contents))) {
UpdateAppItemState(old_contents, false /*remove*/);
}
if (new_contents) {
UpdateAppItemState(new_contents, false /*remove*/);
UpdateBrowserItemState();
SetShelfIDForBrowserWindowContents(browser, new_contents);
}
if (app_service_instance_helper_) {
app_service_instance_helper_->OnActiveTabChanged(old_contents,
new_contents);
}
}
void BrowserStatusMonitor::OnTabReplaced(TabStripModel* tab_strip_model,
content::WebContents* old_contents,
content::WebContents* new_contents) {
DCHECK(old_contents && new_contents);
Browser* browser = chrome::FindBrowserWithTab(new_contents);
UpdateAppItemState(old_contents, true /*remove*/);
RemoveWebContentsObserver(old_contents);
UpdateAppItemState(new_contents, false /*remove*/);
UpdateBrowserItemState();
if (browser && IsAppBrowserInShelf(browser) &&
multi_user_util::IsProfileFromActiveUser(browser->profile())) {
shelf_controller_->SetAppStatus(
web_app::GetAppIdFromApplicationName(browser->app_name()),
ash::STATUS_RUNNING);
}
if (tab_strip_model->GetActiveWebContents() == new_contents) {
SetShelfIDForBrowserWindowContents(browser, new_contents);
}
AddWebContentsObserver(new_contents);
if (app_service_instance_helper_) {
app_service_instance_helper_->OnTabReplaced(old_contents, new_contents);
}
}
void BrowserStatusMonitor::OnTabInserted(TabStripModel* tab_strip_model,
content::WebContents* contents) {
UpdateAppItemState(contents, false /*remove*/);
// If the visible navigation entry is the initial entry, wait until a
// navigation status changes before setting the browser window Shelf ID
// (done by the web contents observer added by AddWebContentsObserver()).
if (tab_strip_model->GetActiveWebContents() == contents &&
!contents->GetController().GetVisibleEntry()->IsInitialEntry()) {
Browser* browser = chrome::FindBrowserWithTab(contents);
SetShelfIDForBrowserWindowContents(browser, contents);
}
AddWebContentsObserver(contents);
if (app_service_instance_helper_) {
app_service_instance_helper_->OnTabInserted(contents);
}
}
void BrowserStatusMonitor::OnTabClosing(content::WebContents* contents) {
UpdateAppItemState(contents, true /*remove*/);
RemoveWebContentsObserver(contents);
if (app_service_instance_helper_) {
app_service_instance_helper_->OnTabClosing(contents);
}
}
void BrowserStatusMonitor::OnTabMoved(TabStripModel* tab_strip_model,
content::WebContents* contents) {
// TODO(crbug.com/40763808): split this into inserted and moved cases.
OnTabInserted(tab_strip_model, contents);
}
void BrowserStatusMonitor::OnTabNavigationFinished(
content::WebContents* contents) {
UpdateAppItemState(contents, false /*remove*/);
UpdateBrowserItemState();
// Navigating may change the ShelfID associated with the WebContents.
Browser* browser = chrome::FindBrowserWithTab(contents);
if (browser &&
browser->tab_strip_model()->GetActiveWebContents() == contents) {
SetShelfIDForBrowserWindowContents(browser, contents);
}
}
void BrowserStatusMonitor::AddWebContentsObserver(
content::WebContents* contents) {
if (webcontents_to_observer_map_.find(contents) ==
webcontents_to_observer_map_.end()) {
webcontents_to_observer_map_[contents] =
std::make_unique<LocalWebContentsObserver>(contents, this);
}
}
void BrowserStatusMonitor::RemoveWebContentsObserver(
content::WebContents* contents) {
DCHECK(webcontents_to_observer_map_.find(contents) !=
webcontents_to_observer_map_.end());
webcontents_to_observer_map_.erase(contents);
}
void BrowserStatusMonitor::SetShelfIDForBrowserWindowContents(
Browser* browser,
content::WebContents* web_contents) {
shelf_controller_->SetShelfIDForBrowserWindowContents(browser, web_contents);
if (app_service_instance_helper_) {
app_service_instance_helper_->OnSetShelfIDForBrowserWindowContents(
web_contents);
}
}
|