Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors. All rights reserved. |
| 2 | // 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 Falkenhagen | f78c219 | 2021-07-24 02:01:43 | [diff] [blame] | 7 | #include "base/barrier_closure.h" |
Dominic Farolino | 5c606c1 | 2021-12-18 09:40:14 | [diff] [blame] | 8 | #include "base/i18n/character_encoding.h" |
Lingqi Chi | dcf72244 | 2021-09-02 01:47:19 | [diff] [blame] | 9 | #include "base/trace_event/optional_trace_event.h" |
Jeremy Roman | 4bd173d | 2021-06-17 00:05:44 | [diff] [blame] | 10 | #include "content/browser/manifest/manifest_manager_host.h" |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 11 | #include "content/browser/renderer_host/frame_tree_node.h" |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 12 | #include "content/browser/renderer_host/page_delegate.h" |
Julie Jeongeun Kim | 9e20451 | 2021-06-24 07:28:54 | [diff] [blame] | 13 | #include "content/browser/renderer_host/render_frame_host_delegate.h" |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 14 | #include "content/browser/renderer_host/render_frame_host_impl.h" |
Lingqi Chi | dcf72244 | 2021-09-02 01:47:19 | [diff] [blame] | 15 | #include "content/browser/renderer_host/render_frame_proxy_host.h" |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 16 | #include "content/browser/renderer_host/render_view_host_delegate.h" |
Matt Falkenhagen | f78c219 | 2021-07-24 02:01:43 | [diff] [blame] | 17 | #include "content/browser/renderer_host/render_view_host_impl.h" |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 18 | #include "content/public/browser/render_view_host.h" |
Sreeja Kamishetty | 0be3b1b | 2021-08-12 17:04:15 | [diff] [blame] | 19 | #include "third_party/blink/public/common/loader/loader_constants.h" |
Julie Jeongeun Kim | 33ef6a2 | 2022-03-22 09:46:11 | [diff] [blame] | 20 | #include "third_party/blink/public/mojom/manifest/manifest.mojom.h" |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 21 | #include "third_party/perfetto/include/perfetto/tracing/traced_value.h" |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 22 | |
| 23 | namespace content { |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 24 | |
| 25 | PageImpl::PageImpl(RenderFrameHostImpl& rfh, PageDelegate& delegate) |
Lingqi Chi | dcf72244 | 2021-09-02 01:47:19 | [diff] [blame] | 26 | : main_document_(rfh), |
| 27 | delegate_(delegate), |
| 28 | text_autosizer_page_info_({0, 0, 1.f}) {} |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 29 | |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 30 | PageImpl::~PageImpl() { |
| 31 | // As SupportsUserData is a base class of PageImpl, Page members will be |
| 32 | // destroyed before running ~SupportsUserData, which would delete the |
| 33 | // associated PageUserData objects. Avoid this by calling ClearAllUserData |
| 34 | // explicitly here to ensure that the PageUserData destructors can access |
| 35 | // associated Page object. |
| 36 | ClearAllUserData(); |
| 37 | } |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 38 | |
Julie Jeongeun Kim | 9e20451 | 2021-06-24 07:28:54 | [diff] [blame] | 39 | const absl::optional<GURL>& PageImpl::GetManifestUrl() const { |
Sreeja Kamishetty | 7c91ab2 | 2021-06-03 13:29:52 | [diff] [blame] | 40 | return manifest_url_; |
| 41 | } |
| 42 | |
Jeremy Roman | 4bd173d | 2021-06-17 00:05:44 | [diff] [blame] | 43 | void PageImpl::GetManifest(GetManifestCallback callback) { |
| 44 | ManifestManagerHost* manifest_manager_host = |
Julie Jeongeun Kim | 33ef6a2 | 2022-03-22 09:46:11 | [diff] [blame] | 45 | ManifestManagerHost::GetOrCreateForPage(*this); |
Jeremy Roman | 4bd173d | 2021-06-17 00:05:44 | [diff] [blame] | 46 | manifest_manager_host->GetManifest(std::move(callback)); |
| 47 | } |
| 48 | |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 49 | bool PageImpl::IsPrimary() { |
Dominic Farolino | 4bc10ee | 2021-08-31 00:37:36 | [diff] [blame] | 50 | // TODO(1244137): Check for portals as well, once they are migrated to MPArch. |
| 51 | if (main_document_.IsFencedFrameRoot()) |
| 52 | return false; |
| 53 | |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 54 | return main_document_.lifecycle_state() == |
Khushal | c5eaf22 | 2021-06-30 20:15:48 | [diff] [blame] | 55 | RenderFrameHostImpl::LifecycleStateImpl::kActive; |
Dave Tapuska | 9c9afe8 | 2021-06-22 19:07:45 | [diff] [blame] | 56 | } |
| 57 | |
Julie Jeongeun Kim | 9e20451 | 2021-06-24 07:28:54 | [diff] [blame] | 58 | void PageImpl::UpdateManifestUrl(const GURL& manifest_url) { |
| 59 | manifest_url_ = manifest_url; |
| 60 | |
| 61 | // If |main_document_| is not active, the notification is sent on the page |
| 62 | // activation. |
| 63 | if (!main_document_.IsActive()) |
| 64 | return; |
| 65 | |
| 66 | main_document_.delegate()->OnManifestUrlChanged(*this); |
| 67 | } |
| 68 | |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 69 | void PageImpl::WriteIntoTrace(perfetto::TracedValue context) { |
| 70 | auto dict = std::move(context).WriteDictionary(); |
| 71 | dict.Add("main_document", main_document_); |
| 72 | } |
| 73 | |
Miyoung Shin | fa182e47 | 2021-09-03 12:39:32 | [diff] [blame] | 74 | base::WeakPtr<Page> PageImpl::GetWeakPtr() { |
| 75 | return weak_factory_.GetWeakPtr(); |
| 76 | } |
| 77 | |
Yao Xiao | c722436 | 2022-02-16 08:21:40 | [diff] [blame] | 78 | base::WeakPtr<PageImpl> PageImpl::GetWeakPtrImpl() { |
| 79 | return weak_factory_.GetWeakPtr(); |
| 80 | } |
| 81 | |
Kevin McNee | 3183a779 | 2021-11-09 21:03:36 | [diff] [blame] | 82 | bool PageImpl::IsPageScaleFactorOne() { |
Kevin McNee | c4325ba | 2022-04-08 23:18:23 | [diff] [blame^] | 83 | return GetPageScaleFactor() == 1.f; |
Kevin McNee | 3183a779 | 2021-11-09 21:03:36 | [diff] [blame] | 84 | } |
| 85 | |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 86 | void PageImpl::OnFirstVisuallyNonEmptyPaint() { |
| 87 | did_first_visually_non_empty_paint_ = true; |
| 88 | delegate_.OnFirstVisuallyNonEmptyPaint(*this); |
| 89 | } |
| 90 | |
| 91 | void PageImpl::OnThemeColorChanged(const absl::optional<SkColor>& theme_color) { |
| 92 | main_document_theme_color_ = theme_color; |
| 93 | delegate_.OnThemeColorChanged(*this); |
| 94 | } |
| 95 | |
| 96 | void PageImpl::DidChangeBackgroundColor(SkColor background_color, |
| 97 | bool color_adjust) { |
| 98 | main_document_background_color_ = background_color; |
| 99 | delegate_.OnBackgroundColorChanged(*this); |
| 100 | if (color_adjust) { |
| 101 | // <meta name="color-scheme" content="dark"> may pass the dark canvas |
| 102 | // background before the first paint in order to avoid flashing the white |
| 103 | // background in between loading documents. If we perform a navigation |
| 104 | // within the same renderer process, we keep the content background from the |
| 105 | // previous page while rendering is blocked in the new page, but for cross |
| 106 | // process navigations we would paint the default background (typically |
| 107 | // white) while the rendering is blocked. |
| 108 | main_document_.GetRenderWidgetHost()->GetView()->SetContentBackgroundColor( |
| 109 | background_color); |
| 110 | } |
| 111 | } |
| 112 | |
Michael Bai | 19f17a30 | 2021-12-08 04:08:33 | [diff] [blame] | 113 | void PageImpl::DidInferColorScheme( |
| 114 | blink::mojom::PreferredColorScheme color_scheme) { |
| 115 | main_document_inferred_color_scheme_ = color_scheme; |
| 116 | delegate_.DidInferColorScheme(*this); |
| 117 | } |
| 118 | |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 119 | void PageImpl::SetContentsMimeType(std::string mime_type) { |
| 120 | contents_mime_type_ = std::move(mime_type); |
| 121 | } |
| 122 | |
Lingqi Chi | dcf72244 | 2021-09-02 01:47:19 | [diff] [blame] | 123 | void PageImpl::OnTextAutosizerPageInfoChanged( |
| 124 | blink::mojom::TextAutosizerPageInfoPtr page_info) { |
| 125 | OPTIONAL_TRACE_EVENT0("content", "PageImpl::OnTextAutosizerPageInfoChanged"); |
| 126 | |
| 127 | // Keep a copy of |page_info| in case we create a new RenderView before |
| 128 | // the next update, so that the PageImpl can tell the newly created RenderView |
| 129 | // about the autosizer info. |
| 130 | text_autosizer_page_info_.main_frame_width = page_info->main_frame_width; |
| 131 | text_autosizer_page_info_.main_frame_layout_width = |
| 132 | page_info->main_frame_layout_width; |
| 133 | text_autosizer_page_info_.device_scale_adjustment = |
| 134 | page_info->device_scale_adjustment; |
| 135 | |
| 136 | auto remote_frames_broadcast_callback = base::BindRepeating( |
| 137 | [](const blink::mojom::TextAutosizerPageInfo& page_info, |
| 138 | RenderFrameProxyHost* proxy_host) { |
| 139 | DCHECK(proxy_host); |
| 140 | proxy_host->GetAssociatedRemoteMainFrame()->UpdateTextAutosizerPageInfo( |
| 141 | page_info.Clone()); |
| 142 | }, |
| 143 | text_autosizer_page_info_); |
| 144 | |
| 145 | main_document_.frame_tree() |
| 146 | ->root() |
| 147 | ->render_manager() |
| 148 | ->ExecuteRemoteFramesBroadcastMethod( |
| 149 | std::move(remote_frames_broadcast_callback), |
| 150 | main_document_.GetSiteInstance()); |
| 151 | } |
| 152 | |
Matt Falkenhagen | f78c219 | 2021-07-24 02:01:43 | [diff] [blame] | 153 | void PageImpl::SetActivationStartTime(base::TimeTicks activation_start) { |
| 154 | DCHECK(!activation_start_time_for_prerendering_); |
| 155 | activation_start_time_for_prerendering_ = activation_start; |
| 156 | } |
| 157 | |
| 158 | void PageImpl::ActivateForPrerendering( |
| 159 | std::set<RenderViewHostImpl*>& render_view_hosts) { |
| 160 | base::OnceClosure did_activate_render_views = |
| 161 | base::BindOnce(&PageImpl::DidActivateAllRenderViewsForPrerendering, |
| 162 | weak_factory_.GetWeakPtr()); |
| 163 | |
| 164 | base::RepeatingClosure barrier = base::BarrierClosure( |
| 165 | render_view_hosts.size(), std::move(did_activate_render_views)); |
| 166 | for (RenderViewHostImpl* rvh : render_view_hosts) { |
| 167 | base::TimeTicks navigation_start_to_send; |
| 168 | // Only send navigation_start to the RenderViewHost for the main frame to |
| 169 | // avoid sending the info cross-origin. Only this RenderViewHost needs the |
| 170 | // info, as we expect the other RenderViewHosts are made for cross-origin |
| 171 | // iframes which have not yet loaded their document. To the renderer, it |
| 172 | // just looks like an ongoing navigation is happening in the frame and has |
| 173 | // not yet committed. These RenderViews still need to know about activation |
| 174 | // so their documents are created in the non-prerendered state once their |
| 175 | // navigation is committed. |
| 176 | if (main_document_.GetRenderViewHost() == rvh) |
| 177 | navigation_start_to_send = *activation_start_time_for_prerendering_; |
| 178 | |
| 179 | rvh->ActivatePrerenderedPage(navigation_start_to_send, barrier); |
| 180 | } |
| 181 | |
| 182 | // Prepare each RenderFrameHostImpl in this Page for activation. |
| 183 | // TODO(https://crbug.com/1232528): Currently we check GetPage() below because |
| 184 | // RenderFrameHostImpls may be in a different Page, if, e.g., they are in an |
| 185 | // inner WebContents. These are in a different FrameTree which might not know |
| 186 | // it is being prerendered. We should teach these FrameTrees that they are |
| 187 | // being prerendered, or ban inner FrameTrees in a prerendering page. |
Lingqi Chi | 2cfb83bb5 | 2022-01-12 13:15:37 | [diff] [blame] | 188 | main_document_.ForEachRenderFrameHostIncludingSpeculative(base::BindRepeating( |
Matt Falkenhagen | f78c219 | 2021-07-24 02:01:43 | [diff] [blame] | 189 | [](PageImpl* page, RenderFrameHostImpl* rfh) { |
| 190 | if (&rfh->GetPage() != page) |
| 191 | return; |
| 192 | rfh->RendererWillActivateForPrerendering(); |
| 193 | }, |
| 194 | this)); |
| 195 | } |
| 196 | |
Sreeja Kamishetty | 81fbeefb | 2021-08-12 07:21:41 | [diff] [blame] | 197 | void PageImpl::MaybeDispatchLoadEventsOnPrerenderActivation() { |
| 198 | DCHECK(IsPrimary()); |
| 199 | |
Sreeja Kamishetty | 0be3b1b | 2021-08-12 17:04:15 | [diff] [blame] | 200 | // Dispatch LoadProgressChanged notification on activation with the |
| 201 | // prerender last load progress value if the value is not equal to |
| 202 | // blink::kFinalLoadProgress, whose notification is dispatched during call |
| 203 | // to DidStopLoading. |
| 204 | if (load_progress() != blink::kFinalLoadProgress) |
| 205 | main_document_.DidChangeLoadProgress(load_progress()); |
| 206 | |
Sreeja Kamishetty | 4978330 | 2022-01-28 17:52:25 | [diff] [blame] | 207 | // Dispatch PrimaryMainDocumentElementAvailable before dispatching following |
| 208 | // load complete events. |
| 209 | if (is_main_document_element_available()) |
| 210 | main_document_.MainDocumentElementAvailable(uses_temporary_zoom_level()); |
Sreeja Kamishetty | cd556091 | 2021-11-22 11:54:53 | [diff] [blame] | 211 | |
Sreeja Kamishetty | 81fbeefb | 2021-08-12 07:21:41 | [diff] [blame] | 212 | main_document_.ForEachRenderFrameHost( |
| 213 | base::BindRepeating([](RenderFrameHostImpl* rfh) { |
| 214 | rfh->MaybeDispatchDOMContentLoadedOnPrerenderActivation(); |
| 215 | })); |
| 216 | |
| 217 | if (is_on_load_completed_in_main_document()) |
| 218 | main_document_.DocumentOnLoadCompleted(); |
| 219 | |
| 220 | main_document_.ForEachRenderFrameHost( |
| 221 | base::BindRepeating([](RenderFrameHostImpl* rfh) { |
| 222 | rfh->MaybeDispatchDidFinishLoadOnPrerenderActivation(); |
| 223 | })); |
| 224 | } |
| 225 | |
Matt Falkenhagen | f78c219 | 2021-07-24 02:01:43 | [diff] [blame] | 226 | void PageImpl::DidActivateAllRenderViewsForPrerendering() { |
| 227 | // Tell each RenderFrameHostImpl in this Page that activation finished. |
| 228 | main_document_.ForEachRenderFrameHost(base::BindRepeating( |
| 229 | [](PageImpl* page, RenderFrameHostImpl* rfh) { |
| 230 | if (&rfh->GetPage() != page) |
| 231 | return; |
| 232 | rfh->RendererDidActivateForPrerendering(); |
| 233 | }, |
| 234 | this)); |
| 235 | } |
| 236 | |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 237 | RenderFrameHost& PageImpl::GetMainDocumentHelper() { |
| 238 | return main_document_; |
| 239 | } |
| 240 | |
| 241 | RenderFrameHostImpl& PageImpl::GetMainDocument() const { |
| 242 | return main_document_; |
| 243 | } |
| 244 | |
Yoshisato Yanagisawa | d016d62d3 | 2021-10-15 04:38:55 | [diff] [blame] | 245 | void PageImpl::UpdateBrowserControlsState(cc::BrowserControlsState constraints, |
| 246 | cc::BrowserControlsState current, |
| 247 | bool animate) { |
| 248 | // TODO(https://crbug.com/1154852): Asking for the LocalMainFrame interface |
| 249 | // before the RenderFrame is created is racy. |
| 250 | if (!GetMainDocument().IsRenderFrameCreated()) |
| 251 | return; |
| 252 | |
| 253 | GetMainDocument().GetAssociatedLocalMainFrame()->UpdateBrowserControlsState( |
| 254 | constraints, current, animate); |
| 255 | } |
| 256 | |
Kevin McNee | c4325ba | 2022-04-08 23:18:23 | [diff] [blame^] | 257 | float PageImpl::GetPageScaleFactor() const { |
| 258 | return GetMainDocument().GetPageScaleFactor(); |
| 259 | } |
| 260 | |
Dominic Farolino | 5c606c1 | 2021-12-18 09:40:14 | [diff] [blame] | 261 | void PageImpl::UpdateEncoding(const std::string& encoding_name) { |
| 262 | if (encoding_name == last_reported_encoding_) |
| 263 | return; |
| 264 | last_reported_encoding_ = encoding_name; |
| 265 | |
| 266 | canonical_encoding_ = |
| 267 | base::GetCanonicalEncodingNameByAliasName(encoding_name); |
| 268 | } |
| 269 | |
Sreeja Kamishetty | 9e1d0e73 | 2021-05-27 18:20:09 | [diff] [blame] | 270 | } // namespace content |