blob: b70d62482a1278fba1dbb1198ac5a20a1b4965e9 [file] [log] [blame]
[email protected]d4a8ca482013-10-30 21:06:401// Copyright 2013 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
creis4e2ecb72015-06-20 00:46:305/*
6 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
7 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved.
9 * (http://www.torchmobile.com/)
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
21 * its contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
25 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
28 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
danakjc492bf82020-09-09 20:02:4436#include "content/browser/renderer_host/navigation_controller_impl.h"
initial.commit09911bf2008-07-26 23:55:2937
Lei Zhang96031532019-10-10 19:05:4738#include <algorithm>
dcheng36b6aec92015-12-26 06:16:3639#include <utility>
40
[email protected]c5b88d82012-10-06 17:03:3341#include "base/bind.h"
[email protected]eabfe1912014-05-12 10:07:2842#include "base/command_line.h"
Ayu Ishii2f825852022-03-08 19:47:3843#include "base/containers/adapters.h"
Rakina Zata Amni627360d2022-02-24 00:53:4044#include "base/debug/dump_without_crashing.h"
initial.commit09911bf2008-07-26 23:55:2945#include "base/logging.h"
asvitkine30330812016-08-30 04:01:0846#include "base/metrics/histogram_macros.h"
Charlie Reis23c26da2022-01-29 00:57:4747#include "base/numerics/safe_conversions.h"
Chris Hamilton83272dc2021-02-23 00:24:0248#include "base/stl_util.h"
Ryan Hamilton7f3bd3d2022-04-23 00:07:3949#include "base/strings/escape.h"
Peter Kastingb53b81912021-04-28 19:23:3050#include "base/strings/string_piece.h"
[email protected]348fbaac2013-06-11 06:31:5151#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0052#include "base/strings/utf_string_conversions.h"
[email protected]a43858f2013-06-28 15:18:3753#include "base/time/time.h"
Carlos Caballero40b0efd2021-01-26 11:55:0054#include "base/trace_event/optional_trace_event.h"
Carlos Caballeroede6f8c2021-01-28 11:01:5055#include "base/trace_event/trace_conversion_helper.h"
ssid3e765612015-01-28 04:03:4256#include "base/trace_event/trace_event.h"
servolkf3955532015-05-16 00:01:5957#include "build/build_config.h"
[email protected]eabfe1912014-05-12 10:07:2858#include "cc/base/switches.h"
jamescookda2505812015-03-20 18:01:1859#include "content/browser/bad_message.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3960#include "content/browser/blob_storage/chrome_blob_storage_context.h"
[email protected]825b1662012-03-12 19:07:3161#include "content/browser/browser_url_handler_impl.h"
[email protected]5f2aa722013-08-07 16:59:4162#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0463#include "content/browser/dom_storage/session_storage_namespace_impl.h"
danakjc492bf82020-09-09 20:02:4464#include "content/browser/renderer_host/debug_urls.h"
65#include "content/browser/renderer_host/frame_tree.h"
Carlos Caballero40b0efd2021-01-26 11:55:0066#include "content/browser/renderer_host/frame_tree_node.h"
Chris Bookholt27faf8d2022-01-20 01:03:3367#include "content/browser/renderer_host/navigation_controller_delegate.h"
danakjc492bf82020-09-09 20:02:4468#include "content/browser/renderer_host/navigation_entry_impl.h"
Nate Chapin214a86a2021-06-21 20:35:5769#include "content/browser/renderer_host/navigation_entry_restore_context_impl.h"
danakjc492bf82020-09-09 20:02:4470#include "content/browser/renderer_host/navigation_request.h"
71#include "content/browser/renderer_host/navigator.h"
72#include "content/browser/renderer_host/render_frame_host_delegate.h"
Carlos Caballero40b0efd2021-01-26 11:55:0073#include "content/browser/renderer_host/render_view_host_impl.h"
Sharon Yangd70a5392021-10-26 23:06:3274#include "content/browser/site_info.h"
[email protected]b6583592012-01-25 19:52:3375#include "content/browser/site_instance_impl.h"
Kunihiko Sakamoto346a74e2021-03-10 08:57:4876#include "content/browser/web_package/subresource_web_bundle_navigation_info.h"
Tsuyoshi Horo37493af2019-11-21 23:43:1477#include "content/browser/web_package/web_bundle_navigation_info.h"
Camille Lamy5193caa2018-10-12 11:59:4278#include "content/common/content_constants_internal.h"
Nate Chapind1fe3612021-04-16 20:45:5779#include "content/common/navigation_params_utils.h"
Nasko Oskovae49e292020-08-13 02:08:5180#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1181#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1982#include "content/public/browser/content_browser_client.h"
Fergal Daly1336ac642021-09-14 15:13:1183#include "content/public/browser/disallow_activation_reason.h"
[email protected]d9083482012-01-06 00:38:4684#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0085#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3886#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1687#include "content/public/browser/notification_types.h"
Lei Zhang96031532019-10-10 19:05:4788#include "content/public/browser/render_view_host.h"
[email protected]9677a3c2012-12-22 04:18:5889#include "content/public/browser/render_widget_host.h"
90#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1091#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3492#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:1993#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:3894#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:1095#include "content/public/common/content_features.h"
Lei Zhang96031532019-10-10 19:05:4796#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4397#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:5998#include "media/base/mime_util.h"
Arthur Sonzogni620cec62018-12-13 13:08:5799#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:18100#include "services/metrics/public/cpp/ukm_builders.h"
101#include "services/metrics/public/cpp/ukm_recorder.h"
Yue Ru Sun128804932020-09-30 22:19:17102#include "services/metrics/public/cpp/ukm_source_id.h"
Nan Lind91c8152021-10-21 16:22:37103#include "services/network/public/mojom/fetch_api.mojom.h"
[email protected]9677a3c2012-12-22 04:18:58104#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:39105#include "third_party/blink/public/common/blob/blob_utils.h"
Gyuyoung Kim107c2a02021-04-13 01:49:30106#include "third_party/blink/public/common/chrome_debug_urls.h"
Miyoung Shin1c565c912021-03-17 12:11:21107#include "third_party/blink/public/common/history/session_history_constants.h"
Blink Reformata30d4232018-04-07 15:31:06108#include "third_party/blink/public/common/mime_util/mime_util.h"
Minggang Wanga13c796e2021-07-02 05:54:43109#include "third_party/blink/public/common/navigation/navigation_params.h"
Carlos Caballeroede6f8c2021-01-28 11:01:50110#include "third_party/blink/public/common/page_state/page_state_serialization.h"
Minggang Wangb9f3fa92021-07-01 15:30:31111#include "third_party/blink/public/mojom/navigation/navigation_params.mojom.h"
Minggang Wang7ee0c742021-06-16 16:16:51112#include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h"
[email protected]cca6f392014-05-28 21:32:26113#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:29114
[email protected]8ff00d72012-10-23 19:12:21115namespace content {
[email protected]e9ba4472008-09-14 15:42:43116namespace {
117
118// Invoked when entries have been pruned, or removed. For example, if the
119// current entries are [google, digg, yahoo], with the current entry google,
120// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47121void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50122 int index,
[email protected]c12bf1a12008-09-17 16:28:49123 int count) {
[email protected]8ff00d72012-10-23 19:12:21124 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50125 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49126 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14127 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43128}
129
[email protected]e9ba4472008-09-14 15:42:43130// Configure all the NavigationEntries in entries for restore. This resets
131// the transition type to reload and makes sure the content state isn't empty.
132void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57133 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48134 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47135 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43136 // Use a transition type of reload so that we don't incorrectly increase
137 // the typed count.
Lei Zhang96031532019-10-10 19:05:47138 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
139 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43140 }
141}
142
[email protected]bf70edce2012-06-20 22:32:22143// Determines whether or not we should be carrying over a user agent override
144// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57145bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22146 return last_entry && last_entry->GetIsOverridingUserAgent();
147}
148
Camille Lamy5193caa2018-10-12 11:59:42149// Determines whether to override user agent for a navigation.
150bool ShouldOverrideUserAgent(
151 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57152 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42153 switch (override_user_agent) {
154 case NavigationController::UA_OVERRIDE_INHERIT:
155 return ShouldKeepOverride(last_committed_entry);
156 case NavigationController::UA_OVERRIDE_TRUE:
157 return true;
158 case NavigationController::UA_OVERRIDE_FALSE:
159 return false;
Camille Lamy5193caa2018-10-12 11:59:42160 }
161 NOTREACHED();
162 return false;
163}
164
Rakina Zata Amni312822d72021-06-04 16:13:37165// Returns true if this navigation should be treated as a reload. For e.g.
clamy0a656e42018-02-06 18:18:28166// navigating to the last committed url via the address bar or clicking on a
Rakina Zata Amni312822d72021-06-04 16:13:37167// link which results in a navigation to the last committed URL (but wasn't
168// converted to do a replacement navigation in the renderer), etc.
Fergal Daly766177d2020-07-07 07:54:04169// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
170// |base_url_for_data_url|, |transition_type| correspond to the new navigation
171// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
172// navigation that committed.
173bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
174 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57175 const GURL& virtual_url,
176 const GURL& base_url_for_data_url,
177 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57178 bool is_post,
Rakina Zata Amni312822d72021-06-04 16:13:37179 bool should_replace_current_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57180 NavigationEntryImpl* last_committed_entry) {
Rakina Zata Amni312822d72021-06-04 16:13:37181 // Navigations intended to do a replacement shouldn't be converted to do a
182 // reload.
183 if (should_replace_current_entry)
clamy0a656e42018-02-06 18:18:28184 return false;
clamy0a656e42018-02-06 18:18:28185 // Only convert to reload if at least one navigation committed.
Rakina Zata Amni2322f4f82022-01-24 13:24:24186 if (!last_committed_entry || last_committed_entry->IsInitialEntry())
ananta3bdd8ae2016-12-22 17:11:55187 return false;
188
arthursonzogni7a8243682017-12-14 16:41:42189 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28190 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42191 return false;
192
ananta3bdd8ae2016-12-22 17:11:55193 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
194 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
195 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28196 bool transition_type_can_be_converted = false;
197 if (ui::PageTransitionCoreTypeIs(transition_type,
198 ui::PAGE_TRANSITION_RELOAD) &&
199 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
200 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55201 }
clamy0a656e42018-02-06 18:18:28202 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55203 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28204 transition_type_can_be_converted = true;
205 }
206 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
207 transition_type_can_be_converted = true;
208 if (!transition_type_can_be_converted)
209 return false;
210
211 // This check is required for cases like view-source:, etc. Here the URL of
212 // the navigation entry would contain the url of the page, while the virtual
213 // URL contains the full URL including the view-source prefix.
214 if (virtual_url != last_committed_entry->GetVirtualURL())
215 return false;
216
Fergal Daly766177d2020-07-07 07:54:04217 // Check that the URLs match.
218 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
219 // If there's no frame entry then by definition the URLs don't match.
220 if (!frame_entry)
221 return false;
222
223 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28224 return false;
225
226 // This check is required for Android WebView loadDataWithBaseURL. Apps
227 // can pass in anything in the base URL and we need to ensure that these
228 // match before classifying it as a reload.
229 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
230 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
231 return false;
ananta3bdd8ae2016-12-22 17:11:55232 }
233
clamy0a656e42018-02-06 18:18:28234 // Skip entries with SSL errors.
235 if (last_committed_entry->ssl_error())
236 return false;
237
238 // Don't convert to a reload when the last navigation was a POST or the new
239 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04240 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28241 return false;
242
243 return true;
ananta3bdd8ae2016-12-22 17:11:55244}
245
Nasko Oskov03912102019-01-11 00:21:32246bool DoesURLMatchOriginForNavigation(
Wang Hui14cac7a2022-01-28 06:07:47247 const GURL& destination_url,
Anton Bikineevf62d1bf2021-05-15 17:56:07248 const absl::optional<url::Origin>& origin,
Wang Hui14cac7a2022-01-28 06:07:47249 SubresourceWebBundleNavigationInfo* subresource_web_bundle_navigation_info,
250 NavigationEntry* entry,
251 bool is_main_frame) {
Nasko Oskov03912102019-01-11 00:21:32252 // If there is no origin supplied there is nothing to match. This can happen
253 // for navigations to a pending entry and therefore it should be allowed.
254 if (!origin)
255 return true;
256
Wang Hui14cac7a2022-01-28 06:07:47257 // For a history navigation to a document loaded with loadDataWithBaseURL,
258 // compare the origin to the entry's base URL instead of the data: URL used to
259 // commit.
260 const GURL& base_url_for_data_url = entry->GetBaseURLForDataURL();
261 bool check_origin_against_base_url =
262 is_main_frame && !base_url_for_data_url.is_empty();
263
264 const GURL& url_for_origin =
265 check_origin_against_base_url ? base_url_for_data_url : destination_url;
Kunihiko Sakamotof32652502022-04-05 03:05:59266 // Uuid-in-package: subframe from WebBundle has an opaque origin derived from
267 // the Bundle's origin.
268 if (url_for_origin.SchemeIs(url::kUuidInPackageScheme) &&
Kunihiko Sakamoto260ee0a2021-10-15 01:44:08269 subresource_web_bundle_navigation_info) {
Kunihiko Sakamoto346a74e2021-03-10 08:57:48270 return origin->CanBeDerivedFrom(
271 subresource_web_bundle_navigation_info->bundle_url());
272 }
273
Wang Hui14cac7a2022-01-28 06:07:47274 return origin->CanBeDerivedFrom(url_for_origin);
Nasko Oskov03912102019-01-11 00:21:32275}
276
Anton Bikineevf62d1bf2021-05-15 17:56:07277absl::optional<url::Origin> GetCommittedOriginForFrameEntry(
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12278 const mojom::DidCommitProvisionalLoadParams& params,
279 NavigationRequest* request) {
Nasko Oskov03912102019-01-11 00:21:32280 // Error pages commit in an opaque origin, yet have the real URL that resulted
281 // in an error as the |params.url|. Since successful reload of an error page
282 // should commit in the correct origin, setting the opaque origin on the
283 // FrameNavigationEntry will be incorrect.
Rakina Zata Amniafd3c6582021-11-30 06:19:17284 if (request && request->DidEncounterError())
Anton Bikineevf62d1bf2021-05-15 17:56:07285 return absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:32286
Anton Bikineevf62d1bf2021-05-15 17:56:07287 return absl::make_optional(params.origin);
Nasko Oskov03912102019-01-11 00:21:32288}
289
Camille Lamy5193caa2018-10-12 11:59:42290bool IsValidURLForNavigation(bool is_main_frame,
291 const GURL& virtual_url,
292 const GURL& dest_url) {
293 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
294 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
295 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
296 << virtual_url.possibly_invalid_spec();
297 return false;
298 }
299
300 // Don't attempt to navigate to non-empty invalid URLs.
301 if (!dest_url.is_valid() && !dest_url.is_empty()) {
302 LOG(WARNING) << "Refusing to load invalid URL: "
303 << dest_url.possibly_invalid_spec();
304 return false;
305 }
306
307 // The renderer will reject IPC messages with URLs longer than
308 // this limit, so don't attempt to navigate with a longer URL.
309 if (dest_url.spec().size() > url::kMaxURLChars) {
310 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
311 << " characters.";
312 return false;
313 }
314
Aaron Colwell33109c592020-04-21 21:31:19315 // Reject renderer debug URLs because they should have been handled before
316 // we get to this point. This check handles renderer debug URLs
317 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
318 // provides defense-in-depth if a renderer debug URL manages to get here via
319 // some other path. We want to reject the navigation here so it doesn't
320 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30321 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19322 LOG(WARNING) << "Refusing to load renderer debug URL: "
323 << dest_url.possibly_invalid_spec();
324 return false;
325 }
326
Camille Lamy5193caa2018-10-12 11:59:42327 return true;
328}
329
Mikel Astizba9cf2fd2017-12-17 10:38:10330// See replaced_navigation_entry_data.h for details: this information is meant
331// to ensure |*output_entry| keeps track of its original URL (landing page in
332// case of server redirects) as it gets replaced (e.g. history.replaceState()),
333// without overwriting it later, for main frames.
334void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57335 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10336 NavigationEntryImpl* output_entry) {
Rakina Zata Amniafd3c6582021-11-30 06:19:17337 if (output_entry->GetReplacedEntryData().has_value() ||
338 replaced_entry->IsInitialEntry()) {
Mikel Astizba9cf2fd2017-12-17 10:38:10339 return;
Rakina Zata Amniafd3c6582021-11-30 06:19:17340 }
Mikel Astizba9cf2fd2017-12-17 10:38:10341
342 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57343 data.first_committed_url = replaced_entry->GetURL();
344 data.first_timestamp = replaced_entry->GetTimestamp();
345 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29346 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10347}
348
Minggang Wangb9f3fa92021-07-01 15:30:31349blink::mojom::NavigationType GetNavigationType(
350 const GURL& old_url,
351 const GURL& new_url,
352 ReloadType reload_type,
353 NavigationEntryImpl* entry,
354 const FrameNavigationEntry& frame_entry,
355 bool has_pending_cross_document_commit,
356 bool is_currently_error_page,
Garrett Tanzer405f3402022-07-21 20:12:49357 bool is_same_document_history_load,
Garrett Tanzer267c2b82022-07-26 16:53:13358 bool is_embedder_initiated_fenced_frame_navigation,
Garrett Tanzer405f3402022-07-21 20:12:49359 bool is_unfenced_top_navigation) {
clamyea99ea12018-05-28 13:54:23360 // Reload navigations
361 switch (reload_type) {
362 case ReloadType::NORMAL:
Minggang Wangb9f3fa92021-07-01 15:30:31363 return blink::mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23364 case ReloadType::BYPASSING_CACHE:
Minggang Wangb9f3fa92021-07-01 15:30:31365 return blink::mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23366 case ReloadType::ORIGINAL_REQUEST_URL:
Minggang Wangb9f3fa92021-07-01 15:30:31367 return blink::mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23368 case ReloadType::NONE:
369 break; // Fall through to rest of function.
370 }
371
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08372 if (entry->IsRestored()) {
Minggang Wangb9f3fa92021-07-01 15:30:31373 return entry->GetHasPostData()
374 ? blink::mojom::NavigationType::RESTORE_WITH_POST
375 : blink::mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23376 }
377
danakjb952ef12021-01-14 19:58:49378 const bool can_be_same_document =
379 // A pending cross-document commit means this navigation will not occur in
380 // the current document, as that document would end up being replaced in
381 // the meantime.
382 !has_pending_cross_document_commit &&
383 // If the current document is an error page, we should always treat it as
384 // a different-document navigation so that we'll attempt to load the
385 // document we're navigating to (and not stay in the current error page).
Garrett Tanzer405f3402022-07-21 20:12:49386 !is_currently_error_page &&
Garrett Tanzer267c2b82022-07-26 16:53:13387 // If the navigation is an embedder-initiated navigation of a fenced
388 // frame root (i.e. enters a fenced frame tree from outside),
389 // same-document navigations should be disabled because we don't want to
390 // allow information to be joined across multiple embedder-initiated
391 // fenced frame navigations (which may contain separate cross-site data).
392 !is_embedder_initiated_fenced_frame_navigation &&
Garrett Tanzer405f3402022-07-21 20:12:49393 // If the navigation is to _unfencedTop (i.e. escapes a fenced frame),
394 // same-document navigations should be disabled because we want to force
395 // the creation of a new browsing context group.
396 !is_unfenced_top_navigation;
danakjd83d706d2020-11-25 22:11:12397
clamyea99ea12018-05-28 13:54:23398 // History navigations.
399 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12400 return can_be_same_document && is_same_document_history_load
Minggang Wangb9f3fa92021-07-01 15:30:31401 ? blink::mojom::NavigationType::HISTORY_SAME_DOCUMENT
402 : blink::mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23403 }
404 DCHECK(!is_same_document_history_load);
405
406 // A same-document fragment-navigation happens when the only part of the url
407 // that is modified is after the '#' character.
408 //
409 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12410 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23411 //
412 // Note: this check is only valid for navigations that are not history
413 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
414 // history navigation from 'A#foo' to 'A#bar' is not a same-document
415 // navigation, but a different-document one. This is why history navigation
416 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47417 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
418 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12419
420 // The one case where we do the wrong thing here and incorrectly choose
421 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
422 // the renderer is a frameset. All frameset navigations should be
423 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
424 // navigation would do the right thing, as it would send it to the browser and
425 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
426 // into this method). But since we can't tell that case here for browser-
427 // initiated navigations, we have to get the renderer involved. In that case
428 // the navigation would be restarted due to the renderer spending a reply of
429 // mojom::CommitResult::RestartCrossDocument.
430
431 return can_be_same_document && is_same_doc
Minggang Wangb9f3fa92021-07-01 15:30:31432 ? blink::mojom::NavigationType::SAME_DOCUMENT
433 : blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23434}
435
Camille Lamy5193caa2018-10-12 11:59:42436// Adjusts the original input URL if needed, to get the URL to actually load and
437// the virtual URL, which may differ.
438void RewriteUrlForNavigation(const GURL& original_url,
439 BrowserContext* browser_context,
440 GURL* url_to_load,
441 GURL* virtual_url,
442 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42443 // Allow the browser URL handler to rewrite the URL. This will, for example,
444 // remove "view-source:" from the beginning of the URL to get the URL that
445 // will actually be loaded. This real URL won't be shown to the user, just
446 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31447 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42448 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
449 url_to_load, browser_context, reverse_on_redirect);
450}
451
452#if DCHECK_IS_ON()
453// Helper sanity check function used in debug mode.
454void ValidateRequestMatchesEntry(NavigationRequest* request,
455 NavigationEntryImpl* entry) {
456 if (request->frame_tree_node()->IsMainFrame()) {
457 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
458 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
Minggang Wangb9f3fa92021-07-01 15:30:31459 ui::PageTransitionFromInt(request->common_params().transition),
460 entry->GetTransitionType()));
Camille Lamy5193caa2018-10-12 11:59:42461 }
Nasko Oskovc36327d2019-01-03 23:23:04462 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42463 entry->should_clear_history_list());
464 DCHECK_EQ(request->common_params().has_user_gesture,
465 entry->has_user_gesture());
466 DCHECK_EQ(request->common_params().base_url_for_data_url,
467 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04468 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42469 entry->GetCanLoadLocalResources());
470 DCHECK_EQ(request->common_params().started_from_context_menu,
471 entry->has_started_from_context_menu());
472
473 FrameNavigationEntry* frame_entry =
474 entry->GetFrameEntry(request->frame_tree_node());
475 if (!frame_entry) {
476 NOTREACHED();
477 return;
478 }
479
Camille Lamy5193caa2018-10-12 11:59:42480 DCHECK_EQ(request->common_params().method, frame_entry->method());
481
Nasko Oskovc36327d2019-01-03 23:23:04482 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42483 if (redirect_size == frame_entry->redirect_chain().size()) {
484 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04485 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42486 frame_entry->redirect_chain()[i]);
487 }
488 } else {
489 NOTREACHED();
490 }
491}
492#endif // DCHECK_IS_ON()
493
Dave Tapuska8bfd84c2019-03-26 20:47:16494// Returns whether the session history NavigationRequests in |navigations|
495// would stay within the subtree of the sandboxed iframe in
496// |sandbox_frame_tree_node_id|.
497bool DoesSandboxNavigationStayWithinSubtree(
498 int sandbox_frame_tree_node_id,
499 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
500 for (auto& item : navigations) {
501 bool within_subtree = false;
502 // Check whether this NavigationRequest affects a frame within the
503 // sandboxed frame's subtree by walking up the tree looking for the
504 // sandboxed frame.
505 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03506 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16507 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
508 within_subtree = true;
509 break;
510 }
511 }
512 if (!within_subtree)
513 return false;
514 }
515 return true;
516}
517
[email protected]e9ba4472008-09-14 15:42:43518} // namespace
519
arthursonzogni66f711c2019-10-08 14:40:36520// NavigationControllerImpl::PendingEntryRef------------------------------------
521
522NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
523 base::WeakPtr<NavigationControllerImpl> controller)
524 : controller_(controller) {}
525
526NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
527 if (!controller_) // Can be null with interstitials.
528 return;
529
530 controller_->PendingEntryRefDeleted(this);
531}
532
[email protected]d202a7c2012-01-04 07:53:47533// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29534
[email protected]23a918b2014-07-15 09:51:36535const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23536
[email protected]765b35502008-08-21 00:51:20537// static
[email protected]d202a7c2012-01-04 07:53:47538size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23539 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20540
[email protected]e6fec472013-05-14 05:29:02541// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20542// when testing.
543static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29544
[email protected]71fde352011-12-29 03:29:56545// static
dcheng9bfa5162016-04-09 01:00:57546std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
547 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10548 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07549 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10550 ui::PageTransition transition,
551 bool is_renderer_initiated,
552 const std::string& extra_headers,
553 BrowserContext* browser_context,
554 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
555 return NavigationControllerImpl::CreateNavigationEntry(
556 url, referrer, std::move(initiator_origin),
557 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Julie Jeongeun Kim5b9aff72022-05-02 02:10:17558 extra_headers, browser_context, std::move(blob_url_loader_factory),
559 true /* rewrite_virtual_urls */);
Lukasz Anforowicz641234d52019-11-07 21:07:10560}
561
562// static
563std::unique_ptr<NavigationEntryImpl>
564NavigationControllerImpl::CreateNavigationEntry(
565 const GURL& url,
566 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07567 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10568 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57569 ui::PageTransition transition,
570 bool is_renderer_initiated,
571 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09572 BrowserContext* browser_context,
Julie Jeongeun Kim5b9aff72022-05-02 02:10:17573 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
574 bool rewrite_virtual_urls) {
575 GURL url_to_load = url;
576 GURL virtual_url = url;
[email protected]71fde352011-12-29 03:29:56577 bool reverse_on_redirect = false;
Julie Jeongeun Kim5b9aff72022-05-02 02:10:17578 if (rewrite_virtual_urls) {
579 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
580 &reverse_on_redirect);
581 }
Lukasz Anforowicz641234d52019-11-07 21:07:10582 // Let the NTP override the navigation params and pretend that this is a
583 // browser-initiated, bookmark-like navigation.
584 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violetcf6ea7e2021-06-09 21:09:21585 source_site_instance, &transition, &is_renderer_initiated, &referrer,
586 &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10587
Patrick Monettef507e982019-06-19 20:18:06588 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28589 nullptr, // The site instance for tabs is sent on navigation
590 // (WebContents::GetSiteInstance).
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58591 url_to_load, referrer, initiator_origin, std::u16string(), transition,
Rakina Zata Amniafd3c6582021-11-30 06:19:17592 is_renderer_initiated, blob_url_loader_factory,
593 /* is_initial_entry = */ false);
Camille Lamy5193caa2018-10-12 11:59:42594 entry->SetVirtualURL(virtual_url);
595 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56596 entry->set_update_virtual_url_with_url(reverse_on_redirect);
597 entry->set_extra_headers(extra_headers);
Patrick Monettef507e982019-06-19 20:18:06598 return entry;
[email protected]71fde352011-12-29 03:29:56599}
600
[email protected]cdcb1dee2012-01-04 00:46:20601// static
602void NavigationController::DisablePromptOnRepost() {
603 g_check_for_repost = false;
604}
605
[email protected]c5b88d82012-10-06 17:03:33606base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
607 base::Time t) {
608 // If |t| is between the water marks, we're in a run of duplicates
609 // or just getting out of it, so increase the high-water mark to get
610 // a time that probably hasn't been used before and return it.
611 if (low_water_mark_ <= t && t <= high_water_mark_) {
Peter Kastinge5a38ed2021-10-02 03:06:35612 high_water_mark_ += base::Microseconds(1);
[email protected]c5b88d82012-10-06 17:03:33613 return high_water_mark_;
614 }
615
616 // Otherwise, we're clear of the last duplicate run, so reset the
617 // water marks.
618 low_water_mark_ = high_water_mark_ = t;
619 return t;
620}
621
ckitagawa0faa5e42020-06-17 17:30:54622NavigationControllerImpl::ScopedShowRepostDialogForTesting::
623 ScopedShowRepostDialogForTesting()
624 : was_disallowed_(g_check_for_repost) {
625 g_check_for_repost = true;
626}
627
628NavigationControllerImpl::ScopedShowRepostDialogForTesting::
629 ~ScopedShowRepostDialogForTesting() {
630 g_check_for_repost = was_disallowed_;
631}
632
[email protected]d202a7c2012-01-04 07:53:47633NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00634 BrowserContext* browser_context,
635 FrameTree& frame_tree,
636 NavigationControllerDelegate* delegate)
637 : frame_tree_(frame_tree),
638 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57639 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22640 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47641 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37642 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29643}
644
[email protected]d202a7c2012-01-04 07:53:47645NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00646 // The NavigationControllerImpl might be called inside its delegate
647 // destructor. Calling it is not valid anymore.
648 delegate_ = nullptr;
649 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29650}
651
Matt Falkenhagen548ed1562021-07-06 01:38:26652WebContents* NavigationControllerImpl::DeprecatedGetWebContents() {
653 return delegate_->DeprecatedGetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32654}
655
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57656BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55657 return browser_context_;
658}
659
[email protected]d202a7c2012-01-04 07:53:47660void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30661 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36662 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57663 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
Charlie Reis23c26da2022-01-29 00:57:47664 // Note that it's possible for `entries_` to contain multiple entries at this
665 // point, as Restore() might be called on a NavigationController that is
666 // already used (e.g. due to WebView's restoreState() API), not only for fresh
667 // NavigationControllers. These entries are not cleared to preserve legacy
668 // behavior and also because `pending_entry_` might point to one of the
669 // pre-existing entries. An exception for this is when `entries_` contains
670 // only the initial NavigationEntry, which must be removed.
671
672 // Do not proceed if selected_navigation will be out of bounds for the updated
673 // entries_ list, since it will be assigned to last_committed_entry_index_ and
674 // used to index entries_.
675 // TODO(https://crbug.com/1287624): Consider also returning early if entries
676 // is empty, since there should be no work to do (rather than marking the
677 // existing entries as needing reload). Also consider returning early if the
678 // selected index is -1, which represents a no-committed-entry state.
679 if (selected_navigation < -1 ||
680 selected_navigation >=
681 base::checked_cast<int>(entries->size() + entries_.size())) {
682 return;
Rakina Zata Amni2322f4f82022-01-24 13:24:24683 }
Charlie Reis23c26da2022-01-29 00:57:47684
685 if (blink::features::IsInitialNavigationEntryEnabled()) {
686 // In InitialNavigationEntry mode, there will always be at least one entry.
687 if (selected_navigation == -1)
688 selected_navigation = 0;
689
690 if (GetLastCommittedEntry()->IsInitialEntry() && entries->size() > 0) {
691 // If we are on the initial NavigationEntry, it must be the only entry in
692 // the list. Since it's impossible to do a history navigation to the
693 // initial NavigationEntry, `pending_entry_index_` must be -1 (but
694 // `pending_entry` might be set for a new non-history navigation).
695 // Note that we should not clear `entries_` if `entries` is empty (when
696 // InitialNavigationEntry mode is enabled), since that would leave us
697 // without any NavigationEntry.
698 CHECK_EQ(1, GetEntryCount());
699 CHECK_EQ(-1, pending_entry_index_);
700 entries_.clear();
701 }
702 }
[email protected]ce3fa3c2009-04-20 19:55:57703
[email protected]ce3fa3c2009-04-20 19:55:57704 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44705 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03706 entries_.reserve(entries->size());
Charlie Reis23c26da2022-01-29 00:57:47707 for (auto& entry : *entries) {
Rakina Zata Amni996ee412022-02-17 04:51:43708 if (entry->GetURL().is_empty()) {
709 // We're trying to restore an entry with an empty URL (e.g. from
Rakina Zata Amni2729a512022-03-16 05:54:01710 // persisting the initial NavigationEntry [which is no longer possible but
711 // some old persisted sessions might still contain it] or when the
712 // serializer failed to write the URL because it's too long). Trying to
713 // restore and navigate to an entry with an empty URL will result in
714 // crashes, so change the URL to about:blank. See also
715 // https://crbug.com/1240138 and https://crbug.com/1299335.
Rakina Zata Amni996ee412022-02-17 04:51:43716 entry->SetURL(GURL(url::kAboutBlankURL));
717 }
dcheng36b6aec92015-12-26 06:16:36718 entries_.push_back(
719 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
Charlie Reis23c26da2022-01-29 00:57:47720 }
avif16f85a72015-11-13 18:25:03721
722 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
723 // cleared out safely.
724 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57725
726 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36727 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57728}
729
toyoshim6142d96f2016-12-19 09:07:25730void NavigationControllerImpl::Reload(ReloadType reload_type,
731 bool check_for_repost) {
Rakina Zata Amnid605d462022-06-01 10:17:03732 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "Reload_type",
733 (int)reload_type);
734 SCOPED_CRASH_KEY_BOOL("nav_reentrancy_caller1", "Reload_check",
735 check_for_repost);
liaoyuke9168fba2017-03-10 19:20:28736 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28737 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32738 int current_index = -1;
739
Carlos IL42b416592019-10-07 23:10:36740 if (entry_replaced_by_post_commit_error_) {
741 // If there is an entry that was replaced by a currently active post-commit
742 // error navigation, this can't be the initial navigation.
743 DCHECK(!IsInitialNavigation());
744 // If the current entry is a post commit error, we reload the entry it
745 // replaced instead. We leave the error entry in place until a commit
746 // replaces it, but the pending entry points to the original entry in the
747 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
748 // case that pending_entry_ != entries_[pending_entry_index_].
749 entry = entry_replaced_by_post_commit_error_.get();
750 current_index = GetCurrentEntryIndex();
751 } else if (IsInitialNavigation() && pending_entry_) {
752 // If we are reloading the initial navigation, just use the current
753 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32754 entry = pending_entry_;
755 // The pending entry might be in entries_ (e.g., after a Clone), so we
756 // should also update the current_index.
757 current_index = pending_entry_index_;
758 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00759 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32760 current_index = GetCurrentEntryIndex();
761 if (current_index != -1) {
creis3da03872015-02-20 21:12:32762 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32763 }
[email protected]979a4bc2013-04-24 01:27:15764 }
[email protected]241db352013-04-22 18:04:05765
[email protected]59167c22013-06-03 18:07:32766 // If we are no where, then we can't reload. TODO(darin): We should add a
767 // CanReload method.
768 if (!entry)
769 return;
770
Rakina Zata Amnif297a802022-01-18 03:53:43771 if (entry->IsInitialEntryNotForSynchronousAboutBlank()) {
772 // We should never navigate to an existing initial NavigationEntry that is
773 // the initial NavigationEntry for the initial empty document that hasn't
774 // been overridden by the synchronous about:blank commit, to preserve
775 // legacy behavior where trying to reload when the main frame is on the
776 // initial empty document won't result in a navigation. See also
777 // https://crbug.com/1277414.
778 return;
779 }
780
Takashi Toyoshimac7df3c22019-06-25 14:18:47781 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26782 entry->set_reload_type(reload_type);
783
Aran Gilman37d11632019-10-08 23:07:15784 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30785 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07786 // they really want to do this. If they do, the dialog will call us back
787 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57788 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02789
[email protected]106a0812010-03-18 00:15:12790 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57791 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47792 return;
initial.commit09911bf2008-07-26 23:55:29793 }
Lei Zhang96031532019-10-10 19:05:47794
795 if (!IsInitialNavigation())
796 DiscardNonCommittedEntries();
797
798 pending_entry_ = entry;
799 pending_entry_index_ = current_index;
800 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
801
Nate Chapin45f620582021-09-30 17:45:43802 // location.reload() goes through BeginNavigation, so all reloads triggered
803 // via this codepath are browser initiated.
Lei Zhang96031532019-10-10 19:05:47804 NavigateToExistingPendingEntry(reload_type,
Nate Chapin45f620582021-09-30 17:45:43805 FrameTreeNode::kFrameTreeNodeInvalidId,
806 true /* is_browser_initiated */);
initial.commit09911bf2008-07-26 23:55:29807}
808
[email protected]d202a7c2012-01-04 07:53:47809void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48810 DCHECK(pending_reload_ != ReloadType::NONE);
811 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12812}
813
[email protected]d202a7c2012-01-04 07:53:47814void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48815 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12816 NOTREACHED();
817 } else {
toyoshim6142d96f2016-12-19 09:07:25818 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48819 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12820 }
821}
822
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57823bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09824 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11825}
826
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57827bool NavigationControllerImpl::IsInitialBlankNavigation() {
Rakina Zata Amni2322f4f82022-01-24 13:24:24828 if (blink::features::IsInitialNavigationEntryEnabled()) {
829 // Check that we're on the initial NavigationEntry and that this is not a
830 // cloned tab.
831 return IsInitialNavigation() && GetEntryCount() == 1 &&
832 GetLastCommittedEntry()->IsInitialEntry() &&
833 GetLastCommittedEntry()->restore_type() == RestoreType::kNotRestored;
834 } else {
835 return IsInitialNavigation() && GetEntryCount() == 0;
836 }
creis10a4ab72015-10-13 17:22:40837}
838
Aran Gilman37d11632019-10-08 23:07:15839NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
840 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58841 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03842 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58843}
844
Adithya Srinivasan9b0c99c2021-08-10 15:19:45845NavigationEntryImpl*
846NavigationControllerImpl::GetEntryWithUniqueIDIncludingPending(
847 int nav_entry_id) const {
848 NavigationEntryImpl* entry = GetEntryWithUniqueID(nav_entry_id);
849 if (entry)
850 return entry;
851 return pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id
Keishi Hattori0e45c022021-11-27 09:25:52852 ? pending_entry_.get()
Adithya Srinivasan9b0c99c2021-08-10 15:19:45853 : nullptr;
854}
855
W. James MacLeanc07dc41b2022-07-25 18:52:16856void NavigationControllerImpl::RegisterExistingOriginAsHavingDefaultIsolation(
W. James MacLean1c40862c2020-04-27 21:05:57857 const url::Origin& origin) {
858 for (int i = 0; i < GetEntryCount(); i++) {
859 auto* entry = GetEntryAtIndex(i);
W. James MacLeanc07dc41b2022-07-25 18:52:16860 entry->RegisterExistingOriginAsHavingDefaultIsolation(origin);
W. James MacLean1c40862c2020-04-27 21:05:57861 }
862 if (entry_replaced_by_post_commit_error_) {
863 // It's possible we could come back to this entry if the error
864 // page/interstitial goes away.
865 entry_replaced_by_post_commit_error_
W. James MacLeanc07dc41b2022-07-25 18:52:16866 ->RegisterExistingOriginAsHavingDefaultIsolation(origin);
W. James MacLean1c40862c2020-04-27 21:05:57867 }
868 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
869 // visiting pending_entry_, which lacks a committed origin. This will be done
870 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
871}
872
avi25764702015-06-23 15:43:37873void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57874 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00875 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37876 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27877 DCHECK_EQ(-1, pending_entry_index_);
[email protected]765b35502008-08-21 00:51:20878}
879
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57880NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47881 if (pending_entry_)
882 return pending_entry_;
883 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20884}
885
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57886NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32887 // The pending entry is safe to return for new (non-history), browser-
888 // initiated navigations. Most renderer-initiated navigations should not
889 // show the pending entry, to prevent URL spoof attacks.
890 //
891 // We make an exception for renderer-initiated navigations in new tabs, as
892 // long as no other page has tried to access the initial empty document in
893 // the new tab. If another page modifies this blank page, a URL spoof is
894 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32895 bool safe_to_show_pending =
896 pending_entry_ &&
897 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09898 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32899 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46900 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32901
902 // Also allow showing the pending entry for history navigations in a new tab,
903 // such as Ctrl+Back. In this case, no existing page is visible and no one
904 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15905 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
906 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32907 safe_to_show_pending = true;
908
909 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19910 return pending_entry_;
911 return GetLastCommittedEntry();
912}
913
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57914int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20915 if (pending_entry_index_ != -1)
916 return pending_entry_index_;
917 return last_committed_entry_index_;
918}
919
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57920NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
Rakina Zata Amni2322f4f82022-01-24 13:24:24921 if (last_committed_entry_index_ == -1) {
922 // The last committed entry must always exist when InitialNavigationEntry
923 // is enabled. TODO(https://crbug.com/524208): Remove this case and all
924 // related nullchecks entirely.
925 DCHECK(!blink::features::IsInitialNavigationEntryEnabled());
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28926 return nullptr;
Rakina Zata Amni2322f4f82022-01-24 13:24:24927 }
avif16f85a72015-11-13 18:25:03928 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20929}
930
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57931bool NavigationControllerImpl::CanViewSource() {
Jeremy Roman2d8dfe132021-07-06 20:51:26932 const std::string& mime_type =
933 frame_tree_.root()->current_frame_host()->GetPage().contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53934 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
935 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27936 NavigationEntry* visible_entry = GetVisibleEntry();
937 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39938 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16939}
940
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57941int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27942 // The last committed entry index must always be less than the number of
Rakina Zata Amni2322f4f82022-01-24 13:24:24943 // entries. If there are no entries, it must be -1.
arthursonzogni5c4c202d2017-04-25 23:41:27944 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
Rakina Zata Amni2322f4f82022-01-24 13:24:24945 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55946 return last_committed_entry_index_;
947}
948
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57949int NavigationControllerImpl::GetEntryCount() {
Carlos IL4dea8902020-05-26 15:14:29950 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55951 return static_cast<int>(entries_.size());
952}
953
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57954NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37955 if (index < 0 || index >= GetEntryCount())
956 return nullptr;
957
avif16f85a72015-11-13 18:25:03958 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25959}
960
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57961NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47962 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20963}
964
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57965int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46966 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03967}
968
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57969bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03970 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
971 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
972 return true;
973 }
974 return false;
[email protected]765b35502008-08-21 00:51:20975}
976
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57977bool NavigationControllerImpl::CanGoForward() {
WangHui74286d52021-03-31 16:17:15978 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
979 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
980 return true;
981 }
982 return false;
[email protected]765b35502008-08-21 00:51:20983}
984
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57985bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03986 int index = GetIndexForOffset(offset);
987 return index >= 0 && index < GetEntryCount();
988}
989
Xiaohan Wang7f8052e02022-01-14 18:44:28990#if BUILDFLAG(IS_ANDROID)
WangHui74286d52021-03-31 16:17:15991bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
WangHui74286d52021-03-31 16:17:15992 if (offset == 0)
993 return true;
994 int increment = offset > 0 ? 1 : -1;
995 int non_skippable_entries = 0;
996 for (int index = GetIndexForOffset(increment);
997 index >= 0 && index < GetEntryCount(); index += increment) {
998 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
999 non_skippable_entries++;
1000
1001 if (non_skippable_entries == std::abs(offset))
1002 return true;
1003 }
1004 return false;
1005}
1006#endif
1007
[email protected]d202a7c2012-01-04 07:53:471008void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:061009 int target_index = GetIndexForOffset(-1);
1010
Elly Fong-Jones40ee8112021-06-23 19:10:521011 // Move the target index past the skippable entries.
Shivani Sharma298d12852019-01-22 20:04:031012 bool all_skippable_entries = true;
Elly Fong-Jones40ee8112021-06-23 19:10:521013 while (target_index >= 0) {
1014 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui()) {
Shivani Sharma298d12852019-01-22 20:04:031015 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:061016 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:161017 }
Elly Fong-Jones40ee8112021-06-23 19:10:521018 target_index--;
shivanisha55201872018-12-13 04:29:061019 }
Miyoung Shin1c565c912021-03-17 12:11:211020
Shivani Sharma298d12852019-01-22 20:04:031021 // Do nothing if all entries are skippable. Normally this path would not
1022 // happen as consumers would have already checked it in CanGoBack but a lot of
1023 // tests do not do that.
Elly Fong-Jonesccc6d1f2021-06-14 18:32:421024 if (all_skippable_entries)
Shivani Sharma298d12852019-01-22 20:04:031025 return;
shivanisha55201872018-12-13 04:29:061026
shivanisha55201872018-12-13 04:29:061027 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:201028}
1029
[email protected]d202a7c2012-01-04 07:53:471030void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:061031 int target_index = GetIndexForOffset(1);
1032
Shivani Sharma2d5b4b6b2019-01-08 16:07:161033 // Note that at least one entry (the last one) will be non-skippable since
1034 // entries are marked skippable only when they add another entry because of
1035 // redirect or pushState.
Elly Fong-Jones40ee8112021-06-23 19:10:521036 while (target_index < static_cast<int>(entries_.size())) {
1037 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui())
shivanisha55201872018-12-13 04:29:061038 break;
Elly Fong-Jones40ee8112021-06-23 19:10:521039 target_index++;
shivanisha55201872018-12-13 04:29:061040 }
shivanisha55201872018-12-13 04:29:061041 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:201042}
1043
[email protected]d202a7c2012-01-04 07:53:471044void NavigationControllerImpl::GoToIndex(int index) {
Nate Chapin45f620582021-09-30 17:45:431045 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId,
1046 true /* is_browser_initiated */);
Dave Tapuska8bfd84c2019-03-26 20:47:161047}
1048
1049void NavigationControllerImpl::GoToIndex(int index,
Nate Chapin45f620582021-09-30 17:45:431050 int sandbox_frame_tree_node_id,
1051 bool is_browser_initiated) {
Rakina Zata Amnid605d462022-06-01 10:17:031052 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy_caller1", "GoToIndex_index", index);
Nate Chapin45f620582021-09-30 17:45:431053 DCHECK(sandbox_frame_tree_node_id == FrameTreeNode::kFrameTreeNodeInvalidId ||
1054 !is_browser_initiated);
sunjian30574a62017-03-21 21:39:441055 TRACE_EVENT0("browser,navigation,benchmark",
1056 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:201057 if (index < 0 || index >= static_cast<int>(entries_.size())) {
1058 NOTREACHED();
1059 return;
1060 }
1061
Rakina Zata Amnif297a802022-01-18 03:53:431062 if (entries_[index]->IsInitialEntryNotForSynchronousAboutBlank()) {
1063 // We should never navigate to an existing initial NavigationEntry that is
1064 // the initial NavigationEntry for the initial empty document that hasn't
1065 // been overridden by the synchronous about:blank commit, to preserve
1066 // legacy behavior where trying to reload when the main frame is on the
1067 // initial empty document won't result in a navigation. See also
1068 // https://crbug.com/1277414.
1069 return;
1070 }
1071
[email protected]cbab76d2008-10-13 22:42:471072 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:201073
arthursonzogni5c4c202d2017-04-25 23:41:271074 DCHECK_EQ(nullptr, pending_entry_);
1075 DCHECK_EQ(-1, pending_entry_index_);
Rakina Zata Amnif297a802022-01-18 03:53:431076
arthursonzogni5c4c202d2017-04-25 23:41:271077 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:201078 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:271079 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
1080 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Nate Chapin45f620582021-09-30 17:45:431081 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id,
1082 is_browser_initiated);
[email protected]765b35502008-08-21 00:51:201083}
1084
[email protected]d202a7c2012-01-04 07:53:471085void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:121086 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:031087 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:201088 return;
1089
[email protected]9ba14052012-06-22 23:50:031090 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:201091}
1092
Nate Chapin45f620582021-09-30 17:45:431093void NavigationControllerImpl::GoToOffsetFromRenderer(int offset) {
1094 // Note: This is actually reached in unit tests.
1095 if (!CanGoToOffset(offset))
1096 return;
1097
1098 GoToIndex(GetIndexForOffset(offset), FrameTreeNode::kFrameTreeNodeInvalidId,
1099 false /* is_browser_initiated */);
1100}
1101
Xiaohan Wang7f8052e02022-01-14 18:44:281102#if BUILDFLAG(IS_ANDROID)
WangHui74286d52021-03-31 16:17:151103void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
1104 // Note: This is actually reached in unit tests.
1105 if (!CanGoToOffsetWithSkipping(offset))
1106 return;
1107
Elly Fong-Jonesccc6d1f2021-06-14 18:32:421108 if (offset == 0) {
WangHui74286d52021-03-31 16:17:151109 GoToIndex(GetIndexForOffset(offset));
1110 return;
1111 }
1112 int increment = offset > 0 ? 1 : -1;
1113 // Find the offset without counting skippable entries.
1114 int target_index = GetIndexForOffset(increment);
1115 int non_skippable_entries = 0;
1116 for (int index = target_index; index >= 0 && index < GetEntryCount();
1117 index += increment) {
1118 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1119 non_skippable_entries++;
1120
1121 if (non_skippable_entries == std::abs(offset)) {
1122 target_index = index;
1123 break;
1124 }
1125 }
1126
1127 GoToIndex(target_index);
1128}
1129#endif
1130
[email protected]41374f12013-07-24 02:49:281131bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151132 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281133 return false;
[email protected]6a13a6c2011-12-20 21:47:121134
[email protected]43032342011-03-21 14:10:311135 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281136 return true;
[email protected]cbab76d2008-10-13 22:42:471137}
1138
Michael Thiessen9b14d512019-09-23 21:19:471139void NavigationControllerImpl::PruneForwardEntries() {
1140 DiscardNonCommittedEntries();
1141 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471142 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471143 if (num_removed <= 0)
1144 return;
1145 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1146 NotifyPrunedEntries(this, remove_start_index /* start index */,
1147 num_removed /* count */);
1148}
1149
Aran Gilman37d11632019-10-08 23:07:151150void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1151 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321152 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311153 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511154 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1155 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321156 }
1157}
1158
Harkiran Bolariaba823e42021-05-21 18:30:361159base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL(
1160 const GURL& url,
1161 const Referrer& referrer,
1162 ui::PageTransition transition,
1163 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471164 LoadURLParams params(url);
1165 params.referrer = referrer;
1166 params.transition_type = transition;
1167 params.extra_headers = extra_headers;
Harkiran Bolariaba823e42021-05-21 18:30:361168 return LoadURLWithParams(params);
[email protected]cf002332012-08-14 19:17:471169}
1170
Harkiran Bolariaba823e42021-05-21 18:30:361171base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams(
1172 const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061173 if (params.is_renderer_initiated)
1174 DCHECK(params.initiator_origin.has_value());
1175
naskob8744d22014-08-28 17:07:431176 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151177 "NavigationControllerImpl::LoadURLWithParams", "url",
1178 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291179 bool is_explicit_navigation =
1180 GetContentClient()->browser()->IsExplicitNavigation(
1181 params.transition_type);
1182 if (HandleDebugURL(params.url, params.transition_type,
1183 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431184 // If Telemetry is running, allow the URL load to proceed as if it's
1185 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491186 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431187 cc::switches::kEnableGpuBenchmarking))
Harkiran Bolariaba823e42021-05-21 18:30:361188 return nullptr;
[email protected]47752982014-07-29 08:01:431189 }
[email protected]8bf1048012012-02-08 01:22:181190
[email protected]cf002332012-08-14 19:17:471191 // Checks based on params.load_type.
1192 switch (params.load_type) {
1193 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431194 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471195 break;
1196 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261197 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471198 NOTREACHED() << "Data load must use data scheme.";
Harkiran Bolariaba823e42021-05-21 18:30:361199 return nullptr;
[email protected]cf002332012-08-14 19:17:471200 }
1201 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461202 }
[email protected]e47ae9472011-10-13 19:48:341203
[email protected]e47ae9472011-10-13 19:48:341204 // The user initiated a load, we don't need to reload anymore.
1205 needs_reload_ = false;
1206
Harkiran Bolariaba823e42021-05-21 18:30:361207 return NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441208}
1209
Mohamed Abdelhalim833de902019-09-16 17:41:451210bool NavigationControllerImpl::PendingEntryMatchesRequest(
1211 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191212 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451213 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191214}
1215
[email protected]d202a7c2012-01-04 07:53:471216bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321217 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071218 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331219 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441220 bool is_same_document_navigation,
Nate Chapinc7019dd7d2021-06-25 18:29:251221 bool was_on_initial_empty_document,
Shivani Sharmaffb32b82019-04-09 16:58:471222 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131223 NavigationRequest* navigation_request) {
1224 DCHECK(navigation_request);
Chris Bookholt27faf8d2022-01-20 01:03:331225
1226 // Note: validation checks and renderer kills due to invalid commit messages
1227 // must happen before getting here, in
1228 // RenderFrameHostImpl::ValidateDidCommitParams. By the time we get here, some
1229 // effects of the navigation have already occurred.
1230
[email protected]cd2e15742013-03-08 04:08:311231 is_initial_navigation_ = false;
1232
[email protected]0e8db942008-09-24 21:21:481233 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431234 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481235 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361236 if (entry_replaced_by_post_commit_error_) {
Chris Bookholt27faf8d2022-01-20 01:03:331237 // Same document navigation events with a post-commit error should already
1238 // be blocked by RenderFrameHostImpl::ValidateDidCommitParams() before
1239 // reaching here.
1240 CHECK(!is_same_document_navigation);
1241
Carlos IL42b416592019-10-07 23:10:361242 // Any commit while a post-commit error page is showing should put the
1243 // original entry back, replacing the error page's entry. This includes
1244 // reloads, where the original entry was used as the pending entry and
1245 // should now be at the correct index at commit time.
1246 entries_[last_committed_entry_index_] =
1247 std::move(entry_replaced_by_post_commit_error_);
1248 }
Fergal Daly8e33cf62020-12-12 01:06:071249 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551250 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431251 if (pending_entry_ &&
1252 pending_entry_->GetIsOverridingUserAgent() !=
1253 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1254 overriding_user_agent_changed = true;
Xiaohan Wang7f8052e02022-01-14 18:44:281255#if BUILDFLAG(IS_ANDROID)
Shu Yang5756fa422021-11-05 23:02:541256 // TODO(crbug.com/1266277): Clean up the logic of setting
1257 // |overriding_user_agent_changed| post-launch.
Shu Yangd7cb62142022-08-04 21:08:161258 if (base::FeatureList::IsEnabled(features::kRequestDesktopSiteExceptions) ||
1259 base::FeatureList::IsEnabled(features::kRequestDesktopSiteAdditions)) {
Shu Yang5756fa422021-11-05 23:02:541260 // Must honor user agent overrides in the |navigation_request|, such as
1261 // from things like RequestDesktopSiteWebContentsObserverAndroid. As a
1262 // result, besides comparing |pending_entry_|'s user agent against
1263 // LastCommittedEntry's, also need to compare |navigation_request|'s user
1264 // agent against LastCommittedEntry's.
1265 if (navigation_request->is_overriding_user_agent() !=
1266 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
1267 overriding_user_agent_changed = true;
1268 }
1269 }
Xiaohan Wang7f8052e02022-01-14 18:44:281270#endif // BUILDFLAG(IS_ANDROID)
[email protected]0e8db942008-09-24 21:21:481271 } else {
Gang Wu325f03f42021-02-25 20:00:461272 // GetLastCommittedEntry() is null, so this is the first entry.
Fergal Daly8e33cf62020-12-12 01:06:071273 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481274 details->previous_entry_index = -1;
Gang Wu325f03f42021-02-25 20:00:461275 if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) {
1276 // Default setting is NOT override the user agent, so overriding the user
1277 // agent in first entry should be considered as user agent changed as
1278 // well.
1279 overriding_user_agent_changed = true;
1280 }
Xiaohan Wang7f8052e02022-01-14 18:44:281281#if BUILDFLAG(IS_ANDROID)
Shu Yang5756fa422021-11-05 23:02:541282 // TODO(crbug.com/1266277): Clean up the logic of setting
1283 // |overriding_user_agent_changed| post-launch.
Shu Yangd7cb62142022-08-04 21:08:161284 if (base::FeatureList::IsEnabled(features::kRequestDesktopSiteExceptions) ||
1285 base::FeatureList::IsEnabled(features::kRequestDesktopSiteAdditions)) {
Shu Yang5756fa422021-11-05 23:02:541286 // Must honor user agent overrides in the |navigation_request|, such as
1287 // from things like RequestDesktopSiteWebContentsObserverAndroid. As a
1288 // result, besides checking |pending_entry_|'s user agent, also need to
1289 // check |navigation_request|'s.
1290 if (navigation_request->is_overriding_user_agent()) {
1291 overriding_user_agent_changed = true;
1292 }
1293 }
Xiaohan Wang7f8052e02022-01-14 18:44:281294#endif // BUILDFLAG(IS_ANDROID)
[email protected]0e8db942008-09-24 21:21:481295 }
[email protected]ecd9d8702008-08-28 22:10:171296
Dave Tapuskaa2ab4f252021-07-08 21:31:281297 bool is_main_frame_navigation = !rfh->GetParent();
1298
Alexander Timind2f2e4f22019-04-02 20:04:531299 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1300 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281301 // For primary frame tree navigations, choose an appropriate
Rakina Zata Amni63b5e8092022-05-20 11:25:141302 // BackForwardCacheMetrics to be associated with the new navigation's
1303 // NavigationEntry, by either creating a new object or reusing the previous
1304 // entry's one.
Dave Tapuskaa2ab4f252021-07-08 21:31:281305 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics;
1306 if (navigation_request->frame_tree_node()->frame_tree()->type() ==
1307 FrameTree::Type::kPrimary) {
Rakina Zata Amni63b5e8092022-05-20 11:25:141308 back_forward_cache_metrics = BackForwardCacheMetrics::
1309 CreateOrReuseBackForwardCacheMetricsForNavigation(
Dave Tapuskaa2ab4f252021-07-08 21:31:281310 GetLastCommittedEntry(), is_main_frame_navigation,
1311 params.document_sequence_number);
1312 }
Alexander Timind2f2e4f22019-04-02 20:04:531313 // Notify the last active entry that we have navigated away.
Dave Tapuskaa2ab4f252021-07-08 21:31:281314 if (is_main_frame_navigation && !is_same_document_navigation) {
Alexander Timind2f2e4f22019-04-02 20:04:531315 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1316 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Rakina Zata Amni63b5e8092022-05-20 11:25:141317 metrics->MainFrameDidNavigateAwayFromDocument();
Alexander Timind2f2e4f22019-04-02 20:04:531318 }
1319 }
1320 }
1321
fdegans9caf66a2015-07-30 21:10:421322 // If there is a pending entry at this point, it should have a SiteInstance,
1323 // except for restored entries.
jam48cea9082017-02-15 06:13:291324 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481325 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081326 pending_entry_->IsRestored());
1327 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151328 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291329 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481330 }
[email protected]e9ba4472008-09-14 15:42:431331
Nasko Oskovaee2f862018-06-15 00:05:521332 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1333 // has changed, this must be treated as a new navigation with replacement.
1334 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491335 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521336 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101337 pending_entry_->GetUniqueID() ==
1338 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521339 pending_entry_->site_instance() &&
1340 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1341 DCHECK_NE(-1, pending_entry_index_);
1342 // TODO(nasko,creis): Instead of setting this value here, set
1343 // should_replace_current_entry on the parameters we send to the
1344 // renderer process as part of CommitNavigation. The renderer should
1345 // in turn send it back here as part of |params| and it can be just
1346 // enforced and renderer process terminated on mismatch.
1347 details->did_replace_entry = true;
1348 } else {
1349 // The renderer tells us whether the navigation replaces the current entry.
1350 details->did_replace_entry = params.should_replace_current_entry;
1351 }
[email protected]bcd904482012-02-01 01:54:221352
[email protected]e9ba4472008-09-14 15:42:431353 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amni2322f4f82022-01-24 13:24:241354 details->type = ClassifyNavigation(rfh, params, navigation_request);
shivanigithub189833f2022-04-27 18:08:451355
1356 if (ShouldMaintainTrivialSessionHistory(rfh->frame_tree_node()) &&
1357 GetLastCommittedEntry()) {
1358 // Ensure that this navigation does not add a navigation entry, since
1359 // ShouldMaintainTrivialSessionHistory() means we should not add an entry
1360 // beyond the last committed one. Therefore, `should_replace_current_entry`
1361 // should be set, which replaces the current entry, or this should be a
1362 // reload, which does not create a new entry.
1363 // In shadowDOM fenced frames, on a history/tab-restore navigation, any
1364 // navigation that is restored will not be creating a new entry anyways, so
1365 // exclude that case by checking NAVIGATION_TYPE_AUTO_SUBFRAME.
1366 // TODO(crbug.com/1319919): Consider adjusting the dcheck for more cases as
1367 // pointed out in the issue.
1368 DCHECK(params.should_replace_current_entry ||
1369 navigation_request->GetReloadType() != ReloadType::NONE ||
1370 details->type == NAVIGATION_TYPE_AUTO_SUBFRAME);
1371 }
1372
Rakina Zata Amni2322f4f82022-01-24 13:24:241373 if (GetLastCommittedEntry() && GetLastCommittedEntry()->IsInitialEntry()) {
Rakina Zata Amniddf10502022-01-15 02:56:551374 if (rfh->GetParent()) {
Rakina Zata Amni2322f4f82022-01-24 13:24:241375 // This is a subframe navigation on the initial empty document, which used
1376 // to not have a NavigationEntry to attach to. Now it can attach to the
1377 // initial NavigationEntry, and we must ensure that its NavigationEntry
1378 // will keep the "initial NavigationEntry" status and won't append a new
1379 // NavigationEntry (it should always do replacement instead).
1380 // See also https://crbug.com/1277414.
1381 details->should_stay_as_initial_entry = true;
Rakina Zata Amniddf10502022-01-15 02:56:551382 // Subframe navigation on initial NavigationEntry must not append a new
1383 // NavigationEntry (i.e. should not be classified as NEW_SUBFRAME). This
1384 // means every subframe navigation that happens while we're on the initial
1385 // NavigationEntry will always reuse the existing NavigationEntry and
1386 // just update the corresponding FrameNavigationEntry.
1387 DCHECK_EQ(details->type, NAVIGATION_TYPE_AUTO_SUBFRAME);
1388 } else if (details->type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY) {
1389 // This is a navigation that modifies the initial NavigationEntry, either
1390 // for a replacement or a reload. The initial NavigationEntry should
1391 // retain its "initial NavigationEntry" status in this case.
1392 details->should_stay_as_initial_entry = true;
Rakina Zata Amni2322f4f82022-01-24 13:24:241393 } else if (navigation_request->is_synchronous_renderer_commit() &&
1394 !navigation_request->IsSameDocument() && !rfh->GetParent() &&
1395 params.should_replace_current_entry) {
1396 // This is a synchronous about:blank navigation on the main frame, which
1397 // used to not create a NavigationEntry when we have no NavigationEntry on
1398 // FrameTree creation. We now have the initial NavigationEntry and are on
1399 // the initial NavigationEntry. To preserve old behavior, we should still
1400 // keep the "initial" status for the new NavigationEntry that we will
1401 // create for this navigation, so that subframe navigations under the
1402 // synchronously committed about:blank document will never append new
1403 // NavigationEntry, and instead will just reuse the initial
1404 // NavigationEntry and modify the corresponding FrameNavigationEntries.
1405 // See also https://crbug.com/1277414.
1406 details->should_stay_as_initial_entry = true;
Rakina Zata Amniddf10502022-01-15 02:56:551407 }
1408 }
1409 DCHECK(!details->should_stay_as_initial_entry ||
1410 GetLastCommittedEntry()->IsInitialEntry());
[email protected]4bf3522c2010-08-19 21:00:201411
eugenebutee08663a2017-04-27 17:43:121412 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441413 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121414
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071415 details->is_prerender_activation =
1416 navigation_request->IsPrerenderedPageActivation();
Robert Lin540dbd12022-04-28 22:07:241417 details->is_in_active_page = navigation_request->GetRenderFrameHost()
1418 ->GetOutermostMainFrame()
1419 ->IsInPrimaryMainFrame();
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071420
Peter Boströmd7592132019-01-30 04:50:311421 // Make sure we do not discard the pending entry for a different ongoing
1422 // navigation when a same document commit comes in unexpectedly from the
1423 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1424 // other navigation types. See https://crbug.com/900036.
1425 // TODO(crbug.com/926009): Handle history.pushState() as well.
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061426 bool keep_pending_entry =
1427 is_same_document_navigation &&
1428 details->type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY &&
1429 pending_entry_ && !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311430
[email protected]0e8db942008-09-24 21:21:481431 switch (details->type) {
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061432 case NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY:
Charlie Reisc0f17d2d2021-01-12 18:52:491433 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051434 rfh, params, details->is_same_document, details->did_replace_entry,
Rakina Zata Amnia4e27222021-12-22 01:05:001435 previous_document_was_activated, navigation_request, details);
[email protected]e9ba4472008-09-14 15:42:431436 break;
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061437 case NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY:
Charlie Reisc0f17d2d2021-01-12 18:52:491438 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1439 was_restored, navigation_request,
Rakina Zata Amnia4e27222021-12-22 01:05:001440 keep_pending_entry, details);
[email protected]e9ba4472008-09-14 15:42:431441 break;
[email protected]8ff00d72012-10-23 19:12:211442 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471443 RendererDidNavigateNewSubframe(
1444 rfh, params, details->is_same_document, details->did_replace_entry,
Rakina Zata Amnia4e27222021-12-22 01:05:001445 previous_document_was_activated, navigation_request, details);
[email protected]e9ba4472008-09-14 15:42:431446 break;
[email protected]8ff00d72012-10-23 19:12:211447 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391448 if (!RendererDidNavigateAutoSubframe(
Nate Chapinc7019dd7d2021-06-25 18:29:251449 rfh, params, details->is_same_document,
Rakina Zata Amnia4e27222021-12-22 01:05:001450 was_on_initial_empty_document, navigation_request, details)) {
creisce0ef3572017-01-26 17:53:081451 // We don't send a notification about auto-subframe PageState during
1452 // UpdateStateForFrame, since it looks like nothing has changed. Send
1453 // it here at commit time instead.
1454 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431455 return false;
creis59d5a47cb2016-08-24 23:57:191456 }
[email protected]e9ba4472008-09-14 15:42:431457 break;
Rakina Zata Amni2322f4f82022-01-24 13:24:241458 case NAVIGATION_TYPE_NAV_IGNORE:
1459 // If a pending navigation was in progress, this canceled it. We should
1460 // discard it and make sure it is removed from the URL bar. After that,
1461 // there is nothing we can do with this navigation, so we just return to
1462 // the caller that nothing has happened.
1463 if (pending_entry_)
1464 DiscardNonCommittedEntries();
1465 return false;
Aran Gilman37d11632019-10-08 23:07:151466 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431467 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151468 break;
[email protected]765b35502008-08-21 00:51:201469 }
1470
[email protected]688aa65c62012-09-28 04:32:221471 // At this point, we know that the navigation has just completed, so
1472 // record the time.
1473 //
1474 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261475 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331476 base::Time timestamp =
1477 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1478 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131479 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221480
Peter Boströmd7592132019-01-30 04:50:311481 // If we aren't keeping the pending entry, there shouldn't be one at this
1482 // point. Clear it again in case any error cases above forgot to do so.
1483 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1484 // been cleared instead of protecting against it.
1485 if (!keep_pending_entry)
Rakina Zata Amniddf10502022-01-15 02:56:551486 DiscardNonCommittedEntriesWithCommitDetails(details);
[email protected]f233e4232013-02-23 00:55:141487
[email protected]e9ba4472008-09-14 15:42:431488 // All committed entries should have nonempty content state so WebKit doesn't
1489 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471490 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321491 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221492 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441493 active_entry->SetHttpStatusCode(params.http_status_code);
Fergal Daly0686c0e2022-06-28 02:08:141494
Alexander Timind2f2e4f22019-04-02 20:04:531495 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1496 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281497 if (back_forward_cache_metrics &&
1498 !active_entry->back_forward_cache_metrics()) {
Alexander Timind2f2e4f22019-04-02 20:04:531499 active_entry->set_back_forward_cache_metrics(
1500 std::move(back_forward_cache_metrics));
1501 }
Dave Tapuskaa2ab4f252021-07-08 21:31:281502
1503 // `back_forward_cache_metrics()` may return null as we do not record
1504 // back-forward cache metrics for navigations in non-primary frame trees.
1505 if (active_entry->back_forward_cache_metrics()) {
Fergal Daly0686c0e2022-06-28 02:08:141506 // TODO(https://crbug.com/1338089): Remove this.
1507 // These are both only available from details at this point, so we capture
1508 // them here.
1509 SCOPED_CRASH_KEY_NUMBER("BFCacheMismatch", "navigation_type",
1510 details->type);
1511 SCOPED_CRASH_KEY_BOOL("BFCacheMismatch", "did_replace",
1512 details->did_replace_entry);
Dave Tapuskaa2ab4f252021-07-08 21:31:281513 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
1514 navigation_request,
1515 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
1516 }
naskoc7533512016-05-06 17:01:121517
Charles Reisc0507202017-09-21 00:40:021518 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1519 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1520 // A mismatch can occur if the renderer lies or due to a unique name collision
1521 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121522 FrameNavigationEntry* frame_entry =
1523 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021524 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1525 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031526 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081527 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1528 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031529 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201530
1531 // Remember the bindings the renderer process has at this point, so that
1532 // we do not grant this entry additional bindings if we come back to it.
1533 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301534 }
[email protected]132e281a2012-07-31 18:32:441535
[email protected]97d8f0d2013-10-29 16:49:211536 // Once it is committed, we no longer need to track several pieces of state on
1537 // the entry.
naskoc7533512016-05-06 17:01:121538 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131539
[email protected]49bd30e62011-03-22 20:12:591540 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221541 // TODO(creis): This check won't pass for subframes until we create entries
1542 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001543 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421544 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591545
[email protected]e9ba4472008-09-14 15:42:431546 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001547 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001548 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311549 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531550
arthursonzogni7ddc6542021-04-09 09:16:501551 active_entry->SetIsOverridingUserAgent(
1552 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031553
[email protected]93f230e02011-06-01 14:40:001554 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291555
aelias100c9192017-01-13 00:01:431556 if (overriding_user_agent_changed)
1557 delegate_->UpdateOverridingUserAgent();
1558
creis03b48002015-11-04 00:54:561559 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1560 // one knows the latest NavigationEntry it is showing (whether it has
1561 // committed anything in this navigation or not). This allows things like
1562 // state and title updates from RenderFrames to apply to the latest relevant
1563 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381564 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001565 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381566 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
Hayato Ito2c8c08d02021-06-23 03:38:431567
1568 if (navigation_request->IsPrerenderedPageActivation()) {
1569 BroadcastHistoryOffsetAndLength();
1570 // TODO(crbug.com/1222893): Broadcasting happens after the prerendered page
1571 // is activated. As a result, a "prerenderingchange" event listener sees the
1572 // history.length which is not updated yet. We should guarantee that
1573 // history's length and offset should be updated before a
1574 // "prerenderingchange" event listener runs. One possible approach is to use
1575 // the same IPC which "prerenderingchange" uses, and propagate history's
1576 // length and offset together with that.
1577 }
1578
[email protected]e9ba4472008-09-14 15:42:431579 return true;
initial.commit09911bf2008-07-26 23:55:291580}
1581
[email protected]8ff00d72012-10-23 19:12:211582NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321583 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101584 const mojom::DidCommitProvisionalLoadParams& params,
Rakina Zata Amni2322f4f82022-01-24 13:24:241585 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591586 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511587 "ClassifyNavigation");
1588
avi7c6f35e2015-05-08 17:52:381589 if (params.did_create_new_entry) {
Rakina Zata Amni2322f4f82022-01-24 13:24:241590 // A new entry. We may or may not have a corresponding pending entry, and
1591 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001592 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491593 trace_return.set_return_reason("new entry, no parent, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061594 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381595 }
1596
Rakina Zata Amni2322f4f82022-01-24 13:24:241597 // When this is a new subframe navigation, we should have a committed page
1598 // in which it's a subframe. This may not be the case when an iframe is
1599 // navigated on a popup navigated to about:blank (the iframe would be
1600 // written into the popup by script on the main page). For these cases,
1601 // there isn't any navigation stuff we can do, so just ignore it.
1602 if (!GetLastCommittedEntry()) {
1603 trace_return.set_return_reason("new entry, no last committed, ignore");
1604 return NAVIGATION_TYPE_NAV_IGNORE;
1605 }
1606
1607 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511608 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381609 return NAVIGATION_TYPE_NEW_SUBFRAME;
1610 }
1611
Charlie Reisc0f17d2d2021-01-12 18:52:491612 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381613 DCHECK(!params.history_list_was_cleared);
1614
avi39c1edd32015-06-04 20:06:001615 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381616 // All manual subframes would be did_create_new_entry and handled above, so
1617 // we know this is auto.
Rakina Zata Amni2322f4f82022-01-24 13:24:241618 if (GetLastCommittedEntry()) {
1619 trace_return.set_return_reason("subframe, last commmited, auto subframe");
1620 return NAVIGATION_TYPE_AUTO_SUBFRAME;
1621 }
1622
1623 // We ignore subframes created in non-committed pages; we'd appreciate if
1624 // people stopped doing that.
1625 trace_return.set_return_reason("subframe, no last commmited, ignore");
1626 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381627 }
1628
Rakina Zata Amnif6950d552020-11-24 03:26:101629 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1630 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381631 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1632 // create a new page.
1633
Rakina Zata Amni2322f4f82022-01-24 13:24:241634 // Just like above in the did_create_new_entry case, it's possible to
1635 // scribble onto an uncommitted page. Again, there isn't any navigation
1636 // stuff that we can do, so ignore it here as well.
1637 NavigationEntry* last_committed = GetLastCommittedEntry();
1638 if (!last_committed) {
1639 trace_return.set_return_reason("nav entry 0, no last committed, ignore");
1640 return NAVIGATION_TYPE_NAV_IGNORE;
1641 }
1642
Hayato Ito2ae49442021-07-02 02:59:251643 // This main frame navigation is not a history navigation (since
1644 // nav_entry_id is 0), but didn't create a new entry. So this must be a
1645 // reload or a replacement navigation, which will modify the existing entry.
1646 //
Nasko Oskov332593c2018-08-16 17:21:341647 // TODO(nasko): With error page isolation, reloading an existing session
1648 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491649 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341650 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511651 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491652 "nav entry 0, last committed, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061653 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381654 }
1655
Rakina Zata Amnif6950d552020-11-24 03:26:101656 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521657 // If the SiteInstance of the |pending_entry_| does not match the
1658 // SiteInstance that got committed, treat this as a new navigation with
1659 // replacement. This can happen if back/forward/reload encounters a server
1660 // redirect to a different site or an isolated error page gets successfully
1661 // reloaded into a different SiteInstance.
1662 if (pending_entry_->site_instance() &&
1663 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491664 trace_return.set_return_reason("pending matching nav entry, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061665 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521666 }
creis77c9aa32015-09-25 19:59:421667
Nasko Oskovaee2f862018-06-15 00:05:521668 if (pending_entry_index_ == -1) {
1669 // In this case, we have a pending entry for a load of a new URL but Blink
1670 // didn't do a new navigation (params.did_create_new_entry). First check
1671 // to make sure Blink didn't treat a new cross-process navigation as
1672 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161673 // we must treat it as NEW rather than the converted reload case below,
1674 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521675 if (!GetLastCommittedEntry() ||
1676 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161677 trace_return.set_return_reason("new pending, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061678 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521679 }
1680
1681 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161682 // We will create a pending entry, but NavigateWithoutEntry will convert
1683 // it to a reload since it's the same page and not create a new entry for
1684 // it. (The user doesn't want to have a new back/forward entry when they
1685 // do this.) Therefore we want to just ignore the pending entry and go
1686 // back to where we were (the "existing entry").
1687 trace_return.set_return_reason("new pending, existing (same) entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061688 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521689 }
avi7c6f35e2015-05-08 17:52:381690 }
1691
creis26d22632017-04-21 20:23:561692 // Everything below here is assumed to be an existing entry, but if there is
1693 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511694 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491695 trace_return.set_return_reason("no last committed, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061696 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511697 }
creis26d22632017-04-21 20:23:561698
Rakina Zata Amni153d5702021-09-13 22:48:001699 if (navigation_request->commit_params().intended_as_new_entry) {
avi7c6f35e2015-05-08 17:52:381700 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491701 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1702 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161703 trace_return.set_return_reason("intended as new entry, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061704 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381705 }
1706
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121707 if (navigation_request->DidEncounterError() &&
1708 failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101709 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381710 // If the renderer was going to a new pending entry that got cleared because
1711 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491712 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381713 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511714 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491715 "unreachable, matching pending, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061716 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381717 }
1718
Charlie Reisc0f17d2d2021-01-12 18:52:491719 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101720 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511721 trace_return.traced_value()->SetInteger("existing_entry_index",
1722 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381723 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441724 // The renderer has committed a navigation to an entry that no longer
1725 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491726 trace_return.set_return_reason("existing entry -1, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061727 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381728 }
1729
avi7c6f35e2015-05-08 17:52:381730 // Since we weeded out "new" navigations above, we know this is an existing
1731 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491732 trace_return.set_return_reason("default return, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061733 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381734}
1735
Rakina Zata Amni3460d382021-10-29 00:43:371736void NavigationControllerImpl::UpdateNavigationEntryDetails(
1737 NavigationEntryImpl* entry,
1738 RenderFrameHostImpl* rfh,
1739 const mojom::DidCommitProvisionalLoadParams& params,
1740 NavigationRequest* request,
1741 NavigationEntryImpl::UpdatePolicy update_policy,
Rakina Zata Amnia4e27222021-12-22 01:05:001742 bool is_new_entry,
1743 LoadCommittedDetails* commit_details) {
Rakina Zata Amni3460d382021-10-29 00:43:371744 // Update the FrameNavigationEntry.
Rakina Zata Amniafd3c6582021-11-30 06:19:171745 std::vector<GURL> redirects;
Rakina Zata Amni3460d382021-10-29 00:43:371746 entry->AddOrUpdateFrameEntry(
1747 rfh->frame_tree_node(), update_policy, params.item_sequence_number,
Domenic Denicolacc094fb2022-03-16 23:40:571748 params.document_sequence_number, params.navigation_api_key,
Rakina Zata Amni3460d382021-10-29 00:43:371749 rfh->GetSiteInstance(), nullptr, params.url,
1750 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amniafd3c6582021-11-30 06:19:171751 Referrer(*params.referrer),
1752 request ? request->common_params().initiator_origin : params.origin,
1753 request ? request->GetRedirectChain() : redirects, params.page_state,
1754 params.method, params.post_id, nullptr /* blob_url_loader_factory */,
1755 (request && request->web_bundle_navigation_info())
Rakina Zata Amni3460d382021-10-29 00:43:371756 ? request->web_bundle_navigation_info()->Clone()
1757 : nullptr,
Rakina Zata Amniafd3c6582021-11-30 06:19:171758 (request ? request->GetSubresourceWebBundleNavigationInfo() : nullptr),
Rakina Zata Amni3460d382021-10-29 00:43:371759 ComputePolicyContainerPoliciesForFrameEntry(
Rakina Zata Amniafd3c6582021-11-30 06:19:171760 rfh, request && request->IsSameDocument(),
1761 request ? request->common_params().url : params.url));
Rakina Zata Amni3460d382021-10-29 00:43:371762
1763 if (rfh->GetParent()) {
1764 // Only modify the NavigationEntry for main frame navigations.
1765 return;
1766 }
1767 if (entry->update_virtual_url_with_url())
1768 UpdateVirtualURLToURL(entry, params.url);
1769 // Don't use the page type from the pending entry. Some interstitial page
1770 // may have set the type to interstitial. Once we commit, however, the page
1771 // type must always be normal or error.
Rakina Zata Amniafd3c6582021-11-30 06:19:171772 entry->set_page_type((request && request->DidEncounterError())
1773 ? PAGE_TYPE_ERROR
1774 : PAGE_TYPE_NORMAL);
Rakina Zata Amnif297a802022-01-18 03:53:431775 if (commit_details && commit_details->should_stay_as_initial_entry) {
1776 // Retain the "initial NavigationEntry" status.
1777 if (request->IsSameDocument()) {
1778 // If this is for a same-document navigation, the NavigationEntry must be
1779 // reused and should already be marked as the initial NavigationEntry.
1780 DCHECK(entry->IsInitialEntry());
1781 } else {
1782 // If this is for a cross-document navigation, it can be caused by a
1783 // renderer-initiated reload, or the synchronous about:blank commit. Mark
1784 // "for synchronous about:blank" in the latter case, and also when it is
1785 // reloading a "for synchronous about:blank" entry. Otherwise, the entry
1786 // is not for a synchronous about:blank commit.
1787 NavigationEntryImpl::InitialNavigationEntryState new_state =
1788 NavigationEntryImpl::InitialNavigationEntryState::
1789 kInitialNotForSynchronousAboutBlank;
1790 if (entry->IsInitialEntryForSynchronousAboutBlank() ||
1791 request->is_synchronous_renderer_commit()) {
1792 new_state = NavigationEntryImpl::InitialNavigationEntryState::
1793 kInitialForSynchronousAboutBlank;
1794 }
1795 entry->set_initial_navigation_entry_state(new_state);
1796 }
1797 } else if (commit_details && !commit_details->should_stay_as_initial_entry) {
1798 // Remove the "initial NavigationEntry" status.
1799 entry->set_initial_navigation_entry_state(
1800 NavigationEntryImpl::InitialNavigationEntryState::kNonInitial);
Rakina Zata Amnia4e27222021-12-22 01:05:001801 }
Rakina Zata Amniddf10502022-01-15 02:56:551802
Rakina Zata Amni3460d382021-10-29 00:43:371803 if (is_new_entry) {
1804 // Some properties of the NavigationEntry are only set when the entry is
1805 // new (we aren't reusing it).
1806 entry->SetTransitionType(params.transition);
Rakina Zata Amniafd3c6582021-11-30 06:19:171807 entry->SetOriginalRequestURL(request ? request->GetOriginalRequestURL()
1808 : GURL::EmptyGURL());
Rakina Zata Amni3460d382021-10-29 00:43:371809 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
1810 entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
1811 } else {
1812 // We are reusing the NavigationEntry. The site instance will normally be
1813 // the same except for a few cases:
1814 // 1) session restore, when no site instance will be assigned or
1815 // 2) redirect, when the site instance is reset.
1816 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
1817 entry->site_instance() == rfh->GetSiteInstance());
1818 }
1819}
1820
Rakina Zata Amniafd3c6582021-11-30 06:19:171821void NavigationControllerImpl::CreateInitialEntry() {
1822 DCHECK_EQ(entries_.size(), 0u);
1823 RenderFrameHostImpl* rfh = frame_tree_.root()->current_frame_host();
1824 auto params = mojom::DidCommitProvisionalLoadParams::New();
1825 // The initial NavigationEntry's URL is the empty URL. This preserves the old
1826 // behavior of WebContent's GetLastCommittedURL() and GetVisibleURL() from
1827 // before we have initial NavigationEntries.
1828 params->url = GURL::EmptyGURL();
1829 params->http_status_code = 0;
1830 params->url_is_unreachable = false;
1831 params->method = "GET";
1832 params->should_replace_current_entry = false;
1833 params->post_id = -1;
1834 params->embedding_token = base::UnguessableToken::Create();
1835 params->navigation_token = base::UnguessableToken::Create();
1836 params->did_create_new_entry = true;
1837 params->origin = rfh->GetLastCommittedOrigin();
1838 params->should_update_history = true;
1839 params->item_sequence_number = 0;
1840 params->document_sequence_number = 0;
1841 bool is_in_fenced_frame_tree = rfh->frame_tree_node()->IsInFencedFrameTree();
1842 params->transition = is_in_fenced_frame_tree
1843 ? ui::PAGE_TRANSITION_AUTO_SUBFRAME
1844 : ui::PAGE_TRANSITION_LINK;
1845 params->referrer = blink::mojom::Referrer::New();
1846
1847 // Create and insert the initial NavigationEntry.
1848 auto new_entry = std::make_unique<NavigationEntryImpl>(
1849 rfh->GetSiteInstance(), params->url, Referrer(*params->referrer),
1850 rfh->GetLastCommittedOrigin(), std::u16string() /* title */,
1851 ui::PAGE_TRANSITION_TYPED, false /* renderer_initiated */,
1852 nullptr /* blob_url_loader_factory */, true /* is_initial_entry */);
1853 UpdateNavigationEntryDetails(
1854 new_entry.get(), rfh, *params, nullptr /* request */,
Rakina Zata Amnia4e27222021-12-22 01:05:001855 NavigationEntryImpl::UpdatePolicy::kUpdate, true /* is_new_entry */,
1856 nullptr /* commit_details */);
Rakina Zata Amniafd3c6582021-11-30 06:19:171857
1858 InsertOrReplaceEntry(std::move(new_entry), false /* replace_entry */,
1859 false /* was_post_commit_error */,
Rakina Zata Amnia4e27222021-12-22 01:05:001860 is_in_fenced_frame_tree, nullptr /* commit_details */);
Rakina Zata Amniafd3c6582021-11-30 06:19:171861}
1862
Charlie Reisc0f17d2d2021-01-12 18:52:491863void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321864 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071865 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361866 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441867 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471868 bool previous_document_was_activated,
Rakina Zata Amnia4e27222021-12-22 01:05:001869 NavigationRequest* request,
1870 LoadCommittedDetails* commit_details) {
dcheng9bfa5162016-04-09 01:00:571871 std::unique_ptr<NavigationEntryImpl> new_entry;
Anton Bikineevf62d1bf2021-05-15 17:56:071872 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451873 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061874
creisf49dfc92016-07-26 17:05:181875 // First check if this is an in-page navigation. If so, clone the current
1876 // entry instead of looking at the pending entry, because the pending entry
1877 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361878 if (is_same_document && GetLastCommittedEntry()) {
Nate Chapin63db0d12022-01-20 22:03:301879 FrameNavigationEntry* previous_frame_entry =
1880 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
Patrick Monette50e8bd82019-06-13 22:40:451881 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481882 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Domenic Denicolacc094fb2022-03-16 23:40:571883 params.document_sequence_number, params.navigation_api_key,
Nate Chapinfbfe5af2021-06-10 17:22:081884 rfh->GetSiteInstance(), nullptr, params.url,
1885 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091886 Referrer(*params.referrer), initiator_origin,
1887 request->GetRedirectChain(), params.page_state, params.method,
1888 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391889 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481890 request->GetSubresourceWebBundleNavigationInfo(),
Antonio Sartori78a749f2020-11-30 12:03:391891 // We will set the document policies later in this function.
Nate Chapin63db0d12022-01-20 22:03:301892 nullptr /* policy_container_policies */,
Domenic Denicolacc094fb2022-03-16 23:40:571893 // Try to preserve protect_url_in_navigation_api from the previous
Nate Chapin63db0d12022-01-20 22:03:301894 // FrameNavigationEntry.
1895 previous_frame_entry &&
Domenic Denicolacc094fb2022-03-16 23:40:571896 previous_frame_entry->protect_url_in_navigation_api());
Charles Reisf44482022017-10-13 21:15:031897
creisf49dfc92016-07-26 17:05:181898 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Harkiran Bolaria081c5c22021-09-13 08:44:531899 frame_entry, true, request->frame_tree_node(), frame_tree_.root());
Mike West800532c2021-10-14 09:26:521900 if (new_entry->GetURL().DeprecatedGetOriginAsURL() !=
1901 params.url.DeprecatedGetOriginAsURL()) {
jama78746e2017-02-22 17:21:571902 // TODO(jam): we had one report of this with a URL that was redirecting to
1903 // only tildes. Until we understand that better, don't copy the cert in
1904 // this case.
1905 new_entry->GetSSL() = SSLStatus();
jama78746e2017-02-22 17:21:571906 }
creisf49dfc92016-07-26 17:05:181907
Patrick Monette50e8bd82019-06-13 22:40:451908 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1909 // that |frame_entry| should now have a reference count of exactly 2: one
1910 // due to the local variable |frame_entry|, and another due to |new_entry|
1911 // also retaining one. This should never fail, because it's the main frame.
1912 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181913 }
1914
Harkiran Bolaria59290d62021-03-17 01:53:011915 // If this is an activation navigation from a prerendered page, transfer the
1916 // new entry from an entry already created and stored in the
1917 // NavigationRequest. |new_entry| will not have been set prior to this as
1918 // |is_same_document| is mutually exclusive with
1919 // |IsPrerenderedPageActivation|.
1920 if (request->IsPrerenderedPageActivation()) {
1921 DCHECK(!is_same_document);
1922 DCHECK(!new_entry);
1923 new_entry = request->TakePrerenderNavigationEntry();
1924 DCHECK(new_entry);
1925 }
1926
Charlie Reisc0f17d2d2021-01-12 18:52:491927 // Only make a copy of the pending entry if it is appropriate for the new
1928 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451929 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041930 // 1. The SiteInstance hasn't been assigned to something else.
1931 // 2. The pending entry was intended as a new entry, rather than being a
1932 // history navigation that was interrupted by an unrelated,
1933 // renderer-initiated navigation.
1934 // TODO(csharrison): Investigate whether we can remove some of the coarser
1935 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451936 if (!new_entry && PendingEntryMatchesRequest(request) &&
1937 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341938 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431939 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351940 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431941
Camille Lamy62b826012019-02-26 09:15:471942 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451943 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
creisf49dfc92016-07-26 17:05:181944 }
1945
Charlie Reisc0f17d2d2021-01-12 18:52:491946 // For cross-document commits with no matching pending entry, create a new
1947 // entry.
creisf49dfc92016-07-26 17:05:181948 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061949 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071950 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1951 initiator_origin,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:581952 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451953 params.transition, request->IsRendererInitiated(),
Rakina Zata Amniafd3c6582021-11-30 06:19:171954 nullptr, // blob_url_loader_factory
1955 false); // is_initial_entry
[email protected]f8f93eb2012-09-25 03:06:241956
1957 // Find out whether the new entry needs to update its virtual URL on URL
1958 // change and set up the entry accordingly. This is needed to correctly
1959 // update the virtual URL when replaceState is called after a pushState.
1960 GURL url = params.url;
1961 bool needs_update = false;
Charlie Reisc0f17d2d2021-01-12 18:52:491962 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411963 // update the virtual URL based on the new URL. For example, this is needed
1964 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1965 // the URL.
Rakina Zata Amni3460d382021-10-29 00:43:371966 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1967 &url, browser_context_, &needs_update);
1968 new_entry->set_update_virtual_url_with_url(needs_update);
1969
Camille Lamy62b826012019-02-26 09:15:471970 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451971 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
[email protected]e9ba4472008-09-14 15:42:431972 }
1973
Harkiran Bolaria59290d62021-03-17 01:53:011974 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
1975 // for prerendered contents, if any. This is because prerendering/activation
1976 // technically won't be creating a new document. Unlike BFCache, prerendering
1977 // creates a new NavigationEntry rather than using an existing one.
1978 if (!request->IsPrerenderedPageActivation()) {
Rakina Zata Amni3460d382021-10-29 00:43:371979 UpdateNavigationEntryDetails(new_entry.get(), rfh, params, request,
1980 NavigationEntryImpl::UpdatePolicy::kUpdate,
Rakina Zata Amnia4e27222021-12-22 01:05:001981 true /* is_new_entry */, commit_details);
creis8b5cd4c2015-06-19 00:11:081982
Harkiran Bolaria59290d62021-03-17 01:53:011983 // history.pushState() is classified as a navigation to a new page, but sets
1984 // is_same_document to true. In this case, we already have the title and
1985 // favicon available, so set them immediately.
1986 if (is_same_document && GetLastCommittedEntry()) {
1987 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
1988 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1989 }
[email protected]3a868f212014-08-09 10:41:191990 }
[email protected]ff64b3e2014-05-31 04:07:331991
[email protected]60d6cca2013-04-30 08:47:131992 DCHECK(!params.history_list_was_cleared || !replace_entry);
1993 // The browser requested to clear the session history when it initiated the
1994 // navigation. Now we know that the renderer has updated its state accordingly
1995 // and it is safe to also clear the browser side history.
1996 if (params.history_list_was_cleared) {
Rakina Zata Amniddf10502022-01-15 02:56:551997 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]60d6cca2013-04-30 08:47:131998 entries_.clear();
1999 last_committed_entry_index_ = -1;
2000 }
2001
Nasko Oskovaee2f862018-06-15 00:05:522002 // If this is a new navigation with replacement and there is a
2003 // pending_entry_ which matches the navigation reported by the renderer
2004 // process, then it should be the one replaced, so update the
2005 // last_committed_entry_index_ to use it.
2006 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:102007 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:522008 last_committed_entry_index_ = pending_entry_index_;
2009 }
2010
Alexander Timine3ec4192020-04-20 16:39:402011 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:412012 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:402013 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:052014
Carlos IL42b416592019-10-07 23:10:362015 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
Dave Tapuska87696ae2021-11-18 18:48:312016 !request->post_commit_error_page_html().empty(),
Rakina Zata Amnia4e27222021-12-22 01:05:002017 rfh->frame_tree_node()->IsInFencedFrameTree(),
2018 commit_details);
[email protected]e9ba4472008-09-14 15:42:432019}
2020
Charlie Reisc0f17d2d2021-01-12 18:52:492021void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:322022 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072023 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:362024 bool is_same_document,
jam48cea9082017-02-15 06:13:292025 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:452026 NavigationRequest* request,
Rakina Zata Amnia4e27222021-12-22 01:05:002027 bool keep_pending_entry,
2028 LoadCommittedDetails* commit_details) {
creis26d22632017-04-21 20:23:562029 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
2030 << "that a last committed entry exists.";
2031
[email protected]e9ba4472008-09-14 15:42:432032 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:002033 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:432034
Charlie Reis7e2cb6d2021-01-26 01:27:162035 NavigationEntryImpl* entry = nullptr;
Rakina Zata Amni153d5702021-09-13 22:48:002036 if (request->commit_params().intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:162037 // We're guaranteed to have a last committed entry if intended_as_new_entry
2038 // is true.
avicbdc4c12015-07-01 16:07:112039 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:162040 DCHECK(entry);
2041
2042 // If the NavigationRequest matches a new pending entry and is classified as
2043 // EXISTING_ENTRY, then it is a navigation to the same URL that was
2044 // converted to a reload, such as a user pressing enter in the omnibox.
2045 if (pending_entry_ && pending_entry_index_ == -1 &&
2046 pending_entry_->GetUniqueID() ==
2047 request->commit_params().nav_entry_id) {
2048 // Note: The pending entry will usually have a real ReloadType here, but
2049 // it can still be ReloadType::NONE in cases that
2050 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
2051
2052 // If we classified this correctly, the SiteInstance should not have
2053 // changed.
2054 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
2055
2056 // For converted reloads, we assign the entry's unique ID to be that of
2057 // the new one. Since this is always the result of a user action, we want
2058 // to dismiss infobars, etc. like a regular user-initiated navigation.
2059 entry->set_unique_id(pending_entry_->GetUniqueID());
2060
2061 // The extra headers may have changed due to reloading with different
2062 // headers.
2063 entry->set_extra_headers(pending_entry_->extra_headers());
2064 }
2065 // Otherwise, this was intended as a new entry but the pending entry was
2066 // lost in the meantime and no new entry was created. We are stuck at the
2067 // last committed entry.
2068
2069 // Even if this is a converted reload from pressing enter in the omnibox,
2070 // the server could redirect, requiring an update to the SSL status. If this
2071 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:452072 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:162073 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:472074 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:452075 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:162076 }
Rakina Zata Amnif6950d552020-11-24 03:26:102077 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:112078 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:102079 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b902016-09-01 16:58:162080
eugenebut604866f2017-05-10 21:35:362081 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:452082 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:362083 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
2084 // this was a restored same document navigation entry, then it won't have
2085 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
2086 // navigated it.
jam48cea9082017-02-15 06:13:292087 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
Mike West800532c2021-10-14 09:26:522088 if (entry->GetURL().DeprecatedGetOriginAsURL() ==
2089 last_entry->GetURL().DeprecatedGetOriginAsURL() &&
jam48cea9082017-02-15 06:13:292090 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
2091 was_restored) {
2092 entry->GetSSL() = last_entry->GetSSL();
2093 }
2094 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:452095 // In rapid back/forward navigations |request| sometimes won't have a cert
2096 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:192097 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:452098 if (request->GetSSLInfo().has_value() &&
2099 request->GetSSLInfo()->is_valid()) {
2100 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
Mike West800532c2021-10-14 09:26:522101 } else if (entry->GetURL().DeprecatedGetOriginAsURL() !=
2102 request->GetURL().DeprecatedGetOriginAsURL()) {
John Abd-El-Malek3f247082017-12-07 19:02:192103 entry->GetSSL() = SSLStatus();
2104 }
jam48cea9082017-02-15 06:13:292105 }
avicbdc4c12015-07-01 16:07:112106 } else {
Feifei Wang2ab8ba6c2022-04-13 22:19:272107 // This is renderer-initiated. The only kinds of renderer-initiated
Rakina Zata Amni557afb92021-07-17 04:39:572108 // navigations that are EXISTING_ENTRY are same-document navigations that
2109 // result in replacement (e.g. history.replaceState(), location.replace(),
2110 // forced replacements for trivial session history contexts). For these
2111 // cases, we reuse the last committed entry.
avicbdc4c12015-07-01 16:07:112112 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:102113
Mikel Astizba9cf2fd2017-12-17 10:38:102114 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
2115 // to avoid misleading interpretations (e.g. URLs paired with
2116 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
2117 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
2118 // | PAGE_TRANSITION_CLIENT_REDIRECT).
2119
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572120 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:102121
eugenebut604866f2017-05-10 21:35:362122 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:452123 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:362124 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:472125 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:452126 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
avicbdc4c12015-07-01 16:07:112127 }
2128 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:432129
Rakina Zata Amni3460d382021-10-29 00:43:372130 UpdateNavigationEntryDetails(entry, rfh, params, request,
2131 NavigationEntryImpl::UpdatePolicy::kUpdate,
Rakina Zata Amnia4e27222021-12-22 01:05:002132 false /* is_new_entry */, commit_details);
creis22a7b4c2016-04-28 07:20:302133
[email protected]5ccd4dc2012-10-24 02:28:142134 // The redirected to page should not inherit the favicon from the previous
2135 // page.
eugenebut604866f2017-05-10 21:35:362136 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:482137 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:142138
Peter Boströmd7592132019-01-30 04:50:312139 // We should also usually discard the pending entry if it corresponds to a
2140 // different navigation, since that one is now likely canceled. In rare
2141 // cases, we leave the pending entry for another navigation in place when we
2142 // know it is still ongoing, to avoid a flicker in the omnibox (see
2143 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:432144 //
2145 // Note that we need to use the "internal" version since we don't want to
2146 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:312147 if (!keep_pending_entry)
Rakina Zata Amnia4e27222021-12-22 01:05:002148 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]40bcc302009-03-02 20:50:392149
Carlos IL4dea8902020-05-26 15:14:292150 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:112151 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:432152}
2153
[email protected]d202a7c2012-01-04 07:53:472154void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:322155 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072156 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:362157 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:472158 bool replace_entry,
2159 bool previous_document_was_activated,
Rakina Zata Amnia4e27222021-12-22 01:05:002160 NavigationRequest* request,
2161 LoadCommittedDetails* commit_details) {
avi25f5f9e2015-07-17 20:08:262162 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2163 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
Rakina Zata Amniddf10502022-01-15 02:56:552164 // The NEW_SUBFRAME path should never result in an initial NavigationEntry.
2165 DCHECK(!commit_details->should_stay_as_initial_entry);
[email protected]09b8f82f2009-06-16 20:22:112166
[email protected]e9ba4472008-09-14 15:42:432167 // Manual subframe navigations just get the current entry cloned so the user
2168 // can go back or forward to it. The actual subframe information will be
2169 // stored in the page state for each of those entries. This happens out of
2170 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:092171 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
2172 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:382173
Mikel Astizba9cf2fd2017-12-17 10:38:102174 // The DCHECK below documents the fact that we don't know of any situation
2175 // where |replace_entry| is true for subframe navigations. This simplifies
2176 // reasoning about the replacement struct for subframes (see
2177 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
2178 DCHECK(!replace_entry);
2179
Patrick Monette50e8bd82019-06-13 22:40:452180 // This FrameNavigationEntry might not end up being used in the
2181 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Anton Bikineevf62d1bf2021-05-15 17:56:072182 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452183 request->common_params().initiator_origin;
Nate Chapin63db0d12022-01-20 22:03:302184 std::unique_ptr<PolicyContainerPolicies> policy_container_policies =
2185 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
2186 request->GetURL());
Domenic Denicolacc094fb2022-03-16 23:40:572187 bool protect_url_in_navigation_api = false;
Nate Chapin63db0d12022-01-20 22:03:302188 if (is_same_document) {
2189 FrameNavigationEntry* previous_frame_entry =
2190 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
Domenic Denicolacc094fb2022-03-16 23:40:572191 // Try to preserve protect_url_in_navigation_api from the previous
Nate Chapin63db0d12022-01-20 22:03:302192 // FrameNavigationEntry.
Domenic Denicolacc094fb2022-03-16 23:40:572193 protect_url_in_navigation_api =
Nate Chapin63db0d12022-01-20 22:03:302194 previous_frame_entry &&
Domenic Denicolacc094fb2022-03-16 23:40:572195 previous_frame_entry->protect_url_in_navigation_api();
Nate Chapin63db0d12022-01-20 22:03:302196 } else {
Domenic Denicolacc094fb2022-03-16 23:40:572197 protect_url_in_navigation_api =
Nate Chapin63db0d12022-01-20 22:03:302198 policy_container_policies &&
Domenic Denicolacc094fb2022-03-16 23:40:572199 ShouldProtectUrlInNavigationApi(
Nate Chapin63db0d12022-01-20 22:03:302200 policy_container_policies->referrer_policy);
2201 }
2202
Patrick Monette50e8bd82019-06-13 22:40:452203 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:482204 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Domenic Denicolacc094fb2022-03-16 23:40:572205 params.document_sequence_number, params.navigation_api_key,
Nate Chapinfbfe5af2021-06-10 17:22:082206 rfh->GetSiteInstance(), nullptr, params.url,
2207 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:092208 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072209 params.page_state, params.method, params.post_id,
2210 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032211 request->web_bundle_navigation_info()
2212 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392213 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482214 request->GetSubresourceWebBundleNavigationInfo(),
Domenic Denicolacc094fb2022-03-16 23:40:572215 std::move(policy_container_policies), protect_url_in_navigation_api);
Charles Reisf44482022017-10-13 21:15:032216
creisce0ef3572017-01-26 17:53:082217 std::unique_ptr<NavigationEntryImpl> new_entry =
2218 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:452219 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:002220 frame_tree_.root());
creise062d542015-08-25 02:01:552221
Alexander Timine3ec4192020-04-20 16:39:402222 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:412223 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:402224 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:472225
creisce0ef3572017-01-26 17:53:082226 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:452227 // to replace, which can happen due to a unique name change. See
2228 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
2229 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:382230
Dave Tapuska87696ae2021-11-18 18:48:312231 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false,
Rakina Zata Amnia4e27222021-12-22 01:05:002232 rfh->frame_tree_node()->IsInFencedFrameTree(),
2233 commit_details);
[email protected]e9ba4472008-09-14 15:42:432234}
2235
[email protected]d202a7c2012-01-04 07:53:472236bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:322237 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072238 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392239 bool is_same_document,
Nate Chapinc7019dd7d2021-06-25 18:29:252240 bool was_on_initial_empty_document,
Rakina Zata Amnia4e27222021-12-22 01:05:002241 NavigationRequest* request,
2242 LoadCommittedDetails* commit_details) {
avi9f07a0c2015-02-18 22:51:292243 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2244 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2245
[email protected]e9ba4472008-09-14 15:42:432246 // We're guaranteed to have a previously committed entry, and we now need to
2247 // handle navigation inside of a subframe in it without creating a new entry.
2248 DCHECK(GetLastCommittedEntry());
2249
creis913c63ce2016-07-16 19:52:522250 // For newly created subframes, we don't need to send a commit notification.
2251 // This is only necessary for history navigations in subframes.
2252 bool send_commit_notification = false;
2253
Rakina Zata Amnif6950d552020-11-24 03:26:102254 // If |nav_entry_id| is non-zero and matches an existing entry, this
2255 // is a history navigation. Update the last committed index accordingly. If
2256 // we don't recognize the |nav_entry_id|, it might be a recently
2257 // pruned entry. We'll handle it below.
2258 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2259 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472260 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062261 // Make sure that a subframe commit isn't changing the main frame's
2262 // origin. Otherwise the renderer process may be confused, leading to a
2263 // URL spoof. We can't check the path since that may change
2264 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572265 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2266 // about:blank, file, and unique origins are more subtle to get right.
Charlie Reis95fbca442021-05-21 21:38:242267 // We should use checks similar to RenderFrameHostImpl's
2268 // CanCommitUrlAndOrigin on the main frame during subframe commits.
2269 // See https://crbug.com/1209092.
creis37988b92016-06-10 18:03:572270 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2271 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2272 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2273 dest_top_url.SchemeIsHTTPOrHTTPS() &&
Mike West800532c2021-10-14 09:26:522274 current_top_url.DeprecatedGetOriginAsURL() !=
2275 dest_top_url.DeprecatedGetOriginAsURL()) {
Chris Bookholt10f4b7332022-02-14 18:25:442276 bad_message::ReceivedBadMessage(rfh->GetMainFrame()->GetProcess(),
creisfb6eeb62016-05-10 19:01:512277 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062278 }
creis3cdc3b02015-05-29 23:00:472279
creis913c63ce2016-07-16 19:52:522280 // We only need to discard the pending entry in this history navigation
2281 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062282 last_committed_entry_index_ = entry_index;
Rakina Zata Amnia4e27222021-12-22 01:05:002283 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
creis913c63ce2016-07-16 19:52:522284
2285 // History navigations should send a commit notification.
2286 send_commit_notification = true;
avi98405c22015-05-21 20:47:062287 }
[email protected]e9ba4472008-09-14 15:42:432288 }
[email protected]f233e4232013-02-23 00:55:142289
creisce0ef3572017-01-26 17:53:082290 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2291 // it may be a "history auto" case where we update an existing one.
2292 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Nate Chapin9f169072021-06-09 19:32:372293
2294 // We may want to update |last_committed|'s FrameNavigationEntry (if one
2295 // exists), or we may want to clobber it and create a new one. We update in
2296 // cases where the corresponding FrameNavigationEntry is conceptually similar
2297 // to the document described by the commit params: same-document
2298 // navigations, history traversal to an existing entry, and reloads (including
2299 // a "soft reload" where we navigate to the same url without flagging it as a
2300 // reload). But in the case of a different document that is not logically
2301 // related to the committed FrameNavigationEntry's document (cross-document,
2302 // not same url, not a reload, not a history traversal), we replace rather
2303 // than update.
Nate Chapinc7019dd7d2021-06-25 18:29:252304 //
Nate Chapin9f169072021-06-09 19:32:372305 // In the case where we update, the FrameNavigationEntry will potentially be
2306 // shared across multiple NavigationEntries, and any updates will be reflected
2307 // in all of those NavigationEntries. In the replace case, any existing
2308 // sharing with other NavigationEntries will stop.
Nate Chapinc7019dd7d2021-06-25 18:29:252309 //
2310 // When navigating away from the initial empty document, we also update rather
2311 // than replace. Either update or replace will overwrite the initial empty
2312 // document state for |last_committed|, but if the FrameNavigationEntry for
2313 // the initial empty document is shared across multiple NavigationEntries (due
2314 // to a navigation in another frame), we want to make sure we overwrite the
2315 // initial empty document state everywhere this FrameNavigationEntry is used,
2316 // which is accompished by updating the existing FrameNavigationEntry.
Nate Chapin9f169072021-06-09 19:32:372317 FrameNavigationEntry* last_committed_frame_entry =
2318 last_committed->GetFrameEntry(rfh->frame_tree_node());
2319 NavigationEntryImpl::UpdatePolicy update_policy =
2320 NavigationEntryImpl::UpdatePolicy::kUpdate;
2321 if (request->common_params().navigation_type ==
Minggang Wangb9f3fa92021-07-01 15:30:312322 blink::mojom::NavigationType::DIFFERENT_DOCUMENT &&
Nate Chapin9f169072021-06-09 19:32:372323 last_committed_frame_entry &&
Nate Chapinc7019dd7d2021-06-25 18:29:252324 last_committed_frame_entry->url() != params.url &&
2325 !was_on_initial_empty_document) {
Nate Chapin9f169072021-06-09 19:32:372326 update_policy = NavigationEntryImpl::UpdatePolicy::kReplace;
2327 }
2328
Rakina Zata Amni3460d382021-10-29 00:43:372329 UpdateNavigationEntryDetails(last_committed, rfh, params, request,
Rakina Zata Amnia4e27222021-12-22 01:05:002330 update_policy, false /* is_new_entry */,
2331 commit_details);
creis625a0c7d2015-03-24 23:17:122332
creis913c63ce2016-07-16 19:52:522333 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432334}
2335
[email protected]d202a7c2012-01-04 07:53:472336int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232337 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032338 for (size_t i = 0; i < entries_.size(); ++i) {
2339 if (entries_[i].get() == entry)
2340 return i;
2341 }
2342 return -1;
[email protected]765b35502008-08-21 00:51:202343}
2344
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572345void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242346 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572347 NavigationControllerImpl* source =
2348 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572349 // Verify that we look new.
Rakina Zata Amni2322f4f82022-01-24 13:24:242350 if (blink::features::IsInitialNavigationEntryEnabled()) {
2351 DCHECK_EQ(1, GetEntryCount());
2352 DCHECK(GetLastCommittedEntry()->IsInitialEntry());
2353 } else {
2354 DCHECK_EQ(0, GetEntryCount());
2355 }
Lei Zhang96031532019-10-10 19:05:472356 DCHECK(!GetPendingEntry());
Rakina Zata Amniafd3c6582021-11-30 06:19:172357 entries_.clear();
[email protected]ce3fa3c2009-04-20 19:55:572358
Rakina Zata Amniafd3c6582021-11-30 06:19:172359 if (source->GetEntryCount() == 0) {
Rakina Zata Amniafd3c6582021-11-30 06:19:172360 return;
2361 }
[email protected]ce3fa3c2009-04-20 19:55:572362
Francois Dorayeaace782017-06-21 16:37:242363 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442364 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572365 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572366
Fergal Dalya1d569972021-03-16 03:24:532367 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302368 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532369 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2370 session_storage_namespace_map_[it.first] = source_namespace->Clone();
[email protected]fdac6ade2013-07-20 01:06:302371 }
[email protected]4e6419c2010-01-15 04:50:342372
Lukasz Anforowicz0de0f452020-12-02 19:57:152373 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572374}
2375
Aran Gilman37d11632019-10-08 23:07:152376void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2377 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162378 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012379 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162380
[email protected]d202a7c2012-01-04 07:53:472381 NavigationControllerImpl* source =
2382 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252383
avi2b177592014-12-10 02:08:022384 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012385 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252386
[email protected]474f8512013-05-31 22:31:162387 // We now have one entry, possibly with a new pending entry. Ensure that
2388 // adding the entries from source won't put us over the limit.
2389 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572390 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572391 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252392
Carlos IL4dea8902020-05-26 15:14:292393 // Insert the entries from source. Ignore any pending entry, since it has not
2394 // committed in source.
[email protected]474f8512013-05-31 22:31:162395 int max_source_index = source->last_committed_entry_index_;
Rakina Zata Amniafd3c6582021-11-30 06:19:172396 DCHECK_NE(max_source_index, -1);
2397 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572398
2399 // Ignore the source's current entry if merging with replacement.
2400 // TODO(davidben): This should preserve entries forward of the current
2401 // too. http://crbug.com/317872
2402 if (replace_entry && max_source_index > 0)
2403 max_source_index--;
2404
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572405 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252406
2407 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552408 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142409
Hayato Ito2c8c08d02021-06-23 03:38:432410 BroadcastHistoryOffsetAndLength();
[email protected]e1cd5452010-08-26 18:03:252411}
2412
[email protected]79368982013-11-13 01:11:012413bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162414 // If there is no last committed entry, we cannot prune. Even if there is a
2415 // pending entry, it may not commit, leaving this WebContents blank, despite
2416 // possibly giving it new entries via CopyStateFromAndPrune.
2417 if (last_committed_entry_index_ == -1)
2418 return false;
[email protected]9350602e2013-02-26 23:27:442419
[email protected]474f8512013-05-31 22:31:162420 // We cannot prune if there is a pending entry at an existing entry index.
2421 // It may not commit, so we have to keep the last committed entry, and thus
2422 // there is no sensible place to keep the pending entry. It is ok to have
2423 // a new pending entry, which can optionally commit as a new navigation.
2424 if (pending_entry_index_ != -1)
2425 return false;
2426
[email protected]474f8512013-05-31 22:31:162427 return true;
2428}
2429
[email protected]79368982013-11-13 01:11:012430void NavigationControllerImpl::PruneAllButLastCommitted() {
2431 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162432
avi2b177592014-12-10 02:08:022433 DCHECK_EQ(0, last_committed_entry_index_);
2434 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442435
Hayato Ito2c8c08d02021-06-23 03:38:432436 BroadcastHistoryOffsetAndLength();
[email protected]9350602e2013-02-26 23:27:442437}
2438
[email protected]79368982013-11-13 01:11:012439void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162440 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012441 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262442
[email protected]474f8512013-05-31 22:31:162443 // Erase all entries but the last committed entry. There may still be a
2444 // new pending entry after this.
2445 entries_.erase(entries_.begin(),
2446 entries_.begin() + last_committed_entry_index_);
2447 entries_.erase(entries_.begin() + 1, entries_.end());
2448 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262449}
2450
Christian Dullweber1af31e62018-02-22 11:49:482451void NavigationControllerImpl::DeleteNavigationEntries(
2452 const DeletionPredicate& deletionPredicate) {
2453 // It is up to callers to check the invariants before calling this.
2454 CHECK(CanPruneAllButLastCommitted());
2455 std::vector<int> delete_indices;
2456 for (size_t i = 0; i < entries_.size(); i++) {
2457 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572458 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482459 delete_indices.push_back(i);
2460 }
2461 }
2462 if (delete_indices.empty())
2463 return;
2464
2465 if (delete_indices.size() == GetEntryCount() - 1U) {
2466 PruneAllButLastCommitted();
2467 } else {
2468 // Do the deletion in reverse to preserve indices.
Ayu Ishii2f825852022-03-08 19:47:382469 for (const auto& index : base::Reversed(delete_indices)) {
2470 RemoveEntryAtIndex(index);
Christian Dullweber1af31e62018-02-22 11:49:482471 }
Hayato Ito2c8c08d02021-06-23 03:38:432472 BroadcastHistoryOffsetAndLength();
Christian Dullweber1af31e62018-02-22 11:49:482473 }
2474 delegate()->NotifyNavigationEntriesDeleted();
2475}
2476
Shivani Sharma883f5f32019-02-12 18:20:012477bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2478 auto* entry = GetEntryAtIndex(index);
2479 return entry && entry->should_skip_on_back_forward_ui();
2480}
2481
Carlos Caballero35ce710c2019-09-19 10:59:452482BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2483 return back_forward_cache_;
2484}
2485
clamy987a3752018-05-03 17:36:262486void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2487 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2488 // progress, since this will cause a use-after-free. (We only allow this
2489 // when the tab is being destroyed for shutdown, since it won't return to
2490 // NavigateToEntry in that case.) http://crbug.com/347742.
Takashi Toyoshimaea534ef22021-07-21 03:27:592491 CHECK(!in_navigate_to_pending_entry_ || frame_tree_.IsBeingDestroyed());
clamy987a3752018-05-03 17:36:262492
2493 if (was_failure && pending_entry_) {
2494 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2495 } else {
2496 failed_pending_entry_id_ = 0;
2497 }
2498
2499 if (pending_entry_) {
2500 if (pending_entry_index_ == -1)
Paul Semel7e51469e2022-07-12 12:16:332501 pending_entry_.ClearAndDelete();
clamy987a3752018-05-03 17:36:262502 pending_entry_index_ = -1;
2503 pending_entry_ = nullptr;
2504 }
arthursonzogni66f711c2019-10-08 14:40:362505
2506 // Ensure any refs to the current pending entry are ignored if they get
2507 // deleted, by clearing the set of known refs. All future pending entries will
2508 // only be affected by new refs.
2509 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262510}
2511
2512void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2513 if (pending_entry_)
2514 pending_entry_->set_ssl_error(error);
2515}
2516
Xiaohan Wang7f8052e02022-01-14 18:44:282517#if BUILDFLAG(IS_ANDROID)
Camille Lamy5193caa2018-10-12 11:59:422518// static
2519bool NavigationControllerImpl::ValidateDataURLAsString(
2520 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2521 if (!data_url_as_string)
2522 return false;
2523
2524 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2525 return false;
2526
2527 // The number of characters that is enough for validating a data: URI.
2528 // From the GURL's POV, the only important part here is scheme, it doesn't
2529 // check the actual content. Thus we can take only the prefix of the url, to
2530 // avoid unneeded copying of a potentially long string.
2531 const size_t kDataUriPrefixMaxLen = 64;
2532 GURL data_url(
2533 std::string(data_url_as_string->front_as<char>(),
2534 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2535 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2536 return false;
2537
2538 return true;
2539}
2540#endif
2541
Shivani Sharma194877032019-03-07 17:52:472542void NavigationControllerImpl::NotifyUserActivation() {
2543 // When a user activation occurs, ensure that all adjacent entries for the
2544 // same document clear their skippable bit, so that the history manipulation
2545 // intervention does not apply to them.
2546 auto* last_committed_entry = GetLastCommittedEntry();
2547 if (!last_committed_entry)
2548 return;
Shivani Sharma194877032019-03-07 17:52:472549
shivanigithub99368382021-06-16 18:33:372550 if (base::FeatureList::IsEnabled(
2551 features::kDebugHistoryInterventionNoUserActivation)) {
2552 return;
2553 }
2554
Shivani Sharmac4cc8922019-04-18 03:11:172555 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472556}
2557
clamy987a3752018-05-03 17:36:262558bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2559 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172560 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262561 NavigationEntryImpl* entry =
2562 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2563 if (!entry)
2564 return false;
2565
2566 FrameNavigationEntry* frame_entry =
2567 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2568 if (!frame_entry)
2569 return false;
2570
Nate Chapin45f620582021-09-30 17:45:432571 // |is_browser_initiated| is false here because a navigation in a new subframe
2572 // always begins with renderer action (i.e., an HTML element being inserted
2573 // into the DOM), so it is always renderer-initiated.
Camille Lamy5193caa2018-10-12 11:59:422574 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572575 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232576 ReloadType::NONE, false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432577 true /* is_history_navigation_in_new_child */,
2578 false /* is_browser_initiated */);
clamyea99ea12018-05-28 13:54:232579
2580 if (!request)
2581 return false;
2582
arthursonzognif046d4a2019-12-12 19:08:102583 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412584
Lukasz Anforowicz9ee83c272020-12-01 20:14:052585 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2586 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232587
2588 return true;
clamy987a3752018-05-03 17:36:262589}
2590
Tsuyoshi Horo52fd08e2020-07-07 07:03:452591bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2592 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2593 if (!entry)
2594 return false;
Rakina Zata Amnif297a802022-01-18 03:53:432595
2596 if (entry->IsInitialEntryNotForSynchronousAboutBlank()) {
2597 // We should never navigate to an existing initial NavigationEntry that is
2598 // the initial NavigationEntry for the initial empty document that hasn't
2599 // been overridden by the synchronous about:blank commit, to preserve
2600 // legacy behavior where trying to reload when the main frame is on the
2601 // initial empty document won't result in a navigation. See also
2602 // https://crbug.com/1277414.
2603 return false;
2604 }
Tsuyoshi Horo52fd08e2020-07-07 07:03:452605 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2606 if (!frame_entry)
2607 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142608 ReloadType reload_type = ReloadType::NORMAL;
2609 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452610 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142611 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452612 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432613 false /* is_history_navigation_in_new_child */,
2614 true /* is_browser_initiated */);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452615 if (!request)
2616 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052617 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452618 return true;
2619}
2620
Dave Tapuska8bfd84c2019-03-26 20:47:162621void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2622 int offset,
2623 int sandbox_frame_tree_node_id) {
2624 if (!CanGoToOffset(offset))
2625 return;
Nate Chapin45f620582021-09-30 17:45:432626 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id,
2627 false /* is_browser_initiated */);
Dave Tapuska8bfd84c2019-03-26 20:47:162628}
2629
clamy987a3752018-05-03 17:36:262630void NavigationControllerImpl::NavigateFromFrameProxy(
2631 RenderFrameHostImpl* render_frame_host,
2632 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022633 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452634 int initiator_process_id,
Anton Bikineevf62d1bf2021-05-15 17:56:072635 const absl::optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262636 bool is_renderer_initiated,
2637 SiteInstance* source_site_instance,
2638 const Referrer& referrer,
2639 ui::PageTransition page_transition,
2640 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252641 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262642 const std::string& method,
2643 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092644 const std::string& extra_headers,
Antonio Sartori2f763d9d2021-04-21 10:04:142645 network::mojom::SourceLocationPtr source_location,
John Delaney50425f82020-04-07 16:26:212646 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
jongdeok.kim5de823b32022-06-14 04:37:502647 bool is_form_submission,
Tsuyoshi Horo167ca6432022-03-09 05:16:392648 const absl::optional<blink::Impression>& impression,
Nan Lin944e9b4e2022-04-12 13:51:222649 base::TimeTicks navigation_start_time,
Garrett Tanzer405f3402022-07-21 20:12:492650 bool is_embedder_initiated_fenced_frame_navigation,
2651 bool is_unfenced_top_navigation) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582652 if (is_renderer_initiated)
2653 DCHECK(initiator_origin.has_value());
2654
clamy987a3752018-05-03 17:36:262655 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582656
Rakina Zata Amni2322f4f82022-01-24 13:24:242657 // Don't allow an entry replacement if there is no entry to replace.
2658 // http://crbug.com/457149
2659 if (GetEntryCount() == 0)
2660 should_replace_current_entry = false;
2661
clamy987a3752018-05-03 17:36:262662 // Create a NavigationEntry for the transfer, without making it the pending
2663 // entry. Subframe transfers should have a clone of the last committed entry
2664 // with a FrameNavigationEntry for the target frame. Main frame transfers
2665 // should have a new NavigationEntry.
2666 // TODO(creis): Make this unnecessary by creating (and validating) the params
2667 // directly, passing them to the destination RenderFrameHost. See
2668 // https://crbug.com/536906.
2669 std::unique_ptr<NavigationEntryImpl> entry;
Harkiran Bolariae1b5158b2021-09-16 19:03:262670 if (!render_frame_host->is_main_frame()) {
clamy987a3752018-05-03 17:36:262671 // Subframe case: create FrameNavigationEntry.
Rakina Zata Amni2322f4f82022-01-24 13:24:242672 if (GetLastCommittedEntry()) {
2673 entry = GetLastCommittedEntry()->Clone();
2674 entry->set_extra_headers(extra_headers);
2675 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2676 // Renderer-initiated navigation that target a remote frame are currently
2677 // classified as browser-initiated when this one has already navigated.
2678 // See https://crbug.com/722251.
2679 } else {
2680 // If there's no last committed entry, create an entry for about:blank
2681 // with a subframe entry for our destination.
2682 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2683 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
2684 GURL(url::kAboutBlankURL), referrer, initiator_origin,
2685 source_site_instance, page_transition, is_renderer_initiated,
2686 extra_headers, browser_context_,
Julie Jeongeun Kim5b9aff72022-05-02 02:10:172687 nullptr /* blob_url_loader_factory */,
2688 false /* rewrite_virtual_urls */));
Rakina Zata Amni2322f4f82022-01-24 13:24:242689 }
Rakina Zata Amniafd3c6582021-11-30 06:19:172690 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2691 // Renderer-initiated navigation that target a remote frame are currently
2692 // classified as browser-initiated when this one has already navigated.
2693 // See https://crbug.com/722251.
Nate Chapin9f169072021-06-09 19:32:372694 // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter
2695 // to CreateNavigationRequestFromLoadParams(), so while kReplace might
2696 // remove child FrameNavigationEntries (e.g., if this is a cross-process
2697 // same-document navigation), they will still be present in the
2698 // committed NavigationEntry that will be referenced to construct the new
2699 // FrameNavigationEntry tree when this navigation commits.
clamy987a3752018-05-03 17:36:262700 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:082701 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582702 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072703 absl::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202704 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392705 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482706 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori79d549d2021-02-18 12:59:542707 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262708 } else {
2709 // Main frame case.
Julie Jeongeun Kim5b9aff72022-05-02 02:10:172710 // If `node` is the outermost main frame, it rewrites a virtual url in order
2711 // to adjust the original input url if needed. For inner frames such as
2712 // fenced frames or subframes, they don't rewrite urls as the urls are not
2713 // input urls by users.
2714 bool rewrite_virtual_urls = node->IsOutermostMainFrame();
clamy987a3752018-05-03 17:36:262715 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102716 url, referrer, initiator_origin, source_site_instance, page_transition,
2717 is_renderer_initiated, extra_headers, browser_context_,
Julie Jeongeun Kim5b9aff72022-05-02 02:10:172718 blob_url_loader_factory, rewrite_virtual_urls));
clamy987a3752018-05-03 17:36:262719 entry->root_node()->frame_entry->set_source_site_instance(
2720 static_cast<SiteInstanceImpl*>(source_site_instance));
2721 entry->root_node()->frame_entry->set_method(method);
2722 }
clamy987a3752018-05-03 17:36:262723
Camille Lamy5193caa2018-10-12 11:59:422724 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262725 if (GetLastCommittedEntry() &&
2726 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2727 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422728 override_user_agent = true;
clamy987a3752018-05-03 17:36:262729 }
2730 // TODO(creis): Set user gesture and intent received timestamp on Android.
2731
2732 // We may not have successfully added the FrameNavigationEntry to |entry|
2733 // above (per https://crbug.com/608402), in which case we create it from
2734 // scratch. This works because we do not depend on |frame_entry| being inside
2735 // |entry| during NavigateToEntry. This will go away when we shortcut this
2736 // further in https://crbug.com/536906.
2737 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2738 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452739 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Nate Chapinfbfe5af2021-06-10 17:22:082740 node->unique_name(), -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582741 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072742 absl::nullopt /* origin */, referrer, initiator_origin,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:122743 std::vector<GURL>(), blink::PageState(), method, -1,
2744 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482745 nullptr /* subresource_web_bundle_navigation_info */,
Nate Chapin63db0d12022-01-20 22:03:302746 nullptr /* policy_container_policies */,
Domenic Denicolacc094fb2022-03-16 23:40:572747 false /* protect_url_in_navigation_api */);
clamy987a3752018-05-03 17:36:262748 }
2749
Camille Lamy5193caa2018-10-12 11:59:422750 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022751 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452752 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292753 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422754 params.source_site_instance = source_site_instance;
2755 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2756 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032757 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422758 params.referrer = referrer;
2759 /* params.redirect_chain: skip */
2760 params.extra_headers = extra_headers;
2761 params.is_renderer_initiated = is_renderer_initiated;
2762 params.override_user_agent = UA_OVERRIDE_INHERIT;
2763 /* params.base_url_for_data_url: skip */
2764 /* params.virtual_url_for_data_url: skip */
2765 /* params.data_url_as_string: skip */
2766 params.post_data = post_body;
2767 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582768 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422769 /* params.frame_name: skip */
2770 // TODO(clamy): See if user gesture should be propagated to this function.
2771 params.has_user_gesture = false;
2772 params.should_clear_history_list = false;
2773 params.started_from_context_menu = false;
2774 /* params.navigation_ui_data: skip */
2775 /* params.input_start: skip */
Minggang Wangf59db47b2021-06-16 01:56:222776 params.was_activated = blink::mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542777 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212778 params.impression = impression;
Antonio Sartori6984c742021-08-26 08:03:412779 params.download_policy = std::move(download_policy);
jongdeok.kim5de823b32022-06-14 04:37:502780 params.is_form_submission = is_form_submission;
Camille Lamy5193caa2018-10-12 11:59:422781
2782 std::unique_ptr<NavigationRequest> request =
2783 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032784 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:142785 false /* has_user_gesture */, std::move(source_location),
Tsuyoshi Horo167ca6432022-03-09 05:16:392786 ReloadType::NONE, entry.get(), frame_entry.get(),
Garrett Tanzer405f3402022-07-21 20:12:492787 navigation_start_time, is_embedder_initiated_fenced_frame_navigation,
2788 is_unfenced_top_navigation);
clamyea99ea12018-05-28 13:54:232789
2790 if (!request)
2791 return;
2792
Arthur Hemery948742762019-09-18 10:06:242793 // At this stage we are proceeding with this navigation. If this was renderer
2794 // initiated with user gesture, we need to make sure we clear up potential
2795 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2796 DiscardNonCommittedEntries();
2797
Lukasz Anforowicz9ee83c272020-12-01 20:14:052798 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262799}
2800
[email protected]d1198fd2012-08-13 22:50:192801void NavigationControllerImpl::SetSessionStorageNamespace(
Alex Moshchuk8015afcf2022-01-31 22:59:252802 const StoragePartitionConfig& partition_config,
[email protected]8ff00d72012-10-23 19:12:212803 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192804 if (!session_storage_namespace)
2805 return;
2806
2807 // We can't overwrite an existing SessionStorage without violating spec.
2808 // Attempts to do so may give a tab access to another tab's session storage
2809 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152810 bool successful_insert =
2811 session_storage_namespace_map_
Alex Moshchuk8015afcf2022-01-31 22:59:252812 .insert(std::make_pair(partition_config,
Aaron Colwellf3b316e2021-03-11 20:17:052813 static_cast<SessionStorageNamespaceImpl*>(
2814 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302815 .second;
2816 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
[email protected]d1198fd2012-08-13 22:50:192817}
2818
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572819bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Rakina Zata Amni2322f4f82022-01-24 13:24:242820 return IsInitialNavigation() &&
2821 (!GetLastCommittedEntry() ||
2822 GetLastCommittedEntry()->IsInitialEntry()) &&
Carlos Caballeroede6f8c2021-01-28 11:01:502823 !frame_tree_.has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462824}
2825
Aran Gilman37d11632019-10-08 23:07:152826SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Alex Moshchuk8015afcf2022-01-31 22:59:252827 const StoragePartitionConfig& partition_config) {
[email protected]fdac6ade2013-07-20 01:06:302828 StoragePartition* partition =
Lukasz Anforowiczb9a969a2021-04-29 15:26:252829 browser_context_->GetStoragePartition(partition_config);
michaelnbacbcbd2016-02-09 00:32:032830 DOMStorageContextWrapper* context_wrapper =
2831 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2832
2833 SessionStorageNamespaceMap::const_iterator it =
Alex Moshchuk8015afcf2022-01-31 22:59:252834 session_storage_namespace_map_.find(partition_config);
michaelnbacbcbd2016-02-09 00:32:032835 if (it != session_storage_namespace_map_.end()) {
2836 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152837 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2838 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:472839
michaelnbacbcbd2016-02-09 00:32:032840 return it->second.get();
2841 }
2842
2843 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102844 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2845 SessionStorageNamespaceImpl::Create(context_wrapper);
2846 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2847 session_storage_namespace.get();
Alex Moshchuk8015afcf2022-01-31 22:59:252848 session_storage_namespace_map_[partition_config] =
Daniel Murphy31bbb8b12018-02-07 21:44:102849 std::move(session_storage_namespace);
[email protected]fdac6ade2013-07-20 01:06:302850
Daniel Murphy31bbb8b12018-02-07 21:44:102851 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302852}
2853
2854SessionStorageNamespace*
2855NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Alex Moshchuk8015afcf2022-01-31 22:59:252856 return GetSessionStorageNamespace(
2857 StoragePartitionConfig::CreateDefault(GetBrowserContext()));
[email protected]fdac6ade2013-07-20 01:06:302858}
2859
2860const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572861NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302862 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552863}
[email protected]d202a7c2012-01-04 07:53:472864
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572865bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562866 return needs_reload_;
2867}
[email protected]a26023822011-12-29 00:23:552868
[email protected]46bb5e9c2013-10-03 22:16:472869void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412870 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2871}
2872
2873void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472874 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412875 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542876
2877 if (last_committed_entry_index_ != -1) {
2878 entries_[last_committed_entry_index_]->SetTransitionType(
2879 ui::PAGE_TRANSITION_RELOAD);
2880 }
[email protected]46bb5e9c2013-10-03 22:16:472881}
2882
[email protected]d202a7c2012-01-04 07:53:472883void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572884 DCHECK_LT(index, GetEntryCount());
2885 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312886 DiscardNonCommittedEntries();
2887
2888 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122889 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312890 last_committed_entry_index_--;
2891}
2892
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572893NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272894 // If there is no pending_entry_, there should be no pending_entry_index_.
2895 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2896
2897 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362898 // at that index. An exception is while a reload of a post commit error page
2899 // is ongoing; in that case pending entry will point to the entry replaced
2900 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272901 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362902 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2903 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272904
[email protected]022af742011-12-28 18:37:252905 return pending_entry_;
2906}
2907
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572908int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272909 // The pending entry index must always be less than the number of entries.
Rakina Zata Amni2322f4f82022-01-24 13:24:242910 // If there are no entries, it must be exactly -1.
arthursonzogni5c4c202d2017-04-25 23:41:272911 DCHECK_LT(pending_entry_index_, GetEntryCount());
Rakina Zata Amni2322f4f82022-01-24 13:24:242912 DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:552913 return pending_entry_index_;
2914}
2915
avi25764702015-06-23 15:43:372916void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572917 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362918 bool replace,
Dave Tapuska87696ae2021-11-18 18:48:312919 bool was_post_commit_error,
Rakina Zata Amnia4e27222021-12-22 01:05:002920 bool in_fenced_frame_tree,
2921 LoadCommittedDetails* commit_details) {
Dave Tapuska87696ae2021-11-18 18:48:312922 // Fenced frame trees should always have `ui::PAGE_TRANSITION_AUTO_SUBFRAME`
2923 // set because:
2924 // 1) They don't influence the history of the outer page.
2925 // 2) They are always replace only navigation (there is always only one entry
2926 // in their history stack).
2927 // 3) Are not top level navigations and appear similar to iframes.
2928 // Navigations of the fenced frame might create a new NavigationEntry, which
2929 // will call this function. Non fenced frame navigations will never have
2930 // `ui::PAGE_TRANSITION_AUTO_SUBFRAME` because they won't call
2931 // InsertOrReplaceEntry.
2932 DCHECK_EQ(in_fenced_frame_tree,
2933 ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2934 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202935
avi5cad4912015-06-19 05:25:442936 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2937 // need to keep continuity with the pending entry, so copy the pending entry's
2938 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2939 // then the renderer navigated on its own, independent of the pending entry,
2940 // so don't copy anything.
2941 if (pending_entry_ && pending_entry_index_ == -1)
2942 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202943
Rakina Zata Amnia4e27222021-12-22 01:05:002944 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]765b35502008-08-21 00:51:202945
creisee17e932015-07-17 17:56:222946 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362947 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102948 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572949 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362950 // If the new entry is a post-commit error page, we store the current last
2951 // committed entry to the side so that we can put it back when navigating
2952 // away from the error.
2953 if (was_post_commit_error) {
2954 DCHECK(!entry_replaced_by_post_commit_error_);
2955 entry_replaced_by_post_commit_error_ =
2956 std::move(entries_[last_committed_entry_index_]);
2957 }
dcheng36b6aec92015-12-26 06:16:362958 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222959 return;
2960 }
[email protected]765b35502008-08-21 00:51:202961
creis37979a62015-08-04 19:48:182962 // We shouldn't see replace == true when there's no committed entries.
2963 DCHECK(!replace);
2964
Michael Thiessen9b14d512019-09-23 21:19:472965 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202966
Shivani Sharmad8c8d652019-02-13 17:27:572967 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202968
dcheng36b6aec92015-12-26 06:16:362969 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202970 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292971}
2972
Shivani Sharmad8c8d652019-02-13 17:27:572973void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162974 if (entries_.size() < max_entry_count())
2975 return;
2976
2977 DCHECK_EQ(max_entry_count(), entries_.size());
2978 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572979 CHECK_EQ(pending_entry_index_, -1);
2980
2981 int index = 0;
Elly Fong-Jonesccc6d1f2021-06-14 18:32:422982 // Retrieve the oldest skippable entry.
2983 for (; index < GetEntryCount(); index++) {
2984 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2985 break;
Shivani Sharmad8c8d652019-02-13 17:27:572986 }
2987
2988 // If there is no skippable entry or if it is the last committed entry then
2989 // fall back to pruning the oldest entry. It is not safe to prune the last
2990 // committed entry.
2991 if (index == GetEntryCount() || index == last_committed_entry_index_)
2992 index = 0;
2993
2994 bool should_succeed = RemoveEntryAtIndex(index);
2995 DCHECK_EQ(true, should_succeed);
2996
2997 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252998}
2999
clamy3cb9bea92018-07-10 12:42:023000void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:163001 ReloadType reload_type,
Nate Chapin45f620582021-09-30 17:45:433002 int sandboxed_source_frame_tree_node_id,
3003 bool is_browser_initiated) {
Alexander Timin3a92df72019-09-20 11:59:503004 TRACE_EVENT0("navigation",
3005 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:273006 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:023007 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:363008 if (pending_entry_index_ != -1) {
3009 // The pending entry may not be in entries_ if a post-commit error page is
3010 // showing.
3011 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
3012 pending_entry_ == entry_replaced_by_post_commit_error_.get());
3013 }
Gyuyoung Kim107c2a02021-04-13 01:49:303014 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:573015 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:013016 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:003017 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:573018 int nav_entry_id = pending_entry_->GetUniqueID();
3019
[email protected]83c2e232011-10-07 21:36:463020 // If we were navigating to a slow-to-commit page, and the user performs
3021 // a session history navigation to the last committed page, RenderViewHost
3022 // will force the throbber to start, but WebKit will essentially ignore the
3023 // navigation, and won't send a message to stop the throbber. To prevent this
3024 // from happening, we drop the navigation here and stop the slow-to-commit
3025 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:023026 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:083027 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:273028 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
Takashi Toyoshimabf549472021-07-01 07:47:203029 frame_tree_.StopLoading();
[email protected]6a13a6c2011-12-20 21:47:123030
[email protected]83c2e232011-10-07 21:36:463031 DiscardNonCommittedEntries();
3032 return;
3033 }
3034
creisce0ef3572017-01-26 17:53:083035 // Compare FrameNavigationEntries to see which frames in the tree need to be
3036 // navigated.
clamy3cb9bea92018-07-10 12:42:023037 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
3038 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Nate Chapin45f620582021-09-30 17:45:433039 FindFramesToNavigate(root, reload_type, is_browser_initiated,
3040 &same_document_loads, &different_document_loads);
creis4e2ecb72015-06-20 00:46:303041
3042 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573043 // We were unable to match any frames to navigate. This can happen if a
3044 // history navigation targets a subframe that no longer exists
3045 // (https://crbug.com/705550). In this case, we need to update the current
3046 // history entry to the pending one but keep the main document loaded. We
3047 // also need to ensure that observers are informed about the updated
3048 // current history entry (e.g., for greying out back/forward buttons), and
3049 // that renderer processes update their history offsets. The easiest way
3050 // to do all that is to schedule a "redundant" same-document navigation in
3051 // the main frame.
3052 //
3053 // Note that we don't want to remove this history entry, as it might still
3054 // be valid later, since a frame that it's targeting may be recreated.
3055 //
3056 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
3057 // need to repeat history navigations until finding the one that works.
3058 // Consider changing this behavior to keep looking for the first valid
3059 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:023060 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:423061 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573062 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:573063 ReloadType::NONE /* reload_type */,
3064 true /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433065 false /* is_history_navigation_in_new_child */,
3066 is_browser_initiated);
clamy3cb9bea92018-07-10 12:42:023067 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573068 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:023069 DiscardPendingEntry(false);
3070 return;
3071 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573072 same_document_loads.push_back(std::move(navigation_request));
3073
3074 // Sanity check that we never take this branch for any kinds of reloads,
3075 // as those should've queued a different-document load in the main frame.
3076 DCHECK(!is_forced_reload);
3077 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:303078 }
3079
Dave Tapuska8bfd84c2019-03-26 20:47:163080 // If |sandboxed_source_frame_node_id| is valid, then track whether this
3081 // navigation affects any frame outside the frame's subtree.
3082 if (sandboxed_source_frame_tree_node_id !=
3083 FrameTreeNode::kFrameTreeNodeInvalidId) {
3084 bool navigates_inside_tree =
3085 DoesSandboxNavigationStayWithinSubtree(
3086 sandboxed_source_frame_tree_node_id, same_document_loads) &&
3087 DoesSandboxNavigationStayWithinSubtree(
3088 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:503089 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:163090 // the number of pages that trigger this.
3091 FrameTreeNode* sandbox_source_frame_tree_node =
3092 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
3093 if (sandbox_source_frame_tree_node) {
3094 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
3095 sandbox_source_frame_tree_node->current_frame_host(),
3096 navigates_inside_tree
3097 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
3098 : blink::mojom::WebFeature::
3099 kSandboxBackForwardAffectsFramesOutsideSubtree);
3100 }
Dave Tapuska855c1e12019-08-23 20:45:523101
3102 // If the navigation occurred outside the tree discard it because
3103 // the sandboxed frame didn't have permission to navigate outside
3104 // its tree. If it is possible that the navigation is both inside and
3105 // outside the frame tree and we discard it entirely because we don't
3106 // want to end up in a history state that didn't exist before.
Dominic Farolino057440042022-01-19 18:18:143107 if (!navigates_inside_tree) {
Dave Tapuska855c1e12019-08-23 20:45:523108 DiscardPendingEntry(false);
3109 return;
3110 }
Dave Tapuska8bfd84c2019-03-26 20:47:163111 }
3112
Carlos Caballero539a421c2020-07-06 10:25:573113 // BackForwardCache:
3114 // Navigate immediately if the document is in the BackForwardCache.
3115 if (back_forward_cache_.GetEntry(nav_entry_id)) {
3116 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
3117 DCHECK_EQ(reload_type, ReloadType::NONE);
3118 auto navigation_request = CreateNavigationRequestFromEntry(
3119 root, pending_entry_, pending_entry_->GetFrameEntry(root),
3120 ReloadType::NONE, false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433121 false /* is_history_navigation_in_new_child */, is_browser_initiated);
Lukasz Anforowicz9ee83c272020-12-01 20:14:053122 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:573123
3124 return;
3125 }
3126
3127 // History navigation might try to reuse a specific BrowsingInstance, already
3128 // used by a page in the cache. To avoid having two different main frames that
3129 // live in the same BrowsingInstance, evict the all pages with this
3130 // BrowsingInstance from the cache.
3131 //
3132 // For example, take the following scenario:
3133 //
3134 // A1 = Some page on a.com
3135 // A2 = Some other page on a.com
3136 // B3 = An uncacheable page on b.com
3137 //
3138 // Then the following navigations occur:
3139 // A1->A2->B3->A1
3140 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
3141 // take its place) and A1 will be created in the same BrowsingInstance (and
3142 // SiteInstance), as A2.
3143 //
3144 // If we didn't do anything, both A1 and A2 would remain alive in the same
3145 // BrowsingInstance/SiteInstance, which is unsupported by
3146 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
3147 // A2 from the cache.
3148 if (pending_entry_->site_instance()) {
3149 back_forward_cache_.EvictFramesInRelatedSiteInstances(
3150 pending_entry_->site_instance());
3151 }
3152
Rakina Zata Amnid605d462022-06-01 10:17:033153 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "pending_entry_restored",
3154 pending_entry_ && pending_entry_->IsRestored());
3155 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_entry_id",
3156 pending_entry_ ? pending_entry_->GetUniqueID() : -1);
3157 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_entry_index",
3158 pending_entry_index_);
3159 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "last_committed_index",
3160 last_committed_entry_index_);
3161 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "entries_size", entries_.size());
3162 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "pending_entry_initial",
3163 pending_entry_ && pending_entry_->IsInitialEntry());
3164 SCOPED_CRASH_KEY_BOOL(
3165 "nav_reentrancy", "pending_entry_initial2",
3166 pending_entry_ &&
3167 pending_entry_->IsInitialEntryNotForSynchronousAboutBlank());
3168 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_initial_nav",
3169 IsInitialNavigation());
3170 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_initial_blank_nav",
3171 IsInitialBlankNavigation());
3172 SCOPED_CRASH_KEY_BOOL("nav_reentrancy", "is_forced_reload", is_forced_reload);
3173 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "pending_reload_type",
3174 (int)pending_reload_);
3175
clamy3cb9bea92018-07-10 12:42:023176 // This call does not support re-entrancy. See http://crbug.com/347742.
3177 CHECK(!in_navigate_to_pending_entry_);
3178 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:303179
Rakina Zata Amnid605d462022-06-01 10:17:033180 // If the navigation-reentrancy is caused by calling
3181 // NavigateToExistingPendingEntry twice, this will note the previous call's
3182 // pending entry's ID.
3183 SCOPED_CRASH_KEY_NUMBER("nav_reentrancy", "prev_pending_entry_id",
3184 pending_entry_ ? pending_entry_->GetUniqueID() : -1);
3185
arthursonzogni66f711c2019-10-08 14:40:363186 // It is not possible to delete the pending NavigationEntry while navigating
3187 // to it. Grab a reference to delay potential deletion until the end of this
3188 // function.
3189 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3190
creis4e2ecb72015-06-20 00:46:303191 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:023192 for (auto& item : same_document_loads) {
3193 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:053194 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:303195 }
clamy3cb9bea92018-07-10 12:42:023196 for (auto& item : different_document_loads) {
3197 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:053198 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:303199 }
clamy3cb9bea92018-07-10 12:42:023200
3201 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:303202}
3203
Alex Moshchuk3a4e77a2020-05-29 21:32:573204NavigationControllerImpl::HistoryNavigationAction
3205NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:303206 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:573207 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:423208 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:583209 // Only active and prerendered documents are allowed to navigate in their
3210 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:423211 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:013212 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:583213 // - If the document is in pending deletion, the browser already committed
3214 // to destroying this RenderFrameHost. See https://crbug.com/930278.
3215 // - If the document is in back-forward cache, it's not allowed to navigate
3216 // as it should remain frozen. Ignore the request and evict the document
3217 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:423218 //
Sreeja Kamishettydb8e2892021-03-10 09:30:583219 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:423220 // which should all be inactive as well.
Fergal Daly1336ac642021-09-14 15:13:113221 if (frame->current_frame_host()->IsInactiveAndDisallowActivation(
3222 DisallowActivationReasonId::kDetermineActionForHistoryNavigation)) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:423223 return HistoryNavigationAction::kStopLooking;
Fergal Daly1336ac642021-09-14 15:13:113224 }
Sreeja Kamishetty8eacabb2021-03-09 11:45:423225 }
arthursonzogni03f76152019-02-12 10:35:203226
Alex Moshchuk3a4e77a2020-05-29 21:32:573227 // If there's no last committed entry, there is no previous history entry to
3228 // compare against, so fall back to a different-document load. Note that we
3229 // should only reach this case for the root frame and not descend further
3230 // into subframes.
3231 if (!GetLastCommittedEntry()) {
3232 DCHECK(frame->IsMainFrame());
3233 return HistoryNavigationAction::kDifferentDocument;
3234 }
3235
3236 // Reloads should result in a different-document load. Note that reloads may
3237 // also happen via the |needs_reload_| mechanism where the reload_type is
3238 // NONE, so detect this by comparing whether we're going to the same
3239 // entry that we're currently on. Similarly to above, only main frames
3240 // should reach this. Note that subframes support reloads, but that's done
3241 // via a different path that doesn't involve FindFramesToNavigate (see
3242 // RenderFrameHost::Reload()).
3243 if (reload_type != ReloadType::NONE ||
3244 pending_entry_index_ == last_committed_entry_index_) {
3245 DCHECK(frame->IsMainFrame());
3246 return HistoryNavigationAction::kDifferentDocument;
3247 }
3248
Alex Moshchuk47d1a4bd2020-06-01 22:15:343249 // If there is no new FrameNavigationEntry for the frame, ignore the
3250 // load. For example, this may happen when going back to an entry before a
3251 // frame was created. Suppose we commit a same-document navigation that also
3252 // results in adding a new subframe somewhere in the tree. If we go back,
3253 // the new subframe will be missing a FrameNavigationEntry in the previous
3254 // NavigationEntry, but we shouldn't delete or change what's loaded in
3255 // it.
3256 //
Alex Moshchuke65c39272020-06-03 17:55:373257 // Note that in this case, there is no need to keep looking for navigations
3258 // in subframes, which would be missing FrameNavigationEntries as well.
3259 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:343260 // It's important to check this before checking |old_item| below, since both
3261 // might be null, and in that case we still shouldn't change what's loaded in
3262 // this frame. Note that scheduling any loads assumes that |new_item| is
3263 // non-null. See https://crbug.com/1088354.
3264 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3265 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:373266 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:343267
Charlie Reisa474fb62022-03-17 02:31:363268 // Use the RenderFrameHost's last committed FrameNavigationEntry to identify
3269 // which history item it is currently on, since this may be different than the
3270 // FrameNavigationEntry for the frame in the last committed NavigationEntry
3271 // (e.g., if a history navigation is targeting multiple frames and only some
3272 // have committed so far).
creis4e2ecb72015-06-20 00:46:303273 FrameNavigationEntry* old_item =
Charlie Reisa474fb62022-03-17 02:31:363274 frame->current_frame_host()->last_committed_frame_entry();
3275 if (!old_item) {
3276 // In cases where the RenderFrameHost does not have a FrameNavigationEntry,
3277 // fall back to the last committed NavigationEntry's record for this frame.
3278 // This may happen in cases like the initial state of the RenderFrameHost.
3279 // TODO(https://crbug.com/1304466): Ensure the RenderFrameHost always has an
3280 // accurate FrameNavigationEntry and eliminate this case.
3281 old_item = GetLastCommittedEntry()->GetFrameEntry(frame);
3282 }
3283 // If neither approach finds a FrameNavigationEntry, schedule a
3284 // different-document load.
3285 // TODO(https://crbug.com/608402): Remove this case.
Alex Moshchuk3a4e77a2020-05-29 21:32:573286 if (!old_item)
3287 return HistoryNavigationAction::kDifferentDocument;
3288
Alex Moshchuk3a4e77a2020-05-29 21:32:573289 // If the new item is not in the same SiteInstance, schedule a
3290 // different-document load. Newly restored items may not have a SiteInstance
3291 // yet, in which case it will be assigned on first commit.
3292 if (new_item->site_instance() &&
3293 new_item->site_instance() != old_item->site_instance())
3294 return HistoryNavigationAction::kDifferentDocument;
3295
3296 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:313297 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
3298 // that we do this even if the history navigation would not be modifying this
3299 // frame were it live.
3300 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:573301 return HistoryNavigationAction::kDifferentDocument;
3302
3303 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:313304 // Starting a navigation after a crash early-promotes the speculative
3305 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
3306 // committed yet, so we can not possibly perform a same-document history
3307 // navigation. The frame would need to be reloaded with a cross-document
3308 // navigation.
3309 if (!frame->current_frame_host()->has_committed_any_navigation())
3310 return HistoryNavigationAction::kDifferentDocument;
3311
creis54131692016-08-12 18:32:253312 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:493313 // sequence number but different item sequence number.
3314 if (new_item->document_sequence_number() ==
3315 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573316 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493317 }
avib48cb312016-05-05 21:35:003318
Alex Moshchuk3a4e77a2020-05-29 21:32:573319 // Otherwise, if both item and document sequence numbers differ, this
3320 // should be a different document load.
3321 return HistoryNavigationAction::kDifferentDocument;
3322 }
3323
3324 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373325 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573326 DCHECK_EQ(new_item->document_sequence_number(),
3327 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373328 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573329}
3330
3331void NavigationControllerImpl::FindFramesToNavigate(
3332 FrameTreeNode* frame,
3333 ReloadType reload_type,
Nate Chapin45f620582021-09-30 17:45:433334 bool is_browser_initiated,
Alex Moshchuk3a4e77a2020-05-29 21:32:573335 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3336 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3337 DCHECK(pending_entry_);
3338 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3339
3340 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3341
3342 if (action == HistoryNavigationAction::kSameDocument) {
3343 std::unique_ptr<NavigationRequest> navigation_request =
3344 CreateNavigationRequestFromEntry(
3345 frame, pending_entry_, new_item, reload_type,
3346 true /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433347 false /* is_history_navigation_in_new_child */,
3348 is_browser_initiated);
Alex Moshchuk3a4e77a2020-05-29 21:32:573349 if (navigation_request) {
3350 // Only add the request if was properly created. It's possible for the
3351 // creation to fail in certain cases, e.g. when the URL is invalid.
3352 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303353 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573354 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473355 std::unique_ptr<NavigationRequest> navigation_request =
3356 CreateNavigationRequestFromEntry(
3357 frame, pending_entry_, new_item, reload_type,
3358 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433359 false /* is_history_navigation_in_new_child */,
3360 is_browser_initiated);
Lei Zhang96031532019-10-10 19:05:473361 if (navigation_request) {
3362 // Only add the request if was properly created. It's possible for the
3363 // creation to fail in certain cases, e.g. when the URL is invalid.
3364 different_document_loads->push_back(std::move(navigation_request));
3365 }
3366 // For a different document, the subframes will be destroyed, so there's
3367 // no need to consider them.
3368 return;
Alex Moshchuke65c39272020-06-03 17:55:373369 } else if (action == HistoryNavigationAction::kStopLooking) {
3370 return;
creis4e2ecb72015-06-20 00:46:303371 }
3372
3373 for (size_t i = 0; i < frame->child_count(); i++) {
Nate Chapin45f620582021-09-30 17:45:433374 FindFramesToNavigate(frame->child_at(i), reload_type, is_browser_initiated,
3375 same_document_loads, different_document_loads);
creis4e2ecb72015-06-20 00:46:303376 }
3377}
3378
Harkiran Bolariaba823e42021-05-21 18:30:363379base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry(
clamy21718cc22018-06-13 13:34:243380 const LoadURLParams& params) {
3381 // Find the appropriate FrameTreeNode.
3382 FrameTreeNode* node = nullptr;
3383 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3384 !params.frame_name.empty()) {
3385 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003386 ? frame_tree_.FindByID(params.frame_tree_node_id)
3387 : frame_tree_.FindByName(params.frame_name);
Hayato Ito7a80db42021-07-05 06:18:543388 DCHECK(!node || node->frame_tree() == &frame_tree_);
clamy21718cc22018-06-13 13:34:243389 }
3390
3391 // If no FrameTreeNode was specified, navigate the main frame.
3392 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003393 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243394
Camille Lamy5193caa2018-10-12 11:59:423395 // Compute overrides to the LoadURLParams for |override_user_agent|,
3396 // |should_replace_current_entry| and |has_user_gesture| that will be used
3397 // both in the creation of the NavigationEntry and the NavigationRequest.
3398 // Ideally, the LoadURLParams themselves would be updated, but since they are
3399 // passed as a const reference, this is not possible.
3400 // TODO(clamy): When we only create a NavigationRequest, move this to
3401 // CreateNavigationRequestFromLoadURLParams.
3402 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3403 GetLastCommittedEntry());
3404
3405 // Don't allow an entry replacement if there is no entry to replace.
3406 // http://crbug.com/457149
Hayato Ito7a80db42021-07-05 06:18:543407 //
3408 // If there is an entry, an entry replacement must happen if the current
3409 // browsing context should maintain a trivial session history.
shivanigithubf405bf0d2021-11-05 17:58:333410 bool should_replace_current_entry =
3411 (params.should_replace_current_entry ||
3412 ShouldMaintainTrivialSessionHistory(node)) &&
3413 entries_.size();
Camille Lamy5193caa2018-10-12 11:59:423414
clamy21718cc22018-06-13 13:34:243415 // Javascript URLs should not create NavigationEntries. All other navigations
3416 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243417 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373418 std::unique_ptr<NavigationEntryImpl> entry =
3419 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3420 should_replace_current_entry,
3421 params.has_user_gesture);
clamy21718cc22018-06-13 13:34:243422 DiscardPendingEntry(false);
3423 SetPendingEntry(std::move(entry));
3424 }
3425
3426 // Renderer-debug URLs are sent to the renderer process immediately for
3427 // processing and don't need to create a NavigationRequest.
3428 // Note: this includes navigations to JavaScript URLs, which are considered
3429 // renderer-debug URLs.
3430 // Note: we intentionally leave the pending entry in place for renderer debug
3431 // URLs, unlike the cases below where we clear it if the navigation doesn't
3432 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303433 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483434 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3435 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153436 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483437 params.url, browser_context_)) {
3438 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363439 return nullptr;
Oleg Davydov2cc0167b2019-02-05 14:32:483440 }
3441
clamy21718cc22018-06-13 13:34:243442 HandleRendererDebugURL(node, params.url);
Harkiran Bolariaba823e42021-05-21 18:30:363443 return nullptr;
clamy21718cc22018-06-13 13:34:243444 }
3445
Antonio Sartori78a749f2020-11-30 12:03:393446 DCHECK(pending_entry_);
3447
clamy21718cc22018-06-13 13:34:243448 // Convert navigations to the current URL to a reload.
3449 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3450 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3451 // DevTools sometimes issues navigations to main frames that they do not
3452 // expect to see treated as reload, and it only works because they pass a
3453 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3454 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543455 ReloadType reload_type = params.reload_type;
3456 if (reload_type == ReloadType::NONE &&
3457 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043458 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243459 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243460 params.load_type ==
3461 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
Hayato Ito7a80db42021-07-05 06:18:543462 should_replace_current_entry, GetLastCommittedEntry())) {
clamy21718cc22018-06-13 13:34:243463 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473464 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393465
3466 // If this is a reload of an existing FrameNavigationEntry and we had a
3467 // policy container for it, then we should copy it into the pending entry,
3468 // so that it is copied to the navigation request in
3469 // CreateNavigationRequestFromLoadParams later.
3470 if (GetLastCommittedEntry()) {
3471 FrameNavigationEntry* previous_frame_entry =
3472 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193473 if (previous_frame_entry &&
3474 previous_frame_entry->policy_container_policies()) {
3475 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Titouan Rigoudy72f892d2022-05-02 18:21:233476 previous_frame_entry->policy_container_policies()->ClonePtr());
Antonio Sartori78a749f2020-11-30 12:03:393477 }
3478 }
3479 }
3480
3481 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
Rakina Zata Amniafd3c6582021-11-30 06:19:173482 // document, then we should copy the document polices from RenderFrameHost's
3483 // PolicyContainerHost. The NavigationRequest will create a new
3484 // PolicyContainerHost with the document policies from the |pending_entry_|,
3485 // and that PolicyContainerHost will be put in the final RenderFrameHost for
3486 // the navigation. This way, we ensure that we keep enforcing the right
3487 // policies on the initial empty document after the reload.
Rakina Zata Amni2322f4f82022-01-24 13:24:243488 if ((!GetLastCommittedEntry() || GetLastCommittedEntry()->IsInitialEntry()) &&
3489 params.url.IsAboutBlank()) {
Antonio Sartori78a749f2020-11-30 12:03:393490 if (node->current_frame_host() &&
3491 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193492 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163493 node->current_frame_host()
3494 ->policy_container_host()
3495 ->policies()
Titouan Rigoudy72f892d2022-05-02 18:21:233496 .ClonePtr());
Antonio Sartori78a749f2020-11-30 12:03:393497 }
clamy21718cc22018-06-13 13:34:243498 }
3499
3500 // navigation_ui_data should only be present for main frame navigations.
Ian Vollick1c6dd3e2022-04-13 02:06:263501 DCHECK(node->IsOutermostMainFrame() || !params.navigation_ui_data);
clamy21718cc22018-06-13 13:34:243502
Tsuyoshi Horo167ca6432022-03-09 05:16:393503 // This will be used to set the Navigation Timing API navigationStart
3504 // parameter for browser navigations in new tabs (intents, tabs opened through
3505 // "Open link in new tab"). If the navigation must wait on the current
3506 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3507 // will be updated when the BeforeUnload ack is received.
3508 const auto navigation_start_time = base::TimeTicks::Now();
3509
Camille Lamy5193caa2018-10-12 11:59:423510 std::unique_ptr<NavigationRequest> request =
3511 CreateNavigationRequestFromLoadParams(
3512 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143513 params.has_user_gesture, network::mojom::SourceLocation::New(),
Tsuyoshi Horo167ca6432022-03-09 05:16:393514 reload_type, pending_entry_, pending_entry_->GetFrameEntry(node),
3515 navigation_start_time);
clamy21718cc22018-06-13 13:34:243516
3517 // If the navigation couldn't start, return immediately and discard the
3518 // pending NavigationEntry.
3519 if (!request) {
3520 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363521 return nullptr;
clamy21718cc22018-06-13 13:34:243522 }
3523
Camille Lamy5193caa2018-10-12 11:59:423524#if DCHECK_IS_ON()
3525 // Safety check that NavigationRequest and NavigationEntry match.
3526 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3527#endif
3528
clamy21718cc22018-06-13 13:34:243529 // This call does not support re-entrancy. See http://crbug.com/347742.
3530 CHECK(!in_navigate_to_pending_entry_);
3531 in_navigate_to_pending_entry_ = true;
3532
arthursonzogni66f711c2019-10-08 14:40:363533 // It is not possible to delete the pending NavigationEntry while navigating
3534 // to it. Grab a reference to delay potential deletion until the end of this
3535 // function.
3536 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3537
Harkiran Bolariaba823e42021-05-21 18:30:363538 base::WeakPtr<NavigationHandle> created_navigation_handle(
3539 request->GetWeakPtr());
Lukasz Anforowicz9ee83c272020-12-01 20:14:053540 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243541
3542 in_navigate_to_pending_entry_ = false;
Harkiran Bolariaba823e42021-05-21 18:30:363543 return created_navigation_handle;
clamy21718cc22018-06-13 13:34:243544}
3545
clamyea99ea12018-05-28 13:54:233546void NavigationControllerImpl::HandleRendererDebugURL(
3547 FrameTreeNode* frame_tree_node,
3548 const GURL& url) {
3549 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243550 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3551 // the renderer process is not live, unless it is the initial navigation
3552 // of the tab.
clamyea99ea12018-05-28 13:54:233553 if (!IsInitialNavigation()) {
3554 DiscardNonCommittedEntries();
3555 return;
3556 }
Fergal Dalyecd3b0202020-06-25 01:57:373557 // The current frame is always a main frame. If IsInitialNavigation() is
3558 // true then there have been no navigations and any frames of this tab must
3559 // be in the same renderer process. If that has crashed then the only frame
3560 // that can be revived is the main frame.
3561 frame_tree_node->render_manager()
3562 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233563 }
3564 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3565}
3566
clamy21718cc22018-06-13 13:34:243567std::unique_ptr<NavigationEntryImpl>
3568NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3569 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423570 const LoadURLParams& params,
3571 bool override_user_agent,
3572 bool should_replace_current_entry,
3573 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393574 // Browser initiated navigations might not have a blob_url_loader_factory set
3575 // in params even if the navigation is to a blob URL. If that happens, lookup
3576 // the correct url loader factory to use here.
3577 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483578 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473579 // Resolve the blob URL in the storage partition associated with the target
3580 // frame. This is the storage partition the URL will be loaded in, and only
3581 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393582 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473583 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393584 }
3585
clamy21718cc22018-06-13 13:34:243586 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443587 // extra_headers in params are \n separated; navigation entries want \r\n.
3588 std::string extra_headers_crlf;
3589 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243590
3591 // For subframes, create a pending entry with a corresponding frame entry.
3592 if (!node->IsMainFrame()) {
Rakina Zata Amni2322f4f82022-01-24 13:24:243593 if (GetLastCommittedEntry()) {
3594 entry = GetLastCommittedEntry()->Clone();
3595 } else {
3596 // If there's no last committed entry, create an entry for about:blank
3597 // with a subframe entry for our destination.
3598 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
3599 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
3600 GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin,
3601 params.source_site_instance.get(), params.transition_type,
3602 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Julie Jeongeun Kim5b9aff72022-05-02 02:10:173603 blob_url_loader_factory, false /* rewrite_virtual_urls */));
Rakina Zata Amni2322f4f82022-01-24 13:24:243604 }
clamy21718cc22018-06-13 13:34:243605 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:083606 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
clamy21718cc22018-06-13 13:34:243607 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Anton Bikineevf62d1bf2021-05-15 17:56:073608 params.url, absl::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203609 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393610 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:483611 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori78a749f2020-11-30 12:03:393612 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163613 // a conversion of a new navigation into a reload, we will set the right
3614 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193615 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243616 } else {
3617 // Otherwise, create a pending entry for the main frame.
Julie Jeongeun Kim5b9aff72022-05-02 02:10:173618 // If `node` is the outermost main frame, it rewrites a virtual url in order
3619 // to adjust the original input url if needed. For inner frames such as
3620 // fenced frames or subframes, they don't rewrite urls as the urls are not
3621 // input urls by users.
3622 bool rewrite_virtual_urls = node->IsOutermostMainFrame();
clamy21718cc22018-06-13 13:34:243623 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063624 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103625 params.source_site_instance.get(), params.transition_type,
3626 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Julie Jeongeun Kim5b9aff72022-05-02 02:10:173627 blob_url_loader_factory, rewrite_virtual_urls));
clamy21718cc22018-06-13 13:34:243628 entry->set_source_site_instance(
3629 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3630 entry->SetRedirectChain(params.redirect_chain);
3631 }
3632
3633 // Set the FTN ID (only used in non-site-per-process, for tests).
3634 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243635 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423636 entry->SetIsOverridingUserAgent(override_user_agent);
3637 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543638 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243639
clamy21718cc22018-06-13 13:34:243640 switch (params.load_type) {
3641 case LOAD_TYPE_DEFAULT:
3642 break;
3643 case LOAD_TYPE_HTTP_POST:
3644 entry->SetHasPostData(true);
3645 entry->SetPostData(params.post_data);
3646 break;
3647 case LOAD_TYPE_DATA:
3648 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3649 entry->SetVirtualURL(params.virtual_url_for_data_url);
Xiaohan Wang7f8052e02022-01-14 18:44:283650#if BUILDFLAG(IS_ANDROID)
clamy21718cc22018-06-13 13:34:243651 entry->SetDataURLAsString(params.data_url_as_string);
3652#endif
3653 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3654 break;
clamy21718cc22018-06-13 13:34:243655 }
3656
3657 // TODO(clamy): NavigationEntry is meant for information that will be kept
3658 // after the navigation ended and therefore is not appropriate for
3659 // started_from_context_menu. Move started_from_context_menu to
3660 // NavigationUIData.
3661 entry->set_started_from_context_menu(params.started_from_context_menu);
3662
3663 return entry;
3664}
3665
clamyea99ea12018-05-28 13:54:233666std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423667NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3668 FrameTreeNode* node,
3669 const LoadURLParams& params,
3670 bool override_user_agent,
3671 bool should_replace_current_entry,
3672 bool has_user_gesture,
Antonio Sartori2f763d9d2021-04-21 10:04:143673 network::mojom::SourceLocationPtr source_location,
Camille Lamy5193caa2018-10-12 11:59:423674 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573675 NavigationEntryImpl* entry,
Tsuyoshi Horo167ca6432022-03-09 05:16:393676 FrameNavigationEntry* frame_entry,
Nan Lin944e9b4e2022-04-12 13:51:223677 base::TimeTicks navigation_start_time,
Garrett Tanzer405f3402022-07-21 20:12:493678 bool is_embedder_initiated_fenced_frame_navigation,
3679 bool is_unfenced_top_navigation) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573680 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283681 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533682 // All renderer-initiated navigations must have an initiator_origin.
3683 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513684
Camille Lamy5193caa2018-10-12 11:59:423685 GURL url_to_load;
3686 GURL virtual_url;
Anton Bikineevf62d1bf2021-05-15 17:56:073687 absl::optional<url::Origin> origin_to_commit =
3688 frame_entry ? frame_entry->committed_origin() : absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:323689
Camille Lamy2baa8022018-10-19 16:43:173690 // For main frames, rewrite the URL if necessary and compute the virtual URL
3691 // that should be shown in the address bar.
Ian Vollick1c6dd3e2022-04-13 02:06:263692 if (node->IsOutermostMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423693 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173694 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423695 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423696
Camille Lamy2baa8022018-10-19 16:43:173697 // For DATA loads, override the virtual URL.
3698 if (params.load_type == LOAD_TYPE_DATA)
3699 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423700
Camille Lamy2baa8022018-10-19 16:43:173701 if (virtual_url.is_empty())
3702 virtual_url = url_to_load;
3703
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573704 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283705
Aran Gilman249eb122019-12-02 23:32:463706 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3707 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3708 // return different results, leading to a different URL in the
3709 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3710 // pending NavigationEntry, as we'll only make one call to
3711 // RewriteUrlForNavigation.
3712 VLOG_IF(1, (url_to_load != frame_entry->url()))
3713 << "NavigationRequest and FrameEntry have different URLs: "
3714 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283715
Camille Lamy2baa8022018-10-19 16:43:173716 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423717 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173718 // NavigationRequest.
3719 } else {
3720 url_to_load = params.url;
3721 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243722 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173723 }
Camille Lamy5193caa2018-10-12 11:59:423724
Ehsan Karamad44fc72112019-02-26 18:15:473725 if (node->render_manager()->is_attaching_inner_delegate()) {
3726 // Avoid starting any new navigations since this node is now preparing for
3727 // attaching an inner delegate.
3728 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203729 }
Camille Lamy5193caa2018-10-12 11:59:423730
Ian Vollick1c6dd3e2022-04-13 02:06:263731 if (!IsValidURLForNavigation(node->IsOutermostMainFrame(), virtual_url,
3732 url_to_load))
Camille Lamy5193caa2018-10-12 11:59:423733 return nullptr;
3734
Kunihiko Sakamoto346a74e2021-03-10 08:57:483735 if (!DoesURLMatchOriginForNavigation(
3736 url_to_load, origin_to_commit,
Wang Hui14cac7a2022-01-28 06:07:473737 frame_entry->subresource_web_bundle_navigation_info(), entry,
Ian Vollick1c6dd3e2022-04-13 02:06:263738 node->IsOutermostMainFrame())) {
Nasko Oskov03912102019-01-11 00:21:323739 DCHECK(false) << " url:" << url_to_load
3740 << " origin:" << origin_to_commit.value();
3741 return nullptr;
3742 }
3743
danakjd83d706d2020-11-25 22:11:123744 // Look for a pending commit that is to another document in this
3745 // FrameTreeNode. If one exists, then the last committed URL will not be the
3746 // current URL by the time this navigation commits.
3747 bool has_pending_cross_document_commit =
3748 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
Miyoung Shina2dd6a42021-10-07 12:19:213749 bool is_currently_error_page = node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:123750
Minggang Wangb9f3fa92021-07-01 15:30:313751 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjb952ef12021-01-14 19:58:493752 /*old_url=*/node->current_url(),
3753 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3754 has_pending_cross_document_commit, is_currently_error_page,
Garrett Tanzer267c2b82022-07-26 16:53:133755 /*is_same_document_history_load=*/false,
3756 is_embedder_initiated_fenced_frame_navigation,
3757 is_unfenced_top_navigation);
Camille Lamy5193caa2018-10-12 11:59:423758
3759 // Create the NavigationParams based on |params|.
3760
Hiroki Nakagawa4ed61282021-06-18 05:37:233761 bool is_view_source_mode = entry->IsViewSourceMode();
3762 DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme));
Charlie Harrison8c113a32019-01-07 16:08:293763
Antonio Sartori6984c742021-08-26 08:03:413764 blink::NavigationDownloadPolicy download_policy = params.download_policy;
Hiroki Nakagawa4ed61282021-06-18 05:37:233765 // Update |download_policy| if the virtual URL is view-source.
Charlie Harrison8c113a32019-01-07 16:08:293766 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253767 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293768
Minggang Wangb9f3fa92021-07-01 15:30:313769 blink::mojom::CommonNavigationParamsPtr common_params =
3770 blink::mojom::CommonNavigationParams::New(
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513771 url_to_load, params.initiator_origin,
3772 blink::mojom::Referrer::New(params.referrer.url,
3773 params.referrer.policy),
Scott Violetcf6ea7e2021-06-09 21:09:213774 params.transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513775 should_replace_current_entry, params.base_url_for_data_url,
Tsuyoshi Horo167ca6432022-03-09 05:16:393776 navigation_start_time,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513777 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
Antonio Sartori2f763d9d2021-04-21 10:04:143778 params.post_data, std::move(source_location),
arthursonzogniaf7c62c52020-02-12 10:49:413779 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273780 false /* has_text_fragment_token */,
3781 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3782 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513783 false /* is_history_navigation_in_new_child_frame */,
Nan Lind91c8152021-10-21 16:22:373784 params.input_start, network::mojom::RequestDestination::kEmpty);
Camille Lamy5193caa2018-10-12 11:59:423785
Minggang Wangb9f3fa92021-07-01 15:30:313786 blink::mojom::CommitNavigationParamsPtr commit_params =
3787 blink::mojom::CommitNavigationParams::New(
Antonio Sartori3e8de6d2021-07-26 10:28:413788 frame_entry->committed_origin(),
3789 // The correct storage key will be computed before committing the
3790 // navigation.
Pâris Meuleman4d97a702022-07-05 10:42:003791 blink::StorageKey(), override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323792 std::vector<network::mojom::URLResponseHeadPtr>(),
jongdeok.kim5de823b32022-06-14 04:37:503793 std::vector<net::RedirectInfo>(), params.post_content_type,
3794 common_params->url, common_params->method,
3795 params.can_load_local_resources,
Minggang Wangb9f3fa92021-07-01 15:30:313796 frame_entry->page_state().ToEncodedData(), entry->GetUniqueID(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143797 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3798 -1 /* pending_history_list_offset */,
3799 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3800 params.should_clear_history_list ? 0 : GetEntryCount(),
3801 false /* was_discarded */, is_view_source_mode,
Minggang Wangb9f3fa92021-07-01 15:30:313802 params.should_clear_history_list,
3803 blink::mojom::NavigationTiming::New(),
Minggang Wangf59db47b2021-06-16 01:56:223804 blink::mojom::WasActivatedOption::kUnknown,
Lucas Furukawa Gadania9c45682019-07-31 22:05:143805 base::UnguessableToken::Create() /* navigation_token */,
Minggang Wang7ee0c742021-06-16 16:16:513806 std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(),
Xiaohan Wang7f8052e02022-01-14 18:44:283807#if BUILDFLAG(IS_ANDROID)
Lucas Furukawa Gadania9c45682019-07-31 22:05:143808 std::string(), /* data_url_as_string */
3809#endif
arthursonzogni14379782020-05-15 09:09:273810 !params.is_renderer_initiated, /* is_browser_initiated */
Tsuyoshi Horoe86d7702019-11-29 01:52:473811 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533812 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173813 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073814 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163815 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333816 false /* origin_agent_cluster */,
Daniel Vogelheim243df9f2022-02-22 10:32:033817 true /* origin_agent_cluster_left_as_default */,
Maks Orlovichc66745a2020-06-30 17:40:023818 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213819 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Nate Chapind1fe3612021-04-16 20:45:573820 false /* is_cross_browsing_instance */, nullptr /* old_page_info */,
3821 -1 /* http_response_code */,
Domenic Denicolacd30f5f82022-03-16 21:48:013822 blink::mojom::NavigationApiHistoryEntryArrays::New(),
Matt Menke4e209082021-11-09 04:59:313823 std::vector<GURL>() /* early_hints_preloaded_resources */,
Clark DuVall8ee487a22021-11-10 02:25:583824 absl::nullopt /* ad_auction_components */,
shivanigithubc7b97ca2022-04-05 19:41:403825 /*fenced_frame_reporting_metadata=*/nullptr,
Clark DuVall8ee487a22021-11-10 02:25:583826 // This timestamp will be populated when the commit IPC is sent.
Arthur Sonzognie41678a2022-06-16 15:51:193827 base::TimeTicks() /* commit_sent */, std::string() /* srcdoc_value */,
Liam Bradyd2a41e152022-07-19 13:58:483828 false /* should_load_data_url */,
3829 /*ancestor_or_self_has_cspee=*/node->AncestorOrSelfHasCSPEE());
Xiaohan Wang7f8052e02022-01-14 18:44:283830#if BUILDFLAG(IS_ANDROID)
Camille Lamy5193caa2018-10-12 11:59:423831 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143832 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423833 }
3834#endif
3835
Lucas Furukawa Gadania9c45682019-07-31 22:05:143836 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423837
Camille Lamy5193caa2018-10-12 11:59:423838 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3839 std::string extra_headers_crlf;
3840 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093841
3842 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143843 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083844 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453845 params.initiator_frame_token.has_value()
3846 ? &(params.initiator_frame_token.value())
3847 : nullptr,
3848 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
jongdeok.kim5de823b32022-06-14 04:37:503849 params.is_form_submission,
John Delaney50425f82020-04-07 16:26:213850 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
Garrett Tanzer47852462022-07-20 18:16:413851 params.impression, params.is_pdf,
3852 is_embedder_initiated_fenced_frame_navigation);
Yao Xiaodc5ed102019-06-04 19:19:093853 navigation_request->set_from_download_cross_origin_redirect(
3854 params.from_download_cross_origin_redirect);
W. James MacLean00568d72022-02-24 19:36:553855 navigation_request->set_force_new_browsing_instance(
3856 params.force_new_browsing_instance);
Yao Xiaodc5ed102019-06-04 19:19:093857 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423858}
3859
3860std::unique_ptr<NavigationRequest>
3861NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233862 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573863 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233864 FrameNavigationEntry* frame_entry,
3865 ReloadType reload_type,
3866 bool is_same_document_history_load,
Nate Chapin45f620582021-09-30 17:45:433867 bool is_history_navigation_in_new_child_frame,
3868 bool is_browser_initiated) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343869 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233870 GURL dest_url = frame_entry->url();
Rakina Zata Amnif297a802022-01-18 03:53:433871 // We should never navigate to an existing initial NavigationEntry that is the
3872 // initial NavigationEntry for the initial empty document that hasn't been
3873 // overridden by the synchronous about:blank commit, to preserve previous
3874 // behavior where trying to reload when the main frame is on the initial empty
3875 // document won't result in a navigation.
3876 // See also https://crbug.com/1277414.
3877 DCHECK(!entry->IsInitialEntryNotForSynchronousAboutBlank());
Anton Bikineevf62d1bf2021-05-15 17:56:073878 absl::optional<url::Origin> origin_to_commit =
Nasko Oskov03912102019-01-11 00:21:323879 frame_entry->committed_origin();
3880
clamyea99ea12018-05-28 13:54:233881 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013882 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573883 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233884 // We may have been redirected when navigating to the current URL.
3885 // Use the URL the user originally intended to visit as signaled by the
3886 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013887 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573888 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233889 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323890 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233891 }
3892
Ehsan Karamad44fc72112019-02-26 18:15:473893 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3894 // Avoid starting any new navigations since this node is now preparing for
3895 // attaching an inner delegate.
3896 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203897 }
3898
Ian Vollick1c6dd3e2022-04-13 02:06:263899 if (!IsValidURLForNavigation(frame_tree_node->IsOutermostMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573900 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233901 return nullptr;
3902 }
3903
Kunihiko Sakamoto346a74e2021-03-10 08:57:483904 if (!DoesURLMatchOriginForNavigation(
3905 dest_url, origin_to_commit,
Wang Hui14cac7a2022-01-28 06:07:473906 frame_entry->subresource_web_bundle_navigation_info(), entry,
Ian Vollick1c6dd3e2022-04-13 02:06:263907 frame_tree_node->IsOutermostMainFrame())) {
Kunihiko Sakamoto346a74e2021-03-10 08:57:483908 DCHECK(false) << " url:" << dest_url
Wang Hui14cac7a2022-01-28 06:07:473909 << " base_url_for_data_url: " << entry->GetBaseURLForDataURL()
Kunihiko Sakamoto346a74e2021-03-10 08:57:483910 << " origin:" << origin_to_commit.value();
Nasko Oskov03912102019-01-11 00:21:323911 return nullptr;
3912 }
3913
clamyea99ea12018-05-28 13:54:233914 // This will be used to set the Navigation Timing API navigationStart
3915 // parameter for browser navigations in new tabs (intents, tabs opened through
3916 // "Open link in new tab"). If the navigation must wait on the current
3917 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3918 // will be updated when the BeforeUnload ack is received.
3919 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233920
danakjd83d706d2020-11-25 22:11:123921 // Look for a pending commit that is to another document in this
3922 // FrameTreeNode. If one exists, then the last committed URL will not be the
3923 // current URL by the time this navigation commits.
3924 bool has_pending_cross_document_commit =
3925 frame_tree_node->render_manager()
3926 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493927 bool is_currently_error_page =
Miyoung Shina2dd6a42021-10-07 12:19:213928 frame_tree_node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:123929
Minggang Wangb9f3fa92021-07-01 15:30:313930 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123931 /*old_url=*/frame_tree_node->current_url(),
3932 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493933 has_pending_cross_document_commit, is_currently_error_page,
Garrett Tanzer405f3402022-07-21 20:12:493934 is_same_document_history_load,
Garrett Tanzer267c2b82022-07-26 16:53:133935 /*is_embedder_initiated_fenced_frame_navigation=*/false,
Garrett Tanzer405f3402022-07-21 20:12:493936 /*is_unfenced_top_navigation=*/false);
Camille Lamy5193caa2018-10-12 11:59:423937
3938 // A form submission may happen here if the navigation is a
3939 // back/forward/reload navigation that does a form resubmission.
3940 scoped_refptr<network::ResourceRequestBody> request_body;
3941 std::string post_content_type;
jongdeok.kim5de823b32022-06-14 04:37:503942 // TODO(https://crbug.com/931209) Store |is_form_submission| in the history
3943 // entry. This way, it could be directly retrieved here. Right now, it is only
3944 // partially recovered when request.method == "POST" and request.body exists.
3945 bool is_form_submission = false;
Camille Lamy5193caa2018-10-12 11:59:423946 if (frame_entry->method() == "POST") {
3947 request_body = frame_entry->GetPostData(&post_content_type);
3948 // Might have a LF at end.
Peter Kastingb53b81912021-04-28 19:23:303949 post_content_type = std::string(
3950 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL));
jongdeok.kim5de823b32022-06-14 04:37:503951 is_form_submission = !!request_body;
Camille Lamy5193caa2018-10-12 11:59:423952 }
3953
3954 // Create the NavigationParams based on |entry| and |frame_entry|.
Minggang Wangb9f3fa92021-07-01 15:30:313955 blink::mojom::CommonNavigationParamsPtr common_params =
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513956 entry->ConstructCommonNavigationParams(
3957 *frame_entry, request_body, dest_url,
3958 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
Tarun Bansalbcd62c82022-01-18 17:27:383959 navigation_type, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533960 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513961 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553962 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423963
3964 // TODO(clamy): |intended_as_new_entry| below should always be false once
3965 // Reload no longer leads to this being called for a pending NavigationEntry
3966 // of index -1.
Minggang Wangb9f3fa92021-07-01 15:30:313967 blink::mojom::CommitNavigationParamsPtr commit_params =
Lucas Furukawa Gadania9c45682019-07-31 22:05:143968 entry->ConstructCommitNavigationParams(
3969 *frame_entry, common_params->url, origin_to_commit,
3970 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3971 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533972 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
Liam Bradyd2a41e152022-07-19 13:58:483973 frame_tree_node->pending_frame_policy(),
3974 frame_tree_node->AncestorOrSelfHasCSPEE());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143975 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423976
W. James MacLean81b8d01f2022-01-25 20:50:593977 if (common_params->url.IsAboutSrcdoc())
3978 commit_params->srcdoc_value = frame_tree_node->srcdoc_value();
clamyea99ea12018-05-28 13:54:233979 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143980 frame_tree_node, std::move(common_params), std::move(commit_params),
Nate Chapin45f620582021-09-30 17:45:433981 is_browser_initiated, false /* was_opener_suppressed */,
Takashi Toyoshimae87b7be2021-01-22 11:51:083982 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453983 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
jongdeok.kim5de823b32022-06-14 04:37:503984 entry->extra_headers(), frame_entry, entry, is_form_submission,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473985 nullptr /* navigation_ui_data */, absl::nullopt /* impression */,
3986 false /* is_pdf */);
clamyea99ea12018-05-28 13:54:233987}
3988
[email protected]d202a7c2012-01-04 07:53:473989void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213990 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273991 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403992
Takashi Toyoshimaea534ef22021-07-21 03:27:593993 // We need to notify the ssl_manager_ before the WebContents so the
[email protected]df1af242009-05-01 00:11:403994 // location bar will have up-to-date information about the security style
3995 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133996 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403997
Rakina Zata Amnidaa84f62022-02-17 00:55:313998 bool should_fire_navigation_state_changed = true;
3999#if BUILDFLAG(IS_ANDROID)
4000 if (details && details->should_stay_as_initial_entry) {
4001 // For initial NavigationEntries, only fire NavigationStateChanged() if the
4002 // embedder wants to hear about it.
4003 should_fire_navigation_state_changed =
4004 !GetContentClient()
4005 ->browser()
4006 ->ShouldIgnoreInitialNavigationEntryNavigationStateChangedForLegacySupport();
4007 }
4008#endif
4009 if (should_fire_navigation_state_changed)
4010 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
4011
[email protected]ec6c05f2013-10-23 18:41:574012 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:464013
[email protected]b0f724c2013-09-05 04:21:134014 // TODO(avi): Remove. http://crbug.com/170921
4015 NotificationDetails notification_details =
4016 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:154017 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
4018 Source<NavigationController>(this),
4019 notification_details);
initial.commit09911bf2008-07-26 23:55:294020}
4021
initial.commit09911bf2008-07-26 23:55:294022// static
[email protected]d202a7c2012-01-04 07:53:474023size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:234024 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:154025 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:214026 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:234027}
4028
[email protected]d202a7c2012-01-04 07:53:474029void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:224030 if (is_active && needs_reload_)
4031 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:294032}
4033
[email protected]d202a7c2012-01-04 07:53:474034void NavigationControllerImpl::LoadIfNecessary() {
Rakina Zata Amnid605d462022-06-01 10:17:034035 SCOPED_CRASH_KEY_BOOL("nav_reentrancy_caller1", "LoadIf_pending",
4036 !!pending_entry_);
initial.commit09911bf2008-07-26 23:55:294037 if (!needs_reload_)
4038 return;
4039
Bo Liucdfa4b12018-11-06 00:21:444040 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
4041 needs_reload_type_);
4042
initial.commit09911bf2008-07-26 23:55:294043 // Calling Reload() results in ignoring state, and not loading.
4044 // Explicitly use NavigateToPendingEntry so that the renderer uses the
4045 // cached state.
avicc872d7242015-08-19 21:26:344046 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:164047 NavigateToExistingPendingEntry(ReloadType::NONE,
Nate Chapin45f620582021-09-30 17:45:434048 FrameTreeNode::kFrameTreeNodeInvalidId,
4049 true /* is_browser_initiated */);
Rakina Zata Amni2322f4f82022-01-24 13:24:244050 } else if (last_committed_entry_index_ != -1 &&
4051 !GetLastCommittedEntry()
Rakina Zata Amnif297a802022-01-18 03:53:434052 ->IsInitialEntryNotForSynchronousAboutBlank()) {
arthursonzogni5c4c202d2017-04-25 23:41:274053 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:344054 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:164055 NavigateToExistingPendingEntry(ReloadType::NONE,
Nate Chapin45f620582021-09-30 17:45:434056 FrameTreeNode::kFrameTreeNodeInvalidId,
4057 true /* is_browser_initiated */);
avicc872d7242015-08-19 21:26:344058 } else {
Rakina Zata Amnif297a802022-01-18 03:53:434059 // We should never navigate to an existing initial NavigationEntry that is
4060 // the initial NavigationEntry for the initial empty document that hasn't
4061 // been overridden by the synchronous about:blank commit, to preserve
4062 // legacy behavior where trying to reload when the main frame is on the
4063 // initial empty document won't result in a navigation. See also
4064 // https://crbug.com/1277414. If there is something to reload, the
4065 // successful reload will clear the |needs_reload_| flag. Otherwise, just do
4066 // it here.
avicc872d7242015-08-19 21:26:344067 needs_reload_ = false;
4068 }
initial.commit09911bf2008-07-26 23:55:294069}
4070
Kevin McNeeccca6172021-10-19 17:11:144071base::WeakPtr<NavigationHandle>
4072NavigationControllerImpl::LoadPostCommitErrorPage(
Carlos IL42b416592019-10-07 23:10:364073 RenderFrameHost* render_frame_host,
4074 const GURL& url,
4075 const std::string& error_page_html,
4076 net::Error error) {
Rakina Zata Amni919b7922020-12-11 09:03:134077 RenderFrameHostImpl* rfhi =
4078 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:584079
4080 // Only active documents can load post-commit error pages:
4081 // - If the document is in pending deletion, the browser already committed to
4082 // destroying this RenderFrameHost so ignore loading the error page.
4083 // - If the document is in back-forward cache, it's not allowed to navigate as
4084 // it should remain frozen. Ignore the request and evict the document from
4085 // back-forward cache.
4086 // - If the document is prerendering, it can navigate but when loading error
4087 // pages, cancel prerendering.
Fergal Daly1336ac642021-09-14 15:13:114088 if (rfhi->IsInactiveAndDisallowActivation(
4089 DisallowActivationReasonId::kLoadPostCommitErrorPage)) {
Kevin McNeeccca6172021-10-19 17:11:144090 return nullptr;
Fergal Daly1336ac642021-09-14 15:13:114091 }
Sreeja Kamishettydb8e2892021-03-10 09:30:584092
Rakina Zata Amni919b7922020-12-11 09:03:134093 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:414094
Minggang Wangb9f3fa92021-07-01 15:30:314095 blink::mojom::CommonNavigationParamsPtr common_params =
Minggang Wanga13c796e2021-07-02 05:54:434096 blink::CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:594097 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
4098 // the frame actually committed (e.g. iframe with "src" set to a
4099 // slow-responding URL). We should rewrite the URL to about:blank in this
4100 // case, as the renderer will only think a page is an error page if it has a
4101 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:134102 common_params->url = url.is_empty() ? GURL("about:blank") : url;
Minggang Wangb9f3fa92021-07-01 15:30:314103 blink::mojom::CommitNavigationParamsPtr commit_params =
Minggang Wanga13c796e2021-07-02 05:54:434104 blink::CreateCommitNavigationParams();
Antonio Sartori58591c892021-04-21 06:54:334105 commit_params->original_url = common_params->url;
John Delaney131ad362019-08-08 21:57:414106
arthursonzogni70ac7302020-05-28 08:49:054107 // Error pages have a fully permissive FramePolicy.
4108 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
4109 // error pages.
4110 commit_params->frame_policy = blink::FramePolicy();
4111
John Delaney131ad362019-08-08 21:57:414112 std::unique_ptr<NavigationRequest> navigation_request =
4113 NavigationRequest::CreateBrowserInitiated(
4114 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:084115 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:194116 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:454117 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:064118 "" /* extra_headers */, nullptr /* frame_entry */,
jongdeok.kim5de823b32022-06-14 04:37:504119 nullptr /* entry */, false /* is_form_submission */,
Daniel Hosseinianf0fbfb42021-09-08 02:20:474120 nullptr /* navigation_ui_data */, absl::nullopt /* impression */,
4121 false /* is_pdf */);
Carlos IL42b416592019-10-07 23:10:364122 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:414123 navigation_request->set_net_error(error);
4124 node->CreatedNavigationRequest(std::move(navigation_request));
4125 DCHECK(node->navigation_request());
Kevin McNeeccca6172021-10-19 17:11:144126
4127 // Calling BeginNavigation may destroy the NavigationRequest.
4128 base::WeakPtr<NavigationRequest> created_navigation_request(
4129 node->navigation_request()->GetWeakPtr());
John Delaney131ad362019-08-08 21:57:414130 node->navigation_request()->BeginNavigation();
Kevin McNeeccca6172021-10-19 17:11:144131 return created_navigation_request;
John Delaney131ad362019-08-08 21:57:414132}
4133
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574134void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:214135 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:094136 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:154137 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:144138 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:294139}
4140
[email protected]d202a7c2012-01-04 07:53:474141void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:364142 RestoreType type) {
Charlie Reis23c26da2022-01-29 00:57:474143 // TODO(https://crbug.com/1287624): Don't allow an index of -1, which would
4144 // represent a no-committed-entry state.
4145 DCHECK(selected_index >= -1 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:364146 ConfigureEntriesForRestore(&entries_, type);
Charlie Reis23c26da2022-01-29 00:57:474147 // TODO(https://crbug.com/1287624): This will be pointing to the wrong entry
4148 // if `entries_` contains pre-existing entries from the NavigationController
4149 // before restore, which would not be removed and will be at the front of the
4150 // entries list, causing the index to be off by the amount of pre-existing
4151 // entries in the list. Fix this to point to the correct entry.
initial.commit09911bf2008-07-26 23:55:294152 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:294153}
[email protected]765b35502008-08-21 00:51:204154
arthursonzogni69a6a1b2019-09-17 09:23:004155void NavigationControllerImpl::DiscardNonCommittedEntries() {
Rakina Zata Amnia4e27222021-12-22 01:05:004156 DiscardNonCommittedEntriesWithCommitDetails(nullptr /* commit_details */);
4157}
4158
4159void NavigationControllerImpl::DiscardNonCommittedEntriesWithCommitDetails(
4160 LoadCommittedDetails* commit_details) {
Michael Thiessen9b14d512019-09-23 21:19:474161 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:104162 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
4163 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:294164 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:474165 return;
Michael Thiessenc5676d22019-09-25 22:32:104166 }
avi45a72532015-04-07 21:01:454167 DiscardPendingEntry(false);
Rakina Zata Amnidaa84f62022-02-17 00:55:314168
4169 if (!delegate_)
4170 return;
4171
4172 bool should_fire_navigation_state_changed = true;
4173#if BUILDFLAG(IS_ANDROID)
4174 if (commit_details && commit_details->should_stay_as_initial_entry) {
4175 // For initial NavigationEntries, only fire NavigationStateChanged() if the
4176 // embedder wants to hear about it.
4177 should_fire_navigation_state_changed =
4178 !GetContentClient()
4179 ->browser()
4180 ->ShouldIgnoreInitialNavigationEntryNavigationStateChangedForLegacySupport();
Rakina Zata Amniddf10502022-01-15 02:56:554181 }
Rakina Zata Amnidaa84f62022-02-17 00:55:314182#endif
4183 if (should_fire_navigation_state_changed)
4184 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:484185}
4186
avi7c6f35e2015-05-08 17:52:384187int NavigationControllerImpl::GetEntryIndexWithUniqueID(
4188 int nav_entry_id) const {
4189 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
4190 if (entries_[i]->GetUniqueID() == nav_entry_id)
4191 return i;
4192 }
4193 return -1;
4194}
4195
[email protected]d202a7c2012-01-04 07:53:474196void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574197 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:254198 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574199 DCHECK_LE(max_index, source->GetEntryCount());
Nate Chapin214a86a2021-06-21 20:35:574200 std::unique_ptr<NavigationEntryRestoreContextImpl> context =
4201 std::make_unique<NavigationEntryRestoreContextImpl>();
[email protected]e1cd5452010-08-26 18:03:254202 for (int i = 0; i < max_index; i++) {
Nate Chapin9f169072021-06-09 19:32:374203 // Normally, cloning a NavigationEntryImpl results in sharing
4204 // FrameNavigationEntries between the original and the clone. However, when
4205 // cloning from a different NavigationControllerImpl, we want to fork the
4206 // FrameNavigationEntries.
Nate Chapin9f169072021-06-09 19:32:374207 entries_.insert(entries_.begin() + i,
Nate Chapin214a86a2021-06-21 20:35:574208 source->entries_[i]->CloneWithoutSharing(context.get()));
[email protected]e1cd5452010-08-26 18:03:254209 }
arthursonzogni5c4c202d2017-04-25 23:41:274210 DCHECK(pending_entry_index_ == -1 ||
4211 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:254212}
[email protected]c5b88d82012-10-06 17:03:334213
4214void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:184215 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:334216 get_timestamp_callback_ = get_timestamp_callback;
4217}
[email protected]8ff00d72012-10-23 19:12:214218
Shivani Sharmaffb32b82019-04-09 16:58:474219// History manipulation intervention:
4220void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:474221 bool replace_entry,
4222 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:404223 bool is_renderer_initiated,
4224 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:454225 // Note that for a subframe, previous_document_was_activated is true if the
4226 // gesture happened in any subframe (propagated to main frame) or in the main
4227 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:474228 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:274229 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:474230 return;
4231 }
4232 if (last_committed_entry_index_ == -1)
4233 return;
4234
Shivani Sharmac4cc8922019-04-18 03:11:174235 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:474236
Alexander Timine3ec4192020-04-20 16:39:404237 // Log UKM with the URL we are navigating away from.
4238 ukm::builders::HistoryManipulationIntervention(
4239 previous_page_load_ukm_source_id)
4240 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:474241}
4242
Shivani Sharmac4cc8922019-04-18 03:11:174243void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
4244 int reference_index,
4245 bool skippable) {
4246 auto* reference_entry = GetEntryAtIndex(reference_index);
4247 reference_entry->set_should_skip_on_back_forward_ui(skippable);
4248
4249 int64_t document_sequence_number =
4250 reference_entry->root_node()->frame_entry->document_sequence_number();
4251 for (int index = 0; index < GetEntryCount(); index++) {
4252 auto* entry = GetEntryAtIndex(index);
4253 if (entry->root_node()->frame_entry->document_sequence_number() ==
4254 document_sequence_number) {
4255 entry->set_should_skip_on_back_forward_ui(skippable);
4256 }
4257 }
4258}
4259
arthursonzogni66f711c2019-10-08 14:40:364260std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
4261NavigationControllerImpl::ReferencePendingEntry() {
4262 DCHECK(pending_entry_);
4263 auto pending_entry_ref =
4264 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
4265 pending_entry_refs_.insert(pending_entry_ref.get());
4266 return pending_entry_ref;
4267}
4268
4269void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
4270 // Ignore refs that don't correspond to the current pending entry.
4271 auto it = pending_entry_refs_.find(ref);
4272 if (it == pending_entry_refs_.end())
4273 return;
4274 pending_entry_refs_.erase(it);
4275
4276 if (!pending_entry_refs_.empty())
4277 return;
4278
4279 // The pending entry may be deleted before the last PendingEntryRef.
4280 if (!pending_entry_)
4281 return;
4282
4283 // We usually clear the pending entry when the matching NavigationRequest
4284 // fails, so that an arbitrary URL isn't left visible above a committed page.
4285 //
4286 // However, we do preserve the pending entry in some cases, such as on the
4287 // initial navigation of an unmodified blank tab. We also allow the delegate
4288 // to say when it's safe to leave aborted URLs in the omnibox, to let the
4289 // user edit the URL and try again. This may be useful in cases that the
4290 // committed page cannot be attacker-controlled. In these cases, we still
4291 // allow the view to clear the pending entry and typed URL if the user
4292 // requests (e.g., hitting Escape with focus in the address bar).
4293 //
4294 // Do not leave the pending entry visible if it has an invalid URL, since this
4295 // might be formatted in an unexpected or unsafe way.
4296 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:364297 bool should_preserve_entry =
4298 (pending_entry_ == GetVisibleEntry()) &&
4299 pending_entry_->GetURL().is_valid() &&
4300 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
4301 if (should_preserve_entry)
4302 return;
4303
4304 DiscardPendingEntry(true);
4305 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
4306}
4307
Titouan Rigoudy6ec70402021-02-02 15:42:194308std::unique_ptr<PolicyContainerPolicies>
4309NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:394310 RenderFrameHostImpl* rfh,
4311 bool is_same_document,
Rakina Zata Amniafd3c6582021-11-30 06:19:174312 const GURL& url) {
Antonio Sartori78a749f2020-11-30 12:03:394313 if (is_same_document) {
Rakina Zata Amni2322f4f82022-01-24 13:24:244314 // TODO(https://crbug.com/524208): Remove this nullptr check when we can
4315 // ensure we always have a FrameNavigationEntry here.
4316 if (!GetLastCommittedEntry())
4317 return nullptr;
Charlie Reis73e356242021-04-02 17:10:314318
Antonio Sartori78a749f2020-11-30 12:03:394319 FrameNavigationEntry* previous_frame_entry =
4320 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
4321
4322 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
4323 // ensure we always have a FrameNavigationEntry here.
4324 if (!previous_frame_entry)
4325 return nullptr;
4326
Titouan Rigoudy6ec70402021-02-02 15:42:194327 const PolicyContainerPolicies* previous_policies =
4328 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:394329
Titouan Rigoudy6ec70402021-02-02 15:42:194330 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:394331 return nullptr;
4332
4333 // Make a copy of the policy container for the new FrameNavigationEntry.
Titouan Rigoudy72f892d2022-05-02 18:21:234334 return previous_policies->ClonePtr();
Antonio Sartori78a749f2020-11-30 12:03:394335 }
4336
Titouan Rigoudy72f892d2022-05-02 18:21:234337 return rfh->policy_container_host()->policies().ClonePtr();
Antonio Sartori78a749f2020-11-30 12:03:394338}
4339
Hayato Ito2c8c08d02021-06-23 03:38:434340void NavigationControllerImpl::BroadcastHistoryOffsetAndLength() {
Carlos Caballeroede6f8c2021-01-28 11:01:504341 OPTIONAL_TRACE_EVENT2(
Hayato Ito2c8c08d02021-06-23 03:38:434342 "content", "NavigationControllerImpl::BroadcastHistoryOffsetAndLength",
4343 "history_offset", GetLastCommittedEntryIndex(), "history_length",
4344 GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:504345
4346 auto callback = base::BindRepeating(
4347 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
4348 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
4349 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
4350 }
4351 },
Hayato Ito2c8c08d02021-06-23 03:38:434352 GetLastCommittedEntryIndex(), GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:504353 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
4354}
4355
4356void NavigationControllerImpl::DidAccessInitialMainDocument() {
4357 // We may have left a failed browser-initiated navigation in the address bar
4358 // to let the user edit it and try again. Clear it now that content might
4359 // show up underneath it.
Yu Gaoc8c18552022-06-22 14:38:454360 if (!frame_tree_.IsLoadingIncludingInnerFrameTrees() && GetPendingEntry())
Carlos Caballeroede6f8c2021-01-28 11:01:504361 DiscardPendingEntry(false);
4362
4363 // Update the URL display.
4364 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
4365}
4366
4367void NavigationControllerImpl::UpdateStateForFrame(
4368 RenderFrameHostImpl* rfhi,
4369 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:564370 OPTIONAL_TRACE_EVENT1("content",
4371 "NavigationControllerImpl::UpdateStateForFrame",
4372 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:504373 // The state update affects the last NavigationEntry associated with the given
4374 // |render_frame_host|. This may not be the last committed NavigationEntry (as
4375 // in the case of an UpdateState from a frame being swapped out). We track
4376 // which entry this is in the RenderFrameHost's nav_entry_id.
4377 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
4378 if (!entry)
4379 return;
4380
4381 FrameNavigationEntry* frame_entry =
4382 entry->GetFrameEntry(rfhi->frame_tree_node());
4383 if (!frame_entry)
4384 return;
4385
4386 // The SiteInstance might not match if we do a cross-process navigation with
4387 // replacement (e.g., auto-subframe), in which case the swap out of the old
4388 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4389 // has already been replaced and destroyed.
4390 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4391 return;
4392
4393 if (page_state == frame_entry->page_state())
4394 return; // Nothing to update.
4395
4396 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4397
4398 // The document_sequence_number and item_sequence_number recorded in the
4399 // FrameNavigationEntry should not differ from the one coming with the update,
4400 // since it must come from the same document. Do not update it if a difference
4401 // is detected, as this indicates that |frame_entry| is not the correct one.
4402 blink::ExplodedPageState exploded_state;
4403 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4404 return;
4405
4406 if (exploded_state.top.document_sequence_number !=
4407 frame_entry->document_sequence_number() ||
4408 exploded_state.top.item_sequence_number !=
4409 frame_entry->item_sequence_number()) {
4410 return;
4411 }
4412
4413 frame_entry->SetPageState(page_state);
4414 NotifyEntryChanged(entry);
4415}
4416
Domenic Denicolacd30f5f82022-03-16 21:48:014417std::vector<blink::mojom::NavigationApiHistoryEntryPtr>
4418NavigationControllerImpl::PopulateSingleNavigationApiHistoryEntryVector(
Nate Chapind1fe3612021-04-16 20:45:574419 Direction direction,
4420 int entry_index,
4421 const url::Origin& pending_origin,
4422 FrameTreeNode* node,
4423 SiteInstance* site_instance,
Nate Chapin63db0d12022-01-20 22:03:304424 int64_t pending_item_sequence_number,
4425 int64_t pending_document_sequence_number) {
Domenic Denicolacd30f5f82022-03-16 21:48:014426 std::vector<blink::mojom::NavigationApiHistoryEntryPtr> entries;
Rakina Zata Amni2322f4f82022-01-24 13:24:244427 if (GetLastCommittedEntry() && GetLastCommittedEntry()->IsInitialEntry()) {
Rakina Zata Amniafd3c6582021-11-30 06:19:174428 // Don't process the initial entry.
4429 DCHECK_EQ(GetEntryCount(), 1);
4430 return entries;
4431 }
Nate Chapind1fe3612021-04-16 20:45:574432 int offset = direction == Direction::kForward ? 1 : -1;
Nate Chapin63db0d12022-01-20 22:03:304433 int64_t previous_item_sequence_number = pending_item_sequence_number;
Nate Chapind1fe3612021-04-16 20:45:574434 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4435 i += offset) {
4436 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
Nate Chapindedfa642022-01-28 23:59:414437 if (!frame_entry)
Nate Chapind1fe3612021-04-16 20:45:574438 break;
Domenic Denicolacd30f5f82022-03-16 21:48:014439 // An entry should only appear in the navigation API entries if it is for
4440 // the same origin as the document being committed. Check the committed
4441 // origin, or if that is not available (during restore), check against the
4442 // FNE's url.
Nate Chapindedfa642022-01-28 23:59:414443 url::Origin frame_entry_origin =
4444 frame_entry->committed_origin().value_or(url::Origin::Resolve(
4445 frame_entry->url(),
4446 frame_entry->initiator_origin().value_or(url::Origin())));
4447 if (!pending_origin.IsSameOriginWith(frame_entry_origin))
Nate Chapind1fe3612021-04-16 20:45:574448 break;
4449 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4450 continue;
4451 blink::ExplodedPageState exploded_page_state;
4452 if (blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4453 &exploded_page_state)) {
4454 blink::ExplodedFrameState frame_state = exploded_page_state.top;
Nate Chapin63db0d12022-01-20 22:03:304455
4456 // If the document represented by this FNE hid its full url from appearing
4457 // in a referrer via a "no-referrer" or "origin" referrer policy, censor
Domenic Denicolacd30f5f82022-03-16 21:48:014458 // the url in the navigation API as well (unless we're navigating to that
Nate Chapin63db0d12022-01-20 22:03:304459 // document).
4460 std::u16string url;
4461 if (pending_document_sequence_number ==
4462 frame_entry->document_sequence_number() ||
Domenic Denicolacc094fb2022-03-16 23:40:574463 !frame_entry->protect_url_in_navigation_api()) {
Nate Chapin63db0d12022-01-20 22:03:304464 url = frame_state.url_string.value_or(std::u16string());
4465 }
4466
Domenic Denicolacd30f5f82022-03-16 21:48:014467 blink::mojom::NavigationApiHistoryEntryPtr entry =
4468 blink::mojom::NavigationApiHistoryEntry::New(
Domenic Denicolacc094fb2022-03-16 23:40:574469 frame_state.navigation_api_key.value_or(std::u16string()),
4470 frame_state.navigation_api_id.value_or(std::u16string()), url,
Nate Chapinab5c3a712021-11-18 22:17:094471 frame_state.item_sequence_number,
4472 frame_state.document_sequence_number,
Nate Chapin393cbde12022-05-27 00:36:304473 frame_state.navigation_api_state);
Rakina Zata Amniafd3c6582021-11-30 06:19:174474
Nate Chapin63db0d12022-01-20 22:03:304475 DCHECK(entry->url.empty() ||
4476 pending_origin.CanBeDerivedFrom(GURL(entry->url)));
Nate Chapind1fe3612021-04-16 20:45:574477 entries.push_back(std::move(entry));
4478 previous_item_sequence_number = frame_entry->item_sequence_number();
4479 }
4480 }
4481 // If |entries| was constructed by iterating backwards from
4482 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4483 // earliest-at-the-front. Reverse it.
4484 if (direction == Direction::kBack)
4485 std::reverse(entries.begin(), entries.end());
4486 return entries;
4487}
4488
Domenic Denicolacd30f5f82022-03-16 21:48:014489blink::mojom::NavigationApiHistoryEntryArraysPtr
4490NavigationControllerImpl::GetNavigationApiHistoryEntryVectors(
Nate Chapin97d2f542022-02-18 01:34:554491 FrameTreeNode* node,
Nate Chapind1fe3612021-04-16 20:45:574492 NavigationRequest* request) {
4493 url::Origin pending_origin =
Nate Chapin97d2f542022-02-18 01:34:554494 request ? request->commit_params().origin_to_commit
4495 ? *request->commit_params().origin_to_commit
4496 : url::Origin::Create(request->common_params().url)
4497 : url::Origin::Create(node->current_url());
Nate Chapind1fe3612021-04-16 20:45:574498
Nate Chapind1fe3612021-04-16 20:45:574499 scoped_refptr<SiteInstance> site_instance =
Nate Chapin97d2f542022-02-18 01:34:554500 node->current_frame_host()->GetSiteInstance();
Nate Chapind1fe3612021-04-16 20:45:574501
Nate Chapine82339d02022-05-03 23:48:254502 // NOTE: |entry_index| is the index where this entry will commit if no
4503 // modifications are made between now and DidCommitNavigation. This is used to
4504 // walk |entries_| and determine which entries should be exposed by the
4505 // navigation API. It is important to calculate this correctly, because blink
4506 // will cancel a same-document history commit if it's not present in the
4507 // entries blink knows about.
4508 int entry_index = GetLastCommittedEntryIndex();
Nate Chapind1fe3612021-04-16 20:45:574509 int64_t pending_item_sequence_number = 0;
Nate Chapin63db0d12022-01-20 22:03:304510 int64_t pending_document_sequence_number = 0;
Nate Chapine82339d02022-05-03 23:48:254511 bool will_create_new_entry = false;
4512 if (GetPendingEntryIndex() != -1) {
4513 entry_index = GetPendingEntryIndex();
4514 if (auto* frame_entry = GetPendingEntry()->GetFrameEntry(node)) {
4515 pending_item_sequence_number = frame_entry->item_sequence_number();
4516 pending_document_sequence_number =
4517 frame_entry->document_sequence_number();
4518 }
4519 } else if (request &&
4520 !NavigationTypeUtils::IsReload(
4521 request->common_params().navigation_type) &&
4522 !NavigationTypeUtils::IsHistory(
4523 request->common_params().navigation_type) &&
4524 !request->common_params().should_replace_current_entry &&
4525 !request->common_params()
4526 .is_history_navigation_in_new_child_frame) {
4527 will_create_new_entry = true;
4528 entry_index = GetLastCommittedEntryIndex() + 1;
4529 // Don't set pending_item_sequence_number or
4530 // pending_document_sequence_number in this case - a new unique isn/dsn will
4531 // be calculated in the renderer later.
4532 } else if (GetLastCommittedEntryIndex() != -1) {
4533 entry_index = GetLastCommittedEntryIndex();
4534 if (auto* frame_entry = GetLastCommittedEntry()->GetFrameEntry(node)) {
Nate Chapind1fe3612021-04-16 20:45:574535 pending_item_sequence_number = frame_entry->item_sequence_number();
Nate Chapin63db0d12022-01-20 22:03:304536 pending_document_sequence_number =
4537 frame_entry->document_sequence_number();
4538 }
Nate Chapind1fe3612021-04-16 20:45:574539 }
4540
Domenic Denicolacd30f5f82022-03-16 21:48:014541 auto entry_arrays = blink::mojom::NavigationApiHistoryEntryArrays::New();
Nate Chapine82339d02022-05-03 23:48:254542 if (entry_index == -1) {
4543 // TODO(rakina): Exit early when there is no last committed entry.
4544 // Remove when InitialNavigationEntry ships.
4545 return entry_arrays;
4546 }
4547
Domenic Denicolacd30f5f82022-03-16 21:48:014548 entry_arrays->back_entries = PopulateSingleNavigationApiHistoryEntryVector(
Nate Chapind1fe3612021-04-16 20:45:574549 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
Nate Chapin4e657a472022-02-06 03:38:374550 pending_item_sequence_number, pending_document_sequence_number);
Nate Chapind1fe3612021-04-16 20:45:574551
4552 // Don't populate forward entries if they will be truncated by a new entry.
4553 if (!will_create_new_entry) {
Domenic Denicolacd30f5f82022-03-16 21:48:014554 entry_arrays->forward_entries =
4555 PopulateSingleNavigationApiHistoryEntryVector(
4556 Direction::kForward, entry_index, pending_origin, node,
4557 site_instance.get(), pending_item_sequence_number,
4558 pending_document_sequence_number);
Nate Chapind1fe3612021-04-16 20:45:574559 }
Nate Chapin4e657a472022-02-06 03:38:374560 return entry_arrays;
Nate Chapind1fe3612021-04-16 20:45:574561}
4562
Nate Chapinfbfe5af2021-06-10 17:22:084563NavigationControllerImpl::HistoryNavigationAction
Domenic Denicolacc094fb2022-03-16 23:40:574564NavigationControllerImpl::ShouldNavigateToEntryForNavigationApiKey(
Nate Chapinfbfe5af2021-06-10 17:22:084565 FrameNavigationEntry* current_entry,
4566 FrameNavigationEntry* target_entry,
Domenic Denicolacc094fb2022-03-16 23:40:574567 const std::string& navigation_api_key) {
Nate Chapinfbfe5af2021-06-10 17:22:084568 if (!target_entry || !target_entry->committed_origin())
4569 return HistoryNavigationAction::kStopLooking;
4570 if (current_entry->site_instance() != target_entry->site_instance())
4571 return HistoryNavigationAction::kStopLooking;
4572 if (!current_entry->committed_origin()->IsSameOriginWith(
4573 *target_entry->committed_origin())) {
4574 return HistoryNavigationAction::kStopLooking;
4575 }
4576
4577 // NOTE: We don't actually care between kSameDocument and
4578 // kDifferentDocument, so always use kDifferentDocument by convention.
Domenic Denicolacc094fb2022-03-16 23:40:574579 if (target_entry->navigation_api_key() == navigation_api_key)
Nate Chapinfbfe5af2021-06-10 17:22:084580 return HistoryNavigationAction::kDifferentDocument;
4581 return HistoryNavigationAction::kKeepLooking;
4582}
4583
Domenic Denicolacc094fb2022-03-16 23:40:574584void NavigationControllerImpl::NavigateToNavigationApiKey(
4585 FrameTreeNode* node,
Nate Chapinc39f43a2022-04-01 00:16:254586 int sandboxed_source_frame_tree_node_id,
Domenic Denicolacc094fb2022-03-16 23:40:574587 const std::string& key) {
Nate Chapinfbfe5af2021-06-10 17:22:084588 FrameNavigationEntry* current_entry =
4589 GetLastCommittedEntry()->GetFrameEntry(node);
4590 if (!current_entry)
4591 return;
4592
4593 // We want to find the nearest matching entry that is contiguously
4594 // same-instance and same-origin. Check back first, then forward.
4595 // TODO(japhet): Link spec here once it exists.
4596 for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) {
Domenic Denicolacc094fb2022-03-16 23:40:574597 auto result = ShouldNavigateToEntryForNavigationApiKey(
Nate Chapinfbfe5af2021-06-10 17:22:084598 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4599 if (result == HistoryNavigationAction::kStopLooking)
4600 break;
4601 if (result != HistoryNavigationAction::kKeepLooking) {
Nate Chapinc39f43a2022-04-01 00:16:254602 GoToIndex(i, sandboxed_source_frame_tree_node_id,
Nate Chapin2892b37c2021-10-08 17:33:114603 false /* is_browser_initiated*/);
Nate Chapinfbfe5af2021-06-10 17:22:084604 return;
4605 }
4606 }
4607 for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) {
Domenic Denicolacc094fb2022-03-16 23:40:574608 auto result = ShouldNavigateToEntryForNavigationApiKey(
Nate Chapinfbfe5af2021-06-10 17:22:084609 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4610 if (result == HistoryNavigationAction::kStopLooking)
4611 break;
4612 if (result != HistoryNavigationAction::kKeepLooking) {
Nate Chapinc39f43a2022-04-01 00:16:254613 GoToIndex(i, sandboxed_source_frame_tree_node_id,
Nate Chapin2892b37c2021-10-08 17:33:114614 false /* is_browser_initiated*/);
Nate Chapinfbfe5af2021-06-10 17:22:084615 return;
4616 }
4617 }
4618}
4619
Domenic Denicolacc094fb2022-03-16 23:40:574620bool NavigationControllerImpl::ShouldProtectUrlInNavigationApi(
Nate Chapin63db0d12022-01-20 22:03:304621 network::mojom::ReferrerPolicy referrer_policy) {
4622 return referrer_policy == network::mojom::ReferrerPolicy::kNever ||
4623 referrer_policy == network::mojom::ReferrerPolicy::kOrigin;
4624}
4625
shivanigithubf405bf0d2021-11-05 17:58:334626bool NavigationControllerImpl::ShouldMaintainTrivialSessionHistory(
4627 const FrameTreeNode* frame_tree_node) const {
4628 // TODO(https://crbug.com/1197384): We may have to add portals in addition to
4629 // prerender and fenced frames. This should be kept in sync with
Hayato Ito7a80db42021-07-05 06:18:544630 // LocalFrame version, LocalFrame::ShouldMaintainTrivialSessionHistory.
shivanigithubf405bf0d2021-11-05 17:58:334631 return frame_tree_.is_prerendering() ||
4632 frame_tree_node->IsInFencedFrameTree();
Hayato Ito7a80db42021-07-05 06:18:544633}
4634
[email protected]8ff00d72012-10-23 19:12:214635} // namespace content