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 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
|
// Copyright 2024 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/webui/top_chrome/webui_contents_preload_manager.h"
#include <memory>
#include <optional>
#include <string>
#include "base/auto_reset.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/memory/memory_pressure_monitor.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/no_destructor.h"
#include "base/scoped_observation.h"
#include "base/strings/strcat.h"
#include "base/timer/timer.h"
#include "chrome/browser/page_load_metrics/page_load_metrics_initialize.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/task_manager/web_contents_tags.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/prefs/prefs_tab_helper.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/webui/log_web_ui_url.h"
#include "chrome/browser/ui/webui/top_chrome/per_profile_webui_tracker.h"
#include "chrome/browser/ui/webui/top_chrome/preload_context.h"
#include "chrome/browser/ui/webui/top_chrome/profile_preload_candidate_selector.h"
#include "chrome/browser/ui/webui/top_chrome/top_chrome_web_ui_controller.h"
#include "chrome/browser/ui/webui/top_chrome/top_chrome_webui_config.h"
#include "chrome/browser/ui/webui/top_chrome/webui_contents_preload_state.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "components/crash/core/common/crash_key.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_user_data.h"
#include "ui/base/models/menu_model.h"
#include "url/gurl.h"
#include "url/url_constants.h"
namespace {
// Enum class representing the results of attempting to use a preloaded WebUI
// when WebUIContentsPreloadedManager::Request() is called.
// The description of each value is also in tools/metrics/histograms/enums.xml.
enum class WebUIPreloadResult {
// No preloaded WebUI is available when a WebUI is requested.
kNoPreload = 0,
// The preloaded WebUI matches the requested WebUI.
kHit = 1,
// The preloaded WebUI is redirected to the requested WebUI.
kHitRedirected = 2,
// The preloaded WebUI does not match the requested WebUI and cannot be
// redirected.
kMiss = 3,
kMaxValue = kMiss,
};
// A candidate selector that always preloads a fixed WebUI.
class FixedCandidateSelector : public webui::PreloadCandidateSelector {
public:
explicit FixedCandidateSelector(GURL webui_url) : webui_url_(webui_url) {}
FixedCandidateSelector(const FixedCandidateSelector&) = default;
FixedCandidateSelector(FixedCandidateSelector&&) = default;
FixedCandidateSelector& operator=(const FixedCandidateSelector&) = default;
FixedCandidateSelector& operator=(FixedCandidateSelector&&) = default;
// webui::PreloadCandidateSelector:
void Init(const std::vector<GURL>& preloadable_urls) override {
DCHECK(base::Contains(preloadable_urls, webui_url_));
}
std::optional<GURL> GetURLToPreload(
const webui::PreloadContext& context) const override {
return IsUrlExcludedByFlag(webui_url_) ? std::nullopt
: std::make_optional(webui_url_);
}
private:
GURL webui_url_;
};
bool IsFeatureEnabled() {
return base::FeatureList::IsEnabled(features::kPreloadTopChromeWebUI);
}
bool IsSmartPreloadEnabled() {
return IsFeatureEnabled() &&
features::kPreloadTopChromeWebUISmartPreload.Get();
}
content::WebContents::CreateParams GetWebContentsCreateParams(
const GURL& webui_url,
content::BrowserContext* browser_context) {
content::WebContents::CreateParams create_params(browser_context);
// Set it to visible so that the resources are immediately loaded.
create_params.initially_hidden = !IsFeatureEnabled();
create_params.site_instance =
content::SiteInstance::CreateForURL(browser_context, webui_url);
return create_params;
}
content::WebUIController* GetWebUIController(
content::WebContents* web_contents) {
if (!web_contents) {
return nullptr;
}
content::WebUI* webui = web_contents->GetWebUI();
if (!webui) {
return nullptr;
}
return webui->GetController();
}
} // namespace
// A stub WebUI page embdeder that captures the ready-to-show signal.
class WebUIContentsPreloadManager::WebUIControllerEmbedderStub final
: public TopChromeWebUIController::Embedder {
public:
WebUIControllerEmbedderStub() = default;
~WebUIControllerEmbedderStub() = default;
// TopChromeWebUIController::Embedder:
void CloseUI() override {}
void ShowContextMenu(gfx::Point point,
std::unique_ptr<ui::MenuModel> menu_model) override {}
void HideContextMenu() override {}
void ShowUI() override {
CHECK(preload_state_);
preload_state_->ready_to_show = true;
}
// Attach this stub as the embedder of `web_contents`, assuming that the
// contents is not yet ready to be shown.
void AttachTo(content::WebContents* web_contents) {
CHECK_NE(web_contents, nullptr);
content::WebUIController* webui_controller =
GetWebUIController(web_contents);
if (!webui_controller) {
return;
}
// TODO(40168622): Add type check. This is currently not possible because a
// WebUIController subclass does not retain its parent class' type info.
auto* bubble_controller =
static_cast<TopChromeWebUIController*>(webui_controller);
bubble_controller->set_embedder(this->GetWeakPtr());
web_contents_ = web_contents;
preload_state_ = WebUIContentsPreloadState::FromWebContents(web_contents);
CHECK(preload_state_);
}
// Detach from the previously attached `web_contents`.
void Detach() {
if (!web_contents_) {
return;
}
content::WebUIController* webui_controller =
GetWebUIController(web_contents_);
if (!webui_controller) {
return;
}
auto* bubble_controller =
static_cast<TopChromeWebUIController*>(webui_controller);
bubble_controller->set_embedder(nullptr);
web_contents_ = nullptr;
preload_state_ = nullptr;
}
base::WeakPtr<WebUIControllerEmbedderStub> GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
private:
raw_ptr<content::WebContents> web_contents_ = nullptr;
raw_ptr<WebUIContentsPreloadState> preload_state_ = nullptr;
base::WeakPtrFactory<WebUIControllerEmbedderStub> weak_ptr_factory_{this};
};
class WebUIContentsPreloadManager::PendingPreload
: public content::WebContentsObserver,
public ProfileObserver {
public:
// Notifies the manager to preload when `busy_web_contents_to_watch` emits the
// first non-empty paint or when `deadline` has passed.
PendingPreload(WebUIContentsPreloadManager* manager,
Profile* profile,
content::WebContents* busy_web_contents_to_watch,
PreloadReason preload_reason,
base::TimeDelta deadline)
: manager_(manager), profile_(profile), preload_reason_(preload_reason) {
WebContentsObserver::Observe(busy_web_contents_to_watch);
profile_observation_.Observe(profile_);
deadline_timer_.Start(FROM_HERE, deadline, this, &PendingPreload::Preload);
}
void Preload() {
deadline_timer_.Stop();
WebContentsObserver::Observe(nullptr);
manager_->MaybePreloadForBrowserContext(profile_, preload_reason_);
}
// content::WebContentsObserver:
void DidFirstVisuallyNonEmptyPaint() override { Preload(); }
// ProfileObserver:
void OnProfileWillBeDestroyed(Profile* profile) override {
profile_observation_.Reset();
profile_ = nullptr;
WebContentsObserver::Observe(nullptr);
deadline_timer_.Stop();
}
private:
raw_ptr<WebUIContentsPreloadManager> manager_;
raw_ptr<Profile> profile_;
base::ScopedObservation<Profile, ProfileObserver> profile_observation_{this};
const PreloadReason preload_reason_;
base::OneShotTimer deadline_timer_;
};
using RequestResult = WebUIContentsPreloadManager::RequestResult;
RequestResult::RequestResult() = default;
RequestResult::~RequestResult() = default;
RequestResult::RequestResult(RequestResult&&) = default;
RequestResult& RequestResult::operator=(RequestResult&&) = default;
WebUIContentsPreloadManager::WebUIContentsPreloadManager() {
preload_mode_ =
static_cast<PreloadMode>(features::kPreloadTopChromeWebUIMode.Get());
webui_controller_embedder_stub_ =
std::make_unique<WebUIControllerEmbedderStub>();
webui_tracker_ = PerProfileWebUITracker::Create();
webui_tracker_observation_.Observe(webui_tracker_.get());
if (IsSmartPreloadEnabled()) {
// Use ProfilePreloadCandidateSelector to find the WebUI with the
// highest engagement score and is not present under the current profile.
SetPreloadCandidateSelector(
std::make_unique<webui::ProfilePreloadCandidateSelector>(
webui_tracker_.get()));
} else {
// Old behavior always preloads Tab Search.
SetPreloadCandidateSelector(std::make_unique<FixedCandidateSelector>(
GURL(chrome::kChromeUITabSearchURL)));
}
}
WebUIContentsPreloadManager::~WebUIContentsPreloadManager() = default;
// static
WebUIContentsPreloadManager* WebUIContentsPreloadManager::GetInstance() {
static base::NoDestructor<WebUIContentsPreloadManager> s_instance;
return s_instance.get();
}
void WebUIContentsPreloadManager::WarmupForBrowser(Browser* browser) {
// Most WebUIs, if not all, are hosted by a TYPE_NORMAL browser. This check
// skips unnecessary preloading for the majority of WebUIs.
if (!browser->is_type_normal()) {
return;
}
if (preload_mode_ == PreloadMode::kPreloadOnMakeContents) {
return;
}
CHECK_EQ(preload_mode_, PreloadMode::kPreloadOnWarmup);
if (IsDelayPreloadEnabled()) {
MaybePreloadForBrowserContextLater(
browser->profile(), browser->tab_strip_model()->GetActiveWebContents(),
PreloadReason::kBrowserWarmup);
} else {
MaybePreloadForBrowserContext(browser->profile(),
PreloadReason::kBrowserWarmup);
}
}
std::optional<GURL> WebUIContentsPreloadManager::GetNextWebUIURLToPreload(
content::BrowserContext* browser_context) const {
return preload_candidate_selector_->GetURLToPreload(
webui::PreloadContext::From(
Profile::FromBrowserContext(browser_context)));
}
std::vector<GURL> WebUIContentsPreloadManager::GetAllPreloadableWebUIURLs() {
// Retrieves top-chrome WebUIs that enables IsPreloadable() in its WebUI
// config.
std::vector<GURL> preloadable_urls;
TopChromeWebUIConfig::ForEachConfig([&preloadable_urls](
TopChromeWebUIConfig* config) {
if (config->IsPreloadable()) {
preloadable_urls.emplace_back(base::StrCat(
{config->scheme(), url::kStandardSchemeSeparator, config->host()}));
}
});
return preloadable_urls;
}
void WebUIContentsPreloadManager::SetPreloadCandidateSelector(
std::unique_ptr<webui::PreloadCandidateSelector>
preload_candidate_selector) {
preload_candidate_selector_ = std::move(preload_candidate_selector);
if (preload_candidate_selector_) {
preload_candidate_selector_->Init(GetAllPreloadableWebUIURLs());
}
}
void WebUIContentsPreloadManager::MaybePreloadForBrowserContext(
content::BrowserContext* browser_context, PreloadReason preload_reason) {
pending_preload_.reset();
if (!ShouldPreloadForBrowserContext(browser_context)) {
return;
}
// Usually destroying a WebContents may trigger preload, but if the
// destroy is caused by setting new preload contents, ignore it.
if (is_setting_preloaded_web_contents_) {
return;
}
std::optional<GURL> preload_url = GetNextWebUIURLToPreload(browser_context);
if (!preload_url.has_value()) {
return;
}
// Don't preload if already preloaded for this `browser_context`.
if (preloaded_web_contents_ &&
preloaded_web_contents_->GetBrowserContext() == browser_context &&
preloaded_web_contents_->GetVisibleURL().GetWithEmptyPath() ==
preload_url->GetWithEmptyPath()) {
return;
}
SetPreloadedContents(CreateNewContents(browser_context, *preload_url));
base::UmaHistogramEnumeration("WebUI.TopChrome.Preload.Reason",
preload_reason);
}
void WebUIContentsPreloadManager::MaybePreloadForBrowserContextLater(
content::BrowserContext* browser_context,
content::WebContents* busy_web_contents_to_watch,
PreloadReason preload_reason,
base::TimeDelta deadline) {
CHECK(!is_delay_preload_disabled_for_test_);
// Usually destroying a WebContents may trigger preload, but if the
// destroy is caused by setting new preload contents, ignore it.
if (is_setting_preloaded_web_contents_) {
return;
}
pending_preload_ = std::make_unique<PendingPreload>(
this, Profile::FromBrowserContext(browser_context),
busy_web_contents_to_watch, preload_reason, deadline);
}
std::unique_ptr<content::WebContents>
WebUIContentsPreloadManager::SetPreloadedContents(
std::unique_ptr<content::WebContents> web_contents) {
std::unique_ptr<content::WebContents> previous_preloaded_web_contents =
std::move(preloaded_web_contents_);
webui_controller_embedder_stub_->Detach();
profile_observation_.Reset();
base::AutoReset<bool> is_setting_preloaded_web_contents(
&is_setting_preloaded_web_contents_, true);
preloaded_web_contents_ = std::move(web_contents);
if (preloaded_web_contents_) {
webui_controller_embedder_stub_->AttachTo(preloaded_web_contents_.get());
profile_observation_.Observe(Profile::FromBrowserContext(
preloaded_web_contents_->GetBrowserContext()));
WebUIContentsPreloadState::FromWebContents(preloaded_web_contents_.get())
->preloaded = true;
}
return previous_preloaded_web_contents;
}
RequestResult WebUIContentsPreloadManager::Request(
const GURL& webui_url,
content::BrowserContext* browser_context) {
const base::TimeTicks request_time = base::TimeTicks::Now();
std::unique_ptr<content::WebContents> web_contents_ret;
WebUIPreloadResult preload_result = preloaded_web_contents_
? WebUIPreloadResult::kMiss
: WebUIPreloadResult::kNoPreload;
// Use preloaded contents if requested the same WebUI under the same browser
// context. Navigating to or from a blank page is also allowed.
// TODO(325836830): allow navigations between WebUIs.
if (preloaded_web_contents_ &&
preloaded_web_contents_->GetBrowserContext() == browser_context &&
(preloaded_web_contents_->GetURL().host() == webui_url.host() ||
preloaded_web_contents_->GetURL().IsAboutBlank() ||
webui_url.IsAboutBlank())) {
preload_result = WebUIPreloadResult::kHit;
// Redirect if requested a different URL.
if (!url::IsSameOriginWith(preloaded_web_contents_->GetURL(), webui_url)) {
preload_result = WebUIPreloadResult::kHitRedirected;
LoadURLForContents(preloaded_web_contents_.get(), webui_url);
}
web_contents_ret = std::move(preloaded_web_contents_);
SetPreloadedContents(nullptr);
} else {
web_contents_ret = CreateNewContents(browser_context, webui_url);
}
// Navigate to path if the request URL has a different path.
if (!is_navigation_disabled_for_test_ &&
webui_url.path() != web_contents_ret->GetURL().path()) {
CHECK(url::IsSameOriginWith(webui_url, web_contents_ret->GetURL()));
LoadURLForContents(web_contents_ret.get(), webui_url);
}
base::UmaHistogramEnumeration("WebUI.TopChrome.Preload.Result",
preload_result);
// Preload a new contents.
if (IsDelayPreloadEnabled()) {
MaybePreloadForBrowserContextLater(browser_context, web_contents_ret.get(),
PreloadReason::kWebUIRequested);
} else {
MaybePreloadForBrowserContext(browser_context,
PreloadReason::kWebUIRequested);
}
task_manager::WebContentsTags::ClearTag(web_contents_ret.get());
auto* preload_state =
WebUIContentsPreloadState::FromWebContents(web_contents_ret.get());
CHECK(preload_state);
preload_state->request_time = request_time;
// Non-preloaded WebUIs are logged by WebUIMainFrameObserver.
if (preload_state->preloaded) {
webui::LogWebUIShown(web_contents_ret->GetSiteInstance()->GetSiteURL());
}
RequestResult result;
result.web_contents = std::move(web_contents_ret);
result.is_ready_to_show = preload_state->ready_to_show;
return result;
}
std::optional<base::TimeTicks> WebUIContentsPreloadManager::GetRequestTime(
content::WebContents* web_contents) {
auto* preload_state =
WebUIContentsPreloadState::FromWebContents(web_contents);
if (!preload_state) {
return std::nullopt;
}
return preload_state->request_time;
}
bool WebUIContentsPreloadManager::WasPreloaded(
content::WebContents* web_contents) const {
if (!web_contents) {
return false;
}
auto* preload_state =
WebUIContentsPreloadState::FromWebContents(web_contents);
return preload_state ? preload_state->preloaded : false;
}
void WebUIContentsPreloadManager::DisableNavigationForTesting() {
is_navigation_disabled_for_test_ = true;
}
std::unique_ptr<content::WebContents>
WebUIContentsPreloadManager::CreateNewContents(
content::BrowserContext* browser_context,
GURL url) {
std::unique_ptr<content::WebContents> web_contents =
content::WebContents::Create(
GetWebContentsCreateParams(url, browser_context));
// Propagates user prefs to web contents.
// This is needed by, for example, text selection color on ChromeOS.
PrefsTabHelper::CreateForWebContents(web_contents.get());
WebUIContentsPreloadState::CreateForWebContents(web_contents.get());
task_manager::WebContentsTags::CreateForToolContents(
web_contents.get(), IDS_TASK_MANAGER_PRELOADED_RENDERER_FOR_UI);
InitializePageLoadMetricsForWebContents(web_contents.get());
webui_tracker_->AddWebContents(web_contents.get());
LoadURLForContents(web_contents.get(), url);
return web_contents;
}
void WebUIContentsPreloadManager::LoadURLForContents(
content::WebContents* web_contents,
GURL url) {
if (is_navigation_disabled_for_test_) {
return;
}
web_contents->GetController().LoadURL(url, content::Referrer(),
ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
std::string());
}
bool WebUIContentsPreloadManager::ShouldPreloadForBrowserContext(
content::BrowserContext* browser_context) const {
// Don't preload if the feature is disabled.
if (!IsFeatureEnabled()) {
return false;
}
if (browser_context->ShutdownStarted()) {
return false;
}
// Only preloads for regular profiles because WebContents::GetWebUI()
// may crash due to dangling RFH if navigation fails. See crbug.com/409389408.
// TODO(crbug.com/424551539): remove after fixing dangling RFH.
if (!Profile::FromBrowserContext(browser_context)->IsRegularProfile()) {
return false;
}
// Don't preload if under heavy memory pressure.
const auto* memory_monitor = base::MemoryPressureMonitor::Get();
if (memory_monitor && memory_monitor->GetCurrentPressureLevel() >=
base::MemoryPressureMonitor::MemoryPressureLevel::
MEMORY_PRESSURE_LEVEL_MODERATE) {
return false;
}
return true;
}
bool WebUIContentsPreloadManager::IsDelayPreloadEnabled() const {
if (is_delay_preload_disabled_for_test_) {
return false;
}
return IsFeatureEnabled() &&
features::kPreloadTopChromeWebUIDelayPreload.Get();
}
void WebUIContentsPreloadManager::OnProfileWillBeDestroyed(Profile* profile) {
profile_observation_.Reset();
if (!preloaded_web_contents_) {
return;
}
webui_controller_embedder_stub_->Detach();
CHECK_EQ(preloaded_web_contents_->GetBrowserContext(), profile);
preloaded_web_contents_.reset();
}
void WebUIContentsPreloadManager::OnWebContentsDestroyed(
content::WebContents* web_contents) {
// Triggers preloading when a WebUI is destroyed. Without this step, the
// preloaded content would only be the second highest engaged WebUI for
// the most time.
if (IsDelayPreloadEnabled()) {
MaybePreloadForBrowserContextLater(web_contents->GetBrowserContext(),
nullptr, PreloadReason::kWebUIDestroyed);
} else {
MaybePreloadForBrowserContext(web_contents->GetBrowserContext(),
PreloadReason::kWebUIDestroyed);
}
}
void WebUIContentsPreloadManager::OnWebContentsPrimaryPageChanged(
content::WebContents* web_contents) {
if (web_contents == preloaded_web_contents_.get()) {
content::RenderWidgetHostView* render_widget_host_view =
web_contents->GetRenderWidgetHostView();
// TODO(crbug.com/383937390): TopChromeWebUIConfig::From() returns nullptr.
// This crash key is to investigate if the visible URL is empty.
static crash_reporter::CrashKeyString<1024> visible_url(
"webui-preload-visible-url");
visible_url.Set(web_contents->GetVisibleURL().possibly_invalid_spec());
static crash_reporter::CrashKeyString<1024> site_instance_url(
"webui-preload-site-instance-url");
site_instance_url.Set(
web_contents->GetSiteInstance()->GetSiteURL().possibly_invalid_spec());
const bool should_auto_reisze_host =
TopChromeWebUIConfig::From(web_contents->GetBrowserContext(),
web_contents->GetVisibleURL())
->ShouldAutoResizeHost();
if (render_widget_host_view && should_auto_reisze_host) {
render_widget_host_view->EnableAutoResize(gfx::Size(1, 1),
gfx::Size(INT_MAX, INT_MAX));
}
}
}
|