blob: 54291b69f285197fe2e67a316b16fac879338d3f [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,
Kush Sinhaa8856ecb2021-09-01 14:28:31734 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
Adithya Srinivasan9b0c99c2021-08-10 15:19:45768NavigationEntryImpl*
769NavigationControllerImpl::GetEntryWithUniqueIDIncludingPending(
770 int nav_entry_id) const {
771 NavigationEntryImpl* entry = GetEntryWithUniqueID(nav_entry_id);
772 if (entry)
773 return entry;
774 return pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id
775 ? pending_entry_
776 : nullptr;
777}
778
W. James MacLean1c40862c2020-04-27 21:05:57779void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
780 const url::Origin& origin) {
781 for (int i = 0; i < GetEntryCount(); i++) {
782 auto* entry = GetEntryAtIndex(i);
783 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
784 }
785 if (entry_replaced_by_post_commit_error_) {
786 // It's possible we could come back to this entry if the error
787 // page/interstitial goes away.
788 entry_replaced_by_post_commit_error_
789 ->RegisterExistingOriginToPreventOptInIsolation(origin);
790 }
791 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
792 // visiting pending_entry_, which lacks a committed origin. This will be done
793 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
794}
795
avi25764702015-06-23 15:43:37796void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57797 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00798 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37799 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27800 DCHECK_EQ(-1, pending_entry_index_);
[email protected]8ff00d72012-10-23 19:12:21801 NotificationService::current()->Notify(
Aran Gilman37d11632019-10-08 23:07:15802 NOTIFICATION_NAV_ENTRY_PENDING, Source<NavigationController>(this),
avi25764702015-06-23 15:43:37803 Details<NavigationEntry>(pending_entry_));
[email protected]765b35502008-08-21 00:51:20804}
805
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57806NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47807 if (pending_entry_)
808 return pending_entry_;
809 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20810}
811
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57812NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32813 // The pending entry is safe to return for new (non-history), browser-
814 // initiated navigations. Most renderer-initiated navigations should not
815 // show the pending entry, to prevent URL spoof attacks.
816 //
817 // We make an exception for renderer-initiated navigations in new tabs, as
818 // long as no other page has tried to access the initial empty document in
819 // the new tab. If another page modifies this blank page, a URL spoof is
820 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32821 bool safe_to_show_pending =
822 pending_entry_ &&
823 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09824 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32825 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46826 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32827
828 // Also allow showing the pending entry for history navigations in a new tab,
829 // such as Ctrl+Back. In this case, no existing page is visible and no one
830 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15831 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
832 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32833 safe_to_show_pending = true;
834
835 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19836 return pending_entry_;
837 return GetLastCommittedEntry();
838}
839
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57840int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20841 if (pending_entry_index_ != -1)
842 return pending_entry_index_;
843 return last_committed_entry_index_;
844}
845
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57846NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20847 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28848 return nullptr;
avif16f85a72015-11-13 18:25:03849 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20850}
851
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57852bool NavigationControllerImpl::CanViewSource() {
Jeremy Roman2d8dfe132021-07-06 20:51:26853 const std::string& mime_type =
854 frame_tree_.root()->current_frame_host()->GetPage().contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53855 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
856 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27857 NavigationEntry* visible_entry = GetVisibleEntry();
858 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39859 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16860}
861
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57862int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27863 // The last committed entry index must always be less than the number of
Carlos IL4dea8902020-05-26 15:14:29864 // entries. If there are no entries, it must be -1.
arthursonzogni5c4c202d2017-04-25 23:41:27865 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
866 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55867 return last_committed_entry_index_;
868}
869
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57870int NavigationControllerImpl::GetEntryCount() {
Carlos IL4dea8902020-05-26 15:14:29871 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55872 return static_cast<int>(entries_.size());
873}
874
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57875NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37876 if (index < 0 || index >= GetEntryCount())
877 return nullptr;
878
avif16f85a72015-11-13 18:25:03879 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25880}
881
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57882NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47883 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20884}
885
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57886int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46887 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03888}
889
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57890bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03891 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
892 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
893 return true;
894 }
895 return false;
[email protected]765b35502008-08-21 00:51:20896}
897
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57898bool NavigationControllerImpl::CanGoForward() {
WangHui74286d52021-03-31 16:17:15899 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
900 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
901 return true;
902 }
903 return false;
[email protected]765b35502008-08-21 00:51:20904}
905
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57906bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03907 int index = GetIndexForOffset(offset);
908 return index >= 0 && index < GetEntryCount();
909}
910
WangHui74286d52021-03-31 16:17:15911#if defined(OS_ANDROID)
912bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
WangHui74286d52021-03-31 16:17:15913 if (offset == 0)
914 return true;
915 int increment = offset > 0 ? 1 : -1;
916 int non_skippable_entries = 0;
917 for (int index = GetIndexForOffset(increment);
918 index >= 0 && index < GetEntryCount(); index += increment) {
919 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
920 non_skippable_entries++;
921
922 if (non_skippable_entries == std::abs(offset))
923 return true;
924 }
925 return false;
926}
927#endif
928
[email protected]d202a7c2012-01-04 07:53:47929void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06930 int target_index = GetIndexForOffset(-1);
931
Elly Fong-Jones40ee8112021-06-23 19:10:52932 // Move the target index past the skippable entries.
Shivani Sharma298d12852019-01-22 20:04:03933 bool all_skippable_entries = true;
Elly Fong-Jones40ee8112021-06-23 19:10:52934 while (target_index >= 0) {
935 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui()) {
Shivani Sharma298d12852019-01-22 20:04:03936 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06937 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16938 }
Elly Fong-Jones40ee8112021-06-23 19:10:52939 target_index--;
shivanisha55201872018-12-13 04:29:06940 }
Miyoung Shin1c565c912021-03-17 12:11:21941
Shivani Sharma298d12852019-01-22 20:04:03942 // Do nothing if all entries are skippable. Normally this path would not
943 // happen as consumers would have already checked it in CanGoBack but a lot of
944 // tests do not do that.
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42945 if (all_skippable_entries)
Shivani Sharma298d12852019-01-22 20:04:03946 return;
shivanisha55201872018-12-13 04:29:06947
shivanisha55201872018-12-13 04:29:06948 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20949}
950
[email protected]d202a7c2012-01-04 07:53:47951void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06952 int target_index = GetIndexForOffset(1);
953
Shivani Sharma2d5b4b6b2019-01-08 16:07:16954 // Note that at least one entry (the last one) will be non-skippable since
955 // entries are marked skippable only when they add another entry because of
956 // redirect or pushState.
Elly Fong-Jones40ee8112021-06-23 19:10:52957 while (target_index < static_cast<int>(entries_.size())) {
958 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui())
shivanisha55201872018-12-13 04:29:06959 break;
Elly Fong-Jones40ee8112021-06-23 19:10:52960 target_index++;
shivanisha55201872018-12-13 04:29:06961 }
shivanisha55201872018-12-13 04:29:06962 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20963}
964
[email protected]d202a7c2012-01-04 07:53:47965void NavigationControllerImpl::GoToIndex(int index) {
Kush Sinhaa8856ecb2021-09-01 14:28:31966 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId);
Dave Tapuska8bfd84c2019-03-26 20:47:16967}
968
969void NavigationControllerImpl::GoToIndex(int index,
Kush Sinhaa8856ecb2021-09-01 14:28:31970 int sandbox_frame_tree_node_id) {
sunjian30574a62017-03-21 21:39:44971 TRACE_EVENT0("browser,navigation,benchmark",
972 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:20973 if (index < 0 || index >= static_cast<int>(entries_.size())) {
974 NOTREACHED();
975 return;
976 }
977
[email protected]cbab76d2008-10-13 22:42:47978 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20979
arthursonzogni5c4c202d2017-04-25 23:41:27980 DCHECK_EQ(nullptr, pending_entry_);
981 DCHECK_EQ(-1, pending_entry_index_);
982 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:20983 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:27984 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
985 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Kush Sinhaa8856ecb2021-09-01 14:28:31986 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id);
[email protected]765b35502008-08-21 00:51:20987}
988
[email protected]d202a7c2012-01-04 07:53:47989void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:12990 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:03991 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:20992 return;
993
[email protected]9ba14052012-06-22 23:50:03994 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20995}
996
WangHui74286d52021-03-31 16:17:15997#if defined(OS_ANDROID)
998void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
999 // Note: This is actually reached in unit tests.
1000 if (!CanGoToOffsetWithSkipping(offset))
1001 return;
1002
Elly Fong-Jonesccc6d1f2021-06-14 18:32:421003 if (offset == 0) {
WangHui74286d52021-03-31 16:17:151004 GoToIndex(GetIndexForOffset(offset));
1005 return;
1006 }
1007 int increment = offset > 0 ? 1 : -1;
1008 // Find the offset without counting skippable entries.
1009 int target_index = GetIndexForOffset(increment);
1010 int non_skippable_entries = 0;
1011 for (int index = target_index; index >= 0 && index < GetEntryCount();
1012 index += increment) {
1013 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1014 non_skippable_entries++;
1015
1016 if (non_skippable_entries == std::abs(offset)) {
1017 target_index = index;
1018 break;
1019 }
1020 }
1021
1022 GoToIndex(target_index);
1023}
1024#endif
1025
[email protected]41374f12013-07-24 02:49:281026bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151027 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281028 return false;
[email protected]6a13a6c2011-12-20 21:47:121029
[email protected]43032342011-03-21 14:10:311030 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281031 return true;
[email protected]cbab76d2008-10-13 22:42:471032}
1033
Michael Thiessen9b14d512019-09-23 21:19:471034void NavigationControllerImpl::PruneForwardEntries() {
1035 DiscardNonCommittedEntries();
1036 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471037 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471038 if (num_removed <= 0)
1039 return;
1040 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1041 NotifyPrunedEntries(this, remove_start_index /* start index */,
1042 num_removed /* count */);
1043}
1044
Aran Gilman37d11632019-10-08 23:07:151045void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1046 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321047 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311048 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511049 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1050 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321051 }
1052}
1053
Harkiran Bolariaba823e42021-05-21 18:30:361054base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL(
1055 const GURL& url,
1056 const Referrer& referrer,
1057 ui::PageTransition transition,
1058 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471059 LoadURLParams params(url);
1060 params.referrer = referrer;
1061 params.transition_type = transition;
1062 params.extra_headers = extra_headers;
Harkiran Bolariaba823e42021-05-21 18:30:361063 return LoadURLWithParams(params);
[email protected]cf002332012-08-14 19:17:471064}
1065
Harkiran Bolariaba823e42021-05-21 18:30:361066base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams(
1067 const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061068 if (params.is_renderer_initiated)
1069 DCHECK(params.initiator_origin.has_value());
1070
naskob8744d22014-08-28 17:07:431071 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151072 "NavigationControllerImpl::LoadURLWithParams", "url",
1073 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291074 bool is_explicit_navigation =
1075 GetContentClient()->browser()->IsExplicitNavigation(
1076 params.transition_type);
1077 if (HandleDebugURL(params.url, params.transition_type,
1078 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431079 // If Telemetry is running, allow the URL load to proceed as if it's
1080 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491081 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431082 cc::switches::kEnableGpuBenchmarking))
Harkiran Bolariaba823e42021-05-21 18:30:361083 return nullptr;
[email protected]47752982014-07-29 08:01:431084 }
[email protected]8bf1048012012-02-08 01:22:181085
[email protected]cf002332012-08-14 19:17:471086 // Checks based on params.load_type.
1087 switch (params.load_type) {
1088 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431089 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471090 break;
1091 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261092 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471093 NOTREACHED() << "Data load must use data scheme.";
Harkiran Bolariaba823e42021-05-21 18:30:361094 return nullptr;
[email protected]cf002332012-08-14 19:17:471095 }
1096 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461097 }
[email protected]e47ae9472011-10-13 19:48:341098
[email protected]e47ae9472011-10-13 19:48:341099 // The user initiated a load, we don't need to reload anymore.
1100 needs_reload_ = false;
1101
Harkiran Bolariaba823e42021-05-21 18:30:361102 return NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441103}
1104
Mohamed Abdelhalim833de902019-09-16 17:41:451105bool NavigationControllerImpl::PendingEntryMatchesRequest(
1106 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191107 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451108 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191109}
1110
[email protected]d202a7c2012-01-04 07:53:471111bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321112 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071113 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331114 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441115 bool is_same_document_navigation,
Nate Chapinc7019dd7d2021-06-25 18:29:251116 bool was_on_initial_empty_document,
Shivani Sharmaffb32b82019-04-09 16:58:471117 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131118 NavigationRequest* navigation_request) {
1119 DCHECK(navigation_request);
Takashi Toyoshimaa9f66802021-08-18 02:40:171120 if (ShouldMaintainTrivialSessionHistory() && GetLastCommittedEntry()) {
1121 // Ensure that this navigation does not add a navigation entry, since
1122 // ShouldMaintainTrivialSessionHistory() means we should not add an entry
1123 // beyond the last committed one. Therefore, `should_replace_current_entry`
1124 // should be set, which replaces the current entry, or this should be a
1125 // reload, which does not create a new entry.
1126 DCHECK(params.should_replace_current_entry ||
1127 navigation_request->GetReloadType() != ReloadType::NONE);
1128 }
[email protected]cd2e15742013-03-08 04:08:311129 is_initial_navigation_ = false;
1130
[email protected]0e8db942008-09-24 21:21:481131 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431132 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481133 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361134 if (entry_replaced_by_post_commit_error_) {
1135 if (is_same_document_navigation) {
1136 // Same document navigations should not be possible on error pages and
1137 // would leave the controller in a weird state. Kill the renderer if
1138 // that happens.
1139 bad_message::ReceivedBadMessage(
1140 rfh->GetProcess(), bad_message::NC_SAME_DOCUMENT_POST_COMMIT_ERROR);
1141 }
1142 // Any commit while a post-commit error page is showing should put the
1143 // original entry back, replacing the error page's entry. This includes
1144 // reloads, where the original entry was used as the pending entry and
1145 // should now be at the correct index at commit time.
1146 entries_[last_committed_entry_index_] =
1147 std::move(entry_replaced_by_post_commit_error_);
1148 }
Fergal Daly8e33cf62020-12-12 01:06:071149 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551150 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431151 if (pending_entry_ &&
1152 pending_entry_->GetIsOverridingUserAgent() !=
1153 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1154 overriding_user_agent_changed = true;
[email protected]0e8db942008-09-24 21:21:481155 } else {
Gang Wu325f03f42021-02-25 20:00:461156 // GetLastCommittedEntry() is null, so this is the first entry.
Fergal Daly8e33cf62020-12-12 01:06:071157 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481158 details->previous_entry_index = -1;
Gang Wu325f03f42021-02-25 20:00:461159 if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) {
1160 // Default setting is NOT override the user agent, so overriding the user
1161 // agent in first entry should be considered as user agent changed as
1162 // well.
1163 overriding_user_agent_changed = true;
1164 }
[email protected]0e8db942008-09-24 21:21:481165 }
[email protected]ecd9d8702008-08-28 22:10:171166
Dave Tapuskaa2ab4f252021-07-08 21:31:281167 bool is_main_frame_navigation = !rfh->GetParent();
1168
Alexander Timind2f2e4f22019-04-02 20:04:531169 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1170 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281171 // For primary frame tree navigations, choose an appropriate
1172 // BackForwardCacheMetrics to be associated with the NavigationEntry, by
1173 // either creating a new object or reusing the previous one depending on
1174 // whether it's a main frame navigation or not.
1175 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics;
1176 if (navigation_request->frame_tree_node()->frame_tree()->type() ==
1177 FrameTree::Type::kPrimary) {
1178 back_forward_cache_metrics =
1179 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1180 GetLastCommittedEntry(), is_main_frame_navigation,
1181 params.document_sequence_number);
1182 }
Alexander Timind2f2e4f22019-04-02 20:04:531183 // Notify the last active entry that we have navigated away.
Dave Tapuskaa2ab4f252021-07-08 21:31:281184 if (is_main_frame_navigation && !is_same_document_navigation) {
Alexander Timind2f2e4f22019-04-02 20:04:531185 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1186 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Hajime Hoshic7606502021-04-14 02:42:161187 metrics->MainFrameDidNavigateAwayFromDocument(rfh, navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531188 }
1189 }
1190 }
1191
fdegans9caf66a2015-07-30 21:10:421192 // If there is a pending entry at this point, it should have a SiteInstance,
1193 // except for restored entries.
jam48cea9082017-02-15 06:13:291194 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481195 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081196 pending_entry_->IsRestored());
1197 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151198 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291199 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481200 }
[email protected]e9ba4472008-09-14 15:42:431201
Nasko Oskovaee2f862018-06-15 00:05:521202 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1203 // has changed, this must be treated as a new navigation with replacement.
1204 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491205 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521206 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101207 pending_entry_->GetUniqueID() ==
1208 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521209 pending_entry_->site_instance() &&
1210 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1211 DCHECK_NE(-1, pending_entry_index_);
1212 // TODO(nasko,creis): Instead of setting this value here, set
1213 // should_replace_current_entry on the parameters we send to the
1214 // renderer process as part of CommitNavigation. The renderer should
1215 // in turn send it back here as part of |params| and it can be just
1216 // enforced and renderer process terminated on mismatch.
1217 details->did_replace_entry = true;
1218 } else {
1219 // The renderer tells us whether the navigation replaces the current entry.
1220 details->did_replace_entry = params.should_replace_current_entry;
1221 }
[email protected]bcd904482012-02-01 01:54:221222
[email protected]e9ba4472008-09-14 15:42:431223 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amnif6950d552020-11-24 03:26:101224 details->type = ClassifyNavigation(rfh, params, navigation_request);
[email protected]4bf3522c2010-08-19 21:00:201225
eugenebutee08663a2017-04-27 17:43:121226 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441227 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121228
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071229 details->is_prerender_activation =
1230 navigation_request->IsPrerenderedPageActivation();
1231
Peter Boströmd7592132019-01-30 04:50:311232 // Make sure we do not discard the pending entry for a different ongoing
1233 // navigation when a same document commit comes in unexpectedly from the
1234 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1235 // other navigation types. See https://crbug.com/900036.
1236 // TODO(crbug.com/926009): Handle history.pushState() as well.
1237 bool keep_pending_entry = is_same_document_navigation &&
Charlie Reisc0f17d2d2021-01-12 18:52:491238 details->type == NAVIGATION_TYPE_EXISTING_ENTRY &&
Peter Boströmd7592132019-01-30 04:50:311239 pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451240 !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311241
[email protected]0e8db942008-09-24 21:21:481242 switch (details->type) {
Charlie Reisc0f17d2d2021-01-12 18:52:491243 case NAVIGATION_TYPE_NEW_ENTRY:
1244 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051245 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451246 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431247 break;
Charlie Reisc0f17d2d2021-01-12 18:52:491248 case NAVIGATION_TYPE_EXISTING_ENTRY:
1249 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1250 was_restored, navigation_request,
1251 keep_pending_entry);
[email protected]e9ba4472008-09-14 15:42:431252 break;
[email protected]8ff00d72012-10-23 19:12:211253 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471254 RendererDidNavigateNewSubframe(
1255 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451256 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431257 break;
[email protected]8ff00d72012-10-23 19:12:211258 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391259 if (!RendererDidNavigateAutoSubframe(
Nate Chapinc7019dd7d2021-06-25 18:29:251260 rfh, params, details->is_same_document,
1261 was_on_initial_empty_document, navigation_request)) {
creisce0ef3572017-01-26 17:53:081262 // We don't send a notification about auto-subframe PageState during
1263 // UpdateStateForFrame, since it looks like nothing has changed. Send
1264 // it here at commit time instead.
1265 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431266 return false;
creis59d5a47cb2016-08-24 23:57:191267 }
[email protected]e9ba4472008-09-14 15:42:431268 break;
[email protected]8ff00d72012-10-23 19:12:211269 case NAVIGATION_TYPE_NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:491270 // If a pending navigation was in progress, this canceled it. We should
1271 // discard it and make sure it is removed from the URL bar. After that,
1272 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:431273 // the caller that nothing has happened.
arthursonzogni69a6a1b2019-09-17 09:23:001274 if (pending_entry_)
[email protected]20d1c992011-04-12 21:17:491275 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431276 return false;
Aran Gilman37d11632019-10-08 23:07:151277 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431278 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151279 break;
[email protected]765b35502008-08-21 00:51:201280 }
1281
[email protected]688aa65c62012-09-28 04:32:221282 // At this point, we know that the navigation has just completed, so
1283 // record the time.
1284 //
1285 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261286 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331287 base::Time timestamp =
1288 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1289 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131290 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221291
Peter Boströmd7592132019-01-30 04:50:311292 // If we aren't keeping the pending entry, there shouldn't be one at this
1293 // point. Clear it again in case any error cases above forgot to do so.
1294 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1295 // been cleared instead of protecting against it.
1296 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001297 DiscardNonCommittedEntries();
[email protected]f233e4232013-02-23 00:55:141298
[email protected]e9ba4472008-09-14 15:42:431299 // All committed entries should have nonempty content state so WebKit doesn't
1300 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471301 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321302 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221303 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441304 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531305 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1306 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281307 if (back_forward_cache_metrics &&
1308 !active_entry->back_forward_cache_metrics()) {
Alexander Timind2f2e4f22019-04-02 20:04:531309 active_entry->set_back_forward_cache_metrics(
1310 std::move(back_forward_cache_metrics));
1311 }
Dave Tapuskaa2ab4f252021-07-08 21:31:281312
1313 // `back_forward_cache_metrics()` may return null as we do not record
1314 // back-forward cache metrics for navigations in non-primary frame trees.
1315 if (active_entry->back_forward_cache_metrics()) {
1316 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
1317 navigation_request,
1318 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
1319 }
naskoc7533512016-05-06 17:01:121320
Charles Reisc0507202017-09-21 00:40:021321 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1322 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1323 // A mismatch can occur if the renderer lies or due to a unique name collision
1324 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121325 FrameNavigationEntry* frame_entry =
1326 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021327 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1328 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031329 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081330 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1331 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031332 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201333
1334 // Remember the bindings the renderer process has at this point, so that
1335 // we do not grant this entry additional bindings if we come back to it.
1336 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301337 }
[email protected]132e281a2012-07-31 18:32:441338
[email protected]97d8f0d2013-10-29 16:49:211339 // Once it is committed, we no longer need to track several pieces of state on
1340 // the entry.
naskoc7533512016-05-06 17:01:121341 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131342
[email protected]49bd30e62011-03-22 20:12:591343 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221344 // TODO(creis): This check won't pass for subframes until we create entries
1345 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001346 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421347 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591348
[email protected]e9ba4472008-09-14 15:42:431349 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001350 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001351 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311352 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531353
arthursonzogni7ddc6542021-04-09 09:16:501354 active_entry->SetIsOverridingUserAgent(
1355 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031356
[email protected]93f230e02011-06-01 14:40:001357 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291358
John Abd-El-Malek380d3c5922017-09-08 00:20:311359 if (active_entry->GetURL().SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121360 !navigation_request->DidEncounterError()) {
John Abd-El-Malek969cdd94e2017-07-10 22:18:161361 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus",
1362 !!active_entry->GetSSL().certificate);
1363 }
1364
aelias100c9192017-01-13 00:01:431365 if (overriding_user_agent_changed)
1366 delegate_->UpdateOverridingUserAgent();
1367
creis03b48002015-11-04 00:54:561368 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1369 // one knows the latest NavigationEntry it is showing (whether it has
1370 // committed anything in this navigation or not). This allows things like
1371 // state and title updates from RenderFrames to apply to the latest relevant
1372 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381373 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001374 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381375 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
Hayato Ito2c8c08d02021-06-23 03:38:431376
1377 if (navigation_request->IsPrerenderedPageActivation()) {
1378 BroadcastHistoryOffsetAndLength();
1379 // TODO(crbug.com/1222893): Broadcasting happens after the prerendered page
1380 // is activated. As a result, a "prerenderingchange" event listener sees the
1381 // history.length which is not updated yet. We should guarantee that
1382 // history's length and offset should be updated before a
1383 // "prerenderingchange" event listener runs. One possible approach is to use
1384 // the same IPC which "prerenderingchange" uses, and propagate history's
1385 // length and offset together with that.
1386 }
1387
[email protected]e9ba4472008-09-14 15:42:431388 return true;
initial.commit09911bf2008-07-26 23:55:291389}
1390
[email protected]8ff00d72012-10-23 19:12:211391NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321392 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101393 const mojom::DidCommitProvisionalLoadParams& params,
1394 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591395 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511396 "ClassifyNavigation");
1397
avi7c6f35e2015-05-08 17:52:381398 if (params.did_create_new_entry) {
Charlie Reisc0f17d2d2021-01-12 18:52:491399 // A new entry. We may or may not have a corresponding pending entry, and
avi7c6f35e2015-05-08 17:52:381400 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001401 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491402 trace_return.set_return_reason("new entry, no parent, new entry");
1403 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381404 }
1405
1406 // When this is a new subframe navigation, we should have a committed page
1407 // in which it's a subframe. This may not be the case when an iframe is
1408 // navigated on a popup navigated to about:blank (the iframe would be
1409 // written into the popup by script on the main page). For these cases,
1410 // there isn't any navigation stuff we can do, so just ignore it.
Nasko Oskovae49e292020-08-13 02:08:511411 if (!GetLastCommittedEntry()) {
1412 trace_return.set_return_reason("new entry, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381413 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511414 }
avi7c6f35e2015-05-08 17:52:381415
1416 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511417 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381418 return NAVIGATION_TYPE_NEW_SUBFRAME;
1419 }
1420
Charlie Reisc0f17d2d2021-01-12 18:52:491421 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381422 DCHECK(!params.history_list_was_cleared);
1423
avi39c1edd32015-06-04 20:06:001424 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381425 // All manual subframes would be did_create_new_entry and handled above, so
1426 // we know this is auto.
Nasko Oskovae49e292020-08-13 02:08:511427 if (GetLastCommittedEntry()) {
1428 trace_return.set_return_reason("subframe, last commmited, auto subframe");
avi7c6f35e2015-05-08 17:52:381429 return NAVIGATION_TYPE_AUTO_SUBFRAME;
Nasko Oskovae49e292020-08-13 02:08:511430 }
Lei Zhang96031532019-10-10 19:05:471431
1432 // We ignore subframes created in non-committed pages; we'd appreciate if
1433 // people stopped doing that.
Nasko Oskovae49e292020-08-13 02:08:511434 trace_return.set_return_reason("subframe, no last commmited, ignore");
Lei Zhang96031532019-10-10 19:05:471435 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381436 }
1437
Rakina Zata Amnif6950d552020-11-24 03:26:101438 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1439 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381440 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1441 // create a new page.
1442
1443 // Just like above in the did_create_new_entry case, it's possible to
1444 // scribble onto an uncommitted page. Again, there isn't any navigation
1445 // stuff that we can do, so ignore it here as well.
avi3a5b8f32015-05-28 17:50:231446 NavigationEntry* last_committed = GetLastCommittedEntry();
Nasko Oskovae49e292020-08-13 02:08:511447 if (!last_committed) {
1448 trace_return.set_return_reason("nav entry 0, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381449 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511450 }
avi7c6f35e2015-05-08 17:52:381451
Hayato Ito2ae49442021-07-02 02:59:251452 // This main frame navigation is not a history navigation (since
1453 // nav_entry_id is 0), but didn't create a new entry. So this must be a
1454 // reload or a replacement navigation, which will modify the existing entry.
1455 //
Nasko Oskov332593c2018-08-16 17:21:341456 // TODO(nasko): With error page isolation, reloading an existing session
1457 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491458 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341459 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511460 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491461 "nav entry 0, last committed, existing entry");
1462 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381463 }
1464
Rakina Zata Amnif6950d552020-11-24 03:26:101465 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521466 // If the SiteInstance of the |pending_entry_| does not match the
1467 // SiteInstance that got committed, treat this as a new navigation with
1468 // replacement. This can happen if back/forward/reload encounters a server
1469 // redirect to a different site or an isolated error page gets successfully
1470 // reloaded into a different SiteInstance.
1471 if (pending_entry_->site_instance() &&
1472 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491473 trace_return.set_return_reason("pending matching nav entry, new entry");
1474 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521475 }
creis77c9aa32015-09-25 19:59:421476
Nasko Oskovaee2f862018-06-15 00:05:521477 if (pending_entry_index_ == -1) {
1478 // In this case, we have a pending entry for a load of a new URL but Blink
1479 // didn't do a new navigation (params.did_create_new_entry). First check
1480 // to make sure Blink didn't treat a new cross-process navigation as
1481 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161482 // we must treat it as NEW rather than the converted reload case below,
1483 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521484 if (!GetLastCommittedEntry() ||
1485 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161486 trace_return.set_return_reason("new pending, new entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491487 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521488 }
1489
1490 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161491 // We will create a pending entry, but NavigateWithoutEntry will convert
1492 // it to a reload since it's the same page and not create a new entry for
1493 // it. (The user doesn't want to have a new back/forward entry when they
1494 // do this.) Therefore we want to just ignore the pending entry and go
1495 // back to where we were (the "existing entry").
1496 trace_return.set_return_reason("new pending, existing (same) entry");
1497 return NAVIGATION_TYPE_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521498 }
avi7c6f35e2015-05-08 17:52:381499 }
1500
creis26d22632017-04-21 20:23:561501 // Everything below here is assumed to be an existing entry, but if there is
1502 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511503 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491504 trace_return.set_return_reason("no last committed, new entry");
1505 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511506 }
creis26d22632017-04-21 20:23:561507
avi7c6f35e2015-05-08 17:52:381508 if (params.intended_as_new_entry) {
1509 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491510 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1511 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161512 trace_return.set_return_reason("intended as new entry, existing entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491513 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381514 }
1515
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121516 if (navigation_request->DidEncounterError() &&
1517 failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101518 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381519 // If the renderer was going to a new pending entry that got cleared because
1520 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491521 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381522 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511523 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491524 "unreachable, matching pending, existing entry");
1525 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381526 }
1527
Charlie Reisc0f17d2d2021-01-12 18:52:491528 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101529 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511530 trace_return.traced_value()->SetInteger("existing_entry_index",
1531 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381532 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441533 // The renderer has committed a navigation to an entry that no longer
1534 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491535 trace_return.set_return_reason("existing entry -1, new entry");
1536 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381537 }
1538
avi7c6f35e2015-05-08 17:52:381539 // Since we weeded out "new" navigations above, we know this is an existing
1540 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491541 trace_return.set_return_reason("default return, existing entry");
1542 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381543}
1544
Charlie Reisc0f17d2d2021-01-12 18:52:491545void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321546 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071547 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361548 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441549 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471550 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451551 NavigationRequest* request) {
dcheng9bfa5162016-04-09 01:00:571552 std::unique_ptr<NavigationEntryImpl> new_entry;
creisf49dfc92016-07-26 17:05:181553 bool update_virtual_url = false;
1554
Anton Bikineevf62d1bf2021-05-15 17:56:071555 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451556 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061557
creisf49dfc92016-07-26 17:05:181558 // First check if this is an in-page navigation. If so, clone the current
1559 // entry instead of looking at the pending entry, because the pending entry
1560 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361561 if (is_same_document && GetLastCommittedEntry()) {
Patrick Monette50e8bd82019-06-13 22:40:451562 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481563 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081564 params.document_sequence_number, params.app_history_key,
1565 rfh->GetSiteInstance(), nullptr, params.url,
1566 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091567 Referrer(*params.referrer), initiator_origin,
1568 request->GetRedirectChain(), params.page_state, params.method,
1569 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391570 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481571 request->GetSubresourceWebBundleNavigationInfo(),
Antonio Sartori78a749f2020-11-30 12:03:391572 // We will set the document policies later in this function.
Titouan Rigoudy6ec70402021-02-02 15:42:191573 nullptr /* policy_container_policies */);
Charles Reisf44482022017-10-13 21:15:031574
creisf49dfc92016-07-26 17:05:181575 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Harkiran Bolaria081c5c22021-09-13 08:44:531576 frame_entry, true, request->frame_tree_node(), frame_tree_.root());
jama78746e2017-02-22 17:21:571577 if (new_entry->GetURL().GetOrigin() != params.url.GetOrigin()) {
1578 // TODO(jam): we had one report of this with a URL that was redirecting to
1579 // only tildes. Until we understand that better, don't copy the cert in
1580 // this case.
1581 new_entry->GetSSL() = SSLStatus();
John Abd-El-Malek9cd697e2017-07-12 21:53:141582
John Abd-El-Malek380d3c5922017-09-08 00:20:311583 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121584 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141585 UMA_HISTOGRAM_BOOLEAN(
1586 "Navigation.SecureSchemeHasSSLStatus.NewPageInPageOriginMismatch",
1587 !!new_entry->GetSSL().certificate);
1588 }
jama78746e2017-02-22 17:21:571589 }
creisf49dfc92016-07-26 17:05:181590
Patrick Monette50e8bd82019-06-13 22:40:451591 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1592 // that |frame_entry| should now have a reference count of exactly 2: one
1593 // due to the local variable |frame_entry|, and another due to |new_entry|
1594 // also retaining one. This should never fail, because it's the main frame.
1595 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181596
1597 update_virtual_url = new_entry->update_virtual_url_with_url();
John Abd-El-Malek9cd697e2017-07-12 21:53:141598
John Abd-El-Malek380d3c5922017-09-08 00:20:311599 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121600 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141601 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus.NewPageInPage",
1602 !!new_entry->GetSSL().certificate);
1603 }
creisf49dfc92016-07-26 17:05:181604 }
1605
Harkiran Bolaria59290d62021-03-17 01:53:011606 // If this is an activation navigation from a prerendered page, transfer the
1607 // new entry from an entry already created and stored in the
1608 // NavigationRequest. |new_entry| will not have been set prior to this as
1609 // |is_same_document| is mutually exclusive with
1610 // |IsPrerenderedPageActivation|.
1611 if (request->IsPrerenderedPageActivation()) {
1612 DCHECK(!is_same_document);
1613 DCHECK(!new_entry);
1614 new_entry = request->TakePrerenderNavigationEntry();
1615 DCHECK(new_entry);
1616 }
1617
Charlie Reisc0f17d2d2021-01-12 18:52:491618 // Only make a copy of the pending entry if it is appropriate for the new
1619 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451620 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041621 // 1. The SiteInstance hasn't been assigned to something else.
1622 // 2. The pending entry was intended as a new entry, rather than being a
1623 // history navigation that was interrupted by an unrelated,
1624 // renderer-initiated navigation.
1625 // TODO(csharrison): Investigate whether we can remove some of the coarser
1626 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451627 if (!new_entry && PendingEntryMatchesRequest(request) &&
1628 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341629 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431630 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351631 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431632
[email protected]f1eb87a2011-05-06 17:49:411633 update_virtual_url = new_entry->update_virtual_url_with_url();
Camille Lamy62b826012019-02-26 09:15:471634 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451635 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141636
John Abd-El-Malek380d3c5922017-09-08 00:20:311637 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121638 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141639 UMA_HISTOGRAM_BOOLEAN(
1640 "Navigation.SecureSchemeHasSSLStatus.NewPagePendingEntryMatches",
1641 !!new_entry->GetSSL().certificate);
1642 }
creisf49dfc92016-07-26 17:05:181643 }
1644
Charlie Reisc0f17d2d2021-01-12 18:52:491645 // For cross-document commits with no matching pending entry, create a new
1646 // entry.
creisf49dfc92016-07-26 17:05:181647 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061648 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071649 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1650 initiator_origin,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:581651 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451652 params.transition, request->IsRendererInitiated(),
Lukasz Anforowicz435bcb582019-07-12 20:50:061653 nullptr); // blob_url_loader_factory
[email protected]f8f93eb2012-09-25 03:06:241654
1655 // Find out whether the new entry needs to update its virtual URL on URL
1656 // change and set up the entry accordingly. This is needed to correctly
1657 // update the virtual URL when replaceState is called after a pushState.
1658 GURL url = params.url;
1659 bool needs_update = false;
[email protected]47752982014-07-29 08:01:431660 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1661 &url, browser_context_, &needs_update);
[email protected]f8f93eb2012-09-25 03:06:241662 new_entry->set_update_virtual_url_with_url(needs_update);
1663
Charlie Reisc0f17d2d2021-01-12 18:52:491664 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411665 // update the virtual URL based on the new URL. For example, this is needed
1666 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1667 // the URL.
[email protected]f8f93eb2012-09-25 03:06:241668 update_virtual_url = needs_update;
Camille Lamy62b826012019-02-26 09:15:471669 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451670 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141671
John Abd-El-Malek380d3c5922017-09-08 00:20:311672 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121673 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141674 UMA_HISTOGRAM_BOOLEAN(
1675 "Navigation.SecureSchemeHasSSLStatus.NewPageNoMatchingEntry",
1676 !!new_entry->GetSSL().certificate);
1677 }
[email protected]e9ba4472008-09-14 15:42:431678 }
1679
Harkiran Bolaria59290d62021-03-17 01:53:011680 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
1681 // for prerendered contents, if any. This is because prerendering/activation
1682 // technically won't be creating a new document. Unlike BFCache, prerendering
1683 // creates a new NavigationEntry rather than using an existing one.
1684 if (!request->IsPrerenderedPageActivation()) {
1685 // Don't use the page type from the pending entry. Some interstitial page
1686 // may have set the type to interstitial. Once we commit, however, the page
1687 // type must always be normal or error.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121688 new_entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1689 : PAGE_TYPE_NORMAL);
Harkiran Bolaria59290d62021-03-17 01:53:011690 new_entry->SetURL(params.url);
1691 if (update_virtual_url)
1692 UpdateVirtualURLToURL(new_entry.get(), params.url);
1693 new_entry->SetReferrer(Referrer(*params.referrer));
1694 new_entry->SetTransitionType(params.transition);
1695 new_entry->set_site_instance(
1696 static_cast<SiteInstanceImpl*>(rfh->GetSiteInstance()));
1697 new_entry->SetOriginalRequestURL(request->GetOriginalRequestURL());
Rakina Zata Amnib597d632020-12-01 00:56:001698
Antonio Sartori4f5373792021-05-31 10:56:471699 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
Harkiran Bolaria59290d62021-03-17 01:53:011700 new_entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
[email protected]e9ba4472008-09-14 15:42:431701
Harkiran Bolaria59290d62021-03-17 01:53:011702 // Update the FrameNavigationEntry for new main frame commits.
1703 FrameNavigationEntry* frame_entry =
1704 new_entry->GetFrameEntry(rfh->frame_tree_node());
1705 frame_entry->set_frame_unique_name(rfh->frame_tree_node()->unique_name());
1706 frame_entry->set_item_sequence_number(params.item_sequence_number);
1707 frame_entry->set_document_sequence_number(params.document_sequence_number);
Nate Chapinfbfe5af2021-06-10 17:22:081708 frame_entry->set_app_history_key(params.app_history_key);
Harkiran Bolaria59290d62021-03-17 01:53:011709 frame_entry->set_redirect_chain(request->GetRedirectChain());
1710 frame_entry->SetPageState(params.page_state);
1711 frame_entry->set_method(params.method);
1712 frame_entry->set_post_id(params.post_id);
1713 frame_entry->set_policy_container_policies(
1714 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1715 request));
Antonio Sartori78a749f2020-11-30 12:03:391716
Anton Bikineevf62d1bf2021-05-15 17:56:071717 if (absl::optional<url::Origin> committed_origin =
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121718 GetCommittedOriginForFrameEntry(params, request)) {
1719 if (committed_origin.has_value())
1720 frame_entry->set_committed_origin(committed_origin.value());
1721 }
Harkiran Bolaria59290d62021-03-17 01:53:011722 if (request->web_bundle_navigation_info()) {
1723 frame_entry->set_web_bundle_navigation_info(
1724 request->web_bundle_navigation_info()->Clone());
1725 }
creis8b5cd4c2015-06-19 00:11:081726
Harkiran Bolaria59290d62021-03-17 01:53:011727 // history.pushState() is classified as a navigation to a new page, but sets
1728 // is_same_document to true. In this case, we already have the title and
1729 // favicon available, so set them immediately.
1730 if (is_same_document && GetLastCommittedEntry()) {
1731 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
1732 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1733 }
[email protected]3a868f212014-08-09 10:41:191734 }
[email protected]ff64b3e2014-05-31 04:07:331735
[email protected]60d6cca2013-04-30 08:47:131736 DCHECK(!params.history_list_was_cleared || !replace_entry);
1737 // The browser requested to clear the session history when it initiated the
1738 // navigation. Now we know that the renderer has updated its state accordingly
1739 // and it is safe to also clear the browser side history.
1740 if (params.history_list_was_cleared) {
arthursonzogni69a6a1b2019-09-17 09:23:001741 DiscardNonCommittedEntries();
[email protected]60d6cca2013-04-30 08:47:131742 entries_.clear();
1743 last_committed_entry_index_ = -1;
1744 }
1745
Nasko Oskovaee2f862018-06-15 00:05:521746 // If this is a new navigation with replacement and there is a
1747 // pending_entry_ which matches the navigation reported by the renderer
1748 // process, then it should be the one replaced, so update the
1749 // last_committed_entry_index_ to use it.
1750 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101751 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521752 last_committed_entry_index_ = pending_entry_index_;
1753 }
1754
Alexander Timine3ec4192020-04-20 16:39:401755 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411756 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401757 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051758
Carlos IL42b416592019-10-07 23:10:361759 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
1760 !request->post_commit_error_page_html().empty());
[email protected]e9ba4472008-09-14 15:42:431761}
1762
Charlie Reisc0f17d2d2021-01-12 18:52:491763void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321764 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071765 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361766 bool is_same_document,
jam48cea9082017-02-15 06:13:291767 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451768 NavigationRequest* request,
Peter Boströmd7592132019-01-30 04:50:311769 bool keep_pending_entry) {
creis26d22632017-04-21 20:23:561770 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1771 << "that a last committed entry exists.";
1772
[email protected]e9ba4472008-09-14 15:42:431773 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001774 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431775
Charlie Reis7e2cb6d2021-01-26 01:27:161776 NavigationEntryImpl* entry = nullptr;
avicbdc4c12015-07-01 16:07:111777 if (params.intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:161778 // We're guaranteed to have a last committed entry if intended_as_new_entry
1779 // is true.
avicbdc4c12015-07-01 16:07:111780 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:161781 DCHECK(entry);
1782
1783 // If the NavigationRequest matches a new pending entry and is classified as
1784 // EXISTING_ENTRY, then it is a navigation to the same URL that was
1785 // converted to a reload, such as a user pressing enter in the omnibox.
1786 if (pending_entry_ && pending_entry_index_ == -1 &&
1787 pending_entry_->GetUniqueID() ==
1788 request->commit_params().nav_entry_id) {
1789 // Note: The pending entry will usually have a real ReloadType here, but
1790 // it can still be ReloadType::NONE in cases that
1791 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
1792
1793 // If we classified this correctly, the SiteInstance should not have
1794 // changed.
1795 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
1796
1797 // For converted reloads, we assign the entry's unique ID to be that of
1798 // the new one. Since this is always the result of a user action, we want
1799 // to dismiss infobars, etc. like a regular user-initiated navigation.
1800 entry->set_unique_id(pending_entry_->GetUniqueID());
1801
1802 // The extra headers may have changed due to reloading with different
1803 // headers.
1804 entry->set_extra_headers(pending_entry_->extra_headers());
1805 }
1806 // Otherwise, this was intended as a new entry but the pending entry was
1807 // lost in the meantime and no new entry was created. We are stuck at the
1808 // last committed entry.
1809
1810 // Even if this is a converted reload from pressing enter in the omnibox,
1811 // the server could redirect, requiring an update to the SSL status. If this
1812 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451813 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:161814 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:471815 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451816 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:161817 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141818
Charlie Reis7e2cb6d2021-01-26 01:27:161819 if (params.url.SchemeIs(url::kHttpsScheme) &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121820 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141821 bool has_cert = !!entry->GetSSL().certificate;
1822 if (is_same_document) {
1823 UMA_HISTOGRAM_BOOLEAN(
1824 "Navigation.SecureSchemeHasSSLStatus."
1825 "ExistingPageSameDocumentIntendedAsNew",
1826 has_cert);
1827 } else {
1828 UMA_HISTOGRAM_BOOLEAN(
1829 "Navigation.SecureSchemeHasSSLStatus."
1830 "ExistingPageDifferentDocumentIntendedAsNew",
1831 has_cert);
1832 }
1833 }
Rakina Zata Amnif6950d552020-11-24 03:26:101834 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:111835 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:101836 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b902016-09-01 16:58:161837
eugenebut604866f2017-05-10 21:35:361838 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451839 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361840 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1841 // this was a restored same document navigation entry, then it won't have
1842 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1843 // navigated it.
jam48cea9082017-02-15 06:13:291844 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
1845 if (entry->GetURL().GetOrigin() == last_entry->GetURL().GetOrigin() &&
1846 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1847 was_restored) {
1848 entry->GetSSL() = last_entry->GetSSL();
1849 }
1850 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451851 // In rapid back/forward navigations |request| sometimes won't have a cert
1852 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191853 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451854 if (request->GetSSLInfo().has_value() &&
1855 request->GetSSLInfo()->is_valid()) {
1856 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
1857 } else if (entry->GetURL().GetOrigin() != request->GetURL().GetOrigin()) {
John Abd-El-Malek3f247082017-12-07 19:02:191858 entry->GetSSL() = SSLStatus();
1859 }
jam48cea9082017-02-15 06:13:291860 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141861
John Abd-El-Malek380d3c5922017-09-08 00:20:311862 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121863 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141864 bool has_cert = !!entry->GetSSL().certificate;
1865 if (is_same_document && was_restored) {
1866 UMA_HISTOGRAM_BOOLEAN(
1867 "Navigation.SecureSchemeHasSSLStatus."
1868 "ExistingPageSameDocumentRestoredBrowserInitiated",
1869 has_cert);
1870 } else if (is_same_document && !was_restored) {
1871 UMA_HISTOGRAM_BOOLEAN(
1872 "Navigation.SecureSchemeHasSSLStatus."
1873 "ExistingPageSameDocumentBrowserInitiated",
1874 has_cert);
1875 } else if (!is_same_document && was_restored) {
1876 UMA_HISTOGRAM_BOOLEAN(
1877 "Navigation.SecureSchemeHasSSLStatus."
1878 "ExistingPageRestoredBrowserInitiated",
1879 has_cert);
1880 } else {
1881 UMA_HISTOGRAM_BOOLEAN(
1882 "Navigation.SecureSchemeHasSSLStatus.ExistingPageBrowserInitiated",
1883 has_cert);
1884 }
1885 }
avicbdc4c12015-07-01 16:07:111886 } else {
1887 // This is renderer-initiated. The only kinds of renderer-initated
Rakina Zata Amni557afb92021-07-17 04:39:571888 // navigations that are EXISTING_ENTRY are same-document navigations that
1889 // result in replacement (e.g. history.replaceState(), location.replace(),
1890 // forced replacements for trivial session history contexts). For these
1891 // cases, we reuse the last committed entry.
avicbdc4c12015-07-01 16:07:111892 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101893
Mikel Astizba9cf2fd2017-12-17 10:38:101894 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1895 // to avoid misleading interpretations (e.g. URLs paired with
1896 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1897 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1898 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1899
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571900 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:101901
eugenebut604866f2017-05-10 21:35:361902 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451903 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361904 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471905 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451906 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141907
John Abd-El-Malek380d3c5922017-09-08 00:20:311908 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121909 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141910 bool has_cert = !!entry->GetSSL().certificate;
1911 if (is_same_document) {
1912 UMA_HISTOGRAM_BOOLEAN(
1913 "Navigation.SecureSchemeHasSSLStatus."
1914 "ExistingPageSameDocumentRendererInitiated",
1915 has_cert);
1916 } else {
1917 UMA_HISTOGRAM_BOOLEAN(
1918 "Navigation.SecureSchemeHasSSLStatus."
1919 "ExistingPageDifferentDocumentRendererInitiated",
1920 has_cert);
1921 }
1922 }
avicbdc4c12015-07-01 16:07:111923 }
1924 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:431925
[email protected]5ccd4dc2012-10-24 02:28:141926 // The URL may have changed due to redirects.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121927 entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1928 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041929 entry->SetURL(params.url);
arthursonzogni73fe3212020-11-17 13:24:071930 entry->SetReferrer(Referrer(*params.referrer));
[email protected]38178a42009-12-17 18:58:321931 if (entry->update_virtual_url_with_url())
1932 UpdateVirtualURLToURL(entry, params.url);
[email protected]5ccd4dc2012-10-24 02:28:141933
jam015ba062017-01-06 21:17:001934 // The site instance will normally be the same except
1935 // 1) session restore, when no site instance will be assigned or
1936 // 2) redirect, when the site instance is reset.
Lei Zhang96031532019-10-10 19:05:471937 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
naskoaf182192016-08-11 02:12:011938 entry->site_instance() == rfh->GetSiteInstance());
clamy432acb22016-04-15 19:41:431939
naskoaf182192016-08-11 02:12:011940 // Update the existing FrameNavigationEntry to ensure all of its members
1941 // reflect the parameters coming from the renderer process.
Anton Bikineevf62d1bf2021-05-15 17:56:071942 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451943 request->common_params().initiator_origin;
naskoaf182192016-08-11 02:12:011944 entry->AddOrUpdateFrameEntry(
Nate Chapin9f169072021-06-09 19:32:371945 rfh->frame_tree_node(), NavigationEntryImpl::UpdatePolicy::kUpdate,
1946 params.item_sequence_number, params.document_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081947 params.app_history_key, rfh->GetSiteInstance(), nullptr, params.url,
Nate Chapin9f169072021-06-09 19:32:371948 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091949 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071950 params.page_state, params.method, params.post_id,
1951 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031952 request->web_bundle_navigation_info()
1953 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391954 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481955 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191956 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1957 request));
creis22a7b4c2016-04-28 07:20:301958
[email protected]5ccd4dc2012-10-24 02:28:141959 // The redirected to page should not inherit the favicon from the previous
1960 // page.
eugenebut604866f2017-05-10 21:35:361961 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:481962 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:141963
Peter Boströmd7592132019-01-30 04:50:311964 // We should also usually discard the pending entry if it corresponds to a
1965 // different navigation, since that one is now likely canceled. In rare
1966 // cases, we leave the pending entry for another navigation in place when we
1967 // know it is still ongoing, to avoid a flicker in the omnibox (see
1968 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:431969 //
1970 // Note that we need to use the "internal" version since we don't want to
1971 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:311972 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001973 DiscardNonCommittedEntries();
[email protected]40bcc302009-03-02 20:50:391974
Carlos IL4dea8902020-05-26 15:14:291975 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:111976 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:431977}
1978
[email protected]d202a7c2012-01-04 07:53:471979void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:321980 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071981 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361982 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:471983 bool replace_entry,
1984 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451985 NavigationRequest* request) {
avi25f5f9e2015-07-17 20:08:261986 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1987 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
[email protected]09b8f82f2009-06-16 20:22:111988
[email protected]e9ba4472008-09-14 15:42:431989 // Manual subframe navigations just get the current entry cloned so the user
1990 // can go back or forward to it. The actual subframe information will be
1991 // stored in the page state for each of those entries. This happens out of
1992 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:091993 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1994 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:381995
Mikel Astizba9cf2fd2017-12-17 10:38:101996 // The DCHECK below documents the fact that we don't know of any situation
1997 // where |replace_entry| is true for subframe navigations. This simplifies
1998 // reasoning about the replacement struct for subframes (see
1999 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
2000 DCHECK(!replace_entry);
2001
Patrick Monette50e8bd82019-06-13 22:40:452002 // This FrameNavigationEntry might not end up being used in the
2003 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Anton Bikineevf62d1bf2021-05-15 17:56:072004 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452005 request->common_params().initiator_origin;
Patrick Monette50e8bd82019-06-13 22:40:452006 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:482007 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:082008 params.document_sequence_number, params.app_history_key,
2009 rfh->GetSiteInstance(), nullptr, params.url,
2010 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:092011 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072012 params.page_state, params.method, params.post_id,
2013 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032014 request->web_bundle_navigation_info()
2015 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392016 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482017 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:192018 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
2019 request));
Charles Reisf44482022017-10-13 21:15:032020
creisce0ef3572017-01-26 17:53:082021 std::unique_ptr<NavigationEntryImpl> new_entry =
2022 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:452023 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:002024 frame_tree_.root());
creise062d542015-08-25 02:01:552025
Alexander Timine3ec4192020-04-20 16:39:402026 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:412027 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:402028 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:472029
creisce0ef3572017-01-26 17:53:082030 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:452031 // to replace, which can happen due to a unique name change. See
2032 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
2033 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:382034
Carlos IL42b416592019-10-07 23:10:362035 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false);
[email protected]e9ba4472008-09-14 15:42:432036}
2037
[email protected]d202a7c2012-01-04 07:53:472038bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:322039 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072040 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392041 bool is_same_document,
Nate Chapinc7019dd7d2021-06-25 18:29:252042 bool was_on_initial_empty_document,
Mohamed Abdelhalim833de902019-09-16 17:41:452043 NavigationRequest* request) {
avi9f07a0c2015-02-18 22:51:292044 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2045 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2046
[email protected]e9ba4472008-09-14 15:42:432047 // We're guaranteed to have a previously committed entry, and we now need to
2048 // handle navigation inside of a subframe in it without creating a new entry.
2049 DCHECK(GetLastCommittedEntry());
2050
creis913c63ce2016-07-16 19:52:522051 // For newly created subframes, we don't need to send a commit notification.
2052 // This is only necessary for history navigations in subframes.
2053 bool send_commit_notification = false;
2054
Rakina Zata Amnif6950d552020-11-24 03:26:102055 // If |nav_entry_id| is non-zero and matches an existing entry, this
2056 // is a history navigation. Update the last committed index accordingly. If
2057 // we don't recognize the |nav_entry_id|, it might be a recently
2058 // pruned entry. We'll handle it below.
2059 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2060 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472061 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062062 // Make sure that a subframe commit isn't changing the main frame's
2063 // origin. Otherwise the renderer process may be confused, leading to a
2064 // URL spoof. We can't check the path since that may change
2065 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572066 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2067 // about:blank, file, and unique origins are more subtle to get right.
Charlie Reis95fbca442021-05-21 21:38:242068 // We should use checks similar to RenderFrameHostImpl's
2069 // CanCommitUrlAndOrigin on the main frame during subframe commits.
2070 // See https://crbug.com/1209092.
creis37988b92016-06-10 18:03:572071 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2072 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2073 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2074 dest_top_url.SchemeIsHTTPOrHTTPS() &&
2075 current_top_url.GetOrigin() != dest_top_url.GetOrigin()) {
creisfb6eeb62016-05-10 19:01:512076 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2077 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062078 }
creis3cdc3b02015-05-29 23:00:472079
creis913c63ce2016-07-16 19:52:522080 // We only need to discard the pending entry in this history navigation
2081 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062082 last_committed_entry_index_ = entry_index;
arthursonzogni69a6a1b2019-09-17 09:23:002083 DiscardNonCommittedEntries();
creis913c63ce2016-07-16 19:52:522084
2085 // History navigations should send a commit notification.
2086 send_commit_notification = true;
avi98405c22015-05-21 20:47:062087 }
[email protected]e9ba4472008-09-14 15:42:432088 }
[email protected]f233e4232013-02-23 00:55:142089
creisce0ef3572017-01-26 17:53:082090 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2091 // it may be a "history auto" case where we update an existing one.
2092 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Nate Chapin9f169072021-06-09 19:32:372093
2094 // We may want to update |last_committed|'s FrameNavigationEntry (if one
2095 // exists), or we may want to clobber it and create a new one. We update in
2096 // cases where the corresponding FrameNavigationEntry is conceptually similar
2097 // to the document described by the commit params: same-document
2098 // navigations, history traversal to an existing entry, and reloads (including
2099 // a "soft reload" where we navigate to the same url without flagging it as a
2100 // reload). But in the case of a different document that is not logically
2101 // related to the committed FrameNavigationEntry's document (cross-document,
2102 // not same url, not a reload, not a history traversal), we replace rather
2103 // than update.
Nate Chapinc7019dd7d2021-06-25 18:29:252104 //
Nate Chapin9f169072021-06-09 19:32:372105 // In the case where we update, the FrameNavigationEntry will potentially be
2106 // shared across multiple NavigationEntries, and any updates will be reflected
2107 // in all of those NavigationEntries. In the replace case, any existing
2108 // sharing with other NavigationEntries will stop.
Nate Chapinc7019dd7d2021-06-25 18:29:252109 //
2110 // When navigating away from the initial empty document, we also update rather
2111 // than replace. Either update or replace will overwrite the initial empty
2112 // document state for |last_committed|, but if the FrameNavigationEntry for
2113 // the initial empty document is shared across multiple NavigationEntries (due
2114 // to a navigation in another frame), we want to make sure we overwrite the
2115 // initial empty document state everywhere this FrameNavigationEntry is used,
2116 // which is accompished by updating the existing FrameNavigationEntry.
Nate Chapin9f169072021-06-09 19:32:372117 FrameNavigationEntry* last_committed_frame_entry =
2118 last_committed->GetFrameEntry(rfh->frame_tree_node());
2119 NavigationEntryImpl::UpdatePolicy update_policy =
2120 NavigationEntryImpl::UpdatePolicy::kUpdate;
2121 if (request->common_params().navigation_type ==
Minggang Wangb9f3fa92021-07-01 15:30:312122 blink::mojom::NavigationType::DIFFERENT_DOCUMENT &&
Nate Chapin9f169072021-06-09 19:32:372123 last_committed_frame_entry &&
Nate Chapinc7019dd7d2021-06-25 18:29:252124 last_committed_frame_entry->url() != params.url &&
2125 !was_on_initial_empty_document) {
Nate Chapin9f169072021-06-09 19:32:372126 update_policy = NavigationEntryImpl::UpdatePolicy::kReplace;
2127 }
2128
Anton Bikineevf62d1bf2021-05-15 17:56:072129 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452130 request->common_params().initiator_origin;
creisce0ef3572017-01-26 17:53:082131 last_committed->AddOrUpdateFrameEntry(
Nate Chapin9f169072021-06-09 19:32:372132 rfh->frame_tree_node(), update_policy, params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:082133 params.document_sequence_number, params.app_history_key,
2134 rfh->GetSiteInstance(), nullptr, params.url,
2135 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:092136 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072137 params.page_state, params.method, params.post_id,
2138 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032139 request->web_bundle_navigation_info()
2140 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392141 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482142 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:192143 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
2144 request));
creis625a0c7d2015-03-24 23:17:122145
creis913c63ce2016-07-16 19:52:522146 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432147}
2148
[email protected]d202a7c2012-01-04 07:53:472149int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232150 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032151 for (size_t i = 0; i < entries_.size(); ++i) {
2152 if (entries_[i].get() == entry)
2153 return i;
2154 }
2155 return -1;
[email protected]765b35502008-08-21 00:51:202156}
2157
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572158void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242159 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572160 NavigationControllerImpl* source =
2161 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572162 // Verify that we look new.
Lei Zhang96031532019-10-10 19:05:472163 DCHECK_EQ(0, GetEntryCount());
2164 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:572165
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572166 if (source->GetEntryCount() == 0)
[email protected]ce3fa3c2009-04-20 19:55:572167 return; // Nothing new to do.
2168
Francois Dorayeaace782017-06-21 16:37:242169 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442170 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572171 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572172
Fergal Dalya1d569972021-03-16 03:24:532173 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302174 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532175 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2176 session_storage_namespace_map_[it.first] = source_namespace->Clone();
Aaron Colwellb731a0ae2021-03-19 19:14:472177 OnStoragePartitionIdAdded(it.first);
[email protected]fdac6ade2013-07-20 01:06:302178 }
[email protected]4e6419c2010-01-15 04:50:342179
Lukasz Anforowicz0de0f452020-12-02 19:57:152180 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572181}
2182
Aran Gilman37d11632019-10-08 23:07:152183void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2184 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162185 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012186 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162187
[email protected]d202a7c2012-01-04 07:53:472188 NavigationControllerImpl* source =
2189 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252190
avi2b177592014-12-10 02:08:022191 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012192 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252193
[email protected]474f8512013-05-31 22:31:162194 // We now have one entry, possibly with a new pending entry. Ensure that
2195 // adding the entries from source won't put us over the limit.
2196 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572197 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572198 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252199
Carlos IL4dea8902020-05-26 15:14:292200 // Insert the entries from source. Ignore any pending entry, since it has not
2201 // committed in source.
[email protected]474f8512013-05-31 22:31:162202 int max_source_index = source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:252203 if (max_source_index == -1)
[email protected]a26023822011-12-29 00:23:552204 max_source_index = source->GetEntryCount();
[email protected]e1cd5452010-08-26 18:03:252205 else
2206 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572207
2208 // Ignore the source's current entry if merging with replacement.
2209 // TODO(davidben): This should preserve entries forward of the current
2210 // too. http://crbug.com/317872
2211 if (replace_entry && max_source_index > 0)
2212 max_source_index--;
2213
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572214 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252215
2216 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552217 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142218
Hayato Ito2c8c08d02021-06-23 03:38:432219 BroadcastHistoryOffsetAndLength();
[email protected]e1cd5452010-08-26 18:03:252220}
2221
[email protected]79368982013-11-13 01:11:012222bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162223 // If there is no last committed entry, we cannot prune. Even if there is a
2224 // pending entry, it may not commit, leaving this WebContents blank, despite
2225 // possibly giving it new entries via CopyStateFromAndPrune.
2226 if (last_committed_entry_index_ == -1)
2227 return false;
[email protected]9350602e2013-02-26 23:27:442228
[email protected]474f8512013-05-31 22:31:162229 // We cannot prune if there is a pending entry at an existing entry index.
2230 // It may not commit, so we have to keep the last committed entry, and thus
2231 // there is no sensible place to keep the pending entry. It is ok to have
2232 // a new pending entry, which can optionally commit as a new navigation.
2233 if (pending_entry_index_ != -1)
2234 return false;
2235
[email protected]474f8512013-05-31 22:31:162236 return true;
2237}
2238
[email protected]79368982013-11-13 01:11:012239void NavigationControllerImpl::PruneAllButLastCommitted() {
2240 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162241
avi2b177592014-12-10 02:08:022242 DCHECK_EQ(0, last_committed_entry_index_);
2243 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442244
Hayato Ito2c8c08d02021-06-23 03:38:432245 BroadcastHistoryOffsetAndLength();
[email protected]9350602e2013-02-26 23:27:442246}
2247
[email protected]79368982013-11-13 01:11:012248void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162249 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012250 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262251
[email protected]474f8512013-05-31 22:31:162252 // Erase all entries but the last committed entry. There may still be a
2253 // new pending entry after this.
2254 entries_.erase(entries_.begin(),
2255 entries_.begin() + last_committed_entry_index_);
2256 entries_.erase(entries_.begin() + 1, entries_.end());
2257 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262258}
2259
Christian Dullweber1af31e62018-02-22 11:49:482260void NavigationControllerImpl::DeleteNavigationEntries(
2261 const DeletionPredicate& deletionPredicate) {
2262 // It is up to callers to check the invariants before calling this.
2263 CHECK(CanPruneAllButLastCommitted());
2264 std::vector<int> delete_indices;
2265 for (size_t i = 0; i < entries_.size(); i++) {
2266 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572267 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482268 delete_indices.push_back(i);
2269 }
2270 }
2271 if (delete_indices.empty())
2272 return;
2273
2274 if (delete_indices.size() == GetEntryCount() - 1U) {
2275 PruneAllButLastCommitted();
2276 } else {
2277 // Do the deletion in reverse to preserve indices.
2278 for (auto it = delete_indices.rbegin(); it != delete_indices.rend(); ++it) {
2279 RemoveEntryAtIndex(*it);
2280 }
Hayato Ito2c8c08d02021-06-23 03:38:432281 BroadcastHistoryOffsetAndLength();
Christian Dullweber1af31e62018-02-22 11:49:482282 }
2283 delegate()->NotifyNavigationEntriesDeleted();
2284}
2285
Shivani Sharma883f5f32019-02-12 18:20:012286bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2287 auto* entry = GetEntryAtIndex(index);
2288 return entry && entry->should_skip_on_back_forward_ui();
2289}
2290
Carlos Caballero35ce710c2019-09-19 10:59:452291BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2292 return back_forward_cache_;
2293}
2294
clamy987a3752018-05-03 17:36:262295void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2296 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2297 // progress, since this will cause a use-after-free. (We only allow this
2298 // when the tab is being destroyed for shutdown, since it won't return to
2299 // NavigateToEntry in that case.) http://crbug.com/347742.
Takashi Toyoshimaea534ef22021-07-21 03:27:592300 CHECK(!in_navigate_to_pending_entry_ || frame_tree_.IsBeingDestroyed());
clamy987a3752018-05-03 17:36:262301
2302 if (was_failure && pending_entry_) {
2303 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2304 } else {
2305 failed_pending_entry_id_ = 0;
2306 }
2307
2308 if (pending_entry_) {
2309 if (pending_entry_index_ == -1)
2310 delete pending_entry_;
2311 pending_entry_index_ = -1;
2312 pending_entry_ = nullptr;
2313 }
arthursonzogni66f711c2019-10-08 14:40:362314
2315 // Ensure any refs to the current pending entry are ignored if they get
2316 // deleted, by clearing the set of known refs. All future pending entries will
2317 // only be affected by new refs.
2318 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262319}
2320
2321void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2322 if (pending_entry_)
2323 pending_entry_->set_ssl_error(error);
2324}
2325
Camille Lamy5193caa2018-10-12 11:59:422326#if defined(OS_ANDROID)
2327// static
2328bool NavigationControllerImpl::ValidateDataURLAsString(
2329 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2330 if (!data_url_as_string)
2331 return false;
2332
2333 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2334 return false;
2335
2336 // The number of characters that is enough for validating a data: URI.
2337 // From the GURL's POV, the only important part here is scheme, it doesn't
2338 // check the actual content. Thus we can take only the prefix of the url, to
2339 // avoid unneeded copying of a potentially long string.
2340 const size_t kDataUriPrefixMaxLen = 64;
2341 GURL data_url(
2342 std::string(data_url_as_string->front_as<char>(),
2343 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2344 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2345 return false;
2346
2347 return true;
2348}
2349#endif
2350
Shivani Sharma194877032019-03-07 17:52:472351void NavigationControllerImpl::NotifyUserActivation() {
2352 // When a user activation occurs, ensure that all adjacent entries for the
2353 // same document clear their skippable bit, so that the history manipulation
2354 // intervention does not apply to them.
2355 auto* last_committed_entry = GetLastCommittedEntry();
2356 if (!last_committed_entry)
2357 return;
Shivani Sharma194877032019-03-07 17:52:472358
shivanigithub99368382021-06-16 18:33:372359 if (base::FeatureList::IsEnabled(
2360 features::kDebugHistoryInterventionNoUserActivation)) {
2361 return;
2362 }
2363
Shivani Sharmac4cc8922019-04-18 03:11:172364 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472365}
2366
clamy987a3752018-05-03 17:36:262367bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2368 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172369 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262370 NavigationEntryImpl* entry =
2371 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2372 if (!entry)
2373 return false;
2374
2375 FrameNavigationEntry* frame_entry =
2376 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2377 if (!frame_entry)
2378 return false;
2379
Camille Lamy5193caa2018-10-12 11:59:422380 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572381 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232382 ReloadType::NONE, false /* is_same_document_history_load */,
Kush Sinhaa8856ecb2021-09-01 14:28:312383 true /* is_history_navigation_in_new_child */);
clamyea99ea12018-05-28 13:54:232384
2385 if (!request)
2386 return false;
2387
arthursonzognif046d4a2019-12-12 19:08:102388 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412389
Lukasz Anforowicz9ee83c272020-12-01 20:14:052390 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2391 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232392
2393 return true;
clamy987a3752018-05-03 17:36:262394}
2395
Tsuyoshi Horo52fd08e2020-07-07 07:03:452396bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2397 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2398 if (!entry)
2399 return false;
2400 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2401 if (!frame_entry)
2402 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142403 ReloadType reload_type = ReloadType::NORMAL;
2404 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452405 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142406 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452407 false /* is_same_document_history_load */,
Kush Sinhaa8856ecb2021-09-01 14:28:312408 false /* is_history_navigation_in_new_child */);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452409 if (!request)
2410 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052411 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452412 return true;
2413}
2414
Dave Tapuska8bfd84c2019-03-26 20:47:162415void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2416 int offset,
2417 int sandbox_frame_tree_node_id) {
2418 if (!CanGoToOffset(offset))
2419 return;
Kush Sinhaa8856ecb2021-09-01 14:28:312420 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id);
Dave Tapuska8bfd84c2019-03-26 20:47:162421}
2422
clamy987a3752018-05-03 17:36:262423void NavigationControllerImpl::NavigateFromFrameProxy(
2424 RenderFrameHostImpl* render_frame_host,
2425 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022426 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452427 int initiator_process_id,
Anton Bikineevf62d1bf2021-05-15 17:56:072428 const absl::optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262429 bool is_renderer_initiated,
2430 SiteInstance* source_site_instance,
2431 const Referrer& referrer,
2432 ui::PageTransition page_transition,
2433 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252434 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262435 const std::string& method,
2436 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092437 const std::string& extra_headers,
Antonio Sartori2f763d9d2021-04-21 10:04:142438 network::mojom::SourceLocationPtr source_location,
John Delaney50425f82020-04-07 16:26:212439 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Anton Bikineevf62d1bf2021-05-15 17:56:072440 const absl::optional<blink::Impression>& impression) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582441 if (is_renderer_initiated)
2442 DCHECK(initiator_origin.has_value());
2443
clamy987a3752018-05-03 17:36:262444 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582445
Rakina Zata Amni66992a82020-06-24 03:57:522446 // Don't allow an entry replacement if there is no entry to replace.
2447 // http://crbug.com/457149
2448 if (GetEntryCount() == 0)
2449 should_replace_current_entry = false;
2450
clamy987a3752018-05-03 17:36:262451 // Create a NavigationEntry for the transfer, without making it the pending
2452 // entry. Subframe transfers should have a clone of the last committed entry
2453 // with a FrameNavigationEntry for the target frame. Main frame transfers
2454 // should have a new NavigationEntry.
2455 // TODO(creis): Make this unnecessary by creating (and validating) the params
2456 // directly, passing them to the destination RenderFrameHost. See
2457 // https://crbug.com/536906.
2458 std::unique_ptr<NavigationEntryImpl> entry;
2459 if (!node->IsMainFrame()) {
2460 // Subframe case: create FrameNavigationEntry.
2461 if (GetLastCommittedEntry()) {
2462 entry = GetLastCommittedEntry()->Clone();
2463 entry->set_extra_headers(extra_headers);
2464 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2465 // Renderer-initiated navigation that target a remote frame are currently
2466 // classified as browser-initiated when this one has already navigated.
2467 // See https://crbug.com/722251.
2468 } else {
2469 // If there's no last committed entry, create an entry for about:blank
2470 // with a subframe entry for our destination.
2471 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2472 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:062473 GURL(url::kAboutBlankURL), referrer, initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:102474 source_site_instance, page_transition, is_renderer_initiated,
2475 extra_headers, browser_context_,
Hayato Ito303654c2021-06-30 09:07:542476 nullptr /* blob_url_loader_factory */));
clamy987a3752018-05-03 17:36:262477 }
Nate Chapin9f169072021-06-09 19:32:372478 // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter
2479 // to CreateNavigationRequestFromLoadParams(), so while kReplace might
2480 // remove child FrameNavigationEntries (e.g., if this is a cross-process
2481 // same-document navigation), they will still be present in the
2482 // committed NavigationEntry that will be referenced to construct the new
2483 // FrameNavigationEntry tree when this navigation commits.
clamy987a3752018-05-03 17:36:262484 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:082485 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582486 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072487 absl::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202488 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392489 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482490 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori79d549d2021-02-18 12:59:542491 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262492 } else {
2493 // Main frame case.
2494 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102495 url, referrer, initiator_origin, source_site_instance, page_transition,
2496 is_renderer_initiated, extra_headers, browser_context_,
Hayato Ito303654c2021-06-30 09:07:542497 blob_url_loader_factory));
clamy987a3752018-05-03 17:36:262498 entry->root_node()->frame_entry->set_source_site_instance(
2499 static_cast<SiteInstanceImpl*>(source_site_instance));
2500 entry->root_node()->frame_entry->set_method(method);
2501 }
clamy987a3752018-05-03 17:36:262502
Camille Lamy5193caa2018-10-12 11:59:422503 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262504 if (GetLastCommittedEntry() &&
2505 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2506 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422507 override_user_agent = true;
clamy987a3752018-05-03 17:36:262508 }
2509 // TODO(creis): Set user gesture and intent received timestamp on Android.
2510
2511 // We may not have successfully added the FrameNavigationEntry to |entry|
2512 // above (per https://crbug.com/608402), in which case we create it from
2513 // scratch. This works because we do not depend on |frame_entry| being inside
2514 // |entry| during NavigateToEntry. This will go away when we shortcut this
2515 // further in https://crbug.com/536906.
2516 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2517 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452518 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Nate Chapinfbfe5af2021-06-10 17:22:082519 node->unique_name(), -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582520 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072521 absl::nullopt /* origin */, referrer, initiator_origin,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:122522 std::vector<GURL>(), blink::PageState(), method, -1,
2523 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482524 nullptr /* subresource_web_bundle_navigation_info */,
Titouan Rigoudy6ec70402021-02-02 15:42:192525 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262526 }
2527
Camille Lamy5193caa2018-10-12 11:59:422528 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022529 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452530 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292531 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422532 params.source_site_instance = source_site_instance;
2533 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2534 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032535 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422536 params.referrer = referrer;
2537 /* params.redirect_chain: skip */
2538 params.extra_headers = extra_headers;
2539 params.is_renderer_initiated = is_renderer_initiated;
2540 params.override_user_agent = UA_OVERRIDE_INHERIT;
2541 /* params.base_url_for_data_url: skip */
2542 /* params.virtual_url_for_data_url: skip */
2543 /* params.data_url_as_string: skip */
2544 params.post_data = post_body;
2545 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582546 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422547 /* params.frame_name: skip */
2548 // TODO(clamy): See if user gesture should be propagated to this function.
2549 params.has_user_gesture = false;
2550 params.should_clear_history_list = false;
2551 params.started_from_context_menu = false;
2552 /* params.navigation_ui_data: skip */
2553 /* params.input_start: skip */
Minggang Wangf59db47b2021-06-16 01:56:222554 params.was_activated = blink::mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542555 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212556 params.impression = impression;
Antonio Sartori6984c742021-08-26 08:03:412557 params.download_policy = std::move(download_policy);
Camille Lamy5193caa2018-10-12 11:59:422558
2559 std::unique_ptr<NavigationRequest> request =
2560 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032561 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:142562 false /* has_user_gesture */, std::move(source_location),
Antonio Sartori6984c742021-08-26 08:03:412563 ReloadType::NONE, entry.get(), frame_entry.get());
clamyea99ea12018-05-28 13:54:232564
2565 if (!request)
2566 return;
2567
Arthur Hemery948742762019-09-18 10:06:242568 // At this stage we are proceeding with this navigation. If this was renderer
2569 // initiated with user gesture, we need to make sure we clear up potential
2570 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2571 DiscardNonCommittedEntries();
2572
Lukasz Anforowicz9ee83c272020-12-01 20:14:052573 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262574}
2575
[email protected]d1198fd2012-08-13 22:50:192576void NavigationControllerImpl::SetSessionStorageNamespace(
Aaron Colwellf3b316e2021-03-11 20:17:052577 const StoragePartitionId& partition_id,
[email protected]8ff00d72012-10-23 19:12:212578 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192579 if (!session_storage_namespace)
2580 return;
2581
2582 // We can't overwrite an existing SessionStorage without violating spec.
2583 // Attempts to do so may give a tab access to another tab's session storage
2584 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152585 bool successful_insert =
2586 session_storage_namespace_map_
Aaron Colwellf3b316e2021-03-11 20:17:052587 .insert(std::make_pair(partition_id,
2588 static_cast<SessionStorageNamespaceImpl*>(
2589 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302590 .second;
2591 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
Aaron Colwellb731a0ae2021-03-19 19:14:472592 OnStoragePartitionIdAdded(partition_id);
[email protected]d1198fd2012-08-13 22:50:192593}
2594
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572595bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Aran Gilman37d11632019-10-08 23:07:152596 return IsInitialNavigation() && !GetLastCommittedEntry() &&
Carlos Caballeroede6f8c2021-01-28 11:01:502597 !frame_tree_.has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462598}
2599
Aran Gilman37d11632019-10-08 23:07:152600SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Aaron Colwell78b4bde2021-03-16 16:16:092601 const SiteInfo& site_info) {
2602 // TODO(acolwell): Remove partition_id logic once we have successfully
2603 // migrated the implementation to be a StoragePartitionConfig. At that point
2604 // |site_info| can be replaced with a StoragePartitionConfig.
2605 const StoragePartitionId partition_id =
2606 site_info.GetStoragePartitionId(browser_context_);
2607 const StoragePartitionConfig partition_config =
Robbie McElrath7d4bd852021-07-24 04:02:192608 site_info.storage_partition_config();
[email protected]d1198fd2012-08-13 22:50:192609
[email protected]fdac6ade2013-07-20 01:06:302610 StoragePartition* partition =
Lukasz Anforowiczb9a969a2021-04-29 15:26:252611 browser_context_->GetStoragePartition(partition_config);
michaelnbacbcbd2016-02-09 00:32:032612 DOMStorageContextWrapper* context_wrapper =
2613 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2614
2615 SessionStorageNamespaceMap::const_iterator it =
2616 session_storage_namespace_map_.find(partition_id);
2617 if (it != session_storage_namespace_map_.end()) {
2618 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152619 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2620 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:472621
2622 // Verify that the config we generated now matches the one that
2623 // was generated when the namespace was added to the map.
Aaron Colwellb731a0ae2021-03-19 19:14:472624 if (partition_config != it->first.config()) {
2625 LogStoragePartitionIdCrashKeys(it->first, partition_id);
2626 }
Sharon Yangd4f48792021-06-30 17:02:532627 CHECK_EQ(partition_config, it->first.config());
Aaron Colwellb731a0ae2021-03-19 19:14:472628
michaelnbacbcbd2016-02-09 00:32:032629 return it->second.get();
2630 }
2631
2632 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102633 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2634 SessionStorageNamespaceImpl::Create(context_wrapper);
2635 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2636 session_storage_namespace.get();
2637 session_storage_namespace_map_[partition_id] =
2638 std::move(session_storage_namespace);
Aaron Colwellb731a0ae2021-03-19 19:14:472639 OnStoragePartitionIdAdded(partition_id);
[email protected]fdac6ade2013-07-20 01:06:302640
Daniel Murphy31bbb8b12018-02-07 21:44:102641 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302642}
2643
2644SessionStorageNamespace*
2645NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Robbie McElrath7d4bd852021-07-24 04:02:192646 return GetSessionStorageNamespace(SiteInfo(GetBrowserContext()));
[email protected]fdac6ade2013-07-20 01:06:302647}
2648
2649const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572650NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302651 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552652}
[email protected]d202a7c2012-01-04 07:53:472653
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572654bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562655 return needs_reload_;
2656}
[email protected]a26023822011-12-29 00:23:552657
[email protected]46bb5e9c2013-10-03 22:16:472658void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412659 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2660}
2661
2662void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472663 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412664 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542665
2666 if (last_committed_entry_index_ != -1) {
2667 entries_[last_committed_entry_index_]->SetTransitionType(
2668 ui::PAGE_TRANSITION_RELOAD);
2669 }
[email protected]46bb5e9c2013-10-03 22:16:472670}
2671
[email protected]d202a7c2012-01-04 07:53:472672void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572673 DCHECK_LT(index, GetEntryCount());
2674 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312675 DiscardNonCommittedEntries();
2676
2677 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122678 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312679 last_committed_entry_index_--;
2680}
2681
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572682NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272683 // If there is no pending_entry_, there should be no pending_entry_index_.
2684 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2685
2686 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362687 // at that index. An exception is while a reload of a post commit error page
2688 // is ongoing; in that case pending entry will point to the entry replaced
2689 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272690 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362691 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2692 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272693
[email protected]022af742011-12-28 18:37:252694 return pending_entry_;
2695}
2696
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572697int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272698 // The pending entry index must always be less than the number of entries.
2699 // If there are no entries, it must be exactly -1.
2700 DCHECK_LT(pending_entry_index_, GetEntryCount());
2701 DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:552702 return pending_entry_index_;
2703}
2704
avi25764702015-06-23 15:43:372705void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572706 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362707 bool replace,
2708 bool was_post_commit_error) {
mastiz7eddb5f72016-06-23 09:52:452709 DCHECK(!ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2710 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202711
avi5cad4912015-06-19 05:25:442712 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2713 // need to keep continuity with the pending entry, so copy the pending entry's
2714 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2715 // then the renderer navigated on its own, independent of the pending entry,
2716 // so don't copy anything.
2717 if (pending_entry_ && pending_entry_index_ == -1)
2718 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202719
arthursonzogni69a6a1b2019-09-17 09:23:002720 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:202721
creisee17e932015-07-17 17:56:222722 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362723 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102724 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572725 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362726 // If the new entry is a post-commit error page, we store the current last
2727 // committed entry to the side so that we can put it back when navigating
2728 // away from the error.
2729 if (was_post_commit_error) {
2730 DCHECK(!entry_replaced_by_post_commit_error_);
2731 entry_replaced_by_post_commit_error_ =
2732 std::move(entries_[last_committed_entry_index_]);
2733 }
dcheng36b6aec92015-12-26 06:16:362734 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222735 return;
2736 }
[email protected]765b35502008-08-21 00:51:202737
creis37979a62015-08-04 19:48:182738 // We shouldn't see replace == true when there's no committed entries.
2739 DCHECK(!replace);
2740
Michael Thiessen9b14d512019-09-23 21:19:472741 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202742
Shivani Sharmad8c8d652019-02-13 17:27:572743 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202744
dcheng36b6aec92015-12-26 06:16:362745 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202746 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292747}
2748
Shivani Sharmad8c8d652019-02-13 17:27:572749void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162750 if (entries_.size() < max_entry_count())
2751 return;
2752
2753 DCHECK_EQ(max_entry_count(), entries_.size());
2754 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572755 CHECK_EQ(pending_entry_index_, -1);
2756
2757 int index = 0;
Elly Fong-Jonesccc6d1f2021-06-14 18:32:422758 // Retrieve the oldest skippable entry.
2759 for (; index < GetEntryCount(); index++) {
2760 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2761 break;
Shivani Sharmad8c8d652019-02-13 17:27:572762 }
2763
2764 // If there is no skippable entry or if it is the last committed entry then
2765 // fall back to pruning the oldest entry. It is not safe to prune the last
2766 // committed entry.
2767 if (index == GetEntryCount() || index == last_committed_entry_index_)
2768 index = 0;
2769
2770 bool should_succeed = RemoveEntryAtIndex(index);
2771 DCHECK_EQ(true, should_succeed);
2772
2773 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252774}
2775
clamy3cb9bea92018-07-10 12:42:022776void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162777 ReloadType reload_type,
Kush Sinhaa8856ecb2021-09-01 14:28:312778 int sandboxed_source_frame_tree_node_id) {
Alexander Timin3a92df72019-09-20 11:59:502779 TRACE_EVENT0("navigation",
2780 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272781 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022782 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362783 if (pending_entry_index_ != -1) {
2784 // The pending entry may not be in entries_ if a post-commit error page is
2785 // showing.
2786 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2787 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2788 }
Gyuyoung Kim107c2a02021-04-13 01:49:302789 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572790 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:012791 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:002792 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:572793 int nav_entry_id = pending_entry_->GetUniqueID();
2794
[email protected]83c2e232011-10-07 21:36:462795 // If we were navigating to a slow-to-commit page, and the user performs
2796 // a session history navigation to the last committed page, RenderViewHost
2797 // will force the throbber to start, but WebKit will essentially ignore the
2798 // navigation, and won't send a message to stop the throbber. To prevent this
2799 // from happening, we drop the navigation here and stop the slow-to-commit
2800 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022801 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:082802 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:272803 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
Takashi Toyoshimabf549472021-07-01 07:47:202804 frame_tree_.StopLoading();
[email protected]6a13a6c2011-12-20 21:47:122805
[email protected]83c2e232011-10-07 21:36:462806 DiscardNonCommittedEntries();
2807 return;
2808 }
2809
creisce0ef3572017-01-26 17:53:082810 // Compare FrameNavigationEntries to see which frames in the tree need to be
2811 // navigated.
clamy3cb9bea92018-07-10 12:42:022812 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2813 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Kush Sinhaa8856ecb2021-09-01 14:28:312814 FindFramesToNavigate(root, reload_type, &same_document_loads,
2815 &different_document_loads);
creis4e2ecb72015-06-20 00:46:302816
2817 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572818 // We were unable to match any frames to navigate. This can happen if a
2819 // history navigation targets a subframe that no longer exists
2820 // (https://crbug.com/705550). In this case, we need to update the current
2821 // history entry to the pending one but keep the main document loaded. We
2822 // also need to ensure that observers are informed about the updated
2823 // current history entry (e.g., for greying out back/forward buttons), and
2824 // that renderer processes update their history offsets. The easiest way
2825 // to do all that is to schedule a "redundant" same-document navigation in
2826 // the main frame.
2827 //
2828 // Note that we don't want to remove this history entry, as it might still
2829 // be valid later, since a frame that it's targeting may be recreated.
2830 //
2831 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
2832 // need to repeat history navigations until finding the one that works.
2833 // Consider changing this behavior to keep looking for the first valid
2834 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:022835 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422836 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572837 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:572838 ReloadType::NONE /* reload_type */,
2839 true /* is_same_document_history_load */,
Kush Sinhaa8856ecb2021-09-01 14:28:312840 false /* is_history_navigation_in_new_child */);
clamy3cb9bea92018-07-10 12:42:022841 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572842 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:022843 DiscardPendingEntry(false);
2844 return;
2845 }
Alex Moshchuk3a4e77a2020-05-29 21:32:572846 same_document_loads.push_back(std::move(navigation_request));
2847
2848 // Sanity check that we never take this branch for any kinds of reloads,
2849 // as those should've queued a different-document load in the main frame.
2850 DCHECK(!is_forced_reload);
2851 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:302852 }
2853
Dave Tapuska8bfd84c2019-03-26 20:47:162854 // If |sandboxed_source_frame_node_id| is valid, then track whether this
2855 // navigation affects any frame outside the frame's subtree.
2856 if (sandboxed_source_frame_tree_node_id !=
2857 FrameTreeNode::kFrameTreeNodeInvalidId) {
2858 bool navigates_inside_tree =
2859 DoesSandboxNavigationStayWithinSubtree(
2860 sandboxed_source_frame_tree_node_id, same_document_loads) &&
2861 DoesSandboxNavigationStayWithinSubtree(
2862 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:502863 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:162864 // the number of pages that trigger this.
2865 FrameTreeNode* sandbox_source_frame_tree_node =
2866 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
2867 if (sandbox_source_frame_tree_node) {
2868 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
2869 sandbox_source_frame_tree_node->current_frame_host(),
2870 navigates_inside_tree
2871 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
2872 : blink::mojom::WebFeature::
2873 kSandboxBackForwardAffectsFramesOutsideSubtree);
2874 }
Dave Tapuska855c1e12019-08-23 20:45:522875
2876 // If the navigation occurred outside the tree discard it because
2877 // the sandboxed frame didn't have permission to navigate outside
2878 // its tree. If it is possible that the navigation is both inside and
2879 // outside the frame tree and we discard it entirely because we don't
2880 // want to end up in a history state that didn't exist before.
2881 if (base::FeatureList::IsEnabled(
2882 features::kHistoryPreventSandboxedNavigation) &&
2883 !navigates_inside_tree) {
2884 DiscardPendingEntry(false);
2885 return;
2886 }
Dave Tapuska8bfd84c2019-03-26 20:47:162887 }
2888
Carlos Caballero539a421c2020-07-06 10:25:572889 // BackForwardCache:
2890 // Navigate immediately if the document is in the BackForwardCache.
2891 if (back_forward_cache_.GetEntry(nav_entry_id)) {
2892 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
2893 DCHECK_EQ(reload_type, ReloadType::NONE);
2894 auto navigation_request = CreateNavigationRequestFromEntry(
2895 root, pending_entry_, pending_entry_->GetFrameEntry(root),
2896 ReloadType::NONE, false /* is_same_document_history_load */,
Kush Sinhaa8856ecb2021-09-01 14:28:312897 false /* is_history_navigation_in_new_child */);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052898 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:572899
2900 return;
2901 }
2902
2903 // History navigation might try to reuse a specific BrowsingInstance, already
2904 // used by a page in the cache. To avoid having two different main frames that
2905 // live in the same BrowsingInstance, evict the all pages with this
2906 // BrowsingInstance from the cache.
2907 //
2908 // For example, take the following scenario:
2909 //
2910 // A1 = Some page on a.com
2911 // A2 = Some other page on a.com
2912 // B3 = An uncacheable page on b.com
2913 //
2914 // Then the following navigations occur:
2915 // A1->A2->B3->A1
2916 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
2917 // take its place) and A1 will be created in the same BrowsingInstance (and
2918 // SiteInstance), as A2.
2919 //
2920 // If we didn't do anything, both A1 and A2 would remain alive in the same
2921 // BrowsingInstance/SiteInstance, which is unsupported by
2922 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
2923 // A2 from the cache.
2924 if (pending_entry_->site_instance()) {
2925 back_forward_cache_.EvictFramesInRelatedSiteInstances(
2926 pending_entry_->site_instance());
2927 }
2928
clamy3cb9bea92018-07-10 12:42:022929 // This call does not support re-entrancy. See http://crbug.com/347742.
2930 CHECK(!in_navigate_to_pending_entry_);
2931 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:302932
arthursonzogni66f711c2019-10-08 14:40:362933 // It is not possible to delete the pending NavigationEntry while navigating
2934 // to it. Grab a reference to delay potential deletion until the end of this
2935 // function.
2936 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
2937
creis4e2ecb72015-06-20 00:46:302938 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:022939 for (auto& item : same_document_loads) {
2940 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052941 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302942 }
clamy3cb9bea92018-07-10 12:42:022943 for (auto& item : different_document_loads) {
2944 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052945 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302946 }
clamy3cb9bea92018-07-10 12:42:022947
2948 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:302949}
2950
Alex Moshchuk3a4e77a2020-05-29 21:32:572951NavigationControllerImpl::HistoryNavigationAction
2952NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:302953 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:572954 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:422955 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:582956 // Only active and prerendered documents are allowed to navigate in their
2957 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422958 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:012959 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:582960 // - If the document is in pending deletion, the browser already committed
2961 // to destroying this RenderFrameHost. See https://crbug.com/930278.
2962 // - If the document is in back-forward cache, it's not allowed to navigate
2963 // as it should remain frozen. Ignore the request and evict the document
2964 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422965 //
Sreeja Kamishettydb8e2892021-03-10 09:30:582966 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:422967 // which should all be inactive as well.
Sreeja Kamishettydb8e2892021-03-10 09:30:582968 if (render_frame_host->IsInactiveAndDisallowActivation())
Sreeja Kamishetty8eacabb2021-03-09 11:45:422969 return HistoryNavigationAction::kStopLooking;
2970 }
arthursonzogni03f76152019-02-12 10:35:202971
Alex Moshchuk3a4e77a2020-05-29 21:32:572972 // If there's no last committed entry, there is no previous history entry to
2973 // compare against, so fall back to a different-document load. Note that we
2974 // should only reach this case for the root frame and not descend further
2975 // into subframes.
2976 if (!GetLastCommittedEntry()) {
2977 DCHECK(frame->IsMainFrame());
2978 return HistoryNavigationAction::kDifferentDocument;
2979 }
2980
2981 // Reloads should result in a different-document load. Note that reloads may
2982 // also happen via the |needs_reload_| mechanism where the reload_type is
2983 // NONE, so detect this by comparing whether we're going to the same
2984 // entry that we're currently on. Similarly to above, only main frames
2985 // should reach this. Note that subframes support reloads, but that's done
2986 // via a different path that doesn't involve FindFramesToNavigate (see
2987 // RenderFrameHost::Reload()).
2988 if (reload_type != ReloadType::NONE ||
2989 pending_entry_index_ == last_committed_entry_index_) {
2990 DCHECK(frame->IsMainFrame());
2991 return HistoryNavigationAction::kDifferentDocument;
2992 }
2993
Alex Moshchuk47d1a4bd2020-06-01 22:15:342994 // If there is no new FrameNavigationEntry for the frame, ignore the
2995 // load. For example, this may happen when going back to an entry before a
2996 // frame was created. Suppose we commit a same-document navigation that also
2997 // results in adding a new subframe somewhere in the tree. If we go back,
2998 // the new subframe will be missing a FrameNavigationEntry in the previous
2999 // NavigationEntry, but we shouldn't delete or change what's loaded in
3000 // it.
3001 //
Alex Moshchuke65c39272020-06-03 17:55:373002 // Note that in this case, there is no need to keep looking for navigations
3003 // in subframes, which would be missing FrameNavigationEntries as well.
3004 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:343005 // It's important to check this before checking |old_item| below, since both
3006 // might be null, and in that case we still shouldn't change what's loaded in
3007 // this frame. Note that scheduling any loads assumes that |new_item| is
3008 // non-null. See https://crbug.com/1088354.
3009 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3010 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:373011 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:343012
Alex Moshchuk3a4e77a2020-05-29 21:32:573013 // If there is no old FrameNavigationEntry, schedule a different-document
3014 // load.
3015 //
creis225a7432016-06-03 22:56:273016 // TODO(creis): Store the last committed FrameNavigationEntry to use here,
3017 // rather than assuming the NavigationEntry has up to date info on subframes.
creis4e2ecb72015-06-20 00:46:303018 FrameNavigationEntry* old_item =
3019 GetLastCommittedEntry()->GetFrameEntry(frame);
Alex Moshchuk3a4e77a2020-05-29 21:32:573020 if (!old_item)
3021 return HistoryNavigationAction::kDifferentDocument;
3022
Alex Moshchuk3a4e77a2020-05-29 21:32:573023 // If the new item is not in the same SiteInstance, schedule a
3024 // different-document load. Newly restored items may not have a SiteInstance
3025 // yet, in which case it will be assigned on first commit.
3026 if (new_item->site_instance() &&
3027 new_item->site_instance() != old_item->site_instance())
3028 return HistoryNavigationAction::kDifferentDocument;
3029
3030 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:313031 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
3032 // that we do this even if the history navigation would not be modifying this
3033 // frame were it live.
3034 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:573035 return HistoryNavigationAction::kDifferentDocument;
3036
3037 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:313038 // Starting a navigation after a crash early-promotes the speculative
3039 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
3040 // committed yet, so we can not possibly perform a same-document history
3041 // navigation. The frame would need to be reloaded with a cross-document
3042 // navigation.
3043 if (!frame->current_frame_host()->has_committed_any_navigation())
3044 return HistoryNavigationAction::kDifferentDocument;
3045
creis54131692016-08-12 18:32:253046 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:493047 // sequence number but different item sequence number.
3048 if (new_item->document_sequence_number() ==
3049 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573050 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493051 }
avib48cb312016-05-05 21:35:003052
Alex Moshchuk3a4e77a2020-05-29 21:32:573053 // Otherwise, if both item and document sequence numbers differ, this
3054 // should be a different document load.
3055 return HistoryNavigationAction::kDifferentDocument;
3056 }
3057
3058 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373059 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573060 DCHECK_EQ(new_item->document_sequence_number(),
3061 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373062 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573063}
3064
3065void NavigationControllerImpl::FindFramesToNavigate(
3066 FrameTreeNode* frame,
3067 ReloadType reload_type,
3068 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3069 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3070 DCHECK(pending_entry_);
3071 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3072
3073 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3074
3075 if (action == HistoryNavigationAction::kSameDocument) {
3076 std::unique_ptr<NavigationRequest> navigation_request =
3077 CreateNavigationRequestFromEntry(
3078 frame, pending_entry_, new_item, reload_type,
3079 true /* is_same_document_history_load */,
Kush Sinhaa8856ecb2021-09-01 14:28:313080 false /* is_history_navigation_in_new_child */);
Alex Moshchuk3a4e77a2020-05-29 21:32:573081 if (navigation_request) {
3082 // Only add the request if was properly created. It's possible for the
3083 // creation to fail in certain cases, e.g. when the URL is invalid.
3084 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303085 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573086 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473087 std::unique_ptr<NavigationRequest> navigation_request =
3088 CreateNavigationRequestFromEntry(
3089 frame, pending_entry_, new_item, reload_type,
3090 false /* is_same_document_history_load */,
Kush Sinhaa8856ecb2021-09-01 14:28:313091 false /* is_history_navigation_in_new_child */);
Lei Zhang96031532019-10-10 19:05:473092 if (navigation_request) {
3093 // Only add the request if was properly created. It's possible for the
3094 // creation to fail in certain cases, e.g. when the URL is invalid.
3095 different_document_loads->push_back(std::move(navigation_request));
3096 }
3097 // For a different document, the subframes will be destroyed, so there's
3098 // no need to consider them.
3099 return;
Alex Moshchuke65c39272020-06-03 17:55:373100 } else if (action == HistoryNavigationAction::kStopLooking) {
3101 return;
creis4e2ecb72015-06-20 00:46:303102 }
3103
3104 for (size_t i = 0; i < frame->child_count(); i++) {
Kush Sinhaa8856ecb2021-09-01 14:28:313105 FindFramesToNavigate(frame->child_at(i), reload_type, same_document_loads,
3106 different_document_loads);
creis4e2ecb72015-06-20 00:46:303107 }
3108}
3109
Harkiran Bolariaba823e42021-05-21 18:30:363110base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry(
clamy21718cc22018-06-13 13:34:243111 const LoadURLParams& params) {
3112 // Find the appropriate FrameTreeNode.
3113 FrameTreeNode* node = nullptr;
3114 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3115 !params.frame_name.empty()) {
3116 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003117 ? frame_tree_.FindByID(params.frame_tree_node_id)
3118 : frame_tree_.FindByName(params.frame_name);
Hayato Ito7a80db42021-07-05 06:18:543119 DCHECK(!node || node->frame_tree() == &frame_tree_);
clamy21718cc22018-06-13 13:34:243120 }
3121
3122 // If no FrameTreeNode was specified, navigate the main frame.
3123 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003124 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243125
Camille Lamy5193caa2018-10-12 11:59:423126 // Compute overrides to the LoadURLParams for |override_user_agent|,
3127 // |should_replace_current_entry| and |has_user_gesture| that will be used
3128 // both in the creation of the NavigationEntry and the NavigationRequest.
3129 // Ideally, the LoadURLParams themselves would be updated, but since they are
3130 // passed as a const reference, this is not possible.
3131 // TODO(clamy): When we only create a NavigationRequest, move this to
3132 // CreateNavigationRequestFromLoadURLParams.
3133 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3134 GetLastCommittedEntry());
3135
3136 // Don't allow an entry replacement if there is no entry to replace.
3137 // http://crbug.com/457149
Hayato Ito7a80db42021-07-05 06:18:543138 //
3139 // If there is an entry, an entry replacement must happen if the current
3140 // browsing context should maintain a trivial session history.
3141 bool should_replace_current_entry = (params.should_replace_current_entry ||
3142 ShouldMaintainTrivialSessionHistory()) &&
3143 entries_.size();
Camille Lamy5193caa2018-10-12 11:59:423144
clamy21718cc22018-06-13 13:34:243145 // Javascript URLs should not create NavigationEntries. All other navigations
3146 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243147 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373148 std::unique_ptr<NavigationEntryImpl> entry =
3149 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3150 should_replace_current_entry,
3151 params.has_user_gesture);
clamy21718cc22018-06-13 13:34:243152 DiscardPendingEntry(false);
3153 SetPendingEntry(std::move(entry));
3154 }
3155
3156 // Renderer-debug URLs are sent to the renderer process immediately for
3157 // processing and don't need to create a NavigationRequest.
3158 // Note: this includes navigations to JavaScript URLs, which are considered
3159 // renderer-debug URLs.
3160 // Note: we intentionally leave the pending entry in place for renderer debug
3161 // URLs, unlike the cases below where we clear it if the navigation doesn't
3162 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303163 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483164 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3165 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153166 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483167 params.url, browser_context_)) {
3168 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363169 return nullptr;
Oleg Davydov2cc0167b2019-02-05 14:32:483170 }
3171
clamy21718cc22018-06-13 13:34:243172 HandleRendererDebugURL(node, params.url);
Harkiran Bolariaba823e42021-05-21 18:30:363173 return nullptr;
clamy21718cc22018-06-13 13:34:243174 }
3175
Antonio Sartori78a749f2020-11-30 12:03:393176 DCHECK(pending_entry_);
3177
clamy21718cc22018-06-13 13:34:243178 // Convert navigations to the current URL to a reload.
3179 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3180 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3181 // DevTools sometimes issues navigations to main frames that they do not
3182 // expect to see treated as reload, and it only works because they pass a
3183 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3184 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543185 ReloadType reload_type = params.reload_type;
3186 if (reload_type == ReloadType::NONE &&
3187 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043188 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243189 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243190 params.load_type ==
3191 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
Hayato Ito7a80db42021-07-05 06:18:543192 should_replace_current_entry, GetLastCommittedEntry())) {
clamy21718cc22018-06-13 13:34:243193 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473194 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393195
3196 // If this is a reload of an existing FrameNavigationEntry and we had a
3197 // policy container for it, then we should copy it into the pending entry,
3198 // so that it is copied to the navigation request in
3199 // CreateNavigationRequestFromLoadParams later.
3200 if (GetLastCommittedEntry()) {
3201 FrameNavigationEntry* previous_frame_entry =
3202 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193203 if (previous_frame_entry &&
3204 previous_frame_entry->policy_container_policies()) {
3205 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163206 previous_frame_entry->policy_container_policies()->Clone());
Antonio Sartori78a749f2020-11-30 12:03:393207 }
3208 }
3209 }
3210
3211 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
3212 // document (so no last commit), then we should copy the document polices from
3213 // RenderFrameHost's PolicyContainerHost. The NavigationRequest will create a
3214 // new PolicyContainerHost with the document policies from the
3215 // |pending_entry_|, and that PolicyContainerHost will be put in the final
3216 // RenderFrameHost for the navigation. This way, we ensure that we keep
3217 // enforcing the right policies on the initial empty document after the
3218 // reload.
3219 if (!GetLastCommittedEntry() && params.url.IsAboutBlank()) {
3220 if (node->current_frame_host() &&
3221 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193222 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163223 node->current_frame_host()
3224 ->policy_container_host()
3225 ->policies()
3226 .Clone());
Antonio Sartori78a749f2020-11-30 12:03:393227 }
clamy21718cc22018-06-13 13:34:243228 }
3229
3230 // navigation_ui_data should only be present for main frame navigations.
3231 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
3232
Camille Lamy5193caa2018-10-12 11:59:423233 std::unique_ptr<NavigationRequest> request =
3234 CreateNavigationRequestFromLoadParams(
3235 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143236 params.has_user_gesture, network::mojom::SourceLocation::New(),
Antonio Sartori6984c742021-08-26 08:03:413237 reload_type, pending_entry_, pending_entry_->GetFrameEntry(node));
clamy21718cc22018-06-13 13:34:243238
3239 // If the navigation couldn't start, return immediately and discard the
3240 // pending NavigationEntry.
3241 if (!request) {
3242 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363243 return nullptr;
clamy21718cc22018-06-13 13:34:243244 }
3245
Camille Lamy5193caa2018-10-12 11:59:423246#if DCHECK_IS_ON()
3247 // Safety check that NavigationRequest and NavigationEntry match.
3248 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3249#endif
3250
clamy21718cc22018-06-13 13:34:243251 // This call does not support re-entrancy. See http://crbug.com/347742.
3252 CHECK(!in_navigate_to_pending_entry_);
3253 in_navigate_to_pending_entry_ = true;
3254
arthursonzogni66f711c2019-10-08 14:40:363255 // It is not possible to delete the pending NavigationEntry while navigating
3256 // to it. Grab a reference to delay potential deletion until the end of this
3257 // function.
3258 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3259
Harkiran Bolariaba823e42021-05-21 18:30:363260 base::WeakPtr<NavigationHandle> created_navigation_handle(
3261 request->GetWeakPtr());
Lukasz Anforowicz9ee83c272020-12-01 20:14:053262 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243263
3264 in_navigate_to_pending_entry_ = false;
Harkiran Bolariaba823e42021-05-21 18:30:363265 return created_navigation_handle;
clamy21718cc22018-06-13 13:34:243266}
3267
clamyea99ea12018-05-28 13:54:233268void NavigationControllerImpl::HandleRendererDebugURL(
3269 FrameTreeNode* frame_tree_node,
3270 const GURL& url) {
3271 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243272 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3273 // the renderer process is not live, unless it is the initial navigation
3274 // of the tab.
clamyea99ea12018-05-28 13:54:233275 if (!IsInitialNavigation()) {
3276 DiscardNonCommittedEntries();
3277 return;
3278 }
Fergal Dalyecd3b0202020-06-25 01:57:373279 // The current frame is always a main frame. If IsInitialNavigation() is
3280 // true then there have been no navigations and any frames of this tab must
3281 // be in the same renderer process. If that has crashed then the only frame
3282 // that can be revived is the main frame.
3283 frame_tree_node->render_manager()
3284 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233285 }
3286 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3287}
3288
clamy21718cc22018-06-13 13:34:243289std::unique_ptr<NavigationEntryImpl>
3290NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3291 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423292 const LoadURLParams& params,
3293 bool override_user_agent,
3294 bool should_replace_current_entry,
3295 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393296 // Browser initiated navigations might not have a blob_url_loader_factory set
3297 // in params even if the navigation is to a blob URL. If that happens, lookup
3298 // the correct url loader factory to use here.
3299 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483300 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473301 // Resolve the blob URL in the storage partition associated with the target
3302 // frame. This is the storage partition the URL will be loaded in, and only
3303 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393304 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473305 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393306 }
3307
clamy21718cc22018-06-13 13:34:243308 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443309 // extra_headers in params are \n separated; navigation entries want \r\n.
3310 std::string extra_headers_crlf;
3311 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243312
3313 // For subframes, create a pending entry with a corresponding frame entry.
3314 if (!node->IsMainFrame()) {
Tommy C. Li03eee77a2019-02-05 02:07:443315 if (GetLastCommittedEntry()) {
Tommy C. Li03eee77a2019-02-05 02:07:443316 entry = GetLastCommittedEntry()->Clone();
3317 } else {
3318 // If there's no last committed entry, create an entry for about:blank
3319 // with a subframe entry for our destination.
3320 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
3321 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063322 GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103323 params.source_site_instance.get(), params.transition_type,
3324 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Hayato Ito303654c2021-06-30 09:07:543325 blob_url_loader_factory));
Tommy C. Li03eee77a2019-02-05 02:07:443326 }
Nasko Oskov18006bc2018-12-06 02:53:583327
clamy21718cc22018-06-13 13:34:243328 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:083329 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
clamy21718cc22018-06-13 13:34:243330 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Anton Bikineevf62d1bf2021-05-15 17:56:073331 params.url, absl::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203332 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393333 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:483334 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori78a749f2020-11-30 12:03:393335 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163336 // a conversion of a new navigation into a reload, we will set the right
3337 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193338 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243339 } else {
3340 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243341 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063342 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103343 params.source_site_instance.get(), params.transition_type,
3344 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Hayato Ito303654c2021-06-30 09:07:543345 blob_url_loader_factory));
clamy21718cc22018-06-13 13:34:243346 entry->set_source_site_instance(
3347 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3348 entry->SetRedirectChain(params.redirect_chain);
3349 }
3350
3351 // Set the FTN ID (only used in non-site-per-process, for tests).
3352 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243353 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423354 entry->SetIsOverridingUserAgent(override_user_agent);
3355 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543356 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243357
clamy21718cc22018-06-13 13:34:243358 switch (params.load_type) {
3359 case LOAD_TYPE_DEFAULT:
3360 break;
3361 case LOAD_TYPE_HTTP_POST:
3362 entry->SetHasPostData(true);
3363 entry->SetPostData(params.post_data);
3364 break;
3365 case LOAD_TYPE_DATA:
3366 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3367 entry->SetVirtualURL(params.virtual_url_for_data_url);
3368#if defined(OS_ANDROID)
3369 entry->SetDataURLAsString(params.data_url_as_string);
3370#endif
3371 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3372 break;
clamy21718cc22018-06-13 13:34:243373 }
3374
3375 // TODO(clamy): NavigationEntry is meant for information that will be kept
3376 // after the navigation ended and therefore is not appropriate for
3377 // started_from_context_menu. Move started_from_context_menu to
3378 // NavigationUIData.
3379 entry->set_started_from_context_menu(params.started_from_context_menu);
3380
3381 return entry;
3382}
3383
clamyea99ea12018-05-28 13:54:233384std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423385NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3386 FrameTreeNode* node,
3387 const LoadURLParams& params,
3388 bool override_user_agent,
3389 bool should_replace_current_entry,
3390 bool has_user_gesture,
Antonio Sartori2f763d9d2021-04-21 10:04:143391 network::mojom::SourceLocationPtr source_location,
Camille Lamy5193caa2018-10-12 11:59:423392 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573393 NavigationEntryImpl* entry,
Scott Violetcf6ea7e2021-06-09 21:09:213394 FrameNavigationEntry* frame_entry) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573395 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283396 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533397 // All renderer-initiated navigations must have an initiator_origin.
3398 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513399
Camille Lamy5193caa2018-10-12 11:59:423400 GURL url_to_load;
3401 GURL virtual_url;
Anton Bikineevf62d1bf2021-05-15 17:56:073402 absl::optional<url::Origin> origin_to_commit =
3403 frame_entry ? frame_entry->committed_origin() : absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:323404
Camille Lamy2baa8022018-10-19 16:43:173405 // For main frames, rewrite the URL if necessary and compute the virtual URL
3406 // that should be shown in the address bar.
3407 if (node->IsMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423408 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173409 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423410 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423411
Camille Lamy2baa8022018-10-19 16:43:173412 // For DATA loads, override the virtual URL.
3413 if (params.load_type == LOAD_TYPE_DATA)
3414 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423415
Camille Lamy2baa8022018-10-19 16:43:173416 if (virtual_url.is_empty())
3417 virtual_url = url_to_load;
3418
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573419 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283420
Aran Gilman249eb122019-12-02 23:32:463421 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3422 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3423 // return different results, leading to a different URL in the
3424 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3425 // pending NavigationEntry, as we'll only make one call to
3426 // RewriteUrlForNavigation.
3427 VLOG_IF(1, (url_to_load != frame_entry->url()))
3428 << "NavigationRequest and FrameEntry have different URLs: "
3429 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283430
Camille Lamy2baa8022018-10-19 16:43:173431 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423432 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173433 // NavigationRequest.
3434 } else {
3435 url_to_load = params.url;
3436 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243437 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173438 }
Camille Lamy5193caa2018-10-12 11:59:423439
Ehsan Karamad44fc72112019-02-26 18:15:473440 if (node->render_manager()->is_attaching_inner_delegate()) {
3441 // Avoid starting any new navigations since this node is now preparing for
3442 // attaching an inner delegate.
3443 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203444 }
Camille Lamy5193caa2018-10-12 11:59:423445
Camille Lamy5193caa2018-10-12 11:59:423446 if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load))
3447 return nullptr;
3448
Kunihiko Sakamoto346a74e2021-03-10 08:57:483449 if (!DoesURLMatchOriginForNavigation(
3450 url_to_load, origin_to_commit,
3451 frame_entry->subresource_web_bundle_navigation_info())) {
Nasko Oskov03912102019-01-11 00:21:323452 DCHECK(false) << " url:" << url_to_load
3453 << " origin:" << origin_to_commit.value();
3454 return nullptr;
3455 }
3456
Camille Lamy5193caa2018-10-12 11:59:423457 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403458 blink::PreviewsState previews_state =
3459 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
Camille Lamy5193caa2018-10-12 11:59:423460 if (!node->IsMainFrame()) {
3461 // For subframes, use the state of the top-level frame.
3462 previews_state = node->frame_tree()
3463 ->root()
3464 ->current_frame_host()
3465 ->last_navigation_previews_state();
3466 }
3467
Camille Lamy5193caa2018-10-12 11:59:423468 // This will be used to set the Navigation Timing API navigationStart
3469 // parameter for browser navigations in new tabs (intents, tabs opened through
3470 // "Open link in new tab"). If the navigation must wait on the current
3471 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3472 // will be updated when the BeforeUnload ack is received.
3473 base::TimeTicks navigation_start = base::TimeTicks::Now();
3474
danakjd83d706d2020-11-25 22:11:123475 // Look for a pending commit that is to another document in this
3476 // FrameTreeNode. If one exists, then the last committed URL will not be the
3477 // current URL by the time this navigation commits.
3478 bool has_pending_cross_document_commit =
3479 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493480 bool is_currently_error_page = node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123481
Minggang Wangb9f3fa92021-07-01 15:30:313482 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjb952ef12021-01-14 19:58:493483 /*old_url=*/node->current_url(),
3484 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3485 has_pending_cross_document_commit, is_currently_error_page,
3486 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423487
3488 // Create the NavigationParams based on |params|.
3489
Hiroki Nakagawa4ed61282021-06-18 05:37:233490 bool is_view_source_mode = entry->IsViewSourceMode();
3491 DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme));
Charlie Harrison8c113a32019-01-07 16:08:293492
Antonio Sartori6984c742021-08-26 08:03:413493 blink::NavigationDownloadPolicy download_policy = params.download_policy;
Hiroki Nakagawa4ed61282021-06-18 05:37:233494 // Update |download_policy| if the virtual URL is view-source.
Charlie Harrison8c113a32019-01-07 16:08:293495 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253496 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293497
Camille Lamy5193caa2018-10-12 11:59:423498 const GURL& history_url_for_data_url =
3499 params.base_url_for_data_url.is_empty() ? GURL() : virtual_url;
Minggang Wangb9f3fa92021-07-01 15:30:313500 blink::mojom::CommonNavigationParamsPtr common_params =
3501 blink::mojom::CommonNavigationParams::New(
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513502 url_to_load, params.initiator_origin,
3503 blink::mojom::Referrer::New(params.referrer.url,
3504 params.referrer.policy),
Scott Violetcf6ea7e2021-06-09 21:09:213505 params.transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513506 should_replace_current_entry, params.base_url_for_data_url,
3507 history_url_for_data_url, previews_state, navigation_start,
3508 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
Antonio Sartori2f763d9d2021-04-21 10:04:143509 params.post_data, std::move(source_location),
arthursonzogniaf7c62c52020-02-12 10:49:413510 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273511 false /* has_text_fragment_token */,
3512 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3513 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513514 false /* is_history_navigation_in_new_child_frame */,
Charlie Hu5ffc0152019-12-06 15:59:533515 params.input_start);
Camille Lamy5193caa2018-10-12 11:59:423516
Minggang Wangb9f3fa92021-07-01 15:30:313517 blink::mojom::CommitNavigationParamsPtr commit_params =
3518 blink::mojom::CommitNavigationParams::New(
Antonio Sartori3e8de6d2021-07-26 10:28:413519 frame_entry->committed_origin(),
3520 // The correct storage key will be computed before committing the
3521 // navigation.
3522 blink::StorageKey(), network::mojom::WebSandboxFlags(),
arthursonzognid5a8d0b2021-03-11 17:36:433523 override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323524 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143525 std::vector<net::RedirectInfo>(),
3526 std::string() /* post_content_type */, common_params->url,
3527 common_params->method, params.can_load_local_resources,
Minggang Wangb9f3fa92021-07-01 15:30:313528 frame_entry->page_state().ToEncodedData(), entry->GetUniqueID(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143529 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3530 -1 /* pending_history_list_offset */,
3531 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3532 params.should_clear_history_list ? 0 : GetEntryCount(),
3533 false /* was_discarded */, is_view_source_mode,
Minggang Wangb9f3fa92021-07-01 15:30:313534 params.should_clear_history_list,
3535 blink::mojom::NavigationTiming::New(),
Anton Bikineevf62d1bf2021-05-15 17:56:073536 absl::nullopt /* appcache_host_id */,
Minggang Wangf59db47b2021-06-16 01:56:223537 blink::mojom::WasActivatedOption::kUnknown,
Lucas Furukawa Gadania9c45682019-07-31 22:05:143538 base::UnguessableToken::Create() /* navigation_token */,
Minggang Wang7ee0c742021-06-16 16:16:513539 std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143540#if defined(OS_ANDROID)
3541 std::string(), /* data_url_as_string */
3542#endif
arthursonzogni14379782020-05-15 09:09:273543 !params.is_renderer_initiated, /* is_browser_initiated */
Tsuyoshi Horoe86d7702019-11-29 01:52:473544 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533545 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173546 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073547 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163548 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333549 false /* origin_agent_cluster */,
Maks Orlovichc66745a2020-06-30 17:40:023550 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213551 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Nate Chapind1fe3612021-04-16 20:45:573552 false /* is_cross_browsing_instance */, nullptr /* old_page_info */,
3553 -1 /* http_response_code */,
Minggang Wangb9f3fa92021-07-01 15:30:313554 std::vector<blink::mojom::
3555 AppHistoryEntryPtr>() /* app_history_back_entries */,
Nate Chapind1fe3612021-04-16 20:45:573556 std::vector<
Minggang Wangb9f3fa92021-07-01 15:30:313557 blink::mojom::
3558 AppHistoryEntryPtr>() /* app_history_forward_entries */,
Kenichi Ishibashi17ecfb62021-06-21 03:31:153559 std::vector<GURL>() /* early_hints_preloaded_resources */);
Camille Lamy5193caa2018-10-12 11:59:423560#if defined(OS_ANDROID)
3561 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143562 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423563 }
3564#endif
3565
Lucas Furukawa Gadania9c45682019-07-31 22:05:143566 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423567
3568 // A form submission may happen here if the navigation is a renderer-initiated
3569 // form submission that took the OpenURL path.
3570 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3571
3572 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3573 std::string extra_headers_crlf;
3574 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093575
3576 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143577 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083578 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453579 params.initiator_frame_token.has_value()
3580 ? &(params.initiator_frame_token.value())
3581 : nullptr,
3582 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3583 request_body,
John Delaney50425f82020-04-07 16:26:213584 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473585 params.impression, params.is_pdf);
Yao Xiaodc5ed102019-06-04 19:19:093586 navigation_request->set_from_download_cross_origin_redirect(
3587 params.from_download_cross_origin_redirect);
3588 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423589}
3590
3591std::unique_ptr<NavigationRequest>
3592NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233593 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573594 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233595 FrameNavigationEntry* frame_entry,
3596 ReloadType reload_type,
3597 bool is_same_document_history_load,
Kush Sinhaa8856ecb2021-09-01 14:28:313598 bool is_history_navigation_in_new_child_frame) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343599 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233600 GURL dest_url = frame_entry->url();
Anton Bikineevf62d1bf2021-05-15 17:56:073601 absl::optional<url::Origin> origin_to_commit =
Nasko Oskov03912102019-01-11 00:21:323602 frame_entry->committed_origin();
3603
clamyea99ea12018-05-28 13:54:233604 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013605 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573606 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233607 // We may have been redirected when navigating to the current URL.
3608 // Use the URL the user originally intended to visit as signaled by the
3609 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013610 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573611 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233612 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323613 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233614 }
3615
Ehsan Karamad44fc72112019-02-26 18:15:473616 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3617 // Avoid starting any new navigations since this node is now preparing for
3618 // attaching an inner delegate.
3619 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203620 }
3621
Camille Lamy5193caa2018-10-12 11:59:423622 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573623 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233624 return nullptr;
3625 }
3626
Kunihiko Sakamoto346a74e2021-03-10 08:57:483627 if (!DoesURLMatchOriginForNavigation(
3628 dest_url, origin_to_commit,
3629 frame_entry->subresource_web_bundle_navigation_info())) {
3630 DCHECK(false) << " url:" << dest_url
3631 << " origin:" << origin_to_commit.value();
Nasko Oskov03912102019-01-11 00:21:323632 return nullptr;
3633 }
3634
clamyea99ea12018-05-28 13:54:233635 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403636 blink::PreviewsState previews_state =
3637 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
clamyea99ea12018-05-28 13:54:233638 if (!frame_tree_node->IsMainFrame()) {
3639 // For subframes, use the state of the top-level frame.
3640 previews_state = frame_tree_node->frame_tree()
3641 ->root()
3642 ->current_frame_host()
3643 ->last_navigation_previews_state();
clamyea99ea12018-05-28 13:54:233644 }
3645
clamyea99ea12018-05-28 13:54:233646 // This will be used to set the Navigation Timing API navigationStart
3647 // parameter for browser navigations in new tabs (intents, tabs opened through
3648 // "Open link in new tab"). If the navigation must wait on the current
3649 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3650 // will be updated when the BeforeUnload ack is received.
3651 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233652
danakjd83d706d2020-11-25 22:11:123653 // Look for a pending commit that is to another document in this
3654 // FrameTreeNode. If one exists, then the last committed URL will not be the
3655 // current URL by the time this navigation commits.
3656 bool has_pending_cross_document_commit =
3657 frame_tree_node->render_manager()
3658 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493659 bool is_currently_error_page =
3660 frame_tree_node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123661
Minggang Wangb9f3fa92021-07-01 15:30:313662 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123663 /*old_url=*/frame_tree_node->current_url(),
3664 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493665 has_pending_cross_document_commit, is_currently_error_page,
3666 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423667
3668 // A form submission may happen here if the navigation is a
3669 // back/forward/reload navigation that does a form resubmission.
3670 scoped_refptr<network::ResourceRequestBody> request_body;
3671 std::string post_content_type;
3672 if (frame_entry->method() == "POST") {
3673 request_body = frame_entry->GetPostData(&post_content_type);
3674 // Might have a LF at end.
Peter Kastingb53b81912021-04-28 19:23:303675 post_content_type = std::string(
3676 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL));
Camille Lamy5193caa2018-10-12 11:59:423677 }
3678
3679 // Create the NavigationParams based on |entry| and |frame_entry|.
Minggang Wangb9f3fa92021-07-01 15:30:313680 blink::mojom::CommonNavigationParamsPtr common_params =
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513681 entry->ConstructCommonNavigationParams(
3682 *frame_entry, request_body, dest_url,
3683 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
3684 navigation_type, previews_state, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533685 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513686 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553687 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423688
3689 // TODO(clamy): |intended_as_new_entry| below should always be false once
3690 // Reload no longer leads to this being called for a pending NavigationEntry
3691 // of index -1.
Minggang Wangb9f3fa92021-07-01 15:30:313692 blink::mojom::CommitNavigationParamsPtr commit_params =
Lucas Furukawa Gadania9c45682019-07-31 22:05:143693 entry->ConstructCommitNavigationParams(
3694 *frame_entry, common_params->url, origin_to_commit,
3695 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3696 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533697 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3698 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143699 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423700
clamyea99ea12018-05-28 13:54:233701 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143702 frame_tree_node, std::move(common_params), std::move(commit_params),
Kush Sinhaa8856ecb2021-09-01 14:28:313703 !entry->is_renderer_initiated(), false /* was_opener_suppressed */,
Takashi Toyoshimae87b7be2021-01-22 11:51:083704 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453705 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3706 entry->extra_headers(), frame_entry, entry, request_body,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473707 nullptr /* navigation_ui_data */, absl::nullopt /* impression */,
3708 false /* is_pdf */);
clamyea99ea12018-05-28 13:54:233709}
3710
[email protected]d202a7c2012-01-04 07:53:473711void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213712 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273713 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403714
Takashi Toyoshimaea534ef22021-07-21 03:27:593715 // We need to notify the ssl_manager_ before the WebContents so the
[email protected]df1af242009-05-01 00:11:403716 // location bar will have up-to-date information about the security style
3717 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133718 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403719
[email protected]7f924832014-08-09 05:57:223720 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:573721 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463722
[email protected]b0f724c2013-09-05 04:21:133723 // TODO(avi): Remove. http://crbug.com/170921
3724 NotificationDetails notification_details =
3725 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153726 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3727 Source<NavigationController>(this),
3728 notification_details);
initial.commit09911bf2008-07-26 23:55:293729}
3730
initial.commit09911bf2008-07-26 23:55:293731// static
[email protected]d202a7c2012-01-04 07:53:473732size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233733 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153734 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:213735 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233736}
3737
[email protected]d202a7c2012-01-04 07:53:473738void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223739 if (is_active && needs_reload_)
3740 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293741}
3742
[email protected]d202a7c2012-01-04 07:53:473743void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293744 if (!needs_reload_)
3745 return;
3746
Bo Liucdfa4b12018-11-06 00:21:443747 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3748 needs_reload_type_);
3749
initial.commit09911bf2008-07-26 23:55:293750 // Calling Reload() results in ignoring state, and not loading.
3751 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3752 // cached state.
avicc872d7242015-08-19 21:26:343753 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163754 NavigateToExistingPendingEntry(ReloadType::NONE,
Kush Sinhaa8856ecb2021-09-01 14:28:313755 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343756 } else if (last_committed_entry_index_ != -1) {
arthursonzogni5c4c202d2017-04-25 23:41:273757 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343758 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163759 NavigateToExistingPendingEntry(ReloadType::NONE,
Kush Sinhaa8856ecb2021-09-01 14:28:313760 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343761 } else {
3762 // If there is something to reload, the successful reload will clear the
3763 // |needs_reload_| flag. Otherwise, just do it here.
3764 needs_reload_ = false;
3765 }
initial.commit09911bf2008-07-26 23:55:293766}
3767
Carlos IL42b416592019-10-07 23:10:363768void NavigationControllerImpl::LoadPostCommitErrorPage(
3769 RenderFrameHost* render_frame_host,
3770 const GURL& url,
3771 const std::string& error_page_html,
3772 net::Error error) {
Rakina Zata Amni919b7922020-12-11 09:03:133773 RenderFrameHostImpl* rfhi =
3774 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:583775
3776 // Only active documents can load post-commit error pages:
3777 // - If the document is in pending deletion, the browser already committed to
3778 // destroying this RenderFrameHost so ignore loading the error page.
3779 // - If the document is in back-forward cache, it's not allowed to navigate as
3780 // it should remain frozen. Ignore the request and evict the document from
3781 // back-forward cache.
3782 // - If the document is prerendering, it can navigate but when loading error
3783 // pages, cancel prerendering.
3784 if (rfhi->IsInactiveAndDisallowActivation())
3785 return;
3786
Rakina Zata Amni919b7922020-12-11 09:03:133787 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:413788
Minggang Wangb9f3fa92021-07-01 15:30:313789 blink::mojom::CommonNavigationParamsPtr common_params =
Minggang Wanga13c796e2021-07-02 05:54:433790 blink::CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:593791 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
3792 // the frame actually committed (e.g. iframe with "src" set to a
3793 // slow-responding URL). We should rewrite the URL to about:blank in this
3794 // case, as the renderer will only think a page is an error page if it has a
3795 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:133796 common_params->url = url.is_empty() ? GURL("about:blank") : url;
Minggang Wangb9f3fa92021-07-01 15:30:313797 blink::mojom::CommitNavigationParamsPtr commit_params =
Minggang Wanga13c796e2021-07-02 05:54:433798 blink::CreateCommitNavigationParams();
Antonio Sartori58591c892021-04-21 06:54:333799 commit_params->original_url = common_params->url;
John Delaney131ad362019-08-08 21:57:413800
arthursonzogni70ac7302020-05-28 08:49:053801 // Error pages have a fully permissive FramePolicy.
3802 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
3803 // error pages.
3804 commit_params->frame_policy = blink::FramePolicy();
3805
John Delaney131ad362019-08-08 21:57:413806 std::unique_ptr<NavigationRequest> navigation_request =
3807 NavigationRequest::CreateBrowserInitiated(
3808 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083809 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:193810 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453811 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:063812 "" /* extra_headers */, nullptr /* frame_entry */,
3813 nullptr /* entry */, nullptr /* post_body */,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473814 nullptr /* navigation_ui_data */, absl::nullopt /* impression */,
3815 false /* is_pdf */);
Carlos IL42b416592019-10-07 23:10:363816 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:413817 navigation_request->set_net_error(error);
3818 node->CreatedNavigationRequest(std::move(navigation_request));
3819 DCHECK(node->navigation_request());
3820 node->navigation_request()->BeginNavigation();
3821}
3822
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573823void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213824 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093825 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:153826 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143827 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293828}
3829
[email protected]d202a7c2012-01-04 07:53:473830void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363831 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553832 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363833 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293834
initial.commit09911bf2008-07-26 23:55:293835 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293836}
[email protected]765b35502008-08-21 00:51:203837
arthursonzogni69a6a1b2019-09-17 09:23:003838void NavigationControllerImpl::DiscardNonCommittedEntries() {
Michael Thiessen9b14d512019-09-23 21:19:473839 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:103840 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
3841 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:293842 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:473843 return;
Michael Thiessenc5676d22019-09-25 22:32:103844 }
avi45a72532015-04-07 21:01:453845 DiscardPendingEntry(false);
arthursonzogni69a6a1b2019-09-17 09:23:003846 if (delegate_)
3847 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:483848}
3849
avi7c6f35e2015-05-08 17:52:383850int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3851 int nav_entry_id) const {
3852 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3853 if (entries_[i]->GetUniqueID() == nav_entry_id)
3854 return i;
3855 }
3856 return -1;
3857}
3858
[email protected]d202a7c2012-01-04 07:53:473859void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573860 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253861 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573862 DCHECK_LE(max_index, source->GetEntryCount());
Nate Chapin214a86a2021-06-21 20:35:573863 std::unique_ptr<NavigationEntryRestoreContextImpl> context =
3864 std::make_unique<NavigationEntryRestoreContextImpl>();
[email protected]e1cd5452010-08-26 18:03:253865 for (int i = 0; i < max_index; i++) {
Nate Chapin9f169072021-06-09 19:32:373866 // Normally, cloning a NavigationEntryImpl results in sharing
3867 // FrameNavigationEntries between the original and the clone. However, when
3868 // cloning from a different NavigationControllerImpl, we want to fork the
3869 // FrameNavigationEntries.
Nate Chapin9f169072021-06-09 19:32:373870 entries_.insert(entries_.begin() + i,
Nate Chapin214a86a2021-06-21 20:35:573871 source->entries_[i]->CloneWithoutSharing(context.get()));
[email protected]e1cd5452010-08-26 18:03:253872 }
arthursonzogni5c4c202d2017-04-25 23:41:273873 DCHECK(pending_entry_index_ == -1 ||
3874 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:253875}
[email protected]c5b88d82012-10-06 17:03:333876
3877void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:183878 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:333879 get_timestamp_callback_ = get_timestamp_callback;
3880}
[email protected]8ff00d72012-10-23 19:12:213881
Shivani Sharmaffb32b82019-04-09 16:58:473882// History manipulation intervention:
3883void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:473884 bool replace_entry,
3885 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:403886 bool is_renderer_initiated,
3887 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:453888 // Note that for a subframe, previous_document_was_activated is true if the
3889 // gesture happened in any subframe (propagated to main frame) or in the main
3890 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:473891 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:273892 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:473893 return;
3894 }
3895 if (last_committed_entry_index_ == -1)
3896 return;
3897
Shivani Sharmac4cc8922019-04-18 03:11:173898 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473899
Alexander Timine3ec4192020-04-20 16:39:403900 // Log UKM with the URL we are navigating away from.
3901 ukm::builders::HistoryManipulationIntervention(
3902 previous_page_load_ukm_source_id)
3903 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:473904}
3905
Shivani Sharmac4cc8922019-04-18 03:11:173906void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
3907 int reference_index,
3908 bool skippable) {
3909 auto* reference_entry = GetEntryAtIndex(reference_index);
3910 reference_entry->set_should_skip_on_back_forward_ui(skippable);
3911
3912 int64_t document_sequence_number =
3913 reference_entry->root_node()->frame_entry->document_sequence_number();
3914 for (int index = 0; index < GetEntryCount(); index++) {
3915 auto* entry = GetEntryAtIndex(index);
3916 if (entry->root_node()->frame_entry->document_sequence_number() ==
3917 document_sequence_number) {
3918 entry->set_should_skip_on_back_forward_ui(skippable);
3919 }
3920 }
3921}
3922
arthursonzogni66f711c2019-10-08 14:40:363923std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
3924NavigationControllerImpl::ReferencePendingEntry() {
3925 DCHECK(pending_entry_);
3926 auto pending_entry_ref =
3927 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
3928 pending_entry_refs_.insert(pending_entry_ref.get());
3929 return pending_entry_ref;
3930}
3931
3932void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
3933 // Ignore refs that don't correspond to the current pending entry.
3934 auto it = pending_entry_refs_.find(ref);
3935 if (it == pending_entry_refs_.end())
3936 return;
3937 pending_entry_refs_.erase(it);
3938
3939 if (!pending_entry_refs_.empty())
3940 return;
3941
3942 // The pending entry may be deleted before the last PendingEntryRef.
3943 if (!pending_entry_)
3944 return;
3945
3946 // We usually clear the pending entry when the matching NavigationRequest
3947 // fails, so that an arbitrary URL isn't left visible above a committed page.
3948 //
3949 // However, we do preserve the pending entry in some cases, such as on the
3950 // initial navigation of an unmodified blank tab. We also allow the delegate
3951 // to say when it's safe to leave aborted URLs in the omnibox, to let the
3952 // user edit the URL and try again. This may be useful in cases that the
3953 // committed page cannot be attacker-controlled. In these cases, we still
3954 // allow the view to clear the pending entry and typed URL if the user
3955 // requests (e.g., hitting Escape with focus in the address bar).
3956 //
3957 // Do not leave the pending entry visible if it has an invalid URL, since this
3958 // might be formatted in an unexpected or unsafe way.
3959 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:363960 bool should_preserve_entry =
3961 (pending_entry_ == GetVisibleEntry()) &&
3962 pending_entry_->GetURL().is_valid() &&
3963 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
3964 if (should_preserve_entry)
3965 return;
3966
3967 DiscardPendingEntry(true);
3968 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3969}
3970
Titouan Rigoudy6ec70402021-02-02 15:42:193971std::unique_ptr<PolicyContainerPolicies>
3972NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:393973 RenderFrameHostImpl* rfh,
3974 bool is_same_document,
3975 NavigationRequest* request) {
Titouan Rigoudy6ec70402021-02-02 15:42:193976 if (!ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(request))
Antonio Sartori78a749f2020-11-30 12:03:393977 return nullptr;
3978
3979 if (is_same_document) {
Charlie Reis73e356242021-04-02 17:10:313980 // TODO(https://crbug.com/524208): Remove this nullptr check when we can
3981 // ensure we always have a FrameNavigationEntry here.
3982 if (!GetLastCommittedEntry())
3983 return nullptr;
3984
Antonio Sartori78a749f2020-11-30 12:03:393985 FrameNavigationEntry* previous_frame_entry =
3986 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
3987
3988 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
3989 // ensure we always have a FrameNavigationEntry here.
3990 if (!previous_frame_entry)
3991 return nullptr;
3992
Titouan Rigoudy6ec70402021-02-02 15:42:193993 const PolicyContainerPolicies* previous_policies =
3994 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:393995
Titouan Rigoudy6ec70402021-02-02 15:42:193996 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:393997 return nullptr;
3998
3999 // Make a copy of the policy container for the new FrameNavigationEntry.
Antonio Sartori5d09b30f2021-03-02 09:27:164000 return previous_policies->Clone();
Antonio Sartori78a749f2020-11-30 12:03:394001 }
4002
Antonio Sartori4f5373792021-05-31 10:56:474003 return rfh->policy_container_host()->policies().Clone();
Antonio Sartori78a749f2020-11-30 12:03:394004}
4005
Hayato Ito2c8c08d02021-06-23 03:38:434006void NavigationControllerImpl::BroadcastHistoryOffsetAndLength() {
Carlos Caballeroede6f8c2021-01-28 11:01:504007 OPTIONAL_TRACE_EVENT2(
Hayato Ito2c8c08d02021-06-23 03:38:434008 "content", "NavigationControllerImpl::BroadcastHistoryOffsetAndLength",
4009 "history_offset", GetLastCommittedEntryIndex(), "history_length",
4010 GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:504011
4012 auto callback = base::BindRepeating(
4013 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
4014 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
4015 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
4016 }
4017 },
Hayato Ito2c8c08d02021-06-23 03:38:434018 GetLastCommittedEntryIndex(), GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:504019 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
4020}
4021
4022void NavigationControllerImpl::DidAccessInitialMainDocument() {
4023 // We may have left a failed browser-initiated navigation in the address bar
4024 // to let the user edit it and try again. Clear it now that content might
4025 // show up underneath it.
4026 if (!frame_tree_.IsLoading() && GetPendingEntry())
4027 DiscardPendingEntry(false);
4028
4029 // Update the URL display.
4030 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
4031}
4032
4033void NavigationControllerImpl::UpdateStateForFrame(
4034 RenderFrameHostImpl* rfhi,
4035 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:564036 OPTIONAL_TRACE_EVENT1("content",
4037 "NavigationControllerImpl::UpdateStateForFrame",
4038 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:504039 // The state update affects the last NavigationEntry associated with the given
4040 // |render_frame_host|. This may not be the last committed NavigationEntry (as
4041 // in the case of an UpdateState from a frame being swapped out). We track
4042 // which entry this is in the RenderFrameHost's nav_entry_id.
4043 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
4044 if (!entry)
4045 return;
4046
4047 FrameNavigationEntry* frame_entry =
4048 entry->GetFrameEntry(rfhi->frame_tree_node());
4049 if (!frame_entry)
4050 return;
4051
4052 // The SiteInstance might not match if we do a cross-process navigation with
4053 // replacement (e.g., auto-subframe), in which case the swap out of the old
4054 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4055 // has already been replaced and destroyed.
4056 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4057 return;
4058
4059 if (page_state == frame_entry->page_state())
4060 return; // Nothing to update.
4061
4062 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4063
4064 // The document_sequence_number and item_sequence_number recorded in the
4065 // FrameNavigationEntry should not differ from the one coming with the update,
4066 // since it must come from the same document. Do not update it if a difference
4067 // is detected, as this indicates that |frame_entry| is not the correct one.
4068 blink::ExplodedPageState exploded_state;
4069 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4070 return;
4071
4072 if (exploded_state.top.document_sequence_number !=
4073 frame_entry->document_sequence_number() ||
4074 exploded_state.top.item_sequence_number !=
4075 frame_entry->item_sequence_number()) {
4076 return;
4077 }
4078
4079 frame_entry->SetPageState(page_state);
4080 NotifyEntryChanged(entry);
4081}
4082
Aaron Colwellb731a0ae2021-03-19 19:14:474083void NavigationControllerImpl::OnStoragePartitionIdAdded(
4084 const StoragePartitionId& partition_id) {
4085 auto it = partition_config_to_id_map_.insert(
4086 std::make_pair(partition_id.config(), partition_id));
4087 bool successful_insert = it.second;
Aaron Colwellb731a0ae2021-03-19 19:14:474088 if (!successful_insert) {
4089 LogStoragePartitionIdCrashKeys(it.first->second, partition_id);
4090 }
Sharon Yangd4f48792021-06-30 17:02:534091 CHECK(successful_insert);
Aaron Colwellb731a0ae2021-03-19 19:14:474092}
4093
4094void NavigationControllerImpl::LogStoragePartitionIdCrashKeys(
4095 const StoragePartitionId& original_partition_id,
4096 const StoragePartitionId& new_partition_id) {
4097 base::debug::SetCrashKeyString(
4098 base::debug::AllocateCrashKeyString("original_partition_id",
4099 base::debug::CrashKeySize::Size256),
4100 original_partition_id.ToString());
4101
4102 base::debug::SetCrashKeyString(
4103 base::debug::AllocateCrashKeyString("new_partition_id",
4104 base::debug::CrashKeySize::Size256),
4105 new_partition_id.ToString());
Aaron Colwellb731a0ae2021-03-19 19:14:474106}
4107
Minggang Wangb9f3fa92021-07-01 15:30:314108std::vector<blink::mojom::AppHistoryEntryPtr>
Nate Chapind1fe3612021-04-16 20:45:574109NavigationControllerImpl::PopulateSingleAppHistoryEntryVector(
4110 Direction direction,
4111 int entry_index,
4112 const url::Origin& pending_origin,
4113 FrameTreeNode* node,
4114 SiteInstance* site_instance,
4115 int64_t previous_item_sequence_number) {
Minggang Wangb9f3fa92021-07-01 15:30:314116 std::vector<blink::mojom::AppHistoryEntryPtr> entries;
Nate Chapind1fe3612021-04-16 20:45:574117 int offset = direction == Direction::kForward ? 1 : -1;
4118 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4119 i += offset) {
4120 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
4121 if (!frame_entry || !frame_entry->committed_origin())
4122 break;
4123 if (site_instance != frame_entry->site_instance())
4124 break;
4125 if (!pending_origin.IsSameOriginWith(*frame_entry->committed_origin()))
4126 break;
4127 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4128 continue;
4129 blink::ExplodedPageState exploded_page_state;
4130 if (blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4131 &exploded_page_state)) {
4132 blink::ExplodedFrameState frame_state = exploded_page_state.top;
Minggang Wangb9f3fa92021-07-01 15:30:314133 blink::mojom::AppHistoryEntryPtr entry =
4134 blink::mojom::AppHistoryEntry::New(
4135 frame_state.app_history_key.value_or(std::u16string()),
4136 frame_state.app_history_id.value_or(std::u16string()),
4137 frame_state.url_string.value_or(std::u16string()));
Nate Chapind1fe3612021-04-16 20:45:574138 DCHECK(pending_origin.CanBeDerivedFrom(GURL(entry->url)));
4139 entries.push_back(std::move(entry));
4140 previous_item_sequence_number = frame_entry->item_sequence_number();
4141 }
4142 }
4143 // If |entries| was constructed by iterating backwards from
4144 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4145 // earliest-at-the-front. Reverse it.
4146 if (direction == Direction::kBack)
4147 std::reverse(entries.begin(), entries.end());
4148 return entries;
4149}
4150
4151void NavigationControllerImpl::PopulateAppHistoryEntryVectors(
4152 NavigationRequest* request) {
4153 url::Origin pending_origin =
4154 request->commit_params().origin_to_commit
4155 ? *request->commit_params().origin_to_commit
4156 : url::Origin::Create(request->common_params().url);
4157
4158 FrameTreeNode* node = request->frame_tree_node();
4159 scoped_refptr<SiteInstance> site_instance =
4160 request->GetRenderFrameHost()->GetSiteInstance();
4161
4162 // NOTE: |entry_index| is an estimate of the index where this entry will
4163 // commit, but it may be wrong in corner cases (e.g., if we are at the max
4164 // entry limit, the earliest entry will be dropped). This is ok because this
4165 // algorithm only uses |entry_index| to walk the entry list as it stands right
4166 // now, and it isn't saved for anything post-commit.
4167 int entry_index = GetPendingEntryIndex();
4168 bool will_create_new_entry = false;
4169 if (NavigationTypeUtils::IsReload(request->common_params().navigation_type) ||
4170 request->common_params().should_replace_current_entry) {
4171 entry_index = GetLastCommittedEntryIndex();
4172 } else if (entry_index == -1) {
4173 will_create_new_entry = true;
4174 entry_index = GetLastCommittedEntryIndex() + 1;
4175 }
4176
4177 int64_t pending_item_sequence_number = 0;
4178 if (auto* pending_entry = GetPendingEntry()) {
4179 if (auto* frame_entry = pending_entry->GetFrameEntry(node))
4180 pending_item_sequence_number = frame_entry->item_sequence_number();
4181 }
4182
4183 request->set_app_history_back_entries(PopulateSingleAppHistoryEntryVector(
4184 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
4185 pending_item_sequence_number));
4186
4187 // Don't populate forward entries if they will be truncated by a new entry.
4188 if (!will_create_new_entry) {
4189 request->set_app_history_forward_entries(
4190 PopulateSingleAppHistoryEntryVector(
4191 Direction::kForward, entry_index, pending_origin, node,
4192 site_instance.get(), pending_item_sequence_number));
4193 }
4194}
4195
Nate Chapinfbfe5af2021-06-10 17:22:084196NavigationControllerImpl::HistoryNavigationAction
4197NavigationControllerImpl::ShouldNavigateToEntryForAppHistoryKey(
4198 FrameNavigationEntry* current_entry,
4199 FrameNavigationEntry* target_entry,
4200 const std::string& app_history_key) {
4201 if (!target_entry || !target_entry->committed_origin())
4202 return HistoryNavigationAction::kStopLooking;
4203 if (current_entry->site_instance() != target_entry->site_instance())
4204 return HistoryNavigationAction::kStopLooking;
4205 if (!current_entry->committed_origin()->IsSameOriginWith(
4206 *target_entry->committed_origin())) {
4207 return HistoryNavigationAction::kStopLooking;
4208 }
4209
4210 // NOTE: We don't actually care between kSameDocument and
4211 // kDifferentDocument, so always use kDifferentDocument by convention.
4212 if (target_entry->app_history_key() == app_history_key)
4213 return HistoryNavigationAction::kDifferentDocument;
4214 return HistoryNavigationAction::kKeepLooking;
4215}
4216
4217void NavigationControllerImpl::NavigateToAppHistoryKey(FrameTreeNode* node,
4218 const std::string& key) {
4219 FrameNavigationEntry* current_entry =
4220 GetLastCommittedEntry()->GetFrameEntry(node);
4221 if (!current_entry)
4222 return;
4223
4224 // We want to find the nearest matching entry that is contiguously
4225 // same-instance and same-origin. Check back first, then forward.
4226 // TODO(japhet): Link spec here once it exists.
4227 for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) {
4228 auto result = ShouldNavigateToEntryForAppHistoryKey(
4229 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4230 if (result == HistoryNavigationAction::kStopLooking)
4231 break;
4232 if (result != HistoryNavigationAction::kKeepLooking) {
4233 GoToIndex(i);
4234 return;
4235 }
4236 }
4237 for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) {
4238 auto result = ShouldNavigateToEntryForAppHistoryKey(
4239 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4240 if (result == HistoryNavigationAction::kStopLooking)
4241 break;
4242 if (result != HistoryNavigationAction::kKeepLooking) {
4243 GoToIndex(i);
4244 return;
4245 }
4246 }
4247}
4248
Hayato Ito7a80db42021-07-05 06:18:544249bool NavigationControllerImpl::ShouldMaintainTrivialSessionHistory() const {
4250 // TODO(https://crbug.com/1197384): We may have to add portals and fenced
4251 // frames in addition to prerender. This should be kept in sync with
4252 // LocalFrame version, LocalFrame::ShouldMaintainTrivialSessionHistory.
4253 return frame_tree_.is_prerendering();
4254}
4255
[email protected]8ff00d72012-10-23 19:12:214256} // namespace content