blob: 7f8899672491a500c174921708e6ac1130e4645e [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"
initial.commit09911bf2008-07-26 23:55:2943#include "base/logging.h"
asvitkine30330812016-08-30 04:01:0844#include "base/metrics/histogram_macros.h"
Chris Hamilton83272dc2021-02-23 00:24:0245#include "base/stl_util.h"
Peter Kastingb53b81912021-04-28 19:23:3046#include "base/strings/string_piece.h"
[email protected]348fbaac2013-06-11 06:31:5147#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0048#include "base/strings/utf_string_conversions.h"
[email protected]a43858f2013-06-28 15:18:3749#include "base/time/time.h"
Carlos Caballero40b0efd2021-01-26 11:55:0050#include "base/trace_event/optional_trace_event.h"
Carlos Caballeroede6f8c2021-01-28 11:01:5051#include "base/trace_event/trace_conversion_helper.h"
ssid3e765612015-01-28 04:03:4252#include "base/trace_event/trace_event.h"
servolkf3955532015-05-16 00:01:5953#include "build/build_config.h"
[email protected]eabfe1912014-05-12 10:07:2854#include "cc/base/switches.h"
jamescookda2505812015-03-20 18:01:1855#include "content/browser/bad_message.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3956#include "content/browser/blob_storage/chrome_blob_storage_context.h"
[email protected]825b1662012-03-12 19:07:3157#include "content/browser/browser_url_handler_impl.h"
[email protected]5f2aa722013-08-07 16:59:4158#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0459#include "content/browser/dom_storage/session_storage_namespace_impl.h"
danakjc492bf82020-09-09 20:02:4460#include "content/browser/renderer_host/debug_urls.h"
61#include "content/browser/renderer_host/frame_tree.h"
Carlos Caballero40b0efd2021-01-26 11:55:0062#include "content/browser/renderer_host/frame_tree_node.h"
danakjc492bf82020-09-09 20:02:4463#include "content/browser/renderer_host/navigation_entry_impl.h"
Nate Chapin214a86a2021-06-21 20:35:5764#include "content/browser/renderer_host/navigation_entry_restore_context_impl.h"
danakjc492bf82020-09-09 20:02:4465#include "content/browser/renderer_host/navigation_request.h"
66#include "content/browser/renderer_host/navigator.h"
67#include "content/browser/renderer_host/render_frame_host_delegate.h"
Carlos Caballero40b0efd2021-01-26 11:55:0068#include "content/browser/renderer_host/render_view_host_impl.h"
Sharon Yangd70a5392021-10-26 23:06:3269#include "content/browser/site_info.h"
[email protected]b6583592012-01-25 19:52:3370#include "content/browser/site_instance_impl.h"
Kunihiko Sakamoto346a74e2021-03-10 08:57:4871#include "content/browser/web_package/subresource_web_bundle_navigation_info.h"
Tsuyoshi Horo37493af2019-11-21 23:43:1472#include "content/browser/web_package/web_bundle_navigation_info.h"
Camille Lamy5193caa2018-10-12 11:59:4273#include "content/common/content_constants_internal.h"
Nate Chapind1fe3612021-04-16 20:45:5774#include "content/common/navigation_params_utils.h"
Nasko Oskovae49e292020-08-13 02:08:5175#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1176#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1977#include "content/public/browser/content_browser_client.h"
Fergal Daly1336ac642021-09-14 15:13:1178#include "content/public/browser/disallow_activation_reason.h"
[email protected]d9083482012-01-06 00:38:4679#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0080#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3881#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1682#include "content/public/browser/notification_types.h"
Lei Zhang96031532019-10-10 19:05:4783#include "content/public/browser/render_view_host.h"
[email protected]9677a3c2012-12-22 04:18:5884#include "content/public/browser/render_widget_host.h"
85#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1086#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3487#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:1988#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:3889#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:1090#include "content/public/common/content_features.h"
Lei Zhang96031532019-10-10 19:05:4791#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4392#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:5993#include "media/base/mime_util.h"
[email protected]a23de8572009-06-03 02:16:3294#include "net/base/escape.h"
Arthur Sonzogni620cec62018-12-13 13:08:5795#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:1896#include "services/metrics/public/cpp/ukm_builders.h"
97#include "services/metrics/public/cpp/ukm_recorder.h"
Yue Ru Sun128804932020-09-30 22:19:1798#include "services/metrics/public/cpp/ukm_source_id.h"
Nan Lind91c8152021-10-21 16:22:3799#include "services/network/public/mojom/fetch_api.mojom.h"
[email protected]9677a3c2012-12-22 04:18:58100#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:39101#include "third_party/blink/public/common/blob/blob_utils.h"
Gyuyoung Kim107c2a02021-04-13 01:49:30102#include "third_party/blink/public/common/chrome_debug_urls.h"
Miyoung Shin1c565c912021-03-17 12:11:21103#include "third_party/blink/public/common/history/session_history_constants.h"
Blink Reformata30d4232018-04-07 15:31:06104#include "third_party/blink/public/common/mime_util/mime_util.h"
Minggang Wanga13c796e2021-07-02 05:54:43105#include "third_party/blink/public/common/navigation/navigation_params.h"
Carlos Caballeroede6f8c2021-01-28 11:01:50106#include "third_party/blink/public/common/page_state/page_state_serialization.h"
Minggang Wangb9f3fa92021-07-01 15:30:31107#include "third_party/blink/public/mojom/navigation/navigation_params.mojom.h"
Minggang Wang7ee0c742021-06-16 16:16:51108#include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h"
[email protected]cca6f392014-05-28 21:32:26109#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:29110
[email protected]8ff00d72012-10-23 19:12:21111namespace content {
[email protected]e9ba4472008-09-14 15:42:43112namespace {
113
114// Invoked when entries have been pruned, or removed. For example, if the
115// current entries are [google, digg, yahoo], with the current entry google,
116// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47117void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50118 int index,
[email protected]c12bf1a12008-09-17 16:28:49119 int count) {
[email protected]8ff00d72012-10-23 19:12:21120 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50121 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49122 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14123 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43124}
125
[email protected]e9ba4472008-09-14 15:42:43126// Configure all the NavigationEntries in entries for restore. This resets
127// the transition type to reload and makes sure the content state isn't empty.
128void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57129 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48130 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47131 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43132 // Use a transition type of reload so that we don't incorrectly increase
133 // the typed count.
Lei Zhang96031532019-10-10 19:05:47134 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
135 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43136 }
137}
138
[email protected]bf70edce2012-06-20 22:32:22139// Determines whether or not we should be carrying over a user agent override
140// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57141bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22142 return last_entry && last_entry->GetIsOverridingUserAgent();
143}
144
Camille Lamy5193caa2018-10-12 11:59:42145// Determines whether to override user agent for a navigation.
146bool ShouldOverrideUserAgent(
147 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57148 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42149 switch (override_user_agent) {
150 case NavigationController::UA_OVERRIDE_INHERIT:
151 return ShouldKeepOverride(last_committed_entry);
152 case NavigationController::UA_OVERRIDE_TRUE:
153 return true;
154 case NavigationController::UA_OVERRIDE_FALSE:
155 return false;
Camille Lamy5193caa2018-10-12 11:59:42156 }
157 NOTREACHED();
158 return false;
159}
160
Rakina Zata Amni312822d72021-06-04 16:13:37161// Returns true if this navigation should be treated as a reload. For e.g.
clamy0a656e42018-02-06 18:18:28162// navigating to the last committed url via the address bar or clicking on a
Rakina Zata Amni312822d72021-06-04 16:13:37163// link which results in a navigation to the last committed URL (but wasn't
164// converted to do a replacement navigation in the renderer), etc.
Fergal Daly766177d2020-07-07 07:54:04165// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
166// |base_url_for_data_url|, |transition_type| correspond to the new navigation
167// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
168// navigation that committed.
169bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
170 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57171 const GURL& virtual_url,
172 const GURL& base_url_for_data_url,
173 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57174 bool is_post,
Rakina Zata Amni312822d72021-06-04 16:13:37175 bool should_replace_current_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57176 NavigationEntryImpl* last_committed_entry) {
Rakina Zata Amni312822d72021-06-04 16:13:37177 // Navigations intended to do a replacement shouldn't be converted to do a
178 // reload.
179 if (should_replace_current_entry)
clamy0a656e42018-02-06 18:18:28180 return false;
clamy0a656e42018-02-06 18:18:28181 // Only convert to reload if at least one navigation committed.
Rakina Zata Amniafd3c6582021-11-30 06:19:17182 if (last_committed_entry->IsInitialEntry())
ananta3bdd8ae2016-12-22 17:11:55183 return false;
184
arthursonzogni7a8243682017-12-14 16:41:42185 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28186 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42187 return false;
188
ananta3bdd8ae2016-12-22 17:11:55189 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
190 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
191 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28192 bool transition_type_can_be_converted = false;
193 if (ui::PageTransitionCoreTypeIs(transition_type,
194 ui::PAGE_TRANSITION_RELOAD) &&
195 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
196 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55197 }
clamy0a656e42018-02-06 18:18:28198 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55199 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28200 transition_type_can_be_converted = true;
201 }
202 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
203 transition_type_can_be_converted = true;
204 if (!transition_type_can_be_converted)
205 return false;
206
207 // This check is required for cases like view-source:, etc. Here the URL of
208 // the navigation entry would contain the url of the page, while the virtual
209 // URL contains the full URL including the view-source prefix.
210 if (virtual_url != last_committed_entry->GetVirtualURL())
211 return false;
212
Fergal Daly766177d2020-07-07 07:54:04213 // Check that the URLs match.
214 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
215 // If there's no frame entry then by definition the URLs don't match.
216 if (!frame_entry)
217 return false;
218
219 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28220 return false;
221
222 // This check is required for Android WebView loadDataWithBaseURL. Apps
223 // can pass in anything in the base URL and we need to ensure that these
224 // match before classifying it as a reload.
225 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
226 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
227 return false;
ananta3bdd8ae2016-12-22 17:11:55228 }
229
clamy0a656e42018-02-06 18:18:28230 // Skip entries with SSL errors.
231 if (last_committed_entry->ssl_error())
232 return false;
233
234 // Don't convert to a reload when the last navigation was a POST or the new
235 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04236 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28237 return false;
238
239 return true;
ananta3bdd8ae2016-12-22 17:11:55240}
241
Nasko Oskov03912102019-01-11 00:21:32242bool DoesURLMatchOriginForNavigation(
243 const GURL& url,
Anton Bikineevf62d1bf2021-05-15 17:56:07244 const absl::optional<url::Origin>& origin,
Kunihiko Sakamoto346a74e2021-03-10 08:57:48245 SubresourceWebBundleNavigationInfo*
246 subresource_web_bundle_navigation_info) {
Nasko Oskov03912102019-01-11 00:21:32247 // If there is no origin supplied there is nothing to match. This can happen
248 // for navigations to a pending entry and therefore it should be allowed.
249 if (!origin)
250 return true;
251
Kunihiko Sakamoto260ee0a2021-10-15 01:44:08252 // Urn: and uuid-in-package: subframe from WebBundle has an opaque origin
253 // derived from the Bundle's origin.
254 // TODO(https://crbug.com/1257045): Remove urn: scheme support.
255 if ((url.SchemeIs(url::kUrnScheme) ||
256 url.SchemeIs(url::kUuidInPackageScheme)) &&
257 subresource_web_bundle_navigation_info) {
Kunihiko Sakamoto346a74e2021-03-10 08:57:48258 return origin->CanBeDerivedFrom(
259 subresource_web_bundle_navigation_info->bundle_url());
260 }
261
Nasko Oskov03912102019-01-11 00:21:32262 return origin->CanBeDerivedFrom(url);
263}
264
Anton Bikineevf62d1bf2021-05-15 17:56:07265absl::optional<url::Origin> GetCommittedOriginForFrameEntry(
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12266 const mojom::DidCommitProvisionalLoadParams& params,
267 NavigationRequest* request) {
Nasko Oskov03912102019-01-11 00:21:32268 // Error pages commit in an opaque origin, yet have the real URL that resulted
269 // in an error as the |params.url|. Since successful reload of an error page
270 // should commit in the correct origin, setting the opaque origin on the
271 // FrameNavigationEntry will be incorrect.
Rakina Zata Amniafd3c6582021-11-30 06:19:17272 if (request && request->DidEncounterError())
Anton Bikineevf62d1bf2021-05-15 17:56:07273 return absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:32274
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12275 // We also currently don't save committed origins for loadDataWithBaseURL
276 // navigations (probably accidentally). Without this check, navigations to
277 // the FrameNavigationEntry might fail the DoesURLMatchOriginForNavigation()
278 // check since the origin will be based on the base URL instead of the data:
279 // URL used for the navigation.
280 // TODO(https://crbug.com/1198406): Save committed origin in
281 // FrameNavigationEntry for this case too.
Rakina Zata Amniafd3c6582021-11-30 06:19:17282 if (request && request->IsLoadDataWithBaseURL())
Anton Bikineevf62d1bf2021-05-15 17:56:07283 return absl::nullopt;
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12284
Anton Bikineevf62d1bf2021-05-15 17:56:07285 return absl::make_optional(params.origin);
Nasko Oskov03912102019-01-11 00:21:32286}
287
Camille Lamy5193caa2018-10-12 11:59:42288bool IsValidURLForNavigation(bool is_main_frame,
289 const GURL& virtual_url,
290 const GURL& dest_url) {
291 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
292 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
293 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
294 << virtual_url.possibly_invalid_spec();
295 return false;
296 }
297
298 // Don't attempt to navigate to non-empty invalid URLs.
299 if (!dest_url.is_valid() && !dest_url.is_empty()) {
300 LOG(WARNING) << "Refusing to load invalid URL: "
301 << dest_url.possibly_invalid_spec();
302 return false;
303 }
304
305 // The renderer will reject IPC messages with URLs longer than
306 // this limit, so don't attempt to navigate with a longer URL.
307 if (dest_url.spec().size() > url::kMaxURLChars) {
308 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
309 << " characters.";
310 return false;
311 }
312
Aaron Colwell33109c592020-04-21 21:31:19313 // Reject renderer debug URLs because they should have been handled before
314 // we get to this point. This check handles renderer debug URLs
315 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
316 // provides defense-in-depth if a renderer debug URL manages to get here via
317 // some other path. We want to reject the navigation here so it doesn't
318 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30319 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19320 LOG(WARNING) << "Refusing to load renderer debug URL: "
321 << dest_url.possibly_invalid_spec();
322 return false;
323 }
324
Camille Lamy5193caa2018-10-12 11:59:42325 return true;
326}
327
Mikel Astizba9cf2fd2017-12-17 10:38:10328// See replaced_navigation_entry_data.h for details: this information is meant
329// to ensure |*output_entry| keeps track of its original URL (landing page in
330// case of server redirects) as it gets replaced (e.g. history.replaceState()),
331// without overwriting it later, for main frames.
332void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57333 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10334 NavigationEntryImpl* output_entry) {
Rakina Zata Amniafd3c6582021-11-30 06:19:17335 if (output_entry->GetReplacedEntryData().has_value() ||
336 replaced_entry->IsInitialEntry()) {
Mikel Astizba9cf2fd2017-12-17 10:38:10337 return;
Rakina Zata Amniafd3c6582021-11-30 06:19:17338 }
Mikel Astizba9cf2fd2017-12-17 10:38:10339
340 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57341 data.first_committed_url = replaced_entry->GetURL();
342 data.first_timestamp = replaced_entry->GetTimestamp();
343 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29344 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10345}
346
Minggang Wangb9f3fa92021-07-01 15:30:31347blink::mojom::NavigationType GetNavigationType(
348 const GURL& old_url,
349 const GURL& new_url,
350 ReloadType reload_type,
351 NavigationEntryImpl* entry,
352 const FrameNavigationEntry& frame_entry,
353 bool has_pending_cross_document_commit,
354 bool is_currently_error_page,
355 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23356 // Reload navigations
357 switch (reload_type) {
358 case ReloadType::NORMAL:
Minggang Wangb9f3fa92021-07-01 15:30:31359 return blink::mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23360 case ReloadType::BYPASSING_CACHE:
Minggang Wangb9f3fa92021-07-01 15:30:31361 return blink::mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23362 case ReloadType::ORIGINAL_REQUEST_URL:
Minggang Wangb9f3fa92021-07-01 15:30:31363 return blink::mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23364 case ReloadType::NONE:
365 break; // Fall through to rest of function.
366 }
367
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08368 if (entry->IsRestored()) {
Minggang Wangb9f3fa92021-07-01 15:30:31369 return entry->GetHasPostData()
370 ? blink::mojom::NavigationType::RESTORE_WITH_POST
371 : blink::mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23372 }
373
danakjb952ef12021-01-14 19:58:49374 const bool can_be_same_document =
375 // A pending cross-document commit means this navigation will not occur in
376 // the current document, as that document would end up being replaced in
377 // the meantime.
378 !has_pending_cross_document_commit &&
379 // If the current document is an error page, we should always treat it as
380 // a different-document navigation so that we'll attempt to load the
381 // document we're navigating to (and not stay in the current error page).
382 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12383
clamyea99ea12018-05-28 13:54:23384 // History navigations.
385 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12386 return can_be_same_document && is_same_document_history_load
Minggang Wangb9f3fa92021-07-01 15:30:31387 ? blink::mojom::NavigationType::HISTORY_SAME_DOCUMENT
388 : blink::mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23389 }
390 DCHECK(!is_same_document_history_load);
391
392 // A same-document fragment-navigation happens when the only part of the url
393 // that is modified is after the '#' character.
394 //
395 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12396 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23397 //
398 // Note: this check is only valid for navigations that are not history
399 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
400 // history navigation from 'A#foo' to 'A#bar' is not a same-document
401 // navigation, but a different-document one. This is why history navigation
402 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47403 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
404 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12405
406 // The one case where we do the wrong thing here and incorrectly choose
407 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
408 // the renderer is a frameset. All frameset navigations should be
409 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
410 // navigation would do the right thing, as it would send it to the browser and
411 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
412 // into this method). But since we can't tell that case here for browser-
413 // initiated navigations, we have to get the renderer involved. In that case
414 // the navigation would be restarted due to the renderer spending a reply of
415 // mojom::CommitResult::RestartCrossDocument.
416
417 return can_be_same_document && is_same_doc
Minggang Wangb9f3fa92021-07-01 15:30:31418 ? blink::mojom::NavigationType::SAME_DOCUMENT
419 : blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23420}
421
Camille Lamy5193caa2018-10-12 11:59:42422// Adjusts the original input URL if needed, to get the URL to actually load and
423// the virtual URL, which may differ.
424void RewriteUrlForNavigation(const GURL& original_url,
425 BrowserContext* browser_context,
426 GURL* url_to_load,
427 GURL* virtual_url,
428 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42429 // Allow the browser URL handler to rewrite the URL. This will, for example,
430 // remove "view-source:" from the beginning of the URL to get the URL that
431 // will actually be loaded. This real URL won't be shown to the user, just
432 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31433 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42434 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
435 url_to_load, browser_context, reverse_on_redirect);
436}
437
438#if DCHECK_IS_ON()
439// Helper sanity check function used in debug mode.
440void ValidateRequestMatchesEntry(NavigationRequest* request,
441 NavigationEntryImpl* entry) {
442 if (request->frame_tree_node()->IsMainFrame()) {
443 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
444 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
Minggang Wangb9f3fa92021-07-01 15:30:31445 ui::PageTransitionFromInt(request->common_params().transition),
446 entry->GetTransitionType()));
Camille Lamy5193caa2018-10-12 11:59:42447 }
Nasko Oskovc36327d2019-01-03 23:23:04448 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42449 entry->should_clear_history_list());
450 DCHECK_EQ(request->common_params().has_user_gesture,
451 entry->has_user_gesture());
452 DCHECK_EQ(request->common_params().base_url_for_data_url,
453 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04454 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42455 entry->GetCanLoadLocalResources());
456 DCHECK_EQ(request->common_params().started_from_context_menu,
457 entry->has_started_from_context_menu());
458
459 FrameNavigationEntry* frame_entry =
460 entry->GetFrameEntry(request->frame_tree_node());
461 if (!frame_entry) {
462 NOTREACHED();
463 return;
464 }
465
Camille Lamy5193caa2018-10-12 11:59:42466 DCHECK_EQ(request->common_params().method, frame_entry->method());
467
Nasko Oskovc36327d2019-01-03 23:23:04468 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42469 if (redirect_size == frame_entry->redirect_chain().size()) {
470 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04471 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42472 frame_entry->redirect_chain()[i]);
473 }
474 } else {
475 NOTREACHED();
476 }
477}
478#endif // DCHECK_IS_ON()
479
Dave Tapuska8bfd84c2019-03-26 20:47:16480// Returns whether the session history NavigationRequests in |navigations|
481// would stay within the subtree of the sandboxed iframe in
482// |sandbox_frame_tree_node_id|.
483bool DoesSandboxNavigationStayWithinSubtree(
484 int sandbox_frame_tree_node_id,
485 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
486 for (auto& item : navigations) {
487 bool within_subtree = false;
488 // Check whether this NavigationRequest affects a frame within the
489 // sandboxed frame's subtree by walking up the tree looking for the
490 // sandboxed frame.
491 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03492 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16493 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
494 within_subtree = true;
495 break;
496 }
497 }
498 if (!within_subtree)
499 return false;
500 }
501 return true;
502}
503
[email protected]e9ba4472008-09-14 15:42:43504} // namespace
505
arthursonzogni66f711c2019-10-08 14:40:36506// NavigationControllerImpl::PendingEntryRef------------------------------------
507
508NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
509 base::WeakPtr<NavigationControllerImpl> controller)
510 : controller_(controller) {}
511
512NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
513 if (!controller_) // Can be null with interstitials.
514 return;
515
516 controller_->PendingEntryRefDeleted(this);
517}
518
[email protected]d202a7c2012-01-04 07:53:47519// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29520
[email protected]23a918b2014-07-15 09:51:36521const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23522
[email protected]765b35502008-08-21 00:51:20523// static
[email protected]d202a7c2012-01-04 07:53:47524size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23525 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20526
[email protected]e6fec472013-05-14 05:29:02527// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20528// when testing.
529static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29530
[email protected]71fde352011-12-29 03:29:56531// static
dcheng9bfa5162016-04-09 01:00:57532std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
533 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10534 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07535 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10536 ui::PageTransition transition,
537 bool is_renderer_initiated,
538 const std::string& extra_headers,
539 BrowserContext* browser_context,
540 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
541 return NavigationControllerImpl::CreateNavigationEntry(
542 url, referrer, std::move(initiator_origin),
543 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Hayato Ito303654c2021-06-30 09:07:54544 extra_headers, browser_context, std::move(blob_url_loader_factory));
Lukasz Anforowicz641234d52019-11-07 21:07:10545}
546
547// static
548std::unique_ptr<NavigationEntryImpl>
549NavigationControllerImpl::CreateNavigationEntry(
550 const GURL& url,
551 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07552 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10553 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57554 ui::PageTransition transition,
555 bool is_renderer_initiated,
556 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09557 BrowserContext* browser_context,
Hayato Ito303654c2021-06-30 09:07:54558 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
Camille Lamy5193caa2018-10-12 11:59:42559 GURL url_to_load;
560 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56561 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42562 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
563 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56564
Lukasz Anforowicz641234d52019-11-07 21:07:10565 // Let the NTP override the navigation params and pretend that this is a
566 // browser-initiated, bookmark-like navigation.
567 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violetcf6ea7e2021-06-09 21:09:21568 source_site_instance, &transition, &is_renderer_initiated, &referrer,
569 &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10570
Patrick Monettef507e982019-06-19 20:18:06571 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28572 nullptr, // The site instance for tabs is sent on navigation
573 // (WebContents::GetSiteInstance).
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58574 url_to_load, referrer, initiator_origin, std::u16string(), transition,
Rakina Zata Amniafd3c6582021-11-30 06:19:17575 is_renderer_initiated, blob_url_loader_factory,
576 /* is_initial_entry = */ false);
Camille Lamy5193caa2018-10-12 11:59:42577 entry->SetVirtualURL(virtual_url);
578 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56579 entry->set_update_virtual_url_with_url(reverse_on_redirect);
580 entry->set_extra_headers(extra_headers);
Patrick Monettef507e982019-06-19 20:18:06581 return entry;
[email protected]71fde352011-12-29 03:29:56582}
583
[email protected]cdcb1dee2012-01-04 00:46:20584// static
585void NavigationController::DisablePromptOnRepost() {
586 g_check_for_repost = false;
587}
588
[email protected]c5b88d82012-10-06 17:03:33589base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
590 base::Time t) {
591 // If |t| is between the water marks, we're in a run of duplicates
592 // or just getting out of it, so increase the high-water mark to get
593 // a time that probably hasn't been used before and return it.
594 if (low_water_mark_ <= t && t <= high_water_mark_) {
Peter Kastinge5a38ed2021-10-02 03:06:35595 high_water_mark_ += base::Microseconds(1);
[email protected]c5b88d82012-10-06 17:03:33596 return high_water_mark_;
597 }
598
599 // Otherwise, we're clear of the last duplicate run, so reset the
600 // water marks.
601 low_water_mark_ = high_water_mark_ = t;
602 return t;
603}
604
ckitagawa0faa5e42020-06-17 17:30:54605NavigationControllerImpl::ScopedShowRepostDialogForTesting::
606 ScopedShowRepostDialogForTesting()
607 : was_disallowed_(g_check_for_repost) {
608 g_check_for_repost = true;
609}
610
611NavigationControllerImpl::ScopedShowRepostDialogForTesting::
612 ~ScopedShowRepostDialogForTesting() {
613 g_check_for_repost = was_disallowed_;
614}
615
[email protected]d202a7c2012-01-04 07:53:47616NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00617 BrowserContext* browser_context,
618 FrameTree& frame_tree,
619 NavigationControllerDelegate* delegate)
620 : frame_tree_(frame_tree),
621 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57622 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22623 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47624 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37625 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29626}
627
[email protected]d202a7c2012-01-04 07:53:47628NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00629 // The NavigationControllerImpl might be called inside its delegate
630 // destructor. Calling it is not valid anymore.
631 delegate_ = nullptr;
632 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29633}
634
Matt Falkenhagen548ed1562021-07-06 01:38:26635WebContents* NavigationControllerImpl::DeprecatedGetWebContents() {
636 return delegate_->DeprecatedGetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32637}
638
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57639BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55640 return browser_context_;
641}
642
[email protected]d202a7c2012-01-04 07:53:47643void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30644 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36645 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57646 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57647 // Verify that this controller is unused and that the input is valid.
Rakina Zata Amniafd3c6582021-11-30 06:19:17648 DCHECK_EQ(1, GetEntryCount());
649 DCHECK(GetLastCommittedEntry()->IsInitialEntry());
Lei Zhang96031532019-10-10 19:05:47650 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57651 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14652 selected_navigation < static_cast<int>(entries->size()));
arthursonzogni5c4c202d2017-04-25 23:41:27653 DCHECK_EQ(-1, pending_entry_index_);
[email protected]ce3fa3c2009-04-20 19:55:57654
[email protected]ce3fa3c2009-04-20 19:55:57655 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44656 needs_reload_type_ = NeedsReloadType::kRestoreSession;
Rakina Zata Amniafd3c6582021-11-30 06:19:17657 entries_.clear();
avif16f85a72015-11-13 18:25:03658 entries_.reserve(entries->size());
659 for (auto& entry : *entries)
dcheng36b6aec92015-12-26 06:16:36660 entries_.push_back(
661 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
avif16f85a72015-11-13 18:25:03662
663 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
664 // cleared out safely.
665 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57666
667 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36668 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57669}
670
toyoshim6142d96f2016-12-19 09:07:25671void NavigationControllerImpl::Reload(ReloadType reload_type,
672 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28673 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28674 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32675 int current_index = -1;
676
Carlos IL42b416592019-10-07 23:10:36677 if (entry_replaced_by_post_commit_error_) {
678 // If there is an entry that was replaced by a currently active post-commit
679 // error navigation, this can't be the initial navigation.
680 DCHECK(!IsInitialNavigation());
681 // If the current entry is a post commit error, we reload the entry it
682 // replaced instead. We leave the error entry in place until a commit
683 // replaces it, but the pending entry points to the original entry in the
684 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
685 // case that pending_entry_ != entries_[pending_entry_index_].
686 entry = entry_replaced_by_post_commit_error_.get();
687 current_index = GetCurrentEntryIndex();
688 } else if (IsInitialNavigation() && pending_entry_) {
689 // If we are reloading the initial navigation, just use the current
690 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32691 entry = pending_entry_;
692 // The pending entry might be in entries_ (e.g., after a Clone), so we
693 // should also update the current_index.
694 current_index = pending_entry_index_;
695 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00696 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32697 current_index = GetCurrentEntryIndex();
698 if (current_index != -1) {
creis3da03872015-02-20 21:12:32699 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32700 }
[email protected]979a4bc2013-04-24 01:27:15701 }
[email protected]241db352013-04-22 18:04:05702
[email protected]59167c22013-06-03 18:07:32703 // If we are no where, then we can't reload. TODO(darin): We should add a
704 // CanReload method.
705 if (!entry)
706 return;
707
Rakina Zata Amnif297a802022-01-18 03:53:43708 if (entry->IsInitialEntryNotForSynchronousAboutBlank()) {
709 // We should never navigate to an existing initial NavigationEntry that is
710 // the initial NavigationEntry for the initial empty document that hasn't
711 // been overridden by the synchronous about:blank commit, to preserve
712 // legacy behavior where trying to reload when the main frame is on the
713 // initial empty document won't result in a navigation. See also
714 // https://crbug.com/1277414.
715 return;
716 }
717
Takashi Toyoshimac7df3c22019-06-25 14:18:47718 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26719 entry->set_reload_type(reload_type);
720
Aran Gilman37d11632019-10-08 23:07:15721 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30722 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07723 // they really want to do this. If they do, the dialog will call us back
724 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57725 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02726
[email protected]106a0812010-03-18 00:15:12727 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57728 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47729 return;
initial.commit09911bf2008-07-26 23:55:29730 }
Lei Zhang96031532019-10-10 19:05:47731
732 if (!IsInitialNavigation())
733 DiscardNonCommittedEntries();
734
735 pending_entry_ = entry;
736 pending_entry_index_ = current_index;
737 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
738
Nate Chapin45f620582021-09-30 17:45:43739 // location.reload() goes through BeginNavigation, so all reloads triggered
740 // via this codepath are browser initiated.
Lei Zhang96031532019-10-10 19:05:47741 NavigateToExistingPendingEntry(reload_type,
Nate Chapin45f620582021-09-30 17:45:43742 FrameTreeNode::kFrameTreeNodeInvalidId,
743 true /* is_browser_initiated */);
initial.commit09911bf2008-07-26 23:55:29744}
745
[email protected]d202a7c2012-01-04 07:53:47746void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48747 DCHECK(pending_reload_ != ReloadType::NONE);
748 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12749}
750
[email protected]d202a7c2012-01-04 07:53:47751void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48752 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12753 NOTREACHED();
754 } else {
toyoshim6142d96f2016-12-19 09:07:25755 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48756 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12757 }
758}
759
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57760bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09761 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11762}
763
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57764bool NavigationControllerImpl::IsInitialBlankNavigation() {
Rakina Zata Amniafd3c6582021-11-30 06:19:17765 return IsInitialNavigation() && GetEntryCount() == 1 &&
766 GetLastCommittedEntry()->IsInitialEntry() &&
767 GetLastCommittedEntry()->restore_type() == RestoreType::kNotRestored;
creis10a4ab72015-10-13 17:22:40768}
769
Aran Gilman37d11632019-10-08 23:07:15770NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
771 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58772 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03773 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58774}
775
Adithya Srinivasan9b0c99c2021-08-10 15:19:45776NavigationEntryImpl*
777NavigationControllerImpl::GetEntryWithUniqueIDIncludingPending(
778 int nav_entry_id) const {
779 NavigationEntryImpl* entry = GetEntryWithUniqueID(nav_entry_id);
780 if (entry)
781 return entry;
782 return pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id
Keishi Hattori0e45c022021-11-27 09:25:52783 ? pending_entry_.get()
Adithya Srinivasan9b0c99c2021-08-10 15:19:45784 : nullptr;
785}
786
W. James MacLean1c40862c2020-04-27 21:05:57787void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
788 const url::Origin& origin) {
789 for (int i = 0; i < GetEntryCount(); i++) {
790 auto* entry = GetEntryAtIndex(i);
791 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
792 }
793 if (entry_replaced_by_post_commit_error_) {
794 // It's possible we could come back to this entry if the error
795 // page/interstitial goes away.
796 entry_replaced_by_post_commit_error_
797 ->RegisterExistingOriginToPreventOptInIsolation(origin);
798 }
799 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
800 // visiting pending_entry_, which lacks a committed origin. This will be done
801 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
802}
803
avi25764702015-06-23 15:43:37804void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57805 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00806 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37807 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27808 DCHECK_EQ(-1, pending_entry_index_);
[email protected]765b35502008-08-21 00:51:20809}
810
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57811NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47812 if (pending_entry_)
813 return pending_entry_;
814 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20815}
816
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57817NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32818 // The pending entry is safe to return for new (non-history), browser-
819 // initiated navigations. Most renderer-initiated navigations should not
820 // show the pending entry, to prevent URL spoof attacks.
821 //
822 // We make an exception for renderer-initiated navigations in new tabs, as
823 // long as no other page has tried to access the initial empty document in
824 // the new tab. If another page modifies this blank page, a URL spoof is
825 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32826 bool safe_to_show_pending =
827 pending_entry_ &&
828 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09829 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32830 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46831 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32832
833 // Also allow showing the pending entry for history navigations in a new tab,
834 // such as Ctrl+Back. In this case, no existing page is visible and no one
835 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15836 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
837 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32838 safe_to_show_pending = true;
839
840 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19841 return pending_entry_;
842 return GetLastCommittedEntry();
843}
844
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57845int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20846 if (pending_entry_index_ != -1)
847 return pending_entry_index_;
848 return last_committed_entry_index_;
849}
850
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57851NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20852 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28853 return nullptr;
avif16f85a72015-11-13 18:25:03854 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20855}
856
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57857bool NavigationControllerImpl::CanViewSource() {
Jeremy Roman2d8dfe132021-07-06 20:51:26858 const std::string& mime_type =
859 frame_tree_.root()->current_frame_host()->GetPage().contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53860 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
861 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27862 NavigationEntry* visible_entry = GetVisibleEntry();
863 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39864 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16865}
866
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57867int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27868 // The last committed entry index must always be less than the number of
Rakina Zata Amniafd3c6582021-11-30 06:19:17869 // entries.
arthursonzogni5c4c202d2017-04-25 23:41:27870 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
[email protected]a26023822011-12-29 00:23:55871 return last_committed_entry_index_;
872}
873
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57874int NavigationControllerImpl::GetEntryCount() {
Rakina Zata Amniafd3c6582021-11-30 06:19:17875 DCHECK_GE(entries_.size(), 1u);
Carlos IL4dea8902020-05-26 15:14:29876 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55877 return static_cast<int>(entries_.size());
878}
879
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57880NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37881 if (index < 0 || index >= GetEntryCount())
882 return nullptr;
883
avif16f85a72015-11-13 18:25:03884 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25885}
886
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57887NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47888 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20889}
890
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57891int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46892 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03893}
894
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57895bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03896 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
897 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
898 return true;
899 }
900 return false;
[email protected]765b35502008-08-21 00:51:20901}
902
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57903bool NavigationControllerImpl::CanGoForward() {
WangHui74286d52021-03-31 16:17:15904 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
905 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
906 return true;
907 }
908 return false;
[email protected]765b35502008-08-21 00:51:20909}
910
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57911bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03912 int index = GetIndexForOffset(offset);
913 return index >= 0 && index < GetEntryCount();
914}
915
Xiaohan Wang7f8052e02022-01-14 18:44:28916#if BUILDFLAG(IS_ANDROID)
WangHui74286d52021-03-31 16:17:15917bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
WangHui74286d52021-03-31 16:17:15918 if (offset == 0)
919 return true;
920 int increment = offset > 0 ? 1 : -1;
921 int non_skippable_entries = 0;
922 for (int index = GetIndexForOffset(increment);
923 index >= 0 && index < GetEntryCount(); index += increment) {
924 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
925 non_skippable_entries++;
926
927 if (non_skippable_entries == std::abs(offset))
928 return true;
929 }
930 return false;
931}
932#endif
933
[email protected]d202a7c2012-01-04 07:53:47934void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06935 int target_index = GetIndexForOffset(-1);
936
Elly Fong-Jones40ee8112021-06-23 19:10:52937 // Move the target index past the skippable entries.
Shivani Sharma298d12852019-01-22 20:04:03938 bool all_skippable_entries = true;
Elly Fong-Jones40ee8112021-06-23 19:10:52939 while (target_index >= 0) {
940 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui()) {
Shivani Sharma298d12852019-01-22 20:04:03941 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06942 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16943 }
Elly Fong-Jones40ee8112021-06-23 19:10:52944 target_index--;
shivanisha55201872018-12-13 04:29:06945 }
Miyoung Shin1c565c912021-03-17 12:11:21946
Shivani Sharma298d12852019-01-22 20:04:03947 // Do nothing if all entries are skippable. Normally this path would not
948 // happen as consumers would have already checked it in CanGoBack but a lot of
949 // tests do not do that.
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42950 if (all_skippable_entries)
Shivani Sharma298d12852019-01-22 20:04:03951 return;
shivanisha55201872018-12-13 04:29:06952
shivanisha55201872018-12-13 04:29:06953 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20954}
955
[email protected]d202a7c2012-01-04 07:53:47956void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06957 int target_index = GetIndexForOffset(1);
958
Shivani Sharma2d5b4b6b2019-01-08 16:07:16959 // Note that at least one entry (the last one) will be non-skippable since
960 // entries are marked skippable only when they add another entry because of
961 // redirect or pushState.
Elly Fong-Jones40ee8112021-06-23 19:10:52962 while (target_index < static_cast<int>(entries_.size())) {
963 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui())
shivanisha55201872018-12-13 04:29:06964 break;
Elly Fong-Jones40ee8112021-06-23 19:10:52965 target_index++;
shivanisha55201872018-12-13 04:29:06966 }
shivanisha55201872018-12-13 04:29:06967 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20968}
969
[email protected]d202a7c2012-01-04 07:53:47970void NavigationControllerImpl::GoToIndex(int index) {
Nate Chapin45f620582021-09-30 17:45:43971 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId,
972 true /* is_browser_initiated */);
Dave Tapuska8bfd84c2019-03-26 20:47:16973}
974
975void NavigationControllerImpl::GoToIndex(int index,
Nate Chapin45f620582021-09-30 17:45:43976 int sandbox_frame_tree_node_id,
977 bool is_browser_initiated) {
978 DCHECK(sandbox_frame_tree_node_id == FrameTreeNode::kFrameTreeNodeInvalidId ||
979 !is_browser_initiated);
sunjian30574a62017-03-21 21:39:44980 TRACE_EVENT0("browser,navigation,benchmark",
981 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:20982 if (index < 0 || index >= static_cast<int>(entries_.size())) {
983 NOTREACHED();
984 return;
985 }
986
Rakina Zata Amnif297a802022-01-18 03:53:43987 if (entries_[index]->IsInitialEntryNotForSynchronousAboutBlank()) {
988 // We should never navigate to an existing initial NavigationEntry that is
989 // the initial NavigationEntry for the initial empty document that hasn't
990 // been overridden by the synchronous about:blank commit, to preserve
991 // legacy behavior where trying to reload when the main frame is on the
992 // initial empty document won't result in a navigation. See also
993 // https://crbug.com/1277414.
994 return;
995 }
996
[email protected]cbab76d2008-10-13 22:42:47997 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20998
arthursonzogni5c4c202d2017-04-25 23:41:27999 DCHECK_EQ(nullptr, pending_entry_);
1000 DCHECK_EQ(-1, pending_entry_index_);
Rakina Zata Amnif297a802022-01-18 03:53:431001
arthursonzogni5c4c202d2017-04-25 23:41:271002 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:201003 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:271004 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
1005 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Nate Chapin45f620582021-09-30 17:45:431006 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id,
1007 is_browser_initiated);
[email protected]765b35502008-08-21 00:51:201008}
1009
[email protected]d202a7c2012-01-04 07:53:471010void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:121011 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:031012 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:201013 return;
1014
[email protected]9ba14052012-06-22 23:50:031015 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:201016}
1017
Nate Chapin45f620582021-09-30 17:45:431018void NavigationControllerImpl::GoToOffsetFromRenderer(int offset) {
1019 // Note: This is actually reached in unit tests.
1020 if (!CanGoToOffset(offset))
1021 return;
1022
1023 GoToIndex(GetIndexForOffset(offset), FrameTreeNode::kFrameTreeNodeInvalidId,
1024 false /* is_browser_initiated */);
1025}
1026
Xiaohan Wang7f8052e02022-01-14 18:44:281027#if BUILDFLAG(IS_ANDROID)
WangHui74286d52021-03-31 16:17:151028void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
1029 // Note: This is actually reached in unit tests.
1030 if (!CanGoToOffsetWithSkipping(offset))
1031 return;
1032
Elly Fong-Jonesccc6d1f2021-06-14 18:32:421033 if (offset == 0) {
WangHui74286d52021-03-31 16:17:151034 GoToIndex(GetIndexForOffset(offset));
1035 return;
1036 }
1037 int increment = offset > 0 ? 1 : -1;
1038 // Find the offset without counting skippable entries.
1039 int target_index = GetIndexForOffset(increment);
1040 int non_skippable_entries = 0;
1041 for (int index = target_index; index >= 0 && index < GetEntryCount();
1042 index += increment) {
1043 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1044 non_skippable_entries++;
1045
1046 if (non_skippable_entries == std::abs(offset)) {
1047 target_index = index;
1048 break;
1049 }
1050 }
1051
1052 GoToIndex(target_index);
1053}
1054#endif
1055
[email protected]41374f12013-07-24 02:49:281056bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151057 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281058 return false;
[email protected]6a13a6c2011-12-20 21:47:121059
[email protected]43032342011-03-21 14:10:311060 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281061 return true;
[email protected]cbab76d2008-10-13 22:42:471062}
1063
Michael Thiessen9b14d512019-09-23 21:19:471064void NavigationControllerImpl::PruneForwardEntries() {
1065 DiscardNonCommittedEntries();
1066 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471067 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471068 if (num_removed <= 0)
1069 return;
1070 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1071 NotifyPrunedEntries(this, remove_start_index /* start index */,
1072 num_removed /* count */);
1073}
1074
Aran Gilman37d11632019-10-08 23:07:151075void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1076 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321077 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311078 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511079 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1080 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321081 }
1082}
1083
Harkiran Bolariaba823e42021-05-21 18:30:361084base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL(
1085 const GURL& url,
1086 const Referrer& referrer,
1087 ui::PageTransition transition,
1088 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471089 LoadURLParams params(url);
1090 params.referrer = referrer;
1091 params.transition_type = transition;
1092 params.extra_headers = extra_headers;
Harkiran Bolariaba823e42021-05-21 18:30:361093 return LoadURLWithParams(params);
[email protected]cf002332012-08-14 19:17:471094}
1095
Harkiran Bolariaba823e42021-05-21 18:30:361096base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams(
1097 const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061098 if (params.is_renderer_initiated)
1099 DCHECK(params.initiator_origin.has_value());
1100
naskob8744d22014-08-28 17:07:431101 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151102 "NavigationControllerImpl::LoadURLWithParams", "url",
1103 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291104 bool is_explicit_navigation =
1105 GetContentClient()->browser()->IsExplicitNavigation(
1106 params.transition_type);
1107 if (HandleDebugURL(params.url, params.transition_type,
1108 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431109 // If Telemetry is running, allow the URL load to proceed as if it's
1110 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491111 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431112 cc::switches::kEnableGpuBenchmarking))
Harkiran Bolariaba823e42021-05-21 18:30:361113 return nullptr;
[email protected]47752982014-07-29 08:01:431114 }
[email protected]8bf1048012012-02-08 01:22:181115
[email protected]cf002332012-08-14 19:17:471116 // Checks based on params.load_type.
1117 switch (params.load_type) {
1118 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431119 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471120 break;
1121 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261122 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471123 NOTREACHED() << "Data load must use data scheme.";
Harkiran Bolariaba823e42021-05-21 18:30:361124 return nullptr;
[email protected]cf002332012-08-14 19:17:471125 }
1126 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461127 }
[email protected]e47ae9472011-10-13 19:48:341128
[email protected]e47ae9472011-10-13 19:48:341129 // The user initiated a load, we don't need to reload anymore.
1130 needs_reload_ = false;
1131
Harkiran Bolariaba823e42021-05-21 18:30:361132 return NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441133}
1134
Mohamed Abdelhalim833de902019-09-16 17:41:451135bool NavigationControllerImpl::PendingEntryMatchesRequest(
1136 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191137 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451138 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191139}
1140
[email protected]d202a7c2012-01-04 07:53:471141bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321142 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071143 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331144 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441145 bool is_same_document_navigation,
Nate Chapinc7019dd7d2021-06-25 18:29:251146 bool was_on_initial_empty_document,
Shivani Sharmaffb32b82019-04-09 16:58:471147 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131148 NavigationRequest* navigation_request) {
1149 DCHECK(navigation_request);
shivanigithubf405bf0d2021-11-05 17:58:331150 if (ShouldMaintainTrivialSessionHistory(rfh->frame_tree_node()) &&
1151 GetLastCommittedEntry()) {
Takashi Toyoshimaa9f66802021-08-18 02:40:171152 // Ensure that this navigation does not add a navigation entry, since
1153 // ShouldMaintainTrivialSessionHistory() means we should not add an entry
1154 // beyond the last committed one. Therefore, `should_replace_current_entry`
1155 // should be set, which replaces the current entry, or this should be a
1156 // reload, which does not create a new entry.
1157 DCHECK(params.should_replace_current_entry ||
1158 navigation_request->GetReloadType() != ReloadType::NONE);
1159 }
[email protected]cd2e15742013-03-08 04:08:311160 is_initial_navigation_ = false;
1161
[email protected]0e8db942008-09-24 21:21:481162 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431163 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481164 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361165 if (entry_replaced_by_post_commit_error_) {
1166 if (is_same_document_navigation) {
1167 // Same document navigations should not be possible on error pages and
1168 // would leave the controller in a weird state. Kill the renderer if
1169 // that happens.
1170 bad_message::ReceivedBadMessage(
1171 rfh->GetProcess(), bad_message::NC_SAME_DOCUMENT_POST_COMMIT_ERROR);
1172 }
1173 // Any commit while a post-commit error page is showing should put the
1174 // original entry back, replacing the error page's entry. This includes
1175 // reloads, where the original entry was used as the pending entry and
1176 // should now be at the correct index at commit time.
1177 entries_[last_committed_entry_index_] =
1178 std::move(entry_replaced_by_post_commit_error_);
1179 }
Fergal Daly8e33cf62020-12-12 01:06:071180 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551181 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431182 if (pending_entry_ &&
1183 pending_entry_->GetIsOverridingUserAgent() !=
1184 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1185 overriding_user_agent_changed = true;
Xiaohan Wang7f8052e02022-01-14 18:44:281186#if BUILDFLAG(IS_ANDROID)
Shu Yang5756fa422021-11-05 23:02:541187 // TODO(crbug.com/1266277): Clean up the logic of setting
1188 // |overriding_user_agent_changed| post-launch.
Shu Yang9eabbb902021-11-17 00:45:031189 if (base::FeatureList::IsEnabled(features::kRequestDesktopSiteExceptions)) {
Shu Yang5756fa422021-11-05 23:02:541190 // Must honor user agent overrides in the |navigation_request|, such as
1191 // from things like RequestDesktopSiteWebContentsObserverAndroid. As a
1192 // result, besides comparing |pending_entry_|'s user agent against
1193 // LastCommittedEntry's, also need to compare |navigation_request|'s user
1194 // agent against LastCommittedEntry's.
1195 if (navigation_request->is_overriding_user_agent() !=
1196 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
1197 overriding_user_agent_changed = true;
1198 }
1199 }
Xiaohan Wang7f8052e02022-01-14 18:44:281200#endif // BUILDFLAG(IS_ANDROID)
[email protected]0e8db942008-09-24 21:21:481201 } else {
Gang Wu325f03f42021-02-25 20:00:461202 // GetLastCommittedEntry() is null, so this is the first entry.
Fergal Daly8e33cf62020-12-12 01:06:071203 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481204 details->previous_entry_index = -1;
Gang Wu325f03f42021-02-25 20:00:461205 if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) {
1206 // Default setting is NOT override the user agent, so overriding the user
1207 // agent in first entry should be considered as user agent changed as
1208 // well.
1209 overriding_user_agent_changed = true;
1210 }
Xiaohan Wang7f8052e02022-01-14 18:44:281211#if BUILDFLAG(IS_ANDROID)
Shu Yang5756fa422021-11-05 23:02:541212 // TODO(crbug.com/1266277): Clean up the logic of setting
1213 // |overriding_user_agent_changed| post-launch.
Shu Yang9eabbb902021-11-17 00:45:031214 if (base::FeatureList::IsEnabled(features::kRequestDesktopSiteExceptions)) {
Shu Yang5756fa422021-11-05 23:02:541215 // Must honor user agent overrides in the |navigation_request|, such as
1216 // from things like RequestDesktopSiteWebContentsObserverAndroid. As a
1217 // result, besides checking |pending_entry_|'s user agent, also need to
1218 // check |navigation_request|'s.
1219 if (navigation_request->is_overriding_user_agent()) {
1220 overriding_user_agent_changed = true;
1221 }
1222 }
Xiaohan Wang7f8052e02022-01-14 18:44:281223#endif // BUILDFLAG(IS_ANDROID)
[email protected]0e8db942008-09-24 21:21:481224 }
[email protected]ecd9d8702008-08-28 22:10:171225
Dave Tapuskaa2ab4f252021-07-08 21:31:281226 bool is_main_frame_navigation = !rfh->GetParent();
1227
Alexander Timind2f2e4f22019-04-02 20:04:531228 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1229 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281230 // For primary frame tree navigations, choose an appropriate
1231 // BackForwardCacheMetrics to be associated with the NavigationEntry, by
1232 // either creating a new object or reusing the previous one depending on
1233 // whether it's a main frame navigation or not.
1234 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics;
1235 if (navigation_request->frame_tree_node()->frame_tree()->type() ==
1236 FrameTree::Type::kPrimary) {
1237 back_forward_cache_metrics =
1238 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1239 GetLastCommittedEntry(), is_main_frame_navigation,
1240 params.document_sequence_number);
1241 }
Alexander Timind2f2e4f22019-04-02 20:04:531242 // Notify the last active entry that we have navigated away.
Dave Tapuskaa2ab4f252021-07-08 21:31:281243 if (is_main_frame_navigation && !is_same_document_navigation) {
Alexander Timind2f2e4f22019-04-02 20:04:531244 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1245 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Hajime Hoshic7606502021-04-14 02:42:161246 metrics->MainFrameDidNavigateAwayFromDocument(rfh, navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531247 }
1248 }
1249 }
1250
fdegans9caf66a2015-07-30 21:10:421251 // If there is a pending entry at this point, it should have a SiteInstance,
1252 // except for restored entries.
jam48cea9082017-02-15 06:13:291253 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481254 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081255 pending_entry_->IsRestored());
1256 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151257 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291258 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481259 }
[email protected]e9ba4472008-09-14 15:42:431260
Nasko Oskovaee2f862018-06-15 00:05:521261 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1262 // has changed, this must be treated as a new navigation with replacement.
1263 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491264 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521265 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101266 pending_entry_->GetUniqueID() ==
1267 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521268 pending_entry_->site_instance() &&
1269 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1270 DCHECK_NE(-1, pending_entry_index_);
1271 // TODO(nasko,creis): Instead of setting this value here, set
1272 // should_replace_current_entry on the parameters we send to the
1273 // renderer process as part of CommitNavigation. The renderer should
1274 // in turn send it back here as part of |params| and it can be just
1275 // enforced and renderer process terminated on mismatch.
1276 details->did_replace_entry = true;
1277 } else {
1278 // The renderer tells us whether the navigation replaces the current entry.
1279 details->did_replace_entry = params.should_replace_current_entry;
1280 }
[email protected]bcd904482012-02-01 01:54:221281
[email protected]e9ba4472008-09-14 15:42:431282 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amnia4e27222021-12-22 01:05:001283 details->type = ClassifyNavigation(rfh, params, navigation_request, details);
Rakina Zata Amniddf10502022-01-15 02:56:551284 if (GetLastCommittedEntry()->IsInitialEntry()) {
1285 if (rfh->GetParent()) {
1286 // Subframe navigation on initial NavigationEntry must not append a new
1287 // NavigationEntry (i.e. should not be classified as NEW_SUBFRAME). This
1288 // means every subframe navigation that happens while we're on the initial
1289 // NavigationEntry will always reuse the existing NavigationEntry and
1290 // just update the corresponding FrameNavigationEntry.
1291 DCHECK_EQ(details->type, NAVIGATION_TYPE_AUTO_SUBFRAME);
1292 } else if (details->type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY) {
1293 // This is a navigation that modifies the initial NavigationEntry, either
1294 // for a replacement or a reload. The initial NavigationEntry should
1295 // retain its "initial NavigationEntry" status in this case.
1296 details->should_stay_as_initial_entry = true;
1297 }
1298 }
1299 DCHECK(!details->should_stay_as_initial_entry ||
1300 GetLastCommittedEntry()->IsInitialEntry());
[email protected]4bf3522c2010-08-19 21:00:201301
eugenebutee08663a2017-04-27 17:43:121302 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441303 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121304
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071305 details->is_prerender_activation =
1306 navigation_request->IsPrerenderedPageActivation();
1307
Peter Boströmd7592132019-01-30 04:50:311308 // Make sure we do not discard the pending entry for a different ongoing
1309 // navigation when a same document commit comes in unexpectedly from the
1310 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1311 // other navigation types. See https://crbug.com/900036.
1312 // TODO(crbug.com/926009): Handle history.pushState() as well.
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061313 bool keep_pending_entry =
1314 is_same_document_navigation &&
1315 details->type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY &&
1316 pending_entry_ && !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311317
[email protected]0e8db942008-09-24 21:21:481318 switch (details->type) {
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061319 case NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY:
Charlie Reisc0f17d2d2021-01-12 18:52:491320 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051321 rfh, params, details->is_same_document, details->did_replace_entry,
Rakina Zata Amnia4e27222021-12-22 01:05:001322 previous_document_was_activated, navigation_request, details);
[email protected]e9ba4472008-09-14 15:42:431323 break;
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061324 case NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY:
Charlie Reisc0f17d2d2021-01-12 18:52:491325 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1326 was_restored, navigation_request,
Rakina Zata Amnia4e27222021-12-22 01:05:001327 keep_pending_entry, details);
[email protected]e9ba4472008-09-14 15:42:431328 break;
[email protected]8ff00d72012-10-23 19:12:211329 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471330 RendererDidNavigateNewSubframe(
1331 rfh, params, details->is_same_document, details->did_replace_entry,
Rakina Zata Amnia4e27222021-12-22 01:05:001332 previous_document_was_activated, navigation_request, details);
[email protected]e9ba4472008-09-14 15:42:431333 break;
[email protected]8ff00d72012-10-23 19:12:211334 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391335 if (!RendererDidNavigateAutoSubframe(
Nate Chapinc7019dd7d2021-06-25 18:29:251336 rfh, params, details->is_same_document,
Rakina Zata Amnia4e27222021-12-22 01:05:001337 was_on_initial_empty_document, navigation_request, details)) {
creisce0ef3572017-01-26 17:53:081338 // We don't send a notification about auto-subframe PageState during
1339 // UpdateStateForFrame, since it looks like nothing has changed. Send
1340 // it here at commit time instead.
1341 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431342 return false;
creis59d5a47cb2016-08-24 23:57:191343 }
[email protected]e9ba4472008-09-14 15:42:431344 break;
Aran Gilman37d11632019-10-08 23:07:151345 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431346 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151347 break;
[email protected]765b35502008-08-21 00:51:201348 }
1349
[email protected]688aa65c62012-09-28 04:32:221350 // At this point, we know that the navigation has just completed, so
1351 // record the time.
1352 //
1353 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261354 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331355 base::Time timestamp =
1356 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1357 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131358 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221359
Peter Boströmd7592132019-01-30 04:50:311360 // If we aren't keeping the pending entry, there shouldn't be one at this
1361 // point. Clear it again in case any error cases above forgot to do so.
1362 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1363 // been cleared instead of protecting against it.
1364 if (!keep_pending_entry)
Rakina Zata Amniddf10502022-01-15 02:56:551365 DiscardNonCommittedEntriesWithCommitDetails(details);
[email protected]f233e4232013-02-23 00:55:141366
[email protected]e9ba4472008-09-14 15:42:431367 // All committed entries should have nonempty content state so WebKit doesn't
1368 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471369 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321370 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221371 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441372 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531373 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1374 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281375 if (back_forward_cache_metrics &&
1376 !active_entry->back_forward_cache_metrics()) {
Alexander Timind2f2e4f22019-04-02 20:04:531377 active_entry->set_back_forward_cache_metrics(
1378 std::move(back_forward_cache_metrics));
1379 }
Dave Tapuskaa2ab4f252021-07-08 21:31:281380
1381 // `back_forward_cache_metrics()` may return null as we do not record
1382 // back-forward cache metrics for navigations in non-primary frame trees.
1383 if (active_entry->back_forward_cache_metrics()) {
1384 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
1385 navigation_request,
1386 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
1387 }
naskoc7533512016-05-06 17:01:121388
Charles Reisc0507202017-09-21 00:40:021389 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1390 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1391 // A mismatch can occur if the renderer lies or due to a unique name collision
1392 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121393 FrameNavigationEntry* frame_entry =
1394 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021395 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1396 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031397 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081398 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1399 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031400 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201401
1402 // Remember the bindings the renderer process has at this point, so that
1403 // we do not grant this entry additional bindings if we come back to it.
1404 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301405 }
[email protected]132e281a2012-07-31 18:32:441406
[email protected]97d8f0d2013-10-29 16:49:211407 // Once it is committed, we no longer need to track several pieces of state on
1408 // the entry.
naskoc7533512016-05-06 17:01:121409 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131410
[email protected]49bd30e62011-03-22 20:12:591411 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221412 // TODO(creis): This check won't pass for subframes until we create entries
1413 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001414 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421415 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591416
[email protected]e9ba4472008-09-14 15:42:431417 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001418 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001419 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311420 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531421
arthursonzogni7ddc6542021-04-09 09:16:501422 active_entry->SetIsOverridingUserAgent(
1423 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031424
[email protected]93f230e02011-06-01 14:40:001425 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291426
aelias100c9192017-01-13 00:01:431427 if (overriding_user_agent_changed)
1428 delegate_->UpdateOverridingUserAgent();
1429
creis03b48002015-11-04 00:54:561430 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1431 // one knows the latest NavigationEntry it is showing (whether it has
1432 // committed anything in this navigation or not). This allows things like
1433 // state and title updates from RenderFrames to apply to the latest relevant
1434 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381435 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001436 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381437 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
Hayato Ito2c8c08d02021-06-23 03:38:431438
1439 if (navigation_request->IsPrerenderedPageActivation()) {
1440 BroadcastHistoryOffsetAndLength();
1441 // TODO(crbug.com/1222893): Broadcasting happens after the prerendered page
1442 // is activated. As a result, a "prerenderingchange" event listener sees the
1443 // history.length which is not updated yet. We should guarantee that
1444 // history's length and offset should be updated before a
1445 // "prerenderingchange" event listener runs. One possible approach is to use
1446 // the same IPC which "prerenderingchange" uses, and propagate history's
1447 // length and offset together with that.
1448 }
1449
[email protected]e9ba4472008-09-14 15:42:431450 return true;
initial.commit09911bf2008-07-26 23:55:291451}
1452
[email protected]8ff00d72012-10-23 19:12:211453NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321454 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101455 const mojom::DidCommitProvisionalLoadParams& params,
Rakina Zata Amnia4e27222021-12-22 01:05:001456 NavigationRequest* navigation_request,
1457 LoadCommittedDetails* details) {
1458 if (GetLastCommittedEntry()->IsInitialEntry() &&
1459 navigation_request->is_synchronous_renderer_commit() &&
1460 !navigation_request->IsSameDocument() && !rfh->GetParent() &&
1461 params.should_replace_current_entry) {
Rakina Zata Amniddf10502022-01-15 02:56:551462 // This is a synchronous about:blank navigation on the main frame, which
1463 // used to not create a NavigationEntry when we have no NavigationEntry on
1464 // FrameTree creation. We now have the initial NavigationEntry and are on
1465 // the initial NavigationEntry. To preserve old behavior, we should still
1466 // keep the "initial" status for the new NavigationEntry that we will create
1467 // for this navigation, so that subframe navigations under the synchronously
1468 // committed about:blank document will never append new NavigationEntry, and
1469 // instead will just reuse the initial NavigationEntry and modify the
1470 // corresponding FrameNavigationEntries. See also https://crbug.com/1277414.
1471 details->should_stay_as_initial_entry = true;
Rakina Zata Amnia4e27222021-12-22 01:05:001472 }
1473
Piotr Tworekbad51282020-09-30 19:17:591474 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511475 "ClassifyNavigation");
Rakina Zata Amniafd3c6582021-11-30 06:19:171476 DCHECK(GetLastCommittedEntry());
Rakina Zata Amniddf10502022-01-15 02:56:551477 if (rfh->GetParent() && GetLastCommittedEntry()->IsInitialEntry()) {
Rakina Zata Amnia4e27222021-12-22 01:05:001478 // This is a subframe navigation on the initial empty document, which used
Rakina Zata Amniddf10502022-01-15 02:56:551479 // to not have a NavigationEntry to attach to. Now it can attach to the
1480 // initial NavigationEntry, and we must ensure that its NavigationEntry will
1481 // keep the "initial NavigationEntry" status and won't append a new
1482 // NavigationEntry (it should always do replacement instead).
Rakina Zata Amnia4e27222021-12-22 01:05:001483 // See also https://crbug.com/1277414.
Rakina Zata Amniddf10502022-01-15 02:56:551484 details->should_stay_as_initial_entry = true;
Rakina Zata Amnia4e27222021-12-22 01:05:001485 }
Nasko Oskovae49e292020-08-13 02:08:511486
avi7c6f35e2015-05-08 17:52:381487 if (params.did_create_new_entry) {
Rakina Zata Amniafd3c6582021-11-30 06:19:171488 // A new entry for either the main frame or a subframe.
avi39c1edd32015-06-04 20:06:001489 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491490 trace_return.set_return_reason("new entry, no parent, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061491 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381492 }
1493
Rakina Zata Amniafd3c6582021-11-30 06:19:171494 // Subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511495 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381496 return NAVIGATION_TYPE_NEW_SUBFRAME;
1497 }
1498
Charlie Reisc0f17d2d2021-01-12 18:52:491499 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381500 DCHECK(!params.history_list_was_cleared);
1501
avi39c1edd32015-06-04 20:06:001502 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381503 // All manual subframes would be did_create_new_entry and handled above, so
1504 // we know this is auto.
Rakina Zata Amniafd3c6582021-11-30 06:19:171505 trace_return.set_return_reason("subframe, last commmited, auto subframe");
1506 return NAVIGATION_TYPE_AUTO_SUBFRAME;
avi7c6f35e2015-05-08 17:52:381507 }
1508
Rakina Zata Amnif6950d552020-11-24 03:26:101509 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1510 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381511 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1512 // create a new page.
1513
Hayato Ito2ae49442021-07-02 02:59:251514 // This main frame navigation is not a history navigation (since
1515 // nav_entry_id is 0), but didn't create a new entry. So this must be a
1516 // reload or a replacement navigation, which will modify the existing entry.
1517 //
Nasko Oskov332593c2018-08-16 17:21:341518 // TODO(nasko): With error page isolation, reloading an existing session
1519 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491520 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341521 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511522 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491523 "nav entry 0, last committed, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061524 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381525 }
1526
Rakina Zata Amnif6950d552020-11-24 03:26:101527 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521528 // If the SiteInstance of the |pending_entry_| does not match the
1529 // SiteInstance that got committed, treat this as a new navigation with
1530 // replacement. This can happen if back/forward/reload encounters a server
1531 // redirect to a different site or an isolated error page gets successfully
1532 // reloaded into a different SiteInstance.
1533 if (pending_entry_->site_instance() &&
1534 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491535 trace_return.set_return_reason("pending matching nav entry, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061536 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521537 }
creis77c9aa32015-09-25 19:59:421538
Nasko Oskovaee2f862018-06-15 00:05:521539 if (pending_entry_index_ == -1) {
1540 // In this case, we have a pending entry for a load of a new URL but Blink
1541 // didn't do a new navigation (params.did_create_new_entry). First check
1542 // to make sure Blink didn't treat a new cross-process navigation as
1543 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161544 // we must treat it as NEW rather than the converted reload case below,
1545 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521546 if (!GetLastCommittedEntry() ||
1547 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161548 trace_return.set_return_reason("new pending, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061549 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521550 }
1551
1552 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161553 // We will create a pending entry, but NavigateWithoutEntry will convert
1554 // it to a reload since it's the same page and not create a new entry for
1555 // it. (The user doesn't want to have a new back/forward entry when they
1556 // do this.) Therefore we want to just ignore the pending entry and go
1557 // back to where we were (the "existing entry").
1558 trace_return.set_return_reason("new pending, existing (same) entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061559 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521560 }
avi7c6f35e2015-05-08 17:52:381561 }
1562
creis26d22632017-04-21 20:23:561563 // Everything below here is assumed to be an existing entry, but if there is
1564 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511565 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491566 trace_return.set_return_reason("no last committed, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061567 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511568 }
creis26d22632017-04-21 20:23:561569
Rakina Zata Amni153d5702021-09-13 22:48:001570 if (navigation_request->commit_params().intended_as_new_entry) {
avi7c6f35e2015-05-08 17:52:381571 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491572 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1573 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161574 trace_return.set_return_reason("intended as new entry, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061575 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381576 }
1577
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121578 if (navigation_request->DidEncounterError() &&
1579 failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101580 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381581 // If the renderer was going to a new pending entry that got cleared because
1582 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491583 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381584 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511585 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491586 "unreachable, matching pending, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061587 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381588 }
1589
Charlie Reisc0f17d2d2021-01-12 18:52:491590 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101591 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511592 trace_return.traced_value()->SetInteger("existing_entry_index",
1593 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381594 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441595 // The renderer has committed a navigation to an entry that no longer
1596 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491597 trace_return.set_return_reason("existing entry -1, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061598 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381599 }
1600
avi7c6f35e2015-05-08 17:52:381601 // Since we weeded out "new" navigations above, we know this is an existing
1602 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491603 trace_return.set_return_reason("default return, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061604 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381605}
1606
Rakina Zata Amni3460d382021-10-29 00:43:371607void NavigationControllerImpl::UpdateNavigationEntryDetails(
1608 NavigationEntryImpl* entry,
1609 RenderFrameHostImpl* rfh,
1610 const mojom::DidCommitProvisionalLoadParams& params,
1611 NavigationRequest* request,
1612 NavigationEntryImpl::UpdatePolicy update_policy,
Rakina Zata Amnia4e27222021-12-22 01:05:001613 bool is_new_entry,
1614 LoadCommittedDetails* commit_details) {
Rakina Zata Amni3460d382021-10-29 00:43:371615 // Update the FrameNavigationEntry.
Rakina Zata Amniafd3c6582021-11-30 06:19:171616 std::vector<GURL> redirects;
Rakina Zata Amni3460d382021-10-29 00:43:371617 entry->AddOrUpdateFrameEntry(
1618 rfh->frame_tree_node(), update_policy, params.item_sequence_number,
1619 params.document_sequence_number, params.app_history_key,
1620 rfh->GetSiteInstance(), nullptr, params.url,
1621 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amniafd3c6582021-11-30 06:19:171622 Referrer(*params.referrer),
1623 request ? request->common_params().initiator_origin : params.origin,
1624 request ? request->GetRedirectChain() : redirects, params.page_state,
1625 params.method, params.post_id, nullptr /* blob_url_loader_factory */,
1626 (request && request->web_bundle_navigation_info())
Rakina Zata Amni3460d382021-10-29 00:43:371627 ? request->web_bundle_navigation_info()->Clone()
1628 : nullptr,
Rakina Zata Amniafd3c6582021-11-30 06:19:171629 (request ? request->GetSubresourceWebBundleNavigationInfo() : nullptr),
Rakina Zata Amni3460d382021-10-29 00:43:371630 ComputePolicyContainerPoliciesForFrameEntry(
Rakina Zata Amniafd3c6582021-11-30 06:19:171631 rfh, request && request->IsSameDocument(),
1632 request ? request->common_params().url : params.url));
Rakina Zata Amni3460d382021-10-29 00:43:371633
1634 if (rfh->GetParent()) {
1635 // Only modify the NavigationEntry for main frame navigations.
1636 return;
1637 }
1638 if (entry->update_virtual_url_with_url())
1639 UpdateVirtualURLToURL(entry, params.url);
1640 // Don't use the page type from the pending entry. Some interstitial page
1641 // may have set the type to interstitial. Once we commit, however, the page
1642 // type must always be normal or error.
Rakina Zata Amniafd3c6582021-11-30 06:19:171643 entry->set_page_type((request && request->DidEncounterError())
1644 ? PAGE_TYPE_ERROR
1645 : PAGE_TYPE_NORMAL);
Rakina Zata Amnif297a802022-01-18 03:53:431646 if (commit_details && commit_details->should_stay_as_initial_entry) {
1647 // Retain the "initial NavigationEntry" status.
1648 if (request->IsSameDocument()) {
1649 // If this is for a same-document navigation, the NavigationEntry must be
1650 // reused and should already be marked as the initial NavigationEntry.
1651 DCHECK(entry->IsInitialEntry());
1652 } else {
1653 // If this is for a cross-document navigation, it can be caused by a
1654 // renderer-initiated reload, or the synchronous about:blank commit. Mark
1655 // "for synchronous about:blank" in the latter case, and also when it is
1656 // reloading a "for synchronous about:blank" entry. Otherwise, the entry
1657 // is not for a synchronous about:blank commit.
1658 NavigationEntryImpl::InitialNavigationEntryState new_state =
1659 NavigationEntryImpl::InitialNavigationEntryState::
1660 kInitialNotForSynchronousAboutBlank;
1661 if (entry->IsInitialEntryForSynchronousAboutBlank() ||
1662 request->is_synchronous_renderer_commit()) {
1663 new_state = NavigationEntryImpl::InitialNavigationEntryState::
1664 kInitialForSynchronousAboutBlank;
1665 }
1666 entry->set_initial_navigation_entry_state(new_state);
1667 }
1668 } else if (commit_details && !commit_details->should_stay_as_initial_entry) {
1669 // Remove the "initial NavigationEntry" status.
1670 entry->set_initial_navigation_entry_state(
1671 NavigationEntryImpl::InitialNavigationEntryState::kNonInitial);
Rakina Zata Amnia4e27222021-12-22 01:05:001672 }
Rakina Zata Amniddf10502022-01-15 02:56:551673
Rakina Zata Amni3460d382021-10-29 00:43:371674 if (is_new_entry) {
1675 // Some properties of the NavigationEntry are only set when the entry is
1676 // new (we aren't reusing it).
1677 entry->SetTransitionType(params.transition);
Rakina Zata Amniafd3c6582021-11-30 06:19:171678 entry->SetOriginalRequestURL(request ? request->GetOriginalRequestURL()
1679 : GURL::EmptyGURL());
Rakina Zata Amni3460d382021-10-29 00:43:371680 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
1681 entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
1682 } else {
1683 // We are reusing the NavigationEntry. The site instance will normally be
1684 // the same except for a few cases:
1685 // 1) session restore, when no site instance will be assigned or
1686 // 2) redirect, when the site instance is reset.
1687 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
1688 entry->site_instance() == rfh->GetSiteInstance());
1689 }
1690}
1691
Rakina Zata Amniafd3c6582021-11-30 06:19:171692void NavigationControllerImpl::CreateInitialEntry() {
1693 DCHECK_EQ(entries_.size(), 0u);
1694 RenderFrameHostImpl* rfh = frame_tree_.root()->current_frame_host();
1695 auto params = mojom::DidCommitProvisionalLoadParams::New();
1696 // The initial NavigationEntry's URL is the empty URL. This preserves the old
1697 // behavior of WebContent's GetLastCommittedURL() and GetVisibleURL() from
1698 // before we have initial NavigationEntries.
1699 params->url = GURL::EmptyGURL();
1700 params->http_status_code = 0;
1701 params->url_is_unreachable = false;
1702 params->method = "GET";
1703 params->should_replace_current_entry = false;
1704 params->post_id = -1;
1705 params->embedding_token = base::UnguessableToken::Create();
1706 params->navigation_token = base::UnguessableToken::Create();
1707 params->did_create_new_entry = true;
1708 params->origin = rfh->GetLastCommittedOrigin();
1709 params->should_update_history = true;
1710 params->item_sequence_number = 0;
1711 params->document_sequence_number = 0;
1712 bool is_in_fenced_frame_tree = rfh->frame_tree_node()->IsInFencedFrameTree();
1713 params->transition = is_in_fenced_frame_tree
1714 ? ui::PAGE_TRANSITION_AUTO_SUBFRAME
1715 : ui::PAGE_TRANSITION_LINK;
1716 params->referrer = blink::mojom::Referrer::New();
1717
1718 // Create and insert the initial NavigationEntry.
1719 auto new_entry = std::make_unique<NavigationEntryImpl>(
1720 rfh->GetSiteInstance(), params->url, Referrer(*params->referrer),
1721 rfh->GetLastCommittedOrigin(), std::u16string() /* title */,
1722 ui::PAGE_TRANSITION_TYPED, false /* renderer_initiated */,
1723 nullptr /* blob_url_loader_factory */, true /* is_initial_entry */);
1724 UpdateNavigationEntryDetails(
1725 new_entry.get(), rfh, *params, nullptr /* request */,
Rakina Zata Amnia4e27222021-12-22 01:05:001726 NavigationEntryImpl::UpdatePolicy::kUpdate, true /* is_new_entry */,
1727 nullptr /* commit_details */);
Rakina Zata Amniafd3c6582021-11-30 06:19:171728
1729 InsertOrReplaceEntry(std::move(new_entry), false /* replace_entry */,
1730 false /* was_post_commit_error */,
Rakina Zata Amnia4e27222021-12-22 01:05:001731 is_in_fenced_frame_tree, nullptr /* commit_details */);
Rakina Zata Amniafd3c6582021-11-30 06:19:171732}
1733
Charlie Reisc0f17d2d2021-01-12 18:52:491734void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321735 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071736 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361737 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441738 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471739 bool previous_document_was_activated,
Rakina Zata Amnia4e27222021-12-22 01:05:001740 NavigationRequest* request,
1741 LoadCommittedDetails* commit_details) {
dcheng9bfa5162016-04-09 01:00:571742 std::unique_ptr<NavigationEntryImpl> new_entry;
Anton Bikineevf62d1bf2021-05-15 17:56:071743 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451744 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061745
creisf49dfc92016-07-26 17:05:181746 // First check if this is an in-page navigation. If so, clone the current
1747 // entry instead of looking at the pending entry, because the pending entry
1748 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361749 if (is_same_document && GetLastCommittedEntry()) {
Patrick Monette50e8bd82019-06-13 22:40:451750 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481751 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081752 params.document_sequence_number, params.app_history_key,
1753 rfh->GetSiteInstance(), nullptr, params.url,
1754 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091755 Referrer(*params.referrer), initiator_origin,
1756 request->GetRedirectChain(), params.page_state, params.method,
1757 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391758 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481759 request->GetSubresourceWebBundleNavigationInfo(),
Antonio Sartori78a749f2020-11-30 12:03:391760 // We will set the document policies later in this function.
Titouan Rigoudy6ec70402021-02-02 15:42:191761 nullptr /* policy_container_policies */);
Charles Reisf44482022017-10-13 21:15:031762
creisf49dfc92016-07-26 17:05:181763 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Harkiran Bolaria081c5c22021-09-13 08:44:531764 frame_entry, true, request->frame_tree_node(), frame_tree_.root());
Mike West800532c2021-10-14 09:26:521765 if (new_entry->GetURL().DeprecatedGetOriginAsURL() !=
1766 params.url.DeprecatedGetOriginAsURL()) {
jama78746e2017-02-22 17:21:571767 // TODO(jam): we had one report of this with a URL that was redirecting to
1768 // only tildes. Until we understand that better, don't copy the cert in
1769 // this case.
1770 new_entry->GetSSL() = SSLStatus();
jama78746e2017-02-22 17:21:571771 }
creisf49dfc92016-07-26 17:05:181772
Patrick Monette50e8bd82019-06-13 22:40:451773 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1774 // that |frame_entry| should now have a reference count of exactly 2: one
1775 // due to the local variable |frame_entry|, and another due to |new_entry|
1776 // also retaining one. This should never fail, because it's the main frame.
1777 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181778 }
1779
Harkiran Bolaria59290d62021-03-17 01:53:011780 // If this is an activation navigation from a prerendered page, transfer the
1781 // new entry from an entry already created and stored in the
1782 // NavigationRequest. |new_entry| will not have been set prior to this as
1783 // |is_same_document| is mutually exclusive with
1784 // |IsPrerenderedPageActivation|.
1785 if (request->IsPrerenderedPageActivation()) {
1786 DCHECK(!is_same_document);
1787 DCHECK(!new_entry);
1788 new_entry = request->TakePrerenderNavigationEntry();
1789 DCHECK(new_entry);
1790 }
1791
Charlie Reisc0f17d2d2021-01-12 18:52:491792 // Only make a copy of the pending entry if it is appropriate for the new
1793 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451794 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041795 // 1. The SiteInstance hasn't been assigned to something else.
1796 // 2. The pending entry was intended as a new entry, rather than being a
1797 // history navigation that was interrupted by an unrelated,
1798 // renderer-initiated navigation.
1799 // TODO(csharrison): Investigate whether we can remove some of the coarser
1800 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451801 if (!new_entry && PendingEntryMatchesRequest(request) &&
1802 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341803 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431804 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351805 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431806
Camille Lamy62b826012019-02-26 09:15:471807 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451808 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
creisf49dfc92016-07-26 17:05:181809 }
1810
Charlie Reisc0f17d2d2021-01-12 18:52:491811 // For cross-document commits with no matching pending entry, create a new
1812 // entry.
creisf49dfc92016-07-26 17:05:181813 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061814 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071815 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1816 initiator_origin,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:581817 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451818 params.transition, request->IsRendererInitiated(),
Rakina Zata Amniafd3c6582021-11-30 06:19:171819 nullptr, // blob_url_loader_factory
1820 false); // is_initial_entry
[email protected]f8f93eb2012-09-25 03:06:241821
1822 // Find out whether the new entry needs to update its virtual URL on URL
1823 // change and set up the entry accordingly. This is needed to correctly
1824 // update the virtual URL when replaceState is called after a pushState.
1825 GURL url = params.url;
1826 bool needs_update = false;
Charlie Reisc0f17d2d2021-01-12 18:52:491827 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411828 // update the virtual URL based on the new URL. For example, this is needed
1829 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1830 // the URL.
Rakina Zata Amni3460d382021-10-29 00:43:371831 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1832 &url, browser_context_, &needs_update);
1833 new_entry->set_update_virtual_url_with_url(needs_update);
1834
Camille Lamy62b826012019-02-26 09:15:471835 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451836 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
[email protected]e9ba4472008-09-14 15:42:431837 }
1838
Harkiran Bolaria59290d62021-03-17 01:53:011839 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
1840 // for prerendered contents, if any. This is because prerendering/activation
1841 // technically won't be creating a new document. Unlike BFCache, prerendering
1842 // creates a new NavigationEntry rather than using an existing one.
1843 if (!request->IsPrerenderedPageActivation()) {
Rakina Zata Amni3460d382021-10-29 00:43:371844 UpdateNavigationEntryDetails(new_entry.get(), rfh, params, request,
1845 NavigationEntryImpl::UpdatePolicy::kUpdate,
Rakina Zata Amnia4e27222021-12-22 01:05:001846 true /* is_new_entry */, commit_details);
creis8b5cd4c2015-06-19 00:11:081847
Harkiran Bolaria59290d62021-03-17 01:53:011848 // history.pushState() is classified as a navigation to a new page, but sets
1849 // is_same_document to true. In this case, we already have the title and
1850 // favicon available, so set them immediately.
1851 if (is_same_document && GetLastCommittedEntry()) {
1852 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
1853 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1854 }
[email protected]3a868f212014-08-09 10:41:191855 }
[email protected]ff64b3e2014-05-31 04:07:331856
[email protected]60d6cca2013-04-30 08:47:131857 DCHECK(!params.history_list_was_cleared || !replace_entry);
1858 // The browser requested to clear the session history when it initiated the
1859 // navigation. Now we know that the renderer has updated its state accordingly
1860 // and it is safe to also clear the browser side history.
1861 if (params.history_list_was_cleared) {
Rakina Zata Amniddf10502022-01-15 02:56:551862 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]60d6cca2013-04-30 08:47:131863 entries_.clear();
1864 last_committed_entry_index_ = -1;
1865 }
1866
Nasko Oskovaee2f862018-06-15 00:05:521867 // If this is a new navigation with replacement and there is a
1868 // pending_entry_ which matches the navigation reported by the renderer
1869 // process, then it should be the one replaced, so update the
1870 // last_committed_entry_index_ to use it.
1871 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101872 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521873 last_committed_entry_index_ = pending_entry_index_;
1874 }
1875
Alexander Timine3ec4192020-04-20 16:39:401876 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411877 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401878 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051879
Carlos IL42b416592019-10-07 23:10:361880 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
Dave Tapuska87696ae2021-11-18 18:48:311881 !request->post_commit_error_page_html().empty(),
Rakina Zata Amnia4e27222021-12-22 01:05:001882 rfh->frame_tree_node()->IsInFencedFrameTree(),
1883 commit_details);
[email protected]e9ba4472008-09-14 15:42:431884}
1885
Charlie Reisc0f17d2d2021-01-12 18:52:491886void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321887 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071888 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361889 bool is_same_document,
jam48cea9082017-02-15 06:13:291890 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451891 NavigationRequest* request,
Rakina Zata Amnia4e27222021-12-22 01:05:001892 bool keep_pending_entry,
1893 LoadCommittedDetails* commit_details) {
creis26d22632017-04-21 20:23:561894 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1895 << "that a last committed entry exists.";
1896
[email protected]e9ba4472008-09-14 15:42:431897 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001898 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431899
Charlie Reis7e2cb6d2021-01-26 01:27:161900 NavigationEntryImpl* entry = nullptr;
Rakina Zata Amni153d5702021-09-13 22:48:001901 if (request->commit_params().intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:161902 // We're guaranteed to have a last committed entry if intended_as_new_entry
1903 // is true.
avicbdc4c12015-07-01 16:07:111904 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:161905 DCHECK(entry);
1906
1907 // If the NavigationRequest matches a new pending entry and is classified as
1908 // EXISTING_ENTRY, then it is a navigation to the same URL that was
1909 // converted to a reload, such as a user pressing enter in the omnibox.
1910 if (pending_entry_ && pending_entry_index_ == -1 &&
1911 pending_entry_->GetUniqueID() ==
1912 request->commit_params().nav_entry_id) {
1913 // Note: The pending entry will usually have a real ReloadType here, but
1914 // it can still be ReloadType::NONE in cases that
1915 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
1916
1917 // If we classified this correctly, the SiteInstance should not have
1918 // changed.
1919 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
1920
1921 // For converted reloads, we assign the entry's unique ID to be that of
1922 // the new one. Since this is always the result of a user action, we want
1923 // to dismiss infobars, etc. like a regular user-initiated navigation.
1924 entry->set_unique_id(pending_entry_->GetUniqueID());
1925
1926 // The extra headers may have changed due to reloading with different
1927 // headers.
1928 entry->set_extra_headers(pending_entry_->extra_headers());
1929 }
1930 // Otherwise, this was intended as a new entry but the pending entry was
1931 // lost in the meantime and no new entry was created. We are stuck at the
1932 // last committed entry.
1933
1934 // Even if this is a converted reload from pressing enter in the omnibox,
1935 // the server could redirect, requiring an update to the SSL status. If this
1936 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451937 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:161938 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:471939 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451940 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:161941 }
Rakina Zata Amnif6950d552020-11-24 03:26:101942 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:111943 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:101944 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b902016-09-01 16:58:161945
eugenebut604866f2017-05-10 21:35:361946 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451947 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361948 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1949 // this was a restored same document navigation entry, then it won't have
1950 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1951 // navigated it.
jam48cea9082017-02-15 06:13:291952 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
Mike West800532c2021-10-14 09:26:521953 if (entry->GetURL().DeprecatedGetOriginAsURL() ==
1954 last_entry->GetURL().DeprecatedGetOriginAsURL() &&
jam48cea9082017-02-15 06:13:291955 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1956 was_restored) {
1957 entry->GetSSL() = last_entry->GetSSL();
1958 }
1959 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451960 // In rapid back/forward navigations |request| sometimes won't have a cert
1961 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191962 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451963 if (request->GetSSLInfo().has_value() &&
1964 request->GetSSLInfo()->is_valid()) {
1965 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
Mike West800532c2021-10-14 09:26:521966 } else if (entry->GetURL().DeprecatedGetOriginAsURL() !=
1967 request->GetURL().DeprecatedGetOriginAsURL()) {
John Abd-El-Malek3f247082017-12-07 19:02:191968 entry->GetSSL() = SSLStatus();
1969 }
jam48cea9082017-02-15 06:13:291970 }
avicbdc4c12015-07-01 16:07:111971 } else {
1972 // This is renderer-initiated. The only kinds of renderer-initated
Rakina Zata Amni557afb92021-07-17 04:39:571973 // navigations that are EXISTING_ENTRY are same-document navigations that
1974 // result in replacement (e.g. history.replaceState(), location.replace(),
1975 // forced replacements for trivial session history contexts). For these
1976 // cases, we reuse the last committed entry.
avicbdc4c12015-07-01 16:07:111977 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101978
Mikel Astizba9cf2fd2017-12-17 10:38:101979 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1980 // to avoid misleading interpretations (e.g. URLs paired with
1981 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1982 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1983 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1984
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571985 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:101986
eugenebut604866f2017-05-10 21:35:361987 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451988 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361989 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471990 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451991 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
avicbdc4c12015-07-01 16:07:111992 }
1993 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:431994
Rakina Zata Amni3460d382021-10-29 00:43:371995 UpdateNavigationEntryDetails(entry, rfh, params, request,
1996 NavigationEntryImpl::UpdatePolicy::kUpdate,
Rakina Zata Amnia4e27222021-12-22 01:05:001997 false /* is_new_entry */, commit_details);
creis22a7b4c2016-04-28 07:20:301998
[email protected]5ccd4dc2012-10-24 02:28:141999 // The redirected to page should not inherit the favicon from the previous
2000 // page.
eugenebut604866f2017-05-10 21:35:362001 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:482002 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:142003
Peter Boströmd7592132019-01-30 04:50:312004 // We should also usually discard the pending entry if it corresponds to a
2005 // different navigation, since that one is now likely canceled. In rare
2006 // cases, we leave the pending entry for another navigation in place when we
2007 // know it is still ongoing, to avoid a flicker in the omnibox (see
2008 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:432009 //
2010 // Note that we need to use the "internal" version since we don't want to
2011 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:312012 if (!keep_pending_entry)
Rakina Zata Amnia4e27222021-12-22 01:05:002013 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]40bcc302009-03-02 20:50:392014
Carlos IL4dea8902020-05-26 15:14:292015 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:112016 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:432017}
2018
[email protected]d202a7c2012-01-04 07:53:472019void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:322020 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072021 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:362022 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:472023 bool replace_entry,
2024 bool previous_document_was_activated,
Rakina Zata Amnia4e27222021-12-22 01:05:002025 NavigationRequest* request,
2026 LoadCommittedDetails* commit_details) {
avi25f5f9e2015-07-17 20:08:262027 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2028 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
Rakina Zata Amniddf10502022-01-15 02:56:552029 // The NEW_SUBFRAME path should never result in an initial NavigationEntry.
2030 DCHECK(!commit_details->should_stay_as_initial_entry);
[email protected]09b8f82f2009-06-16 20:22:112031
[email protected]e9ba4472008-09-14 15:42:432032 // Manual subframe navigations just get the current entry cloned so the user
2033 // can go back or forward to it. The actual subframe information will be
2034 // stored in the page state for each of those entries. This happens out of
2035 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:092036 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
2037 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:382038
Mikel Astizba9cf2fd2017-12-17 10:38:102039 // The DCHECK below documents the fact that we don't know of any situation
2040 // where |replace_entry| is true for subframe navigations. This simplifies
2041 // reasoning about the replacement struct for subframes (see
2042 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
2043 DCHECK(!replace_entry);
2044
Patrick Monette50e8bd82019-06-13 22:40:452045 // This FrameNavigationEntry might not end up being used in the
2046 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Anton Bikineevf62d1bf2021-05-15 17:56:072047 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452048 request->common_params().initiator_origin;
Patrick Monette50e8bd82019-06-13 22:40:452049 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:482050 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:082051 params.document_sequence_number, params.app_history_key,
2052 rfh->GetSiteInstance(), nullptr, params.url,
2053 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:092054 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072055 params.page_state, params.method, params.post_id,
2056 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032057 request->web_bundle_navigation_info()
2058 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392059 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482060 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:192061 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
Rakina Zata Amniafd3c6582021-11-30 06:19:172062 request->GetURL()));
Charles Reisf44482022017-10-13 21:15:032063
creisce0ef3572017-01-26 17:53:082064 std::unique_ptr<NavigationEntryImpl> new_entry =
2065 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:452066 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:002067 frame_tree_.root());
creise062d542015-08-25 02:01:552068
Alexander Timine3ec4192020-04-20 16:39:402069 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:412070 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:402071 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:472072
creisce0ef3572017-01-26 17:53:082073 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:452074 // to replace, which can happen due to a unique name change. See
2075 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
2076 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:382077
Dave Tapuska87696ae2021-11-18 18:48:312078 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false,
Rakina Zata Amnia4e27222021-12-22 01:05:002079 rfh->frame_tree_node()->IsInFencedFrameTree(),
2080 commit_details);
[email protected]e9ba4472008-09-14 15:42:432081}
2082
[email protected]d202a7c2012-01-04 07:53:472083bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:322084 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072085 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392086 bool is_same_document,
Nate Chapinc7019dd7d2021-06-25 18:29:252087 bool was_on_initial_empty_document,
Rakina Zata Amnia4e27222021-12-22 01:05:002088 NavigationRequest* request,
2089 LoadCommittedDetails* commit_details) {
avi9f07a0c2015-02-18 22:51:292090 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2091 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2092
[email protected]e9ba4472008-09-14 15:42:432093 // We're guaranteed to have a previously committed entry, and we now need to
2094 // handle navigation inside of a subframe in it without creating a new entry.
2095 DCHECK(GetLastCommittedEntry());
2096
creis913c63ce2016-07-16 19:52:522097 // For newly created subframes, we don't need to send a commit notification.
2098 // This is only necessary for history navigations in subframes.
2099 bool send_commit_notification = false;
2100
Rakina Zata Amnif6950d552020-11-24 03:26:102101 // If |nav_entry_id| is non-zero and matches an existing entry, this
2102 // is a history navigation. Update the last committed index accordingly. If
2103 // we don't recognize the |nav_entry_id|, it might be a recently
2104 // pruned entry. We'll handle it below.
2105 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2106 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472107 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062108 // Make sure that a subframe commit isn't changing the main frame's
2109 // origin. Otherwise the renderer process may be confused, leading to a
2110 // URL spoof. We can't check the path since that may change
2111 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572112 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2113 // about:blank, file, and unique origins are more subtle to get right.
Charlie Reis95fbca442021-05-21 21:38:242114 // We should use checks similar to RenderFrameHostImpl's
2115 // CanCommitUrlAndOrigin on the main frame during subframe commits.
2116 // See https://crbug.com/1209092.
creis37988b92016-06-10 18:03:572117 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2118 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2119 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2120 dest_top_url.SchemeIsHTTPOrHTTPS() &&
Mike West800532c2021-10-14 09:26:522121 current_top_url.DeprecatedGetOriginAsURL() !=
2122 dest_top_url.DeprecatedGetOriginAsURL()) {
creisfb6eeb62016-05-10 19:01:512123 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2124 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062125 }
creis3cdc3b02015-05-29 23:00:472126
creis913c63ce2016-07-16 19:52:522127 // We only need to discard the pending entry in this history navigation
2128 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062129 last_committed_entry_index_ = entry_index;
Rakina Zata Amnia4e27222021-12-22 01:05:002130 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
creis913c63ce2016-07-16 19:52:522131
2132 // History navigations should send a commit notification.
2133 send_commit_notification = true;
avi98405c22015-05-21 20:47:062134 }
[email protected]e9ba4472008-09-14 15:42:432135 }
[email protected]f233e4232013-02-23 00:55:142136
creisce0ef3572017-01-26 17:53:082137 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2138 // it may be a "history auto" case where we update an existing one.
2139 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Nate Chapin9f169072021-06-09 19:32:372140
2141 // We may want to update |last_committed|'s FrameNavigationEntry (if one
2142 // exists), or we may want to clobber it and create a new one. We update in
2143 // cases where the corresponding FrameNavigationEntry is conceptually similar
2144 // to the document described by the commit params: same-document
2145 // navigations, history traversal to an existing entry, and reloads (including
2146 // a "soft reload" where we navigate to the same url without flagging it as a
2147 // reload). But in the case of a different document that is not logically
2148 // related to the committed FrameNavigationEntry's document (cross-document,
2149 // not same url, not a reload, not a history traversal), we replace rather
2150 // than update.
Nate Chapinc7019dd7d2021-06-25 18:29:252151 //
Nate Chapin9f169072021-06-09 19:32:372152 // In the case where we update, the FrameNavigationEntry will potentially be
2153 // shared across multiple NavigationEntries, and any updates will be reflected
2154 // in all of those NavigationEntries. In the replace case, any existing
2155 // sharing with other NavigationEntries will stop.
Nate Chapinc7019dd7d2021-06-25 18:29:252156 //
2157 // When navigating away from the initial empty document, we also update rather
2158 // than replace. Either update or replace will overwrite the initial empty
2159 // document state for |last_committed|, but if the FrameNavigationEntry for
2160 // the initial empty document is shared across multiple NavigationEntries (due
2161 // to a navigation in another frame), we want to make sure we overwrite the
2162 // initial empty document state everywhere this FrameNavigationEntry is used,
2163 // which is accompished by updating the existing FrameNavigationEntry.
Nate Chapin9f169072021-06-09 19:32:372164 FrameNavigationEntry* last_committed_frame_entry =
2165 last_committed->GetFrameEntry(rfh->frame_tree_node());
2166 NavigationEntryImpl::UpdatePolicy update_policy =
2167 NavigationEntryImpl::UpdatePolicy::kUpdate;
2168 if (request->common_params().navigation_type ==
Minggang Wangb9f3fa92021-07-01 15:30:312169 blink::mojom::NavigationType::DIFFERENT_DOCUMENT &&
Nate Chapin9f169072021-06-09 19:32:372170 last_committed_frame_entry &&
Nate Chapinc7019dd7d2021-06-25 18:29:252171 last_committed_frame_entry->url() != params.url &&
2172 !was_on_initial_empty_document) {
Nate Chapin9f169072021-06-09 19:32:372173 update_policy = NavigationEntryImpl::UpdatePolicy::kReplace;
2174 }
2175
Rakina Zata Amni3460d382021-10-29 00:43:372176 UpdateNavigationEntryDetails(last_committed, rfh, params, request,
Rakina Zata Amnia4e27222021-12-22 01:05:002177 update_policy, false /* is_new_entry */,
2178 commit_details);
creis625a0c7d2015-03-24 23:17:122179
creis913c63ce2016-07-16 19:52:522180 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432181}
2182
[email protected]d202a7c2012-01-04 07:53:472183int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232184 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032185 for (size_t i = 0; i < entries_.size(); ++i) {
2186 if (entries_[i].get() == entry)
2187 return i;
2188 }
2189 return -1;
[email protected]765b35502008-08-21 00:51:202190}
2191
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572192void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242193 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572194 NavigationControllerImpl* source =
2195 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572196 // Verify that we look new.
Rakina Zata Amniafd3c6582021-11-30 06:19:172197 DCHECK_EQ(1, GetEntryCount());
2198 DCHECK(GetLastCommittedEntry()->IsInitialEntry());
Lei Zhang96031532019-10-10 19:05:472199 DCHECK(!GetPendingEntry());
Rakina Zata Amniafd3c6582021-11-30 06:19:172200 entries_.clear();
[email protected]ce3fa3c2009-04-20 19:55:572201
Rakina Zata Amniafd3c6582021-11-30 06:19:172202 if (source->GetEntryCount() == 0) {
2203 NOTREACHED();
2204 return;
2205 }
[email protected]ce3fa3c2009-04-20 19:55:572206
Francois Dorayeaace782017-06-21 16:37:242207 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442208 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572209 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572210
Fergal Dalya1d569972021-03-16 03:24:532211 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302212 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532213 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2214 session_storage_namespace_map_[it.first] = source_namespace->Clone();
Aaron Colwellb731a0ae2021-03-19 19:14:472215 OnStoragePartitionIdAdded(it.first);
[email protected]fdac6ade2013-07-20 01:06:302216 }
[email protected]4e6419c2010-01-15 04:50:342217
Lukasz Anforowicz0de0f452020-12-02 19:57:152218 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572219}
2220
Aran Gilman37d11632019-10-08 23:07:152221void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2222 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162223 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012224 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162225
[email protected]d202a7c2012-01-04 07:53:472226 NavigationControllerImpl* source =
2227 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252228
avi2b177592014-12-10 02:08:022229 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012230 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252231
[email protected]474f8512013-05-31 22:31:162232 // We now have one entry, possibly with a new pending entry. Ensure that
2233 // adding the entries from source won't put us over the limit.
2234 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572235 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572236 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252237
Carlos IL4dea8902020-05-26 15:14:292238 // Insert the entries from source. Ignore any pending entry, since it has not
2239 // committed in source.
[email protected]474f8512013-05-31 22:31:162240 int max_source_index = source->last_committed_entry_index_;
Rakina Zata Amniafd3c6582021-11-30 06:19:172241 DCHECK_NE(max_source_index, -1);
2242 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572243
2244 // Ignore the source's current entry if merging with replacement.
2245 // TODO(davidben): This should preserve entries forward of the current
2246 // too. http://crbug.com/317872
2247 if (replace_entry && max_source_index > 0)
2248 max_source_index--;
2249
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572250 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252251
2252 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552253 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142254
Hayato Ito2c8c08d02021-06-23 03:38:432255 BroadcastHistoryOffsetAndLength();
[email protected]e1cd5452010-08-26 18:03:252256}
2257
[email protected]79368982013-11-13 01:11:012258bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162259 // If there is no last committed entry, we cannot prune. Even if there is a
2260 // pending entry, it may not commit, leaving this WebContents blank, despite
2261 // possibly giving it new entries via CopyStateFromAndPrune.
2262 if (last_committed_entry_index_ == -1)
2263 return false;
[email protected]9350602e2013-02-26 23:27:442264
[email protected]474f8512013-05-31 22:31:162265 // We cannot prune if there is a pending entry at an existing entry index.
2266 // It may not commit, so we have to keep the last committed entry, and thus
2267 // there is no sensible place to keep the pending entry. It is ok to have
2268 // a new pending entry, which can optionally commit as a new navigation.
2269 if (pending_entry_index_ != -1)
2270 return false;
2271
[email protected]474f8512013-05-31 22:31:162272 return true;
2273}
2274
[email protected]79368982013-11-13 01:11:012275void NavigationControllerImpl::PruneAllButLastCommitted() {
2276 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162277
avi2b177592014-12-10 02:08:022278 DCHECK_EQ(0, last_committed_entry_index_);
2279 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442280
Hayato Ito2c8c08d02021-06-23 03:38:432281 BroadcastHistoryOffsetAndLength();
[email protected]9350602e2013-02-26 23:27:442282}
2283
[email protected]79368982013-11-13 01:11:012284void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162285 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012286 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262287
[email protected]474f8512013-05-31 22:31:162288 // Erase all entries but the last committed entry. There may still be a
2289 // new pending entry after this.
2290 entries_.erase(entries_.begin(),
2291 entries_.begin() + last_committed_entry_index_);
2292 entries_.erase(entries_.begin() + 1, entries_.end());
2293 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262294}
2295
Christian Dullweber1af31e62018-02-22 11:49:482296void NavigationControllerImpl::DeleteNavigationEntries(
2297 const DeletionPredicate& deletionPredicate) {
2298 // It is up to callers to check the invariants before calling this.
2299 CHECK(CanPruneAllButLastCommitted());
2300 std::vector<int> delete_indices;
2301 for (size_t i = 0; i < entries_.size(); i++) {
2302 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572303 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482304 delete_indices.push_back(i);
2305 }
2306 }
2307 if (delete_indices.empty())
2308 return;
2309
2310 if (delete_indices.size() == GetEntryCount() - 1U) {
2311 PruneAllButLastCommitted();
2312 } else {
2313 // Do the deletion in reverse to preserve indices.
2314 for (auto it = delete_indices.rbegin(); it != delete_indices.rend(); ++it) {
2315 RemoveEntryAtIndex(*it);
2316 }
Hayato Ito2c8c08d02021-06-23 03:38:432317 BroadcastHistoryOffsetAndLength();
Christian Dullweber1af31e62018-02-22 11:49:482318 }
2319 delegate()->NotifyNavigationEntriesDeleted();
2320}
2321
Shivani Sharma883f5f32019-02-12 18:20:012322bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2323 auto* entry = GetEntryAtIndex(index);
2324 return entry && entry->should_skip_on_back_forward_ui();
2325}
2326
Carlos Caballero35ce710c2019-09-19 10:59:452327BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2328 return back_forward_cache_;
2329}
2330
clamy987a3752018-05-03 17:36:262331void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2332 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2333 // progress, since this will cause a use-after-free. (We only allow this
2334 // when the tab is being destroyed for shutdown, since it won't return to
2335 // NavigateToEntry in that case.) http://crbug.com/347742.
Takashi Toyoshimaea534ef22021-07-21 03:27:592336 CHECK(!in_navigate_to_pending_entry_ || frame_tree_.IsBeingDestroyed());
clamy987a3752018-05-03 17:36:262337
2338 if (was_failure && pending_entry_) {
2339 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2340 } else {
2341 failed_pending_entry_id_ = 0;
2342 }
2343
2344 if (pending_entry_) {
2345 if (pending_entry_index_ == -1)
2346 delete pending_entry_;
2347 pending_entry_index_ = -1;
2348 pending_entry_ = nullptr;
2349 }
arthursonzogni66f711c2019-10-08 14:40:362350
2351 // Ensure any refs to the current pending entry are ignored if they get
2352 // deleted, by clearing the set of known refs. All future pending entries will
2353 // only be affected by new refs.
2354 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262355}
2356
2357void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2358 if (pending_entry_)
2359 pending_entry_->set_ssl_error(error);
2360}
2361
Xiaohan Wang7f8052e02022-01-14 18:44:282362#if BUILDFLAG(IS_ANDROID)
Camille Lamy5193caa2018-10-12 11:59:422363// static
2364bool NavigationControllerImpl::ValidateDataURLAsString(
2365 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2366 if (!data_url_as_string)
2367 return false;
2368
2369 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2370 return false;
2371
2372 // The number of characters that is enough for validating a data: URI.
2373 // From the GURL's POV, the only important part here is scheme, it doesn't
2374 // check the actual content. Thus we can take only the prefix of the url, to
2375 // avoid unneeded copying of a potentially long string.
2376 const size_t kDataUriPrefixMaxLen = 64;
2377 GURL data_url(
2378 std::string(data_url_as_string->front_as<char>(),
2379 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2380 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2381 return false;
2382
2383 return true;
2384}
2385#endif
2386
Shivani Sharma194877032019-03-07 17:52:472387void NavigationControllerImpl::NotifyUserActivation() {
2388 // When a user activation occurs, ensure that all adjacent entries for the
2389 // same document clear their skippable bit, so that the history manipulation
2390 // intervention does not apply to them.
2391 auto* last_committed_entry = GetLastCommittedEntry();
2392 if (!last_committed_entry)
2393 return;
Shivani Sharma194877032019-03-07 17:52:472394
shivanigithub99368382021-06-16 18:33:372395 if (base::FeatureList::IsEnabled(
2396 features::kDebugHistoryInterventionNoUserActivation)) {
2397 return;
2398 }
2399
Shivani Sharmac4cc8922019-04-18 03:11:172400 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472401}
2402
clamy987a3752018-05-03 17:36:262403bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2404 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172405 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262406 NavigationEntryImpl* entry =
2407 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2408 if (!entry)
2409 return false;
2410
2411 FrameNavigationEntry* frame_entry =
2412 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2413 if (!frame_entry)
2414 return false;
2415
Nate Chapin45f620582021-09-30 17:45:432416 // |is_browser_initiated| is false here because a navigation in a new subframe
2417 // always begins with renderer action (i.e., an HTML element being inserted
2418 // into the DOM), so it is always renderer-initiated.
Camille Lamy5193caa2018-10-12 11:59:422419 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572420 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232421 ReloadType::NONE, false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432422 true /* is_history_navigation_in_new_child */,
2423 false /* is_browser_initiated */);
clamyea99ea12018-05-28 13:54:232424
2425 if (!request)
2426 return false;
2427
arthursonzognif046d4a2019-12-12 19:08:102428 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412429
Lukasz Anforowicz9ee83c272020-12-01 20:14:052430 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2431 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232432
2433 return true;
clamy987a3752018-05-03 17:36:262434}
2435
Tsuyoshi Horo52fd08e2020-07-07 07:03:452436bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2437 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2438 if (!entry)
2439 return false;
Rakina Zata Amnif297a802022-01-18 03:53:432440
2441 if (entry->IsInitialEntryNotForSynchronousAboutBlank()) {
2442 // We should never navigate to an existing initial NavigationEntry that is
2443 // the initial NavigationEntry for the initial empty document that hasn't
2444 // been overridden by the synchronous about:blank commit, to preserve
2445 // legacy behavior where trying to reload when the main frame is on the
2446 // initial empty document won't result in a navigation. See also
2447 // https://crbug.com/1277414.
2448 return false;
2449 }
Tsuyoshi Horo52fd08e2020-07-07 07:03:452450 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2451 if (!frame_entry)
2452 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142453 ReloadType reload_type = ReloadType::NORMAL;
2454 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452455 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142456 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452457 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432458 false /* is_history_navigation_in_new_child */,
2459 true /* is_browser_initiated */);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452460 if (!request)
2461 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052462 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452463 return true;
2464}
2465
Dave Tapuska8bfd84c2019-03-26 20:47:162466void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2467 int offset,
2468 int sandbox_frame_tree_node_id) {
2469 if (!CanGoToOffset(offset))
2470 return;
Nate Chapin45f620582021-09-30 17:45:432471 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id,
2472 false /* is_browser_initiated */);
Dave Tapuska8bfd84c2019-03-26 20:47:162473}
2474
clamy987a3752018-05-03 17:36:262475void NavigationControllerImpl::NavigateFromFrameProxy(
2476 RenderFrameHostImpl* render_frame_host,
2477 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022478 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452479 int initiator_process_id,
Anton Bikineevf62d1bf2021-05-15 17:56:072480 const absl::optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262481 bool is_renderer_initiated,
2482 SiteInstance* source_site_instance,
2483 const Referrer& referrer,
2484 ui::PageTransition page_transition,
2485 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252486 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262487 const std::string& method,
2488 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092489 const std::string& extra_headers,
Antonio Sartori2f763d9d2021-04-21 10:04:142490 network::mojom::SourceLocationPtr source_location,
John Delaney50425f82020-04-07 16:26:212491 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Anton Bikineevf62d1bf2021-05-15 17:56:072492 const absl::optional<blink::Impression>& impression) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582493 if (is_renderer_initiated)
2494 DCHECK(initiator_origin.has_value());
2495
clamy987a3752018-05-03 17:36:262496 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582497
clamy987a3752018-05-03 17:36:262498 // Create a NavigationEntry for the transfer, without making it the pending
2499 // entry. Subframe transfers should have a clone of the last committed entry
2500 // with a FrameNavigationEntry for the target frame. Main frame transfers
2501 // should have a new NavigationEntry.
2502 // TODO(creis): Make this unnecessary by creating (and validating) the params
2503 // directly, passing them to the destination RenderFrameHost. See
2504 // https://crbug.com/536906.
2505 std::unique_ptr<NavigationEntryImpl> entry;
Harkiran Bolariae1b5158b2021-09-16 19:03:262506 if (!render_frame_host->is_main_frame()) {
clamy987a3752018-05-03 17:36:262507 // Subframe case: create FrameNavigationEntry.
Rakina Zata Amniafd3c6582021-11-30 06:19:172508 CHECK(GetLastCommittedEntry());
2509 entry = GetLastCommittedEntry()->Clone();
2510 entry->set_extra_headers(extra_headers);
2511 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2512 // Renderer-initiated navigation that target a remote frame are currently
2513 // classified as browser-initiated when this one has already navigated.
2514 // See https://crbug.com/722251.
Nate Chapin9f169072021-06-09 19:32:372515 // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter
2516 // to CreateNavigationRequestFromLoadParams(), so while kReplace might
2517 // remove child FrameNavigationEntries (e.g., if this is a cross-process
2518 // same-document navigation), they will still be present in the
2519 // committed NavigationEntry that will be referenced to construct the new
2520 // FrameNavigationEntry tree when this navigation commits.
clamy987a3752018-05-03 17:36:262521 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:082522 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582523 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072524 absl::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202525 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392526 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482527 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori79d549d2021-02-18 12:59:542528 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262529 } else {
2530 // Main frame case.
2531 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102532 url, referrer, initiator_origin, source_site_instance, page_transition,
2533 is_renderer_initiated, extra_headers, browser_context_,
Hayato Ito303654c2021-06-30 09:07:542534 blob_url_loader_factory));
clamy987a3752018-05-03 17:36:262535 entry->root_node()->frame_entry->set_source_site_instance(
2536 static_cast<SiteInstanceImpl*>(source_site_instance));
2537 entry->root_node()->frame_entry->set_method(method);
2538 }
clamy987a3752018-05-03 17:36:262539
Camille Lamy5193caa2018-10-12 11:59:422540 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262541 if (GetLastCommittedEntry() &&
2542 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2543 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422544 override_user_agent = true;
clamy987a3752018-05-03 17:36:262545 }
2546 // TODO(creis): Set user gesture and intent received timestamp on Android.
2547
2548 // We may not have successfully added the FrameNavigationEntry to |entry|
2549 // above (per https://crbug.com/608402), in which case we create it from
2550 // scratch. This works because we do not depend on |frame_entry| being inside
2551 // |entry| during NavigateToEntry. This will go away when we shortcut this
2552 // further in https://crbug.com/536906.
2553 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2554 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452555 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Nate Chapinfbfe5af2021-06-10 17:22:082556 node->unique_name(), -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582557 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072558 absl::nullopt /* origin */, referrer, initiator_origin,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:122559 std::vector<GURL>(), blink::PageState(), method, -1,
2560 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482561 nullptr /* subresource_web_bundle_navigation_info */,
Titouan Rigoudy6ec70402021-02-02 15:42:192562 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262563 }
2564
Camille Lamy5193caa2018-10-12 11:59:422565 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022566 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452567 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292568 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422569 params.source_site_instance = source_site_instance;
2570 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2571 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032572 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422573 params.referrer = referrer;
2574 /* params.redirect_chain: skip */
2575 params.extra_headers = extra_headers;
2576 params.is_renderer_initiated = is_renderer_initiated;
2577 params.override_user_agent = UA_OVERRIDE_INHERIT;
2578 /* params.base_url_for_data_url: skip */
2579 /* params.virtual_url_for_data_url: skip */
2580 /* params.data_url_as_string: skip */
2581 params.post_data = post_body;
2582 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582583 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422584 /* params.frame_name: skip */
2585 // TODO(clamy): See if user gesture should be propagated to this function.
2586 params.has_user_gesture = false;
2587 params.should_clear_history_list = false;
2588 params.started_from_context_menu = false;
2589 /* params.navigation_ui_data: skip */
2590 /* params.input_start: skip */
Minggang Wangf59db47b2021-06-16 01:56:222591 params.was_activated = blink::mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542592 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212593 params.impression = impression;
Antonio Sartori6984c742021-08-26 08:03:412594 params.download_policy = std::move(download_policy);
Camille Lamy5193caa2018-10-12 11:59:422595
2596 std::unique_ptr<NavigationRequest> request =
2597 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032598 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:142599 false /* has_user_gesture */, std::move(source_location),
Antonio Sartori6984c742021-08-26 08:03:412600 ReloadType::NONE, entry.get(), frame_entry.get());
clamyea99ea12018-05-28 13:54:232601
2602 if (!request)
2603 return;
2604
Arthur Hemery948742762019-09-18 10:06:242605 // At this stage we are proceeding with this navigation. If this was renderer
2606 // initiated with user gesture, we need to make sure we clear up potential
2607 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2608 DiscardNonCommittedEntries();
2609
Lukasz Anforowicz9ee83c272020-12-01 20:14:052610 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262611}
2612
[email protected]d1198fd2012-08-13 22:50:192613void NavigationControllerImpl::SetSessionStorageNamespace(
Aaron Colwellf3b316e2021-03-11 20:17:052614 const StoragePartitionId& partition_id,
[email protected]8ff00d72012-10-23 19:12:212615 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192616 if (!session_storage_namespace)
2617 return;
2618
2619 // We can't overwrite an existing SessionStorage without violating spec.
2620 // Attempts to do so may give a tab access to another tab's session storage
2621 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152622 bool successful_insert =
2623 session_storage_namespace_map_
Aaron Colwellf3b316e2021-03-11 20:17:052624 .insert(std::make_pair(partition_id,
2625 static_cast<SessionStorageNamespaceImpl*>(
2626 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302627 .second;
2628 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
Aaron Colwellb731a0ae2021-03-19 19:14:472629 OnStoragePartitionIdAdded(partition_id);
[email protected]d1198fd2012-08-13 22:50:192630}
2631
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572632bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Rakina Zata Amniafd3c6582021-11-30 06:19:172633 return IsInitialNavigation() && GetLastCommittedEntry()->IsInitialEntry() &&
Carlos Caballeroede6f8c2021-01-28 11:01:502634 !frame_tree_.has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462635}
2636
Aran Gilman37d11632019-10-08 23:07:152637SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Aaron Colwell78b4bde2021-03-16 16:16:092638 const SiteInfo& site_info) {
2639 // TODO(acolwell): Remove partition_id logic once we have successfully
2640 // migrated the implementation to be a StoragePartitionConfig. At that point
2641 // |site_info| can be replaced with a StoragePartitionConfig.
2642 const StoragePartitionId partition_id =
2643 site_info.GetStoragePartitionId(browser_context_);
2644 const StoragePartitionConfig partition_config =
Robbie McElrath7d4bd852021-07-24 04:02:192645 site_info.storage_partition_config();
[email protected]d1198fd2012-08-13 22:50:192646
[email protected]fdac6ade2013-07-20 01:06:302647 StoragePartition* partition =
Lukasz Anforowiczb9a969a2021-04-29 15:26:252648 browser_context_->GetStoragePartition(partition_config);
michaelnbacbcbd2016-02-09 00:32:032649 DOMStorageContextWrapper* context_wrapper =
2650 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2651
2652 SessionStorageNamespaceMap::const_iterator it =
2653 session_storage_namespace_map_.find(partition_id);
2654 if (it != session_storage_namespace_map_.end()) {
2655 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152656 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2657 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:472658
2659 // Verify that the config we generated now matches the one that
2660 // was generated when the namespace was added to the map.
Aaron Colwellb731a0ae2021-03-19 19:14:472661 if (partition_config != it->first.config()) {
2662 LogStoragePartitionIdCrashKeys(it->first, partition_id);
2663 }
Sharon Yangd4f48792021-06-30 17:02:532664 CHECK_EQ(partition_config, it->first.config());
Aaron Colwellb731a0ae2021-03-19 19:14:472665
michaelnbacbcbd2016-02-09 00:32:032666 return it->second.get();
2667 }
2668
2669 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102670 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2671 SessionStorageNamespaceImpl::Create(context_wrapper);
2672 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2673 session_storage_namespace.get();
2674 session_storage_namespace_map_[partition_id] =
2675 std::move(session_storage_namespace);
Aaron Colwellb731a0ae2021-03-19 19:14:472676 OnStoragePartitionIdAdded(partition_id);
[email protected]fdac6ade2013-07-20 01:06:302677
Daniel Murphy31bbb8b12018-02-07 21:44:102678 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302679}
2680
2681SessionStorageNamespace*
2682NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Robbie McElrath7d4bd852021-07-24 04:02:192683 return GetSessionStorageNamespace(SiteInfo(GetBrowserContext()));
[email protected]fdac6ade2013-07-20 01:06:302684}
2685
2686const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572687NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302688 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552689}
[email protected]d202a7c2012-01-04 07:53:472690
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572691bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562692 return needs_reload_;
2693}
[email protected]a26023822011-12-29 00:23:552694
[email protected]46bb5e9c2013-10-03 22:16:472695void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412696 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2697}
2698
2699void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472700 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412701 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542702
2703 if (last_committed_entry_index_ != -1) {
2704 entries_[last_committed_entry_index_]->SetTransitionType(
2705 ui::PAGE_TRANSITION_RELOAD);
2706 }
[email protected]46bb5e9c2013-10-03 22:16:472707}
2708
[email protected]d202a7c2012-01-04 07:53:472709void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572710 DCHECK_LT(index, GetEntryCount());
2711 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312712 DiscardNonCommittedEntries();
2713
2714 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122715 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312716 last_committed_entry_index_--;
2717}
2718
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572719NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272720 // If there is no pending_entry_, there should be no pending_entry_index_.
2721 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2722
2723 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362724 // at that index. An exception is while a reload of a post commit error page
2725 // is ongoing; in that case pending entry will point to the entry replaced
2726 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272727 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362728 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2729 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272730
[email protected]022af742011-12-28 18:37:252731 return pending_entry_;
2732}
2733
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572734int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272735 // The pending entry index must always be less than the number of entries.
arthursonzogni5c4c202d2017-04-25 23:41:272736 DCHECK_LT(pending_entry_index_, GetEntryCount());
[email protected]a26023822011-12-29 00:23:552737 return pending_entry_index_;
2738}
2739
avi25764702015-06-23 15:43:372740void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572741 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362742 bool replace,
Dave Tapuska87696ae2021-11-18 18:48:312743 bool was_post_commit_error,
Rakina Zata Amnia4e27222021-12-22 01:05:002744 bool in_fenced_frame_tree,
2745 LoadCommittedDetails* commit_details) {
Dave Tapuska87696ae2021-11-18 18:48:312746 // Fenced frame trees should always have `ui::PAGE_TRANSITION_AUTO_SUBFRAME`
2747 // set because:
2748 // 1) They don't influence the history of the outer page.
2749 // 2) They are always replace only navigation (there is always only one entry
2750 // in their history stack).
2751 // 3) Are not top level navigations and appear similar to iframes.
2752 // Navigations of the fenced frame might create a new NavigationEntry, which
2753 // will call this function. Non fenced frame navigations will never have
2754 // `ui::PAGE_TRANSITION_AUTO_SUBFRAME` because they won't call
2755 // InsertOrReplaceEntry.
2756 DCHECK_EQ(in_fenced_frame_tree,
2757 ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2758 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202759
avi5cad4912015-06-19 05:25:442760 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2761 // need to keep continuity with the pending entry, so copy the pending entry's
2762 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2763 // then the renderer navigated on its own, independent of the pending entry,
2764 // so don't copy anything.
2765 if (pending_entry_ && pending_entry_index_ == -1)
2766 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202767
Rakina Zata Amnia4e27222021-12-22 01:05:002768 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]765b35502008-08-21 00:51:202769
creisee17e932015-07-17 17:56:222770 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362771 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102772 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572773 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362774 // If the new entry is a post-commit error page, we store the current last
2775 // committed entry to the side so that we can put it back when navigating
2776 // away from the error.
2777 if (was_post_commit_error) {
2778 DCHECK(!entry_replaced_by_post_commit_error_);
2779 entry_replaced_by_post_commit_error_ =
2780 std::move(entries_[last_committed_entry_index_]);
2781 }
dcheng36b6aec92015-12-26 06:16:362782 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222783 return;
2784 }
[email protected]765b35502008-08-21 00:51:202785
creis37979a62015-08-04 19:48:182786 // We shouldn't see replace == true when there's no committed entries.
2787 DCHECK(!replace);
2788
Michael Thiessen9b14d512019-09-23 21:19:472789 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202790
Shivani Sharmad8c8d652019-02-13 17:27:572791 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202792
dcheng36b6aec92015-12-26 06:16:362793 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202794 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292795}
2796
Shivani Sharmad8c8d652019-02-13 17:27:572797void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162798 if (entries_.size() < max_entry_count())
2799 return;
2800
2801 DCHECK_EQ(max_entry_count(), entries_.size());
2802 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572803 CHECK_EQ(pending_entry_index_, -1);
2804
2805 int index = 0;
Elly Fong-Jonesccc6d1f2021-06-14 18:32:422806 // Retrieve the oldest skippable entry.
2807 for (; index < GetEntryCount(); index++) {
2808 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2809 break;
Shivani Sharmad8c8d652019-02-13 17:27:572810 }
2811
2812 // If there is no skippable entry or if it is the last committed entry then
2813 // fall back to pruning the oldest entry. It is not safe to prune the last
2814 // committed entry.
2815 if (index == GetEntryCount() || index == last_committed_entry_index_)
2816 index = 0;
2817
2818 bool should_succeed = RemoveEntryAtIndex(index);
2819 DCHECK_EQ(true, should_succeed);
2820
2821 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252822}
2823
clamy3cb9bea92018-07-10 12:42:022824void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162825 ReloadType reload_type,
Nate Chapin45f620582021-09-30 17:45:432826 int sandboxed_source_frame_tree_node_id,
2827 bool is_browser_initiated) {
Alexander Timin3a92df72019-09-20 11:59:502828 TRACE_EVENT0("navigation",
2829 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272830 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022831 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362832 if (pending_entry_index_ != -1) {
2833 // The pending entry may not be in entries_ if a post-commit error page is
2834 // showing.
2835 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2836 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2837 }
Gyuyoung Kim107c2a02021-04-13 01:49:302838 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572839 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:012840 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:002841 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:572842 int nav_entry_id = pending_entry_->GetUniqueID();
2843
[email protected]83c2e232011-10-07 21:36:462844 // If we were navigating to a slow-to-commit page, and the user performs
2845 // a session history navigation to the last committed page, RenderViewHost
2846 // will force the throbber to start, but WebKit will essentially ignore the
2847 // navigation, and won't send a message to stop the throbber. To prevent this
2848 // from happening, we drop the navigation here and stop the slow-to-commit
2849 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022850 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:082851 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:272852 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
Takashi Toyoshimabf549472021-07-01 07:47:202853 frame_tree_.StopLoading();
[email protected]6a13a6c2011-12-20 21:47:122854
[email protected]83c2e232011-10-07 21:36:462855 DiscardNonCommittedEntries();
2856 return;
2857 }
2858
creisce0ef3572017-01-26 17:53:082859 // Compare FrameNavigationEntries to see which frames in the tree need to be
2860 // navigated.
clamy3cb9bea92018-07-10 12:42:022861 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2862 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Nate Chapin45f620582021-09-30 17:45:432863 FindFramesToNavigate(root, reload_type, is_browser_initiated,
2864 &same_document_loads, &different_document_loads);
creis4e2ecb72015-06-20 00:46:302865
2866 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572867 // We were unable to match any frames to navigate. This can happen if a
2868 // history navigation targets a subframe that no longer exists
2869 // (https://crbug.com/705550). In this case, we need to update the current
2870 // history entry to the pending one but keep the main document loaded. We
2871 // also need to ensure that observers are informed about the updated
2872 // current history entry (e.g., for greying out back/forward buttons), and
2873 // that renderer processes update their history offsets. The easiest way
2874 // to do all that is to schedule a "redundant" same-document navigation in
2875 // the main frame.
2876 //
2877 // Note that we don't want to remove this history entry, as it might still
2878 // be valid later, since a frame that it's targeting may be recreated.
2879 //
2880 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
2881 // need to repeat history navigations until finding the one that works.
2882 // Consider changing this behavior to keep looking for the first valid
2883 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:022884 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422885 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572886 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:572887 ReloadType::NONE /* reload_type */,
2888 true /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432889 false /* is_history_navigation_in_new_child */,
2890 is_browser_initiated);
clamy3cb9bea92018-07-10 12:42:022891 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572892 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:022893 DiscardPendingEntry(false);
2894 return;
2895 }
Alex Moshchuk3a4e77a2020-05-29 21:32:572896 same_document_loads.push_back(std::move(navigation_request));
2897
2898 // Sanity check that we never take this branch for any kinds of reloads,
2899 // as those should've queued a different-document load in the main frame.
2900 DCHECK(!is_forced_reload);
2901 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:302902 }
2903
Dave Tapuska8bfd84c2019-03-26 20:47:162904 // If |sandboxed_source_frame_node_id| is valid, then track whether this
2905 // navigation affects any frame outside the frame's subtree.
2906 if (sandboxed_source_frame_tree_node_id !=
2907 FrameTreeNode::kFrameTreeNodeInvalidId) {
2908 bool navigates_inside_tree =
2909 DoesSandboxNavigationStayWithinSubtree(
2910 sandboxed_source_frame_tree_node_id, same_document_loads) &&
2911 DoesSandboxNavigationStayWithinSubtree(
2912 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:502913 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:162914 // the number of pages that trigger this.
2915 FrameTreeNode* sandbox_source_frame_tree_node =
2916 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
2917 if (sandbox_source_frame_tree_node) {
2918 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
2919 sandbox_source_frame_tree_node->current_frame_host(),
2920 navigates_inside_tree
2921 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
2922 : blink::mojom::WebFeature::
2923 kSandboxBackForwardAffectsFramesOutsideSubtree);
2924 }
Dave Tapuska855c1e12019-08-23 20:45:522925
2926 // If the navigation occurred outside the tree discard it because
2927 // the sandboxed frame didn't have permission to navigate outside
2928 // its tree. If it is possible that the navigation is both inside and
2929 // outside the frame tree and we discard it entirely because we don't
2930 // want to end up in a history state that didn't exist before.
Dominic Farolino057440042022-01-19 18:18:142931 if (!navigates_inside_tree) {
Dave Tapuska855c1e12019-08-23 20:45:522932 DiscardPendingEntry(false);
2933 return;
2934 }
Dave Tapuska8bfd84c2019-03-26 20:47:162935 }
2936
Carlos Caballero539a421c2020-07-06 10:25:572937 // BackForwardCache:
2938 // Navigate immediately if the document is in the BackForwardCache.
2939 if (back_forward_cache_.GetEntry(nav_entry_id)) {
2940 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
2941 DCHECK_EQ(reload_type, ReloadType::NONE);
2942 auto navigation_request = CreateNavigationRequestFromEntry(
2943 root, pending_entry_, pending_entry_->GetFrameEntry(root),
2944 ReloadType::NONE, false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432945 false /* is_history_navigation_in_new_child */, is_browser_initiated);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052946 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:572947
2948 return;
2949 }
2950
2951 // History navigation might try to reuse a specific BrowsingInstance, already
2952 // used by a page in the cache. To avoid having two different main frames that
2953 // live in the same BrowsingInstance, evict the all pages with this
2954 // BrowsingInstance from the cache.
2955 //
2956 // For example, take the following scenario:
2957 //
2958 // A1 = Some page on a.com
2959 // A2 = Some other page on a.com
2960 // B3 = An uncacheable page on b.com
2961 //
2962 // Then the following navigations occur:
2963 // A1->A2->B3->A1
2964 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
2965 // take its place) and A1 will be created in the same BrowsingInstance (and
2966 // SiteInstance), as A2.
2967 //
2968 // If we didn't do anything, both A1 and A2 would remain alive in the same
2969 // BrowsingInstance/SiteInstance, which is unsupported by
2970 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
2971 // A2 from the cache.
2972 if (pending_entry_->site_instance()) {
2973 back_forward_cache_.EvictFramesInRelatedSiteInstances(
2974 pending_entry_->site_instance());
2975 }
2976
clamy3cb9bea92018-07-10 12:42:022977 // This call does not support re-entrancy. See http://crbug.com/347742.
2978 CHECK(!in_navigate_to_pending_entry_);
2979 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:302980
arthursonzogni66f711c2019-10-08 14:40:362981 // It is not possible to delete the pending NavigationEntry while navigating
2982 // to it. Grab a reference to delay potential deletion until the end of this
2983 // function.
2984 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
2985
creis4e2ecb72015-06-20 00:46:302986 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:022987 for (auto& item : same_document_loads) {
2988 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052989 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302990 }
clamy3cb9bea92018-07-10 12:42:022991 for (auto& item : different_document_loads) {
2992 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052993 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302994 }
clamy3cb9bea92018-07-10 12:42:022995
2996 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:302997}
2998
Alex Moshchuk3a4e77a2020-05-29 21:32:572999NavigationControllerImpl::HistoryNavigationAction
3000NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:303001 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:573002 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:423003 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:583004 // Only active and prerendered documents are allowed to navigate in their
3005 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:423006 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:013007 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:583008 // - If the document is in pending deletion, the browser already committed
3009 // to destroying this RenderFrameHost. See https://crbug.com/930278.
3010 // - If the document is in back-forward cache, it's not allowed to navigate
3011 // as it should remain frozen. Ignore the request and evict the document
3012 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:423013 //
Sreeja Kamishettydb8e2892021-03-10 09:30:583014 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:423015 // which should all be inactive as well.
Fergal Daly1336ac642021-09-14 15:13:113016 if (frame->current_frame_host()->IsInactiveAndDisallowActivation(
3017 DisallowActivationReasonId::kDetermineActionForHistoryNavigation)) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:423018 return HistoryNavigationAction::kStopLooking;
Fergal Daly1336ac642021-09-14 15:13:113019 }
Sreeja Kamishetty8eacabb2021-03-09 11:45:423020 }
arthursonzogni03f76152019-02-12 10:35:203021
Alex Moshchuk3a4e77a2020-05-29 21:32:573022 // If there's no last committed entry, there is no previous history entry to
3023 // compare against, so fall back to a different-document load. Note that we
3024 // should only reach this case for the root frame and not descend further
3025 // into subframes.
3026 if (!GetLastCommittedEntry()) {
3027 DCHECK(frame->IsMainFrame());
3028 return HistoryNavigationAction::kDifferentDocument;
3029 }
3030
3031 // Reloads should result in a different-document load. Note that reloads may
3032 // also happen via the |needs_reload_| mechanism where the reload_type is
3033 // NONE, so detect this by comparing whether we're going to the same
3034 // entry that we're currently on. Similarly to above, only main frames
3035 // should reach this. Note that subframes support reloads, but that's done
3036 // via a different path that doesn't involve FindFramesToNavigate (see
3037 // RenderFrameHost::Reload()).
3038 if (reload_type != ReloadType::NONE ||
3039 pending_entry_index_ == last_committed_entry_index_) {
3040 DCHECK(frame->IsMainFrame());
3041 return HistoryNavigationAction::kDifferentDocument;
3042 }
3043
Alex Moshchuk47d1a4bd2020-06-01 22:15:343044 // If there is no new FrameNavigationEntry for the frame, ignore the
3045 // load. For example, this may happen when going back to an entry before a
3046 // frame was created. Suppose we commit a same-document navigation that also
3047 // results in adding a new subframe somewhere in the tree. If we go back,
3048 // the new subframe will be missing a FrameNavigationEntry in the previous
3049 // NavigationEntry, but we shouldn't delete or change what's loaded in
3050 // it.
3051 //
Alex Moshchuke65c39272020-06-03 17:55:373052 // Note that in this case, there is no need to keep looking for navigations
3053 // in subframes, which would be missing FrameNavigationEntries as well.
3054 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:343055 // It's important to check this before checking |old_item| below, since both
3056 // might be null, and in that case we still shouldn't change what's loaded in
3057 // this frame. Note that scheduling any loads assumes that |new_item| is
3058 // non-null. See https://crbug.com/1088354.
3059 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3060 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:373061 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:343062
Alex Moshchuk3a4e77a2020-05-29 21:32:573063 // If there is no old FrameNavigationEntry, schedule a different-document
3064 // load.
3065 //
creis225a7432016-06-03 22:56:273066 // TODO(creis): Store the last committed FrameNavigationEntry to use here,
3067 // rather than assuming the NavigationEntry has up to date info on subframes.
creis4e2ecb72015-06-20 00:46:303068 FrameNavigationEntry* old_item =
3069 GetLastCommittedEntry()->GetFrameEntry(frame);
Alex Moshchuk3a4e77a2020-05-29 21:32:573070 if (!old_item)
3071 return HistoryNavigationAction::kDifferentDocument;
3072
Alex Moshchuk3a4e77a2020-05-29 21:32:573073 // If the new item is not in the same SiteInstance, schedule a
3074 // different-document load. Newly restored items may not have a SiteInstance
3075 // yet, in which case it will be assigned on first commit.
3076 if (new_item->site_instance() &&
3077 new_item->site_instance() != old_item->site_instance())
3078 return HistoryNavigationAction::kDifferentDocument;
3079
3080 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:313081 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
3082 // that we do this even if the history navigation would not be modifying this
3083 // frame were it live.
3084 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:573085 return HistoryNavigationAction::kDifferentDocument;
3086
3087 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:313088 // Starting a navigation after a crash early-promotes the speculative
3089 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
3090 // committed yet, so we can not possibly perform a same-document history
3091 // navigation. The frame would need to be reloaded with a cross-document
3092 // navigation.
3093 if (!frame->current_frame_host()->has_committed_any_navigation())
3094 return HistoryNavigationAction::kDifferentDocument;
3095
creis54131692016-08-12 18:32:253096 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:493097 // sequence number but different item sequence number.
3098 if (new_item->document_sequence_number() ==
3099 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573100 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493101 }
avib48cb312016-05-05 21:35:003102
Alex Moshchuk3a4e77a2020-05-29 21:32:573103 // Otherwise, if both item and document sequence numbers differ, this
3104 // should be a different document load.
3105 return HistoryNavigationAction::kDifferentDocument;
3106 }
3107
3108 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373109 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573110 DCHECK_EQ(new_item->document_sequence_number(),
3111 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373112 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573113}
3114
3115void NavigationControllerImpl::FindFramesToNavigate(
3116 FrameTreeNode* frame,
3117 ReloadType reload_type,
Nate Chapin45f620582021-09-30 17:45:433118 bool is_browser_initiated,
Alex Moshchuk3a4e77a2020-05-29 21:32:573119 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3120 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3121 DCHECK(pending_entry_);
3122 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3123
3124 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3125
3126 if (action == HistoryNavigationAction::kSameDocument) {
3127 std::unique_ptr<NavigationRequest> navigation_request =
3128 CreateNavigationRequestFromEntry(
3129 frame, pending_entry_, new_item, reload_type,
3130 true /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433131 false /* is_history_navigation_in_new_child */,
3132 is_browser_initiated);
Alex Moshchuk3a4e77a2020-05-29 21:32:573133 if (navigation_request) {
3134 // Only add the request if was properly created. It's possible for the
3135 // creation to fail in certain cases, e.g. when the URL is invalid.
3136 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303137 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573138 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473139 std::unique_ptr<NavigationRequest> navigation_request =
3140 CreateNavigationRequestFromEntry(
3141 frame, pending_entry_, new_item, reload_type,
3142 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433143 false /* is_history_navigation_in_new_child */,
3144 is_browser_initiated);
Lei Zhang96031532019-10-10 19:05:473145 if (navigation_request) {
3146 // Only add the request if was properly created. It's possible for the
3147 // creation to fail in certain cases, e.g. when the URL is invalid.
3148 different_document_loads->push_back(std::move(navigation_request));
3149 }
3150 // For a different document, the subframes will be destroyed, so there's
3151 // no need to consider them.
3152 return;
Alex Moshchuke65c39272020-06-03 17:55:373153 } else if (action == HistoryNavigationAction::kStopLooking) {
3154 return;
creis4e2ecb72015-06-20 00:46:303155 }
3156
3157 for (size_t i = 0; i < frame->child_count(); i++) {
Nate Chapin45f620582021-09-30 17:45:433158 FindFramesToNavigate(frame->child_at(i), reload_type, is_browser_initiated,
3159 same_document_loads, different_document_loads);
creis4e2ecb72015-06-20 00:46:303160 }
3161}
3162
Harkiran Bolariaba823e42021-05-21 18:30:363163base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry(
clamy21718cc22018-06-13 13:34:243164 const LoadURLParams& params) {
3165 // Find the appropriate FrameTreeNode.
3166 FrameTreeNode* node = nullptr;
3167 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3168 !params.frame_name.empty()) {
3169 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003170 ? frame_tree_.FindByID(params.frame_tree_node_id)
3171 : frame_tree_.FindByName(params.frame_name);
Hayato Ito7a80db42021-07-05 06:18:543172 DCHECK(!node || node->frame_tree() == &frame_tree_);
clamy21718cc22018-06-13 13:34:243173 }
3174
3175 // If no FrameTreeNode was specified, navigate the main frame.
3176 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003177 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243178
Camille Lamy5193caa2018-10-12 11:59:423179 // Compute overrides to the LoadURLParams for |override_user_agent|,
3180 // |should_replace_current_entry| and |has_user_gesture| that will be used
3181 // both in the creation of the NavigationEntry and the NavigationRequest.
3182 // Ideally, the LoadURLParams themselves would be updated, but since they are
3183 // passed as a const reference, this is not possible.
3184 // TODO(clamy): When we only create a NavigationRequest, move this to
3185 // CreateNavigationRequestFromLoadURLParams.
3186 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3187 GetLastCommittedEntry());
3188
3189 // Don't allow an entry replacement if there is no entry to replace.
3190 // http://crbug.com/457149
Hayato Ito7a80db42021-07-05 06:18:543191 //
3192 // If there is an entry, an entry replacement must happen if the current
3193 // browsing context should maintain a trivial session history.
shivanigithubf405bf0d2021-11-05 17:58:333194 bool should_replace_current_entry =
3195 (params.should_replace_current_entry ||
3196 ShouldMaintainTrivialSessionHistory(node)) &&
3197 entries_.size();
Camille Lamy5193caa2018-10-12 11:59:423198
clamy21718cc22018-06-13 13:34:243199 // Javascript URLs should not create NavigationEntries. All other navigations
3200 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243201 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373202 std::unique_ptr<NavigationEntryImpl> entry =
3203 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3204 should_replace_current_entry,
3205 params.has_user_gesture);
clamy21718cc22018-06-13 13:34:243206 DiscardPendingEntry(false);
3207 SetPendingEntry(std::move(entry));
3208 }
3209
3210 // Renderer-debug URLs are sent to the renderer process immediately for
3211 // processing and don't need to create a NavigationRequest.
3212 // Note: this includes navigations to JavaScript URLs, which are considered
3213 // renderer-debug URLs.
3214 // Note: we intentionally leave the pending entry in place for renderer debug
3215 // URLs, unlike the cases below where we clear it if the navigation doesn't
3216 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303217 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483218 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3219 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153220 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483221 params.url, browser_context_)) {
3222 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363223 return nullptr;
Oleg Davydov2cc0167b2019-02-05 14:32:483224 }
3225
clamy21718cc22018-06-13 13:34:243226 HandleRendererDebugURL(node, params.url);
Harkiran Bolariaba823e42021-05-21 18:30:363227 return nullptr;
clamy21718cc22018-06-13 13:34:243228 }
3229
Antonio Sartori78a749f2020-11-30 12:03:393230 DCHECK(pending_entry_);
3231
clamy21718cc22018-06-13 13:34:243232 // Convert navigations to the current URL to a reload.
3233 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3234 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3235 // DevTools sometimes issues navigations to main frames that they do not
3236 // expect to see treated as reload, and it only works because they pass a
3237 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3238 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543239 ReloadType reload_type = params.reload_type;
3240 if (reload_type == ReloadType::NONE &&
3241 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043242 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243243 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243244 params.load_type ==
3245 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
Hayato Ito7a80db42021-07-05 06:18:543246 should_replace_current_entry, GetLastCommittedEntry())) {
clamy21718cc22018-06-13 13:34:243247 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473248 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393249
3250 // If this is a reload of an existing FrameNavigationEntry and we had a
3251 // policy container for it, then we should copy it into the pending entry,
3252 // so that it is copied to the navigation request in
3253 // CreateNavigationRequestFromLoadParams later.
3254 if (GetLastCommittedEntry()) {
3255 FrameNavigationEntry* previous_frame_entry =
3256 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193257 if (previous_frame_entry &&
3258 previous_frame_entry->policy_container_policies()) {
3259 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163260 previous_frame_entry->policy_container_policies()->Clone());
Antonio Sartori78a749f2020-11-30 12:03:393261 }
3262 }
3263 }
3264
3265 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
Rakina Zata Amniafd3c6582021-11-30 06:19:173266 // document, then we should copy the document polices from RenderFrameHost's
3267 // PolicyContainerHost. The NavigationRequest will create a new
3268 // PolicyContainerHost with the document policies from the |pending_entry_|,
3269 // and that PolicyContainerHost will be put in the final RenderFrameHost for
3270 // the navigation. This way, we ensure that we keep enforcing the right
3271 // policies on the initial empty document after the reload.
3272 if (GetLastCommittedEntry()->IsInitialEntry() && params.url.IsAboutBlank()) {
Antonio Sartori78a749f2020-11-30 12:03:393273 if (node->current_frame_host() &&
3274 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193275 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163276 node->current_frame_host()
3277 ->policy_container_host()
3278 ->policies()
3279 .Clone());
Antonio Sartori78a749f2020-11-30 12:03:393280 }
clamy21718cc22018-06-13 13:34:243281 }
3282
3283 // navigation_ui_data should only be present for main frame navigations.
3284 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
3285
Camille Lamy5193caa2018-10-12 11:59:423286 std::unique_ptr<NavigationRequest> request =
3287 CreateNavigationRequestFromLoadParams(
3288 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143289 params.has_user_gesture, network::mojom::SourceLocation::New(),
Antonio Sartori6984c742021-08-26 08:03:413290 reload_type, pending_entry_, pending_entry_->GetFrameEntry(node));
clamy21718cc22018-06-13 13:34:243291
3292 // If the navigation couldn't start, return immediately and discard the
3293 // pending NavigationEntry.
3294 if (!request) {
3295 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363296 return nullptr;
clamy21718cc22018-06-13 13:34:243297 }
3298
Camille Lamy5193caa2018-10-12 11:59:423299#if DCHECK_IS_ON()
3300 // Safety check that NavigationRequest and NavigationEntry match.
3301 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3302#endif
3303
clamy21718cc22018-06-13 13:34:243304 // This call does not support re-entrancy. See http://crbug.com/347742.
3305 CHECK(!in_navigate_to_pending_entry_);
3306 in_navigate_to_pending_entry_ = true;
3307
arthursonzogni66f711c2019-10-08 14:40:363308 // It is not possible to delete the pending NavigationEntry while navigating
3309 // to it. Grab a reference to delay potential deletion until the end of this
3310 // function.
3311 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3312
Harkiran Bolariaba823e42021-05-21 18:30:363313 base::WeakPtr<NavigationHandle> created_navigation_handle(
3314 request->GetWeakPtr());
Lukasz Anforowicz9ee83c272020-12-01 20:14:053315 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243316
3317 in_navigate_to_pending_entry_ = false;
Harkiran Bolariaba823e42021-05-21 18:30:363318 return created_navigation_handle;
clamy21718cc22018-06-13 13:34:243319}
3320
clamyea99ea12018-05-28 13:54:233321void NavigationControllerImpl::HandleRendererDebugURL(
3322 FrameTreeNode* frame_tree_node,
3323 const GURL& url) {
3324 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243325 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3326 // the renderer process is not live, unless it is the initial navigation
3327 // of the tab.
clamyea99ea12018-05-28 13:54:233328 if (!IsInitialNavigation()) {
3329 DiscardNonCommittedEntries();
3330 return;
3331 }
Fergal Dalyecd3b0202020-06-25 01:57:373332 // The current frame is always a main frame. If IsInitialNavigation() is
3333 // true then there have been no navigations and any frames of this tab must
3334 // be in the same renderer process. If that has crashed then the only frame
3335 // that can be revived is the main frame.
3336 frame_tree_node->render_manager()
3337 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233338 }
3339 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3340}
3341
clamy21718cc22018-06-13 13:34:243342std::unique_ptr<NavigationEntryImpl>
3343NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3344 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423345 const LoadURLParams& params,
3346 bool override_user_agent,
3347 bool should_replace_current_entry,
3348 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393349 // Browser initiated navigations might not have a blob_url_loader_factory set
3350 // in params even if the navigation is to a blob URL. If that happens, lookup
3351 // the correct url loader factory to use here.
3352 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483353 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473354 // Resolve the blob URL in the storage partition associated with the target
3355 // frame. This is the storage partition the URL will be loaded in, and only
3356 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393357 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473358 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393359 }
3360
clamy21718cc22018-06-13 13:34:243361 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443362 // extra_headers in params are \n separated; navigation entries want \r\n.
3363 std::string extra_headers_crlf;
3364 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243365
3366 // For subframes, create a pending entry with a corresponding frame entry.
3367 if (!node->IsMainFrame()) {
Rakina Zata Amniafd3c6582021-11-30 06:19:173368 CHECK(GetLastCommittedEntry());
3369 entry = GetLastCommittedEntry()->Clone();
clamy21718cc22018-06-13 13:34:243370 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:083371 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
clamy21718cc22018-06-13 13:34:243372 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Anton Bikineevf62d1bf2021-05-15 17:56:073373 params.url, absl::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203374 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393375 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:483376 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori78a749f2020-11-30 12:03:393377 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163378 // a conversion of a new navigation into a reload, we will set the right
3379 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193380 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243381 } else {
3382 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243383 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063384 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103385 params.source_site_instance.get(), params.transition_type,
3386 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Hayato Ito303654c2021-06-30 09:07:543387 blob_url_loader_factory));
clamy21718cc22018-06-13 13:34:243388 entry->set_source_site_instance(
3389 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3390 entry->SetRedirectChain(params.redirect_chain);
3391 }
3392
3393 // Set the FTN ID (only used in non-site-per-process, for tests).
3394 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243395 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423396 entry->SetIsOverridingUserAgent(override_user_agent);
3397 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543398 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243399
clamy21718cc22018-06-13 13:34:243400 switch (params.load_type) {
3401 case LOAD_TYPE_DEFAULT:
3402 break;
3403 case LOAD_TYPE_HTTP_POST:
3404 entry->SetHasPostData(true);
3405 entry->SetPostData(params.post_data);
3406 break;
3407 case LOAD_TYPE_DATA:
3408 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3409 entry->SetVirtualURL(params.virtual_url_for_data_url);
Xiaohan Wang7f8052e02022-01-14 18:44:283410#if BUILDFLAG(IS_ANDROID)
clamy21718cc22018-06-13 13:34:243411 entry->SetDataURLAsString(params.data_url_as_string);
3412#endif
3413 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3414 break;
clamy21718cc22018-06-13 13:34:243415 }
3416
3417 // TODO(clamy): NavigationEntry is meant for information that will be kept
3418 // after the navigation ended and therefore is not appropriate for
3419 // started_from_context_menu. Move started_from_context_menu to
3420 // NavigationUIData.
3421 entry->set_started_from_context_menu(params.started_from_context_menu);
3422
3423 return entry;
3424}
3425
clamyea99ea12018-05-28 13:54:233426std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423427NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3428 FrameTreeNode* node,
3429 const LoadURLParams& params,
3430 bool override_user_agent,
3431 bool should_replace_current_entry,
3432 bool has_user_gesture,
Antonio Sartori2f763d9d2021-04-21 10:04:143433 network::mojom::SourceLocationPtr source_location,
Camille Lamy5193caa2018-10-12 11:59:423434 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573435 NavigationEntryImpl* entry,
Scott Violetcf6ea7e2021-06-09 21:09:213436 FrameNavigationEntry* frame_entry) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573437 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283438 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533439 // All renderer-initiated navigations must have an initiator_origin.
3440 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513441
Camille Lamy5193caa2018-10-12 11:59:423442 GURL url_to_load;
3443 GURL virtual_url;
Anton Bikineevf62d1bf2021-05-15 17:56:073444 absl::optional<url::Origin> origin_to_commit =
3445 frame_entry ? frame_entry->committed_origin() : absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:323446
Camille Lamy2baa8022018-10-19 16:43:173447 // For main frames, rewrite the URL if necessary and compute the virtual URL
3448 // that should be shown in the address bar.
3449 if (node->IsMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423450 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173451 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423452 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423453
Camille Lamy2baa8022018-10-19 16:43:173454 // For DATA loads, override the virtual URL.
3455 if (params.load_type == LOAD_TYPE_DATA)
3456 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423457
Camille Lamy2baa8022018-10-19 16:43:173458 if (virtual_url.is_empty())
3459 virtual_url = url_to_load;
3460
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573461 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283462
Aran Gilman249eb122019-12-02 23:32:463463 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3464 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3465 // return different results, leading to a different URL in the
3466 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3467 // pending NavigationEntry, as we'll only make one call to
3468 // RewriteUrlForNavigation.
3469 VLOG_IF(1, (url_to_load != frame_entry->url()))
3470 << "NavigationRequest and FrameEntry have different URLs: "
3471 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283472
Camille Lamy2baa8022018-10-19 16:43:173473 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423474 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173475 // NavigationRequest.
3476 } else {
3477 url_to_load = params.url;
3478 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243479 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173480 }
Camille Lamy5193caa2018-10-12 11:59:423481
Ehsan Karamad44fc72112019-02-26 18:15:473482 if (node->render_manager()->is_attaching_inner_delegate()) {
3483 // Avoid starting any new navigations since this node is now preparing for
3484 // attaching an inner delegate.
3485 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203486 }
Camille Lamy5193caa2018-10-12 11:59:423487
Camille Lamy5193caa2018-10-12 11:59:423488 if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load))
3489 return nullptr;
3490
Kunihiko Sakamoto346a74e2021-03-10 08:57:483491 if (!DoesURLMatchOriginForNavigation(
3492 url_to_load, origin_to_commit,
3493 frame_entry->subresource_web_bundle_navigation_info())) {
Nasko Oskov03912102019-01-11 00:21:323494 DCHECK(false) << " url:" << url_to_load
3495 << " origin:" << origin_to_commit.value();
3496 return nullptr;
3497 }
3498
Camille Lamy5193caa2018-10-12 11:59:423499 // This will be used to set the Navigation Timing API navigationStart
3500 // parameter for browser navigations in new tabs (intents, tabs opened through
3501 // "Open link in new tab"). If the navigation must wait on the current
3502 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3503 // will be updated when the BeforeUnload ack is received.
3504 base::TimeTicks navigation_start = base::TimeTicks::Now();
3505
danakjd83d706d2020-11-25 22:11:123506 // Look for a pending commit that is to another document in this
3507 // FrameTreeNode. If one exists, then the last committed URL will not be the
3508 // current URL by the time this navigation commits.
3509 bool has_pending_cross_document_commit =
3510 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
Miyoung Shina2dd6a42021-10-07 12:19:213511 bool is_currently_error_page = node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:123512
Minggang Wangb9f3fa92021-07-01 15:30:313513 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjb952ef12021-01-14 19:58:493514 /*old_url=*/node->current_url(),
3515 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3516 has_pending_cross_document_commit, is_currently_error_page,
3517 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423518
3519 // Create the NavigationParams based on |params|.
3520
Hiroki Nakagawa4ed61282021-06-18 05:37:233521 bool is_view_source_mode = entry->IsViewSourceMode();
3522 DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme));
Charlie Harrison8c113a32019-01-07 16:08:293523
Antonio Sartori6984c742021-08-26 08:03:413524 blink::NavigationDownloadPolicy download_policy = params.download_policy;
Hiroki Nakagawa4ed61282021-06-18 05:37:233525 // Update |download_policy| if the virtual URL is view-source.
Charlie Harrison8c113a32019-01-07 16:08:293526 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253527 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293528
Minggang Wangb9f3fa92021-07-01 15:30:313529 blink::mojom::CommonNavigationParamsPtr common_params =
3530 blink::mojom::CommonNavigationParams::New(
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513531 url_to_load, params.initiator_origin,
3532 blink::mojom::Referrer::New(params.referrer.url,
3533 params.referrer.policy),
Scott Violetcf6ea7e2021-06-09 21:09:213534 params.transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513535 should_replace_current_entry, params.base_url_for_data_url,
Tarun Bansalbcd62c82022-01-18 17:27:383536 navigation_start,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513537 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
Antonio Sartori2f763d9d2021-04-21 10:04:143538 params.post_data, std::move(source_location),
arthursonzogniaf7c62c52020-02-12 10:49:413539 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273540 false /* has_text_fragment_token */,
3541 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3542 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513543 false /* is_history_navigation_in_new_child_frame */,
Nan Lind91c8152021-10-21 16:22:373544 params.input_start, network::mojom::RequestDestination::kEmpty);
Camille Lamy5193caa2018-10-12 11:59:423545
Minggang Wangb9f3fa92021-07-01 15:30:313546 blink::mojom::CommitNavigationParamsPtr commit_params =
3547 blink::mojom::CommitNavigationParams::New(
Antonio Sartori3e8de6d2021-07-26 10:28:413548 frame_entry->committed_origin(),
3549 // The correct storage key will be computed before committing the
3550 // navigation.
3551 blink::StorageKey(), network::mojom::WebSandboxFlags(),
arthursonzognid5a8d0b2021-03-11 17:36:433552 override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323553 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143554 std::vector<net::RedirectInfo>(),
3555 std::string() /* post_content_type */, common_params->url,
3556 common_params->method, params.can_load_local_resources,
Minggang Wangb9f3fa92021-07-01 15:30:313557 frame_entry->page_state().ToEncodedData(), entry->GetUniqueID(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143558 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3559 -1 /* pending_history_list_offset */,
3560 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3561 params.should_clear_history_list ? 0 : GetEntryCount(),
3562 false /* was_discarded */, is_view_source_mode,
Minggang Wangb9f3fa92021-07-01 15:30:313563 params.should_clear_history_list,
3564 blink::mojom::NavigationTiming::New(),
Minggang Wangf59db47b2021-06-16 01:56:223565 blink::mojom::WasActivatedOption::kUnknown,
Lucas Furukawa Gadania9c45682019-07-31 22:05:143566 base::UnguessableToken::Create() /* navigation_token */,
Minggang Wang7ee0c742021-06-16 16:16:513567 std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(),
Xiaohan Wang7f8052e02022-01-14 18:44:283568#if BUILDFLAG(IS_ANDROID)
Lucas Furukawa Gadania9c45682019-07-31 22:05:143569 std::string(), /* data_url_as_string */
3570#endif
arthursonzogni14379782020-05-15 09:09:273571 !params.is_renderer_initiated, /* is_browser_initiated */
Tsuyoshi Horoe86d7702019-11-29 01:52:473572 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533573 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173574 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073575 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163576 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333577 false /* origin_agent_cluster */,
Maks Orlovichc66745a2020-06-30 17:40:023578 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213579 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Nate Chapind1fe3612021-04-16 20:45:573580 false /* is_cross_browsing_instance */, nullptr /* old_page_info */,
3581 -1 /* http_response_code */,
Minggang Wangb9f3fa92021-07-01 15:30:313582 std::vector<blink::mojom::
3583 AppHistoryEntryPtr>() /* app_history_back_entries */,
Nate Chapind1fe3612021-04-16 20:45:573584 std::vector<
Minggang Wangb9f3fa92021-07-01 15:30:313585 blink::mojom::
3586 AppHistoryEntryPtr>() /* app_history_forward_entries */,
Matt Menke4e209082021-11-09 04:59:313587 std::vector<GURL>() /* early_hints_preloaded_resources */,
Clark DuVall8ee487a22021-11-10 02:25:583588 absl::nullopt /* ad_auction_components */,
3589 // This timestamp will be populated when the commit IPC is sent.
Yifan Luoe1a2e05d2022-01-12 16:47:593590 base::TimeTicks() /* commit_sent */, false /* anonymous */);
Xiaohan Wang7f8052e02022-01-14 18:44:283591#if BUILDFLAG(IS_ANDROID)
Camille Lamy5193caa2018-10-12 11:59:423592 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143593 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423594 }
3595#endif
3596
Lucas Furukawa Gadania9c45682019-07-31 22:05:143597 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423598
3599 // A form submission may happen here if the navigation is a renderer-initiated
3600 // form submission that took the OpenURL path.
3601 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3602
3603 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3604 std::string extra_headers_crlf;
3605 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093606
3607 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143608 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083609 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453610 params.initiator_frame_token.has_value()
3611 ? &(params.initiator_frame_token.value())
3612 : nullptr,
3613 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3614 request_body,
John Delaney50425f82020-04-07 16:26:213615 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473616 params.impression, params.is_pdf);
Yao Xiaodc5ed102019-06-04 19:19:093617 navigation_request->set_from_download_cross_origin_redirect(
3618 params.from_download_cross_origin_redirect);
3619 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423620}
3621
3622std::unique_ptr<NavigationRequest>
3623NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233624 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573625 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233626 FrameNavigationEntry* frame_entry,
3627 ReloadType reload_type,
3628 bool is_same_document_history_load,
Nate Chapin45f620582021-09-30 17:45:433629 bool is_history_navigation_in_new_child_frame,
3630 bool is_browser_initiated) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343631 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233632 GURL dest_url = frame_entry->url();
Rakina Zata Amnif297a802022-01-18 03:53:433633 // We should never navigate to an existing initial NavigationEntry that is the
3634 // initial NavigationEntry for the initial empty document that hasn't been
3635 // overridden by the synchronous about:blank commit, to preserve previous
3636 // behavior where trying to reload when the main frame is on the initial empty
3637 // document won't result in a navigation.
3638 // See also https://crbug.com/1277414.
3639 DCHECK(!entry->IsInitialEntryNotForSynchronousAboutBlank());
Anton Bikineevf62d1bf2021-05-15 17:56:073640 absl::optional<url::Origin> origin_to_commit =
Nasko Oskov03912102019-01-11 00:21:323641 frame_entry->committed_origin();
3642
clamyea99ea12018-05-28 13:54:233643 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013644 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573645 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233646 // We may have been redirected when navigating to the current URL.
3647 // Use the URL the user originally intended to visit as signaled by the
3648 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013649 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573650 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233651 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323652 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233653 }
3654
Ehsan Karamad44fc72112019-02-26 18:15:473655 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3656 // Avoid starting any new navigations since this node is now preparing for
3657 // attaching an inner delegate.
3658 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203659 }
3660
Camille Lamy5193caa2018-10-12 11:59:423661 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573662 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233663 return nullptr;
3664 }
3665
Kunihiko Sakamoto346a74e2021-03-10 08:57:483666 if (!DoesURLMatchOriginForNavigation(
3667 dest_url, origin_to_commit,
3668 frame_entry->subresource_web_bundle_navigation_info())) {
3669 DCHECK(false) << " url:" << dest_url
3670 << " origin:" << origin_to_commit.value();
Nasko Oskov03912102019-01-11 00:21:323671 return nullptr;
3672 }
3673
clamyea99ea12018-05-28 13:54:233674 // This will be used to set the Navigation Timing API navigationStart
3675 // parameter for browser navigations in new tabs (intents, tabs opened through
3676 // "Open link in new tab"). If the navigation must wait on the current
3677 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3678 // will be updated when the BeforeUnload ack is received.
3679 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233680
danakjd83d706d2020-11-25 22:11:123681 // Look for a pending commit that is to another document in this
3682 // FrameTreeNode. If one exists, then the last committed URL will not be the
3683 // current URL by the time this navigation commits.
3684 bool has_pending_cross_document_commit =
3685 frame_tree_node->render_manager()
3686 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493687 bool is_currently_error_page =
Miyoung Shina2dd6a42021-10-07 12:19:213688 frame_tree_node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:123689
Minggang Wangb9f3fa92021-07-01 15:30:313690 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123691 /*old_url=*/frame_tree_node->current_url(),
3692 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493693 has_pending_cross_document_commit, is_currently_error_page,
3694 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423695
3696 // A form submission may happen here if the navigation is a
3697 // back/forward/reload navigation that does a form resubmission.
3698 scoped_refptr<network::ResourceRequestBody> request_body;
3699 std::string post_content_type;
3700 if (frame_entry->method() == "POST") {
3701 request_body = frame_entry->GetPostData(&post_content_type);
3702 // Might have a LF at end.
Peter Kastingb53b81912021-04-28 19:23:303703 post_content_type = std::string(
3704 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL));
Camille Lamy5193caa2018-10-12 11:59:423705 }
3706
3707 // Create the NavigationParams based on |entry| and |frame_entry|.
Minggang Wangb9f3fa92021-07-01 15:30:313708 blink::mojom::CommonNavigationParamsPtr common_params =
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513709 entry->ConstructCommonNavigationParams(
3710 *frame_entry, request_body, dest_url,
3711 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
Tarun Bansalbcd62c82022-01-18 17:27:383712 navigation_type, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533713 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513714 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553715 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423716
3717 // TODO(clamy): |intended_as_new_entry| below should always be false once
3718 // Reload no longer leads to this being called for a pending NavigationEntry
3719 // of index -1.
Minggang Wangb9f3fa92021-07-01 15:30:313720 blink::mojom::CommitNavigationParamsPtr commit_params =
Lucas Furukawa Gadania9c45682019-07-31 22:05:143721 entry->ConstructCommitNavigationParams(
3722 *frame_entry, common_params->url, origin_to_commit,
3723 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3724 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533725 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3726 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143727 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423728
clamyea99ea12018-05-28 13:54:233729 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143730 frame_tree_node, std::move(common_params), std::move(commit_params),
Nate Chapin45f620582021-09-30 17:45:433731 is_browser_initiated, false /* was_opener_suppressed */,
Takashi Toyoshimae87b7be2021-01-22 11:51:083732 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453733 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3734 entry->extra_headers(), frame_entry, entry, request_body,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473735 nullptr /* navigation_ui_data */, absl::nullopt /* impression */,
3736 false /* is_pdf */);
clamyea99ea12018-05-28 13:54:233737}
3738
[email protected]d202a7c2012-01-04 07:53:473739void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213740 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273741 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403742
Takashi Toyoshimaea534ef22021-07-21 03:27:593743 // We need to notify the ssl_manager_ before the WebContents so the
[email protected]df1af242009-05-01 00:11:403744 // location bar will have up-to-date information about the security style
3745 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133746 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403747
Rakina Zata Amnia4e27222021-12-22 01:05:003748 delegate_->NotifyNavigationStateChanged(
Rakina Zata Amniddf10502022-01-15 02:56:553749 (details && details->should_stay_as_initial_entry)
3750 ? INVALIDATE_TYPE_ALL_BUT_KEEPS_INITIAL_NAVIGATION_ENTRY_STATUS
3751 : INVALIDATE_TYPE_ALL_AND_REMOVES_INITIAL_NAVIGATION_ENTRY_STATUS);
[email protected]ec6c05f2013-10-23 18:41:573752 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463753
[email protected]b0f724c2013-09-05 04:21:133754 // TODO(avi): Remove. http://crbug.com/170921
3755 NotificationDetails notification_details =
3756 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153757 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3758 Source<NavigationController>(this),
3759 notification_details);
initial.commit09911bf2008-07-26 23:55:293760}
3761
initial.commit09911bf2008-07-26 23:55:293762// static
[email protected]d202a7c2012-01-04 07:53:473763size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233764 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153765 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:213766 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233767}
3768
[email protected]d202a7c2012-01-04 07:53:473769void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223770 if (is_active && needs_reload_)
3771 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293772}
3773
[email protected]d202a7c2012-01-04 07:53:473774void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293775 if (!needs_reload_)
3776 return;
3777
Bo Liucdfa4b12018-11-06 00:21:443778 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3779 needs_reload_type_);
3780
initial.commit09911bf2008-07-26 23:55:293781 // Calling Reload() results in ignoring state, and not loading.
3782 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3783 // cached state.
avicc872d7242015-08-19 21:26:343784 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163785 NavigateToExistingPendingEntry(ReloadType::NONE,
Nate Chapin45f620582021-09-30 17:45:433786 FrameTreeNode::kFrameTreeNodeInvalidId,
3787 true /* is_browser_initiated */);
Rakina Zata Amnif297a802022-01-18 03:53:433788 } else if (!GetLastCommittedEntry()
3789 ->IsInitialEntryNotForSynchronousAboutBlank()) {
arthursonzogni5c4c202d2017-04-25 23:41:273790 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343791 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163792 NavigateToExistingPendingEntry(ReloadType::NONE,
Nate Chapin45f620582021-09-30 17:45:433793 FrameTreeNode::kFrameTreeNodeInvalidId,
3794 true /* is_browser_initiated */);
avicc872d7242015-08-19 21:26:343795 } else {
Rakina Zata Amnif297a802022-01-18 03:53:433796 // We should never navigate to an existing initial NavigationEntry that is
3797 // the initial NavigationEntry for the initial empty document that hasn't
3798 // been overridden by the synchronous about:blank commit, to preserve
3799 // legacy behavior where trying to reload when the main frame is on the
3800 // initial empty document won't result in a navigation. See also
3801 // https://crbug.com/1277414. If there is something to reload, the
3802 // successful reload will clear the |needs_reload_| flag. Otherwise, just do
3803 // it here.
avicc872d7242015-08-19 21:26:343804 needs_reload_ = false;
3805 }
initial.commit09911bf2008-07-26 23:55:293806}
3807
Kevin McNeeccca6172021-10-19 17:11:143808base::WeakPtr<NavigationHandle>
3809NavigationControllerImpl::LoadPostCommitErrorPage(
Carlos IL42b416592019-10-07 23:10:363810 RenderFrameHost* render_frame_host,
3811 const GURL& url,
3812 const std::string& error_page_html,
3813 net::Error error) {
Rakina Zata Amni919b7922020-12-11 09:03:133814 RenderFrameHostImpl* rfhi =
3815 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:583816
3817 // Only active documents can load post-commit error pages:
3818 // - If the document is in pending deletion, the browser already committed to
3819 // destroying this RenderFrameHost so ignore loading the error page.
3820 // - If the document is in back-forward cache, it's not allowed to navigate as
3821 // it should remain frozen. Ignore the request and evict the document from
3822 // back-forward cache.
3823 // - If the document is prerendering, it can navigate but when loading error
3824 // pages, cancel prerendering.
Fergal Daly1336ac642021-09-14 15:13:113825 if (rfhi->IsInactiveAndDisallowActivation(
3826 DisallowActivationReasonId::kLoadPostCommitErrorPage)) {
Kevin McNeeccca6172021-10-19 17:11:143827 return nullptr;
Fergal Daly1336ac642021-09-14 15:13:113828 }
Sreeja Kamishettydb8e2892021-03-10 09:30:583829
Rakina Zata Amni919b7922020-12-11 09:03:133830 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:413831
Minggang Wangb9f3fa92021-07-01 15:30:313832 blink::mojom::CommonNavigationParamsPtr common_params =
Minggang Wanga13c796e2021-07-02 05:54:433833 blink::CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:593834 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
3835 // the frame actually committed (e.g. iframe with "src" set to a
3836 // slow-responding URL). We should rewrite the URL to about:blank in this
3837 // case, as the renderer will only think a page is an error page if it has a
3838 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:133839 common_params->url = url.is_empty() ? GURL("about:blank") : url;
Minggang Wangb9f3fa92021-07-01 15:30:313840 blink::mojom::CommitNavigationParamsPtr commit_params =
Minggang Wanga13c796e2021-07-02 05:54:433841 blink::CreateCommitNavigationParams();
Antonio Sartori58591c892021-04-21 06:54:333842 commit_params->original_url = common_params->url;
John Delaney131ad362019-08-08 21:57:413843
arthursonzogni70ac7302020-05-28 08:49:053844 // Error pages have a fully permissive FramePolicy.
3845 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
3846 // error pages.
3847 commit_params->frame_policy = blink::FramePolicy();
3848
John Delaney131ad362019-08-08 21:57:413849 std::unique_ptr<NavigationRequest> navigation_request =
3850 NavigationRequest::CreateBrowserInitiated(
3851 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083852 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:193853 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453854 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:063855 "" /* extra_headers */, nullptr /* frame_entry */,
3856 nullptr /* entry */, nullptr /* post_body */,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473857 nullptr /* navigation_ui_data */, absl::nullopt /* impression */,
3858 false /* is_pdf */);
Carlos IL42b416592019-10-07 23:10:363859 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:413860 navigation_request->set_net_error(error);
3861 node->CreatedNavigationRequest(std::move(navigation_request));
3862 DCHECK(node->navigation_request());
Kevin McNeeccca6172021-10-19 17:11:143863
3864 // Calling BeginNavigation may destroy the NavigationRequest.
3865 base::WeakPtr<NavigationRequest> created_navigation_request(
3866 node->navigation_request()->GetWeakPtr());
John Delaney131ad362019-08-08 21:57:413867 node->navigation_request()->BeginNavigation();
Kevin McNeeccca6172021-10-19 17:11:143868 return created_navigation_request;
John Delaney131ad362019-08-08 21:57:413869}
3870
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573871void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213872 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093873 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:153874 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143875 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293876}
3877
[email protected]d202a7c2012-01-04 07:53:473878void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363879 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553880 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363881 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293882
initial.commit09911bf2008-07-26 23:55:293883 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293884}
[email protected]765b35502008-08-21 00:51:203885
arthursonzogni69a6a1b2019-09-17 09:23:003886void NavigationControllerImpl::DiscardNonCommittedEntries() {
Rakina Zata Amnia4e27222021-12-22 01:05:003887 DiscardNonCommittedEntriesWithCommitDetails(nullptr /* commit_details */);
3888}
3889
3890void NavigationControllerImpl::DiscardNonCommittedEntriesWithCommitDetails(
3891 LoadCommittedDetails* commit_details) {
Michael Thiessen9b14d512019-09-23 21:19:473892 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:103893 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
3894 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:293895 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:473896 return;
Michael Thiessenc5676d22019-09-25 22:32:103897 }
avi45a72532015-04-07 21:01:453898 DiscardPendingEntry(false);
Rakina Zata Amniddf10502022-01-15 02:56:553899 if (delegate_) {
Rakina Zata Amnia4e27222021-12-22 01:05:003900 delegate_->NotifyNavigationStateChanged(
Rakina Zata Amniddf10502022-01-15 02:56:553901 (commit_details && commit_details->should_stay_as_initial_entry)
3902 ? INVALIDATE_TYPE_ALL_BUT_KEEPS_INITIAL_NAVIGATION_ENTRY_STATUS
3903 : INVALIDATE_TYPE_ALL_AND_REMOVES_INITIAL_NAVIGATION_ENTRY_STATUS);
3904 }
[email protected]b12eb222013-09-10 00:11:483905}
3906
avi7c6f35e2015-05-08 17:52:383907int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3908 int nav_entry_id) const {
3909 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3910 if (entries_[i]->GetUniqueID() == nav_entry_id)
3911 return i;
3912 }
3913 return -1;
3914}
3915
[email protected]d202a7c2012-01-04 07:53:473916void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573917 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253918 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573919 DCHECK_LE(max_index, source->GetEntryCount());
Nate Chapin214a86a2021-06-21 20:35:573920 std::unique_ptr<NavigationEntryRestoreContextImpl> context =
3921 std::make_unique<NavigationEntryRestoreContextImpl>();
[email protected]e1cd5452010-08-26 18:03:253922 for (int i = 0; i < max_index; i++) {
Nate Chapin9f169072021-06-09 19:32:373923 // Normally, cloning a NavigationEntryImpl results in sharing
3924 // FrameNavigationEntries between the original and the clone. However, when
3925 // cloning from a different NavigationControllerImpl, we want to fork the
3926 // FrameNavigationEntries.
Nate Chapin9f169072021-06-09 19:32:373927 entries_.insert(entries_.begin() + i,
Nate Chapin214a86a2021-06-21 20:35:573928 source->entries_[i]->CloneWithoutSharing(context.get()));
[email protected]e1cd5452010-08-26 18:03:253929 }
Rakina Zata Amniafd3c6582021-11-30 06:19:173930 DCHECK_GE(entries_.size(), 1u);
arthursonzogni5c4c202d2017-04-25 23:41:273931 DCHECK(pending_entry_index_ == -1 ||
3932 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:253933}
[email protected]c5b88d82012-10-06 17:03:333934
3935void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:183936 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:333937 get_timestamp_callback_ = get_timestamp_callback;
3938}
[email protected]8ff00d72012-10-23 19:12:213939
Shivani Sharmaffb32b82019-04-09 16:58:473940// History manipulation intervention:
3941void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:473942 bool replace_entry,
3943 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:403944 bool is_renderer_initiated,
3945 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:453946 // Note that for a subframe, previous_document_was_activated is true if the
3947 // gesture happened in any subframe (propagated to main frame) or in the main
3948 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:473949 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:273950 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:473951 return;
3952 }
3953 if (last_committed_entry_index_ == -1)
3954 return;
3955
Shivani Sharmac4cc8922019-04-18 03:11:173956 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473957
Alexander Timine3ec4192020-04-20 16:39:403958 // Log UKM with the URL we are navigating away from.
3959 ukm::builders::HistoryManipulationIntervention(
3960 previous_page_load_ukm_source_id)
3961 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:473962}
3963
Shivani Sharmac4cc8922019-04-18 03:11:173964void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
3965 int reference_index,
3966 bool skippable) {
3967 auto* reference_entry = GetEntryAtIndex(reference_index);
3968 reference_entry->set_should_skip_on_back_forward_ui(skippable);
3969
3970 int64_t document_sequence_number =
3971 reference_entry->root_node()->frame_entry->document_sequence_number();
3972 for (int index = 0; index < GetEntryCount(); index++) {
3973 auto* entry = GetEntryAtIndex(index);
3974 if (entry->root_node()->frame_entry->document_sequence_number() ==
3975 document_sequence_number) {
3976 entry->set_should_skip_on_back_forward_ui(skippable);
3977 }
3978 }
3979}
3980
arthursonzogni66f711c2019-10-08 14:40:363981std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
3982NavigationControllerImpl::ReferencePendingEntry() {
3983 DCHECK(pending_entry_);
3984 auto pending_entry_ref =
3985 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
3986 pending_entry_refs_.insert(pending_entry_ref.get());
3987 return pending_entry_ref;
3988}
3989
3990void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
3991 // Ignore refs that don't correspond to the current pending entry.
3992 auto it = pending_entry_refs_.find(ref);
3993 if (it == pending_entry_refs_.end())
3994 return;
3995 pending_entry_refs_.erase(it);
3996
3997 if (!pending_entry_refs_.empty())
3998 return;
3999
4000 // The pending entry may be deleted before the last PendingEntryRef.
4001 if (!pending_entry_)
4002 return;
4003
4004 // We usually clear the pending entry when the matching NavigationRequest
4005 // fails, so that an arbitrary URL isn't left visible above a committed page.
4006 //
4007 // However, we do preserve the pending entry in some cases, such as on the
4008 // initial navigation of an unmodified blank tab. We also allow the delegate
4009 // to say when it's safe to leave aborted URLs in the omnibox, to let the
4010 // user edit the URL and try again. This may be useful in cases that the
4011 // committed page cannot be attacker-controlled. In these cases, we still
4012 // allow the view to clear the pending entry and typed URL if the user
4013 // requests (e.g., hitting Escape with focus in the address bar).
4014 //
4015 // Do not leave the pending entry visible if it has an invalid URL, since this
4016 // might be formatted in an unexpected or unsafe way.
4017 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:364018 bool should_preserve_entry =
4019 (pending_entry_ == GetVisibleEntry()) &&
4020 pending_entry_->GetURL().is_valid() &&
4021 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
4022 if (should_preserve_entry)
4023 return;
4024
4025 DiscardPendingEntry(true);
4026 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
4027}
4028
Titouan Rigoudy6ec70402021-02-02 15:42:194029std::unique_ptr<PolicyContainerPolicies>
4030NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:394031 RenderFrameHostImpl* rfh,
4032 bool is_same_document,
Rakina Zata Amniafd3c6582021-11-30 06:19:174033 const GURL& url) {
Antonio Sartori78a749f2020-11-30 12:03:394034 if (is_same_document) {
Rakina Zata Amniafd3c6582021-11-30 06:19:174035 CHECK(GetLastCommittedEntry());
Charlie Reis73e356242021-04-02 17:10:314036
Antonio Sartori78a749f2020-11-30 12:03:394037 FrameNavigationEntry* previous_frame_entry =
4038 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
4039
4040 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
4041 // ensure we always have a FrameNavigationEntry here.
4042 if (!previous_frame_entry)
4043 return nullptr;
4044
Titouan Rigoudy6ec70402021-02-02 15:42:194045 const PolicyContainerPolicies* previous_policies =
4046 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:394047
Titouan Rigoudy6ec70402021-02-02 15:42:194048 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:394049 return nullptr;
4050
4051 // Make a copy of the policy container for the new FrameNavigationEntry.
Antonio Sartori5d09b30f2021-03-02 09:27:164052 return previous_policies->Clone();
Antonio Sartori78a749f2020-11-30 12:03:394053 }
4054
Antonio Sartori4f5373792021-05-31 10:56:474055 return rfh->policy_container_host()->policies().Clone();
Antonio Sartori78a749f2020-11-30 12:03:394056}
4057
Hayato Ito2c8c08d02021-06-23 03:38:434058void NavigationControllerImpl::BroadcastHistoryOffsetAndLength() {
Carlos Caballeroede6f8c2021-01-28 11:01:504059 OPTIONAL_TRACE_EVENT2(
Hayato Ito2c8c08d02021-06-23 03:38:434060 "content", "NavigationControllerImpl::BroadcastHistoryOffsetAndLength",
4061 "history_offset", GetLastCommittedEntryIndex(), "history_length",
4062 GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:504063
4064 auto callback = base::BindRepeating(
4065 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
4066 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
4067 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
4068 }
4069 },
Hayato Ito2c8c08d02021-06-23 03:38:434070 GetLastCommittedEntryIndex(), GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:504071 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
4072}
4073
4074void NavigationControllerImpl::DidAccessInitialMainDocument() {
4075 // We may have left a failed browser-initiated navigation in the address bar
4076 // to let the user edit it and try again. Clear it now that content might
4077 // show up underneath it.
4078 if (!frame_tree_.IsLoading() && GetPendingEntry())
4079 DiscardPendingEntry(false);
4080
4081 // Update the URL display.
4082 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
4083}
4084
4085void NavigationControllerImpl::UpdateStateForFrame(
4086 RenderFrameHostImpl* rfhi,
4087 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:564088 OPTIONAL_TRACE_EVENT1("content",
4089 "NavigationControllerImpl::UpdateStateForFrame",
4090 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:504091 // The state update affects the last NavigationEntry associated with the given
4092 // |render_frame_host|. This may not be the last committed NavigationEntry (as
4093 // in the case of an UpdateState from a frame being swapped out). We track
4094 // which entry this is in the RenderFrameHost's nav_entry_id.
4095 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
4096 if (!entry)
4097 return;
4098
4099 FrameNavigationEntry* frame_entry =
4100 entry->GetFrameEntry(rfhi->frame_tree_node());
4101 if (!frame_entry)
4102 return;
4103
4104 // The SiteInstance might not match if we do a cross-process navigation with
4105 // replacement (e.g., auto-subframe), in which case the swap out of the old
4106 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4107 // has already been replaced and destroyed.
4108 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4109 return;
4110
4111 if (page_state == frame_entry->page_state())
4112 return; // Nothing to update.
4113
4114 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4115
4116 // The document_sequence_number and item_sequence_number recorded in the
4117 // FrameNavigationEntry should not differ from the one coming with the update,
4118 // since it must come from the same document. Do not update it if a difference
4119 // is detected, as this indicates that |frame_entry| is not the correct one.
4120 blink::ExplodedPageState exploded_state;
4121 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4122 return;
4123
4124 if (exploded_state.top.document_sequence_number !=
4125 frame_entry->document_sequence_number() ||
4126 exploded_state.top.item_sequence_number !=
4127 frame_entry->item_sequence_number()) {
4128 return;
4129 }
4130
4131 frame_entry->SetPageState(page_state);
4132 NotifyEntryChanged(entry);
4133}
4134
Aaron Colwellb731a0ae2021-03-19 19:14:474135void NavigationControllerImpl::OnStoragePartitionIdAdded(
4136 const StoragePartitionId& partition_id) {
4137 auto it = partition_config_to_id_map_.insert(
4138 std::make_pair(partition_id.config(), partition_id));
4139 bool successful_insert = it.second;
Aaron Colwellb731a0ae2021-03-19 19:14:474140 if (!successful_insert) {
4141 LogStoragePartitionIdCrashKeys(it.first->second, partition_id);
4142 }
Sharon Yangd4f48792021-06-30 17:02:534143 CHECK(successful_insert);
Aaron Colwellb731a0ae2021-03-19 19:14:474144}
4145
4146void NavigationControllerImpl::LogStoragePartitionIdCrashKeys(
4147 const StoragePartitionId& original_partition_id,
4148 const StoragePartitionId& new_partition_id) {
4149 base::debug::SetCrashKeyString(
4150 base::debug::AllocateCrashKeyString("original_partition_id",
4151 base::debug::CrashKeySize::Size256),
4152 original_partition_id.ToString());
4153
4154 base::debug::SetCrashKeyString(
4155 base::debug::AllocateCrashKeyString("new_partition_id",
4156 base::debug::CrashKeySize::Size256),
4157 new_partition_id.ToString());
Aaron Colwellb731a0ae2021-03-19 19:14:474158}
4159
Minggang Wangb9f3fa92021-07-01 15:30:314160std::vector<blink::mojom::AppHistoryEntryPtr>
Nate Chapind1fe3612021-04-16 20:45:574161NavigationControllerImpl::PopulateSingleAppHistoryEntryVector(
4162 Direction direction,
4163 int entry_index,
4164 const url::Origin& pending_origin,
4165 FrameTreeNode* node,
4166 SiteInstance* site_instance,
4167 int64_t previous_item_sequence_number) {
Minggang Wangb9f3fa92021-07-01 15:30:314168 std::vector<blink::mojom::AppHistoryEntryPtr> entries;
Rakina Zata Amniafd3c6582021-11-30 06:19:174169 if (GetLastCommittedEntry()->IsInitialEntry()) {
4170 // Don't process the initial entry.
4171 DCHECK_EQ(GetEntryCount(), 1);
4172 return entries;
4173 }
Nate Chapind1fe3612021-04-16 20:45:574174 int offset = direction == Direction::kForward ? 1 : -1;
4175 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4176 i += offset) {
4177 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
4178 if (!frame_entry || !frame_entry->committed_origin())
4179 break;
4180 if (site_instance != frame_entry->site_instance())
4181 break;
4182 if (!pending_origin.IsSameOriginWith(*frame_entry->committed_origin()))
4183 break;
4184 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4185 continue;
4186 blink::ExplodedPageState exploded_page_state;
4187 if (blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4188 &exploded_page_state)) {
4189 blink::ExplodedFrameState frame_state = exploded_page_state.top;
Minggang Wangb9f3fa92021-07-01 15:30:314190 blink::mojom::AppHistoryEntryPtr entry =
4191 blink::mojom::AppHistoryEntry::New(
4192 frame_state.app_history_key.value_or(std::u16string()),
4193 frame_state.app_history_id.value_or(std::u16string()),
Nate Chapinab5c3a712021-11-18 22:17:094194 frame_state.url_string.value_or(std::u16string()),
4195 frame_state.item_sequence_number,
4196 frame_state.document_sequence_number,
4197 frame_state.app_history_state.value_or(std::u16string()));
Rakina Zata Amniafd3c6582021-11-30 06:19:174198
Nate Chapind1fe3612021-04-16 20:45:574199 DCHECK(pending_origin.CanBeDerivedFrom(GURL(entry->url)));
4200 entries.push_back(std::move(entry));
4201 previous_item_sequence_number = frame_entry->item_sequence_number();
4202 }
4203 }
4204 // If |entries| was constructed by iterating backwards from
4205 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4206 // earliest-at-the-front. Reverse it.
4207 if (direction == Direction::kBack)
4208 std::reverse(entries.begin(), entries.end());
4209 return entries;
4210}
4211
4212void NavigationControllerImpl::PopulateAppHistoryEntryVectors(
4213 NavigationRequest* request) {
4214 url::Origin pending_origin =
4215 request->commit_params().origin_to_commit
4216 ? *request->commit_params().origin_to_commit
4217 : url::Origin::Create(request->common_params().url);
4218
4219 FrameTreeNode* node = request->frame_tree_node();
4220 scoped_refptr<SiteInstance> site_instance =
4221 request->GetRenderFrameHost()->GetSiteInstance();
4222
4223 // NOTE: |entry_index| is an estimate of the index where this entry will
4224 // commit, but it may be wrong in corner cases (e.g., if we are at the max
4225 // entry limit, the earliest entry will be dropped). This is ok because this
4226 // algorithm only uses |entry_index| to walk the entry list as it stands right
4227 // now, and it isn't saved for anything post-commit.
4228 int entry_index = GetPendingEntryIndex();
4229 bool will_create_new_entry = false;
4230 if (NavigationTypeUtils::IsReload(request->common_params().navigation_type) ||
4231 request->common_params().should_replace_current_entry) {
4232 entry_index = GetLastCommittedEntryIndex();
4233 } else if (entry_index == -1) {
4234 will_create_new_entry = true;
4235 entry_index = GetLastCommittedEntryIndex() + 1;
4236 }
4237
4238 int64_t pending_item_sequence_number = 0;
4239 if (auto* pending_entry = GetPendingEntry()) {
4240 if (auto* frame_entry = pending_entry->GetFrameEntry(node))
4241 pending_item_sequence_number = frame_entry->item_sequence_number();
4242 }
4243
4244 request->set_app_history_back_entries(PopulateSingleAppHistoryEntryVector(
4245 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
4246 pending_item_sequence_number));
4247
4248 // Don't populate forward entries if they will be truncated by a new entry.
4249 if (!will_create_new_entry) {
4250 request->set_app_history_forward_entries(
4251 PopulateSingleAppHistoryEntryVector(
4252 Direction::kForward, entry_index, pending_origin, node,
4253 site_instance.get(), pending_item_sequence_number));
4254 }
4255}
4256
Nate Chapinfbfe5af2021-06-10 17:22:084257NavigationControllerImpl::HistoryNavigationAction
4258NavigationControllerImpl::ShouldNavigateToEntryForAppHistoryKey(
4259 FrameNavigationEntry* current_entry,
4260 FrameNavigationEntry* target_entry,
4261 const std::string& app_history_key) {
4262 if (!target_entry || !target_entry->committed_origin())
4263 return HistoryNavigationAction::kStopLooking;
4264 if (current_entry->site_instance() != target_entry->site_instance())
4265 return HistoryNavigationAction::kStopLooking;
4266 if (!current_entry->committed_origin()->IsSameOriginWith(
4267 *target_entry->committed_origin())) {
4268 return HistoryNavigationAction::kStopLooking;
4269 }
4270
4271 // NOTE: We don't actually care between kSameDocument and
4272 // kDifferentDocument, so always use kDifferentDocument by convention.
4273 if (target_entry->app_history_key() == app_history_key)
4274 return HistoryNavigationAction::kDifferentDocument;
4275 return HistoryNavigationAction::kKeepLooking;
4276}
4277
4278void NavigationControllerImpl::NavigateToAppHistoryKey(FrameTreeNode* node,
4279 const std::string& key) {
4280 FrameNavigationEntry* current_entry =
4281 GetLastCommittedEntry()->GetFrameEntry(node);
4282 if (!current_entry)
4283 return;
4284
4285 // We want to find the nearest matching entry that is contiguously
4286 // same-instance and same-origin. Check back first, then forward.
4287 // TODO(japhet): Link spec here once it exists.
4288 for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) {
4289 auto result = ShouldNavigateToEntryForAppHistoryKey(
4290 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4291 if (result == HistoryNavigationAction::kStopLooking)
4292 break;
4293 if (result != HistoryNavigationAction::kKeepLooking) {
Nate Chapin2892b37c2021-10-08 17:33:114294 GoToIndex(i, FrameTreeNode::kFrameTreeNodeInvalidId,
4295 false /* is_browser_initiated*/);
Nate Chapinfbfe5af2021-06-10 17:22:084296 return;
4297 }
4298 }
4299 for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) {
4300 auto result = ShouldNavigateToEntryForAppHistoryKey(
4301 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4302 if (result == HistoryNavigationAction::kStopLooking)
4303 break;
4304 if (result != HistoryNavigationAction::kKeepLooking) {
Nate Chapin2892b37c2021-10-08 17:33:114305 GoToIndex(i, FrameTreeNode::kFrameTreeNodeInvalidId,
4306 false /* is_browser_initiated*/);
Nate Chapinfbfe5af2021-06-10 17:22:084307 return;
4308 }
4309 }
4310}
4311
shivanigithubf405bf0d2021-11-05 17:58:334312bool NavigationControllerImpl::ShouldMaintainTrivialSessionHistory(
4313 const FrameTreeNode* frame_tree_node) const {
4314 // TODO(https://crbug.com/1197384): We may have to add portals in addition to
4315 // prerender and fenced frames. This should be kept in sync with
Hayato Ito7a80db42021-07-05 06:18:544316 // LocalFrame version, LocalFrame::ShouldMaintainTrivialSessionHistory.
shivanigithubf405bf0d2021-11-05 17:58:334317 return frame_tree_.is_prerendering() ||
4318 frame_tree_node->IsInFencedFrameTree();
Hayato Ito7a80db42021-07-05 06:18:544319}
4320
[email protected]8ff00d72012-10-23 19:12:214321} // namespace content