blob: 0884099c75bc8984898128803e3b9ce646b3ee42 [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"
Rakina Zata Amni627360d2022-02-24 00:53:4043#include "base/debug/dump_without_crashing.h"
Avi Drissmanadac21992023-01-11 23:46:3944#include "base/functional/bind.h"
initial.commit09911bf2008-07-26 23:55:2945#include "base/logging.h"
asvitkine30330812016-08-30 04:01:0846#include "base/metrics/histogram_macros.h"
Charlie Reis23c26da2022-01-29 00:57:4747#include "base/numerics/safe_conversions.h"
Ryan Hamilton7f3bd3d2022-04-23 00:07:3948#include "base/strings/escape.h"
Peter Kastingb53b81912021-04-28 19:23:3049#include "base/strings/string_piece.h"
[email protected]348fbaac2013-06-11 06:31:5150#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0051#include "base/strings/utf_string_conversions.h"
[email protected]a43858f2013-06-28 15:18:3752#include "base/time/time.h"
Carlos Caballero40b0efd2021-01-26 11:55:0053#include "base/trace_event/optional_trace_event.h"
Carlos Caballeroede6f8c2021-01-28 11:01:5054#include "base/trace_event/trace_conversion_helper.h"
ssid3e765612015-01-28 04:03:4255#include "base/trace_event/trace_event.h"
Daniel Chengabb006862022-09-09 22:39:0856#include "base/types/optional_util.h"
servolkf3955532015-05-16 00:01:5957#include "build/build_config.h"
[email protected]eabfe1912014-05-12 10:07:2858#include "cc/base/switches.h"
jamescookda2505812015-03-20 18:01:1859#include "content/browser/bad_message.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3960#include "content/browser/blob_storage/chrome_blob_storage_context.h"
William Liu055a3542023-04-02 17:21:1961#include "content/browser/browser_context_impl.h"
[email protected]825b1662012-03-12 19:07:3162#include "content/browser/browser_url_handler_impl.h"
[email protected]5f2aa722013-08-07 16:59:4163#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0464#include "content/browser/dom_storage/session_storage_namespace_impl.h"
Nate Chapinc06cf80a2023-11-18 01:12:2565#include "content/browser/preloading/prerender/prerender_host.h"
Sharon Yang242ef822023-05-15 21:07:3266#include "content/browser/process_lock.h"
Mingyu Lei68f34412023-08-21 07:31:3767#include "content/browser/renderer_host/back_forward_cache_impl.h"
danakjc492bf82020-09-09 20:02:4468#include "content/browser/renderer_host/debug_urls.h"
69#include "content/browser/renderer_host/frame_tree.h"
Carlos Caballero40b0efd2021-01-26 11:55:0070#include "content/browser/renderer_host/frame_tree_node.h"
Chris Bookholt27faf8d2022-01-20 01:03:3371#include "content/browser/renderer_host/navigation_controller_delegate.h"
danakjc492bf82020-09-09 20:02:4472#include "content/browser/renderer_host/navigation_entry_impl.h"
Nate Chapin214a86a2021-06-21 20:35:5773#include "content/browser/renderer_host/navigation_entry_restore_context_impl.h"
danakjc492bf82020-09-09 20:02:4474#include "content/browser/renderer_host/navigation_request.h"
William Liu055a3542023-04-02 17:21:1975#include "content/browser/renderer_host/navigation_transitions/navigation_entry_screenshot_cache.h"
76#include "content/browser/renderer_host/navigation_transitions/navigation_entry_screenshot_manager.h"
danakjc492bf82020-09-09 20:02:4477#include "content/browser/renderer_host/navigator.h"
Takashi Toyoshima4dad2c12023-11-13 10:04:4678#include "content/browser/renderer_host/page_delegate.h"
danakjc492bf82020-09-09 20:02:4479#include "content/browser/renderer_host/render_frame_host_delegate.h"
Carlos Caballero40b0efd2021-01-26 11:55:0080#include "content/browser/renderer_host/render_view_host_impl.h"
Mike Jacksone2aa7af2023-05-17 06:45:0781#include "content/browser/renderer_host/system_entropy_utils.h"
Sharon Yangd70a5392021-10-26 23:06:3282#include "content/browser/site_info.h"
[email protected]b6583592012-01-25 19:52:3383#include "content/browser/site_instance_impl.h"
Camille Lamy5193caa2018-10-12 11:59:4284#include "content/common/content_constants_internal.h"
Nate Chapind1fe3612021-04-16 20:45:5785#include "content/common/navigation_params_utils.h"
Nasko Oskovae49e292020-08-13 02:08:5186#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1187#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1988#include "content/public/browser/content_browser_client.h"
Fergal Daly1336ac642021-09-14 15:13:1189#include "content/public/browser/disallow_activation_reason.h"
[email protected]d9083482012-01-06 00:38:4690#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0091#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3892#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1693#include "content/public/browser/notification_types.h"
Lei Zhang96031532019-10-10 19:05:4794#include "content/public/browser/render_view_host.h"
[email protected]9677a3c2012-12-22 04:18:5895#include "content/public/browser/render_widget_host.h"
96#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1097#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3498#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:1999#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:38100#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:10101#include "content/public/common/content_features.h"
Lei Zhang96031532019-10-10 19:05:47102#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:43103#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:59104#include "media/base/mime_util.h"
Lei Zhanga4770832023-07-19 18:02:36105#include "net/base/net_errors.h"
Arthur Sonzogni620cec62018-12-13 13:08:57106#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:18107#include "services/metrics/public/cpp/ukm_builders.h"
108#include "services/metrics/public/cpp/ukm_recorder.h"
Yue Ru Sun128804932020-09-30 22:19:17109#include "services/metrics/public/cpp/ukm_source_id.h"
Nan Lind91c8152021-10-21 16:22:37110#include "services/network/public/mojom/fetch_api.mojom.h"
William Liu2c825472022-10-31 12:01:44111#include "services/network/public/mojom/url_response_head.mojom-shared.h"
[email protected]9677a3c2012-12-22 04:18:58112#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:39113#include "third_party/blink/public/common/blob/blob_utils.h"
Gyuyoung Kim107c2a02021-04-13 01:49:30114#include "third_party/blink/public/common/chrome_debug_urls.h"
Miyoung Shin1c565c912021-03-17 12:11:21115#include "third_party/blink/public/common/history/session_history_constants.h"
Blink Reformata30d4232018-04-07 15:31:06116#include "third_party/blink/public/common/mime_util/mime_util.h"
Minggang Wanga13c796e2021-07-02 05:54:43117#include "third_party/blink/public/common/navigation/navigation_params.h"
Carlos Caballeroede6f8c2021-01-28 11:01:50118#include "third_party/blink/public/common/page_state/page_state_serialization.h"
Minggang Wangb9f3fa92021-07-01 15:30:31119#include "third_party/blink/public/mojom/navigation/navigation_params.mojom.h"
Minggang Wang7ee0c742021-06-16 16:16:51120#include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h"
Jiewei Qian87951f52023-10-17 01:33:46121#include "third_party/blink/public/mojom/runtime_feature_state/runtime_feature.mojom.h"
[email protected]cca6f392014-05-28 21:32:26122#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:29123
[email protected]8ff00d72012-10-23 19:12:21124namespace content {
[email protected]e9ba4472008-09-14 15:42:43125namespace {
126
[email protected]e9ba4472008-09-14 15:42:43127// Invoked when entries have been pruned, or removed. For example, if the
128// current entries are [google, digg, yahoo], with the current entry google,
129// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47130void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50131 int index,
[email protected]c12bf1a12008-09-17 16:28:49132 int count) {
[email protected]8ff00d72012-10-23 19:12:21133 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50134 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49135 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14136 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43137}
138
[email protected]e9ba4472008-09-14 15:42:43139// Configure all the NavigationEntries in entries for restore. This resets
140// the transition type to reload and makes sure the content state isn't empty.
141void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57142 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48143 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47144 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43145 // Use a transition type of reload so that we don't incorrectly increase
146 // the typed count.
Lei Zhang96031532019-10-10 19:05:47147 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
148 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43149 }
150}
151
[email protected]bf70edce2012-06-20 22:32:22152// Determines whether or not we should be carrying over a user agent override
153// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57154bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22155 return last_entry && last_entry->GetIsOverridingUserAgent();
156}
157
Camille Lamy5193caa2018-10-12 11:59:42158// Determines whether to override user agent for a navigation.
159bool ShouldOverrideUserAgent(
160 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57161 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42162 switch (override_user_agent) {
163 case NavigationController::UA_OVERRIDE_INHERIT:
164 return ShouldKeepOverride(last_committed_entry);
165 case NavigationController::UA_OVERRIDE_TRUE:
166 return true;
167 case NavigationController::UA_OVERRIDE_FALSE:
168 return false;
Camille Lamy5193caa2018-10-12 11:59:42169 }
170 NOTREACHED();
171 return false;
172}
173
Rakina Zata Amni312822d72021-06-04 16:13:37174// Returns true if this navigation should be treated as a reload. For e.g.
clamy0a656e42018-02-06 18:18:28175// navigating to the last committed url via the address bar or clicking on a
Rakina Zata Amni312822d72021-06-04 16:13:37176// link which results in a navigation to the last committed URL (but wasn't
177// converted to do a replacement navigation in the renderer), etc.
Fergal Daly766177d2020-07-07 07:54:04178// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
179// |base_url_for_data_url|, |transition_type| correspond to the new navigation
180// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
181// navigation that committed.
182bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
183 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57184 const GURL& virtual_url,
185 const GURL& base_url_for_data_url,
186 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57187 bool is_post,
Rakina Zata Amni312822d72021-06-04 16:13:37188 bool should_replace_current_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57189 NavigationEntryImpl* last_committed_entry) {
Rakina Zata Amni312822d72021-06-04 16:13:37190 // Navigations intended to do a replacement shouldn't be converted to do a
191 // reload.
192 if (should_replace_current_entry)
clamy0a656e42018-02-06 18:18:28193 return false;
clamy0a656e42018-02-06 18:18:28194 // Only convert to reload if at least one navigation committed.
Rakina Zata Amnie2d31312022-11-18 03:38:45195 if (last_committed_entry->IsInitialEntry())
ananta3bdd8ae2016-12-22 17:11:55196 return false;
197
arthursonzogni7a8243682017-12-14 16:41:42198 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28199 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42200 return false;
201
ananta3bdd8ae2016-12-22 17:11:55202 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
203 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
204 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28205 bool transition_type_can_be_converted = false;
206 if (ui::PageTransitionCoreTypeIs(transition_type,
207 ui::PAGE_TRANSITION_RELOAD) &&
208 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
209 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55210 }
clamy0a656e42018-02-06 18:18:28211 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55212 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28213 transition_type_can_be_converted = true;
214 }
215 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
216 transition_type_can_be_converted = true;
217 if (!transition_type_can_be_converted)
218 return false;
219
220 // This check is required for cases like view-source:, etc. Here the URL of
221 // the navigation entry would contain the url of the page, while the virtual
222 // URL contains the full URL including the view-source prefix.
223 if (virtual_url != last_committed_entry->GetVirtualURL())
224 return false;
225
Fergal Daly766177d2020-07-07 07:54:04226 // Check that the URLs match.
227 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
228 // If there's no frame entry then by definition the URLs don't match.
229 if (!frame_entry)
230 return false;
231
232 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28233 return false;
234
235 // This check is required for Android WebView loadDataWithBaseURL. Apps
236 // can pass in anything in the base URL and we need to ensure that these
237 // match before classifying it as a reload.
238 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
239 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
240 return false;
ananta3bdd8ae2016-12-22 17:11:55241 }
242
clamy0a656e42018-02-06 18:18:28243 // Skip entries with SSL errors.
244 if (last_committed_entry->ssl_error())
245 return false;
246
247 // Don't convert to a reload when the last navigation was a POST or the new
248 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04249 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28250 return false;
251
252 return true;
ananta3bdd8ae2016-12-22 17:11:55253}
254
Arthur Sonzognic686e8f2024-01-11 08:36:37255std::optional<url::Origin> GetCommittedOriginForFrameEntry(
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12256 const mojom::DidCommitProvisionalLoadParams& params,
257 NavigationRequest* request) {
Nasko Oskov03912102019-01-11 00:21:32258 // Error pages commit in an opaque origin, yet have the real URL that resulted
259 // in an error as the |params.url|. Since successful reload of an error page
260 // should commit in the correct origin, setting the opaque origin on the
261 // FrameNavigationEntry will be incorrect.
Rakina Zata Amniafd3c6582021-11-30 06:19:17262 if (request && request->DidEncounterError())
Arthur Sonzognic686e8f2024-01-11 08:36:37263 return std::nullopt;
Nasko Oskov03912102019-01-11 00:21:32264
Arthur Sonzognic686e8f2024-01-11 08:36:37265 return std::make_optional(params.origin);
Nasko Oskov03912102019-01-11 00:21:32266}
267
Alex Moshchuk99242832023-05-22 17:21:44268bool IsValidURLForNavigation(FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:42269 const GURL& virtual_url,
270 const GURL& dest_url) {
271 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
Alex Moshchuk99242832023-05-22 17:21:44272 if (node->IsOutermostMainFrame() && !virtual_url.is_valid() &&
273 !virtual_url.is_empty()) {
Camille Lamy5193caa2018-10-12 11:59:42274 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
275 << virtual_url.possibly_invalid_spec();
276 return false;
277 }
278
279 // Don't attempt to navigate to non-empty invalid URLs.
280 if (!dest_url.is_valid() && !dest_url.is_empty()) {
281 LOG(WARNING) << "Refusing to load invalid URL: "
282 << dest_url.possibly_invalid_spec();
283 return false;
284 }
285
286 // The renderer will reject IPC messages with URLs longer than
287 // this limit, so don't attempt to navigate with a longer URL.
288 if (dest_url.spec().size() > url::kMaxURLChars) {
289 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
290 << " characters.";
291 return false;
292 }
293
Aaron Colwell33109c592020-04-21 21:31:19294 // Reject renderer debug URLs because they should have been handled before
295 // we get to this point. This check handles renderer debug URLs
296 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
297 // provides defense-in-depth if a renderer debug URL manages to get here via
298 // some other path. We want to reject the navigation here so it doesn't
299 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30300 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19301 LOG(WARNING) << "Refusing to load renderer debug URL: "
302 << dest_url.possibly_invalid_spec();
303 return false;
304 }
305
Alex Moshchuk99242832023-05-22 17:21:44306 // Guests only support navigations to known-safe schemes. This check already
307 // exists in the extensions layer, where it also dispatches proper events to
308 // the guest's embedder (see WebViewGuest::LoadURLWithParams). This check is
309 // for defense-in-depth to ensure that no other places in the codebase
310 // accidentally navigate guests to schemes such as WebUI, which is not
311 // supported. See https://crbug.com/1444221.
312 if (node->current_frame_host()->GetSiteInstance()->IsGuest()) {
313 auto* cpsp = content::ChildProcessSecurityPolicy::GetInstance();
314 if (!cpsp->IsWebSafeScheme(dest_url.scheme()) &&
315 !dest_url.SchemeIs(url::kAboutScheme)) {
316 LOG(WARNING) << "Refusing to load unsafe URL in a guest: "
317 << dest_url.possibly_invalid_spec();
318 return false;
319 }
320 }
321
Camille Lamy5193caa2018-10-12 11:59:42322 return true;
323}
324
Mikel Astizba9cf2fd2017-12-17 10:38:10325// See replaced_navigation_entry_data.h for details: this information is meant
326// to ensure |*output_entry| keeps track of its original URL (landing page in
327// case of server redirects) as it gets replaced (e.g. history.replaceState()),
328// without overwriting it later, for main frames.
329void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57330 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10331 NavigationEntryImpl* output_entry) {
Rakina Zata Amniafd3c6582021-11-30 06:19:17332 if (output_entry->GetReplacedEntryData().has_value() ||
333 replaced_entry->IsInitialEntry()) {
Mikel Astizba9cf2fd2017-12-17 10:38:10334 return;
Rakina Zata Amniafd3c6582021-11-30 06:19:17335 }
Mikel Astizba9cf2fd2017-12-17 10:38:10336
337 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57338 data.first_committed_url = replaced_entry->GetURL();
339 data.first_timestamp = replaced_entry->GetTimestamp();
340 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29341 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10342}
343
Minggang Wangb9f3fa92021-07-01 15:30:31344blink::mojom::NavigationType GetNavigationType(
345 const GURL& old_url,
346 const GURL& new_url,
347 ReloadType reload_type,
348 NavigationEntryImpl* entry,
349 const FrameNavigationEntry& frame_entry,
350 bool has_pending_cross_document_commit,
351 bool is_currently_error_page,
Garrett Tanzer405f3402022-07-21 20:12:49352 bool is_same_document_history_load,
Garrett Tanzer267c2b82022-07-26 16:53:13353 bool is_embedder_initiated_fenced_frame_navigation,
Garrett Tanzer405f3402022-07-21 20:12:49354 bool is_unfenced_top_navigation) {
clamyea99ea12018-05-28 13:54:23355 // Reload navigations
356 switch (reload_type) {
357 case ReloadType::NORMAL:
Minggang Wangb9f3fa92021-07-01 15:30:31358 return blink::mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23359 case ReloadType::BYPASSING_CACHE:
Minggang Wangb9f3fa92021-07-01 15:30:31360 return blink::mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23361 case ReloadType::NONE:
362 break; // Fall through to rest of function.
363 }
364
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08365 if (entry->IsRestored()) {
Minggang Wangb9f3fa92021-07-01 15:30:31366 return entry->GetHasPostData()
367 ? blink::mojom::NavigationType::RESTORE_WITH_POST
368 : blink::mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23369 }
370
danakjb952ef12021-01-14 19:58:49371 const bool can_be_same_document =
372 // A pending cross-document commit means this navigation will not occur in
373 // the current document, as that document would end up being replaced in
374 // the meantime.
375 !has_pending_cross_document_commit &&
376 // If the current document is an error page, we should always treat it as
377 // a different-document navigation so that we'll attempt to load the
378 // document we're navigating to (and not stay in the current error page).
Garrett Tanzer405f3402022-07-21 20:12:49379 !is_currently_error_page &&
Garrett Tanzer267c2b82022-07-26 16:53:13380 // If the navigation is an embedder-initiated navigation of a fenced
381 // frame root (i.e. enters a fenced frame tree from outside),
382 // same-document navigations should be disabled because we don't want to
383 // allow information to be joined across multiple embedder-initiated
384 // fenced frame navigations (which may contain separate cross-site data).
385 !is_embedder_initiated_fenced_frame_navigation &&
Garrett Tanzer405f3402022-07-21 20:12:49386 // If the navigation is to _unfencedTop (i.e. escapes a fenced frame),
387 // same-document navigations should be disabled because we want to force
388 // the creation of a new browsing context group.
389 !is_unfenced_top_navigation;
danakjd83d706d2020-11-25 22:11:12390
clamyea99ea12018-05-28 13:54:23391 // History navigations.
392 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12393 return can_be_same_document && is_same_document_history_load
Minggang Wangb9f3fa92021-07-01 15:30:31394 ? blink::mojom::NavigationType::HISTORY_SAME_DOCUMENT
395 : blink::mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23396 }
397 DCHECK(!is_same_document_history_load);
398
399 // A same-document fragment-navigation happens when the only part of the url
400 // that is modified is after the '#' character.
401 //
402 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12403 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23404 //
405 // Note: this check is only valid for navigations that are not history
406 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
407 // history navigation from 'A#foo' to 'A#bar' is not a same-document
408 // navigation, but a different-document one. This is why history navigation
409 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47410 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
411 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12412
413 // The one case where we do the wrong thing here and incorrectly choose
414 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
415 // the renderer is a frameset. All frameset navigations should be
416 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
417 // navigation would do the right thing, as it would send it to the browser and
418 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
419 // into this method). But since we can't tell that case here for browser-
420 // initiated navigations, we have to get the renderer involved. In that case
421 // the navigation would be restarted due to the renderer spending a reply of
422 // mojom::CommitResult::RestartCrossDocument.
423
424 return can_be_same_document && is_same_doc
Minggang Wangb9f3fa92021-07-01 15:30:31425 ? blink::mojom::NavigationType::SAME_DOCUMENT
426 : blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23427}
428
Camille Lamy5193caa2018-10-12 11:59:42429// Adjusts the original input URL if needed, to get the URL to actually load and
430// the virtual URL, which may differ.
431void RewriteUrlForNavigation(const GURL& original_url,
432 BrowserContext* browser_context,
433 GURL* url_to_load,
434 GURL* virtual_url,
435 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42436 // Allow the browser URL handler to rewrite the URL. This will, for example,
437 // remove "view-source:" from the beginning of the URL to get the URL that
438 // will actually be loaded. This real URL won't be shown to the user, just
439 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31440 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42441 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
442 url_to_load, browser_context, reverse_on_redirect);
443}
444
445#if DCHECK_IS_ON()
446// Helper sanity check function used in debug mode.
447void ValidateRequestMatchesEntry(NavigationRequest* request,
448 NavigationEntryImpl* entry) {
449 if (request->frame_tree_node()->IsMainFrame()) {
450 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
451 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
Minggang Wangb9f3fa92021-07-01 15:30:31452 ui::PageTransitionFromInt(request->common_params().transition),
453 entry->GetTransitionType()));
Camille Lamy5193caa2018-10-12 11:59:42454 }
Nasko Oskovc36327d2019-01-03 23:23:04455 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42456 entry->should_clear_history_list());
457 DCHECK_EQ(request->common_params().has_user_gesture,
458 entry->has_user_gesture());
459 DCHECK_EQ(request->common_params().base_url_for_data_url,
460 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04461 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42462 entry->GetCanLoadLocalResources());
463 DCHECK_EQ(request->common_params().started_from_context_menu,
464 entry->has_started_from_context_menu());
465
466 FrameNavigationEntry* frame_entry =
467 entry->GetFrameEntry(request->frame_tree_node());
468 if (!frame_entry) {
469 NOTREACHED();
470 return;
471 }
472
Camille Lamy5193caa2018-10-12 11:59:42473 DCHECK_EQ(request->common_params().method, frame_entry->method());
474
Nasko Oskovc36327d2019-01-03 23:23:04475 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42476 if (redirect_size == frame_entry->redirect_chain().size()) {
477 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04478 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42479 frame_entry->redirect_chain()[i]);
480 }
481 } else {
482 NOTREACHED();
483 }
484}
485#endif // DCHECK_IS_ON()
486
Dave Tapuska8bfd84c2019-03-26 20:47:16487// Returns whether the session history NavigationRequests in |navigations|
Nate Chapinbf682fa32022-09-26 22:41:20488// would stay within the subtree of |sandboxed_initiator_rfh|.
Dave Tapuska8bfd84c2019-03-26 20:47:16489bool DoesSandboxNavigationStayWithinSubtree(
Nate Chapinbf682fa32022-09-26 22:41:20490 RenderFrameHostImpl* sandboxed_initiator_rfh,
Dave Tapuska8bfd84c2019-03-26 20:47:16491 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
Nate Chapinbf682fa32022-09-26 22:41:20492 DCHECK(sandboxed_initiator_rfh);
493 DCHECK(sandboxed_initiator_rfh->IsSandboxed(
494 network::mojom::WebSandboxFlags::kTopNavigation));
Dave Tapuska8bfd84c2019-03-26 20:47:16495 for (auto& item : navigations) {
496 bool within_subtree = false;
497 // Check whether this NavigationRequest affects a frame within the
498 // sandboxed frame's subtree by walking up the tree looking for the
499 // sandboxed frame.
500 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03501 frame = FrameTreeNode::From(frame->parent())) {
Nate Chapinbf682fa32022-09-26 22:41:20502 if (frame == sandboxed_initiator_rfh->frame_tree_node()) {
Dave Tapuska8bfd84c2019-03-26 20:47:16503 within_subtree = true;
504 break;
505 }
506 }
507 if (!within_subtree)
508 return false;
509 }
510 return true;
511}
512
William Liu122754942024-01-18 22:34:39513// Used for "Navigation.SessionHistoryCount" histogram.
514enum class HistoryNavTypeForHistogram {
515 // A same-doc or cross-doc navigation of the main frame. We can only have one
516 // main frame request per `NavigationController::GoToIndex()`.
517 kMainFrame = 0,
518 // `NavigationController::GoToIndex()` creates one main frame request and
519 // one or more subframe requests.
520 kMainFrameAndSubframe,
521 // `NavigationController::GoToIndex()` creates one or more subframe requests.
522 kSubframe,
523 // Used for histogram boundary.
524 kCount
525};
526
527void CountRequests(
528 const std::vector<std::unique_ptr<NavigationRequest>>& requests,
529 int& mutable_main_frame_cnt,
530 int& mutable_subframe_cnt) {
531 for (const auto& req : requests) {
532 if (req->IsInPrimaryMainFrame()) {
533 // We can only have one main frame navigation at a time.
534 CHECK_EQ(mutable_main_frame_cnt, 0);
535 ++mutable_main_frame_cnt;
536 } else if (req->GetNavigatingFrameType() == FrameType::kSubframe) {
537 ++mutable_subframe_cnt;
538 }
539 }
540}
541
542// Record the number of different types of navigations as histograms. See
543// `HistoryNavTypeForHistogram` for the types.
544void CountBrowserInitiatedMainframeAndSubframeHistoryNavigaions(
545 const std::vector<std::unique_ptr<NavigationRequest>>& cross_doc_requests,
546 const std::vector<std::unique_ptr<NavigationRequest>>& same_doc_requests) {
547 // We must have one request.
548 CHECK(!cross_doc_requests.empty() || !same_doc_requests.empty());
549
550 int main_frame_cnt = 0;
551 int subframe_cnt = 0;
552 CountRequests(cross_doc_requests, main_frame_cnt, subframe_cnt);
553 CountRequests(same_doc_requests, main_frame_cnt, subframe_cnt);
554
555 std::optional<HistoryNavTypeForHistogram> history_nav_type;
556 if (main_frame_cnt > 0) {
557 if (subframe_cnt == 0) {
558 history_nav_type = HistoryNavTypeForHistogram::kMainFrame;
559 } else {
560 history_nav_type = HistoryNavTypeForHistogram::kMainFrameAndSubframe;
561 }
562 } else if (subframe_cnt > 0) {
563 history_nav_type = HistoryNavTypeForHistogram::kSubframe;
564 }
565 if (history_nav_type.has_value()) {
566 UMA_HISTOGRAM_ENUMERATION("Navigation.BrowserInitiatedSessionHistoryCount",
567 history_nav_type.value(),
568 HistoryNavTypeForHistogram::kCount);
569 }
570}
571
[email protected]e9ba4472008-09-14 15:42:43572} // namespace
573
arthursonzogni66f711c2019-10-08 14:40:36574// NavigationControllerImpl::PendingEntryRef------------------------------------
575
576NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
577 base::WeakPtr<NavigationControllerImpl> controller)
578 : controller_(controller) {}
579
580NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
581 if (!controller_) // Can be null with interstitials.
582 return;
583
584 controller_->PendingEntryRefDeleted(this);
585}
586
[email protected]d202a7c2012-01-04 07:53:47587// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29588
[email protected]23a918b2014-07-15 09:51:36589const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23590
[email protected]765b35502008-08-21 00:51:20591// static
[email protected]d202a7c2012-01-04 07:53:47592size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23593 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20594
[email protected]e6fec472013-05-14 05:29:02595// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20596// when testing.
597static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29598
[email protected]71fde352011-12-29 03:29:56599// static
dcheng9bfa5162016-04-09 01:00:57600std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
601 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10602 Referrer referrer,
Arthur Sonzognic686e8f2024-01-11 08:36:37603 std::optional<url::Origin> initiator_origin,
604 std::optional<GURL> initiator_base_url,
Lukasz Anforowicz641234d52019-11-07 21:07:10605 ui::PageTransition transition,
606 bool is_renderer_initiated,
607 const std::string& extra_headers,
608 BrowserContext* browser_context,
609 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
610 return NavigationControllerImpl::CreateNavigationEntry(
W. James MacLean23e90a12022-12-21 04:38:21611 url, referrer, std::move(initiator_origin), std::move(initiator_base_url),
Arthur Sonzognic686e8f2024-01-11 08:36:37612 std::nullopt /* source_process_site_url */, transition,
Sharon Yang242ef822023-05-15 21:07:32613 is_renderer_initiated, extra_headers, browser_context,
614 std::move(blob_url_loader_factory), true /* rewrite_virtual_urls */);
Lukasz Anforowicz641234d52019-11-07 21:07:10615}
616
617// static
618std::unique_ptr<NavigationEntryImpl>
619NavigationControllerImpl::CreateNavigationEntry(
620 const GURL& url,
621 Referrer referrer,
Arthur Sonzognic686e8f2024-01-11 08:36:37622 std::optional<url::Origin> initiator_origin,
623 std::optional<GURL> initiator_base_url,
624 std::optional<GURL> source_process_site_url,
dcheng9bfa5162016-04-09 01:00:57625 ui::PageTransition transition,
626 bool is_renderer_initiated,
627 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09628 BrowserContext* browser_context,
Julie Jeongeun Kim5b9aff72022-05-02 02:10:17629 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
630 bool rewrite_virtual_urls) {
631 GURL url_to_load = url;
632 GURL virtual_url = url;
[email protected]71fde352011-12-29 03:29:56633 bool reverse_on_redirect = false;
Julie Jeongeun Kim5b9aff72022-05-02 02:10:17634 if (rewrite_virtual_urls) {
635 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
636 &reverse_on_redirect);
637 }
Lukasz Anforowicz641234d52019-11-07 21:07:10638 // Let the NTP override the navigation params and pretend that this is a
639 // browser-initiated, bookmark-like navigation.
640 GetContentClient()->browser()->OverrideNavigationParams(
Sharon Yang242ef822023-05-15 21:07:32641 source_process_site_url, &transition, &is_renderer_initiated, &referrer,
Scott Violetcf6ea7e2021-06-09 21:09:21642 &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10643
Patrick Monettef507e982019-06-19 20:18:06644 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28645 nullptr, // The site instance for tabs is sent on navigation
646 // (WebContents::GetSiteInstance).
W. James MacLean23e90a12022-12-21 04:38:21647 url_to_load, referrer, initiator_origin, initiator_base_url,
648 std::u16string(), transition, is_renderer_initiated,
649 blob_url_loader_factory,
Rakina Zata Amniafd3c6582021-11-30 06:19:17650 /* is_initial_entry = */ false);
Camille Lamy5193caa2018-10-12 11:59:42651 entry->SetVirtualURL(virtual_url);
652 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56653 entry->set_update_virtual_url_with_url(reverse_on_redirect);
654 entry->set_extra_headers(extra_headers);
Patrick Monettef507e982019-06-19 20:18:06655 return entry;
[email protected]71fde352011-12-29 03:29:56656}
657
[email protected]cdcb1dee2012-01-04 00:46:20658// static
659void NavigationController::DisablePromptOnRepost() {
660 g_check_for_repost = false;
661}
662
[email protected]c5b88d82012-10-06 17:03:33663base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
664 base::Time t) {
665 // If |t| is between the water marks, we're in a run of duplicates
666 // or just getting out of it, so increase the high-water mark to get
667 // a time that probably hasn't been used before and return it.
668 if (low_water_mark_ <= t && t <= high_water_mark_) {
Peter Kastinge5a38ed2021-10-02 03:06:35669 high_water_mark_ += base::Microseconds(1);
[email protected]c5b88d82012-10-06 17:03:33670 return high_water_mark_;
671 }
672
673 // Otherwise, we're clear of the last duplicate run, so reset the
674 // water marks.
675 low_water_mark_ = high_water_mark_ = t;
676 return t;
677}
678
ckitagawa0faa5e42020-06-17 17:30:54679NavigationControllerImpl::ScopedShowRepostDialogForTesting::
680 ScopedShowRepostDialogForTesting()
681 : was_disallowed_(g_check_for_repost) {
682 g_check_for_repost = true;
683}
684
685NavigationControllerImpl::ScopedShowRepostDialogForTesting::
686 ~ScopedShowRepostDialogForTesting() {
687 g_check_for_repost = was_disallowed_;
688}
689
Nate Chapin9eb16be72022-09-23 22:54:31690NavigationControllerImpl::RemovedEntriesTracker::RemovedEntriesTracker(
691 base::SafeRef<NavigationControllerImpl> controller)
692 : controller_(controller) {
693 for (FrameTreeNode* frame_tree_node : controller_->frame_tree().Nodes()) {
694 names_to_nodes_.insert({frame_tree_node->unique_name(), frame_tree_node});
695 frame_tree_node_id_to_keys_.insert(
696 {frame_tree_node->frame_tree_node_id(), std::set<std::string>()});
697 if (auto* frame_entry = frame_tree_node->current_frame_host()
698 ->last_committed_frame_entry()) {
699 frame_tree_node_id_to_doc_seq_nos_.insert(
700 {frame_tree_node->frame_tree_node_id(),
701 frame_entry->document_sequence_number()});
702 }
703 }
704 PopulateKeySet(Direction::kBack);
705 PopulateKeySet(Direction::kForward);
706}
707
708void NavigationControllerImpl::RemovedEntriesTracker::PopulateKeySet(
709 Direction direction) {
710 // Keep track of which FrameTreeNodes may still have relevant API keys in
711 // additional FrameNavigationEntries.
712 std::set<FrameTreeNode*> nodes_to_process;
713 for (FrameTreeNode* node : controller_->frame_tree().Nodes()) {
714 nodes_to_process.insert(node);
715 }
716
717 const int offset = direction == Direction::kForward ? 1 : -1;
718 const int start = direction == Direction::kForward
719 ? controller_->GetLastCommittedEntryIndex()
720 : controller_->GetLastCommittedEntryIndex() - 1;
721 for (int i = start;
722 i >= 0 && i < controller_->GetEntryCount() && !nodes_to_process.empty();
723 i += offset) {
724 std::set<FrameTreeNode*> nodes_to_continue_processing;
725
726 NavigationEntryImpl* entry = controller_->GetEntryAtIndex(i);
727 entry->ForEachFrameEntry([this, &nodes_to_process,
728 &nodes_to_continue_processing,
729 &entry](FrameNavigationEntry* frame_entry) {
730 // Find the |node| that matches |frame_entry|, if any.
731 FrameTreeNode* node = nullptr;
732 if (frame_entry == entry->root_node()->frame_entry) {
733 node = controller_->frame_tree().root();
734 } else {
735 auto it = names_to_nodes_.find(frame_entry->frame_unique_name());
736 if (it == names_to_nodes_.end())
737 return;
738 node = it->second;
739 }
740
741 // Skip this node if a previous step determine there are no longer
742 // relevant navigation API keys in this direction.
743 if (nodes_to_process.find(node) == nodes_to_process.end())
744 return;
745
746 // Stop processing |node| if we reach a point where it's cross-origin.
747 // See also PopulateSingleNavigationApiHistoryEntryVector().
748 url::Origin frame_entry_origin =
749 frame_entry->committed_origin().value_or(url::Origin::Resolve(
750 frame_entry->url(),
751 frame_entry->initiator_origin().value_or(url::Origin())));
752 if (!node->current_origin().IsSameOriginWith(frame_entry_origin))
753 return;
754
755 frame_tree_node_id_to_keys_[node->frame_tree_node_id()].insert(
756 frame_entry->navigation_api_key());
757 // Mark |node| as needing more processing for the next entry.
758 nodes_to_continue_processing.insert(node);
759
760 // Check whether |node| went cross-document. If so, its children are
761 // no longer the same conceptual iframe as the current one, and
762 // should no longer be processed. This check is intentionally done
763 // after processing the current |node|, which may still have relevant
764 // discarded API keys.
765 if (frame_entry->document_sequence_number() !=
766 frame_tree_node_id_to_doc_seq_nos_[node->frame_tree_node_id()]) {
Arthur Sonzognif6785ec2022-12-05 10:11:50767 for (auto* descendant : node->frame_tree().SubtreeNodes(node))
Nate Chapin9eb16be72022-09-23 22:54:31768 nodes_to_process.erase(descendant);
769 }
770 });
771
772 nodes_to_process.swap(nodes_to_continue_processing);
773 }
774}
775
776NavigationControllerImpl::RemovedEntriesTracker::~RemovedEntriesTracker() {
777 std::set<std::string> all_keys;
778 // Find all remaining navigation API keys after some entries have been
779 // removed.
780 for (auto& entry : controller_->entries_) {
781 entry->ForEachFrameEntry([&all_keys](FrameNavigationEntry* frame_entry) {
782 all_keys.insert(frame_entry->navigation_api_key());
783 });
784 }
785
786 // Notify each frame in the renderer of any disposed navigation API keys.
787 for (auto& id_to_keys : frame_tree_node_id_to_keys_) {
788 std::vector<std::string> disposed_keys;
789 for (const auto& key : id_to_keys.second) {
790 if (all_keys.find(key) == all_keys.end())
791 disposed_keys.push_back(key);
792 }
793 if (disposed_keys.empty())
794 continue;
795
796 FrameTreeNode* node = controller_->frame_tree().FindByID(id_to_keys.first);
797 auto& frame = node->current_frame_host()->GetAssociatedLocalFrame();
798 frame->NotifyNavigationApiOfDisposedEntries(disposed_keys);
799 }
800}
801
[email protected]d202a7c2012-01-04 07:53:47802NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00803 BrowserContext* browser_context,
804 FrameTree& frame_tree,
805 NavigationControllerDelegate* delegate)
806 : frame_tree_(frame_tree),
807 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57808 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22809 ssl_manager_(this),
Mingyu Lei68f34412023-08-21 07:31:37810 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)),
811 back_forward_cache_(browser_context) {
[email protected]3d7474ff2011-07-27 17:47:37812 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29813}
814
[email protected]d202a7c2012-01-04 07:53:47815NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00816 // The NavigationControllerImpl might be called inside its delegate
817 // destructor. Calling it is not valid anymore.
818 delegate_ = nullptr;
819 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29820}
821
Matt Falkenhagen548ed1562021-07-06 01:38:26822WebContents* NavigationControllerImpl::DeprecatedGetWebContents() {
823 return delegate_->DeprecatedGetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32824}
825
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57826BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55827 return browser_context_;
828}
829
[email protected]d202a7c2012-01-04 07:53:47830void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30831 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36832 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57833 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
Charlie Reis23c26da2022-01-29 00:57:47834 // Note that it's possible for `entries_` to contain multiple entries at this
835 // point, as Restore() might be called on a NavigationController that is
836 // already used (e.g. due to WebView's restoreState() API), not only for fresh
837 // NavigationControllers. These entries are not cleared to preserve legacy
838 // behavior and also because `pending_entry_` might point to one of the
839 // pre-existing entries. An exception for this is when `entries_` contains
840 // only the initial NavigationEntry, which must be removed.
841
842 // Do not proceed if selected_navigation will be out of bounds for the updated
843 // entries_ list, since it will be assigned to last_committed_entry_index_ and
844 // used to index entries_.
845 // TODO(https://crbug.com/1287624): Consider also returning early if entries
846 // is empty, since there should be no work to do (rather than marking the
847 // existing entries as needing reload). Also consider returning early if the
848 // selected index is -1, which represents a no-committed-entry state.
849 if (selected_navigation < -1 ||
850 selected_navigation >=
851 base::checked_cast<int>(entries->size() + entries_.size())) {
852 return;
Rakina Zata Amni2322f4f82022-01-24 13:24:24853 }
Charlie Reis23c26da2022-01-29 00:57:47854
Rakina Zata Amni46087a12022-11-11 08:28:38855 // There will always be at least one entry (new NavigationControllers will
856 // have the initial NavigationEntry).
857 if (selected_navigation == -1)
858 selected_navigation = 0;
Charlie Reis23c26da2022-01-29 00:57:47859
Rakina Zata Amni46087a12022-11-11 08:28:38860 if (GetLastCommittedEntry()->IsInitialEntry() && entries->size() > 0) {
861 // If we are on the initial NavigationEntry, it must be the only entry in
862 // the list. Since it's impossible to do a history navigation to the
863 // initial NavigationEntry, `pending_entry_index_` must be -1 (but
864 // `pending_entry` might be set for a new non-history navigation).
865 // Note that we should not clear `entries_` if `entries` is empty (when
866 // InitialNavigationEntry mode is enabled), since that would leave us
867 // without any NavigationEntry.
868 CHECK_EQ(1, GetEntryCount());
869 CHECK_EQ(-1, pending_entry_index_);
870 entries_.clear();
Charlie Reis23c26da2022-01-29 00:57:47871 }
[email protected]ce3fa3c2009-04-20 19:55:57872
[email protected]ce3fa3c2009-04-20 19:55:57873 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44874 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03875 entries_.reserve(entries->size());
Charlie Reis23c26da2022-01-29 00:57:47876 for (auto& entry : *entries) {
Rakina Zata Amni996ee412022-02-17 04:51:43877 if (entry->GetURL().is_empty()) {
878 // We're trying to restore an entry with an empty URL (e.g. from
Rakina Zata Amni2729a512022-03-16 05:54:01879 // persisting the initial NavigationEntry [which is no longer possible but
880 // some old persisted sessions might still contain it] or when the
881 // serializer failed to write the URL because it's too long). Trying to
882 // restore and navigate to an entry with an empty URL will result in
883 // crashes, so change the URL to about:blank. See also
884 // https://crbug.com/1240138 and https://crbug.com/1299335.
Rakina Zata Amni996ee412022-02-17 04:51:43885 entry->SetURL(GURL(url::kAboutBlankURL));
886 }
dcheng36b6aec92015-12-26 06:16:36887 entries_.push_back(
888 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
Charlie Reis23c26da2022-01-29 00:57:47889 }
avif16f85a72015-11-13 18:25:03890
891 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
892 // cleared out safely.
893 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57894
895 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36896 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57897}
898
toyoshim6142d96f2016-12-19 09:07:25899void NavigationControllerImpl::Reload(ReloadType reload_type,
900 bool check_for_repost) {
Rakina Zata Amnid605d462022-06-01 10:17:03901 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "Reload_type",
902 (int)reload_type);
903 SCOPED_CRASH_KEY_BOOL("nav_reentrancy_caller1", "Reload_check",
904 check_for_repost);
liaoyuke9168fba2017-03-10 19:20:28905 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28906 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32907 int current_index = -1;
908
Carlos IL42b416592019-10-07 23:10:36909 if (entry_replaced_by_post_commit_error_) {
910 // If there is an entry that was replaced by a currently active post-commit
911 // error navigation, this can't be the initial navigation.
912 DCHECK(!IsInitialNavigation());
913 // If the current entry is a post commit error, we reload the entry it
914 // replaced instead. We leave the error entry in place until a commit
915 // replaces it, but the pending entry points to the original entry in the
916 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
917 // case that pending_entry_ != entries_[pending_entry_index_].
918 entry = entry_replaced_by_post_commit_error_.get();
919 current_index = GetCurrentEntryIndex();
920 } else if (IsInitialNavigation() && pending_entry_) {
921 // If we are reloading the initial navigation, just use the current
922 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32923 entry = pending_entry_;
924 // The pending entry might be in entries_ (e.g., after a Clone), so we
925 // should also update the current_index.
926 current_index = pending_entry_index_;
927 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00928 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32929 current_index = GetCurrentEntryIndex();
930 if (current_index != -1) {
creis3da03872015-02-20 21:12:32931 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32932 }
[email protected]979a4bc2013-04-24 01:27:15933 }
[email protected]241db352013-04-22 18:04:05934
[email protected]59167c22013-06-03 18:07:32935 // If we are no where, then we can't reload. TODO(darin): We should add a
936 // CanReload method.
937 if (!entry)
938 return;
939
Rakina Zata Amnif297a802022-01-18 03:53:43940 if (entry->IsInitialEntryNotForSynchronousAboutBlank()) {
941 // We should never navigate to an existing initial NavigationEntry that is
942 // the initial NavigationEntry for the initial empty document that hasn't
943 // been overridden by the synchronous about:blank commit, to preserve
944 // legacy behavior where trying to reload when the main frame is on the
945 // initial empty document won't result in a navigation. See also
946 // https://crbug.com/1277414.
947 return;
948 }
949
Aran Gilman37d11632019-10-08 23:07:15950 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30951 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07952 // they really want to do this. If they do, the dialog will call us back
953 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57954 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02955
[email protected]106a0812010-03-18 00:15:12956 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57957 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47958 return;
initial.commit09911bf2008-07-26 23:55:29959 }
Lei Zhang96031532019-10-10 19:05:47960
Wang Huia25efabc2022-09-24 17:27:22961 // Set ReloadType for |entry|.
962 entry->set_reload_type(reload_type);
963
Lei Zhang96031532019-10-10 19:05:47964 if (!IsInitialNavigation())
965 DiscardNonCommittedEntries();
966
967 pending_entry_ = entry;
968 pending_entry_index_ = current_index;
969 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
970
Nate Chapin45f620582021-09-30 17:45:43971 // location.reload() goes through BeginNavigation, so all reloads triggered
972 // via this codepath are browser initiated.
Yoav Weiss8c573952022-11-17 17:35:13973 NavigateToExistingPendingEntry(
974 reload_type,
975 /*initiator_rfh=*/nullptr,
Arthur Sonzognic686e8f2024-01-11 08:36:37976 /*soft_navigation_heuristics_task_id=*/std::nullopt,
Yoav Weiss8c573952022-11-17 17:35:13977 /*navigation_api_key=*/nullptr);
initial.commit09911bf2008-07-26 23:55:29978}
979
[email protected]d202a7c2012-01-04 07:53:47980void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48981 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12982}
983
[email protected]d202a7c2012-01-04 07:53:47984void NavigationControllerImpl::ContinuePendingReload() {
Wang Hui96ab1012022-10-11 02:05:49985 // If the pending reload type has been cleared by another navigation
986 // committing, then do not proceed to reload after a form repost dialog.
toyoshim0df1d3a2016-09-09 09:52:48987 if (pending_reload_ == ReloadType::NONE) {
Wang Hui96ab1012022-10-11 02:05:49988 return;
[email protected]106a0812010-03-18 00:15:12989 }
Wang Hui96ab1012022-10-11 02:05:49990 Reload(pending_reload_, false);
991 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12992}
993
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57994bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09995 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11996}
997
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57998bool NavigationControllerImpl::IsInitialBlankNavigation() {
Rakina Zata Amni46087a12022-11-11 08:28:38999 // Check that we're on the initial NavigationEntry and that this is not a
1000 // cloned tab.
1001 return IsInitialNavigation() && GetEntryCount() == 1 &&
1002 GetLastCommittedEntry()->IsInitialEntry() &&
1003 GetLastCommittedEntry()->restore_type() == RestoreType::kNotRestored;
creis10a4ab72015-10-13 17:22:401004}
1005
Aran Gilman37d11632019-10-08 23:07:151006NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
1007 int nav_entry_id) const {
avi254eff02015-07-01 08:27:581008 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:031009 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:581010}
1011
Adithya Srinivasan9b0c99c2021-08-10 15:19:451012NavigationEntryImpl*
1013NavigationControllerImpl::GetEntryWithUniqueIDIncludingPending(
1014 int nav_entry_id) const {
1015 NavigationEntryImpl* entry = GetEntryWithUniqueID(nav_entry_id);
1016 if (entry)
1017 return entry;
1018 return pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id
Keishi Hattori0e45c022021-11-27 09:25:521019 ? pending_entry_.get()
Adithya Srinivasan9b0c99c2021-08-10 15:19:451020 : nullptr;
1021}
1022
W. James MacLeanc07dc41b2022-07-25 18:52:161023void NavigationControllerImpl::RegisterExistingOriginAsHavingDefaultIsolation(
W. James MacLean1c40862c2020-04-27 21:05:571024 const url::Origin& origin) {
1025 for (int i = 0; i < GetEntryCount(); i++) {
1026 auto* entry = GetEntryAtIndex(i);
W. James MacLeanc07dc41b2022-07-25 18:52:161027 entry->RegisterExistingOriginAsHavingDefaultIsolation(origin);
W. James MacLean1c40862c2020-04-27 21:05:571028 }
1029 if (entry_replaced_by_post_commit_error_) {
1030 // It's possible we could come back to this entry if the error
1031 // page/interstitial goes away.
1032 entry_replaced_by_post_commit_error_
W. James MacLeanc07dc41b2022-07-25 18:52:161033 ->RegisterExistingOriginAsHavingDefaultIsolation(origin);
W. James MacLean1c40862c2020-04-27 21:05:571034 }
W. James MacLean1c40862c2020-04-27 21:05:571035}
1036
avi25764702015-06-23 15:43:371037void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:571038 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:001039 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:371040 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:271041 DCHECK_EQ(-1, pending_entry_index_);
[email protected]765b35502008-08-21 00:51:201042}
1043
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571044NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:471045 if (pending_entry_)
1046 return pending_entry_;
1047 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:201048}
1049
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571050NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:321051 // The pending entry is safe to return for new (non-history), browser-
1052 // initiated navigations. Most renderer-initiated navigations should not
1053 // show the pending entry, to prevent URL spoof attacks.
1054 //
1055 // We make an exception for renderer-initiated navigations in new tabs, as
1056 // long as no other page has tried to access the initial empty document in
1057 // the new tab. If another page modifies this blank page, a URL spoof is
1058 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:321059 bool safe_to_show_pending =
1060 pending_entry_ &&
1061 // Require a new navigation.
avi0dca04d2015-01-26 20:21:091062 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:321063 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:461064 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:321065
1066 // Also allow showing the pending entry for history navigations in a new tab,
1067 // such as Ctrl+Back. In this case, no existing page is visible and no one
1068 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:151069 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
1070 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:321071 safe_to_show_pending = true;
1072
1073 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:191074 return pending_entry_;
1075 return GetLastCommittedEntry();
1076}
1077
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571078int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:201079 if (pending_entry_index_ != -1)
1080 return pending_entry_index_;
1081 return last_committed_entry_index_;
1082}
1083
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571084NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
Rakina Zata Amni46087a12022-11-11 08:28:381085 CHECK_NE(last_committed_entry_index_, -1);
avif16f85a72015-11-13 18:25:031086 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:201087}
1088
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571089bool NavigationControllerImpl::CanViewSource() {
Takashi Toyoshima6c58bbd2023-05-19 09:41:351090 const std::string& mime_type = frame_tree_->root()
1091 ->current_frame_host()
1092 ->GetPage()
1093 .GetContentsMimeType();
Kinuko Yasuda74702f92017-07-31 03:27:531094 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
1095 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:271096 NavigationEntry* visible_entry = GetVisibleEntry();
1097 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:391098 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:161099}
1100
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571101int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:271102 // The last committed entry index must always be less than the number of
Rakina Zata Amnie2d31312022-11-18 03:38:451103 // entries.
arthursonzogni5c4c202d2017-04-25 23:41:271104 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
[email protected]a26023822011-12-29 00:23:551105 return last_committed_entry_index_;
1106}
1107
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571108int NavigationControllerImpl::GetEntryCount() {
Rakina Zata Amnie2d31312022-11-18 03:38:451109 DCHECK_GE(entries_.size(), 1u);
Carlos IL4dea8902020-05-26 15:14:291110 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:551111 return static_cast<int>(entries_.size());
1112}
1113
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571114NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:371115 if (index < 0 || index >= GetEntryCount())
1116 return nullptr;
1117
avif16f85a72015-11-13 18:25:031118 return entries_[index].get();
[email protected]022af742011-12-28 18:37:251119}
1120
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571121NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:471122 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:201123}
1124
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571125int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:461126 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:031127}
1128
Arthur Sonzognic686e8f2024-01-11 08:36:371129std::optional<int> NavigationControllerImpl::GetIndexForGoBack() {
Shivani Sharma298d12852019-01-22 20:04:031130 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
Kevin McNee3b3a56192023-03-17 14:40:591131 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
1132 return index;
1133 }
Shivani Sharma298d12852019-01-22 20:04:031134 }
Arthur Sonzognic686e8f2024-01-11 08:36:371135 return std::nullopt;
Kevin McNee3b3a56192023-03-17 14:40:591136}
1137
1138bool NavigationControllerImpl::CanGoBack() {
1139 return GetIndexForGoBack().has_value();
1140}
1141
Arthur Sonzognic686e8f2024-01-11 08:36:371142std::optional<int> NavigationControllerImpl::GetIndexForGoForward() {
Kevin McNee3b3a56192023-03-17 14:40:591143 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
1144 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
1145 return index;
1146 }
1147 }
Arthur Sonzognic686e8f2024-01-11 08:36:371148 return std::nullopt;
[email protected]765b35502008-08-21 00:51:201149}
1150
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571151bool NavigationControllerImpl::CanGoForward() {
Kevin McNee3b3a56192023-03-17 14:40:591152 return GetIndexForGoForward().has_value();
[email protected]765b35502008-08-21 00:51:201153}
1154
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571155bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:031156 int index = GetIndexForOffset(offset);
1157 return index >= 0 && index < GetEntryCount();
1158}
1159
Xiaohan Wang7f8052e02022-01-14 18:44:281160#if BUILDFLAG(IS_ANDROID)
WangHui74286d52021-03-31 16:17:151161bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
WangHui74286d52021-03-31 16:17:151162 if (offset == 0)
1163 return true;
1164 int increment = offset > 0 ? 1 : -1;
1165 int non_skippable_entries = 0;
1166 for (int index = GetIndexForOffset(increment);
1167 index >= 0 && index < GetEntryCount(); index += increment) {
1168 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1169 non_skippable_entries++;
1170
1171 if (non_skippable_entries == std::abs(offset))
1172 return true;
1173 }
1174 return false;
1175}
1176#endif
1177
[email protected]d202a7c2012-01-04 07:53:471178void NavigationControllerImpl::GoBack() {
Arthur Sonzognic686e8f2024-01-11 08:36:371179 const std::optional<int> target_index = GetIndexForGoBack();
shivanisha55201872018-12-13 04:29:061180
Kevin McNeeedc481c2023-04-27 22:30:581181 CHECK(target_index.has_value());
Miyoung Shin1c565c912021-03-17 12:11:211182
Kevin McNee3b3a56192023-03-17 14:40:591183 GoToIndex(*target_index);
[email protected]765b35502008-08-21 00:51:201184}
1185
[email protected]d202a7c2012-01-04 07:53:471186void NavigationControllerImpl::GoForward() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:161187 // Note that at least one entry (the last one) will be non-skippable since
1188 // entries are marked skippable only when they add another entry because of
1189 // redirect or pushState.
Arthur Sonzognic686e8f2024-01-11 08:36:371190 const std::optional<int> target_index = GetIndexForGoForward();
Kevin McNee3b3a56192023-03-17 14:40:591191
Kevin McNeeedc481c2023-04-27 22:30:581192 CHECK(target_index.has_value());
Kevin McNee3b3a56192023-03-17 14:40:591193
1194 GoToIndex(*target_index);
[email protected]765b35502008-08-21 00:51:201195}
1196
[email protected]d202a7c2012-01-04 07:53:471197void NavigationControllerImpl::GoToIndex(int index) {
Yoav Weiss8c573952022-11-17 17:35:131198 GoToIndex(index, /*initiator_rfh=*/nullptr,
Arthur Sonzognic686e8f2024-01-11 08:36:371199 /*soft_navigation_heuristics_task_id=*/std::nullopt,
Yoav Weiss8c573952022-11-17 17:35:131200 /*navigation_api_key=*/nullptr);
Dave Tapuska8bfd84c2019-03-26 20:47:161201}
1202
Nate Chapinbf682fa32022-09-26 22:41:201203void NavigationControllerImpl::GoToIndex(
1204 int index,
1205 RenderFrameHostImpl* initiator_rfh,
Arthur Sonzognic686e8f2024-01-11 08:36:371206 std::optional<blink::scheduler::TaskAttributionId>
Yoav Weiss8c573952022-11-17 17:35:131207 soft_navigation_heuristics_task_id,
Nate Chapinbf682fa32022-09-26 22:41:201208 const std::string* navigation_api_key) {
sunjian30574a62017-03-21 21:39:441209 TRACE_EVENT0("browser,navigation,benchmark",
1210 "NavigationControllerImpl::GoToIndex");
Peter Boströmf68fe042023-06-07 18:27:501211 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "GoToIndex_index", index);
1212 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "GoToIndex_size",
1213 entries_.size());
1214 // Indices should have have been validated by the caller.
1215 CHECK_GE(index, 0);
1216 CHECK_LT(static_cast<size_t>(index), entries_.size());
[email protected]765b35502008-08-21 00:51:201217
Rakina Zata Amnif297a802022-01-18 03:53:431218 if (entries_[index]->IsInitialEntryNotForSynchronousAboutBlank()) {
1219 // We should never navigate to an existing initial NavigationEntry that is
1220 // the initial NavigationEntry for the initial empty document that hasn't
1221 // been overridden by the synchronous about:blank commit, to preserve
1222 // legacy behavior where trying to reload when the main frame is on the
1223 // initial empty document won't result in a navigation. See also
1224 // https://crbug.com/1277414.
1225 return;
1226 }
1227
[email protected]cbab76d2008-10-13 22:42:471228 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:201229
arthursonzogni5c4c202d2017-04-25 23:41:271230 DCHECK_EQ(nullptr, pending_entry_);
1231 DCHECK_EQ(-1, pending_entry_index_);
Rakina Zata Amnif297a802022-01-18 03:53:431232
arthursonzogni5c4c202d2017-04-25 23:41:271233 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:201234 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:271235 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
1236 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Nate Chapinbf682fa32022-09-26 22:41:201237 NavigateToExistingPendingEntry(ReloadType::NONE, initiator_rfh,
Yoav Weiss8c573952022-11-17 17:35:131238 soft_navigation_heuristics_task_id,
Nate Chapinbf682fa32022-09-26 22:41:201239 navigation_api_key);
[email protected]765b35502008-08-21 00:51:201240}
1241
[email protected]d202a7c2012-01-04 07:53:471242void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:121243 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:031244 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:201245 return;
1246
[email protected]9ba14052012-06-22 23:50:031247 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:201248}
1249
Nate Chapinbf682fa32022-09-26 22:41:201250void NavigationControllerImpl::GoToOffsetFromRenderer(
1251 int offset,
Yoav Weiss8c573952022-11-17 17:35:131252 RenderFrameHostImpl* initiator_rfh,
Arthur Sonzognic686e8f2024-01-11 08:36:371253 std::optional<blink::scheduler::TaskAttributionId>
Yoav Weiss8c573952022-11-17 17:35:131254 soft_navigation_heuristics_task_id) {
Nate Chapinbb74c5a52023-07-21 23:13:351255 // Note: This is actually reached in unit tests.
1256 if (!CanGoToOffset(offset))
Nate Chapin45f620582021-09-30 17:45:431257 return;
1258
Nate Chapinbf682fa32022-09-26 22:41:201259 GoToIndex(GetIndexForOffset(offset), initiator_rfh,
Yoav Weiss8c573952022-11-17 17:35:131260 soft_navigation_heuristics_task_id,
1261 /*navigation_api_key=*/nullptr);
Nate Chapin45f620582021-09-30 17:45:431262}
1263
Xiaohan Wang7f8052e02022-01-14 18:44:281264#if BUILDFLAG(IS_ANDROID)
WangHui74286d52021-03-31 16:17:151265void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
1266 // Note: This is actually reached in unit tests.
1267 if (!CanGoToOffsetWithSkipping(offset))
1268 return;
1269
Elly Fong-Jonesccc6d1f2021-06-14 18:32:421270 if (offset == 0) {
WangHui74286d52021-03-31 16:17:151271 GoToIndex(GetIndexForOffset(offset));
1272 return;
1273 }
1274 int increment = offset > 0 ? 1 : -1;
1275 // Find the offset without counting skippable entries.
1276 int target_index = GetIndexForOffset(increment);
1277 int non_skippable_entries = 0;
1278 for (int index = target_index; index >= 0 && index < GetEntryCount();
1279 index += increment) {
1280 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1281 non_skippable_entries++;
1282
1283 if (non_skippable_entries == std::abs(offset)) {
1284 target_index = index;
1285 break;
1286 }
1287 }
1288
1289 GoToIndex(target_index);
1290}
1291#endif
1292
[email protected]41374f12013-07-24 02:49:281293bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151294 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281295 return false;
[email protected]6a13a6c2011-12-20 21:47:121296
[email protected]43032342011-03-21 14:10:311297 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281298 return true;
[email protected]cbab76d2008-10-13 22:42:471299}
1300
Michael Thiessen9b14d512019-09-23 21:19:471301void NavigationControllerImpl::PruneForwardEntries() {
1302 DiscardNonCommittedEntries();
1303 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471304 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471305 if (num_removed <= 0)
1306 return;
Nate Chapin9eb16be72022-09-23 22:54:311307 RemovedEntriesTracker tracker(weak_factory_.GetSafeRef());
Michael Thiessen9b14d512019-09-23 21:19:471308 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1309 NotifyPrunedEntries(this, remove_start_index /* start index */,
1310 num_removed /* count */);
1311}
1312
Aran Gilman37d11632019-10-08 23:07:151313void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1314 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321315 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311316 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511317 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1318 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321319 }
1320}
1321
Harkiran Bolariaba823e42021-05-21 18:30:361322base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL(
1323 const GURL& url,
1324 const Referrer& referrer,
1325 ui::PageTransition transition,
1326 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471327 LoadURLParams params(url);
1328 params.referrer = referrer;
1329 params.transition_type = transition;
1330 params.extra_headers = extra_headers;
Harkiran Bolariaba823e42021-05-21 18:30:361331 return LoadURLWithParams(params);
[email protected]cf002332012-08-14 19:17:471332}
1333
Harkiran Bolariaba823e42021-05-21 18:30:361334base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams(
1335 const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061336 if (params.is_renderer_initiated)
1337 DCHECK(params.initiator_origin.has_value());
1338
naskob8744d22014-08-28 17:07:431339 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151340 "NavigationControllerImpl::LoadURLWithParams", "url",
1341 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291342 bool is_explicit_navigation =
1343 GetContentClient()->browser()->IsExplicitNavigation(
1344 params.transition_type);
1345 if (HandleDebugURL(params.url, params.transition_type,
1346 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431347 // If Telemetry is running, allow the URL load to proceed as if it's
1348 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491349 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431350 cc::switches::kEnableGpuBenchmarking))
Harkiran Bolariaba823e42021-05-21 18:30:361351 return nullptr;
[email protected]47752982014-07-29 08:01:431352 }
[email protected]8bf1048012012-02-08 01:22:181353
[email protected]cf002332012-08-14 19:17:471354 // Checks based on params.load_type.
1355 switch (params.load_type) {
1356 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431357 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471358 break;
1359 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261360 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471361 NOTREACHED() << "Data load must use data scheme.";
Harkiran Bolariaba823e42021-05-21 18:30:361362 return nullptr;
[email protected]cf002332012-08-14 19:17:471363 }
1364 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461365 }
[email protected]e47ae9472011-10-13 19:48:341366
[email protected]e47ae9472011-10-13 19:48:341367 // The user initiated a load, we don't need to reload anymore.
1368 needs_reload_ = false;
1369
Harkiran Bolariaba823e42021-05-21 18:30:361370 return NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441371}
1372
Charlie Reis4c53a962023-06-21 23:17:531373void NavigationControllerImpl::LoadOriginalRequestURL() {
1374 // If the original request URL is not valid, matches the current URL, or
1375 // involves POST data, then simply reload. The POST check avoids issues with
1376 // sending data to the wrong page.
1377 const GURL& last_committed_url = GetLastCommittedEntry()->GetURL();
1378 const GURL& original_request_url =
1379 GetLastCommittedEntry()->GetOriginalRequestURL();
1380 if (!original_request_url.is_valid() ||
1381 original_request_url == last_committed_url ||
1382 GetLastCommittedEntry()->GetHasPostData()) {
1383 Reload(ReloadType::NORMAL, true);
1384 return;
1385 }
1386
1387 // Otherwise, attempt to load the original request URL without any of the
1388 // other data from the current NavigationEntry, replacing the current entry.
1389 // Loading the original URL is useful in cases such as modifying the user
1390 // agent.
1391 std::unique_ptr<NavigationController::LoadURLParams> load_params =
1392 std::make_unique<NavigationController::LoadURLParams>(
1393 original_request_url);
1394 load_params->should_replace_current_entry = true;
1395 load_params->transition_type = ui::PAGE_TRANSITION_RELOAD;
1396 LoadURLWithParams(*load_params.get());
1397}
1398
Mohamed Abdelhalim833de902019-09-16 17:41:451399bool NavigationControllerImpl::PendingEntryMatchesRequest(
1400 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191401 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451402 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191403}
1404
[email protected]d202a7c2012-01-04 07:53:471405bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321406 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071407 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331408 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441409 bool is_same_document_navigation,
Nate Chapinc7019dd7d2021-06-25 18:29:251410 bool was_on_initial_empty_document,
Shivani Sharmaeef521b2024-01-18 13:03:561411 bool previous_document_had_history_intervention_activation,
Camille Lamy10aafcd32018-12-05 15:48:131412 NavigationRequest* navigation_request) {
1413 DCHECK(navigation_request);
Chris Bookholt27faf8d2022-01-20 01:03:331414
1415 // Note: validation checks and renderer kills due to invalid commit messages
1416 // must happen before getting here, in
1417 // RenderFrameHostImpl::ValidateDidCommitParams. By the time we get here, some
1418 // effects of the navigation have already occurred.
1419
[email protected]cd2e15742013-03-08 04:08:311420 is_initial_navigation_ = false;
1421
Wang Hui96ab1012022-10-11 02:05:491422 // Any pending request to repost a form submission is no longer valid, since a
1423 // different NavigationEntry is committing.
1424 pending_reload_ = ReloadType::NONE;
1425
[email protected]0e8db942008-09-24 21:21:481426 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431427 bool overriding_user_agent_changed = false;
Rakina Zata Amnie2d31312022-11-18 03:38:451428 if (entry_replaced_by_post_commit_error_) {
1429 // Same document navigation events with a post-commit error should already
1430 // be blocked by RenderFrameHostImpl::ValidateDidCommitParams() before
1431 // reaching here.
1432 CHECK(!is_same_document_navigation);
Chris Bookholt27faf8d2022-01-20 01:03:331433
Rakina Zata Amnie2d31312022-11-18 03:38:451434 // Any commit while a post-commit error page is showing should put the
1435 // original entry back, replacing the error page's entry. This includes
1436 // reloads, where the original entry was used as the pending entry and
1437 // should now be at the correct index at commit time.
1438 entries_[last_committed_entry_index_] =
1439 std::move(entry_replaced_by_post_commit_error_);
[email protected]0e8db942008-09-24 21:21:481440 }
Rakina Zata Amnie2d31312022-11-18 03:38:451441 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
1442 details->previous_entry_index = GetLastCommittedEntryIndex();
Shu Yang7a3ec532023-06-21 17:49:001443 // Must honor user agent overrides in the |navigation_request|, such as
1444 // from things like RequestDesktopSiteWebContentsObserverAndroid. As a
1445 // result, besides comparing |pending_entry_|'s user agent against
1446 // LastCommittedEntry's, also need to compare |navigation_request|'s user
1447 // agent against LastCommittedEntry's.
1448 if (navigation_request->is_overriding_user_agent() !=
Shu Yang127e41a2023-12-19 01:26:541449 GetLastCommittedEntry()->GetIsOverridingUserAgent() ||
1450 (PendingEntryMatchesRequest(navigation_request) &&
1451 pending_entry_->GetIsOverridingUserAgent() !=
1452 GetLastCommittedEntry()->GetIsOverridingUserAgent())) {
Shu Yang7a3ec532023-06-21 17:49:001453 overriding_user_agent_changed = true;
Rakina Zata Amnie2d31312022-11-18 03:38:451454 }
[email protected]ecd9d8702008-08-28 22:10:171455
Dave Tapuskaa2ab4f252021-07-08 21:31:281456 bool is_main_frame_navigation = !rfh->GetParent();
1457
Alexander Timind2f2e4f22019-04-02 20:04:531458 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1459 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281460 // For primary frame tree navigations, choose an appropriate
Rakina Zata Amni63b5e8092022-05-20 11:25:141461 // BackForwardCacheMetrics to be associated with the new navigation's
1462 // NavigationEntry, by either creating a new object or reusing the previous
1463 // entry's one.
Dave Tapuskaa2ab4f252021-07-08 21:31:281464 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics;
Takashi Toyoshima7c041d82023-09-26 16:09:211465 if (navigation_request->frame_tree_node()->frame_tree().is_primary()) {
Rakina Zata Amni63b5e8092022-05-20 11:25:141466 back_forward_cache_metrics = BackForwardCacheMetrics::
1467 CreateOrReuseBackForwardCacheMetricsForNavigation(
Dave Tapuskaa2ab4f252021-07-08 21:31:281468 GetLastCommittedEntry(), is_main_frame_navigation,
1469 params.document_sequence_number);
1470 }
Yuzu Saijo29f96ca92022-12-08 04:54:121471
Alexander Timind2f2e4f22019-04-02 20:04:531472 // Notify the last active entry that we have navigated away.
Dave Tapuskaa2ab4f252021-07-08 21:31:281473 if (is_main_frame_navigation && !is_same_document_navigation) {
Rakina Zata Amnie2d31312022-11-18 03:38:451474 if (auto* metrics = GetLastCommittedEntry()->back_forward_cache_metrics()) {
1475 metrics->MainFrameDidNavigateAwayFromDocument();
Alexander Timind2f2e4f22019-04-02 20:04:531476 }
1477 }
1478
Rakina Zata Amnifd8370b2022-11-14 13:32:251479 // Use CommonNavigationParam's `should_replace_current_entry` to determine
1480 // whether the current NavigationEntry should be replaced.
Charlie Reisf8cde712022-10-20 16:25:091481 // (See below for a case where we might override that.)
Rakina Zata Amnifd8370b2022-11-14 13:32:251482 details->did_replace_entry =
1483 navigation_request->common_params().should_replace_current_entry;
Charlie Reisf8cde712022-10-20 16:25:091484
fdegans9caf66a2015-07-30 21:10:421485 // If there is a pending entry at this point, it should have a SiteInstance,
Charlie Reisc4155af2022-10-19 15:33:111486 // except for restored entries. This should be true even if the current commit
1487 // is not related to the pending entry.
jam48cea9082017-02-15 06:13:291488 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481489 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081490 pending_entry_->IsRestored());
Charlie Reisc4155af2022-10-19 15:33:111491
1492 // Only make changes based on the pending entry if the NavigationRequest
1493 // matches it. Otherwise, the pending entry may be for a different request
1494 // (e.g., if a slow history navigation is pending while an auto-subframe
1495 // commit occurs).
1496 if (PendingEntryMatchesRequest(navigation_request)) {
1497 // It is no longer necessary to consider the pending entry as restored.
1498 if (pending_entry_->IsRestored()) {
1499 pending_entry_->set_restore_type(RestoreType::kNotRestored);
1500 was_restored = true;
1501 }
[email protected]e9ba4472008-09-14 15:42:431502
Charlie Reisf8cde712022-10-20 16:25:091503 // If the SiteInstance has changed from the matching pending entry, this
1504 // must be treated as a new navigation with replacement. Set the replacement
1505 // bit here and ClassifyNavigation will identify this case and return
1506 // NEW_ENTRY.
1507 if (!rfh->GetParent() && pending_entry_->site_instance() &&
1508 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1509 DCHECK_NE(-1, pending_entry_index_);
Rakina Zata Amnifd8370b2022-11-14 13:32:251510 // TODO(nasko,creis,rakina): Move this to happen before committing the
1511 // navigation. This is a bit complicated because we don't currently
1512 // set `should_replace_current_entry` for reload/history navigations.
Charlie Reisf8cde712022-10-20 16:25:091513 details->did_replace_entry = true;
1514 }
Nasko Oskovaee2f862018-06-15 00:05:521515 }
[email protected]bcd904482012-02-01 01:54:221516
[email protected]e9ba4472008-09-14 15:42:431517 // Do navigation-type specific actions. These will make and commit an entry.
Miyoung Shin3299cbf2022-11-22 01:41:101518 NavigationType navigation_type =
1519 ClassifyNavigation(rfh, params, navigation_request);
1520 navigation_request->set_navigation_type(navigation_type);
shivanigithub189833f2022-04-27 18:08:451521
Rakina Zata Amnie2d31312022-11-18 03:38:451522 if (ShouldMaintainTrivialSessionHistory(rfh->frame_tree_node())) {
shivanigithub189833f2022-04-27 18:08:451523 // Ensure that this navigation does not add a navigation entry, since
1524 // ShouldMaintainTrivialSessionHistory() means we should not add an entry
1525 // beyond the last committed one. Therefore, `should_replace_current_entry`
1526 // should be set, which replaces the current entry, or this should be a
1527 // reload, which does not create a new entry.
Rakina Zata Amnifd8370b2022-11-14 13:32:251528 DCHECK(navigation_request->common_params().should_replace_current_entry ||
Dominic Farolinoe0f8d7c2023-08-16 15:38:331529 navigation_request->GetReloadType() != ReloadType::NONE);
shivanigithub189833f2022-04-27 18:08:451530 }
1531
Rakina Zata Amnie2d31312022-11-18 03:38:451532 if (GetLastCommittedEntry()->IsInitialEntry()) {
Rakina Zata Amniddf10502022-01-15 02:56:551533 if (rfh->GetParent()) {
Rakina Zata Amni2322f4f82022-01-24 13:24:241534 // This is a subframe navigation on the initial empty document, which used
1535 // to not have a NavigationEntry to attach to. Now it can attach to the
1536 // initial NavigationEntry, and we must ensure that its NavigationEntry
1537 // will keep the "initial NavigationEntry" status and won't append a new
1538 // NavigationEntry (it should always do replacement instead).
1539 // See also https://crbug.com/1277414.
1540 details->should_stay_as_initial_entry = true;
Rakina Zata Amniddf10502022-01-15 02:56:551541 // Subframe navigation on initial NavigationEntry must not append a new
1542 // NavigationEntry (i.e. should not be classified as NEW_SUBFRAME). This
1543 // means every subframe navigation that happens while we're on the initial
1544 // NavigationEntry will always reuse the existing NavigationEntry and
1545 // just update the corresponding FrameNavigationEntry.
Miyoung Shin3299cbf2022-11-22 01:41:101546 DCHECK_EQ(navigation_type, NAVIGATION_TYPE_AUTO_SUBFRAME);
1547 } else if (navigation_type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY) {
Rakina Zata Amniddf10502022-01-15 02:56:551548 // This is a navigation that modifies the initial NavigationEntry, either
1549 // for a replacement or a reload. The initial NavigationEntry should
1550 // retain its "initial NavigationEntry" status in this case.
1551 details->should_stay_as_initial_entry = true;
Rakina Zata Amni2322f4f82022-01-24 13:24:241552 } else if (navigation_request->is_synchronous_renderer_commit() &&
Rakina Zata Amnifd8370b2022-11-14 13:32:251553 !navigation_request->IsSameDocument() && !rfh->GetParent()) {
1554 DCHECK(navigation_request->common_params().should_replace_current_entry);
Rakina Zata Amni2322f4f82022-01-24 13:24:241555 // This is a synchronous about:blank navigation on the main frame, which
1556 // used to not create a NavigationEntry when we have no NavigationEntry on
1557 // FrameTree creation. We now have the initial NavigationEntry and are on
1558 // the initial NavigationEntry. To preserve old behavior, we should still
1559 // keep the "initial" status for the new NavigationEntry that we will
1560 // create for this navigation, so that subframe navigations under the
1561 // synchronously committed about:blank document will never append new
1562 // NavigationEntry, and instead will just reuse the initial
1563 // NavigationEntry and modify the corresponding FrameNavigationEntries.
1564 // See also https://crbug.com/1277414.
1565 details->should_stay_as_initial_entry = true;
Rakina Zata Amniddf10502022-01-15 02:56:551566 }
1567 }
1568 DCHECK(!details->should_stay_as_initial_entry ||
1569 GetLastCommittedEntry()->IsInitialEntry());
[email protected]4bf3522c2010-08-19 21:00:201570
eugenebutee08663a2017-04-27 17:43:121571 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441572 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121573
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071574 details->is_prerender_activation =
1575 navigation_request->IsPrerenderedPageActivation();
Robert Lin540dbd12022-04-28 22:07:241576 details->is_in_active_page = navigation_request->GetRenderFrameHost()
1577 ->GetOutermostMainFrame()
1578 ->IsInPrimaryMainFrame();
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071579
Peter Boströmd7592132019-01-30 04:50:311580 // Make sure we do not discard the pending entry for a different ongoing
1581 // navigation when a same document commit comes in unexpectedly from the
1582 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1583 // other navigation types. See https://crbug.com/900036.
1584 // TODO(crbug.com/926009): Handle history.pushState() as well.
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061585 bool keep_pending_entry =
1586 is_same_document_navigation &&
Miyoung Shin3299cbf2022-11-22 01:41:101587 navigation_type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY &&
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061588 pending_entry_ && !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311589
Miyoung Shin3299cbf2022-11-22 01:41:101590 switch (navigation_type) {
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061591 case NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY:
Charlie Reisc0f17d2d2021-01-12 18:52:491592 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051593 rfh, params, details->is_same_document, details->did_replace_entry,
Shivani Sharmaeef521b2024-01-18 13:03:561594 previous_document_had_history_intervention_activation,
1595 navigation_request, details);
[email protected]e9ba4472008-09-14 15:42:431596 break;
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061597 case NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY:
Charlie Reisc0f17d2d2021-01-12 18:52:491598 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1599 was_restored, navigation_request,
Rakina Zata Amnia4e27222021-12-22 01:05:001600 keep_pending_entry, details);
[email protected]e9ba4472008-09-14 15:42:431601 break;
[email protected]8ff00d72012-10-23 19:12:211602 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471603 RendererDidNavigateNewSubframe(
1604 rfh, params, details->is_same_document, details->did_replace_entry,
Shivani Sharmaeef521b2024-01-18 13:03:561605 previous_document_had_history_intervention_activation,
1606 navigation_request, details);
[email protected]e9ba4472008-09-14 15:42:431607 break;
[email protected]8ff00d72012-10-23 19:12:211608 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391609 if (!RendererDidNavigateAutoSubframe(
Nate Chapinc7019dd7d2021-06-25 18:29:251610 rfh, params, details->is_same_document,
Rakina Zata Amnia4e27222021-12-22 01:05:001611 was_on_initial_empty_document, navigation_request, details)) {
creisce0ef3572017-01-26 17:53:081612 // We don't send a notification about auto-subframe PageState during
1613 // UpdateStateForFrame, since it looks like nothing has changed. Send
1614 // it here at commit time instead.
1615 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431616 return false;
creis59d5a47cb2016-08-24 23:57:191617 }
[email protected]e9ba4472008-09-14 15:42:431618 break;
Aran Gilman37d11632019-10-08 23:07:151619 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431620 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151621 break;
[email protected]765b35502008-08-21 00:51:201622 }
1623
[email protected]688aa65c62012-09-28 04:32:221624 // At this point, we know that the navigation has just completed, so
1625 // record the time.
1626 //
1627 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261628 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331629 base::Time timestamp =
1630 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1631 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131632 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221633
Peter Boströmd7592132019-01-30 04:50:311634 // If we aren't keeping the pending entry, there shouldn't be one at this
1635 // point. Clear it again in case any error cases above forgot to do so.
1636 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1637 // been cleared instead of protecting against it.
1638 if (!keep_pending_entry)
Rakina Zata Amniddf10502022-01-15 02:56:551639 DiscardNonCommittedEntriesWithCommitDetails(details);
[email protected]f233e4232013-02-23 00:55:141640
[email protected]e9ba4472008-09-14 15:42:431641 // All committed entries should have nonempty content state so WebKit doesn't
1642 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471643 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321644 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221645 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441646 active_entry->SetHttpStatusCode(params.http_status_code);
Fergal Daly0686c0e2022-06-28 02:08:141647
Alexander Timind2f2e4f22019-04-02 20:04:531648 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1649 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281650 if (back_forward_cache_metrics &&
1651 !active_entry->back_forward_cache_metrics()) {
Alexander Timind2f2e4f22019-04-02 20:04:531652 active_entry->set_back_forward_cache_metrics(
1653 std::move(back_forward_cache_metrics));
1654 }
Dave Tapuskaa2ab4f252021-07-08 21:31:281655
1656 // `back_forward_cache_metrics()` may return null as we do not record
1657 // back-forward cache metrics for navigations in non-primary frame trees.
1658 if (active_entry->back_forward_cache_metrics()) {
Fergal Daly0686c0e2022-06-28 02:08:141659 // TODO(https://crbug.com/1338089): Remove this.
1660 // These are both only available from details at this point, so we capture
1661 // them here.
1662 SCOPED_CRASH_KEY_NUMBER("BFCacheMismatch", "navigation_type",
Miyoung Shin3299cbf2022-11-22 01:41:101663 navigation_type);
Fergal Daly0686c0e2022-06-28 02:08:141664 SCOPED_CRASH_KEY_BOOL("BFCacheMismatch", "did_replace",
1665 details->did_replace_entry);
Dave Tapuskaa2ab4f252021-07-08 21:31:281666 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
1667 navigation_request,
1668 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
1669 }
naskoc7533512016-05-06 17:01:121670
Charles Reisc0507202017-09-21 00:40:021671 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1672 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1673 // A mismatch can occur if the renderer lies or due to a unique name collision
1674 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121675 FrameNavigationEntry* frame_entry =
1676 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021677 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1678 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031679 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081680 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1681 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031682 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201683
1684 // Remember the bindings the renderer process has at this point, so that
1685 // we do not grant this entry additional bindings if we come back to it.
1686 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301687 }
[email protected]132e281a2012-07-31 18:32:441688
[email protected]97d8f0d2013-10-29 16:49:211689 // Once it is committed, we no longer need to track several pieces of state on
1690 // the entry.
naskoc7533512016-05-06 17:01:121691 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131692
[email protected]49bd30e62011-03-22 20:12:591693 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221694 // TODO(creis): This check won't pass for subframes until we create entries
1695 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001696 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421697 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591698
[email protected]e9ba4472008-09-14 15:42:431699 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001700 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001701 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311702 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531703
arthursonzogni7ddc6542021-04-09 09:16:501704 active_entry->SetIsOverridingUserAgent(
1705 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031706
[email protected]93f230e02011-06-01 14:40:001707 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291708
aelias100c9192017-01-13 00:01:431709 if (overriding_user_agent_changed)
1710 delegate_->UpdateOverridingUserAgent();
1711
creis03b48002015-11-04 00:54:561712 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1713 // one knows the latest NavigationEntry it is showing (whether it has
1714 // committed anything in this navigation or not). This allows things like
1715 // state and title updates from RenderFrames to apply to the latest relevant
1716 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381717 int nav_entry_id = active_entry->GetUniqueID();
Ali Hijazid87307d2022-11-07 20:15:031718 for (FrameTreeNode* node : frame_tree_->Nodes())
dcheng57e39e22016-01-21 00:25:381719 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
Hayato Ito2c8c08d02021-06-23 03:38:431720
1721 if (navigation_request->IsPrerenderedPageActivation()) {
1722 BroadcastHistoryOffsetAndLength();
1723 // TODO(crbug.com/1222893): Broadcasting happens after the prerendered page
1724 // is activated. As a result, a "prerenderingchange" event listener sees the
1725 // history.length which is not updated yet. We should guarantee that
1726 // history's length and offset should be updated before a
1727 // "prerenderingchange" event listener runs. One possible approach is to use
1728 // the same IPC which "prerenderingchange" uses, and propagate history's
1729 // length and offset together with that.
1730 }
1731
[email protected]e9ba4472008-09-14 15:42:431732 return true;
initial.commit09911bf2008-07-26 23:55:291733}
1734
[email protected]8ff00d72012-10-23 19:12:211735NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321736 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101737 const mojom::DidCommitProvisionalLoadParams& params,
Rakina Zata Amni2322f4f82022-01-24 13:24:241738 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591739 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511740 "ClassifyNavigation");
1741
avi7c6f35e2015-05-08 17:52:381742 if (params.did_create_new_entry) {
Rakina Zata Amni2322f4f82022-01-24 13:24:241743 // A new entry. We may or may not have a corresponding pending entry, and
1744 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001745 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491746 trace_return.set_return_reason("new entry, no parent, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061747 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381748 }
Rakina Zata Amni2322f4f82022-01-24 13:24:241749 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511750 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381751 return NAVIGATION_TYPE_NEW_SUBFRAME;
1752 }
1753
Charlie Reisc0f17d2d2021-01-12 18:52:491754 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381755 DCHECK(!params.history_list_was_cleared);
1756
avi39c1edd32015-06-04 20:06:001757 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381758 // All manual subframes would be did_create_new_entry and handled above, so
1759 // we know this is auto.
Rakina Zata Amniacd4df662022-11-15 06:49:081760 trace_return.set_return_reason("subframe, last commmited, auto subframe");
1761 return NAVIGATION_TYPE_AUTO_SUBFRAME;
avi7c6f35e2015-05-08 17:52:381762 }
1763
Rakina Zata Amnif6950d552020-11-24 03:26:101764 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1765 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381766 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1767 // create a new page.
1768
Hayato Ito2ae49442021-07-02 02:59:251769 // This main frame navigation is not a history navigation (since
1770 // nav_entry_id is 0), but didn't create a new entry. So this must be a
1771 // reload or a replacement navigation, which will modify the existing entry.
1772 //
Nasko Oskov332593c2018-08-16 17:21:341773 // TODO(nasko): With error page isolation, reloading an existing session
1774 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491775 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341776 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511777 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491778 "nav entry 0, last committed, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061779 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381780 }
1781
Charlie Reisf8cde712022-10-20 16:25:091782 if (PendingEntryMatchesRequest(navigation_request)) {
Nasko Oskovaee2f862018-06-15 00:05:521783 // If the SiteInstance of the |pending_entry_| does not match the
1784 // SiteInstance that got committed, treat this as a new navigation with
1785 // replacement. This can happen if back/forward/reload encounters a server
1786 // redirect to a different site or an isolated error page gets successfully
1787 // reloaded into a different SiteInstance.
1788 if (pending_entry_->site_instance() &&
1789 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491790 trace_return.set_return_reason("pending matching nav entry, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061791 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521792 }
creis77c9aa32015-09-25 19:59:421793
Nasko Oskovaee2f862018-06-15 00:05:521794 if (pending_entry_index_ == -1) {
1795 // In this case, we have a pending entry for a load of a new URL but Blink
1796 // didn't do a new navigation (params.did_create_new_entry). First check
1797 // to make sure Blink didn't treat a new cross-process navigation as
1798 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161799 // we must treat it as NEW rather than the converted reload case below,
1800 // since the new SiteInstance doesn't match the last committed entry.
Rakina Zata Amnie2d31312022-11-18 03:38:451801 if (GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161802 trace_return.set_return_reason("new pending, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061803 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521804 }
1805
1806 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161807 // We will create a pending entry, but NavigateWithoutEntry will convert
1808 // it to a reload since it's the same page and not create a new entry for
1809 // it. (The user doesn't want to have a new back/forward entry when they
1810 // do this.) Therefore we want to just ignore the pending entry and go
1811 // back to where we were (the "existing entry").
1812 trace_return.set_return_reason("new pending, existing (same) entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061813 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521814 }
avi7c6f35e2015-05-08 17:52:381815 }
1816
Rakina Zata Amni153d5702021-09-13 22:48:001817 if (navigation_request->commit_params().intended_as_new_entry) {
avi7c6f35e2015-05-08 17:52:381818 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491819 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1820 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161821 trace_return.set_return_reason("intended as new entry, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061822 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381823 }
1824
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121825 if (navigation_request->DidEncounterError() &&
1826 failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101827 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381828 // If the renderer was going to a new pending entry that got cleared because
1829 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491830 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381831 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511832 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491833 "unreachable, matching pending, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061834 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381835 }
1836
Charlie Reisc0f17d2d2021-01-12 18:52:491837 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101838 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511839 trace_return.traced_value()->SetInteger("existing_entry_index",
1840 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381841 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441842 // The renderer has committed a navigation to an entry that no longer
1843 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491844 trace_return.set_return_reason("existing entry -1, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061845 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381846 }
1847
avi7c6f35e2015-05-08 17:52:381848 // Since we weeded out "new" navigations above, we know this is an existing
1849 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491850 trace_return.set_return_reason("default return, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061851 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381852}
1853
Rakina Zata Amni3460d382021-10-29 00:43:371854void NavigationControllerImpl::UpdateNavigationEntryDetails(
1855 NavigationEntryImpl* entry,
1856 RenderFrameHostImpl* rfh,
1857 const mojom::DidCommitProvisionalLoadParams& params,
1858 NavigationRequest* request,
1859 NavigationEntryImpl::UpdatePolicy update_policy,
Rakina Zata Amnia4e27222021-12-22 01:05:001860 bool is_new_entry,
1861 LoadCommittedDetails* commit_details) {
Rakina Zata Amni3460d382021-10-29 00:43:371862 // Update the FrameNavigationEntry.
Rakina Zata Amniafd3c6582021-11-30 06:19:171863 std::vector<GURL> redirects;
Rakina Zata Amni3460d382021-10-29 00:43:371864 entry->AddOrUpdateFrameEntry(
1865 rfh->frame_tree_node(), update_policy, params.item_sequence_number,
Domenic Denicolacc094fb2022-03-16 23:40:571866 params.document_sequence_number, params.navigation_api_key,
Rakina Zata Amni3460d382021-10-29 00:43:371867 rfh->GetSiteInstance(), nullptr, params.url,
1868 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amniafd3c6582021-11-30 06:19:171869 Referrer(*params.referrer),
1870 request ? request->common_params().initiator_origin : params.origin,
Arthur Sonzognic686e8f2024-01-11 08:36:371871 request ? request->common_params().initiator_base_url : std::nullopt,
Rakina Zata Amniafd3c6582021-11-30 06:19:171872 request ? request->GetRedirectChain() : redirects, params.page_state,
1873 params.method, params.post_id, nullptr /* blob_url_loader_factory */,
Rakina Zata Amni3460d382021-10-29 00:43:371874 ComputePolicyContainerPoliciesForFrameEntry(
Rakina Zata Amniafd3c6582021-11-30 06:19:171875 rfh, request && request->IsSameDocument(),
1876 request ? request->common_params().url : params.url));
Rakina Zata Amni3460d382021-10-29 00:43:371877
1878 if (rfh->GetParent()) {
1879 // Only modify the NavigationEntry for main frame navigations.
1880 return;
1881 }
1882 if (entry->update_virtual_url_with_url())
1883 UpdateVirtualURLToURL(entry, params.url);
1884 // Don't use the page type from the pending entry. Some interstitial page
1885 // may have set the type to interstitial. Once we commit, however, the page
1886 // type must always be normal or error.
Rakina Zata Amniafd3c6582021-11-30 06:19:171887 entry->set_page_type((request && request->DidEncounterError())
1888 ? PAGE_TYPE_ERROR
1889 : PAGE_TYPE_NORMAL);
Rakina Zata Amnif297a802022-01-18 03:53:431890 if (commit_details && commit_details->should_stay_as_initial_entry) {
1891 // Retain the "initial NavigationEntry" status.
1892 if (request->IsSameDocument()) {
1893 // If this is for a same-document navigation, the NavigationEntry must be
1894 // reused and should already be marked as the initial NavigationEntry.
1895 DCHECK(entry->IsInitialEntry());
1896 } else {
1897 // If this is for a cross-document navigation, it can be caused by a
1898 // renderer-initiated reload, or the synchronous about:blank commit. Mark
1899 // "for synchronous about:blank" in the latter case, and also when it is
1900 // reloading a "for synchronous about:blank" entry. Otherwise, the entry
1901 // is not for a synchronous about:blank commit.
1902 NavigationEntryImpl::InitialNavigationEntryState new_state =
1903 NavigationEntryImpl::InitialNavigationEntryState::
1904 kInitialNotForSynchronousAboutBlank;
1905 if (entry->IsInitialEntryForSynchronousAboutBlank() ||
1906 request->is_synchronous_renderer_commit()) {
1907 new_state = NavigationEntryImpl::InitialNavigationEntryState::
1908 kInitialForSynchronousAboutBlank;
1909 }
1910 entry->set_initial_navigation_entry_state(new_state);
1911 }
1912 } else if (commit_details && !commit_details->should_stay_as_initial_entry) {
1913 // Remove the "initial NavigationEntry" status.
1914 entry->set_initial_navigation_entry_state(
1915 NavigationEntryImpl::InitialNavigationEntryState::kNonInitial);
Rakina Zata Amnia4e27222021-12-22 01:05:001916 }
Rakina Zata Amniddf10502022-01-15 02:56:551917
Rakina Zata Amni3460d382021-10-29 00:43:371918 if (is_new_entry) {
1919 // Some properties of the NavigationEntry are only set when the entry is
1920 // new (we aren't reusing it).
1921 entry->SetTransitionType(params.transition);
Rakina Zata Amniafd3c6582021-11-30 06:19:171922 entry->SetOriginalRequestURL(request ? request->GetOriginalRequestURL()
1923 : GURL::EmptyGURL());
Adithya Srinivasan72b07352023-12-21 15:56:011924 DCHECK_EQ(rfh->GetPage().is_overriding_user_agent(),
1925 params.is_overriding_user_agent);
Rakina Zata Amni3460d382021-10-29 00:43:371926 entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
1927 } else {
1928 // We are reusing the NavigationEntry. The site instance will normally be
1929 // the same except for a few cases:
1930 // 1) session restore, when no site instance will be assigned or
1931 // 2) redirect, when the site instance is reset.
1932 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
1933 entry->site_instance() == rfh->GetSiteInstance());
1934 }
1935}
1936
Rakina Zata Amniafd3c6582021-11-30 06:19:171937void NavigationControllerImpl::CreateInitialEntry() {
1938 DCHECK_EQ(entries_.size(), 0u);
Ali Hijazid87307d2022-11-07 20:15:031939 RenderFrameHostImpl* rfh = frame_tree_->root()->current_frame_host();
Rakina Zata Amniafd3c6582021-11-30 06:19:171940 auto params = mojom::DidCommitProvisionalLoadParams::New();
1941 // The initial NavigationEntry's URL is the empty URL. This preserves the old
1942 // behavior of WebContent's GetLastCommittedURL() and GetVisibleURL() from
1943 // before we have initial NavigationEntries.
1944 params->url = GURL::EmptyGURL();
1945 params->http_status_code = 0;
1946 params->url_is_unreachable = false;
1947 params->method = "GET";
Rakina Zata Amniafd3c6582021-11-30 06:19:171948 params->post_id = -1;
1949 params->embedding_token = base::UnguessableToken::Create();
1950 params->navigation_token = base::UnguessableToken::Create();
1951 params->did_create_new_entry = true;
1952 params->origin = rfh->GetLastCommittedOrigin();
1953 params->should_update_history = true;
1954 params->item_sequence_number = 0;
1955 params->document_sequence_number = 0;
Abhijeet Kandalkare26014a92022-10-13 04:21:151956 bool is_in_fenced_frame_tree = rfh->IsNestedWithinFencedFrame();
Rakina Zata Amniafd3c6582021-11-30 06:19:171957 params->transition = is_in_fenced_frame_tree
1958 ? ui::PAGE_TRANSITION_AUTO_SUBFRAME
1959 : ui::PAGE_TRANSITION_LINK;
1960 params->referrer = blink::mojom::Referrer::New();
1961
Rakina Zata Amniafd3c6582021-11-30 06:19:171962 auto new_entry = std::make_unique<NavigationEntryImpl>(
1963 rfh->GetSiteInstance(), params->url, Referrer(*params->referrer),
W. James MacLean78e2f872023-01-24 17:59:381964 rfh->GetLastCommittedOrigin(), rfh->GetInheritedBaseUrl(),
W. James MacLean23e90a12022-12-21 04:38:211965 std::u16string() /* title */, ui::PAGE_TRANSITION_TYPED,
1966 false /* renderer_initiated */, nullptr /* blob_url_loader_factory */,
1967 true /* is_initial_entry */);
Rakina Zata Amniafd3c6582021-11-30 06:19:171968 UpdateNavigationEntryDetails(
1969 new_entry.get(), rfh, *params, nullptr /* request */,
Rakina Zata Amnia4e27222021-12-22 01:05:001970 NavigationEntryImpl::UpdatePolicy::kUpdate, true /* is_new_entry */,
1971 nullptr /* commit_details */);
Rakina Zata Amniafd3c6582021-11-30 06:19:171972
1973 InsertOrReplaceEntry(std::move(new_entry), false /* replace_entry */,
1974 false /* was_post_commit_error */,
Rakina Zata Amnia4e27222021-12-22 01:05:001975 is_in_fenced_frame_tree, nullptr /* commit_details */);
Rakina Zata Amniafd3c6582021-11-30 06:19:171976}
1977
Charlie Reisc0f17d2d2021-01-12 18:52:491978void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321979 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071980 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361981 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441982 bool replace_entry,
Shivani Sharmaeef521b2024-01-18 13:03:561983 bool previous_document_had_history_intervention_activation,
Rakina Zata Amnia4e27222021-12-22 01:05:001984 NavigationRequest* request,
1985 LoadCommittedDetails* commit_details) {
dcheng9bfa5162016-04-09 01:00:571986 std::unique_ptr<NavigationEntryImpl> new_entry;
Arthur Sonzognic686e8f2024-01-11 08:36:371987 const std::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451988 request->common_params().initiator_origin;
Arthur Sonzognic686e8f2024-01-11 08:36:371989 std::optional<GURL> initiator_base_url;
W. James MacLean8be423a2023-03-31 21:35:521990 if (params.url.IsAboutBlank() || params.url.IsAboutSrcdoc()) {
1991 initiator_base_url = request->common_params().initiator_base_url;
1992 }
Lukasz Anforowicz435bcb582019-07-12 20:50:061993
creisf49dfc92016-07-26 17:05:181994 // First check if this is an in-page navigation. If so, clone the current
1995 // entry instead of looking at the pending entry, because the pending entry
1996 // does not have any subframe history items.
Rakina Zata Amnie2d31312022-11-18 03:38:451997 if (is_same_document) {
Nate Chapin63db0d12022-01-20 22:03:301998 FrameNavigationEntry* previous_frame_entry =
1999 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
Patrick Monette50e8bd82019-06-13 22:40:452000 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:482001 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Domenic Denicolacc094fb2022-03-16 23:40:572002 params.document_sequence_number, params.navigation_api_key,
Nate Chapinfbfe5af2021-06-10 17:22:082003 rfh->GetSiteInstance(), nullptr, params.url,
2004 GetCommittedOriginForFrameEntry(params, request),
W. James MacLean23e90a12022-12-21 04:38:212005 Referrer(*params.referrer), initiator_origin, initiator_base_url,
Rakina Zata Amni82fafba2021-03-11 07:07:092006 request->GetRedirectChain(), params.page_state, params.method,
2007 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:392008 // We will set the document policies later in this function.
Nate Chapin63db0d12022-01-20 22:03:302009 nullptr /* policy_container_policies */,
Domenic Denicolacc094fb2022-03-16 23:40:572010 // Try to preserve protect_url_in_navigation_api from the previous
Nate Chapin63db0d12022-01-20 22:03:302011 // FrameNavigationEntry.
2012 previous_frame_entry &&
Domenic Denicolacc094fb2022-03-16 23:40:572013 previous_frame_entry->protect_url_in_navigation_api());
Charles Reisf44482022017-10-13 21:15:032014
creisf49dfc92016-07-26 17:05:182015 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Ali Hijazid87307d2022-11-07 20:15:032016 frame_entry, true, request->frame_tree_node(), frame_tree_->root());
Mike West800532c2021-10-14 09:26:522017 if (new_entry->GetURL().DeprecatedGetOriginAsURL() !=
2018 params.url.DeprecatedGetOriginAsURL()) {
jama78746e2017-02-22 17:21:572019 // TODO(jam): we had one report of this with a URL that was redirecting to
2020 // only tildes. Until we understand that better, don't copy the cert in
2021 // this case.
2022 new_entry->GetSSL() = SSLStatus();
2023 }
creisf49dfc92016-07-26 17:05:182024
Patrick Monette50e8bd82019-06-13 22:40:452025 // It is expected that |frame_entry| is now owned by |new_entry|. This means
2026 // that |frame_entry| should now have a reference count of exactly 2: one
2027 // due to the local variable |frame_entry|, and another due to |new_entry|
2028 // also retaining one. This should never fail, because it's the main frame.
2029 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:182030 }
2031
Harkiran Bolaria59290d62021-03-17 01:53:012032 // If this is an activation navigation from a prerendered page, transfer the
2033 // new entry from an entry already created and stored in the
2034 // NavigationRequest. |new_entry| will not have been set prior to this as
2035 // |is_same_document| is mutually exclusive with
2036 // |IsPrerenderedPageActivation|.
2037 if (request->IsPrerenderedPageActivation()) {
2038 DCHECK(!is_same_document);
2039 DCHECK(!new_entry);
2040 new_entry = request->TakePrerenderNavigationEntry();
2041 DCHECK(new_entry);
2042 }
2043
Charlie Reisc0f17d2d2021-01-12 18:52:492044 // Only make a copy of the pending entry if it is appropriate for the new
2045 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:452046 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:042047 // 1. The SiteInstance hasn't been assigned to something else.
2048 // 2. The pending entry was intended as a new entry, rather than being a
2049 // history navigation that was interrupted by an unrelated,
2050 // renderer-initiated navigation.
2051 // TODO(csharrison): Investigate whether we can remove some of the coarser
2052 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:452053 if (!new_entry && PendingEntryMatchesRequest(request) &&
2054 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:342055 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:432056 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:352057 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:432058
Camille Lamy62b826012019-02-26 09:15:472059 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:452060 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
creisf49dfc92016-07-26 17:05:182061 }
2062
Charlie Reisc0f17d2d2021-01-12 18:52:492063 // For cross-document commits with no matching pending entry, create a new
2064 // entry.
creisf49dfc92016-07-26 17:05:182065 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:062066 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:072067 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
W. James MacLean23e90a12022-12-21 04:38:212068 initiator_origin, initiator_base_url,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:582069 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:452070 params.transition, request->IsRendererInitiated(),
Rakina Zata Amniafd3c6582021-11-30 06:19:172071 nullptr, // blob_url_loader_factory
2072 false); // is_initial_entry
[email protected]f8f93eb2012-09-25 03:06:242073
2074 // Find out whether the new entry needs to update its virtual URL on URL
2075 // change and set up the entry accordingly. This is needed to correctly
2076 // update the virtual URL when replaceState is called after a pushState.
2077 GURL url = params.url;
2078 bool needs_update = false;
Charlie Reisc0f17d2d2021-01-12 18:52:492079 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:412080 // update the virtual URL based on the new URL. For example, this is needed
2081 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
2082 // the URL.
Rakina Zata Amni3460d382021-10-29 00:43:372083 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
2084 &url, browser_context_, &needs_update);
2085 new_entry->set_update_virtual_url_with_url(needs_update);
2086
Camille Lamy62b826012019-02-26 09:15:472087 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:452088 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
[email protected]e9ba4472008-09-14 15:42:432089 }
2090
Harkiran Bolaria59290d62021-03-17 01:53:012091 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
2092 // for prerendered contents, if any. This is because prerendering/activation
2093 // technically won't be creating a new document. Unlike BFCache, prerendering
2094 // creates a new NavigationEntry rather than using an existing one.
2095 if (!request->IsPrerenderedPageActivation()) {
Rakina Zata Amni3460d382021-10-29 00:43:372096 UpdateNavigationEntryDetails(new_entry.get(), rfh, params, request,
2097 NavigationEntryImpl::UpdatePolicy::kUpdate,
Rakina Zata Amnia4e27222021-12-22 01:05:002098 true /* is_new_entry */, commit_details);
creis8b5cd4c2015-06-19 00:11:082099
Harkiran Bolaria59290d62021-03-17 01:53:012100 // history.pushState() is classified as a navigation to a new page, but sets
2101 // is_same_document to true. In this case, we already have the title and
2102 // favicon available, so set them immediately.
Rakina Zata Amnie2d31312022-11-18 03:38:452103 if (is_same_document) {
Harkiran Bolaria59290d62021-03-17 01:53:012104 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
2105 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
2106 }
[email protected]3a868f212014-08-09 10:41:192107 }
[email protected]ff64b3e2014-05-31 04:07:332108
[email protected]60d6cca2013-04-30 08:47:132109 DCHECK(!params.history_list_was_cleared || !replace_entry);
2110 // The browser requested to clear the session history when it initiated the
2111 // navigation. Now we know that the renderer has updated its state accordingly
2112 // and it is safe to also clear the browser side history.
2113 if (params.history_list_was_cleared) {
Rakina Zata Amniddf10502022-01-15 02:56:552114 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]60d6cca2013-04-30 08:47:132115 entries_.clear();
2116 last_committed_entry_index_ = -1;
2117 }
2118
Nasko Oskovaee2f862018-06-15 00:05:522119 // If this is a new navigation with replacement and there is a
2120 // pending_entry_ which matches the navigation reported by the renderer
2121 // process, then it should be the one replaced, so update the
2122 // last_committed_entry_index_ to use it.
2123 if (replace_entry && pending_entry_index_ != -1 &&
Charlie Reisf8cde712022-10-20 16:25:092124 PendingEntryMatchesRequest(request)) {
Nasko Oskovaee2f862018-06-15 00:05:522125 last_committed_entry_index_ = pending_entry_index_;
2126 }
2127
Alexander Timine3ec4192020-04-20 16:39:402128 SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaeef521b2024-01-18 13:03:562129 replace_entry, previous_document_had_history_intervention_activation,
Alexander Timine3ec4192020-04-20 16:39:402130 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:052131
Yuzu Saijoa725585f2022-11-28 04:14:032132 // If this is a history navigation and the old entry has an existing
2133 // back/forward cache metrics object, keep using the old one so that the
2134 // reasons logged from the last time the page navigated gets preserved.
2135 if (BackForwardCacheMetrics::IsCrossDocumentMainFrameHistoryNavigation(
2136 request)) {
2137 // Use |request->GetNavigationEntry()| instead of |pending_entry_| here
2138 // because some tests do not have a pending entry.
2139 NavigationEntryImpl* entry =
2140 static_cast<NavigationEntryImpl*>(request->GetNavigationEntry());
2141 if (entry && entry->back_forward_cache_metrics()) {
2142 scoped_refptr<BackForwardCacheMetrics> metrics =
2143 entry->TakeBackForwardCacheMetrics();
2144 new_entry->set_back_forward_cache_metrics(std::move(metrics));
2145 }
2146 }
2147
Carlos IL42b416592019-10-07 23:10:362148 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
Dave Tapuska87696ae2021-11-18 18:48:312149 !request->post_commit_error_page_html().empty(),
Abhijeet Kandalkare26014a92022-10-13 04:21:152150 rfh->IsNestedWithinFencedFrame(), commit_details);
[email protected]e9ba4472008-09-14 15:42:432151}
2152
Charlie Reisc0f17d2d2021-01-12 18:52:492153void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:322154 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072155 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:362156 bool is_same_document,
jam48cea9082017-02-15 06:13:292157 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:452158 NavigationRequest* request,
Rakina Zata Amnia4e27222021-12-22 01:05:002159 bool keep_pending_entry,
2160 LoadCommittedDetails* commit_details) {
creis26d22632017-04-21 20:23:562161 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
2162 << "that a last committed entry exists.";
2163
[email protected]e9ba4472008-09-14 15:42:432164 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:002165 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:432166
Charlie Reis7e2cb6d2021-01-26 01:27:162167 NavigationEntryImpl* entry = nullptr;
Rakina Zata Amni153d5702021-09-13 22:48:002168 if (request->commit_params().intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:162169 // We're guaranteed to have a last committed entry if intended_as_new_entry
2170 // is true.
avicbdc4c12015-07-01 16:07:112171 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:162172
2173 // If the NavigationRequest matches a new pending entry and is classified as
2174 // EXISTING_ENTRY, then it is a navigation to the same URL that was
2175 // converted to a reload, such as a user pressing enter in the omnibox.
Charlie Reisf8cde712022-10-20 16:25:092176 if (pending_entry_index_ == -1 && PendingEntryMatchesRequest(request)) {
Charlie Reis7e2cb6d2021-01-26 01:27:162177 // Note: The pending entry will usually have a real ReloadType here, but
2178 // it can still be ReloadType::NONE in cases that
2179 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
2180
2181 // If we classified this correctly, the SiteInstance should not have
2182 // changed.
2183 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
2184
2185 // For converted reloads, we assign the entry's unique ID to be that of
2186 // the new one. Since this is always the result of a user action, we want
2187 // to dismiss infobars, etc. like a regular user-initiated navigation.
2188 entry->set_unique_id(pending_entry_->GetUniqueID());
2189
2190 // The extra headers may have changed due to reloading with different
2191 // headers.
2192 entry->set_extra_headers(pending_entry_->extra_headers());
2193 }
2194 // Otherwise, this was intended as a new entry but the pending entry was
2195 // lost in the meantime and no new entry was created. We are stuck at the
2196 // last committed entry.
2197
2198 // Even if this is a converted reload from pressing enter in the omnibox,
2199 // the server could redirect, requiring an update to the SSL status. If this
2200 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:452201 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:162202 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:472203 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:452204 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:162205 }
Rakina Zata Amnif6950d552020-11-24 03:26:102206 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:112207 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:102208 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b902016-09-01 16:58:162209
eugenebut604866f2017-05-10 21:35:362210 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:452211 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:362212 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
2213 // this was a restored same document navigation entry, then it won't have
2214 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
2215 // navigated it.
jam48cea9082017-02-15 06:13:292216 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
Mike West800532c2021-10-14 09:26:522217 if (entry->GetURL().DeprecatedGetOriginAsURL() ==
2218 last_entry->GetURL().DeprecatedGetOriginAsURL() &&
jam48cea9082017-02-15 06:13:292219 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
2220 was_restored) {
2221 entry->GetSSL() = last_entry->GetSSL();
2222 }
2223 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:452224 // In rapid back/forward navigations |request| sometimes won't have a cert
2225 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:192226 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:452227 if (request->GetSSLInfo().has_value() &&
2228 request->GetSSLInfo()->is_valid()) {
2229 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
Mike West800532c2021-10-14 09:26:522230 } else if (entry->GetURL().DeprecatedGetOriginAsURL() !=
2231 request->GetURL().DeprecatedGetOriginAsURL()) {
John Abd-El-Malek3f247082017-12-07 19:02:192232 entry->GetSSL() = SSLStatus();
2233 }
jam48cea9082017-02-15 06:13:292234 }
avicbdc4c12015-07-01 16:07:112235 } else {
Feifei Wang2ab8ba6c2022-04-13 22:19:272236 // This is renderer-initiated. The only kinds of renderer-initiated
Rakina Zata Amni557afb92021-07-17 04:39:572237 // navigations that are EXISTING_ENTRY are same-document navigations that
2238 // result in replacement (e.g. history.replaceState(), location.replace(),
2239 // forced replacements for trivial session history contexts). For these
2240 // cases, we reuse the last committed entry.
avicbdc4c12015-07-01 16:07:112241 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:102242
Mikel Astizba9cf2fd2017-12-17 10:38:102243 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
2244 // to avoid misleading interpretations (e.g. URLs paired with
2245 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
2246 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
2247 // | PAGE_TRANSITION_CLIENT_REDIRECT).
2248
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572249 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:102250
eugenebut604866f2017-05-10 21:35:362251 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:452252 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:362253 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:472254 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:452255 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
avicbdc4c12015-07-01 16:07:112256 }
2257 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:432258
Rakina Zata Amni3460d382021-10-29 00:43:372259 UpdateNavigationEntryDetails(entry, rfh, params, request,
2260 NavigationEntryImpl::UpdatePolicy::kUpdate,
Rakina Zata Amnia4e27222021-12-22 01:05:002261 false /* is_new_entry */, commit_details);
creis22a7b4c2016-04-28 07:20:302262
[email protected]5ccd4dc2012-10-24 02:28:142263 // The redirected to page should not inherit the favicon from the previous
2264 // page.
eugenebut604866f2017-05-10 21:35:362265 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:482266 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:142267
Charlie Reis951f43372023-05-05 00:30:072268 // Update the last committed index to reflect the committed entry. Do this
2269 // before calling DiscardNonCommittedEntriesWithCommitDetails, so that the
2270 // delegate sees the correct committed index when notified of navigation
2271 // state changes. (Otherwise CanGoBack may incorrectly return true, as in
2272 // https://crbug.com/1439948.)
Charlie Reisfbe5f1022023-10-03 15:21:212273 last_committed_entry_index_ = GetIndexOfEntry(entry);
Charlie Reis951f43372023-05-05 00:30:072274
Peter Boströmd7592132019-01-30 04:50:312275 // We should also usually discard the pending entry if it corresponds to a
2276 // different navigation, since that one is now likely canceled. In rare
2277 // cases, we leave the pending entry for another navigation in place when we
2278 // know it is still ongoing, to avoid a flicker in the omnibox (see
2279 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:432280 //
2281 // Note that we need to use the "internal" version since we don't want to
2282 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:312283 if (!keep_pending_entry)
Rakina Zata Amnia4e27222021-12-22 01:05:002284 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]e9ba4472008-09-14 15:42:432285}
2286
[email protected]d202a7c2012-01-04 07:53:472287void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:322288 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072289 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:362290 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:472291 bool replace_entry,
Shivani Sharmaeef521b2024-01-18 13:03:562292 bool previous_document_had_history_intervention_activation,
Rakina Zata Amnia4e27222021-12-22 01:05:002293 NavigationRequest* request,
2294 LoadCommittedDetails* commit_details) {
avi25f5f9e2015-07-17 20:08:262295 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2296 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
Rakina Zata Amniddf10502022-01-15 02:56:552297 // The NEW_SUBFRAME path should never result in an initial NavigationEntry.
2298 DCHECK(!commit_details->should_stay_as_initial_entry);
[email protected]09b8f82f2009-06-16 20:22:112299
[email protected]e9ba4472008-09-14 15:42:432300 // Manual subframe navigations just get the current entry cloned so the user
2301 // can go back or forward to it. The actual subframe information will be
2302 // stored in the page state for each of those entries. This happens out of
2303 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:092304 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
2305 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:382306
Mikel Astizba9cf2fd2017-12-17 10:38:102307 // The DCHECK below documents the fact that we don't know of any situation
2308 // where |replace_entry| is true for subframe navigations. This simplifies
2309 // reasoning about the replacement struct for subframes (see
2310 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
2311 DCHECK(!replace_entry);
2312
Patrick Monette50e8bd82019-06-13 22:40:452313 // This FrameNavigationEntry might not end up being used in the
2314 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Arthur Sonzognic686e8f2024-01-11 08:36:372315 const std::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452316 request->common_params().initiator_origin;
Arthur Sonzognic686e8f2024-01-11 08:36:372317 std::optional<GURL> initiator_base_url;
W. James MacLean8be423a2023-03-31 21:35:522318 if (params.url.IsAboutBlank() || params.url.IsAboutSrcdoc()) {
2319 initiator_base_url = request->common_params().initiator_base_url;
2320 }
Nate Chapin63db0d12022-01-20 22:03:302321 std::unique_ptr<PolicyContainerPolicies> policy_container_policies =
2322 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
2323 request->GetURL());
Domenic Denicolacc094fb2022-03-16 23:40:572324 bool protect_url_in_navigation_api = false;
Nate Chapin63db0d12022-01-20 22:03:302325 if (is_same_document) {
2326 FrameNavigationEntry* previous_frame_entry =
2327 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
Domenic Denicolacc094fb2022-03-16 23:40:572328 // Try to preserve protect_url_in_navigation_api from the previous
Nate Chapin63db0d12022-01-20 22:03:302329 // FrameNavigationEntry.
Domenic Denicolacc094fb2022-03-16 23:40:572330 protect_url_in_navigation_api =
Nate Chapin63db0d12022-01-20 22:03:302331 previous_frame_entry &&
Domenic Denicolacc094fb2022-03-16 23:40:572332 previous_frame_entry->protect_url_in_navigation_api();
Nate Chapin63db0d12022-01-20 22:03:302333 } else {
Domenic Denicolacc094fb2022-03-16 23:40:572334 protect_url_in_navigation_api =
Nate Chapin63db0d12022-01-20 22:03:302335 policy_container_policies &&
Domenic Denicolacc094fb2022-03-16 23:40:572336 ShouldProtectUrlInNavigationApi(
Nate Chapin63db0d12022-01-20 22:03:302337 policy_container_policies->referrer_policy);
2338 }
2339
Patrick Monette50e8bd82019-06-13 22:40:452340 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:482341 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Domenic Denicolacc094fb2022-03-16 23:40:572342 params.document_sequence_number, params.navigation_api_key,
Nate Chapinfbfe5af2021-06-10 17:22:082343 rfh->GetSiteInstance(), nullptr, params.url,
2344 GetCommittedOriginForFrameEntry(params, request),
W. James MacLean23e90a12022-12-21 04:38:212345 Referrer(*params.referrer), initiator_origin, initiator_base_url,
2346 request->GetRedirectChain(), params.page_state, params.method,
2347 params.post_id, nullptr /* blob_url_loader_factory */,
Domenic Denicolacc094fb2022-03-16 23:40:572348 std::move(policy_container_policies), protect_url_in_navigation_api);
Charles Reisf44482022017-10-13 21:15:032349
creisce0ef3572017-01-26 17:53:082350 std::unique_ptr<NavigationEntryImpl> new_entry =
2351 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:452352 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Ali Hijazid87307d2022-11-07 20:15:032353 frame_tree_->root());
creise062d542015-08-25 02:01:552354
Alexander Timine3ec4192020-04-20 16:39:402355 SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaeef521b2024-01-18 13:03:562356 replace_entry, previous_document_had_history_intervention_activation,
Alexander Timine3ec4192020-04-20 16:39:402357 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:472358
creisce0ef3572017-01-26 17:53:082359 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:452360 // to replace, which can happen due to a unique name change. See
2361 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
2362 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:382363
Dave Tapuska87696ae2021-11-18 18:48:312364 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false,
Abhijeet Kandalkare26014a92022-10-13 04:21:152365 rfh->IsNestedWithinFencedFrame(), commit_details);
[email protected]e9ba4472008-09-14 15:42:432366}
2367
[email protected]d202a7c2012-01-04 07:53:472368bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:322369 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072370 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392371 bool is_same_document,
Nate Chapinc7019dd7d2021-06-25 18:29:252372 bool was_on_initial_empty_document,
Rakina Zata Amnia4e27222021-12-22 01:05:002373 NavigationRequest* request,
2374 LoadCommittedDetails* commit_details) {
avi9f07a0c2015-02-18 22:51:292375 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2376 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2377
[email protected]e9ba4472008-09-14 15:42:432378 // We're guaranteed to have a previously committed entry, and we now need to
2379 // handle navigation inside of a subframe in it without creating a new entry.
2380 DCHECK(GetLastCommittedEntry());
2381
creis913c63ce2016-07-16 19:52:522382 // For newly created subframes, we don't need to send a commit notification.
2383 // This is only necessary for history navigations in subframes.
2384 bool send_commit_notification = false;
2385
Rakina Zata Amnif6950d552020-11-24 03:26:102386 // If |nav_entry_id| is non-zero and matches an existing entry, this
2387 // is a history navigation. Update the last committed index accordingly. If
2388 // we don't recognize the |nav_entry_id|, it might be a recently
2389 // pruned entry. We'll handle it below.
2390 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2391 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472392 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062393 // Make sure that a subframe commit isn't changing the main frame's
2394 // origin. Otherwise the renderer process may be confused, leading to a
2395 // URL spoof. We can't check the path since that may change
2396 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572397 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2398 // about:blank, file, and unique origins are more subtle to get right.
Charlie Reis95fbca442021-05-21 21:38:242399 // We should use checks similar to RenderFrameHostImpl's
2400 // CanCommitUrlAndOrigin on the main frame during subframe commits.
2401 // See https://crbug.com/1209092.
creis37988b92016-06-10 18:03:572402 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2403 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2404 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2405 dest_top_url.SchemeIsHTTPOrHTTPS() &&
Mike West800532c2021-10-14 09:26:522406 current_top_url.DeprecatedGetOriginAsURL() !=
2407 dest_top_url.DeprecatedGetOriginAsURL()) {
Chris Bookholt10f4b7332022-02-14 18:25:442408 bad_message::ReceivedBadMessage(rfh->GetMainFrame()->GetProcess(),
creisfb6eeb62016-05-10 19:01:512409 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062410 }
creis3cdc3b02015-05-29 23:00:472411
creis913c63ce2016-07-16 19:52:522412 // We only need to discard the pending entry in this history navigation
2413 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062414 last_committed_entry_index_ = entry_index;
Rakina Zata Amnia4e27222021-12-22 01:05:002415 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
creis913c63ce2016-07-16 19:52:522416
2417 // History navigations should send a commit notification.
2418 send_commit_notification = true;
avi98405c22015-05-21 20:47:062419 }
[email protected]e9ba4472008-09-14 15:42:432420 }
[email protected]f233e4232013-02-23 00:55:142421
creisce0ef3572017-01-26 17:53:082422 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2423 // it may be a "history auto" case where we update an existing one.
Nate Chapin9f169072021-06-09 19:32:372424 // We may want to update |last_committed|'s FrameNavigationEntry (if one
2425 // exists), or we may want to clobber it and create a new one. We update in
2426 // cases where the corresponding FrameNavigationEntry is conceptually similar
2427 // to the document described by the commit params: same-document
2428 // navigations, history traversal to an existing entry, and reloads (including
2429 // a "soft reload" where we navigate to the same url without flagging it as a
2430 // reload). But in the case of a different document that is not logically
2431 // related to the committed FrameNavigationEntry's document (cross-document,
2432 // not same url, not a reload, not a history traversal), we replace rather
2433 // than update.
Nate Chapinc7019dd7d2021-06-25 18:29:252434 //
Nate Chapin9f169072021-06-09 19:32:372435 // In the case where we update, the FrameNavigationEntry will potentially be
2436 // shared across multiple NavigationEntries, and any updates will be reflected
2437 // in all of those NavigationEntries. In the replace case, any existing
2438 // sharing with other NavigationEntries will stop.
Nate Chapinc7019dd7d2021-06-25 18:29:252439 //
2440 // When navigating away from the initial empty document, we also update rather
2441 // than replace. Either update or replace will overwrite the initial empty
2442 // document state for |last_committed|, but if the FrameNavigationEntry for
2443 // the initial empty document is shared across multiple NavigationEntries (due
2444 // to a navigation in another frame), we want to make sure we overwrite the
2445 // initial empty document state everywhere this FrameNavigationEntry is used,
2446 // which is accompished by updating the existing FrameNavigationEntry.
Rakina Zata Amnie2d31312022-11-18 03:38:452447 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Nate Chapin9f169072021-06-09 19:32:372448 FrameNavigationEntry* last_committed_frame_entry =
2449 last_committed->GetFrameEntry(rfh->frame_tree_node());
2450 NavigationEntryImpl::UpdatePolicy update_policy =
2451 NavigationEntryImpl::UpdatePolicy::kUpdate;
2452 if (request->common_params().navigation_type ==
Minggang Wangb9f3fa92021-07-01 15:30:312453 blink::mojom::NavigationType::DIFFERENT_DOCUMENT &&
Nate Chapin9f169072021-06-09 19:32:372454 last_committed_frame_entry &&
Nate Chapinc7019dd7d2021-06-25 18:29:252455 last_committed_frame_entry->url() != params.url &&
2456 !was_on_initial_empty_document) {
Nate Chapin9f169072021-06-09 19:32:372457 update_policy = NavigationEntryImpl::UpdatePolicy::kReplace;
2458 }
2459
Rakina Zata Amni3460d382021-10-29 00:43:372460 UpdateNavigationEntryDetails(last_committed, rfh, params, request,
Rakina Zata Amnia4e27222021-12-22 01:05:002461 update_policy, false /* is_new_entry */,
2462 commit_details);
creis625a0c7d2015-03-24 23:17:122463
creis913c63ce2016-07-16 19:52:522464 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432465}
2466
[email protected]d202a7c2012-01-04 07:53:472467int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232468 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032469 for (size_t i = 0; i < entries_.size(); ++i) {
2470 if (entries_[i].get() == entry)
2471 return i;
2472 }
2473 return -1;
[email protected]765b35502008-08-21 00:51:202474}
2475
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572476void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242477 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572478 NavigationControllerImpl* source =
2479 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572480 // Verify that we look new.
Rakina Zata Amni46087a12022-11-11 08:28:382481 DCHECK_EQ(1, GetEntryCount());
2482 DCHECK(GetLastCommittedEntry()->IsInitialEntry());
Lei Zhang96031532019-10-10 19:05:472483 DCHECK(!GetPendingEntry());
Rakina Zata Amniafd3c6582021-11-30 06:19:172484 entries_.clear();
[email protected]ce3fa3c2009-04-20 19:55:572485
Francois Dorayeaace782017-06-21 16:37:242486 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442487 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572488 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572489
Fergal Dalya1d569972021-03-16 03:24:532490 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302491 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532492 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2493 session_storage_namespace_map_[it.first] = source_namespace->Clone();
[email protected]fdac6ade2013-07-20 01:06:302494 }
[email protected]4e6419c2010-01-15 04:50:342495
Lukasz Anforowicz0de0f452020-12-02 19:57:152496 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572497}
2498
Aran Gilman37d11632019-10-08 23:07:152499void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2500 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162501 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012502 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162503
[email protected]d202a7c2012-01-04 07:53:472504 NavigationControllerImpl* source =
2505 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252506
avi2b177592014-12-10 02:08:022507 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012508 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252509
[email protected]474f8512013-05-31 22:31:162510 // We now have one entry, possibly with a new pending entry. Ensure that
2511 // adding the entries from source won't put us over the limit.
2512 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572513 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572514 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252515
Carlos IL4dea8902020-05-26 15:14:292516 // Insert the entries from source. Ignore any pending entry, since it has not
2517 // committed in source.
[email protected]474f8512013-05-31 22:31:162518 int max_source_index = source->last_committed_entry_index_;
Rakina Zata Amniafd3c6582021-11-30 06:19:172519 DCHECK_NE(max_source_index, -1);
2520 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572521
2522 // Ignore the source's current entry if merging with replacement.
2523 // TODO(davidben): This should preserve entries forward of the current
2524 // too. http://crbug.com/317872
2525 if (replace_entry && max_source_index > 0)
2526 max_source_index--;
2527
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572528 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252529
2530 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552531 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142532
Hayato Ito2c8c08d02021-06-23 03:38:432533 BroadcastHistoryOffsetAndLength();
[email protected]e1cd5452010-08-26 18:03:252534}
2535
[email protected]79368982013-11-13 01:11:012536bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162537 // If there is no last committed entry, we cannot prune. Even if there is a
2538 // pending entry, it may not commit, leaving this WebContents blank, despite
2539 // possibly giving it new entries via CopyStateFromAndPrune.
2540 if (last_committed_entry_index_ == -1)
2541 return false;
[email protected]9350602e2013-02-26 23:27:442542
[email protected]474f8512013-05-31 22:31:162543 // We cannot prune if there is a pending entry at an existing entry index.
2544 // It may not commit, so we have to keep the last committed entry, and thus
2545 // there is no sensible place to keep the pending entry. It is ok to have
2546 // a new pending entry, which can optionally commit as a new navigation.
2547 if (pending_entry_index_ != -1)
2548 return false;
2549
[email protected]474f8512013-05-31 22:31:162550 return true;
2551}
2552
[email protected]79368982013-11-13 01:11:012553void NavigationControllerImpl::PruneAllButLastCommitted() {
2554 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162555
avi2b177592014-12-10 02:08:022556 DCHECK_EQ(0, last_committed_entry_index_);
2557 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442558
Hayato Ito2c8c08d02021-06-23 03:38:432559 BroadcastHistoryOffsetAndLength();
[email protected]9350602e2013-02-26 23:27:442560}
2561
[email protected]79368982013-11-13 01:11:012562void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162563 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012564 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262565
Nate Chapin9eb16be72022-09-23 22:54:312566 RemovedEntriesTracker tracker(weak_factory_.GetSafeRef());
2567
[email protected]474f8512013-05-31 22:31:162568 // Erase all entries but the last committed entry. There may still be a
2569 // new pending entry after this.
2570 entries_.erase(entries_.begin(),
2571 entries_.begin() + last_committed_entry_index_);
2572 entries_.erase(entries_.begin() + 1, entries_.end());
2573 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262574}
2575
Christian Dullweber1af31e62018-02-22 11:49:482576void NavigationControllerImpl::DeleteNavigationEntries(
2577 const DeletionPredicate& deletionPredicate) {
2578 // It is up to callers to check the invariants before calling this.
2579 CHECK(CanPruneAllButLastCommitted());
2580 std::vector<int> delete_indices;
2581 for (size_t i = 0; i < entries_.size(); i++) {
2582 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572583 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482584 delete_indices.push_back(i);
2585 }
2586 }
2587 if (delete_indices.empty())
2588 return;
2589
2590 if (delete_indices.size() == GetEntryCount() - 1U) {
2591 PruneAllButLastCommitted();
2592 } else {
2593 // Do the deletion in reverse to preserve indices.
Ayu Ishii2f825852022-03-08 19:47:382594 for (const auto& index : base::Reversed(delete_indices)) {
2595 RemoveEntryAtIndex(index);
Christian Dullweber1af31e62018-02-22 11:49:482596 }
Hayato Ito2c8c08d02021-06-23 03:38:432597 BroadcastHistoryOffsetAndLength();
Christian Dullweber1af31e62018-02-22 11:49:482598 }
2599 delegate()->NotifyNavigationEntriesDeleted();
2600}
2601
Carlos Caballero35ce710c2019-09-19 10:59:452602BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2603 return back_forward_cache_;
2604}
2605
William Liu055a3542023-04-02 17:21:192606NavigationEntryScreenshotCache*
2607NavigationControllerImpl::GetNavigationEntryScreenshotCache() {
Takashi Toyoshima7c041d82023-09-26 16:09:212608 CHECK(frame_tree_->is_primary());
William Liu055a3542023-04-02 17:21:192609 if (!nav_entry_screenshot_cache_ && AreBackForwardTransitionsEnabled()) {
2610 nav_entry_screenshot_cache_ =
2611 std::make_unique<NavigationEntryScreenshotCache>(
2612 BrowserContextImpl::From(browser_context_)
2613 ->GetNavigationEntryScreenshotManager()
2614 ->GetSafeRef(),
2615 this);
2616 }
2617 return nav_entry_screenshot_cache_.get();
2618}
2619
clamy987a3752018-05-03 17:36:262620void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2621 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2622 // progress, since this will cause a use-after-free. (We only allow this
2623 // when the tab is being destroyed for shutdown, since it won't return to
2624 // NavigateToEntry in that case.) http://crbug.com/347742.
Ali Hijazid87307d2022-11-07 20:15:032625 CHECK(!in_navigate_to_pending_entry_ || frame_tree_->IsBeingDestroyed());
clamy987a3752018-05-03 17:36:262626
2627 if (was_failure && pending_entry_) {
2628 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2629 } else {
2630 failed_pending_entry_id_ = 0;
2631 }
2632
2633 if (pending_entry_) {
2634 if (pending_entry_index_ == -1)
Paul Semel7e51469e2022-07-12 12:16:332635 pending_entry_.ClearAndDelete();
clamy987a3752018-05-03 17:36:262636 pending_entry_index_ = -1;
2637 pending_entry_ = nullptr;
2638 }
arthursonzogni66f711c2019-10-08 14:40:362639
2640 // Ensure any refs to the current pending entry are ignored if they get
2641 // deleted, by clearing the set of known refs. All future pending entries will
2642 // only be affected by new refs.
2643 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262644}
2645
2646void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2647 if (pending_entry_)
2648 pending_entry_->set_ssl_error(error);
2649}
2650
Xiaohan Wang7f8052e02022-01-14 18:44:282651#if BUILDFLAG(IS_ANDROID)
Camille Lamy5193caa2018-10-12 11:59:422652// static
2653bool NavigationControllerImpl::ValidateDataURLAsString(
2654 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2655 if (!data_url_as_string)
2656 return false;
2657
2658 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2659 return false;
2660
2661 // The number of characters that is enough for validating a data: URI.
2662 // From the GURL's POV, the only important part here is scheme, it doesn't
2663 // check the actual content. Thus we can take only the prefix of the url, to
2664 // avoid unneeded copying of a potentially long string.
2665 const size_t kDataUriPrefixMaxLen = 64;
2666 GURL data_url(
2667 std::string(data_url_as_string->front_as<char>(),
2668 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2669 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2670 return false;
2671
2672 return true;
2673}
2674#endif
2675
Shivani Sharma194877032019-03-07 17:52:472676void NavigationControllerImpl::NotifyUserActivation() {
2677 // When a user activation occurs, ensure that all adjacent entries for the
2678 // same document clear their skippable bit, so that the history manipulation
2679 // intervention does not apply to them.
Lijin Shen9c475d32023-09-02 00:15:012680 const bool can_go_back = CanGoBack();
Shivani Sharmac4cc8922019-04-18 03:11:172681 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Lijin Shen9c475d32023-09-02 00:15:012682 // If the value of CanGoBack changes as a result of making some entries
2683 // non-skippable, then we must let the delegate know to update its UI state.
2684 // See https://crbug.com/1477784.
2685 if (!can_go_back && CanGoBack()) {
2686 delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_ALL);
2687 }
Shivani Sharma194877032019-03-07 17:52:472688}
2689
clamy987a3752018-05-03 17:36:262690bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2691 RenderFrameHostImpl* render_frame_host,
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:332692 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client,
2693 blink::LocalFrameToken initiator_frame_token,
2694 int initiator_process_id) {
clamy987a3752018-05-03 17:36:262695 NavigationEntryImpl* entry =
2696 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2697 if (!entry)
2698 return false;
2699
2700 FrameNavigationEntry* frame_entry =
2701 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2702 if (!frame_entry)
2703 return false;
2704
Camille Lamy5193caa2018-10-12 11:59:422705 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572706 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232707 ReloadType::NONE, false /* is_same_document_history_load */,
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:332708 true /* is_history_navigation_in_new_child */, initiator_frame_token,
2709 initiator_process_id);
clamyea99ea12018-05-28 13:54:232710
2711 if (!request)
2712 return false;
2713
arthursonzognif046d4a2019-12-12 19:08:102714 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412715
Rakina Zata Amni1c83b082023-02-08 01:09:002716 SCOPED_CRASH_KEY_STRING256(
2717 "Bug1400009", "req_url",
2718 request->GetURL().GetWithEmptyPath().possibly_invalid_spec());
2719 SCOPED_CRASH_KEY_NUMBER(
2720 "Bug1400009", "nav_entry_si",
2721 entry->site_instance() ? ((int)entry->site_instance()->GetId()) : -1);
2722 SCOPED_CRASH_KEY_NUMBER("Bug1400009", "fne_si",
2723 frame_entry->site_instance()
2724 ? ((int)frame_entry->site_instance()->GetId())
2725 : -1);
2726 bool has_sig =
2727 (frame_entry->site_instance() && frame_entry->site_instance()->group());
2728 SCOPED_CRASH_KEY_BOOL("Bug1400009", "fne_sig_exists", has_sig);
2729 SCOPED_CRASH_KEY_BOOL("Bug1400009", "fne_sig_has_rvh",
2730 has_sig ? (!!frame_tree_->GetRenderViewHost(
2731 frame_entry->site_instance()->group()))
2732 : false);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052733 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2734 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232735
2736 return true;
clamy987a3752018-05-03 17:36:262737}
2738
Tsuyoshi Horo52fd08e2020-07-07 07:03:452739bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2740 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2741 if (!entry)
2742 return false;
Rakina Zata Amnif297a802022-01-18 03:53:432743
2744 if (entry->IsInitialEntryNotForSynchronousAboutBlank()) {
2745 // We should never navigate to an existing initial NavigationEntry that is
2746 // the initial NavigationEntry for the initial empty document that hasn't
2747 // been overridden by the synchronous about:blank commit, to preserve
2748 // legacy behavior where trying to reload when the main frame is on the
2749 // initial empty document won't result in a navigation. See also
2750 // https://crbug.com/1277414.
2751 return false;
2752 }
Tsuyoshi Horo52fd08e2020-07-07 07:03:452753 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2754 if (!frame_entry)
2755 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142756 ReloadType reload_type = ReloadType::NORMAL;
2757 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452758 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142759 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452760 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432761 false /* is_history_navigation_in_new_child */,
Arthur Sonzognic686e8f2024-01-11 08:36:372762 std::nullopt /* initiator_frame_token */,
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:332763 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452764 if (!request)
2765 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052766 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452767 return true;
2768}
2769
clamy987a3752018-05-03 17:36:262770void NavigationControllerImpl::NavigateFromFrameProxy(
2771 RenderFrameHostImpl* render_frame_host,
2772 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022773 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452774 int initiator_process_id,
Arthur Sonzognic686e8f2024-01-11 08:36:372775 const std::optional<url::Origin>& initiator_origin,
2776 const std::optional<GURL>& initiator_base_url,
clamy987a3752018-05-03 17:36:262777 bool is_renderer_initiated,
2778 SiteInstance* source_site_instance,
2779 const Referrer& referrer,
2780 ui::PageTransition page_transition,
2781 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252782 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262783 const std::string& method,
2784 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092785 const std::string& extra_headers,
Antonio Sartori2f763d9d2021-04-21 10:04:142786 network::mojom::SourceLocationPtr source_location,
John Delaney50425f82020-04-07 16:26:212787 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
jongdeok.kim5de823b32022-06-14 04:37:502788 bool is_form_submission,
Arthur Sonzognic686e8f2024-01-11 08:36:372789 const std::optional<blink::Impression>& impression,
Yao Xiao720ef9d62022-12-09 05:18:292790 blink::mojom::NavigationInitiatorActivationAndAdStatus
2791 initiator_activation_and_ad_status,
Nan Lin944e9b4e2022-04-12 13:51:222792 base::TimeTicks navigation_start_time,
Garrett Tanzer405f3402022-07-21 20:12:492793 bool is_embedder_initiated_fenced_frame_navigation,
Garrett Tanzerbb8db412022-09-27 21:59:462794 bool is_unfenced_top_navigation,
Sergey Poromovdd557c12023-03-01 11:28:452795 bool force_new_browsing_instance,
Camillia Smith Barnes6a643962023-03-03 00:28:582796 bool is_container_initiated,
Arthur Sonzognic686e8f2024-01-11 08:36:372797 std::optional<std::u16string> embedder_shared_storage_context) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582798 if (is_renderer_initiated)
2799 DCHECK(initiator_origin.has_value());
2800
clamy987a3752018-05-03 17:36:262801 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582802
Rakina Zata Amni2322f4f82022-01-24 13:24:242803 // Don't allow an entry replacement if there is no entry to replace.
2804 // http://crbug.com/457149
2805 if (GetEntryCount() == 0)
2806 should_replace_current_entry = false;
2807
clamy987a3752018-05-03 17:36:262808 // Create a NavigationEntry for the transfer, without making it the pending
2809 // entry. Subframe transfers should have a clone of the last committed entry
2810 // with a FrameNavigationEntry for the target frame. Main frame transfers
2811 // should have a new NavigationEntry.
2812 // TODO(creis): Make this unnecessary by creating (and validating) the params
2813 // directly, passing them to the destination RenderFrameHost. See
2814 // https://crbug.com/536906.
2815 std::unique_ptr<NavigationEntryImpl> entry;
Harkiran Bolariae1b5158b2021-09-16 19:03:262816 if (!render_frame_host->is_main_frame()) {
clamy987a3752018-05-03 17:36:262817 // Subframe case: create FrameNavigationEntry.
Rakina Zata Amnie2d31312022-11-18 03:38:452818 DCHECK(GetLastCommittedEntry());
2819 entry = GetLastCommittedEntry()->Clone();
2820 entry->set_extra_headers(extra_headers);
Rakina Zata Amniafd3c6582021-11-30 06:19:172821 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2822 // Renderer-initiated navigation that target a remote frame are currently
2823 // classified as browser-initiated when this one has already navigated.
2824 // See https://crbug.com/722251.
Nate Chapin9f169072021-06-09 19:32:372825 // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter
2826 // to CreateNavigationRequestFromLoadParams(), so while kReplace might
2827 // remove child FrameNavigationEntries (e.g., if this is a cross-process
2828 // same-document navigation), they will still be present in the
2829 // committed NavigationEntry that will be referenced to construct the new
2830 // FrameNavigationEntry tree when this navigation commits.
clamy987a3752018-05-03 17:36:262831 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:082832 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582833 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Arthur Sonzognic686e8f2024-01-11 08:36:372834 std::nullopt /* commit_origin */, referrer, initiator_origin,
W. James MacLean23e90a12022-12-21 04:38:212835 initiator_base_url, std::vector<GURL>(), blink::PageState(), method, -1,
Kunihiko Sakamoto2ae79e62023-05-26 00:34:152836 blob_url_loader_factory, nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262837 } else {
2838 // Main frame case.
Julie Jeongeun Kim5b9aff72022-05-02 02:10:172839 // If `node` is the outermost main frame, it rewrites a virtual url in order
2840 // to adjust the original input url if needed. For inner frames such as
2841 // fenced frames or subframes, they don't rewrite urls as the urls are not
2842 // input urls by users.
2843 bool rewrite_virtual_urls = node->IsOutermostMainFrame();
Arthur Sonzognic686e8f2024-01-11 08:36:372844 std::optional<GURL> source_process_site_url = std::nullopt;
Sharon Yang242ef822023-05-15 21:07:322845 if (source_site_instance && source_site_instance->HasProcess()) {
2846 source_process_site_url =
2847 source_site_instance->GetProcess()->GetProcessLock().site_url();
2848 }
clamy987a3752018-05-03 17:36:262849 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
W. James MacLean23e90a12022-12-21 04:38:212850 url, referrer, initiator_origin, initiator_base_url,
Sharon Yang242ef822023-05-15 21:07:322851 source_process_site_url, page_transition, is_renderer_initiated,
W. James MacLean23e90a12022-12-21 04:38:212852 extra_headers, browser_context_, blob_url_loader_factory,
2853 rewrite_virtual_urls));
clamy987a3752018-05-03 17:36:262854 entry->root_node()->frame_entry->set_source_site_instance(
2855 static_cast<SiteInstanceImpl*>(source_site_instance));
2856 entry->root_node()->frame_entry->set_method(method);
2857 }
clamy987a3752018-05-03 17:36:262858
Camille Lamy5193caa2018-10-12 11:59:422859 bool override_user_agent = false;
Rakina Zata Amnie2d31312022-11-18 03:38:452860 if (GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
clamy987a3752018-05-03 17:36:262861 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422862 override_user_agent = true;
clamy987a3752018-05-03 17:36:262863 }
2864 // TODO(creis): Set user gesture and intent received timestamp on Android.
2865
2866 // We may not have successfully added the FrameNavigationEntry to |entry|
2867 // above (per https://crbug.com/608402), in which case we create it from
2868 // scratch. This works because we do not depend on |frame_entry| being inside
2869 // |entry| during NavigateToEntry. This will go away when we shortcut this
2870 // further in https://crbug.com/536906.
2871 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2872 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452873 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Nate Chapinfbfe5af2021-06-10 17:22:082874 node->unique_name(), -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582875 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Arthur Sonzognic686e8f2024-01-11 08:36:372876 std::nullopt /* origin */, referrer, initiator_origin,
W. James MacLean23e90a12022-12-21 04:38:212877 initiator_base_url, std::vector<GURL>(), blink::PageState(), method, -1,
Kunihiko Sakamoto2ae79e62023-05-26 00:34:152878 blob_url_loader_factory, nullptr /* policy_container_policies */,
Domenic Denicolacc094fb2022-03-16 23:40:572879 false /* protect_url_in_navigation_api */);
clamy987a3752018-05-03 17:36:262880 }
2881
Camille Lamy5193caa2018-10-12 11:59:422882 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022883 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452884 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292885 params.initiator_origin = initiator_origin;
W. James MacLean23e90a12022-12-21 04:38:212886 params.initiator_base_url = initiator_base_url;
Camille Lamy5193caa2018-10-12 11:59:422887 params.source_site_instance = source_site_instance;
2888 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2889 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032890 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422891 params.referrer = referrer;
2892 /* params.redirect_chain: skip */
2893 params.extra_headers = extra_headers;
2894 params.is_renderer_initiated = is_renderer_initiated;
2895 params.override_user_agent = UA_OVERRIDE_INHERIT;
2896 /* params.base_url_for_data_url: skip */
2897 /* params.virtual_url_for_data_url: skip */
2898 /* params.data_url_as_string: skip */
2899 params.post_data = post_body;
2900 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582901 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422902 /* params.frame_name: skip */
2903 // TODO(clamy): See if user gesture should be propagated to this function.
2904 params.has_user_gesture = false;
2905 params.should_clear_history_list = false;
2906 params.started_from_context_menu = false;
2907 /* params.navigation_ui_data: skip */
2908 /* params.input_start: skip */
Minggang Wangf59db47b2021-06-16 01:56:222909 params.was_activated = blink::mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542910 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212911 params.impression = impression;
Antonio Sartori6984c742021-08-26 08:03:412912 params.download_policy = std::move(download_policy);
jongdeok.kim5de823b32022-06-14 04:37:502913 params.is_form_submission = is_form_submission;
Yao Xiao720ef9d62022-12-09 05:18:292914 params.initiator_activation_and_ad_status =
2915 initiator_activation_and_ad_status;
Camille Lamy5193caa2018-10-12 11:59:422916
2917 std::unique_ptr<NavigationRequest> request =
2918 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032919 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:142920 false /* has_user_gesture */, std::move(source_location),
Tsuyoshi Horo167ca6432022-03-09 05:16:392921 ReloadType::NONE, entry.get(), frame_entry.get(),
Garrett Tanzer405f3402022-07-21 20:12:492922 navigation_start_time, is_embedder_initiated_fenced_frame_navigation,
Camillia Smith Barnes6a643962023-03-03 00:28:582923 is_unfenced_top_navigation, is_container_initiated,
2924 embedder_shared_storage_context);
clamyea99ea12018-05-28 13:54:232925
2926 if (!request)
2927 return;
2928
Garrett Tanzerbb8db412022-09-27 21:59:462929 // Force the navigation to take place in a new browsing instance.
2930 // This is used by _unfencedTop in fenced frames to ensure that navigations
2931 // leaving the fenced context create a new browsing instance.
2932 if (force_new_browsing_instance) {
2933 request->coop_status().ForceBrowsingInstanceSwap();
2934 }
2935
Arthur Hemery948742762019-09-18 10:06:242936 // At this stage we are proceeding with this navigation. If this was renderer
2937 // initiated with user gesture, we need to make sure we clear up potential
2938 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2939 DiscardNonCommittedEntries();
2940
Lukasz Anforowicz9ee83c272020-12-01 20:14:052941 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262942}
2943
[email protected]d1198fd2012-08-13 22:50:192944void NavigationControllerImpl::SetSessionStorageNamespace(
Alex Moshchuk8015afcf2022-01-31 22:59:252945 const StoragePartitionConfig& partition_config,
[email protected]8ff00d72012-10-23 19:12:212946 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192947 if (!session_storage_namespace)
2948 return;
2949
2950 // We can't overwrite an existing SessionStorage without violating spec.
2951 // Attempts to do so may give a tab access to another tab's session storage
2952 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152953 bool successful_insert =
2954 session_storage_namespace_map_
Alex Moshchuk8015afcf2022-01-31 22:59:252955 .insert(std::make_pair(partition_config,
Aaron Colwellf3b316e2021-03-11 20:17:052956 static_cast<SessionStorageNamespaceImpl*>(
2957 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302958 .second;
2959 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
[email protected]d1198fd2012-08-13 22:50:192960}
2961
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572962bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Rakina Zata Amnie2d31312022-11-18 03:38:452963 return IsInitialNavigation() && GetLastCommittedEntry()->IsInitialEntry() &&
Ali Hijazid87307d2022-11-07 20:15:032964 !frame_tree_->has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462965}
2966
Aran Gilman37d11632019-10-08 23:07:152967SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Alex Moshchuk8015afcf2022-01-31 22:59:252968 const StoragePartitionConfig& partition_config) {
[email protected]fdac6ade2013-07-20 01:06:302969 StoragePartition* partition =
Lukasz Anforowiczb9a969a2021-04-29 15:26:252970 browser_context_->GetStoragePartition(partition_config);
michaelnbacbcbd2016-02-09 00:32:032971 DOMStorageContextWrapper* context_wrapper =
2972 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2973
2974 SessionStorageNamespaceMap::const_iterator it =
Alex Moshchuk8015afcf2022-01-31 22:59:252975 session_storage_namespace_map_.find(partition_config);
michaelnbacbcbd2016-02-09 00:32:032976 if (it != session_storage_namespace_map_.end()) {
2977 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152978 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2979 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:472980
michaelnbacbcbd2016-02-09 00:32:032981 return it->second.get();
2982 }
2983
2984 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102985 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2986 SessionStorageNamespaceImpl::Create(context_wrapper);
2987 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2988 session_storage_namespace.get();
Alex Moshchuk8015afcf2022-01-31 22:59:252989 session_storage_namespace_map_[partition_config] =
Daniel Murphy31bbb8b12018-02-07 21:44:102990 std::move(session_storage_namespace);
[email protected]fdac6ade2013-07-20 01:06:302991
Daniel Murphy31bbb8b12018-02-07 21:44:102992 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302993}
2994
2995SessionStorageNamespace*
2996NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Alex Moshchuk8015afcf2022-01-31 22:59:252997 return GetSessionStorageNamespace(
2998 StoragePartitionConfig::CreateDefault(GetBrowserContext()));
[email protected]fdac6ade2013-07-20 01:06:302999}
3000
3001const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573002NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:303003 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:553004}
[email protected]d202a7c2012-01-04 07:53:473005
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573006bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:563007 return needs_reload_;
3008}
[email protected]a26023822011-12-29 00:23:553009
[email protected]46bb5e9c2013-10-03 22:16:473010void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:413011 SetNeedsReload(NeedsReloadType::kRequestedByClient);
3012}
3013
3014void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:473015 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:413016 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:543017
3018 if (last_committed_entry_index_ != -1) {
3019 entries_[last_committed_entry_index_]->SetTransitionType(
3020 ui::PAGE_TRANSITION_RELOAD);
3021 }
[email protected]46bb5e9c2013-10-03 22:16:473022}
3023
[email protected]d202a7c2012-01-04 07:53:473024void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:573025 DCHECK_LT(index, GetEntryCount());
3026 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:313027 DiscardNonCommittedEntries();
3028
Nate Chapin9eb16be72022-09-23 22:54:313029 RemovedEntriesTracker tracker(weak_factory_.GetSafeRef());
[email protected]43032342011-03-21 14:10:313030 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:123031 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:313032 last_committed_entry_index_--;
3033}
3034
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573035NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:273036 // If there is no pending_entry_, there should be no pending_entry_index_.
3037 DCHECK(pending_entry_ || pending_entry_index_ == -1);
3038
3039 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:363040 // at that index. An exception is while a reload of a post commit error page
3041 // is ongoing; in that case pending entry will point to the entry replaced
3042 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:273043 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:363044 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
3045 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:273046
[email protected]022af742011-12-28 18:37:253047 return pending_entry_;
3048}
3049
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573050int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:273051 // The pending entry index must always be less than the number of entries.
arthursonzogni5c4c202d2017-04-25 23:41:273052 DCHECK_LT(pending_entry_index_, GetEntryCount());
[email protected]a26023822011-12-29 00:23:553053 return pending_entry_index_;
3054}
3055
avi25764702015-06-23 15:43:373056void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:573057 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:363058 bool replace,
Dave Tapuska87696ae2021-11-18 18:48:313059 bool was_post_commit_error,
Rakina Zata Amnia4e27222021-12-22 01:05:003060 bool in_fenced_frame_tree,
3061 LoadCommittedDetails* commit_details) {
Dave Tapuska87696ae2021-11-18 18:48:313062 // Fenced frame trees should always have `ui::PAGE_TRANSITION_AUTO_SUBFRAME`
3063 // set because:
3064 // 1) They don't influence the history of the outer page.
3065 // 2) They are always replace only navigation (there is always only one entry
3066 // in their history stack).
3067 // 3) Are not top level navigations and appear similar to iframes.
3068 // Navigations of the fenced frame might create a new NavigationEntry, which
3069 // will call this function. Non fenced frame navigations will never have
3070 // `ui::PAGE_TRANSITION_AUTO_SUBFRAME` because they won't call
3071 // InsertOrReplaceEntry.
3072 DCHECK_EQ(in_fenced_frame_tree,
3073 ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
3074 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:203075
avi5cad4912015-06-19 05:25:443076 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
3077 // need to keep continuity with the pending entry, so copy the pending entry's
3078 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
3079 // then the renderer navigated on its own, independent of the pending entry,
3080 // so don't copy anything.
3081 if (pending_entry_ && pending_entry_index_ == -1)
3082 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:203083
Rakina Zata Amnia4e27222021-12-22 01:05:003084 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]765b35502008-08-21 00:51:203085
creisee17e932015-07-17 17:56:223086 // When replacing, don't prune the forward history.
Rakina Zata Amnie2d31312022-11-18 03:38:453087 if (replace || was_post_commit_error) {
Mikel Astizba9cf2fd2017-12-17 10:38:103088 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573089 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:363090 // If the new entry is a post-commit error page, we store the current last
3091 // committed entry to the side so that we can put it back when navigating
3092 // away from the error.
3093 if (was_post_commit_error) {
3094 DCHECK(!entry_replaced_by_post_commit_error_);
3095 entry_replaced_by_post_commit_error_ =
3096 std::move(entries_[last_committed_entry_index_]);
3097 }
dcheng36b6aec92015-12-26 06:16:363098 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:223099 return;
3100 }
[email protected]765b35502008-08-21 00:51:203101
creis37979a62015-08-04 19:48:183102 // We shouldn't see replace == true when there's no committed entries.
3103 DCHECK(!replace);
3104
Michael Thiessen9b14d512019-09-23 21:19:473105 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:203106
Shivani Sharmad8c8d652019-02-13 17:27:573107 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:203108
dcheng36b6aec92015-12-26 06:16:363109 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:203110 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:293111}
3112
Shivani Sharmad8c8d652019-02-13 17:27:573113void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:163114 if (entries_.size() < max_entry_count())
3115 return;
3116
3117 DCHECK_EQ(max_entry_count(), entries_.size());
3118 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:573119 CHECK_EQ(pending_entry_index_, -1);
3120
3121 int index = 0;
Elly Fong-Jonesccc6d1f2021-06-14 18:32:423122 // Retrieve the oldest skippable entry.
3123 for (; index < GetEntryCount(); index++) {
3124 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
3125 break;
Shivani Sharmad8c8d652019-02-13 17:27:573126 }
3127
3128 // If there is no skippable entry or if it is the last committed entry then
3129 // fall back to pruning the oldest entry. It is not safe to prune the last
3130 // committed entry.
3131 if (index == GetEntryCount() || index == last_committed_entry_index_)
3132 index = 0;
3133
3134 bool should_succeed = RemoveEntryAtIndex(index);
3135 DCHECK_EQ(true, should_succeed);
3136
3137 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:253138}
3139
clamy3cb9bea92018-07-10 12:42:023140void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:163141 ReloadType reload_type,
Nate Chapinbf682fa32022-09-26 22:41:203142 RenderFrameHostImpl* initiator_rfh,
Arthur Sonzognic686e8f2024-01-11 08:36:373143 std::optional<blink::scheduler::TaskAttributionId>
Yoav Weiss8c573952022-11-17 17:35:133144 soft_navigation_heuristics_task_id,
Nate Chapinbf682fa32022-09-26 22:41:203145 const std::string* navigation_api_key) {
Alexander Timin3a92df72019-09-20 11:59:503146 TRACE_EVENT0("navigation",
3147 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:273148 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:023149 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:363150 if (pending_entry_index_ != -1) {
3151 // The pending entry may not be in entries_ if a post-commit error page is
3152 // showing.
3153 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
3154 pending_entry_ == entry_replaced_by_post_commit_error_.get());
3155 }
Gyuyoung Kim107c2a02021-04-13 01:49:303156 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:573157 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:013158 needs_reload_ = false;
Ali Hijazid87307d2022-11-07 20:15:033159 FrameTreeNode* root = frame_tree_->root();
Arthur Sonzogni620cec62018-12-13 13:08:573160 int nav_entry_id = pending_entry_->GetUniqueID();
Yoav Weiss8c573952022-11-17 17:35:133161 // Only pass down the soft_navigation_heuristics_task_id when the initiator is
3162 // the same as the top level frame being navigated.
3163 if (root->current_frame_host() != initiator_rfh) {
Arthur Sonzognic686e8f2024-01-11 08:36:373164 soft_navigation_heuristics_task_id = std::nullopt;
Yoav Weiss8c573952022-11-17 17:35:133165 }
Arthur Sonzogni620cec62018-12-13 13:08:573166
[email protected]83c2e232011-10-07 21:36:463167 // If we were navigating to a slow-to-commit page, and the user performs
3168 // a session history navigation to the last committed page, RenderViewHost
3169 // will force the throbber to start, but WebKit will essentially ignore the
3170 // navigation, and won't send a message to stop the throbber. To prevent this
3171 // from happening, we drop the navigation here and stop the slow-to-commit
3172 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:023173 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:083174 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:273175 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
Ali Hijazid87307d2022-11-07 20:15:033176 frame_tree_->StopLoading();
[email protected]6a13a6c2011-12-20 21:47:123177
[email protected]83c2e232011-10-07 21:36:463178 DiscardNonCommittedEntries();
3179 return;
3180 }
3181
Arthur Sonzognic686e8f2024-01-11 08:36:373182 std::optional<blink::LocalFrameToken> initiator_frame_token;
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:333183 int initiator_process_id = ChildProcessHost::kInvalidUniqueID;
3184 if (initiator_rfh) {
3185 initiator_frame_token = initiator_rfh->GetFrameToken();
3186 initiator_process_id = initiator_rfh->GetProcess()->GetID();
3187 DCHECK(initiator_frame_token);
3188 }
3189
creisce0ef3572017-01-26 17:53:083190 // Compare FrameNavigationEntries to see which frames in the tree need to be
3191 // navigated.
clamy3cb9bea92018-07-10 12:42:023192 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
3193 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:333194 FindFramesToNavigate(root, reload_type, initiator_frame_token,
3195 initiator_process_id, soft_navigation_heuristics_task_id,
3196 &same_document_loads, &different_document_loads);
creis4e2ecb72015-06-20 00:46:303197
3198 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573199 // We were unable to match any frames to navigate. This can happen if a
3200 // history navigation targets a subframe that no longer exists
3201 // (https://crbug.com/705550). In this case, we need to update the current
3202 // history entry to the pending one but keep the main document loaded. We
3203 // also need to ensure that observers are informed about the updated
3204 // current history entry (e.g., for greying out back/forward buttons), and
3205 // that renderer processes update their history offsets. The easiest way
3206 // to do all that is to schedule a "redundant" same-document navigation in
3207 // the main frame.
3208 //
3209 // Note that we don't want to remove this history entry, as it might still
3210 // be valid later, since a frame that it's targeting may be recreated.
3211 //
3212 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
3213 // need to repeat history navigations until finding the one that works.
3214 // Consider changing this behavior to keep looking for the first valid
3215 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:023216 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:423217 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573218 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:573219 ReloadType::NONE /* reload_type */,
3220 true /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433221 false /* is_history_navigation_in_new_child */,
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:333222 initiator_frame_token, initiator_process_id);
clamy3cb9bea92018-07-10 12:42:023223 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573224 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:023225 DiscardPendingEntry(false);
3226 return;
3227 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573228 same_document_loads.push_back(std::move(navigation_request));
3229
3230 // Sanity check that we never take this branch for any kinds of reloads,
3231 // as those should've queued a different-document load in the main frame.
3232 DCHECK(!is_forced_reload);
3233 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:303234 }
3235
Nate Chapinbf682fa32022-09-26 22:41:203236 // If the initiator is top-navigation sandboxed, then track whether this
Dave Tapuska8bfd84c2019-03-26 20:47:163237 // navigation affects any frame outside the frame's subtree.
Nate Chapinbf682fa32022-09-26 22:41:203238 if (initiator_rfh && initiator_rfh->IsSandboxed(
3239 network::mojom::WebSandboxFlags::kTopNavigation)) {
3240 bool navigates_inside_tree = DoesSandboxNavigationStayWithinSubtree(
3241 initiator_rfh, same_document_loads) &&
3242 DoesSandboxNavigationStayWithinSubtree(
3243 initiator_rfh, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:503244 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:163245 // the number of pages that trigger this.
Nate Chapinbf682fa32022-09-26 22:41:203246 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
3247 initiator_rfh,
3248 navigates_inside_tree
3249 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
3250 : blink::mojom::WebFeature::
3251 kSandboxBackForwardAffectsFramesOutsideSubtree);
Dave Tapuska855c1e12019-08-23 20:45:523252
3253 // If the navigation occurred outside the tree discard it because
3254 // the sandboxed frame didn't have permission to navigate outside
3255 // its tree. If it is possible that the navigation is both inside and
3256 // outside the frame tree and we discard it entirely because we don't
3257 // want to end up in a history state that didn't exist before.
Dominic Farolino057440042022-01-19 18:18:143258 if (!navigates_inside_tree) {
Nate Chapinbf682fa32022-09-26 22:41:203259 // If a |navigation_api_key| was provided, this navigation originated from
3260 // the navigation API. Notify the renderer that the navigation was
3261 // cancelled so the navigation API can fire an error event and reject the
3262 // relevant promise.
3263 if (navigation_api_key) {
3264 initiator_rfh->GetAssociatedLocalFrame()->TraverseCancelled(
3265 *navigation_api_key,
3266 blink::mojom::TraverseCancelledReason::kSandboxViolation);
3267 }
Dave Tapuska855c1e12019-08-23 20:45:523268 DiscardPendingEntry(false);
3269 return;
3270 }
Dave Tapuska8bfd84c2019-03-26 20:47:163271 }
3272
Nate Chapin6c43c022023-02-13 23:32:423273 // If it is possible that this traverse may involve a same-document navigation
3274 // in the initiator and there is a Navigation API key involved, then we may
3275 // need to notify the initiator if it fails. (The early returns above either
3276 // do not involve these cases or already notify the initiator.)
3277 // The event only needs to fire for the initiator, and only if the initiator
3278 // itself is performing a same-document navigation (because the event will not
3279 // fire if it navigates cross-document).
3280 if (navigation_api_key) {
3281 for (auto& item : same_document_loads) {
3282 if (item->frame_tree_node() == initiator_rfh->frame_tree_node()) {
3283 item->set_pending_navigation_api_key(*navigation_api_key);
3284 break;
3285 }
3286 }
3287 }
3288
Carlos Caballero539a421c2020-07-06 10:25:573289 // BackForwardCache:
3290 // Navigate immediately if the document is in the BackForwardCache.
Mingyu Lei7584b6b2023-04-13 03:02:563291 if (back_forward_cache_.GetOrEvictEntry(nav_entry_id).has_value()) {
Carlos Caballero539a421c2020-07-06 10:25:573292 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
3293 DCHECK_EQ(reload_type, ReloadType::NONE);
3294 auto navigation_request = CreateNavigationRequestFromEntry(
3295 root, pending_entry_, pending_entry_->GetFrameEntry(root),
3296 ReloadType::NONE, false /* is_same_document_history_load */,
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:333297 false /* is_history_navigation_in_new_child */, initiator_frame_token,
3298 initiator_process_id);
Lukasz Anforowicz9ee83c272020-12-01 20:14:053299 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:573300
3301 return;
3302 }
3303
3304 // History navigation might try to reuse a specific BrowsingInstance, already
3305 // used by a page in the cache. To avoid having two different main frames that
3306 // live in the same BrowsingInstance, evict the all pages with this
3307 // BrowsingInstance from the cache.
3308 //
3309 // For example, take the following scenario:
3310 //
3311 // A1 = Some page on a.com
3312 // A2 = Some other page on a.com
3313 // B3 = An uncacheable page on b.com
3314 //
3315 // Then the following navigations occur:
3316 // A1->A2->B3->A1
3317 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
3318 // take its place) and A1 will be created in the same BrowsingInstance (and
3319 // SiteInstance), as A2.
3320 //
3321 // If we didn't do anything, both A1 and A2 would remain alive in the same
3322 // BrowsingInstance/SiteInstance, which is unsupported by
3323 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
3324 // A2 from the cache.
3325 if (pending_entry_->site_instance()) {
3326 back_forward_cache_.EvictFramesInRelatedSiteInstances(
3327 pending_entry_->site_instance());
3328 }
3329
Rakina Zata Amnid605d462022-06-01 10:17:033330 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "pending_entry_restored",
3331 pending_entry_ && pending_entry_->IsRestored());
3332 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_entry_id",
3333 pending_entry_ ? pending_entry_->GetUniqueID() : -1);
3334 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_entry_index",
3335 pending_entry_index_);
3336 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "last_committed_index",
3337 last_committed_entry_index_);
3338 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "entries_size", entries_.size());
3339 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "pending_entry_initial",
3340 pending_entry_ && pending_entry_->IsInitialEntry());
3341 SCOPED_CRASH_KEY_BOOL(
3342 "nav_reentrancy", "pending_entry_initial2",
3343 pending_entry_ &&
3344 pending_entry_->IsInitialEntryNotForSynchronousAboutBlank());
3345 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_initial_nav",
3346 IsInitialNavigation());
3347 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_initial_blank_nav",
3348 IsInitialBlankNavigation());
3349 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_forced_reload", is_forced_reload);
3350 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_reload_type",
3351 (int)pending_reload_);
3352
clamy3cb9bea92018-07-10 12:42:023353 // This call does not support re-entrancy. See http://crbug.com/347742.
3354 CHECK(!in_navigate_to_pending_entry_);
3355 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:303356
Rakina Zata Amnid605d462022-06-01 10:17:033357 // If the navigation-reentrancy is caused by calling
3358 // NavigateToExistingPendingEntry twice, this will note the previous call's
3359 // pending entry's ID.
3360 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "prev_pending_entry_id",
3361 pending_entry_ ? pending_entry_->GetUniqueID() : -1);
3362
arthursonzogni66f711c2019-10-08 14:40:363363 // It is not possible to delete the pending NavigationEntry while navigating
3364 // to it. Grab a reference to delay potential deletion until the end of this
3365 // function.
3366 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3367
Nate Chapin154b14b2023-02-17 18:28:163368 // If there is a main-frame same-document history navigation, we may defer
3369 // the subframe history navigations in order to give JS in the main frame the
3370 // opportunity to cancel the entire traverse via the navigate event. In that
3371 // case, we need to stash the main frame request's navigation token on the
3372 // subframes, so they can look up the main frame request and defer themselves
3373 // until it completes.
3374 if (!same_document_loads.empty() &&
3375 same_document_loads.at(0)->frame_tree_node()->IsMainFrame()) {
3376 NavigationRequest* main_frame_request = same_document_loads.at(0).get();
3377 // The token will only be returned in cases where deferring the navigation
3378 // is necessary.
3379 if (auto main_frame_same_document_token =
3380 main_frame_request->GetNavigationTokenForDeferringSubframes()) {
3381 for (auto& item : same_document_loads) {
3382 if (item.get() != main_frame_request) {
3383 item->set_main_frame_same_document_history_token(
3384 main_frame_same_document_token);
3385 }
3386 }
3387 for (auto& item : different_document_loads) {
3388 item->set_main_frame_same_document_history_token(
3389 main_frame_same_document_token);
3390 }
3391 }
3392 }
3393
William Liu122754942024-01-18 22:34:393394 if (!initiator_rfh) {
3395 // A browser-initiated navigation won't have a `initiator_rfh`.
3396 CountBrowserInitiatedMainframeAndSubframeHistoryNavigaions(
3397 different_document_loads, same_document_loads);
3398 }
3399
creis4e2ecb72015-06-20 00:46:303400 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:023401 for (auto& item : same_document_loads) {
3402 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:053403 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:303404 }
clamy3cb9bea92018-07-10 12:42:023405 for (auto& item : different_document_loads) {
3406 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:053407 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:303408 }
clamy3cb9bea92018-07-10 12:42:023409
3410 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:303411}
3412
Alex Moshchuk3a4e77a2020-05-29 21:32:573413NavigationControllerImpl::HistoryNavigationAction
3414NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:303415 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:573416 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:423417 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:583418 // Only active and prerendered documents are allowed to navigate in their
3419 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:423420 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:013421 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:583422 // - If the document is in pending deletion, the browser already committed
3423 // to destroying this RenderFrameHost. See https://crbug.com/930278.
3424 // - If the document is in back-forward cache, it's not allowed to navigate
3425 // as it should remain frozen. Ignore the request and evict the document
3426 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:423427 //
Sreeja Kamishettydb8e2892021-03-10 09:30:583428 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:423429 // which should all be inactive as well.
Fergal Daly1336ac642021-09-14 15:13:113430 if (frame->current_frame_host()->IsInactiveAndDisallowActivation(
3431 DisallowActivationReasonId::kDetermineActionForHistoryNavigation)) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:423432 return HistoryNavigationAction::kStopLooking;
Fergal Daly1336ac642021-09-14 15:13:113433 }
Sreeja Kamishetty8eacabb2021-03-09 11:45:423434 }
arthursonzogni03f76152019-02-12 10:35:203435
Alex Moshchuk3a4e77a2020-05-29 21:32:573436 // Reloads should result in a different-document load. Note that reloads may
3437 // also happen via the |needs_reload_| mechanism where the reload_type is
3438 // NONE, so detect this by comparing whether we're going to the same
3439 // entry that we're currently on. Similarly to above, only main frames
3440 // should reach this. Note that subframes support reloads, but that's done
3441 // via a different path that doesn't involve FindFramesToNavigate (see
3442 // RenderFrameHost::Reload()).
3443 if (reload_type != ReloadType::NONE ||
3444 pending_entry_index_ == last_committed_entry_index_) {
3445 DCHECK(frame->IsMainFrame());
3446 return HistoryNavigationAction::kDifferentDocument;
3447 }
3448
Alex Moshchuk47d1a4bd2020-06-01 22:15:343449 // If there is no new FrameNavigationEntry for the frame, ignore the
3450 // load. For example, this may happen when going back to an entry before a
3451 // frame was created. Suppose we commit a same-document navigation that also
3452 // results in adding a new subframe somewhere in the tree. If we go back,
3453 // the new subframe will be missing a FrameNavigationEntry in the previous
3454 // NavigationEntry, but we shouldn't delete or change what's loaded in
3455 // it.
3456 //
Alex Moshchuke65c39272020-06-03 17:55:373457 // Note that in this case, there is no need to keep looking for navigations
3458 // in subframes, which would be missing FrameNavigationEntries as well.
3459 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:343460 // It's important to check this before checking |old_item| below, since both
3461 // might be null, and in that case we still shouldn't change what's loaded in
3462 // this frame. Note that scheduling any loads assumes that |new_item| is
3463 // non-null. See https://crbug.com/1088354.
3464 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3465 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:373466 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:343467
Charlie Reisa474fb62022-03-17 02:31:363468 // Use the RenderFrameHost's last committed FrameNavigationEntry to identify
3469 // which history item it is currently on, since this may be different than the
3470 // FrameNavigationEntry for the frame in the last committed NavigationEntry
3471 // (e.g., if a history navigation is targeting multiple frames and only some
3472 // have committed so far).
creis4e2ecb72015-06-20 00:46:303473 FrameNavigationEntry* old_item =
Charlie Reisa474fb62022-03-17 02:31:363474 frame->current_frame_host()->last_committed_frame_entry();
3475 if (!old_item) {
3476 // In cases where the RenderFrameHost does not have a FrameNavigationEntry,
3477 // fall back to the last committed NavigationEntry's record for this frame.
3478 // This may happen in cases like the initial state of the RenderFrameHost.
3479 // TODO(https://crbug.com/1304466): Ensure the RenderFrameHost always has an
3480 // accurate FrameNavigationEntry and eliminate this case.
3481 old_item = GetLastCommittedEntry()->GetFrameEntry(frame);
3482 }
3483 // If neither approach finds a FrameNavigationEntry, schedule a
3484 // different-document load.
3485 // TODO(https://crbug.com/608402): Remove this case.
Alex Moshchuk3a4e77a2020-05-29 21:32:573486 if (!old_item)
3487 return HistoryNavigationAction::kDifferentDocument;
3488
Alex Moshchuk3a4e77a2020-05-29 21:32:573489 // If the new item is not in the same SiteInstance, schedule a
3490 // different-document load. Newly restored items may not have a SiteInstance
3491 // yet, in which case it will be assigned on first commit.
3492 if (new_item->site_instance() &&
3493 new_item->site_instance() != old_item->site_instance())
3494 return HistoryNavigationAction::kDifferentDocument;
3495
3496 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:313497 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
3498 // that we do this even if the history navigation would not be modifying this
3499 // frame were it live.
3500 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:573501 return HistoryNavigationAction::kDifferentDocument;
3502
3503 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:313504 // Starting a navigation after a crash early-promotes the speculative
3505 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
3506 // committed yet, so we can not possibly perform a same-document history
3507 // navigation. The frame would need to be reloaded with a cross-document
3508 // navigation.
3509 if (!frame->current_frame_host()->has_committed_any_navigation())
3510 return HistoryNavigationAction::kDifferentDocument;
3511
creis54131692016-08-12 18:32:253512 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:493513 // sequence number but different item sequence number.
3514 if (new_item->document_sequence_number() ==
3515 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573516 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493517 }
avib48cb312016-05-05 21:35:003518
Alex Moshchuk3a4e77a2020-05-29 21:32:573519 // Otherwise, if both item and document sequence numbers differ, this
3520 // should be a different document load.
3521 return HistoryNavigationAction::kDifferentDocument;
3522 }
3523
3524 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373525 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573526 DCHECK_EQ(new_item->document_sequence_number(),
3527 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373528 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573529}
3530
3531void NavigationControllerImpl::FindFramesToNavigate(
3532 FrameTreeNode* frame,
3533 ReloadType reload_type,
Arthur Sonzognic686e8f2024-01-11 08:36:373534 const std::optional<blink::LocalFrameToken>& initiator_frame_token,
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:333535 int initiator_process_id,
Arthur Sonzognic686e8f2024-01-11 08:36:373536 std::optional<blink::scheduler::TaskAttributionId>
Yoav Weiss8c573952022-11-17 17:35:133537 soft_navigation_heuristics_task_id,
Alex Moshchuk3a4e77a2020-05-29 21:32:573538 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3539 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3540 DCHECK(pending_entry_);
3541 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3542
3543 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3544
3545 if (action == HistoryNavigationAction::kSameDocument) {
3546 std::unique_ptr<NavigationRequest> navigation_request =
3547 CreateNavigationRequestFromEntry(
3548 frame, pending_entry_, new_item, reload_type,
Yoav Weiss8c573952022-11-17 17:35:133549 /*is_same_document_history_load=*/true,
3550 /*is_history_navigation_in_new_child_frame=*/false,
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:333551 initiator_frame_token, initiator_process_id,
3552 soft_navigation_heuristics_task_id);
Alex Moshchuk3a4e77a2020-05-29 21:32:573553 if (navigation_request) {
3554 // Only add the request if was properly created. It's possible for the
3555 // creation to fail in certain cases, e.g. when the URL is invalid.
3556 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303557 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573558 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473559 std::unique_ptr<NavigationRequest> navigation_request =
3560 CreateNavigationRequestFromEntry(
3561 frame, pending_entry_, new_item, reload_type,
3562 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433563 false /* is_history_navigation_in_new_child */,
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:333564 initiator_frame_token, initiator_process_id);
Lei Zhang96031532019-10-10 19:05:473565 if (navigation_request) {
3566 // Only add the request if was properly created. It's possible for the
3567 // creation to fail in certain cases, e.g. when the URL is invalid.
3568 different_document_loads->push_back(std::move(navigation_request));
3569 }
3570 // For a different document, the subframes will be destroyed, so there's
3571 // no need to consider them.
3572 return;
Alex Moshchuke65c39272020-06-03 17:55:373573 } else if (action == HistoryNavigationAction::kStopLooking) {
3574 return;
creis4e2ecb72015-06-20 00:46:303575 }
3576
Yoav Weiss8c573952022-11-17 17:35:133577 // Do not pass down the soft_navigation_heuristics_task_id to child frames, as
3578 // we currently only support soft navigation heuristics for the top level
3579 // frame.
creis4e2ecb72015-06-20 00:46:303580 for (size_t i = 0; i < frame->child_count(); i++) {
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:333581 FindFramesToNavigate(frame->child_at(i), reload_type, initiator_frame_token,
3582 initiator_process_id,
Arthur Sonzognic686e8f2024-01-11 08:36:373583 /*soft_navigation_heuristics_task_id=*/std::nullopt,
Nate Chapin45f620582021-09-30 17:45:433584 same_document_loads, different_document_loads);
creis4e2ecb72015-06-20 00:46:303585 }
3586}
3587
Harkiran Bolariaba823e42021-05-21 18:30:363588base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry(
clamy21718cc22018-06-13 13:34:243589 const LoadURLParams& params) {
3590 // Find the appropriate FrameTreeNode.
3591 FrameTreeNode* node = nullptr;
3592 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3593 !params.frame_name.empty()) {
3594 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Ali Hijazid87307d2022-11-07 20:15:033595 ? frame_tree_->FindByID(params.frame_tree_node_id)
3596 : frame_tree_->FindByName(params.frame_name);
Arthur Sonzognif6785ec2022-12-05 10:11:503597 DCHECK(!node || &node->frame_tree() == &frame_tree());
clamy21718cc22018-06-13 13:34:243598 }
3599
3600 // If no FrameTreeNode was specified, navigate the main frame.
3601 if (!node)
Ali Hijazid87307d2022-11-07 20:15:033602 node = frame_tree_->root();
clamy21718cc22018-06-13 13:34:243603
Camille Lamy5193caa2018-10-12 11:59:423604 // Compute overrides to the LoadURLParams for |override_user_agent|,
3605 // |should_replace_current_entry| and |has_user_gesture| that will be used
3606 // both in the creation of the NavigationEntry and the NavigationRequest.
3607 // Ideally, the LoadURLParams themselves would be updated, but since they are
3608 // passed as a const reference, this is not possible.
3609 // TODO(clamy): When we only create a NavigationRequest, move this to
3610 // CreateNavigationRequestFromLoadURLParams.
3611 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3612 GetLastCommittedEntry());
3613
Rakina Zata Amnie2d31312022-11-18 03:38:453614 // An entry replacement must happen if the current browsing context should
3615 // maintain a trivial session history.
shivanigithubf405bf0d2021-11-05 17:58:333616 bool should_replace_current_entry =
3617 (params.should_replace_current_entry ||
Rakina Zata Amnie2d31312022-11-18 03:38:453618 ShouldMaintainTrivialSessionHistory(node));
Camille Lamy5193caa2018-10-12 11:59:423619
clamy21718cc22018-06-13 13:34:243620 // Javascript URLs should not create NavigationEntries. All other navigations
3621 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243622 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373623 std::unique_ptr<NavigationEntryImpl> entry =
3624 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3625 should_replace_current_entry,
3626 params.has_user_gesture);
clamy21718cc22018-06-13 13:34:243627 DiscardPendingEntry(false);
3628 SetPendingEntry(std::move(entry));
3629 }
3630
Tim Judkins59548192023-05-17 17:51:203631 // Renderer-debug URLs are sent to the current renderer process immediately
3632 // for processing and don't need to create a NavigationRequest. Note: this
3633 // includes navigations to JavaScript URLs, which are considered
clamy21718cc22018-06-13 13:34:243634 // renderer-debug URLs.
3635 // Note: we intentionally leave the pending entry in place for renderer debug
3636 // URLs, unlike the cases below where we clear it if the navigation doesn't
3637 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303638 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483639 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3640 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153641 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Tim Judkins59548192023-05-17 17:51:203642 params.url, browser_context_, node->current_frame_host())) {
Oleg Davydov2cc0167b2019-02-05 14:32:483643 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363644 return nullptr;
Oleg Davydov2cc0167b2019-02-05 14:32:483645 }
3646
clamy21718cc22018-06-13 13:34:243647 HandleRendererDebugURL(node, params.url);
Harkiran Bolariaba823e42021-05-21 18:30:363648 return nullptr;
clamy21718cc22018-06-13 13:34:243649 }
3650
Antonio Sartori78a749f2020-11-30 12:03:393651 DCHECK(pending_entry_);
3652
clamy21718cc22018-06-13 13:34:243653 // Convert navigations to the current URL to a reload.
3654 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3655 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3656 // DevTools sometimes issues navigations to main frames that they do not
3657 // expect to see treated as reload, and it only works because they pass a
3658 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3659 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543660 ReloadType reload_type = params.reload_type;
3661 if (reload_type == ReloadType::NONE &&
3662 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043663 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243664 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243665 params.load_type ==
3666 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
Hayato Ito7a80db42021-07-05 06:18:543667 should_replace_current_entry, GetLastCommittedEntry())) {
clamy21718cc22018-06-13 13:34:243668 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473669 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393670
3671 // If this is a reload of an existing FrameNavigationEntry and we had a
3672 // policy container for it, then we should copy it into the pending entry,
3673 // so that it is copied to the navigation request in
3674 // CreateNavigationRequestFromLoadParams later.
Rakina Zata Amnie2d31312022-11-18 03:38:453675 FrameNavigationEntry* previous_frame_entry =
3676 GetLastCommittedEntry()->GetFrameEntry(node);
3677 if (previous_frame_entry &&
3678 previous_frame_entry->policy_container_policies()) {
3679 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
3680 previous_frame_entry->policy_container_policies()->ClonePtr());
Antonio Sartori78a749f2020-11-30 12:03:393681 }
3682 }
3683
3684 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
Rakina Zata Amniafd3c6582021-11-30 06:19:173685 // document, then we should copy the document polices from RenderFrameHost's
3686 // PolicyContainerHost. The NavigationRequest will create a new
3687 // PolicyContainerHost with the document policies from the |pending_entry_|,
3688 // and that PolicyContainerHost will be put in the final RenderFrameHost for
3689 // the navigation. This way, we ensure that we keep enforcing the right
3690 // policies on the initial empty document after the reload.
Rakina Zata Amnie2d31312022-11-18 03:38:453691 if (GetLastCommittedEntry()->IsInitialEntry() && params.url.IsAboutBlank()) {
Antonio Sartori78a749f2020-11-30 12:03:393692 if (node->current_frame_host() &&
3693 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193694 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163695 node->current_frame_host()
3696 ->policy_container_host()
3697 ->policies()
Titouan Rigoudy72f892d2022-05-02 18:21:233698 .ClonePtr());
Antonio Sartori78a749f2020-11-30 12:03:393699 }
clamy21718cc22018-06-13 13:34:243700 }
3701
3702 // navigation_ui_data should only be present for main frame navigations.
Ian Vollick1c6dd3e2022-04-13 02:06:263703 DCHECK(node->IsOutermostMainFrame() || !params.navigation_ui_data);
clamy21718cc22018-06-13 13:34:243704
Tsuyoshi Horo167ca6432022-03-09 05:16:393705 // This will be used to set the Navigation Timing API navigationStart
3706 // parameter for browser navigations in new tabs (intents, tabs opened through
3707 // "Open link in new tab"). If the navigation must wait on the current
3708 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3709 // will be updated when the BeforeUnload ack is received.
3710 const auto navigation_start_time = base::TimeTicks::Now();
3711
Camille Lamy5193caa2018-10-12 11:59:423712 std::unique_ptr<NavigationRequest> request =
3713 CreateNavigationRequestFromLoadParams(
3714 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143715 params.has_user_gesture, network::mojom::SourceLocation::New(),
Tsuyoshi Horo167ca6432022-03-09 05:16:393716 reload_type, pending_entry_, pending_entry_->GetFrameEntry(node),
3717 navigation_start_time);
clamy21718cc22018-06-13 13:34:243718
3719 // If the navigation couldn't start, return immediately and discard the
3720 // pending NavigationEntry.
3721 if (!request) {
3722 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363723 return nullptr;
clamy21718cc22018-06-13 13:34:243724 }
3725
Camille Lamy5193caa2018-10-12 11:59:423726#if DCHECK_IS_ON()
3727 // Safety check that NavigationRequest and NavigationEntry match.
3728 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3729#endif
3730
clamy21718cc22018-06-13 13:34:243731 // This call does not support re-entrancy. See http://crbug.com/347742.
3732 CHECK(!in_navigate_to_pending_entry_);
3733 in_navigate_to_pending_entry_ = true;
3734
arthursonzogni66f711c2019-10-08 14:40:363735 // It is not possible to delete the pending NavigationEntry while navigating
3736 // to it. Grab a reference to delay potential deletion until the end of this
3737 // function.
3738 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3739
Harkiran Bolariaba823e42021-05-21 18:30:363740 base::WeakPtr<NavigationHandle> created_navigation_handle(
3741 request->GetWeakPtr());
Lukasz Anforowicz9ee83c272020-12-01 20:14:053742 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243743
3744 in_navigate_to_pending_entry_ = false;
Harkiran Bolariaba823e42021-05-21 18:30:363745 return created_navigation_handle;
clamy21718cc22018-06-13 13:34:243746}
3747
clamyea99ea12018-05-28 13:54:233748void NavigationControllerImpl::HandleRendererDebugURL(
3749 FrameTreeNode* frame_tree_node,
3750 const GURL& url) {
3751 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243752 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3753 // the renderer process is not live, unless it is the initial navigation
3754 // of the tab.
clamyea99ea12018-05-28 13:54:233755 if (!IsInitialNavigation()) {
3756 DiscardNonCommittedEntries();
3757 return;
3758 }
Fergal Dalyecd3b0202020-06-25 01:57:373759 // The current frame is always a main frame. If IsInitialNavigation() is
3760 // true then there have been no navigations and any frames of this tab must
3761 // be in the same renderer process. If that has crashed then the only frame
3762 // that can be revived is the main frame.
3763 frame_tree_node->render_manager()
3764 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233765 }
Julie Jeongeun Kim50d124c2022-10-21 13:51:223766
3767 // Several tests expect a load of Chrome Debug URLs to send a DidStopLoading
3768 // notification, so set is loading to true here to properly surface it when
3769 // the renderer process is done handling the URL.
3770 // TODO(crbug.com/1254130): Remove the test dependency on this behavior.
3771 if (!url.SchemeIs(url::kJavaScriptScheme)) {
Julie Jeongeun Kim50d124c2022-10-21 13:51:223772 frame_tree_node->current_frame_host()->SetIsLoadingForRendererDebugURL();
Julie Jeongeun Kim50d124c2022-10-21 13:51:223773 }
clamyea99ea12018-05-28 13:54:233774 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3775}
3776
clamy21718cc22018-06-13 13:34:243777std::unique_ptr<NavigationEntryImpl>
3778NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3779 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423780 const LoadURLParams& params,
3781 bool override_user_agent,
3782 bool should_replace_current_entry,
3783 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393784 // Browser initiated navigations might not have a blob_url_loader_factory set
3785 // in params even if the navigation is to a blob URL. If that happens, lookup
3786 // the correct url loader factory to use here.
3787 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483788 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473789 // Resolve the blob URL in the storage partition associated with the target
3790 // frame. This is the storage partition the URL will be loaded in, and only
3791 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393792 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473793 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393794 }
3795
clamy21718cc22018-06-13 13:34:243796 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443797 // extra_headers in params are \n separated; navigation entries want \r\n.
3798 std::string extra_headers_crlf;
3799 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243800
3801 // For subframes, create a pending entry with a corresponding frame entry.
3802 if (!node->IsMainFrame()) {
Rakina Zata Amnie2d31312022-11-18 03:38:453803 entry = GetLastCommittedEntry()->Clone();
clamy21718cc22018-06-13 13:34:243804 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:083805 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
clamy21718cc22018-06-13 13:34:243806 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Arthur Sonzognic686e8f2024-01-11 08:36:373807 params.url, std::nullopt, params.referrer, params.initiator_origin,
W. James MacLean23e90a12022-12-21 04:38:213808 params.initiator_base_url, params.redirect_chain, blink::PageState(),
3809 "GET", -1, blob_url_loader_factory,
Antonio Sartori78a749f2020-11-30 12:03:393810 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163811 // a conversion of a new navigation into a reload, we will set the right
3812 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193813 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243814 } else {
3815 // Otherwise, create a pending entry for the main frame.
Julie Jeongeun Kim5b9aff72022-05-02 02:10:173816 // If `node` is the outermost main frame, it rewrites a virtual url in order
3817 // to adjust the original input url if needed. For inner frames such as
3818 // fenced frames or subframes, they don't rewrite urls as the urls are not
3819 // input urls by users.
3820 bool rewrite_virtual_urls = node->IsOutermostMainFrame();
Sharon Yang242ef822023-05-15 21:07:323821 scoped_refptr<SiteInstance> source_site_instance =
3822 params.source_site_instance;
Arthur Sonzognic686e8f2024-01-11 08:36:373823 std::optional<GURL> source_process_site_url = std::nullopt;
Sharon Yang242ef822023-05-15 21:07:323824 if (source_site_instance && source_site_instance->HasProcess()) {
3825 source_process_site_url =
3826 source_site_instance->GetProcess()->GetProcessLock().site_url();
3827 }
clamy21718cc22018-06-13 13:34:243828 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063829 params.url, params.referrer, params.initiator_origin,
Sharon Yang242ef822023-05-15 21:07:323830 params.initiator_base_url, source_process_site_url,
W. James MacLean23e90a12022-12-21 04:38:213831 params.transition_type, params.is_renderer_initiated,
3832 extra_headers_crlf, browser_context_, blob_url_loader_factory,
3833 rewrite_virtual_urls));
clamy21718cc22018-06-13 13:34:243834 entry->set_source_site_instance(
3835 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3836 entry->SetRedirectChain(params.redirect_chain);
3837 }
3838
3839 // Set the FTN ID (only used in non-site-per-process, for tests).
3840 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243841 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423842 entry->SetIsOverridingUserAgent(override_user_agent);
3843 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543844 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243845
clamy21718cc22018-06-13 13:34:243846 switch (params.load_type) {
3847 case LOAD_TYPE_DEFAULT:
3848 break;
3849 case LOAD_TYPE_HTTP_POST:
3850 entry->SetHasPostData(true);
3851 entry->SetPostData(params.post_data);
3852 break;
3853 case LOAD_TYPE_DATA:
3854 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3855 entry->SetVirtualURL(params.virtual_url_for_data_url);
Xiaohan Wang7f8052e02022-01-14 18:44:283856#if BUILDFLAG(IS_ANDROID)
clamy21718cc22018-06-13 13:34:243857 entry->SetDataURLAsString(params.data_url_as_string);
3858#endif
3859 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3860 break;
clamy21718cc22018-06-13 13:34:243861 }
3862
3863 // TODO(clamy): NavigationEntry is meant for information that will be kept
3864 // after the navigation ended and therefore is not appropriate for
3865 // started_from_context_menu. Move started_from_context_menu to
3866 // NavigationUIData.
3867 entry->set_started_from_context_menu(params.started_from_context_menu);
3868
3869 return entry;
3870}
3871
clamyea99ea12018-05-28 13:54:233872std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423873NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3874 FrameTreeNode* node,
3875 const LoadURLParams& params,
3876 bool override_user_agent,
3877 bool should_replace_current_entry,
3878 bool has_user_gesture,
Antonio Sartori2f763d9d2021-04-21 10:04:143879 network::mojom::SourceLocationPtr source_location,
Camille Lamy5193caa2018-10-12 11:59:423880 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573881 NavigationEntryImpl* entry,
Tsuyoshi Horo167ca6432022-03-09 05:16:393882 FrameNavigationEntry* frame_entry,
Nan Lin944e9b4e2022-04-12 13:51:223883 base::TimeTicks navigation_start_time,
Garrett Tanzer405f3402022-07-21 20:12:493884 bool is_embedder_initiated_fenced_frame_navigation,
Sergey Poromovdd557c12023-03-01 11:28:453885 bool is_unfenced_top_navigation,
Camillia Smith Barnes6a643962023-03-03 00:28:583886 bool is_container_initiated,
Arthur Sonzognic686e8f2024-01-11 08:36:373887 std::optional<std::u16string> embedder_shared_storage_context) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573888 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283889 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533890 // All renderer-initiated navigations must have an initiator_origin.
3891 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513892
Camille Lamy5193caa2018-10-12 11:59:423893 GURL url_to_load;
3894 GURL virtual_url;
Nasko Oskov03912102019-01-11 00:21:323895
Camille Lamy2baa8022018-10-19 16:43:173896 // For main frames, rewrite the URL if necessary and compute the virtual URL
3897 // that should be shown in the address bar.
Ian Vollick1c6dd3e2022-04-13 02:06:263898 if (node->IsOutermostMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423899 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173900 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423901 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423902
Camille Lamy2baa8022018-10-19 16:43:173903 // For DATA loads, override the virtual URL.
3904 if (params.load_type == LOAD_TYPE_DATA)
3905 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423906
Camille Lamy2baa8022018-10-19 16:43:173907 if (virtual_url.is_empty())
3908 virtual_url = url_to_load;
3909
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573910 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283911
Aran Gilman249eb122019-12-02 23:32:463912 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3913 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3914 // return different results, leading to a different URL in the
3915 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3916 // pending NavigationEntry, as we'll only make one call to
3917 // RewriteUrlForNavigation.
3918 VLOG_IF(1, (url_to_load != frame_entry->url()))
3919 << "NavigationRequest and FrameEntry have different URLs: "
3920 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283921
Camille Lamy2baa8022018-10-19 16:43:173922 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423923 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173924 // NavigationRequest.
3925 } else {
3926 url_to_load = params.url;
3927 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243928 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173929 }
Camille Lamy5193caa2018-10-12 11:59:423930
Ehsan Karamad44fc72112019-02-26 18:15:473931 if (node->render_manager()->is_attaching_inner_delegate()) {
3932 // Avoid starting any new navigations since this node is now preparing for
3933 // attaching an inner delegate.
3934 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203935 }
Camille Lamy5193caa2018-10-12 11:59:423936
Alex Moshchuk99242832023-05-22 17:21:443937 if (!IsValidURLForNavigation(node, virtual_url, url_to_load)) {
Camille Lamy5193caa2018-10-12 11:59:423938 return nullptr;
Alex Moshchuk99242832023-05-22 17:21:443939 }
Camille Lamy5193caa2018-10-12 11:59:423940
danakjd83d706d2020-11-25 22:11:123941 // Look for a pending commit that is to another document in this
3942 // FrameTreeNode. If one exists, then the last committed URL will not be the
3943 // current URL by the time this navigation commits.
3944 bool has_pending_cross_document_commit =
3945 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
Miyoung Shina2dd6a42021-10-07 12:19:213946 bool is_currently_error_page = node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:123947
Minggang Wangb9f3fa92021-07-01 15:30:313948 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjb952ef12021-01-14 19:58:493949 /*old_url=*/node->current_url(),
3950 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3951 has_pending_cross_document_commit, is_currently_error_page,
Garrett Tanzer267c2b82022-07-26 16:53:133952 /*is_same_document_history_load=*/false,
3953 is_embedder_initiated_fenced_frame_navigation,
3954 is_unfenced_top_navigation);
Camille Lamy5193caa2018-10-12 11:59:423955
3956 // Create the NavigationParams based on |params|.
3957
Hiroki Nakagawa4ed61282021-06-18 05:37:233958 bool is_view_source_mode = entry->IsViewSourceMode();
3959 DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme));
Charlie Harrison8c113a32019-01-07 16:08:293960
Antonio Sartori6984c742021-08-26 08:03:413961 blink::NavigationDownloadPolicy download_policy = params.download_policy;
Yao Xiao720ef9d62022-12-09 05:18:293962
Hiroki Nakagawa4ed61282021-06-18 05:37:233963 // Update |download_policy| if the virtual URL is view-source.
Charlie Harrison8c113a32019-01-07 16:08:293964 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253965 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293966
Minggang Wangb9f3fa92021-07-01 15:30:313967 blink::mojom::CommonNavigationParamsPtr common_params =
3968 blink::mojom::CommonNavigationParams::New(
W. James MacLean23e90a12022-12-21 04:38:213969 url_to_load, params.initiator_origin, params.initiator_base_url,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513970 blink::mojom::Referrer::New(params.referrer.url,
3971 params.referrer.policy),
Scott Violetcf6ea7e2021-06-09 21:09:213972 params.transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513973 should_replace_current_entry, params.base_url_for_data_url,
Tsuyoshi Horo167ca6432022-03-09 05:16:393974 navigation_start_time,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513975 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
Antonio Sartori2f763d9d2021-04-21 10:04:143976 params.post_data, std::move(source_location),
arthursonzogniaf7c62c52020-02-12 10:49:413977 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273978 false /* has_text_fragment_token */,
3979 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3980 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513981 false /* is_history_navigation_in_new_child_frame */,
Chris Fredricksonb52bcd02023-03-28 14:48:053982 params.input_start, network::mojom::RequestDestination::kEmpty);
Camille Lamy5193caa2018-10-12 11:59:423983
Minggang Wangb9f3fa92021-07-01 15:30:313984 blink::mojom::CommitNavigationParamsPtr commit_params =
3985 blink::mojom::CommitNavigationParams::New(
Arthur Sonzognic686e8f2024-01-11 08:36:373986 std::nullopt,
Ari Chivukula43d2cf82023-01-24 03:16:073987 // The correct storage key and session storage key will be computed
3988 // before committing the navigation.
3989 blink::StorageKey(), blink::StorageKey(), override_user_agent,
3990 params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323991 std::vector<network::mojom::URLResponseHeadPtr>(),
jongdeok.kim5de823b32022-06-14 04:37:503992 std::vector<net::RedirectInfo>(), params.post_content_type,
3993 common_params->url, common_params->method,
3994 params.can_load_local_resources,
Minggang Wangb9f3fa92021-07-01 15:30:313995 frame_entry->page_state().ToEncodedData(), entry->GetUniqueID(),
Yoav Weiss8c573952022-11-17 17:35:133996 entry->GetSubframeUniqueNames(node),
3997 /*intended_as_new_entry=*/true,
3998 /*pending_history_list_offset=*/-1,
Lucas Furukawa Gadania9c45682019-07-31 22:05:143999 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
4000 params.should_clear_history_list ? 0 : GetEntryCount(),
Yoav Weiss8c573952022-11-17 17:35:134001 /*was_discarded=*/false, is_view_source_mode,
Minggang Wangb9f3fa92021-07-01 15:30:314002 params.should_clear_history_list,
4003 blink::mojom::NavigationTiming::New(),
Minggang Wangf59db47b2021-06-16 01:56:224004 blink::mojom::WasActivatedOption::kUnknown,
Yoav Weiss8c573952022-11-17 17:35:134005 /*navigation_token=*/base::UnguessableToken::Create(),
Minggang Wang7ee0c742021-06-16 16:16:514006 std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(),
Xiaohan Wang7f8052e02022-01-14 18:44:284007#if BUILDFLAG(IS_ANDROID)
Yoav Weiss8c573952022-11-17 17:35:134008 /*data_url_as_string=*/std::string(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:144009#endif
Yoav Weiss8c573952022-11-17 17:35:134010 /*is_browser_initiated=*/!params.is_renderer_initiated,
Yoav Weiss8c573952022-11-17 17:35:134011 /*document_ukm_source_id=*/ukm::kInvalidSourceId,
Jiewei Qian0406fc02020-03-09 06:02:074012 node->pending_frame_policy(),
Yoav Weiss8c573952022-11-17 17:35:134013 /*force_enabled_origin_trials=*/std::vector<std::string>(),
4014 /*origin_agent_cluster=*/false,
4015 /*origin_agent_cluster_left_as_default=*/true,
4016 /*enabled_client_hints=*/
4017 std::vector<network::mojom::WebClientHintsType>(),
4018 /*is_cross_browsing_instance=*/false, /*old_page_info=*/nullptr,
4019 /*http_response_code=*/-1,
Domenic Denicolacd30f5f82022-03-16 21:48:014020 blink::mojom::NavigationApiHistoryEntryArrays::New(),
Yoav Weiss8c573952022-11-17 17:35:134021 /*early_hints_preloaded_resources=*/std::vector<GURL>(),
Clark DuVall8ee487a22021-11-10 02:25:584022 // This timestamp will be populated when the commit IPC is sent.
Yoav Weiss8c573952022-11-17 17:35:134023 /*commit_sent=*/base::TimeTicks(), /*srcdoc_value=*/std::string(),
Yoav Weiss8c573952022-11-17 17:35:134024 /*should_load_data_url=*/false,
Victor Tan10d93aca2022-08-12 16:46:284025 /*ancestor_or_self_has_cspee=*/node->AncestorOrSelfHasCSPEE(),
Yoav Weiss8c573952022-11-17 17:35:134026 /*reduced_accept_language=*/std::string(),
William Liu2c825472022-10-31 12:01:444027 /*navigation_delivery_type=*/
Khushal Sagar7b26135c62022-11-08 20:25:424028 network::mojom::NavigationDeliveryType::kDefault,
Arthur Sonzognic686e8f2024-01-11 08:36:374029 /*view_transition_state=*/std::nullopt,
4030 /*soft_navigation_heuristics_task_id=*/std::nullopt,
sbinglera07ae732022-12-02 20:49:054031 /*modified_runtime_features=*/
Jiewei Qian87951f52023-10-17 01:33:464032 base::flat_map<::blink::mojom::RuntimeFeature, bool>(),
Arthur Sonzognic686e8f2024-01-11 08:36:374033 /*fenced_frame_properties=*/std::nullopt,
Chris Fredricksonb52bcd02023-03-28 14:48:054034 /*not_restored_reasons=*/nullptr,
Arthur Hemeryd51484b2023-06-02 15:50:004035 /*load_with_storage_access=*/false,
Arthur Sonzognic686e8f2024-01-11 08:36:374036 /*browsing_context_group_info=*/std::nullopt,
Nan Linda394ba2023-11-03 21:17:374037 /*lcpp_hint=*/nullptr, blink::CreateDefaultRendererContentSettings(),
Arthur Sonzognic686e8f2024-01-11 08:36:374038 /*cookie_deprecation_label=*/std::nullopt);
Xiaohan Wang7f8052e02022-01-14 18:44:284039#if BUILDFLAG(IS_ANDROID)
Camille Lamy5193caa2018-10-12 11:59:424040 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:144041 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:424042 }
4043#endif
4044
Lucas Furukawa Gadania9c45682019-07-31 22:05:144045 commit_params->was_activated = params.was_activated;
Mike Jacksone2aa7af2023-05-17 06:45:074046 commit_params->navigation_timing->system_entropy_at_navigation_start =
4047 SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode(
4048 node, params.suggested_system_entropy);
Camille Lamy5193caa2018-10-12 11:59:424049
Camille Lamy5193caa2018-10-12 11:59:424050 // extra_headers in params are \n separated; NavigationRequests want \r\n.
4051 std::string extra_headers_crlf;
4052 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:094053
Alex Moshchuk9321e6a2022-12-07 21:58:314054 auto navigation_request = NavigationRequest::Create(
Lucas Furukawa Gadania9c45682019-07-31 22:05:144055 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:084056 !params.is_renderer_initiated, params.was_opener_suppressed,
Hiroshige Hayashizakif07ad7812023-05-10 02:26:094057 params.initiator_frame_token, params.initiator_process_id,
4058 extra_headers_crlf, frame_entry, entry, params.is_form_submission,
John Delaney50425f82020-04-07 16:26:214059 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
Yao Xiao720ef9d62022-12-09 05:18:294060 params.impression, params.initiator_activation_and_ad_status,
Sergey Poromovdd557c12023-03-01 11:28:454061 params.is_pdf, is_embedder_initiated_fenced_frame_navigation,
Camillia Smith Barnes6a643962023-03-03 00:28:584062 is_container_initiated, embedder_shared_storage_context);
Yao Xiaodc5ed102019-06-04 19:19:094063 navigation_request->set_from_download_cross_origin_redirect(
4064 params.from_download_cross_origin_redirect);
W. James MacLean00568d72022-02-24 19:36:554065 navigation_request->set_force_new_browsing_instance(
4066 params.force_new_browsing_instance);
Yao Xiaodc5ed102019-06-04 19:19:094067 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:424068}
4069
4070std::unique_ptr<NavigationRequest>
4071NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:234072 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574073 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:234074 FrameNavigationEntry* frame_entry,
4075 ReloadType reload_type,
4076 bool is_same_document_history_load,
Nate Chapin45f620582021-09-30 17:45:434077 bool is_history_navigation_in_new_child_frame,
Arthur Sonzognic686e8f2024-01-11 08:36:374078 const std::optional<blink::LocalFrameToken>& initiator_frame_token,
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:334079 int initiator_process_id,
Arthur Sonzognic686e8f2024-01-11 08:36:374080 std::optional<blink::scheduler::TaskAttributionId>
Yoav Weiss8c573952022-11-17 17:35:134081 soft_navigation_heuristics_task_id) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:344082 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:234083 GURL dest_url = frame_entry->url();
Rakina Zata Amnif297a802022-01-18 03:53:434084 // We should never navigate to an existing initial NavigationEntry that is the
4085 // initial NavigationEntry for the initial empty document that hasn't been
4086 // overridden by the synchronous about:blank commit, to preserve previous
4087 // behavior where trying to reload when the main frame is on the initial empty
4088 // document won't result in a navigation.
4089 // See also https://crbug.com/1277414.
4090 DCHECK(!entry->IsInitialEntryNotForSynchronousAboutBlank());
Nasko Oskov03912102019-01-11 00:21:324091
clamyea99ea12018-05-28 13:54:234092 Referrer dest_referrer = frame_entry->referrer();
clamyea99ea12018-05-28 13:54:234093
Ehsan Karamad44fc72112019-02-26 18:15:474094 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
4095 // Avoid starting any new navigations since this node is now preparing for
4096 // attaching an inner delegate.
4097 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:204098 }
4099
Alex Moshchuk99242832023-05-22 17:21:444100 if (!IsValidURLForNavigation(frame_tree_node, entry->GetVirtualURL(),
4101 dest_url)) {
clamyea99ea12018-05-28 13:54:234102 return nullptr;
4103 }
4104
clamyea99ea12018-05-28 13:54:234105 // This will be used to set the Navigation Timing API navigationStart
4106 // parameter for browser navigations in new tabs (intents, tabs opened through
4107 // "Open link in new tab"). If the navigation must wait on the current
4108 // RenderFrameHost to execute its BeforeUnload event, the navigation start
4109 // will be updated when the BeforeUnload ack is received.
Mike Jacksone2aa7af2023-05-17 06:45:074110
clamyea99ea12018-05-28 13:54:234111 base::TimeTicks navigation_start = base::TimeTicks::Now();
Mike Jacksone2aa7af2023-05-17 06:45:074112 const auto navigation_start_system_entropy =
4113 SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode(
4114 frame_tree_node, blink::mojom::SystemEntropy::kNormal);
clamyea99ea12018-05-28 13:54:234115
danakjd83d706d2020-11-25 22:11:124116 // Look for a pending commit that is to another document in this
4117 // FrameTreeNode. If one exists, then the last committed URL will not be the
4118 // current URL by the time this navigation commits.
4119 bool has_pending_cross_document_commit =
4120 frame_tree_node->render_manager()
4121 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:494122 bool is_currently_error_page =
Miyoung Shina2dd6a42021-10-07 12:19:214123 frame_tree_node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:124124
Minggang Wangb9f3fa92021-07-01 15:30:314125 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:124126 /*old_url=*/frame_tree_node->current_url(),
4127 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:494128 has_pending_cross_document_commit, is_currently_error_page,
Garrett Tanzer405f3402022-07-21 20:12:494129 is_same_document_history_load,
Garrett Tanzer267c2b82022-07-26 16:53:134130 /*is_embedder_initiated_fenced_frame_navigation=*/false,
Garrett Tanzer405f3402022-07-21 20:12:494131 /*is_unfenced_top_navigation=*/false);
Camille Lamy5193caa2018-10-12 11:59:424132
4133 // A form submission may happen here if the navigation is a
4134 // back/forward/reload navigation that does a form resubmission.
4135 scoped_refptr<network::ResourceRequestBody> request_body;
4136 std::string post_content_type;
jongdeok.kim5de823b32022-06-14 04:37:504137 // TODO(https://crbug.com/931209) Store |is_form_submission| in the history
4138 // entry. This way, it could be directly retrieved here. Right now, it is only
4139 // partially recovered when request.method == "POST" and request.body exists.
4140 bool is_form_submission = false;
Camille Lamy5193caa2018-10-12 11:59:424141 if (frame_entry->method() == "POST") {
4142 request_body = frame_entry->GetPostData(&post_content_type);
4143 // Might have a LF at end.
Peter Kastingb53b81912021-04-28 19:23:304144 post_content_type = std::string(
4145 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL));
jongdeok.kim5de823b32022-06-14 04:37:504146 is_form_submission = !!request_body;
Camille Lamy5193caa2018-10-12 11:59:424147 }
4148
4149 // Create the NavigationParams based on |entry| and |frame_entry|.
Minggang Wangb9f3fa92021-07-01 15:30:314150 blink::mojom::CommonNavigationParamsPtr common_params =
Lucas Furukawa Gadanief8290a2019-07-29 20:27:514151 entry->ConstructCommonNavigationParams(
4152 *frame_entry, request_body, dest_url,
4153 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
Tarun Bansalbcd62c82022-01-18 17:27:384154 navigation_type, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:534155 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:514156 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:554157 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:424158
4159 // TODO(clamy): |intended_as_new_entry| below should always be false once
4160 // Reload no longer leads to this being called for a pending NavigationEntry
4161 // of index -1.
Minggang Wangb9f3fa92021-07-01 15:30:314162 blink::mojom::CommitNavigationParamsPtr commit_params =
Lucas Furukawa Gadania9c45682019-07-31 22:05:144163 entry->ConstructCommitNavigationParams(
Rakina Zata Amnic7367852022-11-07 17:10:404164 *frame_entry, common_params->url, common_params->method,
4165 entry->GetSubframeUniqueNames(frame_tree_node),
Lucas Furukawa Gadania9c45682019-07-31 22:05:144166 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:534167 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
Liam Bradyd2a41e152022-07-19 13:58:484168 frame_tree_node->pending_frame_policy(),
Yoav Weiss8c573952022-11-17 17:35:134169 frame_tree_node->AncestorOrSelfHasCSPEE(),
Mike Jacksone2aa7af2023-05-17 06:45:074170 navigation_start_system_entropy, soft_navigation_heuristics_task_id);
Lucas Furukawa Gadania9c45682019-07-31 22:05:144171 commit_params->post_content_type = post_content_type;
Mike Jacksone2aa7af2023-05-17 06:45:074172 commit_params->navigation_timing->system_entropy_at_navigation_start =
4173 SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode(
4174 frame_tree_node, blink::mojom::SystemEntropy::kNormal);
Camille Lamy5193caa2018-10-12 11:59:424175
W. James MacLeanb7d6092682022-10-05 15:23:264176 if (common_params->url.IsAboutSrcdoc()) {
4177 // TODO(wjmaclean): initialize this in NavigationRequest's constructor
4178 // instead.
W. James MacLean81b8d01f2022-01-25 20:50:594179 commit_params->srcdoc_value = frame_tree_node->srcdoc_value();
W. James MacLeanb7d6092682022-10-05 15:23:264180 }
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:334181 const bool is_browser_initiated = !initiator_frame_token;
Alex Moshchuk9321e6a2022-12-07 21:58:314182 return NavigationRequest::Create(
Lucas Furukawa Gadania9c45682019-07-31 22:05:144183 frame_tree_node, std::move(common_params), std::move(commit_params),
Nate Chapin45f620582021-09-30 17:45:434184 is_browser_initiated, false /* was_opener_suppressed */,
Hiroshige Hayashizaki5466bfe82023-05-17 00:34:334185 initiator_frame_token, initiator_process_id, entry->extra_headers(),
4186 frame_entry, entry, is_form_submission, nullptr /* navigation_ui_data */,
Arthur Sonzognic686e8f2024-01-11 08:36:374187 std::nullopt /* impression */,
Yao Xiao720ef9d62022-12-09 05:18:294188 blink::mojom::NavigationInitiatorActivationAndAdStatus::
4189 kDidNotStartWithTransientActivation,
Daniel Hosseinianf0fbfb42021-09-08 02:20:474190 false /* is_pdf */);
clamyea99ea12018-05-28 13:54:234191}
4192
[email protected]d202a7c2012-01-04 07:53:474193void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:214194 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:274195 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:404196
Takashi Toyoshimaea534ef22021-07-21 03:27:594197 // We need to notify the ssl_manager_ before the WebContents so the
[email protected]df1af242009-05-01 00:11:404198 // location bar will have up-to-date information about the security style
4199 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:134200 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:404201
Abhijeet Kandalkar3dc6e602022-11-09 05:08:374202 delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:574203 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:464204
[email protected]b0f724c2013-09-05 04:21:134205 // TODO(avi): Remove. http://crbug.com/170921
4206 NotificationDetails notification_details =
4207 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:154208 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
4209 Source<NavigationController>(this),
4210 notification_details);
initial.commit09911bf2008-07-26 23:55:294211}
4212
initial.commit09911bf2008-07-26 23:55:294213// static
[email protected]d202a7c2012-01-04 07:53:474214size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:234215 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:154216 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:214217 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:234218}
4219
[email protected]d202a7c2012-01-04 07:53:474220void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:224221 if (is_active && needs_reload_)
4222 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:294223}
4224
[email protected]d202a7c2012-01-04 07:53:474225void NavigationControllerImpl::LoadIfNecessary() {
Rakina Zata Amnid605d462022-06-01 10:17:034226 SCOPED_CRASH_KEY_BOOL("nav_reentrancy_caller1", "LoadIf_pending",
4227 !!pending_entry_);
initial.commit09911bf2008-07-26 23:55:294228 if (!needs_reload_)
4229 return;
4230
Bo Liucdfa4b12018-11-06 00:21:444231 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
4232 needs_reload_type_);
4233
initial.commit09911bf2008-07-26 23:55:294234 // Calling Reload() results in ignoring state, and not loading.
4235 // Explicitly use NavigateToPendingEntry so that the renderer uses the
4236 // cached state.
avicc872d7242015-08-19 21:26:344237 if (pending_entry_) {
Yoav Weiss8c573952022-11-17 17:35:134238 NavigateToExistingPendingEntry(
4239 ReloadType::NONE,
4240 /*initiator_rfh=*/nullptr,
Arthur Sonzognic686e8f2024-01-11 08:36:374241 /*soft_navigation_heuristics_task_id=*/std::nullopt,
Yoav Weiss8c573952022-11-17 17:35:134242 /*navigation_api_key=*/nullptr);
Rakina Zata Amnie2d31312022-11-18 03:38:454243 } else if (!GetLastCommittedEntry()
Rakina Zata Amnif297a802022-01-18 03:53:434244 ->IsInitialEntryNotForSynchronousAboutBlank()) {
arthursonzogni5c4c202d2017-04-25 23:41:274245 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:344246 pending_entry_index_ = last_committed_entry_index_;
Yoav Weiss8c573952022-11-17 17:35:134247 NavigateToExistingPendingEntry(
4248 ReloadType::NONE,
4249 /*initiator_rfh=*/nullptr,
Arthur Sonzognic686e8f2024-01-11 08:36:374250 /*soft_navigation_heuristics_task_id=*/std::nullopt,
Yoav Weiss8c573952022-11-17 17:35:134251 /*navigation_api_key=*/nullptr);
avicc872d7242015-08-19 21:26:344252 } else {
Rakina Zata Amnif297a802022-01-18 03:53:434253 // We should never navigate to an existing initial NavigationEntry that is
4254 // the initial NavigationEntry for the initial empty document that hasn't
4255 // been overridden by the synchronous about:blank commit, to preserve
4256 // legacy behavior where trying to reload when the main frame is on the
4257 // initial empty document won't result in a navigation. See also
4258 // https://crbug.com/1277414. If there is something to reload, the
4259 // successful reload will clear the |needs_reload_| flag. Otherwise, just do
4260 // it here.
avicc872d7242015-08-19 21:26:344261 needs_reload_ = false;
4262 }
initial.commit09911bf2008-07-26 23:55:294263}
4264
Kevin McNeeccca6172021-10-19 17:11:144265base::WeakPtr<NavigationHandle>
4266NavigationControllerImpl::LoadPostCommitErrorPage(
Carlos IL42b416592019-10-07 23:10:364267 RenderFrameHost* render_frame_host,
4268 const GURL& url,
Lei Zhanga4770832023-07-19 18:02:364269 const std::string& error_page_html) {
Rakina Zata Amni919b7922020-12-11 09:03:134270 RenderFrameHostImpl* rfhi =
4271 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:584272
4273 // Only active documents can load post-commit error pages:
4274 // - If the document is in pending deletion, the browser already committed to
4275 // destroying this RenderFrameHost so ignore loading the error page.
4276 // - If the document is in back-forward cache, it's not allowed to navigate as
4277 // it should remain frozen. Ignore the request and evict the document from
4278 // back-forward cache.
4279 // - If the document is prerendering, it can navigate but when loading error
4280 // pages, cancel prerendering.
Fergal Daly1336ac642021-09-14 15:13:114281 if (rfhi->IsInactiveAndDisallowActivation(
4282 DisallowActivationReasonId::kLoadPostCommitErrorPage)) {
Kevin McNeeccca6172021-10-19 17:11:144283 return nullptr;
Fergal Daly1336ac642021-09-14 15:13:114284 }
Sreeja Kamishettydb8e2892021-03-10 09:30:584285
Rakina Zata Amni919b7922020-12-11 09:03:134286 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:414287
Minggang Wangb9f3fa92021-07-01 15:30:314288 blink::mojom::CommonNavigationParamsPtr common_params =
Minggang Wanga13c796e2021-07-02 05:54:434289 blink::CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:594290 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
4291 // the frame actually committed (e.g. iframe with "src" set to a
4292 // slow-responding URL). We should rewrite the URL to about:blank in this
4293 // case, as the renderer will only think a page is an error page if it has a
4294 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:134295 common_params->url = url.is_empty() ? GURL("about:blank") : url;
Minggang Wangb9f3fa92021-07-01 15:30:314296 blink::mojom::CommitNavigationParamsPtr commit_params =
Minggang Wanga13c796e2021-07-02 05:54:434297 blink::CreateCommitNavigationParams();
Antonio Sartori58591c892021-04-21 06:54:334298 commit_params->original_url = common_params->url;
John Delaney131ad362019-08-08 21:57:414299
Mike Jacksone2aa7af2023-05-17 06:45:074300 commit_params->navigation_timing->system_entropy_at_navigation_start =
4301 SystemEntropyUtils::ComputeSystemEntropyForFrameTreeNode(
4302 node, blink::mojom::SystemEntropy::kNormal);
4303
arthursonzogni70ac7302020-05-28 08:49:054304 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
4305 // error pages.
Liam Brady34639ec92023-10-13 19:50:454306 commit_params->frame_policy = node->pending_frame_policy();
arthursonzogni70ac7302020-05-28 08:49:054307
John Delaney131ad362019-08-08 21:57:414308 std::unique_ptr<NavigationRequest> navigation_request =
4309 NavigationRequest::CreateBrowserInitiated(
4310 node, std::move(common_params), std::move(commit_params),
Hiroshige Hayashizakif07ad7812023-05-10 02:26:094311 false /* was_opener_suppressed */, "" /* extra_headers */,
4312 nullptr /* frame_entry */, nullptr /* entry */,
4313 false /* is_form_submission */, nullptr /* navigation_ui_data */,
Arthur Sonzognic686e8f2024-01-11 08:36:374314 std::nullopt /* impression */, false /* is_pdf */);
Carlos IL42b416592019-10-07 23:10:364315 navigation_request->set_post_commit_error_page_html(error_page_html);
Lei Zhanga4770832023-07-19 18:02:364316 navigation_request->set_net_error(net::ERR_BLOCKED_BY_CLIENT);
Charlie Reis09952ee2022-12-08 16:35:074317 node->TakeNavigationRequest(std::move(navigation_request));
John Delaney131ad362019-08-08 21:57:414318 DCHECK(node->navigation_request());
Kevin McNeeccca6172021-10-19 17:11:144319
4320 // Calling BeginNavigation may destroy the NavigationRequest.
4321 base::WeakPtr<NavigationRequest> created_navigation_request(
4322 node->navigation_request()->GetWeakPtr());
John Delaney131ad362019-08-08 21:57:414323 node->navigation_request()->BeginNavigation();
Kevin McNeeccca6172021-10-19 17:11:144324 return created_navigation_request;
John Delaney131ad362019-08-08 21:57:414325}
4326
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574327void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:214328 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:094329 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:154330 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:144331 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:294332}
4333
[email protected]d202a7c2012-01-04 07:53:474334void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:364335 RestoreType type) {
Charlie Reis23c26da2022-01-29 00:57:474336 // TODO(https://crbug.com/1287624): Don't allow an index of -1, which would
4337 // represent a no-committed-entry state.
4338 DCHECK(selected_index >= -1 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:364339 ConfigureEntriesForRestore(&entries_, type);
Charlie Reis23c26da2022-01-29 00:57:474340 // TODO(https://crbug.com/1287624): This will be pointing to the wrong entry
4341 // if `entries_` contains pre-existing entries from the NavigationController
4342 // before restore, which would not be removed and will be at the front of the
4343 // entries list, causing the index to be off by the amount of pre-existing
4344 // entries in the list. Fix this to point to the correct entry.
initial.commit09911bf2008-07-26 23:55:294345 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:294346}
[email protected]765b35502008-08-21 00:51:204347
arthursonzogni69a6a1b2019-09-17 09:23:004348void NavigationControllerImpl::DiscardNonCommittedEntries() {
Rakina Zata Amnia4e27222021-12-22 01:05:004349 DiscardNonCommittedEntriesWithCommitDetails(nullptr /* commit_details */);
4350}
4351
4352void NavigationControllerImpl::DiscardNonCommittedEntriesWithCommitDetails(
4353 LoadCommittedDetails* commit_details) {
Michael Thiessen9b14d512019-09-23 21:19:474354 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:104355 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
4356 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:294357 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:474358 return;
Michael Thiessenc5676d22019-09-25 22:32:104359 }
avi45a72532015-04-07 21:01:454360 DiscardPendingEntry(false);
Rakina Zata Amnidaa84f62022-02-17 00:55:314361
4362 if (!delegate_)
4363 return;
Abhijeet Kandalkar3dc6e602022-11-09 05:08:374364 delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:484365}
4366
avi7c6f35e2015-05-08 17:52:384367int NavigationControllerImpl::GetEntryIndexWithUniqueID(
4368 int nav_entry_id) const {
4369 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
4370 if (entries_[i]->GetUniqueID() == nav_entry_id)
4371 return i;
4372 }
4373 return -1;
4374}
4375
[email protected]d202a7c2012-01-04 07:53:474376void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574377 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:254378 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574379 DCHECK_LE(max_index, source->GetEntryCount());
Kevin McNee433daf242023-10-31 20:15:594380 NavigationEntryRestoreContextImpl context;
[email protected]e1cd5452010-08-26 18:03:254381 for (int i = 0; i < max_index; i++) {
Nate Chapin9f169072021-06-09 19:32:374382 // Normally, cloning a NavigationEntryImpl results in sharing
4383 // FrameNavigationEntries between the original and the clone. However, when
4384 // cloning from a different NavigationControllerImpl, we want to fork the
4385 // FrameNavigationEntries.
Nate Chapin9f169072021-06-09 19:32:374386 entries_.insert(entries_.begin() + i,
Kevin McNee433daf242023-10-31 20:15:594387 source->entries_[i]->CloneWithoutSharing(&context));
[email protected]e1cd5452010-08-26 18:03:254388 }
Rakina Zata Amnie2d31312022-11-18 03:38:454389 DCHECK_GE(entries_.size(), 1u);
arthursonzogni5c4c202d2017-04-25 23:41:274390 DCHECK(pending_entry_index_ == -1 ||
4391 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:254392}
[email protected]c5b88d82012-10-06 17:03:334393
4394void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:184395 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:334396 get_timestamp_callback_ = get_timestamp_callback;
4397}
[email protected]8ff00d72012-10-23 19:12:214398
Shivani Sharmaffb32b82019-04-09 16:58:474399// History manipulation intervention:
4400void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:474401 bool replace_entry,
Shivani Sharmaeef521b2024-01-18 13:03:564402 bool previous_document_had_history_intervention_activation,
Alexander Timine3ec4192020-04-20 16:39:404403 bool is_renderer_initiated,
4404 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharmaeef521b2024-01-18 13:03:564405 // Note that for a subframe,
4406 // previous_document_had_history_intervention_activation is true if the
Shivani Sharma712d5d72019-04-16 21:56:454407 // gesture happened in any subframe (propagated to main frame) or in the main
4408 // frame itself.
Shivani Sharmaeef521b2024-01-18 13:03:564409 if (replace_entry || previous_document_had_history_intervention_activation ||
shivanigithubcceeacf2020-03-06 20:00:274410 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:474411 return;
4412 }
4413 if (last_committed_entry_index_ == -1)
4414 return;
4415
Shivani Sharmac4cc8922019-04-18 03:11:174416 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:474417
Alexander Timine3ec4192020-04-20 16:39:404418 // Log UKM with the URL we are navigating away from.
4419 ukm::builders::HistoryManipulationIntervention(
4420 previous_page_load_ukm_source_id)
4421 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:474422}
4423
Shivani Sharmac4cc8922019-04-18 03:11:174424void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
4425 int reference_index,
4426 bool skippable) {
4427 auto* reference_entry = GetEntryAtIndex(reference_index);
4428 reference_entry->set_should_skip_on_back_forward_ui(skippable);
4429
4430 int64_t document_sequence_number =
4431 reference_entry->root_node()->frame_entry->document_sequence_number();
4432 for (int index = 0; index < GetEntryCount(); index++) {
4433 auto* entry = GetEntryAtIndex(index);
4434 if (entry->root_node()->frame_entry->document_sequence_number() ==
4435 document_sequence_number) {
4436 entry->set_should_skip_on_back_forward_ui(skippable);
4437 }
4438 }
4439}
4440
arthursonzogni66f711c2019-10-08 14:40:364441std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
4442NavigationControllerImpl::ReferencePendingEntry() {
4443 DCHECK(pending_entry_);
4444 auto pending_entry_ref =
4445 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
4446 pending_entry_refs_.insert(pending_entry_ref.get());
4447 return pending_entry_ref;
4448}
4449
4450void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
4451 // Ignore refs that don't correspond to the current pending entry.
4452 auto it = pending_entry_refs_.find(ref);
4453 if (it == pending_entry_refs_.end())
4454 return;
4455 pending_entry_refs_.erase(it);
4456
4457 if (!pending_entry_refs_.empty())
4458 return;
4459
4460 // The pending entry may be deleted before the last PendingEntryRef.
4461 if (!pending_entry_)
4462 return;
4463
4464 // We usually clear the pending entry when the matching NavigationRequest
4465 // fails, so that an arbitrary URL isn't left visible above a committed page.
4466 //
4467 // However, we do preserve the pending entry in some cases, such as on the
4468 // initial navigation of an unmodified blank tab. We also allow the delegate
4469 // to say when it's safe to leave aborted URLs in the omnibox, to let the
4470 // user edit the URL and try again. This may be useful in cases that the
4471 // committed page cannot be attacker-controlled. In these cases, we still
4472 // allow the view to clear the pending entry and typed URL if the user
4473 // requests (e.g., hitting Escape with focus in the address bar).
4474 //
4475 // Do not leave the pending entry visible if it has an invalid URL, since this
4476 // might be formatted in an unexpected or unsafe way.
4477 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:364478 bool should_preserve_entry =
4479 (pending_entry_ == GetVisibleEntry()) &&
4480 pending_entry_->GetURL().is_valid() &&
4481 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
4482 if (should_preserve_entry)
4483 return;
4484
4485 DiscardPendingEntry(true);
Abhijeet Kandalkar3dc6e602022-11-09 05:08:374486 delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_URL);
arthursonzogni66f711c2019-10-08 14:40:364487}
4488
Titouan Rigoudy6ec70402021-02-02 15:42:194489std::unique_ptr<PolicyContainerPolicies>
4490NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:394491 RenderFrameHostImpl* rfh,
4492 bool is_same_document,
Rakina Zata Amniafd3c6582021-11-30 06:19:174493 const GURL& url) {
Antonio Sartori78a749f2020-11-30 12:03:394494 if (is_same_document) {
Rakina Zata Amnie2d31312022-11-18 03:38:454495 DCHECK(GetLastCommittedEntry());
Antonio Sartori78a749f2020-11-30 12:03:394496 FrameNavigationEntry* previous_frame_entry =
4497 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
4498
4499 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
4500 // ensure we always have a FrameNavigationEntry here.
4501 if (!previous_frame_entry)
4502 return nullptr;
4503
Titouan Rigoudy6ec70402021-02-02 15:42:194504 const PolicyContainerPolicies* previous_policies =
4505 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:394506
Titouan Rigoudy6ec70402021-02-02 15:42:194507 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:394508 return nullptr;
4509
4510 // Make a copy of the policy container for the new FrameNavigationEntry.
Titouan Rigoudy72f892d2022-05-02 18:21:234511 return previous_policies->ClonePtr();
Antonio Sartori78a749f2020-11-30 12:03:394512 }
4513
Titouan Rigoudy72f892d2022-05-02 18:21:234514 return rfh->policy_container_host()->policies().ClonePtr();
Antonio Sartori78a749f2020-11-30 12:03:394515}
4516
Hayato Ito2c8c08d02021-06-23 03:38:434517void NavigationControllerImpl::BroadcastHistoryOffsetAndLength() {
Carlos Caballeroede6f8c2021-01-28 11:01:504518 OPTIONAL_TRACE_EVENT2(
Hayato Ito2c8c08d02021-06-23 03:38:434519 "content", "NavigationControllerImpl::BroadcastHistoryOffsetAndLength",
4520 "history_offset", GetLastCommittedEntryIndex(), "history_length",
4521 GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:504522
4523 auto callback = base::BindRepeating(
4524 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
4525 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
4526 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
4527 }
4528 },
Hayato Ito2c8c08d02021-06-23 03:38:434529 GetLastCommittedEntryIndex(), GetEntryCount());
Ali Hijazid87307d2022-11-07 20:15:034530 frame_tree_->root()->render_manager()->ExecutePageBroadcastMethod(callback);
Carlos Caballeroede6f8c2021-01-28 11:01:504531}
4532
4533void NavigationControllerImpl::DidAccessInitialMainDocument() {
4534 // We may have left a failed browser-initiated navigation in the address bar
4535 // to let the user edit it and try again. Clear it now that content might
4536 // show up underneath it.
Ali Hijazid87307d2022-11-07 20:15:034537 if (!frame_tree_->IsLoadingIncludingInnerFrameTrees() && GetPendingEntry())
Carlos Caballeroede6f8c2021-01-28 11:01:504538 DiscardPendingEntry(false);
4539
4540 // Update the URL display.
Abhijeet Kandalkar3dc6e602022-11-09 05:08:374541 delegate_->NotifyNavigationStateChangedFromController(INVALIDATE_TYPE_URL);
Carlos Caballeroede6f8c2021-01-28 11:01:504542}
4543
4544void NavigationControllerImpl::UpdateStateForFrame(
4545 RenderFrameHostImpl* rfhi,
4546 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:564547 OPTIONAL_TRACE_EVENT1("content",
4548 "NavigationControllerImpl::UpdateStateForFrame",
4549 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:504550 // The state update affects the last NavigationEntry associated with the given
4551 // |render_frame_host|. This may not be the last committed NavigationEntry (as
4552 // in the case of an UpdateState from a frame being swapped out). We track
4553 // which entry this is in the RenderFrameHost's nav_entry_id.
4554 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
4555 if (!entry)
4556 return;
4557
4558 FrameNavigationEntry* frame_entry =
4559 entry->GetFrameEntry(rfhi->frame_tree_node());
4560 if (!frame_entry)
4561 return;
4562
4563 // The SiteInstance might not match if we do a cross-process navigation with
4564 // replacement (e.g., auto-subframe), in which case the swap out of the old
4565 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4566 // has already been replaced and destroyed.
4567 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4568 return;
4569
4570 if (page_state == frame_entry->page_state())
4571 return; // Nothing to update.
4572
4573 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4574
4575 // The document_sequence_number and item_sequence_number recorded in the
4576 // FrameNavigationEntry should not differ from the one coming with the update,
4577 // since it must come from the same document. Do not update it if a difference
4578 // is detected, as this indicates that |frame_entry| is not the correct one.
4579 blink::ExplodedPageState exploded_state;
4580 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4581 return;
4582
4583 if (exploded_state.top.document_sequence_number !=
4584 frame_entry->document_sequence_number() ||
4585 exploded_state.top.item_sequence_number !=
4586 frame_entry->item_sequence_number()) {
4587 return;
4588 }
4589
4590 frame_entry->SetPageState(page_state);
4591 NotifyEntryChanged(entry);
4592}
4593
Nate Chapina2c881f52023-11-07 17:02:094594namespace {
4595
4596// The caller is responsible for ensuring the entry is same-origin to the
4597// origin to be committed.
4598blink::mojom::NavigationApiHistoryEntryPtr ToNavigationApiHistoryEntry(
4599 FrameNavigationEntry* frame_entry,
4600 int64_t pending_document_sequence_number) {
4601 blink::ExplodedPageState exploded_state;
4602 if (!blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4603 &exploded_state)) {
4604 return nullptr;
4605 }
4606 blink::ExplodedFrameState frame_state = exploded_state.top;
4607
4608 // If the document represented by this FNE hid its full url from appearing in
4609 // a referrer via a "no-referrer" or "origin" referrer policy, censor the url
4610 // in the navigation API as well (unless we're navigating to that document).
4611 std::u16string url;
4612 if (pending_document_sequence_number ==
4613 frame_entry->document_sequence_number() ||
4614 !frame_entry->protect_url_in_navigation_api()) {
4615 url = frame_state.url_string.value_or(std::u16string());
4616 }
4617
4618 return blink::mojom::NavigationApiHistoryEntry::New(
4619 frame_state.navigation_api_key.value_or(std::u16string()),
4620 frame_state.navigation_api_id.value_or(std::u16string()), url,
4621 frame_state.item_sequence_number, frame_state.document_sequence_number,
4622 frame_state.navigation_api_state);
4623}
4624
4625} // namespace
4626
Domenic Denicolacd30f5f82022-03-16 21:48:014627std::vector<blink::mojom::NavigationApiHistoryEntryPtr>
4628NavigationControllerImpl::PopulateSingleNavigationApiHistoryEntryVector(
Nate Chapind1fe3612021-04-16 20:45:574629 Direction direction,
4630 int entry_index,
4631 const url::Origin& pending_origin,
4632 FrameTreeNode* node,
4633 SiteInstance* site_instance,
Nate Chapin63db0d12022-01-20 22:03:304634 int64_t pending_item_sequence_number,
Nate Chapina2c881f52023-11-07 17:02:094635 int64_t pending_document_sequence_number,
4636 int& last_index_checked) {
Domenic Denicolacd30f5f82022-03-16 21:48:014637 std::vector<blink::mojom::NavigationApiHistoryEntryPtr> entries;
Rakina Zata Amnie2d31312022-11-18 03:38:454638 if (GetLastCommittedEntry()->IsInitialEntry()) {
Rakina Zata Amniafd3c6582021-11-30 06:19:174639 // Don't process the initial entry.
4640 DCHECK_EQ(GetEntryCount(), 1);
4641 return entries;
4642 }
Nate Chapind1fe3612021-04-16 20:45:574643 int offset = direction == Direction::kForward ? 1 : -1;
Nate Chapin63db0d12022-01-20 22:03:304644 int64_t previous_item_sequence_number = pending_item_sequence_number;
Nate Chapind1fe3612021-04-16 20:45:574645 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4646 i += offset) {
Nate Chapina2c881f52023-11-07 17:02:094647 last_index_checked = i;
Nate Chapind1fe3612021-04-16 20:45:574648 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
Nate Chapindedfa642022-01-28 23:59:414649 if (!frame_entry)
Nate Chapind1fe3612021-04-16 20:45:574650 break;
Domenic Denicolacd30f5f82022-03-16 21:48:014651 // An entry should only appear in the navigation API entries if it is for
4652 // the same origin as the document being committed. Check the committed
4653 // origin, or if that is not available (during restore), check against the
4654 // FNE's url.
Nate Chapina2c881f52023-11-07 17:02:094655 // TODO(crbug.com/1209092): Move this into ToNavigationApiHistoryEntry()
4656 // once we can be sure that entries with the same ISN will never be
4657 // cross-origin.
Nate Chapindedfa642022-01-28 23:59:414658 url::Origin frame_entry_origin =
4659 frame_entry->committed_origin().value_or(url::Origin::Resolve(
4660 frame_entry->url(),
4661 frame_entry->initiator_origin().value_or(url::Origin())));
4662 if (!pending_origin.IsSameOriginWith(frame_entry_origin))
Nate Chapind1fe3612021-04-16 20:45:574663 break;
4664 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4665 continue;
Nate Chapina2c881f52023-11-07 17:02:094666 if (blink::mojom::NavigationApiHistoryEntryPtr entry =
4667 ToNavigationApiHistoryEntry(frame_entry,
4668 pending_document_sequence_number)) {
Nate Chapin63db0d12022-01-20 22:03:304669 DCHECK(entry->url.empty() ||
4670 pending_origin.CanBeDerivedFrom(GURL(entry->url)));
Nate Chapind1fe3612021-04-16 20:45:574671 entries.push_back(std::move(entry));
4672 previous_item_sequence_number = frame_entry->item_sequence_number();
4673 }
4674 }
4675 // If |entries| was constructed by iterating backwards from
4676 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4677 // earliest-at-the-front. Reverse it.
4678 if (direction == Direction::kBack)
4679 std::reverse(entries.begin(), entries.end());
4680 return entries;
4681}
4682
Domenic Denicolacd30f5f82022-03-16 21:48:014683blink::mojom::NavigationApiHistoryEntryArraysPtr
4684NavigationControllerImpl::GetNavigationApiHistoryEntryVectors(
Nate Chapin97d2f542022-02-18 01:34:554685 FrameTreeNode* node,
Nate Chapind1fe3612021-04-16 20:45:574686 NavigationRequest* request) {
Rakina Zata Amnic7367852022-11-07 17:10:404687 url::Origin pending_origin = request
Lukasz Anforowicz435e68d2022-11-09 21:47:444688 ? request->GetOriginToCommit().value()
Rakina Zata Amnic7367852022-11-07 17:10:404689 : url::Origin::Create(node->current_url());
Nate Chapind1fe3612021-04-16 20:45:574690
Nate Chapind1fe3612021-04-16 20:45:574691 scoped_refptr<SiteInstance> site_instance =
Nate Chapin97d2f542022-02-18 01:34:554692 node->current_frame_host()->GetSiteInstance();
Nate Chapind1fe3612021-04-16 20:45:574693
Nate Chapine82339d02022-05-03 23:48:254694 // NOTE: |entry_index| is the index where this entry will commit if no
4695 // modifications are made between now and DidCommitNavigation. This is used to
4696 // walk |entries_| and determine which entries should be exposed by the
4697 // navigation API. It is important to calculate this correctly, because blink
4698 // will cancel a same-document history commit if it's not present in the
4699 // entries blink knows about.
4700 int entry_index = GetLastCommittedEntryIndex();
Nate Chapind1fe3612021-04-16 20:45:574701 int64_t pending_item_sequence_number = 0;
Nate Chapin63db0d12022-01-20 22:03:304702 int64_t pending_document_sequence_number = 0;
Nate Chapine82339d02022-05-03 23:48:254703 bool will_create_new_entry = false;
4704 if (GetPendingEntryIndex() != -1) {
4705 entry_index = GetPendingEntryIndex();
4706 if (auto* frame_entry = GetPendingEntry()->GetFrameEntry(node)) {
4707 pending_item_sequence_number = frame_entry->item_sequence_number();
4708 pending_document_sequence_number =
4709 frame_entry->document_sequence_number();
4710 }
4711 } else if (request &&
4712 !NavigationTypeUtils::IsReload(
4713 request->common_params().navigation_type) &&
4714 !NavigationTypeUtils::IsHistory(
4715 request->common_params().navigation_type) &&
4716 !request->common_params().should_replace_current_entry &&
4717 !request->common_params()
4718 .is_history_navigation_in_new_child_frame) {
4719 will_create_new_entry = true;
4720 entry_index = GetLastCommittedEntryIndex() + 1;
4721 // Don't set pending_item_sequence_number or
4722 // pending_document_sequence_number in this case - a new unique isn/dsn will
4723 // be calculated in the renderer later.
4724 } else if (GetLastCommittedEntryIndex() != -1) {
4725 entry_index = GetLastCommittedEntryIndex();
4726 if (auto* frame_entry = GetLastCommittedEntry()->GetFrameEntry(node)) {
Nate Chapind1fe3612021-04-16 20:45:574727 pending_item_sequence_number = frame_entry->item_sequence_number();
Nate Chapin63db0d12022-01-20 22:03:304728 pending_document_sequence_number =
4729 frame_entry->document_sequence_number();
4730 }
Nate Chapind1fe3612021-04-16 20:45:574731 }
4732
Domenic Denicolacd30f5f82022-03-16 21:48:014733 auto entry_arrays = blink::mojom::NavigationApiHistoryEntryArrays::New();
Nate Chapine82339d02022-05-03 23:48:254734 if (entry_index == -1) {
4735 // TODO(rakina): Exit early when there is no last committed entry.
4736 // Remove when InitialNavigationEntry ships.
4737 return entry_arrays;
4738 }
4739
Nate Chapina2c881f52023-11-07 17:02:094740 int backmost_index = entry_index;
Domenic Denicolacd30f5f82022-03-16 21:48:014741 entry_arrays->back_entries = PopulateSingleNavigationApiHistoryEntryVector(
Nate Chapind1fe3612021-04-16 20:45:574742 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
Nate Chapina2c881f52023-11-07 17:02:094743 pending_item_sequence_number, pending_document_sequence_number,
4744 backmost_index);
Nate Chapind1fe3612021-04-16 20:45:574745
4746 // Don't populate forward entries if they will be truncated by a new entry.
Nate Chapina2c881f52023-11-07 17:02:094747 int forwardmost_index = entry_index;
Nate Chapind1fe3612021-04-16 20:45:574748 if (!will_create_new_entry) {
Domenic Denicolacd30f5f82022-03-16 21:48:014749 entry_arrays->forward_entries =
4750 PopulateSingleNavigationApiHistoryEntryVector(
4751 Direction::kForward, entry_index, pending_origin, node,
4752 site_instance.get(), pending_item_sequence_number,
Nate Chapina2c881f52023-11-07 17:02:094753 pending_document_sequence_number, forwardmost_index);
4754 }
4755
4756 // If the previous entry is within the block of contiguous entries being
4757 // provided, then report it as the `previous_entry`.
Nate Chapinc06cf80a2023-11-18 01:12:254758 FrameNavigationEntry* previous_entry = nullptr;
4759 if (frame_tree_->is_prerendering()) {
4760 int initiator_id = PrerenderHost::GetFromFrameTreeNode(*node)
4761 .initiator_frame_tree_node_id();
4762 if (initiator_id != RenderFrameHost::kNoFrameTreeNodeId) {
4763 auto* initiator_node = FrameTreeNode::GloballyFindByID(initiator_id);
4764 previous_entry = initiator_node->frame_tree()
4765 .controller()
4766 .GetLastCommittedEntry()
4767 ->GetFrameEntry(initiator_node);
4768 }
4769 } else if (GetLastCommittedEntryIndex() != -1 &&
4770 GetLastCommittedEntryIndex() >= backmost_index &&
4771 GetLastCommittedEntryIndex() <= forwardmost_index) {
4772 previous_entry = GetLastCommittedEntry()->GetFrameEntry(node);
4773 }
4774 if (previous_entry) {
4775 url::Origin previous_entry_origin =
4776 previous_entry->committed_origin().value_or(url::Origin::Resolve(
4777 previous_entry->url(),
4778 previous_entry->initiator_origin().value_or(url::Origin())));
4779 // TODO(crbug.com/1209092): Move this into ToNavigationApiHistoryEntry()
4780 // once we can be sure that entries with the same ISN will never be
4781 // cross-origin.
4782 if (pending_origin.IsSameOriginWith(previous_entry_origin)) {
4783 entry_arrays->previous_entry = ToNavigationApiHistoryEntry(
4784 previous_entry, pending_document_sequence_number);
Nate Chapina2c881f52023-11-07 17:02:094785 }
Nate Chapind1fe3612021-04-16 20:45:574786 }
Nate Chapinc06cf80a2023-11-18 01:12:254787
Nate Chapin4e657a472022-02-06 03:38:374788 return entry_arrays;
Nate Chapind1fe3612021-04-16 20:45:574789}
4790
Nate Chapinfbfe5af2021-06-10 17:22:084791NavigationControllerImpl::HistoryNavigationAction
Domenic Denicolacc094fb2022-03-16 23:40:574792NavigationControllerImpl::ShouldNavigateToEntryForNavigationApiKey(
Nate Chapinfbfe5af2021-06-10 17:22:084793 FrameNavigationEntry* current_entry,
4794 FrameNavigationEntry* target_entry,
Domenic Denicolacc094fb2022-03-16 23:40:574795 const std::string& navigation_api_key) {
Nate Chapinfbfe5af2021-06-10 17:22:084796 if (!target_entry || !target_entry->committed_origin())
4797 return HistoryNavigationAction::kStopLooking;
Nate Chapinfbfe5af2021-06-10 17:22:084798 if (!current_entry->committed_origin()->IsSameOriginWith(
4799 *target_entry->committed_origin())) {
4800 return HistoryNavigationAction::kStopLooking;
4801 }
4802
4803 // NOTE: We don't actually care between kSameDocument and
4804 // kDifferentDocument, so always use kDifferentDocument by convention.
Domenic Denicolacc094fb2022-03-16 23:40:574805 if (target_entry->navigation_api_key() == navigation_api_key)
Nate Chapinfbfe5af2021-06-10 17:22:084806 return HistoryNavigationAction::kDifferentDocument;
4807 return HistoryNavigationAction::kKeepLooking;
4808}
4809
Domenic Denicolacc094fb2022-03-16 23:40:574810void NavigationControllerImpl::NavigateToNavigationApiKey(
Nate Chapinbf682fa32022-09-26 22:41:204811 RenderFrameHostImpl* initiator_rfh,
Arthur Sonzognic686e8f2024-01-11 08:36:374812 std::optional<blink::scheduler::TaskAttributionId>
Yoav Weissa7449c3b2022-11-22 15:15:144813 soft_navigation_heuristics_task_id,
Domenic Denicolacc094fb2022-03-16 23:40:574814 const std::string& key) {
Nate Chapinbf682fa32022-09-26 22:41:204815 FrameTreeNode* node = initiator_rfh->frame_tree_node();
Nate Chapinfbfe5af2021-06-10 17:22:084816 FrameNavigationEntry* current_entry =
4817 GetLastCommittedEntry()->GetFrameEntry(node);
4818 if (!current_entry)
4819 return;
4820
Yoav Weiss8c573952022-11-17 17:35:134821 // TODO(https://crbug.com/1383704): Make sure that the right task ID is passed
4822 // when `navigation.traverseTo()` is called.
4823
Nate Chapinfbfe5af2021-06-10 17:22:084824 // We want to find the nearest matching entry that is contiguously
4825 // same-instance and same-origin. Check back first, then forward.
4826 // TODO(japhet): Link spec here once it exists.
4827 for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) {
Domenic Denicolacc094fb2022-03-16 23:40:574828 auto result = ShouldNavigateToEntryForNavigationApiKey(
Nate Chapinfbfe5af2021-06-10 17:22:084829 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4830 if (result == HistoryNavigationAction::kStopLooking)
4831 break;
4832 if (result != HistoryNavigationAction::kKeepLooking) {
Yoav Weissa7449c3b2022-11-22 15:15:144833 GoToIndex(i, initiator_rfh, soft_navigation_heuristics_task_id, &key);
Nate Chapinfbfe5af2021-06-10 17:22:084834 return;
4835 }
4836 }
4837 for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) {
Domenic Denicolacc094fb2022-03-16 23:40:574838 auto result = ShouldNavigateToEntryForNavigationApiKey(
Nate Chapinfbfe5af2021-06-10 17:22:084839 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4840 if (result == HistoryNavigationAction::kStopLooking)
4841 break;
4842 if (result != HistoryNavigationAction::kKeepLooking) {
Yoav Weissa7449c3b2022-11-22 15:15:144843 GoToIndex(i, initiator_rfh, soft_navigation_heuristics_task_id, &key);
Nate Chapinfbfe5af2021-06-10 17:22:084844 return;
4845 }
4846 }
Nate Chapinbf682fa32022-09-26 22:41:204847
4848 // If we fall through to here, a matching NavigationEntry couldn't be found.
4849 // Notify the renderer that the navigation was cancelled.
4850 initiator_rfh->GetAssociatedLocalFrame()->TraverseCancelled(
4851 key, blink::mojom::TraverseCancelledReason::kNotFound);
Nate Chapinfbfe5af2021-06-10 17:22:084852}
4853
Domenic Denicolacc094fb2022-03-16 23:40:574854bool NavigationControllerImpl::ShouldProtectUrlInNavigationApi(
Nate Chapin63db0d12022-01-20 22:03:304855 network::mojom::ReferrerPolicy referrer_policy) {
4856 return referrer_policy == network::mojom::ReferrerPolicy::kNever ||
4857 referrer_policy == network::mojom::ReferrerPolicy::kOrigin;
4858}
4859
shivanigithubf405bf0d2021-11-05 17:58:334860bool NavigationControllerImpl::ShouldMaintainTrivialSessionHistory(
4861 const FrameTreeNode* frame_tree_node) const {
4862 // TODO(https://crbug.com/1197384): We may have to add portals in addition to
4863 // prerender and fenced frames. This should be kept in sync with
Hayato Ito7a80db42021-07-05 06:18:544864 // LocalFrame version, LocalFrame::ShouldMaintainTrivialSessionHistory.
Takashi Toyoshima4dad2c12023-11-13 10:04:464865 // The preview mode appears as prerendered page in renderers, and
4866 // GetDocument()->IsPrerendering() covers the case together.
Ali Hijazid87307d2022-11-07 20:15:034867 return frame_tree_->is_prerendering() ||
Takashi Toyoshima4dad2c12023-11-13 10:04:464868 frame_tree_->page_delegate()->IsInPreviewMode() ||
shivanigithubf405bf0d2021-11-05 17:58:334869 frame_tree_node->IsInFencedFrameTree();
Hayato Ito7a80db42021-07-05 06:18:544870}
4871
Julie Jeongeun Kim0e242242022-11-30 10:45:094872void NavigationControllerImpl::DidChangeReferrerPolicy(
4873 FrameTreeNode* node,
4874 network::mojom::ReferrerPolicy referrer_policy) {
4875 FrameNavigationEntry* entry = GetLastCommittedEntry()->GetFrameEntry(node);
4876 if (!entry)
4877 return;
4878
4879 // The FrameNavigationEntry may want to change whether to protect its url
4880 // in the navigation API when the referrer policy changes.
4881 entry->set_protect_url_in_navigation_api(
4882 ShouldProtectUrlInNavigationApi(referrer_policy));
4883}
4884
[email protected]8ff00d72012-10-23 19:12:214885} // namespace content