blob: 4a4cd9cce4f0874abff3c29fb573460dcb8d9657 [file] [log] [blame]
[email protected]d4a8ca482013-10-30 21:06:401// Copyright 2013 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
creis4e2ecb72015-06-20 00:46:305/*
6 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
7 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved.
9 * (http://www.torchmobile.com/)
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
21 * its contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
25 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
28 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
danakjc492bf82020-09-09 20:02:4436#include "content/browser/renderer_host/navigation_controller_impl.h"
initial.commit09911bf2008-07-26 23:55:2937
Lei Zhang96031532019-10-10 19:05:4738#include <algorithm>
dcheng36b6aec92015-12-26 06:16:3639#include <utility>
40
[email protected]c5b88d82012-10-06 17:03:3341#include "base/bind.h"
[email protected]eabfe1912014-05-12 10:07:2842#include "base/command_line.h"
Ayu Ishii2f825852022-03-08 19:47:3843#include "base/containers/adapters.h"
Rakina Zata Amni627360d2022-02-24 00:53:4044#include "base/debug/dump_without_crashing.h"
initial.commit09911bf2008-07-26 23:55:2945#include "base/logging.h"
asvitkine30330812016-08-30 04:01:0846#include "base/metrics/histogram_macros.h"
Charlie Reis23c26da2022-01-29 00:57:4747#include "base/numerics/safe_conversions.h"
Chris Hamilton83272dc2021-02-23 00:24:0248#include "base/stl_util.h"
Ryan Hamilton7f3bd3d2022-04-23 00:07:3949#include "base/strings/escape.h"
Peter Kastingb53b81912021-04-28 19:23:3050#include "base/strings/string_piece.h"
[email protected]348fbaac2013-06-11 06:31:5151#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0052#include "base/strings/utf_string_conversions.h"
[email protected]a43858f2013-06-28 15:18:3753#include "base/time/time.h"
Carlos Caballero40b0efd2021-01-26 11:55:0054#include "base/trace_event/optional_trace_event.h"
Carlos Caballeroede6f8c2021-01-28 11:01:5055#include "base/trace_event/trace_conversion_helper.h"
ssid3e765612015-01-28 04:03:4256#include "base/trace_event/trace_event.h"
servolkf3955532015-05-16 00:01:5957#include "build/build_config.h"
[email protected]eabfe1912014-05-12 10:07:2858#include "cc/base/switches.h"
jamescookda2505812015-03-20 18:01:1859#include "content/browser/bad_message.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3960#include "content/browser/blob_storage/chrome_blob_storage_context.h"
[email protected]825b1662012-03-12 19:07:3161#include "content/browser/browser_url_handler_impl.h"
[email protected]5f2aa722013-08-07 16:59:4162#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0463#include "content/browser/dom_storage/session_storage_namespace_impl.h"
danakjc492bf82020-09-09 20:02:4464#include "content/browser/renderer_host/debug_urls.h"
65#include "content/browser/renderer_host/frame_tree.h"
Carlos Caballero40b0efd2021-01-26 11:55:0066#include "content/browser/renderer_host/frame_tree_node.h"
Chris Bookholt27faf8d2022-01-20 01:03:3367#include "content/browser/renderer_host/navigation_controller_delegate.h"
danakjc492bf82020-09-09 20:02:4468#include "content/browser/renderer_host/navigation_entry_impl.h"
Nate Chapin214a86a2021-06-21 20:35:5769#include "content/browser/renderer_host/navigation_entry_restore_context_impl.h"
danakjc492bf82020-09-09 20:02:4470#include "content/browser/renderer_host/navigation_request.h"
71#include "content/browser/renderer_host/navigator.h"
72#include "content/browser/renderer_host/render_frame_host_delegate.h"
Carlos Caballero40b0efd2021-01-26 11:55:0073#include "content/browser/renderer_host/render_view_host_impl.h"
Sharon Yangd70a5392021-10-26 23:06:3274#include "content/browser/site_info.h"
[email protected]b6583592012-01-25 19:52:3375#include "content/browser/site_instance_impl.h"
Kunihiko Sakamoto346a74e2021-03-10 08:57:4876#include "content/browser/web_package/subresource_web_bundle_navigation_info.h"
Tsuyoshi Horo37493af2019-11-21 23:43:1477#include "content/browser/web_package/web_bundle_navigation_info.h"
Camille Lamy5193caa2018-10-12 11:59:4278#include "content/common/content_constants_internal.h"
Nate Chapind1fe3612021-04-16 20:45:5779#include "content/common/navigation_params_utils.h"
Nasko Oskovae49e292020-08-13 02:08:5180#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1181#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1982#include "content/public/browser/content_browser_client.h"
Fergal Daly1336ac642021-09-14 15:13:1183#include "content/public/browser/disallow_activation_reason.h"
[email protected]d9083482012-01-06 00:38:4684#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0085#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3886#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1687#include "content/public/browser/notification_types.h"
Lei Zhang96031532019-10-10 19:05:4788#include "content/public/browser/render_view_host.h"
[email protected]9677a3c2012-12-22 04:18:5889#include "content/public/browser/render_widget_host.h"
90#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1091#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3492#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:1993#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:3894#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:1095#include "content/public/common/content_features.h"
Lei Zhang96031532019-10-10 19:05:4796#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4397#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:5998#include "media/base/mime_util.h"
Arthur Sonzogni620cec62018-12-13 13:08:5799#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:18100#include "services/metrics/public/cpp/ukm_builders.h"
101#include "services/metrics/public/cpp/ukm_recorder.h"
Yue Ru Sun128804932020-09-30 22:19:17102#include "services/metrics/public/cpp/ukm_source_id.h"
Nan Lind91c8152021-10-21 16:22:37103#include "services/network/public/mojom/fetch_api.mojom.h"
[email protected]9677a3c2012-12-22 04:18:58104#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:39105#include "third_party/blink/public/common/blob/blob_utils.h"
Gyuyoung Kim107c2a02021-04-13 01:49:30106#include "third_party/blink/public/common/chrome_debug_urls.h"
Miyoung Shin1c565c912021-03-17 12:11:21107#include "third_party/blink/public/common/history/session_history_constants.h"
Blink Reformata30d4232018-04-07 15:31:06108#include "third_party/blink/public/common/mime_util/mime_util.h"
Minggang Wanga13c796e2021-07-02 05:54:43109#include "third_party/blink/public/common/navigation/navigation_params.h"
Carlos Caballeroede6f8c2021-01-28 11:01:50110#include "third_party/blink/public/common/page_state/page_state_serialization.h"
Minggang Wangb9f3fa92021-07-01 15:30:31111#include "third_party/blink/public/mojom/navigation/navigation_params.mojom.h"
Minggang Wang7ee0c742021-06-16 16:16:51112#include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h"
[email protected]cca6f392014-05-28 21:32:26113#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:29114
[email protected]8ff00d72012-10-23 19:12:21115namespace content {
[email protected]e9ba4472008-09-14 15:42:43116namespace {
117
118// Invoked when entries have been pruned, or removed. For example, if the
119// current entries are [google, digg, yahoo], with the current entry google,
120// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47121void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50122 int index,
[email protected]c12bf1a12008-09-17 16:28:49123 int count) {
[email protected]8ff00d72012-10-23 19:12:21124 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50125 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49126 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14127 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43128}
129
[email protected]e9ba4472008-09-14 15:42:43130// Configure all the NavigationEntries in entries for restore. This resets
131// the transition type to reload and makes sure the content state isn't empty.
132void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57133 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48134 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47135 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43136 // Use a transition type of reload so that we don't incorrectly increase
137 // the typed count.
Lei Zhang96031532019-10-10 19:05:47138 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
139 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43140 }
141}
142
[email protected]bf70edce2012-06-20 22:32:22143// Determines whether or not we should be carrying over a user agent override
144// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57145bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22146 return last_entry && last_entry->GetIsOverridingUserAgent();
147}
148
Camille Lamy5193caa2018-10-12 11:59:42149// Determines whether to override user agent for a navigation.
150bool ShouldOverrideUserAgent(
151 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57152 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42153 switch (override_user_agent) {
154 case NavigationController::UA_OVERRIDE_INHERIT:
155 return ShouldKeepOverride(last_committed_entry);
156 case NavigationController::UA_OVERRIDE_TRUE:
157 return true;
158 case NavigationController::UA_OVERRIDE_FALSE:
159 return false;
Camille Lamy5193caa2018-10-12 11:59:42160 }
161 NOTREACHED();
162 return false;
163}
164
Rakina Zata Amni312822d72021-06-04 16:13:37165// Returns true if this navigation should be treated as a reload. For e.g.
clamy0a656e42018-02-06 18:18:28166// navigating to the last committed url via the address bar or clicking on a
Rakina Zata Amni312822d72021-06-04 16:13:37167// link which results in a navigation to the last committed URL (but wasn't
168// converted to do a replacement navigation in the renderer), etc.
Fergal Daly766177d2020-07-07 07:54:04169// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
170// |base_url_for_data_url|, |transition_type| correspond to the new navigation
171// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
172// navigation that committed.
173bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
174 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57175 const GURL& virtual_url,
176 const GURL& base_url_for_data_url,
177 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57178 bool is_post,
Rakina Zata Amni312822d72021-06-04 16:13:37179 bool should_replace_current_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57180 NavigationEntryImpl* last_committed_entry) {
Rakina Zata Amni312822d72021-06-04 16:13:37181 // Navigations intended to do a replacement shouldn't be converted to do a
182 // reload.
183 if (should_replace_current_entry)
clamy0a656e42018-02-06 18:18:28184 return false;
clamy0a656e42018-02-06 18:18:28185 // Only convert to reload if at least one navigation committed.
Rakina Zata Amni2322f4f82022-01-24 13:24:24186 if (!last_committed_entry || last_committed_entry->IsInitialEntry())
ananta3bdd8ae2016-12-22 17:11:55187 return false;
188
arthursonzogni7a8243682017-12-14 16:41:42189 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28190 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42191 return false;
192
ananta3bdd8ae2016-12-22 17:11:55193 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
194 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
195 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28196 bool transition_type_can_be_converted = false;
197 if (ui::PageTransitionCoreTypeIs(transition_type,
198 ui::PAGE_TRANSITION_RELOAD) &&
199 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
200 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55201 }
clamy0a656e42018-02-06 18:18:28202 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55203 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28204 transition_type_can_be_converted = true;
205 }
206 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
207 transition_type_can_be_converted = true;
208 if (!transition_type_can_be_converted)
209 return false;
210
211 // This check is required for cases like view-source:, etc. Here the URL of
212 // the navigation entry would contain the url of the page, while the virtual
213 // URL contains the full URL including the view-source prefix.
214 if (virtual_url != last_committed_entry->GetVirtualURL())
215 return false;
216
Fergal Daly766177d2020-07-07 07:54:04217 // Check that the URLs match.
218 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
219 // If there's no frame entry then by definition the URLs don't match.
220 if (!frame_entry)
221 return false;
222
223 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28224 return false;
225
226 // This check is required for Android WebView loadDataWithBaseURL. Apps
227 // can pass in anything in the base URL and we need to ensure that these
228 // match before classifying it as a reload.
229 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
230 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
231 return false;
ananta3bdd8ae2016-12-22 17:11:55232 }
233
clamy0a656e42018-02-06 18:18:28234 // Skip entries with SSL errors.
235 if (last_committed_entry->ssl_error())
236 return false;
237
238 // Don't convert to a reload when the last navigation was a POST or the new
239 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04240 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28241 return false;
242
243 return true;
ananta3bdd8ae2016-12-22 17:11:55244}
245
Nasko Oskov03912102019-01-11 00:21:32246bool DoesURLMatchOriginForNavigation(
Wang Hui14cac7a2022-01-28 06:07:47247 const GURL& destination_url,
Anton Bikineevf62d1bf2021-05-15 17:56:07248 const absl::optional<url::Origin>& origin,
Wang Hui14cac7a2022-01-28 06:07:47249 SubresourceWebBundleNavigationInfo* subresource_web_bundle_navigation_info,
250 NavigationEntry* entry,
251 bool is_main_frame) {
Nasko Oskov03912102019-01-11 00:21:32252 // If there is no origin supplied there is nothing to match. This can happen
253 // for navigations to a pending entry and therefore it should be allowed.
254 if (!origin)
255 return true;
256
Wang Hui14cac7a2022-01-28 06:07:47257 // For a history navigation to a document loaded with loadDataWithBaseURL,
258 // compare the origin to the entry's base URL instead of the data: URL used to
259 // commit.
260 const GURL& base_url_for_data_url = entry->GetBaseURLForDataURL();
261 bool check_origin_against_base_url =
262 is_main_frame && !base_url_for_data_url.is_empty();
263
264 const GURL& url_for_origin =
265 check_origin_against_base_url ? base_url_for_data_url : destination_url;
Kunihiko Sakamotof32652502022-04-05 03:05:59266 // Uuid-in-package: subframe from WebBundle has an opaque origin derived from
267 // the Bundle's origin.
268 if (url_for_origin.SchemeIs(url::kUuidInPackageScheme) &&
Kunihiko Sakamoto260ee0a2021-10-15 01:44:08269 subresource_web_bundle_navigation_info) {
Kunihiko Sakamoto346a74e2021-03-10 08:57:48270 return origin->CanBeDerivedFrom(
271 subresource_web_bundle_navigation_info->bundle_url());
272 }
273
Wang Hui14cac7a2022-01-28 06:07:47274 return origin->CanBeDerivedFrom(url_for_origin);
Nasko Oskov03912102019-01-11 00:21:32275}
276
Anton Bikineevf62d1bf2021-05-15 17:56:07277absl::optional<url::Origin> GetCommittedOriginForFrameEntry(
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12278 const mojom::DidCommitProvisionalLoadParams& params,
279 NavigationRequest* request) {
Nasko Oskov03912102019-01-11 00:21:32280 // Error pages commit in an opaque origin, yet have the real URL that resulted
281 // in an error as the |params.url|. Since successful reload of an error page
282 // should commit in the correct origin, setting the opaque origin on the
283 // FrameNavigationEntry will be incorrect.
Rakina Zata Amniafd3c6582021-11-30 06:19:17284 if (request && request->DidEncounterError())
Anton Bikineevf62d1bf2021-05-15 17:56:07285 return absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:32286
Anton Bikineevf62d1bf2021-05-15 17:56:07287 return absl::make_optional(params.origin);
Nasko Oskov03912102019-01-11 00:21:32288}
289
Camille Lamy5193caa2018-10-12 11:59:42290bool IsValidURLForNavigation(bool is_main_frame,
291 const GURL& virtual_url,
292 const GURL& dest_url) {
293 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
294 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
295 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
296 << virtual_url.possibly_invalid_spec();
297 return false;
298 }
299
300 // Don't attempt to navigate to non-empty invalid URLs.
301 if (!dest_url.is_valid() && !dest_url.is_empty()) {
302 LOG(WARNING) << "Refusing to load invalid URL: "
303 << dest_url.possibly_invalid_spec();
304 return false;
305 }
306
307 // The renderer will reject IPC messages with URLs longer than
308 // this limit, so don't attempt to navigate with a longer URL.
309 if (dest_url.spec().size() > url::kMaxURLChars) {
310 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
311 << " characters.";
312 return false;
313 }
314
Aaron Colwell33109c592020-04-21 21:31:19315 // Reject renderer debug URLs because they should have been handled before
316 // we get to this point. This check handles renderer debug URLs
317 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
318 // provides defense-in-depth if a renderer debug URL manages to get here via
319 // some other path. We want to reject the navigation here so it doesn't
320 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30321 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19322 LOG(WARNING) << "Refusing to load renderer debug URL: "
323 << dest_url.possibly_invalid_spec();
324 return false;
325 }
326
Camille Lamy5193caa2018-10-12 11:59:42327 return true;
328}
329
Mikel Astizba9cf2fd2017-12-17 10:38:10330// See replaced_navigation_entry_data.h for details: this information is meant
331// to ensure |*output_entry| keeps track of its original URL (landing page in
332// case of server redirects) as it gets replaced (e.g. history.replaceState()),
333// without overwriting it later, for main frames.
334void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57335 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10336 NavigationEntryImpl* output_entry) {
Rakina Zata Amniafd3c6582021-11-30 06:19:17337 if (output_entry->GetReplacedEntryData().has_value() ||
338 replaced_entry->IsInitialEntry()) {
Mikel Astizba9cf2fd2017-12-17 10:38:10339 return;
Rakina Zata Amniafd3c6582021-11-30 06:19:17340 }
Mikel Astizba9cf2fd2017-12-17 10:38:10341
342 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57343 data.first_committed_url = replaced_entry->GetURL();
344 data.first_timestamp = replaced_entry->GetTimestamp();
345 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29346 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10347}
348
Minggang Wangb9f3fa92021-07-01 15:30:31349blink::mojom::NavigationType GetNavigationType(
350 const GURL& old_url,
351 const GURL& new_url,
352 ReloadType reload_type,
353 NavigationEntryImpl* entry,
354 const FrameNavigationEntry& frame_entry,
355 bool has_pending_cross_document_commit,
356 bool is_currently_error_page,
357 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23358 // Reload navigations
359 switch (reload_type) {
360 case ReloadType::NORMAL:
Minggang Wangb9f3fa92021-07-01 15:30:31361 return blink::mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23362 case ReloadType::BYPASSING_CACHE:
Minggang Wangb9f3fa92021-07-01 15:30:31363 return blink::mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23364 case ReloadType::ORIGINAL_REQUEST_URL:
Minggang Wangb9f3fa92021-07-01 15:30:31365 return blink::mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23366 case ReloadType::NONE:
367 break; // Fall through to rest of function.
368 }
369
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08370 if (entry->IsRestored()) {
Minggang Wangb9f3fa92021-07-01 15:30:31371 return entry->GetHasPostData()
372 ? blink::mojom::NavigationType::RESTORE_WITH_POST
373 : blink::mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23374 }
375
danakjb952ef12021-01-14 19:58:49376 const bool can_be_same_document =
377 // A pending cross-document commit means this navigation will not occur in
378 // the current document, as that document would end up being replaced in
379 // the meantime.
380 !has_pending_cross_document_commit &&
381 // If the current document is an error page, we should always treat it as
382 // a different-document navigation so that we'll attempt to load the
383 // document we're navigating to (and not stay in the current error page).
384 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12385
clamyea99ea12018-05-28 13:54:23386 // History navigations.
387 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12388 return can_be_same_document && is_same_document_history_load
Minggang Wangb9f3fa92021-07-01 15:30:31389 ? blink::mojom::NavigationType::HISTORY_SAME_DOCUMENT
390 : blink::mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23391 }
392 DCHECK(!is_same_document_history_load);
393
394 // A same-document fragment-navigation happens when the only part of the url
395 // that is modified is after the '#' character.
396 //
397 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12398 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23399 //
400 // Note: this check is only valid for navigations that are not history
401 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
402 // history navigation from 'A#foo' to 'A#bar' is not a same-document
403 // navigation, but a different-document one. This is why history navigation
404 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47405 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
406 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12407
408 // The one case where we do the wrong thing here and incorrectly choose
409 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
410 // the renderer is a frameset. All frameset navigations should be
411 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
412 // navigation would do the right thing, as it would send it to the browser and
413 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
414 // into this method). But since we can't tell that case here for browser-
415 // initiated navigations, we have to get the renderer involved. In that case
416 // the navigation would be restarted due to the renderer spending a reply of
417 // mojom::CommitResult::RestartCrossDocument.
418
419 return can_be_same_document && is_same_doc
Minggang Wangb9f3fa92021-07-01 15:30:31420 ? blink::mojom::NavigationType::SAME_DOCUMENT
421 : blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23422}
423
Camille Lamy5193caa2018-10-12 11:59:42424// Adjusts the original input URL if needed, to get the URL to actually load and
425// the virtual URL, which may differ.
426void RewriteUrlForNavigation(const GURL& original_url,
427 BrowserContext* browser_context,
428 GURL* url_to_load,
429 GURL* virtual_url,
430 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42431 // Allow the browser URL handler to rewrite the URL. This will, for example,
432 // remove "view-source:" from the beginning of the URL to get the URL that
433 // will actually be loaded. This real URL won't be shown to the user, just
434 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31435 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42436 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
437 url_to_load, browser_context, reverse_on_redirect);
438}
439
440#if DCHECK_IS_ON()
441// Helper sanity check function used in debug mode.
442void ValidateRequestMatchesEntry(NavigationRequest* request,
443 NavigationEntryImpl* entry) {
444 if (request->frame_tree_node()->IsMainFrame()) {
445 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
446 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
Minggang Wangb9f3fa92021-07-01 15:30:31447 ui::PageTransitionFromInt(request->common_params().transition),
448 entry->GetTransitionType()));
Camille Lamy5193caa2018-10-12 11:59:42449 }
Nasko Oskovc36327d2019-01-03 23:23:04450 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42451 entry->should_clear_history_list());
452 DCHECK_EQ(request->common_params().has_user_gesture,
453 entry->has_user_gesture());
454 DCHECK_EQ(request->common_params().base_url_for_data_url,
455 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04456 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42457 entry->GetCanLoadLocalResources());
458 DCHECK_EQ(request->common_params().started_from_context_menu,
459 entry->has_started_from_context_menu());
460
461 FrameNavigationEntry* frame_entry =
462 entry->GetFrameEntry(request->frame_tree_node());
463 if (!frame_entry) {
464 NOTREACHED();
465 return;
466 }
467
Camille Lamy5193caa2018-10-12 11:59:42468 DCHECK_EQ(request->common_params().method, frame_entry->method());
469
Nasko Oskovc36327d2019-01-03 23:23:04470 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42471 if (redirect_size == frame_entry->redirect_chain().size()) {
472 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04473 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42474 frame_entry->redirect_chain()[i]);
475 }
476 } else {
477 NOTREACHED();
478 }
479}
480#endif // DCHECK_IS_ON()
481
Dave Tapuska8bfd84c2019-03-26 20:47:16482// Returns whether the session history NavigationRequests in |navigations|
483// would stay within the subtree of the sandboxed iframe in
484// |sandbox_frame_tree_node_id|.
485bool DoesSandboxNavigationStayWithinSubtree(
486 int sandbox_frame_tree_node_id,
487 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
488 for (auto& item : navigations) {
489 bool within_subtree = false;
490 // Check whether this NavigationRequest affects a frame within the
491 // sandboxed frame's subtree by walking up the tree looking for the
492 // sandboxed frame.
493 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03494 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16495 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
496 within_subtree = true;
497 break;
498 }
499 }
500 if (!within_subtree)
501 return false;
502 }
503 return true;
504}
505
[email protected]e9ba4472008-09-14 15:42:43506} // namespace
507
arthursonzogni66f711c2019-10-08 14:40:36508// NavigationControllerImpl::PendingEntryRef------------------------------------
509
510NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
511 base::WeakPtr<NavigationControllerImpl> controller)
512 : controller_(controller) {}
513
514NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
515 if (!controller_) // Can be null with interstitials.
516 return;
517
518 controller_->PendingEntryRefDeleted(this);
519}
520
[email protected]d202a7c2012-01-04 07:53:47521// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29522
[email protected]23a918b2014-07-15 09:51:36523const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23524
[email protected]765b35502008-08-21 00:51:20525// static
[email protected]d202a7c2012-01-04 07:53:47526size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23527 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20528
[email protected]e6fec472013-05-14 05:29:02529// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20530// when testing.
531static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29532
[email protected]71fde352011-12-29 03:29:56533// static
dcheng9bfa5162016-04-09 01:00:57534std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
535 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10536 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07537 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10538 ui::PageTransition transition,
539 bool is_renderer_initiated,
540 const std::string& extra_headers,
541 BrowserContext* browser_context,
542 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
543 return NavigationControllerImpl::CreateNavigationEntry(
544 url, referrer, std::move(initiator_origin),
545 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Hayato Ito303654c2021-06-30 09:07:54546 extra_headers, browser_context, std::move(blob_url_loader_factory));
Lukasz Anforowicz641234d52019-11-07 21:07:10547}
548
549// static
550std::unique_ptr<NavigationEntryImpl>
551NavigationControllerImpl::CreateNavigationEntry(
552 const GURL& url,
553 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07554 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10555 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57556 ui::PageTransition transition,
557 bool is_renderer_initiated,
558 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09559 BrowserContext* browser_context,
Hayato Ito303654c2021-06-30 09:07:54560 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
Camille Lamy5193caa2018-10-12 11:59:42561 GURL url_to_load;
562 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56563 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42564 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
565 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56566
Lukasz Anforowicz641234d52019-11-07 21:07:10567 // Let the NTP override the navigation params and pretend that this is a
568 // browser-initiated, bookmark-like navigation.
569 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violetcf6ea7e2021-06-09 21:09:21570 source_site_instance, &transition, &is_renderer_initiated, &referrer,
571 &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10572
Patrick Monettef507e982019-06-19 20:18:06573 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28574 nullptr, // The site instance for tabs is sent on navigation
575 // (WebContents::GetSiteInstance).
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58576 url_to_load, referrer, initiator_origin, std::u16string(), transition,
Rakina Zata Amniafd3c6582021-11-30 06:19:17577 is_renderer_initiated, blob_url_loader_factory,
578 /* is_initial_entry = */ false);
Camille Lamy5193caa2018-10-12 11:59:42579 entry->SetVirtualURL(virtual_url);
580 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56581 entry->set_update_virtual_url_with_url(reverse_on_redirect);
582 entry->set_extra_headers(extra_headers);
Patrick Monettef507e982019-06-19 20:18:06583 return entry;
[email protected]71fde352011-12-29 03:29:56584}
585
[email protected]cdcb1dee2012-01-04 00:46:20586// static
587void NavigationController::DisablePromptOnRepost() {
588 g_check_for_repost = false;
589}
590
[email protected]c5b88d82012-10-06 17:03:33591base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
592 base::Time t) {
593 // If |t| is between the water marks, we're in a run of duplicates
594 // or just getting out of it, so increase the high-water mark to get
595 // a time that probably hasn't been used before and return it.
596 if (low_water_mark_ <= t && t <= high_water_mark_) {
Peter Kastinge5a38ed2021-10-02 03:06:35597 high_water_mark_ += base::Microseconds(1);
[email protected]c5b88d82012-10-06 17:03:33598 return high_water_mark_;
599 }
600
601 // Otherwise, we're clear of the last duplicate run, so reset the
602 // water marks.
603 low_water_mark_ = high_water_mark_ = t;
604 return t;
605}
606
ckitagawa0faa5e42020-06-17 17:30:54607NavigationControllerImpl::ScopedShowRepostDialogForTesting::
608 ScopedShowRepostDialogForTesting()
609 : was_disallowed_(g_check_for_repost) {
610 g_check_for_repost = true;
611}
612
613NavigationControllerImpl::ScopedShowRepostDialogForTesting::
614 ~ScopedShowRepostDialogForTesting() {
615 g_check_for_repost = was_disallowed_;
616}
617
[email protected]d202a7c2012-01-04 07:53:47618NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00619 BrowserContext* browser_context,
620 FrameTree& frame_tree,
621 NavigationControllerDelegate* delegate)
622 : frame_tree_(frame_tree),
623 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57624 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22625 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47626 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37627 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29628}
629
[email protected]d202a7c2012-01-04 07:53:47630NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00631 // The NavigationControllerImpl might be called inside its delegate
632 // destructor. Calling it is not valid anymore.
633 delegate_ = nullptr;
634 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29635}
636
Matt Falkenhagen548ed1562021-07-06 01:38:26637WebContents* NavigationControllerImpl::DeprecatedGetWebContents() {
638 return delegate_->DeprecatedGetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32639}
640
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57641BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55642 return browser_context_;
643}
644
[email protected]d202a7c2012-01-04 07:53:47645void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30646 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36647 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57648 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
Charlie Reis23c26da2022-01-29 00:57:47649 // Note that it's possible for `entries_` to contain multiple entries at this
650 // point, as Restore() might be called on a NavigationController that is
651 // already used (e.g. due to WebView's restoreState() API), not only for fresh
652 // NavigationControllers. These entries are not cleared to preserve legacy
653 // behavior and also because `pending_entry_` might point to one of the
654 // pre-existing entries. An exception for this is when `entries_` contains
655 // only the initial NavigationEntry, which must be removed.
656
657 // Do not proceed if selected_navigation will be out of bounds for the updated
658 // entries_ list, since it will be assigned to last_committed_entry_index_ and
659 // used to index entries_.
660 // TODO(https://crbug.com/1287624): Consider also returning early if entries
661 // is empty, since there should be no work to do (rather than marking the
662 // existing entries as needing reload). Also consider returning early if the
663 // selected index is -1, which represents a no-committed-entry state.
664 if (selected_navigation < -1 ||
665 selected_navigation >=
666 base::checked_cast<int>(entries->size() + entries_.size())) {
667 return;
Rakina Zata Amni2322f4f82022-01-24 13:24:24668 }
Charlie Reis23c26da2022-01-29 00:57:47669
670 if (blink::features::IsInitialNavigationEntryEnabled()) {
671 // In InitialNavigationEntry mode, there will always be at least one entry.
672 if (selected_navigation == -1)
673 selected_navigation = 0;
674
675 if (GetLastCommittedEntry()->IsInitialEntry() && entries->size() > 0) {
676 // If we are on the initial NavigationEntry, it must be the only entry in
677 // the list. Since it's impossible to do a history navigation to the
678 // initial NavigationEntry, `pending_entry_index_` must be -1 (but
679 // `pending_entry` might be set for a new non-history navigation).
680 // Note that we should not clear `entries_` if `entries` is empty (when
681 // InitialNavigationEntry mode is enabled), since that would leave us
682 // without any NavigationEntry.
683 CHECK_EQ(1, GetEntryCount());
684 CHECK_EQ(-1, pending_entry_index_);
685 entries_.clear();
686 }
687 }
[email protected]ce3fa3c2009-04-20 19:55:57688
[email protected]ce3fa3c2009-04-20 19:55:57689 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44690 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03691 entries_.reserve(entries->size());
Charlie Reis23c26da2022-01-29 00:57:47692 for (auto& entry : *entries) {
Rakina Zata Amni996ee412022-02-17 04:51:43693 if (entry->GetURL().is_empty()) {
694 // We're trying to restore an entry with an empty URL (e.g. from
Rakina Zata Amni2729a512022-03-16 05:54:01695 // persisting the initial NavigationEntry [which is no longer possible but
696 // some old persisted sessions might still contain it] or when the
697 // serializer failed to write the URL because it's too long). Trying to
698 // restore and navigate to an entry with an empty URL will result in
699 // crashes, so change the URL to about:blank. See also
700 // https://crbug.com/1240138 and https://crbug.com/1299335.
Rakina Zata Amni996ee412022-02-17 04:51:43701 entry->SetURL(GURL(url::kAboutBlankURL));
702 }
dcheng36b6aec92015-12-26 06:16:36703 entries_.push_back(
704 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
Charlie Reis23c26da2022-01-29 00:57:47705 }
avif16f85a72015-11-13 18:25:03706
707 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
708 // cleared out safely.
709 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57710
711 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36712 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57713}
714
toyoshim6142d96f2016-12-19 09:07:25715void NavigationControllerImpl::Reload(ReloadType reload_type,
716 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28717 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28718 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32719 int current_index = -1;
720
Carlos IL42b416592019-10-07 23:10:36721 if (entry_replaced_by_post_commit_error_) {
722 // If there is an entry that was replaced by a currently active post-commit
723 // error navigation, this can't be the initial navigation.
724 DCHECK(!IsInitialNavigation());
725 // If the current entry is a post commit error, we reload the entry it
726 // replaced instead. We leave the error entry in place until a commit
727 // replaces it, but the pending entry points to the original entry in the
728 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
729 // case that pending_entry_ != entries_[pending_entry_index_].
730 entry = entry_replaced_by_post_commit_error_.get();
731 current_index = GetCurrentEntryIndex();
732 } else if (IsInitialNavigation() && pending_entry_) {
733 // If we are reloading the initial navigation, just use the current
734 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32735 entry = pending_entry_;
736 // The pending entry might be in entries_ (e.g., after a Clone), so we
737 // should also update the current_index.
738 current_index = pending_entry_index_;
739 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00740 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32741 current_index = GetCurrentEntryIndex();
742 if (current_index != -1) {
creis3da03872015-02-20 21:12:32743 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32744 }
[email protected]979a4bc2013-04-24 01:27:15745 }
[email protected]241db352013-04-22 18:04:05746
[email protected]59167c22013-06-03 18:07:32747 // If we are no where, then we can't reload. TODO(darin): We should add a
748 // CanReload method.
749 if (!entry)
750 return;
751
Rakina Zata Amnif297a802022-01-18 03:53:43752 if (entry->IsInitialEntryNotForSynchronousAboutBlank()) {
753 // We should never navigate to an existing initial NavigationEntry that is
754 // the initial NavigationEntry for the initial empty document that hasn't
755 // been overridden by the synchronous about:blank commit, to preserve
756 // legacy behavior where trying to reload when the main frame is on the
757 // initial empty document won't result in a navigation. See also
758 // https://crbug.com/1277414.
759 return;
760 }
761
Takashi Toyoshimac7df3c22019-06-25 14:18:47762 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26763 entry->set_reload_type(reload_type);
764
Aran Gilman37d11632019-10-08 23:07:15765 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30766 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07767 // they really want to do this. If they do, the dialog will call us back
768 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57769 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02770
[email protected]106a0812010-03-18 00:15:12771 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57772 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47773 return;
initial.commit09911bf2008-07-26 23:55:29774 }
Lei Zhang96031532019-10-10 19:05:47775
776 if (!IsInitialNavigation())
777 DiscardNonCommittedEntries();
778
779 pending_entry_ = entry;
780 pending_entry_index_ = current_index;
781 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
782
Nate Chapin45f620582021-09-30 17:45:43783 // location.reload() goes through BeginNavigation, so all reloads triggered
784 // via this codepath are browser initiated.
Lei Zhang96031532019-10-10 19:05:47785 NavigateToExistingPendingEntry(reload_type,
Nate Chapin45f620582021-09-30 17:45:43786 FrameTreeNode::kFrameTreeNodeInvalidId,
787 true /* is_browser_initiated */);
initial.commit09911bf2008-07-26 23:55:29788}
789
[email protected]d202a7c2012-01-04 07:53:47790void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48791 DCHECK(pending_reload_ != ReloadType::NONE);
792 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12793}
794
[email protected]d202a7c2012-01-04 07:53:47795void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48796 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12797 NOTREACHED();
798 } else {
toyoshim6142d96f2016-12-19 09:07:25799 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48800 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12801 }
802}
803
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57804bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09805 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11806}
807
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57808bool NavigationControllerImpl::IsInitialBlankNavigation() {
Rakina Zata Amni2322f4f82022-01-24 13:24:24809 if (blink::features::IsInitialNavigationEntryEnabled()) {
810 // Check that we're on the initial NavigationEntry and that this is not a
811 // cloned tab.
812 return IsInitialNavigation() && GetEntryCount() == 1 &&
813 GetLastCommittedEntry()->IsInitialEntry() &&
814 GetLastCommittedEntry()->restore_type() == RestoreType::kNotRestored;
815 } else {
816 return IsInitialNavigation() && GetEntryCount() == 0;
817 }
creis10a4ab72015-10-13 17:22:40818}
819
Aran Gilman37d11632019-10-08 23:07:15820NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
821 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58822 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03823 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58824}
825
Adithya Srinivasan9b0c99c2021-08-10 15:19:45826NavigationEntryImpl*
827NavigationControllerImpl::GetEntryWithUniqueIDIncludingPending(
828 int nav_entry_id) const {
829 NavigationEntryImpl* entry = GetEntryWithUniqueID(nav_entry_id);
830 if (entry)
831 return entry;
832 return pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id
Keishi Hattori0e45c022021-11-27 09:25:52833 ? pending_entry_.get()
Adithya Srinivasan9b0c99c2021-08-10 15:19:45834 : nullptr;
835}
836
W. James MacLean1c40862c2020-04-27 21:05:57837void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
838 const url::Origin& origin) {
839 for (int i = 0; i < GetEntryCount(); i++) {
840 auto* entry = GetEntryAtIndex(i);
841 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
842 }
843 if (entry_replaced_by_post_commit_error_) {
844 // It's possible we could come back to this entry if the error
845 // page/interstitial goes away.
846 entry_replaced_by_post_commit_error_
847 ->RegisterExistingOriginToPreventOptInIsolation(origin);
848 }
849 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
850 // visiting pending_entry_, which lacks a committed origin. This will be done
851 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
852}
853
avi25764702015-06-23 15:43:37854void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57855 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00856 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37857 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27858 DCHECK_EQ(-1, pending_entry_index_);
[email protected]765b35502008-08-21 00:51:20859}
860
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57861NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47862 if (pending_entry_)
863 return pending_entry_;
864 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20865}
866
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57867NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32868 // The pending entry is safe to return for new (non-history), browser-
869 // initiated navigations. Most renderer-initiated navigations should not
870 // show the pending entry, to prevent URL spoof attacks.
871 //
872 // We make an exception for renderer-initiated navigations in new tabs, as
873 // long as no other page has tried to access the initial empty document in
874 // the new tab. If another page modifies this blank page, a URL spoof is
875 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32876 bool safe_to_show_pending =
877 pending_entry_ &&
878 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09879 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32880 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46881 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32882
883 // Also allow showing the pending entry for history navigations in a new tab,
884 // such as Ctrl+Back. In this case, no existing page is visible and no one
885 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15886 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
887 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32888 safe_to_show_pending = true;
889
890 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19891 return pending_entry_;
892 return GetLastCommittedEntry();
893}
894
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57895int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20896 if (pending_entry_index_ != -1)
897 return pending_entry_index_;
898 return last_committed_entry_index_;
899}
900
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57901NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
Rakina Zata Amni2322f4f82022-01-24 13:24:24902 if (last_committed_entry_index_ == -1) {
903 // The last committed entry must always exist when InitialNavigationEntry
904 // is enabled. TODO(https://crbug.com/524208): Remove this case and all
905 // related nullchecks entirely.
906 DCHECK(!blink::features::IsInitialNavigationEntryEnabled());
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28907 return nullptr;
Rakina Zata Amni2322f4f82022-01-24 13:24:24908 }
avif16f85a72015-11-13 18:25:03909 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20910}
911
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57912bool NavigationControllerImpl::CanViewSource() {
Jeremy Roman2d8dfe132021-07-06 20:51:26913 const std::string& mime_type =
914 frame_tree_.root()->current_frame_host()->GetPage().contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53915 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
916 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27917 NavigationEntry* visible_entry = GetVisibleEntry();
918 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39919 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16920}
921
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57922int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27923 // The last committed entry index must always be less than the number of
Rakina Zata Amni2322f4f82022-01-24 13:24:24924 // entries. If there are no entries, it must be -1.
arthursonzogni5c4c202d2017-04-25 23:41:27925 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
Rakina Zata Amni2322f4f82022-01-24 13:24:24926 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55927 return last_committed_entry_index_;
928}
929
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57930int NavigationControllerImpl::GetEntryCount() {
Carlos IL4dea8902020-05-26 15:14:29931 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55932 return static_cast<int>(entries_.size());
933}
934
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57935NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37936 if (index < 0 || index >= GetEntryCount())
937 return nullptr;
938
avif16f85a72015-11-13 18:25:03939 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25940}
941
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57942NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47943 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20944}
945
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57946int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46947 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03948}
949
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57950bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03951 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
952 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
953 return true;
954 }
955 return false;
[email protected]765b35502008-08-21 00:51:20956}
957
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57958bool NavigationControllerImpl::CanGoForward() {
WangHui74286d52021-03-31 16:17:15959 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
960 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
961 return true;
962 }
963 return false;
[email protected]765b35502008-08-21 00:51:20964}
965
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57966bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03967 int index = GetIndexForOffset(offset);
968 return index >= 0 && index < GetEntryCount();
969}
970
Xiaohan Wang7f8052e02022-01-14 18:44:28971#if BUILDFLAG(IS_ANDROID)
WangHui74286d52021-03-31 16:17:15972bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
WangHui74286d52021-03-31 16:17:15973 if (offset == 0)
974 return true;
975 int increment = offset > 0 ? 1 : -1;
976 int non_skippable_entries = 0;
977 for (int index = GetIndexForOffset(increment);
978 index >= 0 && index < GetEntryCount(); index += increment) {
979 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
980 non_skippable_entries++;
981
982 if (non_skippable_entries == std::abs(offset))
983 return true;
984 }
985 return false;
986}
987#endif
988
[email protected]d202a7c2012-01-04 07:53:47989void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06990 int target_index = GetIndexForOffset(-1);
991
Elly Fong-Jones40ee8112021-06-23 19:10:52992 // Move the target index past the skippable entries.
Shivani Sharma298d12852019-01-22 20:04:03993 bool all_skippable_entries = true;
Elly Fong-Jones40ee8112021-06-23 19:10:52994 while (target_index >= 0) {
995 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui()) {
Shivani Sharma298d12852019-01-22 20:04:03996 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06997 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16998 }
Elly Fong-Jones40ee8112021-06-23 19:10:52999 target_index--;
shivanisha55201872018-12-13 04:29:061000 }
Miyoung Shin1c565c912021-03-17 12:11:211001
Shivani Sharma298d12852019-01-22 20:04:031002 // Do nothing if all entries are skippable. Normally this path would not
1003 // happen as consumers would have already checked it in CanGoBack but a lot of
1004 // tests do not do that.
Elly Fong-Jonesccc6d1f2021-06-14 18:32:421005 if (all_skippable_entries)
Shivani Sharma298d12852019-01-22 20:04:031006 return;
shivanisha55201872018-12-13 04:29:061007
shivanisha55201872018-12-13 04:29:061008 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:201009}
1010
[email protected]d202a7c2012-01-04 07:53:471011void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:061012 int target_index = GetIndexForOffset(1);
1013
Shivani Sharma2d5b4b6b2019-01-08 16:07:161014 // Note that at least one entry (the last one) will be non-skippable since
1015 // entries are marked skippable only when they add another entry because of
1016 // redirect or pushState.
Elly Fong-Jones40ee8112021-06-23 19:10:521017 while (target_index < static_cast<int>(entries_.size())) {
1018 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui())
shivanisha55201872018-12-13 04:29:061019 break;
Elly Fong-Jones40ee8112021-06-23 19:10:521020 target_index++;
shivanisha55201872018-12-13 04:29:061021 }
shivanisha55201872018-12-13 04:29:061022 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:201023}
1024
[email protected]d202a7c2012-01-04 07:53:471025void NavigationControllerImpl::GoToIndex(int index) {
Nate Chapin45f620582021-09-30 17:45:431026 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId,
1027 true /* is_browser_initiated */);
Dave Tapuska8bfd84c2019-03-26 20:47:161028}
1029
1030void NavigationControllerImpl::GoToIndex(int index,
Nate Chapin45f620582021-09-30 17:45:431031 int sandbox_frame_tree_node_id,
1032 bool is_browser_initiated) {
1033 DCHECK(sandbox_frame_tree_node_id == FrameTreeNode::kFrameTreeNodeInvalidId ||
1034 !is_browser_initiated);
sunjian30574a62017-03-21 21:39:441035 TRACE_EVENT0("browser,navigation,benchmark",
1036 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:201037 if (index < 0 || index >= static_cast<int>(entries_.size())) {
1038 NOTREACHED();
1039 return;
1040 }
1041
Rakina Zata Amnif297a802022-01-18 03:53:431042 if (entries_[index]->IsInitialEntryNotForSynchronousAboutBlank()) {
1043 // We should never navigate to an existing initial NavigationEntry that is
1044 // the initial NavigationEntry for the initial empty document that hasn't
1045 // been overridden by the synchronous about:blank commit, to preserve
1046 // legacy behavior where trying to reload when the main frame is on the
1047 // initial empty document won't result in a navigation. See also
1048 // https://crbug.com/1277414.
1049 return;
1050 }
1051
[email protected]cbab76d2008-10-13 22:42:471052 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:201053
arthursonzogni5c4c202d2017-04-25 23:41:271054 DCHECK_EQ(nullptr, pending_entry_);
1055 DCHECK_EQ(-1, pending_entry_index_);
Rakina Zata Amnif297a802022-01-18 03:53:431056
arthursonzogni5c4c202d2017-04-25 23:41:271057 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:201058 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:271059 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
1060 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Nate Chapin45f620582021-09-30 17:45:431061 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id,
1062 is_browser_initiated);
[email protected]765b35502008-08-21 00:51:201063}
1064
[email protected]d202a7c2012-01-04 07:53:471065void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:121066 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:031067 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:201068 return;
1069
[email protected]9ba14052012-06-22 23:50:031070 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:201071}
1072
Nate Chapin45f620582021-09-30 17:45:431073void NavigationControllerImpl::GoToOffsetFromRenderer(int offset) {
1074 // Note: This is actually reached in unit tests.
1075 if (!CanGoToOffset(offset))
1076 return;
1077
1078 GoToIndex(GetIndexForOffset(offset), FrameTreeNode::kFrameTreeNodeInvalidId,
1079 false /* is_browser_initiated */);
1080}
1081
Xiaohan Wang7f8052e02022-01-14 18:44:281082#if BUILDFLAG(IS_ANDROID)
WangHui74286d52021-03-31 16:17:151083void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
1084 // Note: This is actually reached in unit tests.
1085 if (!CanGoToOffsetWithSkipping(offset))
1086 return;
1087
Elly Fong-Jonesccc6d1f2021-06-14 18:32:421088 if (offset == 0) {
WangHui74286d52021-03-31 16:17:151089 GoToIndex(GetIndexForOffset(offset));
1090 return;
1091 }
1092 int increment = offset > 0 ? 1 : -1;
1093 // Find the offset without counting skippable entries.
1094 int target_index = GetIndexForOffset(increment);
1095 int non_skippable_entries = 0;
1096 for (int index = target_index; index >= 0 && index < GetEntryCount();
1097 index += increment) {
1098 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1099 non_skippable_entries++;
1100
1101 if (non_skippable_entries == std::abs(offset)) {
1102 target_index = index;
1103 break;
1104 }
1105 }
1106
1107 GoToIndex(target_index);
1108}
1109#endif
1110
[email protected]41374f12013-07-24 02:49:281111bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151112 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281113 return false;
[email protected]6a13a6c2011-12-20 21:47:121114
[email protected]43032342011-03-21 14:10:311115 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281116 return true;
[email protected]cbab76d2008-10-13 22:42:471117}
1118
Michael Thiessen9b14d512019-09-23 21:19:471119void NavigationControllerImpl::PruneForwardEntries() {
1120 DiscardNonCommittedEntries();
1121 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471122 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471123 if (num_removed <= 0)
1124 return;
1125 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1126 NotifyPrunedEntries(this, remove_start_index /* start index */,
1127 num_removed /* count */);
1128}
1129
Aran Gilman37d11632019-10-08 23:07:151130void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1131 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321132 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311133 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511134 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1135 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321136 }
1137}
1138
Harkiran Bolariaba823e42021-05-21 18:30:361139base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL(
1140 const GURL& url,
1141 const Referrer& referrer,
1142 ui::PageTransition transition,
1143 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471144 LoadURLParams params(url);
1145 params.referrer = referrer;
1146 params.transition_type = transition;
1147 params.extra_headers = extra_headers;
Harkiran Bolariaba823e42021-05-21 18:30:361148 return LoadURLWithParams(params);
[email protected]cf002332012-08-14 19:17:471149}
1150
Harkiran Bolariaba823e42021-05-21 18:30:361151base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams(
1152 const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061153 if (params.is_renderer_initiated)
1154 DCHECK(params.initiator_origin.has_value());
1155
naskob8744d22014-08-28 17:07:431156 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151157 "NavigationControllerImpl::LoadURLWithParams", "url",
1158 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291159 bool is_explicit_navigation =
1160 GetContentClient()->browser()->IsExplicitNavigation(
1161 params.transition_type);
1162 if (HandleDebugURL(params.url, params.transition_type,
1163 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431164 // If Telemetry is running, allow the URL load to proceed as if it's
1165 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491166 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431167 cc::switches::kEnableGpuBenchmarking))
Harkiran Bolariaba823e42021-05-21 18:30:361168 return nullptr;
[email protected]47752982014-07-29 08:01:431169 }
[email protected]8bf1048012012-02-08 01:22:181170
[email protected]cf002332012-08-14 19:17:471171 // Checks based on params.load_type.
1172 switch (params.load_type) {
1173 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431174 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471175 break;
1176 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261177 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471178 NOTREACHED() << "Data load must use data scheme.";
Harkiran Bolariaba823e42021-05-21 18:30:361179 return nullptr;
[email protected]cf002332012-08-14 19:17:471180 }
1181 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461182 }
[email protected]e47ae9472011-10-13 19:48:341183
[email protected]e47ae9472011-10-13 19:48:341184 // The user initiated a load, we don't need to reload anymore.
1185 needs_reload_ = false;
1186
Harkiran Bolariaba823e42021-05-21 18:30:361187 return NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441188}
1189
Mohamed Abdelhalim833de902019-09-16 17:41:451190bool NavigationControllerImpl::PendingEntryMatchesRequest(
1191 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191192 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451193 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191194}
1195
[email protected]d202a7c2012-01-04 07:53:471196bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321197 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071198 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331199 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441200 bool is_same_document_navigation,
Nate Chapinc7019dd7d2021-06-25 18:29:251201 bool was_on_initial_empty_document,
Shivani Sharmaffb32b82019-04-09 16:58:471202 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131203 NavigationRequest* navigation_request) {
1204 DCHECK(navigation_request);
Chris Bookholt27faf8d2022-01-20 01:03:331205
1206 // Note: validation checks and renderer kills due to invalid commit messages
1207 // must happen before getting here, in
1208 // RenderFrameHostImpl::ValidateDidCommitParams. By the time we get here, some
1209 // effects of the navigation have already occurred.
1210
[email protected]cd2e15742013-03-08 04:08:311211 is_initial_navigation_ = false;
1212
[email protected]0e8db942008-09-24 21:21:481213 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431214 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481215 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361216 if (entry_replaced_by_post_commit_error_) {
Chris Bookholt27faf8d2022-01-20 01:03:331217 // Same document navigation events with a post-commit error should already
1218 // be blocked by RenderFrameHostImpl::ValidateDidCommitParams() before
1219 // reaching here.
1220 CHECK(!is_same_document_navigation);
1221
Carlos IL42b416592019-10-07 23:10:361222 // Any commit while a post-commit error page is showing should put the
1223 // original entry back, replacing the error page's entry. This includes
1224 // reloads, where the original entry was used as the pending entry and
1225 // should now be at the correct index at commit time.
1226 entries_[last_committed_entry_index_] =
1227 std::move(entry_replaced_by_post_commit_error_);
1228 }
Fergal Daly8e33cf62020-12-12 01:06:071229 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551230 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431231 if (pending_entry_ &&
1232 pending_entry_->GetIsOverridingUserAgent() !=
1233 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1234 overriding_user_agent_changed = true;
Xiaohan Wang7f8052e02022-01-14 18:44:281235#if BUILDFLAG(IS_ANDROID)
Shu Yang5756fa422021-11-05 23:02:541236 // TODO(crbug.com/1266277): Clean up the logic of setting
1237 // |overriding_user_agent_changed| post-launch.
Shu Yang9eabbb902021-11-17 00:45:031238 if (base::FeatureList::IsEnabled(features::kRequestDesktopSiteExceptions)) {
Shu Yang5756fa422021-11-05 23:02:541239 // Must honor user agent overrides in the |navigation_request|, such as
1240 // from things like RequestDesktopSiteWebContentsObserverAndroid. As a
1241 // result, besides comparing |pending_entry_|'s user agent against
1242 // LastCommittedEntry's, also need to compare |navigation_request|'s user
1243 // agent against LastCommittedEntry's.
1244 if (navigation_request->is_overriding_user_agent() !=
1245 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
1246 overriding_user_agent_changed = true;
1247 }
1248 }
Xiaohan Wang7f8052e02022-01-14 18:44:281249#endif // BUILDFLAG(IS_ANDROID)
[email protected]0e8db942008-09-24 21:21:481250 } else {
Gang Wu325f03f42021-02-25 20:00:461251 // GetLastCommittedEntry() is null, so this is the first entry.
Fergal Daly8e33cf62020-12-12 01:06:071252 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481253 details->previous_entry_index = -1;
Gang Wu325f03f42021-02-25 20:00:461254 if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) {
1255 // Default setting is NOT override the user agent, so overriding the user
1256 // agent in first entry should be considered as user agent changed as
1257 // well.
1258 overriding_user_agent_changed = true;
1259 }
Xiaohan Wang7f8052e02022-01-14 18:44:281260#if BUILDFLAG(IS_ANDROID)
Shu Yang5756fa422021-11-05 23:02:541261 // TODO(crbug.com/1266277): Clean up the logic of setting
1262 // |overriding_user_agent_changed| post-launch.
Shu Yang9eabbb902021-11-17 00:45:031263 if (base::FeatureList::IsEnabled(features::kRequestDesktopSiteExceptions)) {
Shu Yang5756fa422021-11-05 23:02:541264 // Must honor user agent overrides in the |navigation_request|, such as
1265 // from things like RequestDesktopSiteWebContentsObserverAndroid. As a
1266 // result, besides checking |pending_entry_|'s user agent, also need to
1267 // check |navigation_request|'s.
1268 if (navigation_request->is_overriding_user_agent()) {
1269 overriding_user_agent_changed = true;
1270 }
1271 }
Xiaohan Wang7f8052e02022-01-14 18:44:281272#endif // BUILDFLAG(IS_ANDROID)
[email protected]0e8db942008-09-24 21:21:481273 }
[email protected]ecd9d8702008-08-28 22:10:171274
Dave Tapuskaa2ab4f252021-07-08 21:31:281275 bool is_main_frame_navigation = !rfh->GetParent();
1276
Alexander Timind2f2e4f22019-04-02 20:04:531277 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1278 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281279 // For primary frame tree navigations, choose an appropriate
1280 // BackForwardCacheMetrics to be associated with the NavigationEntry, by
1281 // either creating a new object or reusing the previous one depending on
1282 // whether it's a main frame navigation or not.
1283 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics;
1284 if (navigation_request->frame_tree_node()->frame_tree()->type() ==
1285 FrameTree::Type::kPrimary) {
1286 back_forward_cache_metrics =
1287 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1288 GetLastCommittedEntry(), is_main_frame_navigation,
1289 params.document_sequence_number);
1290 }
Alexander Timind2f2e4f22019-04-02 20:04:531291 // Notify the last active entry that we have navigated away.
Dave Tapuskaa2ab4f252021-07-08 21:31:281292 if (is_main_frame_navigation && !is_same_document_navigation) {
Alexander Timind2f2e4f22019-04-02 20:04:531293 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1294 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Hajime Hoshic7606502021-04-14 02:42:161295 metrics->MainFrameDidNavigateAwayFromDocument(rfh, navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531296 }
1297 }
1298 }
1299
fdegans9caf66a2015-07-30 21:10:421300 // If there is a pending entry at this point, it should have a SiteInstance,
1301 // except for restored entries.
jam48cea9082017-02-15 06:13:291302 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481303 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081304 pending_entry_->IsRestored());
1305 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151306 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291307 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481308 }
[email protected]e9ba4472008-09-14 15:42:431309
Nasko Oskovaee2f862018-06-15 00:05:521310 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1311 // has changed, this must be treated as a new navigation with replacement.
1312 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491313 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521314 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101315 pending_entry_->GetUniqueID() ==
1316 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521317 pending_entry_->site_instance() &&
1318 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1319 DCHECK_NE(-1, pending_entry_index_);
1320 // TODO(nasko,creis): Instead of setting this value here, set
1321 // should_replace_current_entry on the parameters we send to the
1322 // renderer process as part of CommitNavigation. The renderer should
1323 // in turn send it back here as part of |params| and it can be just
1324 // enforced and renderer process terminated on mismatch.
1325 details->did_replace_entry = true;
1326 } else {
1327 // The renderer tells us whether the navigation replaces the current entry.
1328 details->did_replace_entry = params.should_replace_current_entry;
1329 }
[email protected]bcd904482012-02-01 01:54:221330
[email protected]e9ba4472008-09-14 15:42:431331 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amni2322f4f82022-01-24 13:24:241332 details->type = ClassifyNavigation(rfh, params, navigation_request);
shivanigithub189833f2022-04-27 18:08:451333
1334 if (ShouldMaintainTrivialSessionHistory(rfh->frame_tree_node()) &&
1335 GetLastCommittedEntry()) {
1336 // Ensure that this navigation does not add a navigation entry, since
1337 // ShouldMaintainTrivialSessionHistory() means we should not add an entry
1338 // beyond the last committed one. Therefore, `should_replace_current_entry`
1339 // should be set, which replaces the current entry, or this should be a
1340 // reload, which does not create a new entry.
1341 // In shadowDOM fenced frames, on a history/tab-restore navigation, any
1342 // navigation that is restored will not be creating a new entry anyways, so
1343 // exclude that case by checking NAVIGATION_TYPE_AUTO_SUBFRAME.
1344 // TODO(crbug.com/1319919): Consider adjusting the dcheck for more cases as
1345 // pointed out in the issue.
1346 DCHECK(params.should_replace_current_entry ||
1347 navigation_request->GetReloadType() != ReloadType::NONE ||
1348 details->type == NAVIGATION_TYPE_AUTO_SUBFRAME);
1349 }
1350
Rakina Zata Amni2322f4f82022-01-24 13:24:241351 if (GetLastCommittedEntry() && GetLastCommittedEntry()->IsInitialEntry()) {
Rakina Zata Amniddf10502022-01-15 02:56:551352 if (rfh->GetParent()) {
Rakina Zata Amni2322f4f82022-01-24 13:24:241353 // This is a subframe navigation on the initial empty document, which used
1354 // to not have a NavigationEntry to attach to. Now it can attach to the
1355 // initial NavigationEntry, and we must ensure that its NavigationEntry
1356 // will keep the "initial NavigationEntry" status and won't append a new
1357 // NavigationEntry (it should always do replacement instead).
1358 // See also https://crbug.com/1277414.
1359 details->should_stay_as_initial_entry = true;
Rakina Zata Amniddf10502022-01-15 02:56:551360 // Subframe navigation on initial NavigationEntry must not append a new
1361 // NavigationEntry (i.e. should not be classified as NEW_SUBFRAME). This
1362 // means every subframe navigation that happens while we're on the initial
1363 // NavigationEntry will always reuse the existing NavigationEntry and
1364 // just update the corresponding FrameNavigationEntry.
1365 DCHECK_EQ(details->type, NAVIGATION_TYPE_AUTO_SUBFRAME);
1366 } else if (details->type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY) {
1367 // This is a navigation that modifies the initial NavigationEntry, either
1368 // for a replacement or a reload. The initial NavigationEntry should
1369 // retain its "initial NavigationEntry" status in this case.
1370 details->should_stay_as_initial_entry = true;
Rakina Zata Amni2322f4f82022-01-24 13:24:241371 } else if (navigation_request->is_synchronous_renderer_commit() &&
1372 !navigation_request->IsSameDocument() && !rfh->GetParent() &&
1373 params.should_replace_current_entry) {
1374 // This is a synchronous about:blank navigation on the main frame, which
1375 // used to not create a NavigationEntry when we have no NavigationEntry on
1376 // FrameTree creation. We now have the initial NavigationEntry and are on
1377 // the initial NavigationEntry. To preserve old behavior, we should still
1378 // keep the "initial" status for the new NavigationEntry that we will
1379 // create for this navigation, so that subframe navigations under the
1380 // synchronously committed about:blank document will never append new
1381 // NavigationEntry, and instead will just reuse the initial
1382 // NavigationEntry and modify the corresponding FrameNavigationEntries.
1383 // See also https://crbug.com/1277414.
1384 details->should_stay_as_initial_entry = true;
Rakina Zata Amniddf10502022-01-15 02:56:551385 }
1386 }
1387 DCHECK(!details->should_stay_as_initial_entry ||
1388 GetLastCommittedEntry()->IsInitialEntry());
[email protected]4bf3522c2010-08-19 21:00:201389
eugenebutee08663a2017-04-27 17:43:121390 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441391 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121392
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071393 details->is_prerender_activation =
1394 navigation_request->IsPrerenderedPageActivation();
1395
Peter Boströmd7592132019-01-30 04:50:311396 // Make sure we do not discard the pending entry for a different ongoing
1397 // navigation when a same document commit comes in unexpectedly from the
1398 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1399 // other navigation types. See https://crbug.com/900036.
1400 // TODO(crbug.com/926009): Handle history.pushState() as well.
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061401 bool keep_pending_entry =
1402 is_same_document_navigation &&
1403 details->type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY &&
1404 pending_entry_ && !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311405
[email protected]0e8db942008-09-24 21:21:481406 switch (details->type) {
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061407 case NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY:
Charlie Reisc0f17d2d2021-01-12 18:52:491408 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051409 rfh, params, details->is_same_document, details->did_replace_entry,
Rakina Zata Amnia4e27222021-12-22 01:05:001410 previous_document_was_activated, navigation_request, details);
[email protected]e9ba4472008-09-14 15:42:431411 break;
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061412 case NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY:
Charlie Reisc0f17d2d2021-01-12 18:52:491413 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1414 was_restored, navigation_request,
Rakina Zata Amnia4e27222021-12-22 01:05:001415 keep_pending_entry, details);
[email protected]e9ba4472008-09-14 15:42:431416 break;
[email protected]8ff00d72012-10-23 19:12:211417 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471418 RendererDidNavigateNewSubframe(
1419 rfh, params, details->is_same_document, details->did_replace_entry,
Rakina Zata Amnia4e27222021-12-22 01:05:001420 previous_document_was_activated, navigation_request, details);
[email protected]e9ba4472008-09-14 15:42:431421 break;
[email protected]8ff00d72012-10-23 19:12:211422 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391423 if (!RendererDidNavigateAutoSubframe(
Nate Chapinc7019dd7d2021-06-25 18:29:251424 rfh, params, details->is_same_document,
Rakina Zata Amnia4e27222021-12-22 01:05:001425 was_on_initial_empty_document, navigation_request, details)) {
creisce0ef3572017-01-26 17:53:081426 // We don't send a notification about auto-subframe PageState during
1427 // UpdateStateForFrame, since it looks like nothing has changed. Send
1428 // it here at commit time instead.
1429 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431430 return false;
creis59d5a47cb2016-08-24 23:57:191431 }
[email protected]e9ba4472008-09-14 15:42:431432 break;
Rakina Zata Amni2322f4f82022-01-24 13:24:241433 case NAVIGATION_TYPE_NAV_IGNORE:
1434 // If a pending navigation was in progress, this canceled it. We should
1435 // discard it and make sure it is removed from the URL bar. After that,
1436 // there is nothing we can do with this navigation, so we just return to
1437 // the caller that nothing has happened.
1438 if (pending_entry_)
1439 DiscardNonCommittedEntries();
1440 return false;
Aran Gilman37d11632019-10-08 23:07:151441 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431442 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151443 break;
[email protected]765b35502008-08-21 00:51:201444 }
1445
[email protected]688aa65c62012-09-28 04:32:221446 // At this point, we know that the navigation has just completed, so
1447 // record the time.
1448 //
1449 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261450 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331451 base::Time timestamp =
1452 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1453 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131454 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221455
Peter Boströmd7592132019-01-30 04:50:311456 // If we aren't keeping the pending entry, there shouldn't be one at this
1457 // point. Clear it again in case any error cases above forgot to do so.
1458 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1459 // been cleared instead of protecting against it.
1460 if (!keep_pending_entry)
Rakina Zata Amniddf10502022-01-15 02:56:551461 DiscardNonCommittedEntriesWithCommitDetails(details);
[email protected]f233e4232013-02-23 00:55:141462
[email protected]e9ba4472008-09-14 15:42:431463 // All committed entries should have nonempty content state so WebKit doesn't
1464 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471465 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321466 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221467 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441468 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531469 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1470 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281471 if (back_forward_cache_metrics &&
1472 !active_entry->back_forward_cache_metrics()) {
Alexander Timind2f2e4f22019-04-02 20:04:531473 active_entry->set_back_forward_cache_metrics(
1474 std::move(back_forward_cache_metrics));
1475 }
Dave Tapuskaa2ab4f252021-07-08 21:31:281476
1477 // `back_forward_cache_metrics()` may return null as we do not record
1478 // back-forward cache metrics for navigations in non-primary frame trees.
1479 if (active_entry->back_forward_cache_metrics()) {
1480 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
1481 navigation_request,
1482 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
1483 }
naskoc7533512016-05-06 17:01:121484
Charles Reisc0507202017-09-21 00:40:021485 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1486 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1487 // A mismatch can occur if the renderer lies or due to a unique name collision
1488 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121489 FrameNavigationEntry* frame_entry =
1490 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021491 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1492 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031493 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081494 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1495 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031496 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201497
1498 // Remember the bindings the renderer process has at this point, so that
1499 // we do not grant this entry additional bindings if we come back to it.
1500 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301501 }
[email protected]132e281a2012-07-31 18:32:441502
[email protected]97d8f0d2013-10-29 16:49:211503 // Once it is committed, we no longer need to track several pieces of state on
1504 // the entry.
naskoc7533512016-05-06 17:01:121505 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131506
[email protected]49bd30e62011-03-22 20:12:591507 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221508 // TODO(creis): This check won't pass for subframes until we create entries
1509 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001510 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421511 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591512
[email protected]e9ba4472008-09-14 15:42:431513 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001514 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001515 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311516 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531517
arthursonzogni7ddc6542021-04-09 09:16:501518 active_entry->SetIsOverridingUserAgent(
1519 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031520
[email protected]93f230e02011-06-01 14:40:001521 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291522
aelias100c9192017-01-13 00:01:431523 if (overriding_user_agent_changed)
1524 delegate_->UpdateOverridingUserAgent();
1525
creis03b48002015-11-04 00:54:561526 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1527 // one knows the latest NavigationEntry it is showing (whether it has
1528 // committed anything in this navigation or not). This allows things like
1529 // state and title updates from RenderFrames to apply to the latest relevant
1530 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381531 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001532 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381533 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
Hayato Ito2c8c08d02021-06-23 03:38:431534
1535 if (navigation_request->IsPrerenderedPageActivation()) {
1536 BroadcastHistoryOffsetAndLength();
1537 // TODO(crbug.com/1222893): Broadcasting happens after the prerendered page
1538 // is activated. As a result, a "prerenderingchange" event listener sees the
1539 // history.length which is not updated yet. We should guarantee that
1540 // history's length and offset should be updated before a
1541 // "prerenderingchange" event listener runs. One possible approach is to use
1542 // the same IPC which "prerenderingchange" uses, and propagate history's
1543 // length and offset together with that.
1544 }
1545
[email protected]e9ba4472008-09-14 15:42:431546 return true;
initial.commit09911bf2008-07-26 23:55:291547}
1548
[email protected]8ff00d72012-10-23 19:12:211549NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321550 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101551 const mojom::DidCommitProvisionalLoadParams& params,
Rakina Zata Amni2322f4f82022-01-24 13:24:241552 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591553 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511554 "ClassifyNavigation");
1555
avi7c6f35e2015-05-08 17:52:381556 if (params.did_create_new_entry) {
Rakina Zata Amni2322f4f82022-01-24 13:24:241557 // A new entry. We may or may not have a corresponding pending entry, and
1558 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001559 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491560 trace_return.set_return_reason("new entry, no parent, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061561 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381562 }
1563
Rakina Zata Amni2322f4f82022-01-24 13:24:241564 // When this is a new subframe navigation, we should have a committed page
1565 // in which it's a subframe. This may not be the case when an iframe is
1566 // navigated on a popup navigated to about:blank (the iframe would be
1567 // written into the popup by script on the main page). For these cases,
1568 // there isn't any navigation stuff we can do, so just ignore it.
1569 if (!GetLastCommittedEntry()) {
1570 trace_return.set_return_reason("new entry, no last committed, ignore");
1571 return NAVIGATION_TYPE_NAV_IGNORE;
1572 }
1573
1574 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511575 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381576 return NAVIGATION_TYPE_NEW_SUBFRAME;
1577 }
1578
Charlie Reisc0f17d2d2021-01-12 18:52:491579 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381580 DCHECK(!params.history_list_was_cleared);
1581
avi39c1edd32015-06-04 20:06:001582 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381583 // All manual subframes would be did_create_new_entry and handled above, so
1584 // we know this is auto.
Rakina Zata Amni2322f4f82022-01-24 13:24:241585 if (GetLastCommittedEntry()) {
1586 trace_return.set_return_reason("subframe, last commmited, auto subframe");
1587 return NAVIGATION_TYPE_AUTO_SUBFRAME;
1588 }
1589
1590 // We ignore subframes created in non-committed pages; we'd appreciate if
1591 // people stopped doing that.
1592 trace_return.set_return_reason("subframe, no last commmited, ignore");
1593 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381594 }
1595
Rakina Zata Amnif6950d552020-11-24 03:26:101596 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1597 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381598 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1599 // create a new page.
1600
Rakina Zata Amni2322f4f82022-01-24 13:24:241601 // Just like above in the did_create_new_entry case, it's possible to
1602 // scribble onto an uncommitted page. Again, there isn't any navigation
1603 // stuff that we can do, so ignore it here as well.
1604 NavigationEntry* last_committed = GetLastCommittedEntry();
1605 if (!last_committed) {
1606 trace_return.set_return_reason("nav entry 0, no last committed, ignore");
1607 return NAVIGATION_TYPE_NAV_IGNORE;
1608 }
1609
Hayato Ito2ae49442021-07-02 02:59:251610 // This main frame navigation is not a history navigation (since
1611 // nav_entry_id is 0), but didn't create a new entry. So this must be a
1612 // reload or a replacement navigation, which will modify the existing entry.
1613 //
Nasko Oskov332593c2018-08-16 17:21:341614 // TODO(nasko): With error page isolation, reloading an existing session
1615 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491616 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341617 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511618 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491619 "nav entry 0, last committed, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061620 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381621 }
1622
Rakina Zata Amnif6950d552020-11-24 03:26:101623 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521624 // If the SiteInstance of the |pending_entry_| does not match the
1625 // SiteInstance that got committed, treat this as a new navigation with
1626 // replacement. This can happen if back/forward/reload encounters a server
1627 // redirect to a different site or an isolated error page gets successfully
1628 // reloaded into a different SiteInstance.
1629 if (pending_entry_->site_instance() &&
1630 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491631 trace_return.set_return_reason("pending matching nav entry, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061632 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521633 }
creis77c9aa32015-09-25 19:59:421634
Nasko Oskovaee2f862018-06-15 00:05:521635 if (pending_entry_index_ == -1) {
1636 // In this case, we have a pending entry for a load of a new URL but Blink
1637 // didn't do a new navigation (params.did_create_new_entry). First check
1638 // to make sure Blink didn't treat a new cross-process navigation as
1639 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161640 // we must treat it as NEW rather than the converted reload case below,
1641 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521642 if (!GetLastCommittedEntry() ||
1643 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161644 trace_return.set_return_reason("new pending, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061645 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521646 }
1647
1648 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161649 // We will create a pending entry, but NavigateWithoutEntry will convert
1650 // it to a reload since it's the same page and not create a new entry for
1651 // it. (The user doesn't want to have a new back/forward entry when they
1652 // do this.) Therefore we want to just ignore the pending entry and go
1653 // back to where we were (the "existing entry").
1654 trace_return.set_return_reason("new pending, existing (same) entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061655 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521656 }
avi7c6f35e2015-05-08 17:52:381657 }
1658
creis26d22632017-04-21 20:23:561659 // Everything below here is assumed to be an existing entry, but if there is
1660 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511661 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491662 trace_return.set_return_reason("no last committed, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061663 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511664 }
creis26d22632017-04-21 20:23:561665
Rakina Zata Amni153d5702021-09-13 22:48:001666 if (navigation_request->commit_params().intended_as_new_entry) {
avi7c6f35e2015-05-08 17:52:381667 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491668 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1669 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161670 trace_return.set_return_reason("intended as new entry, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061671 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381672 }
1673
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121674 if (navigation_request->DidEncounterError() &&
1675 failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101676 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381677 // If the renderer was going to a new pending entry that got cleared because
1678 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491679 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381680 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511681 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491682 "unreachable, matching pending, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061683 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381684 }
1685
Charlie Reisc0f17d2d2021-01-12 18:52:491686 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101687 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511688 trace_return.traced_value()->SetInteger("existing_entry_index",
1689 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381690 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441691 // The renderer has committed a navigation to an entry that no longer
1692 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491693 trace_return.set_return_reason("existing entry -1, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061694 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381695 }
1696
avi7c6f35e2015-05-08 17:52:381697 // Since we weeded out "new" navigations above, we know this is an existing
1698 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491699 trace_return.set_return_reason("default return, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061700 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381701}
1702
Rakina Zata Amni3460d382021-10-29 00:43:371703void NavigationControllerImpl::UpdateNavigationEntryDetails(
1704 NavigationEntryImpl* entry,
1705 RenderFrameHostImpl* rfh,
1706 const mojom::DidCommitProvisionalLoadParams& params,
1707 NavigationRequest* request,
1708 NavigationEntryImpl::UpdatePolicy update_policy,
Rakina Zata Amnia4e27222021-12-22 01:05:001709 bool is_new_entry,
1710 LoadCommittedDetails* commit_details) {
Rakina Zata Amni3460d382021-10-29 00:43:371711 // Update the FrameNavigationEntry.
Rakina Zata Amniafd3c6582021-11-30 06:19:171712 std::vector<GURL> redirects;
Rakina Zata Amni3460d382021-10-29 00:43:371713 entry->AddOrUpdateFrameEntry(
1714 rfh->frame_tree_node(), update_policy, params.item_sequence_number,
Domenic Denicolacc094fb2022-03-16 23:40:571715 params.document_sequence_number, params.navigation_api_key,
Rakina Zata Amni3460d382021-10-29 00:43:371716 rfh->GetSiteInstance(), nullptr, params.url,
1717 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amniafd3c6582021-11-30 06:19:171718 Referrer(*params.referrer),
1719 request ? request->common_params().initiator_origin : params.origin,
1720 request ? request->GetRedirectChain() : redirects, params.page_state,
1721 params.method, params.post_id, nullptr /* blob_url_loader_factory */,
1722 (request && request->web_bundle_navigation_info())
Rakina Zata Amni3460d382021-10-29 00:43:371723 ? request->web_bundle_navigation_info()->Clone()
1724 : nullptr,
Rakina Zata Amniafd3c6582021-11-30 06:19:171725 (request ? request->GetSubresourceWebBundleNavigationInfo() : nullptr),
Rakina Zata Amni3460d382021-10-29 00:43:371726 ComputePolicyContainerPoliciesForFrameEntry(
Rakina Zata Amniafd3c6582021-11-30 06:19:171727 rfh, request && request->IsSameDocument(),
1728 request ? request->common_params().url : params.url));
Rakina Zata Amni3460d382021-10-29 00:43:371729
1730 if (rfh->GetParent()) {
1731 // Only modify the NavigationEntry for main frame navigations.
1732 return;
1733 }
1734 if (entry->update_virtual_url_with_url())
1735 UpdateVirtualURLToURL(entry, params.url);
1736 // Don't use the page type from the pending entry. Some interstitial page
1737 // may have set the type to interstitial. Once we commit, however, the page
1738 // type must always be normal or error.
Rakina Zata Amniafd3c6582021-11-30 06:19:171739 entry->set_page_type((request && request->DidEncounterError())
1740 ? PAGE_TYPE_ERROR
1741 : PAGE_TYPE_NORMAL);
Rakina Zata Amnif297a802022-01-18 03:53:431742 if (commit_details && commit_details->should_stay_as_initial_entry) {
1743 // Retain the "initial NavigationEntry" status.
1744 if (request->IsSameDocument()) {
1745 // If this is for a same-document navigation, the NavigationEntry must be
1746 // reused and should already be marked as the initial NavigationEntry.
1747 DCHECK(entry->IsInitialEntry());
1748 } else {
1749 // If this is for a cross-document navigation, it can be caused by a
1750 // renderer-initiated reload, or the synchronous about:blank commit. Mark
1751 // "for synchronous about:blank" in the latter case, and also when it is
1752 // reloading a "for synchronous about:blank" entry. Otherwise, the entry
1753 // is not for a synchronous about:blank commit.
1754 NavigationEntryImpl::InitialNavigationEntryState new_state =
1755 NavigationEntryImpl::InitialNavigationEntryState::
1756 kInitialNotForSynchronousAboutBlank;
1757 if (entry->IsInitialEntryForSynchronousAboutBlank() ||
1758 request->is_synchronous_renderer_commit()) {
1759 new_state = NavigationEntryImpl::InitialNavigationEntryState::
1760 kInitialForSynchronousAboutBlank;
1761 }
1762 entry->set_initial_navigation_entry_state(new_state);
1763 }
1764 } else if (commit_details && !commit_details->should_stay_as_initial_entry) {
1765 // Remove the "initial NavigationEntry" status.
1766 entry->set_initial_navigation_entry_state(
1767 NavigationEntryImpl::InitialNavigationEntryState::kNonInitial);
Rakina Zata Amnia4e27222021-12-22 01:05:001768 }
Rakina Zata Amniddf10502022-01-15 02:56:551769
Rakina Zata Amni3460d382021-10-29 00:43:371770 if (is_new_entry) {
1771 // Some properties of the NavigationEntry are only set when the entry is
1772 // new (we aren't reusing it).
1773 entry->SetTransitionType(params.transition);
Rakina Zata Amniafd3c6582021-11-30 06:19:171774 entry->SetOriginalRequestURL(request ? request->GetOriginalRequestURL()
1775 : GURL::EmptyGURL());
Rakina Zata Amni3460d382021-10-29 00:43:371776 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
1777 entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
1778 } else {
1779 // We are reusing the NavigationEntry. The site instance will normally be
1780 // the same except for a few cases:
1781 // 1) session restore, when no site instance will be assigned or
1782 // 2) redirect, when the site instance is reset.
1783 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
1784 entry->site_instance() == rfh->GetSiteInstance());
1785 }
1786}
1787
Rakina Zata Amniafd3c6582021-11-30 06:19:171788void NavigationControllerImpl::CreateInitialEntry() {
1789 DCHECK_EQ(entries_.size(), 0u);
1790 RenderFrameHostImpl* rfh = frame_tree_.root()->current_frame_host();
1791 auto params = mojom::DidCommitProvisionalLoadParams::New();
1792 // The initial NavigationEntry's URL is the empty URL. This preserves the old
1793 // behavior of WebContent's GetLastCommittedURL() and GetVisibleURL() from
1794 // before we have initial NavigationEntries.
1795 params->url = GURL::EmptyGURL();
1796 params->http_status_code = 0;
1797 params->url_is_unreachable = false;
1798 params->method = "GET";
1799 params->should_replace_current_entry = false;
1800 params->post_id = -1;
1801 params->embedding_token = base::UnguessableToken::Create();
1802 params->navigation_token = base::UnguessableToken::Create();
1803 params->did_create_new_entry = true;
1804 params->origin = rfh->GetLastCommittedOrigin();
1805 params->should_update_history = true;
1806 params->item_sequence_number = 0;
1807 params->document_sequence_number = 0;
1808 bool is_in_fenced_frame_tree = rfh->frame_tree_node()->IsInFencedFrameTree();
1809 params->transition = is_in_fenced_frame_tree
1810 ? ui::PAGE_TRANSITION_AUTO_SUBFRAME
1811 : ui::PAGE_TRANSITION_LINK;
1812 params->referrer = blink::mojom::Referrer::New();
1813
1814 // Create and insert the initial NavigationEntry.
1815 auto new_entry = std::make_unique<NavigationEntryImpl>(
1816 rfh->GetSiteInstance(), params->url, Referrer(*params->referrer),
1817 rfh->GetLastCommittedOrigin(), std::u16string() /* title */,
1818 ui::PAGE_TRANSITION_TYPED, false /* renderer_initiated */,
1819 nullptr /* blob_url_loader_factory */, true /* is_initial_entry */);
1820 UpdateNavigationEntryDetails(
1821 new_entry.get(), rfh, *params, nullptr /* request */,
Rakina Zata Amnia4e27222021-12-22 01:05:001822 NavigationEntryImpl::UpdatePolicy::kUpdate, true /* is_new_entry */,
1823 nullptr /* commit_details */);
Rakina Zata Amniafd3c6582021-11-30 06:19:171824
1825 InsertOrReplaceEntry(std::move(new_entry), false /* replace_entry */,
1826 false /* was_post_commit_error */,
Rakina Zata Amnia4e27222021-12-22 01:05:001827 is_in_fenced_frame_tree, nullptr /* commit_details */);
Rakina Zata Amniafd3c6582021-11-30 06:19:171828}
1829
Charlie Reisc0f17d2d2021-01-12 18:52:491830void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321831 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071832 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361833 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441834 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471835 bool previous_document_was_activated,
Rakina Zata Amnia4e27222021-12-22 01:05:001836 NavigationRequest* request,
1837 LoadCommittedDetails* commit_details) {
dcheng9bfa5162016-04-09 01:00:571838 std::unique_ptr<NavigationEntryImpl> new_entry;
Anton Bikineevf62d1bf2021-05-15 17:56:071839 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451840 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061841
creisf49dfc92016-07-26 17:05:181842 // First check if this is an in-page navigation. If so, clone the current
1843 // entry instead of looking at the pending entry, because the pending entry
1844 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361845 if (is_same_document && GetLastCommittedEntry()) {
Nate Chapin63db0d12022-01-20 22:03:301846 FrameNavigationEntry* previous_frame_entry =
1847 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
Patrick Monette50e8bd82019-06-13 22:40:451848 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481849 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Domenic Denicolacc094fb2022-03-16 23:40:571850 params.document_sequence_number, params.navigation_api_key,
Nate Chapinfbfe5af2021-06-10 17:22:081851 rfh->GetSiteInstance(), nullptr, params.url,
1852 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091853 Referrer(*params.referrer), initiator_origin,
1854 request->GetRedirectChain(), params.page_state, params.method,
1855 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391856 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481857 request->GetSubresourceWebBundleNavigationInfo(),
Antonio Sartori78a749f2020-11-30 12:03:391858 // We will set the document policies later in this function.
Nate Chapin63db0d12022-01-20 22:03:301859 nullptr /* policy_container_policies */,
Domenic Denicolacc094fb2022-03-16 23:40:571860 // Try to preserve protect_url_in_navigation_api from the previous
Nate Chapin63db0d12022-01-20 22:03:301861 // FrameNavigationEntry.
1862 previous_frame_entry &&
Domenic Denicolacc094fb2022-03-16 23:40:571863 previous_frame_entry->protect_url_in_navigation_api());
Charles Reisf44482022017-10-13 21:15:031864
creisf49dfc92016-07-26 17:05:181865 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Harkiran Bolaria081c5c22021-09-13 08:44:531866 frame_entry, true, request->frame_tree_node(), frame_tree_.root());
Mike West800532c2021-10-14 09:26:521867 if (new_entry->GetURL().DeprecatedGetOriginAsURL() !=
1868 params.url.DeprecatedGetOriginAsURL()) {
jama78746e2017-02-22 17:21:571869 // TODO(jam): we had one report of this with a URL that was redirecting to
1870 // only tildes. Until we understand that better, don't copy the cert in
1871 // this case.
1872 new_entry->GetSSL() = SSLStatus();
jama78746e2017-02-22 17:21:571873 }
creisf49dfc92016-07-26 17:05:181874
Patrick Monette50e8bd82019-06-13 22:40:451875 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1876 // that |frame_entry| should now have a reference count of exactly 2: one
1877 // due to the local variable |frame_entry|, and another due to |new_entry|
1878 // also retaining one. This should never fail, because it's the main frame.
1879 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181880 }
1881
Harkiran Bolaria59290d62021-03-17 01:53:011882 // If this is an activation navigation from a prerendered page, transfer the
1883 // new entry from an entry already created and stored in the
1884 // NavigationRequest. |new_entry| will not have been set prior to this as
1885 // |is_same_document| is mutually exclusive with
1886 // |IsPrerenderedPageActivation|.
1887 if (request->IsPrerenderedPageActivation()) {
1888 DCHECK(!is_same_document);
1889 DCHECK(!new_entry);
1890 new_entry = request->TakePrerenderNavigationEntry();
1891 DCHECK(new_entry);
1892 }
1893
Charlie Reisc0f17d2d2021-01-12 18:52:491894 // Only make a copy of the pending entry if it is appropriate for the new
1895 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451896 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041897 // 1. The SiteInstance hasn't been assigned to something else.
1898 // 2. The pending entry was intended as a new entry, rather than being a
1899 // history navigation that was interrupted by an unrelated,
1900 // renderer-initiated navigation.
1901 // TODO(csharrison): Investigate whether we can remove some of the coarser
1902 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451903 if (!new_entry && PendingEntryMatchesRequest(request) &&
1904 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341905 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431906 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351907 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431908
Camille Lamy62b826012019-02-26 09:15:471909 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451910 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
creisf49dfc92016-07-26 17:05:181911 }
1912
Charlie Reisc0f17d2d2021-01-12 18:52:491913 // For cross-document commits with no matching pending entry, create a new
1914 // entry.
creisf49dfc92016-07-26 17:05:181915 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061916 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071917 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1918 initiator_origin,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:581919 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451920 params.transition, request->IsRendererInitiated(),
Rakina Zata Amniafd3c6582021-11-30 06:19:171921 nullptr, // blob_url_loader_factory
1922 false); // is_initial_entry
[email protected]f8f93eb2012-09-25 03:06:241923
1924 // Find out whether the new entry needs to update its virtual URL on URL
1925 // change and set up the entry accordingly. This is needed to correctly
1926 // update the virtual URL when replaceState is called after a pushState.
1927 GURL url = params.url;
1928 bool needs_update = false;
Charlie Reisc0f17d2d2021-01-12 18:52:491929 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411930 // update the virtual URL based on the new URL. For example, this is needed
1931 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1932 // the URL.
Rakina Zata Amni3460d382021-10-29 00:43:371933 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1934 &url, browser_context_, &needs_update);
1935 new_entry->set_update_virtual_url_with_url(needs_update);
1936
Camille Lamy62b826012019-02-26 09:15:471937 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451938 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
[email protected]e9ba4472008-09-14 15:42:431939 }
1940
Harkiran Bolaria59290d62021-03-17 01:53:011941 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
1942 // for prerendered contents, if any. This is because prerendering/activation
1943 // technically won't be creating a new document. Unlike BFCache, prerendering
1944 // creates a new NavigationEntry rather than using an existing one.
1945 if (!request->IsPrerenderedPageActivation()) {
Rakina Zata Amni3460d382021-10-29 00:43:371946 UpdateNavigationEntryDetails(new_entry.get(), rfh, params, request,
1947 NavigationEntryImpl::UpdatePolicy::kUpdate,
Rakina Zata Amnia4e27222021-12-22 01:05:001948 true /* is_new_entry */, commit_details);
creis8b5cd4c2015-06-19 00:11:081949
Harkiran Bolaria59290d62021-03-17 01:53:011950 // history.pushState() is classified as a navigation to a new page, but sets
1951 // is_same_document to true. In this case, we already have the title and
1952 // favicon available, so set them immediately.
1953 if (is_same_document && GetLastCommittedEntry()) {
1954 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
1955 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1956 }
[email protected]3a868f212014-08-09 10:41:191957 }
[email protected]ff64b3e2014-05-31 04:07:331958
[email protected]60d6cca2013-04-30 08:47:131959 DCHECK(!params.history_list_was_cleared || !replace_entry);
1960 // The browser requested to clear the session history when it initiated the
1961 // navigation. Now we know that the renderer has updated its state accordingly
1962 // and it is safe to also clear the browser side history.
1963 if (params.history_list_was_cleared) {
Rakina Zata Amniddf10502022-01-15 02:56:551964 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]60d6cca2013-04-30 08:47:131965 entries_.clear();
1966 last_committed_entry_index_ = -1;
1967 }
1968
Nasko Oskovaee2f862018-06-15 00:05:521969 // If this is a new navigation with replacement and there is a
1970 // pending_entry_ which matches the navigation reported by the renderer
1971 // process, then it should be the one replaced, so update the
1972 // last_committed_entry_index_ to use it.
1973 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101974 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521975 last_committed_entry_index_ = pending_entry_index_;
1976 }
1977
Alexander Timine3ec4192020-04-20 16:39:401978 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411979 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401980 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051981
Carlos IL42b416592019-10-07 23:10:361982 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
Dave Tapuska87696ae2021-11-18 18:48:311983 !request->post_commit_error_page_html().empty(),
Rakina Zata Amnia4e27222021-12-22 01:05:001984 rfh->frame_tree_node()->IsInFencedFrameTree(),
1985 commit_details);
[email protected]e9ba4472008-09-14 15:42:431986}
1987
Charlie Reisc0f17d2d2021-01-12 18:52:491988void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321989 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071990 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361991 bool is_same_document,
jam48cea9082017-02-15 06:13:291992 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451993 NavigationRequest* request,
Rakina Zata Amnia4e27222021-12-22 01:05:001994 bool keep_pending_entry,
1995 LoadCommittedDetails* commit_details) {
creis26d22632017-04-21 20:23:561996 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1997 << "that a last committed entry exists.";
1998
[email protected]e9ba4472008-09-14 15:42:431999 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:002000 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:432001
Charlie Reis7e2cb6d2021-01-26 01:27:162002 NavigationEntryImpl* entry = nullptr;
Rakina Zata Amni153d5702021-09-13 22:48:002003 if (request->commit_params().intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:162004 // We're guaranteed to have a last committed entry if intended_as_new_entry
2005 // is true.
avicbdc4c12015-07-01 16:07:112006 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:162007 DCHECK(entry);
2008
2009 // If the NavigationRequest matches a new pending entry and is classified as
2010 // EXISTING_ENTRY, then it is a navigation to the same URL that was
2011 // converted to a reload, such as a user pressing enter in the omnibox.
2012 if (pending_entry_ && pending_entry_index_ == -1 &&
2013 pending_entry_->GetUniqueID() ==
2014 request->commit_params().nav_entry_id) {
2015 // Note: The pending entry will usually have a real ReloadType here, but
2016 // it can still be ReloadType::NONE in cases that
2017 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
2018
2019 // If we classified this correctly, the SiteInstance should not have
2020 // changed.
2021 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
2022
2023 // For converted reloads, we assign the entry's unique ID to be that of
2024 // the new one. Since this is always the result of a user action, we want
2025 // to dismiss infobars, etc. like a regular user-initiated navigation.
2026 entry->set_unique_id(pending_entry_->GetUniqueID());
2027
2028 // The extra headers may have changed due to reloading with different
2029 // headers.
2030 entry->set_extra_headers(pending_entry_->extra_headers());
2031 }
2032 // Otherwise, this was intended as a new entry but the pending entry was
2033 // lost in the meantime and no new entry was created. We are stuck at the
2034 // last committed entry.
2035
2036 // Even if this is a converted reload from pressing enter in the omnibox,
2037 // the server could redirect, requiring an update to the SSL status. If this
2038 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:452039 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:162040 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:472041 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:452042 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:162043 }
Rakina Zata Amnif6950d552020-11-24 03:26:102044 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:112045 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:102046 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b902016-09-01 16:58:162047
eugenebut604866f2017-05-10 21:35:362048 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:452049 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:362050 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
2051 // this was a restored same document navigation entry, then it won't have
2052 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
2053 // navigated it.
jam48cea9082017-02-15 06:13:292054 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
Mike West800532c2021-10-14 09:26:522055 if (entry->GetURL().DeprecatedGetOriginAsURL() ==
2056 last_entry->GetURL().DeprecatedGetOriginAsURL() &&
jam48cea9082017-02-15 06:13:292057 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
2058 was_restored) {
2059 entry->GetSSL() = last_entry->GetSSL();
2060 }
2061 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:452062 // In rapid back/forward navigations |request| sometimes won't have a cert
2063 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:192064 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:452065 if (request->GetSSLInfo().has_value() &&
2066 request->GetSSLInfo()->is_valid()) {
2067 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
Mike West800532c2021-10-14 09:26:522068 } else if (entry->GetURL().DeprecatedGetOriginAsURL() !=
2069 request->GetURL().DeprecatedGetOriginAsURL()) {
John Abd-El-Malek3f247082017-12-07 19:02:192070 entry->GetSSL() = SSLStatus();
2071 }
jam48cea9082017-02-15 06:13:292072 }
avicbdc4c12015-07-01 16:07:112073 } else {
Feifei Wang2ab8ba6c2022-04-13 22:19:272074 // This is renderer-initiated. The only kinds of renderer-initiated
Rakina Zata Amni557afb92021-07-17 04:39:572075 // navigations that are EXISTING_ENTRY are same-document navigations that
2076 // result in replacement (e.g. history.replaceState(), location.replace(),
2077 // forced replacements for trivial session history contexts). For these
2078 // cases, we reuse the last committed entry.
avicbdc4c12015-07-01 16:07:112079 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:102080
Mikel Astizba9cf2fd2017-12-17 10:38:102081 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
2082 // to avoid misleading interpretations (e.g. URLs paired with
2083 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
2084 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
2085 // | PAGE_TRANSITION_CLIENT_REDIRECT).
2086
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572087 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:102088
eugenebut604866f2017-05-10 21:35:362089 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:452090 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:362091 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:472092 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:452093 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
avicbdc4c12015-07-01 16:07:112094 }
2095 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:432096
Rakina Zata Amni3460d382021-10-29 00:43:372097 UpdateNavigationEntryDetails(entry, rfh, params, request,
2098 NavigationEntryImpl::UpdatePolicy::kUpdate,
Rakina Zata Amnia4e27222021-12-22 01:05:002099 false /* is_new_entry */, commit_details);
creis22a7b4c2016-04-28 07:20:302100
[email protected]5ccd4dc2012-10-24 02:28:142101 // The redirected to page should not inherit the favicon from the previous
2102 // page.
eugenebut604866f2017-05-10 21:35:362103 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:482104 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:142105
Peter Boströmd7592132019-01-30 04:50:312106 // We should also usually discard the pending entry if it corresponds to a
2107 // different navigation, since that one is now likely canceled. In rare
2108 // cases, we leave the pending entry for another navigation in place when we
2109 // know it is still ongoing, to avoid a flicker in the omnibox (see
2110 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:432111 //
2112 // Note that we need to use the "internal" version since we don't want to
2113 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:312114 if (!keep_pending_entry)
Rakina Zata Amnia4e27222021-12-22 01:05:002115 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]40bcc302009-03-02 20:50:392116
Carlos IL4dea8902020-05-26 15:14:292117 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:112118 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:432119}
2120
[email protected]d202a7c2012-01-04 07:53:472121void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:322122 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072123 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:362124 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:472125 bool replace_entry,
2126 bool previous_document_was_activated,
Rakina Zata Amnia4e27222021-12-22 01:05:002127 NavigationRequest* request,
2128 LoadCommittedDetails* commit_details) {
avi25f5f9e2015-07-17 20:08:262129 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2130 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
Rakina Zata Amniddf10502022-01-15 02:56:552131 // The NEW_SUBFRAME path should never result in an initial NavigationEntry.
2132 DCHECK(!commit_details->should_stay_as_initial_entry);
[email protected]09b8f82f2009-06-16 20:22:112133
[email protected]e9ba4472008-09-14 15:42:432134 // Manual subframe navigations just get the current entry cloned so the user
2135 // can go back or forward to it. The actual subframe information will be
2136 // stored in the page state for each of those entries. This happens out of
2137 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:092138 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
2139 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:382140
Mikel Astizba9cf2fd2017-12-17 10:38:102141 // The DCHECK below documents the fact that we don't know of any situation
2142 // where |replace_entry| is true for subframe navigations. This simplifies
2143 // reasoning about the replacement struct for subframes (see
2144 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
2145 DCHECK(!replace_entry);
2146
Patrick Monette50e8bd82019-06-13 22:40:452147 // This FrameNavigationEntry might not end up being used in the
2148 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Anton Bikineevf62d1bf2021-05-15 17:56:072149 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452150 request->common_params().initiator_origin;
Nate Chapin63db0d12022-01-20 22:03:302151 std::unique_ptr<PolicyContainerPolicies> policy_container_policies =
2152 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
2153 request->GetURL());
Domenic Denicolacc094fb2022-03-16 23:40:572154 bool protect_url_in_navigation_api = false;
Nate Chapin63db0d12022-01-20 22:03:302155 if (is_same_document) {
2156 FrameNavigationEntry* previous_frame_entry =
2157 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
Domenic Denicolacc094fb2022-03-16 23:40:572158 // Try to preserve protect_url_in_navigation_api from the previous
Nate Chapin63db0d12022-01-20 22:03:302159 // FrameNavigationEntry.
Domenic Denicolacc094fb2022-03-16 23:40:572160 protect_url_in_navigation_api =
Nate Chapin63db0d12022-01-20 22:03:302161 previous_frame_entry &&
Domenic Denicolacc094fb2022-03-16 23:40:572162 previous_frame_entry->protect_url_in_navigation_api();
Nate Chapin63db0d12022-01-20 22:03:302163 } else {
Domenic Denicolacc094fb2022-03-16 23:40:572164 protect_url_in_navigation_api =
Nate Chapin63db0d12022-01-20 22:03:302165 policy_container_policies &&
Domenic Denicolacc094fb2022-03-16 23:40:572166 ShouldProtectUrlInNavigationApi(
Nate Chapin63db0d12022-01-20 22:03:302167 policy_container_policies->referrer_policy);
2168 }
2169
Patrick Monette50e8bd82019-06-13 22:40:452170 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:482171 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Domenic Denicolacc094fb2022-03-16 23:40:572172 params.document_sequence_number, params.navigation_api_key,
Nate Chapinfbfe5af2021-06-10 17:22:082173 rfh->GetSiteInstance(), nullptr, params.url,
2174 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:092175 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072176 params.page_state, params.method, params.post_id,
2177 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032178 request->web_bundle_navigation_info()
2179 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392180 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482181 request->GetSubresourceWebBundleNavigationInfo(),
Domenic Denicolacc094fb2022-03-16 23:40:572182 std::move(policy_container_policies), protect_url_in_navigation_api);
Charles Reisf44482022017-10-13 21:15:032183
creisce0ef3572017-01-26 17:53:082184 std::unique_ptr<NavigationEntryImpl> new_entry =
2185 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:452186 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:002187 frame_tree_.root());
creise062d542015-08-25 02:01:552188
Alexander Timine3ec4192020-04-20 16:39:402189 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:412190 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:402191 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:472192
creisce0ef3572017-01-26 17:53:082193 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:452194 // to replace, which can happen due to a unique name change. See
2195 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
2196 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:382197
Dave Tapuska87696ae2021-11-18 18:48:312198 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false,
Rakina Zata Amnia4e27222021-12-22 01:05:002199 rfh->frame_tree_node()->IsInFencedFrameTree(),
2200 commit_details);
[email protected]e9ba4472008-09-14 15:42:432201}
2202
[email protected]d202a7c2012-01-04 07:53:472203bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:322204 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072205 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392206 bool is_same_document,
Nate Chapinc7019dd7d2021-06-25 18:29:252207 bool was_on_initial_empty_document,
Rakina Zata Amnia4e27222021-12-22 01:05:002208 NavigationRequest* request,
2209 LoadCommittedDetails* commit_details) {
avi9f07a0c2015-02-18 22:51:292210 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2211 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2212
[email protected]e9ba4472008-09-14 15:42:432213 // We're guaranteed to have a previously committed entry, and we now need to
2214 // handle navigation inside of a subframe in it without creating a new entry.
2215 DCHECK(GetLastCommittedEntry());
2216
creis913c63ce2016-07-16 19:52:522217 // For newly created subframes, we don't need to send a commit notification.
2218 // This is only necessary for history navigations in subframes.
2219 bool send_commit_notification = false;
2220
Rakina Zata Amnif6950d552020-11-24 03:26:102221 // If |nav_entry_id| is non-zero and matches an existing entry, this
2222 // is a history navigation. Update the last committed index accordingly. If
2223 // we don't recognize the |nav_entry_id|, it might be a recently
2224 // pruned entry. We'll handle it below.
2225 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2226 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472227 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062228 // Make sure that a subframe commit isn't changing the main frame's
2229 // origin. Otherwise the renderer process may be confused, leading to a
2230 // URL spoof. We can't check the path since that may change
2231 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572232 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2233 // about:blank, file, and unique origins are more subtle to get right.
Charlie Reis95fbca442021-05-21 21:38:242234 // We should use checks similar to RenderFrameHostImpl's
2235 // CanCommitUrlAndOrigin on the main frame during subframe commits.
2236 // See https://crbug.com/1209092.
creis37988b92016-06-10 18:03:572237 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2238 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2239 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2240 dest_top_url.SchemeIsHTTPOrHTTPS() &&
Mike West800532c2021-10-14 09:26:522241 current_top_url.DeprecatedGetOriginAsURL() !=
2242 dest_top_url.DeprecatedGetOriginAsURL()) {
Chris Bookholt10f4b7332022-02-14 18:25:442243 bad_message::ReceivedBadMessage(rfh->GetMainFrame()->GetProcess(),
creisfb6eeb62016-05-10 19:01:512244 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062245 }
creis3cdc3b02015-05-29 23:00:472246
creis913c63ce2016-07-16 19:52:522247 // We only need to discard the pending entry in this history navigation
2248 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062249 last_committed_entry_index_ = entry_index;
Rakina Zata Amnia4e27222021-12-22 01:05:002250 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
creis913c63ce2016-07-16 19:52:522251
2252 // History navigations should send a commit notification.
2253 send_commit_notification = true;
avi98405c22015-05-21 20:47:062254 }
[email protected]e9ba4472008-09-14 15:42:432255 }
[email protected]f233e4232013-02-23 00:55:142256
creisce0ef3572017-01-26 17:53:082257 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2258 // it may be a "history auto" case where we update an existing one.
2259 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Nate Chapin9f169072021-06-09 19:32:372260
2261 // We may want to update |last_committed|'s FrameNavigationEntry (if one
2262 // exists), or we may want to clobber it and create a new one. We update in
2263 // cases where the corresponding FrameNavigationEntry is conceptually similar
2264 // to the document described by the commit params: same-document
2265 // navigations, history traversal to an existing entry, and reloads (including
2266 // a "soft reload" where we navigate to the same url without flagging it as a
2267 // reload). But in the case of a different document that is not logically
2268 // related to the committed FrameNavigationEntry's document (cross-document,
2269 // not same url, not a reload, not a history traversal), we replace rather
2270 // than update.
Nate Chapinc7019dd7d2021-06-25 18:29:252271 //
Nate Chapin9f169072021-06-09 19:32:372272 // In the case where we update, the FrameNavigationEntry will potentially be
2273 // shared across multiple NavigationEntries, and any updates will be reflected
2274 // in all of those NavigationEntries. In the replace case, any existing
2275 // sharing with other NavigationEntries will stop.
Nate Chapinc7019dd7d2021-06-25 18:29:252276 //
2277 // When navigating away from the initial empty document, we also update rather
2278 // than replace. Either update or replace will overwrite the initial empty
2279 // document state for |last_committed|, but if the FrameNavigationEntry for
2280 // the initial empty document is shared across multiple NavigationEntries (due
2281 // to a navigation in another frame), we want to make sure we overwrite the
2282 // initial empty document state everywhere this FrameNavigationEntry is used,
2283 // which is accompished by updating the existing FrameNavigationEntry.
Nate Chapin9f169072021-06-09 19:32:372284 FrameNavigationEntry* last_committed_frame_entry =
2285 last_committed->GetFrameEntry(rfh->frame_tree_node());
2286 NavigationEntryImpl::UpdatePolicy update_policy =
2287 NavigationEntryImpl::UpdatePolicy::kUpdate;
2288 if (request->common_params().navigation_type ==
Minggang Wangb9f3fa92021-07-01 15:30:312289 blink::mojom::NavigationType::DIFFERENT_DOCUMENT &&
Nate Chapin9f169072021-06-09 19:32:372290 last_committed_frame_entry &&
Nate Chapinc7019dd7d2021-06-25 18:29:252291 last_committed_frame_entry->url() != params.url &&
2292 !was_on_initial_empty_document) {
Nate Chapin9f169072021-06-09 19:32:372293 update_policy = NavigationEntryImpl::UpdatePolicy::kReplace;
2294 }
2295
Rakina Zata Amni3460d382021-10-29 00:43:372296 UpdateNavigationEntryDetails(last_committed, rfh, params, request,
Rakina Zata Amnia4e27222021-12-22 01:05:002297 update_policy, false /* is_new_entry */,
2298 commit_details);
creis625a0c7d2015-03-24 23:17:122299
creis913c63ce2016-07-16 19:52:522300 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432301}
2302
[email protected]d202a7c2012-01-04 07:53:472303int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232304 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032305 for (size_t i = 0; i < entries_.size(); ++i) {
2306 if (entries_[i].get() == entry)
2307 return i;
2308 }
2309 return -1;
[email protected]765b35502008-08-21 00:51:202310}
2311
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572312void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242313 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572314 NavigationControllerImpl* source =
2315 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572316 // Verify that we look new.
Rakina Zata Amni2322f4f82022-01-24 13:24:242317 if (blink::features::IsInitialNavigationEntryEnabled()) {
2318 DCHECK_EQ(1, GetEntryCount());
2319 DCHECK(GetLastCommittedEntry()->IsInitialEntry());
2320 } else {
2321 DCHECK_EQ(0, GetEntryCount());
2322 }
Lei Zhang96031532019-10-10 19:05:472323 DCHECK(!GetPendingEntry());
Rakina Zata Amniafd3c6582021-11-30 06:19:172324 entries_.clear();
[email protected]ce3fa3c2009-04-20 19:55:572325
Rakina Zata Amniafd3c6582021-11-30 06:19:172326 if (source->GetEntryCount() == 0) {
Rakina Zata Amniafd3c6582021-11-30 06:19:172327 return;
2328 }
[email protected]ce3fa3c2009-04-20 19:55:572329
Francois Dorayeaace782017-06-21 16:37:242330 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442331 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572332 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572333
Fergal Dalya1d569972021-03-16 03:24:532334 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302335 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532336 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2337 session_storage_namespace_map_[it.first] = source_namespace->Clone();
[email protected]fdac6ade2013-07-20 01:06:302338 }
[email protected]4e6419c2010-01-15 04:50:342339
Lukasz Anforowicz0de0f452020-12-02 19:57:152340 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572341}
2342
Aran Gilman37d11632019-10-08 23:07:152343void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2344 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162345 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012346 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162347
[email protected]d202a7c2012-01-04 07:53:472348 NavigationControllerImpl* source =
2349 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252350
avi2b177592014-12-10 02:08:022351 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012352 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252353
[email protected]474f8512013-05-31 22:31:162354 // We now have one entry, possibly with a new pending entry. Ensure that
2355 // adding the entries from source won't put us over the limit.
2356 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572357 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572358 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252359
Carlos IL4dea8902020-05-26 15:14:292360 // Insert the entries from source. Ignore any pending entry, since it has not
2361 // committed in source.
[email protected]474f8512013-05-31 22:31:162362 int max_source_index = source->last_committed_entry_index_;
Rakina Zata Amniafd3c6582021-11-30 06:19:172363 DCHECK_NE(max_source_index, -1);
2364 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572365
2366 // Ignore the source's current entry if merging with replacement.
2367 // TODO(davidben): This should preserve entries forward of the current
2368 // too. http://crbug.com/317872
2369 if (replace_entry && max_source_index > 0)
2370 max_source_index--;
2371
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572372 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252373
2374 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552375 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142376
Hayato Ito2c8c08d02021-06-23 03:38:432377 BroadcastHistoryOffsetAndLength();
[email protected]e1cd5452010-08-26 18:03:252378}
2379
[email protected]79368982013-11-13 01:11:012380bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162381 // If there is no last committed entry, we cannot prune. Even if there is a
2382 // pending entry, it may not commit, leaving this WebContents blank, despite
2383 // possibly giving it new entries via CopyStateFromAndPrune.
2384 if (last_committed_entry_index_ == -1)
2385 return false;
[email protected]9350602e2013-02-26 23:27:442386
[email protected]474f8512013-05-31 22:31:162387 // We cannot prune if there is a pending entry at an existing entry index.
2388 // It may not commit, so we have to keep the last committed entry, and thus
2389 // there is no sensible place to keep the pending entry. It is ok to have
2390 // a new pending entry, which can optionally commit as a new navigation.
2391 if (pending_entry_index_ != -1)
2392 return false;
2393
[email protected]474f8512013-05-31 22:31:162394 return true;
2395}
2396
[email protected]79368982013-11-13 01:11:012397void NavigationControllerImpl::PruneAllButLastCommitted() {
2398 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162399
avi2b177592014-12-10 02:08:022400 DCHECK_EQ(0, last_committed_entry_index_);
2401 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442402
Hayato Ito2c8c08d02021-06-23 03:38:432403 BroadcastHistoryOffsetAndLength();
[email protected]9350602e2013-02-26 23:27:442404}
2405
[email protected]79368982013-11-13 01:11:012406void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162407 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012408 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262409
[email protected]474f8512013-05-31 22:31:162410 // Erase all entries but the last committed entry. There may still be a
2411 // new pending entry after this.
2412 entries_.erase(entries_.begin(),
2413 entries_.begin() + last_committed_entry_index_);
2414 entries_.erase(entries_.begin() + 1, entries_.end());
2415 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262416}
2417
Christian Dullweber1af31e62018-02-22 11:49:482418void NavigationControllerImpl::DeleteNavigationEntries(
2419 const DeletionPredicate& deletionPredicate) {
2420 // It is up to callers to check the invariants before calling this.
2421 CHECK(CanPruneAllButLastCommitted());
2422 std::vector<int> delete_indices;
2423 for (size_t i = 0; i < entries_.size(); i++) {
2424 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572425 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482426 delete_indices.push_back(i);
2427 }
2428 }
2429 if (delete_indices.empty())
2430 return;
2431
2432 if (delete_indices.size() == GetEntryCount() - 1U) {
2433 PruneAllButLastCommitted();
2434 } else {
2435 // Do the deletion in reverse to preserve indices.
Ayu Ishii2f825852022-03-08 19:47:382436 for (const auto& index : base::Reversed(delete_indices)) {
2437 RemoveEntryAtIndex(index);
Christian Dullweber1af31e62018-02-22 11:49:482438 }
Hayato Ito2c8c08d02021-06-23 03:38:432439 BroadcastHistoryOffsetAndLength();
Christian Dullweber1af31e62018-02-22 11:49:482440 }
2441 delegate()->NotifyNavigationEntriesDeleted();
2442}
2443
Shivani Sharma883f5f32019-02-12 18:20:012444bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2445 auto* entry = GetEntryAtIndex(index);
2446 return entry && entry->should_skip_on_back_forward_ui();
2447}
2448
Carlos Caballero35ce710c2019-09-19 10:59:452449BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2450 return back_forward_cache_;
2451}
2452
clamy987a3752018-05-03 17:36:262453void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2454 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2455 // progress, since this will cause a use-after-free. (We only allow this
2456 // when the tab is being destroyed for shutdown, since it won't return to
2457 // NavigateToEntry in that case.) http://crbug.com/347742.
Takashi Toyoshimaea534ef22021-07-21 03:27:592458 CHECK(!in_navigate_to_pending_entry_ || frame_tree_.IsBeingDestroyed());
clamy987a3752018-05-03 17:36:262459
2460 if (was_failure && pending_entry_) {
2461 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2462 } else {
2463 failed_pending_entry_id_ = 0;
2464 }
2465
2466 if (pending_entry_) {
2467 if (pending_entry_index_ == -1)
2468 delete pending_entry_;
2469 pending_entry_index_ = -1;
2470 pending_entry_ = nullptr;
2471 }
arthursonzogni66f711c2019-10-08 14:40:362472
2473 // Ensure any refs to the current pending entry are ignored if they get
2474 // deleted, by clearing the set of known refs. All future pending entries will
2475 // only be affected by new refs.
2476 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262477}
2478
2479void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2480 if (pending_entry_)
2481 pending_entry_->set_ssl_error(error);
2482}
2483
Xiaohan Wang7f8052e02022-01-14 18:44:282484#if BUILDFLAG(IS_ANDROID)
Camille Lamy5193caa2018-10-12 11:59:422485// static
2486bool NavigationControllerImpl::ValidateDataURLAsString(
2487 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2488 if (!data_url_as_string)
2489 return false;
2490
2491 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2492 return false;
2493
2494 // The number of characters that is enough for validating a data: URI.
2495 // From the GURL's POV, the only important part here is scheme, it doesn't
2496 // check the actual content. Thus we can take only the prefix of the url, to
2497 // avoid unneeded copying of a potentially long string.
2498 const size_t kDataUriPrefixMaxLen = 64;
2499 GURL data_url(
2500 std::string(data_url_as_string->front_as<char>(),
2501 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2502 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2503 return false;
2504
2505 return true;
2506}
2507#endif
2508
Shivani Sharma194877032019-03-07 17:52:472509void NavigationControllerImpl::NotifyUserActivation() {
2510 // When a user activation occurs, ensure that all adjacent entries for the
2511 // same document clear their skippable bit, so that the history manipulation
2512 // intervention does not apply to them.
2513 auto* last_committed_entry = GetLastCommittedEntry();
2514 if (!last_committed_entry)
2515 return;
Shivani Sharma194877032019-03-07 17:52:472516
shivanigithub99368382021-06-16 18:33:372517 if (base::FeatureList::IsEnabled(
2518 features::kDebugHistoryInterventionNoUserActivation)) {
2519 return;
2520 }
2521
Shivani Sharmac4cc8922019-04-18 03:11:172522 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472523}
2524
clamy987a3752018-05-03 17:36:262525bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2526 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172527 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262528 NavigationEntryImpl* entry =
2529 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2530 if (!entry)
2531 return false;
2532
2533 FrameNavigationEntry* frame_entry =
2534 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2535 if (!frame_entry)
2536 return false;
2537
Nate Chapin45f620582021-09-30 17:45:432538 // |is_browser_initiated| is false here because a navigation in a new subframe
2539 // always begins with renderer action (i.e., an HTML element being inserted
2540 // into the DOM), so it is always renderer-initiated.
Camille Lamy5193caa2018-10-12 11:59:422541 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572542 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232543 ReloadType::NONE, false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432544 true /* is_history_navigation_in_new_child */,
2545 false /* is_browser_initiated */);
clamyea99ea12018-05-28 13:54:232546
2547 if (!request)
2548 return false;
2549
arthursonzognif046d4a2019-12-12 19:08:102550 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412551
Lukasz Anforowicz9ee83c272020-12-01 20:14:052552 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2553 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232554
2555 return true;
clamy987a3752018-05-03 17:36:262556}
2557
Tsuyoshi Horo52fd08e2020-07-07 07:03:452558bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2559 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2560 if (!entry)
2561 return false;
Rakina Zata Amnif297a802022-01-18 03:53:432562
2563 if (entry->IsInitialEntryNotForSynchronousAboutBlank()) {
2564 // We should never navigate to an existing initial NavigationEntry that is
2565 // the initial NavigationEntry for the initial empty document that hasn't
2566 // been overridden by the synchronous about:blank commit, to preserve
2567 // legacy behavior where trying to reload when the main frame is on the
2568 // initial empty document won't result in a navigation. See also
2569 // https://crbug.com/1277414.
2570 return false;
2571 }
Tsuyoshi Horo52fd08e2020-07-07 07:03:452572 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2573 if (!frame_entry)
2574 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142575 ReloadType reload_type = ReloadType::NORMAL;
2576 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452577 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142578 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452579 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432580 false /* is_history_navigation_in_new_child */,
2581 true /* is_browser_initiated */);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452582 if (!request)
2583 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052584 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452585 return true;
2586}
2587
Dave Tapuska8bfd84c2019-03-26 20:47:162588void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2589 int offset,
2590 int sandbox_frame_tree_node_id) {
2591 if (!CanGoToOffset(offset))
2592 return;
Nate Chapin45f620582021-09-30 17:45:432593 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id,
2594 false /* is_browser_initiated */);
Dave Tapuska8bfd84c2019-03-26 20:47:162595}
2596
clamy987a3752018-05-03 17:36:262597void NavigationControllerImpl::NavigateFromFrameProxy(
2598 RenderFrameHostImpl* render_frame_host,
2599 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022600 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452601 int initiator_process_id,
Anton Bikineevf62d1bf2021-05-15 17:56:072602 const absl::optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262603 bool is_renderer_initiated,
2604 SiteInstance* source_site_instance,
2605 const Referrer& referrer,
2606 ui::PageTransition page_transition,
2607 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252608 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262609 const std::string& method,
2610 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092611 const std::string& extra_headers,
Antonio Sartori2f763d9d2021-04-21 10:04:142612 network::mojom::SourceLocationPtr source_location,
John Delaney50425f82020-04-07 16:26:212613 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Tsuyoshi Horo167ca6432022-03-09 05:16:392614 const absl::optional<blink::Impression>& impression,
Nan Lin944e9b4e2022-04-12 13:51:222615 base::TimeTicks navigation_start_time,
2616 absl::optional<bool> is_fenced_frame_opaque_url) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582617 if (is_renderer_initiated)
2618 DCHECK(initiator_origin.has_value());
2619
clamy987a3752018-05-03 17:36:262620 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582621
Rakina Zata Amni2322f4f82022-01-24 13:24:242622 // Don't allow an entry replacement if there is no entry to replace.
2623 // http://crbug.com/457149
2624 if (GetEntryCount() == 0)
2625 should_replace_current_entry = false;
2626
clamy987a3752018-05-03 17:36:262627 // Create a NavigationEntry for the transfer, without making it the pending
2628 // entry. Subframe transfers should have a clone of the last committed entry
2629 // with a FrameNavigationEntry for the target frame. Main frame transfers
2630 // should have a new NavigationEntry.
2631 // TODO(creis): Make this unnecessary by creating (and validating) the params
2632 // directly, passing them to the destination RenderFrameHost. See
2633 // https://crbug.com/536906.
2634 std::unique_ptr<NavigationEntryImpl> entry;
Harkiran Bolariae1b5158b2021-09-16 19:03:262635 if (!render_frame_host->is_main_frame()) {
clamy987a3752018-05-03 17:36:262636 // Subframe case: create FrameNavigationEntry.
Rakina Zata Amni2322f4f82022-01-24 13:24:242637 if (GetLastCommittedEntry()) {
2638 entry = GetLastCommittedEntry()->Clone();
2639 entry->set_extra_headers(extra_headers);
2640 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2641 // Renderer-initiated navigation that target a remote frame are currently
2642 // classified as browser-initiated when this one has already navigated.
2643 // See https://crbug.com/722251.
2644 } else {
2645 // If there's no last committed entry, create an entry for about:blank
2646 // with a subframe entry for our destination.
2647 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2648 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
2649 GURL(url::kAboutBlankURL), referrer, initiator_origin,
2650 source_site_instance, page_transition, is_renderer_initiated,
2651 extra_headers, browser_context_,
2652 nullptr /* blob_url_loader_factory */));
2653 }
Rakina Zata Amniafd3c6582021-11-30 06:19:172654 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2655 // Renderer-initiated navigation that target a remote frame are currently
2656 // classified as browser-initiated when this one has already navigated.
2657 // See https://crbug.com/722251.
Nate Chapin9f169072021-06-09 19:32:372658 // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter
2659 // to CreateNavigationRequestFromLoadParams(), so while kReplace might
2660 // remove child FrameNavigationEntries (e.g., if this is a cross-process
2661 // same-document navigation), they will still be present in the
2662 // committed NavigationEntry that will be referenced to construct the new
2663 // FrameNavigationEntry tree when this navigation commits.
clamy987a3752018-05-03 17:36:262664 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:082665 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582666 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072667 absl::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202668 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392669 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482670 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori79d549d2021-02-18 12:59:542671 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262672 } else {
2673 // Main frame case.
2674 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102675 url, referrer, initiator_origin, source_site_instance, page_transition,
2676 is_renderer_initiated, extra_headers, browser_context_,
Hayato Ito303654c2021-06-30 09:07:542677 blob_url_loader_factory));
clamy987a3752018-05-03 17:36:262678 entry->root_node()->frame_entry->set_source_site_instance(
2679 static_cast<SiteInstanceImpl*>(source_site_instance));
2680 entry->root_node()->frame_entry->set_method(method);
2681 }
clamy987a3752018-05-03 17:36:262682
Camille Lamy5193caa2018-10-12 11:59:422683 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262684 if (GetLastCommittedEntry() &&
2685 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2686 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422687 override_user_agent = true;
clamy987a3752018-05-03 17:36:262688 }
2689 // TODO(creis): Set user gesture and intent received timestamp on Android.
2690
2691 // We may not have successfully added the FrameNavigationEntry to |entry|
2692 // above (per https://crbug.com/608402), in which case we create it from
2693 // scratch. This works because we do not depend on |frame_entry| being inside
2694 // |entry| during NavigateToEntry. This will go away when we shortcut this
2695 // further in https://crbug.com/536906.
2696 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2697 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452698 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Nate Chapinfbfe5af2021-06-10 17:22:082699 node->unique_name(), -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582700 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072701 absl::nullopt /* origin */, referrer, initiator_origin,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:122702 std::vector<GURL>(), blink::PageState(), method, -1,
2703 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482704 nullptr /* subresource_web_bundle_navigation_info */,
Nate Chapin63db0d12022-01-20 22:03:302705 nullptr /* policy_container_policies */,
Domenic Denicolacc094fb2022-03-16 23:40:572706 false /* protect_url_in_navigation_api */);
clamy987a3752018-05-03 17:36:262707 }
2708
Camille Lamy5193caa2018-10-12 11:59:422709 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022710 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452711 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292712 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422713 params.source_site_instance = source_site_instance;
2714 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2715 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032716 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422717 params.referrer = referrer;
2718 /* params.redirect_chain: skip */
2719 params.extra_headers = extra_headers;
2720 params.is_renderer_initiated = is_renderer_initiated;
2721 params.override_user_agent = UA_OVERRIDE_INHERIT;
2722 /* params.base_url_for_data_url: skip */
2723 /* params.virtual_url_for_data_url: skip */
2724 /* params.data_url_as_string: skip */
2725 params.post_data = post_body;
2726 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582727 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422728 /* params.frame_name: skip */
2729 // TODO(clamy): See if user gesture should be propagated to this function.
2730 params.has_user_gesture = false;
2731 params.should_clear_history_list = false;
2732 params.started_from_context_menu = false;
2733 /* params.navigation_ui_data: skip */
2734 /* params.input_start: skip */
Minggang Wangf59db47b2021-06-16 01:56:222735 params.was_activated = blink::mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542736 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212737 params.impression = impression;
Antonio Sartori6984c742021-08-26 08:03:412738 params.download_policy = std::move(download_policy);
Camille Lamy5193caa2018-10-12 11:59:422739
2740 std::unique_ptr<NavigationRequest> request =
2741 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032742 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:142743 false /* has_user_gesture */, std::move(source_location),
Tsuyoshi Horo167ca6432022-03-09 05:16:392744 ReloadType::NONE, entry.get(), frame_entry.get(),
Nan Lin944e9b4e2022-04-12 13:51:222745 navigation_start_time, is_fenced_frame_opaque_url);
clamyea99ea12018-05-28 13:54:232746
2747 if (!request)
2748 return;
2749
Arthur Hemery948742762019-09-18 10:06:242750 // At this stage we are proceeding with this navigation. If this was renderer
2751 // initiated with user gesture, we need to make sure we clear up potential
2752 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2753 DiscardNonCommittedEntries();
2754
Lukasz Anforowicz9ee83c272020-12-01 20:14:052755 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262756}
2757
[email protected]d1198fd2012-08-13 22:50:192758void NavigationControllerImpl::SetSessionStorageNamespace(
Alex Moshchuk8015afcf2022-01-31 22:59:252759 const StoragePartitionConfig& partition_config,
[email protected]8ff00d72012-10-23 19:12:212760 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192761 if (!session_storage_namespace)
2762 return;
2763
2764 // We can't overwrite an existing SessionStorage without violating spec.
2765 // Attempts to do so may give a tab access to another tab's session storage
2766 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152767 bool successful_insert =
2768 session_storage_namespace_map_
Alex Moshchuk8015afcf2022-01-31 22:59:252769 .insert(std::make_pair(partition_config,
Aaron Colwellf3b316e2021-03-11 20:17:052770 static_cast<SessionStorageNamespaceImpl*>(
2771 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302772 .second;
2773 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
[email protected]d1198fd2012-08-13 22:50:192774}
2775
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572776bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Rakina Zata Amni2322f4f82022-01-24 13:24:242777 return IsInitialNavigation() &&
2778 (!GetLastCommittedEntry() ||
2779 GetLastCommittedEntry()->IsInitialEntry()) &&
Carlos Caballeroede6f8c2021-01-28 11:01:502780 !frame_tree_.has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462781}
2782
Aran Gilman37d11632019-10-08 23:07:152783SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Alex Moshchuk8015afcf2022-01-31 22:59:252784 const StoragePartitionConfig& partition_config) {
[email protected]fdac6ade2013-07-20 01:06:302785 StoragePartition* partition =
Lukasz Anforowiczb9a969a2021-04-29 15:26:252786 browser_context_->GetStoragePartition(partition_config);
michaelnbacbcbd2016-02-09 00:32:032787 DOMStorageContextWrapper* context_wrapper =
2788 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2789
2790 SessionStorageNamespaceMap::const_iterator it =
Alex Moshchuk8015afcf2022-01-31 22:59:252791 session_storage_namespace_map_.find(partition_config);
michaelnbacbcbd2016-02-09 00:32:032792 if (it != session_storage_namespace_map_.end()) {
2793 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152794 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2795 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:472796
michaelnbacbcbd2016-02-09 00:32:032797 return it->second.get();
2798 }
2799
2800 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102801 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2802 SessionStorageNamespaceImpl::Create(context_wrapper);
2803 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2804 session_storage_namespace.get();
Alex Moshchuk8015afcf2022-01-31 22:59:252805 session_storage_namespace_map_[partition_config] =
Daniel Murphy31bbb8b12018-02-07 21:44:102806 std::move(session_storage_namespace);
[email protected]fdac6ade2013-07-20 01:06:302807
Daniel Murphy31bbb8b12018-02-07 21:44:102808 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302809}
2810
2811SessionStorageNamespace*
2812NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Alex Moshchuk8015afcf2022-01-31 22:59:252813 return GetSessionStorageNamespace(
2814 StoragePartitionConfig::CreateDefault(GetBrowserContext()));
[email protected]fdac6ade2013-07-20 01:06:302815}
2816
2817const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572818NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302819 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552820}
[email protected]d202a7c2012-01-04 07:53:472821
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572822bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562823 return needs_reload_;
2824}
[email protected]a26023822011-12-29 00:23:552825
[email protected]46bb5e9c2013-10-03 22:16:472826void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412827 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2828}
2829
2830void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472831 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412832 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542833
2834 if (last_committed_entry_index_ != -1) {
2835 entries_[last_committed_entry_index_]->SetTransitionType(
2836 ui::PAGE_TRANSITION_RELOAD);
2837 }
[email protected]46bb5e9c2013-10-03 22:16:472838}
2839
[email protected]d202a7c2012-01-04 07:53:472840void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572841 DCHECK_LT(index, GetEntryCount());
2842 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312843 DiscardNonCommittedEntries();
2844
2845 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122846 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312847 last_committed_entry_index_--;
2848}
2849
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572850NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272851 // If there is no pending_entry_, there should be no pending_entry_index_.
2852 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2853
2854 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362855 // at that index. An exception is while a reload of a post commit error page
2856 // is ongoing; in that case pending entry will point to the entry replaced
2857 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272858 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362859 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2860 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272861
[email protected]022af742011-12-28 18:37:252862 return pending_entry_;
2863}
2864
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572865int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272866 // The pending entry index must always be less than the number of entries.
Rakina Zata Amni2322f4f82022-01-24 13:24:242867 // If there are no entries, it must be exactly -1.
arthursonzogni5c4c202d2017-04-25 23:41:272868 DCHECK_LT(pending_entry_index_, GetEntryCount());
Rakina Zata Amni2322f4f82022-01-24 13:24:242869 DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:552870 return pending_entry_index_;
2871}
2872
avi25764702015-06-23 15:43:372873void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572874 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362875 bool replace,
Dave Tapuska87696ae2021-11-18 18:48:312876 bool was_post_commit_error,
Rakina Zata Amnia4e27222021-12-22 01:05:002877 bool in_fenced_frame_tree,
2878 LoadCommittedDetails* commit_details) {
Dave Tapuska87696ae2021-11-18 18:48:312879 // Fenced frame trees should always have `ui::PAGE_TRANSITION_AUTO_SUBFRAME`
2880 // set because:
2881 // 1) They don't influence the history of the outer page.
2882 // 2) They are always replace only navigation (there is always only one entry
2883 // in their history stack).
2884 // 3) Are not top level navigations and appear similar to iframes.
2885 // Navigations of the fenced frame might create a new NavigationEntry, which
2886 // will call this function. Non fenced frame navigations will never have
2887 // `ui::PAGE_TRANSITION_AUTO_SUBFRAME` because they won't call
2888 // InsertOrReplaceEntry.
2889 DCHECK_EQ(in_fenced_frame_tree,
2890 ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2891 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202892
avi5cad4912015-06-19 05:25:442893 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2894 // need to keep continuity with the pending entry, so copy the pending entry's
2895 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2896 // then the renderer navigated on its own, independent of the pending entry,
2897 // so don't copy anything.
2898 if (pending_entry_ && pending_entry_index_ == -1)
2899 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202900
Rakina Zata Amnia4e27222021-12-22 01:05:002901 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]765b35502008-08-21 00:51:202902
creisee17e932015-07-17 17:56:222903 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362904 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102905 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572906 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362907 // If the new entry is a post-commit error page, we store the current last
2908 // committed entry to the side so that we can put it back when navigating
2909 // away from the error.
2910 if (was_post_commit_error) {
2911 DCHECK(!entry_replaced_by_post_commit_error_);
2912 entry_replaced_by_post_commit_error_ =
2913 std::move(entries_[last_committed_entry_index_]);
2914 }
dcheng36b6aec92015-12-26 06:16:362915 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222916 return;
2917 }
[email protected]765b35502008-08-21 00:51:202918
creis37979a62015-08-04 19:48:182919 // We shouldn't see replace == true when there's no committed entries.
2920 DCHECK(!replace);
2921
Michael Thiessen9b14d512019-09-23 21:19:472922 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202923
Shivani Sharmad8c8d652019-02-13 17:27:572924 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202925
dcheng36b6aec92015-12-26 06:16:362926 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202927 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292928}
2929
Shivani Sharmad8c8d652019-02-13 17:27:572930void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162931 if (entries_.size() < max_entry_count())
2932 return;
2933
2934 DCHECK_EQ(max_entry_count(), entries_.size());
2935 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572936 CHECK_EQ(pending_entry_index_, -1);
2937
2938 int index = 0;
Elly Fong-Jonesccc6d1f2021-06-14 18:32:422939 // Retrieve the oldest skippable entry.
2940 for (; index < GetEntryCount(); index++) {
2941 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2942 break;
Shivani Sharmad8c8d652019-02-13 17:27:572943 }
2944
2945 // If there is no skippable entry or if it is the last committed entry then
2946 // fall back to pruning the oldest entry. It is not safe to prune the last
2947 // committed entry.
2948 if (index == GetEntryCount() || index == last_committed_entry_index_)
2949 index = 0;
2950
2951 bool should_succeed = RemoveEntryAtIndex(index);
2952 DCHECK_EQ(true, should_succeed);
2953
2954 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252955}
2956
clamy3cb9bea92018-07-10 12:42:022957void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162958 ReloadType reload_type,
Nate Chapin45f620582021-09-30 17:45:432959 int sandboxed_source_frame_tree_node_id,
2960 bool is_browser_initiated) {
Alexander Timin3a92df72019-09-20 11:59:502961 TRACE_EVENT0("navigation",
2962 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272963 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022964 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362965 if (pending_entry_index_ != -1) {
2966 // The pending entry may not be in entries_ if a post-commit error page is
2967 // showing.
2968 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2969 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2970 }
Gyuyoung Kim107c2a02021-04-13 01:49:302971 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572972 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:012973 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:002974 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:572975 int nav_entry_id = pending_entry_->GetUniqueID();
2976
[email protected]83c2e232011-10-07 21:36:462977 // If we were navigating to a slow-to-commit page, and the user performs
2978 // a session history navigation to the last committed page, RenderViewHost
2979 // will force the throbber to start, but WebKit will essentially ignore the
2980 // navigation, and won't send a message to stop the throbber. To prevent this
2981 // from happening, we drop the navigation here and stop the slow-to-commit
2982 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022983 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:082984 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:272985 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
Takashi Toyoshimabf549472021-07-01 07:47:202986 frame_tree_.StopLoading();
[email protected]6a13a6c2011-12-20 21:47:122987
[email protected]83c2e232011-10-07 21:36:462988 DiscardNonCommittedEntries();
2989 return;
2990 }
2991
creisce0ef3572017-01-26 17:53:082992 // Compare FrameNavigationEntries to see which frames in the tree need to be
2993 // navigated.
clamy3cb9bea92018-07-10 12:42:022994 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2995 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Nate Chapin45f620582021-09-30 17:45:432996 FindFramesToNavigate(root, reload_type, is_browser_initiated,
2997 &same_document_loads, &different_document_loads);
creis4e2ecb72015-06-20 00:46:302998
2999 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573000 // We were unable to match any frames to navigate. This can happen if a
3001 // history navigation targets a subframe that no longer exists
3002 // (https://crbug.com/705550). In this case, we need to update the current
3003 // history entry to the pending one but keep the main document loaded. We
3004 // also need to ensure that observers are informed about the updated
3005 // current history entry (e.g., for greying out back/forward buttons), and
3006 // that renderer processes update their history offsets. The easiest way
3007 // to do all that is to schedule a "redundant" same-document navigation in
3008 // the main frame.
3009 //
3010 // Note that we don't want to remove this history entry, as it might still
3011 // be valid later, since a frame that it's targeting may be recreated.
3012 //
3013 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
3014 // need to repeat history navigations until finding the one that works.
3015 // Consider changing this behavior to keep looking for the first valid
3016 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:023017 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:423018 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573019 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:573020 ReloadType::NONE /* reload_type */,
3021 true /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433022 false /* is_history_navigation_in_new_child */,
3023 is_browser_initiated);
clamy3cb9bea92018-07-10 12:42:023024 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573025 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:023026 DiscardPendingEntry(false);
3027 return;
3028 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573029 same_document_loads.push_back(std::move(navigation_request));
3030
3031 // Sanity check that we never take this branch for any kinds of reloads,
3032 // as those should've queued a different-document load in the main frame.
3033 DCHECK(!is_forced_reload);
3034 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:303035 }
3036
Dave Tapuska8bfd84c2019-03-26 20:47:163037 // If |sandboxed_source_frame_node_id| is valid, then track whether this
3038 // navigation affects any frame outside the frame's subtree.
3039 if (sandboxed_source_frame_tree_node_id !=
3040 FrameTreeNode::kFrameTreeNodeInvalidId) {
3041 bool navigates_inside_tree =
3042 DoesSandboxNavigationStayWithinSubtree(
3043 sandboxed_source_frame_tree_node_id, same_document_loads) &&
3044 DoesSandboxNavigationStayWithinSubtree(
3045 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:503046 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:163047 // the number of pages that trigger this.
3048 FrameTreeNode* sandbox_source_frame_tree_node =
3049 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
3050 if (sandbox_source_frame_tree_node) {
3051 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
3052 sandbox_source_frame_tree_node->current_frame_host(),
3053 navigates_inside_tree
3054 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
3055 : blink::mojom::WebFeature::
3056 kSandboxBackForwardAffectsFramesOutsideSubtree);
3057 }
Dave Tapuska855c1e12019-08-23 20:45:523058
3059 // If the navigation occurred outside the tree discard it because
3060 // the sandboxed frame didn't have permission to navigate outside
3061 // its tree. If it is possible that the navigation is both inside and
3062 // outside the frame tree and we discard it entirely because we don't
3063 // want to end up in a history state that didn't exist before.
Dominic Farolino057440042022-01-19 18:18:143064 if (!navigates_inside_tree) {
Dave Tapuska855c1e12019-08-23 20:45:523065 DiscardPendingEntry(false);
3066 return;
3067 }
Dave Tapuska8bfd84c2019-03-26 20:47:163068 }
3069
Carlos Caballero539a421c2020-07-06 10:25:573070 // BackForwardCache:
3071 // Navigate immediately if the document is in the BackForwardCache.
3072 if (back_forward_cache_.GetEntry(nav_entry_id)) {
3073 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
3074 DCHECK_EQ(reload_type, ReloadType::NONE);
3075 auto navigation_request = CreateNavigationRequestFromEntry(
3076 root, pending_entry_, pending_entry_->GetFrameEntry(root),
3077 ReloadType::NONE, false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433078 false /* is_history_navigation_in_new_child */, is_browser_initiated);
Lukasz Anforowicz9ee83c272020-12-01 20:14:053079 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:573080
3081 return;
3082 }
3083
3084 // History navigation might try to reuse a specific BrowsingInstance, already
3085 // used by a page in the cache. To avoid having two different main frames that
3086 // live in the same BrowsingInstance, evict the all pages with this
3087 // BrowsingInstance from the cache.
3088 //
3089 // For example, take the following scenario:
3090 //
3091 // A1 = Some page on a.com
3092 // A2 = Some other page on a.com
3093 // B3 = An uncacheable page on b.com
3094 //
3095 // Then the following navigations occur:
3096 // A1->A2->B3->A1
3097 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
3098 // take its place) and A1 will be created in the same BrowsingInstance (and
3099 // SiteInstance), as A2.
3100 //
3101 // If we didn't do anything, both A1 and A2 would remain alive in the same
3102 // BrowsingInstance/SiteInstance, which is unsupported by
3103 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
3104 // A2 from the cache.
3105 if (pending_entry_->site_instance()) {
3106 back_forward_cache_.EvictFramesInRelatedSiteInstances(
3107 pending_entry_->site_instance());
3108 }
3109
clamy3cb9bea92018-07-10 12:42:023110 // This call does not support re-entrancy. See http://crbug.com/347742.
3111 CHECK(!in_navigate_to_pending_entry_);
3112 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:303113
arthursonzogni66f711c2019-10-08 14:40:363114 // It is not possible to delete the pending NavigationEntry while navigating
3115 // to it. Grab a reference to delay potential deletion until the end of this
3116 // function.
3117 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3118
creis4e2ecb72015-06-20 00:46:303119 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:023120 for (auto& item : same_document_loads) {
3121 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:053122 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:303123 }
clamy3cb9bea92018-07-10 12:42:023124 for (auto& item : different_document_loads) {
3125 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:053126 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:303127 }
clamy3cb9bea92018-07-10 12:42:023128
3129 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:303130}
3131
Alex Moshchuk3a4e77a2020-05-29 21:32:573132NavigationControllerImpl::HistoryNavigationAction
3133NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:303134 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:573135 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:423136 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:583137 // Only active and prerendered documents are allowed to navigate in their
3138 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:423139 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:013140 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:583141 // - If the document is in pending deletion, the browser already committed
3142 // to destroying this RenderFrameHost. See https://crbug.com/930278.
3143 // - If the document is in back-forward cache, it's not allowed to navigate
3144 // as it should remain frozen. Ignore the request and evict the document
3145 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:423146 //
Sreeja Kamishettydb8e2892021-03-10 09:30:583147 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:423148 // which should all be inactive as well.
Fergal Daly1336ac642021-09-14 15:13:113149 if (frame->current_frame_host()->IsInactiveAndDisallowActivation(
3150 DisallowActivationReasonId::kDetermineActionForHistoryNavigation)) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:423151 return HistoryNavigationAction::kStopLooking;
Fergal Daly1336ac642021-09-14 15:13:113152 }
Sreeja Kamishetty8eacabb2021-03-09 11:45:423153 }
arthursonzogni03f76152019-02-12 10:35:203154
Alex Moshchuk3a4e77a2020-05-29 21:32:573155 // If there's no last committed entry, there is no previous history entry to
3156 // compare against, so fall back to a different-document load. Note that we
3157 // should only reach this case for the root frame and not descend further
3158 // into subframes.
3159 if (!GetLastCommittedEntry()) {
3160 DCHECK(frame->IsMainFrame());
3161 return HistoryNavigationAction::kDifferentDocument;
3162 }
3163
3164 // Reloads should result in a different-document load. Note that reloads may
3165 // also happen via the |needs_reload_| mechanism where the reload_type is
3166 // NONE, so detect this by comparing whether we're going to the same
3167 // entry that we're currently on. Similarly to above, only main frames
3168 // should reach this. Note that subframes support reloads, but that's done
3169 // via a different path that doesn't involve FindFramesToNavigate (see
3170 // RenderFrameHost::Reload()).
3171 if (reload_type != ReloadType::NONE ||
3172 pending_entry_index_ == last_committed_entry_index_) {
3173 DCHECK(frame->IsMainFrame());
3174 return HistoryNavigationAction::kDifferentDocument;
3175 }
3176
Alex Moshchuk47d1a4bd2020-06-01 22:15:343177 // If there is no new FrameNavigationEntry for the frame, ignore the
3178 // load. For example, this may happen when going back to an entry before a
3179 // frame was created. Suppose we commit a same-document navigation that also
3180 // results in adding a new subframe somewhere in the tree. If we go back,
3181 // the new subframe will be missing a FrameNavigationEntry in the previous
3182 // NavigationEntry, but we shouldn't delete or change what's loaded in
3183 // it.
3184 //
Alex Moshchuke65c39272020-06-03 17:55:373185 // Note that in this case, there is no need to keep looking for navigations
3186 // in subframes, which would be missing FrameNavigationEntries as well.
3187 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:343188 // It's important to check this before checking |old_item| below, since both
3189 // might be null, and in that case we still shouldn't change what's loaded in
3190 // this frame. Note that scheduling any loads assumes that |new_item| is
3191 // non-null. See https://crbug.com/1088354.
3192 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3193 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:373194 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:343195
Charlie Reisa474fb62022-03-17 02:31:363196 // Use the RenderFrameHost's last committed FrameNavigationEntry to identify
3197 // which history item it is currently on, since this may be different than the
3198 // FrameNavigationEntry for the frame in the last committed NavigationEntry
3199 // (e.g., if a history navigation is targeting multiple frames and only some
3200 // have committed so far).
creis4e2ecb72015-06-20 00:46:303201 FrameNavigationEntry* old_item =
Charlie Reisa474fb62022-03-17 02:31:363202 frame->current_frame_host()->last_committed_frame_entry();
3203 if (!old_item) {
3204 // In cases where the RenderFrameHost does not have a FrameNavigationEntry,
3205 // fall back to the last committed NavigationEntry's record for this frame.
3206 // This may happen in cases like the initial state of the RenderFrameHost.
3207 // TODO(https://crbug.com/1304466): Ensure the RenderFrameHost always has an
3208 // accurate FrameNavigationEntry and eliminate this case.
3209 old_item = GetLastCommittedEntry()->GetFrameEntry(frame);
3210 }
3211 // If neither approach finds a FrameNavigationEntry, schedule a
3212 // different-document load.
3213 // TODO(https://crbug.com/608402): Remove this case.
Alex Moshchuk3a4e77a2020-05-29 21:32:573214 if (!old_item)
3215 return HistoryNavigationAction::kDifferentDocument;
3216
Alex Moshchuk3a4e77a2020-05-29 21:32:573217 // If the new item is not in the same SiteInstance, schedule a
3218 // different-document load. Newly restored items may not have a SiteInstance
3219 // yet, in which case it will be assigned on first commit.
3220 if (new_item->site_instance() &&
3221 new_item->site_instance() != old_item->site_instance())
3222 return HistoryNavigationAction::kDifferentDocument;
3223
3224 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:313225 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
3226 // that we do this even if the history navigation would not be modifying this
3227 // frame were it live.
3228 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:573229 return HistoryNavigationAction::kDifferentDocument;
3230
3231 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:313232 // Starting a navigation after a crash early-promotes the speculative
3233 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
3234 // committed yet, so we can not possibly perform a same-document history
3235 // navigation. The frame would need to be reloaded with a cross-document
3236 // navigation.
3237 if (!frame->current_frame_host()->has_committed_any_navigation())
3238 return HistoryNavigationAction::kDifferentDocument;
3239
creis54131692016-08-12 18:32:253240 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:493241 // sequence number but different item sequence number.
3242 if (new_item->document_sequence_number() ==
3243 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573244 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493245 }
avib48cb312016-05-05 21:35:003246
Alex Moshchuk3a4e77a2020-05-29 21:32:573247 // Otherwise, if both item and document sequence numbers differ, this
3248 // should be a different document load.
3249 return HistoryNavigationAction::kDifferentDocument;
3250 }
3251
3252 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373253 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573254 DCHECK_EQ(new_item->document_sequence_number(),
3255 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373256 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573257}
3258
3259void NavigationControllerImpl::FindFramesToNavigate(
3260 FrameTreeNode* frame,
3261 ReloadType reload_type,
Nate Chapin45f620582021-09-30 17:45:433262 bool is_browser_initiated,
Alex Moshchuk3a4e77a2020-05-29 21:32:573263 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3264 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3265 DCHECK(pending_entry_);
3266 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3267
3268 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3269
3270 if (action == HistoryNavigationAction::kSameDocument) {
3271 std::unique_ptr<NavigationRequest> navigation_request =
3272 CreateNavigationRequestFromEntry(
3273 frame, pending_entry_, new_item, reload_type,
3274 true /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433275 false /* is_history_navigation_in_new_child */,
3276 is_browser_initiated);
Alex Moshchuk3a4e77a2020-05-29 21:32:573277 if (navigation_request) {
3278 // Only add the request if was properly created. It's possible for the
3279 // creation to fail in certain cases, e.g. when the URL is invalid.
3280 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303281 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573282 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473283 std::unique_ptr<NavigationRequest> navigation_request =
3284 CreateNavigationRequestFromEntry(
3285 frame, pending_entry_, new_item, reload_type,
3286 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433287 false /* is_history_navigation_in_new_child */,
3288 is_browser_initiated);
Lei Zhang96031532019-10-10 19:05:473289 if (navigation_request) {
3290 // Only add the request if was properly created. It's possible for the
3291 // creation to fail in certain cases, e.g. when the URL is invalid.
3292 different_document_loads->push_back(std::move(navigation_request));
3293 }
3294 // For a different document, the subframes will be destroyed, so there's
3295 // no need to consider them.
3296 return;
Alex Moshchuke65c39272020-06-03 17:55:373297 } else if (action == HistoryNavigationAction::kStopLooking) {
3298 return;
creis4e2ecb72015-06-20 00:46:303299 }
3300
3301 for (size_t i = 0; i < frame->child_count(); i++) {
Nate Chapin45f620582021-09-30 17:45:433302 FindFramesToNavigate(frame->child_at(i), reload_type, is_browser_initiated,
3303 same_document_loads, different_document_loads);
creis4e2ecb72015-06-20 00:46:303304 }
3305}
3306
Harkiran Bolariaba823e42021-05-21 18:30:363307base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry(
clamy21718cc22018-06-13 13:34:243308 const LoadURLParams& params) {
3309 // Find the appropriate FrameTreeNode.
3310 FrameTreeNode* node = nullptr;
3311 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3312 !params.frame_name.empty()) {
3313 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003314 ? frame_tree_.FindByID(params.frame_tree_node_id)
3315 : frame_tree_.FindByName(params.frame_name);
Hayato Ito7a80db42021-07-05 06:18:543316 DCHECK(!node || node->frame_tree() == &frame_tree_);
clamy21718cc22018-06-13 13:34:243317 }
3318
3319 // If no FrameTreeNode was specified, navigate the main frame.
3320 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003321 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243322
Camille Lamy5193caa2018-10-12 11:59:423323 // Compute overrides to the LoadURLParams for |override_user_agent|,
3324 // |should_replace_current_entry| and |has_user_gesture| that will be used
3325 // both in the creation of the NavigationEntry and the NavigationRequest.
3326 // Ideally, the LoadURLParams themselves would be updated, but since they are
3327 // passed as a const reference, this is not possible.
3328 // TODO(clamy): When we only create a NavigationRequest, move this to
3329 // CreateNavigationRequestFromLoadURLParams.
3330 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3331 GetLastCommittedEntry());
3332
3333 // Don't allow an entry replacement if there is no entry to replace.
3334 // http://crbug.com/457149
Hayato Ito7a80db42021-07-05 06:18:543335 //
3336 // If there is an entry, an entry replacement must happen if the current
3337 // browsing context should maintain a trivial session history.
shivanigithubf405bf0d2021-11-05 17:58:333338 bool should_replace_current_entry =
3339 (params.should_replace_current_entry ||
3340 ShouldMaintainTrivialSessionHistory(node)) &&
3341 entries_.size();
Camille Lamy5193caa2018-10-12 11:59:423342
clamy21718cc22018-06-13 13:34:243343 // Javascript URLs should not create NavigationEntries. All other navigations
3344 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243345 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373346 std::unique_ptr<NavigationEntryImpl> entry =
3347 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3348 should_replace_current_entry,
3349 params.has_user_gesture);
clamy21718cc22018-06-13 13:34:243350 DiscardPendingEntry(false);
3351 SetPendingEntry(std::move(entry));
3352 }
3353
3354 // Renderer-debug URLs are sent to the renderer process immediately for
3355 // processing and don't need to create a NavigationRequest.
3356 // Note: this includes navigations to JavaScript URLs, which are considered
3357 // renderer-debug URLs.
3358 // Note: we intentionally leave the pending entry in place for renderer debug
3359 // URLs, unlike the cases below where we clear it if the navigation doesn't
3360 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303361 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483362 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3363 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153364 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483365 params.url, browser_context_)) {
3366 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363367 return nullptr;
Oleg Davydov2cc0167b2019-02-05 14:32:483368 }
3369
clamy21718cc22018-06-13 13:34:243370 HandleRendererDebugURL(node, params.url);
Harkiran Bolariaba823e42021-05-21 18:30:363371 return nullptr;
clamy21718cc22018-06-13 13:34:243372 }
3373
Antonio Sartori78a749f2020-11-30 12:03:393374 DCHECK(pending_entry_);
3375
clamy21718cc22018-06-13 13:34:243376 // Convert navigations to the current URL to a reload.
3377 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3378 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3379 // DevTools sometimes issues navigations to main frames that they do not
3380 // expect to see treated as reload, and it only works because they pass a
3381 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3382 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543383 ReloadType reload_type = params.reload_type;
3384 if (reload_type == ReloadType::NONE &&
3385 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043386 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243387 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243388 params.load_type ==
3389 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
Hayato Ito7a80db42021-07-05 06:18:543390 should_replace_current_entry, GetLastCommittedEntry())) {
clamy21718cc22018-06-13 13:34:243391 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473392 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393393
3394 // If this is a reload of an existing FrameNavigationEntry and we had a
3395 // policy container for it, then we should copy it into the pending entry,
3396 // so that it is copied to the navigation request in
3397 // CreateNavigationRequestFromLoadParams later.
3398 if (GetLastCommittedEntry()) {
3399 FrameNavigationEntry* previous_frame_entry =
3400 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193401 if (previous_frame_entry &&
3402 previous_frame_entry->policy_container_policies()) {
3403 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163404 previous_frame_entry->policy_container_policies()->Clone());
Antonio Sartori78a749f2020-11-30 12:03:393405 }
3406 }
3407 }
3408
3409 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
Rakina Zata Amniafd3c6582021-11-30 06:19:173410 // document, then we should copy the document polices from RenderFrameHost's
3411 // PolicyContainerHost. The NavigationRequest will create a new
3412 // PolicyContainerHost with the document policies from the |pending_entry_|,
3413 // and that PolicyContainerHost will be put in the final RenderFrameHost for
3414 // the navigation. This way, we ensure that we keep enforcing the right
3415 // policies on the initial empty document after the reload.
Rakina Zata Amni2322f4f82022-01-24 13:24:243416 if ((!GetLastCommittedEntry() || GetLastCommittedEntry()->IsInitialEntry()) &&
3417 params.url.IsAboutBlank()) {
Antonio Sartori78a749f2020-11-30 12:03:393418 if (node->current_frame_host() &&
3419 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193420 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163421 node->current_frame_host()
3422 ->policy_container_host()
3423 ->policies()
3424 .Clone());
Antonio Sartori78a749f2020-11-30 12:03:393425 }
clamy21718cc22018-06-13 13:34:243426 }
3427
3428 // navigation_ui_data should only be present for main frame navigations.
Ian Vollick1c6dd3e2022-04-13 02:06:263429 DCHECK(node->IsOutermostMainFrame() || !params.navigation_ui_data);
clamy21718cc22018-06-13 13:34:243430
Tsuyoshi Horo167ca6432022-03-09 05:16:393431 // This will be used to set the Navigation Timing API navigationStart
3432 // parameter for browser navigations in new tabs (intents, tabs opened through
3433 // "Open link in new tab"). If the navigation must wait on the current
3434 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3435 // will be updated when the BeforeUnload ack is received.
3436 const auto navigation_start_time = base::TimeTicks::Now();
3437
Camille Lamy5193caa2018-10-12 11:59:423438 std::unique_ptr<NavigationRequest> request =
3439 CreateNavigationRequestFromLoadParams(
3440 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143441 params.has_user_gesture, network::mojom::SourceLocation::New(),
Tsuyoshi Horo167ca6432022-03-09 05:16:393442 reload_type, pending_entry_, pending_entry_->GetFrameEntry(node),
3443 navigation_start_time);
clamy21718cc22018-06-13 13:34:243444
3445 // If the navigation couldn't start, return immediately and discard the
3446 // pending NavigationEntry.
3447 if (!request) {
3448 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363449 return nullptr;
clamy21718cc22018-06-13 13:34:243450 }
3451
Camille Lamy5193caa2018-10-12 11:59:423452#if DCHECK_IS_ON()
3453 // Safety check that NavigationRequest and NavigationEntry match.
3454 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3455#endif
3456
clamy21718cc22018-06-13 13:34:243457 // This call does not support re-entrancy. See http://crbug.com/347742.
3458 CHECK(!in_navigate_to_pending_entry_);
3459 in_navigate_to_pending_entry_ = true;
3460
arthursonzogni66f711c2019-10-08 14:40:363461 // It is not possible to delete the pending NavigationEntry while navigating
3462 // to it. Grab a reference to delay potential deletion until the end of this
3463 // function.
3464 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3465
Harkiran Bolariaba823e42021-05-21 18:30:363466 base::WeakPtr<NavigationHandle> created_navigation_handle(
3467 request->GetWeakPtr());
Lukasz Anforowicz9ee83c272020-12-01 20:14:053468 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243469
3470 in_navigate_to_pending_entry_ = false;
Harkiran Bolariaba823e42021-05-21 18:30:363471 return created_navigation_handle;
clamy21718cc22018-06-13 13:34:243472}
3473
clamyea99ea12018-05-28 13:54:233474void NavigationControllerImpl::HandleRendererDebugURL(
3475 FrameTreeNode* frame_tree_node,
3476 const GURL& url) {
3477 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243478 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3479 // the renderer process is not live, unless it is the initial navigation
3480 // of the tab.
clamyea99ea12018-05-28 13:54:233481 if (!IsInitialNavigation()) {
3482 DiscardNonCommittedEntries();
3483 return;
3484 }
Fergal Dalyecd3b0202020-06-25 01:57:373485 // The current frame is always a main frame. If IsInitialNavigation() is
3486 // true then there have been no navigations and any frames of this tab must
3487 // be in the same renderer process. If that has crashed then the only frame
3488 // that can be revived is the main frame.
3489 frame_tree_node->render_manager()
3490 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233491 }
3492 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3493}
3494
clamy21718cc22018-06-13 13:34:243495std::unique_ptr<NavigationEntryImpl>
3496NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3497 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423498 const LoadURLParams& params,
3499 bool override_user_agent,
3500 bool should_replace_current_entry,
3501 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393502 // Browser initiated navigations might not have a blob_url_loader_factory set
3503 // in params even if the navigation is to a blob URL. If that happens, lookup
3504 // the correct url loader factory to use here.
3505 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483506 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473507 // Resolve the blob URL in the storage partition associated with the target
3508 // frame. This is the storage partition the URL will be loaded in, and only
3509 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393510 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473511 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393512 }
3513
clamy21718cc22018-06-13 13:34:243514 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443515 // extra_headers in params are \n separated; navigation entries want \r\n.
3516 std::string extra_headers_crlf;
3517 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243518
3519 // For subframes, create a pending entry with a corresponding frame entry.
3520 if (!node->IsMainFrame()) {
Rakina Zata Amni2322f4f82022-01-24 13:24:243521 if (GetLastCommittedEntry()) {
3522 entry = GetLastCommittedEntry()->Clone();
3523 } else {
3524 // If there's no last committed entry, create an entry for about:blank
3525 // with a subframe entry for our destination.
3526 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
3527 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
3528 GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin,
3529 params.source_site_instance.get(), params.transition_type,
3530 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
3531 blob_url_loader_factory));
3532 }
clamy21718cc22018-06-13 13:34:243533 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:083534 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
clamy21718cc22018-06-13 13:34:243535 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Anton Bikineevf62d1bf2021-05-15 17:56:073536 params.url, absl::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203537 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393538 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:483539 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori78a749f2020-11-30 12:03:393540 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163541 // a conversion of a new navigation into a reload, we will set the right
3542 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193543 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243544 } else {
3545 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243546 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063547 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103548 params.source_site_instance.get(), params.transition_type,
3549 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Hayato Ito303654c2021-06-30 09:07:543550 blob_url_loader_factory));
clamy21718cc22018-06-13 13:34:243551 entry->set_source_site_instance(
3552 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3553 entry->SetRedirectChain(params.redirect_chain);
3554 }
3555
3556 // Set the FTN ID (only used in non-site-per-process, for tests).
3557 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243558 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423559 entry->SetIsOverridingUserAgent(override_user_agent);
3560 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543561 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243562
clamy21718cc22018-06-13 13:34:243563 switch (params.load_type) {
3564 case LOAD_TYPE_DEFAULT:
3565 break;
3566 case LOAD_TYPE_HTTP_POST:
3567 entry->SetHasPostData(true);
3568 entry->SetPostData(params.post_data);
3569 break;
3570 case LOAD_TYPE_DATA:
3571 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3572 entry->SetVirtualURL(params.virtual_url_for_data_url);
Xiaohan Wang7f8052e02022-01-14 18:44:283573#if BUILDFLAG(IS_ANDROID)
clamy21718cc22018-06-13 13:34:243574 entry->SetDataURLAsString(params.data_url_as_string);
3575#endif
3576 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3577 break;
clamy21718cc22018-06-13 13:34:243578 }
3579
3580 // TODO(clamy): NavigationEntry is meant for information that will be kept
3581 // after the navigation ended and therefore is not appropriate for
3582 // started_from_context_menu. Move started_from_context_menu to
3583 // NavigationUIData.
3584 entry->set_started_from_context_menu(params.started_from_context_menu);
3585
3586 return entry;
3587}
3588
clamyea99ea12018-05-28 13:54:233589std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423590NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3591 FrameTreeNode* node,
3592 const LoadURLParams& params,
3593 bool override_user_agent,
3594 bool should_replace_current_entry,
3595 bool has_user_gesture,
Antonio Sartori2f763d9d2021-04-21 10:04:143596 network::mojom::SourceLocationPtr source_location,
Camille Lamy5193caa2018-10-12 11:59:423597 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573598 NavigationEntryImpl* entry,
Tsuyoshi Horo167ca6432022-03-09 05:16:393599 FrameNavigationEntry* frame_entry,
Nan Lin944e9b4e2022-04-12 13:51:223600 base::TimeTicks navigation_start_time,
3601 absl::optional<bool> is_fenced_frame_opaque_url) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573602 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283603 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533604 // All renderer-initiated navigations must have an initiator_origin.
3605 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513606
Camille Lamy5193caa2018-10-12 11:59:423607 GURL url_to_load;
3608 GURL virtual_url;
Anton Bikineevf62d1bf2021-05-15 17:56:073609 absl::optional<url::Origin> origin_to_commit =
3610 frame_entry ? frame_entry->committed_origin() : absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:323611
Camille Lamy2baa8022018-10-19 16:43:173612 // For main frames, rewrite the URL if necessary and compute the virtual URL
3613 // that should be shown in the address bar.
Ian Vollick1c6dd3e2022-04-13 02:06:263614 // TODO(crbug.com/1314749): With MPArch there may be multiple main frames and
3615 // so IsMainFrame should not be used to identify subframes. Follow up to
3616 // confirm correctness.
3617 if (node->IsOutermostMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423618 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173619 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423620 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423621
Camille Lamy2baa8022018-10-19 16:43:173622 // For DATA loads, override the virtual URL.
3623 if (params.load_type == LOAD_TYPE_DATA)
3624 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423625
Camille Lamy2baa8022018-10-19 16:43:173626 if (virtual_url.is_empty())
3627 virtual_url = url_to_load;
3628
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573629 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283630
Aran Gilman249eb122019-12-02 23:32:463631 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3632 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3633 // return different results, leading to a different URL in the
3634 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3635 // pending NavigationEntry, as we'll only make one call to
3636 // RewriteUrlForNavigation.
3637 VLOG_IF(1, (url_to_load != frame_entry->url()))
3638 << "NavigationRequest and FrameEntry have different URLs: "
3639 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283640
Camille Lamy2baa8022018-10-19 16:43:173641 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423642 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173643 // NavigationRequest.
3644 } else {
3645 url_to_load = params.url;
3646 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243647 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173648 }
Camille Lamy5193caa2018-10-12 11:59:423649
Ehsan Karamad44fc72112019-02-26 18:15:473650 if (node->render_manager()->is_attaching_inner_delegate()) {
3651 // Avoid starting any new navigations since this node is now preparing for
3652 // attaching an inner delegate.
3653 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203654 }
Camille Lamy5193caa2018-10-12 11:59:423655
Ian Vollick1c6dd3e2022-04-13 02:06:263656 if (!IsValidURLForNavigation(node->IsOutermostMainFrame(), virtual_url,
3657 url_to_load))
Camille Lamy5193caa2018-10-12 11:59:423658 return nullptr;
3659
Kunihiko Sakamoto346a74e2021-03-10 08:57:483660 if (!DoesURLMatchOriginForNavigation(
3661 url_to_load, origin_to_commit,
Wang Hui14cac7a2022-01-28 06:07:473662 frame_entry->subresource_web_bundle_navigation_info(), entry,
Ian Vollick1c6dd3e2022-04-13 02:06:263663 node->IsOutermostMainFrame())) {
Nasko Oskov03912102019-01-11 00:21:323664 DCHECK(false) << " url:" << url_to_load
3665 << " origin:" << origin_to_commit.value();
3666 return nullptr;
3667 }
3668
danakjd83d706d2020-11-25 22:11:123669 // Look for a pending commit that is to another document in this
3670 // FrameTreeNode. If one exists, then the last committed URL will not be the
3671 // current URL by the time this navigation commits.
3672 bool has_pending_cross_document_commit =
3673 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
Miyoung Shina2dd6a42021-10-07 12:19:213674 bool is_currently_error_page = node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:123675
Minggang Wangb9f3fa92021-07-01 15:30:313676 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjb952ef12021-01-14 19:58:493677 /*old_url=*/node->current_url(),
3678 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3679 has_pending_cross_document_commit, is_currently_error_page,
3680 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423681
3682 // Create the NavigationParams based on |params|.
3683
Hiroki Nakagawa4ed61282021-06-18 05:37:233684 bool is_view_source_mode = entry->IsViewSourceMode();
3685 DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme));
Charlie Harrison8c113a32019-01-07 16:08:293686
Antonio Sartori6984c742021-08-26 08:03:413687 blink::NavigationDownloadPolicy download_policy = params.download_policy;
Hiroki Nakagawa4ed61282021-06-18 05:37:233688 // Update |download_policy| if the virtual URL is view-source.
Charlie Harrison8c113a32019-01-07 16:08:293689 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253690 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293691
Minggang Wangb9f3fa92021-07-01 15:30:313692 blink::mojom::CommonNavigationParamsPtr common_params =
3693 blink::mojom::CommonNavigationParams::New(
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513694 url_to_load, params.initiator_origin,
3695 blink::mojom::Referrer::New(params.referrer.url,
3696 params.referrer.policy),
Scott Violetcf6ea7e2021-06-09 21:09:213697 params.transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513698 should_replace_current_entry, params.base_url_for_data_url,
Tsuyoshi Horo167ca6432022-03-09 05:16:393699 navigation_start_time,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513700 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
Antonio Sartori2f763d9d2021-04-21 10:04:143701 params.post_data, std::move(source_location),
arthursonzogniaf7c62c52020-02-12 10:49:413702 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273703 false /* has_text_fragment_token */,
3704 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3705 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513706 false /* is_history_navigation_in_new_child_frame */,
Nan Lind91c8152021-10-21 16:22:373707 params.input_start, network::mojom::RequestDestination::kEmpty);
Camille Lamy5193caa2018-10-12 11:59:423708
Minggang Wangb9f3fa92021-07-01 15:30:313709 blink::mojom::CommitNavigationParamsPtr commit_params =
3710 blink::mojom::CommitNavigationParams::New(
Antonio Sartori3e8de6d2021-07-26 10:28:413711 frame_entry->committed_origin(),
3712 // The correct storage key will be computed before committing the
3713 // navigation.
3714 blink::StorageKey(), network::mojom::WebSandboxFlags(),
arthursonzognid5a8d0b2021-03-11 17:36:433715 override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323716 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143717 std::vector<net::RedirectInfo>(),
3718 std::string() /* post_content_type */, common_params->url,
3719 common_params->method, params.can_load_local_resources,
Minggang Wangb9f3fa92021-07-01 15:30:313720 frame_entry->page_state().ToEncodedData(), entry->GetUniqueID(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143721 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3722 -1 /* pending_history_list_offset */,
3723 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3724 params.should_clear_history_list ? 0 : GetEntryCount(),
3725 false /* was_discarded */, is_view_source_mode,
Minggang Wangb9f3fa92021-07-01 15:30:313726 params.should_clear_history_list,
3727 blink::mojom::NavigationTiming::New(),
Minggang Wangf59db47b2021-06-16 01:56:223728 blink::mojom::WasActivatedOption::kUnknown,
Lucas Furukawa Gadania9c45682019-07-31 22:05:143729 base::UnguessableToken::Create() /* navigation_token */,
Minggang Wang7ee0c742021-06-16 16:16:513730 std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(),
Xiaohan Wang7f8052e02022-01-14 18:44:283731#if BUILDFLAG(IS_ANDROID)
Lucas Furukawa Gadania9c45682019-07-31 22:05:143732 std::string(), /* data_url_as_string */
3733#endif
arthursonzogni14379782020-05-15 09:09:273734 !params.is_renderer_initiated, /* is_browser_initiated */
Tsuyoshi Horoe86d7702019-11-29 01:52:473735 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533736 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173737 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073738 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163739 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333740 false /* origin_agent_cluster */,
Daniel Vogelheim243df9f2022-02-22 10:32:033741 true /* origin_agent_cluster_left_as_default */,
Maks Orlovichc66745a2020-06-30 17:40:023742 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213743 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Nate Chapind1fe3612021-04-16 20:45:573744 false /* is_cross_browsing_instance */, nullptr /* old_page_info */,
3745 -1 /* http_response_code */,
Domenic Denicolacd30f5f82022-03-16 21:48:013746 blink::mojom::NavigationApiHistoryEntryArrays::New(),
Matt Menke4e209082021-11-09 04:59:313747 std::vector<GURL>() /* early_hints_preloaded_resources */,
Clark DuVall8ee487a22021-11-10 02:25:583748 absl::nullopt /* ad_auction_components */,
shivanigithubc7b97ca2022-04-05 19:41:403749 /*fenced_frame_reporting_metadata=*/nullptr,
Clark DuVall8ee487a22021-11-10 02:25:583750 // This timestamp will be populated when the commit IPC is sent.
W. James MacLean81b8d01f2022-01-25 20:50:593751 base::TimeTicks() /* commit_sent */, false /* anonymous */,
Miyoung Shinaa33e272022-03-18 09:34:453752 std::string() /* srcdoc_value */, false /* should_load_data_url */);
Xiaohan Wang7f8052e02022-01-14 18:44:283753#if BUILDFLAG(IS_ANDROID)
Camille Lamy5193caa2018-10-12 11:59:423754 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143755 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423756 }
3757#endif
3758
Lucas Furukawa Gadania9c45682019-07-31 22:05:143759 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423760
3761 // A form submission may happen here if the navigation is a renderer-initiated
3762 // form submission that took the OpenURL path.
3763 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3764
3765 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3766 std::string extra_headers_crlf;
3767 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093768
3769 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143770 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083771 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453772 params.initiator_frame_token.has_value()
3773 ? &(params.initiator_frame_token.value())
3774 : nullptr,
3775 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3776 request_body,
John Delaney50425f82020-04-07 16:26:213777 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
Nan Lin944e9b4e2022-04-12 13:51:223778 params.impression, params.is_pdf, is_fenced_frame_opaque_url);
Yao Xiaodc5ed102019-06-04 19:19:093779 navigation_request->set_from_download_cross_origin_redirect(
3780 params.from_download_cross_origin_redirect);
W. James MacLean00568d72022-02-24 19:36:553781 navigation_request->set_force_new_browsing_instance(
3782 params.force_new_browsing_instance);
Yao Xiaodc5ed102019-06-04 19:19:093783 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423784}
3785
3786std::unique_ptr<NavigationRequest>
3787NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233788 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573789 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233790 FrameNavigationEntry* frame_entry,
3791 ReloadType reload_type,
3792 bool is_same_document_history_load,
Nate Chapin45f620582021-09-30 17:45:433793 bool is_history_navigation_in_new_child_frame,
3794 bool is_browser_initiated) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343795 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233796 GURL dest_url = frame_entry->url();
Rakina Zata Amnif297a802022-01-18 03:53:433797 // We should never navigate to an existing initial NavigationEntry that is the
3798 // initial NavigationEntry for the initial empty document that hasn't been
3799 // overridden by the synchronous about:blank commit, to preserve previous
3800 // behavior where trying to reload when the main frame is on the initial empty
3801 // document won't result in a navigation.
3802 // See also https://crbug.com/1277414.
3803 DCHECK(!entry->IsInitialEntryNotForSynchronousAboutBlank());
Anton Bikineevf62d1bf2021-05-15 17:56:073804 absl::optional<url::Origin> origin_to_commit =
Nasko Oskov03912102019-01-11 00:21:323805 frame_entry->committed_origin();
3806
clamyea99ea12018-05-28 13:54:233807 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013808 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573809 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233810 // We may have been redirected when navigating to the current URL.
3811 // Use the URL the user originally intended to visit as signaled by the
3812 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013813 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573814 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233815 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323816 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233817 }
3818
Ehsan Karamad44fc72112019-02-26 18:15:473819 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3820 // Avoid starting any new navigations since this node is now preparing for
3821 // attaching an inner delegate.
3822 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203823 }
3824
Ian Vollick1c6dd3e2022-04-13 02:06:263825 if (!IsValidURLForNavigation(frame_tree_node->IsOutermostMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573826 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233827 return nullptr;
3828 }
3829
Kunihiko Sakamoto346a74e2021-03-10 08:57:483830 if (!DoesURLMatchOriginForNavigation(
3831 dest_url, origin_to_commit,
Wang Hui14cac7a2022-01-28 06:07:473832 frame_entry->subresource_web_bundle_navigation_info(), entry,
Ian Vollick1c6dd3e2022-04-13 02:06:263833 frame_tree_node->IsOutermostMainFrame())) {
Kunihiko Sakamoto346a74e2021-03-10 08:57:483834 DCHECK(false) << " url:" << dest_url
Wang Hui14cac7a2022-01-28 06:07:473835 << " base_url_for_data_url: " << entry->GetBaseURLForDataURL()
Kunihiko Sakamoto346a74e2021-03-10 08:57:483836 << " origin:" << origin_to_commit.value();
Nasko Oskov03912102019-01-11 00:21:323837 return nullptr;
3838 }
3839
clamyea99ea12018-05-28 13:54:233840 // This will be used to set the Navigation Timing API navigationStart
3841 // parameter for browser navigations in new tabs (intents, tabs opened through
3842 // "Open link in new tab"). If the navigation must wait on the current
3843 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3844 // will be updated when the BeforeUnload ack is received.
3845 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233846
danakjd83d706d2020-11-25 22:11:123847 // Look for a pending commit that is to another document in this
3848 // FrameTreeNode. If one exists, then the last committed URL will not be the
3849 // current URL by the time this navigation commits.
3850 bool has_pending_cross_document_commit =
3851 frame_tree_node->render_manager()
3852 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493853 bool is_currently_error_page =
Miyoung Shina2dd6a42021-10-07 12:19:213854 frame_tree_node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:123855
Minggang Wangb9f3fa92021-07-01 15:30:313856 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123857 /*old_url=*/frame_tree_node->current_url(),
3858 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493859 has_pending_cross_document_commit, is_currently_error_page,
3860 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423861
3862 // A form submission may happen here if the navigation is a
3863 // back/forward/reload navigation that does a form resubmission.
3864 scoped_refptr<network::ResourceRequestBody> request_body;
3865 std::string post_content_type;
3866 if (frame_entry->method() == "POST") {
3867 request_body = frame_entry->GetPostData(&post_content_type);
3868 // Might have a LF at end.
Peter Kastingb53b81912021-04-28 19:23:303869 post_content_type = std::string(
3870 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL));
Camille Lamy5193caa2018-10-12 11:59:423871 }
3872
3873 // Create the NavigationParams based on |entry| and |frame_entry|.
Minggang Wangb9f3fa92021-07-01 15:30:313874 blink::mojom::CommonNavigationParamsPtr common_params =
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513875 entry->ConstructCommonNavigationParams(
3876 *frame_entry, request_body, dest_url,
3877 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
Tarun Bansalbcd62c82022-01-18 17:27:383878 navigation_type, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533879 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513880 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553881 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423882
3883 // TODO(clamy): |intended_as_new_entry| below should always be false once
3884 // Reload no longer leads to this being called for a pending NavigationEntry
3885 // of index -1.
Minggang Wangb9f3fa92021-07-01 15:30:313886 blink::mojom::CommitNavigationParamsPtr commit_params =
Lucas Furukawa Gadania9c45682019-07-31 22:05:143887 entry->ConstructCommitNavigationParams(
3888 *frame_entry, common_params->url, origin_to_commit,
3889 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3890 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533891 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3892 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143893 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423894
W. James MacLean81b8d01f2022-01-25 20:50:593895 if (common_params->url.IsAboutSrcdoc())
3896 commit_params->srcdoc_value = frame_tree_node->srcdoc_value();
clamyea99ea12018-05-28 13:54:233897 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143898 frame_tree_node, std::move(common_params), std::move(commit_params),
Nate Chapin45f620582021-09-30 17:45:433899 is_browser_initiated, false /* was_opener_suppressed */,
Takashi Toyoshimae87b7be2021-01-22 11:51:083900 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453901 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3902 entry->extra_headers(), frame_entry, entry, request_body,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473903 nullptr /* navigation_ui_data */, absl::nullopt /* impression */,
3904 false /* is_pdf */);
clamyea99ea12018-05-28 13:54:233905}
3906
[email protected]d202a7c2012-01-04 07:53:473907void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213908 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273909 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403910
Takashi Toyoshimaea534ef22021-07-21 03:27:593911 // We need to notify the ssl_manager_ before the WebContents so the
[email protected]df1af242009-05-01 00:11:403912 // location bar will have up-to-date information about the security style
3913 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133914 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403915
Rakina Zata Amnidaa84f62022-02-17 00:55:313916 bool should_fire_navigation_state_changed = true;
3917#if BUILDFLAG(IS_ANDROID)
3918 if (details && details->should_stay_as_initial_entry) {
3919 // For initial NavigationEntries, only fire NavigationStateChanged() if the
3920 // embedder wants to hear about it.
3921 should_fire_navigation_state_changed =
3922 !GetContentClient()
3923 ->browser()
3924 ->ShouldIgnoreInitialNavigationEntryNavigationStateChangedForLegacySupport();
3925 }
3926#endif
3927 if (should_fire_navigation_state_changed)
3928 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
3929
[email protected]ec6c05f2013-10-23 18:41:573930 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463931
[email protected]b0f724c2013-09-05 04:21:133932 // TODO(avi): Remove. http://crbug.com/170921
3933 NotificationDetails notification_details =
3934 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153935 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3936 Source<NavigationController>(this),
3937 notification_details);
initial.commit09911bf2008-07-26 23:55:293938}
3939
initial.commit09911bf2008-07-26 23:55:293940// static
[email protected]d202a7c2012-01-04 07:53:473941size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233942 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153943 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:213944 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233945}
3946
[email protected]d202a7c2012-01-04 07:53:473947void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223948 if (is_active && needs_reload_)
3949 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293950}
3951
[email protected]d202a7c2012-01-04 07:53:473952void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293953 if (!needs_reload_)
3954 return;
3955
Bo Liucdfa4b12018-11-06 00:21:443956 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3957 needs_reload_type_);
3958
initial.commit09911bf2008-07-26 23:55:293959 // Calling Reload() results in ignoring state, and not loading.
3960 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3961 // cached state.
avicc872d7242015-08-19 21:26:343962 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163963 NavigateToExistingPendingEntry(ReloadType::NONE,
Nate Chapin45f620582021-09-30 17:45:433964 FrameTreeNode::kFrameTreeNodeInvalidId,
3965 true /* is_browser_initiated */);
Rakina Zata Amni2322f4f82022-01-24 13:24:243966 } else if (last_committed_entry_index_ != -1 &&
3967 !GetLastCommittedEntry()
Rakina Zata Amnif297a802022-01-18 03:53:433968 ->IsInitialEntryNotForSynchronousAboutBlank()) {
arthursonzogni5c4c202d2017-04-25 23:41:273969 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343970 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163971 NavigateToExistingPendingEntry(ReloadType::NONE,
Nate Chapin45f620582021-09-30 17:45:433972 FrameTreeNode::kFrameTreeNodeInvalidId,
3973 true /* is_browser_initiated */);
avicc872d7242015-08-19 21:26:343974 } else {
Rakina Zata Amnif297a802022-01-18 03:53:433975 // We should never navigate to an existing initial NavigationEntry that is
3976 // the initial NavigationEntry for the initial empty document that hasn't
3977 // been overridden by the synchronous about:blank commit, to preserve
3978 // legacy behavior where trying to reload when the main frame is on the
3979 // initial empty document won't result in a navigation. See also
3980 // https://crbug.com/1277414. If there is something to reload, the
3981 // successful reload will clear the |needs_reload_| flag. Otherwise, just do
3982 // it here.
avicc872d7242015-08-19 21:26:343983 needs_reload_ = false;
3984 }
initial.commit09911bf2008-07-26 23:55:293985}
3986
Kevin McNeeccca6172021-10-19 17:11:143987base::WeakPtr<NavigationHandle>
3988NavigationControllerImpl::LoadPostCommitErrorPage(
Carlos IL42b416592019-10-07 23:10:363989 RenderFrameHost* render_frame_host,
3990 const GURL& url,
3991 const std::string& error_page_html,
3992 net::Error error) {
Rakina Zata Amni919b7922020-12-11 09:03:133993 RenderFrameHostImpl* rfhi =
3994 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:583995
3996 // Only active documents can load post-commit error pages:
3997 // - If the document is in pending deletion, the browser already committed to
3998 // destroying this RenderFrameHost so ignore loading the error page.
3999 // - If the document is in back-forward cache, it's not allowed to navigate as
4000 // it should remain frozen. Ignore the request and evict the document from
4001 // back-forward cache.
4002 // - If the document is prerendering, it can navigate but when loading error
4003 // pages, cancel prerendering.
Fergal Daly1336ac642021-09-14 15:13:114004 if (rfhi->IsInactiveAndDisallowActivation(
4005 DisallowActivationReasonId::kLoadPostCommitErrorPage)) {
Kevin McNeeccca6172021-10-19 17:11:144006 return nullptr;
Fergal Daly1336ac642021-09-14 15:13:114007 }
Sreeja Kamishettydb8e2892021-03-10 09:30:584008
Rakina Zata Amni919b7922020-12-11 09:03:134009 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:414010
Minggang Wangb9f3fa92021-07-01 15:30:314011 blink::mojom::CommonNavigationParamsPtr common_params =
Minggang Wanga13c796e2021-07-02 05:54:434012 blink::CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:594013 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
4014 // the frame actually committed (e.g. iframe with "src" set to a
4015 // slow-responding URL). We should rewrite the URL to about:blank in this
4016 // case, as the renderer will only think a page is an error page if it has a
4017 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:134018 common_params->url = url.is_empty() ? GURL("about:blank") : url;
Minggang Wangb9f3fa92021-07-01 15:30:314019 blink::mojom::CommitNavigationParamsPtr commit_params =
Minggang Wanga13c796e2021-07-02 05:54:434020 blink::CreateCommitNavigationParams();
Antonio Sartori58591c892021-04-21 06:54:334021 commit_params->original_url = common_params->url;
John Delaney131ad362019-08-08 21:57:414022
arthursonzogni70ac7302020-05-28 08:49:054023 // Error pages have a fully permissive FramePolicy.
4024 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
4025 // error pages.
4026 commit_params->frame_policy = blink::FramePolicy();
4027
John Delaney131ad362019-08-08 21:57:414028 std::unique_ptr<NavigationRequest> navigation_request =
4029 NavigationRequest::CreateBrowserInitiated(
4030 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:084031 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:194032 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:454033 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:064034 "" /* extra_headers */, nullptr /* frame_entry */,
4035 nullptr /* entry */, nullptr /* post_body */,
Daniel Hosseinianf0fbfb42021-09-08 02:20:474036 nullptr /* navigation_ui_data */, absl::nullopt /* impression */,
4037 false /* is_pdf */);
Carlos IL42b416592019-10-07 23:10:364038 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:414039 navigation_request->set_net_error(error);
4040 node->CreatedNavigationRequest(std::move(navigation_request));
4041 DCHECK(node->navigation_request());
Kevin McNeeccca6172021-10-19 17:11:144042
4043 // Calling BeginNavigation may destroy the NavigationRequest.
4044 base::WeakPtr<NavigationRequest> created_navigation_request(
4045 node->navigation_request()->GetWeakPtr());
John Delaney131ad362019-08-08 21:57:414046 node->navigation_request()->BeginNavigation();
Kevin McNeeccca6172021-10-19 17:11:144047 return created_navigation_request;
John Delaney131ad362019-08-08 21:57:414048}
4049
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574050void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:214051 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:094052 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:154053 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:144054 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:294055}
4056
[email protected]d202a7c2012-01-04 07:53:474057void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:364058 RestoreType type) {
Charlie Reis23c26da2022-01-29 00:57:474059 // TODO(https://crbug.com/1287624): Don't allow an index of -1, which would
4060 // represent a no-committed-entry state.
4061 DCHECK(selected_index >= -1 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:364062 ConfigureEntriesForRestore(&entries_, type);
Charlie Reis23c26da2022-01-29 00:57:474063 // TODO(https://crbug.com/1287624): This will be pointing to the wrong entry
4064 // if `entries_` contains pre-existing entries from the NavigationController
4065 // before restore, which would not be removed and will be at the front of the
4066 // entries list, causing the index to be off by the amount of pre-existing
4067 // entries in the list. Fix this to point to the correct entry.
initial.commit09911bf2008-07-26 23:55:294068 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:294069}
[email protected]765b35502008-08-21 00:51:204070
arthursonzogni69a6a1b2019-09-17 09:23:004071void NavigationControllerImpl::DiscardNonCommittedEntries() {
Rakina Zata Amnia4e27222021-12-22 01:05:004072 DiscardNonCommittedEntriesWithCommitDetails(nullptr /* commit_details */);
4073}
4074
4075void NavigationControllerImpl::DiscardNonCommittedEntriesWithCommitDetails(
4076 LoadCommittedDetails* commit_details) {
Michael Thiessen9b14d512019-09-23 21:19:474077 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:104078 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
4079 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:294080 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:474081 return;
Michael Thiessenc5676d22019-09-25 22:32:104082 }
avi45a72532015-04-07 21:01:454083 DiscardPendingEntry(false);
Rakina Zata Amnidaa84f62022-02-17 00:55:314084
4085 if (!delegate_)
4086 return;
4087
4088 bool should_fire_navigation_state_changed = true;
4089#if BUILDFLAG(IS_ANDROID)
4090 if (commit_details && commit_details->should_stay_as_initial_entry) {
4091 // For initial NavigationEntries, only fire NavigationStateChanged() if the
4092 // embedder wants to hear about it.
4093 should_fire_navigation_state_changed =
4094 !GetContentClient()
4095 ->browser()
4096 ->ShouldIgnoreInitialNavigationEntryNavigationStateChangedForLegacySupport();
Rakina Zata Amniddf10502022-01-15 02:56:554097 }
Rakina Zata Amnidaa84f62022-02-17 00:55:314098#endif
4099 if (should_fire_navigation_state_changed)
4100 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:484101}
4102
avi7c6f35e2015-05-08 17:52:384103int NavigationControllerImpl::GetEntryIndexWithUniqueID(
4104 int nav_entry_id) const {
4105 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
4106 if (entries_[i]->GetUniqueID() == nav_entry_id)
4107 return i;
4108 }
4109 return -1;
4110}
4111
[email protected]d202a7c2012-01-04 07:53:474112void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574113 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:254114 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:574115 DCHECK_LE(max_index, source->GetEntryCount());
Nate Chapin214a86a2021-06-21 20:35:574116 std::unique_ptr<NavigationEntryRestoreContextImpl> context =
4117 std::make_unique<NavigationEntryRestoreContextImpl>();
[email protected]e1cd5452010-08-26 18:03:254118 for (int i = 0; i < max_index; i++) {
Nate Chapin9f169072021-06-09 19:32:374119 // Normally, cloning a NavigationEntryImpl results in sharing
4120 // FrameNavigationEntries between the original and the clone. However, when
4121 // cloning from a different NavigationControllerImpl, we want to fork the
4122 // FrameNavigationEntries.
Nate Chapin9f169072021-06-09 19:32:374123 entries_.insert(entries_.begin() + i,
Nate Chapin214a86a2021-06-21 20:35:574124 source->entries_[i]->CloneWithoutSharing(context.get()));
[email protected]e1cd5452010-08-26 18:03:254125 }
arthursonzogni5c4c202d2017-04-25 23:41:274126 DCHECK(pending_entry_index_ == -1 ||
4127 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:254128}
[email protected]c5b88d82012-10-06 17:03:334129
4130void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:184131 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:334132 get_timestamp_callback_ = get_timestamp_callback;
4133}
[email protected]8ff00d72012-10-23 19:12:214134
Shivani Sharmaffb32b82019-04-09 16:58:474135// History manipulation intervention:
4136void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:474137 bool replace_entry,
4138 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:404139 bool is_renderer_initiated,
4140 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:454141 // Note that for a subframe, previous_document_was_activated is true if the
4142 // gesture happened in any subframe (propagated to main frame) or in the main
4143 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:474144 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:274145 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:474146 return;
4147 }
4148 if (last_committed_entry_index_ == -1)
4149 return;
4150
Shivani Sharmac4cc8922019-04-18 03:11:174151 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:474152
Alexander Timine3ec4192020-04-20 16:39:404153 // Log UKM with the URL we are navigating away from.
4154 ukm::builders::HistoryManipulationIntervention(
4155 previous_page_load_ukm_source_id)
4156 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:474157}
4158
Shivani Sharmac4cc8922019-04-18 03:11:174159void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
4160 int reference_index,
4161 bool skippable) {
4162 auto* reference_entry = GetEntryAtIndex(reference_index);
4163 reference_entry->set_should_skip_on_back_forward_ui(skippable);
4164
4165 int64_t document_sequence_number =
4166 reference_entry->root_node()->frame_entry->document_sequence_number();
4167 for (int index = 0; index < GetEntryCount(); index++) {
4168 auto* entry = GetEntryAtIndex(index);
4169 if (entry->root_node()->frame_entry->document_sequence_number() ==
4170 document_sequence_number) {
4171 entry->set_should_skip_on_back_forward_ui(skippable);
4172 }
4173 }
4174}
4175
arthursonzogni66f711c2019-10-08 14:40:364176std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
4177NavigationControllerImpl::ReferencePendingEntry() {
4178 DCHECK(pending_entry_);
4179 auto pending_entry_ref =
4180 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
4181 pending_entry_refs_.insert(pending_entry_ref.get());
4182 return pending_entry_ref;
4183}
4184
4185void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
4186 // Ignore refs that don't correspond to the current pending entry.
4187 auto it = pending_entry_refs_.find(ref);
4188 if (it == pending_entry_refs_.end())
4189 return;
4190 pending_entry_refs_.erase(it);
4191
4192 if (!pending_entry_refs_.empty())
4193 return;
4194
4195 // The pending entry may be deleted before the last PendingEntryRef.
4196 if (!pending_entry_)
4197 return;
4198
4199 // We usually clear the pending entry when the matching NavigationRequest
4200 // fails, so that an arbitrary URL isn't left visible above a committed page.
4201 //
4202 // However, we do preserve the pending entry in some cases, such as on the
4203 // initial navigation of an unmodified blank tab. We also allow the delegate
4204 // to say when it's safe to leave aborted URLs in the omnibox, to let the
4205 // user edit the URL and try again. This may be useful in cases that the
4206 // committed page cannot be attacker-controlled. In these cases, we still
4207 // allow the view to clear the pending entry and typed URL if the user
4208 // requests (e.g., hitting Escape with focus in the address bar).
4209 //
4210 // Do not leave the pending entry visible if it has an invalid URL, since this
4211 // might be formatted in an unexpected or unsafe way.
4212 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:364213 bool should_preserve_entry =
4214 (pending_entry_ == GetVisibleEntry()) &&
4215 pending_entry_->GetURL().is_valid() &&
4216 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
4217 if (should_preserve_entry)
4218 return;
4219
4220 DiscardPendingEntry(true);
4221 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
4222}
4223
Titouan Rigoudy6ec70402021-02-02 15:42:194224std::unique_ptr<PolicyContainerPolicies>
4225NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:394226 RenderFrameHostImpl* rfh,
4227 bool is_same_document,
Rakina Zata Amniafd3c6582021-11-30 06:19:174228 const GURL& url) {
Antonio Sartori78a749f2020-11-30 12:03:394229 if (is_same_document) {
Rakina Zata Amni2322f4f82022-01-24 13:24:244230 // TODO(https://crbug.com/524208): Remove this nullptr check when we can
4231 // ensure we always have a FrameNavigationEntry here.
4232 if (!GetLastCommittedEntry())
4233 return nullptr;
Charlie Reis73e356242021-04-02 17:10:314234
Antonio Sartori78a749f2020-11-30 12:03:394235 FrameNavigationEntry* previous_frame_entry =
4236 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
4237
4238 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
4239 // ensure we always have a FrameNavigationEntry here.
4240 if (!previous_frame_entry)
4241 return nullptr;
4242
Titouan Rigoudy6ec70402021-02-02 15:42:194243 const PolicyContainerPolicies* previous_policies =
4244 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:394245
Titouan Rigoudy6ec70402021-02-02 15:42:194246 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:394247 return nullptr;
4248
4249 // Make a copy of the policy container for the new FrameNavigationEntry.
Antonio Sartori5d09b30f2021-03-02 09:27:164250 return previous_policies->Clone();
Antonio Sartori78a749f2020-11-30 12:03:394251 }
4252
Antonio Sartori4f5373792021-05-31 10:56:474253 return rfh->policy_container_host()->policies().Clone();
Antonio Sartori78a749f2020-11-30 12:03:394254}
4255
Hayato Ito2c8c08d02021-06-23 03:38:434256void NavigationControllerImpl::BroadcastHistoryOffsetAndLength() {
Carlos Caballeroede6f8c2021-01-28 11:01:504257 OPTIONAL_TRACE_EVENT2(
Hayato Ito2c8c08d02021-06-23 03:38:434258 "content", "NavigationControllerImpl::BroadcastHistoryOffsetAndLength",
4259 "history_offset", GetLastCommittedEntryIndex(), "history_length",
4260 GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:504261
4262 auto callback = base::BindRepeating(
4263 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
4264 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
4265 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
4266 }
4267 },
Hayato Ito2c8c08d02021-06-23 03:38:434268 GetLastCommittedEntryIndex(), GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:504269 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
4270}
4271
4272void NavigationControllerImpl::DidAccessInitialMainDocument() {
4273 // We may have left a failed browser-initiated navigation in the address bar
4274 // to let the user edit it and try again. Clear it now that content might
4275 // show up underneath it.
4276 if (!frame_tree_.IsLoading() && GetPendingEntry())
4277 DiscardPendingEntry(false);
4278
4279 // Update the URL display.
4280 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
4281}
4282
4283void NavigationControllerImpl::UpdateStateForFrame(
4284 RenderFrameHostImpl* rfhi,
4285 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:564286 OPTIONAL_TRACE_EVENT1("content",
4287 "NavigationControllerImpl::UpdateStateForFrame",
4288 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:504289 // The state update affects the last NavigationEntry associated with the given
4290 // |render_frame_host|. This may not be the last committed NavigationEntry (as
4291 // in the case of an UpdateState from a frame being swapped out). We track
4292 // which entry this is in the RenderFrameHost's nav_entry_id.
4293 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
4294 if (!entry)
4295 return;
4296
4297 FrameNavigationEntry* frame_entry =
4298 entry->GetFrameEntry(rfhi->frame_tree_node());
4299 if (!frame_entry)
4300 return;
4301
4302 // The SiteInstance might not match if we do a cross-process navigation with
4303 // replacement (e.g., auto-subframe), in which case the swap out of the old
4304 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4305 // has already been replaced and destroyed.
4306 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4307 return;
4308
4309 if (page_state == frame_entry->page_state())
4310 return; // Nothing to update.
4311
4312 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4313
4314 // The document_sequence_number and item_sequence_number recorded in the
4315 // FrameNavigationEntry should not differ from the one coming with the update,
4316 // since it must come from the same document. Do not update it if a difference
4317 // is detected, as this indicates that |frame_entry| is not the correct one.
4318 blink::ExplodedPageState exploded_state;
4319 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4320 return;
4321
4322 if (exploded_state.top.document_sequence_number !=
4323 frame_entry->document_sequence_number() ||
4324 exploded_state.top.item_sequence_number !=
4325 frame_entry->item_sequence_number()) {
4326 return;
4327 }
4328
4329 frame_entry->SetPageState(page_state);
4330 NotifyEntryChanged(entry);
4331}
4332
Domenic Denicolacd30f5f82022-03-16 21:48:014333std::vector<blink::mojom::NavigationApiHistoryEntryPtr>
4334NavigationControllerImpl::PopulateSingleNavigationApiHistoryEntryVector(
Nate Chapind1fe3612021-04-16 20:45:574335 Direction direction,
4336 int entry_index,
4337 const url::Origin& pending_origin,
4338 FrameTreeNode* node,
4339 SiteInstance* site_instance,
Nate Chapin63db0d12022-01-20 22:03:304340 int64_t pending_item_sequence_number,
4341 int64_t pending_document_sequence_number) {
Domenic Denicolacd30f5f82022-03-16 21:48:014342 std::vector<blink::mojom::NavigationApiHistoryEntryPtr> entries;
Rakina Zata Amni2322f4f82022-01-24 13:24:244343 if (GetLastCommittedEntry() && GetLastCommittedEntry()->IsInitialEntry()) {
Rakina Zata Amniafd3c6582021-11-30 06:19:174344 // Don't process the initial entry.
4345 DCHECK_EQ(GetEntryCount(), 1);
4346 return entries;
4347 }
Nate Chapind1fe3612021-04-16 20:45:574348 int offset = direction == Direction::kForward ? 1 : -1;
Nate Chapin63db0d12022-01-20 22:03:304349 int64_t previous_item_sequence_number = pending_item_sequence_number;
Nate Chapind1fe3612021-04-16 20:45:574350 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4351 i += offset) {
4352 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
Nate Chapindedfa642022-01-28 23:59:414353 if (!frame_entry)
Nate Chapind1fe3612021-04-16 20:45:574354 break;
Domenic Denicolacd30f5f82022-03-16 21:48:014355 // An entry should only appear in the navigation API entries if it is for
4356 // the same origin as the document being committed. Check the committed
4357 // origin, or if that is not available (during restore), check against the
4358 // FNE's url.
Nate Chapindedfa642022-01-28 23:59:414359 url::Origin frame_entry_origin =
4360 frame_entry->committed_origin().value_or(url::Origin::Resolve(
4361 frame_entry->url(),
4362 frame_entry->initiator_origin().value_or(url::Origin())));
4363 if (!pending_origin.IsSameOriginWith(frame_entry_origin))
Nate Chapind1fe3612021-04-16 20:45:574364 break;
4365 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4366 continue;
4367 blink::ExplodedPageState exploded_page_state;
4368 if (blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4369 &exploded_page_state)) {
4370 blink::ExplodedFrameState frame_state = exploded_page_state.top;
Nate Chapin63db0d12022-01-20 22:03:304371
4372 // If the document represented by this FNE hid its full url from appearing
4373 // in a referrer via a "no-referrer" or "origin" referrer policy, censor
Domenic Denicolacd30f5f82022-03-16 21:48:014374 // the url in the navigation API as well (unless we're navigating to that
Nate Chapin63db0d12022-01-20 22:03:304375 // document).
4376 std::u16string url;
4377 if (pending_document_sequence_number ==
4378 frame_entry->document_sequence_number() ||
Domenic Denicolacc094fb2022-03-16 23:40:574379 !frame_entry->protect_url_in_navigation_api()) {
Nate Chapin63db0d12022-01-20 22:03:304380 url = frame_state.url_string.value_or(std::u16string());
4381 }
4382
Domenic Denicolacd30f5f82022-03-16 21:48:014383 blink::mojom::NavigationApiHistoryEntryPtr entry =
4384 blink::mojom::NavigationApiHistoryEntry::New(
Domenic Denicolacc094fb2022-03-16 23:40:574385 frame_state.navigation_api_key.value_or(std::u16string()),
4386 frame_state.navigation_api_id.value_or(std::u16string()), url,
Nate Chapinab5c3a712021-11-18 22:17:094387 frame_state.item_sequence_number,
4388 frame_state.document_sequence_number,
Domenic Denicolacc094fb2022-03-16 23:40:574389 frame_state.navigation_api_state.value_or(std::u16string()));
Rakina Zata Amniafd3c6582021-11-30 06:19:174390
Nate Chapin63db0d12022-01-20 22:03:304391 DCHECK(entry->url.empty() ||
4392 pending_origin.CanBeDerivedFrom(GURL(entry->url)));
Nate Chapind1fe3612021-04-16 20:45:574393 entries.push_back(std::move(entry));
4394 previous_item_sequence_number = frame_entry->item_sequence_number();
4395 }
4396 }
4397 // If |entries| was constructed by iterating backwards from
4398 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4399 // earliest-at-the-front. Reverse it.
4400 if (direction == Direction::kBack)
4401 std::reverse(entries.begin(), entries.end());
4402 return entries;
4403}
4404
Domenic Denicolacd30f5f82022-03-16 21:48:014405blink::mojom::NavigationApiHistoryEntryArraysPtr
4406NavigationControllerImpl::GetNavigationApiHistoryEntryVectors(
Nate Chapin97d2f542022-02-18 01:34:554407 FrameTreeNode* node,
Nate Chapind1fe3612021-04-16 20:45:574408 NavigationRequest* request) {
4409 url::Origin pending_origin =
Nate Chapin97d2f542022-02-18 01:34:554410 request ? request->commit_params().origin_to_commit
4411 ? *request->commit_params().origin_to_commit
4412 : url::Origin::Create(request->common_params().url)
4413 : url::Origin::Create(node->current_url());
Nate Chapind1fe3612021-04-16 20:45:574414
Nate Chapind1fe3612021-04-16 20:45:574415 scoped_refptr<SiteInstance> site_instance =
Nate Chapin97d2f542022-02-18 01:34:554416 node->current_frame_host()->GetSiteInstance();
Nate Chapind1fe3612021-04-16 20:45:574417
4418 // NOTE: |entry_index| is an estimate of the index where this entry will
4419 // commit, but it may be wrong in corner cases (e.g., if we are at the max
4420 // entry limit, the earliest entry will be dropped). This is ok because this
4421 // algorithm only uses |entry_index| to walk the entry list as it stands right
4422 // now, and it isn't saved for anything post-commit.
4423 int entry_index = GetPendingEntryIndex();
4424 bool will_create_new_entry = false;
Nate Chapin97d2f542022-02-18 01:34:554425 if (!request ||
4426 NavigationTypeUtils::IsReload(request->common_params().navigation_type) ||
Nate Chapin7e6e4982022-04-13 17:37:274427 request->common_params().should_replace_current_entry ||
4428 request->common_params().is_history_navigation_in_new_child_frame) {
Nate Chapind1fe3612021-04-16 20:45:574429 entry_index = GetLastCommittedEntryIndex();
4430 } else if (entry_index == -1) {
4431 will_create_new_entry = true;
4432 entry_index = GetLastCommittedEntryIndex() + 1;
4433 }
4434
4435 int64_t pending_item_sequence_number = 0;
Nate Chapin63db0d12022-01-20 22:03:304436 int64_t pending_document_sequence_number = 0;
Nate Chapind1fe3612021-04-16 20:45:574437 if (auto* pending_entry = GetPendingEntry()) {
Nate Chapin63db0d12022-01-20 22:03:304438 if (auto* frame_entry = pending_entry->GetFrameEntry(node)) {
Nate Chapind1fe3612021-04-16 20:45:574439 pending_item_sequence_number = frame_entry->item_sequence_number();
Nate Chapin63db0d12022-01-20 22:03:304440 pending_document_sequence_number =
4441 frame_entry->document_sequence_number();
4442 }
Nate Chapind1fe3612021-04-16 20:45:574443 }
4444
Domenic Denicolacd30f5f82022-03-16 21:48:014445 auto entry_arrays = blink::mojom::NavigationApiHistoryEntryArrays::New();
4446 entry_arrays->back_entries = PopulateSingleNavigationApiHistoryEntryVector(
Nate Chapind1fe3612021-04-16 20:45:574447 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
Nate Chapin4e657a472022-02-06 03:38:374448 pending_item_sequence_number, pending_document_sequence_number);
Nate Chapind1fe3612021-04-16 20:45:574449
4450 // Don't populate forward entries if they will be truncated by a new entry.
4451 if (!will_create_new_entry) {
Domenic Denicolacd30f5f82022-03-16 21:48:014452 entry_arrays->forward_entries =
4453 PopulateSingleNavigationApiHistoryEntryVector(
4454 Direction::kForward, entry_index, pending_origin, node,
4455 site_instance.get(), pending_item_sequence_number,
4456 pending_document_sequence_number);
Nate Chapind1fe3612021-04-16 20:45:574457 }
Nate Chapin4e657a472022-02-06 03:38:374458 return entry_arrays;
Nate Chapind1fe3612021-04-16 20:45:574459}
4460
Nate Chapinfbfe5af2021-06-10 17:22:084461NavigationControllerImpl::HistoryNavigationAction
Domenic Denicolacc094fb2022-03-16 23:40:574462NavigationControllerImpl::ShouldNavigateToEntryForNavigationApiKey(
Nate Chapinfbfe5af2021-06-10 17:22:084463 FrameNavigationEntry* current_entry,
4464 FrameNavigationEntry* target_entry,
Domenic Denicolacc094fb2022-03-16 23:40:574465 const std::string& navigation_api_key) {
Nate Chapinfbfe5af2021-06-10 17:22:084466 if (!target_entry || !target_entry->committed_origin())
4467 return HistoryNavigationAction::kStopLooking;
4468 if (current_entry->site_instance() != target_entry->site_instance())
4469 return HistoryNavigationAction::kStopLooking;
4470 if (!current_entry->committed_origin()->IsSameOriginWith(
4471 *target_entry->committed_origin())) {
4472 return HistoryNavigationAction::kStopLooking;
4473 }
4474
4475 // NOTE: We don't actually care between kSameDocument and
4476 // kDifferentDocument, so always use kDifferentDocument by convention.
Domenic Denicolacc094fb2022-03-16 23:40:574477 if (target_entry->navigation_api_key() == navigation_api_key)
Nate Chapinfbfe5af2021-06-10 17:22:084478 return HistoryNavigationAction::kDifferentDocument;
4479 return HistoryNavigationAction::kKeepLooking;
4480}
4481
Domenic Denicolacc094fb2022-03-16 23:40:574482void NavigationControllerImpl::NavigateToNavigationApiKey(
4483 FrameTreeNode* node,
Nate Chapinc39f43a2022-04-01 00:16:254484 int sandboxed_source_frame_tree_node_id,
Domenic Denicolacc094fb2022-03-16 23:40:574485 const std::string& key) {
Nate Chapinfbfe5af2021-06-10 17:22:084486 FrameNavigationEntry* current_entry =
4487 GetLastCommittedEntry()->GetFrameEntry(node);
4488 if (!current_entry)
4489 return;
4490
4491 // We want to find the nearest matching entry that is contiguously
4492 // same-instance and same-origin. Check back first, then forward.
4493 // TODO(japhet): Link spec here once it exists.
4494 for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) {
Domenic Denicolacc094fb2022-03-16 23:40:574495 auto result = ShouldNavigateToEntryForNavigationApiKey(
Nate Chapinfbfe5af2021-06-10 17:22:084496 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4497 if (result == HistoryNavigationAction::kStopLooking)
4498 break;
4499 if (result != HistoryNavigationAction::kKeepLooking) {
Nate Chapinc39f43a2022-04-01 00:16:254500 GoToIndex(i, sandboxed_source_frame_tree_node_id,
Nate Chapin2892b37c2021-10-08 17:33:114501 false /* is_browser_initiated*/);
Nate Chapinfbfe5af2021-06-10 17:22:084502 return;
4503 }
4504 }
4505 for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) {
Domenic Denicolacc094fb2022-03-16 23:40:574506 auto result = ShouldNavigateToEntryForNavigationApiKey(
Nate Chapinfbfe5af2021-06-10 17:22:084507 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4508 if (result == HistoryNavigationAction::kStopLooking)
4509 break;
4510 if (result != HistoryNavigationAction::kKeepLooking) {
Nate Chapinc39f43a2022-04-01 00:16:254511 GoToIndex(i, sandboxed_source_frame_tree_node_id,
Nate Chapin2892b37c2021-10-08 17:33:114512 false /* is_browser_initiated*/);
Nate Chapinfbfe5af2021-06-10 17:22:084513 return;
4514 }
4515 }
4516}
4517
Domenic Denicolacc094fb2022-03-16 23:40:574518bool NavigationControllerImpl::ShouldProtectUrlInNavigationApi(
Nate Chapin63db0d12022-01-20 22:03:304519 network::mojom::ReferrerPolicy referrer_policy) {
4520 return referrer_policy == network::mojom::ReferrerPolicy::kNever ||
4521 referrer_policy == network::mojom::ReferrerPolicy::kOrigin;
4522}
4523
shivanigithubf405bf0d2021-11-05 17:58:334524bool NavigationControllerImpl::ShouldMaintainTrivialSessionHistory(
4525 const FrameTreeNode* frame_tree_node) const {
4526 // TODO(https://crbug.com/1197384): We may have to add portals in addition to
4527 // prerender and fenced frames. This should be kept in sync with
Hayato Ito7a80db42021-07-05 06:18:544528 // LocalFrame version, LocalFrame::ShouldMaintainTrivialSessionHistory.
shivanigithubf405bf0d2021-11-05 17:58:334529 return frame_tree_.is_prerendering() ||
4530 frame_tree_node->IsInFencedFrameTree();
Hayato Ito7a80db42021-07-05 06:18:544531}
4532
[email protected]8ff00d72012-10-23 19:12:214533} // namespace content