blob: 64fc5d38dd6e047610a3cecaeed360aaab359ad8 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2013 The Chromium Authors
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]eabfe1912014-05-12 10:07:2841#include "base/command_line.h"
Ayu Ishii2f825852022-03-08 19:47:3842#include "base/containers/adapters.h"
Lei Zhang302bc3fa2025-06-04 20:10:3443#include "base/debug/crash_logging.h"
Rakina Zata Amni627360d2022-02-24 00:53:4044#include "base/debug/dump_without_crashing.h"
Avi Drissmanadac21992023-01-11 23:46:3945#include "base/functional/bind.h"
initial.commit09911bf2008-07-26 23:55:2946#include "base/logging.h"
asvitkine30330812016-08-30 04:01:0847#include "base/metrics/histogram_macros.h"
Kevin McNee37a109792025-04-14 21:16:3848#include "base/not_fatal_until.h"
Charlie Reis23c26da2022-01-29 00:57:4749#include "base/numerics/safe_conversions.h"
Ryan Hamilton7f3bd3d2022-04-23 00:07:3950#include "base/strings/escape.h"
[email protected]348fbaac2013-06-11 06:31:5151#include "base/strings/string_util.h"
Daniel Cheng17390fd2025-06-07 06:38:2652#include "base/strings/string_view_util.h"
[email protected]74ebfb12013-06-07 20:48:0053#include "base/strings/utf_string_conversions.h"
[email protected]a43858f2013-06-28 15:18:3754#include "base/time/time.h"
Carlos Caballero40b0efd2021-01-26 11:55:0055#include "base/trace_event/optional_trace_event.h"
ssid3e765612015-01-28 04:03:4256#include "base/trace_event/trace_event.h"
Daniel Chengabb006862022-09-09 22:39:0857#include "base/types/optional_util.h"
servolkf3955532015-05-16 00:01:5958#include "build/build_config.h"
[email protected]eabfe1912014-05-12 10:07:2859#include "cc/base/switches.h"
jamescookda2505812015-03-20 18:01:1860#include "content/browser/bad_message.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3961#include "content/browser/blob_storage/chrome_blob_storage_context.h"
William Liu055a3542023-04-02 17:21:1962#include "content/browser/browser_context_impl.h"
[email protected]825b1662012-03-12 19:07:3163#include "content/browser/browser_url_handler_impl.h"
[email protected]5f2aa722013-08-07 16:59:4164#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0465#include "content/browser/dom_storage/session_storage_namespace_impl.h"
Nate Chapinc06cf80a2023-11-18 01:12:2566#include "content/browser/preloading/prerender/prerender_host.h"
Sharon Yang242ef822023-05-15 21:07:3267#include "content/browser/process_lock.h"
Mingyu Lei68f34412023-08-21 07:31:3768#include "content/browser/renderer_host/back_forward_cache_impl.h"
danakjc492bf82020-09-09 20:02:4469#include "content/browser/renderer_host/debug_urls.h"
70#include "content/browser/renderer_host/frame_tree.h"
Carlos Caballero40b0efd2021-01-26 11:55:0071#include "content/browser/renderer_host/frame_tree_node.h"
Chris Bookholt27faf8d2022-01-20 01:03:3372#include "content/browser/renderer_host/navigation_controller_delegate.h"
danakjc492bf82020-09-09 20:02:4473#include "content/browser/renderer_host/navigation_entry_impl.h"
Nate Chapin214a86a2021-06-21 20:35:5774#include "content/browser/renderer_host/navigation_entry_restore_context_impl.h"
danakjc492bf82020-09-09 20:02:4475#include "content/browser/renderer_host/navigation_request.h"
William Liu055a3542023-04-02 17:21:1976#include "content/browser/renderer_host/navigation_transitions/navigation_entry_screenshot_cache.h"
77#include "content/browser/renderer_host/navigation_transitions/navigation_entry_screenshot_manager.h"
Khushal Sagar94ea2dc2024-08-06 17:15:2678#include "content/browser/renderer_host/navigation_transitions/navigation_transition_config.h"
danakjc492bf82020-09-09 20:02:4479#include "content/browser/renderer_host/navigator.h"
Takashi Toyoshima4dad2c12023-11-13 10:04:4680#include "content/browser/renderer_host/page_delegate.h"
danakjc492bf82020-09-09 20:02:4481#include "content/browser/renderer_host/render_frame_host_delegate.h"
Carlos Caballero40b0efd2021-01-26 11:55:0082#include "content/browser/renderer_host/render_view_host_impl.h"
Mike Jacksone2aa7af2023-05-17 06:45:0783#include "content/browser/renderer_host/system_entropy_utils.h"
Sharon Yangd70a5392021-10-26 23:06:3284#include "content/browser/site_info.h"
[email protected]b6583592012-01-25 19:52:3385#include "content/browser/site_instance_impl.h"
Camille Lamy5193caa2018-10-12 11:59:4286#include "content/common/content_constants_internal.h"
Rakina Zata Amni84a859df2024-06-07 15:56:3287#include "content/common/content_navigation_policy.h"
Nate Chapind1fe3612021-04-16 20:45:5788#include "content/common/navigation_params_utils.h"
Nasko Oskovae49e292020-08-13 02:08:5189#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1190#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1991#include "content/public/browser/content_browser_client.h"
Fergal Daly1336ac642021-09-14 15:13:1192#include "content/public/browser/disallow_activation_reason.h"
[email protected]d9083482012-01-06 00:38:4693#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0094#include "content/public/browser/navigation_details.h"
Lei Zhang96031532019-10-10 19:05:4795#include "content/public/browser/render_view_host.h"
[email protected]9677a3c2012-12-22 04:18:5896#include "content/public/browser/render_widget_host.h"
97#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1098#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3499#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:19100#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:38101#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:10102#include "content/public/common/content_features.h"
Lei Zhang96031532019-10-10 19:05:47103#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:43104#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:59105#include "media/base/mime_util.h"
Lei Zhanga4770832023-07-19 18:02:36106#include "net/base/net_errors.h"
Arthur Sonzogni620cec62018-12-13 13:08:57107#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:18108#include "services/metrics/public/cpp/ukm_builders.h"
109#include "services/metrics/public/cpp/ukm_recorder.h"
Yue Ru Sun128804932020-09-30 22:19:17110#include "services/metrics/public/cpp/ukm_source_id.h"
Nan Lind91c8152021-10-21 16:22:37111#include "services/network/public/mojom/fetch_api.mojom.h"
William Liu2c825472022-10-31 12:01:44112#include "services/network/public/mojom/url_response_head.mojom-shared.h"
[email protected]9677a3c2012-12-22 04:18:58113#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:39114#include "third_party/blink/public/common/blob/blob_utils.h"
Gyuyoung Kim107c2a02021-04-13 01:49:30115#include "third_party/blink/public/common/chrome_debug_urls.h"
Miyoung Shin1c565c912021-03-17 12:11:21116#include "third_party/blink/public/common/history/session_history_constants.h"
Blink Reformata30d4232018-04-07 15:31:06117#include "third_party/blink/public/common/mime_util/mime_util.h"
Minggang Wanga13c796e2021-07-02 05:54:43118#include "third_party/blink/public/common/navigation/navigation_params.h"
Carlos Caballeroede6f8c2021-01-28 11:01:50119#include "third_party/blink/public/common/page_state/page_state_serialization.h"
Minggang Wangb9f3fa92021-07-01 15:30:31120#include "third_party/blink/public/mojom/navigation/navigation_params.mojom.h"
Minggang Wang7ee0c742021-06-16 16:16:51121#include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h"
Jiewei Qian87951f52023-10-17 01:33:46122#include "third_party/blink/public/mojom/runtime_feature_state/runtime_feature.mojom.h"
[email protected]cca6f392014-05-28 21:32:26123#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:29124
[email protected]8ff00d72012-10-23 19:12:21125namespace content {
[email protected]e9ba4472008-09-14 15:42:43126namespace {
127
Charlie Reis655c8f02025-05-15 01:16:17128// Feature to skip a redundant NavigationRequest creation for bfcache
129// activations, per https://crbug.com/417251428.
Charlie Reis3696ab62025-05-28 07:02:30130// TODO(crbug.com/420275259): Diagnose crashes and enable by default.
Charlie Reis655c8f02025-05-15 01:16:17131BASE_FEATURE(kSkipExtraBfcacheNavigationRequest,
132 "SkipExtraBfcacheNavigationRequest",
Charlie Reis3696ab62025-05-28 07:02:30133 base::FEATURE_DISABLED_BY_DEFAULT);
Charlie Reis655c8f02025-05-15 01:16:17134
Diana Qu21d0c922025-06-25 17:19:08135// Enables a CHECK in RendererDidNavigate to ensure that session
136// history navigations commit in the expected SiteInstance when the
137// document sequence number matches. Helps detect navigation process
138// mismatches and potential security issues.
139BASE_FEATURE(kCheckSiteInstanceOnHistoryNavigation,
140 "CheckSiteInstanceOnHistoryNavigation",
141 base::FEATURE_ENABLED_BY_DEFAULT);
142
[email protected]e9ba4472008-09-14 15:42:43143// Invoked when entries have been pruned, or removed. For example, if the
144// current entries are [google, digg, yahoo], with the current entry google,
145// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47146void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50147 int index,
[email protected]c12bf1a12008-09-17 16:28:49148 int count) {
[email protected]8ff00d72012-10-23 19:12:21149 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50150 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49151 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14152 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43153}
154
[email protected]e9ba4472008-09-14 15:42:43155// Configure all the NavigationEntries in entries for restore. This resets
156// the transition type to reload and makes sure the content state isn't empty.
157void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57158 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48159 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47160 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43161 // Use a transition type of reload so that we don't incorrectly increase
162 // the typed count.
Lei Zhang96031532019-10-10 19:05:47163 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
164 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43165 }
166}
167
[email protected]bf70edce2012-06-20 22:32:22168// Determines whether or not we should be carrying over a user agent override
169// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57170bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22171 return last_entry && last_entry->GetIsOverridingUserAgent();
172}
173
Camille Lamy5193caa2018-10-12 11:59:42174// Determines whether to override user agent for a navigation.
175bool ShouldOverrideUserAgent(
176 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57177 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42178 switch (override_user_agent) {
179 case NavigationController::UA_OVERRIDE_INHERIT:
180 return ShouldKeepOverride(last_committed_entry);
181 case NavigationController::UA_OVERRIDE_TRUE:
182 return true;
183 case NavigationController::UA_OVERRIDE_FALSE:
184 return false;
Camille Lamy5193caa2018-10-12 11:59:42185 }
Peter Boströmfc7ddc182024-10-31 19:37:21186 NOTREACHED();
Camille Lamy5193caa2018-10-12 11:59:42187}
188
Rakina Zata Amni312822d72021-06-04 16:13:37189// Returns true if this navigation should be treated as a reload. For e.g.
Liang Zhao0351d762024-11-13 00:10:07190// clicking on a link which results in a navigation to the last committed URL
191// (but wasn't converted to do a replacement navigation in the renderer), etc.
192// This intentionally excludes navigating to the last committed URL via the
193// address bar, so that the current scroll position is not restored.
Fergal Daly766177d2020-07-07 07:54:04194// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
195// |base_url_for_data_url|, |transition_type| correspond to the new navigation
196// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
197// navigation that committed.
198bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
199 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57200 const GURL& virtual_url,
201 const GURL& base_url_for_data_url,
202 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57203 bool is_post,
Rakina Zata Amni312822d72021-06-04 16:13:37204 bool should_replace_current_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57205 NavigationEntryImpl* last_committed_entry) {
Rakina Zata Amni312822d72021-06-04 16:13:37206 // Navigations intended to do a replacement shouldn't be converted to do a
207 // reload.
208 if (should_replace_current_entry)
clamy0a656e42018-02-06 18:18:28209 return false;
clamy0a656e42018-02-06 18:18:28210 // Only convert to reload if at least one navigation committed.
Rakina Zata Amnie2d31312022-11-18 03:38:45211 if (last_committed_entry->IsInitialEntry())
ananta3bdd8ae2016-12-22 17:11:55212 return false;
213
arthursonzogni7a8243682017-12-14 16:41:42214 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28215 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42216 return false;
217
Liang Zhao0351d762024-11-13 00:10:07218 // Same URL navigations from the address bar should only be treated as reloads
219 // if PAGE_TRANSITION_RELOAD is set (not for PAGE_TRANSITION_TYPED or
220 // PAGE_TRANSITION_LINK). In non-address-bar cases, PAGE_TRANSITION_TYPED
221 // and PAGE_TRANSITION_LINK can be treated as reloads.
clamy0a656e42018-02-06 18:18:28222 bool transition_type_can_be_converted = false;
Liang Zhao0351d762024-11-13 00:10:07223 if (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR) {
224 if (ui::PageTransitionCoreTypeIs(transition_type,
225 ui::PAGE_TRANSITION_RELOAD)) {
226 transition_type_can_be_converted = true;
227 }
228 } else {
229 if (ui::PageTransitionCoreTypeIs(transition_type,
230 ui::PAGE_TRANSITION_TYPED) ||
231 ui::PageTransitionCoreTypeIs(transition_type,
232 ui::PAGE_TRANSITION_LINK)) {
233 transition_type_can_be_converted = true;
234 }
ananta3bdd8ae2016-12-22 17:11:55235 }
clamy0a656e42018-02-06 18:18:28236 if (!transition_type_can_be_converted)
237 return false;
238
239 // This check is required for cases like view-source:, etc. Here the URL of
240 // the navigation entry would contain the url of the page, while the virtual
241 // URL contains the full URL including the view-source prefix.
242 if (virtual_url != last_committed_entry->GetVirtualURL())
243 return false;
244
Fergal Daly766177d2020-07-07 07:54:04245 // Check that the URLs match.
246 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
247 // If there's no frame entry then by definition the URLs don't match.
248 if (!frame_entry)
249 return false;
250
251 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28252 return false;
253
254 // This check is required for Android WebView loadDataWithBaseURL. Apps
255 // can pass in anything in the base URL and we need to ensure that these
256 // match before classifying it as a reload.
257 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
258 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
259 return false;
ananta3bdd8ae2016-12-22 17:11:55260 }
261
clamy0a656e42018-02-06 18:18:28262 // Skip entries with SSL errors.
263 if (last_committed_entry->ssl_error())
264 return false;
265
266 // Don't convert to a reload when the last navigation was a POST or the new
267 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04268 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28269 return false;
270
271 return true;
ananta3bdd8ae2016-12-22 17:11:55272}
273
Arthur Sonzognic686e8f2024-01-11 08:36:37274std::optional<url::Origin> GetCommittedOriginForFrameEntry(
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12275 const mojom::DidCommitProvisionalLoadParams& params,
276 NavigationRequest* request) {
Nasko Oskov03912102019-01-11 00:21:32277 // Error pages commit in an opaque origin, yet have the real URL that resulted
278 // in an error as the |params.url|. Since successful reload of an error page
279 // should commit in the correct origin, setting the opaque origin on the
280 // FrameNavigationEntry will be incorrect.
Rakina Zata Amniafd3c6582021-11-30 06:19:17281 if (request && request->DidEncounterError())
Arthur Sonzognic686e8f2024-01-11 08:36:37282 return std::nullopt;
Nasko Oskov03912102019-01-11 00:21:32283
Arthur Sonzognic686e8f2024-01-11 08:36:37284 return std::make_optional(params.origin);
Nasko Oskov03912102019-01-11 00:21:32285}
286
Alex Moshchuk99242832023-05-22 17:21:44287bool IsValidURLForNavigation(FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:42288 const GURL& virtual_url,
289 const GURL& dest_url) {
290 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
Alex Moshchuk99242832023-05-22 17:21:44291 if (node->IsOutermostMainFrame() && !virtual_url.is_valid() &&
292 !virtual_url.is_empty()) {
Camille Lamy5193caa2018-10-12 11:59:42293 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
294 << virtual_url.possibly_invalid_spec();
295 return false;
296 }
297
298 // Don't attempt to navigate to non-empty invalid URLs.
299 if (!dest_url.is_valid() && !dest_url.is_empty()) {
300 LOG(WARNING) << "Refusing to load invalid URL: "
301 << dest_url.possibly_invalid_spec();
302 return false;
303 }
304
305 // The renderer will reject IPC messages with URLs longer than
306 // this limit, so don't attempt to navigate with a longer URL.
307 if (dest_url.spec().size() > url::kMaxURLChars) {
308 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
309 << " characters.";
310 return false;
311 }
312
Aaron Colwell33109c592020-04-21 21:31:19313 // Reject renderer debug URLs because they should have been handled before
314 // we get to this point. This check handles renderer debug URLs
315 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
316 // provides defense-in-depth if a renderer debug URL manages to get here via
317 // some other path. We want to reject the navigation here so it doesn't
318 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30319 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19320 LOG(WARNING) << "Refusing to load renderer debug URL: "
321 << dest_url.possibly_invalid_spec();
322 return false;
323 }
324
Alex Moshchuk99242832023-05-22 17:21:44325 // Guests only support navigations to known-safe schemes. This check already
326 // exists in the extensions layer, where it also dispatches proper events to
327 // the guest's embedder (see WebViewGuest::LoadURLWithParams). This check is
328 // for defense-in-depth to ensure that no other places in the codebase
329 // accidentally navigate guests to schemes such as WebUI, which is not
330 // supported. See https://crbug.com/1444221.
331 if (node->current_frame_host()->GetSiteInstance()->IsGuest()) {
332 auto* cpsp = content::ChildProcessSecurityPolicy::GetInstance();
333 if (!cpsp->IsWebSafeScheme(dest_url.scheme()) &&
334 !dest_url.SchemeIs(url::kAboutScheme)) {
335 LOG(WARNING) << "Refusing to load unsafe URL in a guest: "
336 << dest_url.possibly_invalid_spec();
337 return false;
338 }
339 }
340
Camille Lamy5193caa2018-10-12 11:59:42341 return true;
342}
343
Mikel Astizba9cf2fd2017-12-17 10:38:10344// See replaced_navigation_entry_data.h for details: this information is meant
345// to ensure |*output_entry| keeps track of its original URL (landing page in
346// case of server redirects) as it gets replaced (e.g. history.replaceState()),
347// without overwriting it later, for main frames.
348void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57349 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10350 NavigationEntryImpl* output_entry) {
Rakina Zata Amniafd3c6582021-11-30 06:19:17351 if (output_entry->GetReplacedEntryData().has_value() ||
352 replaced_entry->IsInitialEntry()) {
Mikel Astizba9cf2fd2017-12-17 10:38:10353 return;
Rakina Zata Amniafd3c6582021-11-30 06:19:17354 }
Mikel Astizba9cf2fd2017-12-17 10:38:10355
356 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57357 data.first_committed_url = replaced_entry->GetURL();
358 data.first_timestamp = replaced_entry->GetTimestamp();
359 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29360 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10361}
362
Minggang Wangb9f3fa92021-07-01 15:30:31363blink::mojom::NavigationType GetNavigationType(
364 const GURL& old_url,
365 const GURL& new_url,
366 ReloadType reload_type,
367 NavigationEntryImpl* entry,
Charlie Reisf21cd182024-08-09 21:44:52368 FrameNavigationEntry* frame_entry,
Minggang Wangb9f3fa92021-07-01 15:30:31369 bool has_pending_cross_document_commit,
370 bool is_currently_error_page,
Garrett Tanzer405f3402022-07-21 20:12:49371 bool is_same_document_history_load,
Garrett Tanzer267c2b82022-07-26 16:53:13372 bool is_embedder_initiated_fenced_frame_navigation,
Garrett Tanzer405f3402022-07-21 20:12:49373 bool is_unfenced_top_navigation) {
clamyea99ea12018-05-28 13:54:23374 // Reload navigations
375 switch (reload_type) {
376 case ReloadType::NORMAL:
Minggang Wangb9f3fa92021-07-01 15:30:31377 return blink::mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23378 case ReloadType::BYPASSING_CACHE:
Minggang Wangb9f3fa92021-07-01 15:30:31379 return blink::mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23380 case ReloadType::NONE:
381 break; // Fall through to rest of function.
382 }
383
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08384 if (entry->IsRestored()) {
Minggang Wangb9f3fa92021-07-01 15:30:31385 return entry->GetHasPostData()
386 ? blink::mojom::NavigationType::RESTORE_WITH_POST
387 : blink::mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23388 }
389
danakjb952ef12021-01-14 19:58:49390 const bool can_be_same_document =
391 // A pending cross-document commit means this navigation will not occur in
392 // the current document, as that document would end up being replaced in
393 // the meantime.
394 !has_pending_cross_document_commit &&
395 // If the current document is an error page, we should always treat it as
396 // a different-document navigation so that we'll attempt to load the
397 // document we're navigating to (and not stay in the current error page).
Garrett Tanzer405f3402022-07-21 20:12:49398 !is_currently_error_page &&
Garrett Tanzer267c2b82022-07-26 16:53:13399 // If the navigation is an embedder-initiated navigation of a fenced
400 // frame root (i.e. enters a fenced frame tree from outside),
401 // same-document navigations should be disabled because we don't want to
402 // allow information to be joined across multiple embedder-initiated
403 // fenced frame navigations (which may contain separate cross-site data).
404 !is_embedder_initiated_fenced_frame_navigation &&
Garrett Tanzer405f3402022-07-21 20:12:49405 // If the navigation is to _unfencedTop (i.e. escapes a fenced frame),
406 // same-document navigations should be disabled because we want to force
407 // the creation of a new browsing context group.
408 !is_unfenced_top_navigation;
danakjd83d706d2020-11-25 22:11:12409
clamyea99ea12018-05-28 13:54:23410 // History navigations.
Charlie Reisf21cd182024-08-09 21:44:52411 if (frame_entry && frame_entry->page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12412 return can_be_same_document && is_same_document_history_load
Minggang Wangb9f3fa92021-07-01 15:30:31413 ? blink::mojom::NavigationType::HISTORY_SAME_DOCUMENT
414 : blink::mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23415 }
416 DCHECK(!is_same_document_history_load);
417
418 // A same-document fragment-navigation happens when the only part of the url
419 // that is modified is after the '#' character.
420 //
421 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12422 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23423 //
424 // Note: this check is only valid for navigations that are not history
425 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
426 // history navigation from 'A#foo' to 'A#bar' is not a same-document
427 // navigation, but a different-document one. This is why history navigation
428 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47429 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
Charlie Reisf21cd182024-08-09 21:44:52430 frame_entry && frame_entry->method() == "GET";
danakjd83d706d2020-11-25 22:11:12431
432 // The one case where we do the wrong thing here and incorrectly choose
433 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
434 // the renderer is a frameset. All frameset navigations should be
435 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
436 // navigation would do the right thing, as it would send it to the browser and
437 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
438 // into this method). But since we can't tell that case here for browser-
439 // initiated navigations, we have to get the renderer involved. In that case
440 // the navigation would be restarted due to the renderer spending a reply of
441 // mojom::CommitResult::RestartCrossDocument.
442
443 return can_be_same_document && is_same_doc
Minggang Wangb9f3fa92021-07-01 15:30:31444 ? blink::mojom::NavigationType::SAME_DOCUMENT
445 : blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23446}
447
Camille Lamy5193caa2018-10-12 11:59:42448// Adjusts the original input URL if needed, to get the URL to actually load and
449// the virtual URL, which may differ.
450void RewriteUrlForNavigation(const GURL& original_url,
451 BrowserContext* browser_context,
452 GURL* url_to_load,
453 GURL* virtual_url,
454 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42455 // Allow the browser URL handler to rewrite the URL. This will, for example,
456 // remove "view-source:" from the beginning of the URL to get the URL that
457 // will actually be loaded. This real URL won't be shown to the user, just
458 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31459 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42460 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
461 url_to_load, browser_context, reverse_on_redirect);
462}
463
464#if DCHECK_IS_ON()
465// Helper sanity check function used in debug mode.
466void ValidateRequestMatchesEntry(NavigationRequest* request,
467 NavigationEntryImpl* entry) {
468 if (request->frame_tree_node()->IsMainFrame()) {
469 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
470 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
Minggang Wangb9f3fa92021-07-01 15:30:31471 ui::PageTransitionFromInt(request->common_params().transition),
472 entry->GetTransitionType()));
Camille Lamy5193caa2018-10-12 11:59:42473 }
Nasko Oskovc36327d2019-01-03 23:23:04474 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42475 entry->should_clear_history_list());
476 DCHECK_EQ(request->common_params().has_user_gesture,
477 entry->has_user_gesture());
478 DCHECK_EQ(request->common_params().base_url_for_data_url,
479 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04480 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42481 entry->GetCanLoadLocalResources());
482 DCHECK_EQ(request->common_params().started_from_context_menu,
483 entry->has_started_from_context_menu());
484
485 FrameNavigationEntry* frame_entry =
486 entry->GetFrameEntry(request->frame_tree_node());
487 if (!frame_entry) {
Charlie Reisf21cd182024-08-09 21:44:52488 // TODO(https://crbug.com/40467594): This case should be unreachable once
489 // all subframes have FrameNavigationEntries associated with them.
Camille Lamy5193caa2018-10-12 11:59:42490 return;
491 }
492
Camille Lamy5193caa2018-10-12 11:59:42493 DCHECK_EQ(request->common_params().method, frame_entry->method());
494
Nasko Oskovc36327d2019-01-03 23:23:04495 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42496 if (redirect_size == frame_entry->redirect_chain().size()) {
497 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04498 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42499 frame_entry->redirect_chain()[i]);
500 }
501 } else {
Peter Boströmfc7ddc182024-10-31 19:37:21502 NOTREACHED();
Camille Lamy5193caa2018-10-12 11:59:42503 }
504}
505#endif // DCHECK_IS_ON()
506
Dave Tapuska8bfd84c2019-03-26 20:47:16507// Returns whether the session history NavigationRequests in |navigations|
Nate Chapinbf682fa32022-09-26 22:41:20508// would stay within the subtree of |sandboxed_initiator_rfh|.
Dave Tapuska8bfd84c2019-03-26 20:47:16509bool DoesSandboxNavigationStayWithinSubtree(
Nate Chapinbf682fa32022-09-26 22:41:20510 RenderFrameHostImpl* sandboxed_initiator_rfh,
Dave Tapuska8bfd84c2019-03-26 20:47:16511 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
Nate Chapinbf682fa32022-09-26 22:41:20512 DCHECK(sandboxed_initiator_rfh);
513 DCHECK(sandboxed_initiator_rfh->IsSandboxed(
514 network::mojom::WebSandboxFlags::kTopNavigation));
Dave Tapuska8bfd84c2019-03-26 20:47:16515 for (auto& item : navigations) {
516 bool within_subtree = false;
517 // Check whether this NavigationRequest affects a frame within the
518 // sandboxed frame's subtree by walking up the tree looking for the
519 // sandboxed frame.
520 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03521 frame = FrameTreeNode::From(frame->parent())) {
Nate Chapinbf682fa32022-09-26 22:41:20522 if (frame == sandboxed_initiator_rfh->frame_tree_node()) {
Dave Tapuska8bfd84c2019-03-26 20:47:16523 within_subtree = true;
524 break;
525 }
526 }
527 if (!within_subtree)
528 return false;
529 }
530 return true;
531}
532
William Liu122754942024-01-18 22:34:39533// Used for "Navigation.SessionHistoryCount" histogram.
534enum class HistoryNavTypeForHistogram {
535 // A same-doc or cross-doc navigation of the main frame. We can only have one
536 // main frame request per `NavigationController::GoToIndex()`.
537 kMainFrame = 0,
538 // `NavigationController::GoToIndex()` creates one main frame request and
539 // one or more subframe requests.
540 kMainFrameAndSubframe,
541 // `NavigationController::GoToIndex()` creates one or more subframe requests.
542 kSubframe,
543 // Used for histogram boundary.
544 kCount
545};
546
547void CountRequests(
548 const std::vector<std::unique_ptr<NavigationRequest>>& requests,
549 int& mutable_main_frame_cnt,
550 int& mutable_subframe_cnt) {
551 for (const auto& req : requests) {
552 if (req->IsInPrimaryMainFrame()) {
553 // We can only have one main frame navigation at a time.
554 CHECK_EQ(mutable_main_frame_cnt, 0);
555 ++mutable_main_frame_cnt;
556 } else if (req->GetNavigatingFrameType() == FrameType::kSubframe) {
557 ++mutable_subframe_cnt;
558 }
559 }
560}
561
562// Record the number of different types of navigations as histograms. See
563// `HistoryNavTypeForHistogram` for the types.
564void CountBrowserInitiatedMainframeAndSubframeHistoryNavigaions(
565 const std::vector<std::unique_ptr<NavigationRequest>>& cross_doc_requests,
566 const std::vector<std::unique_ptr<NavigationRequest>>& same_doc_requests) {
567 // We must have one request.
568 CHECK(!cross_doc_requests.empty() || !same_doc_requests.empty());
569
570 int main_frame_cnt = 0;
571 int subframe_cnt = 0;
572 CountRequests(cross_doc_requests, main_frame_cnt, subframe_cnt);
573 CountRequests(same_doc_requests, main_frame_cnt, subframe_cnt);
574
575 std::optional<HistoryNavTypeForHistogram> history_nav_type;
576 if (main_frame_cnt > 0) {
577 if (subframe_cnt == 0) {
578 history_nav_type = HistoryNavTypeForHistogram::kMainFrame;
579 } else {
580 history_nav_type = HistoryNavTypeForHistogram::kMainFrameAndSubframe;
581 }
582 } else if (subframe_cnt > 0) {
583 history_nav_type = HistoryNavTypeForHistogram::kSubframe;
584 }
585 if (history_nav_type.has_value()) {
586 UMA_HISTOGRAM_ENUMERATION("Navigation.BrowserInitiatedSessionHistoryCount",
587 history_nav_type.value(),
588 HistoryNavTypeForHistogram::kCount);
589 }
590}
591
[email protected]e9ba4472008-09-14 15:42:43592} // namespace
593
arthursonzogni66f711c2019-10-08 14:40:36594// NavigationControllerImpl::PendingEntryRef------------------------------------
595
596NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
597 base::WeakPtr<NavigationControllerImpl> controller)
598 : controller_(controller) {}
599
600NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
601 if (!controller_) // Can be null with interstitials.
602 return;
603
604 controller_->PendingEntryRefDeleted(this);
605}
606
Charlie Reisd3e4fef2025-05-20 02:04:17607// NavigationControllerImpl::ScopedPendingEntryReentrancyGuard------------------
608
609NavigationControllerImpl::ScopedPendingEntryReentrancyGuard::
610 ScopedPendingEntryReentrancyGuard(
611 base::SafeRef<NavigationControllerImpl> controller)
612 : controller_(controller) {
613 // Navigations that involve pending entries do not support re-entrancy.
614 // Encountering this CHECK failure means the caller is attempting to start
615 // another navigation while a navigation to an existing pending entry is on
616 // the stack. In most cases, the new navigation should be done from a posted
617 // task instead. See http://crbug.com/40353566 for context.
618 CHECK(!controller->in_navigate_to_pending_entry_);
619
620 controller->in_navigate_to_pending_entry_ = true;
621 controller->CheckPotentialNavigationReentrancy();
622
623 // It must not be possible to delete the pending NavigationEntry while
624 // navigating to it. Grab a reference to delay potential deletion until
625 // this scoped object is deleted at the end of the relevant function call.
626 pending_entry_ref_ = controller->ReferencePendingEntry();
627}
628
629NavigationControllerImpl::ScopedPendingEntryReentrancyGuard::
630 ~ScopedPendingEntryReentrancyGuard() {
631 controller_->in_navigate_to_pending_entry_ = false;
632}
633
[email protected]d202a7c2012-01-04 07:53:47634// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29635
[email protected]23a918b2014-07-15 09:51:36636const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23637
[email protected]765b35502008-08-21 00:51:20638// static
[email protected]d202a7c2012-01-04 07:53:47639size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23640 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20641
[email protected]e6fec472013-05-14 05:29:02642// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20643// when testing.
644static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29645
[email protected]71fde352011-12-29 03:29:56646// static
dcheng9bfa5162016-04-09 01:00:57647std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
648 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10649 Referrer referrer,
Arthur Sonzognic686e8f2024-01-11 08:36:37650 std::optional<url::Origin> initiator_origin,
651 std::optional<GURL> initiator_base_url,
Lukasz Anforowicz641234d52019-11-07 21:07:10652 ui::PageTransition transition,
653 bool is_renderer_initiated,
654 const std::string& extra_headers,
655 BrowserContext* browser_context,
656 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
657 return NavigationControllerImpl::CreateNavigationEntry(
W. James MacLean23e90a12022-12-21 04:38:21658 url, referrer, std::move(initiator_origin), std::move(initiator_base_url),
Arthur Sonzognic686e8f2024-01-11 08:36:37659 std::nullopt /* source_process_site_url */, transition,
Sharon Yang242ef822023-05-15 21:07:32660 is_renderer_initiated, extra_headers, browser_context,
661 std::move(blob_url_loader_factory), true /* rewrite_virtual_urls */);
Lukasz Anforowicz641234d52019-11-07 21:07:10662}
663
664// static
665std::unique_ptr<NavigationEntryImpl>
666NavigationControllerImpl::CreateNavigationEntry(
667 const GURL& url,
668 Referrer referrer,
Arthur Sonzognic686e8f2024-01-11 08:36:37669 std::optional<url::Origin> initiator_origin,
670 std::optional<GURL> initiator_base_url,
671 std::optional<GURL> source_process_site_url,
dcheng9bfa5162016-04-09 01:00:57672 ui::PageTransition transition,
673 bool is_renderer_initiated,
674 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09675 BrowserContext* browser_context,
Julie Jeongeun Kim5b9aff72022-05-02 02:10:17676 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
677 bool rewrite_virtual_urls) {
678 GURL url_to_load = url;
679 GURL virtual_url = url;
[email protected]71fde352011-12-29 03:29:56680 bool reverse_on_redirect = false;
Julie Jeongeun Kim5b9aff72022-05-02 02:10:17681 if (rewrite_virtual_urls) {
682 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
683 &reverse_on_redirect);
684 }
Lukasz Anforowicz641234d52019-11-07 21:07:10685 // Let the NTP override the navigation params and pretend that this is a
686 // browser-initiated, bookmark-like navigation.
687 GetContentClient()->browser()->OverrideNavigationParams(
Sharon Yang242ef822023-05-15 21:07:32688 source_process_site_url, &transition, &is_renderer_initiated, &referrer,
Scott Violetcf6ea7e2021-06-09 21:09:21689 &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10690
Patrick Monettef507e982019-06-19 20:18:06691 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28692 nullptr, // The site instance for tabs is sent on navigation
693 // (WebContents::GetSiteInstance).
W. James MacLean23e90a12022-12-21 04:38:21694 url_to_load, referrer, initiator_origin, initiator_base_url,
695 std::u16string(), transition, is_renderer_initiated,
696 blob_url_loader_factory,
Rakina Zata Amniafd3c6582021-11-30 06:19:17697 /* is_initial_entry = */ false);
Camille Lamy5193caa2018-10-12 11:59:42698 entry->SetVirtualURL(virtual_url);
699 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56700 entry->set_update_virtual_url_with_url(reverse_on_redirect);
701 entry->set_extra_headers(extra_headers);
Patrick Monettef507e982019-06-19 20:18:06702 return entry;
[email protected]71fde352011-12-29 03:29:56703}
704
[email protected]cdcb1dee2012-01-04 00:46:20705// static
706void NavigationController::DisablePromptOnRepost() {
707 g_check_for_repost = false;
708}
709
[email protected]c5b88d82012-10-06 17:03:33710base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
711 base::Time t) {
712 // If |t| is between the water marks, we're in a run of duplicates
713 // or just getting out of it, so increase the high-water mark to get
714 // a time that probably hasn't been used before and return it.
715 if (low_water_mark_ <= t && t <= high_water_mark_) {
Peter Kastinge5a38ed2021-10-02 03:06:35716 high_water_mark_ += base::Microseconds(1);
[email protected]c5b88d82012-10-06 17:03:33717 return high_water_mark_;
718 }
719
720 // Otherwise, we're clear of the last duplicate run, so reset the
721 // water marks.
722 low_water_mark_ = high_water_mark_ = t;
723 return t;
724}
725
ckitagawa0faa5e42020-06-17 17:30:54726NavigationControllerImpl::ScopedShowRepostDialogForTesting::
727 ScopedShowRepostDialogForTesting()
728 : was_disallowed_(g_check_for_repost) {
729 g_check_for_repost = true;
730}
731
732NavigationControllerImpl::ScopedShowRepostDialogForTesting::
733 ~ScopedShowRepostDialogForTesting() {
734 g_check_for_repost = was_disallowed_;
735}
736
Nate Chapin9eb16be72022-09-23 22:54:31737NavigationControllerImpl::RemovedEntriesTracker::RemovedEntriesTracker(
738 base::SafeRef<NavigationControllerImpl> controller)
739 : controller_(controller) {
740 for (FrameTreeNode* frame_tree_node : controller_->frame_tree().Nodes()) {
741 names_to_nodes_.insert({frame_tree_node->unique_name(), frame_tree_node});
742 frame_tree_node_id_to_keys_.insert(
743 {frame_tree_node->frame_tree_node_id(), std::set<std::string>()});
744 if (auto* frame_entry = frame_tree_node->current_frame_host()
745 ->last_committed_frame_entry()) {
746 frame_tree_node_id_to_doc_seq_nos_.insert(
747 {frame_tree_node->frame_tree_node_id(),
748 frame_entry->document_sequence_number()});
749 }
750 }
751 PopulateKeySet(Direction::kBack);
752 PopulateKeySet(Direction::kForward);
753}
754
755void NavigationControllerImpl::RemovedEntriesTracker::PopulateKeySet(
756 Direction direction) {
757 // Keep track of which FrameTreeNodes may still have relevant API keys in
758 // additional FrameNavigationEntries.
759 std::set<FrameTreeNode*> nodes_to_process;
760 for (FrameTreeNode* node : controller_->frame_tree().Nodes()) {
761 nodes_to_process.insert(node);
762 }
763
764 const int offset = direction == Direction::kForward ? 1 : -1;
765 const int start = direction == Direction::kForward
766 ? controller_->GetLastCommittedEntryIndex()
767 : controller_->GetLastCommittedEntryIndex() - 1;
768 for (int i = start;
769 i >= 0 && i < controller_->GetEntryCount() && !nodes_to_process.empty();
770 i += offset) {
771 std::set<FrameTreeNode*> nodes_to_continue_processing;
772
773 NavigationEntryImpl* entry = controller_->GetEntryAtIndex(i);
774 entry->ForEachFrameEntry([this, &nodes_to_process,
775 &nodes_to_continue_processing,
776 &entry](FrameNavigationEntry* frame_entry) {
777 // Find the |node| that matches |frame_entry|, if any.
778 FrameTreeNode* node = nullptr;
779 if (frame_entry == entry->root_node()->frame_entry) {
780 node = controller_->frame_tree().root();
781 } else {
782 auto it = names_to_nodes_.find(frame_entry->frame_unique_name());
783 if (it == names_to_nodes_.end())
784 return;
785 node = it->second;
786 }
787
788 // Skip this node if a previous step determine there are no longer
789 // relevant navigation API keys in this direction.
790 if (nodes_to_process.find(node) == nodes_to_process.end())
791 return;
792
793 // Stop processing |node| if we reach a point where it's cross-origin.
794 // See also PopulateSingleNavigationApiHistoryEntryVector().
795 url::Origin frame_entry_origin =
796 frame_entry->committed_origin().value_or(url::Origin::Resolve(
797 frame_entry->url(),
798 frame_entry->initiator_origin().value_or(url::Origin())));
799 if (!node->current_origin().IsSameOriginWith(frame_entry_origin))
800 return;
801
802 frame_tree_node_id_to_keys_[node->frame_tree_node_id()].insert(
803 frame_entry->navigation_api_key());
804 // Mark |node| as needing more processing for the next entry.
805 nodes_to_continue_processing.insert(node);
806
807 // Check whether |node| went cross-document. If so, its children are
808 // no longer the same conceptual iframe as the current one, and
809 // should no longer be processed. This check is intentionally done
810 // after processing the current |node|, which may still have relevant
811 // discarded API keys.
812 if (frame_entry->document_sequence_number() !=
813 frame_tree_node_id_to_doc_seq_nos_[node->frame_tree_node_id()]) {
Arthur Sonzognif6785ec2022-12-05 10:11:50814 for (auto* descendant : node->frame_tree().SubtreeNodes(node))
Nate Chapin9eb16be72022-09-23 22:54:31815 nodes_to_process.erase(descendant);
816 }
817 });
818
819 nodes_to_process.swap(nodes_to_continue_processing);
820 }
821}
822
823NavigationControllerImpl::RemovedEntriesTracker::~RemovedEntriesTracker() {
824 std::set<std::string> all_keys;
825 // Find all remaining navigation API keys after some entries have been
826 // removed.
827 for (auto& entry : controller_->entries_) {
828 entry->ForEachFrameEntry([&all_keys](FrameNavigationEntry* frame_entry) {
829 all_keys.insert(frame_entry->navigation_api_key());
830 });
831 }
832
833 // Notify each frame in the renderer of any disposed navigation API keys.
834 for (auto& id_to_keys : frame_tree_node_id_to_keys_) {
835 std::vector<std::string> disposed_keys;
836 for (const auto& key : id_to_keys.second) {
837 if (all_keys.find(key) == all_keys.end())
838 disposed_keys.push_back(key);
839 }
840 if (disposed_keys.empty())
841 continue;
842
843 FrameTreeNode* node = controller_->frame_tree().FindByID(id_to_keys.first);
844 auto& frame = node->current_frame_host()->GetAssociatedLocalFrame();
845 frame->NotifyNavigationApiOfDisposedEntries(disposed_keys);
846 }
847}
848
[email protected]d202a7c2012-01-04 07:53:47849NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00850 BrowserContext* browser_context,
851 FrameTree& frame_tree,
852 NavigationControllerDelegate* delegate)
853 : frame_tree_(frame_tree),
854 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57855 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22856 ssl_manager_(this),
Mingyu Lei68f34412023-08-21 07:31:37857 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)),
858 back_forward_cache_(browser_context) {
[email protected]3d7474ff2011-07-27 17:47:37859 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29860}
861
[email protected]d202a7c2012-01-04 07:53:47862NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00863 // The NavigationControllerImpl might be called inside its delegate
864 // destructor. Calling it is not valid anymore.
865 delegate_ = nullptr;
866 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29867}
868
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57869BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55870 return browser_context_;
871}
872
[email protected]d202a7c2012-01-04 07:53:47873void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30874 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36875 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57876 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
Charlie Reis23c26da2022-01-29 00:57:47877 // Note that it's possible for `entries_` to contain multiple entries at this
878 // point, as Restore() might be called on a NavigationController that is
879 // already used (e.g. due to WebView's restoreState() API), not only for fresh
880 // NavigationControllers. These entries are not cleared to preserve legacy
881 // behavior and also because `pending_entry_` might point to one of the
882 // pre-existing entries. An exception for this is when `entries_` contains
883 // only the initial NavigationEntry, which must be removed.
884
885 // Do not proceed if selected_navigation will be out of bounds for the updated
886 // entries_ list, since it will be assigned to last_committed_entry_index_ and
887 // used to index entries_.
Alison Gale770f3fc2024-04-27 00:39:58888 // TODO(crbug.com/40816356): Consider also returning early if entries
Charlie Reis23c26da2022-01-29 00:57:47889 // is empty, since there should be no work to do (rather than marking the
890 // existing entries as needing reload). Also consider returning early if the
891 // selected index is -1, which represents a no-committed-entry state.
892 if (selected_navigation < -1 ||
893 selected_navigation >=
894 base::checked_cast<int>(entries->size() + entries_.size())) {
895 return;
Rakina Zata Amni2322f4f82022-01-24 13:24:24896 }
Charlie Reis23c26da2022-01-29 00:57:47897
Rakina Zata Amni46087a12022-11-11 08:28:38898 // There will always be at least one entry (new NavigationControllers will
899 // have the initial NavigationEntry).
900 if (selected_navigation == -1)
901 selected_navigation = 0;
Charlie Reis23c26da2022-01-29 00:57:47902
Rakina Zata Amni46087a12022-11-11 08:28:38903 if (GetLastCommittedEntry()->IsInitialEntry() && entries->size() > 0) {
904 // If we are on the initial NavigationEntry, it must be the only entry in
905 // the list. Since it's impossible to do a history navigation to the
906 // initial NavigationEntry, `pending_entry_index_` must be -1 (but
907 // `pending_entry` might be set for a new non-history navigation).
908 // Note that we should not clear `entries_` if `entries` is empty (when
909 // InitialNavigationEntry mode is enabled), since that would leave us
910 // without any NavigationEntry.
911 CHECK_EQ(1, GetEntryCount());
912 CHECK_EQ(-1, pending_entry_index_);
913 entries_.clear();
Charlie Reis23c26da2022-01-29 00:57:47914 }
[email protected]ce3fa3c2009-04-20 19:55:57915
[email protected]ce3fa3c2009-04-20 19:55:57916 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44917 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03918 entries_.reserve(entries->size());
Charlie Reis23c26da2022-01-29 00:57:47919 for (auto& entry : *entries) {
Rakina Zata Amni996ee412022-02-17 04:51:43920 if (entry->GetURL().is_empty()) {
921 // We're trying to restore an entry with an empty URL (e.g. from
Rakina Zata Amni2729a512022-03-16 05:54:01922 // persisting the initial NavigationEntry [which is no longer possible but
923 // some old persisted sessions might still contain it] or when the
924 // serializer failed to write the URL because it's too long). Trying to
925 // restore and navigate to an entry with an empty URL will result in
926 // crashes, so change the URL to about:blank. See also
927 // https://crbug.com/1240138 and https://crbug.com/1299335.
Rakina Zata Amni996ee412022-02-17 04:51:43928 entry->SetURL(GURL(url::kAboutBlankURL));
929 }
dcheng36b6aec92015-12-26 06:16:36930 entries_.push_back(
931 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
Charlie Reis23c26da2022-01-29 00:57:47932 }
avif16f85a72015-11-13 18:25:03933
934 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
935 // cleared out safely.
936 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57937
938 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36939 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57940}
941
toyoshim6142d96f2016-12-19 09:07:25942void NavigationControllerImpl::Reload(ReloadType reload_type,
943 bool check_for_repost) {
Charlie Reisf4d51f402025-05-23 18:00:49944 base::TimeTicks actual_navigation_start = base::TimeTicks::Now();
Rakina Zata Amnid605d462022-06-01 10:17:03945 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "Reload_type",
946 (int)reload_type);
947 SCOPED_CRASH_KEY_BOOL("nav_reentrancy_caller1", "Reload_check",
948 check_for_repost);
liaoyuke9168fba2017-03-10 19:20:28949 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28950 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32951 int current_index = -1;
952
Carlos IL42b416592019-10-07 23:10:36953 if (entry_replaced_by_post_commit_error_) {
954 // If there is an entry that was replaced by a currently active post-commit
955 // error navigation, this can't be the initial navigation.
956 DCHECK(!IsInitialNavigation());
957 // If the current entry is a post commit error, we reload the entry it
958 // replaced instead. We leave the error entry in place until a commit
959 // replaces it, but the pending entry points to the original entry in the
960 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
961 // case that pending_entry_ != entries_[pending_entry_index_].
962 entry = entry_replaced_by_post_commit_error_.get();
963 current_index = GetCurrentEntryIndex();
964 } else if (IsInitialNavigation() && pending_entry_) {
965 // If we are reloading the initial navigation, just use the current
966 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32967 entry = pending_entry_;
968 // The pending entry might be in entries_ (e.g., after a Clone), so we
969 // should also update the current_index.
970 current_index = pending_entry_index_;
971 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00972 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32973 current_index = GetCurrentEntryIndex();
974 if (current_index != -1) {
creis3da03872015-02-20 21:12:32975 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32976 }
[email protected]979a4bc2013-04-24 01:27:15977 }
[email protected]241db352013-04-22 18:04:05978
[email protected]59167c22013-06-03 18:07:32979 // If we are no where, then we can't reload. TODO(darin): We should add a
980 // CanReload method.
981 if (!entry)
982 return;
983
Rakina Zata Amnif297a802022-01-18 03:53:43984 if (entry->IsInitialEntryNotForSynchronousAboutBlank()) {
985 // We should never navigate to an existing initial NavigationEntry that is
986 // the initial NavigationEntry for the initial empty document that hasn't
987 // been overridden by the synchronous about:blank commit, to preserve
988 // legacy behavior where trying to reload when the main frame is on the
989 // initial empty document won't result in a navigation. See also
990 // https://crbug.com/1277414.
991 return;
992 }
993
Aran Gilman37d11632019-10-08 23:07:15994 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30995 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07996 // they really want to do this. If they do, the dialog will call us back
997 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57998 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02999
[email protected]106a0812010-03-18 00:15:121000 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:571001 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:471002 return;
initial.commit09911bf2008-07-26 23:55:291003 }
Lei Zhang96031532019-10-10 19:05:471004
Wang Huia25efabc2022-09-24 17:27:221005 // Set ReloadType for |entry|.
1006 entry->set_reload_type(reload_type);
1007
Lei Zhang96031532019-10-10 19:05:471008 if (!IsInitialNavigation())
1009 DiscardNonCommittedEntries();
1010
1011 pending_entry_ = entry;
1012 pending_entry_index_ = current_index;
1013 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
1014
Nate Chapin45f620582021-09-30 17:45:431015 // location.reload() goes through BeginNavigation, so all reloads triggered
1016 // via this codepath are browser initiated.
Yoav Weiss8c573952022-11-17 17:35:131017 NavigateToExistingPendingEntry(
1018 reload_type,
1019 /*initiator_rfh=*/nullptr,
Arthur Sonzognic686e8f2024-01-11 08:36:371020 /*soft_navigation_heuristics_task_id=*/std::nullopt,
Charlie Reisf4d51f402025-05-23 18:00:491021 /*navigation_api_key=*/nullptr, actual_navigation_start);
initial.commit09911bf2008-07-26 23:55:291022}
1023
[email protected]d202a7c2012-01-04 07:53:471024void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:481025 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:121026}
1027
[email protected]d202a7c2012-01-04 07:53:471028void NavigationControllerImpl::ContinuePendingReload() {
Wang Hui96ab1012022-10-11 02:05:491029 // If the pending reload type has been cleared by another navigation
1030 // committing, then do not proceed to reload after a form repost dialog.
toyoshim0df1d3a2016-09-09 09:52:481031 if (pending_reload_ == ReloadType::NONE) {
Wang Hui96ab1012022-10-11 02:05:491032 return;
[email protected]106a0812010-03-18 00:15:121033 }
Wang Hui96ab1012022-10-11 02:05:491034 Reload(pending_reload_, false);
1035 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:121036}
1037
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571038bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:091039 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:111040}
1041
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571042bool NavigationControllerImpl::IsInitialBlankNavigation() {
Rakina Zata Amni46087a12022-11-11 08:28:381043 // Check that we're on the initial NavigationEntry and that this is not a
1044 // cloned tab.
1045 return IsInitialNavigation() && GetEntryCount() == 1 &&
1046 GetLastCommittedEntry()->IsInitialEntry() &&
1047 GetLastCommittedEntry()->restore_type() == RestoreType::kNotRestored;
creis10a4ab72015-10-13 17:22:401048}
1049
Aran Gilman37d11632019-10-08 23:07:151050NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
1051 int nav_entry_id) const {
avi254eff02015-07-01 08:27:581052 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:031053 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:581054}
1055
Adithya Srinivasan9b0c99c2021-08-10 15:19:451056NavigationEntryImpl*
1057NavigationControllerImpl::GetEntryWithUniqueIDIncludingPending(
1058 int nav_entry_id) const {
1059 NavigationEntryImpl* entry = GetEntryWithUniqueID(nav_entry_id);
1060 if (entry)
1061 return entry;
1062 return pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id
Keishi Hattori0e45c022021-11-27 09:25:521063 ? pending_entry_.get()
Adithya Srinivasan9b0c99c2021-08-10 15:19:451064 : nullptr;
1065}
1066
W. James MacLeanc07dc41b2022-07-25 18:52:161067void NavigationControllerImpl::RegisterExistingOriginAsHavingDefaultIsolation(
W. James MacLean1c40862c2020-04-27 21:05:571068 const url::Origin& origin) {
1069 for (int i = 0; i < GetEntryCount(); i++) {
1070 auto* entry = GetEntryAtIndex(i);
W. James MacLeanc07dc41b2022-07-25 18:52:161071 entry->RegisterExistingOriginAsHavingDefaultIsolation(origin);
W. James MacLean1c40862c2020-04-27 21:05:571072 }
1073 if (entry_replaced_by_post_commit_error_) {
1074 // It's possible we could come back to this entry if the error
1075 // page/interstitial goes away.
1076 entry_replaced_by_post_commit_error_
W. James MacLeanc07dc41b2022-07-25 18:52:161077 ->RegisterExistingOriginAsHavingDefaultIsolation(origin);
W. James MacLean1c40862c2020-04-27 21:05:571078 }
W. James MacLean1c40862c2020-04-27 21:05:571079}
1080
avi25764702015-06-23 15:43:371081void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:571082 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:001083 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:371084 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:271085 DCHECK_EQ(-1, pending_entry_index_);
[email protected]765b35502008-08-21 00:51:201086}
1087
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571088NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:471089 if (pending_entry_)
1090 return pending_entry_;
1091 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:201092}
1093
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571094NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:321095 // The pending entry is safe to return for new (non-history), browser-
1096 // initiated navigations. Most renderer-initiated navigations should not
1097 // show the pending entry, to prevent URL spoof attacks.
1098 //
1099 // We make an exception for renderer-initiated navigations in new tabs, as
1100 // long as no other page has tried to access the initial empty document in
1101 // the new tab. If another page modifies this blank page, a URL spoof is
1102 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:321103 bool safe_to_show_pending =
1104 pending_entry_ &&
1105 // Require a new navigation.
avi0dca04d2015-01-26 20:21:091106 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:321107 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:461108 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:321109
1110 // Also allow showing the pending entry for history navigations in a new tab,
1111 // such as Ctrl+Back. In this case, no existing page is visible and no one
1112 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:151113 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
1114 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:321115 safe_to_show_pending = true;
1116
1117 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:191118 return pending_entry_;
1119 return GetLastCommittedEntry();
1120}
1121
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571122int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:201123 if (pending_entry_index_ != -1)
1124 return pending_entry_index_;
1125 return last_committed_entry_index_;
1126}
1127
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571128NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
Rakina Zata Amni46087a12022-11-11 08:28:381129 CHECK_NE(last_committed_entry_index_, -1);
avif16f85a72015-11-13 18:25:031130 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:201131}
1132
Elad Alon32044f532025-03-04 22:16:031133const NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry()
1134 const {
1135 CHECK_NE(last_committed_entry_index_, -1);
1136 return entries_[last_committed_entry_index_].get();
1137}
1138
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571139bool NavigationControllerImpl::CanViewSource() {
Takashi Toyoshima6c58bbd2023-05-19 09:41:351140 const std::string& mime_type = frame_tree_->root()
1141 ->current_frame_host()
1142 ->GetPage()
1143 .GetContentsMimeType();
Kinuko Yasuda74702f92017-07-31 03:27:531144 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
1145 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:271146 NavigationEntry* visible_entry = GetVisibleEntry();
1147 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:391148 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:161149}
1150
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571151int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:271152 // The last committed entry index must always be less than the number of
Rakina Zata Amnie2d31312022-11-18 03:38:451153 // entries.
arthursonzogni5c4c202d2017-04-25 23:41:271154 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
[email protected]a26023822011-12-29 00:23:551155 return last_committed_entry_index_;
1156}
1157
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571158int NavigationControllerImpl::GetEntryCount() {
Rakina Zata Amnie2d31312022-11-18 03:38:451159 DCHECK_GE(entries_.size(), 1u);
Carlos IL4dea8902020-05-26 15:14:291160 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:551161 return static_cast<int>(entries_.size());
1162}
1163
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571164NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:371165 if (index < 0 || index >= GetEntryCount())
1166 return nullptr;
1167
avif16f85a72015-11-13 18:25:031168 return entries_[index].get();
[email protected]022af742011-12-28 18:37:251169}
1170
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571171NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:471172 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:201173}
1174
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571175int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:461176 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:031177}
1178
Arthur Sonzognic686e8f2024-01-11 08:36:371179std::optional<int> NavigationControllerImpl::GetIndexForGoBack() {
Shivani Sharma298d12852019-01-22 20:04:031180 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
Kevin McNee3b3a56192023-03-17 14:40:591181 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
1182 return index;
1183 }
Shivani Sharma298d12852019-01-22 20:04:031184 }
Arthur Sonzognic686e8f2024-01-11 08:36:371185 return std::nullopt;
Kevin McNee3b3a56192023-03-17 14:40:591186}
1187
1188bool NavigationControllerImpl::CanGoBack() {
1189 return GetIndexForGoBack().has_value();
1190}
1191
Arthur Sonzognic686e8f2024-01-11 08:36:371192std::optional<int> NavigationControllerImpl::GetIndexForGoForward() {
Kevin McNee3b3a56192023-03-17 14:40:591193 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
1194 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
1195 return index;
1196 }
1197 }
Arthur Sonzognic686e8f2024-01-11 08:36:371198 return std::nullopt;
[email protected]765b35502008-08-21 00:51:201199}
1200
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571201bool NavigationControllerImpl::CanGoForward() {
Kevin McNee3b3a56192023-03-17 14:40:591202 return GetIndexForGoForward().has_value();
[email protected]765b35502008-08-21 00:51:201203}
1204
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571205bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:031206 int index = GetIndexForOffset(offset);
1207 return index >= 0 && index < GetEntryCount();
1208}
1209
Xiaohan Wang7f8052e02022-01-14 18:44:281210#if BUILDFLAG(IS_ANDROID)
WangHui74286d52021-03-31 16:17:151211bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
WangHui74286d52021-03-31 16:17:151212 if (offset == 0)
1213 return true;
1214 int increment = offset > 0 ? 1 : -1;
1215 int non_skippable_entries = 0;
1216 for (int index = GetIndexForOffset(increment);
1217 index >= 0 && index < GetEntryCount(); index += increment) {
1218 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1219 non_skippable_entries++;
1220
1221 if (non_skippable_entries == std::abs(offset))
1222 return true;
1223 }
1224 return false;
1225}
1226#endif
1227
David Bokanb8e0df02025-05-14 22:03:481228NavigationController::WeakNavigationHandleVector
1229NavigationControllerImpl::GoBack() {
Arthur Sonzognic686e8f2024-01-11 08:36:371230 const std::optional<int> target_index = GetIndexForGoBack();
shivanisha55201872018-12-13 04:29:061231
Kevin McNeeedc481c2023-04-27 22:30:581232 CHECK(target_index.has_value());
Miyoung Shin1c565c912021-03-17 12:11:211233
David Bokanb8e0df02025-05-14 22:03:481234 return GoToIndex(*target_index);
[email protected]765b35502008-08-21 00:51:201235}
1236
David Bokanb8e0df02025-05-14 22:03:481237NavigationController::WeakNavigationHandleVector
1238NavigationControllerImpl::GoForward() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:161239 // Note that at least one entry (the last one) will be non-skippable since
1240 // entries are marked skippable only when they add another entry because of
1241 // redirect or pushState.
Arthur Sonzognic686e8f2024-01-11 08:36:371242 const std::optional<int> target_index = GetIndexForGoForward();
Kevin McNee3b3a56192023-03-17 14:40:591243
Kevin McNeeedc481c2023-04-27 22:30:581244 CHECK(target_index.has_value());
Kevin McNee3b3a56192023-03-17 14:40:591245
David Bokanb8e0df02025-05-14 22:03:481246 return GoToIndex(*target_index);
[email protected]765b35502008-08-21 00:51:201247}
1248
David Bokanb8e0df02025-05-14 22:03:481249NavigationController::WeakNavigationHandleVector
1250NavigationControllerImpl::GoToIndex(int index) {
1251 std::vector<base::WeakPtr<NavigationRequest>> requests =
1252 GoToIndex(index, /*initiator_rfh=*/nullptr,
1253 /*soft_navigation_heuristics_task_id=*/std::nullopt,
Charlie Reisf4d51f402025-05-23 18:00:491254 /*navigation_api_key=*/nullptr,
1255 /*actual_navigation_start=*/base::TimeTicks::Now());
David Bokanb8e0df02025-05-14 22:03:481256 std::vector<base::WeakPtr<NavigationHandle>> handles;
1257 std::ranges::move(requests, std::back_inserter(handles));
1258 return handles;
Dave Tapuska8bfd84c2019-03-26 20:47:161259}
1260
William Liu62ae26c2024-08-08 14:28:161261std::vector<base::WeakPtr<NavigationRequest>>
1262NavigationControllerImpl::GoToIndex(
Nate Chapinbf682fa32022-09-26 22:41:201263 int index,
1264 RenderFrameHostImpl* initiator_rfh,
Arthur Sonzognic686e8f2024-01-11 08:36:371265 std::optional<blink::scheduler::TaskAttributionId>
Yoav Weiss8c573952022-11-17 17:35:131266 soft_navigation_heuristics_task_id,
Charlie Reisf4d51f402025-05-23 18:00:491267 const std::string* navigation_api_key,
1268 base::TimeTicks actual_navigation_start) {
sunjian30574a62017-03-21 21:39:441269 TRACE_EVENT0("browser,navigation,benchmark",
1270 "NavigationControllerImpl::GoToIndex");
Peter Boströmf68fe042023-06-07 18:27:501271 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "GoToIndex_index", index);
1272 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "GoToIndex_size",
1273 entries_.size());
1274 // Indices should have have been validated by the caller.
1275 CHECK_GE(index, 0);
1276 CHECK_LT(static_cast<size_t>(index), entries_.size());
[email protected]765b35502008-08-21 00:51:201277
Rakina Zata Amnif297a802022-01-18 03:53:431278 if (entries_[index]->IsInitialEntryNotForSynchronousAboutBlank()) {
1279 // We should never navigate to an existing initial NavigationEntry that is
1280 // the initial NavigationEntry for the initial empty document that hasn't
1281 // been overridden by the synchronous about:blank commit, to preserve
1282 // legacy behavior where trying to reload when the main frame is on the
1283 // initial empty document won't result in a navigation. See also
1284 // https://crbug.com/1277414.
William Liu62ae26c2024-08-08 14:28:161285 return {};
Rakina Zata Amnif297a802022-01-18 03:53:431286 }
1287
[email protected]cbab76d2008-10-13 22:42:471288 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:201289
arthursonzogni5c4c202d2017-04-25 23:41:271290 DCHECK_EQ(nullptr, pending_entry_);
1291 DCHECK_EQ(-1, pending_entry_index_);
Rakina Zata Amnif297a802022-01-18 03:53:431292
arthursonzogni5c4c202d2017-04-25 23:41:271293 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:201294 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:271295 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
1296 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Charlie Reisf4d51f402025-05-23 18:00:491297 return NavigateToExistingPendingEntry(
1298 ReloadType::NONE, initiator_rfh, soft_navigation_heuristics_task_id,
1299 navigation_api_key, actual_navigation_start);
[email protected]765b35502008-08-21 00:51:201300}
1301
[email protected]d202a7c2012-01-04 07:53:471302void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:121303 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:031304 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:201305 return;
1306
[email protected]9ba14052012-06-22 23:50:031307 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:201308}
1309
Nate Chapinbf682fa32022-09-26 22:41:201310void NavigationControllerImpl::GoToOffsetFromRenderer(
1311 int offset,
Yoav Weiss8c573952022-11-17 17:35:131312 RenderFrameHostImpl* initiator_rfh,
Arthur Sonzognic686e8f2024-01-11 08:36:371313 std::optional<blink::scheduler::TaskAttributionId>
Charlie Reisf4d51f402025-05-23 18:00:491314 soft_navigation_heuristics_task_id,
1315 base::TimeTicks actual_navigation_start) {
Nate Chapinbb74c5a52023-07-21 23:13:351316 // Note: This is actually reached in unit tests.
1317 if (!CanGoToOffset(offset))
Nate Chapin45f620582021-09-30 17:45:431318 return;
1319
Nate Chapinbf682fa32022-09-26 22:41:201320 GoToIndex(GetIndexForOffset(offset), initiator_rfh,
Yoav Weiss8c573952022-11-17 17:35:131321 soft_navigation_heuristics_task_id,
Charlie Reisf4d51f402025-05-23 18:00:491322 /*navigation_api_key=*/nullptr, actual_navigation_start);
Nate Chapin45f620582021-09-30 17:45:431323}
1324
William Liu62ae26c2024-08-08 14:28:161325std::vector<base::WeakPtr<NavigationRequest>>
1326NavigationControllerImpl::GoToIndexAndReturnAllRequests(int index) {
William Liuec04e382024-05-23 18:03:271327 return GoToIndex(index, /*initiator_rfh=*/nullptr,
1328 /*soft_navigation_heuristics_task_id=*/std::nullopt,
Charlie Reisf4d51f402025-05-23 18:00:491329 /*navigation_api_key=*/nullptr,
1330 /*actual_navigation_start=*/base::TimeTicks::Now());
William Liuec04e382024-05-23 18:03:271331}
1332
Xiaohan Wang7f8052e02022-01-14 18:44:281333#if BUILDFLAG(IS_ANDROID)
WangHui74286d52021-03-31 16:17:151334void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
1335 // Note: This is actually reached in unit tests.
1336 if (!CanGoToOffsetWithSkipping(offset))
1337 return;
1338
Elly Fong-Jonesccc6d1f2021-06-14 18:32:421339 if (offset == 0) {
WangHui74286d52021-03-31 16:17:151340 GoToIndex(GetIndexForOffset(offset));
1341 return;
1342 }
1343 int increment = offset > 0 ? 1 : -1;
1344 // Find the offset without counting skippable entries.
1345 int target_index = GetIndexForOffset(increment);
1346 int non_skippable_entries = 0;
1347 for (int index = target_index; index >= 0 && index < GetEntryCount();
1348 index += increment) {
1349 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1350 non_skippable_entries++;
1351
1352 if (non_skippable_entries == std::abs(offset)) {
1353 target_index = index;
1354 break;
1355 }
1356 }
1357
1358 GoToIndex(target_index);
1359}
1360#endif
1361
[email protected]41374f12013-07-24 02:49:281362bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151363 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281364 return false;
[email protected]6a13a6c2011-12-20 21:47:121365
[email protected]43032342011-03-21 14:10:311366 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281367 return true;
[email protected]cbab76d2008-10-13 22:42:471368}
1369
Michael Thiessen9b14d512019-09-23 21:19:471370void NavigationControllerImpl::PruneForwardEntries() {
1371 DiscardNonCommittedEntries();
1372 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471373 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471374 if (num_removed <= 0)
1375 return;
Nate Chapin9eb16be72022-09-23 22:54:311376 RemovedEntriesTracker tracker(weak_factory_.GetSafeRef());
Michael Thiessen9b14d512019-09-23 21:19:471377 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1378 NotifyPrunedEntries(this, remove_start_index /* start index */,
1379 num_removed /* count */);
1380}
1381
Aran Gilman37d11632019-10-08 23:07:151382void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1383 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321384 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311385 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511386 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1387 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321388 }
1389}
1390
Harkiran Bolariaba823e42021-05-21 18:30:361391base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL(
1392 const GURL& url,
1393 const Referrer& referrer,
1394 ui::PageTransition transition,
1395 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471396 LoadURLParams params(url);
1397 params.referrer = referrer;
1398 params.transition_type = transition;
1399 params.extra_headers = extra_headers;
Harkiran Bolariaba823e42021-05-21 18:30:361400 return LoadURLWithParams(params);
[email protected]cf002332012-08-14 19:17:471401}
1402
Harkiran Bolariaba823e42021-05-21 18:30:361403base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams(
1404 const LoadURLParams& params) {
Charlie Reisf4d51f402025-05-23 18:00:491405 // For now, treat this as the actual navigation start time, even though a fair
1406 // amount of work is done in the browser process between the various ways to
1407 // start a navigation and reaching here (e.g., sending OpenURLParams through
1408 // the embedder and converting them to LoadURLParams). Note that this may be
1409 // used for some renderer-initiated navigations that go through
1410 // RenderFrameHostImpl::OpenURL as well.
1411 // TODO(crbug.com/385170155): Consider whether tracking this earlier work with
1412 // an earlier start time is worthwhile for metrics.
1413 base::TimeTicks actual_navigation_start = base::TimeTicks::Now();
1414
Lukasz Anforowicz435bcb582019-07-12 20:50:061415 if (params.is_renderer_initiated)
1416 DCHECK(params.initiator_origin.has_value());
1417
naskob8744d22014-08-28 17:07:431418 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151419 "NavigationControllerImpl::LoadURLWithParams", "url",
1420 params.url.possibly_invalid_spec());
Emmanuel Arias Soto9e159652025-05-16 07:53:551421
1422 if (IsDebugURL(params.url)) {
1423 // Browser-debug URLs won't go through NavigationThrottles so we have to
1424 // check them explicitly. See crbug.com/40605746.
1425 ContentBrowserClient* client = GetContentClient()->browser();
1426 if (client->ShouldBlockRendererDebugURL(
1427 params.url, browser_context_,
1428 GetTargetFrameTreeNodeForNavigation(params)
1429 ->current_frame_host())) {
1430 DiscardPendingEntry(false);
1431 return nullptr;
1432 }
1433 HandleDebugURL(params.url, params.transition_type,
1434 client->IsExplicitNavigation(params.transition_type));
[email protected]47752982014-07-29 08:01:431435 // If Telemetry is running, allow the URL load to proceed as if it's
1436 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491437 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
Peter Kasting6e6af3c2024-10-15 00:13:181438 switches::kEnableGpuBenchmarking)) {
Harkiran Bolariaba823e42021-05-21 18:30:361439 return nullptr;
Peter Kasting6e6af3c2024-10-15 00:13:181440 }
[email protected]47752982014-07-29 08:01:431441 }
[email protected]8bf1048012012-02-08 01:22:181442
[email protected]cf002332012-08-14 19:17:471443 // Checks based on params.load_type.
1444 switch (params.load_type) {
1445 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431446 case LOAD_TYPE_HTTP_POST:
Shu Yang112ad492024-07-25 17:11:541447#if BUILDFLAG(IS_ANDROID)
1448 case LOAD_TYPE_PDF_ANDROID:
1449#endif
[email protected]cf002332012-08-14 19:17:471450 break;
1451 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261452 if (!params.url.SchemeIs(url::kDataScheme)) {
Peter Boströmfc7ddc182024-10-31 19:37:211453 NOTREACHED() << "Data load must use data scheme.";
[email protected]cf002332012-08-14 19:17:471454 }
1455 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461456 }
[email protected]e47ae9472011-10-13 19:48:341457
[email protected]e47ae9472011-10-13 19:48:341458 // The user initiated a load, we don't need to reload anymore.
1459 needs_reload_ = false;
1460
Charlie Reisf4d51f402025-05-23 18:00:491461 return NavigateWithoutEntry(params, actual_navigation_start);
[email protected]132e281a2012-07-31 18:32:441462}
1463
Charlie Reis4c53a962023-06-21 23:17:531464void NavigationControllerImpl::LoadOriginalRequestURL() {
1465 // If the original request URL is not valid, matches the current URL, or
1466 // involves POST data, then simply reload. The POST check avoids issues with
1467 // sending data to the wrong page.
1468 const GURL& last_committed_url = GetLastCommittedEntry()->GetURL();
1469 const GURL& original_request_url =
1470 GetLastCommittedEntry()->GetOriginalRequestURL();
1471 if (!original_request_url.is_valid() ||
1472 original_request_url == last_committed_url ||
1473 GetLastCommittedEntry()->GetHasPostData()) {
1474 Reload(ReloadType::NORMAL, true);
1475 return;
1476 }
1477
1478 // Otherwise, attempt to load the original request URL without any of the
1479 // other data from the current NavigationEntry, replacing the current entry.
1480 // Loading the original URL is useful in cases such as modifying the user
1481 // agent.
1482 std::unique_ptr<NavigationController::LoadURLParams> load_params =
1483 std::make_unique<NavigationController::LoadURLParams>(
1484 original_request_url);
1485 load_params->should_replace_current_entry = true;
1486 load_params->transition_type = ui::PAGE_TRANSITION_RELOAD;
1487 LoadURLWithParams(*load_params.get());
1488}
1489
Mohamed Abdelhalim833de902019-09-16 17:41:451490bool NavigationControllerImpl::PendingEntryMatchesRequest(
1491 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191492 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451493 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191494}
1495
[email protected]d202a7c2012-01-04 07:53:471496bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321497 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071498 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331499 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441500 bool is_same_document_navigation,
Nate Chapinc7019dd7d2021-06-25 18:29:251501 bool was_on_initial_empty_document,
Shivani Sharmaeef521b2024-01-18 13:03:561502 bool previous_document_had_history_intervention_activation,
Camille Lamy10aafcd32018-12-05 15:48:131503 NavigationRequest* navigation_request) {
1504 DCHECK(navigation_request);
Chris Bookholt27faf8d2022-01-20 01:03:331505
1506 // Note: validation checks and renderer kills due to invalid commit messages
1507 // must happen before getting here, in
1508 // RenderFrameHostImpl::ValidateDidCommitParams. By the time we get here, some
1509 // effects of the navigation have already occurred.
1510
[email protected]cd2e15742013-03-08 04:08:311511 is_initial_navigation_ = false;
1512
Wang Hui96ab1012022-10-11 02:05:491513 // Any pending request to repost a form submission is no longer valid, since a
1514 // different NavigationEntry is committing.
1515 pending_reload_ = ReloadType::NONE;
1516
[email protected]0e8db942008-09-24 21:21:481517 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431518 bool overriding_user_agent_changed = false;
Rakina Zata Amnie2d31312022-11-18 03:38:451519 if (entry_replaced_by_post_commit_error_) {
1520 // Same document navigation events with a post-commit error should already
1521 // be blocked by RenderFrameHostImpl::ValidateDidCommitParams() before
1522 // reaching here.
1523 CHECK(!is_same_document_navigation);
Chris Bookholt27faf8d2022-01-20 01:03:331524
Xinghui Lub9e86a352024-05-17 16:29:421525 if (pending_entry_) {
1526 // Before `entry_replaced_by_post_commit_error_` is moved back, make sure
1527 // `pending_entry_` isn't pointing to the last committed entry.
1528 // Instead, all reload approaches (e.g., in `Reload` and
1529 // `LoadIfNecessary`) should attempt to load the
1530 // `entry_replaced_by_post_commit_error_` instead of the post commit error
1531 // entry itself.
1532 CHECK_NE(pending_entry_, entries_[last_committed_entry_index_].get())
1533 << "Incorrectly reloading the post commit error page entry.";
1534 }
Xinghui Lue4e50be2024-05-15 21:46:001535
Rakina Zata Amnie2d31312022-11-18 03:38:451536 // Any commit while a post-commit error page is showing should put the
1537 // original entry back, replacing the error page's entry. This includes
1538 // reloads, where the original entry was used as the pending entry and
1539 // should now be at the correct index at commit time.
1540 entries_[last_committed_entry_index_] =
1541 std::move(entry_replaced_by_post_commit_error_);
[email protected]0e8db942008-09-24 21:21:481542 }
Rakina Zata Amnie2d31312022-11-18 03:38:451543 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
1544 details->previous_entry_index = GetLastCommittedEntryIndex();
Shu Yang7a3ec532023-06-21 17:49:001545 // Must honor user agent overrides in the |navigation_request|, such as
1546 // from things like RequestDesktopSiteWebContentsObserverAndroid. As a
1547 // result, besides comparing |pending_entry_|'s user agent against
1548 // LastCommittedEntry's, also need to compare |navigation_request|'s user
1549 // agent against LastCommittedEntry's.
1550 if (navigation_request->is_overriding_user_agent() !=
Shu Yang127e41a2023-12-19 01:26:541551 GetLastCommittedEntry()->GetIsOverridingUserAgent() ||
1552 (PendingEntryMatchesRequest(navigation_request) &&
1553 pending_entry_->GetIsOverridingUserAgent() !=
1554 GetLastCommittedEntry()->GetIsOverridingUserAgent())) {
Shu Yang7a3ec532023-06-21 17:49:001555 overriding_user_agent_changed = true;
Rakina Zata Amnie2d31312022-11-18 03:38:451556 }
[email protected]ecd9d8702008-08-28 22:10:171557
Dave Tapuskaa2ab4f252021-07-08 21:31:281558 bool is_main_frame_navigation = !rfh->GetParent();
1559
Alexander Timind2f2e4f22019-04-02 20:04:531560 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1561 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281562 // For primary frame tree navigations, choose an appropriate
Rakina Zata Amni63b5e8092022-05-20 11:25:141563 // BackForwardCacheMetrics to be associated with the new navigation's
1564 // NavigationEntry, by either creating a new object or reusing the previous
1565 // entry's one.
Dave Tapuskaa2ab4f252021-07-08 21:31:281566 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics;
Takashi Toyoshima7c041d82023-09-26 16:09:211567 if (navigation_request->frame_tree_node()->frame_tree().is_primary()) {
Rakina Zata Amni63b5e8092022-05-20 11:25:141568 back_forward_cache_metrics = BackForwardCacheMetrics::
1569 CreateOrReuseBackForwardCacheMetricsForNavigation(
Dave Tapuskaa2ab4f252021-07-08 21:31:281570 GetLastCommittedEntry(), is_main_frame_navigation,
1571 params.document_sequence_number);
1572 }
Yuzu Saijo29f96ca92022-12-08 04:54:121573
Alexander Timind2f2e4f22019-04-02 20:04:531574 // Notify the last active entry that we have navigated away.
Dave Tapuskaa2ab4f252021-07-08 21:31:281575 if (is_main_frame_navigation && !is_same_document_navigation) {
Rakina Zata Amnie2d31312022-11-18 03:38:451576 if (auto* metrics = GetLastCommittedEntry()->back_forward_cache_metrics()) {
1577 metrics->MainFrameDidNavigateAwayFromDocument();
Alexander Timind2f2e4f22019-04-02 20:04:531578 }
1579 }
1580
Rakina Zata Amnifd8370b2022-11-14 13:32:251581 // Use CommonNavigationParam's `should_replace_current_entry` to determine
1582 // whether the current NavigationEntry should be replaced.
Charlie Reisf8cde712022-10-20 16:25:091583 // (See below for a case where we might override that.)
Rakina Zata Amnifd8370b2022-11-14 13:32:251584 details->did_replace_entry =
1585 navigation_request->common_params().should_replace_current_entry;
Charlie Reisf8cde712022-10-20 16:25:091586
fdegans9caf66a2015-07-30 21:10:421587 // If there is a pending entry at this point, it should have a SiteInstance,
Charlie Reisc4155af2022-10-19 15:33:111588 // except for restored entries. This should be true even if the current commit
1589 // is not related to the pending entry.
jam48cea9082017-02-15 06:13:291590 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481591 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081592 pending_entry_->IsRestored());
Charlie Reisc4155af2022-10-19 15:33:111593
1594 // Only make changes based on the pending entry if the NavigationRequest
1595 // matches it. Otherwise, the pending entry may be for a different request
1596 // (e.g., if a slow history navigation is pending while an auto-subframe
1597 // commit occurs).
1598 if (PendingEntryMatchesRequest(navigation_request)) {
1599 // It is no longer necessary to consider the pending entry as restored.
1600 if (pending_entry_->IsRestored()) {
1601 pending_entry_->set_restore_type(RestoreType::kNotRestored);
1602 was_restored = true;
1603 }
[email protected]e9ba4472008-09-14 15:42:431604
Charlie Reisf8cde712022-10-20 16:25:091605 // If the SiteInstance has changed from the matching pending entry, this
1606 // must be treated as a new navigation with replacement. Set the replacement
1607 // bit here and ClassifyNavigation will identify this case and return
1608 // NEW_ENTRY.
1609 if (!rfh->GetParent() && pending_entry_->site_instance() &&
1610 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1611 DCHECK_NE(-1, pending_entry_index_);
Rakina Zata Amnifd8370b2022-11-14 13:32:251612 // TODO(nasko,creis,rakina): Move this to happen before committing the
1613 // navigation. This is a bit complicated because we don't currently
1614 // set `should_replace_current_entry` for reload/history navigations.
Charlie Reisf8cde712022-10-20 16:25:091615 details->did_replace_entry = true;
1616 }
Nasko Oskovaee2f862018-06-15 00:05:521617 }
[email protected]bcd904482012-02-01 01:54:221618
[email protected]e9ba4472008-09-14 15:42:431619 // Do navigation-type specific actions. These will make and commit an entry.
Miyoung Shin3299cbf2022-11-22 01:41:101620 NavigationType navigation_type =
1621 ClassifyNavigation(rfh, params, navigation_request);
1622 navigation_request->set_navigation_type(navigation_type);
shivanigithub189833f2022-04-27 18:08:451623
Rakina Zata Amnie2d31312022-11-18 03:38:451624 if (ShouldMaintainTrivialSessionHistory(rfh->frame_tree_node())) {
shivanigithub189833f2022-04-27 18:08:451625 // Ensure that this navigation does not add a navigation entry, since
1626 // ShouldMaintainTrivialSessionHistory() means we should not add an entry
1627 // beyond the last committed one. Therefore, `should_replace_current_entry`
1628 // should be set, which replaces the current entry, or this should be a
1629 // reload, which does not create a new entry.
Rakina Zata Amnifd8370b2022-11-14 13:32:251630 DCHECK(navigation_request->common_params().should_replace_current_entry ||
Dominic Farolinoe0f8d7c2023-08-16 15:38:331631 navigation_request->GetReloadType() != ReloadType::NONE);
shivanigithub189833f2022-04-27 18:08:451632 }
1633
Rakina Zata Amnie2d31312022-11-18 03:38:451634 if (GetLastCommittedEntry()->IsInitialEntry()) {
Rakina Zata Amniddf10502022-01-15 02:56:551635 if (rfh->GetParent()) {
Rakina Zata Amni2322f4f82022-01-24 13:24:241636 // This is a subframe navigation on the initial empty document, which used
1637 // to not have a NavigationEntry to attach to. Now it can attach to the
1638 // initial NavigationEntry, and we must ensure that its NavigationEntry
1639 // will keep the "initial NavigationEntry" status and won't append a new
1640 // NavigationEntry (it should always do replacement instead).
1641 // See also https://crbug.com/1277414.
1642 details->should_stay_as_initial_entry = true;
Rakina Zata Amniddf10502022-01-15 02:56:551643 // Subframe navigation on initial NavigationEntry must not append a new
1644 // NavigationEntry (i.e. should not be classified as NEW_SUBFRAME). This
1645 // means every subframe navigation that happens while we're on the initial
1646 // NavigationEntry will always reuse the existing NavigationEntry and
1647 // just update the corresponding FrameNavigationEntry.
Miyoung Shin3299cbf2022-11-22 01:41:101648 DCHECK_EQ(navigation_type, NAVIGATION_TYPE_AUTO_SUBFRAME);
1649 } else if (navigation_type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY) {
Rakina Zata Amniddf10502022-01-15 02:56:551650 // This is a navigation that modifies the initial NavigationEntry, either
1651 // for a replacement or a reload. The initial NavigationEntry should
1652 // retain its "initial NavigationEntry" status in this case.
1653 details->should_stay_as_initial_entry = true;
Rakina Zata Amni2322f4f82022-01-24 13:24:241654 } else if (navigation_request->is_synchronous_renderer_commit() &&
Rakina Zata Amnifd8370b2022-11-14 13:32:251655 !navigation_request->IsSameDocument() && !rfh->GetParent()) {
1656 DCHECK(navigation_request->common_params().should_replace_current_entry);
Rakina Zata Amni2322f4f82022-01-24 13:24:241657 // This is a synchronous about:blank navigation on the main frame, which
1658 // used to not create a NavigationEntry when we have no NavigationEntry on
1659 // FrameTree creation. We now have the initial NavigationEntry and are on
1660 // the initial NavigationEntry. To preserve old behavior, we should still
1661 // keep the "initial" status for the new NavigationEntry that we will
1662 // create for this navigation, so that subframe navigations under the
1663 // synchronously committed about:blank document will never append new
1664 // NavigationEntry, and instead will just reuse the initial
1665 // NavigationEntry and modify the corresponding FrameNavigationEntries.
1666 // See also https://crbug.com/1277414.
1667 details->should_stay_as_initial_entry = true;
Rakina Zata Amniddf10502022-01-15 02:56:551668 }
1669 }
1670 DCHECK(!details->should_stay_as_initial_entry ||
1671 GetLastCommittedEntry()->IsInitialEntry());
[email protected]4bf3522c2010-08-19 21:00:201672
eugenebutee08663a2017-04-27 17:43:121673 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441674 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121675
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071676 details->is_prerender_activation =
1677 navigation_request->IsPrerenderedPageActivation();
Robert Lin540dbd12022-04-28 22:07:241678 details->is_in_active_page = navigation_request->GetRenderFrameHost()
1679 ->GetOutermostMainFrame()
1680 ->IsInPrimaryMainFrame();
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071681
Peter Boströmd7592132019-01-30 04:50:311682 // Make sure we do not discard the pending entry for a different ongoing
1683 // navigation when a same document commit comes in unexpectedly from the
1684 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1685 // other navigation types. See https://crbug.com/900036.
Alison Gale770f3fc2024-04-27 00:39:581686 // TODO(crbug.com/41437754): Handle history.pushState() as well.
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061687 bool keep_pending_entry =
1688 is_same_document_navigation &&
Miyoung Shin3299cbf2022-11-22 01:41:101689 navigation_type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY &&
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061690 pending_entry_ && !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311691
Miyoung Shin3299cbf2022-11-22 01:41:101692 switch (navigation_type) {
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061693 case NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY:
Charlie Reisc0f17d2d2021-01-12 18:52:491694 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051695 rfh, params, details->is_same_document, details->did_replace_entry,
Shivani Sharmaeef521b2024-01-18 13:03:561696 previous_document_had_history_intervention_activation,
1697 navigation_request, details);
[email protected]e9ba4472008-09-14 15:42:431698 break;
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061699 case NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY:
Charlie Reisc0f17d2d2021-01-12 18:52:491700 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1701 was_restored, navigation_request,
Rakina Zata Amnia4e27222021-12-22 01:05:001702 keep_pending_entry, details);
[email protected]e9ba4472008-09-14 15:42:431703 break;
[email protected]8ff00d72012-10-23 19:12:211704 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471705 RendererDidNavigateNewSubframe(
1706 rfh, params, details->is_same_document, details->did_replace_entry,
Shivani Sharmaeef521b2024-01-18 13:03:561707 previous_document_had_history_intervention_activation,
1708 navigation_request, details);
[email protected]e9ba4472008-09-14 15:42:431709 break;
[email protected]8ff00d72012-10-23 19:12:211710 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391711 if (!RendererDidNavigateAutoSubframe(
Nate Chapinc7019dd7d2021-06-25 18:29:251712 rfh, params, details->is_same_document,
Rakina Zata Amnia4e27222021-12-22 01:05:001713 was_on_initial_empty_document, navigation_request, details)) {
creisce0ef3572017-01-26 17:53:081714 // We don't send a notification about auto-subframe PageState during
1715 // UpdateStateForFrame, since it looks like nothing has changed. Send
1716 // it here at commit time instead.
1717 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431718 return false;
creis59d5a47cb2016-08-24 23:57:191719 }
[email protected]e9ba4472008-09-14 15:42:431720 break;
Aran Gilman37d11632019-10-08 23:07:151721 case NAVIGATION_TYPE_UNKNOWN:
Peter Boströmfc7ddc182024-10-31 19:37:211722 NOTREACHED();
[email protected]765b35502008-08-21 00:51:201723 }
1724
[email protected]688aa65c62012-09-28 04:32:221725 // At this point, we know that the navigation has just completed, so
1726 // record the time.
1727 //
1728 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261729 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331730 base::Time timestamp =
1731 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1732 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131733 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221734
Peter Boströmd7592132019-01-30 04:50:311735 // If we aren't keeping the pending entry, there shouldn't be one at this
1736 // point. Clear it again in case any error cases above forgot to do so.
1737 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1738 // been cleared instead of protecting against it.
1739 if (!keep_pending_entry)
Rakina Zata Amniddf10502022-01-15 02:56:551740 DiscardNonCommittedEntriesWithCommitDetails(details);
[email protected]f233e4232013-02-23 00:55:141741
[email protected]e9ba4472008-09-14 15:42:431742 // All committed entries should have nonempty content state so WebKit doesn't
1743 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471744 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321745 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221746 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441747 active_entry->SetHttpStatusCode(params.http_status_code);
Fergal Daly0686c0e2022-06-28 02:08:141748
Alexander Timind2f2e4f22019-04-02 20:04:531749 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1750 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281751 if (back_forward_cache_metrics &&
1752 !active_entry->back_forward_cache_metrics()) {
Alexander Timind2f2e4f22019-04-02 20:04:531753 active_entry->set_back_forward_cache_metrics(
1754 std::move(back_forward_cache_metrics));
1755 }
Dave Tapuskaa2ab4f252021-07-08 21:31:281756
1757 // `back_forward_cache_metrics()` may return null as we do not record
1758 // back-forward cache metrics for navigations in non-primary frame trees.
1759 if (active_entry->back_forward_cache_metrics()) {
Alison Gale770f3fc2024-04-27 00:39:581760 // TODO(crbug.com/40229455): Remove this.
Fergal Daly0686c0e2022-06-28 02:08:141761 // These are both only available from details at this point, so we capture
1762 // them here.
1763 SCOPED_CRASH_KEY_NUMBER("BFCacheMismatch", "navigation_type",
Miyoung Shin3299cbf2022-11-22 01:41:101764 navigation_type);
Fergal Daly0686c0e2022-06-28 02:08:141765 SCOPED_CRASH_KEY_BOOL("BFCacheMismatch", "did_replace",
1766 details->did_replace_entry);
Dave Tapuskaa2ab4f252021-07-08 21:31:281767 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
1768 navigation_request,
Rakina Zata Amni84a859df2024-06-07 15:56:321769 IsBackForwardCacheEnabled() &&
1770 rfh->delegate()->IsBackForwardCacheSupported() &&
1771 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
Dave Tapuskaa2ab4f252021-07-08 21:31:281772 }
naskoc7533512016-05-06 17:01:121773
Charles Reisc0507202017-09-21 00:40:021774 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1775 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1776 // A mismatch can occur if the renderer lies or due to a unique name collision
1777 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121778 FrameNavigationEntry* frame_entry =
1779 active_entry->GetFrameEntry(rfh->frame_tree_node());
Diana Qu21d0c922025-06-25 17:19:081780 if (base::FeatureList::IsEnabled(kCheckSiteInstanceOnHistoryNavigation) &&
Diana Qu1cc5eaf2025-06-21 00:04:271781 frame_entry && frame_entry->site_instance()) {
Diana Qu2d6c85b2025-06-17 01:13:141782 int64_t dsn = navigation_request->frame_entry_document_sequence_number();
1783 if (dsn != -1 && dsn == frame_entry->document_sequence_number()) {
1784 // We CHECK that the SiteInstance matches the one stored in the session
1785 // history's FrameNavigationEntry, if the document sequence number (DSN)
1786 // also matches. This ensures the navigation is committing in the expected
1787 // SiteInstance.
1788 //
1789 // It's okay for the SiteInstance to differ if a cross-document redirect
1790 // occurred — in that case, the DSN in NavigationRequest should be cleared
1791 // (set to -1), and we skip the CHECK.
1792 CHECK(rfh->GetSiteInstance() == frame_entry->site_instance(),
1793 base::NotFatalUntil::M141)
1794 << "Session history navigation committed in a different SiteInstance "
1795 "than intended. "
1796 << "FrameNavigationEntry SiteInstance: "
1797 << frame_entry->site_instance()
1798 << ", Committed RFH SiteInstance: " << rfh->GetSiteInstance()
1799 << ", URL: " << params.url;
1800 }
1801 }
Charles Reisc0507202017-09-21 00:40:021802 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1803 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031804 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081805 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1806 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031807 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201808
1809 // Remember the bindings the renderer process has at this point, so that
1810 // we do not grant this entry additional bindings if we come back to it.
1811 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301812 }
[email protected]132e281a2012-07-31 18:32:441813
[email protected]97d8f0d2013-10-29 16:49:211814 // Once it is committed, we no longer need to track several pieces of state on
1815 // the entry.
naskoc7533512016-05-06 17:01:121816 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131817
[email protected]49bd30e62011-03-22 20:12:591818 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221819 // TODO(creis): This check won't pass for subframes until we create entries
1820 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001821 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421822 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591823
[email protected]e9ba4472008-09-14 15:42:431824 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001825 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001826 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311827 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531828
arthursonzogni7ddc6542021-04-09 09:16:501829 active_entry->SetIsOverridingUserAgent(
1830 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031831
[email protected]93f230e02011-06-01 14:40:001832 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291833
aelias100c9192017-01-13 00:01:431834 if (overriding_user_agent_changed)
1835 delegate_->UpdateOverridingUserAgent();
1836
creis03b48002015-11-04 00:54:561837 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1838 // one knows the latest NavigationEntry it is showing (whether it has
1839 // committed anything in this navigation or not). This allows things like
1840 // state and title updates from RenderFrames to apply to the latest relevant
1841 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381842 int nav_entry_id = active_entry->GetUniqueID();
Ali Hijazid87307d2022-11-07 20:15:031843 for (FrameTreeNode* node : frame_tree_->Nodes())
dcheng57e39e22016-01-21 00:25:381844 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
Hayato Ito2c8c08d02021-06-23 03:38:431845
1846 if (navigation_request->IsPrerenderedPageActivation()) {
Charlie Reis99b2eba22025-01-31 19:18:571847 BroadcastHistoryIndexAndLength();
Alison Gale47d1537d2024-04-19 21:31:461848 // TODO(crbug.com/40187392): Broadcasting happens after the prerendered page
Hayato Ito2c8c08d02021-06-23 03:38:431849 // is activated. As a result, a "prerenderingchange" event listener sees the
1850 // history.length which is not updated yet. We should guarantee that
Charlie Reis99b2eba22025-01-31 19:18:571851 // history's length and index should be updated before a
Hayato Ito2c8c08d02021-06-23 03:38:431852 // "prerenderingchange" event listener runs. One possible approach is to use
1853 // the same IPC which "prerenderingchange" uses, and propagate history's
Charlie Reis99b2eba22025-01-31 19:18:571854 // length and index together with that.
Hayato Ito2c8c08d02021-06-23 03:38:431855 }
1856
[email protected]e9ba4472008-09-14 15:42:431857 return true;
initial.commit09911bf2008-07-26 23:55:291858}
1859
[email protected]8ff00d72012-10-23 19:12:211860NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321861 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101862 const mojom::DidCommitProvisionalLoadParams& params,
Rakina Zata Amni2322f4f82022-01-24 13:24:241863 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591864 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511865 "ClassifyNavigation");
1866
avi7c6f35e2015-05-08 17:52:381867 if (params.did_create_new_entry) {
Rakina Zata Amni2322f4f82022-01-24 13:24:241868 // A new entry. We may or may not have a corresponding pending entry, and
1869 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001870 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491871 trace_return.set_return_reason("new entry, no parent, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061872 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381873 }
Rakina Zata Amni2322f4f82022-01-24 13:24:241874 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511875 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381876 return NAVIGATION_TYPE_NEW_SUBFRAME;
1877 }
1878
Charlie Reisc0f17d2d2021-01-12 18:52:491879 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381880 DCHECK(!params.history_list_was_cleared);
1881
avi39c1edd32015-06-04 20:06:001882 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381883 // All manual subframes would be did_create_new_entry and handled above, so
1884 // we know this is auto.
Rakina Zata Amniacd4df662022-11-15 06:49:081885 trace_return.set_return_reason("subframe, last commmited, auto subframe");
1886 return NAVIGATION_TYPE_AUTO_SUBFRAME;
avi7c6f35e2015-05-08 17:52:381887 }
1888
Rakina Zata Amnif6950d552020-11-24 03:26:101889 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1890 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381891 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1892 // create a new page.
1893
Hayato Ito2ae49442021-07-02 02:59:251894 // This main frame navigation is not a history navigation (since
1895 // nav_entry_id is 0), but didn't create a new entry. So this must be a
1896 // reload or a replacement navigation, which will modify the existing entry.
1897 //
Nasko Oskov332593c2018-08-16 17:21:341898 // TODO(nasko): With error page isolation, reloading an existing session
1899 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491900 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341901 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511902 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491903 "nav entry 0, last committed, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061904 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381905 }
1906
Charlie Reisf8cde712022-10-20 16:25:091907 if (PendingEntryMatchesRequest(navigation_request)) {
Nasko Oskovaee2f862018-06-15 00:05:521908 // If the SiteInstance of the |pending_entry_| does not match the
1909 // SiteInstance that got committed, treat this as a new navigation with
1910 // replacement. This can happen if back/forward/reload encounters a server
1911 // redirect to a different site or an isolated error page gets successfully
1912 // reloaded into a different SiteInstance.
1913 if (pending_entry_->site_instance() &&
1914 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491915 trace_return.set_return_reason("pending matching nav entry, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061916 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521917 }
creis77c9aa32015-09-25 19:59:421918
Nasko Oskovaee2f862018-06-15 00:05:521919 if (pending_entry_index_ == -1) {
1920 // In this case, we have a pending entry for a load of a new URL but Blink
1921 // didn't do a new navigation (params.did_create_new_entry). First check
1922 // to make sure Blink didn't treat a new cross-process navigation as
1923 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161924 // we must treat it as NEW rather than the converted reload case below,
1925 // since the new SiteInstance doesn't match the last committed entry.
Rakina Zata Amnie2d31312022-11-18 03:38:451926 if (GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161927 trace_return.set_return_reason("new pending, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061928 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521929 }
1930
1931 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161932 // We will create a pending entry, but NavigateWithoutEntry will convert
1933 // it to a reload since it's the same page and not create a new entry for
1934 // it. (The user doesn't want to have a new back/forward entry when they
1935 // do this.) Therefore we want to just ignore the pending entry and go
1936 // back to where we were (the "existing entry").
1937 trace_return.set_return_reason("new pending, existing (same) entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061938 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521939 }
avi7c6f35e2015-05-08 17:52:381940 }
1941
Rakina Zata Amni153d5702021-09-13 22:48:001942 if (navigation_request->commit_params().intended_as_new_entry) {
avi7c6f35e2015-05-08 17:52:381943 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491944 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1945 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161946 trace_return.set_return_reason("intended as new entry, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061947 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381948 }
1949
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121950 if (navigation_request->DidEncounterError() &&
1951 failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101952 nav_entry_id == failed_pending_entry_id_) {
Charlie Reisb5f39cf2024-11-12 16:55:011953 // If the renderer was going to a pending entry that got cleared because of
1954 // an error, then the error page will replace the existing entry.
Nasko Oskovae49e292020-08-13 02:08:511955 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491956 "unreachable, matching pending, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061957 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381958 }
1959
Charlie Reisc0f17d2d2021-01-12 18:52:491960 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101961 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511962 trace_return.traced_value()->SetInteger("existing_entry_index",
1963 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381964 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441965 // The renderer has committed a navigation to an entry that no longer
1966 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491967 trace_return.set_return_reason("existing entry -1, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061968 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381969 }
1970
avi7c6f35e2015-05-08 17:52:381971 // Since we weeded out "new" navigations above, we know this is an existing
1972 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491973 trace_return.set_return_reason("default return, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061974 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381975}
1976
Rakina Zata Amni3460d382021-10-29 00:43:371977void NavigationControllerImpl::UpdateNavigationEntryDetails(
1978 NavigationEntryImpl* entry,
1979 RenderFrameHostImpl* rfh,
1980 const mojom::DidCommitProvisionalLoadParams& params,
1981 NavigationRequest* request,
1982 NavigationEntryImpl::UpdatePolicy update_policy,
Rakina Zata Amnia4e27222021-12-22 01:05:001983 bool is_new_entry,
1984 LoadCommittedDetails* commit_details) {
Rakina Zata Amni3460d382021-10-29 00:43:371985 // Update the FrameNavigationEntry.
Rakina Zata Amniafd3c6582021-11-30 06:19:171986 std::vector<GURL> redirects;
Rakina Zata Amni3460d382021-10-29 00:43:371987 entry->AddOrUpdateFrameEntry(
1988 rfh->frame_tree_node(), update_policy, params.item_sequence_number,
Domenic Denicolacc094fb2022-03-16 23:40:571989 params.document_sequence_number, params.navigation_api_key,
Rakina Zata Amni3460d382021-10-29 00:43:371990 rfh->GetSiteInstance(), nullptr, params.url,
1991 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amniafd3c6582021-11-30 06:19:171992 Referrer(*params.referrer),
1993 request ? request->common_params().initiator_origin : params.origin,
Arthur Sonzognic686e8f2024-01-11 08:36:371994 request ? request->common_params().initiator_base_url : std::nullopt,
Rakina Zata Amniafd3c6582021-11-30 06:19:171995 request ? request->GetRedirectChain() : redirects, params.page_state,
1996 params.method, params.post_id, nullptr /* blob_url_loader_factory */,
Rakina Zata Amni3460d382021-10-29 00:43:371997 ComputePolicyContainerPoliciesForFrameEntry(
Rakina Zata Amniafd3c6582021-11-30 06:19:171998 rfh, request && request->IsSameDocument(),
Antonio Sartorib8addf62024-09-16 07:59:211999 request ? request->DidEncounterError() : false,
Rakina Zata Amniafd3c6582021-11-30 06:19:172000 request ? request->common_params().url : params.url));
Rakina Zata Amni3460d382021-10-29 00:43:372001
2002 if (rfh->GetParent()) {
2003 // Only modify the NavigationEntry for main frame navigations.
2004 return;
2005 }
2006 if (entry->update_virtual_url_with_url())
2007 UpdateVirtualURLToURL(entry, params.url);
2008 // Don't use the page type from the pending entry. Some interstitial page
2009 // may have set the type to interstitial. Once we commit, however, the page
2010 // type must always be normal or error.
Rakina Zata Amniafd3c6582021-11-30 06:19:172011 entry->set_page_type((request && request->DidEncounterError())
2012 ? PAGE_TYPE_ERROR
2013 : PAGE_TYPE_NORMAL);
Rakina Zata Amnif297a802022-01-18 03:53:432014 if (commit_details && commit_details->should_stay_as_initial_entry) {
2015 // Retain the "initial NavigationEntry" status.
2016 if (request->IsSameDocument()) {
2017 // If this is for a same-document navigation, the NavigationEntry must be
2018 // reused and should already be marked as the initial NavigationEntry.
2019 DCHECK(entry->IsInitialEntry());
2020 } else {
2021 // If this is for a cross-document navigation, it can be caused by a
2022 // renderer-initiated reload, or the synchronous about:blank commit. Mark
2023 // "for synchronous about:blank" in the latter case, and also when it is
2024 // reloading a "for synchronous about:blank" entry. Otherwise, the entry
2025 // is not for a synchronous about:blank commit.
2026 NavigationEntryImpl::InitialNavigationEntryState new_state =
2027 NavigationEntryImpl::InitialNavigationEntryState::
2028 kInitialNotForSynchronousAboutBlank;
2029 if (entry->IsInitialEntryForSynchronousAboutBlank() ||
2030 request->is_synchronous_renderer_commit()) {
2031 new_state = NavigationEntryImpl::InitialNavigationEntryState::
2032 kInitialForSynchronousAboutBlank;
2033 }
2034 entry->set_initial_navigation_entry_state(new_state);
2035 }
2036 } else if (commit_details && !commit_details->should_stay_as_initial_entry) {
2037 // Remove the "initial NavigationEntry" status.
2038 entry->set_initial_navigation_entry_state(
2039 NavigationEntryImpl::InitialNavigationEntryState::kNonInitial);
Rakina Zata Amnia4e27222021-12-22 01:05:002040 }
Rakina Zata Amniddf10502022-01-15 02:56:552041
Rakina Zata Amni3460d382021-10-29 00:43:372042 if (is_new_entry) {
2043 // Some properties of the NavigationEntry are only set when the entry is
2044 // new (we aren't reusing it).
2045 entry->SetTransitionType(params.transition);
Rakina Zata Amniafd3c6582021-11-30 06:19:172046 entry->SetOriginalRequestURL(request ? request->GetOriginalRequestURL()
Peter Kasting8104ba82024-01-31 15:23:402047 : GURL());
Adithya Srinivasan72b07352023-12-21 15:56:012048 DCHECK_EQ(rfh->GetPage().is_overriding_user_agent(),
2049 params.is_overriding_user_agent);
Rakina Zata Amni3460d382021-10-29 00:43:372050 entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
2051 } else {
2052 // We are reusing the NavigationEntry. The site instance will normally be
2053 // the same except for a few cases:
2054 // 1) session restore, when no site instance will be assigned or
2055 // 2) redirect, when the site instance is reset.
2056 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
2057 entry->site_instance() == rfh->GetSiteInstance());
2058 }
2059}
2060
Rakina Zata Amniafd3c6582021-11-30 06:19:172061void NavigationControllerImpl::CreateInitialEntry() {
2062 DCHECK_EQ(entries_.size(), 0u);
Ali Hijazid87307d2022-11-07 20:15:032063 RenderFrameHostImpl* rfh = frame_tree_->root()->current_frame_host();
Rakina Zata Amniafd3c6582021-11-30 06:19:172064 auto params = mojom::DidCommitProvisionalLoadParams::New();
2065 // The initial NavigationEntry's URL is the empty URL. This preserves the old
2066 // behavior of WebContent's GetLastCommittedURL() and GetVisibleURL() from
2067 // before we have initial NavigationEntries.
Peter Kasting8104ba82024-01-31 15:23:402068 params->url = GURL();
Rakina Zata Amniafd3c6582021-11-30 06:19:172069 params->http_status_code = 0;
2070 params->url_is_unreachable = false;
2071 params->method = "GET";
Rakina Zata Amniafd3c6582021-11-30 06:19:172072 params->post_id = -1;
2073 params->embedding_token = base::UnguessableToken::Create();
2074 params->navigation_token = base::UnguessableToken::Create();
2075 params->did_create_new_entry = true;
2076 params->origin = rfh->GetLastCommittedOrigin();
2077 params->should_update_history = true;
2078 params->item_sequence_number = 0;
2079 params->document_sequence_number = 0;
Abhijeet Kandalkare26014a92022-10-13 04:21:152080 bool is_in_fenced_frame_tree = rfh->IsNestedWithinFencedFrame();
Rakina Zata Amniafd3c6582021-11-30 06:19:172081 params->transition = is_in_fenced_frame_tree
2082 ? ui::PAGE_TRANSITION_AUTO_SUBFRAME
2083 : ui::PAGE_TRANSITION_LINK;
2084 params->referrer = blink::mojom::Referrer::New();
2085
Rakina Zata Amniafd3c6582021-11-30 06:19:172086 auto new_entry = std::make_unique<NavigationEntryImpl>(
2087 rfh->GetSiteInstance(), params->url, Referrer(*params->referrer),
W. James MacLean78e2f872023-01-24 17:59:382088 rfh->GetLastCommittedOrigin(), rfh->GetInheritedBaseUrl(),
W. James MacLean23e90a12022-12-21 04:38:212089 std::u16string() /* title */, ui::PAGE_TRANSITION_TYPED,
2090 false /* renderer_initiated */, nullptr /* blob_url_loader_factory */,
2091 true /* is_initial_entry */);
Rakina Zata Amniafd3c6582021-11-30 06:19:172092 UpdateNavigationEntryDetails(
2093 new_entry.get(), rfh, *params, nullptr /* request */,
Rakina Zata Amnia4e27222021-12-22 01:05:002094 NavigationEntryImpl::UpdatePolicy::kUpdate, true /* is_new_entry */,
2095 nullptr /* commit_details */);
Rakina Zata Amniafd3c6582021-11-30 06:19:172096
2097 InsertOrReplaceEntry(std::move(new_entry), false /* replace_entry */,
2098 false /* was_post_commit_error */,
Rakina Zata Amnia4e27222021-12-22 01:05:002099 is_in_fenced_frame_tree, nullptr /* commit_details */);
Rakina Zata Amniafd3c6582021-11-30 06:19:172100}
2101
Charlie Reisc0f17d2d2021-01-12 18:52:492102void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:322103 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072104 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:362105 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:442106 bool replace_entry,
Shivani Sharmaeef521b2024-01-18 13:03:562107 bool previous_document_had_history_intervention_activation,
Rakina Zata Amnia4e27222021-12-22 01:05:002108 NavigationRequest* request,
2109 LoadCommittedDetails* commit_details) {
dcheng9bfa5162016-04-09 01:00:572110 std::unique_ptr<NavigationEntryImpl> new_entry;
Arthur Sonzognic686e8f2024-01-11 08:36:372111 const std::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452112 request->common_params().initiator_origin;
Arthur Sonzognic686e8f2024-01-11 08:36:372113 std::optional<GURL> initiator_base_url;
W. James MacLean8be423a2023-03-31 21:35:522114 if (params.url.IsAboutBlank() || params.url.IsAboutSrcdoc()) {
2115 initiator_base_url = request->common_params().initiator_base_url;
2116 }
Lukasz Anforowicz435bcb582019-07-12 20:50:062117
creisf49dfc92016-07-26 17:05:182118 // First check if this is an in-page navigation. If so, clone the current
2119 // entry instead of looking at the pending entry, because the pending entry
2120 // does not have any subframe history items.
Rakina Zata Amnie2d31312022-11-18 03:38:452121 if (is_same_document) {
Nate Chapin63db0d12022-01-20 22:03:302122 FrameNavigationEntry* previous_frame_entry =
2123 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
Patrick Monette50e8bd82019-06-13 22:40:452124 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:482125 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Domenic Denicolacc094fb2022-03-16 23:40:572126 params.document_sequence_number, params.navigation_api_key,
Nate Chapinfbfe5af2021-06-10 17:22:082127 rfh->GetSiteInstance(), nullptr, params.url,
2128 GetCommittedOriginForFrameEntry(params, request),
W. James MacLean23e90a12022-12-21 04:38:212129 Referrer(*params.referrer), initiator_origin, initiator_base_url,
Rakina Zata Amni82fafba2021-03-11 07:07:092130 request->GetRedirectChain(), params.page_state, params.method,
2131 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:392132 // We will set the document policies later in this function.
Nate Chapin63db0d12022-01-20 22:03:302133 nullptr /* policy_container_policies */,
Domenic Denicolacc094fb2022-03-16 23:40:572134 // Try to preserve protect_url_in_navigation_api from the previous
Nate Chapin63db0d12022-01-20 22:03:302135 // FrameNavigationEntry.
2136 previous_frame_entry &&
Domenic Denicolacc094fb2022-03-16 23:40:572137 previous_frame_entry->protect_url_in_navigation_api());
Charles Reisf44482022017-10-13 21:15:032138
creisf49dfc92016-07-26 17:05:182139 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Ali Hijazid87307d2022-11-07 20:15:032140 frame_entry, true, request->frame_tree_node(), frame_tree_->root());
Mike West800532c2021-10-14 09:26:522141 if (new_entry->GetURL().DeprecatedGetOriginAsURL() !=
2142 params.url.DeprecatedGetOriginAsURL()) {
jama78746e2017-02-22 17:21:572143 // TODO(jam): we had one report of this with a URL that was redirecting to
2144 // only tildes. Until we understand that better, don't copy the cert in
2145 // this case.
2146 new_entry->GetSSL() = SSLStatus();
2147 }
creisf49dfc92016-07-26 17:05:182148
Patrick Monette50e8bd82019-06-13 22:40:452149 // It is expected that |frame_entry| is now owned by |new_entry|. This means
2150 // that |frame_entry| should now have a reference count of exactly 2: one
2151 // due to the local variable |frame_entry|, and another due to |new_entry|
2152 // also retaining one. This should never fail, because it's the main frame.
2153 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:182154 }
2155
Harkiran Bolaria59290d62021-03-17 01:53:012156 // If this is an activation navigation from a prerendered page, transfer the
2157 // new entry from an entry already created and stored in the
2158 // NavigationRequest. |new_entry| will not have been set prior to this as
2159 // |is_same_document| is mutually exclusive with
2160 // |IsPrerenderedPageActivation|.
2161 if (request->IsPrerenderedPageActivation()) {
2162 DCHECK(!is_same_document);
2163 DCHECK(!new_entry);
2164 new_entry = request->TakePrerenderNavigationEntry();
2165 DCHECK(new_entry);
2166 }
2167
Charlie Reisc0f17d2d2021-01-12 18:52:492168 // Only make a copy of the pending entry if it is appropriate for the new
2169 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:452170 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:042171 // 1. The SiteInstance hasn't been assigned to something else.
2172 // 2. The pending entry was intended as a new entry, rather than being a
2173 // history navigation that was interrupted by an unrelated,
2174 // renderer-initiated navigation.
2175 // TODO(csharrison): Investigate whether we can remove some of the coarser
2176 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:452177 if (!new_entry && PendingEntryMatchesRequest(request) &&
2178 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:342179 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:432180 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:352181 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:432182
Camille Lamy62b826012019-02-26 09:15:472183 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:452184 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
creisf49dfc92016-07-26 17:05:182185 }
2186
Charlie Reisc0f17d2d2021-01-12 18:52:492187 // For cross-document commits with no matching pending entry, create a new
2188 // entry.
creisf49dfc92016-07-26 17:05:182189 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:062190 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:072191 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
W. James MacLean23e90a12022-12-21 04:38:212192 initiator_origin, initiator_base_url,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:582193 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:452194 params.transition, request->IsRendererInitiated(),
Rakina Zata Amniafd3c6582021-11-30 06:19:172195 nullptr, // blob_url_loader_factory
2196 false); // is_initial_entry
[email protected]f8f93eb2012-09-25 03:06:242197
2198 // Find out whether the new entry needs to update its virtual URL on URL
2199 // change and set up the entry accordingly. This is needed to correctly
2200 // update the virtual URL when replaceState is called after a pushState.
2201 GURL url = params.url;
2202 bool needs_update = false;
Charlie Reisc0f17d2d2021-01-12 18:52:492203 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:412204 // update the virtual URL based on the new URL. For example, this is needed
2205 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
2206 // the URL.
Rakina Zata Amni3460d382021-10-29 00:43:372207 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
2208 &url, browser_context_, &needs_update);
2209 new_entry->set_update_virtual_url_with_url(needs_update);
2210
Camille Lamy62b826012019-02-26 09:15:472211 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:452212 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
[email protected]e9ba4472008-09-14 15:42:432213 }
2214
Alison Gale770f3fc2024-04-27 00:39:582215 // TODO(crbug.com/40169536) - determine which parts of the entry need to be
2216 // set for prerendered contents, if any. This is because
2217 // prerendering/activation technically won't be creating a new document.
2218 // Unlike BFCache, prerendering creates a new NavigationEntry rather than
2219 // using an existing one.
Harkiran Bolaria59290d62021-03-17 01:53:012220 if (!request->IsPrerenderedPageActivation()) {
Rakina Zata Amni3460d382021-10-29 00:43:372221 UpdateNavigationEntryDetails(new_entry.get(), rfh, params, request,
2222 NavigationEntryImpl::UpdatePolicy::kUpdate,
Rakina Zata Amnia4e27222021-12-22 01:05:002223 true /* is_new_entry */, commit_details);
creis8b5cd4c2015-06-19 00:11:082224
Harkiran Bolaria59290d62021-03-17 01:53:012225 // history.pushState() is classified as a navigation to a new page, but sets
2226 // is_same_document to true. In this case, we already have the title and
2227 // favicon available, so set them immediately.
Rakina Zata Amnie2d31312022-11-18 03:38:452228 if (is_same_document) {
Harkiran Bolaria59290d62021-03-17 01:53:012229 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
2230 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
2231 }
[email protected]3a868f212014-08-09 10:41:192232 }
[email protected]ff64b3e2014-05-31 04:07:332233
[email protected]60d6cca2013-04-30 08:47:132234 DCHECK(!params.history_list_was_cleared || !replace_entry);
2235 // The browser requested to clear the session history when it initiated the
2236 // navigation. Now we know that the renderer has updated its state accordingly
2237 // and it is safe to also clear the browser side history.
2238 if (params.history_list_was_cleared) {
Rakina Zata Amniddf10502022-01-15 02:56:552239 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]60d6cca2013-04-30 08:47:132240 entries_.clear();
2241 last_committed_entry_index_ = -1;
2242 }
2243
Nasko Oskovaee2f862018-06-15 00:05:522244 // If this is a new navigation with replacement and there is a
2245 // pending_entry_ which matches the navigation reported by the renderer
2246 // process, then it should be the one replaced, so update the
2247 // last_committed_entry_index_ to use it.
2248 if (replace_entry && pending_entry_index_ != -1 &&
Charlie Reisf8cde712022-10-20 16:25:092249 PendingEntryMatchesRequest(request)) {
Nasko Oskovaee2f862018-06-15 00:05:522250 last_committed_entry_index_ = pending_entry_index_;
2251 }
2252
Alexander Timine3ec4192020-04-20 16:39:402253 SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaeef521b2024-01-18 13:03:562254 replace_entry, previous_document_had_history_intervention_activation,
Alexander Timine3ec4192020-04-20 16:39:402255 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:052256
Yuzu Saijoa725585f2022-11-28 04:14:032257 // If this is a history navigation and the old entry has an existing
2258 // back/forward cache metrics object, keep using the old one so that the
2259 // reasons logged from the last time the page navigated gets preserved.
2260 if (BackForwardCacheMetrics::IsCrossDocumentMainFrameHistoryNavigation(
2261 request)) {
2262 // Use |request->GetNavigationEntry()| instead of |pending_entry_| here
2263 // because some tests do not have a pending entry.
2264 NavigationEntryImpl* entry =
2265 static_cast<NavigationEntryImpl*>(request->GetNavigationEntry());
2266 if (entry && entry->back_forward_cache_metrics()) {
2267 scoped_refptr<BackForwardCacheMetrics> metrics =
2268 entry->TakeBackForwardCacheMetrics();
2269 new_entry->set_back_forward_cache_metrics(std::move(metrics));
2270 }
2271 }
2272
Andrew Verge754c70a2025-04-17 17:19:192273 bool was_post_commit_error =
2274 request->browser_initiated_error_navigation_type() ==
2275 NavigationRequest::BrowserInitiatedErrorNavigationType::kPostCommit;
2276
Carlos IL42b416592019-10-07 23:10:362277 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
Andrew Verge754c70a2025-04-17 17:19:192278 was_post_commit_error, rfh->IsNestedWithinFencedFrame(),
2279 commit_details);
[email protected]e9ba4472008-09-14 15:42:432280}
2281
Charlie Reisc0f17d2d2021-01-12 18:52:492282void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:322283 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072284 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:362285 bool is_same_document,
jam48cea9082017-02-15 06:13:292286 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:452287 NavigationRequest* request,
Rakina Zata Amnia4e27222021-12-22 01:05:002288 bool keep_pending_entry,
2289 LoadCommittedDetails* commit_details) {
creis26d22632017-04-21 20:23:562290 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
2291 << "that a last committed entry exists.";
2292
[email protected]e9ba4472008-09-14 15:42:432293 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:002294 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:432295
Charlie Reis7e2cb6d2021-01-26 01:27:162296 NavigationEntryImpl* entry = nullptr;
Rakina Zata Amni153d5702021-09-13 22:48:002297 if (request->commit_params().intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:162298 // We're guaranteed to have a last committed entry if intended_as_new_entry
2299 // is true.
avicbdc4c12015-07-01 16:07:112300 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:162301
2302 // If the NavigationRequest matches a new pending entry and is classified as
2303 // EXISTING_ENTRY, then it is a navigation to the same URL that was
2304 // converted to a reload, such as a user pressing enter in the omnibox.
Charlie Reisf8cde712022-10-20 16:25:092305 if (pending_entry_index_ == -1 && PendingEntryMatchesRequest(request)) {
Charlie Reis7e2cb6d2021-01-26 01:27:162306 // Note: The pending entry will usually have a real ReloadType here, but
2307 // it can still be ReloadType::NONE in cases that
2308 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
2309
2310 // If we classified this correctly, the SiteInstance should not have
2311 // changed.
2312 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
2313
2314 // For converted reloads, we assign the entry's unique ID to be that of
2315 // the new one. Since this is always the result of a user action, we want
2316 // to dismiss infobars, etc. like a regular user-initiated navigation.
2317 entry->set_unique_id(pending_entry_->GetUniqueID());
2318
2319 // The extra headers may have changed due to reloading with different
2320 // headers.
2321 entry->set_extra_headers(pending_entry_->extra_headers());
2322 }
2323 // Otherwise, this was intended as a new entry but the pending entry was
2324 // lost in the meantime and no new entry was created. We are stuck at the
2325 // last committed entry.
2326
2327 // Even if this is a converted reload from pressing enter in the omnibox,
2328 // the server could redirect, requiring an update to the SSL status. If this
2329 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:452330 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:162331 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:472332 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:452333 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:162334 }
Rakina Zata Amnif6950d552020-11-24 03:26:102335 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:112336 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:102337 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b902016-09-01 16:58:162338
eugenebut604866f2017-05-10 21:35:362339 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:452340 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:362341 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
2342 // this was a restored same document navigation entry, then it won't have
2343 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
2344 // navigated it.
jam48cea9082017-02-15 06:13:292345 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
Mike West800532c2021-10-14 09:26:522346 if (entry->GetURL().DeprecatedGetOriginAsURL() ==
2347 last_entry->GetURL().DeprecatedGetOriginAsURL() &&
jam48cea9082017-02-15 06:13:292348 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
2349 was_restored) {
2350 entry->GetSSL() = last_entry->GetSSL();
2351 }
2352 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:452353 // In rapid back/forward navigations |request| sometimes won't have a cert
2354 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:192355 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:452356 if (request->GetSSLInfo().has_value() &&
2357 request->GetSSLInfo()->is_valid()) {
2358 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
Mike West800532c2021-10-14 09:26:522359 } else if (entry->GetURL().DeprecatedGetOriginAsURL() !=
2360 request->GetURL().DeprecatedGetOriginAsURL()) {
John Abd-El-Malek3f247082017-12-07 19:02:192361 entry->GetSSL() = SSLStatus();
2362 }
jam48cea9082017-02-15 06:13:292363 }
avicbdc4c12015-07-01 16:07:112364 } else {
Feifei Wang2ab8ba6c2022-04-13 22:19:272365 // This is renderer-initiated. The only kinds of renderer-initiated
Rakina Zata Amni557afb92021-07-17 04:39:572366 // navigations that are EXISTING_ENTRY are same-document navigations that
2367 // result in replacement (e.g. history.replaceState(), location.replace(),
2368 // forced replacements for trivial session history contexts). For these
2369 // cases, we reuse the last committed entry.
avicbdc4c12015-07-01 16:07:112370 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:102371
Alison Gale81f4f2c72024-04-22 19:33:312372 // TODO(crbug.com/40532777): Set page transition type to
2373 // PAGE_TRANSITION_LINK to avoid misleading interpretations (e.g. URLs
2374 // paired with PAGE_TRANSITION_TYPED that haven't actually been typed) as
2375 // well as to fix the inconsistency with what we report to observers
2376 // (PAGE_TRANSITION_LINK | PAGE_TRANSITION_CLIENT_REDIRECT).
Mikel Astizba9cf2fd2017-12-17 10:38:102377
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572378 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:102379
eugenebut604866f2017-05-10 21:35:362380 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:452381 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:362382 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:472383 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:452384 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
avicbdc4c12015-07-01 16:07:112385 }
2386 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:432387
Rakina Zata Amni3460d382021-10-29 00:43:372388 UpdateNavigationEntryDetails(entry, rfh, params, request,
2389 NavigationEntryImpl::UpdatePolicy::kUpdate,
Rakina Zata Amnia4e27222021-12-22 01:05:002390 false /* is_new_entry */, commit_details);
creis22a7b4c2016-04-28 07:20:302391
[email protected]5ccd4dc2012-10-24 02:28:142392 // The redirected to page should not inherit the favicon from the previous
2393 // page.
eugenebut604866f2017-05-10 21:35:362394 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:482395 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:142396
Charlie Reis951f43372023-05-05 00:30:072397 // Update the last committed index to reflect the committed entry. Do this
2398 // before calling DiscardNonCommittedEntriesWithCommitDetails, so that the
2399 // delegate sees the correct committed index when notified of navigation
2400 // state changes. (Otherwise CanGoBack may incorrectly return true, as in
2401 // https://crbug.com/1439948.)
Charlie Reisfbe5f1022023-10-03 15:21:212402 last_committed_entry_index_ = GetIndexOfEntry(entry);
Charlie Reis951f43372023-05-05 00:30:072403
Peter Boströmd7592132019-01-30 04:50:312404 // We should also usually discard the pending entry if it corresponds to a
2405 // different navigation, since that one is now likely canceled. In rare
2406 // cases, we leave the pending entry for another navigation in place when we
2407 // know it is still ongoing, to avoid a flicker in the omnibox (see
2408 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:432409 //
2410 // Note that we need to use the "internal" version since we don't want to
2411 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:312412 if (!keep_pending_entry)
Rakina Zata Amnia4e27222021-12-22 01:05:002413 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]e9ba4472008-09-14 15:42:432414}
2415
[email protected]d202a7c2012-01-04 07:53:472416void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:322417 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072418 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:362419 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:472420 bool replace_entry,
Shivani Sharmaeef521b2024-01-18 13:03:562421 bool previous_document_had_history_intervention_activation,
Rakina Zata Amnia4e27222021-12-22 01:05:002422 NavigationRequest* request,
2423 LoadCommittedDetails* commit_details) {
avi25f5f9e2015-07-17 20:08:262424 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2425 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
Rakina Zata Amniddf10502022-01-15 02:56:552426 // The NEW_SUBFRAME path should never result in an initial NavigationEntry.
2427 DCHECK(!commit_details->should_stay_as_initial_entry);
[email protected]09b8f82f2009-06-16 20:22:112428
[email protected]e9ba4472008-09-14 15:42:432429 // Manual subframe navigations just get the current entry cloned so the user
2430 // can go back or forward to it. The actual subframe information will be
2431 // stored in the page state for each of those entries. This happens out of
2432 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:092433 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
2434 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:382435
Mikel Astizba9cf2fd2017-12-17 10:38:102436 // The DCHECK below documents the fact that we don't know of any situation
2437 // where |replace_entry| is true for subframe navigations. This simplifies
2438 // reasoning about the replacement struct for subframes (see
2439 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
2440 DCHECK(!replace_entry);
2441
Patrick Monette50e8bd82019-06-13 22:40:452442 // This FrameNavigationEntry might not end up being used in the
2443 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Arthur Sonzognic686e8f2024-01-11 08:36:372444 const std::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452445 request->common_params().initiator_origin;
Arthur Sonzognic686e8f2024-01-11 08:36:372446 std::optional<GURL> initiator_base_url;
W. James MacLean8be423a2023-03-31 21:35:522447 if (params.url.IsAboutBlank() || params.url.IsAboutSrcdoc()) {
2448 initiator_base_url = request->common_params().initiator_base_url;
2449 }
Nate Chapin63db0d12022-01-20 22:03:302450 std::unique_ptr<PolicyContainerPolicies> policy_container_policies =
2451 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
Antonio Sartorib8addf62024-09-16 07:59:212452 request->DidEncounterError(),
Nate Chapin63db0d12022-01-20 22:03:302453 request->GetURL());
Domenic Denicolacc094fb2022-03-16 23:40:572454 bool protect_url_in_navigation_api = false;
Nate Chapin63db0d12022-01-20 22:03:302455 if (is_same_document) {
2456 FrameNavigationEntry* previous_frame_entry =
2457 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
Domenic Denicolacc094fb2022-03-16 23:40:572458 // Try to preserve protect_url_in_navigation_api from the previous
Nate Chapin63db0d12022-01-20 22:03:302459 // FrameNavigationEntry.
Domenic Denicolacc094fb2022-03-16 23:40:572460 protect_url_in_navigation_api =
Nate Chapin63db0d12022-01-20 22:03:302461 previous_frame_entry &&
Domenic Denicolacc094fb2022-03-16 23:40:572462 previous_frame_entry->protect_url_in_navigation_api();
Nate Chapin63db0d12022-01-20 22:03:302463 } else {
Domenic Denicolacc094fb2022-03-16 23:40:572464 protect_url_in_navigation_api =
Nate Chapin63db0d12022-01-20 22:03:302465 policy_container_policies &&
Domenic Denicolacc094fb2022-03-16 23:40:572466 ShouldProtectUrlInNavigationApi(
Nate Chapin63db0d12022-01-20 22:03:302467 policy_container_policies->referrer_policy);
2468 }
2469
Patrick Monette50e8bd82019-06-13 22:40:452470 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:482471 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Domenic Denicolacc094fb2022-03-16 23:40:572472 params.document_sequence_number, params.navigation_api_key,
Nate Chapinfbfe5af2021-06-10 17:22:082473 rfh->GetSiteInstance(), nullptr, params.url,
2474 GetCommittedOriginForFrameEntry(params, request),
W. James MacLean23e90a12022-12-21 04:38:212475 Referrer(*params.referrer), initiator_origin, initiator_base_url,
2476 request->GetRedirectChain(), params.page_state, params.method,
2477 params.post_id, nullptr /* blob_url_loader_factory */,
Domenic Denicolacc094fb2022-03-16 23:40:572478 std::move(policy_container_policies), protect_url_in_navigation_api);
Charles Reisf44482022017-10-13 21:15:032479
creisce0ef3572017-01-26 17:53:082480 std::unique_ptr<NavigationEntryImpl> new_entry =
2481 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:452482 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Ali Hijazid87307d2022-11-07 20:15:032483 frame_tree_->root());
creise062d542015-08-25 02:01:552484
Alexander Timine3ec4192020-04-20 16:39:402485 SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaeef521b2024-01-18 13:03:562486 replace_entry, previous_document_had_history_intervention_activation,
Alexander Timine3ec4192020-04-20 16:39:402487 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:472488
creisce0ef3572017-01-26 17:53:082489 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:452490 // to replace, which can happen due to a unique name change. See
2491 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
2492 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:382493
Dave Tapuska87696ae2021-11-18 18:48:312494 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false,
Abhijeet Kandalkare26014a92022-10-13 04:21:152495 rfh->IsNestedWithinFencedFrame(), commit_details);
[email protected]e9ba4472008-09-14 15:42:432496}
2497
[email protected]d202a7c2012-01-04 07:53:472498bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:322499 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072500 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392501 bool is_same_document,
Nate Chapinc7019dd7d2021-06-25 18:29:252502 bool was_on_initial_empty_document,
Rakina Zata Amnia4e27222021-12-22 01:05:002503 NavigationRequest* request,
2504 LoadCommittedDetails* commit_details) {
avi9f07a0c2015-02-18 22:51:292505 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2506 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2507
[email protected]e9ba4472008-09-14 15:42:432508 // We're guaranteed to have a previously committed entry, and we now need to
2509 // handle navigation inside of a subframe in it without creating a new entry.
2510 DCHECK(GetLastCommittedEntry());
2511
creis913c63ce2016-07-16 19:52:522512 // For newly created subframes, we don't need to send a commit notification.
2513 // This is only necessary for history navigations in subframes.
2514 bool send_commit_notification = false;
2515
Rakina Zata Amnif6950d552020-11-24 03:26:102516 // If |nav_entry_id| is non-zero and matches an existing entry, this
2517 // is a history navigation. Update the last committed index accordingly. If
2518 // we don't recognize the |nav_entry_id|, it might be a recently
2519 // pruned entry. We'll handle it below.
2520 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2521 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472522 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062523 // Make sure that a subframe commit isn't changing the main frame's
2524 // origin. Otherwise the renderer process may be confused, leading to a
2525 // URL spoof. We can't check the path since that may change
2526 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572527 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2528 // about:blank, file, and unique origins are more subtle to get right.
Charlie Reis95fbca442021-05-21 21:38:242529 // We should use checks similar to RenderFrameHostImpl's
2530 // CanCommitUrlAndOrigin on the main frame during subframe commits.
2531 // See https://crbug.com/1209092.
creis37988b92016-06-10 18:03:572532 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2533 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2534 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2535 dest_top_url.SchemeIsHTTPOrHTTPS() &&
Mike West800532c2021-10-14 09:26:522536 current_top_url.DeprecatedGetOriginAsURL() !=
2537 dest_top_url.DeprecatedGetOriginAsURL()) {
Chris Bookholt10f4b7332022-02-14 18:25:442538 bad_message::ReceivedBadMessage(rfh->GetMainFrame()->GetProcess(),
creisfb6eeb62016-05-10 19:01:512539 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062540 }
creis3cdc3b02015-05-29 23:00:472541
creis913c63ce2016-07-16 19:52:522542 // We only need to discard the pending entry in this history navigation
2543 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062544 last_committed_entry_index_ = entry_index;
Rakina Zata Amnia4e27222021-12-22 01:05:002545 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
creis913c63ce2016-07-16 19:52:522546
2547 // History navigations should send a commit notification.
2548 send_commit_notification = true;
avi98405c22015-05-21 20:47:062549 }
[email protected]e9ba4472008-09-14 15:42:432550 }
[email protected]f233e4232013-02-23 00:55:142551
creisce0ef3572017-01-26 17:53:082552 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2553 // it may be a "history auto" case where we update an existing one.
Nate Chapin9f169072021-06-09 19:32:372554 // We may want to update |last_committed|'s FrameNavigationEntry (if one
2555 // exists), or we may want to clobber it and create a new one. We update in
2556 // cases where the corresponding FrameNavigationEntry is conceptually similar
2557 // to the document described by the commit params: same-document
2558 // navigations, history traversal to an existing entry, and reloads (including
2559 // a "soft reload" where we navigate to the same url without flagging it as a
2560 // reload). But in the case of a different document that is not logically
2561 // related to the committed FrameNavigationEntry's document (cross-document,
2562 // not same url, not a reload, not a history traversal), we replace rather
2563 // than update.
Nate Chapinc7019dd7d2021-06-25 18:29:252564 //
Nate Chapin9f169072021-06-09 19:32:372565 // In the case where we update, the FrameNavigationEntry will potentially be
2566 // shared across multiple NavigationEntries, and any updates will be reflected
2567 // in all of those NavigationEntries. In the replace case, any existing
2568 // sharing with other NavigationEntries will stop.
Nate Chapinc7019dd7d2021-06-25 18:29:252569 //
2570 // When navigating away from the initial empty document, we also update rather
2571 // than replace. Either update or replace will overwrite the initial empty
2572 // document state for |last_committed|, but if the FrameNavigationEntry for
2573 // the initial empty document is shared across multiple NavigationEntries (due
2574 // to a navigation in another frame), we want to make sure we overwrite the
2575 // initial empty document state everywhere this FrameNavigationEntry is used,
2576 // which is accompished by updating the existing FrameNavigationEntry.
Rakina Zata Amnie2d31312022-11-18 03:38:452577 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Nate Chapin9f169072021-06-09 19:32:372578 FrameNavigationEntry* last_committed_frame_entry =
2579 last_committed->GetFrameEntry(rfh->frame_tree_node());
2580 NavigationEntryImpl::UpdatePolicy update_policy =
2581 NavigationEntryImpl::UpdatePolicy::kUpdate;
2582 if (request->common_params().navigation_type ==
Minggang Wangb9f3fa92021-07-01 15:30:312583 blink::mojom::NavigationType::DIFFERENT_DOCUMENT &&
Nate Chapin9f169072021-06-09 19:32:372584 last_committed_frame_entry &&
Nate Chapinc7019dd7d2021-06-25 18:29:252585 last_committed_frame_entry->url() != params.url &&
2586 !was_on_initial_empty_document) {
Nate Chapin9f169072021-06-09 19:32:372587 update_policy = NavigationEntryImpl::UpdatePolicy::kReplace;
2588 }
2589
Rakina Zata Amni3460d382021-10-29 00:43:372590 UpdateNavigationEntryDetails(last_committed, rfh, params, request,
Rakina Zata Amnia4e27222021-12-22 01:05:002591 update_policy, false /* is_new_entry */,
2592 commit_details);
creis625a0c7d2015-03-24 23:17:122593
creis913c63ce2016-07-16 19:52:522594 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432595}
2596
[email protected]d202a7c2012-01-04 07:53:472597int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232598 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032599 for (size_t i = 0; i < entries_.size(); ++i) {
2600 if (entries_[i].get() == entry)
2601 return i;
2602 }
2603 return -1;
[email protected]765b35502008-08-21 00:51:202604}
2605
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572606void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242607 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572608 NavigationControllerImpl* source =
2609 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572610 // Verify that we look new.
Rakina Zata Amni46087a12022-11-11 08:28:382611 DCHECK_EQ(1, GetEntryCount());
2612 DCHECK(GetLastCommittedEntry()->IsInitialEntry());
Lei Zhang96031532019-10-10 19:05:472613 DCHECK(!GetPendingEntry());
Rakina Zata Amniafd3c6582021-11-30 06:19:172614 entries_.clear();
[email protected]ce3fa3c2009-04-20 19:55:572615
Francois Dorayeaace782017-06-21 16:37:242616 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442617 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572618 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572619
Fergal Dalya1d569972021-03-16 03:24:532620 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302621 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532622 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2623 session_storage_namespace_map_[it.first] = source_namespace->Clone();
[email protected]fdac6ade2013-07-20 01:06:302624 }
[email protected]4e6419c2010-01-15 04:50:342625
Lukasz Anforowicz0de0f452020-12-02 19:57:152626 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572627}
2628
[email protected]79368982013-11-13 01:11:012629bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
Adithya Srinivasane8094c82024-06-26 22:45:022630 // If there is no last committed entry, we cannot prune.
[email protected]474f8512013-05-31 22:31:162631 if (last_committed_entry_index_ == -1)
2632 return false;
[email protected]9350602e2013-02-26 23:27:442633
[email protected]474f8512013-05-31 22:31:162634 // We cannot prune if there is a pending entry at an existing entry index.
2635 // It may not commit, so we have to keep the last committed entry, and thus
2636 // there is no sensible place to keep the pending entry. It is ok to have
2637 // a new pending entry, which can optionally commit as a new navigation.
2638 if (pending_entry_index_ != -1)
2639 return false;
2640
[email protected]474f8512013-05-31 22:31:162641 return true;
2642}
2643
[email protected]79368982013-11-13 01:11:012644void NavigationControllerImpl::PruneAllButLastCommitted() {
2645 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162646
avi2b177592014-12-10 02:08:022647 DCHECK_EQ(0, last_committed_entry_index_);
2648 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442649
Charlie Reis99b2eba22025-01-31 19:18:572650 BroadcastHistoryIndexAndLength();
[email protected]9350602e2013-02-26 23:27:442651}
2652
[email protected]79368982013-11-13 01:11:012653void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162654 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012655 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262656
Nate Chapin9eb16be72022-09-23 22:54:312657 RemovedEntriesTracker tracker(weak_factory_.GetSafeRef());
2658
[email protected]474f8512013-05-31 22:31:162659 // Erase all entries but the last committed entry. There may still be a
2660 // new pending entry after this.
2661 entries_.erase(entries_.begin(),
2662 entries_.begin() + last_committed_entry_index_);
2663 entries_.erase(entries_.begin() + 1, entries_.end());
2664 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262665}
2666
Christian Dullweber1af31e62018-02-22 11:49:482667void NavigationControllerImpl::DeleteNavigationEntries(
2668 const DeletionPredicate& deletionPredicate) {
2669 // It is up to callers to check the invariants before calling this.
2670 CHECK(CanPruneAllButLastCommitted());
2671 std::vector<int> delete_indices;
2672 for (size_t i = 0; i < entries_.size(); i++) {
2673 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572674 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482675 delete_indices.push_back(i);
2676 }
2677 }
2678 if (delete_indices.empty())
2679 return;
2680
2681 if (delete_indices.size() == GetEntryCount() - 1U) {
2682 PruneAllButLastCommitted();
2683 } else {
2684 // Do the deletion in reverse to preserve indices.
Ayu Ishii2f825852022-03-08 19:47:382685 for (const auto& index : base::Reversed(delete_indices)) {
2686 RemoveEntryAtIndex(index);
Christian Dullweber1af31e62018-02-22 11:49:482687 }
Charlie Reis99b2eba22025-01-31 19:18:572688 BroadcastHistoryIndexAndLength();
Christian Dullweber1af31e62018-02-22 11:49:482689 }
2690 delegate()->NotifyNavigationEntriesDeleted();
2691}
2692
Carlos Caballero35ce710c2019-09-19 10:59:452693BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2694 return back_forward_cache_;
2695}
2696
William Liu055a3542023-04-02 17:21:192697NavigationEntryScreenshotCache*
2698NavigationControllerImpl::GetNavigationEntryScreenshotCache() {
Takashi Toyoshima7c041d82023-09-26 16:09:212699 CHECK(frame_tree_->is_primary());
Khushal Sagar94ea2dc2024-08-06 17:15:262700 if (!nav_entry_screenshot_cache_ &&
2701 NavigationTransitionConfig::AreBackForwardTransitionsEnabled()) {
William Liu055a3542023-04-02 17:21:192702 nav_entry_screenshot_cache_ =
2703 std::make_unique<NavigationEntryScreenshotCache>(
2704 BrowserContextImpl::From(browser_context_)
2705 ->GetNavigationEntryScreenshotManager()
2706 ->GetSafeRef(),
2707 this);
2708 }
2709 return nav_entry_screenshot_cache_.get();
2710}
2711
clamy987a3752018-05-03 17:36:262712void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2713 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2714 // progress, since this will cause a use-after-free. (We only allow this
2715 // when the tab is being destroyed for shutdown, since it won't return to
2716 // NavigateToEntry in that case.) http://crbug.com/347742.
Ali Hijazid87307d2022-11-07 20:15:032717 CHECK(!in_navigate_to_pending_entry_ || frame_tree_->IsBeingDestroyed());
Minoru Chikamune13ab4de2025-05-14 21:54:072718 // If `was_failure` is true, it means that the pending entry was discarded by
2719 // a `PendingEntryRefDeleted` call within `OnRequestFailedInternal`, in
2720 // response to a navigation request failure. This case is not at risk for
2721 // re-entrancy when `can_be_in_navigate_to_pending_entry_` is true, because
2722 // that code also creates another `PendingEntryRef` that would prevent the
2723 // `DiscardPendingEntry` call if the PostTask were skipped. See
2724 // https://crbug.com/411855273.
2725 if (!was_failure && can_be_in_navigate_to_pending_entry_ &&
Minoru Chikamune646eba42025-04-14 01:25:032726 !frame_tree_->IsBeingDestroyed()) {
2727 CheckPotentialNavigationReentrancy();
2728 }
clamy987a3752018-05-03 17:36:262729
2730 if (was_failure && pending_entry_) {
2731 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2732 } else {
2733 failed_pending_entry_id_ = 0;
2734 }
2735
2736 if (pending_entry_) {
2737 if (pending_entry_index_ == -1)
Paul Semel7e51469e2022-07-12 12:16:332738 pending_entry_.ClearAndDelete();
clamy987a3752018-05-03 17:36:262739 pending_entry_index_ = -1;
2740 pending_entry_ = nullptr;
2741 }
arthursonzogni66f711c2019-10-08 14:40:362742
2743 // Ensure any refs to the current pending entry are ignored if they get
2744 // deleted, by clearing the set of known refs. All future pending entries will
2745 // only be affected by new refs.
2746 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262747}
2748
2749void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2750 if (pending_entry_)
2751 pending_entry_->set_ssl_error(error);
2752}
2753
Xiaohan Wang7f8052e02022-01-14 18:44:282754#if BUILDFLAG(IS_ANDROID)
Camille Lamy5193caa2018-10-12 11:59:422755// static
2756bool NavigationControllerImpl::ValidateDataURLAsString(
2757 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2758 if (!data_url_as_string)
2759 return false;
2760
2761 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2762 return false;
2763
2764 // The number of characters that is enough for validating a data: URI.
2765 // From the GURL's POV, the only important part here is scheme, it doesn't
2766 // check the actual content. Thus we can take only the prefix of the url, to
2767 // avoid unneeded copying of a potentially long string.
danakj529a3eba2024-04-18 20:14:562768 constexpr size_t kDataUriPrefixMaxLen = 64;
2769 const size_t len = std::min(data_url_as_string->size(), kDataUriPrefixMaxLen);
2770 GURL data_url(base::as_string_view(*data_url_as_string).substr(0u, len));
Camille Lamy5193caa2018-10-12 11:59:422771 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2772 return false;
2773
2774 return true;
2775}
2776#endif
2777
Shivani Sharma194877032019-03-07 17:52:472778void NavigationControllerImpl::NotifyUserActivation() {
2779 // When a user activation occurs, ensure that all adjacent entries for the
2780 // same document clear their skippable bit, so that the history manipulation
2781 // intervention does not apply to them.
Lijin Shen9c475d32023-09-02 00:15:012782 const bool can_go_back = CanGoBack();
Shivani Sharmac4cc8922019-04-18 03:11:172783 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Lijin Shen9c475d32023-09-02 00:15:012784 // If the value of CanGoBack changes as a result of making some entries
2785 // non-skippable, then we must let the delegate know to update its UI state.
2786 // See https://crbug.com/1477784.
2787 if (!can_go_back && CanGoBack()) {
2788 delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_ALL);
2789 }
Shivani Sharma194877032019-03-07 17:52:472790}
2791
clamy987a3752018-05-03 17:36:262792bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2793 RenderFrameHostImpl* render_frame_host,
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:332794 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client,
2795 blink::LocalFrameToken initiator_frame_token,
Charlie Reisf4d51f402025-05-23 18:00:492796 int initiator_process_id,
2797 base::TimeTicks actual_navigation_start) {
clamy987a3752018-05-03 17:36:262798 NavigationEntryImpl* entry =
2799 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2800 if (!entry)
2801 return false;
2802
2803 FrameNavigationEntry* frame_entry =
2804 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2805 if (!frame_entry)
2806 return false;
2807
Camille Lamy5193caa2018-10-12 11:59:422808 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572809 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232810 ReloadType::NONE, false /* is_same_document_history_load */,
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:332811 true /* is_history_navigation_in_new_child */, initiator_frame_token,
Charlie Reisf4d51f402025-05-23 18:00:492812 initiator_process_id, actual_navigation_start);
clamyea99ea12018-05-28 13:54:232813
2814 if (!request)
2815 return false;
2816
arthursonzognif046d4a2019-12-12 19:08:102817 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412818
Rakina Zata Amni1c83b082023-02-08 01:09:002819 SCOPED_CRASH_KEY_STRING256(
2820 "Bug1400009", "req_url",
2821 request->GetURL().GetWithEmptyPath().possibly_invalid_spec());
2822 SCOPED_CRASH_KEY_NUMBER(
2823 "Bug1400009", "nav_entry_si",
2824 entry->site_instance() ? ((int)entry->site_instance()->GetId()) : -1);
2825 SCOPED_CRASH_KEY_NUMBER("Bug1400009", "fne_si",
2826 frame_entry->site_instance()
2827 ? ((int)frame_entry->site_instance()->GetId())
2828 : -1);
2829 bool has_sig =
2830 (frame_entry->site_instance() && frame_entry->site_instance()->group());
2831 SCOPED_CRASH_KEY_BOOL("Bug1400009", "fne_sig_exists", has_sig);
2832 SCOPED_CRASH_KEY_BOOL("Bug1400009", "fne_sig_has_rvh",
2833 has_sig ? (!!frame_tree_->GetRenderViewHost(
2834 frame_entry->site_instance()->group()))
2835 : false);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052836 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2837 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232838
2839 return true;
clamy987a3752018-05-03 17:36:262840}
2841
Tsuyoshi Horo52fd08e2020-07-07 07:03:452842bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
Charlie Reisf4d51f402025-05-23 18:00:492843 base::TimeTicks actual_navigation_start = base::TimeTicks::Now();
Tsuyoshi Horo52fd08e2020-07-07 07:03:452844 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2845 if (!entry)
2846 return false;
Rakina Zata Amnif297a802022-01-18 03:53:432847
2848 if (entry->IsInitialEntryNotForSynchronousAboutBlank()) {
2849 // We should never navigate to an existing initial NavigationEntry that is
2850 // the initial NavigationEntry for the initial empty document that hasn't
2851 // been overridden by the synchronous about:blank commit, to preserve
2852 // legacy behavior where trying to reload when the main frame is on the
2853 // initial empty document won't result in a navigation. See also
2854 // https://crbug.com/1277414.
2855 return false;
2856 }
Tsuyoshi Horo52fd08e2020-07-07 07:03:452857 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2858 if (!frame_entry)
2859 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142860 ReloadType reload_type = ReloadType::NORMAL;
2861 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452862 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142863 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452864 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432865 false /* is_history_navigation_in_new_child */,
Arthur Sonzognic686e8f2024-01-11 08:36:372866 std::nullopt /* initiator_frame_token */,
Charlie Reisf4d51f402025-05-23 18:00:492867 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
2868 actual_navigation_start);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452869 if (!request)
2870 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052871 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452872 return true;
2873}
2874
clamy987a3752018-05-03 17:36:262875void NavigationControllerImpl::NavigateFromFrameProxy(
2876 RenderFrameHostImpl* render_frame_host,
2877 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022878 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452879 int initiator_process_id,
Arthur Sonzognic686e8f2024-01-11 08:36:372880 const std::optional<url::Origin>& initiator_origin,
2881 const std::optional<GURL>& initiator_base_url,
clamy987a3752018-05-03 17:36:262882 bool is_renderer_initiated,
2883 SiteInstance* source_site_instance,
2884 const Referrer& referrer,
2885 ui::PageTransition page_transition,
2886 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252887 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262888 const std::string& method,
2889 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092890 const std::string& extra_headers,
Antonio Sartori2f763d9d2021-04-21 10:04:142891 network::mojom::SourceLocationPtr source_location,
John Delaney50425f82020-04-07 16:26:212892 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
jongdeok.kim5de823b32022-06-14 04:37:502893 bool is_form_submission,
Arthur Sonzognic686e8f2024-01-11 08:36:372894 const std::optional<blink::Impression>& impression,
Yao Xiao720ef9d62022-12-09 05:18:292895 blink::mojom::NavigationInitiatorActivationAndAdStatus
2896 initiator_activation_and_ad_status,
Charlie Reise1d9b8182025-04-02 04:32:122897 base::TimeTicks actual_navigation_start_time,
Nan Lin944e9b4e2022-04-12 13:51:222898 base::TimeTicks navigation_start_time,
Garrett Tanzer405f3402022-07-21 20:12:492899 bool is_embedder_initiated_fenced_frame_navigation,
Garrett Tanzerbb8db412022-09-27 21:59:462900 bool is_unfenced_top_navigation,
Sergey Poromovdd557c12023-03-01 11:28:452901 bool force_new_browsing_instance,
Camillia Smith Barnes6a643962023-03-03 00:28:582902 bool is_container_initiated,
Kevin McNee6455638a2024-06-27 22:05:032903 bool has_rel_opener,
W. James MacLean443ef3e2024-07-16 13:42:342904 net::StorageAccessApiStatus storage_access_api_status,
Arthur Sonzognic686e8f2024-01-11 08:36:372905 std::optional<std::u16string> embedder_shared_storage_context) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582906 if (is_renderer_initiated)
2907 DCHECK(initiator_origin.has_value());
2908
clamy987a3752018-05-03 17:36:262909 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582910
Rakina Zata Amni2322f4f82022-01-24 13:24:242911 // Don't allow an entry replacement if there is no entry to replace.
2912 // http://crbug.com/457149
2913 if (GetEntryCount() == 0)
2914 should_replace_current_entry = false;
2915
clamy987a3752018-05-03 17:36:262916 // Create a NavigationEntry for the transfer, without making it the pending
2917 // entry. Subframe transfers should have a clone of the last committed entry
2918 // with a FrameNavigationEntry for the target frame. Main frame transfers
2919 // should have a new NavigationEntry.
2920 // TODO(creis): Make this unnecessary by creating (and validating) the params
2921 // directly, passing them to the destination RenderFrameHost. See
2922 // https://crbug.com/536906.
2923 std::unique_ptr<NavigationEntryImpl> entry;
Harkiran Bolariae1b5158b2021-09-16 19:03:262924 if (!render_frame_host->is_main_frame()) {
clamy987a3752018-05-03 17:36:262925 // Subframe case: create FrameNavigationEntry.
Rakina Zata Amnie2d31312022-11-18 03:38:452926 DCHECK(GetLastCommittedEntry());
2927 entry = GetLastCommittedEntry()->Clone();
2928 entry->set_extra_headers(extra_headers);
Rakina Zata Amniafd3c6582021-11-30 06:19:172929 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2930 // Renderer-initiated navigation that target a remote frame are currently
2931 // classified as browser-initiated when this one has already navigated.
2932 // See https://crbug.com/722251.
Nate Chapin9f169072021-06-09 19:32:372933 // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter
2934 // to CreateNavigationRequestFromLoadParams(), so while kReplace might
2935 // remove child FrameNavigationEntries (e.g., if this is a cross-process
2936 // same-document navigation), they will still be present in the
2937 // committed NavigationEntry that will be referenced to construct the new
2938 // FrameNavigationEntry tree when this navigation commits.
clamy987a3752018-05-03 17:36:262939 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:082940 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582941 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Arthur Sonzognic686e8f2024-01-11 08:36:372942 std::nullopt /* commit_origin */, referrer, initiator_origin,
W. James MacLean23e90a12022-12-21 04:38:212943 initiator_base_url, std::vector<GURL>(), blink::PageState(), method, -1,
Kunihiko Sakamoto2ae79e62023-05-26 00:34:152944 blob_url_loader_factory, nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262945 } else {
2946 // Main frame case.
Julie Jeongeun Kim5b9aff72022-05-02 02:10:172947 // If `node` is the outermost main frame, it rewrites a virtual url in order
2948 // to adjust the original input url if needed. For inner frames such as
2949 // fenced frames or subframes, they don't rewrite urls as the urls are not
2950 // input urls by users.
2951 bool rewrite_virtual_urls = node->IsOutermostMainFrame();
Arthur Sonzognic686e8f2024-01-11 08:36:372952 std::optional<GURL> source_process_site_url = std::nullopt;
Sharon Yang242ef822023-05-15 21:07:322953 if (source_site_instance && source_site_instance->HasProcess()) {
2954 source_process_site_url =
2955 source_site_instance->GetProcess()->GetProcessLock().site_url();
2956 }
clamy987a3752018-05-03 17:36:262957 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
W. James MacLean23e90a12022-12-21 04:38:212958 url, referrer, initiator_origin, initiator_base_url,
Sharon Yang242ef822023-05-15 21:07:322959 source_process_site_url, page_transition, is_renderer_initiated,
W. James MacLean23e90a12022-12-21 04:38:212960 extra_headers, browser_context_, blob_url_loader_factory,
2961 rewrite_virtual_urls));
clamy987a3752018-05-03 17:36:262962 entry->root_node()->frame_entry->set_source_site_instance(
2963 static_cast<SiteInstanceImpl*>(source_site_instance));
2964 entry->root_node()->frame_entry->set_method(method);
2965 }
clamy987a3752018-05-03 17:36:262966
Camille Lamy5193caa2018-10-12 11:59:422967 bool override_user_agent = false;
Rakina Zata Amnie2d31312022-11-18 03:38:452968 if (GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
clamy987a3752018-05-03 17:36:262969 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422970 override_user_agent = true;
clamy987a3752018-05-03 17:36:262971 }
2972 // TODO(creis): Set user gesture and intent received timestamp on Android.
2973
2974 // We may not have successfully added the FrameNavigationEntry to |entry|
2975 // above (per https://crbug.com/608402), in which case we create it from
2976 // scratch. This works because we do not depend on |frame_entry| being inside
2977 // |entry| during NavigateToEntry. This will go away when we shortcut this
2978 // further in https://crbug.com/536906.
2979 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2980 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452981 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Nate Chapinfbfe5af2021-06-10 17:22:082982 node->unique_name(), -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582983 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Arthur Sonzognic686e8f2024-01-11 08:36:372984 std::nullopt /* origin */, referrer, initiator_origin,
W. James MacLean23e90a12022-12-21 04:38:212985 initiator_base_url, std::vector<GURL>(), blink::PageState(), method, -1,
Kunihiko Sakamoto2ae79e62023-05-26 00:34:152986 blob_url_loader_factory, nullptr /* policy_container_policies */,
Domenic Denicolacc094fb2022-03-16 23:40:572987 false /* protect_url_in_navigation_api */);
clamy987a3752018-05-03 17:36:262988 }
2989
Camille Lamy5193caa2018-10-12 11:59:422990 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022991 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452992 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292993 params.initiator_origin = initiator_origin;
W. James MacLean23e90a12022-12-21 04:38:212994 params.initiator_base_url = initiator_base_url;
Camille Lamy5193caa2018-10-12 11:59:422995 params.source_site_instance = source_site_instance;
2996 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2997 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032998 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422999 params.referrer = referrer;
3000 /* params.redirect_chain: skip */
3001 params.extra_headers = extra_headers;
3002 params.is_renderer_initiated = is_renderer_initiated;
3003 params.override_user_agent = UA_OVERRIDE_INHERIT;
3004 /* params.base_url_for_data_url: skip */
Shu Yang112ad492024-07-25 17:11:543005 /* params.virtual_url_for_special_cases: skip */
Camille Lamy5193caa2018-10-12 11:59:423006 /* params.data_url_as_string: skip */
3007 params.post_data = post_body;
3008 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:583009 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:423010 /* params.frame_name: skip */
3011 // TODO(clamy): See if user gesture should be propagated to this function.
3012 params.has_user_gesture = false;
3013 params.should_clear_history_list = false;
3014 params.started_from_context_menu = false;
3015 /* params.navigation_ui_data: skip */
3016 /* params.input_start: skip */
Minggang Wangf59db47b2021-06-16 01:56:223017 params.was_activated = blink::mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:543018 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:213019 params.impression = impression;
Antonio Sartori6984c742021-08-26 08:03:413020 params.download_policy = std::move(download_policy);
jongdeok.kim5de823b32022-06-14 04:37:503021 params.is_form_submission = is_form_submission;
Yao Xiao720ef9d62022-12-09 05:18:293022 params.initiator_activation_and_ad_status =
3023 initiator_activation_and_ad_status;
Kevin McNee6455638a2024-06-27 22:05:033024 params.has_rel_opener = has_rel_opener;
Camille Lamy5193caa2018-10-12 11:59:423025
3026 std::unique_ptr<NavigationRequest> request =
3027 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:033028 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143029 false /* has_user_gesture */, std::move(source_location),
Tsuyoshi Horo167ca6432022-03-09 05:16:393030 ReloadType::NONE, entry.get(), frame_entry.get(),
Charlie Reise1d9b8182025-04-02 04:32:123031 actual_navigation_start_time, navigation_start_time,
3032 is_embedder_initiated_fenced_frame_navigation,
Camillia Smith Barnes6a643962023-03-03 00:28:583033 is_unfenced_top_navigation, is_container_initiated,
W. James MacLean443ef3e2024-07-16 13:42:343034 storage_access_api_status, embedder_shared_storage_context);
clamyea99ea12018-05-28 13:54:233035
3036 if (!request)
3037 return;
3038
Garrett Tanzerbb8db412022-09-27 21:59:463039 // Force the navigation to take place in a new browsing instance.
3040 // This is used by _unfencedTop in fenced frames to ensure that navigations
3041 // leaving the fenced context create a new browsing instance.
3042 if (force_new_browsing_instance) {
3043 request->coop_status().ForceBrowsingInstanceSwap();
3044 }
3045
Arthur Hemery948742762019-09-18 10:06:243046 // At this stage we are proceeding with this navigation. If this was renderer
3047 // initiated with user gesture, we need to make sure we clear up potential
3048 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
3049 DiscardNonCommittedEntries();
3050
Lukasz Anforowicz9ee83c272020-12-01 20:14:053051 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:263052}
3053
[email protected]d1198fd2012-08-13 22:50:193054void NavigationControllerImpl::SetSessionStorageNamespace(
Alex Moshchuk8015afcf2022-01-31 22:59:253055 const StoragePartitionConfig& partition_config,
[email protected]8ff00d72012-10-23 19:12:213056 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:193057 if (!session_storage_namespace)
3058 return;
3059
3060 // We can't overwrite an existing SessionStorage without violating spec.
3061 // Attempts to do so may give a tab access to another tab's session storage
3062 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:153063 bool successful_insert =
3064 session_storage_namespace_map_
Alex Moshchuk8015afcf2022-01-31 22:59:253065 .insert(std::make_pair(partition_config,
Aaron Colwellf3b316e2021-03-11 20:17:053066 static_cast<SessionStorageNamespaceImpl*>(
3067 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:303068 .second;
3069 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
[email protected]d1198fd2012-08-13 22:50:193070}
3071
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573072bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Rakina Zata Amnie2d31312022-11-18 03:38:453073 return IsInitialNavigation() && GetLastCommittedEntry()->IsInitialEntry() &&
Ali Hijazid87307d2022-11-07 20:15:033074 !frame_tree_->has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:463075}
3076
Aran Gilman37d11632019-10-08 23:07:153077SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Alex Moshchuk8015afcf2022-01-31 22:59:253078 const StoragePartitionConfig& partition_config) {
[email protected]fdac6ade2013-07-20 01:06:303079 StoragePartition* partition =
Lukasz Anforowiczb9a969a2021-04-29 15:26:253080 browser_context_->GetStoragePartition(partition_config);
michaelnbacbcbd2016-02-09 00:32:033081 DOMStorageContextWrapper* context_wrapper =
3082 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
3083
3084 SessionStorageNamespaceMap::const_iterator it =
Alex Moshchuk8015afcf2022-01-31 22:59:253085 session_storage_namespace_map_.find(partition_config);
michaelnbacbcbd2016-02-09 00:32:033086 if (it != session_storage_namespace_map_.end()) {
3087 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:153088 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
3089 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:473090
michaelnbacbcbd2016-02-09 00:32:033091 return it->second.get();
3092 }
3093
3094 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:103095 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
3096 SessionStorageNamespaceImpl::Create(context_wrapper);
3097 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
3098 session_storage_namespace.get();
Alex Moshchuk8015afcf2022-01-31 22:59:253099 session_storage_namespace_map_[partition_config] =
Daniel Murphy31bbb8b12018-02-07 21:44:103100 std::move(session_storage_namespace);
[email protected]fdac6ade2013-07-20 01:06:303101
Daniel Murphy31bbb8b12018-02-07 21:44:103102 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:303103}
3104
3105SessionStorageNamespace*
3106NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Alex Moshchuk8015afcf2022-01-31 22:59:253107 return GetSessionStorageNamespace(
3108 StoragePartitionConfig::CreateDefault(GetBrowserContext()));
[email protected]fdac6ade2013-07-20 01:06:303109}
3110
3111const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573112NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:303113 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:553114}
[email protected]d202a7c2012-01-04 07:53:473115
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573116bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:563117 return needs_reload_;
3118}
[email protected]a26023822011-12-29 00:23:553119
[email protected]46bb5e9c2013-10-03 22:16:473120void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:413121 SetNeedsReload(NeedsReloadType::kRequestedByClient);
3122}
3123
3124void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:473125 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:413126 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:543127
3128 if (last_committed_entry_index_ != -1) {
3129 entries_[last_committed_entry_index_]->SetTransitionType(
3130 ui::PAGE_TRANSITION_RELOAD);
3131 }
[email protected]46bb5e9c2013-10-03 22:16:473132}
3133
[email protected]d202a7c2012-01-04 07:53:473134void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:573135 DCHECK_LT(index, GetEntryCount());
3136 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:313137 DiscardNonCommittedEntries();
3138
Nate Chapin9eb16be72022-09-23 22:54:313139 RemovedEntriesTracker tracker(weak_factory_.GetSafeRef());
[email protected]43032342011-03-21 14:10:313140 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:123141 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:313142 last_committed_entry_index_--;
3143}
3144
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573145NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:273146 // If there is no pending_entry_, there should be no pending_entry_index_.
3147 DCHECK(pending_entry_ || pending_entry_index_ == -1);
3148
3149 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:363150 // at that index. An exception is while a reload of a post commit error page
3151 // is ongoing; in that case pending entry will point to the entry replaced
3152 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:273153 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:363154 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
3155 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:273156
[email protected]022af742011-12-28 18:37:253157 return pending_entry_;
3158}
3159
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573160int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:273161 // The pending entry index must always be less than the number of entries.
arthursonzogni5c4c202d2017-04-25 23:41:273162 DCHECK_LT(pending_entry_index_, GetEntryCount());
[email protected]a26023822011-12-29 00:23:553163 return pending_entry_index_;
3164}
3165
avi25764702015-06-23 15:43:373166void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:573167 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:363168 bool replace,
Dave Tapuska87696ae2021-11-18 18:48:313169 bool was_post_commit_error,
Rakina Zata Amnia4e27222021-12-22 01:05:003170 bool in_fenced_frame_tree,
3171 LoadCommittedDetails* commit_details) {
Dave Tapuska87696ae2021-11-18 18:48:313172 // Fenced frame trees should always have `ui::PAGE_TRANSITION_AUTO_SUBFRAME`
3173 // set because:
3174 // 1) They don't influence the history of the outer page.
3175 // 2) They are always replace only navigation (there is always only one entry
3176 // in their history stack).
3177 // 3) Are not top level navigations and appear similar to iframes.
3178 // Navigations of the fenced frame might create a new NavigationEntry, which
3179 // will call this function. Non fenced frame navigations will never have
3180 // `ui::PAGE_TRANSITION_AUTO_SUBFRAME` because they won't call
3181 // InsertOrReplaceEntry.
3182 DCHECK_EQ(in_fenced_frame_tree,
3183 ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
3184 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:203185
avi5cad4912015-06-19 05:25:443186 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
3187 // need to keep continuity with the pending entry, so copy the pending entry's
3188 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
3189 // then the renderer navigated on its own, independent of the pending entry,
3190 // so don't copy anything.
3191 if (pending_entry_ && pending_entry_index_ == -1)
3192 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:203193
Rakina Zata Amnia4e27222021-12-22 01:05:003194 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]765b35502008-08-21 00:51:203195
creisee17e932015-07-17 17:56:223196 // When replacing, don't prune the forward history.
Rakina Zata Amnie2d31312022-11-18 03:38:453197 if (replace || was_post_commit_error) {
Mikel Astizba9cf2fd2017-12-17 10:38:103198 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573199 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:363200 // If the new entry is a post-commit error page, we store the current last
3201 // committed entry to the side so that we can put it back when navigating
3202 // away from the error.
3203 if (was_post_commit_error) {
3204 DCHECK(!entry_replaced_by_post_commit_error_);
3205 entry_replaced_by_post_commit_error_ =
3206 std::move(entries_[last_committed_entry_index_]);
3207 }
dcheng36b6aec92015-12-26 06:16:363208 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:223209 return;
3210 }
[email protected]765b35502008-08-21 00:51:203211
creis37979a62015-08-04 19:48:183212 // We shouldn't see replace == true when there's no committed entries.
3213 DCHECK(!replace);
3214
Michael Thiessen9b14d512019-09-23 21:19:473215 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:203216
Shivani Sharmad8c8d652019-02-13 17:27:573217 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:203218
dcheng36b6aec92015-12-26 06:16:363219 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:203220 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:293221}
3222
Shivani Sharmad8c8d652019-02-13 17:27:573223void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:163224 if (entries_.size() < max_entry_count())
3225 return;
3226
3227 DCHECK_EQ(max_entry_count(), entries_.size());
3228 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:573229 CHECK_EQ(pending_entry_index_, -1);
3230
3231 int index = 0;
Elly Fong-Jonesccc6d1f2021-06-14 18:32:423232 // Retrieve the oldest skippable entry.
3233 for (; index < GetEntryCount(); index++) {
3234 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
3235 break;
Shivani Sharmad8c8d652019-02-13 17:27:573236 }
3237
3238 // If there is no skippable entry or if it is the last committed entry then
3239 // fall back to pruning the oldest entry. It is not safe to prune the last
3240 // committed entry.
3241 if (index == GetEntryCount() || index == last_committed_entry_index_)
3242 index = 0;
3243
3244 bool should_succeed = RemoveEntryAtIndex(index);
3245 DCHECK_EQ(true, should_succeed);
3246
3247 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:253248}
3249
William Liu62ae26c2024-08-08 14:28:163250std::vector<base::WeakPtr<NavigationRequest>>
William Liuec04e382024-05-23 18:03:273251NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:163252 ReloadType reload_type,
Nate Chapinbf682fa32022-09-26 22:41:203253 RenderFrameHostImpl* initiator_rfh,
Arthur Sonzognic686e8f2024-01-11 08:36:373254 std::optional<blink::scheduler::TaskAttributionId>
Yoav Weiss8c573952022-11-17 17:35:133255 soft_navigation_heuristics_task_id,
Charlie Reisf4d51f402025-05-23 18:00:493256 const std::string* navigation_api_key,
3257 base::TimeTicks actual_navigation_start) {
Alexander Timin3a92df72019-09-20 11:59:503258 TRACE_EVENT0("navigation",
3259 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:273260 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:023261 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:363262 if (pending_entry_index_ != -1) {
3263 // The pending entry may not be in entries_ if a post-commit error page is
3264 // showing.
3265 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
3266 pending_entry_ == entry_replaced_by_post_commit_error_.get());
3267 }
Gyuyoung Kim107c2a02021-04-13 01:49:303268 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:573269 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:013270 needs_reload_ = false;
Ali Hijazid87307d2022-11-07 20:15:033271 FrameTreeNode* root = frame_tree_->root();
Arthur Sonzogni620cec62018-12-13 13:08:573272 int nav_entry_id = pending_entry_->GetUniqueID();
Yoav Weiss8c573952022-11-17 17:35:133273 // Only pass down the soft_navigation_heuristics_task_id when the initiator is
3274 // the same as the top level frame being navigated.
3275 if (root->current_frame_host() != initiator_rfh) {
Arthur Sonzognic686e8f2024-01-11 08:36:373276 soft_navigation_heuristics_task_id = std::nullopt;
Yoav Weiss8c573952022-11-17 17:35:133277 }
Arthur Sonzogni620cec62018-12-13 13:08:573278
[email protected]83c2e232011-10-07 21:36:463279 // If we were navigating to a slow-to-commit page, and the user performs
3280 // a session history navigation to the last committed page, RenderViewHost
3281 // will force the throbber to start, but WebKit will essentially ignore the
3282 // navigation, and won't send a message to stop the throbber. To prevent this
3283 // from happening, we drop the navigation here and stop the slow-to-commit
3284 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:023285 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:083286 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:273287 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
Ali Hijazid87307d2022-11-07 20:15:033288 frame_tree_->StopLoading();
[email protected]6a13a6c2011-12-20 21:47:123289
[email protected]83c2e232011-10-07 21:36:463290 DiscardNonCommittedEntries();
William Liu62ae26c2024-08-08 14:28:163291 return {};
[email protected]83c2e232011-10-07 21:36:463292 }
3293
Arthur Sonzognic686e8f2024-01-11 08:36:373294 std::optional<blink::LocalFrameToken> initiator_frame_token;
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:333295 int initiator_process_id = ChildProcessHost::kInvalidUniqueID;
3296 if (initiator_rfh) {
3297 initiator_frame_token = initiator_rfh->GetFrameToken();
Emily Andrewsd15fd762024-12-10 20:41:543298 initiator_process_id = initiator_rfh->GetProcess()->GetDeprecatedID();
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:333299 DCHECK(initiator_frame_token);
3300 }
3301
creisce0ef3572017-01-26 17:53:083302 // Compare FrameNavigationEntries to see which frames in the tree need to be
3303 // navigated.
clamy3cb9bea92018-07-10 12:42:023304 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
3305 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:333306 FindFramesToNavigate(root, reload_type, initiator_frame_token,
3307 initiator_process_id, soft_navigation_heuristics_task_id,
Charlie Reisf4d51f402025-05-23 18:00:493308 actual_navigation_start, &same_document_loads,
3309 &different_document_loads);
creis4e2ecb72015-06-20 00:46:303310
3311 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573312 // We were unable to match any frames to navigate. This can happen if a
3313 // history navigation targets a subframe that no longer exists
3314 // (https://crbug.com/705550). In this case, we need to update the current
3315 // history entry to the pending one but keep the main document loaded. We
3316 // also need to ensure that observers are informed about the updated
3317 // current history entry (e.g., for greying out back/forward buttons), and
Charlie Reis99b2eba22025-01-31 19:18:573318 // that renderer processes update their history indices. The easiest way
Alex Moshchuk3a4e77a2020-05-29 21:32:573319 // to do all that is to schedule a "redundant" same-document navigation in
3320 // the main frame.
3321 //
3322 // Note that we don't want to remove this history entry, as it might still
3323 // be valid later, since a frame that it's targeting may be recreated.
3324 //
3325 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
3326 // need to repeat history navigations until finding the one that works.
3327 // Consider changing this behavior to keep looking for the first valid
3328 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:023329 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:423330 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573331 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:573332 ReloadType::NONE /* reload_type */,
3333 true /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433334 false /* is_history_navigation_in_new_child */,
Charlie Reisf4d51f402025-05-23 18:00:493335 initiator_frame_token, initiator_process_id,
3336 actual_navigation_start);
clamy3cb9bea92018-07-10 12:42:023337 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573338 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:023339 DiscardPendingEntry(false);
William Liu62ae26c2024-08-08 14:28:163340 return {};
clamy3cb9bea92018-07-10 12:42:023341 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573342 same_document_loads.push_back(std::move(navigation_request));
3343
3344 // Sanity check that we never take this branch for any kinds of reloads,
3345 // as those should've queued a different-document load in the main frame.
3346 DCHECK(!is_forced_reload);
3347 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:303348 }
3349
Nate Chapinbf682fa32022-09-26 22:41:203350 // If the initiator is top-navigation sandboxed, then track whether this
Dave Tapuska8bfd84c2019-03-26 20:47:163351 // navigation affects any frame outside the frame's subtree.
Nate Chapinbf682fa32022-09-26 22:41:203352 if (initiator_rfh && initiator_rfh->IsSandboxed(
3353 network::mojom::WebSandboxFlags::kTopNavigation)) {
3354 bool navigates_inside_tree = DoesSandboxNavigationStayWithinSubtree(
3355 initiator_rfh, same_document_loads) &&
3356 DoesSandboxNavigationStayWithinSubtree(
3357 initiator_rfh, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:503358 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:163359 // the number of pages that trigger this.
Nate Chapinbf682fa32022-09-26 22:41:203360 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
3361 initiator_rfh,
3362 navigates_inside_tree
3363 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
3364 : blink::mojom::WebFeature::
3365 kSandboxBackForwardAffectsFramesOutsideSubtree);
Dave Tapuska855c1e12019-08-23 20:45:523366
3367 // If the navigation occurred outside the tree discard it because
3368 // the sandboxed frame didn't have permission to navigate outside
3369 // its tree. If it is possible that the navigation is both inside and
3370 // outside the frame tree and we discard it entirely because we don't
3371 // want to end up in a history state that didn't exist before.
Dominic Farolino057440042022-01-19 18:18:143372 if (!navigates_inside_tree) {
Nate Chapinbf682fa32022-09-26 22:41:203373 // If a |navigation_api_key| was provided, this navigation originated from
3374 // the navigation API. Notify the renderer that the navigation was
3375 // cancelled so the navigation API can fire an error event and reject the
3376 // relevant promise.
3377 if (navigation_api_key) {
3378 initiator_rfh->GetAssociatedLocalFrame()->TraverseCancelled(
3379 *navigation_api_key,
3380 blink::mojom::TraverseCancelledReason::kSandboxViolation);
3381 }
Dave Tapuska855c1e12019-08-23 20:45:523382 DiscardPendingEntry(false);
Rakina Zata Amni58681c62024-06-25 06:32:133383
3384 for (auto& unused_request : same_document_loads) {
3385 unused_request->set_navigation_discard_reason(
3386 NavigationDiscardReason::kNeverStarted);
3387 }
3388 for (auto& unused_request : different_document_loads) {
3389 unused_request->set_navigation_discard_reason(
3390 NavigationDiscardReason::kNeverStarted);
3391 }
William Liu62ae26c2024-08-08 14:28:163392 return {};
Dave Tapuska855c1e12019-08-23 20:45:523393 }
Dave Tapuska8bfd84c2019-03-26 20:47:163394 }
3395
Nate Chapin6c43c022023-02-13 23:32:423396 // If it is possible that this traverse may involve a same-document navigation
3397 // in the initiator and there is a Navigation API key involved, then we may
3398 // need to notify the initiator if it fails. (The early returns above either
3399 // do not involve these cases or already notify the initiator.)
3400 // The event only needs to fire for the initiator, and only if the initiator
3401 // itself is performing a same-document navigation (because the event will not
3402 // fire if it navigates cross-document).
3403 if (navigation_api_key) {
3404 for (auto& item : same_document_loads) {
3405 if (item->frame_tree_node() == initiator_rfh->frame_tree_node()) {
3406 item->set_pending_navigation_api_key(*navigation_api_key);
3407 break;
3408 }
3409 }
3410 }
3411
Carlos Caballero539a421c2020-07-06 10:25:573412 // BackForwardCache:
3413 // Navigate immediately if the document is in the BackForwardCache.
Mingyu Lei7584b6b2023-04-13 03:02:563414 if (back_forward_cache_.GetOrEvictEntry(nav_entry_id).has_value()) {
Carlos Caballero539a421c2020-07-06 10:25:573415 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
Charlie Reis3696ab62025-05-28 07:02:303416 // TODO(crbug.com/420275259): Diagnose failures and upgrade to a CHECK.
3417 DCHECK_EQ(reload_type, ReloadType::NONE);
Charlie Reis655c8f02025-05-15 01:16:173418 base::WeakPtr<NavigationRequest> request;
Rakina Zata Amni58681c62024-06-25 06:32:133419
Charlie Reis655c8f02025-05-15 01:16:173420 // Skip a redundant NavigationRequest creation, per
3421 // https://crbug.com/417251428.
3422 if (base::FeatureList::IsEnabled(kSkipExtraBfcacheNavigationRequest)) {
3423 // If the BackForwardCache can handle this request, it must be for a main
3424 // frame, cross-document, non-reload request. This means there is only one
3425 // item in `different_document_loads` and no `same_document_loads`.
3426 CHECK_EQ(different_document_loads.size(), 1u);
3427 CHECK(same_document_loads.empty());
3428 request = different_document_loads.at(0)->GetWeakPtr();
Charlie Reisd3e4fef2025-05-20 02:04:173429
3430 // Ensure that no re-entrant calls or discards of the pending entry occur
3431 // while calling `Navigator::Navigate` for a pending entry.
3432 ScopedPendingEntryReentrancyGuard reentrancy_guard(
3433 weak_factory_.GetSafeRef());
3434
Charlie Reis655c8f02025-05-15 01:16:173435 root->navigator().Navigate(std::move(different_document_loads.at(0)),
3436 ReloadType::NONE);
Charlie Reisd3e4fef2025-05-20 02:04:173437
3438 // `reentrancy_guard` deleted here.
Charlie Reis655c8f02025-05-15 01:16:173439 } else {
3440 // The legacy approach creates a new NavigationRequest for the entry and
3441 // discards any previously created NavigationRequests, even though the new
3442 // request is identical to the sole existing request.
3443 // TODO(crbug.com/417251428): Remove this path once we measure the impact.
3444 auto navigation_request = CreateNavigationRequestFromEntry(
3445 root, pending_entry_, pending_entry_->GetFrameEntry(root),
3446 ReloadType::NONE, false /* is_same_document_history_load */,
3447 false /* is_history_navigation_in_new_child */, initiator_frame_token,
Charlie Reisf4d51f402025-05-23 18:00:493448 initiator_process_id, actual_navigation_start);
Charlie Reis655c8f02025-05-15 01:16:173449 request = navigation_request->GetWeakPtr();
3450 root->navigator().Navigate(std::move(navigation_request),
3451 ReloadType::NONE);
3452
3453 for (auto& unused_request : same_document_loads) {
3454 unused_request->set_navigation_discard_reason(
3455 NavigationDiscardReason::kNeverStarted);
3456 }
3457 for (auto& unused_request : different_document_loads) {
3458 unused_request->set_navigation_discard_reason(
3459 NavigationDiscardReason::kNeverStarted);
3460 }
Rakina Zata Amni58681c62024-06-25 06:32:133461 }
3462
Charlie Reis655c8f02025-05-15 01:16:173463 std::vector<base::WeakPtr<NavigationRequest>> bf_cache_requests;
William Liu62ae26c2024-08-08 14:28:163464 if (request) {
Charlie Reis655c8f02025-05-15 01:16:173465 bf_cache_requests.push_back(std::move(request));
William Liu62ae26c2024-08-08 14:28:163466 }
Charlie Reis655c8f02025-05-15 01:16:173467 return bf_cache_requests;
Carlos Caballero539a421c2020-07-06 10:25:573468 }
3469
3470 // History navigation might try to reuse a specific BrowsingInstance, already
3471 // used by a page in the cache. To avoid having two different main frames that
3472 // live in the same BrowsingInstance, evict the all pages with this
3473 // BrowsingInstance from the cache.
3474 //
3475 // For example, take the following scenario:
3476 //
3477 // A1 = Some page on a.com
3478 // A2 = Some other page on a.com
3479 // B3 = An uncacheable page on b.com
3480 //
3481 // Then the following navigations occur:
3482 // A1->A2->B3->A1
3483 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
3484 // take its place) and A1 will be created in the same BrowsingInstance (and
3485 // SiteInstance), as A2.
3486 //
3487 // If we didn't do anything, both A1 and A2 would remain alive in the same
3488 // BrowsingInstance/SiteInstance, which is unsupported by
3489 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
3490 // A2 from the cache.
Rakina Zata Amni2cde21d2024-09-27 04:20:483491 SCOPED_CRASH_KEY_NUMBER("rvh_double", "pending_entry_si",
3492 pending_entry_->site_instance()
3493 ? pending_entry_->site_instance()->GetId().value()
3494 : -1);
3495 SCOPED_CRASH_KEY_NUMBER(
3496 "rvh_double", "pending_entry_bi",
3497 pending_entry_->site_instance()
3498 ? pending_entry_->site_instance()->GetBrowsingInstanceId().value()
3499 : -1);
Carlos Caballero539a421c2020-07-06 10:25:573500 if (pending_entry_->site_instance()) {
3501 back_forward_cache_.EvictFramesInRelatedSiteInstances(
3502 pending_entry_->site_instance());
3503 }
3504
Rakina Zata Amnid605d462022-06-01 10:17:033505 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "pending_entry_restored",
3506 pending_entry_ && pending_entry_->IsRestored());
3507 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_entry_id",
3508 pending_entry_ ? pending_entry_->GetUniqueID() : -1);
3509 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_entry_index",
3510 pending_entry_index_);
3511 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "last_committed_index",
3512 last_committed_entry_index_);
3513 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "entries_size", entries_.size());
3514 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "pending_entry_initial",
3515 pending_entry_ && pending_entry_->IsInitialEntry());
3516 SCOPED_CRASH_KEY_BOOL(
3517 "nav_reentrancy", "pending_entry_initial2",
3518 pending_entry_ &&
3519 pending_entry_->IsInitialEntryNotForSynchronousAboutBlank());
3520 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_initial_nav",
3521 IsInitialNavigation());
3522 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_initial_blank_nav",
3523 IsInitialBlankNavigation());
3524 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_forced_reload", is_forced_reload);
3525 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_reload_type",
3526 (int)pending_reload_);
3527
Charlie Reisd3e4fef2025-05-20 02:04:173528 // Ensure that no re-entrant calls or discards of the pending entry occur
3529 // while calling `Navigator::Navigate` for a pending entry.
3530 ScopedPendingEntryReentrancyGuard reentrancy_guard(
3531 weak_factory_.GetSafeRef());
creis4e2ecb72015-06-20 00:46:303532
Rakina Zata Amnid605d462022-06-01 10:17:033533 // If the navigation-reentrancy is caused by calling
3534 // NavigateToExistingPendingEntry twice, this will note the previous call's
3535 // pending entry's ID.
3536 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "prev_pending_entry_id",
3537 pending_entry_ ? pending_entry_->GetUniqueID() : -1);
3538
Nate Chapin154b14b2023-02-17 18:28:163539 // If there is a main-frame same-document history navigation, we may defer
3540 // the subframe history navigations in order to give JS in the main frame the
3541 // opportunity to cancel the entire traverse via the navigate event. In that
3542 // case, we need to stash the main frame request's navigation token on the
3543 // subframes, so they can look up the main frame request and defer themselves
3544 // until it completes.
3545 if (!same_document_loads.empty() &&
3546 same_document_loads.at(0)->frame_tree_node()->IsMainFrame()) {
3547 NavigationRequest* main_frame_request = same_document_loads.at(0).get();
3548 // The token will only be returned in cases where deferring the navigation
3549 // is necessary.
3550 if (auto main_frame_same_document_token =
3551 main_frame_request->GetNavigationTokenForDeferringSubframes()) {
3552 for (auto& item : same_document_loads) {
3553 if (item.get() != main_frame_request) {
3554 item->set_main_frame_same_document_history_token(
3555 main_frame_same_document_token);
3556 }
3557 }
3558 for (auto& item : different_document_loads) {
3559 item->set_main_frame_same_document_history_token(
3560 main_frame_same_document_token);
3561 }
3562 }
3563 }
3564
William Liu122754942024-01-18 22:34:393565 if (!initiator_rfh) {
3566 // A browser-initiated navigation won't have a `initiator_rfh`.
3567 CountBrowserInitiatedMainframeAndSubframeHistoryNavigaions(
3568 different_document_loads, same_document_loads);
3569 }
3570
William Liu62ae26c2024-08-08 14:28:163571 std::vector<base::WeakPtr<NavigationRequest>> all_requests;
3572 all_requests.reserve(same_document_loads.size() +
3573 different_document_loads.size());
3574 bool seen_primary_main_frame_request = false;
3575
creis4e2ecb72015-06-20 00:46:303576 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:023577 for (auto& item : same_document_loads) {
3578 FrameTreeNode* frame = item->frame_tree_node();
William Liuec04e382024-05-23 18:03:273579 // The request could be destroyed before `navigator().Navigate()` returns.
3580 base::WeakPtr<NavigationRequest> request = item->GetWeakPtr();
Lukasz Anforowicz9ee83c272020-12-01 20:14:053581 frame->navigator().Navigate(std::move(item), reload_type);
William Liu62ae26c2024-08-08 14:28:163582 if (request) {
3583 // Can only have one primary main frame request.
3584 CHECK(!seen_primary_main_frame_request ||
3585 !request->IsInPrimaryMainFrame());
3586 seen_primary_main_frame_request = request->IsInPrimaryMainFrame();
3587 all_requests.push_back(std::move(request));
William Liuec04e382024-05-23 18:03:273588 }
creis4e2ecb72015-06-20 00:46:303589 }
clamy3cb9bea92018-07-10 12:42:023590 for (auto& item : different_document_loads) {
3591 FrameTreeNode* frame = item->frame_tree_node();
William Liuec04e382024-05-23 18:03:273592 base::WeakPtr<NavigationRequest> request = item->GetWeakPtr();
Lukasz Anforowicz9ee83c272020-12-01 20:14:053593 frame->navigator().Navigate(std::move(item), reload_type);
William Liu62ae26c2024-08-08 14:28:163594 if (request) {
3595 // Can only have one primary main frame request.
3596 CHECK(!seen_primary_main_frame_request ||
3597 !request->IsInPrimaryMainFrame());
3598 seen_primary_main_frame_request = request->IsInPrimaryMainFrame();
3599 all_requests.push_back(std::move(request));
William Liuec04e382024-05-23 18:03:273600 }
creis4e2ecb72015-06-20 00:46:303601 }
clamy3cb9bea92018-07-10 12:42:023602
William Liu62ae26c2024-08-08 14:28:163603 return all_requests;
Charlie Reisd3e4fef2025-05-20 02:04:173604 // `reentrancy_guard` deleted here.
creis4e2ecb72015-06-20 00:46:303605}
3606
Alex Moshchuk3a4e77a2020-05-29 21:32:573607NavigationControllerImpl::HistoryNavigationAction
3608NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:303609 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:573610 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:423611 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:583612 // Only active and prerendered documents are allowed to navigate in their
3613 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:423614 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:013615 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:583616 // - If the document is in pending deletion, the browser already committed
3617 // to destroying this RenderFrameHost. See https://crbug.com/930278.
3618 // - If the document is in back-forward cache, it's not allowed to navigate
3619 // as it should remain frozen. Ignore the request and evict the document
3620 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:423621 //
Sreeja Kamishettydb8e2892021-03-10 09:30:583622 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:423623 // which should all be inactive as well.
Fergal Daly1336ac642021-09-14 15:13:113624 if (frame->current_frame_host()->IsInactiveAndDisallowActivation(
3625 DisallowActivationReasonId::kDetermineActionForHistoryNavigation)) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:423626 return HistoryNavigationAction::kStopLooking;
Fergal Daly1336ac642021-09-14 15:13:113627 }
Sreeja Kamishetty8eacabb2021-03-09 11:45:423628 }
arthursonzogni03f76152019-02-12 10:35:203629
Alex Moshchuk3a4e77a2020-05-29 21:32:573630 // Reloads should result in a different-document load. Note that reloads may
3631 // also happen via the |needs_reload_| mechanism where the reload_type is
3632 // NONE, so detect this by comparing whether we're going to the same
3633 // entry that we're currently on. Similarly to above, only main frames
3634 // should reach this. Note that subframes support reloads, but that's done
3635 // via a different path that doesn't involve FindFramesToNavigate (see
3636 // RenderFrameHost::Reload()).
3637 if (reload_type != ReloadType::NONE ||
3638 pending_entry_index_ == last_committed_entry_index_) {
3639 DCHECK(frame->IsMainFrame());
3640 return HistoryNavigationAction::kDifferentDocument;
3641 }
3642
Alex Moshchuk47d1a4bd2020-06-01 22:15:343643 // If there is no new FrameNavigationEntry for the frame, ignore the
3644 // load. For example, this may happen when going back to an entry before a
3645 // frame was created. Suppose we commit a same-document navigation that also
3646 // results in adding a new subframe somewhere in the tree. If we go back,
3647 // the new subframe will be missing a FrameNavigationEntry in the previous
3648 // NavigationEntry, but we shouldn't delete or change what's loaded in
3649 // it.
3650 //
Alex Moshchuke65c39272020-06-03 17:55:373651 // Note that in this case, there is no need to keep looking for navigations
3652 // in subframes, which would be missing FrameNavigationEntries as well.
3653 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:343654 // It's important to check this before checking |old_item| below, since both
3655 // might be null, and in that case we still shouldn't change what's loaded in
3656 // this frame. Note that scheduling any loads assumes that |new_item| is
3657 // non-null. See https://crbug.com/1088354.
3658 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3659 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:373660 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:343661
Charlie Reisa474fb62022-03-17 02:31:363662 // Use the RenderFrameHost's last committed FrameNavigationEntry to identify
3663 // which history item it is currently on, since this may be different than the
3664 // FrameNavigationEntry for the frame in the last committed NavigationEntry
3665 // (e.g., if a history navigation is targeting multiple frames and only some
3666 // have committed so far).
creis4e2ecb72015-06-20 00:46:303667 FrameNavigationEntry* old_item =
Charlie Reisa474fb62022-03-17 02:31:363668 frame->current_frame_host()->last_committed_frame_entry();
3669 if (!old_item) {
3670 // In cases where the RenderFrameHost does not have a FrameNavigationEntry,
3671 // fall back to the last committed NavigationEntry's record for this frame.
3672 // This may happen in cases like the initial state of the RenderFrameHost.
Alison Gale770f3fc2024-04-27 00:39:583673 // TODO(crbug.com/40217743): Ensure the RenderFrameHost always has an
Charlie Reisa474fb62022-03-17 02:31:363674 // accurate FrameNavigationEntry and eliminate this case.
3675 old_item = GetLastCommittedEntry()->GetFrameEntry(frame);
3676 }
3677 // If neither approach finds a FrameNavigationEntry, schedule a
3678 // different-document load.
Alison Gale770f3fc2024-04-27 00:39:583679 // TODO(crbug.com/40467594): Remove this case.
Alex Moshchuk3a4e77a2020-05-29 21:32:573680 if (!old_item)
3681 return HistoryNavigationAction::kDifferentDocument;
3682
Alex Moshchuk3a4e77a2020-05-29 21:32:573683 // If the new item is not in the same SiteInstance, schedule a
3684 // different-document load. Newly restored items may not have a SiteInstance
3685 // yet, in which case it will be assigned on first commit.
3686 if (new_item->site_instance() &&
3687 new_item->site_instance() != old_item->site_instance())
3688 return HistoryNavigationAction::kDifferentDocument;
3689
Charlie Reis71e4ae32025-01-29 21:37:573690 // If the origins of the new and old items are both present but don't match,
3691 // schedule a different document load even if the document sequence numbers
3692 // somehow match.
3693 // TODO(crbug.com/40051596): Also handle session restore cases that lack a
3694 // committed origin on `new_item`, and update the Blink DSN computation to
3695 // avoid a cross-origin DSN match when possible.
3696 if (new_item->committed_origin().has_value() &&
3697 old_item->committed_origin().has_value() &&
3698 !new_item->committed_origin()->IsSameOriginWith(
3699 old_item->committed_origin().value())) {
3700 return HistoryNavigationAction::kDifferentDocument;
3701 }
3702
Alex Moshchuk3a4e77a2020-05-29 21:32:573703 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:313704 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
3705 // that we do this even if the history navigation would not be modifying this
3706 // frame were it live.
3707 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:573708 return HistoryNavigationAction::kDifferentDocument;
3709
3710 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:313711 // Starting a navigation after a crash early-promotes the speculative
3712 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
3713 // committed yet, so we can not possibly perform a same-document history
3714 // navigation. The frame would need to be reloaded with a cross-document
3715 // navigation.
3716 if (!frame->current_frame_host()->has_committed_any_navigation())
3717 return HistoryNavigationAction::kDifferentDocument;
3718
creis54131692016-08-12 18:32:253719 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:493720 // sequence number but different item sequence number.
3721 if (new_item->document_sequence_number() ==
3722 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573723 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493724 }
avib48cb312016-05-05 21:35:003725
Alex Moshchuk3a4e77a2020-05-29 21:32:573726 // Otherwise, if both item and document sequence numbers differ, this
3727 // should be a different document load.
3728 return HistoryNavigationAction::kDifferentDocument;
3729 }
3730
3731 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373732 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573733 DCHECK_EQ(new_item->document_sequence_number(),
3734 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373735 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573736}
3737
3738void NavigationControllerImpl::FindFramesToNavigate(
3739 FrameTreeNode* frame,
3740 ReloadType reload_type,
Arthur Sonzognic686e8f2024-01-11 08:36:373741 const std::optional<blink::LocalFrameToken>& initiator_frame_token,
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:333742 int initiator_process_id,
Arthur Sonzognic686e8f2024-01-11 08:36:373743 std::optional<blink::scheduler::TaskAttributionId>
Yoav Weiss8c573952022-11-17 17:35:133744 soft_navigation_heuristics_task_id,
Charlie Reisf4d51f402025-05-23 18:00:493745 base::TimeTicks actual_navigation_start,
Alex Moshchuk3a4e77a2020-05-29 21:32:573746 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3747 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3748 DCHECK(pending_entry_);
3749 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3750
3751 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3752
3753 if (action == HistoryNavigationAction::kSameDocument) {
3754 std::unique_ptr<NavigationRequest> navigation_request =
3755 CreateNavigationRequestFromEntry(
3756 frame, pending_entry_, new_item, reload_type,
Yoav Weiss8c573952022-11-17 17:35:133757 /*is_same_document_history_load=*/true,
3758 /*is_history_navigation_in_new_child_frame=*/false,
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:333759 initiator_frame_token, initiator_process_id,
Charlie Reisf4d51f402025-05-23 18:00:493760 actual_navigation_start, soft_navigation_heuristics_task_id);
Alex Moshchuk3a4e77a2020-05-29 21:32:573761 if (navigation_request) {
3762 // Only add the request if was properly created. It's possible for the
3763 // creation to fail in certain cases, e.g. when the URL is invalid.
3764 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303765 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573766 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473767 std::unique_ptr<NavigationRequest> navigation_request =
3768 CreateNavigationRequestFromEntry(
3769 frame, pending_entry_, new_item, reload_type,
3770 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433771 false /* is_history_navigation_in_new_child */,
Charlie Reisf4d51f402025-05-23 18:00:493772 initiator_frame_token, initiator_process_id,
3773 actual_navigation_start);
Lei Zhang96031532019-10-10 19:05:473774 if (navigation_request) {
3775 // Only add the request if was properly created. It's possible for the
3776 // creation to fail in certain cases, e.g. when the URL is invalid.
3777 different_document_loads->push_back(std::move(navigation_request));
3778 }
3779 // For a different document, the subframes will be destroyed, so there's
3780 // no need to consider them.
3781 return;
Alex Moshchuke65c39272020-06-03 17:55:373782 } else if (action == HistoryNavigationAction::kStopLooking) {
3783 return;
creis4e2ecb72015-06-20 00:46:303784 }
3785
Yoav Weiss8c573952022-11-17 17:35:133786 // Do not pass down the soft_navigation_heuristics_task_id to child frames, as
3787 // we currently only support soft navigation heuristics for the top level
3788 // frame.
creis4e2ecb72015-06-20 00:46:303789 for (size_t i = 0; i < frame->child_count(); i++) {
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:333790 FindFramesToNavigate(frame->child_at(i), reload_type, initiator_frame_token,
3791 initiator_process_id,
Arthur Sonzognic686e8f2024-01-11 08:36:373792 /*soft_navigation_heuristics_task_id=*/std::nullopt,
Charlie Reisf4d51f402025-05-23 18:00:493793 actual_navigation_start, same_document_loads,
3794 different_document_loads);
creis4e2ecb72015-06-20 00:46:303795 }
3796}
3797
Harkiran Bolariaba823e42021-05-21 18:30:363798base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry(
Charlie Reisf4d51f402025-05-23 18:00:493799 const LoadURLParams& params,
3800 base::TimeTicks actual_navigation_start) {
Emmanuel Arias Soto9e159652025-05-16 07:53:553801 FrameTreeNode* node = GetTargetFrameTreeNodeForNavigation(params);
clamy21718cc22018-06-13 13:34:243802
Camille Lamy5193caa2018-10-12 11:59:423803 // Compute overrides to the LoadURLParams for |override_user_agent|,
3804 // |should_replace_current_entry| and |has_user_gesture| that will be used
3805 // both in the creation of the NavigationEntry and the NavigationRequest.
3806 // Ideally, the LoadURLParams themselves would be updated, but since they are
3807 // passed as a const reference, this is not possible.
3808 // TODO(clamy): When we only create a NavigationRequest, move this to
3809 // CreateNavigationRequestFromLoadURLParams.
3810 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3811 GetLastCommittedEntry());
3812
Rakina Zata Amnie2d31312022-11-18 03:38:453813 // An entry replacement must happen if the current browsing context should
3814 // maintain a trivial session history.
shivanigithubf405bf0d2021-11-05 17:58:333815 bool should_replace_current_entry =
3816 (params.should_replace_current_entry ||
Rakina Zata Amnie2d31312022-11-18 03:38:453817 ShouldMaintainTrivialSessionHistory(node));
Camille Lamy5193caa2018-10-12 11:59:423818
clamy21718cc22018-06-13 13:34:243819 // Javascript URLs should not create NavigationEntries. All other navigations
3820 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243821 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373822 std::unique_ptr<NavigationEntryImpl> entry =
3823 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3824 should_replace_current_entry,
3825 params.has_user_gesture);
clamy21718cc22018-06-13 13:34:243826 DiscardPendingEntry(false);
3827 SetPendingEntry(std::move(entry));
3828 }
3829
Tim Judkins59548192023-05-17 17:51:203830 // Renderer-debug URLs are sent to the current renderer process immediately
3831 // for processing and don't need to create a NavigationRequest. Note: this
3832 // includes navigations to JavaScript URLs, which are considered
clamy21718cc22018-06-13 13:34:243833 // renderer-debug URLs.
3834 // Note: we intentionally leave the pending entry in place for renderer debug
3835 // URLs, unlike the cases below where we clear it if the navigation doesn't
3836 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303837 if (blink::IsRendererDebugURL(params.url)) {
Emmanuel Arias Soto9e159652025-05-16 07:53:553838 // Renderer-debug URLs won't go through NavigationThrottles so we have to
3839 // check them explicitly. See crbug.com/40605746.
Aaron Colwelle1908d982020-06-26 22:08:153840 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Tim Judkins59548192023-05-17 17:51:203841 params.url, browser_context_, node->current_frame_host())) {
Oleg Davydov2cc0167b2019-02-05 14:32:483842 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363843 return nullptr;
Oleg Davydov2cc0167b2019-02-05 14:32:483844 }
3845
clamy21718cc22018-06-13 13:34:243846 HandleRendererDebugURL(node, params.url);
Harkiran Bolariaba823e42021-05-21 18:30:363847 return nullptr;
clamy21718cc22018-06-13 13:34:243848 }
3849
Antonio Sartori78a749f2020-11-30 12:03:393850 DCHECK(pending_entry_);
3851
clamy21718cc22018-06-13 13:34:243852 // Convert navigations to the current URL to a reload.
3853 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3854 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3855 // DevTools sometimes issues navigations to main frames that they do not
3856 // expect to see treated as reload, and it only works because they pass a
3857 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3858 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543859 ReloadType reload_type = params.reload_type;
3860 if (reload_type == ReloadType::NONE &&
3861 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043862 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243863 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243864 params.load_type ==
3865 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
Hayato Ito7a80db42021-07-05 06:18:543866 should_replace_current_entry, GetLastCommittedEntry())) {
clamy21718cc22018-06-13 13:34:243867 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473868 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393869
3870 // If this is a reload of an existing FrameNavigationEntry and we had a
3871 // policy container for it, then we should copy it into the pending entry,
3872 // so that it is copied to the navigation request in
3873 // CreateNavigationRequestFromLoadParams later.
Rakina Zata Amnie2d31312022-11-18 03:38:453874 FrameNavigationEntry* previous_frame_entry =
3875 GetLastCommittedEntry()->GetFrameEntry(node);
3876 if (previous_frame_entry &&
3877 previous_frame_entry->policy_container_policies()) {
3878 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
3879 previous_frame_entry->policy_container_policies()->ClonePtr());
Antonio Sartori78a749f2020-11-30 12:03:393880 }
3881 }
3882
3883 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
Rakina Zata Amniafd3c6582021-11-30 06:19:173884 // document, then we should copy the document polices from RenderFrameHost's
3885 // PolicyContainerHost. The NavigationRequest will create a new
3886 // PolicyContainerHost with the document policies from the |pending_entry_|,
3887 // and that PolicyContainerHost will be put in the final RenderFrameHost for
3888 // the navigation. This way, we ensure that we keep enforcing the right
3889 // policies on the initial empty document after the reload.
Rakina Zata Amnie2d31312022-11-18 03:38:453890 if (GetLastCommittedEntry()->IsInitialEntry() && params.url.IsAboutBlank()) {
Antonio Sartori78a749f2020-11-30 12:03:393891 if (node->current_frame_host() &&
3892 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193893 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163894 node->current_frame_host()
3895 ->policy_container_host()
3896 ->policies()
Titouan Rigoudy72f892d2022-05-02 18:21:233897 .ClonePtr());
Antonio Sartori78a749f2020-11-30 12:03:393898 }
clamy21718cc22018-06-13 13:34:243899 }
3900
3901 // navigation_ui_data should only be present for main frame navigations.
Ian Vollick1c6dd3e2022-04-13 02:06:263902 DCHECK(node->IsOutermostMainFrame() || !params.navigation_ui_data);
clamy21718cc22018-06-13 13:34:243903
Tsuyoshi Horo167ca6432022-03-09 05:16:393904 // This will be used to set the Navigation Timing API navigationStart
3905 // parameter for browser navigations in new tabs (intents, tabs opened through
3906 // "Open link in new tab"). If the navigation must wait on the current
3907 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3908 // will be updated when the BeforeUnload ack is received.
3909 const auto navigation_start_time = base::TimeTicks::Now();
Tsuyoshi Horo167ca6432022-03-09 05:16:393910
Camille Lamy5193caa2018-10-12 11:59:423911 std::unique_ptr<NavigationRequest> request =
3912 CreateNavigationRequestFromLoadParams(
3913 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143914 params.has_user_gesture, network::mojom::SourceLocation::New(),
Tsuyoshi Horo167ca6432022-03-09 05:16:393915 reload_type, pending_entry_, pending_entry_->GetFrameEntry(node),
Charlie Reisf4d51f402025-05-23 18:00:493916 actual_navigation_start, navigation_start_time);
clamy21718cc22018-06-13 13:34:243917
3918 // If the navigation couldn't start, return immediately and discard the
3919 // pending NavigationEntry.
3920 if (!request) {
3921 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363922 return nullptr;
clamy21718cc22018-06-13 13:34:243923 }
3924
Camille Lamy5193caa2018-10-12 11:59:423925#if DCHECK_IS_ON()
3926 // Safety check that NavigationRequest and NavigationEntry match.
3927 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3928#endif
3929
Charlie Reisd3e4fef2025-05-20 02:04:173930 // Ensure that no re-entrant calls or discards of the pending entry occur
3931 // while calling `Navigator::Navigate` for a pending entry.
3932 ScopedPendingEntryReentrancyGuard reentrancy_guard(
3933 weak_factory_.GetSafeRef());
arthursonzogni66f711c2019-10-08 14:40:363934
Harkiran Bolariaba823e42021-05-21 18:30:363935 base::WeakPtr<NavigationHandle> created_navigation_handle(
3936 request->GetWeakPtr());
Lukasz Anforowicz9ee83c272020-12-01 20:14:053937 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243938
Harkiran Bolariaba823e42021-05-21 18:30:363939 return created_navigation_handle;
Charlie Reisd3e4fef2025-05-20 02:04:173940 // `reentrancy_guard` deleted here.
clamy21718cc22018-06-13 13:34:243941}
3942
Emmanuel Arias Soto9e159652025-05-16 07:53:553943FrameTreeNode* NavigationControllerImpl::GetTargetFrameTreeNodeForNavigation(
3944 const LoadURLParams& params) {
3945 FrameTreeNode* node = nullptr;
3946 if (params.frame_tree_node_id || !params.frame_name.empty()) {
3947 node = params.frame_tree_node_id
3948 ? frame_tree_->FindByID(params.frame_tree_node_id)
3949 : frame_tree_->FindByName(params.frame_name);
3950 DCHECK(!node || &node->frame_tree() == &frame_tree());
3951 if (!node && params.frame_tree_node_id) {
3952 // If the specified FrameTreeNode exists in another FrameTree, the caller
3953 // is using the wrong NavigationController.
3954 CHECK(!FrameTreeNode::GloballyFindByID(params.frame_tree_node_id),
3955 base::NotFatalUntil::M140);
3956 }
3957 }
3958
3959 // If no FrameTreeNode was specified, navigate the main frame.
3960 if (!node) {
3961 node = frame_tree_->root();
3962 }
3963 return node;
3964}
3965
clamyea99ea12018-05-28 13:54:233966void NavigationControllerImpl::HandleRendererDebugURL(
3967 FrameTreeNode* frame_tree_node,
3968 const GURL& url) {
3969 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243970 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3971 // the renderer process is not live, unless it is the initial navigation
3972 // of the tab.
clamyea99ea12018-05-28 13:54:233973 if (!IsInitialNavigation()) {
3974 DiscardNonCommittedEntries();
3975 return;
3976 }
Fergal Dalyecd3b0202020-06-25 01:57:373977 // The current frame is always a main frame. If IsInitialNavigation() is
3978 // true then there have been no navigations and any frames of this tab must
3979 // be in the same renderer process. If that has crashed then the only frame
3980 // that can be revived is the main frame.
3981 frame_tree_node->render_manager()
3982 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233983 }
Julie Jeongeun Kim50d124c2022-10-21 13:51:223984
3985 // Several tests expect a load of Chrome Debug URLs to send a DidStopLoading
3986 // notification, so set is loading to true here to properly surface it when
3987 // the renderer process is done handling the URL.
Alison Gale81f4f2c72024-04-22 19:33:313988 // TODO(crbug.com/40199456): Remove the test dependency on this behavior.
Julie Jeongeun Kim50d124c2022-10-21 13:51:223989 if (!url.SchemeIs(url::kJavaScriptScheme)) {
Julie Jeongeun Kim50d124c2022-10-21 13:51:223990 frame_tree_node->current_frame_host()->SetIsLoadingForRendererDebugURL();
Julie Jeongeun Kim50d124c2022-10-21 13:51:223991 }
clamyea99ea12018-05-28 13:54:233992 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3993}
3994
clamy21718cc22018-06-13 13:34:243995std::unique_ptr<NavigationEntryImpl>
3996NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3997 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423998 const LoadURLParams& params,
3999 bool override_user_agent,
4000 bool should_replace_current_entry,
4001 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:394002 // Browser initiated navigations might not have a blob_url_loader_factory set
4003 // in params even if the navigation is to a blob URL. If that happens, lookup
4004 // the correct url loader factory to use here.
4005 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:484006 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:474007 // Resolve the blob URL in the storage partition associated with the target
4008 // frame. This is the storage partition the URL will be loaded in, and only
4009 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:394010 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:474011 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:394012 }
4013
clamy21718cc22018-06-13 13:34:244014 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:444015 // extra_headers in params are \n separated; navigation entries want \r\n.
4016 std::string extra_headers_crlf;
4017 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:244018
4019 // For subframes, create a pending entry with a corresponding frame entry.
4020 if (!node->IsMainFrame()) {
Rakina Zata Amnie2d31312022-11-18 03:38:454021 entry = GetLastCommittedEntry()->Clone();
clamy21718cc22018-06-13 13:34:244022 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:084023 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
clamy21718cc22018-06-13 13:34:244024 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Arthur Sonzognic686e8f2024-01-11 08:36:374025 params.url, std::nullopt, params.referrer, params.initiator_origin,
W. James MacLean23e90a12022-12-21 04:38:214026 params.initiator_base_url, params.redirect_chain, blink::PageState(),
4027 "GET", -1, blob_url_loader_factory,
Antonio Sartori78a749f2020-11-30 12:03:394028 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:164029 // a conversion of a new navigation into a reload, we will set the right
4030 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:194031 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:244032 } else {
4033 // Otherwise, create a pending entry for the main frame.
Julie Jeongeun Kim5b9aff72022-05-02 02:10:174034 // If `node` is the outermost main frame, it rewrites a virtual url in order
4035 // to adjust the original input url if needed. For inner frames such as
4036 // fenced frames or subframes, they don't rewrite urls as the urls are not
4037 // input urls by users.
4038 bool rewrite_virtual_urls = node->IsOutermostMainFrame();
Sharon Yang242ef822023-05-15 21:07:324039 scoped_refptr<SiteInstance> source_site_instance =
4040 params.source_site_instance;
Arthur Sonzognic686e8f2024-01-11 08:36:374041 std::optional<GURL> source_process_site_url = std::nullopt;
Sharon Yang242ef822023-05-15 21:07:324042 if (source_site_instance && source_site_instance->HasProcess()) {
4043 source_process_site_url =
4044 source_site_instance->GetProcess()->GetProcessLock().site_url();
4045 }
clamy21718cc22018-06-13 13:34:244046 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:064047 params.url, params.referrer, params.initiator_origin,
Sharon Yang242ef822023-05-15 21:07:324048 params.initiator_base_url, source_process_site_url,
W. James MacLean23e90a12022-12-21 04:38:214049 params.transition_type, params.is_renderer_initiated,
4050 extra_headers_crlf, browser_context_, blob_url_loader_factory,
4051 rewrite_virtual_urls));
clamy21718cc22018-06-13 13:34:244052 entry->set_source_site_instance(
4053 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
4054 entry->SetRedirectChain(params.redirect_chain);
4055 }
4056
4057 // Set the FTN ID (only used in non-site-per-process, for tests).
4058 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:244059 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:424060 entry->SetIsOverridingUserAgent(override_user_agent);
4061 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:544062 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:244063
clamy21718cc22018-06-13 13:34:244064 switch (params.load_type) {
4065 case LOAD_TYPE_DEFAULT:
4066 break;
4067 case LOAD_TYPE_HTTP_POST:
4068 entry->SetHasPostData(true);
4069 entry->SetPostData(params.post_data);
4070 break;
4071 case LOAD_TYPE_DATA:
Shu Yang112ad492024-07-25 17:11:544072 // LoadDataWithBaseURL is a special case that needs to assign both a base
4073 // URL and a virtual URL, while loading the actual content from a data
4074 // URL.
clamy21718cc22018-06-13 13:34:244075 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
Shu Yang112ad492024-07-25 17:11:544076 entry->SetVirtualURL(params.virtual_url_for_special_cases);
Xiaohan Wang7f8052e02022-01-14 18:44:284077#if BUILDFLAG(IS_ANDROID)
clamy21718cc22018-06-13 13:34:244078 entry->SetDataURLAsString(params.data_url_as_string);
4079#endif
4080 entry->SetCanLoadLocalResources(params.can_load_local_resources);
4081 break;
Shu Yang112ad492024-07-25 17:11:544082#if BUILDFLAG(IS_ANDROID)
4083 case LOAD_TYPE_PDF_ANDROID:
4084 // Android PDF URLs show the actual PDF URL as a virtual URL, while an
4085 // internal URL is used for the navigation URL.
4086 entry->SetVirtualURL(params.virtual_url_for_special_cases);
4087 break;
4088#endif
clamy21718cc22018-06-13 13:34:244089 }
4090
4091 // TODO(clamy): NavigationEntry is meant for information that will be kept
4092 // after the navigation ended and therefore is not appropriate for
4093 // started_from_context_menu. Move started_from_context_menu to
4094 // NavigationUIData.
4095 entry->set_started_from_context_menu(params.started_from_context_menu);
4096
4097 return entry;
4098}
4099
clamyea99ea12018-05-28 13:54:234100std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:424101NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
4102 FrameTreeNode* node,
4103 const LoadURLParams& params,
4104 bool override_user_agent,
4105 bool should_replace_current_entry,
4106 bool has_user_gesture,
Antonio Sartori2f763d9d2021-04-21 10:04:144107 network::mojom::SourceLocationPtr source_location,
Camille Lamy5193caa2018-10-12 11:59:424108 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574109 NavigationEntryImpl* entry,
Tsuyoshi Horo167ca6432022-03-09 05:16:394110 FrameNavigationEntry* frame_entry,
Charlie Reise1d9b8182025-04-02 04:32:124111 base::TimeTicks actual_navigation_start_time,
Nan Lin944e9b4e2022-04-12 13:51:224112 base::TimeTicks navigation_start_time,
Garrett Tanzer405f3402022-07-21 20:12:494113 bool is_embedder_initiated_fenced_frame_navigation,
Sergey Poromovdd557c12023-03-01 11:28:454114 bool is_unfenced_top_navigation,
Camillia Smith Barnes6a643962023-03-03 00:28:584115 bool is_container_initiated,
W. James MacLean443ef3e2024-07-16 13:42:344116 net::StorageAccessApiStatus storage_access_api_status,
Arthur Sonzognic686e8f2024-01-11 08:36:374117 std::optional<std::u16string> embedder_shared_storage_context) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574118 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Charlie Reisf21cd182024-08-09 21:44:524119
4120 // TODO(https://crbug.com/40467594): Add a CHECK(frame_entry) once all
4121 // subframes have FrameNavigationEntries associated with them. Until then,
4122 // there may be cases where a subframe navigation is missing `frame_entry`
4123 // (e.g., see https://crbug.com/358084015).
4124
Nasko Oskov3c2f9e252019-01-10 17:45:534125 // All renderer-initiated navigations must have an initiator_origin.
4126 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:514127
Camille Lamy5193caa2018-10-12 11:59:424128 GURL url_to_load;
4129 GURL virtual_url;
Nasko Oskov03912102019-01-11 00:21:324130
Camille Lamy2baa8022018-10-19 16:43:174131 // For main frames, rewrite the URL if necessary and compute the virtual URL
4132 // that should be shown in the address bar.
Ian Vollick1c6dd3e2022-04-13 02:06:264133 if (node->IsOutermostMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:424134 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:174135 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:424136 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:424137
Shu Yang112ad492024-07-25 17:11:544138 // Both LoadDataWithBaseURL and Android PDF navigations are special cases
4139 // that need to define a virtual URL to display, which differs from the
4140 // navigation URL.
4141 if (params.load_type == LOAD_TYPE_DATA) {
4142 virtual_url = params.virtual_url_for_special_cases;
4143 }
4144#if BUILDFLAG(IS_ANDROID)
4145 if (params.load_type == LOAD_TYPE_PDF_ANDROID) {
4146 virtual_url = params.virtual_url_for_special_cases;
4147 }
4148#endif
Camille Lamy5193caa2018-10-12 11:59:424149
Camille Lamy2baa8022018-10-19 16:43:174150 if (virtual_url.is_empty())
4151 virtual_url = url_to_load;
4152
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574153 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:284154
Aran Gilman249eb122019-12-02 23:32:464155 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
4156 // behavior: it is possible for two calls to RewriteUrlForNavigation to
4157 // return different results, leading to a different URL in the
4158 // NavigationRequest and FrameEntry. This will be fixed once we remove the
4159 // pending NavigationEntry, as we'll only make one call to
4160 // RewriteUrlForNavigation.
Charlie Reisf21cd182024-08-09 21:44:524161 if (frame_entry) {
4162 VLOG_IF(1, (url_to_load != frame_entry->url()))
4163 << "NavigationRequest and FrameEntry have different URLs: "
4164 << url_to_load << " vs " << frame_entry->url();
4165 }
Camille Lamyb9ed3c52018-11-19 15:34:284166
Camille Lamy2baa8022018-10-19 16:43:174167 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:424168 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:174169 // NavigationRequest.
4170 } else {
4171 url_to_load = params.url;
4172 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:244173 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:174174 }
Camille Lamy5193caa2018-10-12 11:59:424175
Ehsan Karamad44fc72112019-02-26 18:15:474176 if (node->render_manager()->is_attaching_inner_delegate()) {
4177 // Avoid starting any new navigations since this node is now preparing for
4178 // attaching an inner delegate.
4179 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:204180 }
Camille Lamy5193caa2018-10-12 11:59:424181
Alex Moshchuk99242832023-05-22 17:21:444182 if (!IsValidURLForNavigation(node, virtual_url, url_to_load)) {
Camille Lamy5193caa2018-10-12 11:59:424183 return nullptr;
Alex Moshchuk99242832023-05-22 17:21:444184 }
Camille Lamy5193caa2018-10-12 11:59:424185
danakjd83d706d2020-11-25 22:11:124186 // Look for a pending commit that is to another document in this
4187 // FrameTreeNode. If one exists, then the last committed URL will not be the
4188 // current URL by the time this navigation commits.
4189 bool has_pending_cross_document_commit =
4190 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
Miyoung Shina2dd6a42021-10-07 12:19:214191 bool is_currently_error_page = node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:124192
Minggang Wangb9f3fa92021-07-01 15:30:314193 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjb952ef12021-01-14 19:58:494194 /*old_url=*/node->current_url(),
Charlie Reisf21cd182024-08-09 21:44:524195 /*new_url=*/url_to_load, reload_type, entry, frame_entry,
danakjb952ef12021-01-14 19:58:494196 has_pending_cross_document_commit, is_currently_error_page,
Garrett Tanzer267c2b82022-07-26 16:53:134197 /*is_same_document_history_load=*/false,
4198 is_embedder_initiated_fenced_frame_navigation,
4199 is_unfenced_top_navigation);
Camille Lamy5193caa2018-10-12 11:59:424200
4201 // Create the NavigationParams based on |params|.
4202
Hiroki Nakagawa4ed61282021-06-18 05:37:234203 bool is_view_source_mode = entry->IsViewSourceMode();
4204 DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme));
Charlie Harrison8c113a32019-01-07 16:08:294205
Antonio Sartori6984c742021-08-26 08:03:414206 blink::NavigationDownloadPolicy download_policy = params.download_policy;
Yao Xiao720ef9d62022-12-09 05:18:294207
Hiroki Nakagawa4ed61282021-06-18 05:37:234208 // Update |download_policy| if the virtual URL is view-source.
Charlie Harrison8c113a32019-01-07 16:08:294209 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:254210 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:294211
Charlie Reisf21cd182024-08-09 21:44:524212 std::string page_state_data =
4213 frame_entry ? frame_entry->page_state().ToEncodedData() : std::string();
4214
Minggang Wangb9f3fa92021-07-01 15:30:314215 blink::mojom::CommonNavigationParamsPtr common_params =
4216 blink::mojom::CommonNavigationParams::New(
W. James MacLean23e90a12022-12-21 04:38:214217 url_to_load, params.initiator_origin, params.initiator_base_url,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:514218 blink::mojom::Referrer::New(params.referrer.url,
4219 params.referrer.policy),
Scott Violetcf6ea7e2021-06-09 21:09:214220 params.transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:514221 should_replace_current_entry, params.base_url_for_data_url,
Charlie Reise1d9b8182025-04-02 04:32:124222 actual_navigation_start_time, navigation_start_time,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:514223 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
Antonio Sartori2f763d9d2021-04-21 10:04:144224 params.post_data, std::move(source_location),
arthursonzogniaf7c62c52020-02-12 10:49:414225 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:274226 false /* has_text_fragment_token */,
4227 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
4228 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:514229 false /* is_history_navigation_in_new_child_frame */,
Chris Fredricksonb52bcd02023-03-28 14:48:054230 params.input_start, network::mojom::RequestDestination::kEmpty);
Camille Lamy5193caa2018-10-12 11:59:424231
Minggang Wangb9f3fa92021-07-01 15:30:314232 blink::mojom::CommitNavigationParamsPtr commit_params =
4233 blink::mojom::CommitNavigationParams::New(
Diana Quae845bf2025-04-03 20:12:374234 url::Origin(),
Ari Chivukulac7533522024-05-14 21:52:344235 // The correct storage key will be computed before committing the
4236 // navigation.
4237 blink::StorageKey(), override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:324238 std::vector<network::mojom::URLResponseHeadPtr>(),
jongdeok.kim5de823b32022-06-14 04:37:504239 std::vector<net::RedirectInfo>(), params.post_content_type,
4240 common_params->url, common_params->method,
Charlie Reisf21cd182024-08-09 21:44:524241 params.can_load_local_resources, page_state_data,
4242 entry->GetUniqueID(), entry->GetSubframeUniqueNames(node),
Yoav Weiss8c573952022-11-17 17:35:134243 /*intended_as_new_entry=*/true,
Charlie Reis99b2eba22025-01-31 19:18:574244 /*pending_history_list_index=*/-1,
Lucas Furukawa Gadania9c45682019-07-31 22:05:144245 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
4246 params.should_clear_history_list ? 0 : GetEntryCount(),
Yoav Weiss8c573952022-11-17 17:35:134247 /*was_discarded=*/false, is_view_source_mode,
Minggang Wangb9f3fa92021-07-01 15:30:314248 params.should_clear_history_list,
4249 blink::mojom::NavigationTiming::New(),
Minggang Wangf59db47b2021-06-16 01:56:224250 blink::mojom::WasActivatedOption::kUnknown,
Yoav Weiss8c573952022-11-17 17:35:134251 /*navigation_token=*/base::UnguessableToken::Create(),
Minggang Wang7ee0c742021-06-16 16:16:514252 std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(),
Xiaohan Wang7f8052e02022-01-14 18:44:284253#if BUILDFLAG(IS_ANDROID)
Yoav Weiss8c573952022-11-17 17:35:134254 /*data_url_as_string=*/std::string(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:144255#endif
Yoav Weiss8c573952022-11-17 17:35:134256 /*is_browser_initiated=*/!params.is_renderer_initiated,
Khushal Sagar71837ad82024-08-09 18:03:154257 /*has_ua_visual_transition*/ false,
Yoav Weiss8c573952022-11-17 17:35:134258 /*document_ukm_source_id=*/ukm::kInvalidSourceId,
Jiewei Qian0406fc02020-03-09 06:02:074259 node->pending_frame_policy(),
Yoav Weiss8c573952022-11-17 17:35:134260 /*force_enabled_origin_trials=*/std::vector<std::string>(),
4261 /*origin_agent_cluster=*/false,
4262 /*origin_agent_cluster_left_as_default=*/true,
4263 /*enabled_client_hints=*/
4264 std::vector<network::mojom::WebClientHintsType>(),
Liam Brady38b84562024-03-07 22:11:264265 /*is_cross_site_cross_browsing_context_group=*/false,
4266 /*should_have_sticky_user_activation=*/false,
4267 /*old_page_info=*/nullptr,
Yoav Weiss8c573952022-11-17 17:35:134268 /*http_response_code=*/-1,
Domenic Denicolacd30f5f82022-03-16 21:48:014269 blink::mojom::NavigationApiHistoryEntryArrays::New(),
Yoav Weiss8c573952022-11-17 17:35:134270 /*early_hints_preloaded_resources=*/std::vector<GURL>(),
Clark DuVall8ee487a22021-11-10 02:25:584271 // This timestamp will be populated when the commit IPC is sent.
Yoav Weiss8c573952022-11-17 17:35:134272 /*commit_sent=*/base::TimeTicks(), /*srcdoc_value=*/std::string(),
Yoav Weiss8c573952022-11-17 17:35:134273 /*should_load_data_url=*/false,
Victor Tan10d93aca2022-08-12 16:46:284274 /*ancestor_or_self_has_cspee=*/node->AncestorOrSelfHasCSPEE(),
Yoav Weiss8c573952022-11-17 17:35:134275 /*reduced_accept_language=*/std::string(),
William Liu2c825472022-10-31 12:01:444276 /*navigation_delivery_type=*/
Khushal Sagar7b26135c62022-11-08 20:25:424277 network::mojom::NavigationDeliveryType::kDefault,
Arthur Sonzognic686e8f2024-01-11 08:36:374278 /*view_transition_state=*/std::nullopt,
4279 /*soft_navigation_heuristics_task_id=*/std::nullopt,
sbinglera07ae732022-12-02 20:49:054280 /*modified_runtime_features=*/
Jiewei Qian87951f52023-10-17 01:33:464281 base::flat_map<::blink::mojom::RuntimeFeature, bool>(),
Arthur Sonzognic686e8f2024-01-11 08:36:374282 /*fenced_frame_properties=*/std::nullopt,
Chris Fredricksonb52bcd02023-03-28 14:48:054283 /*not_restored_reasons=*/nullptr,
Chris Fredrickson9ffdf5b2024-07-09 20:05:094284 /*load_with_storage_access=*/
4285 net::StorageAccessApiStatus::kNone,
Arthur Sonzognic686e8f2024-01-11 08:36:374286 /*browsing_context_group_info=*/std::nullopt,
Nan Linda394ba2023-11-03 21:17:374287 /*lcpp_hint=*/nullptr, blink::CreateDefaultRendererContentSettings(),
Kyra800d1d012024-03-27 17:05:484288 /*cookie_deprecation_label=*/std::nullopt,
Khushal Sagar81f89812024-06-11 13:40:314289 /*visited_link_salt=*/std::nullopt,
Thomas Nguyen42fa4c22024-11-06 14:29:164290 /*local_surface_id=*/std::nullopt,
Aldo Culquicondor17b73f72025-01-29 21:02:174291 node->current_frame_host()->GetCachedPermissionStatuses(),
Charlie Reiscdb798342025-02-14 22:04:464292 /*should_skip_screentshot=*/false,
Alex Moshchukd0d759c22025-05-09 18:18:354293 /*force_new_document_sequence_number=*/false,
4294 /*navigation_metrics_token=*/base::UnguessableToken::Create());
Xiaohan Wang7f8052e02022-01-14 18:44:284295#if BUILDFLAG(IS_ANDROID)
Camille Lamy5193caa2018-10-12 11:59:424296 if (ValidateDataURLAsString(params.data_url_as_string)) {
danakj529a3eba2024-04-18 20:14:564297 commit_params->data_url_as_string = params.data_url_as_string->as_string();
Camille Lamy5193caa2018-10-12 11:59:424298 }
4299#endif
4300
Lucas Furukawa Gadania9c45682019-07-31 22:05:144301 commit_params->was_activated = params.was_activated;
Mike Jacksone2aa7af2023-05-17 06:45:074302 commit_params->navigation_timing->system_entropy_at_navigation_start =
4303 SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode(
4304 node, params.suggested_system_entropy);
Camille Lamy5193caa2018-10-12 11:59:424305
Camille Lamy5193caa2018-10-12 11:59:424306 // extra_headers in params are \n separated; NavigationRequests want \r\n.
4307 std::string extra_headers_crlf;
4308 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:094309
Alex Moshchuk9321e6a2022-12-07 21:58:314310 auto navigation_request = NavigationRequest::Create(
Lucas Furukawa Gadania9c45682019-07-31 22:05:144311 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:084312 !params.is_renderer_initiated, params.was_opener_suppressed,
Hiroshige Hayashizakif07ad7812023-05-10 02:26:094313 params.initiator_frame_token, params.initiator_process_id,
4314 extra_headers_crlf, frame_entry, entry, params.is_form_submission,
John Delaney50425f82020-04-07 16:26:214315 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
Yao Xiao720ef9d62022-12-09 05:18:294316 params.impression, params.initiator_activation_and_ad_status,
Sergey Poromovdd557c12023-03-01 11:28:454317 params.is_pdf, is_embedder_initiated_fenced_frame_navigation,
W. James MacLean443ef3e2024-07-16 13:42:344318 is_container_initiated, params.has_rel_opener, storage_access_api_status,
Kevin McNee6455638a2024-06-27 22:05:034319 embedder_shared_storage_context);
Liam Bradyedb866e2025-01-22 22:28:134320
4321 if (!navigation_request) {
4322 return nullptr;
4323 }
4324
Yao Xiaodc5ed102019-06-04 19:19:094325 navigation_request->set_from_download_cross_origin_redirect(
4326 params.from_download_cross_origin_redirect);
W. James MacLean00568d72022-02-24 19:36:554327 navigation_request->set_force_new_browsing_instance(
4328 params.force_new_browsing_instance);
Steve Kobes8d111ba2024-11-12 15:23:514329 navigation_request->set_force_new_compositor(params.force_new_compositor);
Mustafa Emre Acer1001b3d2024-07-09 23:10:434330 if (params.force_no_https_upgrade) {
4331 navigation_request->set_force_no_https_upgrade();
4332 }
Yao Xiaodc5ed102019-06-04 19:19:094333 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:424334}
4335
4336std::unique_ptr<NavigationRequest>
4337NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:234338 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574339 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:234340 FrameNavigationEntry* frame_entry,
4341 ReloadType reload_type,
4342 bool is_same_document_history_load,
Nate Chapin45f620582021-09-30 17:45:434343 bool is_history_navigation_in_new_child_frame,
Arthur Sonzognic686e8f2024-01-11 08:36:374344 const std::optional<blink::LocalFrameToken>& initiator_frame_token,
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:334345 int initiator_process_id,
Charlie Reisf4d51f402025-05-23 18:00:494346 base::TimeTicks actual_navigation_start,
Arthur Sonzognic686e8f2024-01-11 08:36:374347 std::optional<blink::scheduler::TaskAttributionId>
Yoav Weiss8c573952022-11-17 17:35:134348 soft_navigation_heuristics_task_id) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:344349 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:234350 GURL dest_url = frame_entry->url();
Rakina Zata Amnif297a802022-01-18 03:53:434351 // We should never navigate to an existing initial NavigationEntry that is the
4352 // initial NavigationEntry for the initial empty document that hasn't been
4353 // overridden by the synchronous about:blank commit, to preserve previous
4354 // behavior where trying to reload when the main frame is on the initial empty
4355 // document won't result in a navigation.
4356 // See also https://crbug.com/1277414.
4357 DCHECK(!entry->IsInitialEntryNotForSynchronousAboutBlank());
Nasko Oskov03912102019-01-11 00:21:324358
clamyea99ea12018-05-28 13:54:234359 Referrer dest_referrer = frame_entry->referrer();
clamyea99ea12018-05-28 13:54:234360
Ehsan Karamad44fc72112019-02-26 18:15:474361 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
4362 // Avoid starting any new navigations since this node is now preparing for
4363 // attaching an inner delegate.
4364 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:204365 }
4366
Alex Moshchuk99242832023-05-22 17:21:444367 if (!IsValidURLForNavigation(frame_tree_node, entry->GetVirtualURL(),
4368 dest_url)) {
clamyea99ea12018-05-28 13:54:234369 return nullptr;
4370 }
4371
clamyea99ea12018-05-28 13:54:234372 // This will be used to set the Navigation Timing API navigationStart
4373 // parameter for browser navigations in new tabs (intents, tabs opened through
4374 // "Open link in new tab"). If the navigation must wait on the current
4375 // RenderFrameHost to execute its BeforeUnload event, the navigation start
4376 // will be updated when the BeforeUnload ack is received.
clamyea99ea12018-05-28 13:54:234377 base::TimeTicks navigation_start = base::TimeTicks::Now();
Mike Jacksone2aa7af2023-05-17 06:45:074378 const auto navigation_start_system_entropy =
4379 SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode(
4380 frame_tree_node, blink::mojom::SystemEntropy::kNormal);
clamyea99ea12018-05-28 13:54:234381
danakjd83d706d2020-11-25 22:11:124382 // Look for a pending commit that is to another document in this
4383 // FrameTreeNode. If one exists, then the last committed URL will not be the
4384 // current URL by the time this navigation commits.
4385 bool has_pending_cross_document_commit =
4386 frame_tree_node->render_manager()
4387 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:494388 bool is_currently_error_page =
Miyoung Shina2dd6a42021-10-07 12:19:214389 frame_tree_node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:124390
Minggang Wangb9f3fa92021-07-01 15:30:314391 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:124392 /*old_url=*/frame_tree_node->current_url(),
Charlie Reisf21cd182024-08-09 21:44:524393 /*new_url=*/dest_url, reload_type, entry, frame_entry,
danakjb952ef12021-01-14 19:58:494394 has_pending_cross_document_commit, is_currently_error_page,
Garrett Tanzer405f3402022-07-21 20:12:494395 is_same_document_history_load,
Garrett Tanzer267c2b82022-07-26 16:53:134396 /*is_embedder_initiated_fenced_frame_navigation=*/false,
Garrett Tanzer405f3402022-07-21 20:12:494397 /*is_unfenced_top_navigation=*/false);
Camille Lamy5193caa2018-10-12 11:59:424398
4399 // A form submission may happen here if the navigation is a
4400 // back/forward/reload navigation that does a form resubmission.
4401 scoped_refptr<network::ResourceRequestBody> request_body;
4402 std::string post_content_type;
Alison Galed94ce4f2024-04-22 15:20:394403 // TODO(crbug.com/41440869) Store |is_form_submission| in the history
jongdeok.kim5de823b32022-06-14 04:37:504404 // entry. This way, it could be directly retrieved here. Right now, it is only
4405 // partially recovered when request.method == "POST" and request.body exists.
4406 bool is_form_submission = false;
Camille Lamy5193caa2018-10-12 11:59:424407 if (frame_entry->method() == "POST") {
4408 request_body = frame_entry->GetPostData(&post_content_type);
4409 // Might have a LF at end.
Peter Kastingb53b81912021-04-28 19:23:304410 post_content_type = std::string(
4411 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL));
jongdeok.kim5de823b32022-06-14 04:37:504412 is_form_submission = !!request_body;
Camille Lamy5193caa2018-10-12 11:59:424413 }
4414
4415 // Create the NavigationParams based on |entry| and |frame_entry|.
Minggang Wangb9f3fa92021-07-01 15:30:314416 blink::mojom::CommonNavigationParamsPtr common_params =
Lucas Furukawa Gadanief8290a2019-07-29 20:27:514417 entry->ConstructCommonNavigationParams(
4418 *frame_entry, request_body, dest_url,
4419 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
Charlie Reisf4d51f402025-05-23 18:00:494420 navigation_type, actual_navigation_start, navigation_start,
Charlie Reise1d9b8182025-04-02 04:32:124421 /*input_start=*/base::TimeTicks());
Lucas Furukawa Gadanief8290a2019-07-29 20:27:514422 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:554423 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:424424
4425 // TODO(clamy): |intended_as_new_entry| below should always be false once
4426 // Reload no longer leads to this being called for a pending NavigationEntry
4427 // of index -1.
Minggang Wangb9f3fa92021-07-01 15:30:314428 blink::mojom::CommitNavigationParamsPtr commit_params =
Lucas Furukawa Gadania9c45682019-07-31 22:05:144429 entry->ConstructCommitNavigationParams(
Rakina Zata Amnic7367852022-11-07 17:10:404430 *frame_entry, common_params->url, common_params->method,
4431 entry->GetSubframeUniqueNames(frame_tree_node),
Lucas Furukawa Gadania9c45682019-07-31 22:05:144432 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:534433 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
Liam Bradyd2a41e152022-07-19 13:58:484434 frame_tree_node->pending_frame_policy(),
Yoav Weiss8c573952022-11-17 17:35:134435 frame_tree_node->AncestorOrSelfHasCSPEE(),
Mike Jacksone2aa7af2023-05-17 06:45:074436 navigation_start_system_entropy, soft_navigation_heuristics_task_id);
Lucas Furukawa Gadania9c45682019-07-31 22:05:144437 commit_params->post_content_type = post_content_type;
Mike Jacksone2aa7af2023-05-17 06:45:074438 commit_params->navigation_timing->system_entropy_at_navigation_start =
4439 SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode(
4440 frame_tree_node, blink::mojom::SystemEntropy::kNormal);
Thomas Nguyen42fa4c22024-11-06 14:29:164441 commit_params->initial_permission_statuses =
4442 frame_tree_node->current_frame_host()->GetCachedPermissionStatuses();
Camille Lamy5193caa2018-10-12 11:59:424443
W. James MacLeanb7d6092682022-10-05 15:23:264444 if (common_params->url.IsAboutSrcdoc()) {
4445 // TODO(wjmaclean): initialize this in NavigationRequest's constructor
4446 // instead.
W. James MacLean81b8d01f2022-01-25 20:50:594447 commit_params->srcdoc_value = frame_tree_node->srcdoc_value();
W. James MacLeanb7d6092682022-10-05 15:23:264448 }
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:334449 const bool is_browser_initiated = !initiator_frame_token;
Alex Moshchuk9321e6a2022-12-07 21:58:314450 return NavigationRequest::Create(
Lucas Furukawa Gadania9c45682019-07-31 22:05:144451 frame_tree_node, std::move(common_params), std::move(commit_params),
Nate Chapin45f620582021-09-30 17:45:434452 is_browser_initiated, false /* was_opener_suppressed */,
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:334453 initiator_frame_token, initiator_process_id, entry->extra_headers(),
4454 frame_entry, entry, is_form_submission, nullptr /* navigation_ui_data */,
Arthur Sonzognic686e8f2024-01-11 08:36:374455 std::nullopt /* impression */,
Yao Xiao720ef9d62022-12-09 05:18:294456 blink::mojom::NavigationInitiatorActivationAndAdStatus::
4457 kDidNotStartWithTransientActivation,
Daniel Hosseinianf0fbfb42021-09-08 02:20:474458 false /* is_pdf */);
clamyea99ea12018-05-28 13:54:234459}
4460
[email protected]d202a7c2012-01-04 07:53:474461void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:214462 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:274463 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:404464
Takashi Toyoshimaea534ef22021-07-21 03:27:594465 // We need to notify the ssl_manager_ before the WebContents so the
[email protected]df1af242009-05-01 00:11:404466 // location bar will have up-to-date information about the security style
4467 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:134468 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:404469
Abhijeet Kandalkar3dc6e602022-11-09 05:08:374470 delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:574471 delegate_->NotifyNavigationEntryCommitted(*details);
initial.commit09911bf2008-07-26 23:55:294472}
4473
initial.commit09911bf2008-07-26 23:55:294474// static
[email protected]d202a7c2012-01-04 07:53:474475size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:234476 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:154477 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:214478 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:234479}
4480
[email protected]d202a7c2012-01-04 07:53:474481void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:224482 if (is_active && needs_reload_)
4483 LoadIfNecessary();
Khushal Sagarace0e7342024-08-07 21:34:214484
4485 if (frame_tree_->is_primary();
4486 auto* cache = GetNavigationEntryScreenshotCache()) {
4487 cache->SetVisible(is_active);
4488 }
initial.commit09911bf2008-07-26 23:55:294489}
4490
[email protected]d202a7c2012-01-04 07:53:474491void NavigationControllerImpl::LoadIfNecessary() {
Charlie Reisf4d51f402025-05-23 18:00:494492 base::TimeTicks actual_navigation_start = base::TimeTicks::Now();
Rakina Zata Amnid605d462022-06-01 10:17:034493 SCOPED_CRASH_KEY_BOOL("nav_reentrancy_caller1", "LoadIf_pending",
4494 !!pending_entry_);
initial.commit09911bf2008-07-26 23:55:294495 if (!needs_reload_)
4496 return;
4497
Bo Liucdfa4b12018-11-06 00:21:444498 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
4499 needs_reload_type_);
4500
initial.commit09911bf2008-07-26 23:55:294501 // Calling Reload() results in ignoring state, and not loading.
Xinghui Lue4e50be2024-05-15 21:46:004502 // Explicitly use NavigateToExistingPendingEntry so that the renderer uses the
initial.commit09911bf2008-07-26 23:55:294503 // cached state.
Xinghui Lue4e50be2024-05-15 21:46:004504 if (entry_replaced_by_post_commit_error_) {
4505 // If the current entry is a post commit error, we reload the entry it
4506 // replaced instead. We leave the error entry in place until a commit
4507 // replaces it, but the pending entry points to the original entry in the
4508 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
4509 // case that pending_entry_ != entries_[pending_entry_index_].
4510 // Note that this handling is similar to
4511 // `NavigationControllerImpl::Reload()`.
4512 pending_entry_ = entry_replaced_by_post_commit_error_.get();
4513 pending_entry_index_ = GetCurrentEntryIndex();
4514 NavigateToExistingPendingEntry(
4515 ReloadType::NONE,
4516 /*initiator_rfh=*/nullptr,
4517 /*soft_navigation_heuristics_task_id=*/std::nullopt,
Charlie Reisf4d51f402025-05-23 18:00:494518 /*navigation_api_key=*/nullptr, actual_navigation_start);
Xinghui Lue4e50be2024-05-15 21:46:004519 } else if (pending_entry_) {
Yoav Weiss8c573952022-11-17 17:35:134520 NavigateToExistingPendingEntry(
4521 ReloadType::NONE,
4522 /*initiator_rfh=*/nullptr,
Arthur Sonzognic686e8f2024-01-11 08:36:374523 /*soft_navigation_heuristics_task_id=*/std::nullopt,
Charlie Reisf4d51f402025-05-23 18:00:494524 /*navigation_api_key=*/nullptr, actual_navigation_start);
Rakina Zata Amnie2d31312022-11-18 03:38:454525 } else if (!GetLastCommittedEntry()
Rakina Zata Amnif297a802022-01-18 03:53:434526 ->IsInitialEntryNotForSynchronousAboutBlank()) {
arthursonzogni5c4c202d2017-04-25 23:41:274527 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:344528 pending_entry_index_ = last_committed_entry_index_;
Yoav Weiss8c573952022-11-17 17:35:134529 NavigateToExistingPendingEntry(
4530 ReloadType::NONE,
4531 /*initiator_rfh=*/nullptr,
Arthur Sonzognic686e8f2024-01-11 08:36:374532 /*soft_navigation_heuristics_task_id=*/std::nullopt,
Charlie Reisf4d51f402025-05-23 18:00:494533 /*navigation_api_key=*/nullptr, actual_navigation_start);
avicc872d7242015-08-19 21:26:344534 } else {
Rakina Zata Amnif297a802022-01-18 03:53:434535 // We should never navigate to an existing initial NavigationEntry that is
4536 // the initial NavigationEntry for the initial empty document that hasn't
4537 // been overridden by the synchronous about:blank commit, to preserve
4538 // legacy behavior where trying to reload when the main frame is on the
4539 // initial empty document won't result in a navigation. See also
4540 // https://crbug.com/1277414. If there is something to reload, the
4541 // successful reload will clear the |needs_reload_| flag. Otherwise, just do
4542 // it here.
avicc872d7242015-08-19 21:26:344543 needs_reload_ = false;
4544 }
initial.commit09911bf2008-07-26 23:55:294545}
4546
Kevin McNeeccca6172021-10-19 17:11:144547base::WeakPtr<NavigationHandle>
4548NavigationControllerImpl::LoadPostCommitErrorPage(
Carlos IL42b416592019-10-07 23:10:364549 RenderFrameHost* render_frame_host,
4550 const GURL& url,
Lei Zhanga4770832023-07-19 18:02:364551 const std::string& error_page_html) {
Rakina Zata Amni919b7922020-12-11 09:03:134552 RenderFrameHostImpl* rfhi =
4553 static_cast<RenderFrameHostImpl*>(render_frame_host);
Andrew Verge754c70a2025-04-17 17:19:194554 std::unique_ptr<NavigationRequest> navigation_request =
4555 CreateNavigationRequestForErrorPage(rfhi, url, error_page_html,
4556 /*is_post_commit_error_page=*/true);
4557 if (!navigation_request) {
Kevin McNeeccca6172021-10-19 17:11:144558 return nullptr;
Fergal Daly1336ac642021-09-14 15:13:114559 }
Sreeja Kamishettydb8e2892021-03-10 09:30:584560
Rakina Zata Amni919b7922020-12-11 09:03:134561 FrameTreeNode* node = rfhi->frame_tree_node();
Charlie Reis09952ee2022-12-08 16:35:074562 node->TakeNavigationRequest(std::move(navigation_request));
John Delaney131ad362019-08-08 21:57:414563 DCHECK(node->navigation_request());
Kevin McNeeccca6172021-10-19 17:11:144564
Andrew Verge754c70a2025-04-17 17:19:194565 // TODO(crbug.com/406729265): LoadPostCommitErrorPage() does not initiate a
4566 // navigation via Navigator::Navigate(). We should fix that, so that
4567 // post-commit error page navigations don't bypass other important checks in
4568 // this function.
Kevin McNeeccca6172021-10-19 17:11:144569 // Calling BeginNavigation may destroy the NavigationRequest.
4570 base::WeakPtr<NavigationRequest> created_navigation_request(
4571 node->navigation_request()->GetWeakPtr());
John Delaney131ad362019-08-08 21:57:414572 node->navigation_request()->BeginNavigation();
Kevin McNeeccca6172021-10-19 17:11:144573 return created_navigation_request;
John Delaney131ad362019-08-08 21:57:414574}
4575
Andrew Verge754c70a2025-04-17 17:19:194576void NavigationControllerImpl::NavigateFrameToErrorPage(
4577 RenderFrameHostImpl* render_frame_host_impl,
4578 const GURL& url,
4579 const std::string& error_page_html) {
Andrew Verge754c70a2025-04-17 17:19:194580 std::unique_ptr<NavigationRequest> navigation_request =
4581 CreateNavigationRequestForErrorPage(render_frame_host_impl, url,
4582 error_page_html,
4583 /*is_post_commit_error_page=*/false);
4584 if (!navigation_request) {
4585 return;
4586 }
4587
4588 FrameTreeNode* node = render_frame_host_impl->frame_tree_node();
4589 node->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
4590}
4591
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574592void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:214593 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:094594 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:154595 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:144596 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:294597}
4598
[email protected]d202a7c2012-01-04 07:53:474599void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:364600 RestoreType type) {
Alison Gale770f3fc2024-04-27 00:39:584601 // TODO(crbug.com/40816356): Don't allow an index of -1, which would
Charlie Reis23c26da2022-01-29 00:57:474602 // represent a no-committed-entry state.
4603 DCHECK(selected_index >= -1 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:364604 ConfigureEntriesForRestore(&entries_, type);
Alison Gale770f3fc2024-04-27 00:39:584605 // TODO(crbug.com/40816356): This will be pointing to the wrong entry
Charlie Reis23c26da2022-01-29 00:57:474606 // if `entries_` contains pre-existing entries from the NavigationController
4607 // before restore, which would not be removed and will be at the front of the
4608 // entries list, causing the index to be off by the amount of pre-existing
4609 // entries in the list. Fix this to point to the correct entry.
initial.commit09911bf2008-07-26 23:55:294610 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:294611}
[email protected]765b35502008-08-21 00:51:204612
arthursonzogni69a6a1b2019-09-17 09:23:004613void NavigationControllerImpl::DiscardNonCommittedEntries() {
Rakina Zata Amnia4e27222021-12-22 01:05:004614 DiscardNonCommittedEntriesWithCommitDetails(nullptr /* commit_details */);
4615}
4616
4617void NavigationControllerImpl::DiscardNonCommittedEntriesWithCommitDetails(
4618 LoadCommittedDetails* commit_details) {
Michael Thiessen9b14d512019-09-23 21:19:474619 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:104620 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
4621 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:294622 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:474623 return;
Michael Thiessenc5676d22019-09-25 22:32:104624 }
avi45a72532015-04-07 21:01:454625 DiscardPendingEntry(false);
Rakina Zata Amnidaa84f62022-02-17 00:55:314626
4627 if (!delegate_)
4628 return;
Abhijeet Kandalkar3dc6e602022-11-09 05:08:374629 delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:484630}
4631
avi7c6f35e2015-05-08 17:52:384632int NavigationControllerImpl::GetEntryIndexWithUniqueID(
4633 int nav_entry_id) const {
4634 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
4635 if (entries_[i]->GetUniqueID() == nav_entry_id)
4636 return i;
4637 }
4638 return -1;
4639}
4640
[email protected]d202a7c2012-01-04 07:53:474641void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574642 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:254643 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574644 DCHECK_LE(max_index, source->GetEntryCount());
Kevin McNee433daf242023-10-31 20:15:594645 NavigationEntryRestoreContextImpl context;
[email protected]e1cd5452010-08-26 18:03:254646 for (int i = 0; i < max_index; i++) {
Nate Chapin9f169072021-06-09 19:32:374647 // Normally, cloning a NavigationEntryImpl results in sharing
4648 // FrameNavigationEntries between the original and the clone. However, when
4649 // cloning from a different NavigationControllerImpl, we want to fork the
4650 // FrameNavigationEntries.
Nate Chapin9f169072021-06-09 19:32:374651 entries_.insert(entries_.begin() + i,
Kevin McNee433daf242023-10-31 20:15:594652 source->entries_[i]->CloneWithoutSharing(&context));
[email protected]e1cd5452010-08-26 18:03:254653 }
Rakina Zata Amnie2d31312022-11-18 03:38:454654 DCHECK_GE(entries_.size(), 1u);
arthursonzogni5c4c202d2017-04-25 23:41:274655 DCHECK(pending_entry_index_ == -1 ||
4656 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:254657}
[email protected]c5b88d82012-10-06 17:03:334658
4659void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:184660 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:334661 get_timestamp_callback_ = get_timestamp_callback;
4662}
[email protected]8ff00d72012-10-23 19:12:214663
Shivani Sharmaffb32b82019-04-09 16:58:474664// History manipulation intervention:
4665void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:474666 bool replace_entry,
Shivani Sharmaeef521b2024-01-18 13:03:564667 bool previous_document_had_history_intervention_activation,
Alexander Timine3ec4192020-04-20 16:39:404668 bool is_renderer_initiated,
4669 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharmaeef521b2024-01-18 13:03:564670 // Note that for a subframe,
4671 // previous_document_had_history_intervention_activation is true if the
Shivani Sharma712d5d72019-04-16 21:56:454672 // gesture happened in any subframe (propagated to main frame) or in the main
4673 // frame itself.
Shivani Sharmaeef521b2024-01-18 13:03:564674 if (replace_entry || previous_document_had_history_intervention_activation ||
shivanigithubcceeacf2020-03-06 20:00:274675 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:474676 return;
4677 }
4678 if (last_committed_entry_index_ == -1)
4679 return;
4680
Shivani Sharmac4cc8922019-04-18 03:11:174681 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:474682
Alexander Timine3ec4192020-04-20 16:39:404683 // Log UKM with the URL we are navigating away from.
4684 ukm::builders::HistoryManipulationIntervention(
4685 previous_page_load_ukm_source_id)
4686 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:474687}
4688
Shivani Sharmac4cc8922019-04-18 03:11:174689void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
4690 int reference_index,
4691 bool skippable) {
4692 auto* reference_entry = GetEntryAtIndex(reference_index);
4693 reference_entry->set_should_skip_on_back_forward_ui(skippable);
4694
4695 int64_t document_sequence_number =
4696 reference_entry->root_node()->frame_entry->document_sequence_number();
4697 for (int index = 0; index < GetEntryCount(); index++) {
4698 auto* entry = GetEntryAtIndex(index);
4699 if (entry->root_node()->frame_entry->document_sequence_number() ==
4700 document_sequence_number) {
4701 entry->set_should_skip_on_back_forward_ui(skippable);
4702 }
4703 }
4704}
4705
arthursonzogni66f711c2019-10-08 14:40:364706std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
4707NavigationControllerImpl::ReferencePendingEntry() {
4708 DCHECK(pending_entry_);
4709 auto pending_entry_ref =
4710 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
4711 pending_entry_refs_.insert(pending_entry_ref.get());
4712 return pending_entry_ref;
4713}
4714
4715void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
4716 // Ignore refs that don't correspond to the current pending entry.
4717 auto it = pending_entry_refs_.find(ref);
4718 if (it == pending_entry_refs_.end())
4719 return;
4720 pending_entry_refs_.erase(it);
4721
4722 if (!pending_entry_refs_.empty())
4723 return;
4724
4725 // The pending entry may be deleted before the last PendingEntryRef.
4726 if (!pending_entry_)
4727 return;
4728
4729 // We usually clear the pending entry when the matching NavigationRequest
4730 // fails, so that an arbitrary URL isn't left visible above a committed page.
4731 //
4732 // However, we do preserve the pending entry in some cases, such as on the
4733 // initial navigation of an unmodified blank tab. We also allow the delegate
4734 // to say when it's safe to leave aborted URLs in the omnibox, to let the
4735 // user edit the URL and try again. This may be useful in cases that the
4736 // committed page cannot be attacker-controlled. In these cases, we still
4737 // allow the view to clear the pending entry and typed URL if the user
4738 // requests (e.g., hitting Escape with focus in the address bar).
4739 //
4740 // Do not leave the pending entry visible if it has an invalid URL, since this
4741 // might be formatted in an unexpected or unsafe way.
4742 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:364743 bool should_preserve_entry =
4744 (pending_entry_ == GetVisibleEntry()) &&
4745 pending_entry_->GetURL().is_valid() &&
4746 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
4747 if (should_preserve_entry)
4748 return;
4749
4750 DiscardPendingEntry(true);
Abhijeet Kandalkar3dc6e602022-11-09 05:08:374751 delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_URL);
arthursonzogni66f711c2019-10-08 14:40:364752}
4753
Titouan Rigoudy6ec70402021-02-02 15:42:194754std::unique_ptr<PolicyContainerPolicies>
4755NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:394756 RenderFrameHostImpl* rfh,
4757 bool is_same_document,
Antonio Sartorib8addf62024-09-16 07:59:214758 bool navigation_encountered_error,
Rakina Zata Amniafd3c6582021-11-30 06:19:174759 const GURL& url) {
Antonio Sartorib8addf62024-09-16 07:59:214760 if (navigation_encountered_error) {
4761 // We should never reload the policy container of an error page from
4762 // history, see https://crbug.com/364773822.
4763 return nullptr;
4764 }
4765
Antonio Sartori78a749f2020-11-30 12:03:394766 if (is_same_document) {
Rakina Zata Amnie2d31312022-11-18 03:38:454767 DCHECK(GetLastCommittedEntry());
Antonio Sartori78a749f2020-11-30 12:03:394768 FrameNavigationEntry* previous_frame_entry =
4769 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
4770
Alison Gale770f3fc2024-04-27 00:39:584771 // TODO(crbug.com/40467594): Remove this nullptr check when we can
Antonio Sartori78a749f2020-11-30 12:03:394772 // ensure we always have a FrameNavigationEntry here.
4773 if (!previous_frame_entry)
4774 return nullptr;
4775
Titouan Rigoudy6ec70402021-02-02 15:42:194776 const PolicyContainerPolicies* previous_policies =
4777 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:394778
Titouan Rigoudy6ec70402021-02-02 15:42:194779 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:394780 return nullptr;
4781
4782 // Make a copy of the policy container for the new FrameNavigationEntry.
Titouan Rigoudy72f892d2022-05-02 18:21:234783 return previous_policies->ClonePtr();
Antonio Sartori78a749f2020-11-30 12:03:394784 }
4785
Titouan Rigoudy72f892d2022-05-02 18:21:234786 return rfh->policy_container_host()->policies().ClonePtr();
Antonio Sartori78a749f2020-11-30 12:03:394787}
4788
Charlie Reis99b2eba22025-01-31 19:18:574789void NavigationControllerImpl::BroadcastHistoryIndexAndLength() {
Carlos Caballeroede6f8c2021-01-28 11:01:504790 OPTIONAL_TRACE_EVENT2(
Charlie Reis99b2eba22025-01-31 19:18:574791 "content", "NavigationControllerImpl::BroadcastHistoryIndexAndLength",
4792 "history_index", GetLastCommittedEntryIndex(), "history_length",
Hayato Ito2c8c08d02021-06-23 03:38:434793 GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:504794
Charlie Reis99b2eba22025-01-31 19:18:574795 int history_index = GetLastCommittedEntryIndex();
Kevin McNee7705fe82024-11-07 18:56:314796 int history_length = GetEntryCount();
Charlie Reis99b2eba22025-01-31 19:18:574797 auto callback = [history_index, history_length](RenderViewHostImpl* rvh) {
Kevin McNee7705fe82024-11-07 18:56:314798 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
Charlie Reis99b2eba22025-01-31 19:18:574799 broadcast->SetHistoryIndexAndLength(history_index, history_length);
Kevin McNee7705fe82024-11-07 18:56:314800 }
4801 };
Ali Hijazid87307d2022-11-07 20:15:034802 frame_tree_->root()->render_manager()->ExecutePageBroadcastMethod(callback);
Carlos Caballeroede6f8c2021-01-28 11:01:504803}
4804
4805void NavigationControllerImpl::DidAccessInitialMainDocument() {
4806 // We may have left a failed browser-initiated navigation in the address bar
4807 // to let the user edit it and try again. Clear it now that content might
4808 // show up underneath it.
Ali Hijazid87307d2022-11-07 20:15:034809 if (!frame_tree_->IsLoadingIncludingInnerFrameTrees() && GetPendingEntry())
Carlos Caballeroede6f8c2021-01-28 11:01:504810 DiscardPendingEntry(false);
4811
4812 // Update the URL display.
Abhijeet Kandalkar3dc6e602022-11-09 05:08:374813 delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_URL);
Carlos Caballeroede6f8c2021-01-28 11:01:504814}
4815
4816void NavigationControllerImpl::UpdateStateForFrame(
4817 RenderFrameHostImpl* rfhi,
4818 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:564819 OPTIONAL_TRACE_EVENT1("content",
4820 "NavigationControllerImpl::UpdateStateForFrame",
4821 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:504822 // The state update affects the last NavigationEntry associated with the given
4823 // |render_frame_host|. This may not be the last committed NavigationEntry (as
4824 // in the case of an UpdateState from a frame being swapped out). We track
4825 // which entry this is in the RenderFrameHost's nav_entry_id.
4826 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
4827 if (!entry)
4828 return;
4829
4830 FrameNavigationEntry* frame_entry =
4831 entry->GetFrameEntry(rfhi->frame_tree_node());
4832 if (!frame_entry)
4833 return;
4834
4835 // The SiteInstance might not match if we do a cross-process navigation with
4836 // replacement (e.g., auto-subframe), in which case the swap out of the old
4837 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4838 // has already been replaced and destroyed.
4839 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4840 return;
4841
4842 if (page_state == frame_entry->page_state())
4843 return; // Nothing to update.
4844
4845 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4846
4847 // The document_sequence_number and item_sequence_number recorded in the
4848 // FrameNavigationEntry should not differ from the one coming with the update,
4849 // since it must come from the same document. Do not update it if a difference
4850 // is detected, as this indicates that |frame_entry| is not the correct one.
4851 blink::ExplodedPageState exploded_state;
4852 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4853 return;
4854
4855 if (exploded_state.top.document_sequence_number !=
4856 frame_entry->document_sequence_number() ||
4857 exploded_state.top.item_sequence_number !=
4858 frame_entry->item_sequence_number()) {
4859 return;
4860 }
4861
4862 frame_entry->SetPageState(page_state);
4863 NotifyEntryChanged(entry);
4864}
4865
Nate Chapina2c881f52023-11-07 17:02:094866namespace {
4867
4868// The caller is responsible for ensuring the entry is same-origin to the
4869// origin to be committed.
4870blink::mojom::NavigationApiHistoryEntryPtr ToNavigationApiHistoryEntry(
4871 FrameNavigationEntry* frame_entry,
4872 int64_t pending_document_sequence_number) {
4873 blink::ExplodedPageState exploded_state;
4874 if (!blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4875 &exploded_state)) {
4876 return nullptr;
4877 }
4878 blink::ExplodedFrameState frame_state = exploded_state.top;
4879
4880 // If the document represented by this FNE hid its full url from appearing in
4881 // a referrer via a "no-referrer" or "origin" referrer policy, censor the url
4882 // in the navigation API as well (unless we're navigating to that document).
4883 std::u16string url;
4884 if (pending_document_sequence_number ==
4885 frame_entry->document_sequence_number() ||
4886 !frame_entry->protect_url_in_navigation_api()) {
4887 url = frame_state.url_string.value_or(std::u16string());
4888 }
4889
4890 return blink::mojom::NavigationApiHistoryEntry::New(
4891 frame_state.navigation_api_key.value_or(std::u16string()),
4892 frame_state.navigation_api_id.value_or(std::u16string()), url,
4893 frame_state.item_sequence_number, frame_state.document_sequence_number,
4894 frame_state.navigation_api_state);
4895}
4896
4897} // namespace
4898
Domenic Denicolacd30f5f82022-03-16 21:48:014899std::vector<blink::mojom::NavigationApiHistoryEntryPtr>
4900NavigationControllerImpl::PopulateSingleNavigationApiHistoryEntryVector(
Nate Chapind1fe3612021-04-16 20:45:574901 Direction direction,
4902 int entry_index,
4903 const url::Origin& pending_origin,
4904 FrameTreeNode* node,
4905 SiteInstance* site_instance,
Nate Chapin63db0d12022-01-20 22:03:304906 int64_t pending_item_sequence_number,
Nate Chapina2c881f52023-11-07 17:02:094907 int64_t pending_document_sequence_number,
4908 int& last_index_checked) {
Domenic Denicolacd30f5f82022-03-16 21:48:014909 std::vector<blink::mojom::NavigationApiHistoryEntryPtr> entries;
Rakina Zata Amnie2d31312022-11-18 03:38:454910 if (GetLastCommittedEntry()->IsInitialEntry()) {
Rakina Zata Amniafd3c6582021-11-30 06:19:174911 // Don't process the initial entry.
4912 DCHECK_EQ(GetEntryCount(), 1);
4913 return entries;
4914 }
Nate Chapind1fe3612021-04-16 20:45:574915 int offset = direction == Direction::kForward ? 1 : -1;
Nate Chapin63db0d12022-01-20 22:03:304916 int64_t previous_item_sequence_number = pending_item_sequence_number;
Nate Chapind1fe3612021-04-16 20:45:574917 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4918 i += offset) {
Nate Chapina2c881f52023-11-07 17:02:094919 last_index_checked = i;
Nate Chapind1fe3612021-04-16 20:45:574920 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
Nate Chapindedfa642022-01-28 23:59:414921 if (!frame_entry)
Nate Chapind1fe3612021-04-16 20:45:574922 break;
Domenic Denicolacd30f5f82022-03-16 21:48:014923 // An entry should only appear in the navigation API entries if it is for
4924 // the same origin as the document being committed. Check the committed
4925 // origin, or if that is not available (during restore), check against the
4926 // FNE's url.
Alison Gale770f3fc2024-04-27 00:39:584927 // TODO(crbug.com/40181982): Move this into ToNavigationApiHistoryEntry()
Nate Chapina2c881f52023-11-07 17:02:094928 // once we can be sure that entries with the same ISN will never be
4929 // cross-origin.
Nate Chapindedfa642022-01-28 23:59:414930 url::Origin frame_entry_origin =
4931 frame_entry->committed_origin().value_or(url::Origin::Resolve(
4932 frame_entry->url(),
4933 frame_entry->initiator_origin().value_or(url::Origin())));
4934 if (!pending_origin.IsSameOriginWith(frame_entry_origin))
Nate Chapind1fe3612021-04-16 20:45:574935 break;
4936 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4937 continue;
Nate Chapina2c881f52023-11-07 17:02:094938 if (blink::mojom::NavigationApiHistoryEntryPtr entry =
4939 ToNavigationApiHistoryEntry(frame_entry,
4940 pending_document_sequence_number)) {
Nate Chapin63db0d12022-01-20 22:03:304941 DCHECK(entry->url.empty() ||
4942 pending_origin.CanBeDerivedFrom(GURL(entry->url)));
Nate Chapind1fe3612021-04-16 20:45:574943 entries.push_back(std::move(entry));
4944 previous_item_sequence_number = frame_entry->item_sequence_number();
4945 }
4946 }
4947 // If |entries| was constructed by iterating backwards from
4948 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4949 // earliest-at-the-front. Reverse it.
4950 if (direction == Direction::kBack)
4951 std::reverse(entries.begin(), entries.end());
4952 return entries;
4953}
4954
Domenic Denicolacd30f5f82022-03-16 21:48:014955blink::mojom::NavigationApiHistoryEntryArraysPtr
4956NavigationControllerImpl::GetNavigationApiHistoryEntryVectors(
Nate Chapin97d2f542022-02-18 01:34:554957 FrameTreeNode* node,
Nate Chapind1fe3612021-04-16 20:45:574958 NavigationRequest* request) {
Rakina Zata Amnic7367852022-11-07 17:10:404959 url::Origin pending_origin = request
Lukasz Anforowicz435e68d2022-11-09 21:47:444960 ? request->GetOriginToCommit().value()
Rakina Zata Amnic7367852022-11-07 17:10:404961 : url::Origin::Create(node->current_url());
Nate Chapind1fe3612021-04-16 20:45:574962
Nate Chapind1fe3612021-04-16 20:45:574963 scoped_refptr<SiteInstance> site_instance =
Nate Chapin97d2f542022-02-18 01:34:554964 node->current_frame_host()->GetSiteInstance();
Nate Chapind1fe3612021-04-16 20:45:574965
Nate Chapine82339d02022-05-03 23:48:254966 // NOTE: |entry_index| is the index where this entry will commit if no
4967 // modifications are made between now and DidCommitNavigation. This is used to
4968 // walk |entries_| and determine which entries should be exposed by the
4969 // navigation API. It is important to calculate this correctly, because blink
4970 // will cancel a same-document history commit if it's not present in the
4971 // entries blink knows about.
4972 int entry_index = GetLastCommittedEntryIndex();
Nate Chapind1fe3612021-04-16 20:45:574973 int64_t pending_item_sequence_number = 0;
Nate Chapin63db0d12022-01-20 22:03:304974 int64_t pending_document_sequence_number = 0;
Nate Chapine82339d02022-05-03 23:48:254975 bool will_create_new_entry = false;
4976 if (GetPendingEntryIndex() != -1) {
4977 entry_index = GetPendingEntryIndex();
4978 if (auto* frame_entry = GetPendingEntry()->GetFrameEntry(node)) {
4979 pending_item_sequence_number = frame_entry->item_sequence_number();
4980 pending_document_sequence_number =
4981 frame_entry->document_sequence_number();
4982 }
4983 } else if (request &&
4984 !NavigationTypeUtils::IsReload(
4985 request->common_params().navigation_type) &&
4986 !NavigationTypeUtils::IsHistory(
4987 request->common_params().navigation_type) &&
4988 !request->common_params().should_replace_current_entry &&
4989 !request->common_params()
4990 .is_history_navigation_in_new_child_frame) {
4991 will_create_new_entry = true;
4992 entry_index = GetLastCommittedEntryIndex() + 1;
4993 // Don't set pending_item_sequence_number or
4994 // pending_document_sequence_number in this case - a new unique isn/dsn will
4995 // be calculated in the renderer later.
4996 } else if (GetLastCommittedEntryIndex() != -1) {
4997 entry_index = GetLastCommittedEntryIndex();
4998 if (auto* frame_entry = GetLastCommittedEntry()->GetFrameEntry(node)) {
Nate Chapind1fe3612021-04-16 20:45:574999 pending_item_sequence_number = frame_entry->item_sequence_number();
Nate Chapin63db0d12022-01-20 22:03:305000 pending_document_sequence_number =
5001 frame_entry->document_sequence_number();
5002 }
Nate Chapind1fe3612021-04-16 20:45:575003 }
5004
Domenic Denicolacd30f5f82022-03-16 21:48:015005 auto entry_arrays = blink::mojom::NavigationApiHistoryEntryArrays::New();
Nate Chapine82339d02022-05-03 23:48:255006 if (entry_index == -1) {
5007 // TODO(rakina): Exit early when there is no last committed entry.
5008 // Remove when InitialNavigationEntry ships.
5009 return entry_arrays;
5010 }
5011
Nate Chapina2c881f52023-11-07 17:02:095012 int backmost_index = entry_index;
Domenic Denicolacd30f5f82022-03-16 21:48:015013 entry_arrays->back_entries = PopulateSingleNavigationApiHistoryEntryVector(
Nate Chapind1fe3612021-04-16 20:45:575014 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
Nate Chapina2c881f52023-11-07 17:02:095015 pending_item_sequence_number, pending_document_sequence_number,
5016 backmost_index);
Nate Chapind1fe3612021-04-16 20:45:575017
5018 // Don't populate forward entries if they will be truncated by a new entry.
Nate Chapina2c881f52023-11-07 17:02:095019 int forwardmost_index = entry_index;
Nate Chapind1fe3612021-04-16 20:45:575020 if (!will_create_new_entry) {
Domenic Denicolacd30f5f82022-03-16 21:48:015021 entry_arrays->forward_entries =
5022 PopulateSingleNavigationApiHistoryEntryVector(
5023 Direction::kForward, entry_index, pending_origin, node,
5024 site_instance.get(), pending_item_sequence_number,
Nate Chapina2c881f52023-11-07 17:02:095025 pending_document_sequence_number, forwardmost_index);
5026 }
5027
5028 // If the previous entry is within the block of contiguous entries being
5029 // provided, then report it as the `previous_entry`.
Nate Chapinc06cf80a2023-11-18 01:12:255030 FrameNavigationEntry* previous_entry = nullptr;
5031 if (frame_tree_->is_prerendering()) {
Avi Drissmanbd153642024-09-03 18:58:055032 FrameTreeNodeId initiator_id = PrerenderHost::GetFromFrameTreeNode(*node)
5033 .initiator_frame_tree_node_id();
5034 if (initiator_id) {
Nate Chapinc06cf80a2023-11-18 01:12:255035 auto* initiator_node = FrameTreeNode::GloballyFindByID(initiator_id);
5036 previous_entry = initiator_node->frame_tree()
5037 .controller()
5038 .GetLastCommittedEntry()
5039 ->GetFrameEntry(initiator_node);
5040 }
5041 } else if (GetLastCommittedEntryIndex() != -1 &&
5042 GetLastCommittedEntryIndex() >= backmost_index &&
5043 GetLastCommittedEntryIndex() <= forwardmost_index) {
5044 previous_entry = GetLastCommittedEntry()->GetFrameEntry(node);
5045 }
5046 if (previous_entry) {
5047 url::Origin previous_entry_origin =
5048 previous_entry->committed_origin().value_or(url::Origin::Resolve(
5049 previous_entry->url(),
5050 previous_entry->initiator_origin().value_or(url::Origin())));
Alison Gale770f3fc2024-04-27 00:39:585051 // TODO(crbug.com/40181982): Move this into ToNavigationApiHistoryEntry()
Nate Chapinc06cf80a2023-11-18 01:12:255052 // once we can be sure that entries with the same ISN will never be
5053 // cross-origin.
5054 if (pending_origin.IsSameOriginWith(previous_entry_origin)) {
5055 entry_arrays->previous_entry = ToNavigationApiHistoryEntry(
5056 previous_entry, pending_document_sequence_number);
Nate Chapina2c881f52023-11-07 17:02:095057 }
Nate Chapind1fe3612021-04-16 20:45:575058 }
Nate Chapinc06cf80a2023-11-18 01:12:255059
Nate Chapin4e657a472022-02-06 03:38:375060 return entry_arrays;
Nate Chapind1fe3612021-04-16 20:45:575061}
5062
Nate Chapinfbfe5af2021-06-10 17:22:085063NavigationControllerImpl::HistoryNavigationAction
Domenic Denicolacc094fb2022-03-16 23:40:575064NavigationControllerImpl::ShouldNavigateToEntryForNavigationApiKey(
Nate Chapinfbfe5af2021-06-10 17:22:085065 FrameNavigationEntry* current_entry,
5066 FrameNavigationEntry* target_entry,
Domenic Denicolacc094fb2022-03-16 23:40:575067 const std::string& navigation_api_key) {
Nate Chapinfbfe5af2021-06-10 17:22:085068 if (!target_entry || !target_entry->committed_origin())
5069 return HistoryNavigationAction::kStopLooking;
Nate Chapinfbfe5af2021-06-10 17:22:085070 if (!current_entry->committed_origin()->IsSameOriginWith(
5071 *target_entry->committed_origin())) {
5072 return HistoryNavigationAction::kStopLooking;
5073 }
5074
5075 // NOTE: We don't actually care between kSameDocument and
5076 // kDifferentDocument, so always use kDifferentDocument by convention.
Domenic Denicolacc094fb2022-03-16 23:40:575077 if (target_entry->navigation_api_key() == navigation_api_key)
Nate Chapinfbfe5af2021-06-10 17:22:085078 return HistoryNavigationAction::kDifferentDocument;
5079 return HistoryNavigationAction::kKeepLooking;
5080}
5081
Domenic Denicolacc094fb2022-03-16 23:40:575082void NavigationControllerImpl::NavigateToNavigationApiKey(
Nate Chapinbf682fa32022-09-26 22:41:205083 RenderFrameHostImpl* initiator_rfh,
Arthur Sonzognic686e8f2024-01-11 08:36:375084 std::optional<blink::scheduler::TaskAttributionId>
Yoav Weissa7449c3b2022-11-22 15:15:145085 soft_navigation_heuristics_task_id,
Charlie Reisf4d51f402025-05-23 18:00:495086 const std::string& key,
5087 base::TimeTicks actual_navigation_start) {
Nate Chapinbf682fa32022-09-26 22:41:205088 FrameTreeNode* node = initiator_rfh->frame_tree_node();
Nate Chapinfbfe5af2021-06-10 17:22:085089 FrameNavigationEntry* current_entry =
5090 GetLastCommittedEntry()->GetFrameEntry(node);
Nate Chapin0dbc93af2025-03-28 17:02:525091 if (!current_entry || !current_entry->committed_origin()) {
Nate Chapinfbfe5af2021-06-10 17:22:085092 return;
Nate Chapin0dbc93af2025-03-28 17:02:525093 }
Nate Chapinfbfe5af2021-06-10 17:22:085094
Alison Gale47d1537d2024-04-19 21:31:465095 // TODO(crbug.com/40878000): Make sure that the right task ID is passed
Yoav Weiss8c573952022-11-17 17:35:135096 // when `navigation.traverseTo()` is called.
5097
Nate Chapinfbfe5af2021-06-10 17:22:085098 // We want to find the nearest matching entry that is contiguously
5099 // same-instance and same-origin. Check back first, then forward.
5100 // TODO(japhet): Link spec here once it exists.
5101 for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) {
Domenic Denicolacc094fb2022-03-16 23:40:575102 auto result = ShouldNavigateToEntryForNavigationApiKey(
Nate Chapinfbfe5af2021-06-10 17:22:085103 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
5104 if (result == HistoryNavigationAction::kStopLooking)
5105 break;
5106 if (result != HistoryNavigationAction::kKeepLooking) {
Charlie Reisf4d51f402025-05-23 18:00:495107 GoToIndex(i, initiator_rfh, soft_navigation_heuristics_task_id, &key,
5108 actual_navigation_start);
Nate Chapinfbfe5af2021-06-10 17:22:085109 return;
5110 }
5111 }
5112 for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) {
Domenic Denicolacc094fb2022-03-16 23:40:575113 auto result = ShouldNavigateToEntryForNavigationApiKey(
Nate Chapinfbfe5af2021-06-10 17:22:085114 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
5115 if (result == HistoryNavigationAction::kStopLooking)
5116 break;
5117 if (result != HistoryNavigationAction::kKeepLooking) {
Charlie Reisf4d51f402025-05-23 18:00:495118 GoToIndex(i, initiator_rfh, soft_navigation_heuristics_task_id, &key,
5119 actual_navigation_start);
Nate Chapinfbfe5af2021-06-10 17:22:085120 return;
5121 }
5122 }
Nate Chapinbf682fa32022-09-26 22:41:205123
5124 // If we fall through to here, a matching NavigationEntry couldn't be found.
5125 // Notify the renderer that the navigation was cancelled.
5126 initiator_rfh->GetAssociatedLocalFrame()->TraverseCancelled(
5127 key, blink::mojom::TraverseCancelledReason::kNotFound);
Nate Chapinfbfe5af2021-06-10 17:22:085128}
5129
Domenic Denicolacc094fb2022-03-16 23:40:575130bool NavigationControllerImpl::ShouldProtectUrlInNavigationApi(
Nate Chapin63db0d12022-01-20 22:03:305131 network::mojom::ReferrerPolicy referrer_policy) {
5132 return referrer_policy == network::mojom::ReferrerPolicy::kNever ||
5133 referrer_policy == network::mojom::ReferrerPolicy::kOrigin;
5134}
5135
shivanigithubf405bf0d2021-11-05 17:58:335136bool NavigationControllerImpl::ShouldMaintainTrivialSessionHistory(
5137 const FrameTreeNode* frame_tree_node) const {
Alison Gale81f4f2c72024-04-22 19:33:315138 // TODO(crbug.com/40176906): We may have to add portals in addition to
shivanigithubf405bf0d2021-11-05 17:58:335139 // prerender and fenced frames. This should be kept in sync with
Hayato Ito7a80db42021-07-05 06:18:545140 // LocalFrame version, LocalFrame::ShouldMaintainTrivialSessionHistory.
Takashi Toyoshima4dad2c12023-11-13 10:04:465141 // The preview mode appears as prerendered page in renderers, and
5142 // GetDocument()->IsPrerendering() covers the case together.
Ali Hijazid87307d2022-11-07 20:15:035143 return frame_tree_->is_prerendering() ||
Takashi Toyoshima8dfc05c2024-01-29 21:03:515144 frame_tree_->page_delegate()->IsPageInPreviewMode() ||
shivanigithubf405bf0d2021-11-05 17:58:335145 frame_tree_node->IsInFencedFrameTree();
Hayato Ito7a80db42021-07-05 06:18:545146}
5147
Julie Jeongeun Kim0e242242022-11-30 10:45:095148void NavigationControllerImpl::DidChangeReferrerPolicy(
5149 FrameTreeNode* node,
5150 network::mojom::ReferrerPolicy referrer_policy) {
5151 FrameNavigationEntry* entry = GetLastCommittedEntry()->GetFrameEntry(node);
5152 if (!entry)
5153 return;
5154
5155 // The FrameNavigationEntry may want to change whether to protect its url
5156 // in the navigation API when the referrer policy changes.
5157 entry->set_protect_url_in_navigation_api(
5158 ShouldProtectUrlInNavigationApi(referrer_policy));
5159}
5160
Minoru Chikamune646eba42025-04-14 01:25:035161void NavigationControllerImpl::CheckPotentialNavigationReentrancy() {
Minoru Chikamune13ab4de2025-05-14 21:54:075162 if (can_be_in_navigate_to_pending_entry_) {
Minoru Chikamune646eba42025-04-14 01:25:035163 // This DumpWithoutCrashing is an investigation code for
5164 // https://crbug.com/396998476.
5165 base::debug::DumpWithoutCrashing();
5166 }
5167}
5168
Andrew Verge754c70a2025-04-17 17:19:195169std::unique_ptr<NavigationRequest>
5170NavigationControllerImpl::CreateNavigationRequestForErrorPage(
5171 RenderFrameHostImpl* render_frame_host_impl,
5172 const GURL& url,
5173 const std::string& error_page_html,
5174 bool is_post_commit_error_page) {
5175 // Only active documents can load error pages:
5176 // - If the document is in pending deletion, the browser already committed to
5177 // destroying this RenderFrameHost so ignore loading the error page.
5178 // - If the document is in back-forward cache, it's not allowed to navigate as
5179 // it should remain frozen. Ignore the request and evict the document from
5180 // back-forward cache.
5181 // - If the document is prerendering, it can navigate but when loading error
5182 // pages, cancel prerendering.
5183 if (render_frame_host_impl->IsInactiveAndDisallowActivation(
5184 is_post_commit_error_page
5185 ? DisallowActivationReasonId::kLoadPostCommitErrorPage
5186 : DisallowActivationReasonId::kBrowserInitiatedErrorPage)) {
5187 return nullptr;
5188 }
5189
5190 FrameTreeNode* node = render_frame_host_impl->frame_tree_node();
5191
5192 blink::mojom::CommonNavigationParamsPtr common_params =
5193 blink::CreateCommonNavigationParams();
5194 // |url| might be empty, such as when this method is called before
5195 // the frame actually committed (e.g. iframe with "src" set to a
5196 // slow-responding URL). We should rewrite the URL to about:blank in this
5197 // case, as the renderer will only think a page is an error page if it has a
5198 // non-empty unreachable URL.
5199 common_params->url = url.is_empty() ? GURL("about:blank") : url;
5200
5201 // Loading an error page via LoadPostCommitErrorPage() performs a temporary
5202 // replacement of the entire NavigationEntry and swaps it back in later.
5203 // All other error page types should permanently replace the current entry.
5204 if (!is_post_commit_error_page) {
5205 common_params->should_replace_current_entry = true;
5206 }
5207
5208 blink::mojom::CommitNavigationParamsPtr commit_params =
5209 blink::CreateCommitNavigationParams();
5210 commit_params->original_url = common_params->url;
5211
5212 commit_params->navigation_timing->system_entropy_at_navigation_start =
5213 SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode(
5214 node, blink::mojom::SystemEntropy::kNormal);
5215
5216 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
5217 // error pages.
5218 commit_params->frame_policy = node->pending_frame_policy();
5219
5220 std::unique_ptr<NavigationRequest> navigation_request =
5221 NavigationRequest::CreateBrowserInitiated(
5222 node, std::move(common_params), std::move(commit_params),
5223 false /* was_opener_suppressed */, "" /* extra_headers */,
5224 nullptr /* frame_entry */, nullptr /* entry */,
5225 false /* is_form_submission */, nullptr /* navigation_ui_data */,
5226 std::nullopt /* impression */, false /* is_pdf */);
5227 if (is_post_commit_error_page) {
5228 navigation_request->set_browser_initiated_error_navigation_type(
5229 NavigationRequest::BrowserInitiatedErrorNavigationType::kPostCommit);
5230 } else {
5231 navigation_request->set_browser_initiated_error_navigation_type(
5232 NavigationRequest::BrowserInitiatedErrorNavigationType::kRegular);
5233 }
5234 navigation_request->set_net_error(net::ERR_BLOCKED_BY_CLIENT);
5235 navigation_request->set_error_page_html(error_page_html);
5236 return navigation_request;
5237}
5238
[email protected]8ff00d72012-10-23 19:12:215239} // namespace content