blob: 5f71a5cac564c9668990a9e77c9c7a3e17059fbb [file] [log] [blame]
[email protected]d4a8ca482013-10-30 21:06:401// Copyright 2013 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
creis4e2ecb72015-06-20 00:46:305/*
6 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
7 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved.
9 * (http://www.torchmobile.com/)
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
21 * its contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
25 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
28 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
danakjc492bf82020-09-09 20:02:4436#include "content/browser/renderer_host/navigation_controller_impl.h"
initial.commit09911bf2008-07-26 23:55:2937
Lei Zhang96031532019-10-10 19:05:4738#include <algorithm>
dcheng36b6aec92015-12-26 06:16:3639#include <utility>
40
[email protected]c5b88d82012-10-06 17:03:3341#include "base/bind.h"
[email protected]eabfe1912014-05-12 10:07:2842#include "base/command_line.h"
initial.commit09911bf2008-07-26 23:55:2943#include "base/logging.h"
asvitkine30330812016-08-30 04:01:0844#include "base/metrics/histogram_macros.h"
Chris Hamilton83272dc2021-02-23 00:24:0245#include "base/stl_util.h"
Peter Kastingb53b81912021-04-28 19:23:3046#include "base/strings/string_piece.h"
[email protected]348fbaac2013-06-11 06:31:5147#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0048#include "base/strings/utf_string_conversions.h"
[email protected]a43858f2013-06-28 15:18:3749#include "base/time/time.h"
Carlos Caballero40b0efd2021-01-26 11:55:0050#include "base/trace_event/optional_trace_event.h"
Carlos Caballeroede6f8c2021-01-28 11:01:5051#include "base/trace_event/trace_conversion_helper.h"
ssid3e765612015-01-28 04:03:4252#include "base/trace_event/trace_event.h"
servolkf3955532015-05-16 00:01:5953#include "build/build_config.h"
[email protected]eabfe1912014-05-12 10:07:2854#include "cc/base/switches.h"
jamescookda2505812015-03-20 18:01:1855#include "content/browser/bad_message.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3956#include "content/browser/blob_storage/chrome_blob_storage_context.h"
[email protected]825b1662012-03-12 19:07:3157#include "content/browser/browser_url_handler_impl.h"
[email protected]5f2aa722013-08-07 16:59:4158#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0459#include "content/browser/dom_storage/session_storage_namespace_impl.h"
danakjc492bf82020-09-09 20:02:4460#include "content/browser/renderer_host/debug_urls.h"
61#include "content/browser/renderer_host/frame_tree.h"
Carlos Caballero40b0efd2021-01-26 11:55:0062#include "content/browser/renderer_host/frame_tree_node.h"
danakjc492bf82020-09-09 20:02:4463#include "content/browser/renderer_host/navigation_entry_impl.h"
Nate Chapin214a86a2021-06-21 20:35:5764#include "content/browser/renderer_host/navigation_entry_restore_context_impl.h"
danakjc492bf82020-09-09 20:02:4465#include "content/browser/renderer_host/navigation_request.h"
66#include "content/browser/renderer_host/navigator.h"
67#include "content/browser/renderer_host/render_frame_host_delegate.h"
Carlos Caballero40b0efd2021-01-26 11:55:0068#include "content/browser/renderer_host/render_view_host_impl.h"
Sharon Yangd70a5392021-10-26 23:06:3269#include "content/browser/site_info.h"
[email protected]b6583592012-01-25 19:52:3370#include "content/browser/site_instance_impl.h"
Kunihiko Sakamoto346a74e2021-03-10 08:57:4871#include "content/browser/web_package/subresource_web_bundle_navigation_info.h"
Tsuyoshi Horo37493af2019-11-21 23:43:1472#include "content/browser/web_package/web_bundle_navigation_info.h"
Camille Lamy5193caa2018-10-12 11:59:4273#include "content/common/content_constants_internal.h"
Nate Chapind1fe3612021-04-16 20:45:5774#include "content/common/navigation_params_utils.h"
Nasko Oskovae49e292020-08-13 02:08:5175#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1176#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1977#include "content/public/browser/content_browser_client.h"
Fergal Daly1336ac642021-09-14 15:13:1178#include "content/public/browser/disallow_activation_reason.h"
[email protected]d9083482012-01-06 00:38:4679#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0080#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3881#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1682#include "content/public/browser/notification_types.h"
Lei Zhang96031532019-10-10 19:05:4783#include "content/public/browser/render_view_host.h"
[email protected]9677a3c2012-12-22 04:18:5884#include "content/public/browser/render_widget_host.h"
85#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1086#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3487#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:1988#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:3889#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:1090#include "content/public/common/content_features.h"
Lei Zhang96031532019-10-10 19:05:4791#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4392#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:5993#include "media/base/mime_util.h"
[email protected]a23de8572009-06-03 02:16:3294#include "net/base/escape.h"
Arthur Sonzogni620cec62018-12-13 13:08:5795#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:1896#include "services/metrics/public/cpp/ukm_builders.h"
97#include "services/metrics/public/cpp/ukm_recorder.h"
Yue Ru Sun128804932020-09-30 22:19:1798#include "services/metrics/public/cpp/ukm_source_id.h"
Nan Lind91c8152021-10-21 16:22:3799#include "services/network/public/mojom/fetch_api.mojom.h"
[email protected]9677a3c2012-12-22 04:18:58100#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:39101#include "third_party/blink/public/common/blob/blob_utils.h"
Gyuyoung Kim107c2a02021-04-13 01:49:30102#include "third_party/blink/public/common/chrome_debug_urls.h"
Miyoung Shin1c565c912021-03-17 12:11:21103#include "third_party/blink/public/common/history/session_history_constants.h"
Blink Reformata30d4232018-04-07 15:31:06104#include "third_party/blink/public/common/mime_util/mime_util.h"
Minggang Wanga13c796e2021-07-02 05:54:43105#include "third_party/blink/public/common/navigation/navigation_params.h"
Carlos Caballeroede6f8c2021-01-28 11:01:50106#include "third_party/blink/public/common/page_state/page_state_serialization.h"
Minggang Wangb9f3fa92021-07-01 15:30:31107#include "third_party/blink/public/mojom/navigation/navigation_params.mojom.h"
Minggang Wang7ee0c742021-06-16 16:16:51108#include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h"
[email protected]cca6f392014-05-28 21:32:26109#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:29110
[email protected]8ff00d72012-10-23 19:12:21111namespace content {
[email protected]e9ba4472008-09-14 15:42:43112namespace {
113
114// Invoked when entries have been pruned, or removed. For example, if the
115// current entries are [google, digg, yahoo], with the current entry google,
116// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47117void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50118 int index,
[email protected]c12bf1a12008-09-17 16:28:49119 int count) {
[email protected]8ff00d72012-10-23 19:12:21120 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50121 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49122 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14123 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43124}
125
[email protected]e9ba4472008-09-14 15:42:43126// Configure all the NavigationEntries in entries for restore. This resets
127// the transition type to reload and makes sure the content state isn't empty.
128void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57129 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48130 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47131 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43132 // Use a transition type of reload so that we don't incorrectly increase
133 // the typed count.
Lei Zhang96031532019-10-10 19:05:47134 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
135 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43136 }
137}
138
[email protected]bf70edce2012-06-20 22:32:22139// Determines whether or not we should be carrying over a user agent override
140// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57141bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22142 return last_entry && last_entry->GetIsOverridingUserAgent();
143}
144
Camille Lamy5193caa2018-10-12 11:59:42145// Determines whether to override user agent for a navigation.
146bool ShouldOverrideUserAgent(
147 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57148 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42149 switch (override_user_agent) {
150 case NavigationController::UA_OVERRIDE_INHERIT:
151 return ShouldKeepOverride(last_committed_entry);
152 case NavigationController::UA_OVERRIDE_TRUE:
153 return true;
154 case NavigationController::UA_OVERRIDE_FALSE:
155 return false;
Camille Lamy5193caa2018-10-12 11:59:42156 }
157 NOTREACHED();
158 return false;
159}
160
Rakina Zata Amni312822d72021-06-04 16:13:37161// Returns true if this navigation should be treated as a reload. For e.g.
clamy0a656e42018-02-06 18:18:28162// navigating to the last committed url via the address bar or clicking on a
Rakina Zata Amni312822d72021-06-04 16:13:37163// link which results in a navigation to the last committed URL (but wasn't
164// converted to do a replacement navigation in the renderer), etc.
Fergal Daly766177d2020-07-07 07:54:04165// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
166// |base_url_for_data_url|, |transition_type| correspond to the new navigation
167// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
168// navigation that committed.
169bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
170 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57171 const GURL& virtual_url,
172 const GURL& base_url_for_data_url,
173 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57174 bool is_post,
Rakina Zata Amni312822d72021-06-04 16:13:37175 bool should_replace_current_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57176 NavigationEntryImpl* last_committed_entry) {
Rakina Zata Amni312822d72021-06-04 16:13:37177 // Navigations intended to do a replacement shouldn't be converted to do a
178 // reload.
179 if (should_replace_current_entry)
clamy0a656e42018-02-06 18:18:28180 return false;
clamy0a656e42018-02-06 18:18:28181 // Only convert to reload if at least one navigation committed.
Rakina Zata Amniafd3c6582021-11-30 06:19:17182 if (last_committed_entry->IsInitialEntry())
ananta3bdd8ae2016-12-22 17:11:55183 return false;
184
arthursonzogni7a8243682017-12-14 16:41:42185 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28186 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42187 return false;
188
ananta3bdd8ae2016-12-22 17:11:55189 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
190 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
191 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28192 bool transition_type_can_be_converted = false;
193 if (ui::PageTransitionCoreTypeIs(transition_type,
194 ui::PAGE_TRANSITION_RELOAD) &&
195 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
196 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55197 }
clamy0a656e42018-02-06 18:18:28198 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55199 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28200 transition_type_can_be_converted = true;
201 }
202 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
203 transition_type_can_be_converted = true;
204 if (!transition_type_can_be_converted)
205 return false;
206
207 // This check is required for cases like view-source:, etc. Here the URL of
208 // the navigation entry would contain the url of the page, while the virtual
209 // URL contains the full URL including the view-source prefix.
210 if (virtual_url != last_committed_entry->GetVirtualURL())
211 return false;
212
Fergal Daly766177d2020-07-07 07:54:04213 // Check that the URLs match.
214 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
215 // If there's no frame entry then by definition the URLs don't match.
216 if (!frame_entry)
217 return false;
218
219 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28220 return false;
221
222 // This check is required for Android WebView loadDataWithBaseURL. Apps
223 // can pass in anything in the base URL and we need to ensure that these
224 // match before classifying it as a reload.
225 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
226 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
227 return false;
ananta3bdd8ae2016-12-22 17:11:55228 }
229
clamy0a656e42018-02-06 18:18:28230 // Skip entries with SSL errors.
231 if (last_committed_entry->ssl_error())
232 return false;
233
234 // Don't convert to a reload when the last navigation was a POST or the new
235 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04236 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28237 return false;
238
239 return true;
ananta3bdd8ae2016-12-22 17:11:55240}
241
Nasko Oskov03912102019-01-11 00:21:32242bool DoesURLMatchOriginForNavigation(
243 const GURL& url,
Anton Bikineevf62d1bf2021-05-15 17:56:07244 const absl::optional<url::Origin>& origin,
Kunihiko Sakamoto346a74e2021-03-10 08:57:48245 SubresourceWebBundleNavigationInfo*
246 subresource_web_bundle_navigation_info) {
Nasko Oskov03912102019-01-11 00:21:32247 // If there is no origin supplied there is nothing to match. This can happen
248 // for navigations to a pending entry and therefore it should be allowed.
249 if (!origin)
250 return true;
251
Kunihiko Sakamoto260ee0a2021-10-15 01:44:08252 // Urn: and uuid-in-package: subframe from WebBundle has an opaque origin
253 // derived from the Bundle's origin.
254 // TODO(https://crbug.com/1257045): Remove urn: scheme support.
255 if ((url.SchemeIs(url::kUrnScheme) ||
256 url.SchemeIs(url::kUuidInPackageScheme)) &&
257 subresource_web_bundle_navigation_info) {
Kunihiko Sakamoto346a74e2021-03-10 08:57:48258 return origin->CanBeDerivedFrom(
259 subresource_web_bundle_navigation_info->bundle_url());
260 }
261
Nasko Oskov03912102019-01-11 00:21:32262 return origin->CanBeDerivedFrom(url);
263}
264
Anton Bikineevf62d1bf2021-05-15 17:56:07265absl::optional<url::Origin> GetCommittedOriginForFrameEntry(
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12266 const mojom::DidCommitProvisionalLoadParams& params,
267 NavigationRequest* request) {
Nasko Oskov03912102019-01-11 00:21:32268 // Error pages commit in an opaque origin, yet have the real URL that resulted
269 // in an error as the |params.url|. Since successful reload of an error page
270 // should commit in the correct origin, setting the opaque origin on the
271 // FrameNavigationEntry will be incorrect.
Rakina Zata Amniafd3c6582021-11-30 06:19:17272 if (request && request->DidEncounterError())
Anton Bikineevf62d1bf2021-05-15 17:56:07273 return absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:32274
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12275 // We also currently don't save committed origins for loadDataWithBaseURL
276 // navigations (probably accidentally). Without this check, navigations to
277 // the FrameNavigationEntry might fail the DoesURLMatchOriginForNavigation()
278 // check since the origin will be based on the base URL instead of the data:
279 // URL used for the navigation.
280 // TODO(https://crbug.com/1198406): Save committed origin in
281 // FrameNavigationEntry for this case too.
Rakina Zata Amniafd3c6582021-11-30 06:19:17282 if (request && request->IsLoadDataWithBaseURL())
Anton Bikineevf62d1bf2021-05-15 17:56:07283 return absl::nullopt;
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12284
Anton Bikineevf62d1bf2021-05-15 17:56:07285 return absl::make_optional(params.origin);
Nasko Oskov03912102019-01-11 00:21:32286}
287
Camille Lamy5193caa2018-10-12 11:59:42288bool IsValidURLForNavigation(bool is_main_frame,
289 const GURL& virtual_url,
290 const GURL& dest_url) {
291 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
292 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
293 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
294 << virtual_url.possibly_invalid_spec();
295 return false;
296 }
297
298 // Don't attempt to navigate to non-empty invalid URLs.
299 if (!dest_url.is_valid() && !dest_url.is_empty()) {
300 LOG(WARNING) << "Refusing to load invalid URL: "
301 << dest_url.possibly_invalid_spec();
302 return false;
303 }
304
305 // The renderer will reject IPC messages with URLs longer than
306 // this limit, so don't attempt to navigate with a longer URL.
307 if (dest_url.spec().size() > url::kMaxURLChars) {
308 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
309 << " characters.";
310 return false;
311 }
312
Aaron Colwell33109c592020-04-21 21:31:19313 // Reject renderer debug URLs because they should have been handled before
314 // we get to this point. This check handles renderer debug URLs
315 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
316 // provides defense-in-depth if a renderer debug URL manages to get here via
317 // some other path. We want to reject the navigation here so it doesn't
318 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30319 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19320 LOG(WARNING) << "Refusing to load renderer debug URL: "
321 << dest_url.possibly_invalid_spec();
322 return false;
323 }
324
Camille Lamy5193caa2018-10-12 11:59:42325 return true;
326}
327
Mikel Astizba9cf2fd2017-12-17 10:38:10328// See replaced_navigation_entry_data.h for details: this information is meant
329// to ensure |*output_entry| keeps track of its original URL (landing page in
330// case of server redirects) as it gets replaced (e.g. history.replaceState()),
331// without overwriting it later, for main frames.
332void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57333 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10334 NavigationEntryImpl* output_entry) {
Rakina Zata Amniafd3c6582021-11-30 06:19:17335 if (output_entry->GetReplacedEntryData().has_value() ||
336 replaced_entry->IsInitialEntry()) {
Mikel Astizba9cf2fd2017-12-17 10:38:10337 return;
Rakina Zata Amniafd3c6582021-11-30 06:19:17338 }
Mikel Astizba9cf2fd2017-12-17 10:38:10339
340 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57341 data.first_committed_url = replaced_entry->GetURL();
342 data.first_timestamp = replaced_entry->GetTimestamp();
343 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29344 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10345}
346
Minggang Wangb9f3fa92021-07-01 15:30:31347blink::mojom::NavigationType GetNavigationType(
348 const GURL& old_url,
349 const GURL& new_url,
350 ReloadType reload_type,
351 NavigationEntryImpl* entry,
352 const FrameNavigationEntry& frame_entry,
353 bool has_pending_cross_document_commit,
354 bool is_currently_error_page,
355 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23356 // Reload navigations
357 switch (reload_type) {
358 case ReloadType::NORMAL:
Minggang Wangb9f3fa92021-07-01 15:30:31359 return blink::mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23360 case ReloadType::BYPASSING_CACHE:
Minggang Wangb9f3fa92021-07-01 15:30:31361 return blink::mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23362 case ReloadType::ORIGINAL_REQUEST_URL:
Minggang Wangb9f3fa92021-07-01 15:30:31363 return blink::mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23364 case ReloadType::NONE:
365 break; // Fall through to rest of function.
366 }
367
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08368 if (entry->IsRestored()) {
Minggang Wangb9f3fa92021-07-01 15:30:31369 return entry->GetHasPostData()
370 ? blink::mojom::NavigationType::RESTORE_WITH_POST
371 : blink::mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23372 }
373
danakjb952ef12021-01-14 19:58:49374 const bool can_be_same_document =
375 // A pending cross-document commit means this navigation will not occur in
376 // the current document, as that document would end up being replaced in
377 // the meantime.
378 !has_pending_cross_document_commit &&
379 // If the current document is an error page, we should always treat it as
380 // a different-document navigation so that we'll attempt to load the
381 // document we're navigating to (and not stay in the current error page).
382 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12383
clamyea99ea12018-05-28 13:54:23384 // History navigations.
385 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12386 return can_be_same_document && is_same_document_history_load
Minggang Wangb9f3fa92021-07-01 15:30:31387 ? blink::mojom::NavigationType::HISTORY_SAME_DOCUMENT
388 : blink::mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23389 }
390 DCHECK(!is_same_document_history_load);
391
392 // A same-document fragment-navigation happens when the only part of the url
393 // that is modified is after the '#' character.
394 //
395 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12396 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23397 //
398 // Note: this check is only valid for navigations that are not history
399 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
400 // history navigation from 'A#foo' to 'A#bar' is not a same-document
401 // navigation, but a different-document one. This is why history navigation
402 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47403 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
404 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12405
406 // The one case where we do the wrong thing here and incorrectly choose
407 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
408 // the renderer is a frameset. All frameset navigations should be
409 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
410 // navigation would do the right thing, as it would send it to the browser and
411 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
412 // into this method). But since we can't tell that case here for browser-
413 // initiated navigations, we have to get the renderer involved. In that case
414 // the navigation would be restarted due to the renderer spending a reply of
415 // mojom::CommitResult::RestartCrossDocument.
416
417 return can_be_same_document && is_same_doc
Minggang Wangb9f3fa92021-07-01 15:30:31418 ? blink::mojom::NavigationType::SAME_DOCUMENT
419 : blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23420}
421
Camille Lamy5193caa2018-10-12 11:59:42422// Adjusts the original input URL if needed, to get the URL to actually load and
423// the virtual URL, which may differ.
424void RewriteUrlForNavigation(const GURL& original_url,
425 BrowserContext* browser_context,
426 GURL* url_to_load,
427 GURL* virtual_url,
428 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42429 // Allow the browser URL handler to rewrite the URL. This will, for example,
430 // remove "view-source:" from the beginning of the URL to get the URL that
431 // will actually be loaded. This real URL won't be shown to the user, just
432 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31433 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42434 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
435 url_to_load, browser_context, reverse_on_redirect);
436}
437
438#if DCHECK_IS_ON()
439// Helper sanity check function used in debug mode.
440void ValidateRequestMatchesEntry(NavigationRequest* request,
441 NavigationEntryImpl* entry) {
442 if (request->frame_tree_node()->IsMainFrame()) {
443 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
444 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
Minggang Wangb9f3fa92021-07-01 15:30:31445 ui::PageTransitionFromInt(request->common_params().transition),
446 entry->GetTransitionType()));
Camille Lamy5193caa2018-10-12 11:59:42447 }
Nasko Oskovc36327d2019-01-03 23:23:04448 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42449 entry->should_clear_history_list());
450 DCHECK_EQ(request->common_params().has_user_gesture,
451 entry->has_user_gesture());
452 DCHECK_EQ(request->common_params().base_url_for_data_url,
453 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04454 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42455 entry->GetCanLoadLocalResources());
456 DCHECK_EQ(request->common_params().started_from_context_menu,
457 entry->has_started_from_context_menu());
458
459 FrameNavigationEntry* frame_entry =
460 entry->GetFrameEntry(request->frame_tree_node());
461 if (!frame_entry) {
462 NOTREACHED();
463 return;
464 }
465
Camille Lamy5193caa2018-10-12 11:59:42466 DCHECK_EQ(request->common_params().method, frame_entry->method());
467
Nasko Oskovc36327d2019-01-03 23:23:04468 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42469 if (redirect_size == frame_entry->redirect_chain().size()) {
470 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04471 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42472 frame_entry->redirect_chain()[i]);
473 }
474 } else {
475 NOTREACHED();
476 }
477}
478#endif // DCHECK_IS_ON()
479
Dave Tapuska8bfd84c2019-03-26 20:47:16480// Returns whether the session history NavigationRequests in |navigations|
481// would stay within the subtree of the sandboxed iframe in
482// |sandbox_frame_tree_node_id|.
483bool DoesSandboxNavigationStayWithinSubtree(
484 int sandbox_frame_tree_node_id,
485 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
486 for (auto& item : navigations) {
487 bool within_subtree = false;
488 // Check whether this NavigationRequest affects a frame within the
489 // sandboxed frame's subtree by walking up the tree looking for the
490 // sandboxed frame.
491 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03492 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16493 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
494 within_subtree = true;
495 break;
496 }
497 }
498 if (!within_subtree)
499 return false;
500 }
501 return true;
502}
503
[email protected]e9ba4472008-09-14 15:42:43504} // namespace
505
arthursonzogni66f711c2019-10-08 14:40:36506// NavigationControllerImpl::PendingEntryRef------------------------------------
507
508NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
509 base::WeakPtr<NavigationControllerImpl> controller)
510 : controller_(controller) {}
511
512NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
513 if (!controller_) // Can be null with interstitials.
514 return;
515
516 controller_->PendingEntryRefDeleted(this);
517}
518
[email protected]d202a7c2012-01-04 07:53:47519// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29520
[email protected]23a918b2014-07-15 09:51:36521const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23522
[email protected]765b35502008-08-21 00:51:20523// static
[email protected]d202a7c2012-01-04 07:53:47524size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23525 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20526
[email protected]e6fec472013-05-14 05:29:02527// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20528// when testing.
529static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29530
[email protected]71fde352011-12-29 03:29:56531// static
dcheng9bfa5162016-04-09 01:00:57532std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
533 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10534 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07535 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10536 ui::PageTransition transition,
537 bool is_renderer_initiated,
538 const std::string& extra_headers,
539 BrowserContext* browser_context,
540 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
541 return NavigationControllerImpl::CreateNavigationEntry(
542 url, referrer, std::move(initiator_origin),
543 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Hayato Ito303654c2021-06-30 09:07:54544 extra_headers, browser_context, std::move(blob_url_loader_factory));
Lukasz Anforowicz641234d52019-11-07 21:07:10545}
546
547// static
548std::unique_ptr<NavigationEntryImpl>
549NavigationControllerImpl::CreateNavigationEntry(
550 const GURL& url,
551 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07552 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10553 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57554 ui::PageTransition transition,
555 bool is_renderer_initiated,
556 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09557 BrowserContext* browser_context,
Hayato Ito303654c2021-06-30 09:07:54558 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
Camille Lamy5193caa2018-10-12 11:59:42559 GURL url_to_load;
560 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56561 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42562 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
563 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56564
Lukasz Anforowicz641234d52019-11-07 21:07:10565 // Let the NTP override the navigation params and pretend that this is a
566 // browser-initiated, bookmark-like navigation.
567 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violetcf6ea7e2021-06-09 21:09:21568 source_site_instance, &transition, &is_renderer_initiated, &referrer,
569 &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10570
Patrick Monettef507e982019-06-19 20:18:06571 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28572 nullptr, // The site instance for tabs is sent on navigation
573 // (WebContents::GetSiteInstance).
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58574 url_to_load, referrer, initiator_origin, std::u16string(), transition,
Rakina Zata Amniafd3c6582021-11-30 06:19:17575 is_renderer_initiated, blob_url_loader_factory,
576 /* is_initial_entry = */ false);
Camille Lamy5193caa2018-10-12 11:59:42577 entry->SetVirtualURL(virtual_url);
578 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56579 entry->set_update_virtual_url_with_url(reverse_on_redirect);
580 entry->set_extra_headers(extra_headers);
Patrick Monettef507e982019-06-19 20:18:06581 return entry;
[email protected]71fde352011-12-29 03:29:56582}
583
[email protected]cdcb1dee2012-01-04 00:46:20584// static
585void NavigationController::DisablePromptOnRepost() {
586 g_check_for_repost = false;
587}
588
[email protected]c5b88d82012-10-06 17:03:33589base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
590 base::Time t) {
591 // If |t| is between the water marks, we're in a run of duplicates
592 // or just getting out of it, so increase the high-water mark to get
593 // a time that probably hasn't been used before and return it.
594 if (low_water_mark_ <= t && t <= high_water_mark_) {
Peter Kastinge5a38ed2021-10-02 03:06:35595 high_water_mark_ += base::Microseconds(1);
[email protected]c5b88d82012-10-06 17:03:33596 return high_water_mark_;
597 }
598
599 // Otherwise, we're clear of the last duplicate run, so reset the
600 // water marks.
601 low_water_mark_ = high_water_mark_ = t;
602 return t;
603}
604
ckitagawa0faa5e42020-06-17 17:30:54605NavigationControllerImpl::ScopedShowRepostDialogForTesting::
606 ScopedShowRepostDialogForTesting()
607 : was_disallowed_(g_check_for_repost) {
608 g_check_for_repost = true;
609}
610
611NavigationControllerImpl::ScopedShowRepostDialogForTesting::
612 ~ScopedShowRepostDialogForTesting() {
613 g_check_for_repost = was_disallowed_;
614}
615
[email protected]d202a7c2012-01-04 07:53:47616NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00617 BrowserContext* browser_context,
618 FrameTree& frame_tree,
619 NavigationControllerDelegate* delegate)
620 : frame_tree_(frame_tree),
621 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57622 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22623 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47624 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37625 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29626}
627
[email protected]d202a7c2012-01-04 07:53:47628NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00629 // The NavigationControllerImpl might be called inside its delegate
630 // destructor. Calling it is not valid anymore.
631 delegate_ = nullptr;
632 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29633}
634
Matt Falkenhagen548ed1562021-07-06 01:38:26635WebContents* NavigationControllerImpl::DeprecatedGetWebContents() {
636 return delegate_->DeprecatedGetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32637}
638
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57639BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55640 return browser_context_;
641}
642
[email protected]d202a7c2012-01-04 07:53:47643void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30644 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36645 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57646 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57647 // Verify that this controller is unused and that the input is valid.
Rakina Zata Amniafd3c6582021-11-30 06:19:17648 DCHECK_EQ(1, GetEntryCount());
649 DCHECK(GetLastCommittedEntry()->IsInitialEntry());
Lei Zhang96031532019-10-10 19:05:47650 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57651 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14652 selected_navigation < static_cast<int>(entries->size()));
arthursonzogni5c4c202d2017-04-25 23:41:27653 DCHECK_EQ(-1, pending_entry_index_);
[email protected]ce3fa3c2009-04-20 19:55:57654
[email protected]ce3fa3c2009-04-20 19:55:57655 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44656 needs_reload_type_ = NeedsReloadType::kRestoreSession;
Rakina Zata Amniafd3c6582021-11-30 06:19:17657 entries_.clear();
avif16f85a72015-11-13 18:25:03658 entries_.reserve(entries->size());
659 for (auto& entry : *entries)
dcheng36b6aec92015-12-26 06:16:36660 entries_.push_back(
661 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
avif16f85a72015-11-13 18:25:03662
663 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
664 // cleared out safely.
665 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57666
667 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36668 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57669}
670
toyoshim6142d96f2016-12-19 09:07:25671void NavigationControllerImpl::Reload(ReloadType reload_type,
672 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28673 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28674 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32675 int current_index = -1;
676
Carlos IL42b416592019-10-07 23:10:36677 if (entry_replaced_by_post_commit_error_) {
678 // If there is an entry that was replaced by a currently active post-commit
679 // error navigation, this can't be the initial navigation.
680 DCHECK(!IsInitialNavigation());
681 // If the current entry is a post commit error, we reload the entry it
682 // replaced instead. We leave the error entry in place until a commit
683 // replaces it, but the pending entry points to the original entry in the
684 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
685 // case that pending_entry_ != entries_[pending_entry_index_].
686 entry = entry_replaced_by_post_commit_error_.get();
687 current_index = GetCurrentEntryIndex();
688 } else if (IsInitialNavigation() && pending_entry_) {
689 // If we are reloading the initial navigation, just use the current
690 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32691 entry = pending_entry_;
692 // The pending entry might be in entries_ (e.g., after a Clone), so we
693 // should also update the current_index.
694 current_index = pending_entry_index_;
695 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00696 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32697 current_index = GetCurrentEntryIndex();
698 if (current_index != -1) {
creis3da03872015-02-20 21:12:32699 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32700 }
[email protected]979a4bc2013-04-24 01:27:15701 }
[email protected]241db352013-04-22 18:04:05702
[email protected]59167c22013-06-03 18:07:32703 // If we are no where, then we can't reload. TODO(darin): We should add a
704 // CanReload method.
705 if (!entry)
706 return;
707
Takashi Toyoshimac7df3c22019-06-25 14:18:47708 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26709 entry->set_reload_type(reload_type);
710
Aran Gilman37d11632019-10-08 23:07:15711 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30712 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07713 // they really want to do this. If they do, the dialog will call us back
714 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57715 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02716
[email protected]106a0812010-03-18 00:15:12717 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57718 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47719 return;
initial.commit09911bf2008-07-26 23:55:29720 }
Lei Zhang96031532019-10-10 19:05:47721
722 if (!IsInitialNavigation())
723 DiscardNonCommittedEntries();
724
725 pending_entry_ = entry;
726 pending_entry_index_ = current_index;
727 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
728
Nate Chapin45f620582021-09-30 17:45:43729 // location.reload() goes through BeginNavigation, so all reloads triggered
730 // via this codepath are browser initiated.
Lei Zhang96031532019-10-10 19:05:47731 NavigateToExistingPendingEntry(reload_type,
Nate Chapin45f620582021-09-30 17:45:43732 FrameTreeNode::kFrameTreeNodeInvalidId,
733 true /* is_browser_initiated */);
initial.commit09911bf2008-07-26 23:55:29734}
735
[email protected]d202a7c2012-01-04 07:53:47736void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48737 DCHECK(pending_reload_ != ReloadType::NONE);
738 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12739}
740
[email protected]d202a7c2012-01-04 07:53:47741void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48742 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12743 NOTREACHED();
744 } else {
toyoshim6142d96f2016-12-19 09:07:25745 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48746 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12747 }
748}
749
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57750bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09751 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11752}
753
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57754bool NavigationControllerImpl::IsInitialBlankNavigation() {
Rakina Zata Amniafd3c6582021-11-30 06:19:17755 return IsInitialNavigation() && GetEntryCount() == 1 &&
756 GetLastCommittedEntry()->IsInitialEntry() &&
757 GetLastCommittedEntry()->restore_type() == RestoreType::kNotRestored;
creis10a4ab72015-10-13 17:22:40758}
759
Aran Gilman37d11632019-10-08 23:07:15760NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
761 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58762 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03763 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58764}
765
Adithya Srinivasan9b0c99c2021-08-10 15:19:45766NavigationEntryImpl*
767NavigationControllerImpl::GetEntryWithUniqueIDIncludingPending(
768 int nav_entry_id) const {
769 NavigationEntryImpl* entry = GetEntryWithUniqueID(nav_entry_id);
770 if (entry)
771 return entry;
772 return pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id
Keishi Hattori0e45c022021-11-27 09:25:52773 ? pending_entry_.get()
Adithya Srinivasan9b0c99c2021-08-10 15:19:45774 : nullptr;
775}
776
W. James MacLean1c40862c2020-04-27 21:05:57777void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
778 const url::Origin& origin) {
779 for (int i = 0; i < GetEntryCount(); i++) {
780 auto* entry = GetEntryAtIndex(i);
781 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
782 }
783 if (entry_replaced_by_post_commit_error_) {
784 // It's possible we could come back to this entry if the error
785 // page/interstitial goes away.
786 entry_replaced_by_post_commit_error_
787 ->RegisterExistingOriginToPreventOptInIsolation(origin);
788 }
789 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
790 // visiting pending_entry_, which lacks a committed origin. This will be done
791 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
792}
793
avi25764702015-06-23 15:43:37794void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57795 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00796 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37797 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27798 DCHECK_EQ(-1, pending_entry_index_);
[email protected]765b35502008-08-21 00:51:20799}
800
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57801NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47802 if (pending_entry_)
803 return pending_entry_;
804 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20805}
806
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57807NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32808 // The pending entry is safe to return for new (non-history), browser-
809 // initiated navigations. Most renderer-initiated navigations should not
810 // show the pending entry, to prevent URL spoof attacks.
811 //
812 // We make an exception for renderer-initiated navigations in new tabs, as
813 // long as no other page has tried to access the initial empty document in
814 // the new tab. If another page modifies this blank page, a URL spoof is
815 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32816 bool safe_to_show_pending =
817 pending_entry_ &&
818 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09819 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32820 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46821 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32822
823 // Also allow showing the pending entry for history navigations in a new tab,
824 // such as Ctrl+Back. In this case, no existing page is visible and no one
825 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15826 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
827 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32828 safe_to_show_pending = true;
829
830 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19831 return pending_entry_;
832 return GetLastCommittedEntry();
833}
834
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57835int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20836 if (pending_entry_index_ != -1)
837 return pending_entry_index_;
838 return last_committed_entry_index_;
839}
840
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57841NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20842 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28843 return nullptr;
avif16f85a72015-11-13 18:25:03844 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20845}
846
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57847bool NavigationControllerImpl::CanViewSource() {
Jeremy Roman2d8dfe132021-07-06 20:51:26848 const std::string& mime_type =
849 frame_tree_.root()->current_frame_host()->GetPage().contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53850 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
851 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27852 NavigationEntry* visible_entry = GetVisibleEntry();
853 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39854 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16855}
856
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57857int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27858 // The last committed entry index must always be less than the number of
Rakina Zata Amniafd3c6582021-11-30 06:19:17859 // entries.
arthursonzogni5c4c202d2017-04-25 23:41:27860 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
[email protected]a26023822011-12-29 00:23:55861 return last_committed_entry_index_;
862}
863
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57864int NavigationControllerImpl::GetEntryCount() {
Rakina Zata Amniafd3c6582021-11-30 06:19:17865 DCHECK_GE(entries_.size(), 1u);
Carlos IL4dea8902020-05-26 15:14:29866 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55867 return static_cast<int>(entries_.size());
868}
869
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57870NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37871 if (index < 0 || index >= GetEntryCount())
872 return nullptr;
873
avif16f85a72015-11-13 18:25:03874 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25875}
876
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57877NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47878 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20879}
880
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57881int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46882 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03883}
884
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57885bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03886 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
887 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
888 return true;
889 }
890 return false;
[email protected]765b35502008-08-21 00:51:20891}
892
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57893bool NavigationControllerImpl::CanGoForward() {
WangHui74286d52021-03-31 16:17:15894 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
895 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
896 return true;
897 }
898 return false;
[email protected]765b35502008-08-21 00:51:20899}
900
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57901bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03902 int index = GetIndexForOffset(offset);
903 return index >= 0 && index < GetEntryCount();
904}
905
WangHui74286d52021-03-31 16:17:15906#if defined(OS_ANDROID)
907bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
WangHui74286d52021-03-31 16:17:15908 if (offset == 0)
909 return true;
910 int increment = offset > 0 ? 1 : -1;
911 int non_skippable_entries = 0;
912 for (int index = GetIndexForOffset(increment);
913 index >= 0 && index < GetEntryCount(); index += increment) {
914 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
915 non_skippable_entries++;
916
917 if (non_skippable_entries == std::abs(offset))
918 return true;
919 }
920 return false;
921}
922#endif
923
[email protected]d202a7c2012-01-04 07:53:47924void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06925 int target_index = GetIndexForOffset(-1);
926
Elly Fong-Jones40ee8112021-06-23 19:10:52927 // Move the target index past the skippable entries.
Shivani Sharma298d12852019-01-22 20:04:03928 bool all_skippable_entries = true;
Elly Fong-Jones40ee8112021-06-23 19:10:52929 while (target_index >= 0) {
930 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui()) {
Shivani Sharma298d12852019-01-22 20:04:03931 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06932 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16933 }
Elly Fong-Jones40ee8112021-06-23 19:10:52934 target_index--;
shivanisha55201872018-12-13 04:29:06935 }
Miyoung Shin1c565c912021-03-17 12:11:21936
Shivani Sharma298d12852019-01-22 20:04:03937 // Do nothing if all entries are skippable. Normally this path would not
938 // happen as consumers would have already checked it in CanGoBack but a lot of
939 // tests do not do that.
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42940 if (all_skippable_entries)
Shivani Sharma298d12852019-01-22 20:04:03941 return;
shivanisha55201872018-12-13 04:29:06942
shivanisha55201872018-12-13 04:29:06943 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20944}
945
[email protected]d202a7c2012-01-04 07:53:47946void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06947 int target_index = GetIndexForOffset(1);
948
Shivani Sharma2d5b4b6b2019-01-08 16:07:16949 // Note that at least one entry (the last one) will be non-skippable since
950 // entries are marked skippable only when they add another entry because of
951 // redirect or pushState.
Elly Fong-Jones40ee8112021-06-23 19:10:52952 while (target_index < static_cast<int>(entries_.size())) {
953 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui())
shivanisha55201872018-12-13 04:29:06954 break;
Elly Fong-Jones40ee8112021-06-23 19:10:52955 target_index++;
shivanisha55201872018-12-13 04:29:06956 }
shivanisha55201872018-12-13 04:29:06957 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20958}
959
[email protected]d202a7c2012-01-04 07:53:47960void NavigationControllerImpl::GoToIndex(int index) {
Nate Chapin45f620582021-09-30 17:45:43961 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId,
962 true /* is_browser_initiated */);
Dave Tapuska8bfd84c2019-03-26 20:47:16963}
964
965void NavigationControllerImpl::GoToIndex(int index,
Nate Chapin45f620582021-09-30 17:45:43966 int sandbox_frame_tree_node_id,
967 bool is_browser_initiated) {
968 DCHECK(sandbox_frame_tree_node_id == FrameTreeNode::kFrameTreeNodeInvalidId ||
969 !is_browser_initiated);
sunjian30574a62017-03-21 21:39:44970 TRACE_EVENT0("browser,navigation,benchmark",
971 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:20972 if (index < 0 || index >= static_cast<int>(entries_.size())) {
973 NOTREACHED();
974 return;
975 }
976
[email protected]cbab76d2008-10-13 22:42:47977 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20978
arthursonzogni5c4c202d2017-04-25 23:41:27979 DCHECK_EQ(nullptr, pending_entry_);
980 DCHECK_EQ(-1, pending_entry_index_);
981 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:20982 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:27983 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
984 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Nate Chapin45f620582021-09-30 17:45:43985 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id,
986 is_browser_initiated);
[email protected]765b35502008-08-21 00:51:20987}
988
[email protected]d202a7c2012-01-04 07:53:47989void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:12990 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:03991 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:20992 return;
993
[email protected]9ba14052012-06-22 23:50:03994 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20995}
996
Nate Chapin45f620582021-09-30 17:45:43997void NavigationControllerImpl::GoToOffsetFromRenderer(int offset) {
998 // Note: This is actually reached in unit tests.
999 if (!CanGoToOffset(offset))
1000 return;
1001
1002 GoToIndex(GetIndexForOffset(offset), FrameTreeNode::kFrameTreeNodeInvalidId,
1003 false /* is_browser_initiated */);
1004}
1005
WangHui74286d52021-03-31 16:17:151006#if defined(OS_ANDROID)
1007void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
1008 // Note: This is actually reached in unit tests.
1009 if (!CanGoToOffsetWithSkipping(offset))
1010 return;
1011
Elly Fong-Jonesccc6d1f2021-06-14 18:32:421012 if (offset == 0) {
WangHui74286d52021-03-31 16:17:151013 GoToIndex(GetIndexForOffset(offset));
1014 return;
1015 }
1016 int increment = offset > 0 ? 1 : -1;
1017 // Find the offset without counting skippable entries.
1018 int target_index = GetIndexForOffset(increment);
1019 int non_skippable_entries = 0;
1020 for (int index = target_index; index >= 0 && index < GetEntryCount();
1021 index += increment) {
1022 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1023 non_skippable_entries++;
1024
1025 if (non_skippable_entries == std::abs(offset)) {
1026 target_index = index;
1027 break;
1028 }
1029 }
1030
1031 GoToIndex(target_index);
1032}
1033#endif
1034
[email protected]41374f12013-07-24 02:49:281035bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151036 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281037 return false;
[email protected]6a13a6c2011-12-20 21:47:121038
[email protected]43032342011-03-21 14:10:311039 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281040 return true;
[email protected]cbab76d2008-10-13 22:42:471041}
1042
Michael Thiessen9b14d512019-09-23 21:19:471043void NavigationControllerImpl::PruneForwardEntries() {
1044 DiscardNonCommittedEntries();
1045 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471046 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471047 if (num_removed <= 0)
1048 return;
1049 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1050 NotifyPrunedEntries(this, remove_start_index /* start index */,
1051 num_removed /* count */);
1052}
1053
Aran Gilman37d11632019-10-08 23:07:151054void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1055 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321056 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311057 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511058 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1059 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321060 }
1061}
1062
Harkiran Bolariaba823e42021-05-21 18:30:361063base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL(
1064 const GURL& url,
1065 const Referrer& referrer,
1066 ui::PageTransition transition,
1067 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471068 LoadURLParams params(url);
1069 params.referrer = referrer;
1070 params.transition_type = transition;
1071 params.extra_headers = extra_headers;
Harkiran Bolariaba823e42021-05-21 18:30:361072 return LoadURLWithParams(params);
[email protected]cf002332012-08-14 19:17:471073}
1074
Harkiran Bolariaba823e42021-05-21 18:30:361075base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams(
1076 const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061077 if (params.is_renderer_initiated)
1078 DCHECK(params.initiator_origin.has_value());
1079
naskob8744d22014-08-28 17:07:431080 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151081 "NavigationControllerImpl::LoadURLWithParams", "url",
1082 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291083 bool is_explicit_navigation =
1084 GetContentClient()->browser()->IsExplicitNavigation(
1085 params.transition_type);
1086 if (HandleDebugURL(params.url, params.transition_type,
1087 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431088 // If Telemetry is running, allow the URL load to proceed as if it's
1089 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491090 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431091 cc::switches::kEnableGpuBenchmarking))
Harkiran Bolariaba823e42021-05-21 18:30:361092 return nullptr;
[email protected]47752982014-07-29 08:01:431093 }
[email protected]8bf1048012012-02-08 01:22:181094
[email protected]cf002332012-08-14 19:17:471095 // Checks based on params.load_type.
1096 switch (params.load_type) {
1097 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431098 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471099 break;
1100 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261101 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471102 NOTREACHED() << "Data load must use data scheme.";
Harkiran Bolariaba823e42021-05-21 18:30:361103 return nullptr;
[email protected]cf002332012-08-14 19:17:471104 }
1105 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461106 }
[email protected]e47ae9472011-10-13 19:48:341107
[email protected]e47ae9472011-10-13 19:48:341108 // The user initiated a load, we don't need to reload anymore.
1109 needs_reload_ = false;
1110
Harkiran Bolariaba823e42021-05-21 18:30:361111 return NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441112}
1113
Mohamed Abdelhalim833de902019-09-16 17:41:451114bool NavigationControllerImpl::PendingEntryMatchesRequest(
1115 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191116 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451117 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191118}
1119
[email protected]d202a7c2012-01-04 07:53:471120bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321121 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071122 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331123 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441124 bool is_same_document_navigation,
Nate Chapinc7019dd7d2021-06-25 18:29:251125 bool was_on_initial_empty_document,
Shivani Sharmaffb32b82019-04-09 16:58:471126 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131127 NavigationRequest* navigation_request) {
1128 DCHECK(navigation_request);
shivanigithubf405bf0d2021-11-05 17:58:331129 if (ShouldMaintainTrivialSessionHistory(rfh->frame_tree_node()) &&
1130 GetLastCommittedEntry()) {
Takashi Toyoshimaa9f66802021-08-18 02:40:171131 // Ensure that this navigation does not add a navigation entry, since
1132 // ShouldMaintainTrivialSessionHistory() means we should not add an entry
1133 // beyond the last committed one. Therefore, `should_replace_current_entry`
1134 // should be set, which replaces the current entry, or this should be a
1135 // reload, which does not create a new entry.
1136 DCHECK(params.should_replace_current_entry ||
1137 navigation_request->GetReloadType() != ReloadType::NONE);
1138 }
[email protected]cd2e15742013-03-08 04:08:311139 is_initial_navigation_ = false;
1140
[email protected]0e8db942008-09-24 21:21:481141 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431142 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481143 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361144 if (entry_replaced_by_post_commit_error_) {
1145 if (is_same_document_navigation) {
1146 // Same document navigations should not be possible on error pages and
1147 // would leave the controller in a weird state. Kill the renderer if
1148 // that happens.
1149 bad_message::ReceivedBadMessage(
1150 rfh->GetProcess(), bad_message::NC_SAME_DOCUMENT_POST_COMMIT_ERROR);
1151 }
1152 // Any commit while a post-commit error page is showing should put the
1153 // original entry back, replacing the error page's entry. This includes
1154 // reloads, where the original entry was used as the pending entry and
1155 // should now be at the correct index at commit time.
1156 entries_[last_committed_entry_index_] =
1157 std::move(entry_replaced_by_post_commit_error_);
1158 }
Fergal Daly8e33cf62020-12-12 01:06:071159 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551160 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431161 if (pending_entry_ &&
1162 pending_entry_->GetIsOverridingUserAgent() !=
1163 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1164 overriding_user_agent_changed = true;
Shu Yang5756fa422021-11-05 23:02:541165#if defined(OS_ANDROID)
1166 // TODO(crbug.com/1266277): Clean up the logic of setting
1167 // |overriding_user_agent_changed| post-launch.
Shu Yang9eabbb902021-11-17 00:45:031168 if (base::FeatureList::IsEnabled(features::kRequestDesktopSiteExceptions)) {
Shu Yang5756fa422021-11-05 23:02:541169 // Must honor user agent overrides in the |navigation_request|, such as
1170 // from things like RequestDesktopSiteWebContentsObserverAndroid. As a
1171 // result, besides comparing |pending_entry_|'s user agent against
1172 // LastCommittedEntry's, also need to compare |navigation_request|'s user
1173 // agent against LastCommittedEntry's.
1174 if (navigation_request->is_overriding_user_agent() !=
1175 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
1176 overriding_user_agent_changed = true;
1177 }
1178 }
1179#endif // defined(OS_ANDROID)
[email protected]0e8db942008-09-24 21:21:481180 } else {
Gang Wu325f03f42021-02-25 20:00:461181 // GetLastCommittedEntry() is null, so this is the first entry.
Fergal Daly8e33cf62020-12-12 01:06:071182 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481183 details->previous_entry_index = -1;
Gang Wu325f03f42021-02-25 20:00:461184 if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) {
1185 // Default setting is NOT override the user agent, so overriding the user
1186 // agent in first entry should be considered as user agent changed as
1187 // well.
1188 overriding_user_agent_changed = true;
1189 }
Shu Yang5756fa422021-11-05 23:02:541190#if defined(OS_ANDROID)
1191 // TODO(crbug.com/1266277): Clean up the logic of setting
1192 // |overriding_user_agent_changed| post-launch.
Shu Yang9eabbb902021-11-17 00:45:031193 if (base::FeatureList::IsEnabled(features::kRequestDesktopSiteExceptions)) {
Shu Yang5756fa422021-11-05 23:02:541194 // Must honor user agent overrides in the |navigation_request|, such as
1195 // from things like RequestDesktopSiteWebContentsObserverAndroid. As a
1196 // result, besides checking |pending_entry_|'s user agent, also need to
1197 // check |navigation_request|'s.
1198 if (navigation_request->is_overriding_user_agent()) {
1199 overriding_user_agent_changed = true;
1200 }
1201 }
1202#endif // defined(OS_ANDROID)
[email protected]0e8db942008-09-24 21:21:481203 }
[email protected]ecd9d8702008-08-28 22:10:171204
Dave Tapuskaa2ab4f252021-07-08 21:31:281205 bool is_main_frame_navigation = !rfh->GetParent();
1206
Alexander Timind2f2e4f22019-04-02 20:04:531207 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1208 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281209 // For primary frame tree navigations, choose an appropriate
1210 // BackForwardCacheMetrics to be associated with the NavigationEntry, by
1211 // either creating a new object or reusing the previous one depending on
1212 // whether it's a main frame navigation or not.
1213 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics;
1214 if (navigation_request->frame_tree_node()->frame_tree()->type() ==
1215 FrameTree::Type::kPrimary) {
1216 back_forward_cache_metrics =
1217 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1218 GetLastCommittedEntry(), is_main_frame_navigation,
1219 params.document_sequence_number);
1220 }
Alexander Timind2f2e4f22019-04-02 20:04:531221 // Notify the last active entry that we have navigated away.
Dave Tapuskaa2ab4f252021-07-08 21:31:281222 if (is_main_frame_navigation && !is_same_document_navigation) {
Alexander Timind2f2e4f22019-04-02 20:04:531223 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1224 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Hajime Hoshic7606502021-04-14 02:42:161225 metrics->MainFrameDidNavigateAwayFromDocument(rfh, navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531226 }
1227 }
1228 }
1229
fdegans9caf66a2015-07-30 21:10:421230 // If there is a pending entry at this point, it should have a SiteInstance,
1231 // except for restored entries.
jam48cea9082017-02-15 06:13:291232 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481233 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081234 pending_entry_->IsRestored());
1235 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151236 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291237 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481238 }
[email protected]e9ba4472008-09-14 15:42:431239
Nasko Oskovaee2f862018-06-15 00:05:521240 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1241 // has changed, this must be treated as a new navigation with replacement.
1242 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491243 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521244 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101245 pending_entry_->GetUniqueID() ==
1246 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521247 pending_entry_->site_instance() &&
1248 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1249 DCHECK_NE(-1, pending_entry_index_);
1250 // TODO(nasko,creis): Instead of setting this value here, set
1251 // should_replace_current_entry on the parameters we send to the
1252 // renderer process as part of CommitNavigation. The renderer should
1253 // in turn send it back here as part of |params| and it can be just
1254 // enforced and renderer process terminated on mismatch.
1255 details->did_replace_entry = true;
Rakina Zata Amniafd3c6582021-11-30 06:19:171256 } else if (!params.history_list_was_cleared && !rfh->GetParent() &&
1257 GetLastCommittedEntry()->IsInitialEntry()) {
1258 // Make sure we always replace the initial NavigationEntry.
1259 details->did_replace_entry = true;
Nasko Oskovaee2f862018-06-15 00:05:521260 } else {
1261 // The renderer tells us whether the navigation replaces the current entry.
1262 details->did_replace_entry = params.should_replace_current_entry;
1263 }
[email protected]bcd904482012-02-01 01:54:221264
[email protected]e9ba4472008-09-14 15:42:431265 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amnia4e27222021-12-22 01:05:001266 details->type = ClassifyNavigation(rfh, params, navigation_request, details);
[email protected]4bf3522c2010-08-19 21:00:201267
eugenebutee08663a2017-04-27 17:43:121268 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441269 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121270
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071271 details->is_prerender_activation =
1272 navigation_request->IsPrerenderedPageActivation();
1273
Peter Boströmd7592132019-01-30 04:50:311274 // Make sure we do not discard the pending entry for a different ongoing
1275 // navigation when a same document commit comes in unexpectedly from the
1276 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1277 // other navigation types. See https://crbug.com/900036.
1278 // TODO(crbug.com/926009): Handle history.pushState() as well.
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061279 bool keep_pending_entry =
1280 is_same_document_navigation &&
1281 details->type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY &&
1282 pending_entry_ && !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311283
[email protected]0e8db942008-09-24 21:21:481284 switch (details->type) {
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061285 case NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY:
Charlie Reisc0f17d2d2021-01-12 18:52:491286 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051287 rfh, params, details->is_same_document, details->did_replace_entry,
Rakina Zata Amnia4e27222021-12-22 01:05:001288 previous_document_was_activated, navigation_request, details);
[email protected]e9ba4472008-09-14 15:42:431289 break;
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061290 case NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY:
Charlie Reisc0f17d2d2021-01-12 18:52:491291 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1292 was_restored, navigation_request,
Rakina Zata Amnia4e27222021-12-22 01:05:001293 keep_pending_entry, details);
[email protected]e9ba4472008-09-14 15:42:431294 break;
[email protected]8ff00d72012-10-23 19:12:211295 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471296 RendererDidNavigateNewSubframe(
1297 rfh, params, details->is_same_document, details->did_replace_entry,
Rakina Zata Amnia4e27222021-12-22 01:05:001298 previous_document_was_activated, navigation_request, details);
[email protected]e9ba4472008-09-14 15:42:431299 break;
[email protected]8ff00d72012-10-23 19:12:211300 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391301 if (!RendererDidNavigateAutoSubframe(
Nate Chapinc7019dd7d2021-06-25 18:29:251302 rfh, params, details->is_same_document,
Rakina Zata Amnia4e27222021-12-22 01:05:001303 was_on_initial_empty_document, navigation_request, details)) {
creisce0ef3572017-01-26 17:53:081304 // We don't send a notification about auto-subframe PageState during
1305 // UpdateStateForFrame, since it looks like nothing has changed. Send
1306 // it here at commit time instead.
1307 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431308 return false;
creis59d5a47cb2016-08-24 23:57:191309 }
[email protected]e9ba4472008-09-14 15:42:431310 break;
Aran Gilman37d11632019-10-08 23:07:151311 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431312 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151313 break;
[email protected]765b35502008-08-21 00:51:201314 }
1315
[email protected]688aa65c62012-09-28 04:32:221316 // At this point, we know that the navigation has just completed, so
1317 // record the time.
1318 //
1319 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261320 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331321 base::Time timestamp =
1322 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1323 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131324 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221325
Peter Boströmd7592132019-01-30 04:50:311326 // If we aren't keeping the pending entry, there shouldn't be one at this
1327 // point. Clear it again in case any error cases above forgot to do so.
1328 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1329 // been cleared instead of protecting against it.
1330 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001331 DiscardNonCommittedEntries();
[email protected]f233e4232013-02-23 00:55:141332
[email protected]e9ba4472008-09-14 15:42:431333 // All committed entries should have nonempty content state so WebKit doesn't
1334 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471335 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321336 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221337 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441338 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531339 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1340 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281341 if (back_forward_cache_metrics &&
1342 !active_entry->back_forward_cache_metrics()) {
Alexander Timind2f2e4f22019-04-02 20:04:531343 active_entry->set_back_forward_cache_metrics(
1344 std::move(back_forward_cache_metrics));
1345 }
Dave Tapuskaa2ab4f252021-07-08 21:31:281346
1347 // `back_forward_cache_metrics()` may return null as we do not record
1348 // back-forward cache metrics for navigations in non-primary frame trees.
1349 if (active_entry->back_forward_cache_metrics()) {
1350 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
1351 navigation_request,
1352 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
1353 }
naskoc7533512016-05-06 17:01:121354
Charles Reisc0507202017-09-21 00:40:021355 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1356 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1357 // A mismatch can occur if the renderer lies or due to a unique name collision
1358 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121359 FrameNavigationEntry* frame_entry =
1360 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021361 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1362 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031363 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081364 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1365 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031366 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201367
1368 // Remember the bindings the renderer process has at this point, so that
1369 // we do not grant this entry additional bindings if we come back to it.
1370 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301371 }
[email protected]132e281a2012-07-31 18:32:441372
[email protected]97d8f0d2013-10-29 16:49:211373 // Once it is committed, we no longer need to track several pieces of state on
1374 // the entry.
naskoc7533512016-05-06 17:01:121375 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131376
[email protected]49bd30e62011-03-22 20:12:591377 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221378 // TODO(creis): This check won't pass for subframes until we create entries
1379 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001380 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421381 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591382
[email protected]e9ba4472008-09-14 15:42:431383 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001384 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001385 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311386 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531387
arthursonzogni7ddc6542021-04-09 09:16:501388 active_entry->SetIsOverridingUserAgent(
1389 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031390
[email protected]93f230e02011-06-01 14:40:001391 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291392
aelias100c9192017-01-13 00:01:431393 if (overriding_user_agent_changed)
1394 delegate_->UpdateOverridingUserAgent();
1395
creis03b48002015-11-04 00:54:561396 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1397 // one knows the latest NavigationEntry it is showing (whether it has
1398 // committed anything in this navigation or not). This allows things like
1399 // state and title updates from RenderFrames to apply to the latest relevant
1400 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381401 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001402 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381403 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
Hayato Ito2c8c08d02021-06-23 03:38:431404
1405 if (navigation_request->IsPrerenderedPageActivation()) {
1406 BroadcastHistoryOffsetAndLength();
1407 // TODO(crbug.com/1222893): Broadcasting happens after the prerendered page
1408 // is activated. As a result, a "prerenderingchange" event listener sees the
1409 // history.length which is not updated yet. We should guarantee that
1410 // history's length and offset should be updated before a
1411 // "prerenderingchange" event listener runs. One possible approach is to use
1412 // the same IPC which "prerenderingchange" uses, and propagate history's
1413 // length and offset together with that.
1414 }
1415
[email protected]e9ba4472008-09-14 15:42:431416 return true;
initial.commit09911bf2008-07-26 23:55:291417}
1418
[email protected]8ff00d72012-10-23 19:12:211419NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321420 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101421 const mojom::DidCommitProvisionalLoadParams& params,
Rakina Zata Amnia4e27222021-12-22 01:05:001422 NavigationRequest* navigation_request,
1423 LoadCommittedDetails* details) {
1424 if (GetLastCommittedEntry()->IsInitialEntry() &&
1425 navigation_request->is_synchronous_renderer_commit() &&
1426 !navigation_request->IsSameDocument() && !rfh->GetParent() &&
1427 params.should_replace_current_entry) {
1428 // This is the synchronous about:blank navigation that happens when calling
1429 // window.open() with no URL, which used to not create a NavigationEntry
1430 // when we have no NavigationEntry on FrameTree creation. We now have the
1431 // initial NavigationEntry, but Android WebView still needs this info to
1432 // ignore the NavigationStateChanged() call in some cases to avoid firing
1433 // onPageFinished etc in more cases than it previously did.
1434 // See also https://crbug.com/1277414.
1435 details->used_to_be_ignored_due_to_null_navigation_entry = true;
1436 }
1437
Piotr Tworekbad51282020-09-30 19:17:591438 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511439 "ClassifyNavigation");
Rakina Zata Amniafd3c6582021-11-30 06:19:171440 DCHECK(GetLastCommittedEntry());
Rakina Zata Amnia4e27222021-12-22 01:05:001441 if (rfh->GetParent() &&
1442 GetLastCommittedEntry()
1443 ->did_not_exist_without_initial_navigation_entry()) {
1444 // This is a subframe navigation on the initial empty document, which used
1445 // to not have a NavigationEntry to attach to, or other similarly previously
1446 // non-existent NavigationEntry. Now it can attach to the initial
1447 // NavigationEntry (or other previously non-existent NavigationEntry), but
1448 // Android WebView still needs this info to ignore the
1449 // NavigationStateChanged() call in some cases to avoid firing
1450 // onPageFinished etc in more cases than it previously did.
1451 // See also https://crbug.com/1277414.
1452 details->used_to_be_ignored_due_to_null_navigation_entry = true;
1453 }
Nasko Oskovae49e292020-08-13 02:08:511454
avi7c6f35e2015-05-08 17:52:381455 if (params.did_create_new_entry) {
Rakina Zata Amniafd3c6582021-11-30 06:19:171456 // A new entry for either the main frame or a subframe.
avi39c1edd32015-06-04 20:06:001457 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491458 trace_return.set_return_reason("new entry, no parent, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061459 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381460 }
1461
Rakina Zata Amniafd3c6582021-11-30 06:19:171462 // Subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511463 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381464 return NAVIGATION_TYPE_NEW_SUBFRAME;
1465 }
1466
Charlie Reisc0f17d2d2021-01-12 18:52:491467 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381468 DCHECK(!params.history_list_was_cleared);
1469
avi39c1edd32015-06-04 20:06:001470 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381471 // All manual subframes would be did_create_new_entry and handled above, so
1472 // we know this is auto.
Rakina Zata Amniafd3c6582021-11-30 06:19:171473 trace_return.set_return_reason("subframe, last commmited, auto subframe");
1474 return NAVIGATION_TYPE_AUTO_SUBFRAME;
avi7c6f35e2015-05-08 17:52:381475 }
1476
Rakina Zata Amnif6950d552020-11-24 03:26:101477 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1478 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381479 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1480 // create a new page.
1481
Hayato Ito2ae49442021-07-02 02:59:251482 // This main frame navigation is not a history navigation (since
1483 // nav_entry_id is 0), but didn't create a new entry. So this must be a
1484 // reload or a replacement navigation, which will modify the existing entry.
1485 //
Nasko Oskov332593c2018-08-16 17:21:341486 // TODO(nasko): With error page isolation, reloading an existing session
1487 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491488 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341489 // as new with replacement.
Rakina Zata Amnia4e27222021-12-22 01:05:001490
1491 if (GetLastCommittedEntry()
1492 ->did_not_exist_without_initial_navigation_entry()) {
1493 // This is a navigation that used to try to modify a non-existent
1494 // NavigationEntry. Now a NavigationEntry should always exist for this
1495 // navigation to modify, but Android WebView still needs this info to
1496 // ignore the NavigationStateChanged() call in some cases to avoid firing
1497 // onPageFinished etc in more cases than it previously did.
1498 // See also https://crbug.com/1277414.
1499 details->used_to_be_ignored_due_to_null_navigation_entry = true;
1500 }
Nasko Oskovae49e292020-08-13 02:08:511501 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491502 "nav entry 0, last committed, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061503 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381504 }
1505
Rakina Zata Amnif6950d552020-11-24 03:26:101506 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521507 // If the SiteInstance of the |pending_entry_| does not match the
1508 // SiteInstance that got committed, treat this as a new navigation with
1509 // replacement. This can happen if back/forward/reload encounters a server
1510 // redirect to a different site or an isolated error page gets successfully
1511 // reloaded into a different SiteInstance.
1512 if (pending_entry_->site_instance() &&
1513 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491514 trace_return.set_return_reason("pending matching nav entry, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061515 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521516 }
creis77c9aa32015-09-25 19:59:421517
Nasko Oskovaee2f862018-06-15 00:05:521518 if (pending_entry_index_ == -1) {
1519 // In this case, we have a pending entry for a load of a new URL but Blink
1520 // didn't do a new navigation (params.did_create_new_entry). First check
1521 // to make sure Blink didn't treat a new cross-process navigation as
1522 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161523 // we must treat it as NEW rather than the converted reload case below,
1524 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521525 if (!GetLastCommittedEntry() ||
1526 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161527 trace_return.set_return_reason("new pending, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061528 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521529 }
1530
1531 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161532 // We will create a pending entry, but NavigateWithoutEntry will convert
1533 // it to a reload since it's the same page and not create a new entry for
1534 // it. (The user doesn't want to have a new back/forward entry when they
1535 // do this.) Therefore we want to just ignore the pending entry and go
1536 // back to where we were (the "existing entry").
1537 trace_return.set_return_reason("new pending, existing (same) entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061538 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521539 }
avi7c6f35e2015-05-08 17:52:381540 }
1541
creis26d22632017-04-21 20:23:561542 // Everything below here is assumed to be an existing entry, but if there is
1543 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511544 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491545 trace_return.set_return_reason("no last committed, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061546 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511547 }
creis26d22632017-04-21 20:23:561548
Rakina Zata Amni153d5702021-09-13 22:48:001549 if (navigation_request->commit_params().intended_as_new_entry) {
avi7c6f35e2015-05-08 17:52:381550 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491551 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1552 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161553 trace_return.set_return_reason("intended as new entry, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061554 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381555 }
1556
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121557 if (navigation_request->DidEncounterError() &&
1558 failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101559 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381560 // If the renderer was going to a new pending entry that got cleared because
1561 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491562 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381563 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511564 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491565 "unreachable, matching pending, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061566 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381567 }
1568
Charlie Reisc0f17d2d2021-01-12 18:52:491569 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101570 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511571 trace_return.traced_value()->SetInteger("existing_entry_index",
1572 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381573 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441574 // The renderer has committed a navigation to an entry that no longer
1575 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491576 trace_return.set_return_reason("existing entry -1, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061577 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381578 }
1579
avi7c6f35e2015-05-08 17:52:381580 // Since we weeded out "new" navigations above, we know this is an existing
1581 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491582 trace_return.set_return_reason("default return, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061583 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381584}
1585
Rakina Zata Amni3460d382021-10-29 00:43:371586void NavigationControllerImpl::UpdateNavigationEntryDetails(
1587 NavigationEntryImpl* entry,
1588 RenderFrameHostImpl* rfh,
1589 const mojom::DidCommitProvisionalLoadParams& params,
1590 NavigationRequest* request,
1591 NavigationEntryImpl::UpdatePolicy update_policy,
Rakina Zata Amnia4e27222021-12-22 01:05:001592 bool is_new_entry,
1593 LoadCommittedDetails* commit_details) {
Rakina Zata Amni3460d382021-10-29 00:43:371594 // Update the FrameNavigationEntry.
Rakina Zata Amniafd3c6582021-11-30 06:19:171595
1596 std::vector<GURL> redirects;
Rakina Zata Amni3460d382021-10-29 00:43:371597 entry->AddOrUpdateFrameEntry(
1598 rfh->frame_tree_node(), update_policy, params.item_sequence_number,
1599 params.document_sequence_number, params.app_history_key,
1600 rfh->GetSiteInstance(), nullptr, params.url,
1601 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amniafd3c6582021-11-30 06:19:171602 Referrer(*params.referrer),
1603 request ? request->common_params().initiator_origin : params.origin,
1604 request ? request->GetRedirectChain() : redirects, params.page_state,
1605 params.method, params.post_id, nullptr /* blob_url_loader_factory */,
1606 (request && request->web_bundle_navigation_info())
Rakina Zata Amni3460d382021-10-29 00:43:371607 ? request->web_bundle_navigation_info()->Clone()
1608 : nullptr,
Rakina Zata Amniafd3c6582021-11-30 06:19:171609 (request ? request->GetSubresourceWebBundleNavigationInfo() : nullptr),
Rakina Zata Amni3460d382021-10-29 00:43:371610 ComputePolicyContainerPoliciesForFrameEntry(
Rakina Zata Amniafd3c6582021-11-30 06:19:171611 rfh, request && request->IsSameDocument(),
1612 request ? request->common_params().url : params.url));
Rakina Zata Amni3460d382021-10-29 00:43:371613
1614 if (rfh->GetParent()) {
1615 // Only modify the NavigationEntry for main frame navigations.
1616 return;
1617 }
1618 if (entry->update_virtual_url_with_url())
1619 UpdateVirtualURLToURL(entry, params.url);
1620 // Don't use the page type from the pending entry. Some interstitial page
1621 // may have set the type to interstitial. Once we commit, however, the page
1622 // type must always be normal or error.
Rakina Zata Amniafd3c6582021-11-30 06:19:171623 entry->set_page_type((request && request->DidEncounterError())
1624 ? PAGE_TYPE_ERROR
1625 : PAGE_TYPE_NORMAL);
Rakina Zata Amnia4e27222021-12-22 01:05:001626 if (commit_details) {
1627 // If this navigation used to get ignored because there's no NavigationEntry
1628 // for the FrameNavigationEntry to attach to, note the fact that the new
1629 // NavigationEntry used to not exist.
1630 entry->set_did_not_exist_without_initial_navigation_entry(
1631 commit_details->used_to_be_ignored_due_to_null_navigation_entry);
1632 }
Rakina Zata Amni3460d382021-10-29 00:43:371633 if (is_new_entry) {
1634 // Some properties of the NavigationEntry are only set when the entry is
1635 // new (we aren't reusing it).
1636 entry->SetTransitionType(params.transition);
Rakina Zata Amniafd3c6582021-11-30 06:19:171637 entry->SetOriginalRequestURL(request ? request->GetOriginalRequestURL()
1638 : GURL::EmptyGURL());
Rakina Zata Amni3460d382021-10-29 00:43:371639 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
1640 entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
1641 } else {
1642 // We are reusing the NavigationEntry. The site instance will normally be
1643 // the same except for a few cases:
1644 // 1) session restore, when no site instance will be assigned or
1645 // 2) redirect, when the site instance is reset.
1646 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
1647 entry->site_instance() == rfh->GetSiteInstance());
1648 }
1649}
1650
Rakina Zata Amniafd3c6582021-11-30 06:19:171651void NavigationControllerImpl::CreateInitialEntry() {
1652 DCHECK_EQ(entries_.size(), 0u);
1653 RenderFrameHostImpl* rfh = frame_tree_.root()->current_frame_host();
1654 auto params = mojom::DidCommitProvisionalLoadParams::New();
1655 // The initial NavigationEntry's URL is the empty URL. This preserves the old
1656 // behavior of WebContent's GetLastCommittedURL() and GetVisibleURL() from
1657 // before we have initial NavigationEntries.
1658 params->url = GURL::EmptyGURL();
1659 params->http_status_code = 0;
1660 params->url_is_unreachable = false;
1661 params->method = "GET";
1662 params->should_replace_current_entry = false;
1663 params->post_id = -1;
1664 params->embedding_token = base::UnguessableToken::Create();
1665 params->navigation_token = base::UnguessableToken::Create();
1666 params->did_create_new_entry = true;
1667 params->origin = rfh->GetLastCommittedOrigin();
1668 params->should_update_history = true;
1669 params->item_sequence_number = 0;
1670 params->document_sequence_number = 0;
1671 bool is_in_fenced_frame_tree = rfh->frame_tree_node()->IsInFencedFrameTree();
1672 params->transition = is_in_fenced_frame_tree
1673 ? ui::PAGE_TRANSITION_AUTO_SUBFRAME
1674 : ui::PAGE_TRANSITION_LINK;
1675 params->referrer = blink::mojom::Referrer::New();
1676
1677 // Create and insert the initial NavigationEntry.
1678 auto new_entry = std::make_unique<NavigationEntryImpl>(
1679 rfh->GetSiteInstance(), params->url, Referrer(*params->referrer),
1680 rfh->GetLastCommittedOrigin(), std::u16string() /* title */,
1681 ui::PAGE_TRANSITION_TYPED, false /* renderer_initiated */,
1682 nullptr /* blob_url_loader_factory */, true /* is_initial_entry */);
1683 UpdateNavigationEntryDetails(
1684 new_entry.get(), rfh, *params, nullptr /* request */,
Rakina Zata Amnia4e27222021-12-22 01:05:001685 NavigationEntryImpl::UpdatePolicy::kUpdate, true /* is_new_entry */,
1686 nullptr /* commit_details */);
1687 new_entry->set_did_not_exist_without_initial_navigation_entry(true);
Rakina Zata Amniafd3c6582021-11-30 06:19:171688
1689 InsertOrReplaceEntry(std::move(new_entry), false /* replace_entry */,
1690 false /* was_post_commit_error */,
Rakina Zata Amnia4e27222021-12-22 01:05:001691 is_in_fenced_frame_tree, nullptr /* commit_details */);
Rakina Zata Amniafd3c6582021-11-30 06:19:171692}
1693
Charlie Reisc0f17d2d2021-01-12 18:52:491694void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321695 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071696 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361697 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441698 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471699 bool previous_document_was_activated,
Rakina Zata Amnia4e27222021-12-22 01:05:001700 NavigationRequest* request,
1701 LoadCommittedDetails* commit_details) {
dcheng9bfa5162016-04-09 01:00:571702 std::unique_ptr<NavigationEntryImpl> new_entry;
Anton Bikineevf62d1bf2021-05-15 17:56:071703 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451704 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061705
creisf49dfc92016-07-26 17:05:181706 // First check if this is an in-page navigation. If so, clone the current
1707 // entry instead of looking at the pending entry, because the pending entry
1708 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361709 if (is_same_document && GetLastCommittedEntry()) {
Patrick Monette50e8bd82019-06-13 22:40:451710 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481711 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081712 params.document_sequence_number, params.app_history_key,
1713 rfh->GetSiteInstance(), nullptr, params.url,
1714 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091715 Referrer(*params.referrer), initiator_origin,
1716 request->GetRedirectChain(), params.page_state, params.method,
1717 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391718 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481719 request->GetSubresourceWebBundleNavigationInfo(),
Antonio Sartori78a749f2020-11-30 12:03:391720 // We will set the document policies later in this function.
Titouan Rigoudy6ec70402021-02-02 15:42:191721 nullptr /* policy_container_policies */);
Charles Reisf44482022017-10-13 21:15:031722
creisf49dfc92016-07-26 17:05:181723 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Harkiran Bolaria081c5c22021-09-13 08:44:531724 frame_entry, true, request->frame_tree_node(), frame_tree_.root());
Mike West800532c2021-10-14 09:26:521725 if (new_entry->GetURL().DeprecatedGetOriginAsURL() !=
1726 params.url.DeprecatedGetOriginAsURL()) {
jama78746e2017-02-22 17:21:571727 // TODO(jam): we had one report of this with a URL that was redirecting to
1728 // only tildes. Until we understand that better, don't copy the cert in
1729 // this case.
1730 new_entry->GetSSL() = SSLStatus();
jama78746e2017-02-22 17:21:571731 }
creisf49dfc92016-07-26 17:05:181732
Patrick Monette50e8bd82019-06-13 22:40:451733 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1734 // that |frame_entry| should now have a reference count of exactly 2: one
1735 // due to the local variable |frame_entry|, and another due to |new_entry|
1736 // also retaining one. This should never fail, because it's the main frame.
1737 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181738 }
1739
Harkiran Bolaria59290d62021-03-17 01:53:011740 // If this is an activation navigation from a prerendered page, transfer the
1741 // new entry from an entry already created and stored in the
1742 // NavigationRequest. |new_entry| will not have been set prior to this as
1743 // |is_same_document| is mutually exclusive with
1744 // |IsPrerenderedPageActivation|.
1745 if (request->IsPrerenderedPageActivation()) {
1746 DCHECK(!is_same_document);
1747 DCHECK(!new_entry);
1748 new_entry = request->TakePrerenderNavigationEntry();
1749 DCHECK(new_entry);
1750 }
1751
Charlie Reisc0f17d2d2021-01-12 18:52:491752 // Only make a copy of the pending entry if it is appropriate for the new
1753 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451754 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041755 // 1. The SiteInstance hasn't been assigned to something else.
1756 // 2. The pending entry was intended as a new entry, rather than being a
1757 // history navigation that was interrupted by an unrelated,
1758 // renderer-initiated navigation.
1759 // TODO(csharrison): Investigate whether we can remove some of the coarser
1760 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451761 if (!new_entry && PendingEntryMatchesRequest(request) &&
1762 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341763 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431764 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351765 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431766
Camille Lamy62b826012019-02-26 09:15:471767 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451768 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
creisf49dfc92016-07-26 17:05:181769 }
1770
Charlie Reisc0f17d2d2021-01-12 18:52:491771 // For cross-document commits with no matching pending entry, create a new
1772 // entry.
creisf49dfc92016-07-26 17:05:181773 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061774 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071775 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1776 initiator_origin,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:581777 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451778 params.transition, request->IsRendererInitiated(),
Rakina Zata Amniafd3c6582021-11-30 06:19:171779 nullptr, // blob_url_loader_factory
1780 false); // is_initial_entry
[email protected]f8f93eb2012-09-25 03:06:241781
1782 // Find out whether the new entry needs to update its virtual URL on URL
1783 // change and set up the entry accordingly. This is needed to correctly
1784 // update the virtual URL when replaceState is called after a pushState.
1785 GURL url = params.url;
1786 bool needs_update = false;
Charlie Reisc0f17d2d2021-01-12 18:52:491787 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411788 // update the virtual URL based on the new URL. For example, this is needed
1789 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1790 // the URL.
Rakina Zata Amni3460d382021-10-29 00:43:371791 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1792 &url, browser_context_, &needs_update);
1793 new_entry->set_update_virtual_url_with_url(needs_update);
1794
Camille Lamy62b826012019-02-26 09:15:471795 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451796 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
[email protected]e9ba4472008-09-14 15:42:431797 }
1798
Harkiran Bolaria59290d62021-03-17 01:53:011799 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
1800 // for prerendered contents, if any. This is because prerendering/activation
1801 // technically won't be creating a new document. Unlike BFCache, prerendering
1802 // creates a new NavigationEntry rather than using an existing one.
1803 if (!request->IsPrerenderedPageActivation()) {
Rakina Zata Amni3460d382021-10-29 00:43:371804 UpdateNavigationEntryDetails(new_entry.get(), rfh, params, request,
1805 NavigationEntryImpl::UpdatePolicy::kUpdate,
Rakina Zata Amnia4e27222021-12-22 01:05:001806 true /* is_new_entry */, commit_details);
creis8b5cd4c2015-06-19 00:11:081807
Harkiran Bolaria59290d62021-03-17 01:53:011808 // history.pushState() is classified as a navigation to a new page, but sets
1809 // is_same_document to true. In this case, we already have the title and
1810 // favicon available, so set them immediately.
1811 if (is_same_document && GetLastCommittedEntry()) {
1812 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
1813 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1814 }
[email protected]3a868f212014-08-09 10:41:191815 }
[email protected]ff64b3e2014-05-31 04:07:331816
[email protected]60d6cca2013-04-30 08:47:131817 DCHECK(!params.history_list_was_cleared || !replace_entry);
1818 // The browser requested to clear the session history when it initiated the
1819 // navigation. Now we know that the renderer has updated its state accordingly
1820 // and it is safe to also clear the browser side history.
1821 if (params.history_list_was_cleared) {
arthursonzogni69a6a1b2019-09-17 09:23:001822 DiscardNonCommittedEntries();
[email protected]60d6cca2013-04-30 08:47:131823 entries_.clear();
1824 last_committed_entry_index_ = -1;
1825 }
1826
Nasko Oskovaee2f862018-06-15 00:05:521827 // If this is a new navigation with replacement and there is a
1828 // pending_entry_ which matches the navigation reported by the renderer
1829 // process, then it should be the one replaced, so update the
1830 // last_committed_entry_index_ to use it.
1831 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101832 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521833 last_committed_entry_index_ = pending_entry_index_;
1834 }
1835
Alexander Timine3ec4192020-04-20 16:39:401836 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411837 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401838 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051839
Carlos IL42b416592019-10-07 23:10:361840 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
Dave Tapuska87696ae2021-11-18 18:48:311841 !request->post_commit_error_page_html().empty(),
Rakina Zata Amnia4e27222021-12-22 01:05:001842 rfh->frame_tree_node()->IsInFencedFrameTree(),
1843 commit_details);
[email protected]e9ba4472008-09-14 15:42:431844}
1845
Charlie Reisc0f17d2d2021-01-12 18:52:491846void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321847 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071848 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361849 bool is_same_document,
jam48cea9082017-02-15 06:13:291850 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451851 NavigationRequest* request,
Rakina Zata Amnia4e27222021-12-22 01:05:001852 bool keep_pending_entry,
1853 LoadCommittedDetails* commit_details) {
creis26d22632017-04-21 20:23:561854 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1855 << "that a last committed entry exists.";
1856
[email protected]e9ba4472008-09-14 15:42:431857 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001858 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431859
Charlie Reis7e2cb6d2021-01-26 01:27:161860 NavigationEntryImpl* entry = nullptr;
Rakina Zata Amni153d5702021-09-13 22:48:001861 if (request->commit_params().intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:161862 // We're guaranteed to have a last committed entry if intended_as_new_entry
1863 // is true.
avicbdc4c12015-07-01 16:07:111864 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:161865 DCHECK(entry);
1866
1867 // If the NavigationRequest matches a new pending entry and is classified as
1868 // EXISTING_ENTRY, then it is a navigation to the same URL that was
1869 // converted to a reload, such as a user pressing enter in the omnibox.
1870 if (pending_entry_ && pending_entry_index_ == -1 &&
1871 pending_entry_->GetUniqueID() ==
1872 request->commit_params().nav_entry_id) {
1873 // Note: The pending entry will usually have a real ReloadType here, but
1874 // it can still be ReloadType::NONE in cases that
1875 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
1876
1877 // If we classified this correctly, the SiteInstance should not have
1878 // changed.
1879 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
1880
1881 // For converted reloads, we assign the entry's unique ID to be that of
1882 // the new one. Since this is always the result of a user action, we want
1883 // to dismiss infobars, etc. like a regular user-initiated navigation.
1884 entry->set_unique_id(pending_entry_->GetUniqueID());
1885
1886 // The extra headers may have changed due to reloading with different
1887 // headers.
1888 entry->set_extra_headers(pending_entry_->extra_headers());
1889 }
1890 // Otherwise, this was intended as a new entry but the pending entry was
1891 // lost in the meantime and no new entry was created. We are stuck at the
1892 // last committed entry.
1893
1894 // Even if this is a converted reload from pressing enter in the omnibox,
1895 // the server could redirect, requiring an update to the SSL status. If this
1896 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451897 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:161898 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:471899 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451900 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:161901 }
Rakina Zata Amnif6950d552020-11-24 03:26:101902 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:111903 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:101904 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b902016-09-01 16:58:161905
eugenebut604866f2017-05-10 21:35:361906 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451907 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361908 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1909 // this was a restored same document navigation entry, then it won't have
1910 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1911 // navigated it.
jam48cea9082017-02-15 06:13:291912 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
Mike West800532c2021-10-14 09:26:521913 if (entry->GetURL().DeprecatedGetOriginAsURL() ==
1914 last_entry->GetURL().DeprecatedGetOriginAsURL() &&
jam48cea9082017-02-15 06:13:291915 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1916 was_restored) {
1917 entry->GetSSL() = last_entry->GetSSL();
1918 }
1919 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451920 // In rapid back/forward navigations |request| sometimes won't have a cert
1921 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191922 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451923 if (request->GetSSLInfo().has_value() &&
1924 request->GetSSLInfo()->is_valid()) {
1925 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
Mike West800532c2021-10-14 09:26:521926 } else if (entry->GetURL().DeprecatedGetOriginAsURL() !=
1927 request->GetURL().DeprecatedGetOriginAsURL()) {
John Abd-El-Malek3f247082017-12-07 19:02:191928 entry->GetSSL() = SSLStatus();
1929 }
jam48cea9082017-02-15 06:13:291930 }
avicbdc4c12015-07-01 16:07:111931 } else {
1932 // This is renderer-initiated. The only kinds of renderer-initated
Rakina Zata Amni557afb92021-07-17 04:39:571933 // navigations that are EXISTING_ENTRY are same-document navigations that
1934 // result in replacement (e.g. history.replaceState(), location.replace(),
1935 // forced replacements for trivial session history contexts). For these
1936 // cases, we reuse the last committed entry.
avicbdc4c12015-07-01 16:07:111937 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101938
Mikel Astizba9cf2fd2017-12-17 10:38:101939 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1940 // to avoid misleading interpretations (e.g. URLs paired with
1941 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1942 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1943 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1944
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571945 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:101946
eugenebut604866f2017-05-10 21:35:361947 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451948 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361949 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471950 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451951 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
avicbdc4c12015-07-01 16:07:111952 }
1953 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:431954
Rakina Zata Amni3460d382021-10-29 00:43:371955 UpdateNavigationEntryDetails(entry, rfh, params, request,
1956 NavigationEntryImpl::UpdatePolicy::kUpdate,
Rakina Zata Amnia4e27222021-12-22 01:05:001957 false /* is_new_entry */, commit_details);
creis22a7b4c2016-04-28 07:20:301958
[email protected]5ccd4dc2012-10-24 02:28:141959 // The redirected to page should not inherit the favicon from the previous
1960 // page.
eugenebut604866f2017-05-10 21:35:361961 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:481962 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:141963
Peter Boströmd7592132019-01-30 04:50:311964 // We should also usually discard the pending entry if it corresponds to a
1965 // different navigation, since that one is now likely canceled. In rare
1966 // cases, we leave the pending entry for another navigation in place when we
1967 // know it is still ongoing, to avoid a flicker in the omnibox (see
1968 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:431969 //
1970 // Note that we need to use the "internal" version since we don't want to
1971 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:311972 if (!keep_pending_entry)
Rakina Zata Amnia4e27222021-12-22 01:05:001973 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]40bcc302009-03-02 20:50:391974
Carlos IL4dea8902020-05-26 15:14:291975 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:111976 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:431977}
1978
[email protected]d202a7c2012-01-04 07:53:471979void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:321980 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071981 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361982 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:471983 bool replace_entry,
1984 bool previous_document_was_activated,
Rakina Zata Amnia4e27222021-12-22 01:05:001985 NavigationRequest* request,
1986 LoadCommittedDetails* commit_details) {
avi25f5f9e2015-07-17 20:08:261987 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1988 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
[email protected]09b8f82f2009-06-16 20:22:111989
[email protected]e9ba4472008-09-14 15:42:431990 // Manual subframe navigations just get the current entry cloned so the user
1991 // can go back or forward to it. The actual subframe information will be
1992 // stored in the page state for each of those entries. This happens out of
1993 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:091994 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1995 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:381996
Mikel Astizba9cf2fd2017-12-17 10:38:101997 // The DCHECK below documents the fact that we don't know of any situation
1998 // where |replace_entry| is true for subframe navigations. This simplifies
1999 // reasoning about the replacement struct for subframes (see
2000 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
2001 DCHECK(!replace_entry);
2002
Patrick Monette50e8bd82019-06-13 22:40:452003 // This FrameNavigationEntry might not end up being used in the
2004 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Anton Bikineevf62d1bf2021-05-15 17:56:072005 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452006 request->common_params().initiator_origin;
Patrick Monette50e8bd82019-06-13 22:40:452007 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:482008 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:082009 params.document_sequence_number, params.app_history_key,
2010 rfh->GetSiteInstance(), nullptr, params.url,
2011 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:092012 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072013 params.page_state, params.method, params.post_id,
2014 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032015 request->web_bundle_navigation_info()
2016 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392017 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482018 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:192019 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
Rakina Zata Amniafd3c6582021-11-30 06:19:172020 request->GetURL()));
Charles Reisf44482022017-10-13 21:15:032021
creisce0ef3572017-01-26 17:53:082022 std::unique_ptr<NavigationEntryImpl> new_entry =
2023 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:452024 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:002025 frame_tree_.root());
creise062d542015-08-25 02:01:552026
Alexander Timine3ec4192020-04-20 16:39:402027 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:412028 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:402029 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:472030
Rakina Zata Amnia4e27222021-12-22 01:05:002031 // If this navigation used to get ignored because there's no NavigationEntry
2032 // for the FrameNavigationEntry to attach to, note the fact that the new
2033 // NavigationEntry used to not exist.
2034 new_entry->set_did_not_exist_without_initial_navigation_entry(
2035 commit_details->used_to_be_ignored_due_to_null_navigation_entry);
2036
creisce0ef3572017-01-26 17:53:082037 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:452038 // to replace, which can happen due to a unique name change. See
2039 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
2040 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:382041
Dave Tapuska87696ae2021-11-18 18:48:312042 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false,
Rakina Zata Amnia4e27222021-12-22 01:05:002043 rfh->frame_tree_node()->IsInFencedFrameTree(),
2044 commit_details);
[email protected]e9ba4472008-09-14 15:42:432045}
2046
[email protected]d202a7c2012-01-04 07:53:472047bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:322048 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072049 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392050 bool is_same_document,
Nate Chapinc7019dd7d2021-06-25 18:29:252051 bool was_on_initial_empty_document,
Rakina Zata Amnia4e27222021-12-22 01:05:002052 NavigationRequest* request,
2053 LoadCommittedDetails* commit_details) {
avi9f07a0c2015-02-18 22:51:292054 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2055 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2056
[email protected]e9ba4472008-09-14 15:42:432057 // We're guaranteed to have a previously committed entry, and we now need to
2058 // handle navigation inside of a subframe in it without creating a new entry.
2059 DCHECK(GetLastCommittedEntry());
2060
creis913c63ce2016-07-16 19:52:522061 // For newly created subframes, we don't need to send a commit notification.
2062 // This is only necessary for history navigations in subframes.
2063 bool send_commit_notification = false;
2064
Rakina Zata Amnif6950d552020-11-24 03:26:102065 // If |nav_entry_id| is non-zero and matches an existing entry, this
2066 // is a history navigation. Update the last committed index accordingly. If
2067 // we don't recognize the |nav_entry_id|, it might be a recently
2068 // pruned entry. We'll handle it below.
2069 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2070 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472071 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062072 // Make sure that a subframe commit isn't changing the main frame's
2073 // origin. Otherwise the renderer process may be confused, leading to a
2074 // URL spoof. We can't check the path since that may change
2075 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572076 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2077 // about:blank, file, and unique origins are more subtle to get right.
Charlie Reis95fbca442021-05-21 21:38:242078 // We should use checks similar to RenderFrameHostImpl's
2079 // CanCommitUrlAndOrigin on the main frame during subframe commits.
2080 // See https://crbug.com/1209092.
creis37988b92016-06-10 18:03:572081 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2082 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2083 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2084 dest_top_url.SchemeIsHTTPOrHTTPS() &&
Mike West800532c2021-10-14 09:26:522085 current_top_url.DeprecatedGetOriginAsURL() !=
2086 dest_top_url.DeprecatedGetOriginAsURL()) {
creisfb6eeb62016-05-10 19:01:512087 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2088 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062089 }
creis3cdc3b02015-05-29 23:00:472090
creis913c63ce2016-07-16 19:52:522091 // We only need to discard the pending entry in this history navigation
2092 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062093 last_committed_entry_index_ = entry_index;
Rakina Zata Amnia4e27222021-12-22 01:05:002094 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
creis913c63ce2016-07-16 19:52:522095
2096 // History navigations should send a commit notification.
2097 send_commit_notification = true;
avi98405c22015-05-21 20:47:062098 }
[email protected]e9ba4472008-09-14 15:42:432099 }
[email protected]f233e4232013-02-23 00:55:142100
creisce0ef3572017-01-26 17:53:082101 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2102 // it may be a "history auto" case where we update an existing one.
2103 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Nate Chapin9f169072021-06-09 19:32:372104
2105 // We may want to update |last_committed|'s FrameNavigationEntry (if one
2106 // exists), or we may want to clobber it and create a new one. We update in
2107 // cases where the corresponding FrameNavigationEntry is conceptually similar
2108 // to the document described by the commit params: same-document
2109 // navigations, history traversal to an existing entry, and reloads (including
2110 // a "soft reload" where we navigate to the same url without flagging it as a
2111 // reload). But in the case of a different document that is not logically
2112 // related to the committed FrameNavigationEntry's document (cross-document,
2113 // not same url, not a reload, not a history traversal), we replace rather
2114 // than update.
Nate Chapinc7019dd7d2021-06-25 18:29:252115 //
Nate Chapin9f169072021-06-09 19:32:372116 // In the case where we update, the FrameNavigationEntry will potentially be
2117 // shared across multiple NavigationEntries, and any updates will be reflected
2118 // in all of those NavigationEntries. In the replace case, any existing
2119 // sharing with other NavigationEntries will stop.
Nate Chapinc7019dd7d2021-06-25 18:29:252120 //
2121 // When navigating away from the initial empty document, we also update rather
2122 // than replace. Either update or replace will overwrite the initial empty
2123 // document state for |last_committed|, but if the FrameNavigationEntry for
2124 // the initial empty document is shared across multiple NavigationEntries (due
2125 // to a navigation in another frame), we want to make sure we overwrite the
2126 // initial empty document state everywhere this FrameNavigationEntry is used,
2127 // which is accompished by updating the existing FrameNavigationEntry.
Nate Chapin9f169072021-06-09 19:32:372128 FrameNavigationEntry* last_committed_frame_entry =
2129 last_committed->GetFrameEntry(rfh->frame_tree_node());
2130 NavigationEntryImpl::UpdatePolicy update_policy =
2131 NavigationEntryImpl::UpdatePolicy::kUpdate;
2132 if (request->common_params().navigation_type ==
Minggang Wangb9f3fa92021-07-01 15:30:312133 blink::mojom::NavigationType::DIFFERENT_DOCUMENT &&
Nate Chapin9f169072021-06-09 19:32:372134 last_committed_frame_entry &&
Nate Chapinc7019dd7d2021-06-25 18:29:252135 last_committed_frame_entry->url() != params.url &&
2136 !was_on_initial_empty_document) {
Nate Chapin9f169072021-06-09 19:32:372137 update_policy = NavigationEntryImpl::UpdatePolicy::kReplace;
2138 }
2139
Rakina Zata Amni3460d382021-10-29 00:43:372140 UpdateNavigationEntryDetails(last_committed, rfh, params, request,
Rakina Zata Amnia4e27222021-12-22 01:05:002141 update_policy, false /* is_new_entry */,
2142 commit_details);
creis625a0c7d2015-03-24 23:17:122143
creis913c63ce2016-07-16 19:52:522144 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432145}
2146
[email protected]d202a7c2012-01-04 07:53:472147int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232148 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032149 for (size_t i = 0; i < entries_.size(); ++i) {
2150 if (entries_[i].get() == entry)
2151 return i;
2152 }
2153 return -1;
[email protected]765b35502008-08-21 00:51:202154}
2155
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572156void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242157 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572158 NavigationControllerImpl* source =
2159 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572160 // Verify that we look new.
Rakina Zata Amniafd3c6582021-11-30 06:19:172161 DCHECK_EQ(1, GetEntryCount());
2162 DCHECK(GetLastCommittedEntry()->IsInitialEntry());
Lei Zhang96031532019-10-10 19:05:472163 DCHECK(!GetPendingEntry());
Rakina Zata Amniafd3c6582021-11-30 06:19:172164 entries_.clear();
[email protected]ce3fa3c2009-04-20 19:55:572165
Rakina Zata Amniafd3c6582021-11-30 06:19:172166 if (source->GetEntryCount() == 0) {
2167 NOTREACHED();
2168 return;
2169 }
[email protected]ce3fa3c2009-04-20 19:55:572170
Francois Dorayeaace782017-06-21 16:37:242171 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442172 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572173 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572174
Fergal Dalya1d569972021-03-16 03:24:532175 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302176 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532177 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2178 session_storage_namespace_map_[it.first] = source_namespace->Clone();
Aaron Colwellb731a0ae2021-03-19 19:14:472179 OnStoragePartitionIdAdded(it.first);
[email protected]fdac6ade2013-07-20 01:06:302180 }
[email protected]4e6419c2010-01-15 04:50:342181
Lukasz Anforowicz0de0f452020-12-02 19:57:152182 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572183}
2184
Aran Gilman37d11632019-10-08 23:07:152185void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2186 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162187 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012188 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162189
[email protected]d202a7c2012-01-04 07:53:472190 NavigationControllerImpl* source =
2191 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252192
avi2b177592014-12-10 02:08:022193 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012194 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252195
[email protected]474f8512013-05-31 22:31:162196 // We now have one entry, possibly with a new pending entry. Ensure that
2197 // adding the entries from source won't put us over the limit.
2198 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572199 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572200 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252201
Carlos IL4dea8902020-05-26 15:14:292202 // Insert the entries from source. Ignore any pending entry, since it has not
2203 // committed in source.
[email protected]474f8512013-05-31 22:31:162204 int max_source_index = source->last_committed_entry_index_;
Rakina Zata Amniafd3c6582021-11-30 06:19:172205 DCHECK_NE(max_source_index, -1);
2206 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572207
2208 // Ignore the source's current entry if merging with replacement.
2209 // TODO(davidben): This should preserve entries forward of the current
2210 // too. http://crbug.com/317872
2211 if (replace_entry && max_source_index > 0)
2212 max_source_index--;
2213
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572214 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252215
2216 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552217 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142218
Hayato Ito2c8c08d02021-06-23 03:38:432219 BroadcastHistoryOffsetAndLength();
[email protected]e1cd5452010-08-26 18:03:252220}
2221
[email protected]79368982013-11-13 01:11:012222bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162223 // If there is no last committed entry, we cannot prune. Even if there is a
2224 // pending entry, it may not commit, leaving this WebContents blank, despite
2225 // possibly giving it new entries via CopyStateFromAndPrune.
2226 if (last_committed_entry_index_ == -1)
2227 return false;
[email protected]9350602e2013-02-26 23:27:442228
[email protected]474f8512013-05-31 22:31:162229 // We cannot prune if there is a pending entry at an existing entry index.
2230 // It may not commit, so we have to keep the last committed entry, and thus
2231 // there is no sensible place to keep the pending entry. It is ok to have
2232 // a new pending entry, which can optionally commit as a new navigation.
2233 if (pending_entry_index_ != -1)
2234 return false;
2235
[email protected]474f8512013-05-31 22:31:162236 return true;
2237}
2238
[email protected]79368982013-11-13 01:11:012239void NavigationControllerImpl::PruneAllButLastCommitted() {
2240 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162241
avi2b177592014-12-10 02:08:022242 DCHECK_EQ(0, last_committed_entry_index_);
2243 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442244
Hayato Ito2c8c08d02021-06-23 03:38:432245 BroadcastHistoryOffsetAndLength();
[email protected]9350602e2013-02-26 23:27:442246}
2247
[email protected]79368982013-11-13 01:11:012248void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162249 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012250 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262251
[email protected]474f8512013-05-31 22:31:162252 // Erase all entries but the last committed entry. There may still be a
2253 // new pending entry after this.
2254 entries_.erase(entries_.begin(),
2255 entries_.begin() + last_committed_entry_index_);
2256 entries_.erase(entries_.begin() + 1, entries_.end());
2257 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262258}
2259
Christian Dullweber1af31e62018-02-22 11:49:482260void NavigationControllerImpl::DeleteNavigationEntries(
2261 const DeletionPredicate& deletionPredicate) {
2262 // It is up to callers to check the invariants before calling this.
2263 CHECK(CanPruneAllButLastCommitted());
2264 std::vector<int> delete_indices;
2265 for (size_t i = 0; i < entries_.size(); i++) {
2266 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572267 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482268 delete_indices.push_back(i);
2269 }
2270 }
2271 if (delete_indices.empty())
2272 return;
2273
2274 if (delete_indices.size() == GetEntryCount() - 1U) {
2275 PruneAllButLastCommitted();
2276 } else {
2277 // Do the deletion in reverse to preserve indices.
2278 for (auto it = delete_indices.rbegin(); it != delete_indices.rend(); ++it) {
2279 RemoveEntryAtIndex(*it);
2280 }
Hayato Ito2c8c08d02021-06-23 03:38:432281 BroadcastHistoryOffsetAndLength();
Christian Dullweber1af31e62018-02-22 11:49:482282 }
2283 delegate()->NotifyNavigationEntriesDeleted();
2284}
2285
Shivani Sharma883f5f32019-02-12 18:20:012286bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2287 auto* entry = GetEntryAtIndex(index);
2288 return entry && entry->should_skip_on_back_forward_ui();
2289}
2290
Carlos Caballero35ce710c2019-09-19 10:59:452291BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2292 return back_forward_cache_;
2293}
2294
clamy987a3752018-05-03 17:36:262295void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2296 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2297 // progress, since this will cause a use-after-free. (We only allow this
2298 // when the tab is being destroyed for shutdown, since it won't return to
2299 // NavigateToEntry in that case.) http://crbug.com/347742.
Takashi Toyoshimaea534ef22021-07-21 03:27:592300 CHECK(!in_navigate_to_pending_entry_ || frame_tree_.IsBeingDestroyed());
clamy987a3752018-05-03 17:36:262301
2302 if (was_failure && pending_entry_) {
2303 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2304 } else {
2305 failed_pending_entry_id_ = 0;
2306 }
2307
2308 if (pending_entry_) {
2309 if (pending_entry_index_ == -1)
2310 delete pending_entry_;
2311 pending_entry_index_ = -1;
2312 pending_entry_ = nullptr;
2313 }
arthursonzogni66f711c2019-10-08 14:40:362314
2315 // Ensure any refs to the current pending entry are ignored if they get
2316 // deleted, by clearing the set of known refs. All future pending entries will
2317 // only be affected by new refs.
2318 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262319}
2320
2321void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2322 if (pending_entry_)
2323 pending_entry_->set_ssl_error(error);
2324}
2325
Camille Lamy5193caa2018-10-12 11:59:422326#if defined(OS_ANDROID)
2327// static
2328bool NavigationControllerImpl::ValidateDataURLAsString(
2329 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2330 if (!data_url_as_string)
2331 return false;
2332
2333 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2334 return false;
2335
2336 // The number of characters that is enough for validating a data: URI.
2337 // From the GURL's POV, the only important part here is scheme, it doesn't
2338 // check the actual content. Thus we can take only the prefix of the url, to
2339 // avoid unneeded copying of a potentially long string.
2340 const size_t kDataUriPrefixMaxLen = 64;
2341 GURL data_url(
2342 std::string(data_url_as_string->front_as<char>(),
2343 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2344 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2345 return false;
2346
2347 return true;
2348}
2349#endif
2350
Shivani Sharma194877032019-03-07 17:52:472351void NavigationControllerImpl::NotifyUserActivation() {
2352 // When a user activation occurs, ensure that all adjacent entries for the
2353 // same document clear their skippable bit, so that the history manipulation
2354 // intervention does not apply to them.
2355 auto* last_committed_entry = GetLastCommittedEntry();
2356 if (!last_committed_entry)
2357 return;
Shivani Sharma194877032019-03-07 17:52:472358
shivanigithub99368382021-06-16 18:33:372359 if (base::FeatureList::IsEnabled(
2360 features::kDebugHistoryInterventionNoUserActivation)) {
2361 return;
2362 }
2363
Shivani Sharmac4cc8922019-04-18 03:11:172364 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472365}
2366
clamy987a3752018-05-03 17:36:262367bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2368 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172369 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262370 NavigationEntryImpl* entry =
2371 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2372 if (!entry)
2373 return false;
2374
2375 FrameNavigationEntry* frame_entry =
2376 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2377 if (!frame_entry)
2378 return false;
2379
Nate Chapin45f620582021-09-30 17:45:432380 // |is_browser_initiated| is false here because a navigation in a new subframe
2381 // always begins with renderer action (i.e., an HTML element being inserted
2382 // into the DOM), so it is always renderer-initiated.
Camille Lamy5193caa2018-10-12 11:59:422383 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572384 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232385 ReloadType::NONE, false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432386 true /* is_history_navigation_in_new_child */,
2387 false /* is_browser_initiated */);
clamyea99ea12018-05-28 13:54:232388
2389 if (!request)
2390 return false;
2391
arthursonzognif046d4a2019-12-12 19:08:102392 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412393
Lukasz Anforowicz9ee83c272020-12-01 20:14:052394 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2395 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232396
2397 return true;
clamy987a3752018-05-03 17:36:262398}
2399
Tsuyoshi Horo52fd08e2020-07-07 07:03:452400bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2401 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2402 if (!entry)
2403 return false;
2404 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2405 if (!frame_entry)
2406 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142407 ReloadType reload_type = ReloadType::NORMAL;
2408 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452409 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142410 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452411 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432412 false /* is_history_navigation_in_new_child */,
2413 true /* is_browser_initiated */);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452414 if (!request)
2415 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052416 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452417 return true;
2418}
2419
Dave Tapuska8bfd84c2019-03-26 20:47:162420void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2421 int offset,
2422 int sandbox_frame_tree_node_id) {
2423 if (!CanGoToOffset(offset))
2424 return;
Nate Chapin45f620582021-09-30 17:45:432425 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id,
2426 false /* is_browser_initiated */);
Dave Tapuska8bfd84c2019-03-26 20:47:162427}
2428
clamy987a3752018-05-03 17:36:262429void NavigationControllerImpl::NavigateFromFrameProxy(
2430 RenderFrameHostImpl* render_frame_host,
2431 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022432 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452433 int initiator_process_id,
Anton Bikineevf62d1bf2021-05-15 17:56:072434 const absl::optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262435 bool is_renderer_initiated,
2436 SiteInstance* source_site_instance,
2437 const Referrer& referrer,
2438 ui::PageTransition page_transition,
2439 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252440 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262441 const std::string& method,
2442 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092443 const std::string& extra_headers,
Antonio Sartori2f763d9d2021-04-21 10:04:142444 network::mojom::SourceLocationPtr source_location,
John Delaney50425f82020-04-07 16:26:212445 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Anton Bikineevf62d1bf2021-05-15 17:56:072446 const absl::optional<blink::Impression>& impression) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582447 if (is_renderer_initiated)
2448 DCHECK(initiator_origin.has_value());
2449
clamy987a3752018-05-03 17:36:262450 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582451
clamy987a3752018-05-03 17:36:262452 // Create a NavigationEntry for the transfer, without making it the pending
2453 // entry. Subframe transfers should have a clone of the last committed entry
2454 // with a FrameNavigationEntry for the target frame. Main frame transfers
2455 // should have a new NavigationEntry.
2456 // TODO(creis): Make this unnecessary by creating (and validating) the params
2457 // directly, passing them to the destination RenderFrameHost. See
2458 // https://crbug.com/536906.
2459 std::unique_ptr<NavigationEntryImpl> entry;
Harkiran Bolariae1b5158b2021-09-16 19:03:262460 if (!render_frame_host->is_main_frame()) {
clamy987a3752018-05-03 17:36:262461 // Subframe case: create FrameNavigationEntry.
Rakina Zata Amniafd3c6582021-11-30 06:19:172462 CHECK(GetLastCommittedEntry());
2463 entry = GetLastCommittedEntry()->Clone();
2464 entry->set_extra_headers(extra_headers);
2465 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2466 // Renderer-initiated navigation that target a remote frame are currently
2467 // classified as browser-initiated when this one has already navigated.
2468 // See https://crbug.com/722251.
Nate Chapin9f169072021-06-09 19:32:372469 // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter
2470 // to CreateNavigationRequestFromLoadParams(), so while kReplace might
2471 // remove child FrameNavigationEntries (e.g., if this is a cross-process
2472 // same-document navigation), they will still be present in the
2473 // committed NavigationEntry that will be referenced to construct the new
2474 // FrameNavigationEntry tree when this navigation commits.
clamy987a3752018-05-03 17:36:262475 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:082476 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582477 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072478 absl::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202479 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392480 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482481 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori79d549d2021-02-18 12:59:542482 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262483 } else {
2484 // Main frame case.
2485 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102486 url, referrer, initiator_origin, source_site_instance, page_transition,
2487 is_renderer_initiated, extra_headers, browser_context_,
Hayato Ito303654c2021-06-30 09:07:542488 blob_url_loader_factory));
clamy987a3752018-05-03 17:36:262489 entry->root_node()->frame_entry->set_source_site_instance(
2490 static_cast<SiteInstanceImpl*>(source_site_instance));
2491 entry->root_node()->frame_entry->set_method(method);
2492 }
clamy987a3752018-05-03 17:36:262493
Camille Lamy5193caa2018-10-12 11:59:422494 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262495 if (GetLastCommittedEntry() &&
2496 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2497 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422498 override_user_agent = true;
clamy987a3752018-05-03 17:36:262499 }
2500 // TODO(creis): Set user gesture and intent received timestamp on Android.
2501
2502 // We may not have successfully added the FrameNavigationEntry to |entry|
2503 // above (per https://crbug.com/608402), in which case we create it from
2504 // scratch. This works because we do not depend on |frame_entry| being inside
2505 // |entry| during NavigateToEntry. This will go away when we shortcut this
2506 // further in https://crbug.com/536906.
2507 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2508 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452509 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Nate Chapinfbfe5af2021-06-10 17:22:082510 node->unique_name(), -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582511 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072512 absl::nullopt /* origin */, referrer, initiator_origin,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:122513 std::vector<GURL>(), blink::PageState(), method, -1,
2514 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482515 nullptr /* subresource_web_bundle_navigation_info */,
Titouan Rigoudy6ec70402021-02-02 15:42:192516 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262517 }
2518
Camille Lamy5193caa2018-10-12 11:59:422519 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022520 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452521 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292522 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422523 params.source_site_instance = source_site_instance;
2524 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2525 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032526 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422527 params.referrer = referrer;
2528 /* params.redirect_chain: skip */
2529 params.extra_headers = extra_headers;
2530 params.is_renderer_initiated = is_renderer_initiated;
2531 params.override_user_agent = UA_OVERRIDE_INHERIT;
2532 /* params.base_url_for_data_url: skip */
2533 /* params.virtual_url_for_data_url: skip */
2534 /* params.data_url_as_string: skip */
2535 params.post_data = post_body;
2536 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582537 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422538 /* params.frame_name: skip */
2539 // TODO(clamy): See if user gesture should be propagated to this function.
2540 params.has_user_gesture = false;
2541 params.should_clear_history_list = false;
2542 params.started_from_context_menu = false;
2543 /* params.navigation_ui_data: skip */
2544 /* params.input_start: skip */
Minggang Wangf59db47b2021-06-16 01:56:222545 params.was_activated = blink::mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542546 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212547 params.impression = impression;
Antonio Sartori6984c742021-08-26 08:03:412548 params.download_policy = std::move(download_policy);
Camille Lamy5193caa2018-10-12 11:59:422549
2550 std::unique_ptr<NavigationRequest> request =
2551 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032552 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:142553 false /* has_user_gesture */, std::move(source_location),
Antonio Sartori6984c742021-08-26 08:03:412554 ReloadType::NONE, entry.get(), frame_entry.get());
clamyea99ea12018-05-28 13:54:232555
2556 if (!request)
2557 return;
2558
Arthur Hemery948742762019-09-18 10:06:242559 // At this stage we are proceeding with this navigation. If this was renderer
2560 // initiated with user gesture, we need to make sure we clear up potential
2561 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2562 DiscardNonCommittedEntries();
2563
Lukasz Anforowicz9ee83c272020-12-01 20:14:052564 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262565}
2566
[email protected]d1198fd2012-08-13 22:50:192567void NavigationControllerImpl::SetSessionStorageNamespace(
Aaron Colwellf3b316e2021-03-11 20:17:052568 const StoragePartitionId& partition_id,
[email protected]8ff00d72012-10-23 19:12:212569 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192570 if (!session_storage_namespace)
2571 return;
2572
2573 // We can't overwrite an existing SessionStorage without violating spec.
2574 // Attempts to do so may give a tab access to another tab's session storage
2575 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152576 bool successful_insert =
2577 session_storage_namespace_map_
Aaron Colwellf3b316e2021-03-11 20:17:052578 .insert(std::make_pair(partition_id,
2579 static_cast<SessionStorageNamespaceImpl*>(
2580 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302581 .second;
2582 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
Aaron Colwellb731a0ae2021-03-19 19:14:472583 OnStoragePartitionIdAdded(partition_id);
[email protected]d1198fd2012-08-13 22:50:192584}
2585
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572586bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Rakina Zata Amniafd3c6582021-11-30 06:19:172587 return IsInitialNavigation() && GetLastCommittedEntry()->IsInitialEntry() &&
Carlos Caballeroede6f8c2021-01-28 11:01:502588 !frame_tree_.has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462589}
2590
Aran Gilman37d11632019-10-08 23:07:152591SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Aaron Colwell78b4bde2021-03-16 16:16:092592 const SiteInfo& site_info) {
2593 // TODO(acolwell): Remove partition_id logic once we have successfully
2594 // migrated the implementation to be a StoragePartitionConfig. At that point
2595 // |site_info| can be replaced with a StoragePartitionConfig.
2596 const StoragePartitionId partition_id =
2597 site_info.GetStoragePartitionId(browser_context_);
2598 const StoragePartitionConfig partition_config =
Robbie McElrath7d4bd852021-07-24 04:02:192599 site_info.storage_partition_config();
[email protected]d1198fd2012-08-13 22:50:192600
[email protected]fdac6ade2013-07-20 01:06:302601 StoragePartition* partition =
Lukasz Anforowiczb9a969a2021-04-29 15:26:252602 browser_context_->GetStoragePartition(partition_config);
michaelnbacbcbd2016-02-09 00:32:032603 DOMStorageContextWrapper* context_wrapper =
2604 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2605
2606 SessionStorageNamespaceMap::const_iterator it =
2607 session_storage_namespace_map_.find(partition_id);
2608 if (it != session_storage_namespace_map_.end()) {
2609 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152610 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2611 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:472612
2613 // Verify that the config we generated now matches the one that
2614 // was generated when the namespace was added to the map.
Aaron Colwellb731a0ae2021-03-19 19:14:472615 if (partition_config != it->first.config()) {
2616 LogStoragePartitionIdCrashKeys(it->first, partition_id);
2617 }
Sharon Yangd4f48792021-06-30 17:02:532618 CHECK_EQ(partition_config, it->first.config());
Aaron Colwellb731a0ae2021-03-19 19:14:472619
michaelnbacbcbd2016-02-09 00:32:032620 return it->second.get();
2621 }
2622
2623 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102624 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2625 SessionStorageNamespaceImpl::Create(context_wrapper);
2626 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2627 session_storage_namespace.get();
2628 session_storage_namespace_map_[partition_id] =
2629 std::move(session_storage_namespace);
Aaron Colwellb731a0ae2021-03-19 19:14:472630 OnStoragePartitionIdAdded(partition_id);
[email protected]fdac6ade2013-07-20 01:06:302631
Daniel Murphy31bbb8b12018-02-07 21:44:102632 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302633}
2634
2635SessionStorageNamespace*
2636NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Robbie McElrath7d4bd852021-07-24 04:02:192637 return GetSessionStorageNamespace(SiteInfo(GetBrowserContext()));
[email protected]fdac6ade2013-07-20 01:06:302638}
2639
2640const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572641NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302642 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552643}
[email protected]d202a7c2012-01-04 07:53:472644
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572645bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562646 return needs_reload_;
2647}
[email protected]a26023822011-12-29 00:23:552648
[email protected]46bb5e9c2013-10-03 22:16:472649void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412650 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2651}
2652
2653void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472654 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412655 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542656
2657 if (last_committed_entry_index_ != -1) {
2658 entries_[last_committed_entry_index_]->SetTransitionType(
2659 ui::PAGE_TRANSITION_RELOAD);
2660 }
[email protected]46bb5e9c2013-10-03 22:16:472661}
2662
[email protected]d202a7c2012-01-04 07:53:472663void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572664 DCHECK_LT(index, GetEntryCount());
2665 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312666 DiscardNonCommittedEntries();
2667
2668 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122669 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312670 last_committed_entry_index_--;
2671}
2672
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572673NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272674 // If there is no pending_entry_, there should be no pending_entry_index_.
2675 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2676
2677 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362678 // at that index. An exception is while a reload of a post commit error page
2679 // is ongoing; in that case pending entry will point to the entry replaced
2680 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272681 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362682 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2683 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272684
[email protected]022af742011-12-28 18:37:252685 return pending_entry_;
2686}
2687
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572688int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272689 // The pending entry index must always be less than the number of entries.
arthursonzogni5c4c202d2017-04-25 23:41:272690 DCHECK_LT(pending_entry_index_, GetEntryCount());
[email protected]a26023822011-12-29 00:23:552691 return pending_entry_index_;
2692}
2693
avi25764702015-06-23 15:43:372694void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572695 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362696 bool replace,
Dave Tapuska87696ae2021-11-18 18:48:312697 bool was_post_commit_error,
Rakina Zata Amnia4e27222021-12-22 01:05:002698 bool in_fenced_frame_tree,
2699 LoadCommittedDetails* commit_details) {
Dave Tapuska87696ae2021-11-18 18:48:312700 // Fenced frame trees should always have `ui::PAGE_TRANSITION_AUTO_SUBFRAME`
2701 // set because:
2702 // 1) They don't influence the history of the outer page.
2703 // 2) They are always replace only navigation (there is always only one entry
2704 // in their history stack).
2705 // 3) Are not top level navigations and appear similar to iframes.
2706 // Navigations of the fenced frame might create a new NavigationEntry, which
2707 // will call this function. Non fenced frame navigations will never have
2708 // `ui::PAGE_TRANSITION_AUTO_SUBFRAME` because they won't call
2709 // InsertOrReplaceEntry.
2710 DCHECK_EQ(in_fenced_frame_tree,
2711 ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2712 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202713
avi5cad4912015-06-19 05:25:442714 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2715 // need to keep continuity with the pending entry, so copy the pending entry's
2716 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2717 // then the renderer navigated on its own, independent of the pending entry,
2718 // so don't copy anything.
2719 if (pending_entry_ && pending_entry_index_ == -1)
2720 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202721
Rakina Zata Amnia4e27222021-12-22 01:05:002722 DiscardNonCommittedEntriesWithCommitDetails(commit_details);
[email protected]765b35502008-08-21 00:51:202723
creisee17e932015-07-17 17:56:222724 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362725 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102726 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572727 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362728 // If the new entry is a post-commit error page, we store the current last
2729 // committed entry to the side so that we can put it back when navigating
2730 // away from the error.
2731 if (was_post_commit_error) {
2732 DCHECK(!entry_replaced_by_post_commit_error_);
2733 entry_replaced_by_post_commit_error_ =
2734 std::move(entries_[last_committed_entry_index_]);
2735 }
dcheng36b6aec92015-12-26 06:16:362736 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222737 return;
2738 }
[email protected]765b35502008-08-21 00:51:202739
creis37979a62015-08-04 19:48:182740 // We shouldn't see replace == true when there's no committed entries.
2741 DCHECK(!replace);
2742
Michael Thiessen9b14d512019-09-23 21:19:472743 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202744
Shivani Sharmad8c8d652019-02-13 17:27:572745 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202746
dcheng36b6aec92015-12-26 06:16:362747 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202748 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292749}
2750
Shivani Sharmad8c8d652019-02-13 17:27:572751void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162752 if (entries_.size() < max_entry_count())
2753 return;
2754
2755 DCHECK_EQ(max_entry_count(), entries_.size());
2756 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572757 CHECK_EQ(pending_entry_index_, -1);
2758
2759 int index = 0;
Elly Fong-Jonesccc6d1f2021-06-14 18:32:422760 // Retrieve the oldest skippable entry.
2761 for (; index < GetEntryCount(); index++) {
2762 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2763 break;
Shivani Sharmad8c8d652019-02-13 17:27:572764 }
2765
2766 // If there is no skippable entry or if it is the last committed entry then
2767 // fall back to pruning the oldest entry. It is not safe to prune the last
2768 // committed entry.
2769 if (index == GetEntryCount() || index == last_committed_entry_index_)
2770 index = 0;
2771
2772 bool should_succeed = RemoveEntryAtIndex(index);
2773 DCHECK_EQ(true, should_succeed);
2774
2775 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252776}
2777
clamy3cb9bea92018-07-10 12:42:022778void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162779 ReloadType reload_type,
Nate Chapin45f620582021-09-30 17:45:432780 int sandboxed_source_frame_tree_node_id,
2781 bool is_browser_initiated) {
Alexander Timin3a92df72019-09-20 11:59:502782 TRACE_EVENT0("navigation",
2783 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272784 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022785 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362786 if (pending_entry_index_ != -1) {
2787 // The pending entry may not be in entries_ if a post-commit error page is
2788 // showing.
2789 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2790 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2791 }
Gyuyoung Kim107c2a02021-04-13 01:49:302792 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572793 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:012794 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:002795 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:572796 int nav_entry_id = pending_entry_->GetUniqueID();
2797
[email protected]83c2e232011-10-07 21:36:462798 // If we were navigating to a slow-to-commit page, and the user performs
2799 // a session history navigation to the last committed page, RenderViewHost
2800 // will force the throbber to start, but WebKit will essentially ignore the
2801 // navigation, and won't send a message to stop the throbber. To prevent this
2802 // from happening, we drop the navigation here and stop the slow-to-commit
2803 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022804 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:082805 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:272806 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
Takashi Toyoshimabf549472021-07-01 07:47:202807 frame_tree_.StopLoading();
[email protected]6a13a6c2011-12-20 21:47:122808
[email protected]83c2e232011-10-07 21:36:462809 DiscardNonCommittedEntries();
2810 return;
2811 }
2812
creisce0ef3572017-01-26 17:53:082813 // Compare FrameNavigationEntries to see which frames in the tree need to be
2814 // navigated.
clamy3cb9bea92018-07-10 12:42:022815 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2816 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Nate Chapin45f620582021-09-30 17:45:432817 FindFramesToNavigate(root, reload_type, is_browser_initiated,
2818 &same_document_loads, &different_document_loads);
creis4e2ecb72015-06-20 00:46:302819
2820 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572821 // We were unable to match any frames to navigate. This can happen if a
2822 // history navigation targets a subframe that no longer exists
2823 // (https://crbug.com/705550). In this case, we need to update the current
2824 // history entry to the pending one but keep the main document loaded. We
2825 // also need to ensure that observers are informed about the updated
2826 // current history entry (e.g., for greying out back/forward buttons), and
2827 // that renderer processes update their history offsets. The easiest way
2828 // to do all that is to schedule a "redundant" same-document navigation in
2829 // the main frame.
2830 //
2831 // Note that we don't want to remove this history entry, as it might still
2832 // be valid later, since a frame that it's targeting may be recreated.
2833 //
2834 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
2835 // need to repeat history navigations until finding the one that works.
2836 // Consider changing this behavior to keep looking for the first valid
2837 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:022838 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422839 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572840 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:572841 ReloadType::NONE /* reload_type */,
2842 true /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432843 false /* is_history_navigation_in_new_child */,
2844 is_browser_initiated);
clamy3cb9bea92018-07-10 12:42:022845 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572846 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:022847 DiscardPendingEntry(false);
2848 return;
2849 }
Alex Moshchuk3a4e77a2020-05-29 21:32:572850 same_document_loads.push_back(std::move(navigation_request));
2851
2852 // Sanity check that we never take this branch for any kinds of reloads,
2853 // as those should've queued a different-document load in the main frame.
2854 DCHECK(!is_forced_reload);
2855 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:302856 }
2857
Dave Tapuska8bfd84c2019-03-26 20:47:162858 // If |sandboxed_source_frame_node_id| is valid, then track whether this
2859 // navigation affects any frame outside the frame's subtree.
2860 if (sandboxed_source_frame_tree_node_id !=
2861 FrameTreeNode::kFrameTreeNodeInvalidId) {
2862 bool navigates_inside_tree =
2863 DoesSandboxNavigationStayWithinSubtree(
2864 sandboxed_source_frame_tree_node_id, same_document_loads) &&
2865 DoesSandboxNavigationStayWithinSubtree(
2866 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:502867 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:162868 // the number of pages that trigger this.
2869 FrameTreeNode* sandbox_source_frame_tree_node =
2870 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
2871 if (sandbox_source_frame_tree_node) {
2872 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
2873 sandbox_source_frame_tree_node->current_frame_host(),
2874 navigates_inside_tree
2875 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
2876 : blink::mojom::WebFeature::
2877 kSandboxBackForwardAffectsFramesOutsideSubtree);
2878 }
Dave Tapuska855c1e12019-08-23 20:45:522879
2880 // If the navigation occurred outside the tree discard it because
2881 // the sandboxed frame didn't have permission to navigate outside
2882 // its tree. If it is possible that the navigation is both inside and
2883 // outside the frame tree and we discard it entirely because we don't
2884 // want to end up in a history state that didn't exist before.
2885 if (base::FeatureList::IsEnabled(
2886 features::kHistoryPreventSandboxedNavigation) &&
2887 !navigates_inside_tree) {
2888 DiscardPendingEntry(false);
2889 return;
2890 }
Dave Tapuska8bfd84c2019-03-26 20:47:162891 }
2892
Carlos Caballero539a421c2020-07-06 10:25:572893 // BackForwardCache:
2894 // Navigate immediately if the document is in the BackForwardCache.
2895 if (back_forward_cache_.GetEntry(nav_entry_id)) {
2896 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
2897 DCHECK_EQ(reload_type, ReloadType::NONE);
2898 auto navigation_request = CreateNavigationRequestFromEntry(
2899 root, pending_entry_, pending_entry_->GetFrameEntry(root),
2900 ReloadType::NONE, false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432901 false /* is_history_navigation_in_new_child */, is_browser_initiated);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052902 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:572903
2904 return;
2905 }
2906
2907 // History navigation might try to reuse a specific BrowsingInstance, already
2908 // used by a page in the cache. To avoid having two different main frames that
2909 // live in the same BrowsingInstance, evict the all pages with this
2910 // BrowsingInstance from the cache.
2911 //
2912 // For example, take the following scenario:
2913 //
2914 // A1 = Some page on a.com
2915 // A2 = Some other page on a.com
2916 // B3 = An uncacheable page on b.com
2917 //
2918 // Then the following navigations occur:
2919 // A1->A2->B3->A1
2920 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
2921 // take its place) and A1 will be created in the same BrowsingInstance (and
2922 // SiteInstance), as A2.
2923 //
2924 // If we didn't do anything, both A1 and A2 would remain alive in the same
2925 // BrowsingInstance/SiteInstance, which is unsupported by
2926 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
2927 // A2 from the cache.
2928 if (pending_entry_->site_instance()) {
2929 back_forward_cache_.EvictFramesInRelatedSiteInstances(
2930 pending_entry_->site_instance());
2931 }
2932
clamy3cb9bea92018-07-10 12:42:022933 // This call does not support re-entrancy. See http://crbug.com/347742.
2934 CHECK(!in_navigate_to_pending_entry_);
2935 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:302936
arthursonzogni66f711c2019-10-08 14:40:362937 // It is not possible to delete the pending NavigationEntry while navigating
2938 // to it. Grab a reference to delay potential deletion until the end of this
2939 // function.
2940 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
2941
creis4e2ecb72015-06-20 00:46:302942 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:022943 for (auto& item : same_document_loads) {
2944 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052945 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302946 }
clamy3cb9bea92018-07-10 12:42:022947 for (auto& item : different_document_loads) {
2948 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052949 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302950 }
clamy3cb9bea92018-07-10 12:42:022951
2952 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:302953}
2954
Alex Moshchuk3a4e77a2020-05-29 21:32:572955NavigationControllerImpl::HistoryNavigationAction
2956NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:302957 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:572958 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:422959 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:582960 // Only active and prerendered documents are allowed to navigate in their
2961 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422962 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:012963 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:582964 // - If the document is in pending deletion, the browser already committed
2965 // to destroying this RenderFrameHost. See https://crbug.com/930278.
2966 // - If the document is in back-forward cache, it's not allowed to navigate
2967 // as it should remain frozen. Ignore the request and evict the document
2968 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422969 //
Sreeja Kamishettydb8e2892021-03-10 09:30:582970 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:422971 // which should all be inactive as well.
Fergal Daly1336ac642021-09-14 15:13:112972 if (frame->current_frame_host()->IsInactiveAndDisallowActivation(
2973 DisallowActivationReasonId::kDetermineActionForHistoryNavigation)) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:422974 return HistoryNavigationAction::kStopLooking;
Fergal Daly1336ac642021-09-14 15:13:112975 }
Sreeja Kamishetty8eacabb2021-03-09 11:45:422976 }
arthursonzogni03f76152019-02-12 10:35:202977
Alex Moshchuk3a4e77a2020-05-29 21:32:572978 // If there's no last committed entry, there is no previous history entry to
2979 // compare against, so fall back to a different-document load. Note that we
2980 // should only reach this case for the root frame and not descend further
2981 // into subframes.
2982 if (!GetLastCommittedEntry()) {
2983 DCHECK(frame->IsMainFrame());
2984 return HistoryNavigationAction::kDifferentDocument;
2985 }
2986
2987 // Reloads should result in a different-document load. Note that reloads may
2988 // also happen via the |needs_reload_| mechanism where the reload_type is
2989 // NONE, so detect this by comparing whether we're going to the same
2990 // entry that we're currently on. Similarly to above, only main frames
2991 // should reach this. Note that subframes support reloads, but that's done
2992 // via a different path that doesn't involve FindFramesToNavigate (see
2993 // RenderFrameHost::Reload()).
2994 if (reload_type != ReloadType::NONE ||
2995 pending_entry_index_ == last_committed_entry_index_) {
2996 DCHECK(frame->IsMainFrame());
2997 return HistoryNavigationAction::kDifferentDocument;
2998 }
2999
Alex Moshchuk47d1a4bd2020-06-01 22:15:343000 // If there is no new FrameNavigationEntry for the frame, ignore the
3001 // load. For example, this may happen when going back to an entry before a
3002 // frame was created. Suppose we commit a same-document navigation that also
3003 // results in adding a new subframe somewhere in the tree. If we go back,
3004 // the new subframe will be missing a FrameNavigationEntry in the previous
3005 // NavigationEntry, but we shouldn't delete or change what's loaded in
3006 // it.
3007 //
Alex Moshchuke65c39272020-06-03 17:55:373008 // Note that in this case, there is no need to keep looking for navigations
3009 // in subframes, which would be missing FrameNavigationEntries as well.
3010 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:343011 // It's important to check this before checking |old_item| below, since both
3012 // might be null, and in that case we still shouldn't change what's loaded in
3013 // this frame. Note that scheduling any loads assumes that |new_item| is
3014 // non-null. See https://crbug.com/1088354.
3015 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3016 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:373017 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:343018
Alex Moshchuk3a4e77a2020-05-29 21:32:573019 // If there is no old FrameNavigationEntry, schedule a different-document
3020 // load.
3021 //
creis225a7432016-06-03 22:56:273022 // TODO(creis): Store the last committed FrameNavigationEntry to use here,
3023 // rather than assuming the NavigationEntry has up to date info on subframes.
creis4e2ecb72015-06-20 00:46:303024 FrameNavigationEntry* old_item =
3025 GetLastCommittedEntry()->GetFrameEntry(frame);
Alex Moshchuk3a4e77a2020-05-29 21:32:573026 if (!old_item)
3027 return HistoryNavigationAction::kDifferentDocument;
3028
Alex Moshchuk3a4e77a2020-05-29 21:32:573029 // If the new item is not in the same SiteInstance, schedule a
3030 // different-document load. Newly restored items may not have a SiteInstance
3031 // yet, in which case it will be assigned on first commit.
3032 if (new_item->site_instance() &&
3033 new_item->site_instance() != old_item->site_instance())
3034 return HistoryNavigationAction::kDifferentDocument;
3035
3036 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:313037 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
3038 // that we do this even if the history navigation would not be modifying this
3039 // frame were it live.
3040 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:573041 return HistoryNavigationAction::kDifferentDocument;
3042
3043 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:313044 // Starting a navigation after a crash early-promotes the speculative
3045 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
3046 // committed yet, so we can not possibly perform a same-document history
3047 // navigation. The frame would need to be reloaded with a cross-document
3048 // navigation.
3049 if (!frame->current_frame_host()->has_committed_any_navigation())
3050 return HistoryNavigationAction::kDifferentDocument;
3051
creis54131692016-08-12 18:32:253052 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:493053 // sequence number but different item sequence number.
3054 if (new_item->document_sequence_number() ==
3055 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573056 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493057 }
avib48cb312016-05-05 21:35:003058
Alex Moshchuk3a4e77a2020-05-29 21:32:573059 // Otherwise, if both item and document sequence numbers differ, this
3060 // should be a different document load.
3061 return HistoryNavigationAction::kDifferentDocument;
3062 }
3063
3064 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373065 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573066 DCHECK_EQ(new_item->document_sequence_number(),
3067 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373068 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573069}
3070
3071void NavigationControllerImpl::FindFramesToNavigate(
3072 FrameTreeNode* frame,
3073 ReloadType reload_type,
Nate Chapin45f620582021-09-30 17:45:433074 bool is_browser_initiated,
Alex Moshchuk3a4e77a2020-05-29 21:32:573075 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3076 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3077 DCHECK(pending_entry_);
3078 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3079
3080 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3081
3082 if (action == HistoryNavigationAction::kSameDocument) {
3083 std::unique_ptr<NavigationRequest> navigation_request =
3084 CreateNavigationRequestFromEntry(
3085 frame, pending_entry_, new_item, reload_type,
3086 true /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433087 false /* is_history_navigation_in_new_child */,
3088 is_browser_initiated);
Alex Moshchuk3a4e77a2020-05-29 21:32:573089 if (navigation_request) {
3090 // Only add the request if was properly created. It's possible for the
3091 // creation to fail in certain cases, e.g. when the URL is invalid.
3092 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303093 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573094 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473095 std::unique_ptr<NavigationRequest> navigation_request =
3096 CreateNavigationRequestFromEntry(
3097 frame, pending_entry_, new_item, reload_type,
3098 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433099 false /* is_history_navigation_in_new_child */,
3100 is_browser_initiated);
Lei Zhang96031532019-10-10 19:05:473101 if (navigation_request) {
3102 // Only add the request if was properly created. It's possible for the
3103 // creation to fail in certain cases, e.g. when the URL is invalid.
3104 different_document_loads->push_back(std::move(navigation_request));
3105 }
3106 // For a different document, the subframes will be destroyed, so there's
3107 // no need to consider them.
3108 return;
Alex Moshchuke65c39272020-06-03 17:55:373109 } else if (action == HistoryNavigationAction::kStopLooking) {
3110 return;
creis4e2ecb72015-06-20 00:46:303111 }
3112
3113 for (size_t i = 0; i < frame->child_count(); i++) {
Nate Chapin45f620582021-09-30 17:45:433114 FindFramesToNavigate(frame->child_at(i), reload_type, is_browser_initiated,
3115 same_document_loads, different_document_loads);
creis4e2ecb72015-06-20 00:46:303116 }
3117}
3118
Harkiran Bolariaba823e42021-05-21 18:30:363119base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry(
clamy21718cc22018-06-13 13:34:243120 const LoadURLParams& params) {
3121 // Find the appropriate FrameTreeNode.
3122 FrameTreeNode* node = nullptr;
3123 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3124 !params.frame_name.empty()) {
3125 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003126 ? frame_tree_.FindByID(params.frame_tree_node_id)
3127 : frame_tree_.FindByName(params.frame_name);
Hayato Ito7a80db42021-07-05 06:18:543128 DCHECK(!node || node->frame_tree() == &frame_tree_);
clamy21718cc22018-06-13 13:34:243129 }
3130
3131 // If no FrameTreeNode was specified, navigate the main frame.
3132 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003133 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243134
Camille Lamy5193caa2018-10-12 11:59:423135 // Compute overrides to the LoadURLParams for |override_user_agent|,
3136 // |should_replace_current_entry| and |has_user_gesture| that will be used
3137 // both in the creation of the NavigationEntry and the NavigationRequest.
3138 // Ideally, the LoadURLParams themselves would be updated, but since they are
3139 // passed as a const reference, this is not possible.
3140 // TODO(clamy): When we only create a NavigationRequest, move this to
3141 // CreateNavigationRequestFromLoadURLParams.
3142 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3143 GetLastCommittedEntry());
3144
3145 // Don't allow an entry replacement if there is no entry to replace.
3146 // http://crbug.com/457149
Hayato Ito7a80db42021-07-05 06:18:543147 //
3148 // If there is an entry, an entry replacement must happen if the current
3149 // browsing context should maintain a trivial session history.
shivanigithubf405bf0d2021-11-05 17:58:333150 bool should_replace_current_entry =
3151 (params.should_replace_current_entry ||
3152 ShouldMaintainTrivialSessionHistory(node)) &&
3153 entries_.size();
Camille Lamy5193caa2018-10-12 11:59:423154
clamy21718cc22018-06-13 13:34:243155 // Javascript URLs should not create NavigationEntries. All other navigations
3156 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243157 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373158 std::unique_ptr<NavigationEntryImpl> entry =
3159 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3160 should_replace_current_entry,
3161 params.has_user_gesture);
clamy21718cc22018-06-13 13:34:243162 DiscardPendingEntry(false);
3163 SetPendingEntry(std::move(entry));
3164 }
3165
3166 // Renderer-debug URLs are sent to the renderer process immediately for
3167 // processing and don't need to create a NavigationRequest.
3168 // Note: this includes navigations to JavaScript URLs, which are considered
3169 // renderer-debug URLs.
3170 // Note: we intentionally leave the pending entry in place for renderer debug
3171 // URLs, unlike the cases below where we clear it if the navigation doesn't
3172 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303173 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483174 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3175 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153176 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483177 params.url, browser_context_)) {
3178 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363179 return nullptr;
Oleg Davydov2cc0167b2019-02-05 14:32:483180 }
3181
clamy21718cc22018-06-13 13:34:243182 HandleRendererDebugURL(node, params.url);
Harkiran Bolariaba823e42021-05-21 18:30:363183 return nullptr;
clamy21718cc22018-06-13 13:34:243184 }
3185
Antonio Sartori78a749f2020-11-30 12:03:393186 DCHECK(pending_entry_);
3187
clamy21718cc22018-06-13 13:34:243188 // Convert navigations to the current URL to a reload.
3189 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3190 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3191 // DevTools sometimes issues navigations to main frames that they do not
3192 // expect to see treated as reload, and it only works because they pass a
3193 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3194 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543195 ReloadType reload_type = params.reload_type;
3196 if (reload_type == ReloadType::NONE &&
3197 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043198 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243199 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243200 params.load_type ==
3201 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
Hayato Ito7a80db42021-07-05 06:18:543202 should_replace_current_entry, GetLastCommittedEntry())) {
clamy21718cc22018-06-13 13:34:243203 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473204 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393205
3206 // If this is a reload of an existing FrameNavigationEntry and we had a
3207 // policy container for it, then we should copy it into the pending entry,
3208 // so that it is copied to the navigation request in
3209 // CreateNavigationRequestFromLoadParams later.
3210 if (GetLastCommittedEntry()) {
3211 FrameNavigationEntry* previous_frame_entry =
3212 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193213 if (previous_frame_entry &&
3214 previous_frame_entry->policy_container_policies()) {
3215 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163216 previous_frame_entry->policy_container_policies()->Clone());
Antonio Sartori78a749f2020-11-30 12:03:393217 }
3218 }
3219 }
3220
3221 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
Rakina Zata Amniafd3c6582021-11-30 06:19:173222 // document, then we should copy the document polices from RenderFrameHost's
3223 // PolicyContainerHost. The NavigationRequest will create a new
3224 // PolicyContainerHost with the document policies from the |pending_entry_|,
3225 // and that PolicyContainerHost will be put in the final RenderFrameHost for
3226 // the navigation. This way, we ensure that we keep enforcing the right
3227 // policies on the initial empty document after the reload.
3228 if (GetLastCommittedEntry()->IsInitialEntry() && params.url.IsAboutBlank()) {
Antonio Sartori78a749f2020-11-30 12:03:393229 if (node->current_frame_host() &&
3230 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193231 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163232 node->current_frame_host()
3233 ->policy_container_host()
3234 ->policies()
3235 .Clone());
Antonio Sartori78a749f2020-11-30 12:03:393236 }
clamy21718cc22018-06-13 13:34:243237 }
3238
3239 // navigation_ui_data should only be present for main frame navigations.
3240 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
3241
Camille Lamy5193caa2018-10-12 11:59:423242 std::unique_ptr<NavigationRequest> request =
3243 CreateNavigationRequestFromLoadParams(
3244 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143245 params.has_user_gesture, network::mojom::SourceLocation::New(),
Antonio Sartori6984c742021-08-26 08:03:413246 reload_type, pending_entry_, pending_entry_->GetFrameEntry(node));
clamy21718cc22018-06-13 13:34:243247
3248 // If the navigation couldn't start, return immediately and discard the
3249 // pending NavigationEntry.
3250 if (!request) {
3251 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363252 return nullptr;
clamy21718cc22018-06-13 13:34:243253 }
3254
Camille Lamy5193caa2018-10-12 11:59:423255#if DCHECK_IS_ON()
3256 // Safety check that NavigationRequest and NavigationEntry match.
3257 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3258#endif
3259
clamy21718cc22018-06-13 13:34:243260 // This call does not support re-entrancy. See http://crbug.com/347742.
3261 CHECK(!in_navigate_to_pending_entry_);
3262 in_navigate_to_pending_entry_ = true;
3263
arthursonzogni66f711c2019-10-08 14:40:363264 // It is not possible to delete the pending NavigationEntry while navigating
3265 // to it. Grab a reference to delay potential deletion until the end of this
3266 // function.
3267 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3268
Harkiran Bolariaba823e42021-05-21 18:30:363269 base::WeakPtr<NavigationHandle> created_navigation_handle(
3270 request->GetWeakPtr());
Lukasz Anforowicz9ee83c272020-12-01 20:14:053271 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243272
3273 in_navigate_to_pending_entry_ = false;
Harkiran Bolariaba823e42021-05-21 18:30:363274 return created_navigation_handle;
clamy21718cc22018-06-13 13:34:243275}
3276
clamyea99ea12018-05-28 13:54:233277void NavigationControllerImpl::HandleRendererDebugURL(
3278 FrameTreeNode* frame_tree_node,
3279 const GURL& url) {
3280 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243281 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3282 // the renderer process is not live, unless it is the initial navigation
3283 // of the tab.
clamyea99ea12018-05-28 13:54:233284 if (!IsInitialNavigation()) {
3285 DiscardNonCommittedEntries();
3286 return;
3287 }
Fergal Dalyecd3b0202020-06-25 01:57:373288 // The current frame is always a main frame. If IsInitialNavigation() is
3289 // true then there have been no navigations and any frames of this tab must
3290 // be in the same renderer process. If that has crashed then the only frame
3291 // that can be revived is the main frame.
3292 frame_tree_node->render_manager()
3293 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233294 }
3295 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3296}
3297
clamy21718cc22018-06-13 13:34:243298std::unique_ptr<NavigationEntryImpl>
3299NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3300 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423301 const LoadURLParams& params,
3302 bool override_user_agent,
3303 bool should_replace_current_entry,
3304 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393305 // Browser initiated navigations might not have a blob_url_loader_factory set
3306 // in params even if the navigation is to a blob URL. If that happens, lookup
3307 // the correct url loader factory to use here.
3308 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483309 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473310 // Resolve the blob URL in the storage partition associated with the target
3311 // frame. This is the storage partition the URL will be loaded in, and only
3312 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393313 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473314 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393315 }
3316
clamy21718cc22018-06-13 13:34:243317 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443318 // extra_headers in params are \n separated; navigation entries want \r\n.
3319 std::string extra_headers_crlf;
3320 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243321
3322 // For subframes, create a pending entry with a corresponding frame entry.
3323 if (!node->IsMainFrame()) {
Rakina Zata Amniafd3c6582021-11-30 06:19:173324 CHECK(GetLastCommittedEntry());
3325 entry = GetLastCommittedEntry()->Clone();
clamy21718cc22018-06-13 13:34:243326 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:083327 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
clamy21718cc22018-06-13 13:34:243328 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Anton Bikineevf62d1bf2021-05-15 17:56:073329 params.url, absl::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203330 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393331 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:483332 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori78a749f2020-11-30 12:03:393333 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163334 // a conversion of a new navigation into a reload, we will set the right
3335 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193336 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243337 } else {
3338 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243339 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063340 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103341 params.source_site_instance.get(), params.transition_type,
3342 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Hayato Ito303654c2021-06-30 09:07:543343 blob_url_loader_factory));
clamy21718cc22018-06-13 13:34:243344 entry->set_source_site_instance(
3345 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3346 entry->SetRedirectChain(params.redirect_chain);
3347 }
3348
3349 // Set the FTN ID (only used in non-site-per-process, for tests).
3350 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243351 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423352 entry->SetIsOverridingUserAgent(override_user_agent);
3353 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543354 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243355
clamy21718cc22018-06-13 13:34:243356 switch (params.load_type) {
3357 case LOAD_TYPE_DEFAULT:
3358 break;
3359 case LOAD_TYPE_HTTP_POST:
3360 entry->SetHasPostData(true);
3361 entry->SetPostData(params.post_data);
3362 break;
3363 case LOAD_TYPE_DATA:
3364 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3365 entry->SetVirtualURL(params.virtual_url_for_data_url);
3366#if defined(OS_ANDROID)
3367 entry->SetDataURLAsString(params.data_url_as_string);
3368#endif
3369 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3370 break;
clamy21718cc22018-06-13 13:34:243371 }
3372
3373 // TODO(clamy): NavigationEntry is meant for information that will be kept
3374 // after the navigation ended and therefore is not appropriate for
3375 // started_from_context_menu. Move started_from_context_menu to
3376 // NavigationUIData.
3377 entry->set_started_from_context_menu(params.started_from_context_menu);
3378
3379 return entry;
3380}
3381
clamyea99ea12018-05-28 13:54:233382std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423383NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3384 FrameTreeNode* node,
3385 const LoadURLParams& params,
3386 bool override_user_agent,
3387 bool should_replace_current_entry,
3388 bool has_user_gesture,
Antonio Sartori2f763d9d2021-04-21 10:04:143389 network::mojom::SourceLocationPtr source_location,
Camille Lamy5193caa2018-10-12 11:59:423390 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573391 NavigationEntryImpl* entry,
Scott Violetcf6ea7e2021-06-09 21:09:213392 FrameNavigationEntry* frame_entry) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573393 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283394 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533395 // All renderer-initiated navigations must have an initiator_origin.
3396 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513397
Camille Lamy5193caa2018-10-12 11:59:423398 GURL url_to_load;
3399 GURL virtual_url;
Anton Bikineevf62d1bf2021-05-15 17:56:073400 absl::optional<url::Origin> origin_to_commit =
3401 frame_entry ? frame_entry->committed_origin() : absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:323402
Camille Lamy2baa8022018-10-19 16:43:173403 // For main frames, rewrite the URL if necessary and compute the virtual URL
3404 // that should be shown in the address bar.
3405 if (node->IsMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423406 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173407 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423408 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423409
Camille Lamy2baa8022018-10-19 16:43:173410 // For DATA loads, override the virtual URL.
3411 if (params.load_type == LOAD_TYPE_DATA)
3412 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423413
Camille Lamy2baa8022018-10-19 16:43:173414 if (virtual_url.is_empty())
3415 virtual_url = url_to_load;
3416
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573417 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283418
Aran Gilman249eb122019-12-02 23:32:463419 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3420 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3421 // return different results, leading to a different URL in the
3422 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3423 // pending NavigationEntry, as we'll only make one call to
3424 // RewriteUrlForNavigation.
3425 VLOG_IF(1, (url_to_load != frame_entry->url()))
3426 << "NavigationRequest and FrameEntry have different URLs: "
3427 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283428
Camille Lamy2baa8022018-10-19 16:43:173429 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423430 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173431 // NavigationRequest.
3432 } else {
3433 url_to_load = params.url;
3434 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243435 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173436 }
Camille Lamy5193caa2018-10-12 11:59:423437
Ehsan Karamad44fc72112019-02-26 18:15:473438 if (node->render_manager()->is_attaching_inner_delegate()) {
3439 // Avoid starting any new navigations since this node is now preparing for
3440 // attaching an inner delegate.
3441 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203442 }
Camille Lamy5193caa2018-10-12 11:59:423443
Camille Lamy5193caa2018-10-12 11:59:423444 if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load))
3445 return nullptr;
3446
Kunihiko Sakamoto346a74e2021-03-10 08:57:483447 if (!DoesURLMatchOriginForNavigation(
3448 url_to_load, origin_to_commit,
3449 frame_entry->subresource_web_bundle_navigation_info())) {
Nasko Oskov03912102019-01-11 00:21:323450 DCHECK(false) << " url:" << url_to_load
3451 << " origin:" << origin_to_commit.value();
3452 return nullptr;
3453 }
3454
Camille Lamy5193caa2018-10-12 11:59:423455 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403456 blink::PreviewsState previews_state =
3457 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
Camille Lamy5193caa2018-10-12 11:59:423458 if (!node->IsMainFrame()) {
3459 // For subframes, use the state of the top-level frame.
3460 previews_state = node->frame_tree()
3461 ->root()
3462 ->current_frame_host()
3463 ->last_navigation_previews_state();
3464 }
3465
Camille Lamy5193caa2018-10-12 11:59:423466 // This will be used to set the Navigation Timing API navigationStart
3467 // parameter for browser navigations in new tabs (intents, tabs opened through
3468 // "Open link in new tab"). If the navigation must wait on the current
3469 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3470 // will be updated when the BeforeUnload ack is received.
3471 base::TimeTicks navigation_start = base::TimeTicks::Now();
3472
danakjd83d706d2020-11-25 22:11:123473 // Look for a pending commit that is to another document in this
3474 // FrameTreeNode. If one exists, then the last committed URL will not be the
3475 // current URL by the time this navigation commits.
3476 bool has_pending_cross_document_commit =
3477 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
Miyoung Shina2dd6a42021-10-07 12:19:213478 bool is_currently_error_page = node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:123479
Minggang Wangb9f3fa92021-07-01 15:30:313480 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjb952ef12021-01-14 19:58:493481 /*old_url=*/node->current_url(),
3482 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3483 has_pending_cross_document_commit, is_currently_error_page,
3484 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423485
3486 // Create the NavigationParams based on |params|.
3487
Hiroki Nakagawa4ed61282021-06-18 05:37:233488 bool is_view_source_mode = entry->IsViewSourceMode();
3489 DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme));
Charlie Harrison8c113a32019-01-07 16:08:293490
Antonio Sartori6984c742021-08-26 08:03:413491 blink::NavigationDownloadPolicy download_policy = params.download_policy;
Hiroki Nakagawa4ed61282021-06-18 05:37:233492 // Update |download_policy| if the virtual URL is view-source.
Charlie Harrison8c113a32019-01-07 16:08:293493 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253494 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293495
Minggang Wangb9f3fa92021-07-01 15:30:313496 blink::mojom::CommonNavigationParamsPtr common_params =
3497 blink::mojom::CommonNavigationParams::New(
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513498 url_to_load, params.initiator_origin,
3499 blink::mojom::Referrer::New(params.referrer.url,
3500 params.referrer.policy),
Scott Violetcf6ea7e2021-06-09 21:09:213501 params.transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513502 should_replace_current_entry, params.base_url_for_data_url,
Rakina Zata Amni8a1c6b32021-09-29 06:41:293503 previews_state, navigation_start,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513504 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
Antonio Sartori2f763d9d2021-04-21 10:04:143505 params.post_data, std::move(source_location),
arthursonzogniaf7c62c52020-02-12 10:49:413506 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273507 false /* has_text_fragment_token */,
3508 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3509 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513510 false /* is_history_navigation_in_new_child_frame */,
Nan Lind91c8152021-10-21 16:22:373511 params.input_start, network::mojom::RequestDestination::kEmpty);
Camille Lamy5193caa2018-10-12 11:59:423512
Minggang Wangb9f3fa92021-07-01 15:30:313513 blink::mojom::CommitNavigationParamsPtr commit_params =
3514 blink::mojom::CommitNavigationParams::New(
Antonio Sartori3e8de6d2021-07-26 10:28:413515 frame_entry->committed_origin(),
3516 // The correct storage key will be computed before committing the
3517 // navigation.
3518 blink::StorageKey(), network::mojom::WebSandboxFlags(),
arthursonzognid5a8d0b2021-03-11 17:36:433519 override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323520 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143521 std::vector<net::RedirectInfo>(),
3522 std::string() /* post_content_type */, common_params->url,
3523 common_params->method, params.can_load_local_resources,
Minggang Wangb9f3fa92021-07-01 15:30:313524 frame_entry->page_state().ToEncodedData(), entry->GetUniqueID(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143525 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3526 -1 /* pending_history_list_offset */,
3527 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3528 params.should_clear_history_list ? 0 : GetEntryCount(),
3529 false /* was_discarded */, is_view_source_mode,
Minggang Wangb9f3fa92021-07-01 15:30:313530 params.should_clear_history_list,
3531 blink::mojom::NavigationTiming::New(),
Minggang Wangf59db47b2021-06-16 01:56:223532 blink::mojom::WasActivatedOption::kUnknown,
Lucas Furukawa Gadania9c45682019-07-31 22:05:143533 base::UnguessableToken::Create() /* navigation_token */,
Minggang Wang7ee0c742021-06-16 16:16:513534 std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143535#if defined(OS_ANDROID)
3536 std::string(), /* data_url_as_string */
3537#endif
arthursonzogni14379782020-05-15 09:09:273538 !params.is_renderer_initiated, /* is_browser_initiated */
Tsuyoshi Horoe86d7702019-11-29 01:52:473539 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533540 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173541 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073542 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163543 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333544 false /* origin_agent_cluster */,
Maks Orlovichc66745a2020-06-30 17:40:023545 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213546 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Nate Chapind1fe3612021-04-16 20:45:573547 false /* is_cross_browsing_instance */, nullptr /* old_page_info */,
3548 -1 /* http_response_code */,
Minggang Wangb9f3fa92021-07-01 15:30:313549 std::vector<blink::mojom::
3550 AppHistoryEntryPtr>() /* app_history_back_entries */,
Nate Chapind1fe3612021-04-16 20:45:573551 std::vector<
Minggang Wangb9f3fa92021-07-01 15:30:313552 blink::mojom::
3553 AppHistoryEntryPtr>() /* app_history_forward_entries */,
Matt Menke4e209082021-11-09 04:59:313554 std::vector<GURL>() /* early_hints_preloaded_resources */,
Clark DuVall8ee487a22021-11-10 02:25:583555 absl::nullopt /* ad_auction_components */,
3556 // This timestamp will be populated when the commit IPC is sent.
3557 base::TimeTicks() /* commit_sent */);
Camille Lamy5193caa2018-10-12 11:59:423558#if defined(OS_ANDROID)
3559 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143560 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423561 }
3562#endif
3563
Lucas Furukawa Gadania9c45682019-07-31 22:05:143564 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423565
3566 // A form submission may happen here if the navigation is a renderer-initiated
3567 // form submission that took the OpenURL path.
3568 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3569
3570 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3571 std::string extra_headers_crlf;
3572 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093573
3574 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143575 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083576 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453577 params.initiator_frame_token.has_value()
3578 ? &(params.initiator_frame_token.value())
3579 : nullptr,
3580 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3581 request_body,
John Delaney50425f82020-04-07 16:26:213582 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473583 params.impression, params.is_pdf);
Yao Xiaodc5ed102019-06-04 19:19:093584 navigation_request->set_from_download_cross_origin_redirect(
3585 params.from_download_cross_origin_redirect);
3586 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423587}
3588
3589std::unique_ptr<NavigationRequest>
3590NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233591 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573592 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233593 FrameNavigationEntry* frame_entry,
3594 ReloadType reload_type,
3595 bool is_same_document_history_load,
Nate Chapin45f620582021-09-30 17:45:433596 bool is_history_navigation_in_new_child_frame,
3597 bool is_browser_initiated) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343598 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233599 GURL dest_url = frame_entry->url();
Rakina Zata Amniafd3c6582021-11-30 06:19:173600 if (entry->IsInitialEntry() && frame_tree_node->IsMainFrame()) {
3601 // The main frame of an initial NavigationEntry has an empty URL. Convert it
3602 // to about:blank instead. This should only happen when the initial
3603 // NavigationEntry is reloaded.
3604 DCHECK(dest_url.is_empty());
3605 dest_url = GURL("about:blank");
3606 }
Anton Bikineevf62d1bf2021-05-15 17:56:073607 absl::optional<url::Origin> origin_to_commit =
Nasko Oskov03912102019-01-11 00:21:323608 frame_entry->committed_origin();
3609
clamyea99ea12018-05-28 13:54:233610 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013611 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573612 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233613 // We may have been redirected when navigating to the current URL.
3614 // Use the URL the user originally intended to visit as signaled by the
3615 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013616 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573617 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233618 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323619 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233620 }
3621
Ehsan Karamad44fc72112019-02-26 18:15:473622 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3623 // Avoid starting any new navigations since this node is now preparing for
3624 // attaching an inner delegate.
3625 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203626 }
3627
Camille Lamy5193caa2018-10-12 11:59:423628 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573629 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233630 return nullptr;
3631 }
3632
Kunihiko Sakamoto346a74e2021-03-10 08:57:483633 if (!DoesURLMatchOriginForNavigation(
3634 dest_url, origin_to_commit,
3635 frame_entry->subresource_web_bundle_navigation_info())) {
3636 DCHECK(false) << " url:" << dest_url
3637 << " origin:" << origin_to_commit.value();
Nasko Oskov03912102019-01-11 00:21:323638 return nullptr;
3639 }
3640
clamyea99ea12018-05-28 13:54:233641 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403642 blink::PreviewsState previews_state =
3643 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
clamyea99ea12018-05-28 13:54:233644 if (!frame_tree_node->IsMainFrame()) {
3645 // For subframes, use the state of the top-level frame.
3646 previews_state = frame_tree_node->frame_tree()
3647 ->root()
3648 ->current_frame_host()
3649 ->last_navigation_previews_state();
clamyea99ea12018-05-28 13:54:233650 }
3651
clamyea99ea12018-05-28 13:54:233652 // This will be used to set the Navigation Timing API navigationStart
3653 // parameter for browser navigations in new tabs (intents, tabs opened through
3654 // "Open link in new tab"). If the navigation must wait on the current
3655 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3656 // will be updated when the BeforeUnload ack is received.
3657 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233658
danakjd83d706d2020-11-25 22:11:123659 // Look for a pending commit that is to another document in this
3660 // FrameTreeNode. If one exists, then the last committed URL will not be the
3661 // current URL by the time this navigation commits.
3662 bool has_pending_cross_document_commit =
3663 frame_tree_node->render_manager()
3664 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493665 bool is_currently_error_page =
Miyoung Shina2dd6a42021-10-07 12:19:213666 frame_tree_node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:123667
Minggang Wangb9f3fa92021-07-01 15:30:313668 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123669 /*old_url=*/frame_tree_node->current_url(),
3670 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493671 has_pending_cross_document_commit, is_currently_error_page,
3672 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423673
3674 // A form submission may happen here if the navigation is a
3675 // back/forward/reload navigation that does a form resubmission.
3676 scoped_refptr<network::ResourceRequestBody> request_body;
3677 std::string post_content_type;
3678 if (frame_entry->method() == "POST") {
3679 request_body = frame_entry->GetPostData(&post_content_type);
3680 // Might have a LF at end.
Peter Kastingb53b81912021-04-28 19:23:303681 post_content_type = std::string(
3682 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL));
Camille Lamy5193caa2018-10-12 11:59:423683 }
3684
3685 // Create the NavigationParams based on |entry| and |frame_entry|.
Minggang Wangb9f3fa92021-07-01 15:30:313686 blink::mojom::CommonNavigationParamsPtr common_params =
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513687 entry->ConstructCommonNavigationParams(
3688 *frame_entry, request_body, dest_url,
3689 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
3690 navigation_type, previews_state, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533691 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513692 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553693 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423694
3695 // TODO(clamy): |intended_as_new_entry| below should always be false once
3696 // Reload no longer leads to this being called for a pending NavigationEntry
3697 // of index -1.
Minggang Wangb9f3fa92021-07-01 15:30:313698 blink::mojom::CommitNavigationParamsPtr commit_params =
Lucas Furukawa Gadania9c45682019-07-31 22:05:143699 entry->ConstructCommitNavigationParams(
3700 *frame_entry, common_params->url, origin_to_commit,
3701 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3702 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533703 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3704 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143705 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423706
clamyea99ea12018-05-28 13:54:233707 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143708 frame_tree_node, std::move(common_params), std::move(commit_params),
Nate Chapin45f620582021-09-30 17:45:433709 is_browser_initiated, false /* was_opener_suppressed */,
Takashi Toyoshimae87b7be2021-01-22 11:51:083710 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453711 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3712 entry->extra_headers(), frame_entry, entry, request_body,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473713 nullptr /* navigation_ui_data */, absl::nullopt /* impression */,
3714 false /* is_pdf */);
clamyea99ea12018-05-28 13:54:233715}
3716
[email protected]d202a7c2012-01-04 07:53:473717void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213718 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273719 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403720
Takashi Toyoshimaea534ef22021-07-21 03:27:593721 // We need to notify the ssl_manager_ before the WebContents so the
[email protected]df1af242009-05-01 00:11:403722 // location bar will have up-to-date information about the security style
3723 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133724 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403725
Rakina Zata Amnia4e27222021-12-22 01:05:003726 delegate_->NotifyNavigationStateChanged(
3727 details->used_to_be_ignored_due_to_null_navigation_entry
3728 ? INVALIDATE_TYPE_ALL_BUT_USED_TO_BE_IGNORED_DUE_TO_NULL_NAVIGATION_ENTRY
3729 : INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:573730 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463731
[email protected]b0f724c2013-09-05 04:21:133732 // TODO(avi): Remove. http://crbug.com/170921
3733 NotificationDetails notification_details =
3734 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153735 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3736 Source<NavigationController>(this),
3737 notification_details);
initial.commit09911bf2008-07-26 23:55:293738}
3739
initial.commit09911bf2008-07-26 23:55:293740// static
[email protected]d202a7c2012-01-04 07:53:473741size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233742 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153743 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:213744 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233745}
3746
[email protected]d202a7c2012-01-04 07:53:473747void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223748 if (is_active && needs_reload_)
3749 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293750}
3751
[email protected]d202a7c2012-01-04 07:53:473752void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293753 if (!needs_reload_)
3754 return;
3755
Bo Liucdfa4b12018-11-06 00:21:443756 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3757 needs_reload_type_);
3758
initial.commit09911bf2008-07-26 23:55:293759 // Calling Reload() results in ignoring state, and not loading.
3760 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3761 // cached state.
avicc872d7242015-08-19 21:26:343762 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163763 NavigateToExistingPendingEntry(ReloadType::NONE,
Nate Chapin45f620582021-09-30 17:45:433764 FrameTreeNode::kFrameTreeNodeInvalidId,
3765 true /* is_browser_initiated */);
avicc872d7242015-08-19 21:26:343766 } else if (last_committed_entry_index_ != -1) {
arthursonzogni5c4c202d2017-04-25 23:41:273767 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343768 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163769 NavigateToExistingPendingEntry(ReloadType::NONE,
Nate Chapin45f620582021-09-30 17:45:433770 FrameTreeNode::kFrameTreeNodeInvalidId,
3771 true /* is_browser_initiated */);
avicc872d7242015-08-19 21:26:343772 } else {
3773 // If there is something to reload, the successful reload will clear the
3774 // |needs_reload_| flag. Otherwise, just do it here.
3775 needs_reload_ = false;
3776 }
initial.commit09911bf2008-07-26 23:55:293777}
3778
Kevin McNeeccca6172021-10-19 17:11:143779base::WeakPtr<NavigationHandle>
3780NavigationControllerImpl::LoadPostCommitErrorPage(
Carlos IL42b416592019-10-07 23:10:363781 RenderFrameHost* render_frame_host,
3782 const GURL& url,
3783 const std::string& error_page_html,
3784 net::Error error) {
Rakina Zata Amni919b7922020-12-11 09:03:133785 RenderFrameHostImpl* rfhi =
3786 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:583787
3788 // Only active documents can load post-commit error pages:
3789 // - If the document is in pending deletion, the browser already committed to
3790 // destroying this RenderFrameHost so ignore loading the error page.
3791 // - If the document is in back-forward cache, it's not allowed to navigate as
3792 // it should remain frozen. Ignore the request and evict the document from
3793 // back-forward cache.
3794 // - If the document is prerendering, it can navigate but when loading error
3795 // pages, cancel prerendering.
Fergal Daly1336ac642021-09-14 15:13:113796 if (rfhi->IsInactiveAndDisallowActivation(
3797 DisallowActivationReasonId::kLoadPostCommitErrorPage)) {
Kevin McNeeccca6172021-10-19 17:11:143798 return nullptr;
Fergal Daly1336ac642021-09-14 15:13:113799 }
Sreeja Kamishettydb8e2892021-03-10 09:30:583800
Rakina Zata Amni919b7922020-12-11 09:03:133801 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:413802
Minggang Wangb9f3fa92021-07-01 15:30:313803 blink::mojom::CommonNavigationParamsPtr common_params =
Minggang Wanga13c796e2021-07-02 05:54:433804 blink::CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:593805 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
3806 // the frame actually committed (e.g. iframe with "src" set to a
3807 // slow-responding URL). We should rewrite the URL to about:blank in this
3808 // case, as the renderer will only think a page is an error page if it has a
3809 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:133810 common_params->url = url.is_empty() ? GURL("about:blank") : url;
Minggang Wangb9f3fa92021-07-01 15:30:313811 blink::mojom::CommitNavigationParamsPtr commit_params =
Minggang Wanga13c796e2021-07-02 05:54:433812 blink::CreateCommitNavigationParams();
Antonio Sartori58591c892021-04-21 06:54:333813 commit_params->original_url = common_params->url;
John Delaney131ad362019-08-08 21:57:413814
arthursonzogni70ac7302020-05-28 08:49:053815 // Error pages have a fully permissive FramePolicy.
3816 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
3817 // error pages.
3818 commit_params->frame_policy = blink::FramePolicy();
3819
John Delaney131ad362019-08-08 21:57:413820 std::unique_ptr<NavigationRequest> navigation_request =
3821 NavigationRequest::CreateBrowserInitiated(
3822 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083823 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:193824 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453825 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:063826 "" /* extra_headers */, nullptr /* frame_entry */,
3827 nullptr /* entry */, nullptr /* post_body */,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473828 nullptr /* navigation_ui_data */, absl::nullopt /* impression */,
3829 false /* is_pdf */);
Carlos IL42b416592019-10-07 23:10:363830 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:413831 navigation_request->set_net_error(error);
3832 node->CreatedNavigationRequest(std::move(navigation_request));
3833 DCHECK(node->navigation_request());
Kevin McNeeccca6172021-10-19 17:11:143834
3835 // Calling BeginNavigation may destroy the NavigationRequest.
3836 base::WeakPtr<NavigationRequest> created_navigation_request(
3837 node->navigation_request()->GetWeakPtr());
John Delaney131ad362019-08-08 21:57:413838 node->navigation_request()->BeginNavigation();
Kevin McNeeccca6172021-10-19 17:11:143839 return created_navigation_request;
John Delaney131ad362019-08-08 21:57:413840}
3841
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573842void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213843 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093844 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:153845 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143846 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293847}
3848
[email protected]d202a7c2012-01-04 07:53:473849void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363850 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553851 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363852 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293853
initial.commit09911bf2008-07-26 23:55:293854 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293855}
[email protected]765b35502008-08-21 00:51:203856
arthursonzogni69a6a1b2019-09-17 09:23:003857void NavigationControllerImpl::DiscardNonCommittedEntries() {
Rakina Zata Amnia4e27222021-12-22 01:05:003858 DiscardNonCommittedEntriesWithCommitDetails(nullptr /* commit_details */);
3859}
3860
3861void NavigationControllerImpl::DiscardNonCommittedEntriesWithCommitDetails(
3862 LoadCommittedDetails* commit_details) {
Michael Thiessen9b14d512019-09-23 21:19:473863 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:103864 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
3865 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:293866 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:473867 return;
Michael Thiessenc5676d22019-09-25 22:32:103868 }
avi45a72532015-04-07 21:01:453869 DiscardPendingEntry(false);
arthursonzogni69a6a1b2019-09-17 09:23:003870 if (delegate_)
Rakina Zata Amnia4e27222021-12-22 01:05:003871 delegate_->NotifyNavigationStateChanged(
3872 (commit_details &&
3873 commit_details->used_to_be_ignored_due_to_null_navigation_entry)
3874 ? INVALIDATE_TYPE_ALL_BUT_USED_TO_BE_IGNORED_DUE_TO_NULL_NAVIGATION_ENTRY
3875 : INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:483876}
3877
avi7c6f35e2015-05-08 17:52:383878int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3879 int nav_entry_id) const {
3880 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3881 if (entries_[i]->GetUniqueID() == nav_entry_id)
3882 return i;
3883 }
3884 return -1;
3885}
3886
[email protected]d202a7c2012-01-04 07:53:473887void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573888 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253889 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573890 DCHECK_LE(max_index, source->GetEntryCount());
Nate Chapin214a86a2021-06-21 20:35:573891 std::unique_ptr<NavigationEntryRestoreContextImpl> context =
3892 std::make_unique<NavigationEntryRestoreContextImpl>();
[email protected]e1cd5452010-08-26 18:03:253893 for (int i = 0; i < max_index; i++) {
Nate Chapin9f169072021-06-09 19:32:373894 // Normally, cloning a NavigationEntryImpl results in sharing
3895 // FrameNavigationEntries between the original and the clone. However, when
3896 // cloning from a different NavigationControllerImpl, we want to fork the
3897 // FrameNavigationEntries.
Nate Chapin9f169072021-06-09 19:32:373898 entries_.insert(entries_.begin() + i,
Nate Chapin214a86a2021-06-21 20:35:573899 source->entries_[i]->CloneWithoutSharing(context.get()));
[email protected]e1cd5452010-08-26 18:03:253900 }
Rakina Zata Amniafd3c6582021-11-30 06:19:173901 DCHECK_GE(entries_.size(), 1u);
arthursonzogni5c4c202d2017-04-25 23:41:273902 DCHECK(pending_entry_index_ == -1 ||
3903 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:253904}
[email protected]c5b88d82012-10-06 17:03:333905
3906void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:183907 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:333908 get_timestamp_callback_ = get_timestamp_callback;
3909}
[email protected]8ff00d72012-10-23 19:12:213910
Shivani Sharmaffb32b82019-04-09 16:58:473911// History manipulation intervention:
3912void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:473913 bool replace_entry,
3914 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:403915 bool is_renderer_initiated,
3916 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:453917 // Note that for a subframe, previous_document_was_activated is true if the
3918 // gesture happened in any subframe (propagated to main frame) or in the main
3919 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:473920 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:273921 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:473922 return;
3923 }
3924 if (last_committed_entry_index_ == -1)
3925 return;
3926
Shivani Sharmac4cc8922019-04-18 03:11:173927 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473928
Alexander Timine3ec4192020-04-20 16:39:403929 // Log UKM with the URL we are navigating away from.
3930 ukm::builders::HistoryManipulationIntervention(
3931 previous_page_load_ukm_source_id)
3932 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:473933}
3934
Shivani Sharmac4cc8922019-04-18 03:11:173935void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
3936 int reference_index,
3937 bool skippable) {
3938 auto* reference_entry = GetEntryAtIndex(reference_index);
3939 reference_entry->set_should_skip_on_back_forward_ui(skippable);
3940
3941 int64_t document_sequence_number =
3942 reference_entry->root_node()->frame_entry->document_sequence_number();
3943 for (int index = 0; index < GetEntryCount(); index++) {
3944 auto* entry = GetEntryAtIndex(index);
3945 if (entry->root_node()->frame_entry->document_sequence_number() ==
3946 document_sequence_number) {
3947 entry->set_should_skip_on_back_forward_ui(skippable);
3948 }
3949 }
3950}
3951
arthursonzogni66f711c2019-10-08 14:40:363952std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
3953NavigationControllerImpl::ReferencePendingEntry() {
3954 DCHECK(pending_entry_);
3955 auto pending_entry_ref =
3956 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
3957 pending_entry_refs_.insert(pending_entry_ref.get());
3958 return pending_entry_ref;
3959}
3960
3961void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
3962 // Ignore refs that don't correspond to the current pending entry.
3963 auto it = pending_entry_refs_.find(ref);
3964 if (it == pending_entry_refs_.end())
3965 return;
3966 pending_entry_refs_.erase(it);
3967
3968 if (!pending_entry_refs_.empty())
3969 return;
3970
3971 // The pending entry may be deleted before the last PendingEntryRef.
3972 if (!pending_entry_)
3973 return;
3974
3975 // We usually clear the pending entry when the matching NavigationRequest
3976 // fails, so that an arbitrary URL isn't left visible above a committed page.
3977 //
3978 // However, we do preserve the pending entry in some cases, such as on the
3979 // initial navigation of an unmodified blank tab. We also allow the delegate
3980 // to say when it's safe to leave aborted URLs in the omnibox, to let the
3981 // user edit the URL and try again. This may be useful in cases that the
3982 // committed page cannot be attacker-controlled. In these cases, we still
3983 // allow the view to clear the pending entry and typed URL if the user
3984 // requests (e.g., hitting Escape with focus in the address bar).
3985 //
3986 // Do not leave the pending entry visible if it has an invalid URL, since this
3987 // might be formatted in an unexpected or unsafe way.
3988 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:363989 bool should_preserve_entry =
3990 (pending_entry_ == GetVisibleEntry()) &&
3991 pending_entry_->GetURL().is_valid() &&
3992 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
3993 if (should_preserve_entry)
3994 return;
3995
3996 DiscardPendingEntry(true);
3997 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3998}
3999
Titouan Rigoudy6ec70402021-02-02 15:42:194000std::unique_ptr<PolicyContainerPolicies>
4001NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:394002 RenderFrameHostImpl* rfh,
4003 bool is_same_document,
Rakina Zata Amniafd3c6582021-11-30 06:19:174004 const GURL& url) {
Antonio Sartori78a749f2020-11-30 12:03:394005 if (is_same_document) {
Rakina Zata Amniafd3c6582021-11-30 06:19:174006 CHECK(GetLastCommittedEntry());
Charlie Reis73e356242021-04-02 17:10:314007
Antonio Sartori78a749f2020-11-30 12:03:394008 FrameNavigationEntry* previous_frame_entry =
4009 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
4010
4011 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
4012 // ensure we always have a FrameNavigationEntry here.
4013 if (!previous_frame_entry)
4014 return nullptr;
4015
Titouan Rigoudy6ec70402021-02-02 15:42:194016 const PolicyContainerPolicies* previous_policies =
4017 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:394018
Titouan Rigoudy6ec70402021-02-02 15:42:194019 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:394020 return nullptr;
4021
4022 // Make a copy of the policy container for the new FrameNavigationEntry.
Antonio Sartori5d09b30f2021-03-02 09:27:164023 return previous_policies->Clone();
Antonio Sartori78a749f2020-11-30 12:03:394024 }
4025
Antonio Sartori4f5373792021-05-31 10:56:474026 return rfh->policy_container_host()->policies().Clone();
Antonio Sartori78a749f2020-11-30 12:03:394027}
4028
Hayato Ito2c8c08d02021-06-23 03:38:434029void NavigationControllerImpl::BroadcastHistoryOffsetAndLength() {
Carlos Caballeroede6f8c2021-01-28 11:01:504030 OPTIONAL_TRACE_EVENT2(
Hayato Ito2c8c08d02021-06-23 03:38:434031 "content", "NavigationControllerImpl::BroadcastHistoryOffsetAndLength",
4032 "history_offset", GetLastCommittedEntryIndex(), "history_length",
4033 GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:504034
4035 auto callback = base::BindRepeating(
4036 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
4037 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
4038 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
4039 }
4040 },
Hayato Ito2c8c08d02021-06-23 03:38:434041 GetLastCommittedEntryIndex(), GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:504042 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
4043}
4044
4045void NavigationControllerImpl::DidAccessInitialMainDocument() {
4046 // We may have left a failed browser-initiated navigation in the address bar
4047 // to let the user edit it and try again. Clear it now that content might
4048 // show up underneath it.
4049 if (!frame_tree_.IsLoading() && GetPendingEntry())
4050 DiscardPendingEntry(false);
4051
4052 // Update the URL display.
4053 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
4054}
4055
4056void NavigationControllerImpl::UpdateStateForFrame(
4057 RenderFrameHostImpl* rfhi,
4058 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:564059 OPTIONAL_TRACE_EVENT1("content",
4060 "NavigationControllerImpl::UpdateStateForFrame",
4061 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:504062 // The state update affects the last NavigationEntry associated with the given
4063 // |render_frame_host|. This may not be the last committed NavigationEntry (as
4064 // in the case of an UpdateState from a frame being swapped out). We track
4065 // which entry this is in the RenderFrameHost's nav_entry_id.
4066 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
4067 if (!entry)
4068 return;
4069
4070 FrameNavigationEntry* frame_entry =
4071 entry->GetFrameEntry(rfhi->frame_tree_node());
4072 if (!frame_entry)
4073 return;
4074
4075 // The SiteInstance might not match if we do a cross-process navigation with
4076 // replacement (e.g., auto-subframe), in which case the swap out of the old
4077 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4078 // has already been replaced and destroyed.
4079 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4080 return;
4081
4082 if (page_state == frame_entry->page_state())
4083 return; // Nothing to update.
4084
4085 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4086
4087 // The document_sequence_number and item_sequence_number recorded in the
4088 // FrameNavigationEntry should not differ from the one coming with the update,
4089 // since it must come from the same document. Do not update it if a difference
4090 // is detected, as this indicates that |frame_entry| is not the correct one.
4091 blink::ExplodedPageState exploded_state;
4092 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4093 return;
4094
4095 if (exploded_state.top.document_sequence_number !=
4096 frame_entry->document_sequence_number() ||
4097 exploded_state.top.item_sequence_number !=
4098 frame_entry->item_sequence_number()) {
4099 return;
4100 }
4101
4102 frame_entry->SetPageState(page_state);
4103 NotifyEntryChanged(entry);
4104}
4105
Aaron Colwellb731a0ae2021-03-19 19:14:474106void NavigationControllerImpl::OnStoragePartitionIdAdded(
4107 const StoragePartitionId& partition_id) {
4108 auto it = partition_config_to_id_map_.insert(
4109 std::make_pair(partition_id.config(), partition_id));
4110 bool successful_insert = it.second;
Aaron Colwellb731a0ae2021-03-19 19:14:474111 if (!successful_insert) {
4112 LogStoragePartitionIdCrashKeys(it.first->second, partition_id);
4113 }
Sharon Yangd4f48792021-06-30 17:02:534114 CHECK(successful_insert);
Aaron Colwellb731a0ae2021-03-19 19:14:474115}
4116
4117void NavigationControllerImpl::LogStoragePartitionIdCrashKeys(
4118 const StoragePartitionId& original_partition_id,
4119 const StoragePartitionId& new_partition_id) {
4120 base::debug::SetCrashKeyString(
4121 base::debug::AllocateCrashKeyString("original_partition_id",
4122 base::debug::CrashKeySize::Size256),
4123 original_partition_id.ToString());
4124
4125 base::debug::SetCrashKeyString(
4126 base::debug::AllocateCrashKeyString("new_partition_id",
4127 base::debug::CrashKeySize::Size256),
4128 new_partition_id.ToString());
Aaron Colwellb731a0ae2021-03-19 19:14:474129}
4130
Minggang Wangb9f3fa92021-07-01 15:30:314131std::vector<blink::mojom::AppHistoryEntryPtr>
Nate Chapind1fe3612021-04-16 20:45:574132NavigationControllerImpl::PopulateSingleAppHistoryEntryVector(
4133 Direction direction,
4134 int entry_index,
4135 const url::Origin& pending_origin,
4136 FrameTreeNode* node,
4137 SiteInstance* site_instance,
4138 int64_t previous_item_sequence_number) {
Minggang Wangb9f3fa92021-07-01 15:30:314139 std::vector<blink::mojom::AppHistoryEntryPtr> entries;
Rakina Zata Amniafd3c6582021-11-30 06:19:174140 if (GetLastCommittedEntry()->IsInitialEntry()) {
4141 // Don't process the initial entry.
4142 DCHECK_EQ(GetEntryCount(), 1);
4143 return entries;
4144 }
Nate Chapind1fe3612021-04-16 20:45:574145 int offset = direction == Direction::kForward ? 1 : -1;
4146 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4147 i += offset) {
4148 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
4149 if (!frame_entry || !frame_entry->committed_origin())
4150 break;
4151 if (site_instance != frame_entry->site_instance())
4152 break;
4153 if (!pending_origin.IsSameOriginWith(*frame_entry->committed_origin()))
4154 break;
4155 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4156 continue;
4157 blink::ExplodedPageState exploded_page_state;
4158 if (blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4159 &exploded_page_state)) {
4160 blink::ExplodedFrameState frame_state = exploded_page_state.top;
Minggang Wangb9f3fa92021-07-01 15:30:314161 blink::mojom::AppHistoryEntryPtr entry =
4162 blink::mojom::AppHistoryEntry::New(
4163 frame_state.app_history_key.value_or(std::u16string()),
4164 frame_state.app_history_id.value_or(std::u16string()),
Nate Chapinab5c3a712021-11-18 22:17:094165 frame_state.url_string.value_or(std::u16string()),
4166 frame_state.item_sequence_number,
4167 frame_state.document_sequence_number,
4168 frame_state.app_history_state.value_or(std::u16string()));
Rakina Zata Amniafd3c6582021-11-30 06:19:174169
Nate Chapind1fe3612021-04-16 20:45:574170 DCHECK(pending_origin.CanBeDerivedFrom(GURL(entry->url)));
4171 entries.push_back(std::move(entry));
4172 previous_item_sequence_number = frame_entry->item_sequence_number();
4173 }
4174 }
4175 // If |entries| was constructed by iterating backwards from
4176 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4177 // earliest-at-the-front. Reverse it.
4178 if (direction == Direction::kBack)
4179 std::reverse(entries.begin(), entries.end());
4180 return entries;
4181}
4182
4183void NavigationControllerImpl::PopulateAppHistoryEntryVectors(
4184 NavigationRequest* request) {
4185 url::Origin pending_origin =
4186 request->commit_params().origin_to_commit
4187 ? *request->commit_params().origin_to_commit
4188 : url::Origin::Create(request->common_params().url);
4189
4190 FrameTreeNode* node = request->frame_tree_node();
4191 scoped_refptr<SiteInstance> site_instance =
4192 request->GetRenderFrameHost()->GetSiteInstance();
4193
4194 // NOTE: |entry_index| is an estimate of the index where this entry will
4195 // commit, but it may be wrong in corner cases (e.g., if we are at the max
4196 // entry limit, the earliest entry will be dropped). This is ok because this
4197 // algorithm only uses |entry_index| to walk the entry list as it stands right
4198 // now, and it isn't saved for anything post-commit.
4199 int entry_index = GetPendingEntryIndex();
4200 bool will_create_new_entry = false;
4201 if (NavigationTypeUtils::IsReload(request->common_params().navigation_type) ||
4202 request->common_params().should_replace_current_entry) {
4203 entry_index = GetLastCommittedEntryIndex();
4204 } else if (entry_index == -1) {
4205 will_create_new_entry = true;
4206 entry_index = GetLastCommittedEntryIndex() + 1;
4207 }
4208
4209 int64_t pending_item_sequence_number = 0;
4210 if (auto* pending_entry = GetPendingEntry()) {
4211 if (auto* frame_entry = pending_entry->GetFrameEntry(node))
4212 pending_item_sequence_number = frame_entry->item_sequence_number();
4213 }
4214
4215 request->set_app_history_back_entries(PopulateSingleAppHistoryEntryVector(
4216 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
4217 pending_item_sequence_number));
4218
4219 // Don't populate forward entries if they will be truncated by a new entry.
4220 if (!will_create_new_entry) {
4221 request->set_app_history_forward_entries(
4222 PopulateSingleAppHistoryEntryVector(
4223 Direction::kForward, entry_index, pending_origin, node,
4224 site_instance.get(), pending_item_sequence_number));
4225 }
4226}
4227
Nate Chapinfbfe5af2021-06-10 17:22:084228NavigationControllerImpl::HistoryNavigationAction
4229NavigationControllerImpl::ShouldNavigateToEntryForAppHistoryKey(
4230 FrameNavigationEntry* current_entry,
4231 FrameNavigationEntry* target_entry,
4232 const std::string& app_history_key) {
4233 if (!target_entry || !target_entry->committed_origin())
4234 return HistoryNavigationAction::kStopLooking;
4235 if (current_entry->site_instance() != target_entry->site_instance())
4236 return HistoryNavigationAction::kStopLooking;
4237 if (!current_entry->committed_origin()->IsSameOriginWith(
4238 *target_entry->committed_origin())) {
4239 return HistoryNavigationAction::kStopLooking;
4240 }
4241
4242 // NOTE: We don't actually care between kSameDocument and
4243 // kDifferentDocument, so always use kDifferentDocument by convention.
4244 if (target_entry->app_history_key() == app_history_key)
4245 return HistoryNavigationAction::kDifferentDocument;
4246 return HistoryNavigationAction::kKeepLooking;
4247}
4248
4249void NavigationControllerImpl::NavigateToAppHistoryKey(FrameTreeNode* node,
4250 const std::string& key) {
4251 FrameNavigationEntry* current_entry =
4252 GetLastCommittedEntry()->GetFrameEntry(node);
4253 if (!current_entry)
4254 return;
4255
4256 // We want to find the nearest matching entry that is contiguously
4257 // same-instance and same-origin. Check back first, then forward.
4258 // TODO(japhet): Link spec here once it exists.
4259 for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) {
4260 auto result = ShouldNavigateToEntryForAppHistoryKey(
4261 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4262 if (result == HistoryNavigationAction::kStopLooking)
4263 break;
4264 if (result != HistoryNavigationAction::kKeepLooking) {
Nate Chapin2892b37c2021-10-08 17:33:114265 GoToIndex(i, FrameTreeNode::kFrameTreeNodeInvalidId,
4266 false /* is_browser_initiated*/);
Nate Chapinfbfe5af2021-06-10 17:22:084267 return;
4268 }
4269 }
4270 for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) {
4271 auto result = ShouldNavigateToEntryForAppHistoryKey(
4272 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4273 if (result == HistoryNavigationAction::kStopLooking)
4274 break;
4275 if (result != HistoryNavigationAction::kKeepLooking) {
Nate Chapin2892b37c2021-10-08 17:33:114276 GoToIndex(i, FrameTreeNode::kFrameTreeNodeInvalidId,
4277 false /* is_browser_initiated*/);
Nate Chapinfbfe5af2021-06-10 17:22:084278 return;
4279 }
4280 }
4281}
4282
shivanigithubf405bf0d2021-11-05 17:58:334283bool NavigationControllerImpl::ShouldMaintainTrivialSessionHistory(
4284 const FrameTreeNode* frame_tree_node) const {
4285 // TODO(https://crbug.com/1197384): We may have to add portals in addition to
4286 // prerender and fenced frames. This should be kept in sync with
Hayato Ito7a80db42021-07-05 06:18:544287 // LocalFrame version, LocalFrame::ShouldMaintainTrivialSessionHistory.
shivanigithubf405bf0d2021-11-05 17:58:334288 return frame_tree_.is_prerendering() ||
4289 frame_tree_node->IsInFencedFrameTree();
Hayato Ito7a80db42021-07-05 06:18:544290}
4291
[email protected]8ff00d72012-10-23 19:12:214292} // namespace content