blob: 442f8b498d8e905a993bd9ff634de7b1e4ce714c [file] [log] [blame]
[email protected]d4a8ca482013-10-30 21:06:401// Copyright 2013 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
creis4e2ecb72015-06-20 00:46:305/*
6 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
7 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved.
9 * (http://www.torchmobile.com/)
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
21 * its contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
25 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
28 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
danakjc492bf82020-09-09 20:02:4436#include "content/browser/renderer_host/navigation_controller_impl.h"
initial.commit09911bf2008-07-26 23:55:2937
Lei Zhang96031532019-10-10 19:05:4738#include <algorithm>
dcheng36b6aec92015-12-26 06:16:3639#include <utility>
40
[email protected]c5b88d82012-10-06 17:03:3341#include "base/bind.h"
[email protected]eabfe1912014-05-12 10:07:2842#include "base/command_line.h"
Ayu Ishii2f825852022-03-08 19:47:3843#include "base/containers/adapters.h"
Rakina Zata Amni627360d2022-02-24 00:53:4044#include "base/debug/dump_without_crashing.h"
initial.commit09911bf2008-07-26 23:55:2945#include "base/logging.h"
asvitkine30330812016-08-30 04:01:0846#include "base/metrics/histogram_macros.h"
Charlie Reis23c26da2022-01-29 00:57:4747#include "base/numerics/safe_conversions.h"
Chris Hamilton83272dc2021-02-23 00:24:0248#include "base/stl_util.h"
Ryan Hamilton7f3bd3d2022-04-23 00:07:3949#include "base/strings/escape.h"
Peter Kastingb53b81912021-04-28 19:23:3050#include "base/strings/string_piece.h"
[email protected]348fbaac2013-06-11 06:31:5151#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0052#include "base/strings/utf_string_conversions.h"
[email protected]a43858f2013-06-28 15:18:3753#include "base/time/time.h"
Carlos Caballero40b0efd2021-01-26 11:55:0054#include "base/trace_event/optional_trace_event.h"
Carlos Caballeroede6f8c2021-01-28 11:01:5055#include "base/trace_event/trace_conversion_helper.h"
ssid3e765612015-01-28 04:03:4256#include "base/trace_event/trace_event.h"
servolkf3955532015-05-16 00:01:5957#include "build/build_config.h"
[email protected]eabfe1912014-05-12 10:07:2858#include "cc/base/switches.h"
jamescookda2505812015-03-20 18:01:1859#include "content/browser/bad_message.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3960#include "content/browser/blob_storage/chrome_blob_storage_context.h"
[email protected]825b1662012-03-12 19:07:3161#include "content/browser/browser_url_handler_impl.h"
[email protected]5f2aa722013-08-07 16:59:4162#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0463#include "content/browser/dom_storage/session_storage_namespace_impl.h"
danakjc492bf82020-09-09 20:02:4464#include "content/browser/renderer_host/debug_urls.h"
65#include "content/browser/renderer_host/frame_tree.h"
Carlos Caballero40b0efd2021-01-26 11:55:0066#include "content/browser/renderer_host/frame_tree_node.h"
Chris Bookholt27faf8d2022-01-20 01:03:3367#include "content/browser/renderer_host/navigation_controller_delegate.h"
danakjc492bf82020-09-09 20:02:4468#include "content/browser/renderer_host/navigation_entry_impl.h"
Nate Chapin214a86a2021-06-21 20:35:5769#include "content/browser/renderer_host/navigation_entry_restore_context_impl.h"
danakjc492bf82020-09-09 20:02:4470#include "content/browser/renderer_host/navigation_request.h"
71#include "content/browser/renderer_host/navigator.h"
72#include "content/browser/renderer_host/render_frame_host_delegate.h"
Carlos Caballero40b0efd2021-01-26 11:55:0073#include "content/browser/renderer_host/render_view_host_impl.h"
Sharon Yangd70a5392021-10-26 23:06:3274#include "content/browser/site_info.h"
[email protected]b6583592012-01-25 19:52:3375#include "content/browser/site_instance_impl.h"
Kunihiko Sakamoto346a74e2021-03-10 08:57:4876#include "content/browser/web_package/subresource_web_bundle_navigation_info.h"
Tsuyoshi Horo37493af2019-11-21 23:43:1477#include "content/browser/web_package/web_bundle_navigation_info.h"
Camille Lamy5193caa2018-10-12 11:59:4278#include "content/common/content_constants_internal.h"
Nate Chapind1fe3612021-04-16 20:45:5779#include "content/common/navigation_params_utils.h"
Nasko Oskovae49e292020-08-13 02:08:5180#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1181#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1982#include "content/public/browser/content_browser_client.h"
Fergal Daly1336ac642021-09-14 15:13:1183#include "content/public/browser/disallow_activation_reason.h"
[email protected]d9083482012-01-06 00:38:4684#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0085#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3886#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1687#include "content/public/browser/notification_types.h"
Lei Zhang96031532019-10-10 19:05:4788#include "content/public/browser/render_view_host.h"
[email protected]9677a3c2012-12-22 04:18:5889#include "content/public/browser/render_widget_host.h"
90#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1091#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3492#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:1993#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:3894#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:1095#include "content/public/common/content_features.h"
Lei Zhang96031532019-10-10 19:05:4796#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4397#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:5998#include "media/base/mime_util.h"
Arthur Sonzogni620cec62018-12-13 13:08:5799#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:18100#include "services/metrics/public/cpp/ukm_builders.h"
101#include "services/metrics/public/cpp/ukm_recorder.h"
Yue Ru Sun128804932020-09-30 22:19:17102#include "services/metrics/public/cpp/ukm_source_id.h"
Nan Lind91c8152021-10-21 16:22:37103#include "services/network/public/mojom/fetch_api.mojom.h"
[email protected]9677a3c2012-12-22 04:18:58104#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:39105#include "third_party/blink/public/common/blob/blob_utils.h"
Gyuyoung Kim107c2a02021-04-13 01:49:30106#include "third_party/blink/public/common/chrome_debug_urls.h"
Miyoung Shin1c565c912021-03-17 12:11:21107#include "third_party/blink/public/common/history/session_history_constants.h"
Blink Reformata30d4232018-04-07 15:31:06108#include "third_party/blink/public/common/mime_util/mime_util.h"
Minggang Wanga13c796e2021-07-02 05:54:43109#include "third_party/blink/public/common/navigation/navigation_params.h"
Carlos Caballeroede6f8c2021-01-28 11:01:50110#include "third_party/blink/public/common/page_state/page_state_serialization.h"
Minggang Wangb9f3fa92021-07-01 15:30:31111#include "third_party/blink/public/mojom/navigation/navigation_params.mojom.h"
Minggang Wang7ee0c742021-06-16 16:16:51112#include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h"
[email protected]cca6f392014-05-28 21:32:26113#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:29114
[email protected]8ff00d72012-10-23 19:12:21115namespace content {
[email protected]e9ba4472008-09-14 15:42:43116namespace {
117
118// Invoked when entries have been pruned, or removed. For example, if the
119// current entries are [google, digg, yahoo], with the current entry google,
120// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47121void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50122 int index,
[email protected]c12bf1a12008-09-17 16:28:49123 int count) {
[email protected]8ff00d72012-10-23 19:12:21124 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50125 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49126 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14127 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43128}
129
[email protected]e9ba4472008-09-14 15:42:43130// Configure all the NavigationEntries in entries for restore. This resets
131// the transition type to reload and makes sure the content state isn't empty.
132void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57133 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48134 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47135 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43136 // Use a transition type of reload so that we don't incorrectly increase
137 // the typed count.
Lei Zhang96031532019-10-10 19:05:47138 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
139 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43140 }
141}
142
[email protected]bf70edce2012-06-20 22:32:22143// Determines whether or not we should be carrying over a user agent override
144// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57145bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22146 return last_entry && last_entry->GetIsOverridingUserAgent();
147}
148
Camille Lamy5193caa2018-10-12 11:59:42149// Determines whether to override user agent for a navigation.
150bool ShouldOverrideUserAgent(
151 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57152 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42153 switch (override_user_agent) {
154 case NavigationController::UA_OVERRIDE_INHERIT:
155 return ShouldKeepOverride(last_committed_entry);
156 case NavigationController::UA_OVERRIDE_TRUE:
157 return true;
158 case NavigationController::UA_OVERRIDE_FALSE:
159 return false;
Camille Lamy5193caa2018-10-12 11:59:42160 }
161 NOTREACHED();
162 return false;
163}
164
Rakina Zata Amni312822d72021-06-04 16:13:37165// Returns true if this navigation should be treated as a reload. For e.g.
clamy0a656e42018-02-06 18:18:28166// navigating to the last committed url via the address bar or clicking on a
Rakina Zata Amni312822d72021-06-04 16:13:37167// link which results in a navigation to the last committed URL (but wasn't
168// converted to do a replacement navigation in the renderer), etc.
Fergal Daly766177d2020-07-07 07:54:04169// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
170// |base_url_for_data_url|, |transition_type| correspond to the new navigation
171// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
172// navigation that committed.
173bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
174 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57175 const GURL& virtual_url,
176 const GURL& base_url_for_data_url,
177 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57178 bool is_post,
Rakina Zata Amni312822d72021-06-04 16:13:37179 bool should_replace_current_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57180 NavigationEntryImpl* last_committed_entry) {
Rakina Zata Amni312822d72021-06-04 16:13:37181 // Navigations intended to do a replacement shouldn't be converted to do a
182 // reload.
183 if (should_replace_current_entry)
clamy0a656e42018-02-06 18:18:28184 return false;
clamy0a656e42018-02-06 18:18:28185 // Only convert to reload if at least one navigation committed.
Rakina Zata Amni2322f4f82022-01-24 13:24:24186 if (!last_committed_entry || last_committed_entry->IsInitialEntry())
ananta3bdd8ae2016-12-22 17:11:55187 return false;
188
arthursonzogni7a8243682017-12-14 16:41:42189 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28190 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42191 return false;
192
ananta3bdd8ae2016-12-22 17:11:55193 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
194 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
195 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28196 bool transition_type_can_be_converted = false;
197 if (ui::PageTransitionCoreTypeIs(transition_type,
198 ui::PAGE_TRANSITION_RELOAD) &&
199 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
200 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55201 }
clamy0a656e42018-02-06 18:18:28202 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55203 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28204 transition_type_can_be_converted = true;
205 }
206 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
207 transition_type_can_be_converted = true;
208 if (!transition_type_can_be_converted)
209 return false;
210
211 // This check is required for cases like view-source:, etc. Here the URL of
212 // the navigation entry would contain the url of the page, while the virtual
213 // URL contains the full URL including the view-source prefix.
214 if (virtual_url != last_committed_entry->GetVirtualURL())
215 return false;
216
Fergal Daly766177d2020-07-07 07:54:04217 // Check that the URLs match.
218 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
219 // If there's no frame entry then by definition the URLs don't match.
220 if (!frame_entry)
221 return false;
222
223 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28224 return false;
225
226 // This check is required for Android WebView loadDataWithBaseURL. Apps
227 // can pass in anything in the base URL and we need to ensure that these
228 // match before classifying it as a reload.
229 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
230 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
231 return false;
ananta3bdd8ae2016-12-22 17:11:55232 }
233
clamy0a656e42018-02-06 18:18:28234 // Skip entries with SSL errors.
235 if (last_committed_entry->ssl_error())
236 return false;
237
238 // Don't convert to a reload when the last navigation was a POST or the new
239 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04240 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28241 return false;
242
243 return true;
ananta3bdd8ae2016-12-22 17:11:55244}
245
Nasko Oskov03912102019-01-11 00:21:32246bool DoesURLMatchOriginForNavigation(
Wang Hui14cac7a2022-01-28 06:07:47247 const GURL& destination_url,
Anton Bikineevf62d1bf2021-05-15 17:56:07248 const absl::optional<url::Origin>& origin,
Wang Hui14cac7a2022-01-28 06:07:47249 SubresourceWebBundleNavigationInfo* subresource_web_bundle_navigation_info,
250 NavigationEntry* entry,
251 bool is_main_frame) {
Nasko Oskov03912102019-01-11 00:21:32252 // If there is no origin supplied there is nothing to match. This can happen
253 // for navigations to a pending entry and therefore it should be allowed.
254 if (!origin)
255 return true;
256
Wang Hui14cac7a2022-01-28 06:07:47257 // For a history navigation to a document loaded with loadDataWithBaseURL,
258 // compare the origin to the entry's base URL instead of the data: URL used to
259 // commit.
260 const GURL& base_url_for_data_url = entry->GetBaseURLForDataURL();
261 bool check_origin_against_base_url =
262 is_main_frame && !base_url_for_data_url.is_empty();
263
264 const GURL& url_for_origin =
265 check_origin_against_base_url ? base_url_for_data_url : destination_url;
Kunihiko Sakamotof32652502022-04-05 03:05:59266 // Uuid-in-package: subframe from WebBundle has an opaque origin derived from
267 // the Bundle's origin.
268 if (url_for_origin.SchemeIs(url::kUuidInPackageScheme) &&
Kunihiko Sakamoto260ee0a2021-10-15 01:44:08269 subresource_web_bundle_navigation_info) {
Kunihiko Sakamoto346a74e2021-03-10 08:57:48270 return origin->CanBeDerivedFrom(
271 subresource_web_bundle_navigation_info->bundle_url());
272 }
273
Wang Hui14cac7a2022-01-28 06:07:47274 return origin->CanBeDerivedFrom(url_for_origin);
Nasko Oskov03912102019-01-11 00:21:32275}
276
Anton Bikineevf62d1bf2021-05-15 17:56:07277absl::optional<url::Origin> GetCommittedOriginForFrameEntry(
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12278 const mojom::DidCommitProvisionalLoadParams& params,
279 NavigationRequest* request) {
Nasko Oskov03912102019-01-11 00:21:32280 // Error pages commit in an opaque origin, yet have the real URL that resulted
281 // in an error as the |params.url|. Since successful reload of an error page
282 // should commit in the correct origin, setting the opaque origin on the
283 // FrameNavigationEntry will be incorrect.
Rakina Zata Amniafd3c6582021-11-30 06:19:17284 if (request && request->DidEncounterError())
Anton Bikineevf62d1bf2021-05-15 17:56:07285 return absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:32286
Anton Bikineevf62d1bf2021-05-15 17:56:07287 return absl::make_optional(params.origin);
Nasko Oskov03912102019-01-11 00:21:32288}
289
Camille Lamy5193caa2018-10-12 11:59:42290bool IsValidURLForNavigation(bool is_main_frame,
291 const GURL& virtual_url,
292 const GURL& dest_url) {
293 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
294 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
295 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
296 << virtual_url.possibly_invalid_spec();
297 return false;
298 }
299
300 // Don't attempt to navigate to non-empty invalid URLs.
301 if (!dest_url.is_valid() && !dest_url.is_empty()) {
302 LOG(WARNING) << "Refusing to load invalid URL: "
303 << dest_url.possibly_invalid_spec();
304 return false;
305 }
306
307 // The renderer will reject IPC messages with URLs longer than
308 // this limit, so don't attempt to navigate with a longer URL.
309 if (dest_url.spec().size() > url::kMaxURLChars) {
310 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
311 << " characters.";
312 return false;
313 }
314
Aaron Colwell33109c592020-04-21 21:31:19315 // Reject renderer debug URLs because they should have been handled before
316 // we get to this point. This check handles renderer debug URLs
317 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
318 // provides defense-in-depth if a renderer debug URL manages to get here via
319 // some other path. We want to reject the navigation here so it doesn't
320 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30321 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19322 LOG(WARNING) << "Refusing to load renderer debug URL: "
323 << dest_url.possibly_invalid_spec();
324 return false;
325 }
326
Camille Lamy5193caa2018-10-12 11:59:42327 return true;
328}
329
Mikel Astizba9cf2fd2017-12-17 10:38:10330// See replaced_navigation_entry_data.h for details: this information is meant
331// to ensure |*output_entry| keeps track of its original URL (landing page in
332// case of server redirects) as it gets replaced (e.g. history.replaceState()),
333// without overwriting it later, for main frames.
334void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57335 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10336 NavigationEntryImpl* output_entry) {
Rakina Zata Amniafd3c6582021-11-30 06:19:17337 if (output_entry->GetReplacedEntryData().has_value() ||
338 replaced_entry->IsInitialEntry()) {
Mikel Astizba9cf2fd2017-12-17 10:38:10339 return;
Rakina Zata Amniafd3c6582021-11-30 06:19:17340 }
Mikel Astizba9cf2fd2017-12-17 10:38:10341
342 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57343 data.first_committed_url = replaced_entry->GetURL();
344 data.first_timestamp = replaced_entry->GetTimestamp();
345 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29346 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10347}
348
Minggang Wangb9f3fa92021-07-01 15:30:31349blink::mojom::NavigationType GetNavigationType(
350 const GURL& old_url,
351 const GURL& new_url,
352 ReloadType reload_type,
353 NavigationEntryImpl* entry,
354 const FrameNavigationEntry& frame_entry,
355 bool has_pending_cross_document_commit,
356 bool is_currently_error_page,
357 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23358 // Reload navigations
359 switch (reload_type) {
360 case ReloadType::NORMAL:
Minggang Wangb9f3fa92021-07-01 15:30:31361 return blink::mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23362 case ReloadType::BYPASSING_CACHE:
Minggang Wangb9f3fa92021-07-01 15:30:31363 return blink::mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23364 case ReloadType::ORIGINAL_REQUEST_URL:
Minggang Wangb9f3fa92021-07-01 15:30:31365 return blink::mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23366 case ReloadType::NONE:
367 break; // Fall through to rest of function.
368 }
369
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08370 if (entry->IsRestored()) {
Minggang Wangb9f3fa92021-07-01 15:30:31371 return entry->GetHasPostData()
372 ? blink::mojom::NavigationType::RESTORE_WITH_POST
373 : blink::mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23374 }
375
danakjb952ef12021-01-14 19:58:49376 const bool can_be_same_document =
377 // A pending cross-document commit means this navigation will not occur in
378 // the current document, as that document would end up being replaced in
379 // the meantime.
380 !has_pending_cross_document_commit &&
381 // If the current document is an error page, we should always treat it as
382 // a different-document navigation so that we'll attempt to load the
383 // document we're navigating to (and not stay in the current error page).
384 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12385
clamyea99ea12018-05-28 13:54:23386 // History navigations.
387 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12388 return can_be_same_document && is_same_document_history_load
Minggang Wangb9f3fa92021-07-01 15:30:31389 ? blink::mojom::NavigationType::HISTORY_SAME_DOCUMENT
390 : blink::mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23391 }
392 DCHECK(!is_same_document_history_load);
393
394 // A same-document fragment-navigation happens when the only part of the url
395 // that is modified is after the '#' character.
396 //
397 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12398 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23399 //
400 // Note: this check is only valid for navigations that are not history
401 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
402 // history navigation from 'A#foo' to 'A#bar' is not a same-document
403 // navigation, but a different-document one. This is why history navigation
404 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47405 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
406 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12407
408 // The one case where we do the wrong thing here and incorrectly choose
409 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
410 // the renderer is a frameset. All frameset navigations should be
411 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
412 // navigation would do the right thing, as it would send it to the browser and
413 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
414 // into this method). But since we can't tell that case here for browser-
415 // initiated navigations, we have to get the renderer involved. In that case
416 // the navigation would be restarted due to the renderer spending a reply of
417 // mojom::CommitResult::RestartCrossDocument.
418
419 return can_be_same_document && is_same_doc
Minggang Wangb9f3fa92021-07-01 15:30:31420 ? blink::mojom::NavigationType::SAME_DOCUMENT
421 : blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23422}
423
Camille Lamy5193caa2018-10-12 11:59:42424// Adjusts the original input URL if needed, to get the URL to actually load and
425// the virtual URL, which may differ.
426void RewriteUrlForNavigation(const GURL& original_url,
427 BrowserContext* browser_context,
428 GURL* url_to_load,
429 GURL* virtual_url,
430 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42431 // Allow the browser URL handler to rewrite the URL. This will, for example,
432 // remove "view-source:" from the beginning of the URL to get the URL that
433 // will actually be loaded. This real URL won't be shown to the user, just
434 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31435 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42436 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
437 url_to_load, browser_context, reverse_on_redirect);
438}
439
440#if DCHECK_IS_ON()
441// Helper sanity check function used in debug mode.
442void ValidateRequestMatchesEntry(NavigationRequest* request,
443 NavigationEntryImpl* entry) {
444 if (request->frame_tree_node()->IsMainFrame()) {
445 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
446 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
Minggang Wangb9f3fa92021-07-01 15:30:31447 ui::PageTransitionFromInt(request->common_params().transition),
448 entry->GetTransitionType()));
Camille Lamy5193caa2018-10-12 11:59:42449 }
Nasko Oskovc36327d2019-01-03 23:23:04450 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42451 entry->should_clear_history_list());
452 DCHECK_EQ(request->common_params().has_user_gesture,
453 entry->has_user_gesture());
454 DCHECK_EQ(request->common_params().base_url_for_data_url,
455 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04456 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42457 entry->GetCanLoadLocalResources());
458 DCHECK_EQ(request->common_params().started_from_context_menu,
459 entry->has_started_from_context_menu());
460
461 FrameNavigationEntry* frame_entry =
462 entry->GetFrameEntry(request->frame_tree_node());
463 if (!frame_entry) {
464 NOTREACHED();
465 return;
466 }
467
Camille Lamy5193caa2018-10-12 11:59:42468 DCHECK_EQ(request->common_params().method, frame_entry->method());
469
Nasko Oskovc36327d2019-01-03 23:23:04470 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42471 if (redirect_size == frame_entry->redirect_chain().size()) {
472 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04473 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42474 frame_entry->redirect_chain()[i]);
475 }
476 } else {
477 NOTREACHED();
478 }
479}
480#endif // DCHECK_IS_ON()
481
Dave Tapuska8bfd84c2019-03-26 20:47:16482// Returns whether the session history NavigationRequests in |navigations|
483// would stay within the subtree of the sandboxed iframe in
484// |sandbox_frame_tree_node_id|.
485bool DoesSandboxNavigationStayWithinSubtree(
486 int sandbox_frame_tree_node_id,
487 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
488 for (auto& item : navigations) {
489 bool within_subtree = false;
490 // Check whether this NavigationRequest affects a frame within the
491 // sandboxed frame's subtree by walking up the tree looking for the
492 // sandboxed frame.
493 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03494 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16495 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
496 within_subtree = true;
497 break;
498 }
499 }
500 if (!within_subtree)
501 return false;
502 }
503 return true;
504}
505
[email protected]e9ba4472008-09-14 15:42:43506} // namespace
507
arthursonzogni66f711c2019-10-08 14:40:36508// NavigationControllerImpl::PendingEntryRef------------------------------------
509
510NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
511 base::WeakPtr<NavigationControllerImpl> controller)
512 : controller_(controller) {}
513
514NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
515 if (!controller_) // Can be null with interstitials.
516 return;
517
518 controller_->PendingEntryRefDeleted(this);
519}
520
[email protected]d202a7c2012-01-04 07:53:47521// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29522
[email protected]23a918b2014-07-15 09:51:36523const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23524
[email protected]765b35502008-08-21 00:51:20525// static
[email protected]d202a7c2012-01-04 07:53:47526size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23527 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20528
[email protected]e6fec472013-05-14 05:29:02529// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20530// when testing.
531static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29532
[email protected]71fde352011-12-29 03:29:56533// static
dcheng9bfa5162016-04-09 01:00:57534std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
535 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10536 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07537 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10538 ui::PageTransition transition,
539 bool is_renderer_initiated,
540 const std::string& extra_headers,
541 BrowserContext* browser_context,
542 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
543 return NavigationControllerImpl::CreateNavigationEntry(
544 url, referrer, std::move(initiator_origin),
545 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Julie Jeongeun Kim5b9aff72022-05-02 02:10:17546 extra_headers, browser_context, std::move(blob_url_loader_factory),
547 true /* rewrite_virtual_urls */);
Lukasz Anforowicz641234d52019-11-07 21:07:10548}
549
550// static
551std::unique_ptr<NavigationEntryImpl>
552NavigationControllerImpl::CreateNavigationEntry(
553 const GURL& url,
554 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07555 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10556 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57557 ui::PageTransition transition,
558 bool is_renderer_initiated,
559 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09560 BrowserContext* browser_context,
Julie Jeongeun Kim5b9aff72022-05-02 02:10:17561 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
562 bool rewrite_virtual_urls) {
563 GURL url_to_load = url;
564 GURL virtual_url = url;
[email protected]71fde352011-12-29 03:29:56565 bool reverse_on_redirect = false;
Julie Jeongeun Kim5b9aff72022-05-02 02:10:17566 if (rewrite_virtual_urls) {
567 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
568 &reverse_on_redirect);
569 }
Lukasz Anforowicz641234d52019-11-07 21:07:10570 // Let the NTP override the navigation params and pretend that this is a
571 // browser-initiated, bookmark-like navigation.
572 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violetcf6ea7e2021-06-09 21:09:21573 source_site_instance, &transition, &is_renderer_initiated, &referrer,
574 &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10575
Patrick Monettef507e982019-06-19 20:18:06576 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28577 nullptr, // The site instance for tabs is sent on navigation
578 // (WebContents::GetSiteInstance).
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58579 url_to_load, referrer, initiator_origin, std::u16string(), transition,
Rakina Zata Amniafd3c6582021-11-30 06:19:17580 is_renderer_initiated, blob_url_loader_factory,
581 /* is_initial_entry = */ false);
Camille Lamy5193caa2018-10-12 11:59:42582 entry->SetVirtualURL(virtual_url);
583 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56584 entry->set_update_virtual_url_with_url(reverse_on_redirect);
585 entry->set_extra_headers(extra_headers);
Patrick Monettef507e982019-06-19 20:18:06586 return entry;
[email protected]71fde352011-12-29 03:29:56587}
588
[email protected]cdcb1dee2012-01-04 00:46:20589// static
590void NavigationController::DisablePromptOnRepost() {
591 g_check_for_repost = false;
592}
593
[email protected]c5b88d82012-10-06 17:03:33594base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
595 base::Time t) {
596 // If |t| is between the water marks, we're in a run of duplicates
597 // or just getting out of it, so increase the high-water mark to get
598 // a time that probably hasn't been used before and return it.
599 if (low_water_mark_ <= t && t <= high_water_mark_) {
Peter Kastinge5a38ed2021-10-02 03:06:35600 high_water_mark_ += base::Microseconds(1);
[email protected]c5b88d82012-10-06 17:03:33601 return high_water_mark_;
602 }
603
604 // Otherwise, we're clear of the last duplicate run, so reset the
605 // water marks.
606 low_water_mark_ = high_water_mark_ = t;
607 return t;
608}
609
ckitagawa0faa5e42020-06-17 17:30:54610NavigationControllerImpl::ScopedShowRepostDialogForTesting::
611 ScopedShowRepostDialogForTesting()
612 : was_disallowed_(g_check_for_repost) {
613 g_check_for_repost = true;
614}
615
616NavigationControllerImpl::ScopedShowRepostDialogForTesting::
617 ~ScopedShowRepostDialogForTesting() {
618 g_check_for_repost = was_disallowed_;
619}
620
[email protected]d202a7c2012-01-04 07:53:47621NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00622 BrowserContext* browser_context,
623 FrameTree& frame_tree,
624 NavigationControllerDelegate* delegate)
625 : frame_tree_(frame_tree),
626 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57627 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22628 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47629 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37630 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29631}
632
[email protected]d202a7c2012-01-04 07:53:47633NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00634 // The NavigationControllerImpl might be called inside its delegate
635 // destructor. Calling it is not valid anymore.
636 delegate_ = nullptr;
637 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29638}
639
Matt Falkenhagen548ed1562021-07-06 01:38:26640WebContents* NavigationControllerImpl::DeprecatedGetWebContents() {
641 return delegate_->DeprecatedGetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32642}
643
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57644BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55645 return browser_context_;
646}
647
[email protected]d202a7c2012-01-04 07:53:47648void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30649 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36650 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57651 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
Charlie Reis23c26da2022-01-29 00:57:47652 // Note that it's possible for `entries_` to contain multiple entries at this
653 // point, as Restore() might be called on a NavigationController that is
654 // already used (e.g. due to WebView's restoreState() API), not only for fresh
655 // NavigationControllers. These entries are not cleared to preserve legacy
656 // behavior and also because `pending_entry_` might point to one of the
657 // pre-existing entries. An exception for this is when `entries_` contains
658 // only the initial NavigationEntry, which must be removed.
659
660 // Do not proceed if selected_navigation will be out of bounds for the updated
661 // entries_ list, since it will be assigned to last_committed_entry_index_ and
662 // used to index entries_.
663 // TODO(https://crbug.com/1287624): Consider also returning early if entries
664 // is empty, since there should be no work to do (rather than marking the
665 // existing entries as needing reload). Also consider returning early if the
666 // selected index is -1, which represents a no-committed-entry state.
667 if (selected_navigation < -1 ||
668 selected_navigation >=
669 base::checked_cast<int>(entries->size() + entries_.size())) {
670 return;
Rakina Zata Amni2322f4f82022-01-24 13:24:24671 }
Charlie Reis23c26da2022-01-29 00:57:47672
673 if (blink::features::IsInitialNavigationEntryEnabled()) {
674 // In InitialNavigationEntry mode, there will always be at least one entry.
675 if (selected_navigation == -1)
676 selected_navigation = 0;
677
678 if (GetLastCommittedEntry()->IsInitialEntry() && entries->size() > 0) {
679 // If we are on the initial NavigationEntry, it must be the only entry in
680 // the list. Since it's impossible to do a history navigation to the
681 // initial NavigationEntry, `pending_entry_index_` must be -1 (but
682 // `pending_entry` might be set for a new non-history navigation).
683 // Note that we should not clear `entries_` if `entries` is empty (when
684 // InitialNavigationEntry mode is enabled), since that would leave us
685 // without any NavigationEntry.
686 CHECK_EQ(1, GetEntryCount());
687 CHECK_EQ(-1, pending_entry_index_);
688 entries_.clear();
689 }
690 }
[email protected]ce3fa3c2009-04-20 19:55:57691
[email protected]ce3fa3c2009-04-20 19:55:57692 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44693 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03694 entries_.reserve(entries->size());
Charlie Reis23c26da2022-01-29 00:57:47695 for (auto& entry : *entries) {
Rakina Zata Amni996ee412022-02-17 04:51:43696 if (entry->GetURL().is_empty()) {
697 // We're trying to restore an entry with an empty URL (e.g. from
Rakina Zata Amni2729a512022-03-16 05:54:01698 // persisting the initial NavigationEntry [which is no longer possible but
699 // some old persisted sessions might still contain it] or when the
700 // serializer failed to write the URL because it's too long). Trying to
701 // restore and navigate to an entry with an empty URL will result in
702 // crashes, so change the URL to about:blank. See also
703 // https://crbug.com/1240138 and https://crbug.com/1299335.
Rakina Zata Amni996ee412022-02-17 04:51:43704 entry->SetURL(GURL(url::kAboutBlankURL));
705 }
dcheng36b6aec92015-12-26 06:16:36706 entries_.push_back(
707 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
Charlie Reis23c26da2022-01-29 00:57:47708 }
avif16f85a72015-11-13 18:25:03709
710 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
711 // cleared out safely.
712 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57713
714 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36715 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57716}
717
toyoshim6142d96f2016-12-19 09:07:25718void NavigationControllerImpl::Reload(ReloadType reload_type,
719 bool check_for_repost) {
Rakina Zata Amnid605d462022-06-01 10:17:03720 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "Reload_type",
721 (int)reload_type);
722 SCOPED_CRASH_KEY_BOOL("nav_reentrancy_caller1", "Reload_check",
723 check_for_repost);
liaoyuke9168fba2017-03-10 19:20:28724 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28725 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32726 int current_index = -1;
727
Carlos IL42b416592019-10-07 23:10:36728 if (entry_replaced_by_post_commit_error_) {
729 // If there is an entry that was replaced by a currently active post-commit
730 // error navigation, this can't be the initial navigation.
731 DCHECK(!IsInitialNavigation());
732 // If the current entry is a post commit error, we reload the entry it
733 // replaced instead. We leave the error entry in place until a commit
734 // replaces it, but the pending entry points to the original entry in the
735 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
736 // case that pending_entry_ != entries_[pending_entry_index_].
737 entry = entry_replaced_by_post_commit_error_.get();
738 current_index = GetCurrentEntryIndex();
739 } else if (IsInitialNavigation() && pending_entry_) {
740 // If we are reloading the initial navigation, just use the current
741 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32742 entry = pending_entry_;
743 // The pending entry might be in entries_ (e.g., after a Clone), so we
744 // should also update the current_index.
745 current_index = pending_entry_index_;
746 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00747 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32748 current_index = GetCurrentEntryIndex();
749 if (current_index != -1) {
creis3da03872015-02-20 21:12:32750 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32751 }
[email protected]979a4bc2013-04-24 01:27:15752 }
[email protected]241db352013-04-22 18:04:05753
[email protected]59167c22013-06-03 18:07:32754 // If we are no where, then we can't reload. TODO(darin): We should add a
755 // CanReload method.
756 if (!entry)
757 return;
758
Rakina Zata Amnif297a802022-01-18 03:53:43759 if (entry->IsInitialEntryNotForSynchronousAboutBlank()) {
760 // We should never navigate to an existing initial NavigationEntry that is
761 // the initial NavigationEntry for the initial empty document that hasn't
762 // been overridden by the synchronous about:blank commit, to preserve
763 // legacy behavior where trying to reload when the main frame is on the
764 // initial empty document won't result in a navigation. See also
765 // https://crbug.com/1277414.
766 return;
767 }
768
Takashi Toyoshimac7df3c22019-06-25 14:18:47769 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26770 entry->set_reload_type(reload_type);
771
Aran Gilman37d11632019-10-08 23:07:15772 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30773 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07774 // they really want to do this. If they do, the dialog will call us back
775 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57776 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02777
[email protected]106a0812010-03-18 00:15:12778 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57779 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47780 return;
initial.commit09911bf2008-07-26 23:55:29781 }
Lei Zhang96031532019-10-10 19:05:47782
783 if (!IsInitialNavigation())
784 DiscardNonCommittedEntries();
785
786 pending_entry_ = entry;
787 pending_entry_index_ = current_index;
788 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
789
Nate Chapin45f620582021-09-30 17:45:43790 // location.reload() goes through BeginNavigation, so all reloads triggered
791 // via this codepath are browser initiated.
Lei Zhang96031532019-10-10 19:05:47792 NavigateToExistingPendingEntry(reload_type,
Nate Chapin45f620582021-09-30 17:45:43793 FrameTreeNode::kFrameTreeNodeInvalidId,
794 true /* is_browser_initiated */);
initial.commit09911bf2008-07-26 23:55:29795}
796
[email protected]d202a7c2012-01-04 07:53:47797void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48798 DCHECK(pending_reload_ != ReloadType::NONE);
799 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12800}
801
[email protected]d202a7c2012-01-04 07:53:47802void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48803 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12804 NOTREACHED();
805 } else {
toyoshim6142d96f2016-12-19 09:07:25806 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48807 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12808 }
809}
810
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57811bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09812 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11813}
814
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57815bool NavigationControllerImpl::IsInitialBlankNavigation() {
Rakina Zata Amni2322f4f82022-01-24 13:24:24816 if (blink::features::IsInitialNavigationEntryEnabled()) {
817 // Check that we're on the initial NavigationEntry and that this is not a
818 // cloned tab.
819 return IsInitialNavigation() && GetEntryCount() == 1 &&
820 GetLastCommittedEntry()->IsInitialEntry() &&
821 GetLastCommittedEntry()->restore_type() == RestoreType::kNotRestored;
822 } else {
823 return IsInitialNavigation() && GetEntryCount() == 0;
824 }
creis10a4ab72015-10-13 17:22:40825}
826
Aran Gilman37d11632019-10-08 23:07:15827NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
828 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58829 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03830 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58831}
832
Adithya Srinivasan9b0c99c2021-08-10 15:19:45833NavigationEntryImpl*
834NavigationControllerImpl::GetEntryWithUniqueIDIncludingPending(
835 int nav_entry_id) const {
836 NavigationEntryImpl* entry = GetEntryWithUniqueID(nav_entry_id);
837 if (entry)
838 return entry;
839 return pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id
Keishi Hattori0e45c022021-11-27 09:25:52840 ? pending_entry_.get()
Adithya Srinivasan9b0c99c2021-08-10 15:19:45841 : nullptr;
842}
843
W. James MacLean1c40862c2020-04-27 21:05:57844void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
845 const url::Origin& origin) {
846 for (int i = 0; i < GetEntryCount(); i++) {
847 auto* entry = GetEntryAtIndex(i);
848 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
849 }
850 if (entry_replaced_by_post_commit_error_) {
851 // It's possible we could come back to this entry if the error
852 // page/interstitial goes away.
853 entry_replaced_by_post_commit_error_
854 ->RegisterExistingOriginToPreventOptInIsolation(origin);
855 }
856 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
857 // visiting pending_entry_, which lacks a committed origin. This will be done
858 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
859}
860
avi25764702015-06-23 15:43:37861void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57862 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00863 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37864 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27865 DCHECK_EQ(-1, pending_entry_index_);
[email protected]765b35502008-08-21 00:51:20866}
867
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57868NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47869 if (pending_entry_)
870 return pending_entry_;
871 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20872}
873
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57874NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32875 // The pending entry is safe to return for new (non-history), browser-
876 // initiated navigations. Most renderer-initiated navigations should not
877 // show the pending entry, to prevent URL spoof attacks.
878 //
879 // We make an exception for renderer-initiated navigations in new tabs, as
880 // long as no other page has tried to access the initial empty document in
881 // the new tab. If another page modifies this blank page, a URL spoof is
882 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32883 bool safe_to_show_pending =
884 pending_entry_ &&
885 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09886 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32887 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46888 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32889
890 // Also allow showing the pending entry for history navigations in a new tab,
891 // such as Ctrl+Back. In this case, no existing page is visible and no one
892 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15893 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
894 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32895 safe_to_show_pending = true;
896
897 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19898 return pending_entry_;
899 return GetLastCommittedEntry();
900}
901
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57902int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20903 if (pending_entry_index_ != -1)
904 return pending_entry_index_;
905 return last_committed_entry_index_;
906}
907
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57908NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
Rakina Zata Amni2322f4f82022-01-24 13:24:24909 if (last_committed_entry_index_ == -1) {
910 // The last committed entry must always exist when InitialNavigationEntry
911 // is enabled. TODO(https://crbug.com/524208): Remove this case and all
912 // related nullchecks entirely.
913 DCHECK(!blink::features::IsInitialNavigationEntryEnabled());
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28914 return nullptr;
Rakina Zata Amni2322f4f82022-01-24 13:24:24915 }
avif16f85a72015-11-13 18:25:03916 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20917}
918
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57919bool NavigationControllerImpl::CanViewSource() {
Jeremy Roman2d8dfe132021-07-06 20:51:26920 const std::string& mime_type =
921 frame_tree_.root()->current_frame_host()->GetPage().contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53922 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
923 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27924 NavigationEntry* visible_entry = GetVisibleEntry();
925 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39926 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16927}
928
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57929int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27930 // The last committed entry index must always be less than the number of
Rakina Zata Amni2322f4f82022-01-24 13:24:24931 // entries. If there are no entries, it must be -1.
arthursonzogni5c4c202d2017-04-25 23:41:27932 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
Rakina Zata Amni2322f4f82022-01-24 13:24:24933 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55934 return last_committed_entry_index_;
935}
936
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57937int NavigationControllerImpl::GetEntryCount() {
Carlos IL4dea8902020-05-26 15:14:29938 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55939 return static_cast<int>(entries_.size());
940}
941
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57942NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37943 if (index < 0 || index >= GetEntryCount())
944 return nullptr;
945
avif16f85a72015-11-13 18:25:03946 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25947}
948
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57949NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47950 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20951}
952
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57953int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46954 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03955}
956
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57957bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03958 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
959 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
960 return true;
961 }
962 return false;
[email protected]765b35502008-08-21 00:51:20963}
964
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57965bool NavigationControllerImpl::CanGoForward() {
WangHui74286d52021-03-31 16:17:15966 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
967 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
968 return true;
969 }
970 return false;
[email protected]765b35502008-08-21 00:51:20971}
972
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57973bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03974 int index = GetIndexForOffset(offset);
975 return index >= 0 && index < GetEntryCount();
976}
977
Xiaohan Wang7f8052e02022-01-14 18:44:28978#if BUILDFLAG(IS_ANDROID)
WangHui74286d52021-03-31 16:17:15979bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
WangHui74286d52021-03-31 16:17:15980 if (offset == 0)
981 return true;
982 int increment = offset > 0 ? 1 : -1;
983 int non_skippable_entries = 0;
984 for (int index = GetIndexForOffset(increment);
985 index >= 0 && index < GetEntryCount(); index += increment) {
986 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
987 non_skippable_entries++;
988
989 if (non_skippable_entries == std::abs(offset))
990 return true;
991 }
992 return false;
993}
994#endif
995
[email protected]d202a7c2012-01-04 07:53:47996void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06997 int target_index = GetIndexForOffset(-1);
998
Elly Fong-Jones40ee8112021-06-23 19:10:52999 // Move the target index past the skippable entries.
Shivani Sharma298d12852019-01-22 20:04:031000 bool all_skippable_entries = true;
Elly Fong-Jones40ee8112021-06-23 19:10:521001 while (target_index >= 0) {
1002 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui()) {
Shivani Sharma298d12852019-01-22 20:04:031003 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:061004 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:161005 }
Elly Fong-Jones40ee8112021-06-23 19:10:521006 target_index--;
shivanisha55201872018-12-13 04:29:061007 }
Miyoung Shin1c565c912021-03-17 12:11:211008
Shivani Sharma298d12852019-01-22 20:04:031009 // Do nothing if all entries are skippable. Normally this path would not
1010 // happen as consumers would have already checked it in CanGoBack but a lot of
1011 // tests do not do that.
Elly Fong-Jonesccc6d1f2021-06-14 18:32:421012 if (all_skippable_entries)
Shivani Sharma298d12852019-01-22 20:04:031013 return;
shivanisha55201872018-12-13 04:29:061014
shivanisha55201872018-12-13 04:29:061015 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:201016}
1017
[email protected]d202a7c2012-01-04 07:53:471018void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:061019 int target_index = GetIndexForOffset(1);
1020
Shivani Sharma2d5b4b6b2019-01-08 16:07:161021 // Note that at least one entry (the last one) will be non-skippable since
1022 // entries are marked skippable only when they add another entry because of
1023 // redirect or pushState.
Elly Fong-Jones40ee8112021-06-23 19:10:521024 while (target_index < static_cast<int>(entries_.size())) {
1025 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui())
shivanisha55201872018-12-13 04:29:061026 break;
Elly Fong-Jones40ee8112021-06-23 19:10:521027 target_index++;
shivanisha55201872018-12-13 04:29:061028 }
shivanisha55201872018-12-13 04:29:061029 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:201030}
1031
[email protected]d202a7c2012-01-04 07:53:471032void NavigationControllerImpl::GoToIndex(int index) {
Nate Chapin45f620582021-09-30 17:45:431033 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId,
1034 true /* is_browser_initiated */);
Dave Tapuska8bfd84c2019-03-26 20:47:161035}
1036
1037void NavigationControllerImpl::GoToIndex(int index,
Nate Chapin45f620582021-09-30 17:45:431038 int sandbox_frame_tree_node_id,
1039 bool is_browser_initiated) {
Rakina Zata Amnid605d462022-06-01 10:17:031040 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "GoToIndex_index", index);
Nate Chapin45f620582021-09-30 17:45:431041 DCHECK(sandbox_frame_tree_node_id == FrameTreeNode::kFrameTreeNodeInvalidId ||
1042 !is_browser_initiated);
sunjian30574a62017-03-21 21:39:441043 TRACE_EVENT0("browser,navigation,benchmark",
1044 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:201045 if (index < 0 || index >= static_cast<int>(entries_.size())) {
1046 NOTREACHED();
1047 return;
1048 }
1049
Rakina Zata Amnif297a802022-01-18 03:53:431050 if (entries_[index]->IsInitialEntryNotForSynchronousAboutBlank()) {
1051 // We should never navigate to an existing initial NavigationEntry that is
1052 // the initial NavigationEntry for the initial empty document that hasn't
1053 // been overridden by the synchronous about:blank commit, to preserve
1054 // legacy behavior where trying to reload when the main frame is on the
1055 // initial empty document won't result in a navigation. See also
1056 // https://crbug.com/1277414.
1057 return;
1058 }
1059
[email protected]cbab76d2008-10-13 22:42:471060 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:201061
arthursonzogni5c4c202d2017-04-25 23:41:271062 DCHECK_EQ(nullptr, pending_entry_);
1063 DCHECK_EQ(-1, pending_entry_index_);
Rakina Zata Amnif297a802022-01-18 03:53:431064
arthursonzogni5c4c202d2017-04-25 23:41:271065 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:201066 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:271067 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
1068 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Nate Chapin45f620582021-09-30 17:45:431069 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id,
1070 is_browser_initiated);
[email protected]765b35502008-08-21 00:51:201071}
1072
[email protected]d202a7c2012-01-04 07:53:471073void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:121074 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:031075 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:201076 return;
1077
[email protected]9ba14052012-06-22 23:50:031078 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:201079}
1080
Nate Chapin45f620582021-09-30 17:45:431081void NavigationControllerImpl::GoToOffsetFromRenderer(int offset) {
1082 // Note: This is actually reached in unit tests.
1083 if (!CanGoToOffset(offset))
1084 return;
1085
1086 GoToIndex(GetIndexForOffset(offset), FrameTreeNode::kFrameTreeNodeInvalidId,
1087 false /* is_browser_initiated */);
1088}
1089
Xiaohan Wang7f8052e02022-01-14 18:44:281090#if BUILDFLAG(IS_ANDROID)
WangHui74286d52021-03-31 16:17:151091void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
1092 // Note: This is actually reached in unit tests.
1093 if (!CanGoToOffsetWithSkipping(offset))
1094 return;
1095
Elly Fong-Jonesccc6d1f2021-06-14 18:32:421096 if (offset == 0) {
WangHui74286d52021-03-31 16:17:151097 GoToIndex(GetIndexForOffset(offset));
1098 return;
1099 }
1100 int increment = offset > 0 ? 1 : -1;
1101 // Find the offset without counting skippable entries.
1102 int target_index = GetIndexForOffset(increment);
1103 int non_skippable_entries = 0;
1104 for (int index = target_index; index >= 0 && index < GetEntryCount();
1105 index += increment) {
1106 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1107 non_skippable_entries++;
1108
1109 if (non_skippable_entries == std::abs(offset)) {
1110 target_index = index;
1111 break;
1112 }
1113 }
1114
1115 GoToIndex(target_index);
1116}
1117#endif
1118
[email protected]41374f12013-07-24 02:49:281119bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151120 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281121 return false;
[email protected]6a13a6c2011-12-20 21:47:121122
[email protected]43032342011-03-21 14:10:311123 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281124 return true;
[email protected]cbab76d2008-10-13 22:42:471125}
1126
Michael Thiessen9b14d512019-09-23 21:19:471127void NavigationControllerImpl::PruneForwardEntries() {
1128 DiscardNonCommittedEntries();
1129 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471130 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471131 if (num_removed <= 0)
1132 return;
1133 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1134 NotifyPrunedEntries(this, remove_start_index /* start index */,
1135 num_removed /* count */);
1136}
1137
Aran Gilman37d11632019-10-08 23:07:151138void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1139 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321140 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311141 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511142 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1143 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321144 }
1145}
1146
Harkiran Bolariaba823e42021-05-21 18:30:361147base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL(
1148 const GURL& url,
1149 const Referrer& referrer,
1150 ui::PageTransition transition,
1151 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471152 LoadURLParams params(url);
1153 params.referrer = referrer;
1154 params.transition_type = transition;
1155 params.extra_headers = extra_headers;
Harkiran Bolariaba823e42021-05-21 18:30:361156 return LoadURLWithParams(params);
[email protected]cf002332012-08-14 19:17:471157}
1158
Harkiran Bolariaba823e42021-05-21 18:30:361159base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams(
1160 const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061161 if (params.is_renderer_initiated)
1162 DCHECK(params.initiator_origin.has_value());
1163
naskob8744d22014-08-28 17:07:431164 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151165 "NavigationControllerImpl::LoadURLWithParams", "url",
1166 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291167 bool is_explicit_navigation =
1168 GetContentClient()->browser()->IsExplicitNavigation(
1169 params.transition_type);
1170 if (HandleDebugURL(params.url, params.transition_type,
1171 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431172 // If Telemetry is running, allow the URL load to proceed as if it's
1173 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491174 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431175 cc::switches::kEnableGpuBenchmarking))
Harkiran Bolariaba823e42021-05-21 18:30:361176 return nullptr;
[email protected]47752982014-07-29 08:01:431177 }
[email protected]8bf1048012012-02-08 01:22:181178
[email protected]cf002332012-08-14 19:17:471179 // Checks based on params.load_type.
1180 switch (params.load_type) {
1181 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431182 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471183 break;
1184 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261185 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471186 NOTREACHED() << "Data load must use data scheme.";
Harkiran Bolariaba823e42021-05-21 18:30:361187 return nullptr;
[email protected]cf002332012-08-14 19:17:471188 }
1189 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461190 }
[email protected]e47ae9472011-10-13 19:48:341191
[email protected]e47ae9472011-10-13 19:48:341192 // The user initiated a load, we don't need to reload anymore.
1193 needs_reload_ = false;
1194
Harkiran Bolariaba823e42021-05-21 18:30:361195 return NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441196}
1197
Mohamed Abdelhalim833de902019-09-16 17:41:451198bool NavigationControllerImpl::PendingEntryMatchesRequest(
1199 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191200 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451201 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191202}
1203
[email protected]d202a7c2012-01-04 07:53:471204bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321205 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071206 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331207 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441208 bool is_same_document_navigation,
Nate Chapinc7019dd7d2021-06-25 18:29:251209 bool was_on_initial_empty_document,
Shivani Sharmaffb32b82019-04-09 16:58:471210 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131211 NavigationRequest* navigation_request) {
1212 DCHECK(navigation_request);
Chris Bookholt27faf8d2022-01-20 01:03:331213
1214 // Note: validation checks and renderer kills due to invalid commit messages
1215 // must happen before getting here, in
1216 // RenderFrameHostImpl::ValidateDidCommitParams. By the time we get here, some
1217 // effects of the navigation have already occurred.
1218
[email protected]cd2e15742013-03-08 04:08:311219 is_initial_navigation_ = false;
1220
[email protected]0e8db942008-09-24 21:21:481221 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431222 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481223 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361224 if (entry_replaced_by_post_commit_error_) {
Chris Bookholt27faf8d2022-01-20 01:03:331225 // Same document navigation events with a post-commit error should already
1226 // be blocked by RenderFrameHostImpl::ValidateDidCommitParams() before
1227 // reaching here.
1228 CHECK(!is_same_document_navigation);
1229
Carlos IL42b416592019-10-07 23:10:361230 // Any commit while a post-commit error page is showing should put the
1231 // original entry back, replacing the error page's entry. This includes
1232 // reloads, where the original entry was used as the pending entry and
1233 // should now be at the correct index at commit time.
1234 entries_[last_committed_entry_index_] =
1235 std::move(entry_replaced_by_post_commit_error_);
1236 }
Fergal Daly8e33cf62020-12-12 01:06:071237 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551238 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431239 if (pending_entry_ &&
1240 pending_entry_->GetIsOverridingUserAgent() !=
1241 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1242 overriding_user_agent_changed = true;
Xiaohan Wang7f8052e02022-01-14 18:44:281243#if BUILDFLAG(IS_ANDROID)
Shu Yang5756fa422021-11-05 23:02:541244 // TODO(crbug.com/1266277): Clean up the logic of setting
1245 // |overriding_user_agent_changed| post-launch.
Shu Yang9eabbb902021-11-17 00:45:031246 if (base::FeatureList::IsEnabled(features::kRequestDesktopSiteExceptions)) {
Shu Yang5756fa422021-11-05 23:02:541247 // Must honor user agent overrides in the |navigation_request|, such as
1248 // from things like RequestDesktopSiteWebContentsObserverAndroid. As a
1249 // result, besides comparing |pending_entry_|'s user agent against
1250 // LastCommittedEntry's, also need to compare |navigation_request|'s user
1251 // agent against LastCommittedEntry's.
1252 if (navigation_request->is_overriding_user_agent() !=
1253 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
1254 overriding_user_agent_changed = true;
1255 }
1256 }
Xiaohan Wang7f8052e02022-01-14 18:44:281257#endif // BUILDFLAG(IS_ANDROID)
[email protected]0e8db942008-09-24 21:21:481258 } else {
Gang Wu325f03f42021-02-25 20:00:461259 // GetLastCommittedEntry() is null, so this is the first entry.
Fergal Daly8e33cf62020-12-12 01:06:071260 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481261 details->previous_entry_index = -1;
Gang Wu325f03f42021-02-25 20:00:461262 if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) {
1263 // Default setting is NOT override the user agent, so overriding the user
1264 // agent in first entry should be considered as user agent changed as
1265 // well.
1266 overriding_user_agent_changed = true;
1267 }
Xiaohan Wang7f8052e02022-01-14 18:44:281268#if BUILDFLAG(IS_ANDROID)
Shu Yang5756fa422021-11-05 23:02:541269 // TODO(crbug.com/1266277): Clean up the logic of setting
1270 // |overriding_user_agent_changed| post-launch.
Shu Yang9eabbb902021-11-17 00:45:031271 if (base::FeatureList::IsEnabled(features::kRequestDesktopSiteExceptions)) {
Shu Yang5756fa422021-11-05 23:02:541272 // Must honor user agent overrides in the |navigation_request|, such as
1273 // from things like RequestDesktopSiteWebContentsObserverAndroid. As a
1274 // result, besides checking |pending_entry_|'s user agent, also need to
1275 // check |navigation_request|'s.
1276 if (navigation_request->is_overriding_user_agent()) {
1277 overriding_user_agent_changed = true;
1278 }
1279 }
Xiaohan Wang7f8052e02022-01-14 18:44:281280#endif // BUILDFLAG(IS_ANDROID)
[email protected]0e8db942008-09-24 21:21:481281 }
[email protected]ecd9d8702008-08-28 22:10:171282
Dave Tapuskaa2ab4f252021-07-08 21:31:281283 bool is_main_frame_navigation = !rfh->GetParent();
1284
Alexander Timind2f2e4f22019-04-02 20:04:531285 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1286 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281287 // For primary frame tree navigations, choose an appropriate
Rakina Zata Amni63b5e8092022-05-20 11:25:141288 // BackForwardCacheMetrics to be associated with the new navigation's
1289 // NavigationEntry, by either creating a new object or reusing the previous
1290 // entry's one.
Dave Tapuskaa2ab4f252021-07-08 21:31:281291 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics;
1292 if (navigation_request->frame_tree_node()->frame_tree()->type() ==
1293 FrameTree::Type::kPrimary) {
Rakina Zata Amni63b5e8092022-05-20 11:25:141294 back_forward_cache_metrics = BackForwardCacheMetrics::
1295 CreateOrReuseBackForwardCacheMetricsForNavigation(
Dave Tapuskaa2ab4f252021-07-08 21:31:281296 GetLastCommittedEntry(), is_main_frame_navigation,
1297 params.document_sequence_number);
1298 }
Alexander Timind2f2e4f22019-04-02 20:04:531299 // Notify the last active entry that we have navigated away.
Dave Tapuskaa2ab4f252021-07-08 21:31:281300 if (is_main_frame_navigation && !is_same_document_navigation) {
Alexander Timind2f2e4f22019-04-02 20:04:531301 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1302 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Rakina Zata Amni63b5e8092022-05-20 11:25:141303 metrics->MainFrameDidNavigateAwayFromDocument();
Alexander Timind2f2e4f22019-04-02 20:04:531304 }
1305 }
1306 }
1307
fdegans9caf66a2015-07-30 21:10:421308 // If there is a pending entry at this point, it should have a SiteInstance,
1309 // except for restored entries.
jam48cea9082017-02-15 06:13:291310 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481311 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081312 pending_entry_->IsRestored());
1313 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151314 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291315 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481316 }
[email protected]e9ba4472008-09-14 15:42:431317
Nasko Oskovaee2f862018-06-15 00:05:521318 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1319 // has changed, this must be treated as a new navigation with replacement.
1320 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491321 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521322 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101323 pending_entry_->GetUniqueID() ==
1324 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521325 pending_entry_->site_instance() &&
1326 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1327 DCHECK_NE(-1, pending_entry_index_);
1328 // TODO(nasko,creis): Instead of setting this value here, set
1329 // should_replace_current_entry on the parameters we send to the
1330 // renderer process as part of CommitNavigation. The renderer should
1331 // in turn send it back here as part of |params| and it can be just
1332 // enforced and renderer process terminated on mismatch.
1333 details->did_replace_entry = true;
1334 } else {
1335 // The renderer tells us whether the navigation replaces the current entry.
1336 details->did_replace_entry = params.should_replace_current_entry;
1337 }
[email protected]bcd904482012-02-01 01:54:221338
[email protected]e9ba4472008-09-14 15:42:431339 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amni2322f4f82022-01-24 13:24:241340 details->type = ClassifyNavigation(rfh, params, navigation_request);
shivanigithub189833f2022-04-27 18:08:451341
1342 if (ShouldMaintainTrivialSessionHistory(rfh->frame_tree_node()) &&
1343 GetLastCommittedEntry()) {
1344 // Ensure that this navigation does not add a navigation entry, since
1345 // ShouldMaintainTrivialSessionHistory() means we should not add an entry
1346 // beyond the last committed one. Therefore, `should_replace_current_entry`
1347 // should be set, which replaces the current entry, or this should be a
1348 // reload, which does not create a new entry.
1349 // In shadowDOM fenced frames, on a history/tab-restore navigation, any
1350 // navigation that is restored will not be creating a new entry anyways, so
1351 // exclude that case by checking NAVIGATION_TYPE_AUTO_SUBFRAME.
1352 // TODO(crbug.com/1319919): Consider adjusting the dcheck for more cases as
1353 // pointed out in the issue.
1354 DCHECK(params.should_replace_current_entry ||
1355 navigation_request->GetReloadType() != ReloadType::NONE ||
1356 details->type == NAVIGATION_TYPE_AUTO_SUBFRAME);
1357 }
1358
Rakina Zata Amni2322f4f82022-01-24 13:24:241359 if (GetLastCommittedEntry() && GetLastCommittedEntry()->IsInitialEntry()) {
Rakina Zata Amniddf10502022-01-15 02:56:551360 if (rfh->GetParent()) {
Rakina Zata Amni2322f4f82022-01-24 13:24:241361 // This is a subframe navigation on the initial empty document, which used
1362 // to not have a NavigationEntry to attach to. Now it can attach to the
1363 // initial NavigationEntry, and we must ensure that its NavigationEntry
1364 // will keep the "initial NavigationEntry" status and won't append a new
1365 // NavigationEntry (it should always do replacement instead).
1366 // See also https://crbug.com/1277414.
1367 details->should_stay_as_initial_entry = true;
Rakina Zata Amniddf10502022-01-15 02:56:551368 // Subframe navigation on initial NavigationEntry must not append a new
1369 // NavigationEntry (i.e. should not be classified as NEW_SUBFRAME). This
1370 // means every subframe navigation that happens while we're on the initial
1371 // NavigationEntry will always reuse the existing NavigationEntry and
1372 // just update the corresponding FrameNavigationEntry.
1373 DCHECK_EQ(details->type, NAVIGATION_TYPE_AUTO_SUBFRAME);
1374 } else if (details->type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY) {
1375 // This is a navigation that modifies the initial NavigationEntry, either
1376 // for a replacement or a reload. The initial NavigationEntry should
1377 // retain its "initial NavigationEntry" status in this case.
1378 details->should_stay_as_initial_entry = true;
Rakina Zata Amni2322f4f82022-01-24 13:24:241379 } else if (navigation_request->is_synchronous_renderer_commit() &&
1380 !navigation_request->IsSameDocument() && !rfh->GetParent() &&
1381 params.should_replace_current_entry) {
1382 // This is a synchronous about:blank navigation on the main frame, which
1383 // used to not create a NavigationEntry when we have no NavigationEntry on
1384 // FrameTree creation. We now have the initial NavigationEntry and are on
1385 // the initial NavigationEntry. To preserve old behavior, we should still
1386 // keep the "initial" status for the new NavigationEntry that we will
1387 // create for this navigation, so that subframe navigations under the
1388 // synchronously committed about:blank document will never append new
1389 // NavigationEntry, and instead will just reuse the initial
1390 // NavigationEntry and modify the corresponding FrameNavigationEntries.
1391 // See also https://crbug.com/1277414.
1392 details->should_stay_as_initial_entry = true;
Rakina Zata Amniddf10502022-01-15 02:56:551393 }
1394 }
1395 DCHECK(!details->should_stay_as_initial_entry ||
1396 GetLastCommittedEntry()->IsInitialEntry());
[email protected]4bf3522c2010-08-19 21:00:201397
eugenebutee08663a2017-04-27 17:43:121398 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441399 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121400
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071401 details->is_prerender_activation =
1402 navigation_request->IsPrerenderedPageActivation();
Robert Lin540dbd12022-04-28 22:07:241403 details->is_in_active_page = navigation_request->GetRenderFrameHost()
1404 ->GetOutermostMainFrame()
1405 ->IsInPrimaryMainFrame();
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071406
Peter Boströmd7592132019-01-30 04:50:311407 // Make sure we do not discard the pending entry for a different ongoing
1408 // navigation when a same document commit comes in unexpectedly from the
1409 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1410 // other navigation types. See https://crbug.com/900036.
1411 // TODO(crbug.com/926009): Handle history.pushState() as well.
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061412 bool keep_pending_entry =
1413 is_same_document_navigation &&
1414 details->type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY &&
1415 pending_entry_ && !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311416
[email protected]0e8db942008-09-24 21:21:481417 switch (details->type) {
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061418 case NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY:
Charlie Reisc0f17d2d2021-01-12 18:52:491419 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051420 rfh, params, details->is_same_document, details->did_replace_entry,
Rakina Zata Amnia4e27222021-12-22 01:05:001421 previous_document_was_activated, navigation_request, details);
[email protected]e9ba4472008-09-14 15:42:431422 break;
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061423 case NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY:
Charlie Reisc0f17d2d2021-01-12 18:52:491424 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1425 was_restored, navigation_request,
Rakina Zata Amnia4e27222021-12-22 01:05:001426 keep_pending_entry, details);
[email protected]e9ba4472008-09-14 15:42:431427 break;
[email protected]8ff00d72012-10-23 19:12:211428 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471429 RendererDidNavigateNewSubframe(
1430 rfh, params, details->is_same_document, details->did_replace_entry,
Rakina Zata Amnia4e27222021-12-22 01:05:001431 previous_document_was_activated, navigation_request, details);
[email protected]e9ba4472008-09-14 15:42:431432 break;
[email protected]8ff00d72012-10-23 19:12:211433 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391434 if (!RendererDidNavigateAutoSubframe(
Nate Chapinc7019dd7d2021-06-25 18:29:251435 rfh, params, details->is_same_document,
Rakina Zata Amnia4e27222021-12-22 01:05:001436 was_on_initial_empty_document, navigation_request, details)) {
creisce0ef3572017-01-26 17:53:081437 // We don't send a notification about auto-subframe PageState during
1438 // UpdateStateForFrame, since it looks like nothing has changed. Send
1439 // it here at commit time instead.
1440 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431441 return false;
creis59d5a47cb2016-08-24 23:57:191442 }
[email protected]e9ba4472008-09-14 15:42:431443 break;
Rakina Zata Amni2322f4f82022-01-24 13:24:241444 case NAVIGATION_TYPE_NAV_IGNORE:
1445 // If a pending navigation was in progress, this canceled it. We should
1446 // discard it and make sure it is removed from the URL bar. After that,
1447 // there is nothing we can do with this navigation, so we just return to
1448 // the caller that nothing has happened.
1449 if (pending_entry_)
1450 DiscardNonCommittedEntries();
1451 return false;
Aran Gilman37d11632019-10-08 23:07:151452 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431453 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151454 break;
[email protected]765b35502008-08-21 00:51:201455 }
1456
[email protected]688aa65c62012-09-28 04:32:221457 // At this point, we know that the navigation has just completed, so
1458 // record the time.
1459 //
1460 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261461 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331462 base::Time timestamp =
1463 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1464 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131465 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221466
Peter Boströmd7592132019-01-30 04:50:311467 // If we aren't keeping the pending entry, there shouldn't be one at this
1468 // point. Clear it again in case any error cases above forgot to do so.
1469 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1470 // been cleared instead of protecting against it.
1471 if (!keep_pending_entry)
Rakina Zata Amniddf10502022-01-15 02:56:551472 DiscardNonCommittedEntriesWithCommitDetails(details);
[email protected]f233e4232013-02-23 00:55:141473
[email protected]e9ba4472008-09-14 15:42:431474 // All committed entries should have nonempty content state so WebKit doesn't
1475 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471476 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321477 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221478 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441479 active_entry->SetHttpStatusCode(params.http_status_code);
Fergal Daly0686c0e2022-06-28 02:08:141480
Alexander Timind2f2e4f22019-04-02 20:04:531481 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1482 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281483 if (back_forward_cache_metrics &&
1484 !active_entry->back_forward_cache_metrics()) {
Alexander Timind2f2e4f22019-04-02 20:04:531485 active_entry->set_back_forward_cache_metrics(
1486 std::move(back_forward_cache_metrics));
1487 }
Dave Tapuskaa2ab4f252021-07-08 21:31:281488
1489 // `back_forward_cache_metrics()` may return null as we do not record
1490 // back-forward cache metrics for navigations in non-primary frame trees.
1491 if (active_entry->back_forward_cache_metrics()) {
Fergal Daly0686c0e2022-06-28 02:08:141492 // TODO(https://crbug.com/1338089): Remove this.
1493 // These are both only available from details at this point, so we capture
1494 // them here.
1495 SCOPED_CRASH_KEY_NUMBER("BFCacheMismatch", "navigation_type",
1496 details->type);
1497 SCOPED_CRASH_KEY_BOOL("BFCacheMismatch", "did_replace",
1498 details->did_replace_entry);
Dave Tapuskaa2ab4f252021-07-08 21:31:281499 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
1500 navigation_request,
1501 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
1502 }
naskoc7533512016-05-06 17:01:121503
Charles Reisc0507202017-09-21 00:40:021504 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1505 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1506 // A mismatch can occur if the renderer lies or due to a unique name collision
1507 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121508 FrameNavigationEntry* frame_entry =
1509 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021510 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1511 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031512 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081513 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1514 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031515 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201516
1517 // Remember the bindings the renderer process has at this point, so that
1518 // we do not grant this entry additional bindings if we come back to it.
1519 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301520 }
[email protected]132e281a2012-07-31 18:32:441521
[email protected]97d8f0d2013-10-29 16:49:211522 // Once it is committed, we no longer need to track several pieces of state on
1523 // the entry.
naskoc7533512016-05-06 17:01:121524 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131525
[email protected]49bd30e62011-03-22 20:12:591526 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221527 // TODO(creis): This check won't pass for subframes until we create entries
1528 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001529 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421530 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591531
[email protected]e9ba4472008-09-14 15:42:431532 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001533 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001534 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311535 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531536
arthursonzogni7ddc6542021-04-09 09:16:501537 active_entry->SetIsOverridingUserAgent(
1538 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031539
[email protected]93f230e02011-06-01 14:40:001540 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291541
aelias100c9192017-01-13 00:01:431542 if (overriding_user_agent_changed)
1543 delegate_->UpdateOverridingUserAgent();
1544
creis03b48002015-11-04 00:54:561545 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1546 // one knows the latest NavigationEntry it is showing (whether it has
1547 // committed anything in this navigation or not). This allows things like
1548 // state and title updates from RenderFrames to apply to the latest relevant
1549 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381550 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001551 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381552 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
Hayato Ito2c8c08d02021-06-23 03:38:431553
1554 if (navigation_request->IsPrerenderedPageActivation()) {
1555 BroadcastHistoryOffsetAndLength();
1556 // TODO(crbug.com/1222893): Broadcasting happens after the prerendered page
1557 // is activated. As a result, a "prerenderingchange" event listener sees the
1558 // history.length which is not updated yet. We should guarantee that
1559 // history's length and offset should be updated before a
1560 // "prerenderingchange" event listener runs. One possible approach is to use
1561 // the same IPC which "prerenderingchange" uses, and propagate history's
1562 // length and offset together with that.
1563 }
1564
[email protected]e9ba4472008-09-14 15:42:431565 return true;
initial.commit09911bf2008-07-26 23:55:291566}
1567
[email protected]8ff00d72012-10-23 19:12:211568NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321569 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101570 const mojom::DidCommitProvisionalLoadParams& params,
Rakina Zata Amni2322f4f82022-01-24 13:24:241571 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591572 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511573 "ClassifyNavigation");
1574
avi7c6f35e2015-05-08 17:52:381575 if (params.did_create_new_entry) {
Rakina Zata Amni2322f4f82022-01-24 13:24:241576 // A new entry. We may or may not have a corresponding pending entry, and
1577 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001578 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491579 trace_return.set_return_reason("new entry, no parent, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061580 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381581 }
1582
Rakina Zata Amni2322f4f82022-01-24 13:24:241583 // When this is a new subframe navigation, we should have a committed page
1584 // in which it's a subframe. This may not be the case when an iframe is
1585 // navigated on a popup navigated to about:blank (the iframe would be
1586 // written into the popup by script on the main page). For these cases,
1587 // there isn't any navigation stuff we can do, so just ignore it.
1588 if (!GetLastCommittedEntry()) {
1589 trace_return.set_return_reason("new entry, no last committed, ignore");
1590 return NAVIGATION_TYPE_NAV_IGNORE;
1591 }
1592
1593 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511594 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381595 return NAVIGATION_TYPE_NEW_SUBFRAME;
1596 }
1597
Charlie Reisc0f17d2d2021-01-12 18:52:491598 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381599 DCHECK(!params.history_list_was_cleared);
1600
avi39c1edd32015-06-04 20:06:001601 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381602 // All manual subframes would be did_create_new_entry and handled above, so
1603 // we know this is auto.
Rakina Zata Amni2322f4f82022-01-24 13:24:241604 if (GetLastCommittedEntry()) {
1605 trace_return.set_return_reason("subframe, last commmited, auto subframe");
1606 return NAVIGATION_TYPE_AUTO_SUBFRAME;
1607 }
1608
1609 // We ignore subframes created in non-committed pages; we'd appreciate if
1610 // people stopped doing that.
1611 trace_return.set_return_reason("subframe, no last commmited, ignore");
1612 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381613 }
1614
Rakina Zata Amnif6950d552020-11-24 03:26:101615 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1616 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381617 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1618 // create a new page.
1619
Rakina Zata Amni2322f4f82022-01-24 13:24:241620 // Just like above in the did_create_new_entry case, it's possible to
1621 // scribble onto an uncommitted page. Again, there isn't any navigation
1622 // stuff that we can do, so ignore it here as well.
1623 NavigationEntry* last_committed = GetLastCommittedEntry();
1624 if (!last_committed) {
1625 trace_return.set_return_reason("nav entry 0, no last committed, ignore");
1626 return NAVIGATION_TYPE_NAV_IGNORE;
1627 }
1628
Hayato Ito2ae49442021-07-02 02:59:251629 // This main frame navigation is not a history navigation (since
1630 // nav_entry_id is 0), but didn't create a new entry. So this must be a
1631 // reload or a replacement navigation, which will modify the existing entry.
1632 //
Nasko Oskov332593c2018-08-16 17:21:341633 // TODO(nasko): With error page isolation, reloading an existing session
1634 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491635 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341636 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511637 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491638 "nav entry 0, last committed, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061639 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381640 }
1641
Rakina Zata Amnif6950d552020-11-24 03:26:101642 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521643 // If the SiteInstance of the |pending_entry_| does not match the
1644 // SiteInstance that got committed, treat this as a new navigation with
1645 // replacement. This can happen if back/forward/reload encounters a server
1646 // redirect to a different site or an isolated error page gets successfully
1647 // reloaded into a different SiteInstance.
1648 if (pending_entry_->site_instance() &&
1649 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491650 trace_return.set_return_reason("pending matching nav entry, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061651 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521652 }
creis77c9aa32015-09-25 19:59:421653
Nasko Oskovaee2f862018-06-15 00:05:521654 if (pending_entry_index_ == -1) {
1655 // In this case, we have a pending entry for a load of a new URL but Blink
1656 // didn't do a new navigation (params.did_create_new_entry). First check
1657 // to make sure Blink didn't treat a new cross-process navigation as
1658 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161659 // we must treat it as NEW rather than the converted reload case below,
1660 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521661 if (!GetLastCommittedEntry() ||
1662 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161663 trace_return.set_return_reason("new pending, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061664 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521665 }
1666
1667 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161668 // We will create a pending entry, but NavigateWithoutEntry will convert
1669 // it to a reload since it's the same page and not create a new entry for
1670 // it. (The user doesn't want to have a new back/forward entry when they
1671 // do this.) Therefore we want to just ignore the pending entry and go
1672 // back to where we were (the "existing entry").
1673 trace_return.set_return_reason("new pending, existing (same) entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061674 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521675 }
avi7c6f35e2015-05-08 17:52:381676 }
1677
creis26d22632017-04-21 20:23:561678 // Everything below here is assumed to be an existing entry, but if there is
1679 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511680 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491681 trace_return.set_return_reason("no last committed, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061682 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511683 }
creis26d22632017-04-21 20:23:561684
Rakina Zata Amni153d5702021-09-13 22:48:001685 if (navigation_request->commit_params().intended_as_new_entry) {
avi7c6f35e2015-05-08 17:52:381686 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491687 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1688 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161689 trace_return.set_return_reason("intended as new entry, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061690 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381691 }
1692
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121693 if (navigation_request->DidEncounterError() &&
1694 failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101695 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381696 // If the renderer was going to a new pending entry that got cleared because
1697 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491698 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381699 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511700 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491701 "unreachable, matching pending, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061702 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381703 }
1704
Charlie Reisc0f17d2d2021-01-12 18:52:491705 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101706 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511707 trace_return.traced_value()->SetInteger("existing_entry_index",
1708 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381709 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441710 // The renderer has committed a navigation to an entry that no longer
1711 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491712 trace_return.set_return_reason("existing entry -1, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061713 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381714 }
1715
avi7c6f35e2015-05-08 17:52:381716 // Since we weeded out "new" navigations above, we know this is an existing
1717 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491718 trace_return.set_return_reason("default return, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061719 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381720}
1721
Rakina Zata Amni3460d382021-10-29 00:43:371722void NavigationControllerImpl::UpdateNavigationEntryDetails(
1723 NavigationEntryImpl* entry,
1724 RenderFrameHostImpl* rfh,
1725 const mojom::DidCommitProvisionalLoadParams& params,
1726 NavigationRequest* request,
1727 NavigationEntryImpl::UpdatePolicy update_policy,
Rakina Zata Amnia4e27222021-12-22 01:05:001728 bool is_new_entry,
1729 LoadCommittedDetails* commit_details) {
Rakina Zata Amni3460d382021-10-29 00:43:371730 // Update the FrameNavigationEntry.
Rakina Zata Amniafd3c6582021-11-30 06:19:171731 std::vector<GURL> redirects;
Rakina Zata Amni3460d382021-10-29 00:43:371732 entry->AddOrUpdateFrameEntry(
1733 rfh->frame_tree_node(), update_policy, params.item_sequence_number,
Domenic Denicolacc094fb2022-03-16 23:40:571734 params.document_sequence_number, params.navigation_api_key,
Rakina Zata Amni3460d382021-10-29 00:43:371735 rfh->GetSiteInstance(), nullptr, params.url,
1736 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amniafd3c6582021-11-30 06:19:171737 Referrer(*params.referrer),
1738 request ? request->common_params().initiator_origin : params.origin,
1739 request ? request->GetRedirectChain() : redirects, params.page_state,
1740 params.method, params.post_id, nullptr /* blob_url_loader_factory */,
1741 (request && request->web_bundle_navigation_info())
Rakina Zata Amni3460d382021-10-29 00:43:371742 ? request->web_bundle_navigation_info()->Clone()
1743 : nullptr,
Rakina Zata Amniafd3c6582021-11-30 06:19:171744 (request ? request->GetSubresourceWebBundleNavigationInfo() : nullptr),
Rakina Zata Amni3460d382021-10-29 00:43:371745 ComputePolicyContainerPoliciesForFrameEntry(
Rakina Zata Amniafd3c6582021-11-30 06:19:171746 rfh, request && request->IsSameDocument(),
1747 request ? request->common_params().url : params.url));
Rakina Zata Amni3460d382021-10-29 00:43:371748
1749 if (rfh->GetParent()) {
1750 // Only modify the NavigationEntry for main frame navigations.
1751 return;
1752 }
1753 if (entry->update_virtual_url_with_url())
1754 UpdateVirtualURLToURL(entry, params.url);
1755 // Don't use the page type from the pending entry. Some interstitial page
1756 // may have set the type to interstitial. Once we commit, however, the page
1757 // type must always be normal or error.
Rakina Zata Amniafd3c6582021-11-30 06:19:171758 entry->set_page_type((request && request->DidEncounterError())
1759 ? PAGE_TYPE_ERROR
1760 : PAGE_TYPE_NORMAL);
Rakina Zata Amnif297a802022-01-18 03:53:431761 if (commit_details && commit_details->should_stay_as_initial_entry) {
1762 // Retain the "initial NavigationEntry" status.
1763 if (request->IsSameDocument()) {
1764 // If this is for a same-document navigation, the NavigationEntry must be
1765 // reused and should already be marked as the initial NavigationEntry.
1766 DCHECK(entry->IsInitialEntry());
1767 } else {
1768 // If this is for a cross-document navigation, it can be caused by a
1769 // renderer-initiated reload, or the synchronous about:blank commit. Mark
1770 // "for synchronous about:blank" in the latter case, and also when it is
1771 // reloading a "for synchronous about:blank" entry. Otherwise, the entry
1772 // is not for a synchronous about:blank commit.
1773 NavigationEntryImpl::InitialNavigationEntryState new_state =
1774 NavigationEntryImpl::InitialNavigationEntryState::
1775 kInitialNotForSynchronousAboutBlank;
1776 if (entry->IsInitialEntryForSynchronousAboutBlank() ||
1777 request->is_synchronous_renderer_commit()) {
1778 new_state = NavigationEntryImpl::InitialNavigationEntryState::
1779 kInitialForSynchronousAboutBlank;
1780 }
1781 entry->set_initial_navigation_entry_state(new_state);
1782 }
1783 } else if (commit_details && !commit_details->should_stay_as_initial_entry) {
1784 // Remove the "initial NavigationEntry" status.
1785 entry->set_initial_navigation_entry_state(
1786 NavigationEntryImpl::InitialNavigationEntryState::kNonInitial);
Rakina Zata Amnia4e27222021-12-22 01:05:001787 }
Rakina Zata Amniddf10502022-01-15 02:56:551788
Rakina Zata Amni3460d382021-10-29 00:43:371789 if (is_new_entry) {
1790 // Some properties of the NavigationEntry are only set when the entry is
1791 // new (we aren't reusing it).
1792 entry->SetTransitionType(params.transition);
Rakina Zata Amniafd3c6582021-11-30 06:19:171793 entry->SetOriginalRequestURL(request ? request->GetOriginalRequestURL()
1794 : GURL::EmptyGURL());
Rakina Zata Amni3460d382021-10-29 00:43:371795 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
1796 entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
1797 } else {
1798 // We are reusing the NavigationEntry. The site instance will normally be
1799 // the same except for a few cases:
1800 // 1) session restore, when no site instance will be assigned or
1801 // 2) redirect, when the site instance is reset.
1802 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
1803 entry->site_instance() == rfh->GetSiteInstance());
1804 }
1805}
1806
Rakina Zata Amniafd3c6582021-11-30 06:19:171807void NavigationControllerImpl::CreateInitialEntry() {
1808 DCHECK_EQ(entries_.size(), 0u);
1809 RenderFrameHostImpl* rfh = frame_tree_.root()->current_frame_host();
1810 auto params = mojom::DidCommitProvisionalLoadParams::New();
1811 // The initial NavigationEntry's URL is the empty URL. This preserves the old
1812 // behavior of WebContent's GetLastCommittedURL() and GetVisibleURL() from
1813 // before we have initial NavigationEntries.
1814 params->url = GURL::EmptyGURL();
1815 params->http_status_code = 0;
1816 params->url_is_unreachable = false;
1817 params->method = "GET";
1818 params->should_replace_current_entry = false;
1819 params->post_id = -1;
1820 params->embedding_token = base::UnguessableToken::Create();
1821 params->navigation_token = base::UnguessableToken::Create();
1822 params->did_create_new_entry = true;
1823 params->origin = rfh->GetLastCommittedOrigin();
1824 params->should_update_history = true;
1825 params->item_sequence_number = 0;
1826 params->document_sequence_number = 0;
1827 bool is_in_fenced_frame_tree = rfh->frame_tree_node()->IsInFencedFrameTree();
1828 params->transition = is_in_fenced_frame_tree
1829 ? ui::PAGE_TRANSITION_AUTO_SUBFRAME
1830 : ui::PAGE_TRANSITION_LINK;
1831 params->referrer = blink::mojom::Referrer::New();
1832
1833 // Create and insert the initial NavigationEntry.
1834 auto new_entry = std::make_unique<NavigationEntryImpl>(
1835 rfh->GetSiteInstance(), params->url, Referrer(*params->referrer),
1836 rfh->GetLastCommittedOrigin(), std::u16string() /* title */,
1837 ui::PAGE_TRANSITION_TYPED, false /* renderer_initiated */,
1838 nullptr /* blob_url_loader_factory */, true /* is_initial_entry */);
1839 UpdateNavigationEntryDetails(
1840 new_entry.get(), rfh, *params, nullptr /* request */,
Rakina Zata Amnia4e27222021-12-22 01:05:001841 NavigationEntryImpl::UpdatePolicy::kUpdate, true /* is_new_entry */,
1842 nullptr /* commit_details */);
Rakina Zata Amniafd3c6582021-11-30 06:19:171843
1844 InsertOrReplaceEntry(std::move(new_entry), false /* replace_entry */,
1845 false /* was_post_commit_error */,
Rakina Zata Amnia4e27222021-12-22 01:05:001846 is_in_fenced_frame_tree, nullptr /* commit_details */);
Rakina Zata Amniafd3c6582021-11-30 06:19:171847}
1848
Charlie Reisc0f17d2d2021-01-12 18:52:491849void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321850 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071851 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361852 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441853 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471854 bool previous_document_was_activated,
Rakina Zata Amnia4e27222021-12-22 01:05:001855 NavigationRequest* request,
1856 LoadCommittedDetails* commit_details) {
dcheng9bfa5162016-04-09 01:00:571857 std::unique_ptr<NavigationEntryImpl> new_entry;
Anton Bikineevf62d1bf2021-05-15 17:56:071858 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451859 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061860
creisf49dfc92016-07-26 17:05:181861 // First check if this is an in-page navigation. If so, clone the current
1862 // entry instead of looking at the pending entry, because the pending entry
1863 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361864 if (is_same_document && GetLastCommittedEntry()) {
Nate Chapin63db0d12022-01-20 22:03:301865 FrameNavigationEntry* previous_frame_entry =
1866 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
Patrick Monette50e8bd82019-06-13 22:40:451867 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481868 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Domenic Denicolacc094fb2022-03-16 23:40:571869 params.document_sequence_number, params.navigation_api_key,
Nate Chapinfbfe5af2021-06-10 17:22:081870 rfh->GetSiteInstance(), nullptr, params.url,
1871 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091872 Referrer(*params.referrer), initiator_origin,
1873 request->GetRedirectChain(), params.page_state, params.method,
1874 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391875 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481876 request->GetSubresourceWebBundleNavigationInfo(),
Antonio Sartori78a749f2020-11-30 12:03:391877 // We will set the document policies later in this function.
Nate Chapin63db0d12022-01-20 22:03:301878 nullptr /* policy_container_policies */,
Domenic Denicolacc094fb2022-03-16 23:40:571879 // Try to preserve protect_url_in_navigation_api from the previous
Nate Chapin63db0d12022-01-20 22:03:301880 // FrameNavigationEntry.
1881 previous_frame_entry &&
Domenic Denicolacc094fb2022-03-16 23:40:571882 previous_frame_entry->protect_url_in_navigation_api());
Charles Reisf44482022017-10-13 21:15:031883
creisf49dfc92016-07-26 17:05:181884 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Harkiran Bolaria081c5c22021-09-13 08:44:531885 frame_entry, true, request->frame_tree_node(), frame_tree_.root());
Mike West800532c2021-10-14 09:26:521886 if (new_entry->GetURL().DeprecatedGetOriginAsURL() !=
1887 params.url.DeprecatedGetOriginAsURL()) {
jama78746e2017-02-22 17:21:571888 // TODO(jam): we had one report of this with a URL that was redirecting to
1889 // only tildes. Until we understand that better, don't copy the cert in
1890 // this case.
1891 new_entry->GetSSL() = SSLStatus();
jama78746e2017-02-22 17:21:571892 }
creisf49dfc92016-07-26 17:05:181893
Patrick Monette50e8bd82019-06-13 22:40:451894 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1895 // that |frame_entry| should now have a reference count of exactly 2: one
1896 // due to the local variable |frame_entry|, and another due to |new_entry|
1897 // also retaining one. This should never fail, because it's the main frame.
1898 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181899 }
1900
Harkiran Bolaria59290d62021-03-17 01:53:011901 // If this is an activation navigation from a prerendered page, transfer the
1902 // new entry from an entry already created and stored in the
1903 // NavigationRequest. |new_entry| will not have been set prior to this as
1904 // |is_same_document| is mutually exclusive with
1905 // |IsPrerenderedPageActivation|.
1906 if (request->IsPrerenderedPageActivation()) {
1907 DCHECK(!is_same_document);
1908 DCHECK(!new_entry);
1909 new_entry = request->TakePrerenderNavigationEntry();
1910 DCHECK(new_entry);
1911 }
1912
Charlie Reisc0f17d2d2021-01-12 18:52:491913 // Only make a copy of the pending entry if it is appropriate for the new
1914 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451915 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041916 // 1. The SiteInstance hasn't been assigned to something else.
1917 // 2. The pending entry was intended as a new entry, rather than being a
1918 // history navigation that was interrupted by an unrelated,
1919 // renderer-initiated navigation.
1920 // TODO(csharrison): Investigate whether we can remove some of the coarser
1921 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451922 if (!new_entry && PendingEntryMatchesRequest(request) &&
1923 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341924 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431925 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351926 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431927
Camille Lamy62b826012019-02-26 09:15:471928 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451929 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
creisf49dfc92016-07-26 17:05:181930 }
1931
Charlie Reisc0f17d2d2021-01-12 18:52:491932 // For cross-document commits with no matching pending entry, create a new
1933 // entry.
creisf49dfc92016-07-26 17:05:181934 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061935 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071936 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1937 initiator_origin,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:581938 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451939 params.transition, request->IsRendererInitiated(),
Rakina Zata Amniafd3c6582021-11-30 06:19:171940 nullptr, // blob_url_loader_factory
1941 false); // is_initial_entry
[email protected]f8f93eb2012-09-25 03:06:241942
1943 // Find out whether the new entry needs to update its virtual URL on URL
1944 // change and set up the entry accordingly. This is needed to correctly
1945 // update the virtual URL when replaceState is called after a pushState.
1946 GURL url = params.url;
1947 bool needs_update = false;
Charlie Reisc0f17d2d2021-01-12 18:52:491948 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411949 // update the virtual URL based on the new URL. For example, this is needed
1950 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1951 // the URL.
Rakina Zata Amni3460d382021-10-29 00:43:371952 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1953 &url, browser_context_, &needs_update);
1954 new_entry->set_update_virtual_url_with_url(needs_update);
1955
Camille Lamy62b826012019-02-26 09:15:471956 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451957 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
[email protected]e9ba4472008-09-14 15:42:431958 }
1959
Harkiran Bolaria59290d62021-03-17 01:53:011960 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
1961 // for prerendered contents, if any. This is because prerendering/activation
1962 // technically won't be creating a new document. Unlike BFCache, prerendering
1963 // creates a new NavigationEntry rather than using an existing one.
1964 if (!request->IsPrerenderedPageActivation()) {
Rakina Zata Amni3460d382021-10-29 00:43:371965 UpdateNavigationEntryDetails(new_entry.get(), rfh, params, request,
1966 NavigationEntryImpl::UpdatePolicy::kUpdate,
Rakina Zata Amnia4e27222021-12-22 01:05:001967 true /* is_new_entry */, commit_details);
creis8b5cd4c2015-06-19 00:11:081968
Harkiran Bolaria59290d62021-03-17 01:53:011969 // history.pushState() is classified as a navigation to a new page, but sets
1970 // is_same_document to true. In this case, we already have the title and
1971 // favicon available, so set them immediately.
1972 if (is_same_document && GetLastCommittedEntry()) {
1973 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
1974 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1975 }
[email protected]3a868f212014-08-09 10:41:191976 }
[email protected]ff64b3e2014-05-31 04:07:331977
[email protected]60d6cca2013-04-30 08:47:131978 DCHECK(!params.history_list_was_cleared || !replace_entry);
1979 // The browser requested to clear the session history when it initiated the
1980 // navigation. Now we know that the renderer has updated its state accordingly
1981 // and it is safe to also clear the browser side history.
1982 if (params.history_list_was_cleared) {
Rakina Zata Amniddf10502022-01-15 02:56:551983 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]60d6cca2013-04-30 08:47:131984 entries_.clear();
1985 last_committed_entry_index_ = -1;
1986 }
1987
Nasko Oskovaee2f862018-06-15 00:05:521988 // If this is a new navigation with replacement and there is a
1989 // pending_entry_ which matches the navigation reported by the renderer
1990 // process, then it should be the one replaced, so update the
1991 // last_committed_entry_index_ to use it.
1992 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101993 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521994 last_committed_entry_index_ = pending_entry_index_;
1995 }
1996
Alexander Timine3ec4192020-04-20 16:39:401997 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411998 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401999 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:052000
Carlos IL42b416592019-10-07 23:10:362001 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
Dave Tapuska87696ae2021-11-18 18:48:312002 !request->post_commit_error_page_html().empty(),
Rakina Zata Amnia4e27222021-12-22 01:05:002003 rfh->frame_tree_node()->IsInFencedFrameTree(),
2004 commit_details);
[email protected]e9ba4472008-09-14 15:42:432005}
2006
Charlie Reisc0f17d2d2021-01-12 18:52:492007void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:322008 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072009 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:362010 bool is_same_document,
jam48cea9082017-02-15 06:13:292011 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:452012 NavigationRequest* request,
Rakina Zata Amnia4e27222021-12-22 01:05:002013 bool keep_pending_entry,
2014 LoadCommittedDetails* commit_details) {
creis26d22632017-04-21 20:23:562015 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
2016 << "that a last committed entry exists.";
2017
[email protected]e9ba4472008-09-14 15:42:432018 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:002019 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:432020
Charlie Reis7e2cb6d2021-01-26 01:27:162021 NavigationEntryImpl* entry = nullptr;
Rakina Zata Amni153d5702021-09-13 22:48:002022 if (request->commit_params().intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:162023 // We're guaranteed to have a last committed entry if intended_as_new_entry
2024 // is true.
avicbdc4c12015-07-01 16:07:112025 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:162026 DCHECK(entry);
2027
2028 // If the NavigationRequest matches a new pending entry and is classified as
2029 // EXISTING_ENTRY, then it is a navigation to the same URL that was
2030 // converted to a reload, such as a user pressing enter in the omnibox.
2031 if (pending_entry_ && pending_entry_index_ == -1 &&
2032 pending_entry_->GetUniqueID() ==
2033 request->commit_params().nav_entry_id) {
2034 // Note: The pending entry will usually have a real ReloadType here, but
2035 // it can still be ReloadType::NONE in cases that
2036 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
2037
2038 // If we classified this correctly, the SiteInstance should not have
2039 // changed.
2040 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
2041
2042 // For converted reloads, we assign the entry's unique ID to be that of
2043 // the new one. Since this is always the result of a user action, we want
2044 // to dismiss infobars, etc. like a regular user-initiated navigation.
2045 entry->set_unique_id(pending_entry_->GetUniqueID());
2046
2047 // The extra headers may have changed due to reloading with different
2048 // headers.
2049 entry->set_extra_headers(pending_entry_->extra_headers());
2050 }
2051 // Otherwise, this was intended as a new entry but the pending entry was
2052 // lost in the meantime and no new entry was created. We are stuck at the
2053 // last committed entry.
2054
2055 // Even if this is a converted reload from pressing enter in the omnibox,
2056 // the server could redirect, requiring an update to the SSL status. If this
2057 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:452058 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:162059 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:472060 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:452061 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:162062 }
Rakina Zata Amnif6950d552020-11-24 03:26:102063 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:112064 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:102065 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b902016-09-01 16:58:162066
eugenebut604866f2017-05-10 21:35:362067 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:452068 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:362069 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
2070 // this was a restored same document navigation entry, then it won't have
2071 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
2072 // navigated it.
jam48cea9082017-02-15 06:13:292073 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
Mike West800532c2021-10-14 09:26:522074 if (entry->GetURL().DeprecatedGetOriginAsURL() ==
2075 last_entry->GetURL().DeprecatedGetOriginAsURL() &&
jam48cea9082017-02-15 06:13:292076 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
2077 was_restored) {
2078 entry->GetSSL() = last_entry->GetSSL();
2079 }
2080 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:452081 // In rapid back/forward navigations |request| sometimes won't have a cert
2082 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:192083 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:452084 if (request->GetSSLInfo().has_value() &&
2085 request->GetSSLInfo()->is_valid()) {
2086 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
Mike West800532c2021-10-14 09:26:522087 } else if (entry->GetURL().DeprecatedGetOriginAsURL() !=
2088 request->GetURL().DeprecatedGetOriginAsURL()) {
John Abd-El-Malek3f247082017-12-07 19:02:192089 entry->GetSSL() = SSLStatus();
2090 }
jam48cea9082017-02-15 06:13:292091 }
avicbdc4c12015-07-01 16:07:112092 } else {
Feifei Wang2ab8ba6c2022-04-13 22:19:272093 // This is renderer-initiated. The only kinds of renderer-initiated
Rakina Zata Amni557afb92021-07-17 04:39:572094 // navigations that are EXISTING_ENTRY are same-document navigations that
2095 // result in replacement (e.g. history.replaceState(), location.replace(),
2096 // forced replacements for trivial session history contexts). For these
2097 // cases, we reuse the last committed entry.
avicbdc4c12015-07-01 16:07:112098 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:102099
Mikel Astizba9cf2fd2017-12-17 10:38:102100 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
2101 // to avoid misleading interpretations (e.g. URLs paired with
2102 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
2103 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
2104 // | PAGE_TRANSITION_CLIENT_REDIRECT).
2105
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572106 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:102107
eugenebut604866f2017-05-10 21:35:362108 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:452109 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:362110 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:472111 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:452112 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
avicbdc4c12015-07-01 16:07:112113 }
2114 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:432115
Rakina Zata Amni3460d382021-10-29 00:43:372116 UpdateNavigationEntryDetails(entry, rfh, params, request,
2117 NavigationEntryImpl::UpdatePolicy::kUpdate,
Rakina Zata Amnia4e27222021-12-22 01:05:002118 false /* is_new_entry */, commit_details);
creis22a7b4c2016-04-28 07:20:302119
[email protected]5ccd4dc2012-10-24 02:28:142120 // The redirected to page should not inherit the favicon from the previous
2121 // page.
eugenebut604866f2017-05-10 21:35:362122 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:482123 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:142124
Peter Boströmd7592132019-01-30 04:50:312125 // We should also usually discard the pending entry if it corresponds to a
2126 // different navigation, since that one is now likely canceled. In rare
2127 // cases, we leave the pending entry for another navigation in place when we
2128 // know it is still ongoing, to avoid a flicker in the omnibox (see
2129 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:432130 //
2131 // Note that we need to use the "internal" version since we don't want to
2132 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:312133 if (!keep_pending_entry)
Rakina Zata Amnia4e27222021-12-22 01:05:002134 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]40bcc302009-03-02 20:50:392135
Carlos IL4dea8902020-05-26 15:14:292136 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:112137 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:432138}
2139
[email protected]d202a7c2012-01-04 07:53:472140void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:322141 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072142 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:362143 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:472144 bool replace_entry,
2145 bool previous_document_was_activated,
Rakina Zata Amnia4e27222021-12-22 01:05:002146 NavigationRequest* request,
2147 LoadCommittedDetails* commit_details) {
avi25f5f9e2015-07-17 20:08:262148 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2149 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
Rakina Zata Amniddf10502022-01-15 02:56:552150 // The NEW_SUBFRAME path should never result in an initial NavigationEntry.
2151 DCHECK(!commit_details->should_stay_as_initial_entry);
[email protected]09b8f82f2009-06-16 20:22:112152
[email protected]e9ba4472008-09-14 15:42:432153 // Manual subframe navigations just get the current entry cloned so the user
2154 // can go back or forward to it. The actual subframe information will be
2155 // stored in the page state for each of those entries. This happens out of
2156 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:092157 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
2158 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:382159
Mikel Astizba9cf2fd2017-12-17 10:38:102160 // The DCHECK below documents the fact that we don't know of any situation
2161 // where |replace_entry| is true for subframe navigations. This simplifies
2162 // reasoning about the replacement struct for subframes (see
2163 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
2164 DCHECK(!replace_entry);
2165
Patrick Monette50e8bd82019-06-13 22:40:452166 // This FrameNavigationEntry might not end up being used in the
2167 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Anton Bikineevf62d1bf2021-05-15 17:56:072168 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452169 request->common_params().initiator_origin;
Nate Chapin63db0d12022-01-20 22:03:302170 std::unique_ptr<PolicyContainerPolicies> policy_container_policies =
2171 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
2172 request->GetURL());
Domenic Denicolacc094fb2022-03-16 23:40:572173 bool protect_url_in_navigation_api = false;
Nate Chapin63db0d12022-01-20 22:03:302174 if (is_same_document) {
2175 FrameNavigationEntry* previous_frame_entry =
2176 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
Domenic Denicolacc094fb2022-03-16 23:40:572177 // Try to preserve protect_url_in_navigation_api from the previous
Nate Chapin63db0d12022-01-20 22:03:302178 // FrameNavigationEntry.
Domenic Denicolacc094fb2022-03-16 23:40:572179 protect_url_in_navigation_api =
Nate Chapin63db0d12022-01-20 22:03:302180 previous_frame_entry &&
Domenic Denicolacc094fb2022-03-16 23:40:572181 previous_frame_entry->protect_url_in_navigation_api();
Nate Chapin63db0d12022-01-20 22:03:302182 } else {
Domenic Denicolacc094fb2022-03-16 23:40:572183 protect_url_in_navigation_api =
Nate Chapin63db0d12022-01-20 22:03:302184 policy_container_policies &&
Domenic Denicolacc094fb2022-03-16 23:40:572185 ShouldProtectUrlInNavigationApi(
Nate Chapin63db0d12022-01-20 22:03:302186 policy_container_policies->referrer_policy);
2187 }
2188
Patrick Monette50e8bd82019-06-13 22:40:452189 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:482190 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Domenic Denicolacc094fb2022-03-16 23:40:572191 params.document_sequence_number, params.navigation_api_key,
Nate Chapinfbfe5af2021-06-10 17:22:082192 rfh->GetSiteInstance(), nullptr, params.url,
2193 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:092194 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072195 params.page_state, params.method, params.post_id,
2196 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032197 request->web_bundle_navigation_info()
2198 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392199 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482200 request->GetSubresourceWebBundleNavigationInfo(),
Domenic Denicolacc094fb2022-03-16 23:40:572201 std::move(policy_container_policies), protect_url_in_navigation_api);
Charles Reisf44482022017-10-13 21:15:032202
creisce0ef3572017-01-26 17:53:082203 std::unique_ptr<NavigationEntryImpl> new_entry =
2204 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:452205 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:002206 frame_tree_.root());
creise062d542015-08-25 02:01:552207
Alexander Timine3ec4192020-04-20 16:39:402208 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:412209 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:402210 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:472211
creisce0ef3572017-01-26 17:53:082212 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:452213 // to replace, which can happen due to a unique name change. See
2214 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
2215 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:382216
Dave Tapuska87696ae2021-11-18 18:48:312217 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false,
Rakina Zata Amnia4e27222021-12-22 01:05:002218 rfh->frame_tree_node()->IsInFencedFrameTree(),
2219 commit_details);
[email protected]e9ba4472008-09-14 15:42:432220}
2221
[email protected]d202a7c2012-01-04 07:53:472222bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:322223 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072224 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392225 bool is_same_document,
Nate Chapinc7019dd7d2021-06-25 18:29:252226 bool was_on_initial_empty_document,
Rakina Zata Amnia4e27222021-12-22 01:05:002227 NavigationRequest* request,
2228 LoadCommittedDetails* commit_details) {
avi9f07a0c2015-02-18 22:51:292229 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2230 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2231
[email protected]e9ba4472008-09-14 15:42:432232 // We're guaranteed to have a previously committed entry, and we now need to
2233 // handle navigation inside of a subframe in it without creating a new entry.
2234 DCHECK(GetLastCommittedEntry());
2235
creis913c63ce2016-07-16 19:52:522236 // For newly created subframes, we don't need to send a commit notification.
2237 // This is only necessary for history navigations in subframes.
2238 bool send_commit_notification = false;
2239
Rakina Zata Amnif6950d552020-11-24 03:26:102240 // If |nav_entry_id| is non-zero and matches an existing entry, this
2241 // is a history navigation. Update the last committed index accordingly. If
2242 // we don't recognize the |nav_entry_id|, it might be a recently
2243 // pruned entry. We'll handle it below.
2244 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2245 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472246 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062247 // Make sure that a subframe commit isn't changing the main frame's
2248 // origin. Otherwise the renderer process may be confused, leading to a
2249 // URL spoof. We can't check the path since that may change
2250 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572251 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2252 // about:blank, file, and unique origins are more subtle to get right.
Charlie Reis95fbca442021-05-21 21:38:242253 // We should use checks similar to RenderFrameHostImpl's
2254 // CanCommitUrlAndOrigin on the main frame during subframe commits.
2255 // See https://crbug.com/1209092.
creis37988b92016-06-10 18:03:572256 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2257 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2258 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2259 dest_top_url.SchemeIsHTTPOrHTTPS() &&
Mike West800532c2021-10-14 09:26:522260 current_top_url.DeprecatedGetOriginAsURL() !=
2261 dest_top_url.DeprecatedGetOriginAsURL()) {
Chris Bookholt10f4b7332022-02-14 18:25:442262 bad_message::ReceivedBadMessage(rfh->GetMainFrame()->GetProcess(),
creisfb6eeb62016-05-10 19:01:512263 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062264 }
creis3cdc3b02015-05-29 23:00:472265
creis913c63ce2016-07-16 19:52:522266 // We only need to discard the pending entry in this history navigation
2267 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062268 last_committed_entry_index_ = entry_index;
Rakina Zata Amnia4e27222021-12-22 01:05:002269 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
creis913c63ce2016-07-16 19:52:522270
2271 // History navigations should send a commit notification.
2272 send_commit_notification = true;
avi98405c22015-05-21 20:47:062273 }
[email protected]e9ba4472008-09-14 15:42:432274 }
[email protected]f233e4232013-02-23 00:55:142275
creisce0ef3572017-01-26 17:53:082276 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2277 // it may be a "history auto" case where we update an existing one.
2278 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Nate Chapin9f169072021-06-09 19:32:372279
2280 // We may want to update |last_committed|'s FrameNavigationEntry (if one
2281 // exists), or we may want to clobber it and create a new one. We update in
2282 // cases where the corresponding FrameNavigationEntry is conceptually similar
2283 // to the document described by the commit params: same-document
2284 // navigations, history traversal to an existing entry, and reloads (including
2285 // a "soft reload" where we navigate to the same url without flagging it as a
2286 // reload). But in the case of a different document that is not logically
2287 // related to the committed FrameNavigationEntry's document (cross-document,
2288 // not same url, not a reload, not a history traversal), we replace rather
2289 // than update.
Nate Chapinc7019dd7d2021-06-25 18:29:252290 //
Nate Chapin9f169072021-06-09 19:32:372291 // In the case where we update, the FrameNavigationEntry will potentially be
2292 // shared across multiple NavigationEntries, and any updates will be reflected
2293 // in all of those NavigationEntries. In the replace case, any existing
2294 // sharing with other NavigationEntries will stop.
Nate Chapinc7019dd7d2021-06-25 18:29:252295 //
2296 // When navigating away from the initial empty document, we also update rather
2297 // than replace. Either update or replace will overwrite the initial empty
2298 // document state for |last_committed|, but if the FrameNavigationEntry for
2299 // the initial empty document is shared across multiple NavigationEntries (due
2300 // to a navigation in another frame), we want to make sure we overwrite the
2301 // initial empty document state everywhere this FrameNavigationEntry is used,
2302 // which is accompished by updating the existing FrameNavigationEntry.
Nate Chapin9f169072021-06-09 19:32:372303 FrameNavigationEntry* last_committed_frame_entry =
2304 last_committed->GetFrameEntry(rfh->frame_tree_node());
2305 NavigationEntryImpl::UpdatePolicy update_policy =
2306 NavigationEntryImpl::UpdatePolicy::kUpdate;
2307 if (request->common_params().navigation_type ==
Minggang Wangb9f3fa92021-07-01 15:30:312308 blink::mojom::NavigationType::DIFFERENT_DOCUMENT &&
Nate Chapin9f169072021-06-09 19:32:372309 last_committed_frame_entry &&
Nate Chapinc7019dd7d2021-06-25 18:29:252310 last_committed_frame_entry->url() != params.url &&
2311 !was_on_initial_empty_document) {
Nate Chapin9f169072021-06-09 19:32:372312 update_policy = NavigationEntryImpl::UpdatePolicy::kReplace;
2313 }
2314
Rakina Zata Amni3460d382021-10-29 00:43:372315 UpdateNavigationEntryDetails(last_committed, rfh, params, request,
Rakina Zata Amnia4e27222021-12-22 01:05:002316 update_policy, false /* is_new_entry */,
2317 commit_details);
creis625a0c7d2015-03-24 23:17:122318
creis913c63ce2016-07-16 19:52:522319 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432320}
2321
[email protected]d202a7c2012-01-04 07:53:472322int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232323 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032324 for (size_t i = 0; i < entries_.size(); ++i) {
2325 if (entries_[i].get() == entry)
2326 return i;
2327 }
2328 return -1;
[email protected]765b35502008-08-21 00:51:202329}
2330
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572331void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242332 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572333 NavigationControllerImpl* source =
2334 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572335 // Verify that we look new.
Rakina Zata Amni2322f4f82022-01-24 13:24:242336 if (blink::features::IsInitialNavigationEntryEnabled()) {
2337 DCHECK_EQ(1, GetEntryCount());
2338 DCHECK(GetLastCommittedEntry()->IsInitialEntry());
2339 } else {
2340 DCHECK_EQ(0, GetEntryCount());
2341 }
Lei Zhang96031532019-10-10 19:05:472342 DCHECK(!GetPendingEntry());
Rakina Zata Amniafd3c6582021-11-30 06:19:172343 entries_.clear();
[email protected]ce3fa3c2009-04-20 19:55:572344
Rakina Zata Amniafd3c6582021-11-30 06:19:172345 if (source->GetEntryCount() == 0) {
Rakina Zata Amniafd3c6582021-11-30 06:19:172346 return;
2347 }
[email protected]ce3fa3c2009-04-20 19:55:572348
Francois Dorayeaace782017-06-21 16:37:242349 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442350 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572351 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572352
Fergal Dalya1d569972021-03-16 03:24:532353 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302354 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532355 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2356 session_storage_namespace_map_[it.first] = source_namespace->Clone();
[email protected]fdac6ade2013-07-20 01:06:302357 }
[email protected]4e6419c2010-01-15 04:50:342358
Lukasz Anforowicz0de0f452020-12-02 19:57:152359 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572360}
2361
Aran Gilman37d11632019-10-08 23:07:152362void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2363 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162364 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012365 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162366
[email protected]d202a7c2012-01-04 07:53:472367 NavigationControllerImpl* source =
2368 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252369
avi2b177592014-12-10 02:08:022370 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012371 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252372
[email protected]474f8512013-05-31 22:31:162373 // We now have one entry, possibly with a new pending entry. Ensure that
2374 // adding the entries from source won't put us over the limit.
2375 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572376 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572377 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252378
Carlos IL4dea8902020-05-26 15:14:292379 // Insert the entries from source. Ignore any pending entry, since it has not
2380 // committed in source.
[email protected]474f8512013-05-31 22:31:162381 int max_source_index = source->last_committed_entry_index_;
Rakina Zata Amniafd3c6582021-11-30 06:19:172382 DCHECK_NE(max_source_index, -1);
2383 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572384
2385 // Ignore the source's current entry if merging with replacement.
2386 // TODO(davidben): This should preserve entries forward of the current
2387 // too. http://crbug.com/317872
2388 if (replace_entry && max_source_index > 0)
2389 max_source_index--;
2390
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572391 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252392
2393 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552394 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142395
Hayato Ito2c8c08d02021-06-23 03:38:432396 BroadcastHistoryOffsetAndLength();
[email protected]e1cd5452010-08-26 18:03:252397}
2398
[email protected]79368982013-11-13 01:11:012399bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162400 // If there is no last committed entry, we cannot prune. Even if there is a
2401 // pending entry, it may not commit, leaving this WebContents blank, despite
2402 // possibly giving it new entries via CopyStateFromAndPrune.
2403 if (last_committed_entry_index_ == -1)
2404 return false;
[email protected]9350602e2013-02-26 23:27:442405
[email protected]474f8512013-05-31 22:31:162406 // We cannot prune if there is a pending entry at an existing entry index.
2407 // It may not commit, so we have to keep the last committed entry, and thus
2408 // there is no sensible place to keep the pending entry. It is ok to have
2409 // a new pending entry, which can optionally commit as a new navigation.
2410 if (pending_entry_index_ != -1)
2411 return false;
2412
[email protected]474f8512013-05-31 22:31:162413 return true;
2414}
2415
[email protected]79368982013-11-13 01:11:012416void NavigationControllerImpl::PruneAllButLastCommitted() {
2417 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162418
avi2b177592014-12-10 02:08:022419 DCHECK_EQ(0, last_committed_entry_index_);
2420 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442421
Hayato Ito2c8c08d02021-06-23 03:38:432422 BroadcastHistoryOffsetAndLength();
[email protected]9350602e2013-02-26 23:27:442423}
2424
[email protected]79368982013-11-13 01:11:012425void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162426 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012427 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262428
[email protected]474f8512013-05-31 22:31:162429 // Erase all entries but the last committed entry. There may still be a
2430 // new pending entry after this.
2431 entries_.erase(entries_.begin(),
2432 entries_.begin() + last_committed_entry_index_);
2433 entries_.erase(entries_.begin() + 1, entries_.end());
2434 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262435}
2436
Christian Dullweber1af31e62018-02-22 11:49:482437void NavigationControllerImpl::DeleteNavigationEntries(
2438 const DeletionPredicate& deletionPredicate) {
2439 // It is up to callers to check the invariants before calling this.
2440 CHECK(CanPruneAllButLastCommitted());
2441 std::vector<int> delete_indices;
2442 for (size_t i = 0; i < entries_.size(); i++) {
2443 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572444 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482445 delete_indices.push_back(i);
2446 }
2447 }
2448 if (delete_indices.empty())
2449 return;
2450
2451 if (delete_indices.size() == GetEntryCount() - 1U) {
2452 PruneAllButLastCommitted();
2453 } else {
2454 // Do the deletion in reverse to preserve indices.
Ayu Ishii2f825852022-03-08 19:47:382455 for (const auto& index : base::Reversed(delete_indices)) {
2456 RemoveEntryAtIndex(index);
Christian Dullweber1af31e62018-02-22 11:49:482457 }
Hayato Ito2c8c08d02021-06-23 03:38:432458 BroadcastHistoryOffsetAndLength();
Christian Dullweber1af31e62018-02-22 11:49:482459 }
2460 delegate()->NotifyNavigationEntriesDeleted();
2461}
2462
Shivani Sharma883f5f32019-02-12 18:20:012463bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2464 auto* entry = GetEntryAtIndex(index);
2465 return entry && entry->should_skip_on_back_forward_ui();
2466}
2467
Carlos Caballero35ce710c2019-09-19 10:59:452468BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2469 return back_forward_cache_;
2470}
2471
clamy987a3752018-05-03 17:36:262472void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2473 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2474 // progress, since this will cause a use-after-free. (We only allow this
2475 // when the tab is being destroyed for shutdown, since it won't return to
2476 // NavigateToEntry in that case.) http://crbug.com/347742.
Takashi Toyoshimaea534ef22021-07-21 03:27:592477 CHECK(!in_navigate_to_pending_entry_ || frame_tree_.IsBeingDestroyed());
clamy987a3752018-05-03 17:36:262478
2479 if (was_failure && pending_entry_) {
2480 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2481 } else {
2482 failed_pending_entry_id_ = 0;
2483 }
2484
2485 if (pending_entry_) {
2486 if (pending_entry_index_ == -1)
Paul Semel7e51469e2022-07-12 12:16:332487 pending_entry_.ClearAndDelete();
clamy987a3752018-05-03 17:36:262488 pending_entry_index_ = -1;
2489 pending_entry_ = nullptr;
2490 }
arthursonzogni66f711c2019-10-08 14:40:362491
2492 // Ensure any refs to the current pending entry are ignored if they get
2493 // deleted, by clearing the set of known refs. All future pending entries will
2494 // only be affected by new refs.
2495 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262496}
2497
2498void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2499 if (pending_entry_)
2500 pending_entry_->set_ssl_error(error);
2501}
2502
Xiaohan Wang7f8052e02022-01-14 18:44:282503#if BUILDFLAG(IS_ANDROID)
Camille Lamy5193caa2018-10-12 11:59:422504// static
2505bool NavigationControllerImpl::ValidateDataURLAsString(
2506 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2507 if (!data_url_as_string)
2508 return false;
2509
2510 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2511 return false;
2512
2513 // The number of characters that is enough for validating a data: URI.
2514 // From the GURL's POV, the only important part here is scheme, it doesn't
2515 // check the actual content. Thus we can take only the prefix of the url, to
2516 // avoid unneeded copying of a potentially long string.
2517 const size_t kDataUriPrefixMaxLen = 64;
2518 GURL data_url(
2519 std::string(data_url_as_string->front_as<char>(),
2520 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2521 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2522 return false;
2523
2524 return true;
2525}
2526#endif
2527
Shivani Sharma194877032019-03-07 17:52:472528void NavigationControllerImpl::NotifyUserActivation() {
2529 // When a user activation occurs, ensure that all adjacent entries for the
2530 // same document clear their skippable bit, so that the history manipulation
2531 // intervention does not apply to them.
2532 auto* last_committed_entry = GetLastCommittedEntry();
2533 if (!last_committed_entry)
2534 return;
Shivani Sharma194877032019-03-07 17:52:472535
shivanigithub99368382021-06-16 18:33:372536 if (base::FeatureList::IsEnabled(
2537 features::kDebugHistoryInterventionNoUserActivation)) {
2538 return;
2539 }
2540
Shivani Sharmac4cc8922019-04-18 03:11:172541 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472542}
2543
clamy987a3752018-05-03 17:36:262544bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2545 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172546 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262547 NavigationEntryImpl* entry =
2548 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2549 if (!entry)
2550 return false;
2551
2552 FrameNavigationEntry* frame_entry =
2553 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2554 if (!frame_entry)
2555 return false;
2556
Nate Chapin45f620582021-09-30 17:45:432557 // |is_browser_initiated| is false here because a navigation in a new subframe
2558 // always begins with renderer action (i.e., an HTML element being inserted
2559 // into the DOM), so it is always renderer-initiated.
Camille Lamy5193caa2018-10-12 11:59:422560 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572561 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232562 ReloadType::NONE, false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432563 true /* is_history_navigation_in_new_child */,
2564 false /* is_browser_initiated */);
clamyea99ea12018-05-28 13:54:232565
2566 if (!request)
2567 return false;
2568
arthursonzognif046d4a2019-12-12 19:08:102569 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412570
Lukasz Anforowicz9ee83c272020-12-01 20:14:052571 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2572 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232573
2574 return true;
clamy987a3752018-05-03 17:36:262575}
2576
Tsuyoshi Horo52fd08e2020-07-07 07:03:452577bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2578 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2579 if (!entry)
2580 return false;
Rakina Zata Amnif297a802022-01-18 03:53:432581
2582 if (entry->IsInitialEntryNotForSynchronousAboutBlank()) {
2583 // We should never navigate to an existing initial NavigationEntry that is
2584 // the initial NavigationEntry for the initial empty document that hasn't
2585 // been overridden by the synchronous about:blank commit, to preserve
2586 // legacy behavior where trying to reload when the main frame is on the
2587 // initial empty document won't result in a navigation. See also
2588 // https://crbug.com/1277414.
2589 return false;
2590 }
Tsuyoshi Horo52fd08e2020-07-07 07:03:452591 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2592 if (!frame_entry)
2593 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142594 ReloadType reload_type = ReloadType::NORMAL;
2595 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452596 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142597 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452598 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432599 false /* is_history_navigation_in_new_child */,
2600 true /* is_browser_initiated */);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452601 if (!request)
2602 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052603 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452604 return true;
2605}
2606
Dave Tapuska8bfd84c2019-03-26 20:47:162607void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2608 int offset,
2609 int sandbox_frame_tree_node_id) {
2610 if (!CanGoToOffset(offset))
2611 return;
Nate Chapin45f620582021-09-30 17:45:432612 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id,
2613 false /* is_browser_initiated */);
Dave Tapuska8bfd84c2019-03-26 20:47:162614}
2615
clamy987a3752018-05-03 17:36:262616void NavigationControllerImpl::NavigateFromFrameProxy(
2617 RenderFrameHostImpl* render_frame_host,
2618 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022619 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452620 int initiator_process_id,
Anton Bikineevf62d1bf2021-05-15 17:56:072621 const absl::optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262622 bool is_renderer_initiated,
2623 SiteInstance* source_site_instance,
2624 const Referrer& referrer,
2625 ui::PageTransition page_transition,
2626 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252627 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262628 const std::string& method,
2629 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092630 const std::string& extra_headers,
Antonio Sartori2f763d9d2021-04-21 10:04:142631 network::mojom::SourceLocationPtr source_location,
John Delaney50425f82020-04-07 16:26:212632 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
jongdeok.kim5de823b32022-06-14 04:37:502633 bool is_form_submission,
Tsuyoshi Horo167ca6432022-03-09 05:16:392634 const absl::optional<blink::Impression>& impression,
Nan Lin944e9b4e2022-04-12 13:51:222635 base::TimeTicks navigation_start_time,
2636 absl::optional<bool> is_fenced_frame_opaque_url) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582637 if (is_renderer_initiated)
2638 DCHECK(initiator_origin.has_value());
2639
clamy987a3752018-05-03 17:36:262640 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582641
Rakina Zata Amni2322f4f82022-01-24 13:24:242642 // Don't allow an entry replacement if there is no entry to replace.
2643 // http://crbug.com/457149
2644 if (GetEntryCount() == 0)
2645 should_replace_current_entry = false;
2646
clamy987a3752018-05-03 17:36:262647 // Create a NavigationEntry for the transfer, without making it the pending
2648 // entry. Subframe transfers should have a clone of the last committed entry
2649 // with a FrameNavigationEntry for the target frame. Main frame transfers
2650 // should have a new NavigationEntry.
2651 // TODO(creis): Make this unnecessary by creating (and validating) the params
2652 // directly, passing them to the destination RenderFrameHost. See
2653 // https://crbug.com/536906.
2654 std::unique_ptr<NavigationEntryImpl> entry;
Harkiran Bolariae1b5158b2021-09-16 19:03:262655 if (!render_frame_host->is_main_frame()) {
clamy987a3752018-05-03 17:36:262656 // Subframe case: create FrameNavigationEntry.
Rakina Zata Amni2322f4f82022-01-24 13:24:242657 if (GetLastCommittedEntry()) {
2658 entry = GetLastCommittedEntry()->Clone();
2659 entry->set_extra_headers(extra_headers);
2660 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2661 // Renderer-initiated navigation that target a remote frame are currently
2662 // classified as browser-initiated when this one has already navigated.
2663 // See https://crbug.com/722251.
2664 } else {
2665 // If there's no last committed entry, create an entry for about:blank
2666 // with a subframe entry for our destination.
2667 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2668 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
2669 GURL(url::kAboutBlankURL), referrer, initiator_origin,
2670 source_site_instance, page_transition, is_renderer_initiated,
2671 extra_headers, browser_context_,
Julie Jeongeun Kim5b9aff72022-05-02 02:10:172672 nullptr /* blob_url_loader_factory */,
2673 false /* rewrite_virtual_urls */));
Rakina Zata Amni2322f4f82022-01-24 13:24:242674 }
Rakina Zata Amniafd3c6582021-11-30 06:19:172675 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2676 // Renderer-initiated navigation that target a remote frame are currently
2677 // classified as browser-initiated when this one has already navigated.
2678 // See https://crbug.com/722251.
Nate Chapin9f169072021-06-09 19:32:372679 // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter
2680 // to CreateNavigationRequestFromLoadParams(), so while kReplace might
2681 // remove child FrameNavigationEntries (e.g., if this is a cross-process
2682 // same-document navigation), they will still be present in the
2683 // committed NavigationEntry that will be referenced to construct the new
2684 // FrameNavigationEntry tree when this navigation commits.
clamy987a3752018-05-03 17:36:262685 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:082686 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582687 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072688 absl::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202689 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392690 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482691 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori79d549d2021-02-18 12:59:542692 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262693 } else {
2694 // Main frame case.
Julie Jeongeun Kim5b9aff72022-05-02 02:10:172695 // If `node` is the outermost main frame, it rewrites a virtual url in order
2696 // to adjust the original input url if needed. For inner frames such as
2697 // fenced frames or subframes, they don't rewrite urls as the urls are not
2698 // input urls by users.
2699 bool rewrite_virtual_urls = node->IsOutermostMainFrame();
clamy987a3752018-05-03 17:36:262700 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102701 url, referrer, initiator_origin, source_site_instance, page_transition,
2702 is_renderer_initiated, extra_headers, browser_context_,
Julie Jeongeun Kim5b9aff72022-05-02 02:10:172703 blob_url_loader_factory, rewrite_virtual_urls));
clamy987a3752018-05-03 17:36:262704 entry->root_node()->frame_entry->set_source_site_instance(
2705 static_cast<SiteInstanceImpl*>(source_site_instance));
2706 entry->root_node()->frame_entry->set_method(method);
2707 }
clamy987a3752018-05-03 17:36:262708
Camille Lamy5193caa2018-10-12 11:59:422709 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262710 if (GetLastCommittedEntry() &&
2711 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2712 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422713 override_user_agent = true;
clamy987a3752018-05-03 17:36:262714 }
2715 // TODO(creis): Set user gesture and intent received timestamp on Android.
2716
2717 // We may not have successfully added the FrameNavigationEntry to |entry|
2718 // above (per https://crbug.com/608402), in which case we create it from
2719 // scratch. This works because we do not depend on |frame_entry| being inside
2720 // |entry| during NavigateToEntry. This will go away when we shortcut this
2721 // further in https://crbug.com/536906.
2722 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2723 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452724 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Nate Chapinfbfe5af2021-06-10 17:22:082725 node->unique_name(), -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582726 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072727 absl::nullopt /* origin */, referrer, initiator_origin,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:122728 std::vector<GURL>(), blink::PageState(), method, -1,
2729 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482730 nullptr /* subresource_web_bundle_navigation_info */,
Nate Chapin63db0d12022-01-20 22:03:302731 nullptr /* policy_container_policies */,
Domenic Denicolacc094fb2022-03-16 23:40:572732 false /* protect_url_in_navigation_api */);
clamy987a3752018-05-03 17:36:262733 }
2734
Camille Lamy5193caa2018-10-12 11:59:422735 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022736 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452737 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292738 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422739 params.source_site_instance = source_site_instance;
2740 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2741 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032742 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422743 params.referrer = referrer;
2744 /* params.redirect_chain: skip */
2745 params.extra_headers = extra_headers;
2746 params.is_renderer_initiated = is_renderer_initiated;
2747 params.override_user_agent = UA_OVERRIDE_INHERIT;
2748 /* params.base_url_for_data_url: skip */
2749 /* params.virtual_url_for_data_url: skip */
2750 /* params.data_url_as_string: skip */
2751 params.post_data = post_body;
2752 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582753 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422754 /* params.frame_name: skip */
2755 // TODO(clamy): See if user gesture should be propagated to this function.
2756 params.has_user_gesture = false;
2757 params.should_clear_history_list = false;
2758 params.started_from_context_menu = false;
2759 /* params.navigation_ui_data: skip */
2760 /* params.input_start: skip */
Minggang Wangf59db47b2021-06-16 01:56:222761 params.was_activated = blink::mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542762 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212763 params.impression = impression;
Antonio Sartori6984c742021-08-26 08:03:412764 params.download_policy = std::move(download_policy);
jongdeok.kim5de823b32022-06-14 04:37:502765 params.is_form_submission = is_form_submission;
Camille Lamy5193caa2018-10-12 11:59:422766
2767 std::unique_ptr<NavigationRequest> request =
2768 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032769 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:142770 false /* has_user_gesture */, std::move(source_location),
Tsuyoshi Horo167ca6432022-03-09 05:16:392771 ReloadType::NONE, entry.get(), frame_entry.get(),
Nan Lin944e9b4e2022-04-12 13:51:222772 navigation_start_time, is_fenced_frame_opaque_url);
clamyea99ea12018-05-28 13:54:232773
2774 if (!request)
2775 return;
2776
Arthur Hemery948742762019-09-18 10:06:242777 // At this stage we are proceeding with this navigation. If this was renderer
2778 // initiated with user gesture, we need to make sure we clear up potential
2779 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2780 DiscardNonCommittedEntries();
2781
Lukasz Anforowicz9ee83c272020-12-01 20:14:052782 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262783}
2784
[email protected]d1198fd2012-08-13 22:50:192785void NavigationControllerImpl::SetSessionStorageNamespace(
Alex Moshchuk8015afcf2022-01-31 22:59:252786 const StoragePartitionConfig& partition_config,
[email protected]8ff00d72012-10-23 19:12:212787 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192788 if (!session_storage_namespace)
2789 return;
2790
2791 // We can't overwrite an existing SessionStorage without violating spec.
2792 // Attempts to do so may give a tab access to another tab's session storage
2793 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152794 bool successful_insert =
2795 session_storage_namespace_map_
Alex Moshchuk8015afcf2022-01-31 22:59:252796 .insert(std::make_pair(partition_config,
Aaron Colwellf3b316e2021-03-11 20:17:052797 static_cast<SessionStorageNamespaceImpl*>(
2798 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302799 .second;
2800 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
[email protected]d1198fd2012-08-13 22:50:192801}
2802
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572803bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Rakina Zata Amni2322f4f82022-01-24 13:24:242804 return IsInitialNavigation() &&
2805 (!GetLastCommittedEntry() ||
2806 GetLastCommittedEntry()->IsInitialEntry()) &&
Carlos Caballeroede6f8c2021-01-28 11:01:502807 !frame_tree_.has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462808}
2809
Aran Gilman37d11632019-10-08 23:07:152810SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Alex Moshchuk8015afcf2022-01-31 22:59:252811 const StoragePartitionConfig& partition_config) {
[email protected]fdac6ade2013-07-20 01:06:302812 StoragePartition* partition =
Lukasz Anforowiczb9a969a2021-04-29 15:26:252813 browser_context_->GetStoragePartition(partition_config);
michaelnbacbcbd2016-02-09 00:32:032814 DOMStorageContextWrapper* context_wrapper =
2815 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2816
2817 SessionStorageNamespaceMap::const_iterator it =
Alex Moshchuk8015afcf2022-01-31 22:59:252818 session_storage_namespace_map_.find(partition_config);
michaelnbacbcbd2016-02-09 00:32:032819 if (it != session_storage_namespace_map_.end()) {
2820 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152821 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2822 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:472823
michaelnbacbcbd2016-02-09 00:32:032824 return it->second.get();
2825 }
2826
2827 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102828 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2829 SessionStorageNamespaceImpl::Create(context_wrapper);
2830 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2831 session_storage_namespace.get();
Alex Moshchuk8015afcf2022-01-31 22:59:252832 session_storage_namespace_map_[partition_config] =
Daniel Murphy31bbb8b12018-02-07 21:44:102833 std::move(session_storage_namespace);
[email protected]fdac6ade2013-07-20 01:06:302834
Daniel Murphy31bbb8b12018-02-07 21:44:102835 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302836}
2837
2838SessionStorageNamespace*
2839NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Alex Moshchuk8015afcf2022-01-31 22:59:252840 return GetSessionStorageNamespace(
2841 StoragePartitionConfig::CreateDefault(GetBrowserContext()));
[email protected]fdac6ade2013-07-20 01:06:302842}
2843
2844const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572845NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302846 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552847}
[email protected]d202a7c2012-01-04 07:53:472848
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572849bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562850 return needs_reload_;
2851}
[email protected]a26023822011-12-29 00:23:552852
[email protected]46bb5e9c2013-10-03 22:16:472853void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412854 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2855}
2856
2857void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472858 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412859 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542860
2861 if (last_committed_entry_index_ != -1) {
2862 entries_[last_committed_entry_index_]->SetTransitionType(
2863 ui::PAGE_TRANSITION_RELOAD);
2864 }
[email protected]46bb5e9c2013-10-03 22:16:472865}
2866
[email protected]d202a7c2012-01-04 07:53:472867void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572868 DCHECK_LT(index, GetEntryCount());
2869 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312870 DiscardNonCommittedEntries();
2871
2872 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122873 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312874 last_committed_entry_index_--;
2875}
2876
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572877NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272878 // If there is no pending_entry_, there should be no pending_entry_index_.
2879 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2880
2881 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362882 // at that index. An exception is while a reload of a post commit error page
2883 // is ongoing; in that case pending entry will point to the entry replaced
2884 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272885 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362886 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2887 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272888
[email protected]022af742011-12-28 18:37:252889 return pending_entry_;
2890}
2891
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572892int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272893 // The pending entry index must always be less than the number of entries.
Rakina Zata Amni2322f4f82022-01-24 13:24:242894 // If there are no entries, it must be exactly -1.
arthursonzogni5c4c202d2017-04-25 23:41:272895 DCHECK_LT(pending_entry_index_, GetEntryCount());
Rakina Zata Amni2322f4f82022-01-24 13:24:242896 DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:552897 return pending_entry_index_;
2898}
2899
avi25764702015-06-23 15:43:372900void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572901 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362902 bool replace,
Dave Tapuska87696ae2021-11-18 18:48:312903 bool was_post_commit_error,
Rakina Zata Amnia4e27222021-12-22 01:05:002904 bool in_fenced_frame_tree,
2905 LoadCommittedDetails* commit_details) {
Dave Tapuska87696ae2021-11-18 18:48:312906 // Fenced frame trees should always have `ui::PAGE_TRANSITION_AUTO_SUBFRAME`
2907 // set because:
2908 // 1) They don't influence the history of the outer page.
2909 // 2) They are always replace only navigation (there is always only one entry
2910 // in their history stack).
2911 // 3) Are not top level navigations and appear similar to iframes.
2912 // Navigations of the fenced frame might create a new NavigationEntry, which
2913 // will call this function. Non fenced frame navigations will never have
2914 // `ui::PAGE_TRANSITION_AUTO_SUBFRAME` because they won't call
2915 // InsertOrReplaceEntry.
2916 DCHECK_EQ(in_fenced_frame_tree,
2917 ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2918 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202919
avi5cad4912015-06-19 05:25:442920 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2921 // need to keep continuity with the pending entry, so copy the pending entry's
2922 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2923 // then the renderer navigated on its own, independent of the pending entry,
2924 // so don't copy anything.
2925 if (pending_entry_ && pending_entry_index_ == -1)
2926 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202927
Rakina Zata Amnia4e27222021-12-22 01:05:002928 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]765b35502008-08-21 00:51:202929
creisee17e932015-07-17 17:56:222930 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362931 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102932 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572933 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362934 // If the new entry is a post-commit error page, we store the current last
2935 // committed entry to the side so that we can put it back when navigating
2936 // away from the error.
2937 if (was_post_commit_error) {
2938 DCHECK(!entry_replaced_by_post_commit_error_);
2939 entry_replaced_by_post_commit_error_ =
2940 std::move(entries_[last_committed_entry_index_]);
2941 }
dcheng36b6aec92015-12-26 06:16:362942 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222943 return;
2944 }
[email protected]765b35502008-08-21 00:51:202945
creis37979a62015-08-04 19:48:182946 // We shouldn't see replace == true when there's no committed entries.
2947 DCHECK(!replace);
2948
Michael Thiessen9b14d512019-09-23 21:19:472949 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202950
Shivani Sharmad8c8d652019-02-13 17:27:572951 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202952
dcheng36b6aec92015-12-26 06:16:362953 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202954 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292955}
2956
Shivani Sharmad8c8d652019-02-13 17:27:572957void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162958 if (entries_.size() < max_entry_count())
2959 return;
2960
2961 DCHECK_EQ(max_entry_count(), entries_.size());
2962 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572963 CHECK_EQ(pending_entry_index_, -1);
2964
2965 int index = 0;
Elly Fong-Jonesccc6d1f2021-06-14 18:32:422966 // Retrieve the oldest skippable entry.
2967 for (; index < GetEntryCount(); index++) {
2968 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2969 break;
Shivani Sharmad8c8d652019-02-13 17:27:572970 }
2971
2972 // If there is no skippable entry or if it is the last committed entry then
2973 // fall back to pruning the oldest entry. It is not safe to prune the last
2974 // committed entry.
2975 if (index == GetEntryCount() || index == last_committed_entry_index_)
2976 index = 0;
2977
2978 bool should_succeed = RemoveEntryAtIndex(index);
2979 DCHECK_EQ(true, should_succeed);
2980
2981 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252982}
2983
clamy3cb9bea92018-07-10 12:42:022984void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162985 ReloadType reload_type,
Nate Chapin45f620582021-09-30 17:45:432986 int sandboxed_source_frame_tree_node_id,
2987 bool is_browser_initiated) {
Alexander Timin3a92df72019-09-20 11:59:502988 TRACE_EVENT0("navigation",
2989 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272990 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022991 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362992 if (pending_entry_index_ != -1) {
2993 // The pending entry may not be in entries_ if a post-commit error page is
2994 // showing.
2995 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2996 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2997 }
Gyuyoung Kim107c2a02021-04-13 01:49:302998 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572999 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:013000 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:003001 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:573002 int nav_entry_id = pending_entry_->GetUniqueID();
3003
[email protected]83c2e232011-10-07 21:36:463004 // If we were navigating to a slow-to-commit page, and the user performs
3005 // a session history navigation to the last committed page, RenderViewHost
3006 // will force the throbber to start, but WebKit will essentially ignore the
3007 // navigation, and won't send a message to stop the throbber. To prevent this
3008 // from happening, we drop the navigation here and stop the slow-to-commit
3009 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:023010 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:083011 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:273012 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
Takashi Toyoshimabf549472021-07-01 07:47:203013 frame_tree_.StopLoading();
[email protected]6a13a6c2011-12-20 21:47:123014
[email protected]83c2e232011-10-07 21:36:463015 DiscardNonCommittedEntries();
3016 return;
3017 }
3018
creisce0ef3572017-01-26 17:53:083019 // Compare FrameNavigationEntries to see which frames in the tree need to be
3020 // navigated.
clamy3cb9bea92018-07-10 12:42:023021 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
3022 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Nate Chapin45f620582021-09-30 17:45:433023 FindFramesToNavigate(root, reload_type, is_browser_initiated,
3024 &same_document_loads, &different_document_loads);
creis4e2ecb72015-06-20 00:46:303025
3026 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573027 // We were unable to match any frames to navigate. This can happen if a
3028 // history navigation targets a subframe that no longer exists
3029 // (https://crbug.com/705550). In this case, we need to update the current
3030 // history entry to the pending one but keep the main document loaded. We
3031 // also need to ensure that observers are informed about the updated
3032 // current history entry (e.g., for greying out back/forward buttons), and
3033 // that renderer processes update their history offsets. The easiest way
3034 // to do all that is to schedule a "redundant" same-document navigation in
3035 // the main frame.
3036 //
3037 // Note that we don't want to remove this history entry, as it might still
3038 // be valid later, since a frame that it's targeting may be recreated.
3039 //
3040 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
3041 // need to repeat history navigations until finding the one that works.
3042 // Consider changing this behavior to keep looking for the first valid
3043 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:023044 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:423045 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573046 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:573047 ReloadType::NONE /* reload_type */,
3048 true /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433049 false /* is_history_navigation_in_new_child */,
3050 is_browser_initiated);
clamy3cb9bea92018-07-10 12:42:023051 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573052 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:023053 DiscardPendingEntry(false);
3054 return;
3055 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573056 same_document_loads.push_back(std::move(navigation_request));
3057
3058 // Sanity check that we never take this branch for any kinds of reloads,
3059 // as those should've queued a different-document load in the main frame.
3060 DCHECK(!is_forced_reload);
3061 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:303062 }
3063
Dave Tapuska8bfd84c2019-03-26 20:47:163064 // If |sandboxed_source_frame_node_id| is valid, then track whether this
3065 // navigation affects any frame outside the frame's subtree.
3066 if (sandboxed_source_frame_tree_node_id !=
3067 FrameTreeNode::kFrameTreeNodeInvalidId) {
3068 bool navigates_inside_tree =
3069 DoesSandboxNavigationStayWithinSubtree(
3070 sandboxed_source_frame_tree_node_id, same_document_loads) &&
3071 DoesSandboxNavigationStayWithinSubtree(
3072 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:503073 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:163074 // the number of pages that trigger this.
3075 FrameTreeNode* sandbox_source_frame_tree_node =
3076 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
3077 if (sandbox_source_frame_tree_node) {
3078 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
3079 sandbox_source_frame_tree_node->current_frame_host(),
3080 navigates_inside_tree
3081 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
3082 : blink::mojom::WebFeature::
3083 kSandboxBackForwardAffectsFramesOutsideSubtree);
3084 }
Dave Tapuska855c1e12019-08-23 20:45:523085
3086 // If the navigation occurred outside the tree discard it because
3087 // the sandboxed frame didn't have permission to navigate outside
3088 // its tree. If it is possible that the navigation is both inside and
3089 // outside the frame tree and we discard it entirely because we don't
3090 // want to end up in a history state that didn't exist before.
Dominic Farolino057440042022-01-19 18:18:143091 if (!navigates_inside_tree) {
Dave Tapuska855c1e12019-08-23 20:45:523092 DiscardPendingEntry(false);
3093 return;
3094 }
Dave Tapuska8bfd84c2019-03-26 20:47:163095 }
3096
Carlos Caballero539a421c2020-07-06 10:25:573097 // BackForwardCache:
3098 // Navigate immediately if the document is in the BackForwardCache.
3099 if (back_forward_cache_.GetEntry(nav_entry_id)) {
3100 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
3101 DCHECK_EQ(reload_type, ReloadType::NONE);
3102 auto navigation_request = CreateNavigationRequestFromEntry(
3103 root, pending_entry_, pending_entry_->GetFrameEntry(root),
3104 ReloadType::NONE, false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433105 false /* is_history_navigation_in_new_child */, is_browser_initiated);
Lukasz Anforowicz9ee83c272020-12-01 20:14:053106 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:573107
3108 return;
3109 }
3110
3111 // History navigation might try to reuse a specific BrowsingInstance, already
3112 // used by a page in the cache. To avoid having two different main frames that
3113 // live in the same BrowsingInstance, evict the all pages with this
3114 // BrowsingInstance from the cache.
3115 //
3116 // For example, take the following scenario:
3117 //
3118 // A1 = Some page on a.com
3119 // A2 = Some other page on a.com
3120 // B3 = An uncacheable page on b.com
3121 //
3122 // Then the following navigations occur:
3123 // A1->A2->B3->A1
3124 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
3125 // take its place) and A1 will be created in the same BrowsingInstance (and
3126 // SiteInstance), as A2.
3127 //
3128 // If we didn't do anything, both A1 and A2 would remain alive in the same
3129 // BrowsingInstance/SiteInstance, which is unsupported by
3130 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
3131 // A2 from the cache.
3132 if (pending_entry_->site_instance()) {
3133 back_forward_cache_.EvictFramesInRelatedSiteInstances(
3134 pending_entry_->site_instance());
3135 }
3136
Rakina Zata Amnid605d462022-06-01 10:17:033137 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "pending_entry_restored",
3138 pending_entry_ && pending_entry_->IsRestored());
3139 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_entry_id",
3140 pending_entry_ ? pending_entry_->GetUniqueID() : -1);
3141 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_entry_index",
3142 pending_entry_index_);
3143 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "last_committed_index",
3144 last_committed_entry_index_);
3145 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "entries_size", entries_.size());
3146 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "pending_entry_initial",
3147 pending_entry_ && pending_entry_->IsInitialEntry());
3148 SCOPED_CRASH_KEY_BOOL(
3149 "nav_reentrancy", "pending_entry_initial2",
3150 pending_entry_ &&
3151 pending_entry_->IsInitialEntryNotForSynchronousAboutBlank());
3152 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_initial_nav",
3153 IsInitialNavigation());
3154 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_initial_blank_nav",
3155 IsInitialBlankNavigation());
3156 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_forced_reload", is_forced_reload);
3157 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_reload_type",
3158 (int)pending_reload_);
3159
clamy3cb9bea92018-07-10 12:42:023160 // This call does not support re-entrancy. See http://crbug.com/347742.
3161 CHECK(!in_navigate_to_pending_entry_);
3162 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:303163
Rakina Zata Amnid605d462022-06-01 10:17:033164 // If the navigation-reentrancy is caused by calling
3165 // NavigateToExistingPendingEntry twice, this will note the previous call's
3166 // pending entry's ID.
3167 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "prev_pending_entry_id",
3168 pending_entry_ ? pending_entry_->GetUniqueID() : -1);
3169
arthursonzogni66f711c2019-10-08 14:40:363170 // It is not possible to delete the pending NavigationEntry while navigating
3171 // to it. Grab a reference to delay potential deletion until the end of this
3172 // function.
3173 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3174
creis4e2ecb72015-06-20 00:46:303175 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:023176 for (auto& item : same_document_loads) {
3177 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:053178 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:303179 }
clamy3cb9bea92018-07-10 12:42:023180 for (auto& item : different_document_loads) {
3181 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:053182 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:303183 }
clamy3cb9bea92018-07-10 12:42:023184
3185 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:303186}
3187
Alex Moshchuk3a4e77a2020-05-29 21:32:573188NavigationControllerImpl::HistoryNavigationAction
3189NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:303190 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:573191 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:423192 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:583193 // Only active and prerendered documents are allowed to navigate in their
3194 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:423195 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:013196 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:583197 // - If the document is in pending deletion, the browser already committed
3198 // to destroying this RenderFrameHost. See https://crbug.com/930278.
3199 // - If the document is in back-forward cache, it's not allowed to navigate
3200 // as it should remain frozen. Ignore the request and evict the document
3201 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:423202 //
Sreeja Kamishettydb8e2892021-03-10 09:30:583203 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:423204 // which should all be inactive as well.
Fergal Daly1336ac642021-09-14 15:13:113205 if (frame->current_frame_host()->IsInactiveAndDisallowActivation(
3206 DisallowActivationReasonId::kDetermineActionForHistoryNavigation)) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:423207 return HistoryNavigationAction::kStopLooking;
Fergal Daly1336ac642021-09-14 15:13:113208 }
Sreeja Kamishetty8eacabb2021-03-09 11:45:423209 }
arthursonzogni03f76152019-02-12 10:35:203210
Alex Moshchuk3a4e77a2020-05-29 21:32:573211 // If there's no last committed entry, there is no previous history entry to
3212 // compare against, so fall back to a different-document load. Note that we
3213 // should only reach this case for the root frame and not descend further
3214 // into subframes.
3215 if (!GetLastCommittedEntry()) {
3216 DCHECK(frame->IsMainFrame());
3217 return HistoryNavigationAction::kDifferentDocument;
3218 }
3219
3220 // Reloads should result in a different-document load. Note that reloads may
3221 // also happen via the |needs_reload_| mechanism where the reload_type is
3222 // NONE, so detect this by comparing whether we're going to the same
3223 // entry that we're currently on. Similarly to above, only main frames
3224 // should reach this. Note that subframes support reloads, but that's done
3225 // via a different path that doesn't involve FindFramesToNavigate (see
3226 // RenderFrameHost::Reload()).
3227 if (reload_type != ReloadType::NONE ||
3228 pending_entry_index_ == last_committed_entry_index_) {
3229 DCHECK(frame->IsMainFrame());
3230 return HistoryNavigationAction::kDifferentDocument;
3231 }
3232
Alex Moshchuk47d1a4bd2020-06-01 22:15:343233 // If there is no new FrameNavigationEntry for the frame, ignore the
3234 // load. For example, this may happen when going back to an entry before a
3235 // frame was created. Suppose we commit a same-document navigation that also
3236 // results in adding a new subframe somewhere in the tree. If we go back,
3237 // the new subframe will be missing a FrameNavigationEntry in the previous
3238 // NavigationEntry, but we shouldn't delete or change what's loaded in
3239 // it.
3240 //
Alex Moshchuke65c39272020-06-03 17:55:373241 // Note that in this case, there is no need to keep looking for navigations
3242 // in subframes, which would be missing FrameNavigationEntries as well.
3243 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:343244 // It's important to check this before checking |old_item| below, since both
3245 // might be null, and in that case we still shouldn't change what's loaded in
3246 // this frame. Note that scheduling any loads assumes that |new_item| is
3247 // non-null. See https://crbug.com/1088354.
3248 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3249 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:373250 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:343251
Charlie Reisa474fb62022-03-17 02:31:363252 // Use the RenderFrameHost's last committed FrameNavigationEntry to identify
3253 // which history item it is currently on, since this may be different than the
3254 // FrameNavigationEntry for the frame in the last committed NavigationEntry
3255 // (e.g., if a history navigation is targeting multiple frames and only some
3256 // have committed so far).
creis4e2ecb72015-06-20 00:46:303257 FrameNavigationEntry* old_item =
Charlie Reisa474fb62022-03-17 02:31:363258 frame->current_frame_host()->last_committed_frame_entry();
3259 if (!old_item) {
3260 // In cases where the RenderFrameHost does not have a FrameNavigationEntry,
3261 // fall back to the last committed NavigationEntry's record for this frame.
3262 // This may happen in cases like the initial state of the RenderFrameHost.
3263 // TODO(https://crbug.com/1304466): Ensure the RenderFrameHost always has an
3264 // accurate FrameNavigationEntry and eliminate this case.
3265 old_item = GetLastCommittedEntry()->GetFrameEntry(frame);
3266 }
3267 // If neither approach finds a FrameNavigationEntry, schedule a
3268 // different-document load.
3269 // TODO(https://crbug.com/608402): Remove this case.
Alex Moshchuk3a4e77a2020-05-29 21:32:573270 if (!old_item)
3271 return HistoryNavigationAction::kDifferentDocument;
3272
Alex Moshchuk3a4e77a2020-05-29 21:32:573273 // If the new item is not in the same SiteInstance, schedule a
3274 // different-document load. Newly restored items may not have a SiteInstance
3275 // yet, in which case it will be assigned on first commit.
3276 if (new_item->site_instance() &&
3277 new_item->site_instance() != old_item->site_instance())
3278 return HistoryNavigationAction::kDifferentDocument;
3279
3280 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:313281 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
3282 // that we do this even if the history navigation would not be modifying this
3283 // frame were it live.
3284 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:573285 return HistoryNavigationAction::kDifferentDocument;
3286
3287 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:313288 // Starting a navigation after a crash early-promotes the speculative
3289 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
3290 // committed yet, so we can not possibly perform a same-document history
3291 // navigation. The frame would need to be reloaded with a cross-document
3292 // navigation.
3293 if (!frame->current_frame_host()->has_committed_any_navigation())
3294 return HistoryNavigationAction::kDifferentDocument;
3295
creis54131692016-08-12 18:32:253296 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:493297 // sequence number but different item sequence number.
3298 if (new_item->document_sequence_number() ==
3299 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573300 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493301 }
avib48cb312016-05-05 21:35:003302
Alex Moshchuk3a4e77a2020-05-29 21:32:573303 // Otherwise, if both item and document sequence numbers differ, this
3304 // should be a different document load.
3305 return HistoryNavigationAction::kDifferentDocument;
3306 }
3307
3308 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373309 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573310 DCHECK_EQ(new_item->document_sequence_number(),
3311 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373312 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573313}
3314
3315void NavigationControllerImpl::FindFramesToNavigate(
3316 FrameTreeNode* frame,
3317 ReloadType reload_type,
Nate Chapin45f620582021-09-30 17:45:433318 bool is_browser_initiated,
Alex Moshchuk3a4e77a2020-05-29 21:32:573319 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3320 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3321 DCHECK(pending_entry_);
3322 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3323
3324 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3325
3326 if (action == HistoryNavigationAction::kSameDocument) {
3327 std::unique_ptr<NavigationRequest> navigation_request =
3328 CreateNavigationRequestFromEntry(
3329 frame, pending_entry_, new_item, reload_type,
3330 true /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433331 false /* is_history_navigation_in_new_child */,
3332 is_browser_initiated);
Alex Moshchuk3a4e77a2020-05-29 21:32:573333 if (navigation_request) {
3334 // Only add the request if was properly created. It's possible for the
3335 // creation to fail in certain cases, e.g. when the URL is invalid.
3336 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303337 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573338 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473339 std::unique_ptr<NavigationRequest> navigation_request =
3340 CreateNavigationRequestFromEntry(
3341 frame, pending_entry_, new_item, reload_type,
3342 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433343 false /* is_history_navigation_in_new_child */,
3344 is_browser_initiated);
Lei Zhang96031532019-10-10 19:05:473345 if (navigation_request) {
3346 // Only add the request if was properly created. It's possible for the
3347 // creation to fail in certain cases, e.g. when the URL is invalid.
3348 different_document_loads->push_back(std::move(navigation_request));
3349 }
3350 // For a different document, the subframes will be destroyed, so there's
3351 // no need to consider them.
3352 return;
Alex Moshchuke65c39272020-06-03 17:55:373353 } else if (action == HistoryNavigationAction::kStopLooking) {
3354 return;
creis4e2ecb72015-06-20 00:46:303355 }
3356
3357 for (size_t i = 0; i < frame->child_count(); i++) {
Nate Chapin45f620582021-09-30 17:45:433358 FindFramesToNavigate(frame->child_at(i), reload_type, is_browser_initiated,
3359 same_document_loads, different_document_loads);
creis4e2ecb72015-06-20 00:46:303360 }
3361}
3362
Harkiran Bolariaba823e42021-05-21 18:30:363363base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry(
clamy21718cc22018-06-13 13:34:243364 const LoadURLParams& params) {
3365 // Find the appropriate FrameTreeNode.
3366 FrameTreeNode* node = nullptr;
3367 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3368 !params.frame_name.empty()) {
3369 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003370 ? frame_tree_.FindByID(params.frame_tree_node_id)
3371 : frame_tree_.FindByName(params.frame_name);
Hayato Ito7a80db42021-07-05 06:18:543372 DCHECK(!node || node->frame_tree() == &frame_tree_);
clamy21718cc22018-06-13 13:34:243373 }
3374
3375 // If no FrameTreeNode was specified, navigate the main frame.
3376 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003377 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243378
Camille Lamy5193caa2018-10-12 11:59:423379 // Compute overrides to the LoadURLParams for |override_user_agent|,
3380 // |should_replace_current_entry| and |has_user_gesture| that will be used
3381 // both in the creation of the NavigationEntry and the NavigationRequest.
3382 // Ideally, the LoadURLParams themselves would be updated, but since they are
3383 // passed as a const reference, this is not possible.
3384 // TODO(clamy): When we only create a NavigationRequest, move this to
3385 // CreateNavigationRequestFromLoadURLParams.
3386 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3387 GetLastCommittedEntry());
3388
3389 // Don't allow an entry replacement if there is no entry to replace.
3390 // http://crbug.com/457149
Hayato Ito7a80db42021-07-05 06:18:543391 //
3392 // If there is an entry, an entry replacement must happen if the current
3393 // browsing context should maintain a trivial session history.
shivanigithubf405bf0d2021-11-05 17:58:333394 bool should_replace_current_entry =
3395 (params.should_replace_current_entry ||
3396 ShouldMaintainTrivialSessionHistory(node)) &&
3397 entries_.size();
Camille Lamy5193caa2018-10-12 11:59:423398
clamy21718cc22018-06-13 13:34:243399 // Javascript URLs should not create NavigationEntries. All other navigations
3400 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243401 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373402 std::unique_ptr<NavigationEntryImpl> entry =
3403 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3404 should_replace_current_entry,
3405 params.has_user_gesture);
clamy21718cc22018-06-13 13:34:243406 DiscardPendingEntry(false);
3407 SetPendingEntry(std::move(entry));
3408 }
3409
3410 // Renderer-debug URLs are sent to the renderer process immediately for
3411 // processing and don't need to create a NavigationRequest.
3412 // Note: this includes navigations to JavaScript URLs, which are considered
3413 // renderer-debug URLs.
3414 // Note: we intentionally leave the pending entry in place for renderer debug
3415 // URLs, unlike the cases below where we clear it if the navigation doesn't
3416 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303417 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483418 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3419 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153420 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483421 params.url, browser_context_)) {
3422 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363423 return nullptr;
Oleg Davydov2cc0167b2019-02-05 14:32:483424 }
3425
clamy21718cc22018-06-13 13:34:243426 HandleRendererDebugURL(node, params.url);
Harkiran Bolariaba823e42021-05-21 18:30:363427 return nullptr;
clamy21718cc22018-06-13 13:34:243428 }
3429
Antonio Sartori78a749f2020-11-30 12:03:393430 DCHECK(pending_entry_);
3431
clamy21718cc22018-06-13 13:34:243432 // Convert navigations to the current URL to a reload.
3433 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3434 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3435 // DevTools sometimes issues navigations to main frames that they do not
3436 // expect to see treated as reload, and it only works because they pass a
3437 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3438 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543439 ReloadType reload_type = params.reload_type;
3440 if (reload_type == ReloadType::NONE &&
3441 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043442 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243443 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243444 params.load_type ==
3445 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
Hayato Ito7a80db42021-07-05 06:18:543446 should_replace_current_entry, GetLastCommittedEntry())) {
clamy21718cc22018-06-13 13:34:243447 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473448 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393449
3450 // If this is a reload of an existing FrameNavigationEntry and we had a
3451 // policy container for it, then we should copy it into the pending entry,
3452 // so that it is copied to the navigation request in
3453 // CreateNavigationRequestFromLoadParams later.
3454 if (GetLastCommittedEntry()) {
3455 FrameNavigationEntry* previous_frame_entry =
3456 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193457 if (previous_frame_entry &&
3458 previous_frame_entry->policy_container_policies()) {
3459 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Titouan Rigoudy72f892d2022-05-02 18:21:233460 previous_frame_entry->policy_container_policies()->ClonePtr());
Antonio Sartori78a749f2020-11-30 12:03:393461 }
3462 }
3463 }
3464
3465 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
Rakina Zata Amniafd3c6582021-11-30 06:19:173466 // document, then we should copy the document polices from RenderFrameHost's
3467 // PolicyContainerHost. The NavigationRequest will create a new
3468 // PolicyContainerHost with the document policies from the |pending_entry_|,
3469 // and that PolicyContainerHost will be put in the final RenderFrameHost for
3470 // the navigation. This way, we ensure that we keep enforcing the right
3471 // policies on the initial empty document after the reload.
Rakina Zata Amni2322f4f82022-01-24 13:24:243472 if ((!GetLastCommittedEntry() || GetLastCommittedEntry()->IsInitialEntry()) &&
3473 params.url.IsAboutBlank()) {
Antonio Sartori78a749f2020-11-30 12:03:393474 if (node->current_frame_host() &&
3475 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193476 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163477 node->current_frame_host()
3478 ->policy_container_host()
3479 ->policies()
Titouan Rigoudy72f892d2022-05-02 18:21:233480 .ClonePtr());
Antonio Sartori78a749f2020-11-30 12:03:393481 }
clamy21718cc22018-06-13 13:34:243482 }
3483
3484 // navigation_ui_data should only be present for main frame navigations.
Ian Vollick1c6dd3e2022-04-13 02:06:263485 DCHECK(node->IsOutermostMainFrame() || !params.navigation_ui_data);
clamy21718cc22018-06-13 13:34:243486
Tsuyoshi Horo167ca6432022-03-09 05:16:393487 // This will be used to set the Navigation Timing API navigationStart
3488 // parameter for browser navigations in new tabs (intents, tabs opened through
3489 // "Open link in new tab"). If the navigation must wait on the current
3490 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3491 // will be updated when the BeforeUnload ack is received.
3492 const auto navigation_start_time = base::TimeTicks::Now();
3493
Camille Lamy5193caa2018-10-12 11:59:423494 std::unique_ptr<NavigationRequest> request =
3495 CreateNavigationRequestFromLoadParams(
3496 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143497 params.has_user_gesture, network::mojom::SourceLocation::New(),
Tsuyoshi Horo167ca6432022-03-09 05:16:393498 reload_type, pending_entry_, pending_entry_->GetFrameEntry(node),
3499 navigation_start_time);
clamy21718cc22018-06-13 13:34:243500
3501 // If the navigation couldn't start, return immediately and discard the
3502 // pending NavigationEntry.
3503 if (!request) {
3504 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363505 return nullptr;
clamy21718cc22018-06-13 13:34:243506 }
3507
Camille Lamy5193caa2018-10-12 11:59:423508#if DCHECK_IS_ON()
3509 // Safety check that NavigationRequest and NavigationEntry match.
3510 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3511#endif
3512
clamy21718cc22018-06-13 13:34:243513 // This call does not support re-entrancy. See http://crbug.com/347742.
3514 CHECK(!in_navigate_to_pending_entry_);
3515 in_navigate_to_pending_entry_ = true;
3516
arthursonzogni66f711c2019-10-08 14:40:363517 // It is not possible to delete the pending NavigationEntry while navigating
3518 // to it. Grab a reference to delay potential deletion until the end of this
3519 // function.
3520 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3521
Harkiran Bolariaba823e42021-05-21 18:30:363522 base::WeakPtr<NavigationHandle> created_navigation_handle(
3523 request->GetWeakPtr());
Lukasz Anforowicz9ee83c272020-12-01 20:14:053524 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243525
3526 in_navigate_to_pending_entry_ = false;
Harkiran Bolariaba823e42021-05-21 18:30:363527 return created_navigation_handle;
clamy21718cc22018-06-13 13:34:243528}
3529
clamyea99ea12018-05-28 13:54:233530void NavigationControllerImpl::HandleRendererDebugURL(
3531 FrameTreeNode* frame_tree_node,
3532 const GURL& url) {
3533 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243534 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3535 // the renderer process is not live, unless it is the initial navigation
3536 // of the tab.
clamyea99ea12018-05-28 13:54:233537 if (!IsInitialNavigation()) {
3538 DiscardNonCommittedEntries();
3539 return;
3540 }
Fergal Dalyecd3b0202020-06-25 01:57:373541 // The current frame is always a main frame. If IsInitialNavigation() is
3542 // true then there have been no navigations and any frames of this tab must
3543 // be in the same renderer process. If that has crashed then the only frame
3544 // that can be revived is the main frame.
3545 frame_tree_node->render_manager()
3546 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233547 }
3548 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3549}
3550
clamy21718cc22018-06-13 13:34:243551std::unique_ptr<NavigationEntryImpl>
3552NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3553 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423554 const LoadURLParams& params,
3555 bool override_user_agent,
3556 bool should_replace_current_entry,
3557 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393558 // Browser initiated navigations might not have a blob_url_loader_factory set
3559 // in params even if the navigation is to a blob URL. If that happens, lookup
3560 // the correct url loader factory to use here.
3561 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483562 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473563 // Resolve the blob URL in the storage partition associated with the target
3564 // frame. This is the storage partition the URL will be loaded in, and only
3565 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393566 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473567 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393568 }
3569
clamy21718cc22018-06-13 13:34:243570 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443571 // extra_headers in params are \n separated; navigation entries want \r\n.
3572 std::string extra_headers_crlf;
3573 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243574
3575 // For subframes, create a pending entry with a corresponding frame entry.
3576 if (!node->IsMainFrame()) {
Rakina Zata Amni2322f4f82022-01-24 13:24:243577 if (GetLastCommittedEntry()) {
3578 entry = GetLastCommittedEntry()->Clone();
3579 } else {
3580 // If there's no last committed entry, create an entry for about:blank
3581 // with a subframe entry for our destination.
3582 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
3583 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
3584 GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin,
3585 params.source_site_instance.get(), params.transition_type,
3586 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Julie Jeongeun Kim5b9aff72022-05-02 02:10:173587 blob_url_loader_factory, false /* rewrite_virtual_urls */));
Rakina Zata Amni2322f4f82022-01-24 13:24:243588 }
clamy21718cc22018-06-13 13:34:243589 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:083590 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
clamy21718cc22018-06-13 13:34:243591 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Anton Bikineevf62d1bf2021-05-15 17:56:073592 params.url, absl::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203593 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393594 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:483595 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori78a749f2020-11-30 12:03:393596 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163597 // a conversion of a new navigation into a reload, we will set the right
3598 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193599 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243600 } else {
3601 // Otherwise, create a pending entry for the main frame.
Julie Jeongeun Kim5b9aff72022-05-02 02:10:173602 // If `node` is the outermost main frame, it rewrites a virtual url in order
3603 // to adjust the original input url if needed. For inner frames such as
3604 // fenced frames or subframes, they don't rewrite urls as the urls are not
3605 // input urls by users.
3606 bool rewrite_virtual_urls = node->IsOutermostMainFrame();
clamy21718cc22018-06-13 13:34:243607 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063608 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103609 params.source_site_instance.get(), params.transition_type,
3610 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Julie Jeongeun Kim5b9aff72022-05-02 02:10:173611 blob_url_loader_factory, rewrite_virtual_urls));
clamy21718cc22018-06-13 13:34:243612 entry->set_source_site_instance(
3613 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3614 entry->SetRedirectChain(params.redirect_chain);
3615 }
3616
3617 // Set the FTN ID (only used in non-site-per-process, for tests).
3618 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243619 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423620 entry->SetIsOverridingUserAgent(override_user_agent);
3621 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543622 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243623
clamy21718cc22018-06-13 13:34:243624 switch (params.load_type) {
3625 case LOAD_TYPE_DEFAULT:
3626 break;
3627 case LOAD_TYPE_HTTP_POST:
3628 entry->SetHasPostData(true);
3629 entry->SetPostData(params.post_data);
3630 break;
3631 case LOAD_TYPE_DATA:
3632 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3633 entry->SetVirtualURL(params.virtual_url_for_data_url);
Xiaohan Wang7f8052e02022-01-14 18:44:283634#if BUILDFLAG(IS_ANDROID)
clamy21718cc22018-06-13 13:34:243635 entry->SetDataURLAsString(params.data_url_as_string);
3636#endif
3637 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3638 break;
clamy21718cc22018-06-13 13:34:243639 }
3640
3641 // TODO(clamy): NavigationEntry is meant for information that will be kept
3642 // after the navigation ended and therefore is not appropriate for
3643 // started_from_context_menu. Move started_from_context_menu to
3644 // NavigationUIData.
3645 entry->set_started_from_context_menu(params.started_from_context_menu);
3646
3647 return entry;
3648}
3649
clamyea99ea12018-05-28 13:54:233650std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423651NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3652 FrameTreeNode* node,
3653 const LoadURLParams& params,
3654 bool override_user_agent,
3655 bool should_replace_current_entry,
3656 bool has_user_gesture,
Antonio Sartori2f763d9d2021-04-21 10:04:143657 network::mojom::SourceLocationPtr source_location,
Camille Lamy5193caa2018-10-12 11:59:423658 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573659 NavigationEntryImpl* entry,
Tsuyoshi Horo167ca6432022-03-09 05:16:393660 FrameNavigationEntry* frame_entry,
Nan Lin944e9b4e2022-04-12 13:51:223661 base::TimeTicks navigation_start_time,
3662 absl::optional<bool> is_fenced_frame_opaque_url) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573663 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283664 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533665 // All renderer-initiated navigations must have an initiator_origin.
3666 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513667
Camille Lamy5193caa2018-10-12 11:59:423668 GURL url_to_load;
3669 GURL virtual_url;
Anton Bikineevf62d1bf2021-05-15 17:56:073670 absl::optional<url::Origin> origin_to_commit =
3671 frame_entry ? frame_entry->committed_origin() : absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:323672
Camille Lamy2baa8022018-10-19 16:43:173673 // For main frames, rewrite the URL if necessary and compute the virtual URL
3674 // that should be shown in the address bar.
Ian Vollick1c6dd3e2022-04-13 02:06:263675 if (node->IsOutermostMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423676 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173677 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423678 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423679
Camille Lamy2baa8022018-10-19 16:43:173680 // For DATA loads, override the virtual URL.
3681 if (params.load_type == LOAD_TYPE_DATA)
3682 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423683
Camille Lamy2baa8022018-10-19 16:43:173684 if (virtual_url.is_empty())
3685 virtual_url = url_to_load;
3686
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573687 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283688
Aran Gilman249eb122019-12-02 23:32:463689 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3690 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3691 // return different results, leading to a different URL in the
3692 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3693 // pending NavigationEntry, as we'll only make one call to
3694 // RewriteUrlForNavigation.
3695 VLOG_IF(1, (url_to_load != frame_entry->url()))
3696 << "NavigationRequest and FrameEntry have different URLs: "
3697 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283698
Camille Lamy2baa8022018-10-19 16:43:173699 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423700 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173701 // NavigationRequest.
3702 } else {
3703 url_to_load = params.url;
3704 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243705 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173706 }
Camille Lamy5193caa2018-10-12 11:59:423707
Ehsan Karamad44fc72112019-02-26 18:15:473708 if (node->render_manager()->is_attaching_inner_delegate()) {
3709 // Avoid starting any new navigations since this node is now preparing for
3710 // attaching an inner delegate.
3711 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203712 }
Camille Lamy5193caa2018-10-12 11:59:423713
Ian Vollick1c6dd3e2022-04-13 02:06:263714 if (!IsValidURLForNavigation(node->IsOutermostMainFrame(), virtual_url,
3715 url_to_load))
Camille Lamy5193caa2018-10-12 11:59:423716 return nullptr;
3717
Kunihiko Sakamoto346a74e2021-03-10 08:57:483718 if (!DoesURLMatchOriginForNavigation(
3719 url_to_load, origin_to_commit,
Wang Hui14cac7a2022-01-28 06:07:473720 frame_entry->subresource_web_bundle_navigation_info(), entry,
Ian Vollick1c6dd3e2022-04-13 02:06:263721 node->IsOutermostMainFrame())) {
Nasko Oskov03912102019-01-11 00:21:323722 DCHECK(false) << " url:" << url_to_load
3723 << " origin:" << origin_to_commit.value();
3724 return nullptr;
3725 }
3726
danakjd83d706d2020-11-25 22:11:123727 // Look for a pending commit that is to another document in this
3728 // FrameTreeNode. If one exists, then the last committed URL will not be the
3729 // current URL by the time this navigation commits.
3730 bool has_pending_cross_document_commit =
3731 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
Miyoung Shina2dd6a42021-10-07 12:19:213732 bool is_currently_error_page = node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:123733
Minggang Wangb9f3fa92021-07-01 15:30:313734 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjb952ef12021-01-14 19:58:493735 /*old_url=*/node->current_url(),
3736 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3737 has_pending_cross_document_commit, is_currently_error_page,
3738 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423739
3740 // Create the NavigationParams based on |params|.
3741
Hiroki Nakagawa4ed61282021-06-18 05:37:233742 bool is_view_source_mode = entry->IsViewSourceMode();
3743 DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme));
Charlie Harrison8c113a32019-01-07 16:08:293744
Antonio Sartori6984c742021-08-26 08:03:413745 blink::NavigationDownloadPolicy download_policy = params.download_policy;
Hiroki Nakagawa4ed61282021-06-18 05:37:233746 // Update |download_policy| if the virtual URL is view-source.
Charlie Harrison8c113a32019-01-07 16:08:293747 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253748 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293749
Minggang Wangb9f3fa92021-07-01 15:30:313750 blink::mojom::CommonNavigationParamsPtr common_params =
3751 blink::mojom::CommonNavigationParams::New(
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513752 url_to_load, params.initiator_origin,
3753 blink::mojom::Referrer::New(params.referrer.url,
3754 params.referrer.policy),
Scott Violetcf6ea7e2021-06-09 21:09:213755 params.transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513756 should_replace_current_entry, params.base_url_for_data_url,
Tsuyoshi Horo167ca6432022-03-09 05:16:393757 navigation_start_time,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513758 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
Antonio Sartori2f763d9d2021-04-21 10:04:143759 params.post_data, std::move(source_location),
arthursonzogniaf7c62c52020-02-12 10:49:413760 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273761 false /* has_text_fragment_token */,
3762 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3763 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513764 false /* is_history_navigation_in_new_child_frame */,
Nan Lind91c8152021-10-21 16:22:373765 params.input_start, network::mojom::RequestDestination::kEmpty);
Camille Lamy5193caa2018-10-12 11:59:423766
Minggang Wangb9f3fa92021-07-01 15:30:313767 blink::mojom::CommitNavigationParamsPtr commit_params =
3768 blink::mojom::CommitNavigationParams::New(
Antonio Sartori3e8de6d2021-07-26 10:28:413769 frame_entry->committed_origin(),
3770 // The correct storage key will be computed before committing the
3771 // navigation.
Pâris Meuleman4d97a702022-07-05 10:42:003772 blink::StorageKey(), override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323773 std::vector<network::mojom::URLResponseHeadPtr>(),
jongdeok.kim5de823b32022-06-14 04:37:503774 std::vector<net::RedirectInfo>(), params.post_content_type,
3775 common_params->url, common_params->method,
3776 params.can_load_local_resources,
Minggang Wangb9f3fa92021-07-01 15:30:313777 frame_entry->page_state().ToEncodedData(), entry->GetUniqueID(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143778 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3779 -1 /* pending_history_list_offset */,
3780 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3781 params.should_clear_history_list ? 0 : GetEntryCount(),
3782 false /* was_discarded */, is_view_source_mode,
Minggang Wangb9f3fa92021-07-01 15:30:313783 params.should_clear_history_list,
3784 blink::mojom::NavigationTiming::New(),
Minggang Wangf59db47b2021-06-16 01:56:223785 blink::mojom::WasActivatedOption::kUnknown,
Lucas Furukawa Gadania9c45682019-07-31 22:05:143786 base::UnguessableToken::Create() /* navigation_token */,
Minggang Wang7ee0c742021-06-16 16:16:513787 std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(),
Xiaohan Wang7f8052e02022-01-14 18:44:283788#if BUILDFLAG(IS_ANDROID)
Lucas Furukawa Gadania9c45682019-07-31 22:05:143789 std::string(), /* data_url_as_string */
3790#endif
arthursonzogni14379782020-05-15 09:09:273791 !params.is_renderer_initiated, /* is_browser_initiated */
Tsuyoshi Horoe86d7702019-11-29 01:52:473792 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533793 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173794 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073795 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163796 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333797 false /* origin_agent_cluster */,
Daniel Vogelheim243df9f2022-02-22 10:32:033798 true /* origin_agent_cluster_left_as_default */,
Maks Orlovichc66745a2020-06-30 17:40:023799 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213800 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Nate Chapind1fe3612021-04-16 20:45:573801 false /* is_cross_browsing_instance */, nullptr /* old_page_info */,
3802 -1 /* http_response_code */,
Domenic Denicolacd30f5f82022-03-16 21:48:013803 blink::mojom::NavigationApiHistoryEntryArrays::New(),
Matt Menke4e209082021-11-09 04:59:313804 std::vector<GURL>() /* early_hints_preloaded_resources */,
Clark DuVall8ee487a22021-11-10 02:25:583805 absl::nullopt /* ad_auction_components */,
shivanigithubc7b97ca2022-04-05 19:41:403806 /*fenced_frame_reporting_metadata=*/nullptr,
Clark DuVall8ee487a22021-11-10 02:25:583807 // This timestamp will be populated when the commit IPC is sent.
Arthur Sonzognie41678a2022-06-16 15:51:193808 base::TimeTicks() /* commit_sent */, std::string() /* srcdoc_value */,
3809 false /* should_load_data_url */);
Xiaohan Wang7f8052e02022-01-14 18:44:283810#if BUILDFLAG(IS_ANDROID)
Camille Lamy5193caa2018-10-12 11:59:423811 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143812 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423813 }
3814#endif
3815
Lucas Furukawa Gadania9c45682019-07-31 22:05:143816 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423817
Camille Lamy5193caa2018-10-12 11:59:423818 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3819 std::string extra_headers_crlf;
3820 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093821
3822 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143823 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083824 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453825 params.initiator_frame_token.has_value()
3826 ? &(params.initiator_frame_token.value())
3827 : nullptr,
3828 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
jongdeok.kim5de823b32022-06-14 04:37:503829 params.is_form_submission,
John Delaney50425f82020-04-07 16:26:213830 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
Nan Lin944e9b4e2022-04-12 13:51:223831 params.impression, params.is_pdf, is_fenced_frame_opaque_url);
Yao Xiaodc5ed102019-06-04 19:19:093832 navigation_request->set_from_download_cross_origin_redirect(
3833 params.from_download_cross_origin_redirect);
W. James MacLean00568d72022-02-24 19:36:553834 navigation_request->set_force_new_browsing_instance(
3835 params.force_new_browsing_instance);
Yao Xiaodc5ed102019-06-04 19:19:093836 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423837}
3838
3839std::unique_ptr<NavigationRequest>
3840NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233841 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573842 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233843 FrameNavigationEntry* frame_entry,
3844 ReloadType reload_type,
3845 bool is_same_document_history_load,
Nate Chapin45f620582021-09-30 17:45:433846 bool is_history_navigation_in_new_child_frame,
3847 bool is_browser_initiated) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343848 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233849 GURL dest_url = frame_entry->url();
Rakina Zata Amnif297a802022-01-18 03:53:433850 // We should never navigate to an existing initial NavigationEntry that is the
3851 // initial NavigationEntry for the initial empty document that hasn't been
3852 // overridden by the synchronous about:blank commit, to preserve previous
3853 // behavior where trying to reload when the main frame is on the initial empty
3854 // document won't result in a navigation.
3855 // See also https://crbug.com/1277414.
3856 DCHECK(!entry->IsInitialEntryNotForSynchronousAboutBlank());
Anton Bikineevf62d1bf2021-05-15 17:56:073857 absl::optional<url::Origin> origin_to_commit =
Nasko Oskov03912102019-01-11 00:21:323858 frame_entry->committed_origin();
3859
clamyea99ea12018-05-28 13:54:233860 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013861 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573862 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233863 // We may have been redirected when navigating to the current URL.
3864 // Use the URL the user originally intended to visit as signaled by the
3865 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013866 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573867 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233868 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323869 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233870 }
3871
Ehsan Karamad44fc72112019-02-26 18:15:473872 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3873 // Avoid starting any new navigations since this node is now preparing for
3874 // attaching an inner delegate.
3875 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203876 }
3877
Ian Vollick1c6dd3e2022-04-13 02:06:263878 if (!IsValidURLForNavigation(frame_tree_node->IsOutermostMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573879 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233880 return nullptr;
3881 }
3882
Kunihiko Sakamoto346a74e2021-03-10 08:57:483883 if (!DoesURLMatchOriginForNavigation(
3884 dest_url, origin_to_commit,
Wang Hui14cac7a2022-01-28 06:07:473885 frame_entry->subresource_web_bundle_navigation_info(), entry,
Ian Vollick1c6dd3e2022-04-13 02:06:263886 frame_tree_node->IsOutermostMainFrame())) {
Kunihiko Sakamoto346a74e2021-03-10 08:57:483887 DCHECK(false) << " url:" << dest_url
Wang Hui14cac7a2022-01-28 06:07:473888 << " base_url_for_data_url: " << entry->GetBaseURLForDataURL()
Kunihiko Sakamoto346a74e2021-03-10 08:57:483889 << " origin:" << origin_to_commit.value();
Nasko Oskov03912102019-01-11 00:21:323890 return nullptr;
3891 }
3892
clamyea99ea12018-05-28 13:54:233893 // This will be used to set the Navigation Timing API navigationStart
3894 // parameter for browser navigations in new tabs (intents, tabs opened through
3895 // "Open link in new tab"). If the navigation must wait on the current
3896 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3897 // will be updated when the BeforeUnload ack is received.
3898 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233899
danakjd83d706d2020-11-25 22:11:123900 // Look for a pending commit that is to another document in this
3901 // FrameTreeNode. If one exists, then the last committed URL will not be the
3902 // current URL by the time this navigation commits.
3903 bool has_pending_cross_document_commit =
3904 frame_tree_node->render_manager()
3905 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493906 bool is_currently_error_page =
Miyoung Shina2dd6a42021-10-07 12:19:213907 frame_tree_node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:123908
Minggang Wangb9f3fa92021-07-01 15:30:313909 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123910 /*old_url=*/frame_tree_node->current_url(),
3911 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493912 has_pending_cross_document_commit, is_currently_error_page,
3913 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423914
3915 // A form submission may happen here if the navigation is a
3916 // back/forward/reload navigation that does a form resubmission.
3917 scoped_refptr<network::ResourceRequestBody> request_body;
3918 std::string post_content_type;
jongdeok.kim5de823b32022-06-14 04:37:503919 // TODO(https://crbug.com/931209) Store |is_form_submission| in the history
3920 // entry. This way, it could be directly retrieved here. Right now, it is only
3921 // partially recovered when request.method == "POST" and request.body exists.
3922 bool is_form_submission = false;
Camille Lamy5193caa2018-10-12 11:59:423923 if (frame_entry->method() == "POST") {
3924 request_body = frame_entry->GetPostData(&post_content_type);
3925 // Might have a LF at end.
Peter Kastingb53b81912021-04-28 19:23:303926 post_content_type = std::string(
3927 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL));
jongdeok.kim5de823b32022-06-14 04:37:503928 is_form_submission = !!request_body;
Camille Lamy5193caa2018-10-12 11:59:423929 }
3930
3931 // Create the NavigationParams based on |entry| and |frame_entry|.
Minggang Wangb9f3fa92021-07-01 15:30:313932 blink::mojom::CommonNavigationParamsPtr common_params =
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513933 entry->ConstructCommonNavigationParams(
3934 *frame_entry, request_body, dest_url,
3935 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
Tarun Bansalbcd62c82022-01-18 17:27:383936 navigation_type, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533937 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513938 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553939 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423940
3941 // TODO(clamy): |intended_as_new_entry| below should always be false once
3942 // Reload no longer leads to this being called for a pending NavigationEntry
3943 // of index -1.
Minggang Wangb9f3fa92021-07-01 15:30:313944 blink::mojom::CommitNavigationParamsPtr commit_params =
Lucas Furukawa Gadania9c45682019-07-31 22:05:143945 entry->ConstructCommitNavigationParams(
3946 *frame_entry, common_params->url, origin_to_commit,
3947 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3948 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533949 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3950 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143951 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423952
W. James MacLean81b8d01f2022-01-25 20:50:593953 if (common_params->url.IsAboutSrcdoc())
3954 commit_params->srcdoc_value = frame_tree_node->srcdoc_value();
clamyea99ea12018-05-28 13:54:233955 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143956 frame_tree_node, std::move(common_params), std::move(commit_params),
Nate Chapin45f620582021-09-30 17:45:433957 is_browser_initiated, false /* was_opener_suppressed */,
Takashi Toyoshimae87b7be2021-01-22 11:51:083958 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453959 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
jongdeok.kim5de823b32022-06-14 04:37:503960 entry->extra_headers(), frame_entry, entry, is_form_submission,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473961 nullptr /* navigation_ui_data */, absl::nullopt /* impression */,
3962 false /* is_pdf */);
clamyea99ea12018-05-28 13:54:233963}
3964
[email protected]d202a7c2012-01-04 07:53:473965void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213966 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273967 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403968
Takashi Toyoshimaea534ef22021-07-21 03:27:593969 // We need to notify the ssl_manager_ before the WebContents so the
[email protected]df1af242009-05-01 00:11:403970 // location bar will have up-to-date information about the security style
3971 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133972 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403973
Rakina Zata Amnidaa84f62022-02-17 00:55:313974 bool should_fire_navigation_state_changed = true;
3975#if BUILDFLAG(IS_ANDROID)
3976 if (details && details->should_stay_as_initial_entry) {
3977 // For initial NavigationEntries, only fire NavigationStateChanged() if the
3978 // embedder wants to hear about it.
3979 should_fire_navigation_state_changed =
3980 !GetContentClient()
3981 ->browser()
3982 ->ShouldIgnoreInitialNavigationEntryNavigationStateChangedForLegacySupport();
3983 }
3984#endif
3985 if (should_fire_navigation_state_changed)
3986 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
3987
[email protected]ec6c05f2013-10-23 18:41:573988 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463989
[email protected]b0f724c2013-09-05 04:21:133990 // TODO(avi): Remove. http://crbug.com/170921
3991 NotificationDetails notification_details =
3992 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153993 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3994 Source<NavigationController>(this),
3995 notification_details);
initial.commit09911bf2008-07-26 23:55:293996}
3997
initial.commit09911bf2008-07-26 23:55:293998// static
[email protected]d202a7c2012-01-04 07:53:473999size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:234000 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:154001 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:214002 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:234003}
4004
[email protected]d202a7c2012-01-04 07:53:474005void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:224006 if (is_active && needs_reload_)
4007 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:294008}
4009
[email protected]d202a7c2012-01-04 07:53:474010void NavigationControllerImpl::LoadIfNecessary() {
Rakina Zata Amnid605d462022-06-01 10:17:034011 SCOPED_CRASH_KEY_BOOL("nav_reentrancy_caller1", "LoadIf_pending",
4012 !!pending_entry_);
initial.commit09911bf2008-07-26 23:55:294013 if (!needs_reload_)
4014 return;
4015
Bo Liucdfa4b12018-11-06 00:21:444016 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
4017 needs_reload_type_);
4018
initial.commit09911bf2008-07-26 23:55:294019 // Calling Reload() results in ignoring state, and not loading.
4020 // Explicitly use NavigateToPendingEntry so that the renderer uses the
4021 // cached state.
avicc872d7242015-08-19 21:26:344022 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:164023 NavigateToExistingPendingEntry(ReloadType::NONE,
Nate Chapin45f620582021-09-30 17:45:434024 FrameTreeNode::kFrameTreeNodeInvalidId,
4025 true /* is_browser_initiated */);
Rakina Zata Amni2322f4f82022-01-24 13:24:244026 } else if (last_committed_entry_index_ != -1 &&
4027 !GetLastCommittedEntry()
Rakina Zata Amnif297a802022-01-18 03:53:434028 ->IsInitialEntryNotForSynchronousAboutBlank()) {
arthursonzogni5c4c202d2017-04-25 23:41:274029 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:344030 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:164031 NavigateToExistingPendingEntry(ReloadType::NONE,
Nate Chapin45f620582021-09-30 17:45:434032 FrameTreeNode::kFrameTreeNodeInvalidId,
4033 true /* is_browser_initiated */);
avicc872d7242015-08-19 21:26:344034 } else {
Rakina Zata Amnif297a802022-01-18 03:53:434035 // We should never navigate to an existing initial NavigationEntry that is
4036 // the initial NavigationEntry for the initial empty document that hasn't
4037 // been overridden by the synchronous about:blank commit, to preserve
4038 // legacy behavior where trying to reload when the main frame is on the
4039 // initial empty document won't result in a navigation. See also
4040 // https://crbug.com/1277414. If there is something to reload, the
4041 // successful reload will clear the |needs_reload_| flag. Otherwise, just do
4042 // it here.
avicc872d7242015-08-19 21:26:344043 needs_reload_ = false;
4044 }
initial.commit09911bf2008-07-26 23:55:294045}
4046
Kevin McNeeccca6172021-10-19 17:11:144047base::WeakPtr<NavigationHandle>
4048NavigationControllerImpl::LoadPostCommitErrorPage(
Carlos IL42b416592019-10-07 23:10:364049 RenderFrameHost* render_frame_host,
4050 const GURL& url,
4051 const std::string& error_page_html,
4052 net::Error error) {
Rakina Zata Amni919b7922020-12-11 09:03:134053 RenderFrameHostImpl* rfhi =
4054 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:584055
4056 // Only active documents can load post-commit error pages:
4057 // - If the document is in pending deletion, the browser already committed to
4058 // destroying this RenderFrameHost so ignore loading the error page.
4059 // - If the document is in back-forward cache, it's not allowed to navigate as
4060 // it should remain frozen. Ignore the request and evict the document from
4061 // back-forward cache.
4062 // - If the document is prerendering, it can navigate but when loading error
4063 // pages, cancel prerendering.
Fergal Daly1336ac642021-09-14 15:13:114064 if (rfhi->IsInactiveAndDisallowActivation(
4065 DisallowActivationReasonId::kLoadPostCommitErrorPage)) {
Kevin McNeeccca6172021-10-19 17:11:144066 return nullptr;
Fergal Daly1336ac642021-09-14 15:13:114067 }
Sreeja Kamishettydb8e2892021-03-10 09:30:584068
Rakina Zata Amni919b7922020-12-11 09:03:134069 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:414070
Minggang Wangb9f3fa92021-07-01 15:30:314071 blink::mojom::CommonNavigationParamsPtr common_params =
Minggang Wanga13c796e2021-07-02 05:54:434072 blink::CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:594073 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
4074 // the frame actually committed (e.g. iframe with "src" set to a
4075 // slow-responding URL). We should rewrite the URL to about:blank in this
4076 // case, as the renderer will only think a page is an error page if it has a
4077 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:134078 common_params->url = url.is_empty() ? GURL("about:blank") : url;
Minggang Wangb9f3fa92021-07-01 15:30:314079 blink::mojom::CommitNavigationParamsPtr commit_params =
Minggang Wanga13c796e2021-07-02 05:54:434080 blink::CreateCommitNavigationParams();
Antonio Sartori58591c892021-04-21 06:54:334081 commit_params->original_url = common_params->url;
John Delaney131ad362019-08-08 21:57:414082
arthursonzogni70ac7302020-05-28 08:49:054083 // Error pages have a fully permissive FramePolicy.
4084 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
4085 // error pages.
4086 commit_params->frame_policy = blink::FramePolicy();
4087
John Delaney131ad362019-08-08 21:57:414088 std::unique_ptr<NavigationRequest> navigation_request =
4089 NavigationRequest::CreateBrowserInitiated(
4090 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:084091 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:194092 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:454093 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:064094 "" /* extra_headers */, nullptr /* frame_entry */,
jongdeok.kim5de823b32022-06-14 04:37:504095 nullptr /* entry */, false /* is_form_submission */,
Daniel Hosseinianf0fbfb42021-09-08 02:20:474096 nullptr /* navigation_ui_data */, absl::nullopt /* impression */,
4097 false /* is_pdf */);
Carlos IL42b416592019-10-07 23:10:364098 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:414099 navigation_request->set_net_error(error);
4100 node->CreatedNavigationRequest(std::move(navigation_request));
4101 DCHECK(node->navigation_request());
Kevin McNeeccca6172021-10-19 17:11:144102
4103 // Calling BeginNavigation may destroy the NavigationRequest.
4104 base::WeakPtr<NavigationRequest> created_navigation_request(
4105 node->navigation_request()->GetWeakPtr());
John Delaney131ad362019-08-08 21:57:414106 node->navigation_request()->BeginNavigation();
Kevin McNeeccca6172021-10-19 17:11:144107 return created_navigation_request;
John Delaney131ad362019-08-08 21:57:414108}
4109
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574110void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:214111 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:094112 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:154113 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:144114 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:294115}
4116
[email protected]d202a7c2012-01-04 07:53:474117void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:364118 RestoreType type) {
Charlie Reis23c26da2022-01-29 00:57:474119 // TODO(https://crbug.com/1287624): Don't allow an index of -1, which would
4120 // represent a no-committed-entry state.
4121 DCHECK(selected_index >= -1 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:364122 ConfigureEntriesForRestore(&entries_, type);
Charlie Reis23c26da2022-01-29 00:57:474123 // TODO(https://crbug.com/1287624): This will be pointing to the wrong entry
4124 // if `entries_` contains pre-existing entries from the NavigationController
4125 // before restore, which would not be removed and will be at the front of the
4126 // entries list, causing the index to be off by the amount of pre-existing
4127 // entries in the list. Fix this to point to the correct entry.
initial.commit09911bf2008-07-26 23:55:294128 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:294129}
[email protected]765b35502008-08-21 00:51:204130
arthursonzogni69a6a1b2019-09-17 09:23:004131void NavigationControllerImpl::DiscardNonCommittedEntries() {
Rakina Zata Amnia4e27222021-12-22 01:05:004132 DiscardNonCommittedEntriesWithCommitDetails(nullptr /* commit_details */);
4133}
4134
4135void NavigationControllerImpl::DiscardNonCommittedEntriesWithCommitDetails(
4136 LoadCommittedDetails* commit_details) {
Michael Thiessen9b14d512019-09-23 21:19:474137 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:104138 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
4139 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:294140 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:474141 return;
Michael Thiessenc5676d22019-09-25 22:32:104142 }
avi45a72532015-04-07 21:01:454143 DiscardPendingEntry(false);
Rakina Zata Amnidaa84f62022-02-17 00:55:314144
4145 if (!delegate_)
4146 return;
4147
4148 bool should_fire_navigation_state_changed = true;
4149#if BUILDFLAG(IS_ANDROID)
4150 if (commit_details && commit_details->should_stay_as_initial_entry) {
4151 // For initial NavigationEntries, only fire NavigationStateChanged() if the
4152 // embedder wants to hear about it.
4153 should_fire_navigation_state_changed =
4154 !GetContentClient()
4155 ->browser()
4156 ->ShouldIgnoreInitialNavigationEntryNavigationStateChangedForLegacySupport();
Rakina Zata Amniddf10502022-01-15 02:56:554157 }
Rakina Zata Amnidaa84f62022-02-17 00:55:314158#endif
4159 if (should_fire_navigation_state_changed)
4160 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:484161}
4162
avi7c6f35e2015-05-08 17:52:384163int NavigationControllerImpl::GetEntryIndexWithUniqueID(
4164 int nav_entry_id) const {
4165 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
4166 if (entries_[i]->GetUniqueID() == nav_entry_id)
4167 return i;
4168 }
4169 return -1;
4170}
4171
[email protected]d202a7c2012-01-04 07:53:474172void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574173 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:254174 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574175 DCHECK_LE(max_index, source->GetEntryCount());
Nate Chapin214a86a2021-06-21 20:35:574176 std::unique_ptr<NavigationEntryRestoreContextImpl> context =
4177 std::make_unique<NavigationEntryRestoreContextImpl>();
[email protected]e1cd5452010-08-26 18:03:254178 for (int i = 0; i < max_index; i++) {
Nate Chapin9f169072021-06-09 19:32:374179 // Normally, cloning a NavigationEntryImpl results in sharing
4180 // FrameNavigationEntries between the original and the clone. However, when
4181 // cloning from a different NavigationControllerImpl, we want to fork the
4182 // FrameNavigationEntries.
Nate Chapin9f169072021-06-09 19:32:374183 entries_.insert(entries_.begin() + i,
Nate Chapin214a86a2021-06-21 20:35:574184 source->entries_[i]->CloneWithoutSharing(context.get()));
[email protected]e1cd5452010-08-26 18:03:254185 }
arthursonzogni5c4c202d2017-04-25 23:41:274186 DCHECK(pending_entry_index_ == -1 ||
4187 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:254188}
[email protected]c5b88d82012-10-06 17:03:334189
4190void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:184191 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:334192 get_timestamp_callback_ = get_timestamp_callback;
4193}
[email protected]8ff00d72012-10-23 19:12:214194
Shivani Sharmaffb32b82019-04-09 16:58:474195// History manipulation intervention:
4196void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:474197 bool replace_entry,
4198 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:404199 bool is_renderer_initiated,
4200 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:454201 // Note that for a subframe, previous_document_was_activated is true if the
4202 // gesture happened in any subframe (propagated to main frame) or in the main
4203 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:474204 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:274205 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:474206 return;
4207 }
4208 if (last_committed_entry_index_ == -1)
4209 return;
4210
Shivani Sharmac4cc8922019-04-18 03:11:174211 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:474212
Alexander Timine3ec4192020-04-20 16:39:404213 // Log UKM with the URL we are navigating away from.
4214 ukm::builders::HistoryManipulationIntervention(
4215 previous_page_load_ukm_source_id)
4216 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:474217}
4218
Shivani Sharmac4cc8922019-04-18 03:11:174219void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
4220 int reference_index,
4221 bool skippable) {
4222 auto* reference_entry = GetEntryAtIndex(reference_index);
4223 reference_entry->set_should_skip_on_back_forward_ui(skippable);
4224
4225 int64_t document_sequence_number =
4226 reference_entry->root_node()->frame_entry->document_sequence_number();
4227 for (int index = 0; index < GetEntryCount(); index++) {
4228 auto* entry = GetEntryAtIndex(index);
4229 if (entry->root_node()->frame_entry->document_sequence_number() ==
4230 document_sequence_number) {
4231 entry->set_should_skip_on_back_forward_ui(skippable);
4232 }
4233 }
4234}
4235
arthursonzogni66f711c2019-10-08 14:40:364236std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
4237NavigationControllerImpl::ReferencePendingEntry() {
4238 DCHECK(pending_entry_);
4239 auto pending_entry_ref =
4240 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
4241 pending_entry_refs_.insert(pending_entry_ref.get());
4242 return pending_entry_ref;
4243}
4244
4245void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
4246 // Ignore refs that don't correspond to the current pending entry.
4247 auto it = pending_entry_refs_.find(ref);
4248 if (it == pending_entry_refs_.end())
4249 return;
4250 pending_entry_refs_.erase(it);
4251
4252 if (!pending_entry_refs_.empty())
4253 return;
4254
4255 // The pending entry may be deleted before the last PendingEntryRef.
4256 if (!pending_entry_)
4257 return;
4258
4259 // We usually clear the pending entry when the matching NavigationRequest
4260 // fails, so that an arbitrary URL isn't left visible above a committed page.
4261 //
4262 // However, we do preserve the pending entry in some cases, such as on the
4263 // initial navigation of an unmodified blank tab. We also allow the delegate
4264 // to say when it's safe to leave aborted URLs in the omnibox, to let the
4265 // user edit the URL and try again. This may be useful in cases that the
4266 // committed page cannot be attacker-controlled. In these cases, we still
4267 // allow the view to clear the pending entry and typed URL if the user
4268 // requests (e.g., hitting Escape with focus in the address bar).
4269 //
4270 // Do not leave the pending entry visible if it has an invalid URL, since this
4271 // might be formatted in an unexpected or unsafe way.
4272 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:364273 bool should_preserve_entry =
4274 (pending_entry_ == GetVisibleEntry()) &&
4275 pending_entry_->GetURL().is_valid() &&
4276 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
4277 if (should_preserve_entry)
4278 return;
4279
4280 DiscardPendingEntry(true);
4281 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
4282}
4283
Titouan Rigoudy6ec70402021-02-02 15:42:194284std::unique_ptr<PolicyContainerPolicies>
4285NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:394286 RenderFrameHostImpl* rfh,
4287 bool is_same_document,
Rakina Zata Amniafd3c6582021-11-30 06:19:174288 const GURL& url) {
Antonio Sartori78a749f2020-11-30 12:03:394289 if (is_same_document) {
Rakina Zata Amni2322f4f82022-01-24 13:24:244290 // TODO(https://crbug.com/524208): Remove this nullptr check when we can
4291 // ensure we always have a FrameNavigationEntry here.
4292 if (!GetLastCommittedEntry())
4293 return nullptr;
Charlie Reis73e356242021-04-02 17:10:314294
Antonio Sartori78a749f2020-11-30 12:03:394295 FrameNavigationEntry* previous_frame_entry =
4296 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
4297
4298 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
4299 // ensure we always have a FrameNavigationEntry here.
4300 if (!previous_frame_entry)
4301 return nullptr;
4302
Titouan Rigoudy6ec70402021-02-02 15:42:194303 const PolicyContainerPolicies* previous_policies =
4304 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:394305
Titouan Rigoudy6ec70402021-02-02 15:42:194306 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:394307 return nullptr;
4308
4309 // Make a copy of the policy container for the new FrameNavigationEntry.
Titouan Rigoudy72f892d2022-05-02 18:21:234310 return previous_policies->ClonePtr();
Antonio Sartori78a749f2020-11-30 12:03:394311 }
4312
Titouan Rigoudy72f892d2022-05-02 18:21:234313 return rfh->policy_container_host()->policies().ClonePtr();
Antonio Sartori78a749f2020-11-30 12:03:394314}
4315
Hayato Ito2c8c08d02021-06-23 03:38:434316void NavigationControllerImpl::BroadcastHistoryOffsetAndLength() {
Carlos Caballeroede6f8c2021-01-28 11:01:504317 OPTIONAL_TRACE_EVENT2(
Hayato Ito2c8c08d02021-06-23 03:38:434318 "content", "NavigationControllerImpl::BroadcastHistoryOffsetAndLength",
4319 "history_offset", GetLastCommittedEntryIndex(), "history_length",
4320 GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:504321
4322 auto callback = base::BindRepeating(
4323 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
4324 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
4325 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
4326 }
4327 },
Hayato Ito2c8c08d02021-06-23 03:38:434328 GetLastCommittedEntryIndex(), GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:504329 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
4330}
4331
4332void NavigationControllerImpl::DidAccessInitialMainDocument() {
4333 // We may have left a failed browser-initiated navigation in the address bar
4334 // to let the user edit it and try again. Clear it now that content might
4335 // show up underneath it.
Yu Gaoc8c18552022-06-22 14:38:454336 if (!frame_tree_.IsLoadingIncludingInnerFrameTrees() && GetPendingEntry())
Carlos Caballeroede6f8c2021-01-28 11:01:504337 DiscardPendingEntry(false);
4338
4339 // Update the URL display.
4340 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
4341}
4342
4343void NavigationControllerImpl::UpdateStateForFrame(
4344 RenderFrameHostImpl* rfhi,
4345 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:564346 OPTIONAL_TRACE_EVENT1("content",
4347 "NavigationControllerImpl::UpdateStateForFrame",
4348 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:504349 // The state update affects the last NavigationEntry associated with the given
4350 // |render_frame_host|. This may not be the last committed NavigationEntry (as
4351 // in the case of an UpdateState from a frame being swapped out). We track
4352 // which entry this is in the RenderFrameHost's nav_entry_id.
4353 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
4354 if (!entry)
4355 return;
4356
4357 FrameNavigationEntry* frame_entry =
4358 entry->GetFrameEntry(rfhi->frame_tree_node());
4359 if (!frame_entry)
4360 return;
4361
4362 // The SiteInstance might not match if we do a cross-process navigation with
4363 // replacement (e.g., auto-subframe), in which case the swap out of the old
4364 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4365 // has already been replaced and destroyed.
4366 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4367 return;
4368
4369 if (page_state == frame_entry->page_state())
4370 return; // Nothing to update.
4371
4372 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4373
4374 // The document_sequence_number and item_sequence_number recorded in the
4375 // FrameNavigationEntry should not differ from the one coming with the update,
4376 // since it must come from the same document. Do not update it if a difference
4377 // is detected, as this indicates that |frame_entry| is not the correct one.
4378 blink::ExplodedPageState exploded_state;
4379 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4380 return;
4381
4382 if (exploded_state.top.document_sequence_number !=
4383 frame_entry->document_sequence_number() ||
4384 exploded_state.top.item_sequence_number !=
4385 frame_entry->item_sequence_number()) {
4386 return;
4387 }
4388
4389 frame_entry->SetPageState(page_state);
4390 NotifyEntryChanged(entry);
4391}
4392
Domenic Denicolacd30f5f82022-03-16 21:48:014393std::vector<blink::mojom::NavigationApiHistoryEntryPtr>
4394NavigationControllerImpl::PopulateSingleNavigationApiHistoryEntryVector(
Nate Chapind1fe3612021-04-16 20:45:574395 Direction direction,
4396 int entry_index,
4397 const url::Origin& pending_origin,
4398 FrameTreeNode* node,
4399 SiteInstance* site_instance,
Nate Chapin63db0d12022-01-20 22:03:304400 int64_t pending_item_sequence_number,
4401 int64_t pending_document_sequence_number) {
Domenic Denicolacd30f5f82022-03-16 21:48:014402 std::vector<blink::mojom::NavigationApiHistoryEntryPtr> entries;
Rakina Zata Amni2322f4f82022-01-24 13:24:244403 if (GetLastCommittedEntry() && GetLastCommittedEntry()->IsInitialEntry()) {
Rakina Zata Amniafd3c6582021-11-30 06:19:174404 // Don't process the initial entry.
4405 DCHECK_EQ(GetEntryCount(), 1);
4406 return entries;
4407 }
Nate Chapind1fe3612021-04-16 20:45:574408 int offset = direction == Direction::kForward ? 1 : -1;
Nate Chapin63db0d12022-01-20 22:03:304409 int64_t previous_item_sequence_number = pending_item_sequence_number;
Nate Chapind1fe3612021-04-16 20:45:574410 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4411 i += offset) {
4412 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
Nate Chapindedfa642022-01-28 23:59:414413 if (!frame_entry)
Nate Chapind1fe3612021-04-16 20:45:574414 break;
Domenic Denicolacd30f5f82022-03-16 21:48:014415 // An entry should only appear in the navigation API entries if it is for
4416 // the same origin as the document being committed. Check the committed
4417 // origin, or if that is not available (during restore), check against the
4418 // FNE's url.
Nate Chapindedfa642022-01-28 23:59:414419 url::Origin frame_entry_origin =
4420 frame_entry->committed_origin().value_or(url::Origin::Resolve(
4421 frame_entry->url(),
4422 frame_entry->initiator_origin().value_or(url::Origin())));
4423 if (!pending_origin.IsSameOriginWith(frame_entry_origin))
Nate Chapind1fe3612021-04-16 20:45:574424 break;
4425 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4426 continue;
4427 blink::ExplodedPageState exploded_page_state;
4428 if (blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4429 &exploded_page_state)) {
4430 blink::ExplodedFrameState frame_state = exploded_page_state.top;
Nate Chapin63db0d12022-01-20 22:03:304431
4432 // If the document represented by this FNE hid its full url from appearing
4433 // in a referrer via a "no-referrer" or "origin" referrer policy, censor
Domenic Denicolacd30f5f82022-03-16 21:48:014434 // the url in the navigation API as well (unless we're navigating to that
Nate Chapin63db0d12022-01-20 22:03:304435 // document).
4436 std::u16string url;
4437 if (pending_document_sequence_number ==
4438 frame_entry->document_sequence_number() ||
Domenic Denicolacc094fb2022-03-16 23:40:574439 !frame_entry->protect_url_in_navigation_api()) {
Nate Chapin63db0d12022-01-20 22:03:304440 url = frame_state.url_string.value_or(std::u16string());
4441 }
4442
Domenic Denicolacd30f5f82022-03-16 21:48:014443 blink::mojom::NavigationApiHistoryEntryPtr entry =
4444 blink::mojom::NavigationApiHistoryEntry::New(
Domenic Denicolacc094fb2022-03-16 23:40:574445 frame_state.navigation_api_key.value_or(std::u16string()),
4446 frame_state.navigation_api_id.value_or(std::u16string()), url,
Nate Chapinab5c3a712021-11-18 22:17:094447 frame_state.item_sequence_number,
4448 frame_state.document_sequence_number,
Nate Chapin393cbde12022-05-27 00:36:304449 frame_state.navigation_api_state);
Rakina Zata Amniafd3c6582021-11-30 06:19:174450
Nate Chapin63db0d12022-01-20 22:03:304451 DCHECK(entry->url.empty() ||
4452 pending_origin.CanBeDerivedFrom(GURL(entry->url)));
Nate Chapind1fe3612021-04-16 20:45:574453 entries.push_back(std::move(entry));
4454 previous_item_sequence_number = frame_entry->item_sequence_number();
4455 }
4456 }
4457 // If |entries| was constructed by iterating backwards from
4458 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4459 // earliest-at-the-front. Reverse it.
4460 if (direction == Direction::kBack)
4461 std::reverse(entries.begin(), entries.end());
4462 return entries;
4463}
4464
Domenic Denicolacd30f5f82022-03-16 21:48:014465blink::mojom::NavigationApiHistoryEntryArraysPtr
4466NavigationControllerImpl::GetNavigationApiHistoryEntryVectors(
Nate Chapin97d2f542022-02-18 01:34:554467 FrameTreeNode* node,
Nate Chapind1fe3612021-04-16 20:45:574468 NavigationRequest* request) {
4469 url::Origin pending_origin =
Nate Chapin97d2f542022-02-18 01:34:554470 request ? request->commit_params().origin_to_commit
4471 ? *request->commit_params().origin_to_commit
4472 : url::Origin::Create(request->common_params().url)
4473 : url::Origin::Create(node->current_url());
Nate Chapind1fe3612021-04-16 20:45:574474
Nate Chapind1fe3612021-04-16 20:45:574475 scoped_refptr<SiteInstance> site_instance =
Nate Chapin97d2f542022-02-18 01:34:554476 node->current_frame_host()->GetSiteInstance();
Nate Chapind1fe3612021-04-16 20:45:574477
Nate Chapine82339d02022-05-03 23:48:254478 // NOTE: |entry_index| is the index where this entry will commit if no
4479 // modifications are made between now and DidCommitNavigation. This is used to
4480 // walk |entries_| and determine which entries should be exposed by the
4481 // navigation API. It is important to calculate this correctly, because blink
4482 // will cancel a same-document history commit if it's not present in the
4483 // entries blink knows about.
4484 int entry_index = GetLastCommittedEntryIndex();
Nate Chapind1fe3612021-04-16 20:45:574485 int64_t pending_item_sequence_number = 0;
Nate Chapin63db0d12022-01-20 22:03:304486 int64_t pending_document_sequence_number = 0;
Nate Chapine82339d02022-05-03 23:48:254487 bool will_create_new_entry = false;
4488 if (GetPendingEntryIndex() != -1) {
4489 entry_index = GetPendingEntryIndex();
4490 if (auto* frame_entry = GetPendingEntry()->GetFrameEntry(node)) {
4491 pending_item_sequence_number = frame_entry->item_sequence_number();
4492 pending_document_sequence_number =
4493 frame_entry->document_sequence_number();
4494 }
4495 } else if (request &&
4496 !NavigationTypeUtils::IsReload(
4497 request->common_params().navigation_type) &&
4498 !NavigationTypeUtils::IsHistory(
4499 request->common_params().navigation_type) &&
4500 !request->common_params().should_replace_current_entry &&
4501 !request->common_params()
4502 .is_history_navigation_in_new_child_frame) {
4503 will_create_new_entry = true;
4504 entry_index = GetLastCommittedEntryIndex() + 1;
4505 // Don't set pending_item_sequence_number or
4506 // pending_document_sequence_number in this case - a new unique isn/dsn will
4507 // be calculated in the renderer later.
4508 } else if (GetLastCommittedEntryIndex() != -1) {
4509 entry_index = GetLastCommittedEntryIndex();
4510 if (auto* frame_entry = GetLastCommittedEntry()->GetFrameEntry(node)) {
Nate Chapind1fe3612021-04-16 20:45:574511 pending_item_sequence_number = frame_entry->item_sequence_number();
Nate Chapin63db0d12022-01-20 22:03:304512 pending_document_sequence_number =
4513 frame_entry->document_sequence_number();
4514 }
Nate Chapind1fe3612021-04-16 20:45:574515 }
4516
Domenic Denicolacd30f5f82022-03-16 21:48:014517 auto entry_arrays = blink::mojom::NavigationApiHistoryEntryArrays::New();
Nate Chapine82339d02022-05-03 23:48:254518 if (entry_index == -1) {
4519 // TODO(rakina): Exit early when there is no last committed entry.
4520 // Remove when InitialNavigationEntry ships.
4521 return entry_arrays;
4522 }
4523
Domenic Denicolacd30f5f82022-03-16 21:48:014524 entry_arrays->back_entries = PopulateSingleNavigationApiHistoryEntryVector(
Nate Chapind1fe3612021-04-16 20:45:574525 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
Nate Chapin4e657a472022-02-06 03:38:374526 pending_item_sequence_number, pending_document_sequence_number);
Nate Chapind1fe3612021-04-16 20:45:574527
4528 // Don't populate forward entries if they will be truncated by a new entry.
4529 if (!will_create_new_entry) {
Domenic Denicolacd30f5f82022-03-16 21:48:014530 entry_arrays->forward_entries =
4531 PopulateSingleNavigationApiHistoryEntryVector(
4532 Direction::kForward, entry_index, pending_origin, node,
4533 site_instance.get(), pending_item_sequence_number,
4534 pending_document_sequence_number);
Nate Chapind1fe3612021-04-16 20:45:574535 }
Nate Chapin4e657a472022-02-06 03:38:374536 return entry_arrays;
Nate Chapind1fe3612021-04-16 20:45:574537}
4538
Nate Chapinfbfe5af2021-06-10 17:22:084539NavigationControllerImpl::HistoryNavigationAction
Domenic Denicolacc094fb2022-03-16 23:40:574540NavigationControllerImpl::ShouldNavigateToEntryForNavigationApiKey(
Nate Chapinfbfe5af2021-06-10 17:22:084541 FrameNavigationEntry* current_entry,
4542 FrameNavigationEntry* target_entry,
Domenic Denicolacc094fb2022-03-16 23:40:574543 const std::string& navigation_api_key) {
Nate Chapinfbfe5af2021-06-10 17:22:084544 if (!target_entry || !target_entry->committed_origin())
4545 return HistoryNavigationAction::kStopLooking;
4546 if (current_entry->site_instance() != target_entry->site_instance())
4547 return HistoryNavigationAction::kStopLooking;
4548 if (!current_entry->committed_origin()->IsSameOriginWith(
4549 *target_entry->committed_origin())) {
4550 return HistoryNavigationAction::kStopLooking;
4551 }
4552
4553 // NOTE: We don't actually care between kSameDocument and
4554 // kDifferentDocument, so always use kDifferentDocument by convention.
Domenic Denicolacc094fb2022-03-16 23:40:574555 if (target_entry->navigation_api_key() == navigation_api_key)
Nate Chapinfbfe5af2021-06-10 17:22:084556 return HistoryNavigationAction::kDifferentDocument;
4557 return HistoryNavigationAction::kKeepLooking;
4558}
4559
Domenic Denicolacc094fb2022-03-16 23:40:574560void NavigationControllerImpl::NavigateToNavigationApiKey(
4561 FrameTreeNode* node,
Nate Chapinc39f43a2022-04-01 00:16:254562 int sandboxed_source_frame_tree_node_id,
Domenic Denicolacc094fb2022-03-16 23:40:574563 const std::string& key) {
Nate Chapinfbfe5af2021-06-10 17:22:084564 FrameNavigationEntry* current_entry =
4565 GetLastCommittedEntry()->GetFrameEntry(node);
4566 if (!current_entry)
4567 return;
4568
4569 // We want to find the nearest matching entry that is contiguously
4570 // same-instance and same-origin. Check back first, then forward.
4571 // TODO(japhet): Link spec here once it exists.
4572 for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) {
Domenic Denicolacc094fb2022-03-16 23:40:574573 auto result = ShouldNavigateToEntryForNavigationApiKey(
Nate Chapinfbfe5af2021-06-10 17:22:084574 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4575 if (result == HistoryNavigationAction::kStopLooking)
4576 break;
4577 if (result != HistoryNavigationAction::kKeepLooking) {
Nate Chapinc39f43a2022-04-01 00:16:254578 GoToIndex(i, sandboxed_source_frame_tree_node_id,
Nate Chapin2892b37c2021-10-08 17:33:114579 false /* is_browser_initiated*/);
Nate Chapinfbfe5af2021-06-10 17:22:084580 return;
4581 }
4582 }
4583 for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) {
Domenic Denicolacc094fb2022-03-16 23:40:574584 auto result = ShouldNavigateToEntryForNavigationApiKey(
Nate Chapinfbfe5af2021-06-10 17:22:084585 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4586 if (result == HistoryNavigationAction::kStopLooking)
4587 break;
4588 if (result != HistoryNavigationAction::kKeepLooking) {
Nate Chapinc39f43a2022-04-01 00:16:254589 GoToIndex(i, sandboxed_source_frame_tree_node_id,
Nate Chapin2892b37c2021-10-08 17:33:114590 false /* is_browser_initiated*/);
Nate Chapinfbfe5af2021-06-10 17:22:084591 return;
4592 }
4593 }
4594}
4595
Domenic Denicolacc094fb2022-03-16 23:40:574596bool NavigationControllerImpl::ShouldProtectUrlInNavigationApi(
Nate Chapin63db0d12022-01-20 22:03:304597 network::mojom::ReferrerPolicy referrer_policy) {
4598 return referrer_policy == network::mojom::ReferrerPolicy::kNever ||
4599 referrer_policy == network::mojom::ReferrerPolicy::kOrigin;
4600}
4601
shivanigithubf405bf0d2021-11-05 17:58:334602bool NavigationControllerImpl::ShouldMaintainTrivialSessionHistory(
4603 const FrameTreeNode* frame_tree_node) const {
4604 // TODO(https://crbug.com/1197384): We may have to add portals in addition to
4605 // prerender and fenced frames. This should be kept in sync with
Hayato Ito7a80db42021-07-05 06:18:544606 // LocalFrame version, LocalFrame::ShouldMaintainTrivialSessionHistory.
shivanigithubf405bf0d2021-11-05 17:58:334607 return frame_tree_.is_prerendering() ||
4608 frame_tree_node->IsInFencedFrameTree();
Hayato Ito7a80db42021-07-05 06:18:544609}
4610
[email protected]8ff00d72012-10-23 19:12:214611} // namespace content