blob: 185b535628565c9530d9aba6f9475873e7541e75 [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"
[email protected]b6583592012-01-25 19:52:3369#include "content/browser/site_instance_impl.h"
Kunihiko Sakamoto346a74e2021-03-10 08:57:4870#include "content/browser/web_package/subresource_web_bundle_navigation_info.h"
Tsuyoshi Horo37493af2019-11-21 23:43:1471#include "content/browser/web_package/web_bundle_navigation_info.h"
Camille Lamy5193caa2018-10-12 11:59:4272#include "content/common/content_constants_internal.h"
Nate Chapind1fe3612021-04-16 20:45:5773#include "content/common/navigation_params_utils.h"
Nasko Oskovae49e292020-08-13 02:08:5174#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1175#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1976#include "content/public/browser/content_browser_client.h"
[email protected]d9083482012-01-06 00:38:4677#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0078#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3879#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1680#include "content/public/browser/notification_types.h"
Lei Zhang96031532019-10-10 19:05:4781#include "content/public/browser/render_view_host.h"
[email protected]9677a3c2012-12-22 04:18:5882#include "content/public/browser/render_widget_host.h"
83#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1084#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3485#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:1986#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:3887#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:1088#include "content/public/common/content_features.h"
Lei Zhang96031532019-10-10 19:05:4789#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4390#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:5991#include "media/base/mime_util.h"
[email protected]a23de8572009-06-03 02:16:3292#include "net/base/escape.h"
Arthur Sonzogni620cec62018-12-13 13:08:5793#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:1894#include "services/metrics/public/cpp/ukm_builders.h"
95#include "services/metrics/public/cpp/ukm_recorder.h"
Yue Ru Sun128804932020-09-30 22:19:1796#include "services/metrics/public/cpp/ukm_source_id.h"
[email protected]9677a3c2012-12-22 04:18:5897#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3998#include "third_party/blink/public/common/blob/blob_utils.h"
Gyuyoung Kim107c2a02021-04-13 01:49:3099#include "third_party/blink/public/common/chrome_debug_urls.h"
Miyoung Shin1c565c912021-03-17 12:11:21100#include "third_party/blink/public/common/history/session_history_constants.h"
Blink Reformata30d4232018-04-07 15:31:06101#include "third_party/blink/public/common/mime_util/mime_util.h"
Minggang Wanga13c796e2021-07-02 05:54:43102#include "third_party/blink/public/common/navigation/navigation_params.h"
Carlos Caballeroede6f8c2021-01-28 11:01:50103#include "third_party/blink/public/common/page_state/page_state_serialization.h"
Minggang Wangb9f3fa92021-07-01 15:30:31104#include "third_party/blink/public/mojom/navigation/navigation_params.mojom.h"
Minggang Wang7ee0c742021-06-16 16:16:51105#include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h"
[email protected]cca6f392014-05-28 21:32:26106#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:29107
[email protected]8ff00d72012-10-23 19:12:21108namespace content {
[email protected]e9ba4472008-09-14 15:42:43109namespace {
110
111// Invoked when entries have been pruned, or removed. For example, if the
112// current entries are [google, digg, yahoo], with the current entry google,
113// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47114void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50115 int index,
[email protected]c12bf1a12008-09-17 16:28:49116 int count) {
[email protected]8ff00d72012-10-23 19:12:21117 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50118 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49119 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14120 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43121}
122
[email protected]e9ba4472008-09-14 15:42:43123// Configure all the NavigationEntries in entries for restore. This resets
124// the transition type to reload and makes sure the content state isn't empty.
125void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57126 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48127 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47128 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43129 // Use a transition type of reload so that we don't incorrectly increase
130 // the typed count.
Lei Zhang96031532019-10-10 19:05:47131 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
132 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43133 }
134}
135
[email protected]bf70edce2012-06-20 22:32:22136// Determines whether or not we should be carrying over a user agent override
137// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57138bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22139 return last_entry && last_entry->GetIsOverridingUserAgent();
140}
141
Camille Lamy5193caa2018-10-12 11:59:42142// Determines whether to override user agent for a navigation.
143bool ShouldOverrideUserAgent(
144 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57145 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42146 switch (override_user_agent) {
147 case NavigationController::UA_OVERRIDE_INHERIT:
148 return ShouldKeepOverride(last_committed_entry);
149 case NavigationController::UA_OVERRIDE_TRUE:
150 return true;
151 case NavigationController::UA_OVERRIDE_FALSE:
152 return false;
Camille Lamy5193caa2018-10-12 11:59:42153 }
154 NOTREACHED();
155 return false;
156}
157
Rakina Zata Amni312822d72021-06-04 16:13:37158// Returns true if this navigation should be treated as a reload. For e.g.
clamy0a656e42018-02-06 18:18:28159// navigating to the last committed url via the address bar or clicking on a
Rakina Zata Amni312822d72021-06-04 16:13:37160// link which results in a navigation to the last committed URL (but wasn't
161// converted to do a replacement navigation in the renderer), etc.
Fergal Daly766177d2020-07-07 07:54:04162// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
163// |base_url_for_data_url|, |transition_type| correspond to the new navigation
164// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
165// navigation that committed.
166bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
167 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57168 const GURL& virtual_url,
169 const GURL& base_url_for_data_url,
170 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57171 bool is_post,
Rakina Zata Amni312822d72021-06-04 16:13:37172 bool should_replace_current_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57173 NavigationEntryImpl* last_committed_entry) {
Rakina Zata Amni312822d72021-06-04 16:13:37174 // Navigations intended to do a replacement shouldn't be converted to do a
175 // reload.
176 if (should_replace_current_entry)
clamy0a656e42018-02-06 18:18:28177 return false;
clamy0a656e42018-02-06 18:18:28178 // Only convert to reload if at least one navigation committed.
179 if (!last_committed_entry)
ananta3bdd8ae2016-12-22 17:11:55180 return false;
181
arthursonzogni7a8243682017-12-14 16:41:42182 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28183 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42184 return false;
185
ananta3bdd8ae2016-12-22 17:11:55186 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
187 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
188 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28189 bool transition_type_can_be_converted = false;
190 if (ui::PageTransitionCoreTypeIs(transition_type,
191 ui::PAGE_TRANSITION_RELOAD) &&
192 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
193 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55194 }
clamy0a656e42018-02-06 18:18:28195 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55196 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28197 transition_type_can_be_converted = true;
198 }
199 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
200 transition_type_can_be_converted = true;
201 if (!transition_type_can_be_converted)
202 return false;
203
204 // This check is required for cases like view-source:, etc. Here the URL of
205 // the navigation entry would contain the url of the page, while the virtual
206 // URL contains the full URL including the view-source prefix.
207 if (virtual_url != last_committed_entry->GetVirtualURL())
208 return false;
209
Fergal Daly766177d2020-07-07 07:54:04210 // Check that the URLs match.
211 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
212 // If there's no frame entry then by definition the URLs don't match.
213 if (!frame_entry)
214 return false;
215
216 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28217 return false;
218
219 // This check is required for Android WebView loadDataWithBaseURL. Apps
220 // can pass in anything in the base URL and we need to ensure that these
221 // match before classifying it as a reload.
222 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
223 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
224 return false;
ananta3bdd8ae2016-12-22 17:11:55225 }
226
clamy0a656e42018-02-06 18:18:28227 // Skip entries with SSL errors.
228 if (last_committed_entry->ssl_error())
229 return false;
230
231 // Don't convert to a reload when the last navigation was a POST or the new
232 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04233 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28234 return false;
235
236 return true;
ananta3bdd8ae2016-12-22 17:11:55237}
238
Nasko Oskov03912102019-01-11 00:21:32239bool DoesURLMatchOriginForNavigation(
240 const GURL& url,
Anton Bikineevf62d1bf2021-05-15 17:56:07241 const absl::optional<url::Origin>& origin,
Kunihiko Sakamoto346a74e2021-03-10 08:57:48242 SubresourceWebBundleNavigationInfo*
243 subresource_web_bundle_navigation_info) {
Nasko Oskov03912102019-01-11 00:21:32244 // If there is no origin supplied there is nothing to match. This can happen
245 // for navigations to a pending entry and therefore it should be allowed.
246 if (!origin)
247 return true;
248
Kunihiko Sakamoto346a74e2021-03-10 08:57:48249 if (url.SchemeIs(url::kUrnScheme) && subresource_web_bundle_navigation_info) {
250 // Urn: subframe from WebBundle has an opaque origin derived from the
251 // Bundle's origin.
252 return origin->CanBeDerivedFrom(
253 subresource_web_bundle_navigation_info->bundle_url());
254 }
255
Nasko Oskov03912102019-01-11 00:21:32256 return origin->CanBeDerivedFrom(url);
257}
258
Anton Bikineevf62d1bf2021-05-15 17:56:07259absl::optional<url::Origin> GetCommittedOriginForFrameEntry(
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12260 const mojom::DidCommitProvisionalLoadParams& params,
261 NavigationRequest* request) {
Nasko Oskov03912102019-01-11 00:21:32262 // Error pages commit in an opaque origin, yet have the real URL that resulted
263 // in an error as the |params.url|. Since successful reload of an error page
264 // should commit in the correct origin, setting the opaque origin on the
265 // FrameNavigationEntry will be incorrect.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12266 if (request->DidEncounterError())
Anton Bikineevf62d1bf2021-05-15 17:56:07267 return absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:32268
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12269 // We also currently don't save committed origins for loadDataWithBaseURL
270 // navigations (probably accidentally). Without this check, navigations to
271 // the FrameNavigationEntry might fail the DoesURLMatchOriginForNavigation()
272 // check since the origin will be based on the base URL instead of the data:
273 // URL used for the navigation.
274 // TODO(https://crbug.com/1198406): Save committed origin in
275 // FrameNavigationEntry for this case too.
Rakina Zata Amni5d2ef8aa2021-06-25 01:34:23276 if (request->IsLoadDataWithBaseURLAndHasUnreachableURL())
Anton Bikineevf62d1bf2021-05-15 17:56:07277 return absl::nullopt;
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12278
Anton Bikineevf62d1bf2021-05-15 17:56:07279 return absl::make_optional(params.origin);
Nasko Oskov03912102019-01-11 00:21:32280}
281
Camille Lamy5193caa2018-10-12 11:59:42282bool IsValidURLForNavigation(bool is_main_frame,
283 const GURL& virtual_url,
284 const GURL& dest_url) {
285 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
286 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
287 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
288 << virtual_url.possibly_invalid_spec();
289 return false;
290 }
291
292 // Don't attempt to navigate to non-empty invalid URLs.
293 if (!dest_url.is_valid() && !dest_url.is_empty()) {
294 LOG(WARNING) << "Refusing to load invalid URL: "
295 << dest_url.possibly_invalid_spec();
296 return false;
297 }
298
299 // The renderer will reject IPC messages with URLs longer than
300 // this limit, so don't attempt to navigate with a longer URL.
301 if (dest_url.spec().size() > url::kMaxURLChars) {
302 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
303 << " characters.";
304 return false;
305 }
306
Aaron Colwell33109c592020-04-21 21:31:19307 // Reject renderer debug URLs because they should have been handled before
308 // we get to this point. This check handles renderer debug URLs
309 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
310 // provides defense-in-depth if a renderer debug URL manages to get here via
311 // some other path. We want to reject the navigation here so it doesn't
312 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30313 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19314 LOG(WARNING) << "Refusing to load renderer debug URL: "
315 << dest_url.possibly_invalid_spec();
316 return false;
317 }
318
Camille Lamy5193caa2018-10-12 11:59:42319 return true;
320}
321
Mikel Astizba9cf2fd2017-12-17 10:38:10322// See replaced_navigation_entry_data.h for details: this information is meant
323// to ensure |*output_entry| keeps track of its original URL (landing page in
324// case of server redirects) as it gets replaced (e.g. history.replaceState()),
325// without overwriting it later, for main frames.
326void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57327 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10328 NavigationEntryImpl* output_entry) {
329 if (output_entry->GetReplacedEntryData().has_value())
330 return;
331
332 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57333 data.first_committed_url = replaced_entry->GetURL();
334 data.first_timestamp = replaced_entry->GetTimestamp();
335 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29336 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10337}
338
Minggang Wangb9f3fa92021-07-01 15:30:31339blink::mojom::NavigationType GetNavigationType(
340 const GURL& old_url,
341 const GURL& new_url,
342 ReloadType reload_type,
343 NavigationEntryImpl* entry,
344 const FrameNavigationEntry& frame_entry,
345 bool has_pending_cross_document_commit,
346 bool is_currently_error_page,
347 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23348 // Reload navigations
349 switch (reload_type) {
350 case ReloadType::NORMAL:
Minggang Wangb9f3fa92021-07-01 15:30:31351 return blink::mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23352 case ReloadType::BYPASSING_CACHE:
Minggang Wangb9f3fa92021-07-01 15:30:31353 return blink::mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23354 case ReloadType::ORIGINAL_REQUEST_URL:
Minggang Wangb9f3fa92021-07-01 15:30:31355 return blink::mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23356 case ReloadType::NONE:
357 break; // Fall through to rest of function.
358 }
359
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08360 if (entry->IsRestored()) {
Minggang Wangb9f3fa92021-07-01 15:30:31361 return entry->GetHasPostData()
362 ? blink::mojom::NavigationType::RESTORE_WITH_POST
363 : blink::mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23364 }
365
danakjb952ef12021-01-14 19:58:49366 const bool can_be_same_document =
367 // A pending cross-document commit means this navigation will not occur in
368 // the current document, as that document would end up being replaced in
369 // the meantime.
370 !has_pending_cross_document_commit &&
371 // If the current document is an error page, we should always treat it as
372 // a different-document navigation so that we'll attempt to load the
373 // document we're navigating to (and not stay in the current error page).
374 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12375
clamyea99ea12018-05-28 13:54:23376 // History navigations.
377 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12378 return can_be_same_document && is_same_document_history_load
Minggang Wangb9f3fa92021-07-01 15:30:31379 ? blink::mojom::NavigationType::HISTORY_SAME_DOCUMENT
380 : blink::mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23381 }
382 DCHECK(!is_same_document_history_load);
383
384 // A same-document fragment-navigation happens when the only part of the url
385 // that is modified is after the '#' character.
386 //
387 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12388 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23389 //
390 // Note: this check is only valid for navigations that are not history
391 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
392 // history navigation from 'A#foo' to 'A#bar' is not a same-document
393 // navigation, but a different-document one. This is why history navigation
394 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47395 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
396 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12397
398 // The one case where we do the wrong thing here and incorrectly choose
399 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
400 // the renderer is a frameset. All frameset navigations should be
401 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
402 // navigation would do the right thing, as it would send it to the browser and
403 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
404 // into this method). But since we can't tell that case here for browser-
405 // initiated navigations, we have to get the renderer involved. In that case
406 // the navigation would be restarted due to the renderer spending a reply of
407 // mojom::CommitResult::RestartCrossDocument.
408
409 return can_be_same_document && is_same_doc
Minggang Wangb9f3fa92021-07-01 15:30:31410 ? blink::mojom::NavigationType::SAME_DOCUMENT
411 : blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23412}
413
Camille Lamy5193caa2018-10-12 11:59:42414// Adjusts the original input URL if needed, to get the URL to actually load and
415// the virtual URL, which may differ.
416void RewriteUrlForNavigation(const GURL& original_url,
417 BrowserContext* browser_context,
418 GURL* url_to_load,
419 GURL* virtual_url,
420 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42421 // Allow the browser URL handler to rewrite the URL. This will, for example,
422 // remove "view-source:" from the beginning of the URL to get the URL that
423 // will actually be loaded. This real URL won't be shown to the user, just
424 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31425 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42426 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
427 url_to_load, browser_context, reverse_on_redirect);
428}
429
430#if DCHECK_IS_ON()
431// Helper sanity check function used in debug mode.
432void ValidateRequestMatchesEntry(NavigationRequest* request,
433 NavigationEntryImpl* entry) {
434 if (request->frame_tree_node()->IsMainFrame()) {
435 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
436 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
Minggang Wangb9f3fa92021-07-01 15:30:31437 ui::PageTransitionFromInt(request->common_params().transition),
438 entry->GetTransitionType()));
Camille Lamy5193caa2018-10-12 11:59:42439 }
Nasko Oskovc36327d2019-01-03 23:23:04440 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42441 entry->should_clear_history_list());
442 DCHECK_EQ(request->common_params().has_user_gesture,
443 entry->has_user_gesture());
444 DCHECK_EQ(request->common_params().base_url_for_data_url,
445 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04446 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42447 entry->GetCanLoadLocalResources());
448 DCHECK_EQ(request->common_params().started_from_context_menu,
449 entry->has_started_from_context_menu());
450
451 FrameNavigationEntry* frame_entry =
452 entry->GetFrameEntry(request->frame_tree_node());
453 if (!frame_entry) {
454 NOTREACHED();
455 return;
456 }
457
Camille Lamy5193caa2018-10-12 11:59:42458 DCHECK_EQ(request->common_params().method, frame_entry->method());
459
Nasko Oskovc36327d2019-01-03 23:23:04460 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42461 if (redirect_size == frame_entry->redirect_chain().size()) {
462 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04463 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42464 frame_entry->redirect_chain()[i]);
465 }
466 } else {
467 NOTREACHED();
468 }
469}
470#endif // DCHECK_IS_ON()
471
Dave Tapuska8bfd84c2019-03-26 20:47:16472// Returns whether the session history NavigationRequests in |navigations|
473// would stay within the subtree of the sandboxed iframe in
474// |sandbox_frame_tree_node_id|.
475bool DoesSandboxNavigationStayWithinSubtree(
476 int sandbox_frame_tree_node_id,
477 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
478 for (auto& item : navigations) {
479 bool within_subtree = false;
480 // Check whether this NavigationRequest affects a frame within the
481 // sandboxed frame's subtree by walking up the tree looking for the
482 // sandboxed frame.
483 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03484 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16485 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
486 within_subtree = true;
487 break;
488 }
489 }
490 if (!within_subtree)
491 return false;
492 }
493 return true;
494}
495
Titouan Rigoudy6ec70402021-02-02 15:42:19496bool ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(
Antonio Sartori78a749f2020-11-30 12:03:39497 const NavigationRequest* request) {
498 // For local schemes we need to store the policy container in the
499 // FrameNavigationEntry, so that we can reload it in case of history
500 // navigation.
501 //
502 // TODO(https://crbug.com/1146361 and https://crbug.com/1146362): blob: and
503 // filesystem: should be removed from this list when we have properly
504 // implemented storing their policy container in the respective store.
505 return (request->common_params().url.SchemeIs(url::kAboutScheme) ||
506 request->common_params().url.SchemeIs(url::kDataScheme) ||
507 request->common_params().url.SchemeIsBlob() ||
508 request->common_params().url.SchemeIsFileSystem());
509}
510
[email protected]e9ba4472008-09-14 15:42:43511} // namespace
512
arthursonzogni66f711c2019-10-08 14:40:36513// NavigationControllerImpl::PendingEntryRef------------------------------------
514
515NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
516 base::WeakPtr<NavigationControllerImpl> controller)
517 : controller_(controller) {}
518
519NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
520 if (!controller_) // Can be null with interstitials.
521 return;
522
523 controller_->PendingEntryRefDeleted(this);
524}
525
[email protected]d202a7c2012-01-04 07:53:47526// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29527
[email protected]23a918b2014-07-15 09:51:36528const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23529
[email protected]765b35502008-08-21 00:51:20530// static
[email protected]d202a7c2012-01-04 07:53:47531size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23532 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20533
[email protected]e6fec472013-05-14 05:29:02534// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20535// when testing.
536static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29537
[email protected]71fde352011-12-29 03:29:56538// static
dcheng9bfa5162016-04-09 01:00:57539std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
540 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10541 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07542 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10543 ui::PageTransition transition,
544 bool is_renderer_initiated,
545 const std::string& extra_headers,
546 BrowserContext* browser_context,
547 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
548 return NavigationControllerImpl::CreateNavigationEntry(
549 url, referrer, std::move(initiator_origin),
550 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Hayato Ito303654c2021-06-30 09:07:54551 extra_headers, browser_context, std::move(blob_url_loader_factory));
Lukasz Anforowicz641234d52019-11-07 21:07:10552}
553
554// static
555std::unique_ptr<NavigationEntryImpl>
556NavigationControllerImpl::CreateNavigationEntry(
557 const GURL& url,
558 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07559 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10560 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57561 ui::PageTransition transition,
562 bool is_renderer_initiated,
563 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09564 BrowserContext* browser_context,
Hayato Ito303654c2021-06-30 09:07:54565 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
Camille Lamy5193caa2018-10-12 11:59:42566 GURL url_to_load;
567 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56568 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42569 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
570 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56571
Lukasz Anforowicz641234d52019-11-07 21:07:10572 // Let the NTP override the navigation params and pretend that this is a
573 // browser-initiated, bookmark-like navigation.
574 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violetcf6ea7e2021-06-09 21:09:21575 source_site_instance, &transition, &is_renderer_initiated, &referrer,
576 &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10577
Patrick Monettef507e982019-06-19 20:18:06578 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28579 nullptr, // The site instance for tabs is sent on navigation
580 // (WebContents::GetSiteInstance).
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58581 url_to_load, referrer, initiator_origin, std::u16string(), transition,
Camille Lamy5193caa2018-10-12 11:59:42582 is_renderer_initiated, blob_url_loader_factory);
583 entry->SetVirtualURL(virtual_url);
584 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56585 entry->set_update_virtual_url_with_url(reverse_on_redirect);
586 entry->set_extra_headers(extra_headers);
Patrick Monettef507e982019-06-19 20:18:06587 return entry;
[email protected]71fde352011-12-29 03:29:56588}
589
[email protected]cdcb1dee2012-01-04 00:46:20590// static
591void NavigationController::DisablePromptOnRepost() {
592 g_check_for_repost = false;
593}
594
[email protected]c5b88d82012-10-06 17:03:33595base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
596 base::Time t) {
597 // If |t| is between the water marks, we're in a run of duplicates
598 // or just getting out of it, so increase the high-water mark to get
599 // a time that probably hasn't been used before and return it.
600 if (low_water_mark_ <= t && t <= high_water_mark_) {
601 high_water_mark_ += base::TimeDelta::FromMicroseconds(1);
602 return high_water_mark_;
603 }
604
605 // Otherwise, we're clear of the last duplicate run, so reset the
606 // water marks.
607 low_water_mark_ = high_water_mark_ = t;
608 return t;
609}
610
ckitagawa0faa5e42020-06-17 17:30:54611NavigationControllerImpl::ScopedShowRepostDialogForTesting::
612 ScopedShowRepostDialogForTesting()
613 : was_disallowed_(g_check_for_repost) {
614 g_check_for_repost = true;
615}
616
617NavigationControllerImpl::ScopedShowRepostDialogForTesting::
618 ~ScopedShowRepostDialogForTesting() {
619 g_check_for_repost = was_disallowed_;
620}
621
[email protected]d202a7c2012-01-04 07:53:47622NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00623 BrowserContext* browser_context,
624 FrameTree& frame_tree,
625 NavigationControllerDelegate* delegate)
626 : frame_tree_(frame_tree),
627 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57628 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22629 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47630 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37631 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29632}
633
[email protected]d202a7c2012-01-04 07:53:47634NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00635 // The NavigationControllerImpl might be called inside its delegate
636 // destructor. Calling it is not valid anymore.
637 delegate_ = nullptr;
638 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29639}
640
Matt Falkenhagen548ed1562021-07-06 01:38:26641WebContents* NavigationControllerImpl::DeprecatedGetWebContents() {
642 return delegate_->DeprecatedGetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32643}
644
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57645BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55646 return browser_context_;
647}
648
[email protected]d202a7c2012-01-04 07:53:47649void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30650 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36651 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57652 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57653 // Verify that this controller is unused and that the input is valid.
Lei Zhang96031532019-10-10 19:05:47654 DCHECK_EQ(0, GetEntryCount());
655 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57656 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14657 selected_navigation < static_cast<int>(entries->size()));
arthursonzogni5c4c202d2017-04-25 23:41:27658 DCHECK_EQ(-1, pending_entry_index_);
[email protected]ce3fa3c2009-04-20 19:55:57659
[email protected]ce3fa3c2009-04-20 19:55:57660 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44661 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03662 entries_.reserve(entries->size());
663 for (auto& entry : *entries)
dcheng36b6aec92015-12-26 06:16:36664 entries_.push_back(
665 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
avif16f85a72015-11-13 18:25:03666
667 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
668 // cleared out safely.
669 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57670
671 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36672 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57673}
674
toyoshim6142d96f2016-12-19 09:07:25675void NavigationControllerImpl::Reload(ReloadType reload_type,
676 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28677 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28678 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32679 int current_index = -1;
680
Carlos IL42b416592019-10-07 23:10:36681 if (entry_replaced_by_post_commit_error_) {
682 // If there is an entry that was replaced by a currently active post-commit
683 // error navigation, this can't be the initial navigation.
684 DCHECK(!IsInitialNavigation());
685 // If the current entry is a post commit error, we reload the entry it
686 // replaced instead. We leave the error entry in place until a commit
687 // replaces it, but the pending entry points to the original entry in the
688 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
689 // case that pending_entry_ != entries_[pending_entry_index_].
690 entry = entry_replaced_by_post_commit_error_.get();
691 current_index = GetCurrentEntryIndex();
692 } else if (IsInitialNavigation() && pending_entry_) {
693 // If we are reloading the initial navigation, just use the current
694 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32695 entry = pending_entry_;
696 // The pending entry might be in entries_ (e.g., after a Clone), so we
697 // should also update the current_index.
698 current_index = pending_entry_index_;
699 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00700 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32701 current_index = GetCurrentEntryIndex();
702 if (current_index != -1) {
creis3da03872015-02-20 21:12:32703 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32704 }
[email protected]979a4bc2013-04-24 01:27:15705 }
[email protected]241db352013-04-22 18:04:05706
[email protected]59167c22013-06-03 18:07:32707 // If we are no where, then we can't reload. TODO(darin): We should add a
708 // CanReload method.
709 if (!entry)
710 return;
711
Takashi Toyoshimac7df3c22019-06-25 14:18:47712 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26713 entry->set_reload_type(reload_type);
714
Aran Gilman37d11632019-10-08 23:07:15715 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30716 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07717 // they really want to do this. If they do, the dialog will call us back
718 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57719 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02720
[email protected]106a0812010-03-18 00:15:12721 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57722 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47723 return;
initial.commit09911bf2008-07-26 23:55:29724 }
Lei Zhang96031532019-10-10 19:05:47725
726 if (!IsInitialNavigation())
727 DiscardNonCommittedEntries();
728
729 pending_entry_ = entry;
730 pending_entry_index_ = current_index;
731 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
732
733 NavigateToExistingPendingEntry(reload_type,
734 FrameTreeNode::kFrameTreeNodeInvalidId);
initial.commit09911bf2008-07-26 23:55:29735}
736
[email protected]d202a7c2012-01-04 07:53:47737void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48738 DCHECK(pending_reload_ != ReloadType::NONE);
739 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12740}
741
[email protected]d202a7c2012-01-04 07:53:47742void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48743 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12744 NOTREACHED();
745 } else {
toyoshim6142d96f2016-12-19 09:07:25746 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48747 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12748 }
749}
750
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57751bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09752 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11753}
754
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57755bool NavigationControllerImpl::IsInitialBlankNavigation() {
creis10a4ab72015-10-13 17:22:40756 // TODO(creis): Once we create a NavigationEntry for the initial blank page,
toyoshim0df1d3a2016-09-09 09:52:48757 // we'll need to check for entry count 1 and restore_type NONE (to exclude
758 // the cloned tab case).
creis10a4ab72015-10-13 17:22:40759 return IsInitialNavigation() && GetEntryCount() == 0;
760}
761
Aran Gilman37d11632019-10-08 23:07:15762NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
763 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58764 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03765 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58766}
767
W. James MacLean1c40862c2020-04-27 21:05:57768void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
769 const url::Origin& origin) {
770 for (int i = 0; i < GetEntryCount(); i++) {
771 auto* entry = GetEntryAtIndex(i);
772 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
773 }
774 if (entry_replaced_by_post_commit_error_) {
775 // It's possible we could come back to this entry if the error
776 // page/interstitial goes away.
777 entry_replaced_by_post_commit_error_
778 ->RegisterExistingOriginToPreventOptInIsolation(origin);
779 }
780 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
781 // visiting pending_entry_, which lacks a committed origin. This will be done
782 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
783}
784
avi25764702015-06-23 15:43:37785void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57786 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00787 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37788 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27789 DCHECK_EQ(-1, pending_entry_index_);
[email protected]8ff00d72012-10-23 19:12:21790 NotificationService::current()->Notify(
Aran Gilman37d11632019-10-08 23:07:15791 NOTIFICATION_NAV_ENTRY_PENDING, Source<NavigationController>(this),
avi25764702015-06-23 15:43:37792 Details<NavigationEntry>(pending_entry_));
[email protected]765b35502008-08-21 00:51:20793}
794
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57795NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47796 if (pending_entry_)
797 return pending_entry_;
798 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20799}
800
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57801NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32802 // The pending entry is safe to return for new (non-history), browser-
803 // initiated navigations. Most renderer-initiated navigations should not
804 // show the pending entry, to prevent URL spoof attacks.
805 //
806 // We make an exception for renderer-initiated navigations in new tabs, as
807 // long as no other page has tried to access the initial empty document in
808 // the new tab. If another page modifies this blank page, a URL spoof is
809 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32810 bool safe_to_show_pending =
811 pending_entry_ &&
812 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09813 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32814 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46815 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32816
817 // Also allow showing the pending entry for history navigations in a new tab,
818 // such as Ctrl+Back. In this case, no existing page is visible and no one
819 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15820 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
821 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32822 safe_to_show_pending = true;
823
824 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19825 return pending_entry_;
826 return GetLastCommittedEntry();
827}
828
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57829int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20830 if (pending_entry_index_ != -1)
831 return pending_entry_index_;
832 return last_committed_entry_index_;
833}
834
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57835NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20836 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28837 return nullptr;
avif16f85a72015-11-13 18:25:03838 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20839}
840
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57841bool NavigationControllerImpl::CanViewSource() {
Carlos Caballeroede6f8c2021-01-28 11:01:50842 const std::string& mime_type = frame_tree_.root()
Alex Moshchuk2e470ea2021-02-03 06:46:34843 ->current_frame_host()
844 ->render_view_host()
Carlos Caballeroede6f8c2021-01-28 11:01:50845 ->contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53846 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
847 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27848 NavigationEntry* visible_entry = GetVisibleEntry();
849 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39850 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16851}
852
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57853int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27854 // The last committed entry index must always be less than the number of
Carlos IL4dea8902020-05-26 15:14:29855 // entries. If there are no entries, it must be -1.
arthursonzogni5c4c202d2017-04-25 23:41:27856 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
857 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55858 return last_committed_entry_index_;
859}
860
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57861int NavigationControllerImpl::GetEntryCount() {
Carlos IL4dea8902020-05-26 15:14:29862 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55863 return static_cast<int>(entries_.size());
864}
865
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57866NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37867 if (index < 0 || index >= GetEntryCount())
868 return nullptr;
869
avif16f85a72015-11-13 18:25:03870 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25871}
872
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57873NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47874 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20875}
876
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57877int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46878 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03879}
880
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57881bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03882 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
883 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
884 return true;
885 }
886 return false;
[email protected]765b35502008-08-21 00:51:20887}
888
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57889bool NavigationControllerImpl::CanGoForward() {
WangHui74286d52021-03-31 16:17:15890 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
891 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
892 return true;
893 }
894 return false;
[email protected]765b35502008-08-21 00:51:20895}
896
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57897bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03898 int index = GetIndexForOffset(offset);
899 return index >= 0 && index < GetEntryCount();
900}
901
WangHui74286d52021-03-31 16:17:15902#if defined(OS_ANDROID)
903bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
WangHui74286d52021-03-31 16:17:15904 if (offset == 0)
905 return true;
906 int increment = offset > 0 ? 1 : -1;
907 int non_skippable_entries = 0;
908 for (int index = GetIndexForOffset(increment);
909 index >= 0 && index < GetEntryCount(); index += increment) {
910 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
911 non_skippable_entries++;
912
913 if (non_skippable_entries == std::abs(offset))
914 return true;
915 }
916 return false;
917}
918#endif
919
[email protected]d202a7c2012-01-04 07:53:47920void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06921 int target_index = GetIndexForOffset(-1);
922
Elly Fong-Jones40ee8112021-06-23 19:10:52923 // Move the target index past the skippable entries.
Shivani Sharma298d12852019-01-22 20:04:03924 bool all_skippable_entries = true;
Elly Fong-Jones40ee8112021-06-23 19:10:52925 while (target_index >= 0) {
926 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui()) {
Shivani Sharma298d12852019-01-22 20:04:03927 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06928 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16929 }
Elly Fong-Jones40ee8112021-06-23 19:10:52930 target_index--;
shivanisha55201872018-12-13 04:29:06931 }
Miyoung Shin1c565c912021-03-17 12:11:21932
Shivani Sharma298d12852019-01-22 20:04:03933 // Do nothing if all entries are skippable. Normally this path would not
934 // happen as consumers would have already checked it in CanGoBack but a lot of
935 // tests do not do that.
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42936 if (all_skippable_entries)
Shivani Sharma298d12852019-01-22 20:04:03937 return;
shivanisha55201872018-12-13 04:29:06938
shivanisha55201872018-12-13 04:29:06939 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20940}
941
[email protected]d202a7c2012-01-04 07:53:47942void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06943 int target_index = GetIndexForOffset(1);
944
Shivani Sharma2d5b4b6b2019-01-08 16:07:16945 // Note that at least one entry (the last one) will be non-skippable since
946 // entries are marked skippable only when they add another entry because of
947 // redirect or pushState.
Elly Fong-Jones40ee8112021-06-23 19:10:52948 while (target_index < static_cast<int>(entries_.size())) {
949 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui())
shivanisha55201872018-12-13 04:29:06950 break;
Elly Fong-Jones40ee8112021-06-23 19:10:52951 target_index++;
shivanisha55201872018-12-13 04:29:06952 }
shivanisha55201872018-12-13 04:29:06953 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20954}
955
[email protected]d202a7c2012-01-04 07:53:47956void NavigationControllerImpl::GoToIndex(int index) {
Dave Tapuska8bfd84c2019-03-26 20:47:16957 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId);
958}
959
960void NavigationControllerImpl::GoToIndex(int index,
961 int sandbox_frame_tree_node_id) {
sunjian30574a62017-03-21 21:39:44962 TRACE_EVENT0("browser,navigation,benchmark",
963 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:20964 if (index < 0 || index >= static_cast<int>(entries_.size())) {
965 NOTREACHED();
966 return;
967 }
968
[email protected]cbab76d2008-10-13 22:42:47969 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20970
arthursonzogni5c4c202d2017-04-25 23:41:27971 DCHECK_EQ(nullptr, pending_entry_);
972 DCHECK_EQ(-1, pending_entry_index_);
973 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:20974 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:27975 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
976 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Dave Tapuska8bfd84c2019-03-26 20:47:16977 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id);
[email protected]765b35502008-08-21 00:51:20978}
979
[email protected]d202a7c2012-01-04 07:53:47980void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:12981 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:03982 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:20983 return;
984
[email protected]9ba14052012-06-22 23:50:03985 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20986}
987
WangHui74286d52021-03-31 16:17:15988#if defined(OS_ANDROID)
989void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
990 // Note: This is actually reached in unit tests.
991 if (!CanGoToOffsetWithSkipping(offset))
992 return;
993
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42994 if (offset == 0) {
WangHui74286d52021-03-31 16:17:15995 GoToIndex(GetIndexForOffset(offset));
996 return;
997 }
998 int increment = offset > 0 ? 1 : -1;
999 // Find the offset without counting skippable entries.
1000 int target_index = GetIndexForOffset(increment);
1001 int non_skippable_entries = 0;
1002 for (int index = target_index; index >= 0 && index < GetEntryCount();
1003 index += increment) {
1004 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1005 non_skippable_entries++;
1006
1007 if (non_skippable_entries == std::abs(offset)) {
1008 target_index = index;
1009 break;
1010 }
1011 }
1012
1013 GoToIndex(target_index);
1014}
1015#endif
1016
[email protected]41374f12013-07-24 02:49:281017bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151018 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281019 return false;
[email protected]6a13a6c2011-12-20 21:47:121020
[email protected]43032342011-03-21 14:10:311021 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281022 return true;
[email protected]cbab76d2008-10-13 22:42:471023}
1024
Michael Thiessen9b14d512019-09-23 21:19:471025void NavigationControllerImpl::PruneForwardEntries() {
1026 DiscardNonCommittedEntries();
1027 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471028 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471029 if (num_removed <= 0)
1030 return;
1031 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1032 NotifyPrunedEntries(this, remove_start_index /* start index */,
1033 num_removed /* count */);
1034}
1035
Aran Gilman37d11632019-10-08 23:07:151036void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1037 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321038 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311039 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511040 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1041 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321042 }
1043}
1044
Harkiran Bolariaba823e42021-05-21 18:30:361045base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL(
1046 const GURL& url,
1047 const Referrer& referrer,
1048 ui::PageTransition transition,
1049 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471050 LoadURLParams params(url);
1051 params.referrer = referrer;
1052 params.transition_type = transition;
1053 params.extra_headers = extra_headers;
Harkiran Bolariaba823e42021-05-21 18:30:361054 return LoadURLWithParams(params);
[email protected]cf002332012-08-14 19:17:471055}
1056
Harkiran Bolariaba823e42021-05-21 18:30:361057base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams(
1058 const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061059 if (params.is_renderer_initiated)
1060 DCHECK(params.initiator_origin.has_value());
1061
naskob8744d22014-08-28 17:07:431062 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151063 "NavigationControllerImpl::LoadURLWithParams", "url",
1064 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291065 bool is_explicit_navigation =
1066 GetContentClient()->browser()->IsExplicitNavigation(
1067 params.transition_type);
1068 if (HandleDebugURL(params.url, params.transition_type,
1069 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431070 // If Telemetry is running, allow the URL load to proceed as if it's
1071 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491072 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431073 cc::switches::kEnableGpuBenchmarking))
Harkiran Bolariaba823e42021-05-21 18:30:361074 return nullptr;
[email protected]47752982014-07-29 08:01:431075 }
[email protected]8bf1048012012-02-08 01:22:181076
[email protected]cf002332012-08-14 19:17:471077 // Checks based on params.load_type.
1078 switch (params.load_type) {
1079 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431080 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471081 break;
1082 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261083 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471084 NOTREACHED() << "Data load must use data scheme.";
Harkiran Bolariaba823e42021-05-21 18:30:361085 return nullptr;
[email protected]cf002332012-08-14 19:17:471086 }
1087 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461088 }
[email protected]e47ae9472011-10-13 19:48:341089
[email protected]e47ae9472011-10-13 19:48:341090 // The user initiated a load, we don't need to reload anymore.
1091 needs_reload_ = false;
1092
Harkiran Bolariaba823e42021-05-21 18:30:361093 return NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441094}
1095
Mohamed Abdelhalim833de902019-09-16 17:41:451096bool NavigationControllerImpl::PendingEntryMatchesRequest(
1097 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191098 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451099 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191100}
1101
[email protected]d202a7c2012-01-04 07:53:471102bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321103 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071104 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331105 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441106 bool is_same_document_navigation,
Nate Chapinc7019dd7d2021-06-25 18:29:251107 bool was_on_initial_empty_document,
Shivani Sharmaffb32b82019-04-09 16:58:471108 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131109 NavigationRequest* navigation_request) {
1110 DCHECK(navigation_request);
Hayato Ito7a80db42021-07-05 06:18:541111 if (ShouldMaintainTrivialSessionHistory() && GetLastCommittedEntry())
1112 DCHECK(params.should_replace_current_entry);
[email protected]cd2e15742013-03-08 04:08:311113 is_initial_navigation_ = false;
1114
[email protected]0e8db942008-09-24 21:21:481115 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431116 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481117 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361118 if (entry_replaced_by_post_commit_error_) {
1119 if (is_same_document_navigation) {
1120 // Same document navigations should not be possible on error pages and
1121 // would leave the controller in a weird state. Kill the renderer if
1122 // that happens.
1123 bad_message::ReceivedBadMessage(
1124 rfh->GetProcess(), bad_message::NC_SAME_DOCUMENT_POST_COMMIT_ERROR);
1125 }
1126 // Any commit while a post-commit error page is showing should put the
1127 // original entry back, replacing the error page's entry. This includes
1128 // reloads, where the original entry was used as the pending entry and
1129 // should now be at the correct index at commit time.
1130 entries_[last_committed_entry_index_] =
1131 std::move(entry_replaced_by_post_commit_error_);
1132 }
Fergal Daly8e33cf62020-12-12 01:06:071133 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551134 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431135 if (pending_entry_ &&
1136 pending_entry_->GetIsOverridingUserAgent() !=
1137 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1138 overriding_user_agent_changed = true;
[email protected]0e8db942008-09-24 21:21:481139 } else {
Gang Wu325f03f42021-02-25 20:00:461140 // GetLastCommittedEntry() is null, so this is the first entry.
Fergal Daly8e33cf62020-12-12 01:06:071141 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481142 details->previous_entry_index = -1;
Gang Wu325f03f42021-02-25 20:00:461143 if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) {
1144 // Default setting is NOT override the user agent, so overriding the user
1145 // agent in first entry should be considered as user agent changed as
1146 // well.
1147 overriding_user_agent_changed = true;
1148 }
[email protected]0e8db942008-09-24 21:21:481149 }
[email protected]ecd9d8702008-08-28 22:10:171150
Alexander Timind2f2e4f22019-04-02 20:04:531151 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1152 // implementing back-forward cache.
1153
1154 // Create a new metrics object or reuse the previous one depending on whether
1155 // it's a main frame navigation or not.
1156 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics =
1157 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1158 GetLastCommittedEntry(), !rfh->GetParent(),
1159 params.document_sequence_number);
1160 // Notify the last active entry that we have navigated away.
1161 if (!rfh->GetParent() && !is_same_document_navigation) {
1162 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1163 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Hajime Hoshic7606502021-04-14 02:42:161164 metrics->MainFrameDidNavigateAwayFromDocument(rfh, navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531165 }
1166 }
1167 }
1168
fdegans9caf66a2015-07-30 21:10:421169 // If there is a pending entry at this point, it should have a SiteInstance,
1170 // except for restored entries.
jam48cea9082017-02-15 06:13:291171 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481172 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081173 pending_entry_->IsRestored());
1174 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151175 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291176 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481177 }
[email protected]e9ba4472008-09-14 15:42:431178
Nasko Oskovaee2f862018-06-15 00:05:521179 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1180 // has changed, this must be treated as a new navigation with replacement.
1181 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491182 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521183 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101184 pending_entry_->GetUniqueID() ==
1185 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521186 pending_entry_->site_instance() &&
1187 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1188 DCHECK_NE(-1, pending_entry_index_);
1189 // TODO(nasko,creis): Instead of setting this value here, set
1190 // should_replace_current_entry on the parameters we send to the
1191 // renderer process as part of CommitNavigation. The renderer should
1192 // in turn send it back here as part of |params| and it can be just
1193 // enforced and renderer process terminated on mismatch.
1194 details->did_replace_entry = true;
1195 } else {
1196 // The renderer tells us whether the navigation replaces the current entry.
1197 details->did_replace_entry = params.should_replace_current_entry;
1198 }
[email protected]bcd904482012-02-01 01:54:221199
[email protected]e9ba4472008-09-14 15:42:431200 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amnif6950d552020-11-24 03:26:101201 details->type = ClassifyNavigation(rfh, params, navigation_request);
[email protected]4bf3522c2010-08-19 21:00:201202
eugenebutee08663a2017-04-27 17:43:121203 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441204 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121205
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071206 details->is_prerender_activation =
1207 navigation_request->IsPrerenderedPageActivation();
1208
Peter Boströmd7592132019-01-30 04:50:311209 // Make sure we do not discard the pending entry for a different ongoing
1210 // navigation when a same document commit comes in unexpectedly from the
1211 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1212 // other navigation types. See https://crbug.com/900036.
1213 // TODO(crbug.com/926009): Handle history.pushState() as well.
1214 bool keep_pending_entry = is_same_document_navigation &&
Charlie Reisc0f17d2d2021-01-12 18:52:491215 details->type == NAVIGATION_TYPE_EXISTING_ENTRY &&
Peter Boströmd7592132019-01-30 04:50:311216 pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451217 !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311218
[email protected]0e8db942008-09-24 21:21:481219 switch (details->type) {
Charlie Reisc0f17d2d2021-01-12 18:52:491220 case NAVIGATION_TYPE_NEW_ENTRY:
1221 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051222 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451223 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431224 break;
Charlie Reisc0f17d2d2021-01-12 18:52:491225 case NAVIGATION_TYPE_EXISTING_ENTRY:
1226 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1227 was_restored, navigation_request,
1228 keep_pending_entry);
[email protected]e9ba4472008-09-14 15:42:431229 break;
[email protected]8ff00d72012-10-23 19:12:211230 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471231 RendererDidNavigateNewSubframe(
1232 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451233 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431234 break;
[email protected]8ff00d72012-10-23 19:12:211235 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391236 if (!RendererDidNavigateAutoSubframe(
Nate Chapinc7019dd7d2021-06-25 18:29:251237 rfh, params, details->is_same_document,
1238 was_on_initial_empty_document, navigation_request)) {
creisce0ef3572017-01-26 17:53:081239 // We don't send a notification about auto-subframe PageState during
1240 // UpdateStateForFrame, since it looks like nothing has changed. Send
1241 // it here at commit time instead.
1242 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431243 return false;
creis59d5a47cb2016-08-24 23:57:191244 }
[email protected]e9ba4472008-09-14 15:42:431245 break;
[email protected]8ff00d72012-10-23 19:12:211246 case NAVIGATION_TYPE_NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:491247 // If a pending navigation was in progress, this canceled it. We should
1248 // discard it and make sure it is removed from the URL bar. After that,
1249 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:431250 // the caller that nothing has happened.
arthursonzogni69a6a1b2019-09-17 09:23:001251 if (pending_entry_)
[email protected]20d1c992011-04-12 21:17:491252 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431253 return false;
Aran Gilman37d11632019-10-08 23:07:151254 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431255 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151256 break;
[email protected]765b35502008-08-21 00:51:201257 }
1258
[email protected]688aa65c62012-09-28 04:32:221259 // At this point, we know that the navigation has just completed, so
1260 // record the time.
1261 //
1262 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261263 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331264 base::Time timestamp =
1265 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1266 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131267 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221268
Peter Boströmd7592132019-01-30 04:50:311269 // If we aren't keeping the pending entry, there shouldn't be one at this
1270 // point. Clear it again in case any error cases above forgot to do so.
1271 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1272 // been cleared instead of protecting against it.
1273 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001274 DiscardNonCommittedEntries();
[email protected]f233e4232013-02-23 00:55:141275
[email protected]e9ba4472008-09-14 15:42:431276 // All committed entries should have nonempty content state so WebKit doesn't
1277 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471278 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321279 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221280 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441281 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531282 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1283 // implementing back-forward cache.
1284 if (!active_entry->back_forward_cache_metrics()) {
1285 active_entry->set_back_forward_cache_metrics(
1286 std::move(back_forward_cache_metrics));
1287 }
1288 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
Hajime Hoshi446206e2019-10-18 18:36:251289 navigation_request,
1290 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
naskoc7533512016-05-06 17:01:121291
Charles Reisc0507202017-09-21 00:40:021292 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1293 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1294 // A mismatch can occur if the renderer lies or due to a unique name collision
1295 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121296 FrameNavigationEntry* frame_entry =
1297 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021298 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1299 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031300 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081301 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1302 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031303 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201304
1305 // Remember the bindings the renderer process has at this point, so that
1306 // we do not grant this entry additional bindings if we come back to it.
1307 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301308 }
[email protected]132e281a2012-07-31 18:32:441309
[email protected]97d8f0d2013-10-29 16:49:211310 // Once it is committed, we no longer need to track several pieces of state on
1311 // the entry.
naskoc7533512016-05-06 17:01:121312 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131313
[email protected]49bd30e62011-03-22 20:12:591314 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221315 // TODO(creis): This check won't pass for subframes until we create entries
1316 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001317 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421318 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591319
[email protected]e9ba4472008-09-14 15:42:431320 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001321 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001322 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311323 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531324
arthursonzogni7ddc6542021-04-09 09:16:501325 active_entry->SetIsOverridingUserAgent(
1326 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031327
[email protected]93f230e02011-06-01 14:40:001328 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291329
John Abd-El-Malek380d3c5922017-09-08 00:20:311330 if (active_entry->GetURL().SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121331 !navigation_request->DidEncounterError()) {
John Abd-El-Malek969cdd94e2017-07-10 22:18:161332 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus",
1333 !!active_entry->GetSSL().certificate);
1334 }
1335
aelias100c9192017-01-13 00:01:431336 if (overriding_user_agent_changed)
1337 delegate_->UpdateOverridingUserAgent();
1338
creis03b48002015-11-04 00:54:561339 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1340 // one knows the latest NavigationEntry it is showing (whether it has
1341 // committed anything in this navigation or not). This allows things like
1342 // state and title updates from RenderFrames to apply to the latest relevant
1343 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381344 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001345 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381346 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
Hayato Ito2c8c08d02021-06-23 03:38:431347
1348 if (navigation_request->IsPrerenderedPageActivation()) {
1349 BroadcastHistoryOffsetAndLength();
1350 // TODO(crbug.com/1222893): Broadcasting happens after the prerendered page
1351 // is activated. As a result, a "prerenderingchange" event listener sees the
1352 // history.length which is not updated yet. We should guarantee that
1353 // history's length and offset should be updated before a
1354 // "prerenderingchange" event listener runs. One possible approach is to use
1355 // the same IPC which "prerenderingchange" uses, and propagate history's
1356 // length and offset together with that.
1357 }
1358
[email protected]e9ba4472008-09-14 15:42:431359 return true;
initial.commit09911bf2008-07-26 23:55:291360}
1361
[email protected]8ff00d72012-10-23 19:12:211362NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321363 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101364 const mojom::DidCommitProvisionalLoadParams& params,
1365 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591366 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511367 "ClassifyNavigation");
1368
avi7c6f35e2015-05-08 17:52:381369 if (params.did_create_new_entry) {
Charlie Reisc0f17d2d2021-01-12 18:52:491370 // A new entry. We may or may not have a corresponding pending entry, and
avi7c6f35e2015-05-08 17:52:381371 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001372 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491373 trace_return.set_return_reason("new entry, no parent, new entry");
1374 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381375 }
1376
1377 // When this is a new subframe navigation, we should have a committed page
1378 // in which it's a subframe. This may not be the case when an iframe is
1379 // navigated on a popup navigated to about:blank (the iframe would be
1380 // written into the popup by script on the main page). For these cases,
1381 // there isn't any navigation stuff we can do, so just ignore it.
Nasko Oskovae49e292020-08-13 02:08:511382 if (!GetLastCommittedEntry()) {
1383 trace_return.set_return_reason("new entry, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381384 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511385 }
avi7c6f35e2015-05-08 17:52:381386
1387 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511388 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381389 return NAVIGATION_TYPE_NEW_SUBFRAME;
1390 }
1391
Charlie Reisc0f17d2d2021-01-12 18:52:491392 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381393 DCHECK(!params.history_list_was_cleared);
1394
avi39c1edd32015-06-04 20:06:001395 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381396 // All manual subframes would be did_create_new_entry and handled above, so
1397 // we know this is auto.
Nasko Oskovae49e292020-08-13 02:08:511398 if (GetLastCommittedEntry()) {
1399 trace_return.set_return_reason("subframe, last commmited, auto subframe");
avi7c6f35e2015-05-08 17:52:381400 return NAVIGATION_TYPE_AUTO_SUBFRAME;
Nasko Oskovae49e292020-08-13 02:08:511401 }
Lei Zhang96031532019-10-10 19:05:471402
1403 // We ignore subframes created in non-committed pages; we'd appreciate if
1404 // people stopped doing that.
Nasko Oskovae49e292020-08-13 02:08:511405 trace_return.set_return_reason("subframe, no last commmited, ignore");
Lei Zhang96031532019-10-10 19:05:471406 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381407 }
1408
Rakina Zata Amnif6950d552020-11-24 03:26:101409 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1410 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381411 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1412 // create a new page.
1413
1414 // Just like above in the did_create_new_entry case, it's possible to
1415 // scribble onto an uncommitted page. Again, there isn't any navigation
1416 // stuff that we can do, so ignore it here as well.
avi3a5b8f32015-05-28 17:50:231417 NavigationEntry* last_committed = GetLastCommittedEntry();
Nasko Oskovae49e292020-08-13 02:08:511418 if (!last_committed) {
1419 trace_return.set_return_reason("nav entry 0, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381420 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511421 }
avi7c6f35e2015-05-08 17:52:381422
Hayato Ito2ae49442021-07-02 02:59:251423 // This main frame navigation is not a history navigation (since
1424 // nav_entry_id is 0), but didn't create a new entry. So this must be a
1425 // reload or a replacement navigation, which will modify the existing entry.
1426 //
Nasko Oskov332593c2018-08-16 17:21:341427 // TODO(nasko): With error page isolation, reloading an existing session
1428 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491429 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341430 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511431 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491432 "nav entry 0, last committed, existing entry");
1433 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381434 }
1435
Rakina Zata Amnif6950d552020-11-24 03:26:101436 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521437 // If the SiteInstance of the |pending_entry_| does not match the
1438 // SiteInstance that got committed, treat this as a new navigation with
1439 // replacement. This can happen if back/forward/reload encounters a server
1440 // redirect to a different site or an isolated error page gets successfully
1441 // reloaded into a different SiteInstance.
1442 if (pending_entry_->site_instance() &&
1443 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491444 trace_return.set_return_reason("pending matching nav entry, new entry");
1445 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521446 }
creis77c9aa32015-09-25 19:59:421447
Nasko Oskovaee2f862018-06-15 00:05:521448 if (pending_entry_index_ == -1) {
1449 // In this case, we have a pending entry for a load of a new URL but Blink
1450 // didn't do a new navigation (params.did_create_new_entry). First check
1451 // to make sure Blink didn't treat a new cross-process navigation as
1452 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161453 // we must treat it as NEW rather than the converted reload case below,
1454 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521455 if (!GetLastCommittedEntry() ||
1456 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161457 trace_return.set_return_reason("new pending, new entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491458 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521459 }
1460
1461 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161462 // We will create a pending entry, but NavigateWithoutEntry will convert
1463 // it to a reload since it's the same page and not create a new entry for
1464 // it. (The user doesn't want to have a new back/forward entry when they
1465 // do this.) Therefore we want to just ignore the pending entry and go
1466 // back to where we were (the "existing entry").
1467 trace_return.set_return_reason("new pending, existing (same) entry");
1468 return NAVIGATION_TYPE_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521469 }
avi7c6f35e2015-05-08 17:52:381470 }
1471
creis26d22632017-04-21 20:23:561472 // Everything below here is assumed to be an existing entry, but if there is
1473 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511474 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491475 trace_return.set_return_reason("no last committed, new entry");
1476 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511477 }
creis26d22632017-04-21 20:23:561478
avi7c6f35e2015-05-08 17:52:381479 if (params.intended_as_new_entry) {
1480 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491481 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1482 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161483 trace_return.set_return_reason("intended as new entry, existing entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491484 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381485 }
1486
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121487 if (navigation_request->DidEncounterError() &&
1488 failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101489 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381490 // If the renderer was going to a new pending entry that got cleared because
1491 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491492 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381493 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511494 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491495 "unreachable, matching pending, existing entry");
1496 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381497 }
1498
Charlie Reisc0f17d2d2021-01-12 18:52:491499 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101500 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511501 trace_return.traced_value()->SetInteger("existing_entry_index",
1502 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381503 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441504 // The renderer has committed a navigation to an entry that no longer
1505 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491506 trace_return.set_return_reason("existing entry -1, new entry");
1507 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381508 }
1509
avi7c6f35e2015-05-08 17:52:381510 // Since we weeded out "new" navigations above, we know this is an existing
1511 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491512 trace_return.set_return_reason("default return, existing entry");
1513 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381514}
1515
Charlie Reisc0f17d2d2021-01-12 18:52:491516void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321517 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071518 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361519 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441520 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471521 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451522 NavigationRequest* request) {
dcheng9bfa5162016-04-09 01:00:571523 std::unique_ptr<NavigationEntryImpl> new_entry;
creisf49dfc92016-07-26 17:05:181524 bool update_virtual_url = false;
1525
Anton Bikineevf62d1bf2021-05-15 17:56:071526 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451527 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061528
creisf49dfc92016-07-26 17:05:181529 // First check if this is an in-page navigation. If so, clone the current
1530 // entry instead of looking at the pending entry, because the pending entry
1531 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361532 if (is_same_document && GetLastCommittedEntry()) {
Patrick Monette50e8bd82019-06-13 22:40:451533 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481534 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081535 params.document_sequence_number, params.app_history_key,
1536 rfh->GetSiteInstance(), nullptr, params.url,
1537 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091538 Referrer(*params.referrer), initiator_origin,
1539 request->GetRedirectChain(), params.page_state, params.method,
1540 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391541 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481542 request->GetSubresourceWebBundleNavigationInfo(),
Antonio Sartori78a749f2020-11-30 12:03:391543 // We will set the document policies later in this function.
Titouan Rigoudy6ec70402021-02-02 15:42:191544 nullptr /* policy_container_policies */);
Charles Reisf44482022017-10-13 21:15:031545
creisf49dfc92016-07-26 17:05:181546 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Carlos Caballero40b0efd2021-01-26 11:55:001547 frame_entry, true, rfh->frame_tree_node(), frame_tree_.root());
jama78746e2017-02-22 17:21:571548 if (new_entry->GetURL().GetOrigin() != params.url.GetOrigin()) {
1549 // TODO(jam): we had one report of this with a URL that was redirecting to
1550 // only tildes. Until we understand that better, don't copy the cert in
1551 // this case.
1552 new_entry->GetSSL() = SSLStatus();
John Abd-El-Malek9cd697e2017-07-12 21:53:141553
John Abd-El-Malek380d3c5922017-09-08 00:20:311554 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121555 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141556 UMA_HISTOGRAM_BOOLEAN(
1557 "Navigation.SecureSchemeHasSSLStatus.NewPageInPageOriginMismatch",
1558 !!new_entry->GetSSL().certificate);
1559 }
jama78746e2017-02-22 17:21:571560 }
creisf49dfc92016-07-26 17:05:181561
Patrick Monette50e8bd82019-06-13 22:40:451562 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1563 // that |frame_entry| should now have a reference count of exactly 2: one
1564 // due to the local variable |frame_entry|, and another due to |new_entry|
1565 // also retaining one. This should never fail, because it's the main frame.
1566 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181567
1568 update_virtual_url = new_entry->update_virtual_url_with_url();
John Abd-El-Malek9cd697e2017-07-12 21:53:141569
John Abd-El-Malek380d3c5922017-09-08 00:20:311570 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121571 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141572 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus.NewPageInPage",
1573 !!new_entry->GetSSL().certificate);
1574 }
creisf49dfc92016-07-26 17:05:181575 }
1576
Harkiran Bolaria59290d62021-03-17 01:53:011577 // If this is an activation navigation from a prerendered page, transfer the
1578 // new entry from an entry already created and stored in the
1579 // NavigationRequest. |new_entry| will not have been set prior to this as
1580 // |is_same_document| is mutually exclusive with
1581 // |IsPrerenderedPageActivation|.
1582 if (request->IsPrerenderedPageActivation()) {
1583 DCHECK(!is_same_document);
1584 DCHECK(!new_entry);
1585 new_entry = request->TakePrerenderNavigationEntry();
1586 DCHECK(new_entry);
1587 }
1588
Charlie Reisc0f17d2d2021-01-12 18:52:491589 // Only make a copy of the pending entry if it is appropriate for the new
1590 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451591 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041592 // 1. The SiteInstance hasn't been assigned to something else.
1593 // 2. The pending entry was intended as a new entry, rather than being a
1594 // history navigation that was interrupted by an unrelated,
1595 // renderer-initiated navigation.
1596 // TODO(csharrison): Investigate whether we can remove some of the coarser
1597 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451598 if (!new_entry && PendingEntryMatchesRequest(request) &&
1599 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341600 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431601 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351602 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431603
[email protected]f1eb87a2011-05-06 17:49:411604 update_virtual_url = new_entry->update_virtual_url_with_url();
Camille Lamy62b826012019-02-26 09:15:471605 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451606 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141607
John Abd-El-Malek380d3c5922017-09-08 00:20:311608 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121609 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141610 UMA_HISTOGRAM_BOOLEAN(
1611 "Navigation.SecureSchemeHasSSLStatus.NewPagePendingEntryMatches",
1612 !!new_entry->GetSSL().certificate);
1613 }
creisf49dfc92016-07-26 17:05:181614 }
1615
Charlie Reisc0f17d2d2021-01-12 18:52:491616 // For cross-document commits with no matching pending entry, create a new
1617 // entry.
creisf49dfc92016-07-26 17:05:181618 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061619 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071620 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1621 initiator_origin,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:581622 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451623 params.transition, request->IsRendererInitiated(),
Lukasz Anforowicz435bcb582019-07-12 20:50:061624 nullptr); // blob_url_loader_factory
[email protected]f8f93eb2012-09-25 03:06:241625
1626 // Find out whether the new entry needs to update its virtual URL on URL
1627 // change and set up the entry accordingly. This is needed to correctly
1628 // update the virtual URL when replaceState is called after a pushState.
1629 GURL url = params.url;
1630 bool needs_update = false;
[email protected]47752982014-07-29 08:01:431631 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1632 &url, browser_context_, &needs_update);
[email protected]f8f93eb2012-09-25 03:06:241633 new_entry->set_update_virtual_url_with_url(needs_update);
1634
Charlie Reisc0f17d2d2021-01-12 18:52:491635 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411636 // update the virtual URL based on the new URL. For example, this is needed
1637 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1638 // the URL.
[email protected]f8f93eb2012-09-25 03:06:241639 update_virtual_url = needs_update;
Camille Lamy62b826012019-02-26 09:15:471640 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451641 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141642
John Abd-El-Malek380d3c5922017-09-08 00:20:311643 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121644 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141645 UMA_HISTOGRAM_BOOLEAN(
1646 "Navigation.SecureSchemeHasSSLStatus.NewPageNoMatchingEntry",
1647 !!new_entry->GetSSL().certificate);
1648 }
[email protected]e9ba4472008-09-14 15:42:431649 }
1650
Harkiran Bolaria59290d62021-03-17 01:53:011651 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
1652 // for prerendered contents, if any. This is because prerendering/activation
1653 // technically won't be creating a new document. Unlike BFCache, prerendering
1654 // creates a new NavigationEntry rather than using an existing one.
1655 if (!request->IsPrerenderedPageActivation()) {
1656 // Don't use the page type from the pending entry. Some interstitial page
1657 // may have set the type to interstitial. Once we commit, however, the page
1658 // type must always be normal or error.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121659 new_entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1660 : PAGE_TYPE_NORMAL);
Harkiran Bolaria59290d62021-03-17 01:53:011661 new_entry->SetURL(params.url);
1662 if (update_virtual_url)
1663 UpdateVirtualURLToURL(new_entry.get(), params.url);
1664 new_entry->SetReferrer(Referrer(*params.referrer));
1665 new_entry->SetTransitionType(params.transition);
1666 new_entry->set_site_instance(
1667 static_cast<SiteInstanceImpl*>(rfh->GetSiteInstance()));
1668 new_entry->SetOriginalRequestURL(request->GetOriginalRequestURL());
Rakina Zata Amnib597d632020-12-01 00:56:001669
Antonio Sartori4f5373792021-05-31 10:56:471670 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
Harkiran Bolaria59290d62021-03-17 01:53:011671 new_entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
[email protected]e9ba4472008-09-14 15:42:431672
Harkiran Bolaria59290d62021-03-17 01:53:011673 // Update the FrameNavigationEntry for new main frame commits.
1674 FrameNavigationEntry* frame_entry =
1675 new_entry->GetFrameEntry(rfh->frame_tree_node());
1676 frame_entry->set_frame_unique_name(rfh->frame_tree_node()->unique_name());
1677 frame_entry->set_item_sequence_number(params.item_sequence_number);
1678 frame_entry->set_document_sequence_number(params.document_sequence_number);
Nate Chapinfbfe5af2021-06-10 17:22:081679 frame_entry->set_app_history_key(params.app_history_key);
Harkiran Bolaria59290d62021-03-17 01:53:011680 frame_entry->set_redirect_chain(request->GetRedirectChain());
1681 frame_entry->SetPageState(params.page_state);
1682 frame_entry->set_method(params.method);
1683 frame_entry->set_post_id(params.post_id);
1684 frame_entry->set_policy_container_policies(
1685 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1686 request));
Antonio Sartori78a749f2020-11-30 12:03:391687
Anton Bikineevf62d1bf2021-05-15 17:56:071688 if (absl::optional<url::Origin> committed_origin =
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121689 GetCommittedOriginForFrameEntry(params, request)) {
1690 if (committed_origin.has_value())
1691 frame_entry->set_committed_origin(committed_origin.value());
1692 }
Harkiran Bolaria59290d62021-03-17 01:53:011693 if (request->web_bundle_navigation_info()) {
1694 frame_entry->set_web_bundle_navigation_info(
1695 request->web_bundle_navigation_info()->Clone());
1696 }
creis8b5cd4c2015-06-19 00:11:081697
Harkiran Bolaria59290d62021-03-17 01:53:011698 // history.pushState() is classified as a navigation to a new page, but sets
1699 // is_same_document to true. In this case, we already have the title and
1700 // favicon available, so set them immediately.
1701 if (is_same_document && GetLastCommittedEntry()) {
1702 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
1703 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1704 }
[email protected]3a868f212014-08-09 10:41:191705 }
[email protected]ff64b3e2014-05-31 04:07:331706
[email protected]60d6cca2013-04-30 08:47:131707 DCHECK(!params.history_list_was_cleared || !replace_entry);
1708 // The browser requested to clear the session history when it initiated the
1709 // navigation. Now we know that the renderer has updated its state accordingly
1710 // and it is safe to also clear the browser side history.
1711 if (params.history_list_was_cleared) {
arthursonzogni69a6a1b2019-09-17 09:23:001712 DiscardNonCommittedEntries();
[email protected]60d6cca2013-04-30 08:47:131713 entries_.clear();
1714 last_committed_entry_index_ = -1;
1715 }
1716
Nasko Oskovaee2f862018-06-15 00:05:521717 // If this is a new navigation with replacement and there is a
1718 // pending_entry_ which matches the navigation reported by the renderer
1719 // process, then it should be the one replaced, so update the
1720 // last_committed_entry_index_ to use it.
1721 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101722 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521723 last_committed_entry_index_ = pending_entry_index_;
1724 }
1725
Alexander Timine3ec4192020-04-20 16:39:401726 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411727 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401728 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051729
Carlos IL42b416592019-10-07 23:10:361730 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
1731 !request->post_commit_error_page_html().empty());
[email protected]e9ba4472008-09-14 15:42:431732}
1733
Charlie Reisc0f17d2d2021-01-12 18:52:491734void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321735 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071736 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361737 bool is_same_document,
jam48cea9082017-02-15 06:13:291738 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451739 NavigationRequest* request,
Peter Boströmd7592132019-01-30 04:50:311740 bool keep_pending_entry) {
creis26d22632017-04-21 20:23:561741 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1742 << "that a last committed entry exists.";
1743
[email protected]e9ba4472008-09-14 15:42:431744 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001745 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431746
Charlie Reis7e2cb6d2021-01-26 01:27:161747 NavigationEntryImpl* entry = nullptr;
avicbdc4c12015-07-01 16:07:111748 if (params.intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:161749 // We're guaranteed to have a last committed entry if intended_as_new_entry
1750 // is true.
avicbdc4c12015-07-01 16:07:111751 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:161752 DCHECK(entry);
1753
1754 // If the NavigationRequest matches a new pending entry and is classified as
1755 // EXISTING_ENTRY, then it is a navigation to the same URL that was
1756 // converted to a reload, such as a user pressing enter in the omnibox.
1757 if (pending_entry_ && pending_entry_index_ == -1 &&
1758 pending_entry_->GetUniqueID() ==
1759 request->commit_params().nav_entry_id) {
1760 // Note: The pending entry will usually have a real ReloadType here, but
1761 // it can still be ReloadType::NONE in cases that
1762 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
1763
1764 // If we classified this correctly, the SiteInstance should not have
1765 // changed.
1766 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
1767
1768 // For converted reloads, we assign the entry's unique ID to be that of
1769 // the new one. Since this is always the result of a user action, we want
1770 // to dismiss infobars, etc. like a regular user-initiated navigation.
1771 entry->set_unique_id(pending_entry_->GetUniqueID());
1772
1773 // The extra headers may have changed due to reloading with different
1774 // headers.
1775 entry->set_extra_headers(pending_entry_->extra_headers());
1776 }
1777 // Otherwise, this was intended as a new entry but the pending entry was
1778 // lost in the meantime and no new entry was created. We are stuck at the
1779 // last committed entry.
1780
1781 // Even if this is a converted reload from pressing enter in the omnibox,
1782 // the server could redirect, requiring an update to the SSL status. If this
1783 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451784 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:161785 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:471786 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451787 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:161788 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141789
Charlie Reis7e2cb6d2021-01-26 01:27:161790 if (params.url.SchemeIs(url::kHttpsScheme) &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121791 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141792 bool has_cert = !!entry->GetSSL().certificate;
1793 if (is_same_document) {
1794 UMA_HISTOGRAM_BOOLEAN(
1795 "Navigation.SecureSchemeHasSSLStatus."
1796 "ExistingPageSameDocumentIntendedAsNew",
1797 has_cert);
1798 } else {
1799 UMA_HISTOGRAM_BOOLEAN(
1800 "Navigation.SecureSchemeHasSSLStatus."
1801 "ExistingPageDifferentDocumentIntendedAsNew",
1802 has_cert);
1803 }
1804 }
Rakina Zata Amnif6950d552020-11-24 03:26:101805 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:111806 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:101807 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b902016-09-01 16:58:161808
eugenebut604866f2017-05-10 21:35:361809 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451810 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361811 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1812 // this was a restored same document navigation entry, then it won't have
1813 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1814 // navigated it.
jam48cea9082017-02-15 06:13:291815 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
1816 if (entry->GetURL().GetOrigin() == last_entry->GetURL().GetOrigin() &&
1817 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1818 was_restored) {
1819 entry->GetSSL() = last_entry->GetSSL();
1820 }
1821 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451822 // In rapid back/forward navigations |request| sometimes won't have a cert
1823 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191824 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451825 if (request->GetSSLInfo().has_value() &&
1826 request->GetSSLInfo()->is_valid()) {
1827 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
1828 } else if (entry->GetURL().GetOrigin() != request->GetURL().GetOrigin()) {
John Abd-El-Malek3f247082017-12-07 19:02:191829 entry->GetSSL() = SSLStatus();
1830 }
jam48cea9082017-02-15 06:13:291831 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141832
John Abd-El-Malek380d3c5922017-09-08 00:20:311833 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121834 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141835 bool has_cert = !!entry->GetSSL().certificate;
1836 if (is_same_document && was_restored) {
1837 UMA_HISTOGRAM_BOOLEAN(
1838 "Navigation.SecureSchemeHasSSLStatus."
1839 "ExistingPageSameDocumentRestoredBrowserInitiated",
1840 has_cert);
1841 } else if (is_same_document && !was_restored) {
1842 UMA_HISTOGRAM_BOOLEAN(
1843 "Navigation.SecureSchemeHasSSLStatus."
1844 "ExistingPageSameDocumentBrowserInitiated",
1845 has_cert);
1846 } else if (!is_same_document && was_restored) {
1847 UMA_HISTOGRAM_BOOLEAN(
1848 "Navigation.SecureSchemeHasSSLStatus."
1849 "ExistingPageRestoredBrowserInitiated",
1850 has_cert);
1851 } else {
1852 UMA_HISTOGRAM_BOOLEAN(
1853 "Navigation.SecureSchemeHasSSLStatus.ExistingPageBrowserInitiated",
1854 has_cert);
1855 }
1856 }
avicbdc4c12015-07-01 16:07:111857 } else {
1858 // This is renderer-initiated. The only kinds of renderer-initated
Charlie Reisc0f17d2d2021-01-12 18:52:491859 // navigations that are EXISTING_ENTRY are reloads and history.replaceState,
avicbdc4c12015-07-01 16:07:111860 // which land us at the last committed entry.
1861 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101862
Mikel Astizba9cf2fd2017-12-17 10:38:101863 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1864 // to avoid misleading interpretations (e.g. URLs paired with
1865 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1866 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1867 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1868
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571869 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:101870
eugenebut604866f2017-05-10 21:35:361871 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451872 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361873 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471874 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451875 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141876
John Abd-El-Malek380d3c5922017-09-08 00:20:311877 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121878 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141879 bool has_cert = !!entry->GetSSL().certificate;
1880 if (is_same_document) {
1881 UMA_HISTOGRAM_BOOLEAN(
1882 "Navigation.SecureSchemeHasSSLStatus."
1883 "ExistingPageSameDocumentRendererInitiated",
1884 has_cert);
1885 } else {
1886 UMA_HISTOGRAM_BOOLEAN(
1887 "Navigation.SecureSchemeHasSSLStatus."
1888 "ExistingPageDifferentDocumentRendererInitiated",
1889 has_cert);
1890 }
1891 }
avicbdc4c12015-07-01 16:07:111892 }
1893 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:431894
[email protected]5ccd4dc2012-10-24 02:28:141895 // The URL may have changed due to redirects.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121896 entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1897 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041898 entry->SetURL(params.url);
arthursonzogni73fe3212020-11-17 13:24:071899 entry->SetReferrer(Referrer(*params.referrer));
[email protected]38178a42009-12-17 18:58:321900 if (entry->update_virtual_url_with_url())
1901 UpdateVirtualURLToURL(entry, params.url);
[email protected]5ccd4dc2012-10-24 02:28:141902
jam015ba062017-01-06 21:17:001903 // The site instance will normally be the same except
1904 // 1) session restore, when no site instance will be assigned or
1905 // 2) redirect, when the site instance is reset.
Lei Zhang96031532019-10-10 19:05:471906 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
naskoaf182192016-08-11 02:12:011907 entry->site_instance() == rfh->GetSiteInstance());
clamy432acb22016-04-15 19:41:431908
naskoaf182192016-08-11 02:12:011909 // Update the existing FrameNavigationEntry to ensure all of its members
1910 // reflect the parameters coming from the renderer process.
Anton Bikineevf62d1bf2021-05-15 17:56:071911 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451912 request->common_params().initiator_origin;
naskoaf182192016-08-11 02:12:011913 entry->AddOrUpdateFrameEntry(
Nate Chapin9f169072021-06-09 19:32:371914 rfh->frame_tree_node(), NavigationEntryImpl::UpdatePolicy::kUpdate,
1915 params.item_sequence_number, params.document_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081916 params.app_history_key, rfh->GetSiteInstance(), nullptr, params.url,
Nate Chapin9f169072021-06-09 19:32:371917 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091918 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071919 params.page_state, params.method, params.post_id,
1920 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031921 request->web_bundle_navigation_info()
1922 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391923 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481924 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191925 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1926 request));
creis22a7b4c2016-04-28 07:20:301927
[email protected]5ccd4dc2012-10-24 02:28:141928 // The redirected to page should not inherit the favicon from the previous
1929 // page.
eugenebut604866f2017-05-10 21:35:361930 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:481931 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:141932
Peter Boströmd7592132019-01-30 04:50:311933 // We should also usually discard the pending entry if it corresponds to a
1934 // different navigation, since that one is now likely canceled. In rare
1935 // cases, we leave the pending entry for another navigation in place when we
1936 // know it is still ongoing, to avoid a flicker in the omnibox (see
1937 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:431938 //
1939 // Note that we need to use the "internal" version since we don't want to
1940 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:311941 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001942 DiscardNonCommittedEntries();
[email protected]40bcc302009-03-02 20:50:391943
Carlos IL4dea8902020-05-26 15:14:291944 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:111945 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:431946}
1947
[email protected]d202a7c2012-01-04 07:53:471948void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:321949 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071950 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361951 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:471952 bool replace_entry,
1953 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451954 NavigationRequest* request) {
avi25f5f9e2015-07-17 20:08:261955 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1956 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
[email protected]09b8f82f2009-06-16 20:22:111957
[email protected]e9ba4472008-09-14 15:42:431958 // Manual subframe navigations just get the current entry cloned so the user
1959 // can go back or forward to it. The actual subframe information will be
1960 // stored in the page state for each of those entries. This happens out of
1961 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:091962 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1963 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:381964
Mikel Astizba9cf2fd2017-12-17 10:38:101965 // The DCHECK below documents the fact that we don't know of any situation
1966 // where |replace_entry| is true for subframe navigations. This simplifies
1967 // reasoning about the replacement struct for subframes (see
1968 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
1969 DCHECK(!replace_entry);
1970
Patrick Monette50e8bd82019-06-13 22:40:451971 // This FrameNavigationEntry might not end up being used in the
1972 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Anton Bikineevf62d1bf2021-05-15 17:56:071973 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451974 request->common_params().initiator_origin;
Patrick Monette50e8bd82019-06-13 22:40:451975 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481976 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081977 params.document_sequence_number, params.app_history_key,
1978 rfh->GetSiteInstance(), nullptr, params.url,
1979 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091980 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071981 params.page_state, params.method, params.post_id,
1982 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031983 request->web_bundle_navigation_info()
1984 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391985 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481986 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191987 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1988 request));
Charles Reisf44482022017-10-13 21:15:031989
creisce0ef3572017-01-26 17:53:081990 std::unique_ptr<NavigationEntryImpl> new_entry =
1991 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:451992 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:001993 frame_tree_.root());
creise062d542015-08-25 02:01:551994
Alexander Timine3ec4192020-04-20 16:39:401995 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411996 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401997 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:471998
creisce0ef3572017-01-26 17:53:081999 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:452000 // to replace, which can happen due to a unique name change. See
2001 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
2002 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:382003
Carlos IL42b416592019-10-07 23:10:362004 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false);
[email protected]e9ba4472008-09-14 15:42:432005}
2006
[email protected]d202a7c2012-01-04 07:53:472007bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:322008 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072009 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392010 bool is_same_document,
Nate Chapinc7019dd7d2021-06-25 18:29:252011 bool was_on_initial_empty_document,
Mohamed Abdelhalim833de902019-09-16 17:41:452012 NavigationRequest* request) {
avi9f07a0c2015-02-18 22:51:292013 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2014 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2015
[email protected]e9ba4472008-09-14 15:42:432016 // We're guaranteed to have a previously committed entry, and we now need to
2017 // handle navigation inside of a subframe in it without creating a new entry.
2018 DCHECK(GetLastCommittedEntry());
2019
creis913c63ce2016-07-16 19:52:522020 // For newly created subframes, we don't need to send a commit notification.
2021 // This is only necessary for history navigations in subframes.
2022 bool send_commit_notification = false;
2023
Rakina Zata Amnif6950d552020-11-24 03:26:102024 // If |nav_entry_id| is non-zero and matches an existing entry, this
2025 // is a history navigation. Update the last committed index accordingly. If
2026 // we don't recognize the |nav_entry_id|, it might be a recently
2027 // pruned entry. We'll handle it below.
2028 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2029 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472030 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062031 // Make sure that a subframe commit isn't changing the main frame's
2032 // origin. Otherwise the renderer process may be confused, leading to a
2033 // URL spoof. We can't check the path since that may change
2034 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572035 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2036 // about:blank, file, and unique origins are more subtle to get right.
Charlie Reis95fbca442021-05-21 21:38:242037 // We should use checks similar to RenderFrameHostImpl's
2038 // CanCommitUrlAndOrigin on the main frame during subframe commits.
2039 // See https://crbug.com/1209092.
creis37988b92016-06-10 18:03:572040 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2041 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2042 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2043 dest_top_url.SchemeIsHTTPOrHTTPS() &&
2044 current_top_url.GetOrigin() != dest_top_url.GetOrigin()) {
creisfb6eeb62016-05-10 19:01:512045 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2046 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062047 }
creis3cdc3b02015-05-29 23:00:472048
creis913c63ce2016-07-16 19:52:522049 // We only need to discard the pending entry in this history navigation
2050 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062051 last_committed_entry_index_ = entry_index;
arthursonzogni69a6a1b2019-09-17 09:23:002052 DiscardNonCommittedEntries();
creis913c63ce2016-07-16 19:52:522053
2054 // History navigations should send a commit notification.
2055 send_commit_notification = true;
avi98405c22015-05-21 20:47:062056 }
[email protected]e9ba4472008-09-14 15:42:432057 }
[email protected]f233e4232013-02-23 00:55:142058
creisce0ef3572017-01-26 17:53:082059 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2060 // it may be a "history auto" case where we update an existing one.
2061 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Nate Chapin9f169072021-06-09 19:32:372062
2063 // We may want to update |last_committed|'s FrameNavigationEntry (if one
2064 // exists), or we may want to clobber it and create a new one. We update in
2065 // cases where the corresponding FrameNavigationEntry is conceptually similar
2066 // to the document described by the commit params: same-document
2067 // navigations, history traversal to an existing entry, and reloads (including
2068 // a "soft reload" where we navigate to the same url without flagging it as a
2069 // reload). But in the case of a different document that is not logically
2070 // related to the committed FrameNavigationEntry's document (cross-document,
2071 // not same url, not a reload, not a history traversal), we replace rather
2072 // than update.
Nate Chapinc7019dd7d2021-06-25 18:29:252073 //
Nate Chapin9f169072021-06-09 19:32:372074 // In the case where we update, the FrameNavigationEntry will potentially be
2075 // shared across multiple NavigationEntries, and any updates will be reflected
2076 // in all of those NavigationEntries. In the replace case, any existing
2077 // sharing with other NavigationEntries will stop.
Nate Chapinc7019dd7d2021-06-25 18:29:252078 //
2079 // When navigating away from the initial empty document, we also update rather
2080 // than replace. Either update or replace will overwrite the initial empty
2081 // document state for |last_committed|, but if the FrameNavigationEntry for
2082 // the initial empty document is shared across multiple NavigationEntries (due
2083 // to a navigation in another frame), we want to make sure we overwrite the
2084 // initial empty document state everywhere this FrameNavigationEntry is used,
2085 // which is accompished by updating the existing FrameNavigationEntry.
Nate Chapin9f169072021-06-09 19:32:372086 FrameNavigationEntry* last_committed_frame_entry =
2087 last_committed->GetFrameEntry(rfh->frame_tree_node());
2088 NavigationEntryImpl::UpdatePolicy update_policy =
2089 NavigationEntryImpl::UpdatePolicy::kUpdate;
2090 if (request->common_params().navigation_type ==
Minggang Wangb9f3fa92021-07-01 15:30:312091 blink::mojom::NavigationType::DIFFERENT_DOCUMENT &&
Nate Chapin9f169072021-06-09 19:32:372092 last_committed_frame_entry &&
Nate Chapinc7019dd7d2021-06-25 18:29:252093 last_committed_frame_entry->url() != params.url &&
2094 !was_on_initial_empty_document) {
Nate Chapin9f169072021-06-09 19:32:372095 update_policy = NavigationEntryImpl::UpdatePolicy::kReplace;
2096 }
2097
Anton Bikineevf62d1bf2021-05-15 17:56:072098 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452099 request->common_params().initiator_origin;
creisce0ef3572017-01-26 17:53:082100 last_committed->AddOrUpdateFrameEntry(
Nate Chapin9f169072021-06-09 19:32:372101 rfh->frame_tree_node(), update_policy, params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:082102 params.document_sequence_number, params.app_history_key,
2103 rfh->GetSiteInstance(), nullptr, params.url,
2104 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:092105 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072106 params.page_state, params.method, params.post_id,
2107 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032108 request->web_bundle_navigation_info()
2109 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392110 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482111 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:192112 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
2113 request));
creis625a0c7d2015-03-24 23:17:122114
creis913c63ce2016-07-16 19:52:522115 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432116}
2117
[email protected]d202a7c2012-01-04 07:53:472118int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232119 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032120 for (size_t i = 0; i < entries_.size(); ++i) {
2121 if (entries_[i].get() == entry)
2122 return i;
2123 }
2124 return -1;
[email protected]765b35502008-08-21 00:51:202125}
2126
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572127void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242128 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572129 NavigationControllerImpl* source =
2130 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572131 // Verify that we look new.
Lei Zhang96031532019-10-10 19:05:472132 DCHECK_EQ(0, GetEntryCount());
2133 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:572134
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572135 if (source->GetEntryCount() == 0)
[email protected]ce3fa3c2009-04-20 19:55:572136 return; // Nothing new to do.
2137
Francois Dorayeaace782017-06-21 16:37:242138 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442139 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572140 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572141
Fergal Dalya1d569972021-03-16 03:24:532142 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302143 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532144 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2145 session_storage_namespace_map_[it.first] = source_namespace->Clone();
Aaron Colwellb731a0ae2021-03-19 19:14:472146 OnStoragePartitionIdAdded(it.first);
[email protected]fdac6ade2013-07-20 01:06:302147 }
[email protected]4e6419c2010-01-15 04:50:342148
Lukasz Anforowicz0de0f452020-12-02 19:57:152149 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572150}
2151
Aran Gilman37d11632019-10-08 23:07:152152void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2153 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162154 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012155 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162156
[email protected]d202a7c2012-01-04 07:53:472157 NavigationControllerImpl* source =
2158 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252159
avi2b177592014-12-10 02:08:022160 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012161 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252162
[email protected]474f8512013-05-31 22:31:162163 // We now have one entry, possibly with a new pending entry. Ensure that
2164 // adding the entries from source won't put us over the limit.
2165 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572166 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572167 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252168
Carlos IL4dea8902020-05-26 15:14:292169 // Insert the entries from source. Ignore any pending entry, since it has not
2170 // committed in source.
[email protected]474f8512013-05-31 22:31:162171 int max_source_index = source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:252172 if (max_source_index == -1)
[email protected]a26023822011-12-29 00:23:552173 max_source_index = source->GetEntryCount();
[email protected]e1cd5452010-08-26 18:03:252174 else
2175 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572176
2177 // Ignore the source's current entry if merging with replacement.
2178 // TODO(davidben): This should preserve entries forward of the current
2179 // too. http://crbug.com/317872
2180 if (replace_entry && max_source_index > 0)
2181 max_source_index--;
2182
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572183 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252184
2185 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552186 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142187
Hayato Ito2c8c08d02021-06-23 03:38:432188 BroadcastHistoryOffsetAndLength();
[email protected]e1cd5452010-08-26 18:03:252189}
2190
[email protected]79368982013-11-13 01:11:012191bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162192 // If there is no last committed entry, we cannot prune. Even if there is a
2193 // pending entry, it may not commit, leaving this WebContents blank, despite
2194 // possibly giving it new entries via CopyStateFromAndPrune.
2195 if (last_committed_entry_index_ == -1)
2196 return false;
[email protected]9350602e2013-02-26 23:27:442197
[email protected]474f8512013-05-31 22:31:162198 // We cannot prune if there is a pending entry at an existing entry index.
2199 // It may not commit, so we have to keep the last committed entry, and thus
2200 // there is no sensible place to keep the pending entry. It is ok to have
2201 // a new pending entry, which can optionally commit as a new navigation.
2202 if (pending_entry_index_ != -1)
2203 return false;
2204
[email protected]474f8512013-05-31 22:31:162205 return true;
2206}
2207
[email protected]79368982013-11-13 01:11:012208void NavigationControllerImpl::PruneAllButLastCommitted() {
2209 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162210
avi2b177592014-12-10 02:08:022211 DCHECK_EQ(0, last_committed_entry_index_);
2212 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442213
Hayato Ito2c8c08d02021-06-23 03:38:432214 BroadcastHistoryOffsetAndLength();
[email protected]9350602e2013-02-26 23:27:442215}
2216
[email protected]79368982013-11-13 01:11:012217void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162218 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012219 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262220
[email protected]474f8512013-05-31 22:31:162221 // Erase all entries but the last committed entry. There may still be a
2222 // new pending entry after this.
2223 entries_.erase(entries_.begin(),
2224 entries_.begin() + last_committed_entry_index_);
2225 entries_.erase(entries_.begin() + 1, entries_.end());
2226 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262227}
2228
Christian Dullweber1af31e62018-02-22 11:49:482229void NavigationControllerImpl::DeleteNavigationEntries(
2230 const DeletionPredicate& deletionPredicate) {
2231 // It is up to callers to check the invariants before calling this.
2232 CHECK(CanPruneAllButLastCommitted());
2233 std::vector<int> delete_indices;
2234 for (size_t i = 0; i < entries_.size(); i++) {
2235 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572236 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482237 delete_indices.push_back(i);
2238 }
2239 }
2240 if (delete_indices.empty())
2241 return;
2242
2243 if (delete_indices.size() == GetEntryCount() - 1U) {
2244 PruneAllButLastCommitted();
2245 } else {
2246 // Do the deletion in reverse to preserve indices.
2247 for (auto it = delete_indices.rbegin(); it != delete_indices.rend(); ++it) {
2248 RemoveEntryAtIndex(*it);
2249 }
Hayato Ito2c8c08d02021-06-23 03:38:432250 BroadcastHistoryOffsetAndLength();
Christian Dullweber1af31e62018-02-22 11:49:482251 }
2252 delegate()->NotifyNavigationEntriesDeleted();
2253}
2254
Shivani Sharma883f5f32019-02-12 18:20:012255bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2256 auto* entry = GetEntryAtIndex(index);
2257 return entry && entry->should_skip_on_back_forward_ui();
2258}
2259
Carlos Caballero35ce710c2019-09-19 10:59:452260BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2261 return back_forward_cache_;
2262}
2263
clamy987a3752018-05-03 17:36:262264void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2265 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2266 // progress, since this will cause a use-after-free. (We only allow this
2267 // when the tab is being destroyed for shutdown, since it won't return to
2268 // NavigateToEntry in that case.) http://crbug.com/347742.
2269 CHECK(!in_navigate_to_pending_entry_ || delegate_->IsBeingDestroyed());
2270
2271 if (was_failure && pending_entry_) {
2272 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2273 } else {
2274 failed_pending_entry_id_ = 0;
2275 }
2276
2277 if (pending_entry_) {
2278 if (pending_entry_index_ == -1)
2279 delete pending_entry_;
2280 pending_entry_index_ = -1;
2281 pending_entry_ = nullptr;
2282 }
arthursonzogni66f711c2019-10-08 14:40:362283
2284 // Ensure any refs to the current pending entry are ignored if they get
2285 // deleted, by clearing the set of known refs. All future pending entries will
2286 // only be affected by new refs.
2287 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262288}
2289
2290void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2291 if (pending_entry_)
2292 pending_entry_->set_ssl_error(error);
2293}
2294
Camille Lamy5193caa2018-10-12 11:59:422295#if defined(OS_ANDROID)
2296// static
2297bool NavigationControllerImpl::ValidateDataURLAsString(
2298 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2299 if (!data_url_as_string)
2300 return false;
2301
2302 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2303 return false;
2304
2305 // The number of characters that is enough for validating a data: URI.
2306 // From the GURL's POV, the only important part here is scheme, it doesn't
2307 // check the actual content. Thus we can take only the prefix of the url, to
2308 // avoid unneeded copying of a potentially long string.
2309 const size_t kDataUriPrefixMaxLen = 64;
2310 GURL data_url(
2311 std::string(data_url_as_string->front_as<char>(),
2312 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2313 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2314 return false;
2315
2316 return true;
2317}
2318#endif
2319
Shivani Sharma194877032019-03-07 17:52:472320void NavigationControllerImpl::NotifyUserActivation() {
2321 // When a user activation occurs, ensure that all adjacent entries for the
2322 // same document clear their skippable bit, so that the history manipulation
2323 // intervention does not apply to them.
Shivani Sharma194877032019-03-07 17:52:472324 auto* last_committed_entry = GetLastCommittedEntry();
2325 if (!last_committed_entry)
2326 return;
Shivani Sharma194877032019-03-07 17:52:472327
shivanigithub99368382021-06-16 18:33:372328 if (base::FeatureList::IsEnabled(
2329 features::kDebugHistoryInterventionNoUserActivation)) {
2330 return;
2331 }
2332
Shivani Sharmac4cc8922019-04-18 03:11:172333 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472334}
2335
clamy987a3752018-05-03 17:36:262336bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2337 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172338 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262339 NavigationEntryImpl* entry =
2340 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2341 if (!entry)
2342 return false;
2343
2344 FrameNavigationEntry* frame_entry =
2345 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2346 if (!frame_entry)
2347 return false;
2348
Camille Lamy5193caa2018-10-12 11:59:422349 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572350 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232351 ReloadType::NONE, false /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422352 true /* is_history_navigation_in_new_child */);
clamyea99ea12018-05-28 13:54:232353
2354 if (!request)
2355 return false;
2356
arthursonzognif046d4a2019-12-12 19:08:102357 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412358
Lukasz Anforowicz9ee83c272020-12-01 20:14:052359 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2360 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232361
2362 return true;
clamy987a3752018-05-03 17:36:262363}
2364
Tsuyoshi Horo52fd08e2020-07-07 07:03:452365bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2366 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2367 if (!entry)
2368 return false;
2369 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2370 if (!frame_entry)
2371 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142372 ReloadType reload_type = ReloadType::NORMAL;
2373 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452374 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142375 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452376 false /* is_same_document_history_load */,
2377 false /* is_history_navigation_in_new_child */);
2378 if (!request)
2379 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052380 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452381 return true;
2382}
2383
Dave Tapuska8bfd84c2019-03-26 20:47:162384void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2385 int offset,
2386 int sandbox_frame_tree_node_id) {
2387 if (!CanGoToOffset(offset))
2388 return;
2389 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id);
2390}
2391
clamy987a3752018-05-03 17:36:262392void NavigationControllerImpl::NavigateFromFrameProxy(
2393 RenderFrameHostImpl* render_frame_host,
2394 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022395 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452396 int initiator_process_id,
Anton Bikineevf62d1bf2021-05-15 17:56:072397 const absl::optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262398 bool is_renderer_initiated,
2399 SiteInstance* source_site_instance,
2400 const Referrer& referrer,
2401 ui::PageTransition page_transition,
2402 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252403 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262404 const std::string& method,
2405 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092406 const std::string& extra_headers,
Antonio Sartori2f763d9d2021-04-21 10:04:142407 network::mojom::SourceLocationPtr source_location,
John Delaney50425f82020-04-07 16:26:212408 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Anton Bikineevf62d1bf2021-05-15 17:56:072409 const absl::optional<blink::Impression>& impression) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582410 if (is_renderer_initiated)
2411 DCHECK(initiator_origin.has_value());
2412
clamy987a3752018-05-03 17:36:262413 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582414
Rakina Zata Amni66992a82020-06-24 03:57:522415 // Don't allow an entry replacement if there is no entry to replace.
2416 // http://crbug.com/457149
2417 if (GetEntryCount() == 0)
2418 should_replace_current_entry = false;
2419
clamy987a3752018-05-03 17:36:262420 // Create a NavigationEntry for the transfer, without making it the pending
2421 // entry. Subframe transfers should have a clone of the last committed entry
2422 // with a FrameNavigationEntry for the target frame. Main frame transfers
2423 // should have a new NavigationEntry.
2424 // TODO(creis): Make this unnecessary by creating (and validating) the params
2425 // directly, passing them to the destination RenderFrameHost. See
2426 // https://crbug.com/536906.
2427 std::unique_ptr<NavigationEntryImpl> entry;
2428 if (!node->IsMainFrame()) {
2429 // Subframe case: create FrameNavigationEntry.
2430 if (GetLastCommittedEntry()) {
2431 entry = GetLastCommittedEntry()->Clone();
2432 entry->set_extra_headers(extra_headers);
2433 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2434 // Renderer-initiated navigation that target a remote frame are currently
2435 // classified as browser-initiated when this one has already navigated.
2436 // See https://crbug.com/722251.
2437 } else {
2438 // If there's no last committed entry, create an entry for about:blank
2439 // with a subframe entry for our destination.
2440 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2441 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:062442 GURL(url::kAboutBlankURL), referrer, initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:102443 source_site_instance, page_transition, is_renderer_initiated,
2444 extra_headers, browser_context_,
Hayato Ito303654c2021-06-30 09:07:542445 nullptr /* blob_url_loader_factory */));
clamy987a3752018-05-03 17:36:262446 }
Nate Chapin9f169072021-06-09 19:32:372447 // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter
2448 // to CreateNavigationRequestFromLoadParams(), so while kReplace might
2449 // remove child FrameNavigationEntries (e.g., if this is a cross-process
2450 // same-document navigation), they will still be present in the
2451 // committed NavigationEntry that will be referenced to construct the new
2452 // FrameNavigationEntry tree when this navigation commits.
clamy987a3752018-05-03 17:36:262453 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:082454 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582455 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072456 absl::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202457 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392458 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482459 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori79d549d2021-02-18 12:59:542460 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262461 } else {
2462 // Main frame case.
2463 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102464 url, referrer, initiator_origin, source_site_instance, page_transition,
2465 is_renderer_initiated, extra_headers, browser_context_,
Hayato Ito303654c2021-06-30 09:07:542466 blob_url_loader_factory));
clamy987a3752018-05-03 17:36:262467 entry->root_node()->frame_entry->set_source_site_instance(
2468 static_cast<SiteInstanceImpl*>(source_site_instance));
2469 entry->root_node()->frame_entry->set_method(method);
2470 }
clamy987a3752018-05-03 17:36:262471
Camille Lamy5193caa2018-10-12 11:59:422472 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262473 if (GetLastCommittedEntry() &&
2474 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2475 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422476 override_user_agent = true;
clamy987a3752018-05-03 17:36:262477 }
2478 // TODO(creis): Set user gesture and intent received timestamp on Android.
2479
2480 // We may not have successfully added the FrameNavigationEntry to |entry|
2481 // above (per https://crbug.com/608402), in which case we create it from
2482 // scratch. This works because we do not depend on |frame_entry| being inside
2483 // |entry| during NavigateToEntry. This will go away when we shortcut this
2484 // further in https://crbug.com/536906.
2485 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2486 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452487 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Nate Chapinfbfe5af2021-06-10 17:22:082488 node->unique_name(), -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582489 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072490 absl::nullopt /* origin */, referrer, initiator_origin,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:122491 std::vector<GURL>(), blink::PageState(), method, -1,
2492 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482493 nullptr /* subresource_web_bundle_navigation_info */,
Titouan Rigoudy6ec70402021-02-02 15:42:192494 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262495 }
2496
Camille Lamy5193caa2018-10-12 11:59:422497 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022498 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452499 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292500 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422501 params.source_site_instance = source_site_instance;
2502 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2503 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032504 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422505 params.referrer = referrer;
2506 /* params.redirect_chain: skip */
2507 params.extra_headers = extra_headers;
2508 params.is_renderer_initiated = is_renderer_initiated;
2509 params.override_user_agent = UA_OVERRIDE_INHERIT;
2510 /* params.base_url_for_data_url: skip */
2511 /* params.virtual_url_for_data_url: skip */
2512 /* params.data_url_as_string: skip */
2513 params.post_data = post_body;
2514 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582515 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422516 /* params.frame_name: skip */
2517 // TODO(clamy): See if user gesture should be propagated to this function.
2518 params.has_user_gesture = false;
2519 params.should_clear_history_list = false;
2520 params.started_from_context_menu = false;
2521 /* params.navigation_ui_data: skip */
2522 /* params.input_start: skip */
Minggang Wangf59db47b2021-06-16 01:56:222523 params.was_activated = blink::mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542524 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212525 params.impression = impression;
Camille Lamy5193caa2018-10-12 11:59:422526
2527 std::unique_ptr<NavigationRequest> request =
2528 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032529 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:142530 false /* has_user_gesture */, std::move(source_location),
Scott Violetcf6ea7e2021-06-09 21:09:212531 download_policy, ReloadType::NONE, entry.get(), frame_entry.get());
clamyea99ea12018-05-28 13:54:232532
2533 if (!request)
2534 return;
2535
Arthur Hemery948742762019-09-18 10:06:242536 // At this stage we are proceeding with this navigation. If this was renderer
2537 // initiated with user gesture, we need to make sure we clear up potential
2538 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2539 DiscardNonCommittedEntries();
2540
Lukasz Anforowicz9ee83c272020-12-01 20:14:052541 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262542}
2543
[email protected]d1198fd2012-08-13 22:50:192544void NavigationControllerImpl::SetSessionStorageNamespace(
Aaron Colwellf3b316e2021-03-11 20:17:052545 const StoragePartitionId& partition_id,
[email protected]8ff00d72012-10-23 19:12:212546 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192547 if (!session_storage_namespace)
2548 return;
2549
2550 // We can't overwrite an existing SessionStorage without violating spec.
2551 // Attempts to do so may give a tab access to another tab's session storage
2552 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152553 bool successful_insert =
2554 session_storage_namespace_map_
Aaron Colwellf3b316e2021-03-11 20:17:052555 .insert(std::make_pair(partition_id,
2556 static_cast<SessionStorageNamespaceImpl*>(
2557 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302558 .second;
2559 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
Aaron Colwellb731a0ae2021-03-19 19:14:472560 OnStoragePartitionIdAdded(partition_id);
[email protected]d1198fd2012-08-13 22:50:192561}
2562
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572563bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Aran Gilman37d11632019-10-08 23:07:152564 return IsInitialNavigation() && !GetLastCommittedEntry() &&
Carlos Caballeroede6f8c2021-01-28 11:01:502565 !frame_tree_.has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462566}
2567
Aran Gilman37d11632019-10-08 23:07:152568SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Aaron Colwell78b4bde2021-03-16 16:16:092569 const SiteInfo& site_info) {
2570 // TODO(acolwell): Remove partition_id logic once we have successfully
2571 // migrated the implementation to be a StoragePartitionConfig. At that point
2572 // |site_info| can be replaced with a StoragePartitionConfig.
2573 const StoragePartitionId partition_id =
2574 site_info.GetStoragePartitionId(browser_context_);
2575 const StoragePartitionConfig partition_config =
2576 site_info.GetStoragePartitionConfig(browser_context_);
[email protected]d1198fd2012-08-13 22:50:192577
[email protected]fdac6ade2013-07-20 01:06:302578 StoragePartition* partition =
Lukasz Anforowiczb9a969a2021-04-29 15:26:252579 browser_context_->GetStoragePartition(partition_config);
michaelnbacbcbd2016-02-09 00:32:032580 DOMStorageContextWrapper* context_wrapper =
2581 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2582
2583 SessionStorageNamespaceMap::const_iterator it =
2584 session_storage_namespace_map_.find(partition_id);
2585 if (it != session_storage_namespace_map_.end()) {
2586 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152587 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2588 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:472589
2590 // Verify that the config we generated now matches the one that
2591 // was generated when the namespace was added to the map.
Aaron Colwellb731a0ae2021-03-19 19:14:472592 if (partition_config != it->first.config()) {
2593 LogStoragePartitionIdCrashKeys(it->first, partition_id);
2594 }
Sharon Yangd4f48792021-06-30 17:02:532595 CHECK_EQ(partition_config, it->first.config());
Aaron Colwellb731a0ae2021-03-19 19:14:472596
michaelnbacbcbd2016-02-09 00:32:032597 return it->second.get();
2598 }
2599
2600 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102601 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2602 SessionStorageNamespaceImpl::Create(context_wrapper);
2603 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2604 session_storage_namespace.get();
2605 session_storage_namespace_map_[partition_id] =
2606 std::move(session_storage_namespace);
Aaron Colwellb731a0ae2021-03-19 19:14:472607 OnStoragePartitionIdAdded(partition_id);
[email protected]fdac6ade2013-07-20 01:06:302608
Daniel Murphy31bbb8b12018-02-07 21:44:102609 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302610}
2611
2612SessionStorageNamespace*
2613NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Aaron Colwell78b4bde2021-03-16 16:16:092614 return GetSessionStorageNamespace(SiteInfo());
[email protected]fdac6ade2013-07-20 01:06:302615}
2616
2617const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572618NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302619 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552620}
[email protected]d202a7c2012-01-04 07:53:472621
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572622bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562623 return needs_reload_;
2624}
[email protected]a26023822011-12-29 00:23:552625
[email protected]46bb5e9c2013-10-03 22:16:472626void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412627 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2628}
2629
2630void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472631 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412632 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542633
2634 if (last_committed_entry_index_ != -1) {
2635 entries_[last_committed_entry_index_]->SetTransitionType(
2636 ui::PAGE_TRANSITION_RELOAD);
2637 }
[email protected]46bb5e9c2013-10-03 22:16:472638}
2639
[email protected]d202a7c2012-01-04 07:53:472640void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572641 DCHECK_LT(index, GetEntryCount());
2642 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312643 DiscardNonCommittedEntries();
2644
2645 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122646 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312647 last_committed_entry_index_--;
2648}
2649
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572650NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272651 // If there is no pending_entry_, there should be no pending_entry_index_.
2652 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2653
2654 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362655 // at that index. An exception is while a reload of a post commit error page
2656 // is ongoing; in that case pending entry will point to the entry replaced
2657 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272658 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362659 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2660 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272661
[email protected]022af742011-12-28 18:37:252662 return pending_entry_;
2663}
2664
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572665int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272666 // The pending entry index must always be less than the number of entries.
2667 // If there are no entries, it must be exactly -1.
2668 DCHECK_LT(pending_entry_index_, GetEntryCount());
2669 DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:552670 return pending_entry_index_;
2671}
2672
avi25764702015-06-23 15:43:372673void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572674 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362675 bool replace,
2676 bool was_post_commit_error) {
mastiz7eddb5f72016-06-23 09:52:452677 DCHECK(!ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2678 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202679
avi5cad4912015-06-19 05:25:442680 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2681 // need to keep continuity with the pending entry, so copy the pending entry's
2682 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2683 // then the renderer navigated on its own, independent of the pending entry,
2684 // so don't copy anything.
2685 if (pending_entry_ && pending_entry_index_ == -1)
2686 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202687
arthursonzogni69a6a1b2019-09-17 09:23:002688 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:202689
creisee17e932015-07-17 17:56:222690 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362691 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102692 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572693 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362694 // If the new entry is a post-commit error page, we store the current last
2695 // committed entry to the side so that we can put it back when navigating
2696 // away from the error.
2697 if (was_post_commit_error) {
2698 DCHECK(!entry_replaced_by_post_commit_error_);
2699 entry_replaced_by_post_commit_error_ =
2700 std::move(entries_[last_committed_entry_index_]);
2701 }
dcheng36b6aec92015-12-26 06:16:362702 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222703 return;
2704 }
[email protected]765b35502008-08-21 00:51:202705
creis37979a62015-08-04 19:48:182706 // We shouldn't see replace == true when there's no committed entries.
2707 DCHECK(!replace);
2708
Michael Thiessen9b14d512019-09-23 21:19:472709 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202710
Shivani Sharmad8c8d652019-02-13 17:27:572711 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202712
dcheng36b6aec92015-12-26 06:16:362713 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202714 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292715}
2716
Shivani Sharmad8c8d652019-02-13 17:27:572717void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162718 if (entries_.size() < max_entry_count())
2719 return;
2720
2721 DCHECK_EQ(max_entry_count(), entries_.size());
2722 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572723 CHECK_EQ(pending_entry_index_, -1);
2724
2725 int index = 0;
Elly Fong-Jonesccc6d1f2021-06-14 18:32:422726 // Retrieve the oldest skippable entry.
2727 for (; index < GetEntryCount(); index++) {
2728 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2729 break;
Shivani Sharmad8c8d652019-02-13 17:27:572730 }
2731
2732 // If there is no skippable entry or if it is the last committed entry then
2733 // fall back to pruning the oldest entry. It is not safe to prune the last
2734 // committed entry.
2735 if (index == GetEntryCount() || index == last_committed_entry_index_)
2736 index = 0;
2737
2738 bool should_succeed = RemoveEntryAtIndex(index);
2739 DCHECK_EQ(true, should_succeed);
2740
2741 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252742}
2743
clamy3cb9bea92018-07-10 12:42:022744void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162745 ReloadType reload_type,
2746 int sandboxed_source_frame_tree_node_id) {
Alexander Timin3a92df72019-09-20 11:59:502747 TRACE_EVENT0("navigation",
2748 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272749 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022750 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362751 if (pending_entry_index_ != -1) {
2752 // The pending entry may not be in entries_ if a post-commit error page is
2753 // showing.
2754 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2755 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2756 }
Gyuyoung Kim107c2a02021-04-13 01:49:302757 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572758 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:012759 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:002760 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:572761 int nav_entry_id = pending_entry_->GetUniqueID();
2762
[email protected]83c2e232011-10-07 21:36:462763 // If we were navigating to a slow-to-commit page, and the user performs
2764 // a session history navigation to the last committed page, RenderViewHost
2765 // will force the throbber to start, but WebKit will essentially ignore the
2766 // navigation, and won't send a message to stop the throbber. To prevent this
2767 // from happening, we drop the navigation here and stop the slow-to-commit
2768 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022769 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:082770 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:272771 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
Takashi Toyoshimabf549472021-07-01 07:47:202772 frame_tree_.StopLoading();
[email protected]6a13a6c2011-12-20 21:47:122773
[email protected]83c2e232011-10-07 21:36:462774 DiscardNonCommittedEntries();
2775 return;
2776 }
2777
creisce0ef3572017-01-26 17:53:082778 // Compare FrameNavigationEntries to see which frames in the tree need to be
2779 // navigated.
clamy3cb9bea92018-07-10 12:42:022780 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2781 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Alex Moshchuk3a4e77a2020-05-29 21:32:572782 FindFramesToNavigate(root, reload_type, &same_document_loads,
2783 &different_document_loads);
creis4e2ecb72015-06-20 00:46:302784
2785 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572786 // We were unable to match any frames to navigate. This can happen if a
2787 // history navigation targets a subframe that no longer exists
2788 // (https://crbug.com/705550). In this case, we need to update the current
2789 // history entry to the pending one but keep the main document loaded. We
2790 // also need to ensure that observers are informed about the updated
2791 // current history entry (e.g., for greying out back/forward buttons), and
2792 // that renderer processes update their history offsets. The easiest way
2793 // to do all that is to schedule a "redundant" same-document navigation in
2794 // the main frame.
2795 //
2796 // Note that we don't want to remove this history entry, as it might still
2797 // be valid later, since a frame that it's targeting may be recreated.
2798 //
2799 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
2800 // need to repeat history navigations until finding the one that works.
2801 // Consider changing this behavior to keep looking for the first valid
2802 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:022803 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422804 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572805 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:572806 ReloadType::NONE /* reload_type */,
2807 true /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422808 false /* is_history_navigation_in_new_child */);
clamy3cb9bea92018-07-10 12:42:022809 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572810 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:022811 DiscardPendingEntry(false);
2812 return;
2813 }
Alex Moshchuk3a4e77a2020-05-29 21:32:572814 same_document_loads.push_back(std::move(navigation_request));
2815
2816 // Sanity check that we never take this branch for any kinds of reloads,
2817 // as those should've queued a different-document load in the main frame.
2818 DCHECK(!is_forced_reload);
2819 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:302820 }
2821
Dave Tapuska8bfd84c2019-03-26 20:47:162822 // If |sandboxed_source_frame_node_id| is valid, then track whether this
2823 // navigation affects any frame outside the frame's subtree.
2824 if (sandboxed_source_frame_tree_node_id !=
2825 FrameTreeNode::kFrameTreeNodeInvalidId) {
2826 bool navigates_inside_tree =
2827 DoesSandboxNavigationStayWithinSubtree(
2828 sandboxed_source_frame_tree_node_id, same_document_loads) &&
2829 DoesSandboxNavigationStayWithinSubtree(
2830 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:502831 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:162832 // the number of pages that trigger this.
2833 FrameTreeNode* sandbox_source_frame_tree_node =
2834 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
2835 if (sandbox_source_frame_tree_node) {
2836 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
2837 sandbox_source_frame_tree_node->current_frame_host(),
2838 navigates_inside_tree
2839 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
2840 : blink::mojom::WebFeature::
2841 kSandboxBackForwardAffectsFramesOutsideSubtree);
2842 }
Dave Tapuska855c1e12019-08-23 20:45:522843
2844 // If the navigation occurred outside the tree discard it because
2845 // the sandboxed frame didn't have permission to navigate outside
2846 // its tree. If it is possible that the navigation is both inside and
2847 // outside the frame tree and we discard it entirely because we don't
2848 // want to end up in a history state that didn't exist before.
2849 if (base::FeatureList::IsEnabled(
2850 features::kHistoryPreventSandboxedNavigation) &&
2851 !navigates_inside_tree) {
2852 DiscardPendingEntry(false);
2853 return;
2854 }
Dave Tapuska8bfd84c2019-03-26 20:47:162855 }
2856
Carlos Caballero539a421c2020-07-06 10:25:572857 // BackForwardCache:
2858 // Navigate immediately if the document is in the BackForwardCache.
2859 if (back_forward_cache_.GetEntry(nav_entry_id)) {
2860 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
2861 DCHECK_EQ(reload_type, ReloadType::NONE);
2862 auto navigation_request = CreateNavigationRequestFromEntry(
2863 root, pending_entry_, pending_entry_->GetFrameEntry(root),
2864 ReloadType::NONE, false /* is_same_document_history_load */,
2865 false /* is_history_navigation_in_new_child */);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052866 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:572867
2868 return;
2869 }
2870
2871 // History navigation might try to reuse a specific BrowsingInstance, already
2872 // used by a page in the cache. To avoid having two different main frames that
2873 // live in the same BrowsingInstance, evict the all pages with this
2874 // BrowsingInstance from the cache.
2875 //
2876 // For example, take the following scenario:
2877 //
2878 // A1 = Some page on a.com
2879 // A2 = Some other page on a.com
2880 // B3 = An uncacheable page on b.com
2881 //
2882 // Then the following navigations occur:
2883 // A1->A2->B3->A1
2884 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
2885 // take its place) and A1 will be created in the same BrowsingInstance (and
2886 // SiteInstance), as A2.
2887 //
2888 // If we didn't do anything, both A1 and A2 would remain alive in the same
2889 // BrowsingInstance/SiteInstance, which is unsupported by
2890 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
2891 // A2 from the cache.
2892 if (pending_entry_->site_instance()) {
2893 back_forward_cache_.EvictFramesInRelatedSiteInstances(
2894 pending_entry_->site_instance());
2895 }
2896
clamy3cb9bea92018-07-10 12:42:022897 // This call does not support re-entrancy. See http://crbug.com/347742.
2898 CHECK(!in_navigate_to_pending_entry_);
2899 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:302900
arthursonzogni66f711c2019-10-08 14:40:362901 // It is not possible to delete the pending NavigationEntry while navigating
2902 // to it. Grab a reference to delay potential deletion until the end of this
2903 // function.
2904 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
2905
creis4e2ecb72015-06-20 00:46:302906 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:022907 for (auto& item : same_document_loads) {
2908 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052909 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302910 }
clamy3cb9bea92018-07-10 12:42:022911 for (auto& item : different_document_loads) {
2912 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052913 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302914 }
clamy3cb9bea92018-07-10 12:42:022915
2916 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:302917}
2918
Alex Moshchuk3a4e77a2020-05-29 21:32:572919NavigationControllerImpl::HistoryNavigationAction
2920NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:302921 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:572922 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:422923 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:582924 // Only active and prerendered documents are allowed to navigate in their
2925 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422926 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:012927 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:582928 // - If the document is in pending deletion, the browser already committed
2929 // to destroying this RenderFrameHost. See https://crbug.com/930278.
2930 // - If the document is in back-forward cache, it's not allowed to navigate
2931 // as it should remain frozen. Ignore the request and evict the document
2932 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422933 //
Sreeja Kamishettydb8e2892021-03-10 09:30:582934 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:422935 // which should all be inactive as well.
Sreeja Kamishettydb8e2892021-03-10 09:30:582936 if (render_frame_host->IsInactiveAndDisallowActivation())
Sreeja Kamishetty8eacabb2021-03-09 11:45:422937 return HistoryNavigationAction::kStopLooking;
2938 }
arthursonzogni03f76152019-02-12 10:35:202939
Alex Moshchuk3a4e77a2020-05-29 21:32:572940 // If there's no last committed entry, there is no previous history entry to
2941 // compare against, so fall back to a different-document load. Note that we
2942 // should only reach this case for the root frame and not descend further
2943 // into subframes.
2944 if (!GetLastCommittedEntry()) {
2945 DCHECK(frame->IsMainFrame());
2946 return HistoryNavigationAction::kDifferentDocument;
2947 }
2948
2949 // Reloads should result in a different-document load. Note that reloads may
2950 // also happen via the |needs_reload_| mechanism where the reload_type is
2951 // NONE, so detect this by comparing whether we're going to the same
2952 // entry that we're currently on. Similarly to above, only main frames
2953 // should reach this. Note that subframes support reloads, but that's done
2954 // via a different path that doesn't involve FindFramesToNavigate (see
2955 // RenderFrameHost::Reload()).
2956 if (reload_type != ReloadType::NONE ||
2957 pending_entry_index_ == last_committed_entry_index_) {
2958 DCHECK(frame->IsMainFrame());
2959 return HistoryNavigationAction::kDifferentDocument;
2960 }
2961
Alex Moshchuk47d1a4bd2020-06-01 22:15:342962 // If there is no new FrameNavigationEntry for the frame, ignore the
2963 // load. For example, this may happen when going back to an entry before a
2964 // frame was created. Suppose we commit a same-document navigation that also
2965 // results in adding a new subframe somewhere in the tree. If we go back,
2966 // the new subframe will be missing a FrameNavigationEntry in the previous
2967 // NavigationEntry, but we shouldn't delete or change what's loaded in
2968 // it.
2969 //
Alex Moshchuke65c39272020-06-03 17:55:372970 // Note that in this case, there is no need to keep looking for navigations
2971 // in subframes, which would be missing FrameNavigationEntries as well.
2972 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:342973 // It's important to check this before checking |old_item| below, since both
2974 // might be null, and in that case we still shouldn't change what's loaded in
2975 // this frame. Note that scheduling any loads assumes that |new_item| is
2976 // non-null. See https://crbug.com/1088354.
2977 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
2978 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:372979 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:342980
Alex Moshchuk3a4e77a2020-05-29 21:32:572981 // If there is no old FrameNavigationEntry, schedule a different-document
2982 // load.
2983 //
creis225a7432016-06-03 22:56:272984 // TODO(creis): Store the last committed FrameNavigationEntry to use here,
2985 // rather than assuming the NavigationEntry has up to date info on subframes.
creis4e2ecb72015-06-20 00:46:302986 FrameNavigationEntry* old_item =
2987 GetLastCommittedEntry()->GetFrameEntry(frame);
Alex Moshchuk3a4e77a2020-05-29 21:32:572988 if (!old_item)
2989 return HistoryNavigationAction::kDifferentDocument;
2990
Alex Moshchuk3a4e77a2020-05-29 21:32:572991 // If the new item is not in the same SiteInstance, schedule a
2992 // different-document load. Newly restored items may not have a SiteInstance
2993 // yet, in which case it will be assigned on first commit.
2994 if (new_item->site_instance() &&
2995 new_item->site_instance() != old_item->site_instance())
2996 return HistoryNavigationAction::kDifferentDocument;
2997
2998 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:312999 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
3000 // that we do this even if the history navigation would not be modifying this
3001 // frame were it live.
3002 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:573003 return HistoryNavigationAction::kDifferentDocument;
3004
3005 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:313006 // Starting a navigation after a crash early-promotes the speculative
3007 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
3008 // committed yet, so we can not possibly perform a same-document history
3009 // navigation. The frame would need to be reloaded with a cross-document
3010 // navigation.
3011 if (!frame->current_frame_host()->has_committed_any_navigation())
3012 return HistoryNavigationAction::kDifferentDocument;
3013
creis54131692016-08-12 18:32:253014 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:493015 // sequence number but different item sequence number.
3016 if (new_item->document_sequence_number() ==
3017 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573018 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493019 }
avib48cb312016-05-05 21:35:003020
Alex Moshchuk3a4e77a2020-05-29 21:32:573021 // Otherwise, if both item and document sequence numbers differ, this
3022 // should be a different document load.
3023 return HistoryNavigationAction::kDifferentDocument;
3024 }
3025
3026 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373027 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573028 DCHECK_EQ(new_item->document_sequence_number(),
3029 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373030 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573031}
3032
3033void NavigationControllerImpl::FindFramesToNavigate(
3034 FrameTreeNode* frame,
3035 ReloadType reload_type,
3036 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3037 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3038 DCHECK(pending_entry_);
3039 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3040
3041 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3042
3043 if (action == HistoryNavigationAction::kSameDocument) {
3044 std::unique_ptr<NavigationRequest> navigation_request =
3045 CreateNavigationRequestFromEntry(
3046 frame, pending_entry_, new_item, reload_type,
3047 true /* is_same_document_history_load */,
3048 false /* is_history_navigation_in_new_child */);
3049 if (navigation_request) {
3050 // Only add the request if was properly created. It's possible for the
3051 // creation to fail in certain cases, e.g. when the URL is invalid.
3052 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303053 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573054 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473055 std::unique_ptr<NavigationRequest> navigation_request =
3056 CreateNavigationRequestFromEntry(
3057 frame, pending_entry_, new_item, reload_type,
3058 false /* is_same_document_history_load */,
3059 false /* is_history_navigation_in_new_child */);
3060 if (navigation_request) {
3061 // Only add the request if was properly created. It's possible for the
3062 // creation to fail in certain cases, e.g. when the URL is invalid.
3063 different_document_loads->push_back(std::move(navigation_request));
3064 }
3065 // For a different document, the subframes will be destroyed, so there's
3066 // no need to consider them.
3067 return;
Alex Moshchuke65c39272020-06-03 17:55:373068 } else if (action == HistoryNavigationAction::kStopLooking) {
3069 return;
creis4e2ecb72015-06-20 00:46:303070 }
3071
3072 for (size_t i = 0; i < frame->child_count(); i++) {
clamy3cb9bea92018-07-10 12:42:023073 FindFramesToNavigate(frame->child_at(i), reload_type, same_document_loads,
creis4e2ecb72015-06-20 00:46:303074 different_document_loads);
3075 }
3076}
3077
Harkiran Bolariaba823e42021-05-21 18:30:363078base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry(
clamy21718cc22018-06-13 13:34:243079 const LoadURLParams& params) {
3080 // Find the appropriate FrameTreeNode.
3081 FrameTreeNode* node = nullptr;
3082 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3083 !params.frame_name.empty()) {
3084 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003085 ? frame_tree_.FindByID(params.frame_tree_node_id)
3086 : frame_tree_.FindByName(params.frame_name);
Hayato Ito7a80db42021-07-05 06:18:543087 DCHECK(!node || node->frame_tree() == &frame_tree_);
clamy21718cc22018-06-13 13:34:243088 }
3089
3090 // If no FrameTreeNode was specified, navigate the main frame.
3091 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003092 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243093
Camille Lamy5193caa2018-10-12 11:59:423094 // Compute overrides to the LoadURLParams for |override_user_agent|,
3095 // |should_replace_current_entry| and |has_user_gesture| that will be used
3096 // both in the creation of the NavigationEntry and the NavigationRequest.
3097 // Ideally, the LoadURLParams themselves would be updated, but since they are
3098 // passed as a const reference, this is not possible.
3099 // TODO(clamy): When we only create a NavigationRequest, move this to
3100 // CreateNavigationRequestFromLoadURLParams.
3101 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3102 GetLastCommittedEntry());
3103
3104 // Don't allow an entry replacement if there is no entry to replace.
3105 // http://crbug.com/457149
Hayato Ito7a80db42021-07-05 06:18:543106 //
3107 // If there is an entry, an entry replacement must happen if the current
3108 // browsing context should maintain a trivial session history.
3109 bool should_replace_current_entry = (params.should_replace_current_entry ||
3110 ShouldMaintainTrivialSessionHistory()) &&
3111 entries_.size();
Camille Lamy5193caa2018-10-12 11:59:423112
clamy21718cc22018-06-13 13:34:243113 // Javascript URLs should not create NavigationEntries. All other navigations
3114 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243115 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373116 std::unique_ptr<NavigationEntryImpl> entry =
3117 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3118 should_replace_current_entry,
3119 params.has_user_gesture);
clamy21718cc22018-06-13 13:34:243120 DiscardPendingEntry(false);
3121 SetPendingEntry(std::move(entry));
3122 }
3123
3124 // Renderer-debug URLs are sent to the renderer process immediately for
3125 // processing and don't need to create a NavigationRequest.
3126 // Note: this includes navigations to JavaScript URLs, which are considered
3127 // renderer-debug URLs.
3128 // Note: we intentionally leave the pending entry in place for renderer debug
3129 // URLs, unlike the cases below where we clear it if the navigation doesn't
3130 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303131 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483132 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3133 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153134 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483135 params.url, browser_context_)) {
3136 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363137 return nullptr;
Oleg Davydov2cc0167b2019-02-05 14:32:483138 }
3139
clamy21718cc22018-06-13 13:34:243140 HandleRendererDebugURL(node, params.url);
Harkiran Bolariaba823e42021-05-21 18:30:363141 return nullptr;
clamy21718cc22018-06-13 13:34:243142 }
3143
Antonio Sartori78a749f2020-11-30 12:03:393144 DCHECK(pending_entry_);
3145
clamy21718cc22018-06-13 13:34:243146 // Convert navigations to the current URL to a reload.
3147 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3148 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3149 // DevTools sometimes issues navigations to main frames that they do not
3150 // expect to see treated as reload, and it only works because they pass a
3151 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3152 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543153 ReloadType reload_type = params.reload_type;
3154 if (reload_type == ReloadType::NONE &&
3155 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043156 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243157 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243158 params.load_type ==
3159 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
Hayato Ito7a80db42021-07-05 06:18:543160 should_replace_current_entry, GetLastCommittedEntry())) {
clamy21718cc22018-06-13 13:34:243161 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473162 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393163
3164 // If this is a reload of an existing FrameNavigationEntry and we had a
3165 // policy container for it, then we should copy it into the pending entry,
3166 // so that it is copied to the navigation request in
3167 // CreateNavigationRequestFromLoadParams later.
3168 if (GetLastCommittedEntry()) {
3169 FrameNavigationEntry* previous_frame_entry =
3170 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193171 if (previous_frame_entry &&
3172 previous_frame_entry->policy_container_policies()) {
3173 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163174 previous_frame_entry->policy_container_policies()->Clone());
Antonio Sartori78a749f2020-11-30 12:03:393175 }
3176 }
3177 }
3178
3179 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
3180 // document (so no last commit), then we should copy the document polices from
3181 // RenderFrameHost's PolicyContainerHost. The NavigationRequest will create a
3182 // new PolicyContainerHost with the document policies from the
3183 // |pending_entry_|, and that PolicyContainerHost will be put in the final
3184 // RenderFrameHost for the navigation. This way, we ensure that we keep
3185 // enforcing the right policies on the initial empty document after the
3186 // reload.
3187 if (!GetLastCommittedEntry() && params.url.IsAboutBlank()) {
3188 if (node->current_frame_host() &&
3189 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193190 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163191 node->current_frame_host()
3192 ->policy_container_host()
3193 ->policies()
3194 .Clone());
Antonio Sartori78a749f2020-11-30 12:03:393195 }
clamy21718cc22018-06-13 13:34:243196 }
3197
3198 // navigation_ui_data should only be present for main frame navigations.
3199 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
3200
Camille Lamy5193caa2018-10-12 11:59:423201 std::unique_ptr<NavigationRequest> request =
3202 CreateNavigationRequestFromLoadParams(
3203 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143204 params.has_user_gesture, network::mojom::SourceLocation::New(),
3205 blink::NavigationDownloadPolicy(), reload_type, pending_entry_,
Scott Violetcf6ea7e2021-06-09 21:09:213206 pending_entry_->GetFrameEntry(node));
clamy21718cc22018-06-13 13:34:243207
3208 // If the navigation couldn't start, return immediately and discard the
3209 // pending NavigationEntry.
3210 if (!request) {
3211 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363212 return nullptr;
clamy21718cc22018-06-13 13:34:243213 }
3214
Camille Lamy5193caa2018-10-12 11:59:423215#if DCHECK_IS_ON()
3216 // Safety check that NavigationRequest and NavigationEntry match.
3217 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3218#endif
3219
clamy21718cc22018-06-13 13:34:243220 // This call does not support re-entrancy. See http://crbug.com/347742.
3221 CHECK(!in_navigate_to_pending_entry_);
3222 in_navigate_to_pending_entry_ = true;
3223
arthursonzogni66f711c2019-10-08 14:40:363224 // It is not possible to delete the pending NavigationEntry while navigating
3225 // to it. Grab a reference to delay potential deletion until the end of this
3226 // function.
3227 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3228
Harkiran Bolariaba823e42021-05-21 18:30:363229 base::WeakPtr<NavigationHandle> created_navigation_handle(
3230 request->GetWeakPtr());
Lukasz Anforowicz9ee83c272020-12-01 20:14:053231 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243232
3233 in_navigate_to_pending_entry_ = false;
Harkiran Bolariaba823e42021-05-21 18:30:363234 return created_navigation_handle;
clamy21718cc22018-06-13 13:34:243235}
3236
clamyea99ea12018-05-28 13:54:233237void NavigationControllerImpl::HandleRendererDebugURL(
3238 FrameTreeNode* frame_tree_node,
3239 const GURL& url) {
3240 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243241 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3242 // the renderer process is not live, unless it is the initial navigation
3243 // of the tab.
clamyea99ea12018-05-28 13:54:233244 if (!IsInitialNavigation()) {
3245 DiscardNonCommittedEntries();
3246 return;
3247 }
Fergal Dalyecd3b0202020-06-25 01:57:373248 // The current frame is always a main frame. If IsInitialNavigation() is
3249 // true then there have been no navigations and any frames of this tab must
3250 // be in the same renderer process. If that has crashed then the only frame
3251 // that can be revived is the main frame.
3252 frame_tree_node->render_manager()
3253 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233254 }
3255 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3256}
3257
clamy21718cc22018-06-13 13:34:243258std::unique_ptr<NavigationEntryImpl>
3259NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3260 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423261 const LoadURLParams& params,
3262 bool override_user_agent,
3263 bool should_replace_current_entry,
3264 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393265 // Browser initiated navigations might not have a blob_url_loader_factory set
3266 // in params even if the navigation is to a blob URL. If that happens, lookup
3267 // the correct url loader factory to use here.
3268 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483269 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473270 // Resolve the blob URL in the storage partition associated with the target
3271 // frame. This is the storage partition the URL will be loaded in, and only
3272 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393273 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473274 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393275 }
3276
clamy21718cc22018-06-13 13:34:243277 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443278 // extra_headers in params are \n separated; navigation entries want \r\n.
3279 std::string extra_headers_crlf;
3280 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243281
3282 // For subframes, create a pending entry with a corresponding frame entry.
3283 if (!node->IsMainFrame()) {
Tommy C. Li03eee77a2019-02-05 02:07:443284 if (GetLastCommittedEntry()) {
Tommy C. Li03eee77a2019-02-05 02:07:443285 entry = GetLastCommittedEntry()->Clone();
3286 } else {
3287 // If there's no last committed entry, create an entry for about:blank
3288 // with a subframe entry for our destination.
3289 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
3290 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063291 GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103292 params.source_site_instance.get(), params.transition_type,
3293 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Hayato Ito303654c2021-06-30 09:07:543294 blob_url_loader_factory));
Tommy C. Li03eee77a2019-02-05 02:07:443295 }
Nasko Oskov18006bc2018-12-06 02:53:583296
clamy21718cc22018-06-13 13:34:243297 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:083298 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
clamy21718cc22018-06-13 13:34:243299 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Anton Bikineevf62d1bf2021-05-15 17:56:073300 params.url, absl::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203301 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393302 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:483303 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori78a749f2020-11-30 12:03:393304 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163305 // a conversion of a new navigation into a reload, we will set the right
3306 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193307 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243308 } else {
3309 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243310 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063311 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103312 params.source_site_instance.get(), params.transition_type,
3313 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Hayato Ito303654c2021-06-30 09:07:543314 blob_url_loader_factory));
clamy21718cc22018-06-13 13:34:243315 entry->set_source_site_instance(
3316 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3317 entry->SetRedirectChain(params.redirect_chain);
3318 }
3319
3320 // Set the FTN ID (only used in non-site-per-process, for tests).
3321 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243322 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423323 entry->SetIsOverridingUserAgent(override_user_agent);
3324 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543325 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243326
clamy21718cc22018-06-13 13:34:243327 switch (params.load_type) {
3328 case LOAD_TYPE_DEFAULT:
3329 break;
3330 case LOAD_TYPE_HTTP_POST:
3331 entry->SetHasPostData(true);
3332 entry->SetPostData(params.post_data);
3333 break;
3334 case LOAD_TYPE_DATA:
3335 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3336 entry->SetVirtualURL(params.virtual_url_for_data_url);
3337#if defined(OS_ANDROID)
3338 entry->SetDataURLAsString(params.data_url_as_string);
3339#endif
3340 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3341 break;
clamy21718cc22018-06-13 13:34:243342 }
3343
3344 // TODO(clamy): NavigationEntry is meant for information that will be kept
3345 // after the navigation ended and therefore is not appropriate for
3346 // started_from_context_menu. Move started_from_context_menu to
3347 // NavigationUIData.
3348 entry->set_started_from_context_menu(params.started_from_context_menu);
3349
3350 return entry;
3351}
3352
clamyea99ea12018-05-28 13:54:233353std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423354NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3355 FrameTreeNode* node,
3356 const LoadURLParams& params,
3357 bool override_user_agent,
3358 bool should_replace_current_entry,
3359 bool has_user_gesture,
Antonio Sartori2f763d9d2021-04-21 10:04:143360 network::mojom::SourceLocationPtr source_location,
Yeunjoo Choi3df791a2021-02-17 07:07:253361 blink::NavigationDownloadPolicy download_policy,
Camille Lamy5193caa2018-10-12 11:59:423362 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573363 NavigationEntryImpl* entry,
Scott Violetcf6ea7e2021-06-09 21:09:213364 FrameNavigationEntry* frame_entry) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573365 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283366 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533367 // All renderer-initiated navigations must have an initiator_origin.
3368 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513369
Camille Lamy5193caa2018-10-12 11:59:423370 GURL url_to_load;
3371 GURL virtual_url;
Anton Bikineevf62d1bf2021-05-15 17:56:073372 absl::optional<url::Origin> origin_to_commit =
3373 frame_entry ? frame_entry->committed_origin() : absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:323374
Camille Lamy2baa8022018-10-19 16:43:173375 // For main frames, rewrite the URL if necessary and compute the virtual URL
3376 // that should be shown in the address bar.
3377 if (node->IsMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423378 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173379 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423380 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423381
Camille Lamy2baa8022018-10-19 16:43:173382 // For DATA loads, override the virtual URL.
3383 if (params.load_type == LOAD_TYPE_DATA)
3384 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423385
Camille Lamy2baa8022018-10-19 16:43:173386 if (virtual_url.is_empty())
3387 virtual_url = url_to_load;
3388
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573389 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283390
Aran Gilman249eb122019-12-02 23:32:463391 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3392 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3393 // return different results, leading to a different URL in the
3394 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3395 // pending NavigationEntry, as we'll only make one call to
3396 // RewriteUrlForNavigation.
3397 VLOG_IF(1, (url_to_load != frame_entry->url()))
3398 << "NavigationRequest and FrameEntry have different URLs: "
3399 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283400
Camille Lamy2baa8022018-10-19 16:43:173401 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423402 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173403 // NavigationRequest.
3404 } else {
3405 url_to_load = params.url;
3406 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243407 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173408 }
Camille Lamy5193caa2018-10-12 11:59:423409
Ehsan Karamad44fc72112019-02-26 18:15:473410 if (node->render_manager()->is_attaching_inner_delegate()) {
3411 // Avoid starting any new navigations since this node is now preparing for
3412 // attaching an inner delegate.
3413 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203414 }
Camille Lamy5193caa2018-10-12 11:59:423415
Camille Lamy5193caa2018-10-12 11:59:423416 if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load))
3417 return nullptr;
3418
Kunihiko Sakamoto346a74e2021-03-10 08:57:483419 if (!DoesURLMatchOriginForNavigation(
3420 url_to_load, origin_to_commit,
3421 frame_entry->subresource_web_bundle_navigation_info())) {
Nasko Oskov03912102019-01-11 00:21:323422 DCHECK(false) << " url:" << url_to_load
3423 << " origin:" << origin_to_commit.value();
3424 return nullptr;
3425 }
3426
Camille Lamy5193caa2018-10-12 11:59:423427 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403428 blink::PreviewsState previews_state =
3429 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
Camille Lamy5193caa2018-10-12 11:59:423430 if (!node->IsMainFrame()) {
3431 // For subframes, use the state of the top-level frame.
3432 previews_state = node->frame_tree()
3433 ->root()
3434 ->current_frame_host()
3435 ->last_navigation_previews_state();
3436 }
3437
Camille Lamy5193caa2018-10-12 11:59:423438 // This will be used to set the Navigation Timing API navigationStart
3439 // parameter for browser navigations in new tabs (intents, tabs opened through
3440 // "Open link in new tab"). If the navigation must wait on the current
3441 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3442 // will be updated when the BeforeUnload ack is received.
3443 base::TimeTicks navigation_start = base::TimeTicks::Now();
3444
danakjd83d706d2020-11-25 22:11:123445 // Look for a pending commit that is to another document in this
3446 // FrameTreeNode. If one exists, then the last committed URL will not be the
3447 // current URL by the time this navigation commits.
3448 bool has_pending_cross_document_commit =
3449 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493450 bool is_currently_error_page = node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123451
Minggang Wangb9f3fa92021-07-01 15:30:313452 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjb952ef12021-01-14 19:58:493453 /*old_url=*/node->current_url(),
3454 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3455 has_pending_cross_document_commit, is_currently_error_page,
3456 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423457
3458 // Create the NavigationParams based on |params|.
3459
Hiroki Nakagawa4ed61282021-06-18 05:37:233460 bool is_view_source_mode = entry->IsViewSourceMode();
3461 DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme));
Charlie Harrison8c113a32019-01-07 16:08:293462
Hiroki Nakagawa4ed61282021-06-18 05:37:233463 // Update |download_policy| if the virtual URL is view-source.
Charlie Harrison8c113a32019-01-07 16:08:293464 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253465 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293466
Camille Lamy5193caa2018-10-12 11:59:423467 const GURL& history_url_for_data_url =
3468 params.base_url_for_data_url.is_empty() ? GURL() : virtual_url;
Minggang Wangb9f3fa92021-07-01 15:30:313469 blink::mojom::CommonNavigationParamsPtr common_params =
3470 blink::mojom::CommonNavigationParams::New(
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513471 url_to_load, params.initiator_origin,
3472 blink::mojom::Referrer::New(params.referrer.url,
3473 params.referrer.policy),
Scott Violetcf6ea7e2021-06-09 21:09:213474 params.transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513475 should_replace_current_entry, params.base_url_for_data_url,
3476 history_url_for_data_url, previews_state, navigation_start,
3477 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
Antonio Sartori2f763d9d2021-04-21 10:04:143478 params.post_data, std::move(source_location),
arthursonzogniaf7c62c52020-02-12 10:49:413479 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273480 false /* has_text_fragment_token */,
3481 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3482 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513483 false /* is_history_navigation_in_new_child_frame */,
Charlie Hu5ffc0152019-12-06 15:59:533484 params.input_start);
Camille Lamy5193caa2018-10-12 11:59:423485
Minggang Wangb9f3fa92021-07-01 15:30:313486 blink::mojom::CommitNavigationParamsPtr commit_params =
3487 blink::mojom::CommitNavigationParams::New(
arthursonzognid5a8d0b2021-03-11 17:36:433488 frame_entry->committed_origin(), network::mojom::WebSandboxFlags(),
3489 override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323490 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143491 std::vector<net::RedirectInfo>(),
3492 std::string() /* post_content_type */, common_params->url,
3493 common_params->method, params.can_load_local_resources,
Minggang Wangb9f3fa92021-07-01 15:30:313494 frame_entry->page_state().ToEncodedData(), entry->GetUniqueID(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143495 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3496 -1 /* pending_history_list_offset */,
3497 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3498 params.should_clear_history_list ? 0 : GetEntryCount(),
3499 false /* was_discarded */, is_view_source_mode,
Minggang Wangb9f3fa92021-07-01 15:30:313500 params.should_clear_history_list,
3501 blink::mojom::NavigationTiming::New(),
Anton Bikineevf62d1bf2021-05-15 17:56:073502 absl::nullopt /* appcache_host_id */,
Minggang Wangf59db47b2021-06-16 01:56:223503 blink::mojom::WasActivatedOption::kUnknown,
Lucas Furukawa Gadania9c45682019-07-31 22:05:143504 base::UnguessableToken::Create() /* navigation_token */,
Minggang Wang7ee0c742021-06-16 16:16:513505 std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143506#if defined(OS_ANDROID)
3507 std::string(), /* data_url_as_string */
3508#endif
arthursonzogni14379782020-05-15 09:09:273509 !params.is_renderer_initiated, /* is_browser_initiated */
Sreeja Kamishetty46f762c2021-02-05 07:52:463510 node->frame_tree()->is_prerendering() /* is_prerendering */,
Tsuyoshi Horoe86d7702019-11-29 01:52:473511 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533512 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173513 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073514 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163515 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333516 false /* origin_agent_cluster */,
Maks Orlovichc66745a2020-06-30 17:40:023517 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213518 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Nate Chapind1fe3612021-04-16 20:45:573519 false /* is_cross_browsing_instance */, nullptr /* old_page_info */,
3520 -1 /* http_response_code */,
Minggang Wangb9f3fa92021-07-01 15:30:313521 std::vector<blink::mojom::
3522 AppHistoryEntryPtr>() /* app_history_back_entries */,
Nate Chapind1fe3612021-04-16 20:45:573523 std::vector<
Minggang Wangb9f3fa92021-07-01 15:30:313524 blink::mojom::
3525 AppHistoryEntryPtr>() /* app_history_forward_entries */,
Kenichi Ishibashi17ecfb62021-06-21 03:31:153526 std::vector<GURL>() /* early_hints_preloaded_resources */);
Camille Lamy5193caa2018-10-12 11:59:423527#if defined(OS_ANDROID)
3528 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143529 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423530 }
3531#endif
3532
Lucas Furukawa Gadania9c45682019-07-31 22:05:143533 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423534
3535 // A form submission may happen here if the navigation is a renderer-initiated
3536 // form submission that took the OpenURL path.
3537 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3538
3539 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3540 std::string extra_headers_crlf;
3541 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093542
3543 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143544 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083545 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453546 params.initiator_frame_token.has_value()
3547 ? &(params.initiator_frame_token.value())
3548 : nullptr,
3549 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3550 request_body,
John Delaney50425f82020-04-07 16:26:213551 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
3552 params.impression);
Yao Xiaodc5ed102019-06-04 19:19:093553 navigation_request->set_from_download_cross_origin_redirect(
3554 params.from_download_cross_origin_redirect);
3555 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423556}
3557
3558std::unique_ptr<NavigationRequest>
3559NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233560 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573561 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233562 FrameNavigationEntry* frame_entry,
3563 ReloadType reload_type,
3564 bool is_same_document_history_load,
Arthur Hemerybee4a752019-05-29 10:50:553565 bool is_history_navigation_in_new_child_frame) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343566 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233567 GURL dest_url = frame_entry->url();
Anton Bikineevf62d1bf2021-05-15 17:56:073568 absl::optional<url::Origin> origin_to_commit =
Nasko Oskov03912102019-01-11 00:21:323569 frame_entry->committed_origin();
3570
clamyea99ea12018-05-28 13:54:233571 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013572 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573573 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233574 // We may have been redirected when navigating to the current URL.
3575 // Use the URL the user originally intended to visit as signaled by the
3576 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013577 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573578 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233579 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323580 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233581 }
3582
Ehsan Karamad44fc72112019-02-26 18:15:473583 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3584 // Avoid starting any new navigations since this node is now preparing for
3585 // attaching an inner delegate.
3586 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203587 }
3588
Camille Lamy5193caa2018-10-12 11:59:423589 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573590 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233591 return nullptr;
3592 }
3593
Kunihiko Sakamoto346a74e2021-03-10 08:57:483594 if (!DoesURLMatchOriginForNavigation(
3595 dest_url, origin_to_commit,
3596 frame_entry->subresource_web_bundle_navigation_info())) {
3597 DCHECK(false) << " url:" << dest_url
3598 << " origin:" << origin_to_commit.value();
Nasko Oskov03912102019-01-11 00:21:323599 return nullptr;
3600 }
3601
clamyea99ea12018-05-28 13:54:233602 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403603 blink::PreviewsState previews_state =
3604 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
clamyea99ea12018-05-28 13:54:233605 if (!frame_tree_node->IsMainFrame()) {
3606 // For subframes, use the state of the top-level frame.
3607 previews_state = frame_tree_node->frame_tree()
3608 ->root()
3609 ->current_frame_host()
3610 ->last_navigation_previews_state();
clamyea99ea12018-05-28 13:54:233611 }
3612
clamyea99ea12018-05-28 13:54:233613 // This will be used to set the Navigation Timing API navigationStart
3614 // parameter for browser navigations in new tabs (intents, tabs opened through
3615 // "Open link in new tab"). If the navigation must wait on the current
3616 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3617 // will be updated when the BeforeUnload ack is received.
3618 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233619
danakjd83d706d2020-11-25 22:11:123620 // Look for a pending commit that is to another document in this
3621 // FrameTreeNode. If one exists, then the last committed URL will not be the
3622 // current URL by the time this navigation commits.
3623 bool has_pending_cross_document_commit =
3624 frame_tree_node->render_manager()
3625 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493626 bool is_currently_error_page =
3627 frame_tree_node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123628
Minggang Wangb9f3fa92021-07-01 15:30:313629 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123630 /*old_url=*/frame_tree_node->current_url(),
3631 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493632 has_pending_cross_document_commit, is_currently_error_page,
3633 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423634
3635 // A form submission may happen here if the navigation is a
3636 // back/forward/reload navigation that does a form resubmission.
3637 scoped_refptr<network::ResourceRequestBody> request_body;
3638 std::string post_content_type;
3639 if (frame_entry->method() == "POST") {
3640 request_body = frame_entry->GetPostData(&post_content_type);
3641 // Might have a LF at end.
Peter Kastingb53b81912021-04-28 19:23:303642 post_content_type = std::string(
3643 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL));
Camille Lamy5193caa2018-10-12 11:59:423644 }
3645
3646 // Create the NavigationParams based on |entry| and |frame_entry|.
Minggang Wangb9f3fa92021-07-01 15:30:313647 blink::mojom::CommonNavigationParamsPtr common_params =
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513648 entry->ConstructCommonNavigationParams(
3649 *frame_entry, request_body, dest_url,
3650 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
3651 navigation_type, previews_state, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533652 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513653 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553654 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423655
3656 // TODO(clamy): |intended_as_new_entry| below should always be false once
3657 // Reload no longer leads to this being called for a pending NavigationEntry
3658 // of index -1.
Minggang Wangb9f3fa92021-07-01 15:30:313659 blink::mojom::CommitNavigationParamsPtr commit_params =
Lucas Furukawa Gadania9c45682019-07-31 22:05:143660 entry->ConstructCommitNavigationParams(
3661 *frame_entry, common_params->url, origin_to_commit,
3662 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3663 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533664 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3665 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143666 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423667
clamyea99ea12018-05-28 13:54:233668 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143669 frame_tree_node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083670 !entry->is_renderer_initiated(), false /* was_opener_suppressed */,
3671 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453672 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3673 entry->extra_headers(), frame_entry, entry, request_body,
Anton Bikineevf62d1bf2021-05-15 17:56:073674 nullptr /* navigation_ui_data */, absl::nullopt /* impression */);
clamyea99ea12018-05-28 13:54:233675}
3676
[email protected]d202a7c2012-01-04 07:53:473677void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213678 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273679 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403680
[email protected]2db9bd72012-04-13 20:20:563681 // We need to notify the ssl_manager_ before the web_contents_ so the
[email protected]df1af242009-05-01 00:11:403682 // location bar will have up-to-date information about the security style
3683 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133684 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403685
[email protected]7f924832014-08-09 05:57:223686 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:573687 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463688
[email protected]b0f724c2013-09-05 04:21:133689 // TODO(avi): Remove. http://crbug.com/170921
3690 NotificationDetails notification_details =
3691 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153692 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3693 Source<NavigationController>(this),
3694 notification_details);
initial.commit09911bf2008-07-26 23:55:293695}
3696
initial.commit09911bf2008-07-26 23:55:293697// static
[email protected]d202a7c2012-01-04 07:53:473698size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233699 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153700 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:213701 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233702}
3703
[email protected]d202a7c2012-01-04 07:53:473704void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223705 if (is_active && needs_reload_)
3706 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293707}
3708
[email protected]d202a7c2012-01-04 07:53:473709void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293710 if (!needs_reload_)
3711 return;
3712
Bo Liucdfa4b12018-11-06 00:21:443713 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3714 needs_reload_type_);
3715
initial.commit09911bf2008-07-26 23:55:293716 // Calling Reload() results in ignoring state, and not loading.
3717 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3718 // cached state.
avicc872d7242015-08-19 21:26:343719 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163720 NavigateToExistingPendingEntry(ReloadType::NONE,
3721 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343722 } else if (last_committed_entry_index_ != -1) {
arthursonzogni5c4c202d2017-04-25 23:41:273723 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343724 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163725 NavigateToExistingPendingEntry(ReloadType::NONE,
3726 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343727 } else {
3728 // If there is something to reload, the successful reload will clear the
3729 // |needs_reload_| flag. Otherwise, just do it here.
3730 needs_reload_ = false;
3731 }
initial.commit09911bf2008-07-26 23:55:293732}
3733
Carlos IL42b416592019-10-07 23:10:363734void NavigationControllerImpl::LoadPostCommitErrorPage(
3735 RenderFrameHost* render_frame_host,
3736 const GURL& url,
3737 const std::string& error_page_html,
3738 net::Error error) {
Rakina Zata Amni919b7922020-12-11 09:03:133739 RenderFrameHostImpl* rfhi =
3740 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:583741
3742 // Only active documents can load post-commit error pages:
3743 // - If the document is in pending deletion, the browser already committed to
3744 // destroying this RenderFrameHost so ignore loading the error page.
3745 // - If the document is in back-forward cache, it's not allowed to navigate as
3746 // it should remain frozen. Ignore the request and evict the document from
3747 // back-forward cache.
3748 // - If the document is prerendering, it can navigate but when loading error
3749 // pages, cancel prerendering.
3750 if (rfhi->IsInactiveAndDisallowActivation())
3751 return;
3752
Rakina Zata Amni919b7922020-12-11 09:03:133753 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:413754
Minggang Wangb9f3fa92021-07-01 15:30:313755 blink::mojom::CommonNavigationParamsPtr common_params =
Minggang Wanga13c796e2021-07-02 05:54:433756 blink::CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:593757 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
3758 // the frame actually committed (e.g. iframe with "src" set to a
3759 // slow-responding URL). We should rewrite the URL to about:blank in this
3760 // case, as the renderer will only think a page is an error page if it has a
3761 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:133762 common_params->url = url.is_empty() ? GURL("about:blank") : url;
Minggang Wangb9f3fa92021-07-01 15:30:313763 blink::mojom::CommitNavigationParamsPtr commit_params =
Minggang Wanga13c796e2021-07-02 05:54:433764 blink::CreateCommitNavigationParams();
Antonio Sartori58591c892021-04-21 06:54:333765 commit_params->original_url = common_params->url;
John Delaney131ad362019-08-08 21:57:413766
arthursonzogni70ac7302020-05-28 08:49:053767 // Error pages have a fully permissive FramePolicy.
3768 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
3769 // error pages.
3770 commit_params->frame_policy = blink::FramePolicy();
3771
John Delaney131ad362019-08-08 21:57:413772 std::unique_ptr<NavigationRequest> navigation_request =
3773 NavigationRequest::CreateBrowserInitiated(
3774 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083775 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:193776 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453777 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:063778 "" /* extra_headers */, nullptr /* frame_entry */,
3779 nullptr /* entry */, nullptr /* post_body */,
Anton Bikineevf62d1bf2021-05-15 17:56:073780 nullptr /* navigation_ui_data */, absl::nullopt /* impression */);
Carlos IL42b416592019-10-07 23:10:363781 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:413782 navigation_request->set_net_error(error);
3783 node->CreatedNavigationRequest(std::move(navigation_request));
3784 DCHECK(node->navigation_request());
3785 node->navigation_request()->BeginNavigation();
3786}
3787
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573788void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213789 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093790 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:153791 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143792 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293793}
3794
[email protected]d202a7c2012-01-04 07:53:473795void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363796 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553797 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363798 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293799
initial.commit09911bf2008-07-26 23:55:293800 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293801}
[email protected]765b35502008-08-21 00:51:203802
arthursonzogni69a6a1b2019-09-17 09:23:003803void NavigationControllerImpl::DiscardNonCommittedEntries() {
Michael Thiessen9b14d512019-09-23 21:19:473804 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:103805 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
3806 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:293807 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:473808 return;
Michael Thiessenc5676d22019-09-25 22:32:103809 }
avi45a72532015-04-07 21:01:453810 DiscardPendingEntry(false);
arthursonzogni69a6a1b2019-09-17 09:23:003811 if (delegate_)
3812 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:483813}
3814
avi7c6f35e2015-05-08 17:52:383815int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3816 int nav_entry_id) const {
3817 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3818 if (entries_[i]->GetUniqueID() == nav_entry_id)
3819 return i;
3820 }
3821 return -1;
3822}
3823
[email protected]d202a7c2012-01-04 07:53:473824void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573825 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253826 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573827 DCHECK_LE(max_index, source->GetEntryCount());
Nate Chapin214a86a2021-06-21 20:35:573828 std::unique_ptr<NavigationEntryRestoreContextImpl> context =
3829 std::make_unique<NavigationEntryRestoreContextImpl>();
[email protected]e1cd5452010-08-26 18:03:253830 for (int i = 0; i < max_index; i++) {
Nate Chapin9f169072021-06-09 19:32:373831 // Normally, cloning a NavigationEntryImpl results in sharing
3832 // FrameNavigationEntries between the original and the clone. However, when
3833 // cloning from a different NavigationControllerImpl, we want to fork the
3834 // FrameNavigationEntries.
Nate Chapin9f169072021-06-09 19:32:373835 entries_.insert(entries_.begin() + i,
Nate Chapin214a86a2021-06-21 20:35:573836 source->entries_[i]->CloneWithoutSharing(context.get()));
[email protected]e1cd5452010-08-26 18:03:253837 }
arthursonzogni5c4c202d2017-04-25 23:41:273838 DCHECK(pending_entry_index_ == -1 ||
3839 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:253840}
[email protected]c5b88d82012-10-06 17:03:333841
3842void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:183843 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:333844 get_timestamp_callback_ = get_timestamp_callback;
3845}
[email protected]8ff00d72012-10-23 19:12:213846
Shivani Sharmaffb32b82019-04-09 16:58:473847// History manipulation intervention:
3848void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:473849 bool replace_entry,
3850 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:403851 bool is_renderer_initiated,
3852 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:453853 // Note that for a subframe, previous_document_was_activated is true if the
3854 // gesture happened in any subframe (propagated to main frame) or in the main
3855 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:473856 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:273857 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:473858 return;
3859 }
3860 if (last_committed_entry_index_ == -1)
3861 return;
3862
Shivani Sharmac4cc8922019-04-18 03:11:173863 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473864
Alexander Timine3ec4192020-04-20 16:39:403865 // Log UKM with the URL we are navigating away from.
3866 ukm::builders::HistoryManipulationIntervention(
3867 previous_page_load_ukm_source_id)
3868 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:473869}
3870
Shivani Sharmac4cc8922019-04-18 03:11:173871void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
3872 int reference_index,
3873 bool skippable) {
3874 auto* reference_entry = GetEntryAtIndex(reference_index);
3875 reference_entry->set_should_skip_on_back_forward_ui(skippable);
3876
3877 int64_t document_sequence_number =
3878 reference_entry->root_node()->frame_entry->document_sequence_number();
3879 for (int index = 0; index < GetEntryCount(); index++) {
3880 auto* entry = GetEntryAtIndex(index);
3881 if (entry->root_node()->frame_entry->document_sequence_number() ==
3882 document_sequence_number) {
3883 entry->set_should_skip_on_back_forward_ui(skippable);
3884 }
3885 }
3886}
3887
arthursonzogni66f711c2019-10-08 14:40:363888std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
3889NavigationControllerImpl::ReferencePendingEntry() {
3890 DCHECK(pending_entry_);
3891 auto pending_entry_ref =
3892 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
3893 pending_entry_refs_.insert(pending_entry_ref.get());
3894 return pending_entry_ref;
3895}
3896
3897void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
3898 // Ignore refs that don't correspond to the current pending entry.
3899 auto it = pending_entry_refs_.find(ref);
3900 if (it == pending_entry_refs_.end())
3901 return;
3902 pending_entry_refs_.erase(it);
3903
3904 if (!pending_entry_refs_.empty())
3905 return;
3906
3907 // The pending entry may be deleted before the last PendingEntryRef.
3908 if (!pending_entry_)
3909 return;
3910
3911 // We usually clear the pending entry when the matching NavigationRequest
3912 // fails, so that an arbitrary URL isn't left visible above a committed page.
3913 //
3914 // However, we do preserve the pending entry in some cases, such as on the
3915 // initial navigation of an unmodified blank tab. We also allow the delegate
3916 // to say when it's safe to leave aborted URLs in the omnibox, to let the
3917 // user edit the URL and try again. This may be useful in cases that the
3918 // committed page cannot be attacker-controlled. In these cases, we still
3919 // allow the view to clear the pending entry and typed URL if the user
3920 // requests (e.g., hitting Escape with focus in the address bar).
3921 //
3922 // Do not leave the pending entry visible if it has an invalid URL, since this
3923 // might be formatted in an unexpected or unsafe way.
3924 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:363925 bool should_preserve_entry =
3926 (pending_entry_ == GetVisibleEntry()) &&
3927 pending_entry_->GetURL().is_valid() &&
3928 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
3929 if (should_preserve_entry)
3930 return;
3931
3932 DiscardPendingEntry(true);
3933 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3934}
3935
Titouan Rigoudy6ec70402021-02-02 15:42:193936std::unique_ptr<PolicyContainerPolicies>
3937NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:393938 RenderFrameHostImpl* rfh,
3939 bool is_same_document,
3940 NavigationRequest* request) {
Titouan Rigoudy6ec70402021-02-02 15:42:193941 if (!ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(request))
Antonio Sartori78a749f2020-11-30 12:03:393942 return nullptr;
3943
3944 if (is_same_document) {
Charlie Reis73e356242021-04-02 17:10:313945 // TODO(https://crbug.com/524208): Remove this nullptr check when we can
3946 // ensure we always have a FrameNavigationEntry here.
3947 if (!GetLastCommittedEntry())
3948 return nullptr;
3949
Antonio Sartori78a749f2020-11-30 12:03:393950 FrameNavigationEntry* previous_frame_entry =
3951 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
3952
3953 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
3954 // ensure we always have a FrameNavigationEntry here.
3955 if (!previous_frame_entry)
3956 return nullptr;
3957
Titouan Rigoudy6ec70402021-02-02 15:42:193958 const PolicyContainerPolicies* previous_policies =
3959 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:393960
Titouan Rigoudy6ec70402021-02-02 15:42:193961 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:393962 return nullptr;
3963
3964 // Make a copy of the policy container for the new FrameNavigationEntry.
Antonio Sartori5d09b30f2021-03-02 09:27:163965 return previous_policies->Clone();
Antonio Sartori78a749f2020-11-30 12:03:393966 }
3967
Antonio Sartori4f5373792021-05-31 10:56:473968 return rfh->policy_container_host()->policies().Clone();
Antonio Sartori78a749f2020-11-30 12:03:393969}
3970
Hayato Ito2c8c08d02021-06-23 03:38:433971void NavigationControllerImpl::BroadcastHistoryOffsetAndLength() {
Carlos Caballeroede6f8c2021-01-28 11:01:503972 OPTIONAL_TRACE_EVENT2(
Hayato Ito2c8c08d02021-06-23 03:38:433973 "content", "NavigationControllerImpl::BroadcastHistoryOffsetAndLength",
3974 "history_offset", GetLastCommittedEntryIndex(), "history_length",
3975 GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:503976
3977 auto callback = base::BindRepeating(
3978 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
3979 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
3980 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
3981 }
3982 },
Hayato Ito2c8c08d02021-06-23 03:38:433983 GetLastCommittedEntryIndex(), GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:503984 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
3985}
3986
3987void NavigationControllerImpl::DidAccessInitialMainDocument() {
3988 // We may have left a failed browser-initiated navigation in the address bar
3989 // to let the user edit it and try again. Clear it now that content might
3990 // show up underneath it.
3991 if (!frame_tree_.IsLoading() && GetPendingEntry())
3992 DiscardPendingEntry(false);
3993
3994 // Update the URL display.
3995 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3996}
3997
3998void NavigationControllerImpl::UpdateStateForFrame(
3999 RenderFrameHostImpl* rfhi,
4000 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:564001 OPTIONAL_TRACE_EVENT1("content",
4002 "NavigationControllerImpl::UpdateStateForFrame",
4003 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:504004 // The state update affects the last NavigationEntry associated with the given
4005 // |render_frame_host|. This may not be the last committed NavigationEntry (as
4006 // in the case of an UpdateState from a frame being swapped out). We track
4007 // which entry this is in the RenderFrameHost's nav_entry_id.
4008 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
4009 if (!entry)
4010 return;
4011
4012 FrameNavigationEntry* frame_entry =
4013 entry->GetFrameEntry(rfhi->frame_tree_node());
4014 if (!frame_entry)
4015 return;
4016
4017 // The SiteInstance might not match if we do a cross-process navigation with
4018 // replacement (e.g., auto-subframe), in which case the swap out of the old
4019 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4020 // has already been replaced and destroyed.
4021 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4022 return;
4023
4024 if (page_state == frame_entry->page_state())
4025 return; // Nothing to update.
4026
4027 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4028
4029 // The document_sequence_number and item_sequence_number recorded in the
4030 // FrameNavigationEntry should not differ from the one coming with the update,
4031 // since it must come from the same document. Do not update it if a difference
4032 // is detected, as this indicates that |frame_entry| is not the correct one.
4033 blink::ExplodedPageState exploded_state;
4034 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4035 return;
4036
4037 if (exploded_state.top.document_sequence_number !=
4038 frame_entry->document_sequence_number() ||
4039 exploded_state.top.item_sequence_number !=
4040 frame_entry->item_sequence_number()) {
4041 return;
4042 }
4043
4044 frame_entry->SetPageState(page_state);
4045 NotifyEntryChanged(entry);
4046}
4047
Aaron Colwellb731a0ae2021-03-19 19:14:474048void NavigationControllerImpl::OnStoragePartitionIdAdded(
4049 const StoragePartitionId& partition_id) {
4050 auto it = partition_config_to_id_map_.insert(
4051 std::make_pair(partition_id.config(), partition_id));
4052 bool successful_insert = it.second;
Aaron Colwellb731a0ae2021-03-19 19:14:474053 if (!successful_insert) {
4054 LogStoragePartitionIdCrashKeys(it.first->second, partition_id);
4055 }
Sharon Yangd4f48792021-06-30 17:02:534056 CHECK(successful_insert);
Aaron Colwellb731a0ae2021-03-19 19:14:474057}
4058
4059void NavigationControllerImpl::LogStoragePartitionIdCrashKeys(
4060 const StoragePartitionId& original_partition_id,
4061 const StoragePartitionId& new_partition_id) {
4062 base::debug::SetCrashKeyString(
4063 base::debug::AllocateCrashKeyString("original_partition_id",
4064 base::debug::CrashKeySize::Size256),
4065 original_partition_id.ToString());
4066
4067 base::debug::SetCrashKeyString(
4068 base::debug::AllocateCrashKeyString("new_partition_id",
4069 base::debug::CrashKeySize::Size256),
4070 new_partition_id.ToString());
Aaron Colwellb731a0ae2021-03-19 19:14:474071}
4072
Minggang Wangb9f3fa92021-07-01 15:30:314073std::vector<blink::mojom::AppHistoryEntryPtr>
Nate Chapind1fe3612021-04-16 20:45:574074NavigationControllerImpl::PopulateSingleAppHistoryEntryVector(
4075 Direction direction,
4076 int entry_index,
4077 const url::Origin& pending_origin,
4078 FrameTreeNode* node,
4079 SiteInstance* site_instance,
4080 int64_t previous_item_sequence_number) {
Minggang Wangb9f3fa92021-07-01 15:30:314081 std::vector<blink::mojom::AppHistoryEntryPtr> entries;
Nate Chapind1fe3612021-04-16 20:45:574082 int offset = direction == Direction::kForward ? 1 : -1;
4083 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4084 i += offset) {
4085 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
4086 if (!frame_entry || !frame_entry->committed_origin())
4087 break;
4088 if (site_instance != frame_entry->site_instance())
4089 break;
4090 if (!pending_origin.IsSameOriginWith(*frame_entry->committed_origin()))
4091 break;
4092 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4093 continue;
4094 blink::ExplodedPageState exploded_page_state;
4095 if (blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4096 &exploded_page_state)) {
4097 blink::ExplodedFrameState frame_state = exploded_page_state.top;
Minggang Wangb9f3fa92021-07-01 15:30:314098 blink::mojom::AppHistoryEntryPtr entry =
4099 blink::mojom::AppHistoryEntry::New(
4100 frame_state.app_history_key.value_or(std::u16string()),
4101 frame_state.app_history_id.value_or(std::u16string()),
4102 frame_state.url_string.value_or(std::u16string()));
Nate Chapind1fe3612021-04-16 20:45:574103 DCHECK(pending_origin.CanBeDerivedFrom(GURL(entry->url)));
4104 entries.push_back(std::move(entry));
4105 previous_item_sequence_number = frame_entry->item_sequence_number();
4106 }
4107 }
4108 // If |entries| was constructed by iterating backwards from
4109 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4110 // earliest-at-the-front. Reverse it.
4111 if (direction == Direction::kBack)
4112 std::reverse(entries.begin(), entries.end());
4113 return entries;
4114}
4115
4116void NavigationControllerImpl::PopulateAppHistoryEntryVectors(
4117 NavigationRequest* request) {
4118 url::Origin pending_origin =
4119 request->commit_params().origin_to_commit
4120 ? *request->commit_params().origin_to_commit
4121 : url::Origin::Create(request->common_params().url);
4122
4123 FrameTreeNode* node = request->frame_tree_node();
4124 scoped_refptr<SiteInstance> site_instance =
4125 request->GetRenderFrameHost()->GetSiteInstance();
4126
4127 // NOTE: |entry_index| is an estimate of the index where this entry will
4128 // commit, but it may be wrong in corner cases (e.g., if we are at the max
4129 // entry limit, the earliest entry will be dropped). This is ok because this
4130 // algorithm only uses |entry_index| to walk the entry list as it stands right
4131 // now, and it isn't saved for anything post-commit.
4132 int entry_index = GetPendingEntryIndex();
4133 bool will_create_new_entry = false;
4134 if (NavigationTypeUtils::IsReload(request->common_params().navigation_type) ||
4135 request->common_params().should_replace_current_entry) {
4136 entry_index = GetLastCommittedEntryIndex();
4137 } else if (entry_index == -1) {
4138 will_create_new_entry = true;
4139 entry_index = GetLastCommittedEntryIndex() + 1;
4140 }
4141
4142 int64_t pending_item_sequence_number = 0;
4143 if (auto* pending_entry = GetPendingEntry()) {
4144 if (auto* frame_entry = pending_entry->GetFrameEntry(node))
4145 pending_item_sequence_number = frame_entry->item_sequence_number();
4146 }
4147
4148 request->set_app_history_back_entries(PopulateSingleAppHistoryEntryVector(
4149 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
4150 pending_item_sequence_number));
4151
4152 // Don't populate forward entries if they will be truncated by a new entry.
4153 if (!will_create_new_entry) {
4154 request->set_app_history_forward_entries(
4155 PopulateSingleAppHistoryEntryVector(
4156 Direction::kForward, entry_index, pending_origin, node,
4157 site_instance.get(), pending_item_sequence_number));
4158 }
4159}
4160
Nate Chapinfbfe5af2021-06-10 17:22:084161NavigationControllerImpl::HistoryNavigationAction
4162NavigationControllerImpl::ShouldNavigateToEntryForAppHistoryKey(
4163 FrameNavigationEntry* current_entry,
4164 FrameNavigationEntry* target_entry,
4165 const std::string& app_history_key) {
4166 if (!target_entry || !target_entry->committed_origin())
4167 return HistoryNavigationAction::kStopLooking;
4168 if (current_entry->site_instance() != target_entry->site_instance())
4169 return HistoryNavigationAction::kStopLooking;
4170 if (!current_entry->committed_origin()->IsSameOriginWith(
4171 *target_entry->committed_origin())) {
4172 return HistoryNavigationAction::kStopLooking;
4173 }
4174
4175 // NOTE: We don't actually care between kSameDocument and
4176 // kDifferentDocument, so always use kDifferentDocument by convention.
4177 if (target_entry->app_history_key() == app_history_key)
4178 return HistoryNavigationAction::kDifferentDocument;
4179 return HistoryNavigationAction::kKeepLooking;
4180}
4181
4182void NavigationControllerImpl::NavigateToAppHistoryKey(FrameTreeNode* node,
4183 const std::string& key) {
4184 FrameNavigationEntry* current_entry =
4185 GetLastCommittedEntry()->GetFrameEntry(node);
4186 if (!current_entry)
4187 return;
4188
4189 // We want to find the nearest matching entry that is contiguously
4190 // same-instance and same-origin. Check back first, then forward.
4191 // TODO(japhet): Link spec here once it exists.
4192 for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) {
4193 auto result = ShouldNavigateToEntryForAppHistoryKey(
4194 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4195 if (result == HistoryNavigationAction::kStopLooking)
4196 break;
4197 if (result != HistoryNavigationAction::kKeepLooking) {
4198 GoToIndex(i);
4199 return;
4200 }
4201 }
4202 for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) {
4203 auto result = ShouldNavigateToEntryForAppHistoryKey(
4204 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4205 if (result == HistoryNavigationAction::kStopLooking)
4206 break;
4207 if (result != HistoryNavigationAction::kKeepLooking) {
4208 GoToIndex(i);
4209 return;
4210 }
4211 }
4212}
4213
Hayato Ito7a80db42021-07-05 06:18:544214bool NavigationControllerImpl::ShouldMaintainTrivialSessionHistory() const {
4215 // TODO(https://crbug.com/1197384): We may have to add portals and fenced
4216 // frames in addition to prerender. This should be kept in sync with
4217 // LocalFrame version, LocalFrame::ShouldMaintainTrivialSessionHistory.
4218 return frame_tree_.is_prerendering();
4219}
4220
[email protected]8ff00d72012-10-23 19:12:214221} // namespace content