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