blob: d9e2a1bfd557a7dabdee612998859f2ada44bcee [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2021 The Chromium Authors
Sreeja Kamishetty9e1d0e732021-05-27 18:20:092// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/browser/renderer_host/page_impl.h"
6
Matt Falkenhagenf78c2192021-07-24 02:01:437#include "base/barrier_closure.h"
Sky Malice3774b002022-10-21 19:18:408#include "base/feature_list.h"
Dominic Farolino5c606c12021-12-18 09:40:149#include "base/i18n/character_encoding.h"
Lingqi Chidcf722442021-09-02 01:47:1910#include "base/trace_event/optional_trace_event.h"
Sky Malice3774b002022-10-21 19:18:4011#include "cc/base/features.h"
Peilin Wang1187e4e82025-02-10 08:53:2912#include "cc/input/browser_controls_offset_tag_modifications.h"
Jeremy Roman4bd173d2021-06-17 00:05:4413#include "content/browser/manifest/manifest_manager_host.h"
Dave Tapuska9c9afe82021-06-22 19:07:4514#include "content/browser/renderer_host/frame_tree_node.h"
Jeremy Roman2d8dfe132021-07-06 20:51:2615#include "content/browser/renderer_host/page_delegate.h"
Julie Jeongeun Kim9e204512021-06-24 07:28:5416#include "content/browser/renderer_host/render_frame_host_delegate.h"
Sreeja Kamishetty9e1d0e732021-05-27 18:20:0917#include "content/browser/renderer_host/render_frame_host_impl.h"
Lingqi Chidcf722442021-09-02 01:47:1918#include "content/browser/renderer_host/render_frame_proxy_host.h"
Jeremy Roman2d8dfe132021-07-06 20:51:2619#include "content/browser/renderer_host/render_view_host_delegate.h"
Matt Falkenhagenf78c2192021-07-24 02:01:4320#include "content/browser/renderer_host/render_view_host_impl.h"
Takashi Toyoshima41afbbc2024-11-12 00:31:1221#include "content/browser/shared_storage/shared_storage_features.h"
Camillia Smith Barnes385a2e92024-04-16 17:49:4022#include "content/public/browser/content_browser_client.h"
Aman Verma39fe0492024-06-17 14:07:0123#include "content/public/browser/peak_gpu_memory_tracker_factory.h"
Jeremy Roman2d8dfe132021-07-06 20:51:2624#include "content/public/browser/render_view_host.h"
Camillia Smith Barnes385a2e92024-04-16 17:49:4025#include "content/public/common/content_client.h"
Peilin Wangae08e26e2024-06-10 20:55:3026#include "services/viz/public/mojom/compositing/offset_tag.mojom.h"
Camillia Smith Barnesddaf5b12023-01-24 00:06:3227#include "third_party/blink/public/common/features.h"
Sreeja Kamishetty0be3b1b2021-08-12 17:04:1528#include "third_party/blink/public/common/loader/loader_constants.h"
Camillia Smith Barnes385a2e92024-04-16 17:49:4029#include "third_party/blink/public/common/shared_storage/shared_storage_utils.h"
Julie Jeongeun Kim33ef6a22022-03-22 09:46:1130#include "third_party/blink/public/mojom/manifest/manifest.mojom.h"
Camillia Smith Barnes385a2e92024-04-16 17:49:4031#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom.h"
Jeremy Roman2d8dfe132021-07-06 20:51:2632#include "third_party/perfetto/include/perfetto/tracing/traced_value.h"
Sreeja Kamishetty9e1d0e732021-05-27 18:20:0933
34namespace content {
Jeremy Roman2d8dfe132021-07-06 20:51:2635
36PageImpl::PageImpl(RenderFrameHostImpl& rfh, PageDelegate& delegate)
Lingqi Chidcf722442021-09-02 01:47:1937 : main_document_(rfh),
38 delegate_(delegate),
Camillia Smith Barnesddaf5b12023-01-24 00:06:3239 text_autosizer_page_info_({0, 0, 1.f}) {
Takashi Toyoshima41afbbc2024-11-12 00:31:1240 if (base::FeatureList::IsEnabled(features::kSharedStorageSelectURLLimit)) {
41 select_url_overall_budget_ =
42 features::kSharedStorageSelectURLBitBudgetPerPageLoad.Get();
43 select_url_max_bits_per_site_ =
44 features::kSharedStorageSelectURLBitBudgetPerSitePerPageLoad.Get();
Camillia Smith Barnes477a3112023-02-28 19:06:3045 }
Rakina Zata Amni65a5dc42025-03-03 02:51:5546
47#if BUILDFLAG(IS_ANDROID)
48 page_proxy_ = std::make_unique<PageProxy>(this);
49#endif
Camillia Smith Barnesddaf5b12023-01-24 00:06:3250}
Sreeja Kamishetty9e1d0e732021-05-27 18:20:0951
Sreeja Kamishetty1b5c1432021-06-25 11:32:5952PageImpl::~PageImpl() {
Rakina Zata Amni65a5dc42025-03-03 02:51:5553#if BUILDFLAG(IS_ANDROID)
54 page_proxy_->WillDeletePage(GetMainDocument().IsInLifecycleState(
55 RenderFrameHost::LifecycleState::kPrerendering));
56#endif
57
Sreeja Kamishetty1b5c1432021-06-25 11:32:5958 // As SupportsUserData is a base class of PageImpl, Page members will be
59 // destroyed before running ~SupportsUserData, which would delete the
60 // associated PageUserData objects. Avoid this by calling ClearAllUserData
61 // explicitly here to ensure that the PageUserData destructors can access
62 // associated Page object.
63 ClearAllUserData();
Adithya Srinivasana5795b92023-12-20 22:35:2264
65 // If we still have a PeakGpuMemoryTracker, then the loading it was observing
66 // never completed. Cancel its callback so that we don't report partial
67 // loads to UMA.
68 CancelLoadingMemoryTracker();
Sreeja Kamishetty1b5c1432021-06-25 11:32:5969}
Sreeja Kamishetty9e1d0e732021-05-27 18:20:0970
Arthur Sonzognic686e8f2024-01-11 08:36:3771const std::optional<GURL>& PageImpl::GetManifestUrl() const {
Sreeja Kamishetty7c91ab22021-06-03 13:29:5272 return manifest_url_;
73}
74
Jeremy Roman4bd173d2021-06-17 00:05:4475void PageImpl::GetManifest(GetManifestCallback callback) {
76 ManifestManagerHost* manifest_manager_host =
Julie Jeongeun Kim33ef6a22022-03-22 09:46:1177 ManifestManagerHost::GetOrCreateForPage(*this);
Jeremy Roman4bd173d2021-06-17 00:05:4478 manifest_manager_host->GetManifest(std::move(callback));
79}
80
Julie Jeongeun Kimda529922023-01-13 02:59:5981bool PageImpl::IsPrimary() const {
Kevin McNeef1b0f0b2024-09-17 21:49:4182 return main_document_->IsInPrimaryMainFrame();
Dave Tapuska9c9afe82021-06-22 19:07:4583}
84
Julie Jeongeun Kim9e204512021-06-24 07:28:5485void PageImpl::UpdateManifestUrl(const GURL& manifest_url) {
86 manifest_url_ = manifest_url;
87
88 // If |main_document_| is not active, the notification is sent on the page
89 // activation.
Takashi Toyoshima41afbbc2024-11-12 00:31:1290 if (!main_document_->IsActive()) {
Julie Jeongeun Kim9e204512021-06-24 07:28:5491 return;
Takashi Toyoshima41afbbc2024-11-12 00:31:1292 }
Julie Jeongeun Kim9e204512021-06-24 07:28:5493
Ali Hijazid87307d2022-11-07 20:15:0394 main_document_->delegate()->OnManifestUrlChanged(*this);
Julie Jeongeun Kim9e204512021-06-24 07:28:5495}
96
Jeremy Roman2d8dfe132021-07-06 20:51:2697void PageImpl::WriteIntoTrace(perfetto::TracedValue context) {
98 auto dict = std::move(context).WriteDictionary();
Ali Hijazid87307d2022-11-07 20:15:0399 dict.Add("main_document", *main_document_);
Jeremy Roman2d8dfe132021-07-06 20:51:26100}
101
Miyoung Shinfa182e472021-09-03 12:39:32102base::WeakPtr<Page> PageImpl::GetWeakPtr() {
103 return weak_factory_.GetWeakPtr();
104}
105
Yao Xiaoc7224362022-02-16 08:21:40106base::WeakPtr<PageImpl> PageImpl::GetWeakPtrImpl() {
107 return weak_factory_.GetWeakPtr();
108}
109
Kevin McNee3183a7792021-11-09 21:03:36110bool PageImpl::IsPageScaleFactorOne() {
Kevin McNeec4325ba2022-04-08 23:18:23111 return GetPageScaleFactor() == 1.f;
Kevin McNee3183a7792021-11-09 21:03:36112}
113
Takashi Toyoshima6c58bbd2023-05-19 09:41:35114const std::string& PageImpl::GetContentsMimeType() const {
115 return contents_mime_type_;
116}
117
Arthur Sonzognic686e8f2024-01-11 08:36:37118void PageImpl::SetResizableForTesting(std::optional<bool> resizable) {
Sonja5f1ab742023-11-09 14:48:36119 SetResizable(resizable);
120}
121
Arthur Sonzognic686e8f2024-01-11 08:36:37122void PageImpl::SetResizable(std::optional<bool> resizable) {
Sonja5f1ab742023-11-09 14:48:36123 resizable_ = resizable;
Robert Ferens9610d7282025-02-17 09:52:32124 delegate_->OnWebApiWindowResizableChanged();
Sonja5f1ab742023-11-09 14:48:36125}
126
Arthur Sonzognic686e8f2024-01-11 08:36:37127std::optional<bool> PageImpl::GetResizable() {
Sonja5f1ab742023-11-09 14:48:36128 return resizable_;
129}
130
Rakina Zata Amni65a5dc42025-03-03 02:51:55131#if BUILDFLAG(IS_ANDROID)
132const base::android::JavaRef<jobject>& PageImpl::GetJavaPage() {
133 return page_proxy_->java_page();
134}
135#endif
136
Jeremy Roman2d8dfe132021-07-06 20:51:26137void PageImpl::OnFirstVisuallyNonEmptyPaint() {
138 did_first_visually_non_empty_paint_ = true;
Ali Hijazid87307d2022-11-07 20:15:03139 delegate_->OnFirstVisuallyNonEmptyPaint(*this);
Jeremy Roman2d8dfe132021-07-06 20:51:26140}
141
Arthur Sonzognic686e8f2024-01-11 08:36:37142void PageImpl::OnThemeColorChanged(const std::optional<SkColor>& theme_color) {
Jeremy Roman2d8dfe132021-07-06 20:51:26143 main_document_theme_color_ = theme_color;
Ali Hijazid87307d2022-11-07 20:15:03144 delegate_->OnThemeColorChanged(*this);
Jeremy Roman2d8dfe132021-07-06 20:51:26145}
146
Aaron Krajeski628c58c2023-04-04 16:24:12147void PageImpl::DidChangeBackgroundColor(SkColor4f background_color,
Jeremy Roman2d8dfe132021-07-06 20:51:26148 bool color_adjust) {
Aaron Krajeski628c58c2023-04-04 16:24:12149 // TODO(aaronhk): This should remain an SkColor4f
150 main_document_background_color_ = background_color.toSkColor();
Ali Hijazid87307d2022-11-07 20:15:03151 delegate_->OnBackgroundColorChanged(*this);
Jeremy Roman2d8dfe132021-07-06 20:51:26152 if (color_adjust) {
153 // <meta name="color-scheme" content="dark"> may pass the dark canvas
154 // background before the first paint in order to avoid flashing the white
155 // background in between loading documents. If we perform a navigation
156 // within the same renderer process, we keep the content background from the
157 // previous page while rendering is blocked in the new page, but for cross
158 // process navigations we would paint the default background (typically
159 // white) while the rendering is blocked.
Ali Hijazid87307d2022-11-07 20:15:03160 main_document_->GetRenderWidgetHost()->GetView()->SetContentBackgroundColor(
Aaron Krajeski628c58c2023-04-04 16:24:12161 background_color.toSkColor());
Jeremy Roman2d8dfe132021-07-06 20:51:26162 }
163}
164
Michael Bai19f17a302021-12-08 04:08:33165void PageImpl::DidInferColorScheme(
166 blink::mojom::PreferredColorScheme color_scheme) {
167 main_document_inferred_color_scheme_ = color_scheme;
Ali Hijazid87307d2022-11-07 20:15:03168 delegate_->DidInferColorScheme(*this);
Michael Bai19f17a302021-12-08 04:08:33169}
170
Julie Jeongeun Kimd4597df12022-11-11 02:44:51171void PageImpl::NotifyPageBecameCurrent() {
Takashi Toyoshima41afbbc2024-11-12 00:31:12172 if (!IsPrimary()) {
Julie Jeongeun Kimd4597df12022-11-11 02:44:51173 return;
Takashi Toyoshima41afbbc2024-11-12 00:31:12174 }
Julie Jeongeun Kimd4597df12022-11-11 02:44:51175 delegate_->NotifyPageBecamePrimary(*this);
176}
177
Jeremy Roman2d8dfe132021-07-06 20:51:26178void PageImpl::SetContentsMimeType(std::string mime_type) {
179 contents_mime_type_ = std::move(mime_type);
180}
181
Lingqi Chidcf722442021-09-02 01:47:19182void PageImpl::OnTextAutosizerPageInfoChanged(
183 blink::mojom::TextAutosizerPageInfoPtr page_info) {
184 OPTIONAL_TRACE_EVENT0("content", "PageImpl::OnTextAutosizerPageInfoChanged");
185
Dave Tapuska2cf1f532022-08-10 15:30:49186 // Keep a copy of `page_info` in case we create a new `blink::WebView` before
187 // the next update, so that the PageImpl can tell the newly created
188 // `blink::WebView` about the autosizer info.
Lingqi Chidcf722442021-09-02 01:47:19189 text_autosizer_page_info_.main_frame_width = page_info->main_frame_width;
190 text_autosizer_page_info_.main_frame_layout_width =
191 page_info->main_frame_layout_width;
192 text_autosizer_page_info_.device_scale_adjustment =
193 page_info->device_scale_adjustment;
194
Kevin McNee7705fe82024-11-07 18:56:31195 auto remote_frames_broadcast_callback =
196 [this](RenderFrameProxyHost* proxy_host) {
Lingqi Chidcf722442021-09-02 01:47:19197 DCHECK(proxy_host);
198 proxy_host->GetAssociatedRemoteMainFrame()->UpdateTextAutosizerPageInfo(
Kevin McNee7705fe82024-11-07 18:56:31199 text_autosizer_page_info_.Clone());
200 };
Lingqi Chidcf722442021-09-02 01:47:19201
Charlie Reis0650ad0d2024-12-06 18:12:35202 {
203 TRACE_EVENT("navigation",
204 "PageImpl::OnTextAutosizerPageInfoChanged broadcast");
205 main_document_->frame_tree()
206 ->root()
207 ->render_manager()
208 ->ExecuteRemoteFramesBroadcastMethod(
209 std::move(remote_frames_broadcast_callback),
210 main_document_->GetSiteInstance()->group());
211 }
Lingqi Chidcf722442021-09-02 01:47:19212}
213
Matt Falkenhagenf78c2192021-07-24 02:01:43214void PageImpl::SetActivationStartTime(base::TimeTicks activation_start) {
Takashi Toyoshima881f4d72023-11-09 05:07:43215 CHECK(!activation_start_time_);
216 activation_start_time_ = activation_start;
Matt Falkenhagenf78c2192021-07-24 02:01:43217}
218
Takashi Toyoshima881f4d72023-11-09 05:07:43219void PageImpl::Activate(
220 ActivationType type,
Vladimir Levin48d51002023-02-27 17:23:27221 StoredPage::RenderViewHostImplSafeRefSet& render_view_hosts,
Arthur Sonzognic686e8f2024-01-11 08:36:37222 std::optional<blink::ViewTransitionState> view_transition_state,
Takashi Toyoshima881f4d72023-11-09 05:07:43223 base::OnceCallback<void(base::TimeTicks)> completion_callback) {
224 TRACE_EVENT1("navigation", "PageImpl::Activate", "activation_type", type);
Lingqi Chief8657c2023-06-08 08:40:31225
Takashi Toyoshima881f4d72023-11-09 05:07:43226 // SetActivationStartTime() should be called first as the value is used in
227 // the callback below.
228 CHECK(activation_start_time_.has_value());
229
230 base::OnceClosure did_activate_render_views = base::BindOnce(
231 &PageImpl::DidActivateAllRenderViewsForPrerenderingOrPreview,
232 weak_factory_.GetWeakPtr(), std::move(completion_callback));
Matt Falkenhagenf78c2192021-07-24 02:01:43233
234 base::RepeatingClosure barrier = base::BarrierClosure(
235 render_view_hosts.size(), std::move(did_activate_render_views));
Takashi Toyoshima881f4d72023-11-09 05:07:43236 bool view_transition_state_consumed = false;
Dave Tapuskac3e58352022-09-28 19:05:27237 for (const auto& rvh : render_view_hosts) {
Vladimir Levin48d51002023-02-27 17:23:27238 auto params = blink::mojom::PrerenderPageActivationParams::New();
239
Vladimir Levin48d51002023-02-27 17:23:27240 if (main_document_->GetRenderViewHost() == &*rvh) {
Takashi Toyoshima881f4d72023-11-09 05:07:43241 // For prerendering activation, send activation_start only to the
242 // RenderViewHost for the main frame to avoid sending the info
243 // cross-origin. Only this RenderViewHost needs the info, as we expect the
244 // other RenderViewHosts are made for cross-origin iframes which have not
245 // yet loaded their document. To the renderer, it just looks like an
246 // ongoing navigation is happening in the frame and has not yet committed.
247 // These RenderViews still need to know about activation so their
248 // documents are created in the non-prerendered state once their
249 // navigation is committed.
250 params->activation_start = *activation_start_time_;
251 // Note that there cannot be a use-after-move since the if condition
252 // should be true at most once.
253 CHECK(!view_transition_state_consumed);
Vladimir Levin48d51002023-02-27 17:23:27254 params->view_transition_state = std::move(view_transition_state);
Takashi Toyoshima881f4d72023-11-09 05:07:43255 view_transition_state_consumed = true;
256 } else if (type == ActivationType::kPreview) {
257 // For preview activation, send activation_start to all RenderViewHosts
258 // as preview loads cross-origin subframes under the capability control,
259 // and activation_start time is meaningful there.
260 params->activation_start = *activation_start_time_;
Vladimir Levin48d51002023-02-27 17:23:27261 }
Matt Falkenhagenf78c2192021-07-24 02:01:43262
Takashi Toyoshima881f4d72023-11-09 05:07:43263 // For preview activation, there is no way to activate the previewed page
264 // other than with a user action, or testing only methods.
Hiroki Nakagawaab53cd22022-04-13 19:18:02265 params->was_user_activated =
Takashi Toyoshima881f4d72023-11-09 05:07:43266 (main_document_->frame_tree_node()
267 ->has_received_user_gesture_before_nav() ||
268 type == ActivationType::kPreview)
Hiroki Nakagawaab53cd22022-04-13 19:18:02269 ? blink::mojom::WasActivatedOption::kYes
270 : blink::mojom::WasActivatedOption::kNo;
Hiroki Nakagawaab53cd22022-04-13 19:18:02271 rvh->ActivatePrerenderedPage(std::move(params), barrier);
Matt Falkenhagenf78c2192021-07-24 02:01:43272 }
273
274 // Prepare each RenderFrameHostImpl in this Page for activation.
Peter Kastingd2876a22024-12-03 01:12:55275 main_document_->ForEachRenderFrameHostImplIncludingSpeculative(
Kevin McNee4d35f5a2024-07-09 16:39:38276 [](RenderFrameHostImpl* rfh) {
Takashi Toyoshima881f4d72023-11-09 05:07:43277 rfh->RendererWillActivateForPrerenderingOrPreview();
Daniel Cheng982f2b22022-08-25 23:46:16278 });
Matt Falkenhagenf78c2192021-07-24 02:01:43279}
280
Sreeja Kamishetty81fbeefb2021-08-12 07:21:41281void PageImpl::MaybeDispatchLoadEventsOnPrerenderActivation() {
282 DCHECK(IsPrimary());
283
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15284 // Dispatch LoadProgressChanged notification on activation with the
285 // prerender last load progress value if the value is not equal to
286 // blink::kFinalLoadProgress, whose notification is dispatched during call
287 // to DidStopLoading.
Takashi Toyoshima41afbbc2024-11-12 00:31:12288 if (load_progress() != blink::kFinalLoadProgress) {
Ali Hijazid87307d2022-11-07 20:15:03289 main_document_->DidChangeLoadProgress(load_progress());
Takashi Toyoshima41afbbc2024-11-12 00:31:12290 }
Sreeja Kamishetty0be3b1b2021-08-12 17:04:15291
Sreeja Kamishetty49783302022-01-28 17:52:25292 // Dispatch PrimaryMainDocumentElementAvailable before dispatching following
293 // load complete events.
Takashi Toyoshima41afbbc2024-11-12 00:31:12294 if (is_main_document_element_available()) {
Ali Hijazid87307d2022-11-07 20:15:03295 main_document_->MainDocumentElementAvailable(uses_temporary_zoom_level());
Takashi Toyoshima41afbbc2024-11-12 00:31:12296 }
Sreeja Kamishettycd5560912021-11-22 11:54:53297
Peter Kastingd2876a22024-12-03 01:12:55298 main_document_->ForEachRenderFrameHostImpl(
Daniel Cheng982f2b22022-08-25 23:46:16299 &RenderFrameHostImpl::MaybeDispatchDOMContentLoadedOnPrerenderActivation);
Sreeja Kamishetty81fbeefb2021-08-12 07:21:41300
Takashi Toyoshima41afbbc2024-11-12 00:31:12301 if (is_on_load_completed_in_main_document()) {
Ali Hijazid87307d2022-11-07 20:15:03302 main_document_->DocumentOnLoadCompleted();
Takashi Toyoshima41afbbc2024-11-12 00:31:12303 }
Sreeja Kamishetty81fbeefb2021-08-12 07:21:41304
Rakina Zata Amni2620dcb2024-11-13 01:41:58305 if (did_first_contentful_paint_in_main_document()) {
306 main_document_->OnFirstContentfulPaint();
307 }
308
Peter Kastingd2876a22024-12-03 01:12:55309 main_document_->ForEachRenderFrameHostImpl(
Daniel Cheng982f2b22022-08-25 23:46:16310 &RenderFrameHostImpl::MaybeDispatchDidFinishLoadOnPrerenderActivation);
Sreeja Kamishetty81fbeefb2021-08-12 07:21:41311}
312
Takashi Toyoshima881f4d72023-11-09 05:07:43313void PageImpl::DidActivateAllRenderViewsForPrerenderingOrPreview(
314 base::OnceCallback<void(base::TimeTicks)> completion_callback) {
Lingqi Chief8657c2023-06-08 08:40:31315 TRACE_EVENT0("navigation",
316 "PageImpl::DidActivateAllRenderViewsForPrerendering");
317
Matt Falkenhagenf78c2192021-07-24 02:01:43318 // Tell each RenderFrameHostImpl in this Page that activation finished.
Peter Kastingd2876a22024-12-03 01:12:55319 main_document_->ForEachRenderFrameHostImplIncludingSpeculative(
Hiroki Nakagawaf98d009a2023-03-16 02:26:10320 [this](RenderFrameHostImpl* rfh) {
321 if (&rfh->GetPage() != this) {
322 return;
323 }
324 rfh->RendererDidActivateForPrerendering();
325 });
Takashi Toyoshima881f4d72023-11-09 05:07:43326 CHECK(activation_start_time_.has_value());
327 std::move(completion_callback).Run(*activation_start_time_);
Matt Falkenhagenf78c2192021-07-24 02:01:43328}
329
Sreeja Kamishetty1b5c1432021-06-25 11:32:59330RenderFrameHost& PageImpl::GetMainDocumentHelper() {
Ali Hijazid87307d2022-11-07 20:15:03331 return *main_document_;
Sreeja Kamishetty1b5c1432021-06-25 11:32:59332}
333
334RenderFrameHostImpl& PageImpl::GetMainDocument() const {
Ali Hijazid87307d2022-11-07 20:15:03335 return *main_document_;
Sreeja Kamishetty1b5c1432021-06-25 11:32:59336}
337
Peilin Wangae08e26e2024-06-10 20:55:30338void PageImpl::UpdateBrowserControlsState(
339 cc::BrowserControlsState constraints,
340 cc::BrowserControlsState current,
341 bool animate,
Peilin Wang1187e4e82025-02-10 08:53:29342 const std::optional<cc::BrowserControlsOffsetTagModifications>&
343 offset_tag_modifications) {
Alison Gale47d1537d2024-04-19 21:31:46344 // TODO(crbug.com/40159655): Asking for the LocalMainFrame interface
Yoshisato Yanagisawad016d62d32021-10-15 04:38:55345 // before the RenderFrame is created is racy.
Takashi Toyoshima41afbbc2024-11-12 00:31:12346 if (!GetMainDocument().IsRenderFrameLive()) {
Yoshisato Yanagisawad016d62d32021-10-15 04:38:55347 return;
Takashi Toyoshima41afbbc2024-11-12 00:31:12348 }
Yoshisato Yanagisawad016d62d32021-10-15 04:38:55349
Morten Stenshorne457cad12024-11-06 07:37:09350 GetMainDocument().GetRenderWidgetHost()->UpdateBrowserControlsState(
Peilin Wang1187e4e82025-02-10 08:53:29351 constraints, current, animate, offset_tag_modifications);
Yoshisato Yanagisawad016d62d32021-10-15 04:38:55352}
353
Kevin McNeec4325ba2022-04-08 23:18:23354float PageImpl::GetPageScaleFactor() const {
355 return GetMainDocument().GetPageScaleFactor();
356}
357
Dominic Farolino5c606c12021-12-18 09:40:14358void PageImpl::UpdateEncoding(const std::string& encoding_name) {
Takashi Toyoshima41afbbc2024-11-12 00:31:12359 if (encoding_name == last_reported_encoding_) {
Dominic Farolino5c606c12021-12-18 09:40:14360 return;
Takashi Toyoshima41afbbc2024-11-12 00:31:12361 }
Dominic Farolino5c606c12021-12-18 09:40:14362 last_reported_encoding_ = encoding_name;
363
364 canonical_encoding_ =
365 base::GetCanonicalEncodingNameByAliasName(encoding_name);
366}
367
Yoshisato Yanagisawa8ae0d112022-04-20 01:49:30368void PageImpl::NotifyVirtualKeyboardOverlayRect(
369 const gfx::Rect& keyboard_rect) {
Alison Gale81f4f2c72024-04-22 19:33:31370 // TODO(crbug.com/40222405): send notification to outer frames if
Yoshisato Yanagisawa8ae0d112022-04-20 01:49:30371 // needed.
David Bokand6e44055b2022-09-21 03:58:08372 DCHECK_EQ(virtual_keyboard_mode(),
373 ui::mojom::VirtualKeyboardMode::kOverlaysContent);
Yoshisato Yanagisawa8ae0d112022-04-20 01:49:30374 GetMainDocument().GetAssociatedLocalFrame()->NotifyVirtualKeyboardOverlayRect(
375 keyboard_rect);
376}
377
Mason Freede478e4932025-03-31 23:52:15378void PageImpl::NotifyContextMenuInsetsObservers(const gfx::Rect& safe_area) {
379 GetMainDocument().GetAssociatedLocalFrame()->NotifyContextMenuInsetsObservers(
380 safe_area);
381}
382
Mason Freed098db032025-05-06 11:52:11383void PageImpl::ShowInterestInElement(int nodeID) {
384 GetMainDocument().GetAssociatedLocalFrame()->ShowInterestInElement(nodeID);
385}
386
David Bokand6e44055b2022-09-21 03:58:08387void PageImpl::SetVirtualKeyboardMode(ui::mojom::VirtualKeyboardMode mode) {
Takashi Toyoshima41afbbc2024-11-12 00:31:12388 if (virtual_keyboard_mode_ == mode) {
David Bokand6e44055b2022-09-21 03:58:08389 return;
Takashi Toyoshima41afbbc2024-11-12 00:31:12390 }
David Bokand6e44055b2022-09-21 03:58:08391
392 virtual_keyboard_mode_ = mode;
393
Ali Hijazid87307d2022-11-07 20:15:03394 delegate_->OnVirtualKeyboardModeChanged(*this);
David Bokand6e44055b2022-09-21 03:58:08395}
396
Yoshisato Yanagisawa668f8442022-04-20 04:45:58397base::flat_map<std::string, std::string> PageImpl::GetKeyboardLayoutMap() {
398 return GetMainDocument().GetRenderWidgetHost()->GetKeyboardLayoutMap();
399}
400
Camillia Smith Barnes397c9652024-10-29 21:27:16401int32_t PageImpl::GetSavedQueryResultIndexOrStoreCallback(
402 const url::Origin& origin,
403 const GURL& script_url,
404 const std::string& operation_name,
405 const std::u16string& query_name,
406 base::OnceCallback<void(uint32_t)> callback) {
407 auto key = std::make_tuple(origin, script_url, operation_name, query_name);
408 auto it = select_url_saved_query_index_results_.find(key);
409 if (it == select_url_saved_query_index_results_.end()) {
410 select_url_saved_query_index_results_[key] = SharedStorageSavedQueryData();
411 // The result index will be determined by running the registered worklet
412 // operation upon return to the SHaredStorageWorkletHost.
413 return -2;
414 }
415 if (it->second.index == -1) {
416 // The result index will be determined when a previously initiated worklet
417 // operation finishes running. We save a callback that will notify us of the
418 // result.
419 it->second.callbacks.push(std::move(callback));
420 return -1;
421 }
422 // The result index has been stored from a previously resolved worklet
423 // operation.
424 return it->second.index;
425}
426
427void PageImpl::SetSavedQueryResultIndexAndRunCallbacks(
428 const url::Origin& origin,
429 const GURL& script_url,
430 const std::string& operation_name,
431 const std::u16string& query_name,
432 uint32_t index) {
433 auto key = std::make_tuple(origin, script_url, operation_name, query_name);
434 auto it = select_url_saved_query_index_results_.find(key);
435 CHECK(it != select_url_saved_query_index_results_.end());
436 CHECK_EQ(it->second.index, -1L);
437 it->second.index = index;
438 while (!it->second.callbacks.empty()) {
439 std::move(it->second.callbacks.front()).Run(index);
440 it->second.callbacks.pop();
441 }
442}
443
Camillia Smith Barnes385a2e92024-04-16 17:49:40444blink::SharedStorageSelectUrlBudgetStatus
445PageImpl::CheckAndMaybeDebitSelectURLBudgets(const net::SchemefulSite& site,
446 double bits_to_charge) {
Camillia Smith Barnes477a3112023-02-28 19:06:30447 if (!select_url_overall_budget_) {
448 // The limits are not enabled.
Camillia Smith Barnes385a2e92024-04-16 17:49:40449 return blink::SharedStorageSelectUrlBudgetStatus::kSufficientBudget;
Camillia Smith Barnes9d70e5ae82023-01-18 19:25:24450 }
451
Camillia Smith Barnes385a2e92024-04-16 17:49:40452 // Return insufficient if there is insufficient overall budget.
Camillia Smith Barnes477a3112023-02-28 19:06:30453 if (bits_to_charge > select_url_overall_budget_.value()) {
Camillia Smith Barnes385a2e92024-04-16 17:49:40454 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
455 &GetMainDocument(),
456 blink::mojom::WebFeature::
457 kSharedStorageAPI_SelectURLOverallPageloadBudgetInsufficient);
458 return blink::SharedStorageSelectUrlBudgetStatus::
459 kInsufficientOverallPageloadBudget;
Camillia Smith Barnes574d4d42023-01-10 18:57:47460 }
461
Camillia Smith Barnes74552232023-10-02 18:50:07462 DCHECK(select_url_max_bits_per_site_);
Camillia Smith Barnes477a3112023-02-28 19:06:30463
Camillia Smith Barnes74552232023-10-02 18:50:07464 // Return false if the max bits per site is set to a value smaller than the
Camillia Smith Barnes477a3112023-02-28 19:06:30465 // current bits to charge.
Camillia Smith Barnes74552232023-10-02 18:50:07466 if (bits_to_charge > select_url_max_bits_per_site_.value()) {
Camillia Smith Barnes385a2e92024-04-16 17:49:40467 return blink::SharedStorageSelectUrlBudgetStatus::
468 kInsufficientSitePageloadBudget;
Camillia Smith Barnes477a3112023-02-28 19:06:30469 }
470
Camillia Smith Barnes385a2e92024-04-16 17:49:40471 // Charge the per-site budget or return insufficient if there is not enough.
Camillia Smith Barnes74552232023-10-02 18:50:07472 auto it = select_url_per_site_budget_.find(site);
473 if (it == select_url_per_site_budget_.end()) {
474 select_url_per_site_budget_[site] =
475 select_url_max_bits_per_site_.value() - bits_to_charge;
Camillia Smith Barnes477a3112023-02-28 19:06:30476 } else if (bits_to_charge > it->second) {
Camillia Smith Barnes74552232023-10-02 18:50:07477 // There is insufficient per-site budget remaining.
Camillia Smith Barnes385a2e92024-04-16 17:49:40478 return blink::SharedStorageSelectUrlBudgetStatus::
479 kInsufficientSitePageloadBudget;
Camillia Smith Barnes477a3112023-02-28 19:06:30480 } else {
481 it->second -= bits_to_charge;
482 }
483
484 // Charge the overall budget.
485 select_url_overall_budget_.value() -= bits_to_charge;
Camillia Smith Barnes385a2e92024-04-16 17:49:40486 return blink::SharedStorageSelectUrlBudgetStatus::kSufficientBudget;
Camillia Smith Barnes574d4d42023-01-10 18:57:47487}
488
Adithya Srinivasana5795b92023-12-20 22:35:22489void PageImpl::TakeLoadingMemoryTracker(NavigationRequest* request) {
490 CHECK(IsPrimary());
491 loading_memory_tracker_ = request->TakePeakGpuMemoryTracker();
492}
493
494void PageImpl::ResetLoadingMemoryTracker() {
495 CHECK(IsPrimary());
496 if (loading_memory_tracker_) {
497 loading_memory_tracker_.reset();
498 }
499}
500
501void PageImpl::CancelLoadingMemoryTracker() {
502 if (loading_memory_tracker_) {
503 loading_memory_tracker_->Cancel();
504 loading_memory_tracker_.reset();
505 }
506}
507
Adithya Srinivasanec6b3f92024-01-09 16:31:01508void PageImpl::SetLastCommitParams(
509 mojom::DidCommitProvisionalLoadParamsPtr commit_params) {
510 CHECK(GetMainDocument().IsOutermostMainFrame());
511 last_commit_params_ = std::move(commit_params);
512}
513
514mojom::DidCommitProvisionalLoadParamsPtr PageImpl::TakeLastCommitParams() {
515 CHECK(GetMainDocument().IsOutermostMainFrame());
516 return std::move(last_commit_params_);
517}
518
Sreeja Kamishetty9e1d0e732021-05-27 18:20:09519} // namespace content