blob: ed6cb011a7cfe148c9b95c67c7a909c1f397c339 [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"
Chris Bookholt27faf8d2022-01-20 01:03:3363#include "content/browser/renderer_host/navigation_controller_delegate.h"
danakjc492bf82020-09-09 20:02:4464#include "content/browser/renderer_host/navigation_entry_impl.h"
Nate Chapin214a86a2021-06-21 20:35:5765#include "content/browser/renderer_host/navigation_entry_restore_context_impl.h"
danakjc492bf82020-09-09 20:02:4466#include "content/browser/renderer_host/navigation_request.h"
67#include "content/browser/renderer_host/navigator.h"
68#include "content/browser/renderer_host/render_frame_host_delegate.h"
Carlos Caballero40b0efd2021-01-26 11:55:0069#include "content/browser/renderer_host/render_view_host_impl.h"
Sharon Yangd70a5392021-10-26 23:06:3270#include "content/browser/site_info.h"
[email protected]b6583592012-01-25 19:52:3371#include "content/browser/site_instance_impl.h"
Kunihiko Sakamoto346a74e2021-03-10 08:57:4872#include "content/browser/web_package/subresource_web_bundle_navigation_info.h"
Tsuyoshi Horo37493af2019-11-21 23:43:1473#include "content/browser/web_package/web_bundle_navigation_info.h"
Camille Lamy5193caa2018-10-12 11:59:4274#include "content/common/content_constants_internal.h"
Nate Chapind1fe3612021-04-16 20:45:5775#include "content/common/navigation_params_utils.h"
Nasko Oskovae49e292020-08-13 02:08:5176#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1177#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1978#include "content/public/browser/content_browser_client.h"
Fergal Daly1336ac642021-09-14 15:13:1179#include "content/public/browser/disallow_activation_reason.h"
[email protected]d9083482012-01-06 00:38:4680#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0081#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3882#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1683#include "content/public/browser/notification_types.h"
Lei Zhang96031532019-10-10 19:05:4784#include "content/public/browser/render_view_host.h"
[email protected]9677a3c2012-12-22 04:18:5885#include "content/public/browser/render_widget_host.h"
86#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1087#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3488#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:1989#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:3890#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:1091#include "content/public/common/content_features.h"
Lei Zhang96031532019-10-10 19:05:4792#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4393#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:5994#include "media/base/mime_util.h"
[email protected]a23de8572009-06-03 02:16:3295#include "net/base/escape.h"
Arthur Sonzogni620cec62018-12-13 13:08:5796#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:1897#include "services/metrics/public/cpp/ukm_builders.h"
98#include "services/metrics/public/cpp/ukm_recorder.h"
Yue Ru Sun128804932020-09-30 22:19:1799#include "services/metrics/public/cpp/ukm_source_id.h"
Nan Lind91c8152021-10-21 16:22:37100#include "services/network/public/mojom/fetch_api.mojom.h"
[email protected]9677a3c2012-12-22 04:18:58101#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:39102#include "third_party/blink/public/common/blob/blob_utils.h"
Gyuyoung Kim107c2a02021-04-13 01:49:30103#include "third_party/blink/public/common/chrome_debug_urls.h"
Miyoung Shin1c565c912021-03-17 12:11:21104#include "third_party/blink/public/common/history/session_history_constants.h"
Blink Reformata30d4232018-04-07 15:31:06105#include "third_party/blink/public/common/mime_util/mime_util.h"
Minggang Wanga13c796e2021-07-02 05:54:43106#include "third_party/blink/public/common/navigation/navigation_params.h"
Carlos Caballeroede6f8c2021-01-28 11:01:50107#include "third_party/blink/public/common/page_state/page_state_serialization.h"
Minggang Wangb9f3fa92021-07-01 15:30:31108#include "third_party/blink/public/mojom/navigation/navigation_params.mojom.h"
Minggang Wang7ee0c742021-06-16 16:16:51109#include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h"
[email protected]cca6f392014-05-28 21:32:26110#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:29111
[email protected]8ff00d72012-10-23 19:12:21112namespace content {
[email protected]e9ba4472008-09-14 15:42:43113namespace {
114
115// Invoked when entries have been pruned, or removed. For example, if the
116// current entries are [google, digg, yahoo], with the current entry google,
117// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47118void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50119 int index,
[email protected]c12bf1a12008-09-17 16:28:49120 int count) {
[email protected]8ff00d72012-10-23 19:12:21121 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50122 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49123 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14124 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43125}
126
[email protected]e9ba4472008-09-14 15:42:43127// Configure all the NavigationEntries in entries for restore. This resets
128// the transition type to reload and makes sure the content state isn't empty.
129void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57130 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48131 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47132 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43133 // Use a transition type of reload so that we don't incorrectly increase
134 // the typed count.
Lei Zhang96031532019-10-10 19:05:47135 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
136 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43137 }
138}
139
[email protected]bf70edce2012-06-20 22:32:22140// Determines whether or not we should be carrying over a user agent override
141// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57142bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22143 return last_entry && last_entry->GetIsOverridingUserAgent();
144}
145
Camille Lamy5193caa2018-10-12 11:59:42146// Determines whether to override user agent for a navigation.
147bool ShouldOverrideUserAgent(
148 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57149 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42150 switch (override_user_agent) {
151 case NavigationController::UA_OVERRIDE_INHERIT:
152 return ShouldKeepOverride(last_committed_entry);
153 case NavigationController::UA_OVERRIDE_TRUE:
154 return true;
155 case NavigationController::UA_OVERRIDE_FALSE:
156 return false;
Camille Lamy5193caa2018-10-12 11:59:42157 }
158 NOTREACHED();
159 return false;
160}
161
Rakina Zata Amni312822d72021-06-04 16:13:37162// Returns true if this navigation should be treated as a reload. For e.g.
clamy0a656e42018-02-06 18:18:28163// navigating to the last committed url via the address bar or clicking on a
Rakina Zata Amni312822d72021-06-04 16:13:37164// link which results in a navigation to the last committed URL (but wasn't
165// converted to do a replacement navigation in the renderer), etc.
Fergal Daly766177d2020-07-07 07:54:04166// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
167// |base_url_for_data_url|, |transition_type| correspond to the new navigation
168// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
169// navigation that committed.
170bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
171 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57172 const GURL& virtual_url,
173 const GURL& base_url_for_data_url,
174 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57175 bool is_post,
Rakina Zata Amni312822d72021-06-04 16:13:37176 bool should_replace_current_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57177 NavigationEntryImpl* last_committed_entry) {
Rakina Zata Amni312822d72021-06-04 16:13:37178 // Navigations intended to do a replacement shouldn't be converted to do a
179 // reload.
180 if (should_replace_current_entry)
clamy0a656e42018-02-06 18:18:28181 return false;
clamy0a656e42018-02-06 18:18:28182 // Only convert to reload if at least one navigation committed.
Rakina Zata Amniafd3c6582021-11-30 06:19:17183 if (last_committed_entry->IsInitialEntry())
ananta3bdd8ae2016-12-22 17:11:55184 return false;
185
arthursonzogni7a8243682017-12-14 16:41:42186 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28187 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42188 return false;
189
ananta3bdd8ae2016-12-22 17:11:55190 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
191 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
192 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28193 bool transition_type_can_be_converted = false;
194 if (ui::PageTransitionCoreTypeIs(transition_type,
195 ui::PAGE_TRANSITION_RELOAD) &&
196 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
197 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55198 }
clamy0a656e42018-02-06 18:18:28199 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55200 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28201 transition_type_can_be_converted = true;
202 }
203 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
204 transition_type_can_be_converted = true;
205 if (!transition_type_can_be_converted)
206 return false;
207
208 // This check is required for cases like view-source:, etc. Here the URL of
209 // the navigation entry would contain the url of the page, while the virtual
210 // URL contains the full URL including the view-source prefix.
211 if (virtual_url != last_committed_entry->GetVirtualURL())
212 return false;
213
Fergal Daly766177d2020-07-07 07:54:04214 // Check that the URLs match.
215 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
216 // If there's no frame entry then by definition the URLs don't match.
217 if (!frame_entry)
218 return false;
219
220 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28221 return false;
222
223 // This check is required for Android WebView loadDataWithBaseURL. Apps
224 // can pass in anything in the base URL and we need to ensure that these
225 // match before classifying it as a reload.
226 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
227 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
228 return false;
ananta3bdd8ae2016-12-22 17:11:55229 }
230
clamy0a656e42018-02-06 18:18:28231 // Skip entries with SSL errors.
232 if (last_committed_entry->ssl_error())
233 return false;
234
235 // Don't convert to a reload when the last navigation was a POST or the new
236 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04237 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28238 return false;
239
240 return true;
ananta3bdd8ae2016-12-22 17:11:55241}
242
Nasko Oskov03912102019-01-11 00:21:32243bool DoesURLMatchOriginForNavigation(
244 const GURL& url,
Anton Bikineevf62d1bf2021-05-15 17:56:07245 const absl::optional<url::Origin>& origin,
Kunihiko Sakamoto346a74e2021-03-10 08:57:48246 SubresourceWebBundleNavigationInfo*
247 subresource_web_bundle_navigation_info) {
Nasko Oskov03912102019-01-11 00:21:32248 // If there is no origin supplied there is nothing to match. This can happen
249 // for navigations to a pending entry and therefore it should be allowed.
250 if (!origin)
251 return true;
252
Kunihiko Sakamoto260ee0a2021-10-15 01:44:08253 // Urn: and uuid-in-package: subframe from WebBundle has an opaque origin
254 // derived from the Bundle's origin.
255 // TODO(https://crbug.com/1257045): Remove urn: scheme support.
256 if ((url.SchemeIs(url::kUrnScheme) ||
257 url.SchemeIs(url::kUuidInPackageScheme)) &&
258 subresource_web_bundle_navigation_info) {
Kunihiko Sakamoto346a74e2021-03-10 08:57:48259 return origin->CanBeDerivedFrom(
260 subresource_web_bundle_navigation_info->bundle_url());
261 }
262
Nasko Oskov03912102019-01-11 00:21:32263 return origin->CanBeDerivedFrom(url);
264}
265
Anton Bikineevf62d1bf2021-05-15 17:56:07266absl::optional<url::Origin> GetCommittedOriginForFrameEntry(
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12267 const mojom::DidCommitProvisionalLoadParams& params,
268 NavigationRequest* request) {
Nasko Oskov03912102019-01-11 00:21:32269 // Error pages commit in an opaque origin, yet have the real URL that resulted
270 // in an error as the |params.url|. Since successful reload of an error page
271 // should commit in the correct origin, setting the opaque origin on the
272 // FrameNavigationEntry will be incorrect.
Rakina Zata Amniafd3c6582021-11-30 06:19:17273 if (request && request->DidEncounterError())
Anton Bikineevf62d1bf2021-05-15 17:56:07274 return absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:32275
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12276 // We also currently don't save committed origins for loadDataWithBaseURL
277 // navigations (probably accidentally). Without this check, navigations to
278 // the FrameNavigationEntry might fail the DoesURLMatchOriginForNavigation()
279 // check since the origin will be based on the base URL instead of the data:
280 // URL used for the navigation.
281 // TODO(https://crbug.com/1198406): Save committed origin in
282 // FrameNavigationEntry for this case too.
Rakina Zata Amniafd3c6582021-11-30 06:19:17283 if (request && request->IsLoadDataWithBaseURL())
Anton Bikineevf62d1bf2021-05-15 17:56:07284 return absl::nullopt;
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12285
Anton Bikineevf62d1bf2021-05-15 17:56:07286 return absl::make_optional(params.origin);
Nasko Oskov03912102019-01-11 00:21:32287}
288
Camille Lamy5193caa2018-10-12 11:59:42289bool IsValidURLForNavigation(bool is_main_frame,
290 const GURL& virtual_url,
291 const GURL& dest_url) {
292 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
293 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
294 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
295 << virtual_url.possibly_invalid_spec();
296 return false;
297 }
298
299 // Don't attempt to navigate to non-empty invalid URLs.
300 if (!dest_url.is_valid() && !dest_url.is_empty()) {
301 LOG(WARNING) << "Refusing to load invalid URL: "
302 << dest_url.possibly_invalid_spec();
303 return false;
304 }
305
306 // The renderer will reject IPC messages with URLs longer than
307 // this limit, so don't attempt to navigate with a longer URL.
308 if (dest_url.spec().size() > url::kMaxURLChars) {
309 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
310 << " characters.";
311 return false;
312 }
313
Aaron Colwell33109c592020-04-21 21:31:19314 // Reject renderer debug URLs because they should have been handled before
315 // we get to this point. This check handles renderer debug URLs
316 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
317 // provides defense-in-depth if a renderer debug URL manages to get here via
318 // some other path. We want to reject the navigation here so it doesn't
319 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30320 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19321 LOG(WARNING) << "Refusing to load renderer debug URL: "
322 << dest_url.possibly_invalid_spec();
323 return false;
324 }
325
Camille Lamy5193caa2018-10-12 11:59:42326 return true;
327}
328
Mikel Astizba9cf2fd2017-12-17 10:38:10329// See replaced_navigation_entry_data.h for details: this information is meant
330// to ensure |*output_entry| keeps track of its original URL (landing page in
331// case of server redirects) as it gets replaced (e.g. history.replaceState()),
332// without overwriting it later, for main frames.
333void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57334 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10335 NavigationEntryImpl* output_entry) {
Rakina Zata Amniafd3c6582021-11-30 06:19:17336 if (output_entry->GetReplacedEntryData().has_value() ||
337 replaced_entry->IsInitialEntry()) {
Mikel Astizba9cf2fd2017-12-17 10:38:10338 return;
Rakina Zata Amniafd3c6582021-11-30 06:19:17339 }
Mikel Astizba9cf2fd2017-12-17 10:38:10340
341 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57342 data.first_committed_url = replaced_entry->GetURL();
343 data.first_timestamp = replaced_entry->GetTimestamp();
344 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29345 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10346}
347
Minggang Wangb9f3fa92021-07-01 15:30:31348blink::mojom::NavigationType GetNavigationType(
349 const GURL& old_url,
350 const GURL& new_url,
351 ReloadType reload_type,
352 NavigationEntryImpl* entry,
353 const FrameNavigationEntry& frame_entry,
354 bool has_pending_cross_document_commit,
355 bool is_currently_error_page,
356 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23357 // Reload navigations
358 switch (reload_type) {
359 case ReloadType::NORMAL:
Minggang Wangb9f3fa92021-07-01 15:30:31360 return blink::mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23361 case ReloadType::BYPASSING_CACHE:
Minggang Wangb9f3fa92021-07-01 15:30:31362 return blink::mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23363 case ReloadType::ORIGINAL_REQUEST_URL:
Minggang Wangb9f3fa92021-07-01 15:30:31364 return blink::mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23365 case ReloadType::NONE:
366 break; // Fall through to rest of function.
367 }
368
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08369 if (entry->IsRestored()) {
Minggang Wangb9f3fa92021-07-01 15:30:31370 return entry->GetHasPostData()
371 ? blink::mojom::NavigationType::RESTORE_WITH_POST
372 : blink::mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23373 }
374
danakjb952ef12021-01-14 19:58:49375 const bool can_be_same_document =
376 // A pending cross-document commit means this navigation will not occur in
377 // the current document, as that document would end up being replaced in
378 // the meantime.
379 !has_pending_cross_document_commit &&
380 // If the current document is an error page, we should always treat it as
381 // a different-document navigation so that we'll attempt to load the
382 // document we're navigating to (and not stay in the current error page).
383 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12384
clamyea99ea12018-05-28 13:54:23385 // History navigations.
386 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12387 return can_be_same_document && is_same_document_history_load
Minggang Wangb9f3fa92021-07-01 15:30:31388 ? blink::mojom::NavigationType::HISTORY_SAME_DOCUMENT
389 : blink::mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23390 }
391 DCHECK(!is_same_document_history_load);
392
393 // A same-document fragment-navigation happens when the only part of the url
394 // that is modified is after the '#' character.
395 //
396 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12397 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23398 //
399 // Note: this check is only valid for navigations that are not history
400 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
401 // history navigation from 'A#foo' to 'A#bar' is not a same-document
402 // navigation, but a different-document one. This is why history navigation
403 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47404 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
405 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12406
407 // The one case where we do the wrong thing here and incorrectly choose
408 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
409 // the renderer is a frameset. All frameset navigations should be
410 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
411 // navigation would do the right thing, as it would send it to the browser and
412 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
413 // into this method). But since we can't tell that case here for browser-
414 // initiated navigations, we have to get the renderer involved. In that case
415 // the navigation would be restarted due to the renderer spending a reply of
416 // mojom::CommitResult::RestartCrossDocument.
417
418 return can_be_same_document && is_same_doc
Minggang Wangb9f3fa92021-07-01 15:30:31419 ? blink::mojom::NavigationType::SAME_DOCUMENT
420 : blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23421}
422
Camille Lamy5193caa2018-10-12 11:59:42423// Adjusts the original input URL if needed, to get the URL to actually load and
424// the virtual URL, which may differ.
425void RewriteUrlForNavigation(const GURL& original_url,
426 BrowserContext* browser_context,
427 GURL* url_to_load,
428 GURL* virtual_url,
429 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42430 // Allow the browser URL handler to rewrite the URL. This will, for example,
431 // remove "view-source:" from the beginning of the URL to get the URL that
432 // will actually be loaded. This real URL won't be shown to the user, just
433 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31434 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42435 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
436 url_to_load, browser_context, reverse_on_redirect);
437}
438
439#if DCHECK_IS_ON()
440// Helper sanity check function used in debug mode.
441void ValidateRequestMatchesEntry(NavigationRequest* request,
442 NavigationEntryImpl* entry) {
443 if (request->frame_tree_node()->IsMainFrame()) {
444 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
445 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
Minggang Wangb9f3fa92021-07-01 15:30:31446 ui::PageTransitionFromInt(request->common_params().transition),
447 entry->GetTransitionType()));
Camille Lamy5193caa2018-10-12 11:59:42448 }
Nasko Oskovc36327d2019-01-03 23:23:04449 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42450 entry->should_clear_history_list());
451 DCHECK_EQ(request->common_params().has_user_gesture,
452 entry->has_user_gesture());
453 DCHECK_EQ(request->common_params().base_url_for_data_url,
454 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04455 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42456 entry->GetCanLoadLocalResources());
457 DCHECK_EQ(request->common_params().started_from_context_menu,
458 entry->has_started_from_context_menu());
459
460 FrameNavigationEntry* frame_entry =
461 entry->GetFrameEntry(request->frame_tree_node());
462 if (!frame_entry) {
463 NOTREACHED();
464 return;
465 }
466
Camille Lamy5193caa2018-10-12 11:59:42467 DCHECK_EQ(request->common_params().method, frame_entry->method());
468
Nasko Oskovc36327d2019-01-03 23:23:04469 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42470 if (redirect_size == frame_entry->redirect_chain().size()) {
471 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04472 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42473 frame_entry->redirect_chain()[i]);
474 }
475 } else {
476 NOTREACHED();
477 }
478}
479#endif // DCHECK_IS_ON()
480
Dave Tapuska8bfd84c2019-03-26 20:47:16481// Returns whether the session history NavigationRequests in |navigations|
482// would stay within the subtree of the sandboxed iframe in
483// |sandbox_frame_tree_node_id|.
484bool DoesSandboxNavigationStayWithinSubtree(
485 int sandbox_frame_tree_node_id,
486 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
487 for (auto& item : navigations) {
488 bool within_subtree = false;
489 // Check whether this NavigationRequest affects a frame within the
490 // sandboxed frame's subtree by walking up the tree looking for the
491 // sandboxed frame.
492 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03493 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16494 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
495 within_subtree = true;
496 break;
497 }
498 }
499 if (!within_subtree)
500 return false;
501 }
502 return true;
503}
504
[email protected]e9ba4472008-09-14 15:42:43505} // namespace
506
arthursonzogni66f711c2019-10-08 14:40:36507// NavigationControllerImpl::PendingEntryRef------------------------------------
508
509NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
510 base::WeakPtr<NavigationControllerImpl> controller)
511 : controller_(controller) {}
512
513NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
514 if (!controller_) // Can be null with interstitials.
515 return;
516
517 controller_->PendingEntryRefDeleted(this);
518}
519
[email protected]d202a7c2012-01-04 07:53:47520// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29521
[email protected]23a918b2014-07-15 09:51:36522const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23523
[email protected]765b35502008-08-21 00:51:20524// static
[email protected]d202a7c2012-01-04 07:53:47525size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23526 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20527
[email protected]e6fec472013-05-14 05:29:02528// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20529// when testing.
530static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29531
[email protected]71fde352011-12-29 03:29:56532// static
dcheng9bfa5162016-04-09 01:00:57533std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
534 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10535 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07536 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10537 ui::PageTransition transition,
538 bool is_renderer_initiated,
539 const std::string& extra_headers,
540 BrowserContext* browser_context,
541 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
542 return NavigationControllerImpl::CreateNavigationEntry(
543 url, referrer, std::move(initiator_origin),
544 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Hayato Ito303654c2021-06-30 09:07:54545 extra_headers, browser_context, std::move(blob_url_loader_factory));
Lukasz Anforowicz641234d52019-11-07 21:07:10546}
547
548// static
549std::unique_ptr<NavigationEntryImpl>
550NavigationControllerImpl::CreateNavigationEntry(
551 const GURL& url,
552 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07553 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10554 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57555 ui::PageTransition transition,
556 bool is_renderer_initiated,
557 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09558 BrowserContext* browser_context,
Hayato Ito303654c2021-06-30 09:07:54559 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
Camille Lamy5193caa2018-10-12 11:59:42560 GURL url_to_load;
561 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56562 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42563 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
564 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56565
Lukasz Anforowicz641234d52019-11-07 21:07:10566 // Let the NTP override the navigation params and pretend that this is a
567 // browser-initiated, bookmark-like navigation.
568 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violetcf6ea7e2021-06-09 21:09:21569 source_site_instance, &transition, &is_renderer_initiated, &referrer,
570 &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10571
Patrick Monettef507e982019-06-19 20:18:06572 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28573 nullptr, // The site instance for tabs is sent on navigation
574 // (WebContents::GetSiteInstance).
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58575 url_to_load, referrer, initiator_origin, std::u16string(), transition,
Rakina Zata Amniafd3c6582021-11-30 06:19:17576 is_renderer_initiated, blob_url_loader_factory,
577 /* is_initial_entry = */ false);
Camille Lamy5193caa2018-10-12 11:59:42578 entry->SetVirtualURL(virtual_url);
579 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56580 entry->set_update_virtual_url_with_url(reverse_on_redirect);
581 entry->set_extra_headers(extra_headers);
Patrick Monettef507e982019-06-19 20:18:06582 return entry;
[email protected]71fde352011-12-29 03:29:56583}
584
[email protected]cdcb1dee2012-01-04 00:46:20585// static
586void NavigationController::DisablePromptOnRepost() {
587 g_check_for_repost = false;
588}
589
[email protected]c5b88d82012-10-06 17:03:33590base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
591 base::Time t) {
592 // If |t| is between the water marks, we're in a run of duplicates
593 // or just getting out of it, so increase the high-water mark to get
594 // a time that probably hasn't been used before and return it.
595 if (low_water_mark_ <= t && t <= high_water_mark_) {
Peter Kastinge5a38ed2021-10-02 03:06:35596 high_water_mark_ += base::Microseconds(1);
[email protected]c5b88d82012-10-06 17:03:33597 return high_water_mark_;
598 }
599
600 // Otherwise, we're clear of the last duplicate run, so reset the
601 // water marks.
602 low_water_mark_ = high_water_mark_ = t;
603 return t;
604}
605
ckitagawa0faa5e42020-06-17 17:30:54606NavigationControllerImpl::ScopedShowRepostDialogForTesting::
607 ScopedShowRepostDialogForTesting()
608 : was_disallowed_(g_check_for_repost) {
609 g_check_for_repost = true;
610}
611
612NavigationControllerImpl::ScopedShowRepostDialogForTesting::
613 ~ScopedShowRepostDialogForTesting() {
614 g_check_for_repost = was_disallowed_;
615}
616
[email protected]d202a7c2012-01-04 07:53:47617NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00618 BrowserContext* browser_context,
619 FrameTree& frame_tree,
620 NavigationControllerDelegate* delegate)
621 : frame_tree_(frame_tree),
622 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57623 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22624 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47625 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37626 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29627}
628
[email protected]d202a7c2012-01-04 07:53:47629NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00630 // The NavigationControllerImpl might be called inside its delegate
631 // destructor. Calling it is not valid anymore.
632 delegate_ = nullptr;
633 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29634}
635
Matt Falkenhagen548ed1562021-07-06 01:38:26636WebContents* NavigationControllerImpl::DeprecatedGetWebContents() {
637 return delegate_->DeprecatedGetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32638}
639
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57640BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55641 return browser_context_;
642}
643
[email protected]d202a7c2012-01-04 07:53:47644void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30645 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36646 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57647 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57648 // Verify that this controller is unused and that the input is valid.
Rakina Zata Amniafd3c6582021-11-30 06:19:17649 DCHECK_EQ(1, GetEntryCount());
650 DCHECK(GetLastCommittedEntry()->IsInitialEntry());
Lei Zhang96031532019-10-10 19:05:47651 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57652 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14653 selected_navigation < static_cast<int>(entries->size()));
arthursonzogni5c4c202d2017-04-25 23:41:27654 DCHECK_EQ(-1, pending_entry_index_);
[email protected]ce3fa3c2009-04-20 19:55:57655
[email protected]ce3fa3c2009-04-20 19:55:57656 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44657 needs_reload_type_ = NeedsReloadType::kRestoreSession;
Rakina Zata Amniafd3c6582021-11-30 06:19:17658 entries_.clear();
avif16f85a72015-11-13 18:25:03659 entries_.reserve(entries->size());
660 for (auto& entry : *entries)
dcheng36b6aec92015-12-26 06:16:36661 entries_.push_back(
662 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
avif16f85a72015-11-13 18:25:03663
664 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
665 // cleared out safely.
666 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57667
668 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36669 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57670}
671
toyoshim6142d96f2016-12-19 09:07:25672void NavigationControllerImpl::Reload(ReloadType reload_type,
673 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28674 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28675 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32676 int current_index = -1;
677
Carlos IL42b416592019-10-07 23:10:36678 if (entry_replaced_by_post_commit_error_) {
679 // If there is an entry that was replaced by a currently active post-commit
680 // error navigation, this can't be the initial navigation.
681 DCHECK(!IsInitialNavigation());
682 // If the current entry is a post commit error, we reload the entry it
683 // replaced instead. We leave the error entry in place until a commit
684 // replaces it, but the pending entry points to the original entry in the
685 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
686 // case that pending_entry_ != entries_[pending_entry_index_].
687 entry = entry_replaced_by_post_commit_error_.get();
688 current_index = GetCurrentEntryIndex();
689 } else if (IsInitialNavigation() && pending_entry_) {
690 // If we are reloading the initial navigation, just use the current
691 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32692 entry = pending_entry_;
693 // The pending entry might be in entries_ (e.g., after a Clone), so we
694 // should also update the current_index.
695 current_index = pending_entry_index_;
696 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00697 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32698 current_index = GetCurrentEntryIndex();
699 if (current_index != -1) {
creis3da03872015-02-20 21:12:32700 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32701 }
[email protected]979a4bc2013-04-24 01:27:15702 }
[email protected]241db352013-04-22 18:04:05703
[email protected]59167c22013-06-03 18:07:32704 // If we are no where, then we can't reload. TODO(darin): We should add a
705 // CanReload method.
706 if (!entry)
707 return;
708
Rakina Zata Amnif297a802022-01-18 03:53:43709 if (entry->IsInitialEntryNotForSynchronousAboutBlank()) {
710 // We should never navigate to an existing initial NavigationEntry that is
711 // the initial NavigationEntry for the initial empty document that hasn't
712 // been overridden by the synchronous about:blank commit, to preserve
713 // legacy behavior where trying to reload when the main frame is on the
714 // initial empty document won't result in a navigation. See also
715 // https://crbug.com/1277414.
716 return;
717 }
718
Takashi Toyoshimac7df3c22019-06-25 14:18:47719 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26720 entry->set_reload_type(reload_type);
721
Aran Gilman37d11632019-10-08 23:07:15722 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30723 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07724 // they really want to do this. If they do, the dialog will call us back
725 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57726 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02727
[email protected]106a0812010-03-18 00:15:12728 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57729 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47730 return;
initial.commit09911bf2008-07-26 23:55:29731 }
Lei Zhang96031532019-10-10 19:05:47732
733 if (!IsInitialNavigation())
734 DiscardNonCommittedEntries();
735
736 pending_entry_ = entry;
737 pending_entry_index_ = current_index;
738 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
739
Nate Chapin45f620582021-09-30 17:45:43740 // location.reload() goes through BeginNavigation, so all reloads triggered
741 // via this codepath are browser initiated.
Lei Zhang96031532019-10-10 19:05:47742 NavigateToExistingPendingEntry(reload_type,
Nate Chapin45f620582021-09-30 17:45:43743 FrameTreeNode::kFrameTreeNodeInvalidId,
744 true /* is_browser_initiated */);
initial.commit09911bf2008-07-26 23:55:29745}
746
[email protected]d202a7c2012-01-04 07:53:47747void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48748 DCHECK(pending_reload_ != ReloadType::NONE);
749 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12750}
751
[email protected]d202a7c2012-01-04 07:53:47752void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48753 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12754 NOTREACHED();
755 } else {
toyoshim6142d96f2016-12-19 09:07:25756 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48757 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12758 }
759}
760
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57761bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09762 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11763}
764
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57765bool NavigationControllerImpl::IsInitialBlankNavigation() {
Rakina Zata Amniafd3c6582021-11-30 06:19:17766 return IsInitialNavigation() && GetEntryCount() == 1 &&
767 GetLastCommittedEntry()->IsInitialEntry() &&
768 GetLastCommittedEntry()->restore_type() == RestoreType::kNotRestored;
creis10a4ab72015-10-13 17:22:40769}
770
Aran Gilman37d11632019-10-08 23:07:15771NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
772 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58773 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03774 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58775}
776
Adithya Srinivasan9b0c99c2021-08-10 15:19:45777NavigationEntryImpl*
778NavigationControllerImpl::GetEntryWithUniqueIDIncludingPending(
779 int nav_entry_id) const {
780 NavigationEntryImpl* entry = GetEntryWithUniqueID(nav_entry_id);
781 if (entry)
782 return entry;
783 return pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id
Keishi Hattori0e45c022021-11-27 09:25:52784 ? pending_entry_.get()
Adithya Srinivasan9b0c99c2021-08-10 15:19:45785 : nullptr;
786}
787
W. James MacLean1c40862c2020-04-27 21:05:57788void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
789 const url::Origin& origin) {
790 for (int i = 0; i < GetEntryCount(); i++) {
791 auto* entry = GetEntryAtIndex(i);
792 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
793 }
794 if (entry_replaced_by_post_commit_error_) {
795 // It's possible we could come back to this entry if the error
796 // page/interstitial goes away.
797 entry_replaced_by_post_commit_error_
798 ->RegisterExistingOriginToPreventOptInIsolation(origin);
799 }
800 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
801 // visiting pending_entry_, which lacks a committed origin. This will be done
802 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
803}
804
avi25764702015-06-23 15:43:37805void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57806 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00807 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37808 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27809 DCHECK_EQ(-1, pending_entry_index_);
[email protected]765b35502008-08-21 00:51:20810}
811
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57812NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47813 if (pending_entry_)
814 return pending_entry_;
815 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20816}
817
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57818NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32819 // The pending entry is safe to return for new (non-history), browser-
820 // initiated navigations. Most renderer-initiated navigations should not
821 // show the pending entry, to prevent URL spoof attacks.
822 //
823 // We make an exception for renderer-initiated navigations in new tabs, as
824 // long as no other page has tried to access the initial empty document in
825 // the new tab. If another page modifies this blank page, a URL spoof is
826 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32827 bool safe_to_show_pending =
828 pending_entry_ &&
829 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09830 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32831 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46832 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32833
834 // Also allow showing the pending entry for history navigations in a new tab,
835 // such as Ctrl+Back. In this case, no existing page is visible and no one
836 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15837 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
838 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32839 safe_to_show_pending = true;
840
841 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19842 return pending_entry_;
843 return GetLastCommittedEntry();
844}
845
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57846int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20847 if (pending_entry_index_ != -1)
848 return pending_entry_index_;
849 return last_committed_entry_index_;
850}
851
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57852NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20853 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28854 return nullptr;
avif16f85a72015-11-13 18:25:03855 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20856}
857
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57858bool NavigationControllerImpl::CanViewSource() {
Jeremy Roman2d8dfe132021-07-06 20:51:26859 const std::string& mime_type =
860 frame_tree_.root()->current_frame_host()->GetPage().contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53861 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
862 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27863 NavigationEntry* visible_entry = GetVisibleEntry();
864 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39865 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16866}
867
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57868int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27869 // The last committed entry index must always be less than the number of
Rakina Zata Amniafd3c6582021-11-30 06:19:17870 // entries.
arthursonzogni5c4c202d2017-04-25 23:41:27871 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
[email protected]a26023822011-12-29 00:23:55872 return last_committed_entry_index_;
873}
874
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57875int NavigationControllerImpl::GetEntryCount() {
Rakina Zata Amniafd3c6582021-11-30 06:19:17876 DCHECK_GE(entries_.size(), 1u);
Carlos IL4dea8902020-05-26 15:14:29877 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55878 return static_cast<int>(entries_.size());
879}
880
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57881NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37882 if (index < 0 || index >= GetEntryCount())
883 return nullptr;
884
avif16f85a72015-11-13 18:25:03885 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25886}
887
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57888NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47889 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20890}
891
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57892int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46893 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03894}
895
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57896bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03897 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
898 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
899 return true;
900 }
901 return false;
[email protected]765b35502008-08-21 00:51:20902}
903
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57904bool NavigationControllerImpl::CanGoForward() {
WangHui74286d52021-03-31 16:17:15905 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
906 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
907 return true;
908 }
909 return false;
[email protected]765b35502008-08-21 00:51:20910}
911
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57912bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03913 int index = GetIndexForOffset(offset);
914 return index >= 0 && index < GetEntryCount();
915}
916
Xiaohan Wang7f8052e02022-01-14 18:44:28917#if BUILDFLAG(IS_ANDROID)
WangHui74286d52021-03-31 16:17:15918bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
WangHui74286d52021-03-31 16:17:15919 if (offset == 0)
920 return true;
921 int increment = offset > 0 ? 1 : -1;
922 int non_skippable_entries = 0;
923 for (int index = GetIndexForOffset(increment);
924 index >= 0 && index < GetEntryCount(); index += increment) {
925 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
926 non_skippable_entries++;
927
928 if (non_skippable_entries == std::abs(offset))
929 return true;
930 }
931 return false;
932}
933#endif
934
[email protected]d202a7c2012-01-04 07:53:47935void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06936 int target_index = GetIndexForOffset(-1);
937
Elly Fong-Jones40ee8112021-06-23 19:10:52938 // Move the target index past the skippable entries.
Shivani Sharma298d12852019-01-22 20:04:03939 bool all_skippable_entries = true;
Elly Fong-Jones40ee8112021-06-23 19:10:52940 while (target_index >= 0) {
941 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui()) {
Shivani Sharma298d12852019-01-22 20:04:03942 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06943 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16944 }
Elly Fong-Jones40ee8112021-06-23 19:10:52945 target_index--;
shivanisha55201872018-12-13 04:29:06946 }
Miyoung Shin1c565c912021-03-17 12:11:21947
Shivani Sharma298d12852019-01-22 20:04:03948 // Do nothing if all entries are skippable. Normally this path would not
949 // happen as consumers would have already checked it in CanGoBack but a lot of
950 // tests do not do that.
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42951 if (all_skippable_entries)
Shivani Sharma298d12852019-01-22 20:04:03952 return;
shivanisha55201872018-12-13 04:29:06953
shivanisha55201872018-12-13 04:29:06954 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20955}
956
[email protected]d202a7c2012-01-04 07:53:47957void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06958 int target_index = GetIndexForOffset(1);
959
Shivani Sharma2d5b4b6b2019-01-08 16:07:16960 // Note that at least one entry (the last one) will be non-skippable since
961 // entries are marked skippable only when they add another entry because of
962 // redirect or pushState.
Elly Fong-Jones40ee8112021-06-23 19:10:52963 while (target_index < static_cast<int>(entries_.size())) {
964 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui())
shivanisha55201872018-12-13 04:29:06965 break;
Elly Fong-Jones40ee8112021-06-23 19:10:52966 target_index++;
shivanisha55201872018-12-13 04:29:06967 }
shivanisha55201872018-12-13 04:29:06968 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20969}
970
[email protected]d202a7c2012-01-04 07:53:47971void NavigationControllerImpl::GoToIndex(int index) {
Nate Chapin45f620582021-09-30 17:45:43972 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId,
973 true /* is_browser_initiated */);
Dave Tapuska8bfd84c2019-03-26 20:47:16974}
975
976void NavigationControllerImpl::GoToIndex(int index,
Nate Chapin45f620582021-09-30 17:45:43977 int sandbox_frame_tree_node_id,
978 bool is_browser_initiated) {
979 DCHECK(sandbox_frame_tree_node_id == FrameTreeNode::kFrameTreeNodeInvalidId ||
980 !is_browser_initiated);
sunjian30574a62017-03-21 21:39:44981 TRACE_EVENT0("browser,navigation,benchmark",
982 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:20983 if (index < 0 || index >= static_cast<int>(entries_.size())) {
984 NOTREACHED();
985 return;
986 }
987
Rakina Zata Amnif297a802022-01-18 03:53:43988 if (entries_[index]->IsInitialEntryNotForSynchronousAboutBlank()) {
989 // We should never navigate to an existing initial NavigationEntry that is
990 // the initial NavigationEntry for the initial empty document that hasn't
991 // been overridden by the synchronous about:blank commit, to preserve
992 // legacy behavior where trying to reload when the main frame is on the
993 // initial empty document won't result in a navigation. See also
994 // https://crbug.com/1277414.
995 return;
996 }
997
[email protected]cbab76d2008-10-13 22:42:47998 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20999
arthursonzogni5c4c202d2017-04-25 23:41:271000 DCHECK_EQ(nullptr, pending_entry_);
1001 DCHECK_EQ(-1, pending_entry_index_);
Rakina Zata Amnif297a802022-01-18 03:53:431002
arthursonzogni5c4c202d2017-04-25 23:41:271003 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:201004 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:271005 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
1006 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Nate Chapin45f620582021-09-30 17:45:431007 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id,
1008 is_browser_initiated);
[email protected]765b35502008-08-21 00:51:201009}
1010
[email protected]d202a7c2012-01-04 07:53:471011void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:121012 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:031013 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:201014 return;
1015
[email protected]9ba14052012-06-22 23:50:031016 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:201017}
1018
Nate Chapin45f620582021-09-30 17:45:431019void NavigationControllerImpl::GoToOffsetFromRenderer(int offset) {
1020 // Note: This is actually reached in unit tests.
1021 if (!CanGoToOffset(offset))
1022 return;
1023
1024 GoToIndex(GetIndexForOffset(offset), FrameTreeNode::kFrameTreeNodeInvalidId,
1025 false /* is_browser_initiated */);
1026}
1027
Xiaohan Wang7f8052e02022-01-14 18:44:281028#if BUILDFLAG(IS_ANDROID)
WangHui74286d52021-03-31 16:17:151029void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
1030 // Note: This is actually reached in unit tests.
1031 if (!CanGoToOffsetWithSkipping(offset))
1032 return;
1033
Elly Fong-Jonesccc6d1f2021-06-14 18:32:421034 if (offset == 0) {
WangHui74286d52021-03-31 16:17:151035 GoToIndex(GetIndexForOffset(offset));
1036 return;
1037 }
1038 int increment = offset > 0 ? 1 : -1;
1039 // Find the offset without counting skippable entries.
1040 int target_index = GetIndexForOffset(increment);
1041 int non_skippable_entries = 0;
1042 for (int index = target_index; index >= 0 && index < GetEntryCount();
1043 index += increment) {
1044 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1045 non_skippable_entries++;
1046
1047 if (non_skippable_entries == std::abs(offset)) {
1048 target_index = index;
1049 break;
1050 }
1051 }
1052
1053 GoToIndex(target_index);
1054}
1055#endif
1056
[email protected]41374f12013-07-24 02:49:281057bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151058 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281059 return false;
[email protected]6a13a6c2011-12-20 21:47:121060
[email protected]43032342011-03-21 14:10:311061 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281062 return true;
[email protected]cbab76d2008-10-13 22:42:471063}
1064
Michael Thiessen9b14d512019-09-23 21:19:471065void NavigationControllerImpl::PruneForwardEntries() {
1066 DiscardNonCommittedEntries();
1067 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471068 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471069 if (num_removed <= 0)
1070 return;
1071 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1072 NotifyPrunedEntries(this, remove_start_index /* start index */,
1073 num_removed /* count */);
1074}
1075
Aran Gilman37d11632019-10-08 23:07:151076void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1077 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321078 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311079 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511080 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1081 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321082 }
1083}
1084
Harkiran Bolariaba823e42021-05-21 18:30:361085base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL(
1086 const GURL& url,
1087 const Referrer& referrer,
1088 ui::PageTransition transition,
1089 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471090 LoadURLParams params(url);
1091 params.referrer = referrer;
1092 params.transition_type = transition;
1093 params.extra_headers = extra_headers;
Harkiran Bolariaba823e42021-05-21 18:30:361094 return LoadURLWithParams(params);
[email protected]cf002332012-08-14 19:17:471095}
1096
Harkiran Bolariaba823e42021-05-21 18:30:361097base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams(
1098 const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061099 if (params.is_renderer_initiated)
1100 DCHECK(params.initiator_origin.has_value());
1101
naskob8744d22014-08-28 17:07:431102 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151103 "NavigationControllerImpl::LoadURLWithParams", "url",
1104 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291105 bool is_explicit_navigation =
1106 GetContentClient()->browser()->IsExplicitNavigation(
1107 params.transition_type);
1108 if (HandleDebugURL(params.url, params.transition_type,
1109 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431110 // If Telemetry is running, allow the URL load to proceed as if it's
1111 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491112 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431113 cc::switches::kEnableGpuBenchmarking))
Harkiran Bolariaba823e42021-05-21 18:30:361114 return nullptr;
[email protected]47752982014-07-29 08:01:431115 }
[email protected]8bf1048012012-02-08 01:22:181116
[email protected]cf002332012-08-14 19:17:471117 // Checks based on params.load_type.
1118 switch (params.load_type) {
1119 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431120 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471121 break;
1122 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261123 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471124 NOTREACHED() << "Data load must use data scheme.";
Harkiran Bolariaba823e42021-05-21 18:30:361125 return nullptr;
[email protected]cf002332012-08-14 19:17:471126 }
1127 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461128 }
[email protected]e47ae9472011-10-13 19:48:341129
[email protected]e47ae9472011-10-13 19:48:341130 // The user initiated a load, we don't need to reload anymore.
1131 needs_reload_ = false;
1132
Harkiran Bolariaba823e42021-05-21 18:30:361133 return NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441134}
1135
Mohamed Abdelhalim833de902019-09-16 17:41:451136bool NavigationControllerImpl::PendingEntryMatchesRequest(
1137 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191138 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451139 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191140}
1141
Chris Bookholt27faf8d2022-01-20 01:03:331142bool NavigationControllerImpl::has_post_commit_error_entry() const {
1143 return entry_replaced_by_post_commit_error_ != nullptr;
1144}
1145
[email protected]d202a7c2012-01-04 07:53:471146bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321147 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071148 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331149 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441150 bool is_same_document_navigation,
Nate Chapinc7019dd7d2021-06-25 18:29:251151 bool was_on_initial_empty_document,
Shivani Sharmaffb32b82019-04-09 16:58:471152 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131153 NavigationRequest* navigation_request) {
1154 DCHECK(navigation_request);
Chris Bookholt27faf8d2022-01-20 01:03:331155
1156 // Note: validation checks and renderer kills due to invalid commit messages
1157 // must happen before getting here, in
1158 // RenderFrameHostImpl::ValidateDidCommitParams. By the time we get here, some
1159 // effects of the navigation have already occurred.
1160
shivanigithubf405bf0d2021-11-05 17:58:331161 if (ShouldMaintainTrivialSessionHistory(rfh->frame_tree_node()) &&
1162 GetLastCommittedEntry()) {
Takashi Toyoshimaa9f66802021-08-18 02:40:171163 // Ensure that this navigation does not add a navigation entry, since
1164 // ShouldMaintainTrivialSessionHistory() means we should not add an entry
1165 // beyond the last committed one. Therefore, `should_replace_current_entry`
1166 // should be set, which replaces the current entry, or this should be a
1167 // reload, which does not create a new entry.
1168 DCHECK(params.should_replace_current_entry ||
1169 navigation_request->GetReloadType() != ReloadType::NONE);
1170 }
[email protected]cd2e15742013-03-08 04:08:311171 is_initial_navigation_ = false;
1172
[email protected]0e8db942008-09-24 21:21:481173 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431174 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481175 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361176 if (entry_replaced_by_post_commit_error_) {
Chris Bookholt27faf8d2022-01-20 01:03:331177 // Same document navigation events with a post-commit error should already
1178 // be blocked by RenderFrameHostImpl::ValidateDidCommitParams() before
1179 // reaching here.
1180 CHECK(!is_same_document_navigation);
1181
Carlos IL42b416592019-10-07 23:10:361182 // Any commit while a post-commit error page is showing should put the
1183 // original entry back, replacing the error page's entry. This includes
1184 // reloads, where the original entry was used as the pending entry and
1185 // should now be at the correct index at commit time.
1186 entries_[last_committed_entry_index_] =
1187 std::move(entry_replaced_by_post_commit_error_);
1188 }
Fergal Daly8e33cf62020-12-12 01:06:071189 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551190 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431191 if (pending_entry_ &&
1192 pending_entry_->GetIsOverridingUserAgent() !=
1193 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1194 overriding_user_agent_changed = true;
Xiaohan Wang7f8052e02022-01-14 18:44:281195#if BUILDFLAG(IS_ANDROID)
Shu Yang5756fa422021-11-05 23:02:541196 // TODO(crbug.com/1266277): Clean up the logic of setting
1197 // |overriding_user_agent_changed| post-launch.
Shu Yang9eabbb902021-11-17 00:45:031198 if (base::FeatureList::IsEnabled(features::kRequestDesktopSiteExceptions)) {
Shu Yang5756fa422021-11-05 23:02:541199 // Must honor user agent overrides in the |navigation_request|, such as
1200 // from things like RequestDesktopSiteWebContentsObserverAndroid. As a
1201 // result, besides comparing |pending_entry_|'s user agent against
1202 // LastCommittedEntry's, also need to compare |navigation_request|'s user
1203 // agent against LastCommittedEntry's.
1204 if (navigation_request->is_overriding_user_agent() !=
1205 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
1206 overriding_user_agent_changed = true;
1207 }
1208 }
Xiaohan Wang7f8052e02022-01-14 18:44:281209#endif // BUILDFLAG(IS_ANDROID)
[email protected]0e8db942008-09-24 21:21:481210 } else {
Gang Wu325f03f42021-02-25 20:00:461211 // GetLastCommittedEntry() is null, so this is the first entry.
Fergal Daly8e33cf62020-12-12 01:06:071212 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481213 details->previous_entry_index = -1;
Gang Wu325f03f42021-02-25 20:00:461214 if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) {
1215 // Default setting is NOT override the user agent, so overriding the user
1216 // agent in first entry should be considered as user agent changed as
1217 // well.
1218 overriding_user_agent_changed = true;
1219 }
Xiaohan Wang7f8052e02022-01-14 18:44:281220#if BUILDFLAG(IS_ANDROID)
Shu Yang5756fa422021-11-05 23:02:541221 // TODO(crbug.com/1266277): Clean up the logic of setting
1222 // |overriding_user_agent_changed| post-launch.
Shu Yang9eabbb902021-11-17 00:45:031223 if (base::FeatureList::IsEnabled(features::kRequestDesktopSiteExceptions)) {
Shu Yang5756fa422021-11-05 23:02:541224 // Must honor user agent overrides in the |navigation_request|, such as
1225 // from things like RequestDesktopSiteWebContentsObserverAndroid. As a
1226 // result, besides checking |pending_entry_|'s user agent, also need to
1227 // check |navigation_request|'s.
1228 if (navigation_request->is_overriding_user_agent()) {
1229 overriding_user_agent_changed = true;
1230 }
1231 }
Xiaohan Wang7f8052e02022-01-14 18:44:281232#endif // BUILDFLAG(IS_ANDROID)
[email protected]0e8db942008-09-24 21:21:481233 }
[email protected]ecd9d8702008-08-28 22:10:171234
Dave Tapuskaa2ab4f252021-07-08 21:31:281235 bool is_main_frame_navigation = !rfh->GetParent();
1236
Alexander Timind2f2e4f22019-04-02 20:04:531237 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1238 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281239 // For primary frame tree navigations, choose an appropriate
1240 // BackForwardCacheMetrics to be associated with the NavigationEntry, by
1241 // either creating a new object or reusing the previous one depending on
1242 // whether it's a main frame navigation or not.
1243 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics;
1244 if (navigation_request->frame_tree_node()->frame_tree()->type() ==
1245 FrameTree::Type::kPrimary) {
1246 back_forward_cache_metrics =
1247 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1248 GetLastCommittedEntry(), is_main_frame_navigation,
1249 params.document_sequence_number);
1250 }
Alexander Timind2f2e4f22019-04-02 20:04:531251 // Notify the last active entry that we have navigated away.
Dave Tapuskaa2ab4f252021-07-08 21:31:281252 if (is_main_frame_navigation && !is_same_document_navigation) {
Alexander Timind2f2e4f22019-04-02 20:04:531253 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1254 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Hajime Hoshic7606502021-04-14 02:42:161255 metrics->MainFrameDidNavigateAwayFromDocument(rfh, navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531256 }
1257 }
1258 }
1259
fdegans9caf66a2015-07-30 21:10:421260 // If there is a pending entry at this point, it should have a SiteInstance,
1261 // except for restored entries.
jam48cea9082017-02-15 06:13:291262 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481263 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081264 pending_entry_->IsRestored());
1265 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151266 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291267 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481268 }
[email protected]e9ba4472008-09-14 15:42:431269
Nasko Oskovaee2f862018-06-15 00:05:521270 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1271 // has changed, this must be treated as a new navigation with replacement.
1272 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491273 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521274 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101275 pending_entry_->GetUniqueID() ==
1276 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521277 pending_entry_->site_instance() &&
1278 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1279 DCHECK_NE(-1, pending_entry_index_);
1280 // TODO(nasko,creis): Instead of setting this value here, set
1281 // should_replace_current_entry on the parameters we send to the
1282 // renderer process as part of CommitNavigation. The renderer should
1283 // in turn send it back here as part of |params| and it can be just
1284 // enforced and renderer process terminated on mismatch.
1285 details->did_replace_entry = true;
1286 } else {
1287 // The renderer tells us whether the navigation replaces the current entry.
1288 details->did_replace_entry = params.should_replace_current_entry;
1289 }
[email protected]bcd904482012-02-01 01:54:221290
[email protected]e9ba4472008-09-14 15:42:431291 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amnia4e27222021-12-22 01:05:001292 details->type = ClassifyNavigation(rfh, params, navigation_request, details);
Rakina Zata Amniddf10502022-01-15 02:56:551293 if (GetLastCommittedEntry()->IsInitialEntry()) {
1294 if (rfh->GetParent()) {
1295 // Subframe navigation on initial NavigationEntry must not append a new
1296 // NavigationEntry (i.e. should not be classified as NEW_SUBFRAME). This
1297 // means every subframe navigation that happens while we're on the initial
1298 // NavigationEntry will always reuse the existing NavigationEntry and
1299 // just update the corresponding FrameNavigationEntry.
1300 DCHECK_EQ(details->type, NAVIGATION_TYPE_AUTO_SUBFRAME);
1301 } else if (details->type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY) {
1302 // This is a navigation that modifies the initial NavigationEntry, either
1303 // for a replacement or a reload. The initial NavigationEntry should
1304 // retain its "initial NavigationEntry" status in this case.
1305 details->should_stay_as_initial_entry = true;
1306 }
1307 }
1308 DCHECK(!details->should_stay_as_initial_entry ||
1309 GetLastCommittedEntry()->IsInitialEntry());
[email protected]4bf3522c2010-08-19 21:00:201310
eugenebutee08663a2017-04-27 17:43:121311 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441312 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121313
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071314 details->is_prerender_activation =
1315 navigation_request->IsPrerenderedPageActivation();
1316
Peter Boströmd7592132019-01-30 04:50:311317 // Make sure we do not discard the pending entry for a different ongoing
1318 // navigation when a same document commit comes in unexpectedly from the
1319 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1320 // other navigation types. See https://crbug.com/900036.
1321 // TODO(crbug.com/926009): Handle history.pushState() as well.
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061322 bool keep_pending_entry =
1323 is_same_document_navigation &&
1324 details->type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY &&
1325 pending_entry_ && !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311326
[email protected]0e8db942008-09-24 21:21:481327 switch (details->type) {
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061328 case NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY:
Charlie Reisc0f17d2d2021-01-12 18:52:491329 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051330 rfh, params, details->is_same_document, details->did_replace_entry,
Rakina Zata Amnia4e27222021-12-22 01:05:001331 previous_document_was_activated, navigation_request, details);
[email protected]e9ba4472008-09-14 15:42:431332 break;
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061333 case NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY:
Charlie Reisc0f17d2d2021-01-12 18:52:491334 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1335 was_restored, navigation_request,
Rakina Zata Amnia4e27222021-12-22 01:05:001336 keep_pending_entry, details);
[email protected]e9ba4472008-09-14 15:42:431337 break;
[email protected]8ff00d72012-10-23 19:12:211338 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471339 RendererDidNavigateNewSubframe(
1340 rfh, params, details->is_same_document, details->did_replace_entry,
Rakina Zata Amnia4e27222021-12-22 01:05:001341 previous_document_was_activated, navigation_request, details);
[email protected]e9ba4472008-09-14 15:42:431342 break;
[email protected]8ff00d72012-10-23 19:12:211343 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391344 if (!RendererDidNavigateAutoSubframe(
Nate Chapinc7019dd7d2021-06-25 18:29:251345 rfh, params, details->is_same_document,
Rakina Zata Amnia4e27222021-12-22 01:05:001346 was_on_initial_empty_document, navigation_request, details)) {
creisce0ef3572017-01-26 17:53:081347 // We don't send a notification about auto-subframe PageState during
1348 // UpdateStateForFrame, since it looks like nothing has changed. Send
1349 // it here at commit time instead.
1350 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431351 return false;
creis59d5a47cb2016-08-24 23:57:191352 }
[email protected]e9ba4472008-09-14 15:42:431353 break;
Aran Gilman37d11632019-10-08 23:07:151354 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431355 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151356 break;
[email protected]765b35502008-08-21 00:51:201357 }
1358
[email protected]688aa65c62012-09-28 04:32:221359 // At this point, we know that the navigation has just completed, so
1360 // record the time.
1361 //
1362 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261363 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331364 base::Time timestamp =
1365 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1366 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131367 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221368
Peter Boströmd7592132019-01-30 04:50:311369 // If we aren't keeping the pending entry, there shouldn't be one at this
1370 // point. Clear it again in case any error cases above forgot to do so.
1371 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1372 // been cleared instead of protecting against it.
1373 if (!keep_pending_entry)
Rakina Zata Amniddf10502022-01-15 02:56:551374 DiscardNonCommittedEntriesWithCommitDetails(details);
[email protected]f233e4232013-02-23 00:55:141375
[email protected]e9ba4472008-09-14 15:42:431376 // All committed entries should have nonempty content state so WebKit doesn't
1377 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471378 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321379 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221380 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441381 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531382 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1383 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281384 if (back_forward_cache_metrics &&
1385 !active_entry->back_forward_cache_metrics()) {
Alexander Timind2f2e4f22019-04-02 20:04:531386 active_entry->set_back_forward_cache_metrics(
1387 std::move(back_forward_cache_metrics));
1388 }
Dave Tapuskaa2ab4f252021-07-08 21:31:281389
1390 // `back_forward_cache_metrics()` may return null as we do not record
1391 // back-forward cache metrics for navigations in non-primary frame trees.
1392 if (active_entry->back_forward_cache_metrics()) {
1393 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
1394 navigation_request,
1395 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
1396 }
naskoc7533512016-05-06 17:01:121397
Charles Reisc0507202017-09-21 00:40:021398 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1399 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1400 // A mismatch can occur if the renderer lies or due to a unique name collision
1401 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121402 FrameNavigationEntry* frame_entry =
1403 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021404 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1405 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031406 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081407 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1408 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031409 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201410
1411 // Remember the bindings the renderer process has at this point, so that
1412 // we do not grant this entry additional bindings if we come back to it.
1413 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301414 }
[email protected]132e281a2012-07-31 18:32:441415
[email protected]97d8f0d2013-10-29 16:49:211416 // Once it is committed, we no longer need to track several pieces of state on
1417 // the entry.
naskoc7533512016-05-06 17:01:121418 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131419
[email protected]49bd30e62011-03-22 20:12:591420 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221421 // TODO(creis): This check won't pass for subframes until we create entries
1422 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001423 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421424 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591425
[email protected]e9ba4472008-09-14 15:42:431426 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001427 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001428 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311429 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531430
arthursonzogni7ddc6542021-04-09 09:16:501431 active_entry->SetIsOverridingUserAgent(
1432 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031433
[email protected]93f230e02011-06-01 14:40:001434 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291435
aelias100c9192017-01-13 00:01:431436 if (overriding_user_agent_changed)
1437 delegate_->UpdateOverridingUserAgent();
1438
creis03b48002015-11-04 00:54:561439 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1440 // one knows the latest NavigationEntry it is showing (whether it has
1441 // committed anything in this navigation or not). This allows things like
1442 // state and title updates from RenderFrames to apply to the latest relevant
1443 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381444 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001445 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381446 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
Hayato Ito2c8c08d02021-06-23 03:38:431447
1448 if (navigation_request->IsPrerenderedPageActivation()) {
1449 BroadcastHistoryOffsetAndLength();
1450 // TODO(crbug.com/1222893): Broadcasting happens after the prerendered page
1451 // is activated. As a result, a "prerenderingchange" event listener sees the
1452 // history.length which is not updated yet. We should guarantee that
1453 // history's length and offset should be updated before a
1454 // "prerenderingchange" event listener runs. One possible approach is to use
1455 // the same IPC which "prerenderingchange" uses, and propagate history's
1456 // length and offset together with that.
1457 }
1458
[email protected]e9ba4472008-09-14 15:42:431459 return true;
initial.commit09911bf2008-07-26 23:55:291460}
1461
[email protected]8ff00d72012-10-23 19:12:211462NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321463 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101464 const mojom::DidCommitProvisionalLoadParams& params,
Rakina Zata Amnia4e27222021-12-22 01:05:001465 NavigationRequest* navigation_request,
1466 LoadCommittedDetails* details) {
1467 if (GetLastCommittedEntry()->IsInitialEntry() &&
1468 navigation_request->is_synchronous_renderer_commit() &&
1469 !navigation_request->IsSameDocument() && !rfh->GetParent() &&
1470 params.should_replace_current_entry) {
Rakina Zata Amniddf10502022-01-15 02:56:551471 // This is a synchronous about:blank navigation on the main frame, which
1472 // used to not create a NavigationEntry when we have no NavigationEntry on
1473 // FrameTree creation. We now have the initial NavigationEntry and are on
1474 // the initial NavigationEntry. To preserve old behavior, we should still
1475 // keep the "initial" status for the new NavigationEntry that we will create
1476 // for this navigation, so that subframe navigations under the synchronously
1477 // committed about:blank document will never append new NavigationEntry, and
1478 // instead will just reuse the initial NavigationEntry and modify the
1479 // corresponding FrameNavigationEntries. See also https://crbug.com/1277414.
1480 details->should_stay_as_initial_entry = true;
Rakina Zata Amnia4e27222021-12-22 01:05:001481 }
1482
Piotr Tworekbad51282020-09-30 19:17:591483 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511484 "ClassifyNavigation");
Rakina Zata Amniafd3c6582021-11-30 06:19:171485 DCHECK(GetLastCommittedEntry());
Rakina Zata Amniddf10502022-01-15 02:56:551486 if (rfh->GetParent() && GetLastCommittedEntry()->IsInitialEntry()) {
Rakina Zata Amnia4e27222021-12-22 01:05:001487 // This is a subframe navigation on the initial empty document, which used
Rakina Zata Amniddf10502022-01-15 02:56:551488 // to not have a NavigationEntry to attach to. Now it can attach to the
1489 // initial NavigationEntry, and we must ensure that its NavigationEntry will
1490 // keep the "initial NavigationEntry" status and won't append a new
1491 // NavigationEntry (it should always do replacement instead).
Rakina Zata Amnia4e27222021-12-22 01:05:001492 // See also https://crbug.com/1277414.
Rakina Zata Amniddf10502022-01-15 02:56:551493 details->should_stay_as_initial_entry = true;
Rakina Zata Amnia4e27222021-12-22 01:05:001494 }
Nasko Oskovae49e292020-08-13 02:08:511495
avi7c6f35e2015-05-08 17:52:381496 if (params.did_create_new_entry) {
Rakina Zata Amniafd3c6582021-11-30 06:19:171497 // A new entry for either the main frame or a subframe.
avi39c1edd32015-06-04 20:06:001498 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491499 trace_return.set_return_reason("new entry, no parent, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061500 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381501 }
1502
Rakina Zata Amniafd3c6582021-11-30 06:19:171503 // Subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511504 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381505 return NAVIGATION_TYPE_NEW_SUBFRAME;
1506 }
1507
Charlie Reisc0f17d2d2021-01-12 18:52:491508 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381509 DCHECK(!params.history_list_was_cleared);
1510
avi39c1edd32015-06-04 20:06:001511 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381512 // All manual subframes would be did_create_new_entry and handled above, so
1513 // we know this is auto.
Rakina Zata Amniafd3c6582021-11-30 06:19:171514 trace_return.set_return_reason("subframe, last commmited, auto subframe");
1515 return NAVIGATION_TYPE_AUTO_SUBFRAME;
avi7c6f35e2015-05-08 17:52:381516 }
1517
Rakina Zata Amnif6950d552020-11-24 03:26:101518 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1519 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381520 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1521 // create a new page.
1522
Hayato Ito2ae49442021-07-02 02:59:251523 // This main frame navigation is not a history navigation (since
1524 // nav_entry_id is 0), but didn't create a new entry. So this must be a
1525 // reload or a replacement navigation, which will modify the existing entry.
1526 //
Nasko Oskov332593c2018-08-16 17:21:341527 // TODO(nasko): With error page isolation, reloading an existing session
1528 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491529 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341530 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511531 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491532 "nav entry 0, last committed, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061533 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381534 }
1535
Rakina Zata Amnif6950d552020-11-24 03:26:101536 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521537 // If the SiteInstance of the |pending_entry_| does not match the
1538 // SiteInstance that got committed, treat this as a new navigation with
1539 // replacement. This can happen if back/forward/reload encounters a server
1540 // redirect to a different site or an isolated error page gets successfully
1541 // reloaded into a different SiteInstance.
1542 if (pending_entry_->site_instance() &&
1543 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491544 trace_return.set_return_reason("pending matching nav entry, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061545 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521546 }
creis77c9aa32015-09-25 19:59:421547
Nasko Oskovaee2f862018-06-15 00:05:521548 if (pending_entry_index_ == -1) {
1549 // In this case, we have a pending entry for a load of a new URL but Blink
1550 // didn't do a new navigation (params.did_create_new_entry). First check
1551 // to make sure Blink didn't treat a new cross-process navigation as
1552 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161553 // we must treat it as NEW rather than the converted reload case below,
1554 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521555 if (!GetLastCommittedEntry() ||
1556 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161557 trace_return.set_return_reason("new pending, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061558 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521559 }
1560
1561 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161562 // We will create a pending entry, but NavigateWithoutEntry will convert
1563 // it to a reload since it's the same page and not create a new entry for
1564 // it. (The user doesn't want to have a new back/forward entry when they
1565 // do this.) Therefore we want to just ignore the pending entry and go
1566 // back to where we were (the "existing entry").
1567 trace_return.set_return_reason("new pending, existing (same) entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061568 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521569 }
avi7c6f35e2015-05-08 17:52:381570 }
1571
creis26d22632017-04-21 20:23:561572 // Everything below here is assumed to be an existing entry, but if there is
1573 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511574 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491575 trace_return.set_return_reason("no last committed, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061576 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511577 }
creis26d22632017-04-21 20:23:561578
Rakina Zata Amni153d5702021-09-13 22:48:001579 if (navigation_request->commit_params().intended_as_new_entry) {
avi7c6f35e2015-05-08 17:52:381580 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491581 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1582 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161583 trace_return.set_return_reason("intended as new entry, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061584 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381585 }
1586
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121587 if (navigation_request->DidEncounterError() &&
1588 failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101589 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381590 // If the renderer was going to a new pending entry that got cleared because
1591 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491592 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381593 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511594 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491595 "unreachable, matching pending, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061596 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381597 }
1598
Charlie Reisc0f17d2d2021-01-12 18:52:491599 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101600 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511601 trace_return.traced_value()->SetInteger("existing_entry_index",
1602 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381603 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441604 // The renderer has committed a navigation to an entry that no longer
1605 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491606 trace_return.set_return_reason("existing entry -1, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061607 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381608 }
1609
avi7c6f35e2015-05-08 17:52:381610 // Since we weeded out "new" navigations above, we know this is an existing
1611 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491612 trace_return.set_return_reason("default return, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061613 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381614}
1615
Rakina Zata Amni3460d382021-10-29 00:43:371616void NavigationControllerImpl::UpdateNavigationEntryDetails(
1617 NavigationEntryImpl* entry,
1618 RenderFrameHostImpl* rfh,
1619 const mojom::DidCommitProvisionalLoadParams& params,
1620 NavigationRequest* request,
1621 NavigationEntryImpl::UpdatePolicy update_policy,
Rakina Zata Amnia4e27222021-12-22 01:05:001622 bool is_new_entry,
1623 LoadCommittedDetails* commit_details) {
Rakina Zata Amni3460d382021-10-29 00:43:371624 // Update the FrameNavigationEntry.
Rakina Zata Amniafd3c6582021-11-30 06:19:171625 std::vector<GURL> redirects;
Rakina Zata Amni3460d382021-10-29 00:43:371626 entry->AddOrUpdateFrameEntry(
1627 rfh->frame_tree_node(), update_policy, params.item_sequence_number,
1628 params.document_sequence_number, params.app_history_key,
1629 rfh->GetSiteInstance(), nullptr, params.url,
1630 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amniafd3c6582021-11-30 06:19:171631 Referrer(*params.referrer),
1632 request ? request->common_params().initiator_origin : params.origin,
1633 request ? request->GetRedirectChain() : redirects, params.page_state,
1634 params.method, params.post_id, nullptr /* blob_url_loader_factory */,
1635 (request && request->web_bundle_navigation_info())
Rakina Zata Amni3460d382021-10-29 00:43:371636 ? request->web_bundle_navigation_info()->Clone()
1637 : nullptr,
Rakina Zata Amniafd3c6582021-11-30 06:19:171638 (request ? request->GetSubresourceWebBundleNavigationInfo() : nullptr),
Rakina Zata Amni3460d382021-10-29 00:43:371639 ComputePolicyContainerPoliciesForFrameEntry(
Rakina Zata Amniafd3c6582021-11-30 06:19:171640 rfh, request && request->IsSameDocument(),
1641 request ? request->common_params().url : params.url));
Rakina Zata Amni3460d382021-10-29 00:43:371642
1643 if (rfh->GetParent()) {
1644 // Only modify the NavigationEntry for main frame navigations.
1645 return;
1646 }
1647 if (entry->update_virtual_url_with_url())
1648 UpdateVirtualURLToURL(entry, params.url);
1649 // Don't use the page type from the pending entry. Some interstitial page
1650 // may have set the type to interstitial. Once we commit, however, the page
1651 // type must always be normal or error.
Rakina Zata Amniafd3c6582021-11-30 06:19:171652 entry->set_page_type((request && request->DidEncounterError())
1653 ? PAGE_TYPE_ERROR
1654 : PAGE_TYPE_NORMAL);
Rakina Zata Amnif297a802022-01-18 03:53:431655 if (commit_details && commit_details->should_stay_as_initial_entry) {
1656 // Retain the "initial NavigationEntry" status.
1657 if (request->IsSameDocument()) {
1658 // If this is for a same-document navigation, the NavigationEntry must be
1659 // reused and should already be marked as the initial NavigationEntry.
1660 DCHECK(entry->IsInitialEntry());
1661 } else {
1662 // If this is for a cross-document navigation, it can be caused by a
1663 // renderer-initiated reload, or the synchronous about:blank commit. Mark
1664 // "for synchronous about:blank" in the latter case, and also when it is
1665 // reloading a "for synchronous about:blank" entry. Otherwise, the entry
1666 // is not for a synchronous about:blank commit.
1667 NavigationEntryImpl::InitialNavigationEntryState new_state =
1668 NavigationEntryImpl::InitialNavigationEntryState::
1669 kInitialNotForSynchronousAboutBlank;
1670 if (entry->IsInitialEntryForSynchronousAboutBlank() ||
1671 request->is_synchronous_renderer_commit()) {
1672 new_state = NavigationEntryImpl::InitialNavigationEntryState::
1673 kInitialForSynchronousAboutBlank;
1674 }
1675 entry->set_initial_navigation_entry_state(new_state);
1676 }
1677 } else if (commit_details && !commit_details->should_stay_as_initial_entry) {
1678 // Remove the "initial NavigationEntry" status.
1679 entry->set_initial_navigation_entry_state(
1680 NavigationEntryImpl::InitialNavigationEntryState::kNonInitial);
Rakina Zata Amnia4e27222021-12-22 01:05:001681 }
Rakina Zata Amniddf10502022-01-15 02:56:551682
Rakina Zata Amni3460d382021-10-29 00:43:371683 if (is_new_entry) {
1684 // Some properties of the NavigationEntry are only set when the entry is
1685 // new (we aren't reusing it).
1686 entry->SetTransitionType(params.transition);
Rakina Zata Amniafd3c6582021-11-30 06:19:171687 entry->SetOriginalRequestURL(request ? request->GetOriginalRequestURL()
1688 : GURL::EmptyGURL());
Rakina Zata Amni3460d382021-10-29 00:43:371689 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
1690 entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
1691 } else {
1692 // We are reusing the NavigationEntry. The site instance will normally be
1693 // the same except for a few cases:
1694 // 1) session restore, when no site instance will be assigned or
1695 // 2) redirect, when the site instance is reset.
1696 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
1697 entry->site_instance() == rfh->GetSiteInstance());
1698 }
1699}
1700
Rakina Zata Amniafd3c6582021-11-30 06:19:171701void NavigationControllerImpl::CreateInitialEntry() {
1702 DCHECK_EQ(entries_.size(), 0u);
1703 RenderFrameHostImpl* rfh = frame_tree_.root()->current_frame_host();
1704 auto params = mojom::DidCommitProvisionalLoadParams::New();
1705 // The initial NavigationEntry's URL is the empty URL. This preserves the old
1706 // behavior of WebContent's GetLastCommittedURL() and GetVisibleURL() from
1707 // before we have initial NavigationEntries.
1708 params->url = GURL::EmptyGURL();
1709 params->http_status_code = 0;
1710 params->url_is_unreachable = false;
1711 params->method = "GET";
1712 params->should_replace_current_entry = false;
1713 params->post_id = -1;
1714 params->embedding_token = base::UnguessableToken::Create();
1715 params->navigation_token = base::UnguessableToken::Create();
1716 params->did_create_new_entry = true;
1717 params->origin = rfh->GetLastCommittedOrigin();
1718 params->should_update_history = true;
1719 params->item_sequence_number = 0;
1720 params->document_sequence_number = 0;
1721 bool is_in_fenced_frame_tree = rfh->frame_tree_node()->IsInFencedFrameTree();
1722 params->transition = is_in_fenced_frame_tree
1723 ? ui::PAGE_TRANSITION_AUTO_SUBFRAME
1724 : ui::PAGE_TRANSITION_LINK;
1725 params->referrer = blink::mojom::Referrer::New();
1726
1727 // Create and insert the initial NavigationEntry.
1728 auto new_entry = std::make_unique<NavigationEntryImpl>(
1729 rfh->GetSiteInstance(), params->url, Referrer(*params->referrer),
1730 rfh->GetLastCommittedOrigin(), std::u16string() /* title */,
1731 ui::PAGE_TRANSITION_TYPED, false /* renderer_initiated */,
1732 nullptr /* blob_url_loader_factory */, true /* is_initial_entry */);
1733 UpdateNavigationEntryDetails(
1734 new_entry.get(), rfh, *params, nullptr /* request */,
Rakina Zata Amnia4e27222021-12-22 01:05:001735 NavigationEntryImpl::UpdatePolicy::kUpdate, true /* is_new_entry */,
1736 nullptr /* commit_details */);
Rakina Zata Amniafd3c6582021-11-30 06:19:171737
1738 InsertOrReplaceEntry(std::move(new_entry), false /* replace_entry */,
1739 false /* was_post_commit_error */,
Rakina Zata Amnia4e27222021-12-22 01:05:001740 is_in_fenced_frame_tree, nullptr /* commit_details */);
Rakina Zata Amniafd3c6582021-11-30 06:19:171741}
1742
Charlie Reisc0f17d2d2021-01-12 18:52:491743void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321744 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071745 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361746 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441747 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471748 bool previous_document_was_activated,
Rakina Zata Amnia4e27222021-12-22 01:05:001749 NavigationRequest* request,
1750 LoadCommittedDetails* commit_details) {
dcheng9bfa5162016-04-09 01:00:571751 std::unique_ptr<NavigationEntryImpl> new_entry;
Anton Bikineevf62d1bf2021-05-15 17:56:071752 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451753 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061754
creisf49dfc92016-07-26 17:05:181755 // First check if this is an in-page navigation. If so, clone the current
1756 // entry instead of looking at the pending entry, because the pending entry
1757 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361758 if (is_same_document && GetLastCommittedEntry()) {
Nate Chapin63db0d12022-01-20 22:03:301759 FrameNavigationEntry* previous_frame_entry =
1760 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
Patrick Monette50e8bd82019-06-13 22:40:451761 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481762 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081763 params.document_sequence_number, params.app_history_key,
1764 rfh->GetSiteInstance(), nullptr, params.url,
1765 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091766 Referrer(*params.referrer), initiator_origin,
1767 request->GetRedirectChain(), params.page_state, params.method,
1768 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391769 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481770 request->GetSubresourceWebBundleNavigationInfo(),
Antonio Sartori78a749f2020-11-30 12:03:391771 // We will set the document policies later in this function.
Nate Chapin63db0d12022-01-20 22:03:301772 nullptr /* policy_container_policies */,
1773 // Try to preserve protect_url_in_app_history from the previous
1774 // FrameNavigationEntry.
1775 previous_frame_entry &&
1776 previous_frame_entry->protect_url_in_app_history());
Charles Reisf44482022017-10-13 21:15:031777
creisf49dfc92016-07-26 17:05:181778 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Harkiran Bolaria081c5c22021-09-13 08:44:531779 frame_entry, true, request->frame_tree_node(), frame_tree_.root());
Mike West800532c2021-10-14 09:26:521780 if (new_entry->GetURL().DeprecatedGetOriginAsURL() !=
1781 params.url.DeprecatedGetOriginAsURL()) {
jama78746e2017-02-22 17:21:571782 // TODO(jam): we had one report of this with a URL that was redirecting to
1783 // only tildes. Until we understand that better, don't copy the cert in
1784 // this case.
1785 new_entry->GetSSL() = SSLStatus();
jama78746e2017-02-22 17:21:571786 }
creisf49dfc92016-07-26 17:05:181787
Patrick Monette50e8bd82019-06-13 22:40:451788 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1789 // that |frame_entry| should now have a reference count of exactly 2: one
1790 // due to the local variable |frame_entry|, and another due to |new_entry|
1791 // also retaining one. This should never fail, because it's the main frame.
1792 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181793 }
1794
Harkiran Bolaria59290d62021-03-17 01:53:011795 // If this is an activation navigation from a prerendered page, transfer the
1796 // new entry from an entry already created and stored in the
1797 // NavigationRequest. |new_entry| will not have been set prior to this as
1798 // |is_same_document| is mutually exclusive with
1799 // |IsPrerenderedPageActivation|.
1800 if (request->IsPrerenderedPageActivation()) {
1801 DCHECK(!is_same_document);
1802 DCHECK(!new_entry);
1803 new_entry = request->TakePrerenderNavigationEntry();
1804 DCHECK(new_entry);
1805 }
1806
Charlie Reisc0f17d2d2021-01-12 18:52:491807 // Only make a copy of the pending entry if it is appropriate for the new
1808 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451809 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041810 // 1. The SiteInstance hasn't been assigned to something else.
1811 // 2. The pending entry was intended as a new entry, rather than being a
1812 // history navigation that was interrupted by an unrelated,
1813 // renderer-initiated navigation.
1814 // TODO(csharrison): Investigate whether we can remove some of the coarser
1815 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451816 if (!new_entry && PendingEntryMatchesRequest(request) &&
1817 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341818 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431819 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351820 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431821
Camille Lamy62b826012019-02-26 09:15:471822 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451823 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
creisf49dfc92016-07-26 17:05:181824 }
1825
Charlie Reisc0f17d2d2021-01-12 18:52:491826 // For cross-document commits with no matching pending entry, create a new
1827 // entry.
creisf49dfc92016-07-26 17:05:181828 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061829 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071830 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1831 initiator_origin,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:581832 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451833 params.transition, request->IsRendererInitiated(),
Rakina Zata Amniafd3c6582021-11-30 06:19:171834 nullptr, // blob_url_loader_factory
1835 false); // is_initial_entry
[email protected]f8f93eb2012-09-25 03:06:241836
1837 // Find out whether the new entry needs to update its virtual URL on URL
1838 // change and set up the entry accordingly. This is needed to correctly
1839 // update the virtual URL when replaceState is called after a pushState.
1840 GURL url = params.url;
1841 bool needs_update = false;
Charlie Reisc0f17d2d2021-01-12 18:52:491842 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411843 // update the virtual URL based on the new URL. For example, this is needed
1844 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1845 // the URL.
Rakina Zata Amni3460d382021-10-29 00:43:371846 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1847 &url, browser_context_, &needs_update);
1848 new_entry->set_update_virtual_url_with_url(needs_update);
1849
Camille Lamy62b826012019-02-26 09:15:471850 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451851 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
[email protected]e9ba4472008-09-14 15:42:431852 }
1853
Harkiran Bolaria59290d62021-03-17 01:53:011854 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
1855 // for prerendered contents, if any. This is because prerendering/activation
1856 // technically won't be creating a new document. Unlike BFCache, prerendering
1857 // creates a new NavigationEntry rather than using an existing one.
1858 if (!request->IsPrerenderedPageActivation()) {
Rakina Zata Amni3460d382021-10-29 00:43:371859 UpdateNavigationEntryDetails(new_entry.get(), rfh, params, request,
1860 NavigationEntryImpl::UpdatePolicy::kUpdate,
Rakina Zata Amnia4e27222021-12-22 01:05:001861 true /* is_new_entry */, commit_details);
creis8b5cd4c2015-06-19 00:11:081862
Harkiran Bolaria59290d62021-03-17 01:53:011863 // history.pushState() is classified as a navigation to a new page, but sets
1864 // is_same_document to true. In this case, we already have the title and
1865 // favicon available, so set them immediately.
1866 if (is_same_document && GetLastCommittedEntry()) {
1867 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
1868 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1869 }
[email protected]3a868f212014-08-09 10:41:191870 }
[email protected]ff64b3e2014-05-31 04:07:331871
[email protected]60d6cca2013-04-30 08:47:131872 DCHECK(!params.history_list_was_cleared || !replace_entry);
1873 // The browser requested to clear the session history when it initiated the
1874 // navigation. Now we know that the renderer has updated its state accordingly
1875 // and it is safe to also clear the browser side history.
1876 if (params.history_list_was_cleared) {
Rakina Zata Amniddf10502022-01-15 02:56:551877 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]60d6cca2013-04-30 08:47:131878 entries_.clear();
1879 last_committed_entry_index_ = -1;
1880 }
1881
Nasko Oskovaee2f862018-06-15 00:05:521882 // If this is a new navigation with replacement and there is a
1883 // pending_entry_ which matches the navigation reported by the renderer
1884 // process, then it should be the one replaced, so update the
1885 // last_committed_entry_index_ to use it.
1886 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101887 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521888 last_committed_entry_index_ = pending_entry_index_;
1889 }
1890
Alexander Timine3ec4192020-04-20 16:39:401891 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411892 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401893 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051894
Carlos IL42b416592019-10-07 23:10:361895 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
Dave Tapuska87696ae2021-11-18 18:48:311896 !request->post_commit_error_page_html().empty(),
Rakina Zata Amnia4e27222021-12-22 01:05:001897 rfh->frame_tree_node()->IsInFencedFrameTree(),
1898 commit_details);
[email protected]e9ba4472008-09-14 15:42:431899}
1900
Charlie Reisc0f17d2d2021-01-12 18:52:491901void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321902 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071903 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361904 bool is_same_document,
jam48cea9082017-02-15 06:13:291905 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451906 NavigationRequest* request,
Rakina Zata Amnia4e27222021-12-22 01:05:001907 bool keep_pending_entry,
1908 LoadCommittedDetails* commit_details) {
creis26d22632017-04-21 20:23:561909 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1910 << "that a last committed entry exists.";
1911
[email protected]e9ba4472008-09-14 15:42:431912 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001913 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431914
Charlie Reis7e2cb6d2021-01-26 01:27:161915 NavigationEntryImpl* entry = nullptr;
Rakina Zata Amni153d5702021-09-13 22:48:001916 if (request->commit_params().intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:161917 // We're guaranteed to have a last committed entry if intended_as_new_entry
1918 // is true.
avicbdc4c12015-07-01 16:07:111919 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:161920 DCHECK(entry);
1921
1922 // If the NavigationRequest matches a new pending entry and is classified as
1923 // EXISTING_ENTRY, then it is a navigation to the same URL that was
1924 // converted to a reload, such as a user pressing enter in the omnibox.
1925 if (pending_entry_ && pending_entry_index_ == -1 &&
1926 pending_entry_->GetUniqueID() ==
1927 request->commit_params().nav_entry_id) {
1928 // Note: The pending entry will usually have a real ReloadType here, but
1929 // it can still be ReloadType::NONE in cases that
1930 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
1931
1932 // If we classified this correctly, the SiteInstance should not have
1933 // changed.
1934 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
1935
1936 // For converted reloads, we assign the entry's unique ID to be that of
1937 // the new one. Since this is always the result of a user action, we want
1938 // to dismiss infobars, etc. like a regular user-initiated navigation.
1939 entry->set_unique_id(pending_entry_->GetUniqueID());
1940
1941 // The extra headers may have changed due to reloading with different
1942 // headers.
1943 entry->set_extra_headers(pending_entry_->extra_headers());
1944 }
1945 // Otherwise, this was intended as a new entry but the pending entry was
1946 // lost in the meantime and no new entry was created. We are stuck at the
1947 // last committed entry.
1948
1949 // Even if this is a converted reload from pressing enter in the omnibox,
1950 // the server could redirect, requiring an update to the SSL status. If this
1951 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451952 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:161953 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:471954 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451955 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:161956 }
Rakina Zata Amnif6950d552020-11-24 03:26:101957 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:111958 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:101959 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b902016-09-01 16:58:161960
eugenebut604866f2017-05-10 21:35:361961 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451962 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361963 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1964 // this was a restored same document navigation entry, then it won't have
1965 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1966 // navigated it.
jam48cea9082017-02-15 06:13:291967 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
Mike West800532c2021-10-14 09:26:521968 if (entry->GetURL().DeprecatedGetOriginAsURL() ==
1969 last_entry->GetURL().DeprecatedGetOriginAsURL() &&
jam48cea9082017-02-15 06:13:291970 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1971 was_restored) {
1972 entry->GetSSL() = last_entry->GetSSL();
1973 }
1974 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451975 // In rapid back/forward navigations |request| sometimes won't have a cert
1976 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191977 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451978 if (request->GetSSLInfo().has_value() &&
1979 request->GetSSLInfo()->is_valid()) {
1980 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
Mike West800532c2021-10-14 09:26:521981 } else if (entry->GetURL().DeprecatedGetOriginAsURL() !=
1982 request->GetURL().DeprecatedGetOriginAsURL()) {
John Abd-El-Malek3f247082017-12-07 19:02:191983 entry->GetSSL() = SSLStatus();
1984 }
jam48cea9082017-02-15 06:13:291985 }
avicbdc4c12015-07-01 16:07:111986 } else {
1987 // This is renderer-initiated. The only kinds of renderer-initated
Rakina Zata Amni557afb92021-07-17 04:39:571988 // navigations that are EXISTING_ENTRY are same-document navigations that
1989 // result in replacement (e.g. history.replaceState(), location.replace(),
1990 // forced replacements for trivial session history contexts). For these
1991 // cases, we reuse the last committed entry.
avicbdc4c12015-07-01 16:07:111992 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101993
Mikel Astizba9cf2fd2017-12-17 10:38:101994 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1995 // to avoid misleading interpretations (e.g. URLs paired with
1996 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1997 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1998 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1999
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572000 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:102001
eugenebut604866f2017-05-10 21:35:362002 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:452003 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:362004 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:472005 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:452006 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
avicbdc4c12015-07-01 16:07:112007 }
2008 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:432009
Rakina Zata Amni3460d382021-10-29 00:43:372010 UpdateNavigationEntryDetails(entry, rfh, params, request,
2011 NavigationEntryImpl::UpdatePolicy::kUpdate,
Rakina Zata Amnia4e27222021-12-22 01:05:002012 false /* is_new_entry */, commit_details);
creis22a7b4c2016-04-28 07:20:302013
[email protected]5ccd4dc2012-10-24 02:28:142014 // The redirected to page should not inherit the favicon from the previous
2015 // page.
eugenebut604866f2017-05-10 21:35:362016 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:482017 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:142018
Peter Boströmd7592132019-01-30 04:50:312019 // We should also usually discard the pending entry if it corresponds to a
2020 // different navigation, since that one is now likely canceled. In rare
2021 // cases, we leave the pending entry for another navigation in place when we
2022 // know it is still ongoing, to avoid a flicker in the omnibox (see
2023 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:432024 //
2025 // Note that we need to use the "internal" version since we don't want to
2026 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:312027 if (!keep_pending_entry)
Rakina Zata Amnia4e27222021-12-22 01:05:002028 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]40bcc302009-03-02 20:50:392029
Carlos IL4dea8902020-05-26 15:14:292030 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:112031 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:432032}
2033
[email protected]d202a7c2012-01-04 07:53:472034void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:322035 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072036 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:362037 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:472038 bool replace_entry,
2039 bool previous_document_was_activated,
Rakina Zata Amnia4e27222021-12-22 01:05:002040 NavigationRequest* request,
2041 LoadCommittedDetails* commit_details) {
avi25f5f9e2015-07-17 20:08:262042 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2043 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
Rakina Zata Amniddf10502022-01-15 02:56:552044 // The NEW_SUBFRAME path should never result in an initial NavigationEntry.
2045 DCHECK(!commit_details->should_stay_as_initial_entry);
[email protected]09b8f82f2009-06-16 20:22:112046
[email protected]e9ba4472008-09-14 15:42:432047 // Manual subframe navigations just get the current entry cloned so the user
2048 // can go back or forward to it. The actual subframe information will be
2049 // stored in the page state for each of those entries. This happens out of
2050 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:092051 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
2052 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:382053
Mikel Astizba9cf2fd2017-12-17 10:38:102054 // The DCHECK below documents the fact that we don't know of any situation
2055 // where |replace_entry| is true for subframe navigations. This simplifies
2056 // reasoning about the replacement struct for subframes (see
2057 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
2058 DCHECK(!replace_entry);
2059
Patrick Monette50e8bd82019-06-13 22:40:452060 // This FrameNavigationEntry might not end up being used in the
2061 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Anton Bikineevf62d1bf2021-05-15 17:56:072062 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452063 request->common_params().initiator_origin;
Nate Chapin63db0d12022-01-20 22:03:302064 std::unique_ptr<PolicyContainerPolicies> policy_container_policies =
2065 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
2066 request->GetURL());
2067 bool protect_url_in_app_history = false;
2068 if (is_same_document) {
2069 FrameNavigationEntry* previous_frame_entry =
2070 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
2071 // Try to preserve protect_url_in_app_history from the previous
2072 // FrameNavigationEntry.
2073 protect_url_in_app_history =
2074 previous_frame_entry &&
2075 previous_frame_entry->protect_url_in_app_history();
2076 } else {
2077 protect_url_in_app_history =
2078 policy_container_policies &&
2079 ShouldProtectUrlInAppHistory(
2080 policy_container_policies->referrer_policy);
2081 }
2082
Patrick Monette50e8bd82019-06-13 22:40:452083 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:482084 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:082085 params.document_sequence_number, params.app_history_key,
2086 rfh->GetSiteInstance(), nullptr, params.url,
2087 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:092088 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072089 params.page_state, params.method, params.post_id,
2090 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032091 request->web_bundle_navigation_info()
2092 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392093 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482094 request->GetSubresourceWebBundleNavigationInfo(),
Nate Chapin63db0d12022-01-20 22:03:302095 std::move(policy_container_policies), protect_url_in_app_history);
Charles Reisf44482022017-10-13 21:15:032096
creisce0ef3572017-01-26 17:53:082097 std::unique_ptr<NavigationEntryImpl> new_entry =
2098 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:452099 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:002100 frame_tree_.root());
creise062d542015-08-25 02:01:552101
Alexander Timine3ec4192020-04-20 16:39:402102 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:412103 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:402104 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:472105
creisce0ef3572017-01-26 17:53:082106 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:452107 // to replace, which can happen due to a unique name change. See
2108 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
2109 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:382110
Dave Tapuska87696ae2021-11-18 18:48:312111 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false,
Rakina Zata Amnia4e27222021-12-22 01:05:002112 rfh->frame_tree_node()->IsInFencedFrameTree(),
2113 commit_details);
[email protected]e9ba4472008-09-14 15:42:432114}
2115
[email protected]d202a7c2012-01-04 07:53:472116bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:322117 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072118 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392119 bool is_same_document,
Nate Chapinc7019dd7d2021-06-25 18:29:252120 bool was_on_initial_empty_document,
Rakina Zata Amnia4e27222021-12-22 01:05:002121 NavigationRequest* request,
2122 LoadCommittedDetails* commit_details) {
avi9f07a0c2015-02-18 22:51:292123 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2124 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2125
[email protected]e9ba4472008-09-14 15:42:432126 // We're guaranteed to have a previously committed entry, and we now need to
2127 // handle navigation inside of a subframe in it without creating a new entry.
2128 DCHECK(GetLastCommittedEntry());
2129
creis913c63ce2016-07-16 19:52:522130 // For newly created subframes, we don't need to send a commit notification.
2131 // This is only necessary for history navigations in subframes.
2132 bool send_commit_notification = false;
2133
Rakina Zata Amnif6950d552020-11-24 03:26:102134 // If |nav_entry_id| is non-zero and matches an existing entry, this
2135 // is a history navigation. Update the last committed index accordingly. If
2136 // we don't recognize the |nav_entry_id|, it might be a recently
2137 // pruned entry. We'll handle it below.
2138 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2139 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472140 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062141 // Make sure that a subframe commit isn't changing the main frame's
2142 // origin. Otherwise the renderer process may be confused, leading to a
2143 // URL spoof. We can't check the path since that may change
2144 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572145 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2146 // about:blank, file, and unique origins are more subtle to get right.
Charlie Reis95fbca442021-05-21 21:38:242147 // We should use checks similar to RenderFrameHostImpl's
2148 // CanCommitUrlAndOrigin on the main frame during subframe commits.
2149 // See https://crbug.com/1209092.
creis37988b92016-06-10 18:03:572150 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2151 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2152 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2153 dest_top_url.SchemeIsHTTPOrHTTPS() &&
Mike West800532c2021-10-14 09:26:522154 current_top_url.DeprecatedGetOriginAsURL() !=
2155 dest_top_url.DeprecatedGetOriginAsURL()) {
creisfb6eeb62016-05-10 19:01:512156 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2157 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062158 }
creis3cdc3b02015-05-29 23:00:472159
creis913c63ce2016-07-16 19:52:522160 // We only need to discard the pending entry in this history navigation
2161 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062162 last_committed_entry_index_ = entry_index;
Rakina Zata Amnia4e27222021-12-22 01:05:002163 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
creis913c63ce2016-07-16 19:52:522164
2165 // History navigations should send a commit notification.
2166 send_commit_notification = true;
avi98405c22015-05-21 20:47:062167 }
[email protected]e9ba4472008-09-14 15:42:432168 }
[email protected]f233e4232013-02-23 00:55:142169
creisce0ef3572017-01-26 17:53:082170 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2171 // it may be a "history auto" case where we update an existing one.
2172 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Nate Chapin9f169072021-06-09 19:32:372173
2174 // We may want to update |last_committed|'s FrameNavigationEntry (if one
2175 // exists), or we may want to clobber it and create a new one. We update in
2176 // cases where the corresponding FrameNavigationEntry is conceptually similar
2177 // to the document described by the commit params: same-document
2178 // navigations, history traversal to an existing entry, and reloads (including
2179 // a "soft reload" where we navigate to the same url without flagging it as a
2180 // reload). But in the case of a different document that is not logically
2181 // related to the committed FrameNavigationEntry's document (cross-document,
2182 // not same url, not a reload, not a history traversal), we replace rather
2183 // than update.
Nate Chapinc7019dd7d2021-06-25 18:29:252184 //
Nate Chapin9f169072021-06-09 19:32:372185 // In the case where we update, the FrameNavigationEntry will potentially be
2186 // shared across multiple NavigationEntries, and any updates will be reflected
2187 // in all of those NavigationEntries. In the replace case, any existing
2188 // sharing with other NavigationEntries will stop.
Nate Chapinc7019dd7d2021-06-25 18:29:252189 //
2190 // When navigating away from the initial empty document, we also update rather
2191 // than replace. Either update or replace will overwrite the initial empty
2192 // document state for |last_committed|, but if the FrameNavigationEntry for
2193 // the initial empty document is shared across multiple NavigationEntries (due
2194 // to a navigation in another frame), we want to make sure we overwrite the
2195 // initial empty document state everywhere this FrameNavigationEntry is used,
2196 // which is accompished by updating the existing FrameNavigationEntry.
Nate Chapin9f169072021-06-09 19:32:372197 FrameNavigationEntry* last_committed_frame_entry =
2198 last_committed->GetFrameEntry(rfh->frame_tree_node());
2199 NavigationEntryImpl::UpdatePolicy update_policy =
2200 NavigationEntryImpl::UpdatePolicy::kUpdate;
2201 if (request->common_params().navigation_type ==
Minggang Wangb9f3fa92021-07-01 15:30:312202 blink::mojom::NavigationType::DIFFERENT_DOCUMENT &&
Nate Chapin9f169072021-06-09 19:32:372203 last_committed_frame_entry &&
Nate Chapinc7019dd7d2021-06-25 18:29:252204 last_committed_frame_entry->url() != params.url &&
2205 !was_on_initial_empty_document) {
Nate Chapin9f169072021-06-09 19:32:372206 update_policy = NavigationEntryImpl::UpdatePolicy::kReplace;
2207 }
2208
Rakina Zata Amni3460d382021-10-29 00:43:372209 UpdateNavigationEntryDetails(last_committed, rfh, params, request,
Rakina Zata Amnia4e27222021-12-22 01:05:002210 update_policy, false /* is_new_entry */,
2211 commit_details);
creis625a0c7d2015-03-24 23:17:122212
creis913c63ce2016-07-16 19:52:522213 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432214}
2215
[email protected]d202a7c2012-01-04 07:53:472216int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232217 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032218 for (size_t i = 0; i < entries_.size(); ++i) {
2219 if (entries_[i].get() == entry)
2220 return i;
2221 }
2222 return -1;
[email protected]765b35502008-08-21 00:51:202223}
2224
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572225void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242226 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572227 NavigationControllerImpl* source =
2228 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572229 // Verify that we look new.
Rakina Zata Amniafd3c6582021-11-30 06:19:172230 DCHECK_EQ(1, GetEntryCount());
2231 DCHECK(GetLastCommittedEntry()->IsInitialEntry());
Lei Zhang96031532019-10-10 19:05:472232 DCHECK(!GetPendingEntry());
Rakina Zata Amniafd3c6582021-11-30 06:19:172233 entries_.clear();
[email protected]ce3fa3c2009-04-20 19:55:572234
Rakina Zata Amniafd3c6582021-11-30 06:19:172235 if (source->GetEntryCount() == 0) {
2236 NOTREACHED();
2237 return;
2238 }
[email protected]ce3fa3c2009-04-20 19:55:572239
Francois Dorayeaace782017-06-21 16:37:242240 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442241 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572242 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572243
Fergal Dalya1d569972021-03-16 03:24:532244 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302245 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532246 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2247 session_storage_namespace_map_[it.first] = source_namespace->Clone();
Aaron Colwellb731a0ae2021-03-19 19:14:472248 OnStoragePartitionIdAdded(it.first);
[email protected]fdac6ade2013-07-20 01:06:302249 }
[email protected]4e6419c2010-01-15 04:50:342250
Lukasz Anforowicz0de0f452020-12-02 19:57:152251 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572252}
2253
Aran Gilman37d11632019-10-08 23:07:152254void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2255 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162256 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012257 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162258
[email protected]d202a7c2012-01-04 07:53:472259 NavigationControllerImpl* source =
2260 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252261
avi2b177592014-12-10 02:08:022262 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012263 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252264
[email protected]474f8512013-05-31 22:31:162265 // We now have one entry, possibly with a new pending entry. Ensure that
2266 // adding the entries from source won't put us over the limit.
2267 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572268 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572269 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252270
Carlos IL4dea8902020-05-26 15:14:292271 // Insert the entries from source. Ignore any pending entry, since it has not
2272 // committed in source.
[email protected]474f8512013-05-31 22:31:162273 int max_source_index = source->last_committed_entry_index_;
Rakina Zata Amniafd3c6582021-11-30 06:19:172274 DCHECK_NE(max_source_index, -1);
2275 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572276
2277 // Ignore the source's current entry if merging with replacement.
2278 // TODO(davidben): This should preserve entries forward of the current
2279 // too. http://crbug.com/317872
2280 if (replace_entry && max_source_index > 0)
2281 max_source_index--;
2282
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572283 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252284
2285 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552286 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142287
Hayato Ito2c8c08d02021-06-23 03:38:432288 BroadcastHistoryOffsetAndLength();
[email protected]e1cd5452010-08-26 18:03:252289}
2290
[email protected]79368982013-11-13 01:11:012291bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162292 // If there is no last committed entry, we cannot prune. Even if there is a
2293 // pending entry, it may not commit, leaving this WebContents blank, despite
2294 // possibly giving it new entries via CopyStateFromAndPrune.
2295 if (last_committed_entry_index_ == -1)
2296 return false;
[email protected]9350602e2013-02-26 23:27:442297
[email protected]474f8512013-05-31 22:31:162298 // We cannot prune if there is a pending entry at an existing entry index.
2299 // It may not commit, so we have to keep the last committed entry, and thus
2300 // there is no sensible place to keep the pending entry. It is ok to have
2301 // a new pending entry, which can optionally commit as a new navigation.
2302 if (pending_entry_index_ != -1)
2303 return false;
2304
[email protected]474f8512013-05-31 22:31:162305 return true;
2306}
2307
[email protected]79368982013-11-13 01:11:012308void NavigationControllerImpl::PruneAllButLastCommitted() {
2309 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162310
avi2b177592014-12-10 02:08:022311 DCHECK_EQ(0, last_committed_entry_index_);
2312 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442313
Hayato Ito2c8c08d02021-06-23 03:38:432314 BroadcastHistoryOffsetAndLength();
[email protected]9350602e2013-02-26 23:27:442315}
2316
[email protected]79368982013-11-13 01:11:012317void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162318 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012319 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262320
[email protected]474f8512013-05-31 22:31:162321 // Erase all entries but the last committed entry. There may still be a
2322 // new pending entry after this.
2323 entries_.erase(entries_.begin(),
2324 entries_.begin() + last_committed_entry_index_);
2325 entries_.erase(entries_.begin() + 1, entries_.end());
2326 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262327}
2328
Christian Dullweber1af31e62018-02-22 11:49:482329void NavigationControllerImpl::DeleteNavigationEntries(
2330 const DeletionPredicate& deletionPredicate) {
2331 // It is up to callers to check the invariants before calling this.
2332 CHECK(CanPruneAllButLastCommitted());
2333 std::vector<int> delete_indices;
2334 for (size_t i = 0; i < entries_.size(); i++) {
2335 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572336 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482337 delete_indices.push_back(i);
2338 }
2339 }
2340 if (delete_indices.empty())
2341 return;
2342
2343 if (delete_indices.size() == GetEntryCount() - 1U) {
2344 PruneAllButLastCommitted();
2345 } else {
2346 // Do the deletion in reverse to preserve indices.
2347 for (auto it = delete_indices.rbegin(); it != delete_indices.rend(); ++it) {
2348 RemoveEntryAtIndex(*it);
2349 }
Hayato Ito2c8c08d02021-06-23 03:38:432350 BroadcastHistoryOffsetAndLength();
Christian Dullweber1af31e62018-02-22 11:49:482351 }
2352 delegate()->NotifyNavigationEntriesDeleted();
2353}
2354
Shivani Sharma883f5f32019-02-12 18:20:012355bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2356 auto* entry = GetEntryAtIndex(index);
2357 return entry && entry->should_skip_on_back_forward_ui();
2358}
2359
Carlos Caballero35ce710c2019-09-19 10:59:452360BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2361 return back_forward_cache_;
2362}
2363
clamy987a3752018-05-03 17:36:262364void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2365 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2366 // progress, since this will cause a use-after-free. (We only allow this
2367 // when the tab is being destroyed for shutdown, since it won't return to
2368 // NavigateToEntry in that case.) http://crbug.com/347742.
Takashi Toyoshimaea534ef22021-07-21 03:27:592369 CHECK(!in_navigate_to_pending_entry_ || frame_tree_.IsBeingDestroyed());
clamy987a3752018-05-03 17:36:262370
2371 if (was_failure && pending_entry_) {
2372 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2373 } else {
2374 failed_pending_entry_id_ = 0;
2375 }
2376
2377 if (pending_entry_) {
2378 if (pending_entry_index_ == -1)
2379 delete pending_entry_;
2380 pending_entry_index_ = -1;
2381 pending_entry_ = nullptr;
2382 }
arthursonzogni66f711c2019-10-08 14:40:362383
2384 // Ensure any refs to the current pending entry are ignored if they get
2385 // deleted, by clearing the set of known refs. All future pending entries will
2386 // only be affected by new refs.
2387 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262388}
2389
2390void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2391 if (pending_entry_)
2392 pending_entry_->set_ssl_error(error);
2393}
2394
Xiaohan Wang7f8052e02022-01-14 18:44:282395#if BUILDFLAG(IS_ANDROID)
Camille Lamy5193caa2018-10-12 11:59:422396// static
2397bool NavigationControllerImpl::ValidateDataURLAsString(
2398 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2399 if (!data_url_as_string)
2400 return false;
2401
2402 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2403 return false;
2404
2405 // The number of characters that is enough for validating a data: URI.
2406 // From the GURL's POV, the only important part here is scheme, it doesn't
2407 // check the actual content. Thus we can take only the prefix of the url, to
2408 // avoid unneeded copying of a potentially long string.
2409 const size_t kDataUriPrefixMaxLen = 64;
2410 GURL data_url(
2411 std::string(data_url_as_string->front_as<char>(),
2412 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2413 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2414 return false;
2415
2416 return true;
2417}
2418#endif
2419
Shivani Sharma194877032019-03-07 17:52:472420void NavigationControllerImpl::NotifyUserActivation() {
2421 // When a user activation occurs, ensure that all adjacent entries for the
2422 // same document clear their skippable bit, so that the history manipulation
2423 // intervention does not apply to them.
2424 auto* last_committed_entry = GetLastCommittedEntry();
2425 if (!last_committed_entry)
2426 return;
Shivani Sharma194877032019-03-07 17:52:472427
shivanigithub99368382021-06-16 18:33:372428 if (base::FeatureList::IsEnabled(
2429 features::kDebugHistoryInterventionNoUserActivation)) {
2430 return;
2431 }
2432
Shivani Sharmac4cc8922019-04-18 03:11:172433 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472434}
2435
clamy987a3752018-05-03 17:36:262436bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2437 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172438 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262439 NavigationEntryImpl* entry =
2440 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2441 if (!entry)
2442 return false;
2443
2444 FrameNavigationEntry* frame_entry =
2445 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2446 if (!frame_entry)
2447 return false;
2448
Nate Chapin45f620582021-09-30 17:45:432449 // |is_browser_initiated| is false here because a navigation in a new subframe
2450 // always begins with renderer action (i.e., an HTML element being inserted
2451 // into the DOM), so it is always renderer-initiated.
Camille Lamy5193caa2018-10-12 11:59:422452 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572453 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232454 ReloadType::NONE, false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432455 true /* is_history_navigation_in_new_child */,
2456 false /* is_browser_initiated */);
clamyea99ea12018-05-28 13:54:232457
2458 if (!request)
2459 return false;
2460
arthursonzognif046d4a2019-12-12 19:08:102461 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412462
Lukasz Anforowicz9ee83c272020-12-01 20:14:052463 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2464 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232465
2466 return true;
clamy987a3752018-05-03 17:36:262467}
2468
Tsuyoshi Horo52fd08e2020-07-07 07:03:452469bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2470 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2471 if (!entry)
2472 return false;
Rakina Zata Amnif297a802022-01-18 03:53:432473
2474 if (entry->IsInitialEntryNotForSynchronousAboutBlank()) {
2475 // We should never navigate to an existing initial NavigationEntry that is
2476 // the initial NavigationEntry for the initial empty document that hasn't
2477 // been overridden by the synchronous about:blank commit, to preserve
2478 // legacy behavior where trying to reload when the main frame is on the
2479 // initial empty document won't result in a navigation. See also
2480 // https://crbug.com/1277414.
2481 return false;
2482 }
Tsuyoshi Horo52fd08e2020-07-07 07:03:452483 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2484 if (!frame_entry)
2485 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142486 ReloadType reload_type = ReloadType::NORMAL;
2487 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452488 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142489 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452490 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432491 false /* is_history_navigation_in_new_child */,
2492 true /* is_browser_initiated */);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452493 if (!request)
2494 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052495 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452496 return true;
2497}
2498
Dave Tapuska8bfd84c2019-03-26 20:47:162499void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2500 int offset,
2501 int sandbox_frame_tree_node_id) {
2502 if (!CanGoToOffset(offset))
2503 return;
Nate Chapin45f620582021-09-30 17:45:432504 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id,
2505 false /* is_browser_initiated */);
Dave Tapuska8bfd84c2019-03-26 20:47:162506}
2507
clamy987a3752018-05-03 17:36:262508void NavigationControllerImpl::NavigateFromFrameProxy(
2509 RenderFrameHostImpl* render_frame_host,
2510 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022511 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452512 int initiator_process_id,
Anton Bikineevf62d1bf2021-05-15 17:56:072513 const absl::optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262514 bool is_renderer_initiated,
2515 SiteInstance* source_site_instance,
2516 const Referrer& referrer,
2517 ui::PageTransition page_transition,
2518 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252519 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262520 const std::string& method,
2521 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092522 const std::string& extra_headers,
Antonio Sartori2f763d9d2021-04-21 10:04:142523 network::mojom::SourceLocationPtr source_location,
John Delaney50425f82020-04-07 16:26:212524 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Anton Bikineevf62d1bf2021-05-15 17:56:072525 const absl::optional<blink::Impression>& impression) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582526 if (is_renderer_initiated)
2527 DCHECK(initiator_origin.has_value());
2528
clamy987a3752018-05-03 17:36:262529 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582530
clamy987a3752018-05-03 17:36:262531 // Create a NavigationEntry for the transfer, without making it the pending
2532 // entry. Subframe transfers should have a clone of the last committed entry
2533 // with a FrameNavigationEntry for the target frame. Main frame transfers
2534 // should have a new NavigationEntry.
2535 // TODO(creis): Make this unnecessary by creating (and validating) the params
2536 // directly, passing them to the destination RenderFrameHost. See
2537 // https://crbug.com/536906.
2538 std::unique_ptr<NavigationEntryImpl> entry;
Harkiran Bolariae1b5158b2021-09-16 19:03:262539 if (!render_frame_host->is_main_frame()) {
clamy987a3752018-05-03 17:36:262540 // Subframe case: create FrameNavigationEntry.
Rakina Zata Amniafd3c6582021-11-30 06:19:172541 CHECK(GetLastCommittedEntry());
2542 entry = GetLastCommittedEntry()->Clone();
2543 entry->set_extra_headers(extra_headers);
2544 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2545 // Renderer-initiated navigation that target a remote frame are currently
2546 // classified as browser-initiated when this one has already navigated.
2547 // See https://crbug.com/722251.
Nate Chapin9f169072021-06-09 19:32:372548 // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter
2549 // to CreateNavigationRequestFromLoadParams(), so while kReplace might
2550 // remove child FrameNavigationEntries (e.g., if this is a cross-process
2551 // same-document navigation), they will still be present in the
2552 // committed NavigationEntry that will be referenced to construct the new
2553 // FrameNavigationEntry tree when this navigation commits.
clamy987a3752018-05-03 17:36:262554 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:082555 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582556 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072557 absl::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202558 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392559 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482560 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori79d549d2021-02-18 12:59:542561 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262562 } else {
2563 // Main frame case.
2564 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102565 url, referrer, initiator_origin, source_site_instance, page_transition,
2566 is_renderer_initiated, extra_headers, browser_context_,
Hayato Ito303654c2021-06-30 09:07:542567 blob_url_loader_factory));
clamy987a3752018-05-03 17:36:262568 entry->root_node()->frame_entry->set_source_site_instance(
2569 static_cast<SiteInstanceImpl*>(source_site_instance));
2570 entry->root_node()->frame_entry->set_method(method);
2571 }
clamy987a3752018-05-03 17:36:262572
Camille Lamy5193caa2018-10-12 11:59:422573 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262574 if (GetLastCommittedEntry() &&
2575 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2576 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422577 override_user_agent = true;
clamy987a3752018-05-03 17:36:262578 }
2579 // TODO(creis): Set user gesture and intent received timestamp on Android.
2580
2581 // We may not have successfully added the FrameNavigationEntry to |entry|
2582 // above (per https://crbug.com/608402), in which case we create it from
2583 // scratch. This works because we do not depend on |frame_entry| being inside
2584 // |entry| during NavigateToEntry. This will go away when we shortcut this
2585 // further in https://crbug.com/536906.
2586 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2587 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452588 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Nate Chapinfbfe5af2021-06-10 17:22:082589 node->unique_name(), -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582590 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072591 absl::nullopt /* origin */, referrer, initiator_origin,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:122592 std::vector<GURL>(), blink::PageState(), method, -1,
2593 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482594 nullptr /* subresource_web_bundle_navigation_info */,
Nate Chapin63db0d12022-01-20 22:03:302595 nullptr /* policy_container_policies */,
2596 false /* protect_url_in_app_history */);
clamy987a3752018-05-03 17:36:262597 }
2598
Camille Lamy5193caa2018-10-12 11:59:422599 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022600 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452601 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292602 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422603 params.source_site_instance = source_site_instance;
2604 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2605 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032606 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422607 params.referrer = referrer;
2608 /* params.redirect_chain: skip */
2609 params.extra_headers = extra_headers;
2610 params.is_renderer_initiated = is_renderer_initiated;
2611 params.override_user_agent = UA_OVERRIDE_INHERIT;
2612 /* params.base_url_for_data_url: skip */
2613 /* params.virtual_url_for_data_url: skip */
2614 /* params.data_url_as_string: skip */
2615 params.post_data = post_body;
2616 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582617 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422618 /* params.frame_name: skip */
2619 // TODO(clamy): See if user gesture should be propagated to this function.
2620 params.has_user_gesture = false;
2621 params.should_clear_history_list = false;
2622 params.started_from_context_menu = false;
2623 /* params.navigation_ui_data: skip */
2624 /* params.input_start: skip */
Minggang Wangf59db47b2021-06-16 01:56:222625 params.was_activated = blink::mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542626 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212627 params.impression = impression;
Antonio Sartori6984c742021-08-26 08:03:412628 params.download_policy = std::move(download_policy);
Camille Lamy5193caa2018-10-12 11:59:422629
2630 std::unique_ptr<NavigationRequest> request =
2631 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032632 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:142633 false /* has_user_gesture */, std::move(source_location),
Antonio Sartori6984c742021-08-26 08:03:412634 ReloadType::NONE, entry.get(), frame_entry.get());
clamyea99ea12018-05-28 13:54:232635
2636 if (!request)
2637 return;
2638
Arthur Hemery948742762019-09-18 10:06:242639 // At this stage we are proceeding with this navigation. If this was renderer
2640 // initiated with user gesture, we need to make sure we clear up potential
2641 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2642 DiscardNonCommittedEntries();
2643
Lukasz Anforowicz9ee83c272020-12-01 20:14:052644 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262645}
2646
[email protected]d1198fd2012-08-13 22:50:192647void NavigationControllerImpl::SetSessionStorageNamespace(
Aaron Colwellf3b316e2021-03-11 20:17:052648 const StoragePartitionId& partition_id,
[email protected]8ff00d72012-10-23 19:12:212649 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192650 if (!session_storage_namespace)
2651 return;
2652
2653 // We can't overwrite an existing SessionStorage without violating spec.
2654 // Attempts to do so may give a tab access to another tab's session storage
2655 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152656 bool successful_insert =
2657 session_storage_namespace_map_
Aaron Colwellf3b316e2021-03-11 20:17:052658 .insert(std::make_pair(partition_id,
2659 static_cast<SessionStorageNamespaceImpl*>(
2660 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302661 .second;
2662 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
Aaron Colwellb731a0ae2021-03-19 19:14:472663 OnStoragePartitionIdAdded(partition_id);
[email protected]d1198fd2012-08-13 22:50:192664}
2665
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572666bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Rakina Zata Amniafd3c6582021-11-30 06:19:172667 return IsInitialNavigation() && GetLastCommittedEntry()->IsInitialEntry() &&
Carlos Caballeroede6f8c2021-01-28 11:01:502668 !frame_tree_.has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462669}
2670
Aran Gilman37d11632019-10-08 23:07:152671SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Aaron Colwell78b4bde2021-03-16 16:16:092672 const SiteInfo& site_info) {
2673 // TODO(acolwell): Remove partition_id logic once we have successfully
2674 // migrated the implementation to be a StoragePartitionConfig. At that point
2675 // |site_info| can be replaced with a StoragePartitionConfig.
2676 const StoragePartitionId partition_id =
2677 site_info.GetStoragePartitionId(browser_context_);
2678 const StoragePartitionConfig partition_config =
Robbie McElrath7d4bd852021-07-24 04:02:192679 site_info.storage_partition_config();
[email protected]d1198fd2012-08-13 22:50:192680
[email protected]fdac6ade2013-07-20 01:06:302681 StoragePartition* partition =
Lukasz Anforowiczb9a969a2021-04-29 15:26:252682 browser_context_->GetStoragePartition(partition_config);
michaelnbacbcbd2016-02-09 00:32:032683 DOMStorageContextWrapper* context_wrapper =
2684 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2685
2686 SessionStorageNamespaceMap::const_iterator it =
2687 session_storage_namespace_map_.find(partition_id);
2688 if (it != session_storage_namespace_map_.end()) {
2689 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152690 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2691 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:472692
2693 // Verify that the config we generated now matches the one that
2694 // was generated when the namespace was added to the map.
Aaron Colwellb731a0ae2021-03-19 19:14:472695 if (partition_config != it->first.config()) {
2696 LogStoragePartitionIdCrashKeys(it->first, partition_id);
2697 }
Sharon Yangd4f48792021-06-30 17:02:532698 CHECK_EQ(partition_config, it->first.config());
Aaron Colwellb731a0ae2021-03-19 19:14:472699
michaelnbacbcbd2016-02-09 00:32:032700 return it->second.get();
2701 }
2702
2703 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102704 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2705 SessionStorageNamespaceImpl::Create(context_wrapper);
2706 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2707 session_storage_namespace.get();
2708 session_storage_namespace_map_[partition_id] =
2709 std::move(session_storage_namespace);
Aaron Colwellb731a0ae2021-03-19 19:14:472710 OnStoragePartitionIdAdded(partition_id);
[email protected]fdac6ade2013-07-20 01:06:302711
Daniel Murphy31bbb8b12018-02-07 21:44:102712 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302713}
2714
2715SessionStorageNamespace*
2716NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Robbie McElrath7d4bd852021-07-24 04:02:192717 return GetSessionStorageNamespace(SiteInfo(GetBrowserContext()));
[email protected]fdac6ade2013-07-20 01:06:302718}
2719
2720const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572721NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302722 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552723}
[email protected]d202a7c2012-01-04 07:53:472724
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572725bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562726 return needs_reload_;
2727}
[email protected]a26023822011-12-29 00:23:552728
[email protected]46bb5e9c2013-10-03 22:16:472729void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412730 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2731}
2732
2733void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472734 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412735 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542736
2737 if (last_committed_entry_index_ != -1) {
2738 entries_[last_committed_entry_index_]->SetTransitionType(
2739 ui::PAGE_TRANSITION_RELOAD);
2740 }
[email protected]46bb5e9c2013-10-03 22:16:472741}
2742
[email protected]d202a7c2012-01-04 07:53:472743void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572744 DCHECK_LT(index, GetEntryCount());
2745 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312746 DiscardNonCommittedEntries();
2747
2748 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122749 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312750 last_committed_entry_index_--;
2751}
2752
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572753NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272754 // If there is no pending_entry_, there should be no pending_entry_index_.
2755 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2756
2757 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362758 // at that index. An exception is while a reload of a post commit error page
2759 // is ongoing; in that case pending entry will point to the entry replaced
2760 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272761 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362762 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2763 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272764
[email protected]022af742011-12-28 18:37:252765 return pending_entry_;
2766}
2767
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572768int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272769 // The pending entry index must always be less than the number of entries.
arthursonzogni5c4c202d2017-04-25 23:41:272770 DCHECK_LT(pending_entry_index_, GetEntryCount());
[email protected]a26023822011-12-29 00:23:552771 return pending_entry_index_;
2772}
2773
avi25764702015-06-23 15:43:372774void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572775 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362776 bool replace,
Dave Tapuska87696ae2021-11-18 18:48:312777 bool was_post_commit_error,
Rakina Zata Amnia4e27222021-12-22 01:05:002778 bool in_fenced_frame_tree,
2779 LoadCommittedDetails* commit_details) {
Dave Tapuska87696ae2021-11-18 18:48:312780 // Fenced frame trees should always have `ui::PAGE_TRANSITION_AUTO_SUBFRAME`
2781 // set because:
2782 // 1) They don't influence the history of the outer page.
2783 // 2) They are always replace only navigation (there is always only one entry
2784 // in their history stack).
2785 // 3) Are not top level navigations and appear similar to iframes.
2786 // Navigations of the fenced frame might create a new NavigationEntry, which
2787 // will call this function. Non fenced frame navigations will never have
2788 // `ui::PAGE_TRANSITION_AUTO_SUBFRAME` because they won't call
2789 // InsertOrReplaceEntry.
2790 DCHECK_EQ(in_fenced_frame_tree,
2791 ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2792 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202793
avi5cad4912015-06-19 05:25:442794 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2795 // need to keep continuity with the pending entry, so copy the pending entry's
2796 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2797 // then the renderer navigated on its own, independent of the pending entry,
2798 // so don't copy anything.
2799 if (pending_entry_ && pending_entry_index_ == -1)
2800 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202801
Rakina Zata Amnia4e27222021-12-22 01:05:002802 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]765b35502008-08-21 00:51:202803
creisee17e932015-07-17 17:56:222804 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362805 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102806 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572807 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362808 // If the new entry is a post-commit error page, we store the current last
2809 // committed entry to the side so that we can put it back when navigating
2810 // away from the error.
2811 if (was_post_commit_error) {
2812 DCHECK(!entry_replaced_by_post_commit_error_);
2813 entry_replaced_by_post_commit_error_ =
2814 std::move(entries_[last_committed_entry_index_]);
2815 }
dcheng36b6aec92015-12-26 06:16:362816 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222817 return;
2818 }
[email protected]765b35502008-08-21 00:51:202819
creis37979a62015-08-04 19:48:182820 // We shouldn't see replace == true when there's no committed entries.
2821 DCHECK(!replace);
2822
Michael Thiessen9b14d512019-09-23 21:19:472823 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202824
Shivani Sharmad8c8d652019-02-13 17:27:572825 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202826
dcheng36b6aec92015-12-26 06:16:362827 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202828 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292829}
2830
Shivani Sharmad8c8d652019-02-13 17:27:572831void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162832 if (entries_.size() < max_entry_count())
2833 return;
2834
2835 DCHECK_EQ(max_entry_count(), entries_.size());
2836 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572837 CHECK_EQ(pending_entry_index_, -1);
2838
2839 int index = 0;
Elly Fong-Jonesccc6d1f2021-06-14 18:32:422840 // Retrieve the oldest skippable entry.
2841 for (; index < GetEntryCount(); index++) {
2842 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2843 break;
Shivani Sharmad8c8d652019-02-13 17:27:572844 }
2845
2846 // If there is no skippable entry or if it is the last committed entry then
2847 // fall back to pruning the oldest entry. It is not safe to prune the last
2848 // committed entry.
2849 if (index == GetEntryCount() || index == last_committed_entry_index_)
2850 index = 0;
2851
2852 bool should_succeed = RemoveEntryAtIndex(index);
2853 DCHECK_EQ(true, should_succeed);
2854
2855 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252856}
2857
clamy3cb9bea92018-07-10 12:42:022858void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162859 ReloadType reload_type,
Nate Chapin45f620582021-09-30 17:45:432860 int sandboxed_source_frame_tree_node_id,
2861 bool is_browser_initiated) {
Alexander Timin3a92df72019-09-20 11:59:502862 TRACE_EVENT0("navigation",
2863 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272864 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022865 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362866 if (pending_entry_index_ != -1) {
2867 // The pending entry may not be in entries_ if a post-commit error page is
2868 // showing.
2869 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2870 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2871 }
Gyuyoung Kim107c2a02021-04-13 01:49:302872 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572873 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:012874 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:002875 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:572876 int nav_entry_id = pending_entry_->GetUniqueID();
2877
[email protected]83c2e232011-10-07 21:36:462878 // If we were navigating to a slow-to-commit page, and the user performs
2879 // a session history navigation to the last committed page, RenderViewHost
2880 // will force the throbber to start, but WebKit will essentially ignore the
2881 // navigation, and won't send a message to stop the throbber. To prevent this
2882 // from happening, we drop the navigation here and stop the slow-to-commit
2883 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022884 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:082885 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:272886 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
Takashi Toyoshimabf549472021-07-01 07:47:202887 frame_tree_.StopLoading();
[email protected]6a13a6c2011-12-20 21:47:122888
[email protected]83c2e232011-10-07 21:36:462889 DiscardNonCommittedEntries();
2890 return;
2891 }
2892
creisce0ef3572017-01-26 17:53:082893 // Compare FrameNavigationEntries to see which frames in the tree need to be
2894 // navigated.
clamy3cb9bea92018-07-10 12:42:022895 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2896 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Nate Chapin45f620582021-09-30 17:45:432897 FindFramesToNavigate(root, reload_type, is_browser_initiated,
2898 &same_document_loads, &different_document_loads);
creis4e2ecb72015-06-20 00:46:302899
2900 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572901 // We were unable to match any frames to navigate. This can happen if a
2902 // history navigation targets a subframe that no longer exists
2903 // (https://crbug.com/705550). In this case, we need to update the current
2904 // history entry to the pending one but keep the main document loaded. We
2905 // also need to ensure that observers are informed about the updated
2906 // current history entry (e.g., for greying out back/forward buttons), and
2907 // that renderer processes update their history offsets. The easiest way
2908 // to do all that is to schedule a "redundant" same-document navigation in
2909 // the main frame.
2910 //
2911 // Note that we don't want to remove this history entry, as it might still
2912 // be valid later, since a frame that it's targeting may be recreated.
2913 //
2914 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
2915 // need to repeat history navigations until finding the one that works.
2916 // Consider changing this behavior to keep looking for the first valid
2917 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:022918 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422919 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572920 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:572921 ReloadType::NONE /* reload_type */,
2922 true /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432923 false /* is_history_navigation_in_new_child */,
2924 is_browser_initiated);
clamy3cb9bea92018-07-10 12:42:022925 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572926 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:022927 DiscardPendingEntry(false);
2928 return;
2929 }
Alex Moshchuk3a4e77a2020-05-29 21:32:572930 same_document_loads.push_back(std::move(navigation_request));
2931
2932 // Sanity check that we never take this branch for any kinds of reloads,
2933 // as those should've queued a different-document load in the main frame.
2934 DCHECK(!is_forced_reload);
2935 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:302936 }
2937
Dave Tapuska8bfd84c2019-03-26 20:47:162938 // If |sandboxed_source_frame_node_id| is valid, then track whether this
2939 // navigation affects any frame outside the frame's subtree.
2940 if (sandboxed_source_frame_tree_node_id !=
2941 FrameTreeNode::kFrameTreeNodeInvalidId) {
2942 bool navigates_inside_tree =
2943 DoesSandboxNavigationStayWithinSubtree(
2944 sandboxed_source_frame_tree_node_id, same_document_loads) &&
2945 DoesSandboxNavigationStayWithinSubtree(
2946 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:502947 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:162948 // the number of pages that trigger this.
2949 FrameTreeNode* sandbox_source_frame_tree_node =
2950 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
2951 if (sandbox_source_frame_tree_node) {
2952 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
2953 sandbox_source_frame_tree_node->current_frame_host(),
2954 navigates_inside_tree
2955 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
2956 : blink::mojom::WebFeature::
2957 kSandboxBackForwardAffectsFramesOutsideSubtree);
2958 }
Dave Tapuska855c1e12019-08-23 20:45:522959
2960 // If the navigation occurred outside the tree discard it because
2961 // the sandboxed frame didn't have permission to navigate outside
2962 // its tree. If it is possible that the navigation is both inside and
2963 // outside the frame tree and we discard it entirely because we don't
2964 // want to end up in a history state that didn't exist before.
Dominic Farolino057440042022-01-19 18:18:142965 if (!navigates_inside_tree) {
Dave Tapuska855c1e12019-08-23 20:45:522966 DiscardPendingEntry(false);
2967 return;
2968 }
Dave Tapuska8bfd84c2019-03-26 20:47:162969 }
2970
Carlos Caballero539a421c2020-07-06 10:25:572971 // BackForwardCache:
2972 // Navigate immediately if the document is in the BackForwardCache.
2973 if (back_forward_cache_.GetEntry(nav_entry_id)) {
2974 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
2975 DCHECK_EQ(reload_type, ReloadType::NONE);
2976 auto navigation_request = CreateNavigationRequestFromEntry(
2977 root, pending_entry_, pending_entry_->GetFrameEntry(root),
2978 ReloadType::NONE, false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432979 false /* is_history_navigation_in_new_child */, is_browser_initiated);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052980 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:572981
2982 return;
2983 }
2984
2985 // History navigation might try to reuse a specific BrowsingInstance, already
2986 // used by a page in the cache. To avoid having two different main frames that
2987 // live in the same BrowsingInstance, evict the all pages with this
2988 // BrowsingInstance from the cache.
2989 //
2990 // For example, take the following scenario:
2991 //
2992 // A1 = Some page on a.com
2993 // A2 = Some other page on a.com
2994 // B3 = An uncacheable page on b.com
2995 //
2996 // Then the following navigations occur:
2997 // A1->A2->B3->A1
2998 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
2999 // take its place) and A1 will be created in the same BrowsingInstance (and
3000 // SiteInstance), as A2.
3001 //
3002 // If we didn't do anything, both A1 and A2 would remain alive in the same
3003 // BrowsingInstance/SiteInstance, which is unsupported by
3004 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
3005 // A2 from the cache.
3006 if (pending_entry_->site_instance()) {
3007 back_forward_cache_.EvictFramesInRelatedSiteInstances(
3008 pending_entry_->site_instance());
3009 }
3010
clamy3cb9bea92018-07-10 12:42:023011 // This call does not support re-entrancy. See http://crbug.com/347742.
3012 CHECK(!in_navigate_to_pending_entry_);
3013 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:303014
arthursonzogni66f711c2019-10-08 14:40:363015 // It is not possible to delete the pending NavigationEntry while navigating
3016 // to it. Grab a reference to delay potential deletion until the end of this
3017 // function.
3018 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3019
creis4e2ecb72015-06-20 00:46:303020 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:023021 for (auto& item : same_document_loads) {
3022 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:053023 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:303024 }
clamy3cb9bea92018-07-10 12:42:023025 for (auto& item : different_document_loads) {
3026 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:053027 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:303028 }
clamy3cb9bea92018-07-10 12:42:023029
3030 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:303031}
3032
Alex Moshchuk3a4e77a2020-05-29 21:32:573033NavigationControllerImpl::HistoryNavigationAction
3034NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:303035 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:573036 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:423037 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:583038 // Only active and prerendered documents are allowed to navigate in their
3039 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:423040 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:013041 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:583042 // - If the document is in pending deletion, the browser already committed
3043 // to destroying this RenderFrameHost. See https://crbug.com/930278.
3044 // - If the document is in back-forward cache, it's not allowed to navigate
3045 // as it should remain frozen. Ignore the request and evict the document
3046 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:423047 //
Sreeja Kamishettydb8e2892021-03-10 09:30:583048 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:423049 // which should all be inactive as well.
Fergal Daly1336ac642021-09-14 15:13:113050 if (frame->current_frame_host()->IsInactiveAndDisallowActivation(
3051 DisallowActivationReasonId::kDetermineActionForHistoryNavigation)) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:423052 return HistoryNavigationAction::kStopLooking;
Fergal Daly1336ac642021-09-14 15:13:113053 }
Sreeja Kamishetty8eacabb2021-03-09 11:45:423054 }
arthursonzogni03f76152019-02-12 10:35:203055
Alex Moshchuk3a4e77a2020-05-29 21:32:573056 // If there's no last committed entry, there is no previous history entry to
3057 // compare against, so fall back to a different-document load. Note that we
3058 // should only reach this case for the root frame and not descend further
3059 // into subframes.
3060 if (!GetLastCommittedEntry()) {
3061 DCHECK(frame->IsMainFrame());
3062 return HistoryNavigationAction::kDifferentDocument;
3063 }
3064
3065 // Reloads should result in a different-document load. Note that reloads may
3066 // also happen via the |needs_reload_| mechanism where the reload_type is
3067 // NONE, so detect this by comparing whether we're going to the same
3068 // entry that we're currently on. Similarly to above, only main frames
3069 // should reach this. Note that subframes support reloads, but that's done
3070 // via a different path that doesn't involve FindFramesToNavigate (see
3071 // RenderFrameHost::Reload()).
3072 if (reload_type != ReloadType::NONE ||
3073 pending_entry_index_ == last_committed_entry_index_) {
3074 DCHECK(frame->IsMainFrame());
3075 return HistoryNavigationAction::kDifferentDocument;
3076 }
3077
Alex Moshchuk47d1a4bd2020-06-01 22:15:343078 // If there is no new FrameNavigationEntry for the frame, ignore the
3079 // load. For example, this may happen when going back to an entry before a
3080 // frame was created. Suppose we commit a same-document navigation that also
3081 // results in adding a new subframe somewhere in the tree. If we go back,
3082 // the new subframe will be missing a FrameNavigationEntry in the previous
3083 // NavigationEntry, but we shouldn't delete or change what's loaded in
3084 // it.
3085 //
Alex Moshchuke65c39272020-06-03 17:55:373086 // Note that in this case, there is no need to keep looking for navigations
3087 // in subframes, which would be missing FrameNavigationEntries as well.
3088 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:343089 // It's important to check this before checking |old_item| below, since both
3090 // might be null, and in that case we still shouldn't change what's loaded in
3091 // this frame. Note that scheduling any loads assumes that |new_item| is
3092 // non-null. See https://crbug.com/1088354.
3093 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3094 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:373095 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:343096
Alex Moshchuk3a4e77a2020-05-29 21:32:573097 // If there is no old FrameNavigationEntry, schedule a different-document
3098 // load.
3099 //
creis225a7432016-06-03 22:56:273100 // TODO(creis): Store the last committed FrameNavigationEntry to use here,
3101 // rather than assuming the NavigationEntry has up to date info on subframes.
creis4e2ecb72015-06-20 00:46:303102 FrameNavigationEntry* old_item =
3103 GetLastCommittedEntry()->GetFrameEntry(frame);
Alex Moshchuk3a4e77a2020-05-29 21:32:573104 if (!old_item)
3105 return HistoryNavigationAction::kDifferentDocument;
3106
Alex Moshchuk3a4e77a2020-05-29 21:32:573107 // If the new item is not in the same SiteInstance, schedule a
3108 // different-document load. Newly restored items may not have a SiteInstance
3109 // yet, in which case it will be assigned on first commit.
3110 if (new_item->site_instance() &&
3111 new_item->site_instance() != old_item->site_instance())
3112 return HistoryNavigationAction::kDifferentDocument;
3113
3114 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:313115 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
3116 // that we do this even if the history navigation would not be modifying this
3117 // frame were it live.
3118 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:573119 return HistoryNavigationAction::kDifferentDocument;
3120
3121 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:313122 // Starting a navigation after a crash early-promotes the speculative
3123 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
3124 // committed yet, so we can not possibly perform a same-document history
3125 // navigation. The frame would need to be reloaded with a cross-document
3126 // navigation.
3127 if (!frame->current_frame_host()->has_committed_any_navigation())
3128 return HistoryNavigationAction::kDifferentDocument;
3129
creis54131692016-08-12 18:32:253130 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:493131 // sequence number but different item sequence number.
3132 if (new_item->document_sequence_number() ==
3133 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573134 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493135 }
avib48cb312016-05-05 21:35:003136
Alex Moshchuk3a4e77a2020-05-29 21:32:573137 // Otherwise, if both item and document sequence numbers differ, this
3138 // should be a different document load.
3139 return HistoryNavigationAction::kDifferentDocument;
3140 }
3141
3142 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373143 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573144 DCHECK_EQ(new_item->document_sequence_number(),
3145 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373146 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573147}
3148
3149void NavigationControllerImpl::FindFramesToNavigate(
3150 FrameTreeNode* frame,
3151 ReloadType reload_type,
Nate Chapin45f620582021-09-30 17:45:433152 bool is_browser_initiated,
Alex Moshchuk3a4e77a2020-05-29 21:32:573153 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3154 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3155 DCHECK(pending_entry_);
3156 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3157
3158 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3159
3160 if (action == HistoryNavigationAction::kSameDocument) {
3161 std::unique_ptr<NavigationRequest> navigation_request =
3162 CreateNavigationRequestFromEntry(
3163 frame, pending_entry_, new_item, reload_type,
3164 true /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433165 false /* is_history_navigation_in_new_child */,
3166 is_browser_initiated);
Alex Moshchuk3a4e77a2020-05-29 21:32:573167 if (navigation_request) {
3168 // Only add the request if was properly created. It's possible for the
3169 // creation to fail in certain cases, e.g. when the URL is invalid.
3170 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303171 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573172 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473173 std::unique_ptr<NavigationRequest> navigation_request =
3174 CreateNavigationRequestFromEntry(
3175 frame, pending_entry_, new_item, reload_type,
3176 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433177 false /* is_history_navigation_in_new_child */,
3178 is_browser_initiated);
Lei Zhang96031532019-10-10 19:05:473179 if (navigation_request) {
3180 // Only add the request if was properly created. It's possible for the
3181 // creation to fail in certain cases, e.g. when the URL is invalid.
3182 different_document_loads->push_back(std::move(navigation_request));
3183 }
3184 // For a different document, the subframes will be destroyed, so there's
3185 // no need to consider them.
3186 return;
Alex Moshchuke65c39272020-06-03 17:55:373187 } else if (action == HistoryNavigationAction::kStopLooking) {
3188 return;
creis4e2ecb72015-06-20 00:46:303189 }
3190
3191 for (size_t i = 0; i < frame->child_count(); i++) {
Nate Chapin45f620582021-09-30 17:45:433192 FindFramesToNavigate(frame->child_at(i), reload_type, is_browser_initiated,
3193 same_document_loads, different_document_loads);
creis4e2ecb72015-06-20 00:46:303194 }
3195}
3196
Harkiran Bolariaba823e42021-05-21 18:30:363197base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry(
clamy21718cc22018-06-13 13:34:243198 const LoadURLParams& params) {
3199 // Find the appropriate FrameTreeNode.
3200 FrameTreeNode* node = nullptr;
3201 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3202 !params.frame_name.empty()) {
3203 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003204 ? frame_tree_.FindByID(params.frame_tree_node_id)
3205 : frame_tree_.FindByName(params.frame_name);
Hayato Ito7a80db42021-07-05 06:18:543206 DCHECK(!node || node->frame_tree() == &frame_tree_);
clamy21718cc22018-06-13 13:34:243207 }
3208
3209 // If no FrameTreeNode was specified, navigate the main frame.
3210 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003211 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243212
Camille Lamy5193caa2018-10-12 11:59:423213 // Compute overrides to the LoadURLParams for |override_user_agent|,
3214 // |should_replace_current_entry| and |has_user_gesture| that will be used
3215 // both in the creation of the NavigationEntry and the NavigationRequest.
3216 // Ideally, the LoadURLParams themselves would be updated, but since they are
3217 // passed as a const reference, this is not possible.
3218 // TODO(clamy): When we only create a NavigationRequest, move this to
3219 // CreateNavigationRequestFromLoadURLParams.
3220 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3221 GetLastCommittedEntry());
3222
3223 // Don't allow an entry replacement if there is no entry to replace.
3224 // http://crbug.com/457149
Hayato Ito7a80db42021-07-05 06:18:543225 //
3226 // If there is an entry, an entry replacement must happen if the current
3227 // browsing context should maintain a trivial session history.
shivanigithubf405bf0d2021-11-05 17:58:333228 bool should_replace_current_entry =
3229 (params.should_replace_current_entry ||
3230 ShouldMaintainTrivialSessionHistory(node)) &&
3231 entries_.size();
Camille Lamy5193caa2018-10-12 11:59:423232
clamy21718cc22018-06-13 13:34:243233 // Javascript URLs should not create NavigationEntries. All other navigations
3234 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243235 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373236 std::unique_ptr<NavigationEntryImpl> entry =
3237 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3238 should_replace_current_entry,
3239 params.has_user_gesture);
clamy21718cc22018-06-13 13:34:243240 DiscardPendingEntry(false);
3241 SetPendingEntry(std::move(entry));
3242 }
3243
3244 // Renderer-debug URLs are sent to the renderer process immediately for
3245 // processing and don't need to create a NavigationRequest.
3246 // Note: this includes navigations to JavaScript URLs, which are considered
3247 // renderer-debug URLs.
3248 // Note: we intentionally leave the pending entry in place for renderer debug
3249 // URLs, unlike the cases below where we clear it if the navigation doesn't
3250 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303251 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483252 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3253 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153254 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483255 params.url, browser_context_)) {
3256 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363257 return nullptr;
Oleg Davydov2cc0167b2019-02-05 14:32:483258 }
3259
clamy21718cc22018-06-13 13:34:243260 HandleRendererDebugURL(node, params.url);
Harkiran Bolariaba823e42021-05-21 18:30:363261 return nullptr;
clamy21718cc22018-06-13 13:34:243262 }
3263
Antonio Sartori78a749f2020-11-30 12:03:393264 DCHECK(pending_entry_);
3265
clamy21718cc22018-06-13 13:34:243266 // Convert navigations to the current URL to a reload.
3267 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3268 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3269 // DevTools sometimes issues navigations to main frames that they do not
3270 // expect to see treated as reload, and it only works because they pass a
3271 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3272 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543273 ReloadType reload_type = params.reload_type;
3274 if (reload_type == ReloadType::NONE &&
3275 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043276 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243277 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243278 params.load_type ==
3279 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
Hayato Ito7a80db42021-07-05 06:18:543280 should_replace_current_entry, GetLastCommittedEntry())) {
clamy21718cc22018-06-13 13:34:243281 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473282 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393283
3284 // If this is a reload of an existing FrameNavigationEntry and we had a
3285 // policy container for it, then we should copy it into the pending entry,
3286 // so that it is copied to the navigation request in
3287 // CreateNavigationRequestFromLoadParams later.
3288 if (GetLastCommittedEntry()) {
3289 FrameNavigationEntry* previous_frame_entry =
3290 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193291 if (previous_frame_entry &&
3292 previous_frame_entry->policy_container_policies()) {
3293 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163294 previous_frame_entry->policy_container_policies()->Clone());
Antonio Sartori78a749f2020-11-30 12:03:393295 }
3296 }
3297 }
3298
3299 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
Rakina Zata Amniafd3c6582021-11-30 06:19:173300 // document, then we should copy the document polices from RenderFrameHost's
3301 // PolicyContainerHost. The NavigationRequest will create a new
3302 // PolicyContainerHost with the document policies from the |pending_entry_|,
3303 // and that PolicyContainerHost will be put in the final RenderFrameHost for
3304 // the navigation. This way, we ensure that we keep enforcing the right
3305 // policies on the initial empty document after the reload.
3306 if (GetLastCommittedEntry()->IsInitialEntry() && params.url.IsAboutBlank()) {
Antonio Sartori78a749f2020-11-30 12:03:393307 if (node->current_frame_host() &&
3308 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193309 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163310 node->current_frame_host()
3311 ->policy_container_host()
3312 ->policies()
3313 .Clone());
Antonio Sartori78a749f2020-11-30 12:03:393314 }
clamy21718cc22018-06-13 13:34:243315 }
3316
3317 // navigation_ui_data should only be present for main frame navigations.
3318 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
3319
Camille Lamy5193caa2018-10-12 11:59:423320 std::unique_ptr<NavigationRequest> request =
3321 CreateNavigationRequestFromLoadParams(
3322 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143323 params.has_user_gesture, network::mojom::SourceLocation::New(),
Antonio Sartori6984c742021-08-26 08:03:413324 reload_type, pending_entry_, pending_entry_->GetFrameEntry(node));
clamy21718cc22018-06-13 13:34:243325
3326 // If the navigation couldn't start, return immediately and discard the
3327 // pending NavigationEntry.
3328 if (!request) {
3329 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363330 return nullptr;
clamy21718cc22018-06-13 13:34:243331 }
3332
Camille Lamy5193caa2018-10-12 11:59:423333#if DCHECK_IS_ON()
3334 // Safety check that NavigationRequest and NavigationEntry match.
3335 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3336#endif
3337
clamy21718cc22018-06-13 13:34:243338 // This call does not support re-entrancy. See http://crbug.com/347742.
3339 CHECK(!in_navigate_to_pending_entry_);
3340 in_navigate_to_pending_entry_ = true;
3341
arthursonzogni66f711c2019-10-08 14:40:363342 // It is not possible to delete the pending NavigationEntry while navigating
3343 // to it. Grab a reference to delay potential deletion until the end of this
3344 // function.
3345 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3346
Harkiran Bolariaba823e42021-05-21 18:30:363347 base::WeakPtr<NavigationHandle> created_navigation_handle(
3348 request->GetWeakPtr());
Lukasz Anforowicz9ee83c272020-12-01 20:14:053349 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243350
3351 in_navigate_to_pending_entry_ = false;
Harkiran Bolariaba823e42021-05-21 18:30:363352 return created_navigation_handle;
clamy21718cc22018-06-13 13:34:243353}
3354
clamyea99ea12018-05-28 13:54:233355void NavigationControllerImpl::HandleRendererDebugURL(
3356 FrameTreeNode* frame_tree_node,
3357 const GURL& url) {
3358 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243359 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3360 // the renderer process is not live, unless it is the initial navigation
3361 // of the tab.
clamyea99ea12018-05-28 13:54:233362 if (!IsInitialNavigation()) {
3363 DiscardNonCommittedEntries();
3364 return;
3365 }
Fergal Dalyecd3b0202020-06-25 01:57:373366 // The current frame is always a main frame. If IsInitialNavigation() is
3367 // true then there have been no navigations and any frames of this tab must
3368 // be in the same renderer process. If that has crashed then the only frame
3369 // that can be revived is the main frame.
3370 frame_tree_node->render_manager()
3371 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233372 }
3373 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3374}
3375
clamy21718cc22018-06-13 13:34:243376std::unique_ptr<NavigationEntryImpl>
3377NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3378 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423379 const LoadURLParams& params,
3380 bool override_user_agent,
3381 bool should_replace_current_entry,
3382 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393383 // Browser initiated navigations might not have a blob_url_loader_factory set
3384 // in params even if the navigation is to a blob URL. If that happens, lookup
3385 // the correct url loader factory to use here.
3386 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483387 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473388 // Resolve the blob URL in the storage partition associated with the target
3389 // frame. This is the storage partition the URL will be loaded in, and only
3390 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393391 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473392 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393393 }
3394
clamy21718cc22018-06-13 13:34:243395 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443396 // extra_headers in params are \n separated; navigation entries want \r\n.
3397 std::string extra_headers_crlf;
3398 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243399
3400 // For subframes, create a pending entry with a corresponding frame entry.
3401 if (!node->IsMainFrame()) {
Rakina Zata Amniafd3c6582021-11-30 06:19:173402 CHECK(GetLastCommittedEntry());
3403 entry = GetLastCommittedEntry()->Clone();
clamy21718cc22018-06-13 13:34:243404 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:083405 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
clamy21718cc22018-06-13 13:34:243406 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Anton Bikineevf62d1bf2021-05-15 17:56:073407 params.url, absl::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203408 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393409 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:483410 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori78a749f2020-11-30 12:03:393411 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163412 // a conversion of a new navigation into a reload, we will set the right
3413 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193414 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243415 } else {
3416 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243417 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063418 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103419 params.source_site_instance.get(), params.transition_type,
3420 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Hayato Ito303654c2021-06-30 09:07:543421 blob_url_loader_factory));
clamy21718cc22018-06-13 13:34:243422 entry->set_source_site_instance(
3423 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3424 entry->SetRedirectChain(params.redirect_chain);
3425 }
3426
3427 // Set the FTN ID (only used in non-site-per-process, for tests).
3428 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243429 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423430 entry->SetIsOverridingUserAgent(override_user_agent);
3431 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543432 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243433
clamy21718cc22018-06-13 13:34:243434 switch (params.load_type) {
3435 case LOAD_TYPE_DEFAULT:
3436 break;
3437 case LOAD_TYPE_HTTP_POST:
3438 entry->SetHasPostData(true);
3439 entry->SetPostData(params.post_data);
3440 break;
3441 case LOAD_TYPE_DATA:
3442 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3443 entry->SetVirtualURL(params.virtual_url_for_data_url);
Xiaohan Wang7f8052e02022-01-14 18:44:283444#if BUILDFLAG(IS_ANDROID)
clamy21718cc22018-06-13 13:34:243445 entry->SetDataURLAsString(params.data_url_as_string);
3446#endif
3447 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3448 break;
clamy21718cc22018-06-13 13:34:243449 }
3450
3451 // TODO(clamy): NavigationEntry is meant for information that will be kept
3452 // after the navigation ended and therefore is not appropriate for
3453 // started_from_context_menu. Move started_from_context_menu to
3454 // NavigationUIData.
3455 entry->set_started_from_context_menu(params.started_from_context_menu);
3456
3457 return entry;
3458}
3459
clamyea99ea12018-05-28 13:54:233460std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423461NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3462 FrameTreeNode* node,
3463 const LoadURLParams& params,
3464 bool override_user_agent,
3465 bool should_replace_current_entry,
3466 bool has_user_gesture,
Antonio Sartori2f763d9d2021-04-21 10:04:143467 network::mojom::SourceLocationPtr source_location,
Camille Lamy5193caa2018-10-12 11:59:423468 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573469 NavigationEntryImpl* entry,
Scott Violetcf6ea7e2021-06-09 21:09:213470 FrameNavigationEntry* frame_entry) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573471 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283472 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533473 // All renderer-initiated navigations must have an initiator_origin.
3474 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513475
Camille Lamy5193caa2018-10-12 11:59:423476 GURL url_to_load;
3477 GURL virtual_url;
Anton Bikineevf62d1bf2021-05-15 17:56:073478 absl::optional<url::Origin> origin_to_commit =
3479 frame_entry ? frame_entry->committed_origin() : absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:323480
Camille Lamy2baa8022018-10-19 16:43:173481 // For main frames, rewrite the URL if necessary and compute the virtual URL
3482 // that should be shown in the address bar.
3483 if (node->IsMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423484 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173485 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423486 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423487
Camille Lamy2baa8022018-10-19 16:43:173488 // For DATA loads, override the virtual URL.
3489 if (params.load_type == LOAD_TYPE_DATA)
3490 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423491
Camille Lamy2baa8022018-10-19 16:43:173492 if (virtual_url.is_empty())
3493 virtual_url = url_to_load;
3494
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573495 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283496
Aran Gilman249eb122019-12-02 23:32:463497 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3498 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3499 // return different results, leading to a different URL in the
3500 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3501 // pending NavigationEntry, as we'll only make one call to
3502 // RewriteUrlForNavigation.
3503 VLOG_IF(1, (url_to_load != frame_entry->url()))
3504 << "NavigationRequest and FrameEntry have different URLs: "
3505 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283506
Camille Lamy2baa8022018-10-19 16:43:173507 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423508 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173509 // NavigationRequest.
3510 } else {
3511 url_to_load = params.url;
3512 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243513 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173514 }
Camille Lamy5193caa2018-10-12 11:59:423515
Ehsan Karamad44fc72112019-02-26 18:15:473516 if (node->render_manager()->is_attaching_inner_delegate()) {
3517 // Avoid starting any new navigations since this node is now preparing for
3518 // attaching an inner delegate.
3519 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203520 }
Camille Lamy5193caa2018-10-12 11:59:423521
Camille Lamy5193caa2018-10-12 11:59:423522 if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load))
3523 return nullptr;
3524
Kunihiko Sakamoto346a74e2021-03-10 08:57:483525 if (!DoesURLMatchOriginForNavigation(
3526 url_to_load, origin_to_commit,
3527 frame_entry->subresource_web_bundle_navigation_info())) {
Nasko Oskov03912102019-01-11 00:21:323528 DCHECK(false) << " url:" << url_to_load
3529 << " origin:" << origin_to_commit.value();
3530 return nullptr;
3531 }
3532
Camille Lamy5193caa2018-10-12 11:59:423533 // This will be used to set the Navigation Timing API navigationStart
3534 // parameter for browser navigations in new tabs (intents, tabs opened through
3535 // "Open link in new tab"). If the navigation must wait on the current
3536 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3537 // will be updated when the BeforeUnload ack is received.
3538 base::TimeTicks navigation_start = base::TimeTicks::Now();
3539
danakjd83d706d2020-11-25 22:11:123540 // Look for a pending commit that is to another document in this
3541 // FrameTreeNode. If one exists, then the last committed URL will not be the
3542 // current URL by the time this navigation commits.
3543 bool has_pending_cross_document_commit =
3544 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
Miyoung Shina2dd6a42021-10-07 12:19:213545 bool is_currently_error_page = node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:123546
Minggang Wangb9f3fa92021-07-01 15:30:313547 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjb952ef12021-01-14 19:58:493548 /*old_url=*/node->current_url(),
3549 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3550 has_pending_cross_document_commit, is_currently_error_page,
3551 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423552
3553 // Create the NavigationParams based on |params|.
3554
Hiroki Nakagawa4ed61282021-06-18 05:37:233555 bool is_view_source_mode = entry->IsViewSourceMode();
3556 DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme));
Charlie Harrison8c113a32019-01-07 16:08:293557
Antonio Sartori6984c742021-08-26 08:03:413558 blink::NavigationDownloadPolicy download_policy = params.download_policy;
Hiroki Nakagawa4ed61282021-06-18 05:37:233559 // Update |download_policy| if the virtual URL is view-source.
Charlie Harrison8c113a32019-01-07 16:08:293560 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253561 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293562
Minggang Wangb9f3fa92021-07-01 15:30:313563 blink::mojom::CommonNavigationParamsPtr common_params =
3564 blink::mojom::CommonNavigationParams::New(
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513565 url_to_load, params.initiator_origin,
3566 blink::mojom::Referrer::New(params.referrer.url,
3567 params.referrer.policy),
Scott Violetcf6ea7e2021-06-09 21:09:213568 params.transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513569 should_replace_current_entry, params.base_url_for_data_url,
Tarun Bansalbcd62c82022-01-18 17:27:383570 navigation_start,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513571 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
Antonio Sartori2f763d9d2021-04-21 10:04:143572 params.post_data, std::move(source_location),
arthursonzogniaf7c62c52020-02-12 10:49:413573 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273574 false /* has_text_fragment_token */,
3575 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3576 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513577 false /* is_history_navigation_in_new_child_frame */,
Nan Lind91c8152021-10-21 16:22:373578 params.input_start, network::mojom::RequestDestination::kEmpty);
Camille Lamy5193caa2018-10-12 11:59:423579
Minggang Wangb9f3fa92021-07-01 15:30:313580 blink::mojom::CommitNavigationParamsPtr commit_params =
3581 blink::mojom::CommitNavigationParams::New(
Antonio Sartori3e8de6d2021-07-26 10:28:413582 frame_entry->committed_origin(),
3583 // The correct storage key will be computed before committing the
3584 // navigation.
3585 blink::StorageKey(), network::mojom::WebSandboxFlags(),
arthursonzognid5a8d0b2021-03-11 17:36:433586 override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323587 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143588 std::vector<net::RedirectInfo>(),
3589 std::string() /* post_content_type */, common_params->url,
3590 common_params->method, params.can_load_local_resources,
Minggang Wangb9f3fa92021-07-01 15:30:313591 frame_entry->page_state().ToEncodedData(), entry->GetUniqueID(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143592 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3593 -1 /* pending_history_list_offset */,
3594 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3595 params.should_clear_history_list ? 0 : GetEntryCount(),
3596 false /* was_discarded */, is_view_source_mode,
Minggang Wangb9f3fa92021-07-01 15:30:313597 params.should_clear_history_list,
3598 blink::mojom::NavigationTiming::New(),
Minggang Wangf59db47b2021-06-16 01:56:223599 blink::mojom::WasActivatedOption::kUnknown,
Lucas Furukawa Gadania9c45682019-07-31 22:05:143600 base::UnguessableToken::Create() /* navigation_token */,
Minggang Wang7ee0c742021-06-16 16:16:513601 std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(),
Xiaohan Wang7f8052e02022-01-14 18:44:283602#if BUILDFLAG(IS_ANDROID)
Lucas Furukawa Gadania9c45682019-07-31 22:05:143603 std::string(), /* data_url_as_string */
3604#endif
arthursonzogni14379782020-05-15 09:09:273605 !params.is_renderer_initiated, /* is_browser_initiated */
Tsuyoshi Horoe86d7702019-11-29 01:52:473606 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533607 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173608 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073609 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163610 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333611 false /* origin_agent_cluster */,
Maks Orlovichc66745a2020-06-30 17:40:023612 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213613 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Nate Chapind1fe3612021-04-16 20:45:573614 false /* is_cross_browsing_instance */, nullptr /* old_page_info */,
3615 -1 /* http_response_code */,
Minggang Wangb9f3fa92021-07-01 15:30:313616 std::vector<blink::mojom::
3617 AppHistoryEntryPtr>() /* app_history_back_entries */,
Nate Chapind1fe3612021-04-16 20:45:573618 std::vector<
Minggang Wangb9f3fa92021-07-01 15:30:313619 blink::mojom::
3620 AppHistoryEntryPtr>() /* app_history_forward_entries */,
Matt Menke4e209082021-11-09 04:59:313621 std::vector<GURL>() /* early_hints_preloaded_resources */,
Clark DuVall8ee487a22021-11-10 02:25:583622 absl::nullopt /* ad_auction_components */,
3623 // This timestamp will be populated when the commit IPC is sent.
Yifan Luoe1a2e05d2022-01-12 16:47:593624 base::TimeTicks() /* commit_sent */, false /* anonymous */);
Xiaohan Wang7f8052e02022-01-14 18:44:283625#if BUILDFLAG(IS_ANDROID)
Camille Lamy5193caa2018-10-12 11:59:423626 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143627 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423628 }
3629#endif
3630
Lucas Furukawa Gadania9c45682019-07-31 22:05:143631 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423632
3633 // A form submission may happen here if the navigation is a renderer-initiated
3634 // form submission that took the OpenURL path.
3635 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3636
3637 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3638 std::string extra_headers_crlf;
3639 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093640
3641 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143642 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083643 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453644 params.initiator_frame_token.has_value()
3645 ? &(params.initiator_frame_token.value())
3646 : nullptr,
3647 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3648 request_body,
John Delaney50425f82020-04-07 16:26:213649 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473650 params.impression, params.is_pdf);
Yao Xiaodc5ed102019-06-04 19:19:093651 navigation_request->set_from_download_cross_origin_redirect(
3652 params.from_download_cross_origin_redirect);
3653 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423654}
3655
3656std::unique_ptr<NavigationRequest>
3657NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233658 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573659 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233660 FrameNavigationEntry* frame_entry,
3661 ReloadType reload_type,
3662 bool is_same_document_history_load,
Nate Chapin45f620582021-09-30 17:45:433663 bool is_history_navigation_in_new_child_frame,
3664 bool is_browser_initiated) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343665 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233666 GURL dest_url = frame_entry->url();
Rakina Zata Amnif297a802022-01-18 03:53:433667 // We should never navigate to an existing initial NavigationEntry that is the
3668 // initial NavigationEntry for the initial empty document that hasn't been
3669 // overridden by the synchronous about:blank commit, to preserve previous
3670 // behavior where trying to reload when the main frame is on the initial empty
3671 // document won't result in a navigation.
3672 // See also https://crbug.com/1277414.
3673 DCHECK(!entry->IsInitialEntryNotForSynchronousAboutBlank());
Anton Bikineevf62d1bf2021-05-15 17:56:073674 absl::optional<url::Origin> origin_to_commit =
Nasko Oskov03912102019-01-11 00:21:323675 frame_entry->committed_origin();
3676
clamyea99ea12018-05-28 13:54:233677 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013678 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573679 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233680 // We may have been redirected when navigating to the current URL.
3681 // Use the URL the user originally intended to visit as signaled by the
3682 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013683 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573684 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233685 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323686 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233687 }
3688
Ehsan Karamad44fc72112019-02-26 18:15:473689 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3690 // Avoid starting any new navigations since this node is now preparing for
3691 // attaching an inner delegate.
3692 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203693 }
3694
Camille Lamy5193caa2018-10-12 11:59:423695 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573696 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233697 return nullptr;
3698 }
3699
Kunihiko Sakamoto346a74e2021-03-10 08:57:483700 if (!DoesURLMatchOriginForNavigation(
3701 dest_url, origin_to_commit,
3702 frame_entry->subresource_web_bundle_navigation_info())) {
3703 DCHECK(false) << " url:" << dest_url
3704 << " origin:" << origin_to_commit.value();
Nasko Oskov03912102019-01-11 00:21:323705 return nullptr;
3706 }
3707
clamyea99ea12018-05-28 13:54:233708 // This will be used to set the Navigation Timing API navigationStart
3709 // parameter for browser navigations in new tabs (intents, tabs opened through
3710 // "Open link in new tab"). If the navigation must wait on the current
3711 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3712 // will be updated when the BeforeUnload ack is received.
3713 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233714
danakjd83d706d2020-11-25 22:11:123715 // Look for a pending commit that is to another document in this
3716 // FrameTreeNode. If one exists, then the last committed URL will not be the
3717 // current URL by the time this navigation commits.
3718 bool has_pending_cross_document_commit =
3719 frame_tree_node->render_manager()
3720 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493721 bool is_currently_error_page =
Miyoung Shina2dd6a42021-10-07 12:19:213722 frame_tree_node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:123723
Minggang Wangb9f3fa92021-07-01 15:30:313724 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123725 /*old_url=*/frame_tree_node->current_url(),
3726 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493727 has_pending_cross_document_commit, is_currently_error_page,
3728 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423729
3730 // A form submission may happen here if the navigation is a
3731 // back/forward/reload navigation that does a form resubmission.
3732 scoped_refptr<network::ResourceRequestBody> request_body;
3733 std::string post_content_type;
3734 if (frame_entry->method() == "POST") {
3735 request_body = frame_entry->GetPostData(&post_content_type);
3736 // Might have a LF at end.
Peter Kastingb53b81912021-04-28 19:23:303737 post_content_type = std::string(
3738 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL));
Camille Lamy5193caa2018-10-12 11:59:423739 }
3740
3741 // Create the NavigationParams based on |entry| and |frame_entry|.
Minggang Wangb9f3fa92021-07-01 15:30:313742 blink::mojom::CommonNavigationParamsPtr common_params =
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513743 entry->ConstructCommonNavigationParams(
3744 *frame_entry, request_body, dest_url,
3745 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
Tarun Bansalbcd62c82022-01-18 17:27:383746 navigation_type, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533747 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513748 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553749 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423750
3751 // TODO(clamy): |intended_as_new_entry| below should always be false once
3752 // Reload no longer leads to this being called for a pending NavigationEntry
3753 // of index -1.
Minggang Wangb9f3fa92021-07-01 15:30:313754 blink::mojom::CommitNavigationParamsPtr commit_params =
Lucas Furukawa Gadania9c45682019-07-31 22:05:143755 entry->ConstructCommitNavigationParams(
3756 *frame_entry, common_params->url, origin_to_commit,
3757 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3758 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533759 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3760 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143761 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423762
clamyea99ea12018-05-28 13:54:233763 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143764 frame_tree_node, std::move(common_params), std::move(commit_params),
Nate Chapin45f620582021-09-30 17:45:433765 is_browser_initiated, false /* was_opener_suppressed */,
Takashi Toyoshimae87b7be2021-01-22 11:51:083766 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453767 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3768 entry->extra_headers(), frame_entry, entry, request_body,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473769 nullptr /* navigation_ui_data */, absl::nullopt /* impression */,
3770 false /* is_pdf */);
clamyea99ea12018-05-28 13:54:233771}
3772
[email protected]d202a7c2012-01-04 07:53:473773void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213774 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273775 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403776
Takashi Toyoshimaea534ef22021-07-21 03:27:593777 // We need to notify the ssl_manager_ before the WebContents so the
[email protected]df1af242009-05-01 00:11:403778 // location bar will have up-to-date information about the security style
3779 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133780 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403781
Rakina Zata Amnia4e27222021-12-22 01:05:003782 delegate_->NotifyNavigationStateChanged(
Rakina Zata Amniddf10502022-01-15 02:56:553783 (details && details->should_stay_as_initial_entry)
3784 ? INVALIDATE_TYPE_ALL_BUT_KEEPS_INITIAL_NAVIGATION_ENTRY_STATUS
3785 : INVALIDATE_TYPE_ALL_AND_REMOVES_INITIAL_NAVIGATION_ENTRY_STATUS);
[email protected]ec6c05f2013-10-23 18:41:573786 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463787
[email protected]b0f724c2013-09-05 04:21:133788 // TODO(avi): Remove. http://crbug.com/170921
3789 NotificationDetails notification_details =
3790 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153791 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3792 Source<NavigationController>(this),
3793 notification_details);
initial.commit09911bf2008-07-26 23:55:293794}
3795
initial.commit09911bf2008-07-26 23:55:293796// static
[email protected]d202a7c2012-01-04 07:53:473797size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233798 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153799 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:213800 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233801}
3802
[email protected]d202a7c2012-01-04 07:53:473803void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223804 if (is_active && needs_reload_)
3805 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293806}
3807
[email protected]d202a7c2012-01-04 07:53:473808void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293809 if (!needs_reload_)
3810 return;
3811
Bo Liucdfa4b12018-11-06 00:21:443812 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3813 needs_reload_type_);
3814
initial.commit09911bf2008-07-26 23:55:293815 // Calling Reload() results in ignoring state, and not loading.
3816 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3817 // cached state.
avicc872d7242015-08-19 21:26:343818 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163819 NavigateToExistingPendingEntry(ReloadType::NONE,
Nate Chapin45f620582021-09-30 17:45:433820 FrameTreeNode::kFrameTreeNodeInvalidId,
3821 true /* is_browser_initiated */);
Rakina Zata Amnif297a802022-01-18 03:53:433822 } else if (!GetLastCommittedEntry()
3823 ->IsInitialEntryNotForSynchronousAboutBlank()) {
arthursonzogni5c4c202d2017-04-25 23:41:273824 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343825 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163826 NavigateToExistingPendingEntry(ReloadType::NONE,
Nate Chapin45f620582021-09-30 17:45:433827 FrameTreeNode::kFrameTreeNodeInvalidId,
3828 true /* is_browser_initiated */);
avicc872d7242015-08-19 21:26:343829 } else {
Rakina Zata Amnif297a802022-01-18 03:53:433830 // We should never navigate to an existing initial NavigationEntry that is
3831 // the initial NavigationEntry for the initial empty document that hasn't
3832 // been overridden by the synchronous about:blank commit, to preserve
3833 // legacy behavior where trying to reload when the main frame is on the
3834 // initial empty document won't result in a navigation. See also
3835 // https://crbug.com/1277414. If there is something to reload, the
3836 // successful reload will clear the |needs_reload_| flag. Otherwise, just do
3837 // it here.
avicc872d7242015-08-19 21:26:343838 needs_reload_ = false;
3839 }
initial.commit09911bf2008-07-26 23:55:293840}
3841
Kevin McNeeccca6172021-10-19 17:11:143842base::WeakPtr<NavigationHandle>
3843NavigationControllerImpl::LoadPostCommitErrorPage(
Carlos IL42b416592019-10-07 23:10:363844 RenderFrameHost* render_frame_host,
3845 const GURL& url,
3846 const std::string& error_page_html,
3847 net::Error error) {
Rakina Zata Amni919b7922020-12-11 09:03:133848 RenderFrameHostImpl* rfhi =
3849 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:583850
3851 // Only active documents can load post-commit error pages:
3852 // - If the document is in pending deletion, the browser already committed to
3853 // destroying this RenderFrameHost so ignore loading the error page.
3854 // - If the document is in back-forward cache, it's not allowed to navigate as
3855 // it should remain frozen. Ignore the request and evict the document from
3856 // back-forward cache.
3857 // - If the document is prerendering, it can navigate but when loading error
3858 // pages, cancel prerendering.
Fergal Daly1336ac642021-09-14 15:13:113859 if (rfhi->IsInactiveAndDisallowActivation(
3860 DisallowActivationReasonId::kLoadPostCommitErrorPage)) {
Kevin McNeeccca6172021-10-19 17:11:143861 return nullptr;
Fergal Daly1336ac642021-09-14 15:13:113862 }
Sreeja Kamishettydb8e2892021-03-10 09:30:583863
Rakina Zata Amni919b7922020-12-11 09:03:133864 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:413865
Minggang Wangb9f3fa92021-07-01 15:30:313866 blink::mojom::CommonNavigationParamsPtr common_params =
Minggang Wanga13c796e2021-07-02 05:54:433867 blink::CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:593868 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
3869 // the frame actually committed (e.g. iframe with "src" set to a
3870 // slow-responding URL). We should rewrite the URL to about:blank in this
3871 // case, as the renderer will only think a page is an error page if it has a
3872 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:133873 common_params->url = url.is_empty() ? GURL("about:blank") : url;
Minggang Wangb9f3fa92021-07-01 15:30:313874 blink::mojom::CommitNavigationParamsPtr commit_params =
Minggang Wanga13c796e2021-07-02 05:54:433875 blink::CreateCommitNavigationParams();
Antonio Sartori58591c892021-04-21 06:54:333876 commit_params->original_url = common_params->url;
John Delaney131ad362019-08-08 21:57:413877
arthursonzogni70ac7302020-05-28 08:49:053878 // Error pages have a fully permissive FramePolicy.
3879 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
3880 // error pages.
3881 commit_params->frame_policy = blink::FramePolicy();
3882
John Delaney131ad362019-08-08 21:57:413883 std::unique_ptr<NavigationRequest> navigation_request =
3884 NavigationRequest::CreateBrowserInitiated(
3885 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083886 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:193887 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453888 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:063889 "" /* extra_headers */, nullptr /* frame_entry */,
3890 nullptr /* entry */, nullptr /* post_body */,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473891 nullptr /* navigation_ui_data */, absl::nullopt /* impression */,
3892 false /* is_pdf */);
Carlos IL42b416592019-10-07 23:10:363893 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:413894 navigation_request->set_net_error(error);
3895 node->CreatedNavigationRequest(std::move(navigation_request));
3896 DCHECK(node->navigation_request());
Kevin McNeeccca6172021-10-19 17:11:143897
3898 // Calling BeginNavigation may destroy the NavigationRequest.
3899 base::WeakPtr<NavigationRequest> created_navigation_request(
3900 node->navigation_request()->GetWeakPtr());
John Delaney131ad362019-08-08 21:57:413901 node->navigation_request()->BeginNavigation();
Kevin McNeeccca6172021-10-19 17:11:143902 return created_navigation_request;
John Delaney131ad362019-08-08 21:57:413903}
3904
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573905void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213906 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093907 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:153908 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143909 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293910}
3911
[email protected]d202a7c2012-01-04 07:53:473912void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363913 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553914 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363915 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293916
initial.commit09911bf2008-07-26 23:55:293917 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293918}
[email protected]765b35502008-08-21 00:51:203919
arthursonzogni69a6a1b2019-09-17 09:23:003920void NavigationControllerImpl::DiscardNonCommittedEntries() {
Rakina Zata Amnia4e27222021-12-22 01:05:003921 DiscardNonCommittedEntriesWithCommitDetails(nullptr /* commit_details */);
3922}
3923
3924void NavigationControllerImpl::DiscardNonCommittedEntriesWithCommitDetails(
3925 LoadCommittedDetails* commit_details) {
Michael Thiessen9b14d512019-09-23 21:19:473926 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:103927 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
3928 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:293929 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:473930 return;
Michael Thiessenc5676d22019-09-25 22:32:103931 }
avi45a72532015-04-07 21:01:453932 DiscardPendingEntry(false);
Rakina Zata Amniddf10502022-01-15 02:56:553933 if (delegate_) {
Rakina Zata Amnia4e27222021-12-22 01:05:003934 delegate_->NotifyNavigationStateChanged(
Rakina Zata Amniddf10502022-01-15 02:56:553935 (commit_details && commit_details->should_stay_as_initial_entry)
3936 ? INVALIDATE_TYPE_ALL_BUT_KEEPS_INITIAL_NAVIGATION_ENTRY_STATUS
3937 : INVALIDATE_TYPE_ALL_AND_REMOVES_INITIAL_NAVIGATION_ENTRY_STATUS);
3938 }
[email protected]b12eb222013-09-10 00:11:483939}
3940
avi7c6f35e2015-05-08 17:52:383941int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3942 int nav_entry_id) const {
3943 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3944 if (entries_[i]->GetUniqueID() == nav_entry_id)
3945 return i;
3946 }
3947 return -1;
3948}
3949
[email protected]d202a7c2012-01-04 07:53:473950void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573951 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253952 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573953 DCHECK_LE(max_index, source->GetEntryCount());
Nate Chapin214a86a2021-06-21 20:35:573954 std::unique_ptr<NavigationEntryRestoreContextImpl> context =
3955 std::make_unique<NavigationEntryRestoreContextImpl>();
[email protected]e1cd5452010-08-26 18:03:253956 for (int i = 0; i < max_index; i++) {
Nate Chapin9f169072021-06-09 19:32:373957 // Normally, cloning a NavigationEntryImpl results in sharing
3958 // FrameNavigationEntries between the original and the clone. However, when
3959 // cloning from a different NavigationControllerImpl, we want to fork the
3960 // FrameNavigationEntries.
Nate Chapin9f169072021-06-09 19:32:373961 entries_.insert(entries_.begin() + i,
Nate Chapin214a86a2021-06-21 20:35:573962 source->entries_[i]->CloneWithoutSharing(context.get()));
[email protected]e1cd5452010-08-26 18:03:253963 }
Rakina Zata Amniafd3c6582021-11-30 06:19:173964 DCHECK_GE(entries_.size(), 1u);
arthursonzogni5c4c202d2017-04-25 23:41:273965 DCHECK(pending_entry_index_ == -1 ||
3966 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
Rakina Zata Amni90555282022-01-21 07:35:543967 if (!source->frame_tree_.root()->is_on_initial_empty_document()) {
3968 // If the source frame tree's root is not on the initial empty document,
3969 // also mark this FrameTree's root as such, so that the next navigation
3970 // won't replace the latest NavigationEntry due to it still being marked as
3971 // "on the initial empty document".
3972 frame_tree_.root()->SetNotOnInitialEmptyDocument();
3973 }
[email protected]e1cd5452010-08-26 18:03:253974}
[email protected]c5b88d82012-10-06 17:03:333975
3976void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:183977 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:333978 get_timestamp_callback_ = get_timestamp_callback;
3979}
[email protected]8ff00d72012-10-23 19:12:213980
Shivani Sharmaffb32b82019-04-09 16:58:473981// History manipulation intervention:
3982void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:473983 bool replace_entry,
3984 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:403985 bool is_renderer_initiated,
3986 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:453987 // Note that for a subframe, previous_document_was_activated is true if the
3988 // gesture happened in any subframe (propagated to main frame) or in the main
3989 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:473990 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:273991 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:473992 return;
3993 }
3994 if (last_committed_entry_index_ == -1)
3995 return;
3996
Shivani Sharmac4cc8922019-04-18 03:11:173997 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473998
Alexander Timine3ec4192020-04-20 16:39:403999 // Log UKM with the URL we are navigating away from.
4000 ukm::builders::HistoryManipulationIntervention(
4001 previous_page_load_ukm_source_id)
4002 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:474003}
4004
Shivani Sharmac4cc8922019-04-18 03:11:174005void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
4006 int reference_index,
4007 bool skippable) {
4008 auto* reference_entry = GetEntryAtIndex(reference_index);
4009 reference_entry->set_should_skip_on_back_forward_ui(skippable);
4010
4011 int64_t document_sequence_number =
4012 reference_entry->root_node()->frame_entry->document_sequence_number();
4013 for (int index = 0; index < GetEntryCount(); index++) {
4014 auto* entry = GetEntryAtIndex(index);
4015 if (entry->root_node()->frame_entry->document_sequence_number() ==
4016 document_sequence_number) {
4017 entry->set_should_skip_on_back_forward_ui(skippable);
4018 }
4019 }
4020}
4021
arthursonzogni66f711c2019-10-08 14:40:364022std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
4023NavigationControllerImpl::ReferencePendingEntry() {
4024 DCHECK(pending_entry_);
4025 auto pending_entry_ref =
4026 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
4027 pending_entry_refs_.insert(pending_entry_ref.get());
4028 return pending_entry_ref;
4029}
4030
4031void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
4032 // Ignore refs that don't correspond to the current pending entry.
4033 auto it = pending_entry_refs_.find(ref);
4034 if (it == pending_entry_refs_.end())
4035 return;
4036 pending_entry_refs_.erase(it);
4037
4038 if (!pending_entry_refs_.empty())
4039 return;
4040
4041 // The pending entry may be deleted before the last PendingEntryRef.
4042 if (!pending_entry_)
4043 return;
4044
4045 // We usually clear the pending entry when the matching NavigationRequest
4046 // fails, so that an arbitrary URL isn't left visible above a committed page.
4047 //
4048 // However, we do preserve the pending entry in some cases, such as on the
4049 // initial navigation of an unmodified blank tab. We also allow the delegate
4050 // to say when it's safe to leave aborted URLs in the omnibox, to let the
4051 // user edit the URL and try again. This may be useful in cases that the
4052 // committed page cannot be attacker-controlled. In these cases, we still
4053 // allow the view to clear the pending entry and typed URL if the user
4054 // requests (e.g., hitting Escape with focus in the address bar).
4055 //
4056 // Do not leave the pending entry visible if it has an invalid URL, since this
4057 // might be formatted in an unexpected or unsafe way.
4058 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:364059 bool should_preserve_entry =
4060 (pending_entry_ == GetVisibleEntry()) &&
4061 pending_entry_->GetURL().is_valid() &&
4062 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
4063 if (should_preserve_entry)
4064 return;
4065
4066 DiscardPendingEntry(true);
4067 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
4068}
4069
Titouan Rigoudy6ec70402021-02-02 15:42:194070std::unique_ptr<PolicyContainerPolicies>
4071NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:394072 RenderFrameHostImpl* rfh,
4073 bool is_same_document,
Rakina Zata Amniafd3c6582021-11-30 06:19:174074 const GURL& url) {
Antonio Sartori78a749f2020-11-30 12:03:394075 if (is_same_document) {
Rakina Zata Amniafd3c6582021-11-30 06:19:174076 CHECK(GetLastCommittedEntry());
Charlie Reis73e356242021-04-02 17:10:314077
Antonio Sartori78a749f2020-11-30 12:03:394078 FrameNavigationEntry* previous_frame_entry =
4079 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
4080
4081 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
4082 // ensure we always have a FrameNavigationEntry here.
4083 if (!previous_frame_entry)
4084 return nullptr;
4085
Titouan Rigoudy6ec70402021-02-02 15:42:194086 const PolicyContainerPolicies* previous_policies =
4087 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:394088
Titouan Rigoudy6ec70402021-02-02 15:42:194089 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:394090 return nullptr;
4091
4092 // Make a copy of the policy container for the new FrameNavigationEntry.
Antonio Sartori5d09b30f2021-03-02 09:27:164093 return previous_policies->Clone();
Antonio Sartori78a749f2020-11-30 12:03:394094 }
4095
Antonio Sartori4f5373792021-05-31 10:56:474096 return rfh->policy_container_host()->policies().Clone();
Antonio Sartori78a749f2020-11-30 12:03:394097}
4098
Hayato Ito2c8c08d02021-06-23 03:38:434099void NavigationControllerImpl::BroadcastHistoryOffsetAndLength() {
Carlos Caballeroede6f8c2021-01-28 11:01:504100 OPTIONAL_TRACE_EVENT2(
Hayato Ito2c8c08d02021-06-23 03:38:434101 "content", "NavigationControllerImpl::BroadcastHistoryOffsetAndLength",
4102 "history_offset", GetLastCommittedEntryIndex(), "history_length",
4103 GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:504104
4105 auto callback = base::BindRepeating(
4106 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
4107 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
4108 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
4109 }
4110 },
Hayato Ito2c8c08d02021-06-23 03:38:434111 GetLastCommittedEntryIndex(), GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:504112 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
4113}
4114
4115void NavigationControllerImpl::DidAccessInitialMainDocument() {
4116 // We may have left a failed browser-initiated navigation in the address bar
4117 // to let the user edit it and try again. Clear it now that content might
4118 // show up underneath it.
4119 if (!frame_tree_.IsLoading() && GetPendingEntry())
4120 DiscardPendingEntry(false);
4121
4122 // Update the URL display.
4123 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
4124}
4125
4126void NavigationControllerImpl::UpdateStateForFrame(
4127 RenderFrameHostImpl* rfhi,
4128 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:564129 OPTIONAL_TRACE_EVENT1("content",
4130 "NavigationControllerImpl::UpdateStateForFrame",
4131 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:504132 // The state update affects the last NavigationEntry associated with the given
4133 // |render_frame_host|. This may not be the last committed NavigationEntry (as
4134 // in the case of an UpdateState from a frame being swapped out). We track
4135 // which entry this is in the RenderFrameHost's nav_entry_id.
4136 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
4137 if (!entry)
4138 return;
4139
4140 FrameNavigationEntry* frame_entry =
4141 entry->GetFrameEntry(rfhi->frame_tree_node());
4142 if (!frame_entry)
4143 return;
4144
4145 // The SiteInstance might not match if we do a cross-process navigation with
4146 // replacement (e.g., auto-subframe), in which case the swap out of the old
4147 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4148 // has already been replaced and destroyed.
4149 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4150 return;
4151
4152 if (page_state == frame_entry->page_state())
4153 return; // Nothing to update.
4154
4155 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4156
4157 // The document_sequence_number and item_sequence_number recorded in the
4158 // FrameNavigationEntry should not differ from the one coming with the update,
4159 // since it must come from the same document. Do not update it if a difference
4160 // is detected, as this indicates that |frame_entry| is not the correct one.
4161 blink::ExplodedPageState exploded_state;
4162 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4163 return;
4164
4165 if (exploded_state.top.document_sequence_number !=
4166 frame_entry->document_sequence_number() ||
4167 exploded_state.top.item_sequence_number !=
4168 frame_entry->item_sequence_number()) {
4169 return;
4170 }
4171
4172 frame_entry->SetPageState(page_state);
4173 NotifyEntryChanged(entry);
4174}
4175
Aaron Colwellb731a0ae2021-03-19 19:14:474176void NavigationControllerImpl::OnStoragePartitionIdAdded(
4177 const StoragePartitionId& partition_id) {
4178 auto it = partition_config_to_id_map_.insert(
4179 std::make_pair(partition_id.config(), partition_id));
4180 bool successful_insert = it.second;
Aaron Colwellb731a0ae2021-03-19 19:14:474181 if (!successful_insert) {
4182 LogStoragePartitionIdCrashKeys(it.first->second, partition_id);
4183 }
Sharon Yangd4f48792021-06-30 17:02:534184 CHECK(successful_insert);
Aaron Colwellb731a0ae2021-03-19 19:14:474185}
4186
4187void NavigationControllerImpl::LogStoragePartitionIdCrashKeys(
4188 const StoragePartitionId& original_partition_id,
4189 const StoragePartitionId& new_partition_id) {
4190 base::debug::SetCrashKeyString(
4191 base::debug::AllocateCrashKeyString("original_partition_id",
4192 base::debug::CrashKeySize::Size256),
4193 original_partition_id.ToString());
4194
4195 base::debug::SetCrashKeyString(
4196 base::debug::AllocateCrashKeyString("new_partition_id",
4197 base::debug::CrashKeySize::Size256),
4198 new_partition_id.ToString());
Aaron Colwellb731a0ae2021-03-19 19:14:474199}
4200
Minggang Wangb9f3fa92021-07-01 15:30:314201std::vector<blink::mojom::AppHistoryEntryPtr>
Nate Chapind1fe3612021-04-16 20:45:574202NavigationControllerImpl::PopulateSingleAppHistoryEntryVector(
4203 Direction direction,
4204 int entry_index,
4205 const url::Origin& pending_origin,
4206 FrameTreeNode* node,
4207 SiteInstance* site_instance,
Nate Chapin63db0d12022-01-20 22:03:304208 int64_t pending_item_sequence_number,
4209 int64_t pending_document_sequence_number) {
Minggang Wangb9f3fa92021-07-01 15:30:314210 std::vector<blink::mojom::AppHistoryEntryPtr> entries;
Rakina Zata Amniafd3c6582021-11-30 06:19:174211 if (GetLastCommittedEntry()->IsInitialEntry()) {
4212 // Don't process the initial entry.
4213 DCHECK_EQ(GetEntryCount(), 1);
4214 return entries;
4215 }
Nate Chapind1fe3612021-04-16 20:45:574216 int offset = direction == Direction::kForward ? 1 : -1;
Nate Chapin63db0d12022-01-20 22:03:304217 int64_t previous_item_sequence_number = pending_item_sequence_number;
Nate Chapind1fe3612021-04-16 20:45:574218 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4219 i += offset) {
4220 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
4221 if (!frame_entry || !frame_entry->committed_origin())
4222 break;
Nate Chapind1fe3612021-04-16 20:45:574223 if (!pending_origin.IsSameOriginWith(*frame_entry->committed_origin()))
4224 break;
4225 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4226 continue;
4227 blink::ExplodedPageState exploded_page_state;
4228 if (blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4229 &exploded_page_state)) {
4230 blink::ExplodedFrameState frame_state = exploded_page_state.top;
Nate Chapin63db0d12022-01-20 22:03:304231
4232 // If the document represented by this FNE hid its full url from appearing
4233 // in a referrer via a "no-referrer" or "origin" referrer policy, censor
4234 // the url in appHistory as well (unless we're navigating to that
4235 // document).
4236 std::u16string url;
4237 if (pending_document_sequence_number ==
4238 frame_entry->document_sequence_number() ||
4239 !frame_entry->protect_url_in_app_history()) {
4240 url = frame_state.url_string.value_or(std::u16string());
4241 }
4242
Minggang Wangb9f3fa92021-07-01 15:30:314243 blink::mojom::AppHistoryEntryPtr entry =
4244 blink::mojom::AppHistoryEntry::New(
4245 frame_state.app_history_key.value_or(std::u16string()),
Nate Chapin63db0d12022-01-20 22:03:304246 frame_state.app_history_id.value_or(std::u16string()), url,
Nate Chapinab5c3a712021-11-18 22:17:094247 frame_state.item_sequence_number,
4248 frame_state.document_sequence_number,
4249 frame_state.app_history_state.value_or(std::u16string()));
Rakina Zata Amniafd3c6582021-11-30 06:19:174250
Nate Chapin63db0d12022-01-20 22:03:304251 DCHECK(entry->url.empty() ||
4252 pending_origin.CanBeDerivedFrom(GURL(entry->url)));
Nate Chapind1fe3612021-04-16 20:45:574253 entries.push_back(std::move(entry));
4254 previous_item_sequence_number = frame_entry->item_sequence_number();
4255 }
4256 }
4257 // If |entries| was constructed by iterating backwards from
4258 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4259 // earliest-at-the-front. Reverse it.
4260 if (direction == Direction::kBack)
4261 std::reverse(entries.begin(), entries.end());
4262 return entries;
4263}
4264
4265void NavigationControllerImpl::PopulateAppHistoryEntryVectors(
4266 NavigationRequest* request) {
4267 url::Origin pending_origin =
4268 request->commit_params().origin_to_commit
4269 ? *request->commit_params().origin_to_commit
4270 : url::Origin::Create(request->common_params().url);
4271
4272 FrameTreeNode* node = request->frame_tree_node();
4273 scoped_refptr<SiteInstance> site_instance =
4274 request->GetRenderFrameHost()->GetSiteInstance();
4275
4276 // NOTE: |entry_index| is an estimate of the index where this entry will
4277 // commit, but it may be wrong in corner cases (e.g., if we are at the max
4278 // entry limit, the earliest entry will be dropped). This is ok because this
4279 // algorithm only uses |entry_index| to walk the entry list as it stands right
4280 // now, and it isn't saved for anything post-commit.
4281 int entry_index = GetPendingEntryIndex();
4282 bool will_create_new_entry = false;
4283 if (NavigationTypeUtils::IsReload(request->common_params().navigation_type) ||
4284 request->common_params().should_replace_current_entry) {
4285 entry_index = GetLastCommittedEntryIndex();
4286 } else if (entry_index == -1) {
4287 will_create_new_entry = true;
4288 entry_index = GetLastCommittedEntryIndex() + 1;
4289 }
4290
4291 int64_t pending_item_sequence_number = 0;
Nate Chapin63db0d12022-01-20 22:03:304292 int64_t pending_document_sequence_number = 0;
Nate Chapind1fe3612021-04-16 20:45:574293 if (auto* pending_entry = GetPendingEntry()) {
Nate Chapin63db0d12022-01-20 22:03:304294 if (auto* frame_entry = pending_entry->GetFrameEntry(node)) {
Nate Chapind1fe3612021-04-16 20:45:574295 pending_item_sequence_number = frame_entry->item_sequence_number();
Nate Chapin63db0d12022-01-20 22:03:304296 pending_document_sequence_number =
4297 frame_entry->document_sequence_number();
4298 }
Nate Chapind1fe3612021-04-16 20:45:574299 }
4300
4301 request->set_app_history_back_entries(PopulateSingleAppHistoryEntryVector(
4302 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
Nate Chapin63db0d12022-01-20 22:03:304303 pending_item_sequence_number, pending_document_sequence_number));
Nate Chapind1fe3612021-04-16 20:45:574304
4305 // Don't populate forward entries if they will be truncated by a new entry.
4306 if (!will_create_new_entry) {
4307 request->set_app_history_forward_entries(
4308 PopulateSingleAppHistoryEntryVector(
4309 Direction::kForward, entry_index, pending_origin, node,
Nate Chapin63db0d12022-01-20 22:03:304310 site_instance.get(), pending_item_sequence_number,
4311 pending_document_sequence_number));
Nate Chapind1fe3612021-04-16 20:45:574312 }
4313}
4314
Nate Chapinfbfe5af2021-06-10 17:22:084315NavigationControllerImpl::HistoryNavigationAction
4316NavigationControllerImpl::ShouldNavigateToEntryForAppHistoryKey(
4317 FrameNavigationEntry* current_entry,
4318 FrameNavigationEntry* target_entry,
4319 const std::string& app_history_key) {
4320 if (!target_entry || !target_entry->committed_origin())
4321 return HistoryNavigationAction::kStopLooking;
4322 if (current_entry->site_instance() != target_entry->site_instance())
4323 return HistoryNavigationAction::kStopLooking;
4324 if (!current_entry->committed_origin()->IsSameOriginWith(
4325 *target_entry->committed_origin())) {
4326 return HistoryNavigationAction::kStopLooking;
4327 }
4328
4329 // NOTE: We don't actually care between kSameDocument and
4330 // kDifferentDocument, so always use kDifferentDocument by convention.
4331 if (target_entry->app_history_key() == app_history_key)
4332 return HistoryNavigationAction::kDifferentDocument;
4333 return HistoryNavigationAction::kKeepLooking;
4334}
4335
4336void NavigationControllerImpl::NavigateToAppHistoryKey(FrameTreeNode* node,
4337 const std::string& key) {
4338 FrameNavigationEntry* current_entry =
4339 GetLastCommittedEntry()->GetFrameEntry(node);
4340 if (!current_entry)
4341 return;
4342
4343 // We want to find the nearest matching entry that is contiguously
4344 // same-instance and same-origin. Check back first, then forward.
4345 // TODO(japhet): Link spec here once it exists.
4346 for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) {
4347 auto result = ShouldNavigateToEntryForAppHistoryKey(
4348 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4349 if (result == HistoryNavigationAction::kStopLooking)
4350 break;
4351 if (result != HistoryNavigationAction::kKeepLooking) {
Nate Chapin2892b37c2021-10-08 17:33:114352 GoToIndex(i, FrameTreeNode::kFrameTreeNodeInvalidId,
4353 false /* is_browser_initiated*/);
Nate Chapinfbfe5af2021-06-10 17:22:084354 return;
4355 }
4356 }
4357 for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) {
4358 auto result = ShouldNavigateToEntryForAppHistoryKey(
4359 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4360 if (result == HistoryNavigationAction::kStopLooking)
4361 break;
4362 if (result != HistoryNavigationAction::kKeepLooking) {
Nate Chapin2892b37c2021-10-08 17:33:114363 GoToIndex(i, FrameTreeNode::kFrameTreeNodeInvalidId,
4364 false /* is_browser_initiated*/);
Nate Chapinfbfe5af2021-06-10 17:22:084365 return;
4366 }
4367 }
4368}
4369
Nate Chapin63db0d12022-01-20 22:03:304370bool NavigationControllerImpl::ShouldProtectUrlInAppHistory(
4371 network::mojom::ReferrerPolicy referrer_policy) {
4372 return referrer_policy == network::mojom::ReferrerPolicy::kNever ||
4373 referrer_policy == network::mojom::ReferrerPolicy::kOrigin;
4374}
4375
shivanigithubf405bf0d2021-11-05 17:58:334376bool NavigationControllerImpl::ShouldMaintainTrivialSessionHistory(
4377 const FrameTreeNode* frame_tree_node) const {
4378 // TODO(https://crbug.com/1197384): We may have to add portals in addition to
4379 // prerender and fenced frames. This should be kept in sync with
Hayato Ito7a80db42021-07-05 06:18:544380 // LocalFrame version, LocalFrame::ShouldMaintainTrivialSessionHistory.
shivanigithubf405bf0d2021-11-05 17:58:334381 return frame_tree_.is_prerendering() ||
4382 frame_tree_node->IsInFencedFrameTree();
Hayato Ito7a80db42021-07-05 06:18:544383}
4384
[email protected]8ff00d72012-10-23 19:12:214385} // namespace content