blob: 162cc8e66522cd51ab117f51296b30c7a2ae54a9 [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"
Aaron Colwellb731a0ae2021-03-19 19:14:4743#include "base/debug/dump_without_crashing.h"
initial.commit09911bf2008-07-26 23:55:2944#include "base/logging.h"
asvitkine30330812016-08-30 04:01:0845#include "base/metrics/histogram_macros.h"
Chris Hamilton83272dc2021-02-23 00:24:0246#include "base/stl_util.h"
Peter Kastingb53b81912021-04-28 19:23:3047#include "base/strings/string_piece.h"
[email protected]348fbaac2013-06-11 06:31:5148#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0049#include "base/strings/utf_string_conversions.h"
[email protected]a43858f2013-06-28 15:18:3750#include "base/time/time.h"
Carlos Caballero40b0efd2021-01-26 11:55:0051#include "base/trace_event/optional_trace_event.h"
Carlos Caballeroede6f8c2021-01-28 11:01:5052#include "base/trace_event/trace_conversion_helper.h"
ssid3e765612015-01-28 04:03:4253#include "base/trace_event/trace_event.h"
servolkf3955532015-05-16 00:01:5954#include "build/build_config.h"
[email protected]eabfe1912014-05-12 10:07:2855#include "cc/base/switches.h"
jamescookda2505812015-03-20 18:01:1856#include "content/browser/bad_message.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3957#include "content/browser/blob_storage/chrome_blob_storage_context.h"
[email protected]825b1662012-03-12 19:07:3158#include "content/browser/browser_url_handler_impl.h"
[email protected]5f2aa722013-08-07 16:59:4159#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0460#include "content/browser/dom_storage/session_storage_namespace_impl.h"
danakjc492bf82020-09-09 20:02:4461#include "content/browser/renderer_host/debug_urls.h"
62#include "content/browser/renderer_host/frame_tree.h"
Carlos Caballero40b0efd2021-01-26 11:55:0063#include "content/browser/renderer_host/frame_tree_node.h"
danakjc492bf82020-09-09 20:02:4464#include "content/browser/renderer_host/navigation_entry_impl.h"
65#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"
Carlos Caballeroede6f8c2021-01-28 11:01:50102#include "third_party/blink/public/common/page_state/page_state_serialization.h"
[email protected]cca6f392014-05-28 21:32:26103#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:29104
[email protected]8ff00d72012-10-23 19:12:21105namespace content {
[email protected]e9ba4472008-09-14 15:42:43106namespace {
107
108// Invoked when entries have been pruned, or removed. For example, if the
109// current entries are [google, digg, yahoo], with the current entry google,
110// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47111void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50112 int index,
[email protected]c12bf1a12008-09-17 16:28:49113 int count) {
[email protected]8ff00d72012-10-23 19:12:21114 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50115 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49116 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14117 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43118}
119
[email protected]e9ba4472008-09-14 15:42:43120// Configure all the NavigationEntries in entries for restore. This resets
121// the transition type to reload and makes sure the content state isn't empty.
122void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57123 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48124 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47125 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43126 // Use a transition type of reload so that we don't incorrectly increase
127 // the typed count.
Lei Zhang96031532019-10-10 19:05:47128 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
129 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43130 }
131}
132
[email protected]bf70edce2012-06-20 22:32:22133// Determines whether or not we should be carrying over a user agent override
134// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57135bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22136 return last_entry && last_entry->GetIsOverridingUserAgent();
137}
138
Camille Lamy5193caa2018-10-12 11:59:42139// Determines whether to override user agent for a navigation.
140bool ShouldOverrideUserAgent(
141 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57142 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42143 switch (override_user_agent) {
144 case NavigationController::UA_OVERRIDE_INHERIT:
145 return ShouldKeepOverride(last_committed_entry);
146 case NavigationController::UA_OVERRIDE_TRUE:
147 return true;
148 case NavigationController::UA_OVERRIDE_FALSE:
149 return false;
Camille Lamy5193caa2018-10-12 11:59:42150 }
151 NOTREACHED();
152 return false;
153}
154
clamy0a656e42018-02-06 18:18:28155// Returns true this navigation should be treated as a reload. For e.g.
156// navigating to the last committed url via the address bar or clicking on a
157// link which results in a navigation to the last committed or pending
158// navigation, etc.
Fergal Daly766177d2020-07-07 07:54:04159// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
160// |base_url_for_data_url|, |transition_type| correspond to the new navigation
161// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
162// navigation that committed.
163bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
164 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57165 const GURL& virtual_url,
166 const GURL& base_url_for_data_url,
167 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57168 bool is_post,
169 bool is_reload,
170 bool is_navigation_to_existing_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57171 NavigationEntryImpl* last_committed_entry) {
Fergal Daly766177d2020-07-07 07:54:04172 if (is_reload || is_navigation_to_existing_entry)
clamy0a656e42018-02-06 18:18:28173 return false;
clamy0a656e42018-02-06 18:18:28174 // Only convert to reload if at least one navigation committed.
175 if (!last_committed_entry)
ananta3bdd8ae2016-12-22 17:11:55176 return false;
177
arthursonzogni7a8243682017-12-14 16:41:42178 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28179 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42180 return false;
181
ananta3bdd8ae2016-12-22 17:11:55182 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
183 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
184 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28185 bool transition_type_can_be_converted = false;
186 if (ui::PageTransitionCoreTypeIs(transition_type,
187 ui::PAGE_TRANSITION_RELOAD) &&
188 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
189 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55190 }
clamy0a656e42018-02-06 18:18:28191 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55192 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28193 transition_type_can_be_converted = true;
194 }
195 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
196 transition_type_can_be_converted = true;
197 if (!transition_type_can_be_converted)
198 return false;
199
200 // This check is required for cases like view-source:, etc. Here the URL of
201 // the navigation entry would contain the url of the page, while the virtual
202 // URL contains the full URL including the view-source prefix.
203 if (virtual_url != last_committed_entry->GetVirtualURL())
204 return false;
205
Fergal Daly766177d2020-07-07 07:54:04206 // Check that the URLs match.
207 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
208 // If there's no frame entry then by definition the URLs don't match.
209 if (!frame_entry)
210 return false;
211
212 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28213 return false;
214
215 // This check is required for Android WebView loadDataWithBaseURL. Apps
216 // can pass in anything in the base URL and we need to ensure that these
217 // match before classifying it as a reload.
218 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
219 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
220 return false;
ananta3bdd8ae2016-12-22 17:11:55221 }
222
clamy0a656e42018-02-06 18:18:28223 // Skip entries with SSL errors.
224 if (last_committed_entry->ssl_error())
225 return false;
226
227 // Don't convert to a reload when the last navigation was a POST or the new
228 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04229 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28230 return false;
231
232 return true;
ananta3bdd8ae2016-12-22 17:11:55233}
234
Nasko Oskov03912102019-01-11 00:21:32235bool DoesURLMatchOriginForNavigation(
236 const GURL& url,
Anton Bikineevf62d1bf2021-05-15 17:56:07237 const absl::optional<url::Origin>& origin,
Kunihiko Sakamoto346a74e2021-03-10 08:57:48238 SubresourceWebBundleNavigationInfo*
239 subresource_web_bundle_navigation_info) {
Nasko Oskov03912102019-01-11 00:21:32240 // If there is no origin supplied there is nothing to match. This can happen
241 // for navigations to a pending entry and therefore it should be allowed.
242 if (!origin)
243 return true;
244
Kunihiko Sakamoto346a74e2021-03-10 08:57:48245 if (url.SchemeIs(url::kUrnScheme) && subresource_web_bundle_navigation_info) {
246 // Urn: subframe from WebBundle has an opaque origin derived from the
247 // Bundle's origin.
248 return origin->CanBeDerivedFrom(
249 subresource_web_bundle_navigation_info->bundle_url());
250 }
251
Nasko Oskov03912102019-01-11 00:21:32252 return origin->CanBeDerivedFrom(url);
253}
254
Anton Bikineevf62d1bf2021-05-15 17:56:07255absl::optional<url::Origin> GetCommittedOriginForFrameEntry(
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12256 const mojom::DidCommitProvisionalLoadParams& params,
257 NavigationRequest* request) {
Nasko Oskov03912102019-01-11 00:21:32258 // Error pages commit in an opaque origin, yet have the real URL that resulted
259 // in an error as the |params.url|. Since successful reload of an error page
260 // should commit in the correct origin, setting the opaque origin on the
261 // FrameNavigationEntry will be incorrect.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12262 if (request->DidEncounterError())
Anton Bikineevf62d1bf2021-05-15 17:56:07263 return absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:32264
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12265 // We also currently don't save committed origins for loadDataWithBaseURL
266 // navigations (probably accidentally). Without this check, navigations to
267 // the FrameNavigationEntry might fail the DoesURLMatchOriginForNavigation()
268 // check since the origin will be based on the base URL instead of the data:
269 // URL used for the navigation.
270 // TODO(https://crbug.com/1198406): Save committed origin in
271 // FrameNavigationEntry for this case too.
Anton Bikineevf62d1bf2021-05-15 17:56:07272 absl::optional<std::string> data_url_as_string;
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12273#if defined(OS_ANDROID)
274 data_url_as_string = request->commit_params().data_url_as_string;
275#endif
276 if (NavigationRequest::IsLoadDataWithBaseURLAndUnreachableURL(
277 request->IsInMainFrame(), request->common_params(),
278 data_url_as_string)) {
Anton Bikineevf62d1bf2021-05-15 17:56:07279 return absl::nullopt;
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12280 }
281
Anton Bikineevf62d1bf2021-05-15 17:56:07282 return absl::make_optional(params.origin);
Nasko Oskov03912102019-01-11 00:21:32283}
284
Camille Lamy5193caa2018-10-12 11:59:42285bool IsValidURLForNavigation(bool is_main_frame,
286 const GURL& virtual_url,
287 const GURL& dest_url) {
288 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
289 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
290 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
291 << virtual_url.possibly_invalid_spec();
292 return false;
293 }
294
295 // Don't attempt to navigate to non-empty invalid URLs.
296 if (!dest_url.is_valid() && !dest_url.is_empty()) {
297 LOG(WARNING) << "Refusing to load invalid URL: "
298 << dest_url.possibly_invalid_spec();
299 return false;
300 }
301
302 // The renderer will reject IPC messages with URLs longer than
303 // this limit, so don't attempt to navigate with a longer URL.
304 if (dest_url.spec().size() > url::kMaxURLChars) {
305 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
306 << " characters.";
307 return false;
308 }
309
Aaron Colwell33109c592020-04-21 21:31:19310 // Reject renderer debug URLs because they should have been handled before
311 // we get to this point. This check handles renderer debug URLs
312 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
313 // provides defense-in-depth if a renderer debug URL manages to get here via
314 // some other path. We want to reject the navigation here so it doesn't
315 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30316 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19317 LOG(WARNING) << "Refusing to load renderer debug URL: "
318 << dest_url.possibly_invalid_spec();
319 return false;
320 }
321
Camille Lamy5193caa2018-10-12 11:59:42322 return true;
323}
324
Mikel Astizba9cf2fd2017-12-17 10:38:10325// See replaced_navigation_entry_data.h for details: this information is meant
326// to ensure |*output_entry| keeps track of its original URL (landing page in
327// case of server redirects) as it gets replaced (e.g. history.replaceState()),
328// without overwriting it later, for main frames.
329void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57330 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10331 NavigationEntryImpl* output_entry) {
332 if (output_entry->GetReplacedEntryData().has_value())
333 return;
334
335 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57336 data.first_committed_url = replaced_entry->GetURL();
337 data.first_timestamp = replaced_entry->GetTimestamp();
338 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29339 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10340}
341
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51342mojom::NavigationType GetNavigationType(const GURL& old_url,
343 const GURL& new_url,
344 ReloadType reload_type,
345 NavigationEntryImpl* entry,
346 const FrameNavigationEntry& frame_entry,
danakjd83d706d2020-11-25 22:11:12347 bool has_pending_cross_document_commit,
danakjb952ef12021-01-14 19:58:49348 bool is_currently_error_page,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51349 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23350 // Reload navigations
351 switch (reload_type) {
352 case ReloadType::NORMAL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51353 return mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23354 case ReloadType::BYPASSING_CACHE:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51355 return mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23356 case ReloadType::ORIGINAL_REQUEST_URL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51357 return mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23358 case ReloadType::NONE:
359 break; // Fall through to rest of function.
360 }
361
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08362 if (entry->IsRestored()) {
Lei Zhang96031532019-10-10 19:05:47363 return entry->GetHasPostData() ? mojom::NavigationType::RESTORE_WITH_POST
364 : mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23365 }
366
danakjb952ef12021-01-14 19:58:49367 const bool can_be_same_document =
368 // A pending cross-document commit means this navigation will not occur in
369 // the current document, as that document would end up being replaced in
370 // the meantime.
371 !has_pending_cross_document_commit &&
372 // If the current document is an error page, we should always treat it as
373 // a different-document navigation so that we'll attempt to load the
374 // document we're navigating to (and not stay in the current error page).
375 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12376
clamyea99ea12018-05-28 13:54:23377 // History navigations.
378 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12379 return can_be_same_document && is_same_document_history_load
Lei Zhang96031532019-10-10 19:05:47380 ? mojom::NavigationType::HISTORY_SAME_DOCUMENT
381 : mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23382 }
383 DCHECK(!is_same_document_history_load);
384
385 // A same-document fragment-navigation happens when the only part of the url
386 // that is modified is after the '#' character.
387 //
388 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12389 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23390 //
391 // Note: this check is only valid for navigations that are not history
392 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
393 // history navigation from 'A#foo' to 'A#bar' is not a same-document
394 // navigation, but a different-document one. This is why history navigation
395 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47396 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
397 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12398
399 // The one case where we do the wrong thing here and incorrectly choose
400 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
401 // the renderer is a frameset. All frameset navigations should be
402 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
403 // navigation would do the right thing, as it would send it to the browser and
404 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
405 // into this method). But since we can't tell that case here for browser-
406 // initiated navigations, we have to get the renderer involved. In that case
407 // the navigation would be restarted due to the renderer spending a reply of
408 // mojom::CommitResult::RestartCrossDocument.
409
410 return can_be_same_document && is_same_doc
411 ? mojom::NavigationType::SAME_DOCUMENT
412 : mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23413}
414
Camille Lamy5193caa2018-10-12 11:59:42415// Adjusts the original input URL if needed, to get the URL to actually load and
416// the virtual URL, which may differ.
417void RewriteUrlForNavigation(const GURL& original_url,
418 BrowserContext* browser_context,
419 GURL* url_to_load,
420 GURL* virtual_url,
421 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42422 // Allow the browser URL handler to rewrite the URL. This will, for example,
423 // remove "view-source:" from the beginning of the URL to get the URL that
424 // will actually be loaded. This real URL won't be shown to the user, just
425 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31426 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42427 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
428 url_to_load, browser_context, reverse_on_redirect);
429}
430
431#if DCHECK_IS_ON()
432// Helper sanity check function used in debug mode.
433void ValidateRequestMatchesEntry(NavigationRequest* request,
434 NavigationEntryImpl* entry) {
435 if (request->frame_tree_node()->IsMainFrame()) {
436 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
437 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
438 request->common_params().transition, entry->GetTransitionType()));
439 }
440 DCHECK_EQ(request->common_params().should_replace_current_entry,
441 entry->should_replace_entry());
Nasko Oskovc36327d2019-01-03 23:23:04442 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42443 entry->should_clear_history_list());
444 DCHECK_EQ(request->common_params().has_user_gesture,
445 entry->has_user_gesture());
446 DCHECK_EQ(request->common_params().base_url_for_data_url,
447 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04448 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42449 entry->GetCanLoadLocalResources());
450 DCHECK_EQ(request->common_params().started_from_context_menu,
451 entry->has_started_from_context_menu());
452
453 FrameNavigationEntry* frame_entry =
454 entry->GetFrameEntry(request->frame_tree_node());
455 if (!frame_entry) {
456 NOTREACHED();
457 return;
458 }
459
Camille Lamy5193caa2018-10-12 11:59:42460 DCHECK_EQ(request->common_params().method, frame_entry->method());
461
Nasko Oskovc36327d2019-01-03 23:23:04462 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42463 if (redirect_size == frame_entry->redirect_chain().size()) {
464 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04465 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42466 frame_entry->redirect_chain()[i]);
467 }
468 } else {
469 NOTREACHED();
470 }
471}
472#endif // DCHECK_IS_ON()
473
Dave Tapuska8bfd84c2019-03-26 20:47:16474// Returns whether the session history NavigationRequests in |navigations|
475// would stay within the subtree of the sandboxed iframe in
476// |sandbox_frame_tree_node_id|.
477bool DoesSandboxNavigationStayWithinSubtree(
478 int sandbox_frame_tree_node_id,
479 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
480 for (auto& item : navigations) {
481 bool within_subtree = false;
482 // Check whether this NavigationRequest affects a frame within the
483 // sandboxed frame's subtree by walking up the tree looking for the
484 // sandboxed frame.
485 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03486 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16487 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
488 within_subtree = true;
489 break;
490 }
491 }
492 if (!within_subtree)
493 return false;
494 }
495 return true;
496}
497
Titouan Rigoudy6ec70402021-02-02 15:42:19498bool ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(
Antonio Sartori78a749f2020-11-30 12:03:39499 const NavigationRequest* request) {
500 // For local schemes we need to store the policy container in the
501 // FrameNavigationEntry, so that we can reload it in case of history
502 // navigation.
503 //
504 // TODO(https://crbug.com/1146361 and https://crbug.com/1146362): blob: and
505 // filesystem: should be removed from this list when we have properly
506 // implemented storing their policy container in the respective store.
507 return (request->common_params().url.SchemeIs(url::kAboutScheme) ||
508 request->common_params().url.SchemeIs(url::kDataScheme) ||
509 request->common_params().url.SchemeIsBlob() ||
510 request->common_params().url.SchemeIsFileSystem());
511}
512
[email protected]e9ba4472008-09-14 15:42:43513} // namespace
514
arthursonzogni66f711c2019-10-08 14:40:36515// NavigationControllerImpl::PendingEntryRef------------------------------------
516
517NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
518 base::WeakPtr<NavigationControllerImpl> controller)
519 : controller_(controller) {}
520
521NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
522 if (!controller_) // Can be null with interstitials.
523 return;
524
525 controller_->PendingEntryRefDeleted(this);
526}
527
[email protected]d202a7c2012-01-04 07:53:47528// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29529
[email protected]23a918b2014-07-15 09:51:36530const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23531
[email protected]765b35502008-08-21 00:51:20532// static
[email protected]d202a7c2012-01-04 07:53:47533size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23534 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20535
[email protected]e6fec472013-05-14 05:29:02536// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20537// when testing.
538static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29539
[email protected]71fde352011-12-29 03:29:56540// static
dcheng9bfa5162016-04-09 01:00:57541std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
542 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10543 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07544 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10545 ui::PageTransition transition,
546 bool is_renderer_initiated,
547 const std::string& extra_headers,
548 BrowserContext* browser_context,
549 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
550 return NavigationControllerImpl::CreateNavigationEntry(
551 url, referrer, std::move(initiator_origin),
552 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Rakina Zata Amni66992a82020-06-24 03:57:52553 extra_headers, browser_context, std::move(blob_url_loader_factory),
Scott Violet5ae6c42e2020-10-28 02:47:37554 false /* should_replace_entry */, nullptr /* web_contents */);
Lukasz Anforowicz641234d52019-11-07 21:07:10555}
556
557// static
558std::unique_ptr<NavigationEntryImpl>
559NavigationControllerImpl::CreateNavigationEntry(
560 const GURL& url,
561 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07562 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10563 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57564 ui::PageTransition transition,
565 bool is_renderer_initiated,
566 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09567 BrowserContext* browser_context,
Rakina Zata Amni66992a82020-06-24 03:57:52568 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Scott Violet5ae6c42e2020-10-28 02:47:37569 bool should_replace_entry,
570 WebContents* web_contents) {
Camille Lamy5193caa2018-10-12 11:59:42571 GURL url_to_load;
572 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56573 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42574 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
575 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56576
Lukasz Anforowicz641234d52019-11-07 21:07:10577 // Let the NTP override the navigation params and pretend that this is a
578 // browser-initiated, bookmark-like navigation.
579 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violet5ae6c42e2020-10-28 02:47:37580 web_contents, source_site_instance, &transition, &is_renderer_initiated,
581 &referrer, &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10582
Patrick Monettef507e982019-06-19 20:18:06583 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28584 nullptr, // The site instance for tabs is sent on navigation
585 // (WebContents::GetSiteInstance).
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58586 url_to_load, referrer, initiator_origin, std::u16string(), transition,
Camille Lamy5193caa2018-10-12 11:59:42587 is_renderer_initiated, blob_url_loader_factory);
588 entry->SetVirtualURL(virtual_url);
589 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56590 entry->set_update_virtual_url_with_url(reverse_on_redirect);
591 entry->set_extra_headers(extra_headers);
Rakina Zata Amni66992a82020-06-24 03:57:52592 entry->set_should_replace_entry(should_replace_entry);
Patrick Monettef507e982019-06-19 20:18:06593 return entry;
[email protected]71fde352011-12-29 03:29:56594}
595
[email protected]cdcb1dee2012-01-04 00:46:20596// static
597void NavigationController::DisablePromptOnRepost() {
598 g_check_for_repost = false;
599}
600
[email protected]c5b88d82012-10-06 17:03:33601base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
602 base::Time t) {
603 // If |t| is between the water marks, we're in a run of duplicates
604 // or just getting out of it, so increase the high-water mark to get
605 // a time that probably hasn't been used before and return it.
606 if (low_water_mark_ <= t && t <= high_water_mark_) {
607 high_water_mark_ += base::TimeDelta::FromMicroseconds(1);
608 return high_water_mark_;
609 }
610
611 // Otherwise, we're clear of the last duplicate run, so reset the
612 // water marks.
613 low_water_mark_ = high_water_mark_ = t;
614 return t;
615}
616
ckitagawa0faa5e42020-06-17 17:30:54617NavigationControllerImpl::ScopedShowRepostDialogForTesting::
618 ScopedShowRepostDialogForTesting()
619 : was_disallowed_(g_check_for_repost) {
620 g_check_for_repost = true;
621}
622
623NavigationControllerImpl::ScopedShowRepostDialogForTesting::
624 ~ScopedShowRepostDialogForTesting() {
625 g_check_for_repost = was_disallowed_;
626}
627
[email protected]d202a7c2012-01-04 07:53:47628NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00629 BrowserContext* browser_context,
630 FrameTree& frame_tree,
631 NavigationControllerDelegate* delegate)
632 : frame_tree_(frame_tree),
633 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57634 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22635 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47636 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37637 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29638}
639
[email protected]d202a7c2012-01-04 07:53:47640NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00641 // The NavigationControllerImpl might be called inside its delegate
642 // destructor. Calling it is not valid anymore.
643 delegate_ = nullptr;
644 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29645}
646
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57647WebContents* NavigationControllerImpl::GetWebContents() {
[email protected]ec6c05f2013-10-23 18:41:57648 return delegate_->GetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32649}
650
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57651BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55652 return browser_context_;
653}
654
[email protected]d202a7c2012-01-04 07:53:47655void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30656 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36657 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57658 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57659 // Verify that this controller is unused and that the input is valid.
Lei Zhang96031532019-10-10 19:05:47660 DCHECK_EQ(0, GetEntryCount());
661 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57662 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14663 selected_navigation < static_cast<int>(entries->size()));
arthursonzogni5c4c202d2017-04-25 23:41:27664 DCHECK_EQ(-1, pending_entry_index_);
[email protected]ce3fa3c2009-04-20 19:55:57665
[email protected]ce3fa3c2009-04-20 19:55:57666 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44667 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03668 entries_.reserve(entries->size());
669 for (auto& entry : *entries)
dcheng36b6aec92015-12-26 06:16:36670 entries_.push_back(
671 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
avif16f85a72015-11-13 18:25:03672
673 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
674 // cleared out safely.
675 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57676
677 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36678 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57679}
680
toyoshim6142d96f2016-12-19 09:07:25681void NavigationControllerImpl::Reload(ReloadType reload_type,
682 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28683 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28684 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32685 int current_index = -1;
686
Carlos IL42b416592019-10-07 23:10:36687 if (entry_replaced_by_post_commit_error_) {
688 // If there is an entry that was replaced by a currently active post-commit
689 // error navigation, this can't be the initial navigation.
690 DCHECK(!IsInitialNavigation());
691 // If the current entry is a post commit error, we reload the entry it
692 // replaced instead. We leave the error entry in place until a commit
693 // replaces it, but the pending entry points to the original entry in the
694 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
695 // case that pending_entry_ != entries_[pending_entry_index_].
696 entry = entry_replaced_by_post_commit_error_.get();
697 current_index = GetCurrentEntryIndex();
698 } else if (IsInitialNavigation() && pending_entry_) {
699 // If we are reloading the initial navigation, just use the current
700 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32701 entry = pending_entry_;
702 // The pending entry might be in entries_ (e.g., after a Clone), so we
703 // should also update the current_index.
704 current_index = pending_entry_index_;
705 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00706 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32707 current_index = GetCurrentEntryIndex();
708 if (current_index != -1) {
creis3da03872015-02-20 21:12:32709 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32710 }
[email protected]979a4bc2013-04-24 01:27:15711 }
[email protected]241db352013-04-22 18:04:05712
[email protected]59167c22013-06-03 18:07:32713 // If we are no where, then we can't reload. TODO(darin): We should add a
714 // CanReload method.
715 if (!entry)
716 return;
717
Takashi Toyoshimac7df3c22019-06-25 14:18:47718 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26719 entry->set_reload_type(reload_type);
720
Aran Gilman37d11632019-10-08 23:07:15721 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30722 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07723 // they really want to do this. If they do, the dialog will call us back
724 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57725 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02726
[email protected]106a0812010-03-18 00:15:12727 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57728 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47729 return;
initial.commit09911bf2008-07-26 23:55:29730 }
Lei Zhang96031532019-10-10 19:05:47731
732 if (!IsInitialNavigation())
733 DiscardNonCommittedEntries();
734
735 pending_entry_ = entry;
736 pending_entry_index_ = current_index;
737 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
738
739 NavigateToExistingPendingEntry(reload_type,
740 FrameTreeNode::kFrameTreeNodeInvalidId);
initial.commit09911bf2008-07-26 23:55:29741}
742
[email protected]d202a7c2012-01-04 07:53:47743void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48744 DCHECK(pending_reload_ != ReloadType::NONE);
745 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12746}
747
[email protected]d202a7c2012-01-04 07:53:47748void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48749 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12750 NOTREACHED();
751 } else {
toyoshim6142d96f2016-12-19 09:07:25752 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48753 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12754 }
755}
756
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57757bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09758 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11759}
760
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57761bool NavigationControllerImpl::IsInitialBlankNavigation() {
creis10a4ab72015-10-13 17:22:40762 // TODO(creis): Once we create a NavigationEntry for the initial blank page,
toyoshim0df1d3a2016-09-09 09:52:48763 // we'll need to check for entry count 1 and restore_type NONE (to exclude
764 // the cloned tab case).
creis10a4ab72015-10-13 17:22:40765 return IsInitialNavigation() && GetEntryCount() == 0;
766}
767
Aran Gilman37d11632019-10-08 23:07:15768NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
769 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58770 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03771 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58772}
773
W. James MacLean1c40862c2020-04-27 21:05:57774void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
775 const url::Origin& origin) {
776 for (int i = 0; i < GetEntryCount(); i++) {
777 auto* entry = GetEntryAtIndex(i);
778 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
779 }
780 if (entry_replaced_by_post_commit_error_) {
781 // It's possible we could come back to this entry if the error
782 // page/interstitial goes away.
783 entry_replaced_by_post_commit_error_
784 ->RegisterExistingOriginToPreventOptInIsolation(origin);
785 }
786 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
787 // visiting pending_entry_, which lacks a committed origin. This will be done
788 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
789}
790
avi25764702015-06-23 15:43:37791void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57792 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00793 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37794 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27795 DCHECK_EQ(-1, pending_entry_index_);
[email protected]8ff00d72012-10-23 19:12:21796 NotificationService::current()->Notify(
Aran Gilman37d11632019-10-08 23:07:15797 NOTIFICATION_NAV_ENTRY_PENDING, Source<NavigationController>(this),
avi25764702015-06-23 15:43:37798 Details<NavigationEntry>(pending_entry_));
[email protected]765b35502008-08-21 00:51:20799}
800
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57801NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47802 if (pending_entry_)
803 return pending_entry_;
804 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20805}
806
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57807NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32808 // The pending entry is safe to return for new (non-history), browser-
809 // initiated navigations. Most renderer-initiated navigations should not
810 // show the pending entry, to prevent URL spoof attacks.
811 //
812 // We make an exception for renderer-initiated navigations in new tabs, as
813 // long as no other page has tried to access the initial empty document in
814 // the new tab. If another page modifies this blank page, a URL spoof is
815 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32816 bool safe_to_show_pending =
817 pending_entry_ &&
818 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09819 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32820 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46821 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32822
823 // Also allow showing the pending entry for history navigations in a new tab,
824 // such as Ctrl+Back. In this case, no existing page is visible and no one
825 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15826 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
827 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32828 safe_to_show_pending = true;
829
830 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19831 return pending_entry_;
832 return GetLastCommittedEntry();
833}
834
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57835int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20836 if (pending_entry_index_ != -1)
837 return pending_entry_index_;
838 return last_committed_entry_index_;
839}
840
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57841NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20842 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28843 return nullptr;
avif16f85a72015-11-13 18:25:03844 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20845}
846
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57847bool NavigationControllerImpl::CanViewSource() {
Carlos Caballeroede6f8c2021-01-28 11:01:50848 const std::string& mime_type = frame_tree_.root()
Alex Moshchuk2e470ea2021-02-03 06:46:34849 ->current_frame_host()
850 ->render_view_host()
Carlos Caballeroede6f8c2021-01-28 11:01:50851 ->contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53852 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
853 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27854 NavigationEntry* visible_entry = GetVisibleEntry();
855 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39856 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16857}
858
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57859int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27860 // The last committed entry index must always be less than the number of
Carlos IL4dea8902020-05-26 15:14:29861 // entries. If there are no entries, it must be -1.
arthursonzogni5c4c202d2017-04-25 23:41:27862 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
863 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55864 return last_committed_entry_index_;
865}
866
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57867int NavigationControllerImpl::GetEntryCount() {
Carlos IL4dea8902020-05-26 15:14:29868 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55869 return static_cast<int>(entries_.size());
870}
871
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57872NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37873 if (index < 0 || index >= GetEntryCount())
874 return nullptr;
875
avif16f85a72015-11-13 18:25:03876 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25877}
878
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57879NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47880 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20881}
882
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57883int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46884 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03885}
886
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57887bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03888 if (!base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention))
889 return CanGoToOffset(-1);
890
891 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 if (!base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention))
900 return CanGoToOffset(1);
901
902 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
903 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
904 return true;
905 }
906 return false;
[email protected]765b35502008-08-21 00:51:20907}
908
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57909bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03910 int index = GetIndexForOffset(offset);
911 return index >= 0 && index < GetEntryCount();
912}
913
WangHui74286d52021-03-31 16:17:15914#if defined(OS_ANDROID)
915bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
916 if (!base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention))
917 return CanGoToOffset(offset);
918
919 if (offset == 0)
920 return true;
921 int increment = offset > 0 ? 1 : -1;
922 int non_skippable_entries = 0;
923 for (int index = GetIndexForOffset(increment);
924 index >= 0 && index < GetEntryCount(); index += increment) {
925 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
926 non_skippable_entries++;
927
928 if (non_skippable_entries == std::abs(offset))
929 return true;
930 }
931 return false;
932}
933#endif
934
[email protected]d202a7c2012-01-04 07:53:47935void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06936 int target_index = GetIndexForOffset(-1);
937
938 // Log metrics for the number of entries that are eligible for skipping on
Shivani Sharma2d5b4b6b2019-01-08 16:07:16939 // back button and move the target index past the skippable entries, if
940 // history intervention is enabled.
shivanisha55201872018-12-13 04:29:06941 int count_entries_skipped = 0;
Shivani Sharma298d12852019-01-22 20:04:03942 bool all_skippable_entries = true;
Aran Gilman37d11632019-10-08 23:07:15943 bool history_intervention_enabled =
944 base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention);
shivanisha55201872018-12-13 04:29:06945 for (int index = target_index; index >= 0; index--) {
Shivani Sharma2d5b4b6b2019-01-08 16:07:16946 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
shivanisha55201872018-12-13 04:29:06947 count_entries_skipped++;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16948 } else {
949 if (history_intervention_enabled)
950 target_index = index;
Shivani Sharma298d12852019-01-22 20:04:03951 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06952 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16953 }
shivanisha55201872018-12-13 04:29:06954 }
Miyoung Shin1c565c912021-03-17 12:11:21955
shivanisha55201872018-12-13 04:29:06956 UMA_HISTOGRAM_ENUMERATION("Navigation.BackForward.BackTargetSkipped",
Miyoung Shin1c565c912021-03-17 12:11:21957 count_entries_skipped,
958 blink::kMaxSessionHistoryEntries);
Shivani Sharma298d12852019-01-22 20:04:03959 UMA_HISTOGRAM_BOOLEAN("Navigation.BackForward.AllBackTargetsSkippable",
960 all_skippable_entries);
961
962 // Do nothing if all entries are skippable. Normally this path would not
963 // happen as consumers would have already checked it in CanGoBack but a lot of
964 // tests do not do that.
965 if (history_intervention_enabled && all_skippable_entries)
966 return;
shivanisha55201872018-12-13 04:29:06967
shivanisha55201872018-12-13 04:29:06968 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20969}
970
[email protected]d202a7c2012-01-04 07:53:47971void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06972 int target_index = GetIndexForOffset(1);
973
974 // Log metrics for the number of entries that are eligible for skipping on
Shivani Sharma2d5b4b6b2019-01-08 16:07:16975 // forward button and move the target index past the skippable entries, if
976 // history intervention is enabled.
977 // Note that at least one entry (the last one) will be non-skippable since
978 // entries are marked skippable only when they add another entry because of
979 // redirect or pushState.
shivanisha55201872018-12-13 04:29:06980 int count_entries_skipped = 0;
Aran Gilman37d11632019-10-08 23:07:15981 bool history_intervention_enabled =
982 base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention);
shivanisha55201872018-12-13 04:29:06983 for (size_t index = target_index; index < entries_.size(); index++) {
Shivani Sharma2d5b4b6b2019-01-08 16:07:16984 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
shivanisha55201872018-12-13 04:29:06985 count_entries_skipped++;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16986 } else {
987 if (history_intervention_enabled)
988 target_index = index;
shivanisha55201872018-12-13 04:29:06989 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16990 }
shivanisha55201872018-12-13 04:29:06991 }
992 UMA_HISTOGRAM_ENUMERATION("Navigation.BackForward.ForwardTargetSkipped",
Miyoung Shin1c565c912021-03-17 12:11:21993 count_entries_skipped,
994 blink::kMaxSessionHistoryEntries);
shivanisha55201872018-12-13 04:29:06995
shivanisha55201872018-12-13 04:29:06996 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20997}
998
[email protected]d202a7c2012-01-04 07:53:47999void NavigationControllerImpl::GoToIndex(int index) {
Dave Tapuska8bfd84c2019-03-26 20:47:161000 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId);
1001}
1002
1003void NavigationControllerImpl::GoToIndex(int index,
1004 int sandbox_frame_tree_node_id) {
sunjian30574a62017-03-21 21:39:441005 TRACE_EVENT0("browser,navigation,benchmark",
1006 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:201007 if (index < 0 || index >= static_cast<int>(entries_.size())) {
1008 NOTREACHED();
1009 return;
1010 }
1011
[email protected]cbab76d2008-10-13 22:42:471012 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:201013
arthursonzogni5c4c202d2017-04-25 23:41:271014 DCHECK_EQ(nullptr, pending_entry_);
1015 DCHECK_EQ(-1, pending_entry_index_);
1016 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:201017 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:271018 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
1019 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Dave Tapuska8bfd84c2019-03-26 20:47:161020 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id);
[email protected]765b35502008-08-21 00:51:201021}
1022
[email protected]d202a7c2012-01-04 07:53:471023void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:121024 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:031025 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:201026 return;
1027
[email protected]9ba14052012-06-22 23:50:031028 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:201029}
1030
WangHui74286d52021-03-31 16:17:151031#if defined(OS_ANDROID)
1032void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
1033 // Note: This is actually reached in unit tests.
1034 if (!CanGoToOffsetWithSkipping(offset))
1035 return;
1036
1037 bool history_intervention_enabled =
1038 base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention);
1039 if (offset == 0 || !history_intervention_enabled) {
1040 GoToIndex(GetIndexForOffset(offset));
1041 return;
1042 }
1043 int increment = offset > 0 ? 1 : -1;
1044 // Find the offset without counting skippable entries.
1045 int target_index = GetIndexForOffset(increment);
1046 int non_skippable_entries = 0;
1047 for (int index = target_index; index >= 0 && index < GetEntryCount();
1048 index += increment) {
1049 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1050 non_skippable_entries++;
1051
1052 if (non_skippable_entries == std::abs(offset)) {
1053 target_index = index;
1054 break;
1055 }
1056 }
1057
1058 GoToIndex(target_index);
1059}
1060#endif
1061
[email protected]41374f12013-07-24 02:49:281062bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151063 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281064 return false;
[email protected]6a13a6c2011-12-20 21:47:121065
[email protected]43032342011-03-21 14:10:311066 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281067 return true;
[email protected]cbab76d2008-10-13 22:42:471068}
1069
Michael Thiessen9b14d512019-09-23 21:19:471070void NavigationControllerImpl::PruneForwardEntries() {
1071 DiscardNonCommittedEntries();
1072 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471073 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471074 if (num_removed <= 0)
1075 return;
1076 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1077 NotifyPrunedEntries(this, remove_start_index /* start index */,
1078 num_removed /* count */);
1079}
1080
Aran Gilman37d11632019-10-08 23:07:151081void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1082 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321083 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311084 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511085 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1086 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321087 }
1088}
1089
Harkiran Bolariaba823e42021-05-21 18:30:361090base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL(
1091 const GURL& url,
1092 const Referrer& referrer,
1093 ui::PageTransition transition,
1094 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471095 LoadURLParams params(url);
1096 params.referrer = referrer;
1097 params.transition_type = transition;
1098 params.extra_headers = extra_headers;
Harkiran Bolariaba823e42021-05-21 18:30:361099 return LoadURLWithParams(params);
[email protected]cf002332012-08-14 19:17:471100}
1101
Harkiran Bolariaba823e42021-05-21 18:30:361102base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams(
1103 const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061104 if (params.is_renderer_initiated)
1105 DCHECK(params.initiator_origin.has_value());
1106
naskob8744d22014-08-28 17:07:431107 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151108 "NavigationControllerImpl::LoadURLWithParams", "url",
1109 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291110 bool is_explicit_navigation =
1111 GetContentClient()->browser()->IsExplicitNavigation(
1112 params.transition_type);
1113 if (HandleDebugURL(params.url, params.transition_type,
1114 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431115 // If Telemetry is running, allow the URL load to proceed as if it's
1116 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491117 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431118 cc::switches::kEnableGpuBenchmarking))
Harkiran Bolariaba823e42021-05-21 18:30:361119 return nullptr;
[email protected]47752982014-07-29 08:01:431120 }
[email protected]8bf1048012012-02-08 01:22:181121
[email protected]cf002332012-08-14 19:17:471122 // Checks based on params.load_type.
1123 switch (params.load_type) {
1124 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431125 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471126 break;
1127 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261128 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471129 NOTREACHED() << "Data load must use data scheme.";
Harkiran Bolariaba823e42021-05-21 18:30:361130 return nullptr;
[email protected]cf002332012-08-14 19:17:471131 }
1132 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461133 }
[email protected]e47ae9472011-10-13 19:48:341134
[email protected]e47ae9472011-10-13 19:48:341135 // The user initiated a load, we don't need to reload anymore.
1136 needs_reload_ = false;
1137
Harkiran Bolariaba823e42021-05-21 18:30:361138 return NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441139}
1140
Mohamed Abdelhalim833de902019-09-16 17:41:451141bool NavigationControllerImpl::PendingEntryMatchesRequest(
1142 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191143 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451144 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191145}
1146
[email protected]d202a7c2012-01-04 07:53:471147bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321148 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071149 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331150 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441151 bool is_same_document_navigation,
Shivani Sharmaffb32b82019-04-09 16:58:471152 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131153 NavigationRequest* navigation_request) {
1154 DCHECK(navigation_request);
[email protected]cd2e15742013-03-08 04:08:311155 is_initial_navigation_ = false;
1156
[email protected]0e8db942008-09-24 21:21:481157 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431158 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481159 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361160 if (entry_replaced_by_post_commit_error_) {
1161 if (is_same_document_navigation) {
1162 // Same document navigations should not be possible on error pages and
1163 // would leave the controller in a weird state. Kill the renderer if
1164 // that happens.
1165 bad_message::ReceivedBadMessage(
1166 rfh->GetProcess(), bad_message::NC_SAME_DOCUMENT_POST_COMMIT_ERROR);
1167 }
1168 // Any commit while a post-commit error page is showing should put the
1169 // original entry back, replacing the error page's entry. This includes
1170 // reloads, where the original entry was used as the pending entry and
1171 // should now be at the correct index at commit time.
1172 entries_[last_committed_entry_index_] =
1173 std::move(entry_replaced_by_post_commit_error_);
1174 }
Fergal Daly8e33cf62020-12-12 01:06:071175 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551176 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431177 if (pending_entry_ &&
1178 pending_entry_->GetIsOverridingUserAgent() !=
1179 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1180 overriding_user_agent_changed = true;
[email protected]0e8db942008-09-24 21:21:481181 } else {
Gang Wu325f03f42021-02-25 20:00:461182 // GetLastCommittedEntry() is null, so this is the first entry.
Fergal Daly8e33cf62020-12-12 01:06:071183 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481184 details->previous_entry_index = -1;
Gang Wu325f03f42021-02-25 20:00:461185 if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) {
1186 // Default setting is NOT override the user agent, so overriding the user
1187 // agent in first entry should be considered as user agent changed as
1188 // well.
1189 overriding_user_agent_changed = true;
1190 }
[email protected]0e8db942008-09-24 21:21:481191 }
[email protected]ecd9d8702008-08-28 22:10:171192
Alexander Timind2f2e4f22019-04-02 20:04:531193 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1194 // implementing back-forward cache.
1195
1196 // Create a new metrics object or reuse the previous one depending on whether
1197 // it's a main frame navigation or not.
1198 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics =
1199 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1200 GetLastCommittedEntry(), !rfh->GetParent(),
1201 params.document_sequence_number);
1202 // Notify the last active entry that we have navigated away.
1203 if (!rfh->GetParent() && !is_same_document_navigation) {
1204 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1205 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Hajime Hoshic7606502021-04-14 02:42:161206 metrics->MainFrameDidNavigateAwayFromDocument(rfh, navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531207 }
1208 }
1209 }
1210
fdegans9caf66a2015-07-30 21:10:421211 // If there is a pending entry at this point, it should have a SiteInstance,
1212 // except for restored entries.
jam48cea9082017-02-15 06:13:291213 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481214 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081215 pending_entry_->IsRestored());
1216 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151217 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291218 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481219 }
[email protected]e9ba4472008-09-14 15:42:431220
Nasko Oskovaee2f862018-06-15 00:05:521221 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1222 // has changed, this must be treated as a new navigation with replacement.
1223 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491224 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521225 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101226 pending_entry_->GetUniqueID() ==
1227 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521228 pending_entry_->site_instance() &&
1229 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1230 DCHECK_NE(-1, pending_entry_index_);
1231 // TODO(nasko,creis): Instead of setting this value here, set
1232 // should_replace_current_entry on the parameters we send to the
1233 // renderer process as part of CommitNavigation. The renderer should
1234 // in turn send it back here as part of |params| and it can be just
1235 // enforced and renderer process terminated on mismatch.
1236 details->did_replace_entry = true;
1237 } else {
1238 // The renderer tells us whether the navigation replaces the current entry.
1239 details->did_replace_entry = params.should_replace_current_entry;
1240 }
[email protected]bcd904482012-02-01 01:54:221241
[email protected]e9ba4472008-09-14 15:42:431242 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amnif6950d552020-11-24 03:26:101243 details->type = ClassifyNavigation(rfh, params, navigation_request);
[email protected]4bf3522c2010-08-19 21:00:201244
eugenebutee08663a2017-04-27 17:43:121245 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441246 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121247
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071248 details->is_prerender_activation =
1249 navigation_request->IsPrerenderedPageActivation();
1250
Peter Boströmd7592132019-01-30 04:50:311251 // Make sure we do not discard the pending entry for a different ongoing
1252 // navigation when a same document commit comes in unexpectedly from the
1253 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1254 // other navigation types. See https://crbug.com/900036.
1255 // TODO(crbug.com/926009): Handle history.pushState() as well.
1256 bool keep_pending_entry = is_same_document_navigation &&
Charlie Reisc0f17d2d2021-01-12 18:52:491257 details->type == NAVIGATION_TYPE_EXISTING_ENTRY &&
Peter Boströmd7592132019-01-30 04:50:311258 pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451259 !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311260
[email protected]0e8db942008-09-24 21:21:481261 switch (details->type) {
Charlie Reisc0f17d2d2021-01-12 18:52:491262 case NAVIGATION_TYPE_NEW_ENTRY:
1263 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051264 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451265 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431266 break;
Charlie Reisc0f17d2d2021-01-12 18:52:491267 case NAVIGATION_TYPE_EXISTING_ENTRY:
1268 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1269 was_restored, navigation_request,
1270 keep_pending_entry);
[email protected]e9ba4472008-09-14 15:42:431271 break;
[email protected]8ff00d72012-10-23 19:12:211272 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471273 RendererDidNavigateNewSubframe(
1274 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451275 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431276 break;
[email protected]8ff00d72012-10-23 19:12:211277 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391278 if (!RendererDidNavigateAutoSubframe(
1279 rfh, params, details->is_same_document, navigation_request)) {
creisce0ef3572017-01-26 17:53:081280 // We don't send a notification about auto-subframe PageState during
1281 // UpdateStateForFrame, since it looks like nothing has changed. Send
1282 // it here at commit time instead.
1283 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431284 return false;
creis59d5a47cb2016-08-24 23:57:191285 }
[email protected]e9ba4472008-09-14 15:42:431286 break;
[email protected]8ff00d72012-10-23 19:12:211287 case NAVIGATION_TYPE_NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:491288 // If a pending navigation was in progress, this canceled it. We should
1289 // discard it and make sure it is removed from the URL bar. After that,
1290 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:431291 // the caller that nothing has happened.
arthursonzogni69a6a1b2019-09-17 09:23:001292 if (pending_entry_)
[email protected]20d1c992011-04-12 21:17:491293 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431294 return false;
Aran Gilman37d11632019-10-08 23:07:151295 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431296 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151297 break;
[email protected]765b35502008-08-21 00:51:201298 }
1299
[email protected]688aa65c62012-09-28 04:32:221300 // At this point, we know that the navigation has just completed, so
1301 // record the time.
1302 //
1303 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261304 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331305 base::Time timestamp =
1306 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1307 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131308 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221309
Peter Boströmd7592132019-01-30 04:50:311310 // If we aren't keeping the pending entry, there shouldn't be one at this
1311 // point. Clear it again in case any error cases above forgot to do so.
1312 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1313 // been cleared instead of protecting against it.
1314 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001315 DiscardNonCommittedEntries();
[email protected]f233e4232013-02-23 00:55:141316
[email protected]e9ba4472008-09-14 15:42:431317 // All committed entries should have nonempty content state so WebKit doesn't
1318 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471319 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321320 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221321 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441322 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531323 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1324 // implementing back-forward cache.
1325 if (!active_entry->back_forward_cache_metrics()) {
1326 active_entry->set_back_forward_cache_metrics(
1327 std::move(back_forward_cache_metrics));
1328 }
1329 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
Hajime Hoshi446206e2019-10-18 18:36:251330 navigation_request,
1331 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
naskoc7533512016-05-06 17:01:121332
Charles Reisc0507202017-09-21 00:40:021333 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1334 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1335 // A mismatch can occur if the renderer lies or due to a unique name collision
1336 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121337 FrameNavigationEntry* frame_entry =
1338 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021339 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1340 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031341 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081342 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1343 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031344 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201345
1346 // Remember the bindings the renderer process has at this point, so that
1347 // we do not grant this entry additional bindings if we come back to it.
1348 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301349 }
[email protected]132e281a2012-07-31 18:32:441350
[email protected]97d8f0d2013-10-29 16:49:211351 // Once it is committed, we no longer need to track several pieces of state on
1352 // the entry.
naskoc7533512016-05-06 17:01:121353 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131354
[email protected]49bd30e62011-03-22 20:12:591355 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221356 // TODO(creis): This check won't pass for subframes until we create entries
1357 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001358 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421359 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591360
[email protected]e9ba4472008-09-14 15:42:431361 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001362 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001363 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311364 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531365
arthursonzogni7ddc6542021-04-09 09:16:501366 active_entry->SetIsOverridingUserAgent(
1367 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031368
[email protected]93f230e02011-06-01 14:40:001369 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291370
John Abd-El-Malek380d3c5922017-09-08 00:20:311371 if (active_entry->GetURL().SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121372 !navigation_request->DidEncounterError()) {
John Abd-El-Malek969cdd94e2017-07-10 22:18:161373 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus",
1374 !!active_entry->GetSSL().certificate);
1375 }
1376
aelias100c9192017-01-13 00:01:431377 if (overriding_user_agent_changed)
1378 delegate_->UpdateOverridingUserAgent();
1379
creis03b48002015-11-04 00:54:561380 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1381 // one knows the latest NavigationEntry it is showing (whether it has
1382 // committed anything in this navigation or not). This allows things like
1383 // state and title updates from RenderFrames to apply to the latest relevant
1384 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381385 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001386 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381387 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
[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
Charles Reis1378111f2017-11-08 21:44:061452 // This is history.replaceState() or history.reload().
Nasko Oskov332593c2018-08-16 17:21:341453 // TODO(nasko): With error page isolation, reloading an existing session
1454 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491455 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341456 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511457 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491458 "nav entry 0, last committed, existing entry");
1459 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381460 }
1461
Rakina Zata Amnif6950d552020-11-24 03:26:101462 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521463 // If the SiteInstance of the |pending_entry_| does not match the
1464 // SiteInstance that got committed, treat this as a new navigation with
1465 // replacement. This can happen if back/forward/reload encounters a server
1466 // redirect to a different site or an isolated error page gets successfully
1467 // reloaded into a different SiteInstance.
1468 if (pending_entry_->site_instance() &&
1469 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491470 trace_return.set_return_reason("pending matching nav entry, new entry");
1471 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521472 }
creis77c9aa32015-09-25 19:59:421473
Nasko Oskovaee2f862018-06-15 00:05:521474 if (pending_entry_index_ == -1) {
1475 // In this case, we have a pending entry for a load of a new URL but Blink
1476 // didn't do a new navigation (params.did_create_new_entry). First check
1477 // to make sure Blink didn't treat a new cross-process navigation as
1478 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161479 // we must treat it as NEW rather than the converted reload case below,
1480 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521481 if (!GetLastCommittedEntry() ||
1482 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161483 trace_return.set_return_reason("new pending, new entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491484 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521485 }
1486
1487 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161488 // We will create a pending entry, but NavigateWithoutEntry will convert
1489 // it to a reload since it's the same page and not create a new entry for
1490 // it. (The user doesn't want to have a new back/forward entry when they
1491 // do this.) Therefore we want to just ignore the pending entry and go
1492 // back to where we were (the "existing entry").
1493 trace_return.set_return_reason("new pending, existing (same) entry");
1494 return NAVIGATION_TYPE_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521495 }
avi7c6f35e2015-05-08 17:52:381496 }
1497
creis26d22632017-04-21 20:23:561498 // Everything below here is assumed to be an existing entry, but if there is
1499 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511500 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491501 trace_return.set_return_reason("no last committed, new entry");
1502 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511503 }
creis26d22632017-04-21 20:23:561504
avi7c6f35e2015-05-08 17:52:381505 if (params.intended_as_new_entry) {
1506 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491507 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1508 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161509 trace_return.set_return_reason("intended as new entry, existing entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491510 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381511 }
1512
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121513 if (navigation_request->DidEncounterError() &&
1514 failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101515 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381516 // If the renderer was going to a new pending entry that got cleared because
1517 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491518 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381519 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511520 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491521 "unreachable, matching pending, existing entry");
1522 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381523 }
1524
Charlie Reisc0f17d2d2021-01-12 18:52:491525 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101526 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511527 trace_return.traced_value()->SetInteger("existing_entry_index",
1528 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381529 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441530 // The renderer has committed a navigation to an entry that no longer
1531 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491532 trace_return.set_return_reason("existing entry -1, new entry");
1533 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381534 }
1535
avi7c6f35e2015-05-08 17:52:381536 // Since we weeded out "new" navigations above, we know this is an existing
1537 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491538 trace_return.set_return_reason("default return, existing entry");
1539 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381540}
1541
Charlie Reisc0f17d2d2021-01-12 18:52:491542void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321543 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071544 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361545 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441546 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471547 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451548 NavigationRequest* request) {
dcheng9bfa5162016-04-09 01:00:571549 std::unique_ptr<NavigationEntryImpl> new_entry;
creisf49dfc92016-07-26 17:05:181550 bool update_virtual_url = false;
1551
Anton Bikineevf62d1bf2021-05-15 17:56:071552 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451553 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061554
creisf49dfc92016-07-26 17:05:181555 // First check if this is an in-page navigation. If so, clone the current
1556 // entry instead of looking at the pending entry, because the pending entry
1557 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361558 if (is_same_document && GetLastCommittedEntry()) {
Patrick Monette50e8bd82019-06-13 22:40:451559 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481560 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
creisf49dfc92016-07-26 17:05:181561 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121562 params.url, GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091563 Referrer(*params.referrer), initiator_origin,
1564 request->GetRedirectChain(), params.page_state, params.method,
1565 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391566 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481567 request->GetSubresourceWebBundleNavigationInfo(),
Antonio Sartori78a749f2020-11-30 12:03:391568 // We will set the document policies later in this function.
Titouan Rigoudy6ec70402021-02-02 15:42:191569 nullptr /* policy_container_policies */);
Charles Reisf44482022017-10-13 21:15:031570
creisf49dfc92016-07-26 17:05:181571 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Carlos Caballero40b0efd2021-01-26 11:55:001572 frame_entry, true, rfh->frame_tree_node(), frame_tree_.root());
jama78746e2017-02-22 17:21:571573 if (new_entry->GetURL().GetOrigin() != params.url.GetOrigin()) {
1574 // TODO(jam): we had one report of this with a URL that was redirecting to
1575 // only tildes. Until we understand that better, don't copy the cert in
1576 // this case.
1577 new_entry->GetSSL() = SSLStatus();
John Abd-El-Malek9cd697e2017-07-12 21:53:141578
John Abd-El-Malek380d3c5922017-09-08 00:20:311579 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121580 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141581 UMA_HISTOGRAM_BOOLEAN(
1582 "Navigation.SecureSchemeHasSSLStatus.NewPageInPageOriginMismatch",
1583 !!new_entry->GetSSL().certificate);
1584 }
jama78746e2017-02-22 17:21:571585 }
creisf49dfc92016-07-26 17:05:181586
Patrick Monette50e8bd82019-06-13 22:40:451587 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1588 // that |frame_entry| should now have a reference count of exactly 2: one
1589 // due to the local variable |frame_entry|, and another due to |new_entry|
1590 // also retaining one. This should never fail, because it's the main frame.
1591 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181592
1593 update_virtual_url = new_entry->update_virtual_url_with_url();
John Abd-El-Malek9cd697e2017-07-12 21:53:141594
John Abd-El-Malek380d3c5922017-09-08 00:20:311595 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121596 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141597 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus.NewPageInPage",
1598 !!new_entry->GetSSL().certificate);
1599 }
creisf49dfc92016-07-26 17:05:181600 }
1601
Harkiran Bolaria59290d62021-03-17 01:53:011602 // If this is an activation navigation from a prerendered page, transfer the
1603 // new entry from an entry already created and stored in the
1604 // NavigationRequest. |new_entry| will not have been set prior to this as
1605 // |is_same_document| is mutually exclusive with
1606 // |IsPrerenderedPageActivation|.
1607 if (request->IsPrerenderedPageActivation()) {
1608 DCHECK(!is_same_document);
1609 DCHECK(!new_entry);
1610 new_entry = request->TakePrerenderNavigationEntry();
1611 DCHECK(new_entry);
1612 }
1613
Charlie Reisc0f17d2d2021-01-12 18:52:491614 // Only make a copy of the pending entry if it is appropriate for the new
1615 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451616 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041617 // 1. The SiteInstance hasn't been assigned to something else.
1618 // 2. The pending entry was intended as a new entry, rather than being a
1619 // history navigation that was interrupted by an unrelated,
1620 // renderer-initiated navigation.
1621 // TODO(csharrison): Investigate whether we can remove some of the coarser
1622 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451623 if (!new_entry && PendingEntryMatchesRequest(request) &&
1624 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341625 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431626 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351627 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431628
[email protected]f1eb87a2011-05-06 17:49:411629 update_virtual_url = new_entry->update_virtual_url_with_url();
Camille Lamy62b826012019-02-26 09:15:471630 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451631 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141632
John Abd-El-Malek380d3c5922017-09-08 00:20:311633 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121634 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141635 UMA_HISTOGRAM_BOOLEAN(
1636 "Navigation.SecureSchemeHasSSLStatus.NewPagePendingEntryMatches",
1637 !!new_entry->GetSSL().certificate);
1638 }
creisf49dfc92016-07-26 17:05:181639 }
1640
Charlie Reisc0f17d2d2021-01-12 18:52:491641 // For cross-document commits with no matching pending entry, create a new
1642 // entry.
creisf49dfc92016-07-26 17:05:181643 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061644 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071645 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1646 initiator_origin,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:581647 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451648 params.transition, request->IsRendererInitiated(),
Lukasz Anforowicz435bcb582019-07-12 20:50:061649 nullptr); // blob_url_loader_factory
[email protected]f8f93eb2012-09-25 03:06:241650
1651 // Find out whether the new entry needs to update its virtual URL on URL
1652 // change and set up the entry accordingly. This is needed to correctly
1653 // update the virtual URL when replaceState is called after a pushState.
1654 GURL url = params.url;
1655 bool needs_update = false;
[email protected]47752982014-07-29 08:01:431656 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1657 &url, browser_context_, &needs_update);
[email protected]f8f93eb2012-09-25 03:06:241658 new_entry->set_update_virtual_url_with_url(needs_update);
1659
Charlie Reisc0f17d2d2021-01-12 18:52:491660 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411661 // update the virtual URL based on the new URL. For example, this is needed
1662 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1663 // the URL.
[email protected]f8f93eb2012-09-25 03:06:241664 update_virtual_url = needs_update;
Camille Lamy62b826012019-02-26 09:15:471665 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451666 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141667
John Abd-El-Malek380d3c5922017-09-08 00:20:311668 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121669 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141670 UMA_HISTOGRAM_BOOLEAN(
1671 "Navigation.SecureSchemeHasSSLStatus.NewPageNoMatchingEntry",
1672 !!new_entry->GetSSL().certificate);
1673 }
[email protected]e9ba4472008-09-14 15:42:431674 }
1675
Harkiran Bolaria59290d62021-03-17 01:53:011676 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
1677 // for prerendered contents, if any. This is because prerendering/activation
1678 // technically won't be creating a new document. Unlike BFCache, prerendering
1679 // creates a new NavigationEntry rather than using an existing one.
1680 if (!request->IsPrerenderedPageActivation()) {
1681 // Don't use the page type from the pending entry. Some interstitial page
1682 // may have set the type to interstitial. Once we commit, however, the page
1683 // type must always be normal or error.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121684 new_entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1685 : PAGE_TYPE_NORMAL);
Harkiran Bolaria59290d62021-03-17 01:53:011686 new_entry->SetURL(params.url);
1687 if (update_virtual_url)
1688 UpdateVirtualURLToURL(new_entry.get(), params.url);
1689 new_entry->SetReferrer(Referrer(*params.referrer));
1690 new_entry->SetTransitionType(params.transition);
1691 new_entry->set_site_instance(
1692 static_cast<SiteInstanceImpl*>(rfh->GetSiteInstance()));
1693 new_entry->SetOriginalRequestURL(request->GetOriginalRequestURL());
Rakina Zata Amnib597d632020-12-01 00:56:001694
Antonio Sartori4f5373792021-05-31 10:56:471695 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
Harkiran Bolaria59290d62021-03-17 01:53:011696 new_entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
[email protected]e9ba4472008-09-14 15:42:431697
Harkiran Bolaria59290d62021-03-17 01:53:011698 // Update the FrameNavigationEntry for new main frame commits.
1699 FrameNavigationEntry* frame_entry =
1700 new_entry->GetFrameEntry(rfh->frame_tree_node());
1701 frame_entry->set_frame_unique_name(rfh->frame_tree_node()->unique_name());
1702 frame_entry->set_item_sequence_number(params.item_sequence_number);
1703 frame_entry->set_document_sequence_number(params.document_sequence_number);
1704 frame_entry->set_redirect_chain(request->GetRedirectChain());
1705 frame_entry->SetPageState(params.page_state);
1706 frame_entry->set_method(params.method);
1707 frame_entry->set_post_id(params.post_id);
1708 frame_entry->set_policy_container_policies(
1709 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1710 request));
Antonio Sartori78a749f2020-11-30 12:03:391711
Anton Bikineevf62d1bf2021-05-15 17:56:071712 if (absl::optional<url::Origin> committed_origin =
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121713 GetCommittedOriginForFrameEntry(params, request)) {
1714 if (committed_origin.has_value())
1715 frame_entry->set_committed_origin(committed_origin.value());
1716 }
Harkiran Bolaria59290d62021-03-17 01:53:011717 if (request->web_bundle_navigation_info()) {
1718 frame_entry->set_web_bundle_navigation_info(
1719 request->web_bundle_navigation_info()->Clone());
1720 }
creis8b5cd4c2015-06-19 00:11:081721
Harkiran Bolaria59290d62021-03-17 01:53:011722 // history.pushState() is classified as a navigation to a new page, but sets
1723 // is_same_document to true. In this case, we already have the title and
1724 // favicon available, so set them immediately.
1725 if (is_same_document && GetLastCommittedEntry()) {
1726 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
1727 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1728 }
[email protected]3a868f212014-08-09 10:41:191729 }
[email protected]ff64b3e2014-05-31 04:07:331730
[email protected]60d6cca2013-04-30 08:47:131731 DCHECK(!params.history_list_was_cleared || !replace_entry);
1732 // The browser requested to clear the session history when it initiated the
1733 // navigation. Now we know that the renderer has updated its state accordingly
1734 // and it is safe to also clear the browser side history.
1735 if (params.history_list_was_cleared) {
arthursonzogni69a6a1b2019-09-17 09:23:001736 DiscardNonCommittedEntries();
[email protected]60d6cca2013-04-30 08:47:131737 entries_.clear();
1738 last_committed_entry_index_ = -1;
1739 }
1740
Nasko Oskovaee2f862018-06-15 00:05:521741 // If this is a new navigation with replacement and there is a
1742 // pending_entry_ which matches the navigation reported by the renderer
1743 // process, then it should be the one replaced, so update the
1744 // last_committed_entry_index_ to use it.
1745 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101746 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521747 last_committed_entry_index_ = pending_entry_index_;
1748 }
1749
Alexander Timine3ec4192020-04-20 16:39:401750 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411751 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401752 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051753
Carlos IL42b416592019-10-07 23:10:361754 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
1755 !request->post_commit_error_page_html().empty());
[email protected]e9ba4472008-09-14 15:42:431756}
1757
Charlie Reisc0f17d2d2021-01-12 18:52:491758void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321759 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071760 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361761 bool is_same_document,
jam48cea9082017-02-15 06:13:291762 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451763 NavigationRequest* request,
Peter Boströmd7592132019-01-30 04:50:311764 bool keep_pending_entry) {
creis26d22632017-04-21 20:23:561765 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1766 << "that a last committed entry exists.";
1767
[email protected]e9ba4472008-09-14 15:42:431768 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001769 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431770
Charlie Reis7e2cb6d2021-01-26 01:27:161771 NavigationEntryImpl* entry = nullptr;
avicbdc4c12015-07-01 16:07:111772 if (params.intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:161773 // We're guaranteed to have a last committed entry if intended_as_new_entry
1774 // is true.
avicbdc4c12015-07-01 16:07:111775 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:161776 DCHECK(entry);
1777
1778 // If the NavigationRequest matches a new pending entry and is classified as
1779 // EXISTING_ENTRY, then it is a navigation to the same URL that was
1780 // converted to a reload, such as a user pressing enter in the omnibox.
1781 if (pending_entry_ && pending_entry_index_ == -1 &&
1782 pending_entry_->GetUniqueID() ==
1783 request->commit_params().nav_entry_id) {
1784 // Note: The pending entry will usually have a real ReloadType here, but
1785 // it can still be ReloadType::NONE in cases that
1786 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
1787
1788 // If we classified this correctly, the SiteInstance should not have
1789 // changed.
1790 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
1791
1792 // For converted reloads, we assign the entry's unique ID to be that of
1793 // the new one. Since this is always the result of a user action, we want
1794 // to dismiss infobars, etc. like a regular user-initiated navigation.
1795 entry->set_unique_id(pending_entry_->GetUniqueID());
1796
1797 // The extra headers may have changed due to reloading with different
1798 // headers.
1799 entry->set_extra_headers(pending_entry_->extra_headers());
1800 }
1801 // Otherwise, this was intended as a new entry but the pending entry was
1802 // lost in the meantime and no new entry was created. We are stuck at the
1803 // last committed entry.
1804
1805 // Even if this is a converted reload from pressing enter in the omnibox,
1806 // the server could redirect, requiring an update to the SSL status. If this
1807 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451808 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:161809 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:471810 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451811 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:161812 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141813
Charlie Reis7e2cb6d2021-01-26 01:27:161814 if (params.url.SchemeIs(url::kHttpsScheme) &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121815 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141816 bool has_cert = !!entry->GetSSL().certificate;
1817 if (is_same_document) {
1818 UMA_HISTOGRAM_BOOLEAN(
1819 "Navigation.SecureSchemeHasSSLStatus."
1820 "ExistingPageSameDocumentIntendedAsNew",
1821 has_cert);
1822 } else {
1823 UMA_HISTOGRAM_BOOLEAN(
1824 "Navigation.SecureSchemeHasSSLStatus."
1825 "ExistingPageDifferentDocumentIntendedAsNew",
1826 has_cert);
1827 }
1828 }
Rakina Zata Amnif6950d552020-11-24 03:26:101829 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:111830 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:101831 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b90ce2016-09-01 16:58:161832
eugenebut604866f2017-05-10 21:35:361833 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451834 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361835 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1836 // this was a restored same document navigation entry, then it won't have
1837 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1838 // navigated it.
jam48cea9082017-02-15 06:13:291839 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
1840 if (entry->GetURL().GetOrigin() == last_entry->GetURL().GetOrigin() &&
1841 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1842 was_restored) {
1843 entry->GetSSL() = last_entry->GetSSL();
1844 }
1845 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451846 // In rapid back/forward navigations |request| sometimes won't have a cert
1847 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191848 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451849 if (request->GetSSLInfo().has_value() &&
1850 request->GetSSLInfo()->is_valid()) {
1851 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
1852 } else if (entry->GetURL().GetOrigin() != request->GetURL().GetOrigin()) {
John Abd-El-Malek3f247082017-12-07 19:02:191853 entry->GetSSL() = SSLStatus();
1854 }
jam48cea9082017-02-15 06:13:291855 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141856
John Abd-El-Malek380d3c5922017-09-08 00:20:311857 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121858 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141859 bool has_cert = !!entry->GetSSL().certificate;
1860 if (is_same_document && was_restored) {
1861 UMA_HISTOGRAM_BOOLEAN(
1862 "Navigation.SecureSchemeHasSSLStatus."
1863 "ExistingPageSameDocumentRestoredBrowserInitiated",
1864 has_cert);
1865 } else if (is_same_document && !was_restored) {
1866 UMA_HISTOGRAM_BOOLEAN(
1867 "Navigation.SecureSchemeHasSSLStatus."
1868 "ExistingPageSameDocumentBrowserInitiated",
1869 has_cert);
1870 } else if (!is_same_document && was_restored) {
1871 UMA_HISTOGRAM_BOOLEAN(
1872 "Navigation.SecureSchemeHasSSLStatus."
1873 "ExistingPageRestoredBrowserInitiated",
1874 has_cert);
1875 } else {
1876 UMA_HISTOGRAM_BOOLEAN(
1877 "Navigation.SecureSchemeHasSSLStatus.ExistingPageBrowserInitiated",
1878 has_cert);
1879 }
1880 }
avicbdc4c12015-07-01 16:07:111881 } else {
1882 // This is renderer-initiated. The only kinds of renderer-initated
Charlie Reisc0f17d2d2021-01-12 18:52:491883 // navigations that are EXISTING_ENTRY are reloads and history.replaceState,
avicbdc4c12015-07-01 16:07:111884 // which land us at the last committed entry.
1885 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101886
Mikel Astizba9cf2fd2017-12-17 10:38:101887 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1888 // to avoid misleading interpretations (e.g. URLs paired with
1889 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1890 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1891 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1892
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571893 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:101894
eugenebut604866f2017-05-10 21:35:361895 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451896 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361897 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471898 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451899 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141900
John Abd-El-Malek380d3c5922017-09-08 00:20:311901 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121902 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141903 bool has_cert = !!entry->GetSSL().certificate;
1904 if (is_same_document) {
1905 UMA_HISTOGRAM_BOOLEAN(
1906 "Navigation.SecureSchemeHasSSLStatus."
1907 "ExistingPageSameDocumentRendererInitiated",
1908 has_cert);
1909 } else {
1910 UMA_HISTOGRAM_BOOLEAN(
1911 "Navigation.SecureSchemeHasSSLStatus."
1912 "ExistingPageDifferentDocumentRendererInitiated",
1913 has_cert);
1914 }
1915 }
avicbdc4c12015-07-01 16:07:111916 }
1917 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:431918
[email protected]5ccd4dc2012-10-24 02:28:141919 // The URL may have changed due to redirects.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121920 entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1921 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041922 entry->SetURL(params.url);
arthursonzogni73fe3212020-11-17 13:24:071923 entry->SetReferrer(Referrer(*params.referrer));
[email protected]38178a42009-12-17 18:58:321924 if (entry->update_virtual_url_with_url())
1925 UpdateVirtualURLToURL(entry, params.url);
[email protected]5ccd4dc2012-10-24 02:28:141926
jam015ba062017-01-06 21:17:001927 // The site instance will normally be the same except
1928 // 1) session restore, when no site instance will be assigned or
1929 // 2) redirect, when the site instance is reset.
Lei Zhang96031532019-10-10 19:05:471930 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
naskoaf182192016-08-11 02:12:011931 entry->site_instance() == rfh->GetSiteInstance());
clamy432acb22016-04-15 19:41:431932
naskoaf182192016-08-11 02:12:011933 // Update the existing FrameNavigationEntry to ensure all of its members
1934 // reflect the parameters coming from the renderer process.
Anton Bikineevf62d1bf2021-05-15 17:56:071935 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451936 request->common_params().initiator_origin;
naskoaf182192016-08-11 02:12:011937 entry->AddOrUpdateFrameEntry(
1938 rfh->frame_tree_node(), params.item_sequence_number,
1939 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121940 params.url, GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091941 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071942 params.page_state, params.method, params.post_id,
1943 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031944 request->web_bundle_navigation_info()
1945 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391946 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481947 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191948 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1949 request));
creis22a7b4c2016-04-28 07:20:301950
[email protected]5ccd4dc2012-10-24 02:28:141951 // The redirected to page should not inherit the favicon from the previous
1952 // page.
eugenebut604866f2017-05-10 21:35:361953 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:481954 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:141955
Peter Boströmd7592132019-01-30 04:50:311956 // We should also usually discard the pending entry if it corresponds to a
1957 // different navigation, since that one is now likely canceled. In rare
1958 // cases, we leave the pending entry for another navigation in place when we
1959 // know it is still ongoing, to avoid a flicker in the omnibox (see
1960 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:431961 //
1962 // Note that we need to use the "internal" version since we don't want to
1963 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:311964 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001965 DiscardNonCommittedEntries();
[email protected]40bcc302009-03-02 20:50:391966
Carlos IL4dea8902020-05-26 15:14:291967 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:111968 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:431969}
1970
[email protected]d202a7c2012-01-04 07:53:471971void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:321972 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071973 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361974 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:471975 bool replace_entry,
1976 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451977 NavigationRequest* request) {
avi25f5f9e2015-07-17 20:08:261978 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1979 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
[email protected]09b8f82f2009-06-16 20:22:111980
[email protected]e9ba4472008-09-14 15:42:431981 // Manual subframe navigations just get the current entry cloned so the user
1982 // can go back or forward to it. The actual subframe information will be
1983 // stored in the page state for each of those entries. This happens out of
1984 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:091985 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1986 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:381987
Mikel Astizba9cf2fd2017-12-17 10:38:101988 // The DCHECK below documents the fact that we don't know of any situation
1989 // where |replace_entry| is true for subframe navigations. This simplifies
1990 // reasoning about the replacement struct for subframes (see
1991 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
1992 DCHECK(!replace_entry);
1993
Patrick Monette50e8bd82019-06-13 22:40:451994 // This FrameNavigationEntry might not end up being used in the
1995 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Anton Bikineevf62d1bf2021-05-15 17:56:071996 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451997 request->common_params().initiator_origin;
Patrick Monette50e8bd82019-06-13 22:40:451998 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481999 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
creisce0ef3572017-01-26 17:53:082000 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:122001 params.url, GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:092002 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072003 params.page_state, params.method, params.post_id,
2004 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032005 request->web_bundle_navigation_info()
2006 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392007 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482008 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:192009 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
2010 request));
Charles Reisf44482022017-10-13 21:15:032011
creisce0ef3572017-01-26 17:53:082012 std::unique_ptr<NavigationEntryImpl> new_entry =
2013 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:452014 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:002015 frame_tree_.root());
creise062d542015-08-25 02:01:552016
Alexander Timine3ec4192020-04-20 16:39:402017 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:412018 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:402019 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:472020
creisce0ef3572017-01-26 17:53:082021 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:452022 // to replace, which can happen due to a unique name change. See
2023 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
2024 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:382025
Carlos IL42b416592019-10-07 23:10:362026 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false);
[email protected]e9ba4472008-09-14 15:42:432027}
2028
[email protected]d202a7c2012-01-04 07:53:472029bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:322030 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072031 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392032 bool is_same_document,
Mohamed Abdelhalim833de902019-09-16 17:41:452033 NavigationRequest* request) {
avi9f07a0c2015-02-18 22:51:292034 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2035 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2036
[email protected]e9ba4472008-09-14 15:42:432037 // We're guaranteed to have a previously committed entry, and we now need to
2038 // handle navigation inside of a subframe in it without creating a new entry.
2039 DCHECK(GetLastCommittedEntry());
2040
creis913c63ce2016-07-16 19:52:522041 // For newly created subframes, we don't need to send a commit notification.
2042 // This is only necessary for history navigations in subframes.
2043 bool send_commit_notification = false;
2044
Rakina Zata Amnif6950d552020-11-24 03:26:102045 // If |nav_entry_id| is non-zero and matches an existing entry, this
2046 // is a history navigation. Update the last committed index accordingly. If
2047 // we don't recognize the |nav_entry_id|, it might be a recently
2048 // pruned entry. We'll handle it below.
2049 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2050 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472051 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062052 // Make sure that a subframe commit isn't changing the main frame's
2053 // origin. Otherwise the renderer process may be confused, leading to a
2054 // URL spoof. We can't check the path since that may change
2055 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572056 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2057 // about:blank, file, and unique origins are more subtle to get right.
Charlie Reis95fbca442021-05-21 21:38:242058 // We should use checks similar to RenderFrameHostImpl's
2059 // CanCommitUrlAndOrigin on the main frame during subframe commits.
2060 // See https://crbug.com/1209092.
creis37988b92016-06-10 18:03:572061 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2062 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2063 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2064 dest_top_url.SchemeIsHTTPOrHTTPS() &&
2065 current_top_url.GetOrigin() != dest_top_url.GetOrigin()) {
creisfb6eeb62016-05-10 19:01:512066 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2067 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062068 }
creis3cdc3b02015-05-29 23:00:472069
creis913c63ce2016-07-16 19:52:522070 // We only need to discard the pending entry in this history navigation
2071 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062072 last_committed_entry_index_ = entry_index;
arthursonzogni69a6a1b2019-09-17 09:23:002073 DiscardNonCommittedEntries();
creis913c63ce2016-07-16 19:52:522074
2075 // History navigations should send a commit notification.
2076 send_commit_notification = true;
avi98405c22015-05-21 20:47:062077 }
[email protected]e9ba4472008-09-14 15:42:432078 }
[email protected]f233e4232013-02-23 00:55:142079
creisce0ef3572017-01-26 17:53:082080 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2081 // it may be a "history auto" case where we update an existing one.
2082 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Anton Bikineevf62d1bf2021-05-15 17:56:072083 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452084 request->common_params().initiator_origin;
creisce0ef3572017-01-26 17:53:082085 last_committed->AddOrUpdateFrameEntry(
2086 rfh->frame_tree_node(), params.item_sequence_number,
2087 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:122088 params.url, GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:092089 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072090 params.page_state, params.method, params.post_id,
2091 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032092 request->web_bundle_navigation_info()
2093 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392094 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482095 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:192096 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
2097 request));
creis625a0c7d2015-03-24 23:17:122098
creis913c63ce2016-07-16 19:52:522099 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432100}
2101
[email protected]d202a7c2012-01-04 07:53:472102int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232103 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032104 for (size_t i = 0; i < entries_.size(); ++i) {
2105 if (entries_[i].get() == entry)
2106 return i;
2107 }
2108 return -1;
[email protected]765b35502008-08-21 00:51:202109}
2110
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572111void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242112 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572113 NavigationControllerImpl* source =
2114 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572115 // Verify that we look new.
Lei Zhang96031532019-10-10 19:05:472116 DCHECK_EQ(0, GetEntryCount());
2117 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:572118
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572119 if (source->GetEntryCount() == 0)
[email protected]ce3fa3c2009-04-20 19:55:572120 return; // Nothing new to do.
2121
Francois Dorayeaace782017-06-21 16:37:242122 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442123 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572124 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572125
Fergal Dalya1d569972021-03-16 03:24:532126 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302127 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532128 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2129 session_storage_namespace_map_[it.first] = source_namespace->Clone();
Aaron Colwellb731a0ae2021-03-19 19:14:472130 OnStoragePartitionIdAdded(it.first);
[email protected]fdac6ade2013-07-20 01:06:302131 }
[email protected]4e6419c2010-01-15 04:50:342132
Lukasz Anforowicz0de0f452020-12-02 19:57:152133 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572134}
2135
Aran Gilman37d11632019-10-08 23:07:152136void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2137 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162138 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012139 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162140
[email protected]d202a7c2012-01-04 07:53:472141 NavigationControllerImpl* source =
2142 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252143
avi2b177592014-12-10 02:08:022144 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012145 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252146
[email protected]474f8512013-05-31 22:31:162147 // We now have one entry, possibly with a new pending entry. Ensure that
2148 // adding the entries from source won't put us over the limit.
2149 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572150 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572151 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252152
Carlos IL4dea8902020-05-26 15:14:292153 // Insert the entries from source. Ignore any pending entry, since it has not
2154 // committed in source.
[email protected]474f8512013-05-31 22:31:162155 int max_source_index = source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:252156 if (max_source_index == -1)
[email protected]a26023822011-12-29 00:23:552157 max_source_index = source->GetEntryCount();
[email protected]e1cd5452010-08-26 18:03:252158 else
2159 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572160
2161 // Ignore the source's current entry if merging with replacement.
2162 // TODO(davidben): This should preserve entries forward of the current
2163 // too. http://crbug.com/317872
2164 if (replace_entry && max_source_index > 0)
2165 max_source_index--;
2166
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572167 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252168
2169 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552170 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142171
Carlos Caballeroede6f8c2021-01-28 11:01:502172 SetHistoryOffsetAndLength(last_committed_entry_index_, GetEntryCount());
[email protected]e1cd5452010-08-26 18:03:252173}
2174
[email protected]79368982013-11-13 01:11:012175bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162176 // If there is no last committed entry, we cannot prune. Even if there is a
2177 // pending entry, it may not commit, leaving this WebContents blank, despite
2178 // possibly giving it new entries via CopyStateFromAndPrune.
2179 if (last_committed_entry_index_ == -1)
2180 return false;
[email protected]9350602e2013-02-26 23:27:442181
[email protected]474f8512013-05-31 22:31:162182 // We cannot prune if there is a pending entry at an existing entry index.
2183 // It may not commit, so we have to keep the last committed entry, and thus
2184 // there is no sensible place to keep the pending entry. It is ok to have
2185 // a new pending entry, which can optionally commit as a new navigation.
2186 if (pending_entry_index_ != -1)
2187 return false;
2188
[email protected]474f8512013-05-31 22:31:162189 return true;
2190}
2191
[email protected]79368982013-11-13 01:11:012192void NavigationControllerImpl::PruneAllButLastCommitted() {
2193 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162194
avi2b177592014-12-10 02:08:022195 DCHECK_EQ(0, last_committed_entry_index_);
2196 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442197
Carlos Caballeroede6f8c2021-01-28 11:01:502198 SetHistoryOffsetAndLength(last_committed_entry_index_, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442199}
2200
[email protected]79368982013-11-13 01:11:012201void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162202 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012203 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262204
[email protected]474f8512013-05-31 22:31:162205 // Erase all entries but the last committed entry. There may still be a
2206 // new pending entry after this.
2207 entries_.erase(entries_.begin(),
2208 entries_.begin() + last_committed_entry_index_);
2209 entries_.erase(entries_.begin() + 1, entries_.end());
2210 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262211}
2212
Christian Dullweber1af31e62018-02-22 11:49:482213void NavigationControllerImpl::DeleteNavigationEntries(
2214 const DeletionPredicate& deletionPredicate) {
2215 // It is up to callers to check the invariants before calling this.
2216 CHECK(CanPruneAllButLastCommitted());
2217 std::vector<int> delete_indices;
2218 for (size_t i = 0; i < entries_.size(); i++) {
2219 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572220 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482221 delete_indices.push_back(i);
2222 }
2223 }
2224 if (delete_indices.empty())
2225 return;
2226
2227 if (delete_indices.size() == GetEntryCount() - 1U) {
2228 PruneAllButLastCommitted();
2229 } else {
2230 // Do the deletion in reverse to preserve indices.
2231 for (auto it = delete_indices.rbegin(); it != delete_indices.rend(); ++it) {
2232 RemoveEntryAtIndex(*it);
2233 }
Carlos Caballeroede6f8c2021-01-28 11:01:502234 SetHistoryOffsetAndLength(last_committed_entry_index_, GetEntryCount());
Christian Dullweber1af31e62018-02-22 11:49:482235 }
2236 delegate()->NotifyNavigationEntriesDeleted();
2237}
2238
Shivani Sharma883f5f32019-02-12 18:20:012239bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2240 auto* entry = GetEntryAtIndex(index);
2241 return entry && entry->should_skip_on_back_forward_ui();
2242}
2243
Carlos Caballero35ce710c2019-09-19 10:59:452244BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2245 return back_forward_cache_;
2246}
2247
clamy987a3752018-05-03 17:36:262248void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2249 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2250 // progress, since this will cause a use-after-free. (We only allow this
2251 // when the tab is being destroyed for shutdown, since it won't return to
2252 // NavigateToEntry in that case.) http://crbug.com/347742.
2253 CHECK(!in_navigate_to_pending_entry_ || delegate_->IsBeingDestroyed());
2254
2255 if (was_failure && pending_entry_) {
2256 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2257 } else {
2258 failed_pending_entry_id_ = 0;
2259 }
2260
2261 if (pending_entry_) {
2262 if (pending_entry_index_ == -1)
2263 delete pending_entry_;
2264 pending_entry_index_ = -1;
2265 pending_entry_ = nullptr;
2266 }
arthursonzogni66f711c2019-10-08 14:40:362267
2268 // Ensure any refs to the current pending entry are ignored if they get
2269 // deleted, by clearing the set of known refs. All future pending entries will
2270 // only be affected by new refs.
2271 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262272}
2273
2274void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2275 if (pending_entry_)
2276 pending_entry_->set_ssl_error(error);
2277}
2278
Camille Lamy5193caa2018-10-12 11:59:422279#if defined(OS_ANDROID)
2280// static
2281bool NavigationControllerImpl::ValidateDataURLAsString(
2282 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2283 if (!data_url_as_string)
2284 return false;
2285
2286 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2287 return false;
2288
2289 // The number of characters that is enough for validating a data: URI.
2290 // From the GURL's POV, the only important part here is scheme, it doesn't
2291 // check the actual content. Thus we can take only the prefix of the url, to
2292 // avoid unneeded copying of a potentially long string.
2293 const size_t kDataUriPrefixMaxLen = 64;
2294 GURL data_url(
2295 std::string(data_url_as_string->front_as<char>(),
2296 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2297 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2298 return false;
2299
2300 return true;
2301}
2302#endif
2303
Shivani Sharma194877032019-03-07 17:52:472304void NavigationControllerImpl::NotifyUserActivation() {
2305 // When a user activation occurs, ensure that all adjacent entries for the
2306 // same document clear their skippable bit, so that the history manipulation
2307 // intervention does not apply to them.
Shivani Sharmaffb32b82019-04-09 16:58:472308 // TODO(crbug.com/949279) in case it becomes necessary for resetting based on
2309 // which frame created an entry and which frame has the user gesture.
Shivani Sharma194877032019-03-07 17:52:472310 auto* last_committed_entry = GetLastCommittedEntry();
2311 if (!last_committed_entry)
2312 return;
Shivani Sharma194877032019-03-07 17:52:472313
Shivani Sharmac4cc8922019-04-18 03:11:172314 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472315}
2316
clamy987a3752018-05-03 17:36:262317bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2318 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172319 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262320 NavigationEntryImpl* entry =
2321 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2322 if (!entry)
2323 return false;
2324
2325 FrameNavigationEntry* frame_entry =
2326 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2327 if (!frame_entry)
2328 return false;
2329
Camille Lamy5193caa2018-10-12 11:59:422330 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572331 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232332 ReloadType::NONE, false /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422333 true /* is_history_navigation_in_new_child */);
clamyea99ea12018-05-28 13:54:232334
2335 if (!request)
2336 return false;
2337
arthursonzognif046d4a2019-12-12 19:08:102338 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412339
Lukasz Anforowicz9ee83c272020-12-01 20:14:052340 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2341 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232342
2343 return true;
clamy987a3752018-05-03 17:36:262344}
2345
Tsuyoshi Horo52fd08e2020-07-07 07:03:452346bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2347 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2348 if (!entry)
2349 return false;
2350 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2351 if (!frame_entry)
2352 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142353 ReloadType reload_type = ReloadType::NORMAL;
2354 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452355 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142356 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452357 false /* is_same_document_history_load */,
2358 false /* is_history_navigation_in_new_child */);
2359 if (!request)
2360 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052361 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452362 return true;
2363}
2364
Dave Tapuska8bfd84c2019-03-26 20:47:162365void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2366 int offset,
2367 int sandbox_frame_tree_node_id) {
2368 if (!CanGoToOffset(offset))
2369 return;
2370 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id);
2371}
2372
clamy987a3752018-05-03 17:36:262373void NavigationControllerImpl::NavigateFromFrameProxy(
2374 RenderFrameHostImpl* render_frame_host,
2375 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022376 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452377 int initiator_process_id,
Anton Bikineevf62d1bf2021-05-15 17:56:072378 const absl::optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262379 bool is_renderer_initiated,
2380 SiteInstance* source_site_instance,
2381 const Referrer& referrer,
2382 ui::PageTransition page_transition,
2383 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252384 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262385 const std::string& method,
2386 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092387 const std::string& extra_headers,
Antonio Sartori2f763d9d2021-04-21 10:04:142388 network::mojom::SourceLocationPtr source_location,
John Delaney50425f82020-04-07 16:26:212389 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Anton Bikineevf62d1bf2021-05-15 17:56:072390 const absl::optional<blink::Impression>& impression) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582391 if (is_renderer_initiated)
2392 DCHECK(initiator_origin.has_value());
2393
clamy987a3752018-05-03 17:36:262394 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582395
Rakina Zata Amni66992a82020-06-24 03:57:522396 // Don't allow an entry replacement if there is no entry to replace.
2397 // http://crbug.com/457149
2398 if (GetEntryCount() == 0)
2399 should_replace_current_entry = false;
2400
clamy987a3752018-05-03 17:36:262401 // Create a NavigationEntry for the transfer, without making it the pending
2402 // entry. Subframe transfers should have a clone of the last committed entry
2403 // with a FrameNavigationEntry for the target frame. Main frame transfers
2404 // should have a new NavigationEntry.
2405 // TODO(creis): Make this unnecessary by creating (and validating) the params
2406 // directly, passing them to the destination RenderFrameHost. See
2407 // https://crbug.com/536906.
2408 std::unique_ptr<NavigationEntryImpl> entry;
2409 if (!node->IsMainFrame()) {
2410 // Subframe case: create FrameNavigationEntry.
2411 if (GetLastCommittedEntry()) {
2412 entry = GetLastCommittedEntry()->Clone();
2413 entry->set_extra_headers(extra_headers);
2414 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2415 // Renderer-initiated navigation that target a remote frame are currently
2416 // classified as browser-initiated when this one has already navigated.
2417 // See https://crbug.com/722251.
2418 } else {
2419 // If there's no last committed entry, create an entry for about:blank
2420 // with a subframe entry for our destination.
2421 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2422 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:062423 GURL(url::kAboutBlankURL), referrer, initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:102424 source_site_instance, page_transition, is_renderer_initiated,
2425 extra_headers, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:372426 nullptr /* blob_url_loader_factory */, should_replace_current_entry,
2427 GetWebContents()));
2428 // CreateNavigationEntry() may have changed the transition type.
2429 page_transition = entry->GetTransitionType();
clamy987a3752018-05-03 17:36:262430 }
clamy987a3752018-05-03 17:36:262431 entry->AddOrUpdateFrameEntry(
2432 node, -1, -1, nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582433 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072434 absl::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202435 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392436 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482437 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori79d549d2021-02-18 12:59:542438 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262439 } else {
2440 // Main frame case.
2441 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102442 url, referrer, initiator_origin, source_site_instance, page_transition,
2443 is_renderer_initiated, extra_headers, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:372444 blob_url_loader_factory, should_replace_current_entry,
2445 GetWebContents()));
2446 // CreateNavigationEntry() may have changed the transition type.
2447 page_transition = entry->GetTransitionType();
clamy987a3752018-05-03 17:36:262448 entry->root_node()->frame_entry->set_source_site_instance(
2449 static_cast<SiteInstanceImpl*>(source_site_instance));
2450 entry->root_node()->frame_entry->set_method(method);
2451 }
clamy987a3752018-05-03 17:36:262452
Camille Lamy5193caa2018-10-12 11:59:422453 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262454 if (GetLastCommittedEntry() &&
2455 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2456 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422457 override_user_agent = true;
clamy987a3752018-05-03 17:36:262458 }
2459 // TODO(creis): Set user gesture and intent received timestamp on Android.
2460
2461 // We may not have successfully added the FrameNavigationEntry to |entry|
2462 // above (per https://crbug.com/608402), in which case we create it from
2463 // scratch. This works because we do not depend on |frame_entry| being inside
2464 // |entry| during NavigateToEntry. This will go away when we shortcut this
2465 // further in https://crbug.com/536906.
2466 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2467 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452468 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
clamy987a3752018-05-03 17:36:262469 node->unique_name(), -1, -1, nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582470 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072471 absl::nullopt /* origin */, referrer, initiator_origin,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:122472 std::vector<GURL>(), blink::PageState(), method, -1,
2473 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482474 nullptr /* subresource_web_bundle_navigation_info */,
Titouan Rigoudy6ec70402021-02-02 15:42:192475 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262476 }
2477
Camille Lamy5193caa2018-10-12 11:59:422478 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022479 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452480 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292481 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422482 params.source_site_instance = source_site_instance;
2483 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2484 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032485 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422486 params.referrer = referrer;
2487 /* params.redirect_chain: skip */
2488 params.extra_headers = extra_headers;
2489 params.is_renderer_initiated = is_renderer_initiated;
2490 params.override_user_agent = UA_OVERRIDE_INHERIT;
2491 /* params.base_url_for_data_url: skip */
2492 /* params.virtual_url_for_data_url: skip */
2493 /* params.data_url_as_string: skip */
2494 params.post_data = post_body;
2495 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582496 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422497 /* params.frame_name: skip */
2498 // TODO(clamy): See if user gesture should be propagated to this function.
2499 params.has_user_gesture = false;
2500 params.should_clear_history_list = false;
2501 params.started_from_context_menu = false;
2502 /* params.navigation_ui_data: skip */
2503 /* params.input_start: skip */
Lucas Furukawa Gadania9c45682019-07-31 22:05:142504 params.was_activated = mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542505 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212506 params.impression = impression;
Camille Lamy5193caa2018-10-12 11:59:422507
2508 std::unique_ptr<NavigationRequest> request =
2509 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032510 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:142511 false /* has_user_gesture */, std::move(source_location),
2512 download_policy, ReloadType::NONE, entry.get(), frame_entry.get(),
2513 params.transition_type);
clamyea99ea12018-05-28 13:54:232514
2515 if (!request)
2516 return;
2517
Arthur Hemery948742762019-09-18 10:06:242518 // At this stage we are proceeding with this navigation. If this was renderer
2519 // initiated with user gesture, we need to make sure we clear up potential
2520 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2521 DiscardNonCommittedEntries();
2522
Lukasz Anforowicz9ee83c272020-12-01 20:14:052523 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262524}
2525
[email protected]d1198fd2012-08-13 22:50:192526void NavigationControllerImpl::SetSessionStorageNamespace(
Aaron Colwellf3b316e2021-03-11 20:17:052527 const StoragePartitionId& partition_id,
[email protected]8ff00d72012-10-23 19:12:212528 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192529 if (!session_storage_namespace)
2530 return;
2531
2532 // We can't overwrite an existing SessionStorage without violating spec.
2533 // Attempts to do so may give a tab access to another tab's session storage
2534 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152535 bool successful_insert =
2536 session_storage_namespace_map_
Aaron Colwellf3b316e2021-03-11 20:17:052537 .insert(std::make_pair(partition_id,
2538 static_cast<SessionStorageNamespaceImpl*>(
2539 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302540 .second;
2541 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
Aaron Colwellb731a0ae2021-03-19 19:14:472542 OnStoragePartitionIdAdded(partition_id);
[email protected]d1198fd2012-08-13 22:50:192543}
2544
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572545bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Aran Gilman37d11632019-10-08 23:07:152546 return IsInitialNavigation() && !GetLastCommittedEntry() &&
Carlos Caballeroede6f8c2021-01-28 11:01:502547 !frame_tree_.has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462548}
2549
Aran Gilman37d11632019-10-08 23:07:152550SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Aaron Colwell78b4bde2021-03-16 16:16:092551 const SiteInfo& site_info) {
2552 // TODO(acolwell): Remove partition_id logic once we have successfully
2553 // migrated the implementation to be a StoragePartitionConfig. At that point
2554 // |site_info| can be replaced with a StoragePartitionConfig.
2555 const StoragePartitionId partition_id =
2556 site_info.GetStoragePartitionId(browser_context_);
2557 const StoragePartitionConfig partition_config =
2558 site_info.GetStoragePartitionConfig(browser_context_);
[email protected]d1198fd2012-08-13 22:50:192559
[email protected]fdac6ade2013-07-20 01:06:302560 StoragePartition* partition =
Lukasz Anforowiczb9a969a2021-04-29 15:26:252561 browser_context_->GetStoragePartition(partition_config);
michaelnbacbcbd2016-02-09 00:32:032562 DOMStorageContextWrapper* context_wrapper =
2563 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2564
2565 SessionStorageNamespaceMap::const_iterator it =
2566 session_storage_namespace_map_.find(partition_id);
2567 if (it != session_storage_namespace_map_.end()) {
2568 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152569 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2570 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:472571
2572 // Verify that the config we generated now matches the one that
2573 // was generated when the namespace was added to the map.
2574 DCHECK_EQ(partition_config, it->first.config());
2575 if (partition_config != it->first.config()) {
2576 LogStoragePartitionIdCrashKeys(it->first, partition_id);
2577 }
2578
michaelnbacbcbd2016-02-09 00:32:032579 return it->second.get();
2580 }
2581
2582 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102583 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2584 SessionStorageNamespaceImpl::Create(context_wrapper);
2585 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2586 session_storage_namespace.get();
2587 session_storage_namespace_map_[partition_id] =
2588 std::move(session_storage_namespace);
Aaron Colwellb731a0ae2021-03-19 19:14:472589 OnStoragePartitionIdAdded(partition_id);
[email protected]fdac6ade2013-07-20 01:06:302590
Daniel Murphy31bbb8b12018-02-07 21:44:102591 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302592}
2593
2594SessionStorageNamespace*
2595NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Aaron Colwell78b4bde2021-03-16 16:16:092596 return GetSessionStorageNamespace(SiteInfo());
[email protected]fdac6ade2013-07-20 01:06:302597}
2598
2599const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572600NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302601 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552602}
[email protected]d202a7c2012-01-04 07:53:472603
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572604bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562605 return needs_reload_;
2606}
[email protected]a26023822011-12-29 00:23:552607
[email protected]46bb5e9c2013-10-03 22:16:472608void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412609 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2610}
2611
2612void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472613 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412614 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542615
2616 if (last_committed_entry_index_ != -1) {
2617 entries_[last_committed_entry_index_]->SetTransitionType(
2618 ui::PAGE_TRANSITION_RELOAD);
2619 }
[email protected]46bb5e9c2013-10-03 22:16:472620}
2621
[email protected]d202a7c2012-01-04 07:53:472622void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572623 DCHECK_LT(index, GetEntryCount());
2624 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312625 DiscardNonCommittedEntries();
2626
2627 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122628 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312629 last_committed_entry_index_--;
2630}
2631
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572632NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272633 // If there is no pending_entry_, there should be no pending_entry_index_.
2634 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2635
2636 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362637 // at that index. An exception is while a reload of a post commit error page
2638 // is ongoing; in that case pending entry will point to the entry replaced
2639 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272640 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362641 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2642 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272643
[email protected]022af742011-12-28 18:37:252644 return pending_entry_;
2645}
2646
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572647int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272648 // The pending entry index must always be less than the number of entries.
2649 // If there are no entries, it must be exactly -1.
2650 DCHECK_LT(pending_entry_index_, GetEntryCount());
2651 DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:552652 return pending_entry_index_;
2653}
2654
avi25764702015-06-23 15:43:372655void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572656 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362657 bool replace,
2658 bool was_post_commit_error) {
mastiz7eddb5f72016-06-23 09:52:452659 DCHECK(!ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2660 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202661
avi5cad4912015-06-19 05:25:442662 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2663 // need to keep continuity with the pending entry, so copy the pending entry's
2664 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2665 // then the renderer navigated on its own, independent of the pending entry,
2666 // so don't copy anything.
2667 if (pending_entry_ && pending_entry_index_ == -1)
2668 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202669
arthursonzogni69a6a1b2019-09-17 09:23:002670 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:202671
creisee17e932015-07-17 17:56:222672 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362673 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102674 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572675 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362676 // If the new entry is a post-commit error page, we store the current last
2677 // committed entry to the side so that we can put it back when navigating
2678 // away from the error.
2679 if (was_post_commit_error) {
2680 DCHECK(!entry_replaced_by_post_commit_error_);
2681 entry_replaced_by_post_commit_error_ =
2682 std::move(entries_[last_committed_entry_index_]);
2683 }
dcheng36b6aec92015-12-26 06:16:362684 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222685 return;
2686 }
[email protected]765b35502008-08-21 00:51:202687
creis37979a62015-08-04 19:48:182688 // We shouldn't see replace == true when there's no committed entries.
2689 DCHECK(!replace);
2690
Michael Thiessen9b14d512019-09-23 21:19:472691 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202692
Shivani Sharmad8c8d652019-02-13 17:27:572693 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202694
dcheng36b6aec92015-12-26 06:16:362695 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202696 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292697}
2698
Shivani Sharmad8c8d652019-02-13 17:27:572699void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162700 if (entries_.size() < max_entry_count())
2701 return;
2702
2703 DCHECK_EQ(max_entry_count(), entries_.size());
2704 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572705 CHECK_EQ(pending_entry_index_, -1);
2706
2707 int index = 0;
2708 if (base::FeatureList::IsEnabled(
2709 features::kHistoryManipulationIntervention)) {
2710 // Retrieve the oldest skippable entry.
2711 for (; index < GetEntryCount(); index++) {
2712 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2713 break;
2714 }
2715 }
2716
2717 // If there is no skippable entry or if it is the last committed entry then
2718 // fall back to pruning the oldest entry. It is not safe to prune the last
2719 // committed entry.
2720 if (index == GetEntryCount() || index == last_committed_entry_index_)
2721 index = 0;
2722
2723 bool should_succeed = RemoveEntryAtIndex(index);
2724 DCHECK_EQ(true, should_succeed);
2725
2726 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252727}
2728
clamy3cb9bea92018-07-10 12:42:022729void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162730 ReloadType reload_type,
2731 int sandboxed_source_frame_tree_node_id) {
Alexander Timin3a92df72019-09-20 11:59:502732 TRACE_EVENT0("navigation",
2733 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272734 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022735 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362736 if (pending_entry_index_ != -1) {
2737 // The pending entry may not be in entries_ if a post-commit error page is
2738 // showing.
2739 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2740 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2741 }
Gyuyoung Kim107c2a02021-04-13 01:49:302742 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572743 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:012744 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:002745 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:572746 int nav_entry_id = pending_entry_->GetUniqueID();
2747
[email protected]83c2e232011-10-07 21:36:462748 // If we were navigating to a slow-to-commit page, and the user performs
2749 // a session history navigation to the last committed page, RenderViewHost
2750 // will force the throbber to start, but WebKit will essentially ignore the
2751 // navigation, and won't send a message to stop the throbber. To prevent this
2752 // from happening, we drop the navigation here and stop the slow-to-commit
2753 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022754 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:082755 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:272756 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
[email protected]ec6c05f2013-10-23 18:41:572757 delegate_->Stop();
[email protected]6a13a6c2011-12-20 21:47:122758
[email protected]83c2e232011-10-07 21:36:462759 DiscardNonCommittedEntries();
2760 return;
2761 }
2762
creisce0ef3572017-01-26 17:53:082763 // Compare FrameNavigationEntries to see which frames in the tree need to be
2764 // navigated.
clamy3cb9bea92018-07-10 12:42:022765 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2766 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Alex Moshchuk3a4e77a2020-05-29 21:32:572767 FindFramesToNavigate(root, reload_type, &same_document_loads,
2768 &different_document_loads);
creis4e2ecb72015-06-20 00:46:302769
2770 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572771 // We were unable to match any frames to navigate. This can happen if a
2772 // history navigation targets a subframe that no longer exists
2773 // (https://crbug.com/705550). In this case, we need to update the current
2774 // history entry to the pending one but keep the main document loaded. We
2775 // also need to ensure that observers are informed about the updated
2776 // current history entry (e.g., for greying out back/forward buttons), and
2777 // that renderer processes update their history offsets. The easiest way
2778 // to do all that is to schedule a "redundant" same-document navigation in
2779 // the main frame.
2780 //
2781 // Note that we don't want to remove this history entry, as it might still
2782 // be valid later, since a frame that it's targeting may be recreated.
2783 //
2784 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
2785 // need to repeat history navigations until finding the one that works.
2786 // Consider changing this behavior to keep looking for the first valid
2787 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:022788 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422789 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572790 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:572791 ReloadType::NONE /* reload_type */,
2792 true /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422793 false /* is_history_navigation_in_new_child */);
clamy3cb9bea92018-07-10 12:42:022794 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572795 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:022796 DiscardPendingEntry(false);
2797 return;
2798 }
Alex Moshchuk3a4e77a2020-05-29 21:32:572799 same_document_loads.push_back(std::move(navigation_request));
2800
2801 // Sanity check that we never take this branch for any kinds of reloads,
2802 // as those should've queued a different-document load in the main frame.
2803 DCHECK(!is_forced_reload);
2804 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:302805 }
2806
Dave Tapuska8bfd84c2019-03-26 20:47:162807 // If |sandboxed_source_frame_node_id| is valid, then track whether this
2808 // navigation affects any frame outside the frame's subtree.
2809 if (sandboxed_source_frame_tree_node_id !=
2810 FrameTreeNode::kFrameTreeNodeInvalidId) {
2811 bool navigates_inside_tree =
2812 DoesSandboxNavigationStayWithinSubtree(
2813 sandboxed_source_frame_tree_node_id, same_document_loads) &&
2814 DoesSandboxNavigationStayWithinSubtree(
2815 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:502816 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:162817 // the number of pages that trigger this.
2818 FrameTreeNode* sandbox_source_frame_tree_node =
2819 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
2820 if (sandbox_source_frame_tree_node) {
2821 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
2822 sandbox_source_frame_tree_node->current_frame_host(),
2823 navigates_inside_tree
2824 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
2825 : blink::mojom::WebFeature::
2826 kSandboxBackForwardAffectsFramesOutsideSubtree);
2827 }
Dave Tapuska855c1e12019-08-23 20:45:522828
2829 // If the navigation occurred outside the tree discard it because
2830 // the sandboxed frame didn't have permission to navigate outside
2831 // its tree. If it is possible that the navigation is both inside and
2832 // outside the frame tree and we discard it entirely because we don't
2833 // want to end up in a history state that didn't exist before.
2834 if (base::FeatureList::IsEnabled(
2835 features::kHistoryPreventSandboxedNavigation) &&
2836 !navigates_inside_tree) {
2837 DiscardPendingEntry(false);
2838 return;
2839 }
Dave Tapuska8bfd84c2019-03-26 20:47:162840 }
2841
Carlos Caballero539a421c2020-07-06 10:25:572842 // BackForwardCache:
2843 // Navigate immediately if the document is in the BackForwardCache.
2844 if (back_forward_cache_.GetEntry(nav_entry_id)) {
2845 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
2846 DCHECK_EQ(reload_type, ReloadType::NONE);
2847 auto navigation_request = CreateNavigationRequestFromEntry(
2848 root, pending_entry_, pending_entry_->GetFrameEntry(root),
2849 ReloadType::NONE, false /* is_same_document_history_load */,
2850 false /* is_history_navigation_in_new_child */);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052851 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:572852
2853 return;
2854 }
2855
2856 // History navigation might try to reuse a specific BrowsingInstance, already
2857 // used by a page in the cache. To avoid having two different main frames that
2858 // live in the same BrowsingInstance, evict the all pages with this
2859 // BrowsingInstance from the cache.
2860 //
2861 // For example, take the following scenario:
2862 //
2863 // A1 = Some page on a.com
2864 // A2 = Some other page on a.com
2865 // B3 = An uncacheable page on b.com
2866 //
2867 // Then the following navigations occur:
2868 // A1->A2->B3->A1
2869 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
2870 // take its place) and A1 will be created in the same BrowsingInstance (and
2871 // SiteInstance), as A2.
2872 //
2873 // If we didn't do anything, both A1 and A2 would remain alive in the same
2874 // BrowsingInstance/SiteInstance, which is unsupported by
2875 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
2876 // A2 from the cache.
2877 if (pending_entry_->site_instance()) {
2878 back_forward_cache_.EvictFramesInRelatedSiteInstances(
2879 pending_entry_->site_instance());
2880 }
2881
clamy3cb9bea92018-07-10 12:42:022882 // This call does not support re-entrancy. See http://crbug.com/347742.
2883 CHECK(!in_navigate_to_pending_entry_);
2884 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:302885
arthursonzogni66f711c2019-10-08 14:40:362886 // It is not possible to delete the pending NavigationEntry while navigating
2887 // to it. Grab a reference to delay potential deletion until the end of this
2888 // function.
2889 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
2890
creis4e2ecb72015-06-20 00:46:302891 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:022892 for (auto& item : same_document_loads) {
2893 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052894 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302895 }
clamy3cb9bea92018-07-10 12:42:022896 for (auto& item : different_document_loads) {
2897 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052898 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302899 }
clamy3cb9bea92018-07-10 12:42:022900
2901 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:302902}
2903
Alex Moshchuk3a4e77a2020-05-29 21:32:572904NavigationControllerImpl::HistoryNavigationAction
2905NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:302906 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:572907 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:422908 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:582909 // Only active and prerendered documents are allowed to navigate in their
2910 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422911 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:012912 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:582913 // - If the document is in pending deletion, the browser already committed
2914 // to destroying this RenderFrameHost. See https://crbug.com/930278.
2915 // - If the document is in back-forward cache, it's not allowed to navigate
2916 // as it should remain frozen. Ignore the request and evict the document
2917 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422918 //
Sreeja Kamishettydb8e2892021-03-10 09:30:582919 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:422920 // which should all be inactive as well.
Sreeja Kamishettydb8e2892021-03-10 09:30:582921 if (render_frame_host->IsInactiveAndDisallowActivation())
Sreeja Kamishetty8eacabb2021-03-09 11:45:422922 return HistoryNavigationAction::kStopLooking;
2923 }
arthursonzogni03f76152019-02-12 10:35:202924
Alex Moshchuk3a4e77a2020-05-29 21:32:572925 // If there's no last committed entry, there is no previous history entry to
2926 // compare against, so fall back to a different-document load. Note that we
2927 // should only reach this case for the root frame and not descend further
2928 // into subframes.
2929 if (!GetLastCommittedEntry()) {
2930 DCHECK(frame->IsMainFrame());
2931 return HistoryNavigationAction::kDifferentDocument;
2932 }
2933
2934 // Reloads should result in a different-document load. Note that reloads may
2935 // also happen via the |needs_reload_| mechanism where the reload_type is
2936 // NONE, so detect this by comparing whether we're going to the same
2937 // entry that we're currently on. Similarly to above, only main frames
2938 // should reach this. Note that subframes support reloads, but that's done
2939 // via a different path that doesn't involve FindFramesToNavigate (see
2940 // RenderFrameHost::Reload()).
2941 if (reload_type != ReloadType::NONE ||
2942 pending_entry_index_ == last_committed_entry_index_) {
2943 DCHECK(frame->IsMainFrame());
2944 return HistoryNavigationAction::kDifferentDocument;
2945 }
2946
Alex Moshchuk47d1a4bd2020-06-01 22:15:342947 // If there is no new FrameNavigationEntry for the frame, ignore the
2948 // load. For example, this may happen when going back to an entry before a
2949 // frame was created. Suppose we commit a same-document navigation that also
2950 // results in adding a new subframe somewhere in the tree. If we go back,
2951 // the new subframe will be missing a FrameNavigationEntry in the previous
2952 // NavigationEntry, but we shouldn't delete or change what's loaded in
2953 // it.
2954 //
Alex Moshchuke65c39272020-06-03 17:55:372955 // Note that in this case, there is no need to keep looking for navigations
2956 // in subframes, which would be missing FrameNavigationEntries as well.
2957 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:342958 // It's important to check this before checking |old_item| below, since both
2959 // might be null, and in that case we still shouldn't change what's loaded in
2960 // this frame. Note that scheduling any loads assumes that |new_item| is
2961 // non-null. See https://crbug.com/1088354.
2962 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
2963 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:372964 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:342965
Alex Moshchuk3a4e77a2020-05-29 21:32:572966 // If there is no old FrameNavigationEntry, schedule a different-document
2967 // load.
2968 //
creis225a7432016-06-03 22:56:272969 // TODO(creis): Store the last committed FrameNavigationEntry to use here,
2970 // rather than assuming the NavigationEntry has up to date info on subframes.
Alex Moshchuk3a4e77a2020-05-29 21:32:572971 // Note that this may require sharing FrameNavigationEntries between
2972 // NavigationEntries (https://crbug.com/373041) as a prerequisite, since
2973 // otherwise the stored FrameNavigationEntry might get stale (e.g., after
2974 // subframe navigations, or in the case where we don't find any frames to
2975 // navigate and ignore a back/forward navigation while moving to a different
2976 // NavigationEntry, as in https://crbug.com/705550).
creis4e2ecb72015-06-20 00:46:302977 FrameNavigationEntry* old_item =
2978 GetLastCommittedEntry()->GetFrameEntry(frame);
Alex Moshchuk3a4e77a2020-05-29 21:32:572979 if (!old_item)
2980 return HistoryNavigationAction::kDifferentDocument;
2981
Alex Moshchuk3a4e77a2020-05-29 21:32:572982 // If the new item is not in the same SiteInstance, schedule a
2983 // different-document load. Newly restored items may not have a SiteInstance
2984 // yet, in which case it will be assigned on first commit.
2985 if (new_item->site_instance() &&
2986 new_item->site_instance() != old_item->site_instance())
2987 return HistoryNavigationAction::kDifferentDocument;
2988
2989 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:312990 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
2991 // that we do this even if the history navigation would not be modifying this
2992 // frame were it live.
2993 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:572994 return HistoryNavigationAction::kDifferentDocument;
2995
2996 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:312997 // Starting a navigation after a crash early-promotes the speculative
2998 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
2999 // committed yet, so we can not possibly perform a same-document history
3000 // navigation. The frame would need to be reloaded with a cross-document
3001 // navigation.
3002 if (!frame->current_frame_host()->has_committed_any_navigation())
3003 return HistoryNavigationAction::kDifferentDocument;
3004
creis54131692016-08-12 18:32:253005 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:493006 // sequence number but different item sequence number.
3007 if (new_item->document_sequence_number() ==
3008 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573009 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493010 }
avib48cb312016-05-05 21:35:003011
Alex Moshchuk3a4e77a2020-05-29 21:32:573012 // Otherwise, if both item and document sequence numbers differ, this
3013 // should be a different document load.
3014 return HistoryNavigationAction::kDifferentDocument;
3015 }
3016
3017 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373018 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573019 DCHECK_EQ(new_item->document_sequence_number(),
3020 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373021 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573022}
3023
3024void NavigationControllerImpl::FindFramesToNavigate(
3025 FrameTreeNode* frame,
3026 ReloadType reload_type,
3027 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3028 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3029 DCHECK(pending_entry_);
3030 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3031
3032 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3033
3034 if (action == HistoryNavigationAction::kSameDocument) {
3035 std::unique_ptr<NavigationRequest> navigation_request =
3036 CreateNavigationRequestFromEntry(
3037 frame, pending_entry_, new_item, reload_type,
3038 true /* is_same_document_history_load */,
3039 false /* is_history_navigation_in_new_child */);
3040 if (navigation_request) {
3041 // Only add the request if was properly created. It's possible for the
3042 // creation to fail in certain cases, e.g. when the URL is invalid.
3043 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303044 }
Lei Zhang96031532019-10-10 19:05:473045
Alex Moshchuk3a4e77a2020-05-29 21:32:573046 // TODO(avi, creis): This is a bug; we should not return here. Rather, we
3047 // should continue on and navigate all child frames which have also
3048 // changed. This bug is the cause of <https://crbug.com/542299>, which is
3049 // a NC_IN_PAGE_NAVIGATION renderer kill.
3050 //
3051 // However, this bug is a bandaid over a deeper and worse problem. Doing a
3052 // pushState immediately after loading a subframe is a race, one that no
3053 // web page author expects. If we fix this bug, many large websites break.
3054 // For example, see <https://crbug.com/598043> and the spec discussion at
3055 // <https://github.com/whatwg/html/issues/1191>.
3056 //
3057 // For now, we accept this bug, and hope to resolve the race in a
3058 // different way that will one day allow us to fix this.
3059 return;
3060 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473061 std::unique_ptr<NavigationRequest> navigation_request =
3062 CreateNavigationRequestFromEntry(
3063 frame, pending_entry_, new_item, reload_type,
3064 false /* is_same_document_history_load */,
3065 false /* is_history_navigation_in_new_child */);
3066 if (navigation_request) {
3067 // Only add the request if was properly created. It's possible for the
3068 // creation to fail in certain cases, e.g. when the URL is invalid.
3069 different_document_loads->push_back(std::move(navigation_request));
3070 }
3071 // For a different document, the subframes will be destroyed, so there's
3072 // no need to consider them.
3073 return;
Alex Moshchuke65c39272020-06-03 17:55:373074 } else if (action == HistoryNavigationAction::kStopLooking) {
3075 return;
creis4e2ecb72015-06-20 00:46:303076 }
3077
3078 for (size_t i = 0; i < frame->child_count(); i++) {
clamy3cb9bea92018-07-10 12:42:023079 FindFramesToNavigate(frame->child_at(i), reload_type, same_document_loads,
creis4e2ecb72015-06-20 00:46:303080 different_document_loads);
3081 }
3082}
3083
Harkiran Bolariaba823e42021-05-21 18:30:363084base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry(
clamy21718cc22018-06-13 13:34:243085 const LoadURLParams& params) {
3086 // Find the appropriate FrameTreeNode.
3087 FrameTreeNode* node = nullptr;
3088 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3089 !params.frame_name.empty()) {
3090 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003091 ? frame_tree_.FindByID(params.frame_tree_node_id)
3092 : frame_tree_.FindByName(params.frame_name);
clamy21718cc22018-06-13 13:34:243093 }
3094
3095 // If no FrameTreeNode was specified, navigate the main frame.
3096 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003097 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243098
Camille Lamy5193caa2018-10-12 11:59:423099 // Compute overrides to the LoadURLParams for |override_user_agent|,
3100 // |should_replace_current_entry| and |has_user_gesture| that will be used
3101 // both in the creation of the NavigationEntry and the NavigationRequest.
3102 // Ideally, the LoadURLParams themselves would be updated, but since they are
3103 // passed as a const reference, this is not possible.
3104 // TODO(clamy): When we only create a NavigationRequest, move this to
3105 // CreateNavigationRequestFromLoadURLParams.
3106 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3107 GetLastCommittedEntry());
3108
3109 // Don't allow an entry replacement if there is no entry to replace.
3110 // http://crbug.com/457149
3111 bool should_replace_current_entry =
3112 params.should_replace_current_entry && entries_.size();
3113
Scott Violet5ae6c42e2020-10-28 02:47:373114 ui::PageTransition transition_type = params.transition_type;
3115
clamy21718cc22018-06-13 13:34:243116 // Javascript URLs should not create NavigationEntries. All other navigations
3117 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243118 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373119 std::unique_ptr<NavigationEntryImpl> entry =
3120 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3121 should_replace_current_entry,
3122 params.has_user_gesture);
3123 // CreateNavigationEntryFromLoadParams() may modify the transition type.
3124 transition_type = entry->GetTransitionType();
clamy21718cc22018-06-13 13:34:243125 DiscardPendingEntry(false);
3126 SetPendingEntry(std::move(entry));
3127 }
3128
3129 // Renderer-debug URLs are sent to the renderer process immediately for
3130 // processing and don't need to create a NavigationRequest.
3131 // Note: this includes navigations to JavaScript URLs, which are considered
3132 // renderer-debug URLs.
3133 // Note: we intentionally leave the pending entry in place for renderer debug
3134 // URLs, unlike the cases below where we clear it if the navigation doesn't
3135 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303136 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483137 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3138 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153139 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483140 params.url, browser_context_)) {
3141 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363142 return nullptr;
Oleg Davydov2cc0167b2019-02-05 14:32:483143 }
3144
clamy21718cc22018-06-13 13:34:243145 HandleRendererDebugURL(node, params.url);
Harkiran Bolariaba823e42021-05-21 18:30:363146 return nullptr;
clamy21718cc22018-06-13 13:34:243147 }
3148
Antonio Sartori78a749f2020-11-30 12:03:393149 DCHECK(pending_entry_);
3150
clamy21718cc22018-06-13 13:34:243151 // Convert navigations to the current URL to a reload.
3152 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3153 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3154 // DevTools sometimes issues navigations to main frames that they do not
3155 // expect to see treated as reload, and it only works because they pass a
3156 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3157 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543158 ReloadType reload_type = params.reload_type;
3159 if (reload_type == ReloadType::NONE &&
3160 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043161 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243162 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243163 params.load_type ==
3164 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
3165 false /* is_reload */, false /* is_navigation_to_existing_entry */,
clamy21718cc22018-06-13 13:34:243166 GetLastCommittedEntry())) {
3167 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473168 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393169
3170 // If this is a reload of an existing FrameNavigationEntry and we had a
3171 // policy container for it, then we should copy it into the pending entry,
3172 // so that it is copied to the navigation request in
3173 // CreateNavigationRequestFromLoadParams later.
3174 if (GetLastCommittedEntry()) {
3175 FrameNavigationEntry* previous_frame_entry =
3176 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193177 if (previous_frame_entry &&
3178 previous_frame_entry->policy_container_policies()) {
3179 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163180 previous_frame_entry->policy_container_policies()->Clone());
Antonio Sartori78a749f2020-11-30 12:03:393181 }
3182 }
3183 }
3184
3185 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
3186 // document (so no last commit), then we should copy the document polices from
3187 // RenderFrameHost's PolicyContainerHost. The NavigationRequest will create a
3188 // new PolicyContainerHost with the document policies from the
3189 // |pending_entry_|, and that PolicyContainerHost will be put in the final
3190 // RenderFrameHost for the navigation. This way, we ensure that we keep
3191 // enforcing the right policies on the initial empty document after the
3192 // reload.
3193 if (!GetLastCommittedEntry() && params.url.IsAboutBlank()) {
3194 if (node->current_frame_host() &&
3195 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193196 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163197 node->current_frame_host()
3198 ->policy_container_host()
3199 ->policies()
3200 .Clone());
Antonio Sartori78a749f2020-11-30 12:03:393201 }
clamy21718cc22018-06-13 13:34:243202 }
3203
3204 // navigation_ui_data should only be present for main frame navigations.
3205 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
3206
Camille Lamy5193caa2018-10-12 11:59:423207 std::unique_ptr<NavigationRequest> request =
3208 CreateNavigationRequestFromLoadParams(
3209 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143210 params.has_user_gesture, network::mojom::SourceLocation::New(),
3211 blink::NavigationDownloadPolicy(), reload_type, pending_entry_,
3212 pending_entry_->GetFrameEntry(node), transition_type);
clamy21718cc22018-06-13 13:34:243213
3214 // If the navigation couldn't start, return immediately and discard the
3215 // pending NavigationEntry.
3216 if (!request) {
3217 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363218 return nullptr;
clamy21718cc22018-06-13 13:34:243219 }
3220
Camille Lamy5193caa2018-10-12 11:59:423221#if DCHECK_IS_ON()
3222 // Safety check that NavigationRequest and NavigationEntry match.
3223 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3224#endif
3225
clamy21718cc22018-06-13 13:34:243226 // This call does not support re-entrancy. See http://crbug.com/347742.
3227 CHECK(!in_navigate_to_pending_entry_);
3228 in_navigate_to_pending_entry_ = true;
3229
arthursonzogni66f711c2019-10-08 14:40:363230 // It is not possible to delete the pending NavigationEntry while navigating
3231 // to it. Grab a reference to delay potential deletion until the end of this
3232 // function.
3233 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3234
Harkiran Bolariaba823e42021-05-21 18:30:363235 base::WeakPtr<NavigationHandle> created_navigation_handle(
3236 request->GetWeakPtr());
Lukasz Anforowicz9ee83c272020-12-01 20:14:053237 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243238
3239 in_navigate_to_pending_entry_ = false;
Harkiran Bolariaba823e42021-05-21 18:30:363240 return created_navigation_handle;
clamy21718cc22018-06-13 13:34:243241}
3242
clamyea99ea12018-05-28 13:54:233243void NavigationControllerImpl::HandleRendererDebugURL(
3244 FrameTreeNode* frame_tree_node,
3245 const GURL& url) {
3246 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243247 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3248 // the renderer process is not live, unless it is the initial navigation
3249 // of the tab.
clamyea99ea12018-05-28 13:54:233250 if (!IsInitialNavigation()) {
3251 DiscardNonCommittedEntries();
3252 return;
3253 }
Fergal Dalyecd3b0202020-06-25 01:57:373254 // The current frame is always a main frame. If IsInitialNavigation() is
3255 // true then there have been no navigations and any frames of this tab must
3256 // be in the same renderer process. If that has crashed then the only frame
3257 // that can be revived is the main frame.
3258 frame_tree_node->render_manager()
3259 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233260 }
3261 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3262}
3263
clamy21718cc22018-06-13 13:34:243264std::unique_ptr<NavigationEntryImpl>
3265NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3266 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423267 const LoadURLParams& params,
3268 bool override_user_agent,
3269 bool should_replace_current_entry,
3270 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393271 // Browser initiated navigations might not have a blob_url_loader_factory set
3272 // in params even if the navigation is to a blob URL. If that happens, lookup
3273 // the correct url loader factory to use here.
3274 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483275 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473276 // Resolve the blob URL in the storage partition associated with the target
3277 // frame. This is the storage partition the URL will be loaded in, and only
3278 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393279 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473280 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393281 }
3282
clamy21718cc22018-06-13 13:34:243283 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443284 // extra_headers in params are \n separated; navigation entries want \r\n.
3285 std::string extra_headers_crlf;
3286 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243287
3288 // For subframes, create a pending entry with a corresponding frame entry.
3289 if (!node->IsMainFrame()) {
Tommy C. Li03eee77a2019-02-05 02:07:443290 if (GetLastCommittedEntry()) {
3291 // Create an identical NavigationEntry with a new FrameNavigationEntry for
3292 // the target subframe.
3293 entry = GetLastCommittedEntry()->Clone();
3294 } else {
3295 // If there's no last committed entry, create an entry for about:blank
3296 // with a subframe entry for our destination.
3297 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
3298 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063299 GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103300 params.source_site_instance.get(), params.transition_type,
3301 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:373302 blob_url_loader_factory, should_replace_current_entry,
3303 GetWebContents()));
Tommy C. Li03eee77a2019-02-05 02:07:443304 }
Nasko Oskov18006bc2018-12-06 02:53:583305
clamy21718cc22018-06-13 13:34:243306 entry->AddOrUpdateFrameEntry(
3307 node, -1, -1, nullptr,
3308 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Anton Bikineevf62d1bf2021-05-15 17:56:073309 params.url, absl::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203310 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393311 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:483312 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori78a749f2020-11-30 12:03:393313 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163314 // a conversion of a new navigation into a reload, we will set the right
3315 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193316 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243317 } else {
3318 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243319 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063320 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103321 params.source_site_instance.get(), params.transition_type,
3322 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:373323 blob_url_loader_factory, should_replace_current_entry,
3324 GetWebContents()));
clamy21718cc22018-06-13 13:34:243325 entry->set_source_site_instance(
3326 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3327 entry->SetRedirectChain(params.redirect_chain);
3328 }
3329
3330 // Set the FTN ID (only used in non-site-per-process, for tests).
3331 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243332 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423333 entry->SetIsOverridingUserAgent(override_user_agent);
3334 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543335 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243336
clamy21718cc22018-06-13 13:34:243337 switch (params.load_type) {
3338 case LOAD_TYPE_DEFAULT:
3339 break;
3340 case LOAD_TYPE_HTTP_POST:
3341 entry->SetHasPostData(true);
3342 entry->SetPostData(params.post_data);
3343 break;
3344 case LOAD_TYPE_DATA:
3345 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3346 entry->SetVirtualURL(params.virtual_url_for_data_url);
3347#if defined(OS_ANDROID)
3348 entry->SetDataURLAsString(params.data_url_as_string);
3349#endif
3350 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3351 break;
clamy21718cc22018-06-13 13:34:243352 }
3353
3354 // TODO(clamy): NavigationEntry is meant for information that will be kept
3355 // after the navigation ended and therefore is not appropriate for
3356 // started_from_context_menu. Move started_from_context_menu to
3357 // NavigationUIData.
3358 entry->set_started_from_context_menu(params.started_from_context_menu);
3359
3360 return entry;
3361}
3362
clamyea99ea12018-05-28 13:54:233363std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423364NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3365 FrameTreeNode* node,
3366 const LoadURLParams& params,
3367 bool override_user_agent,
3368 bool should_replace_current_entry,
3369 bool has_user_gesture,
Antonio Sartori2f763d9d2021-04-21 10:04:143370 network::mojom::SourceLocationPtr source_location,
Yeunjoo Choi3df791a2021-02-17 07:07:253371 blink::NavigationDownloadPolicy download_policy,
Camille Lamy5193caa2018-10-12 11:59:423372 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573373 NavigationEntryImpl* entry,
Scott Violet5ae6c42e2020-10-28 02:47:373374 FrameNavigationEntry* frame_entry,
3375 ui::PageTransition transition_type) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573376 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283377 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533378 // All renderer-initiated navigations must have an initiator_origin.
3379 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513380
Camille Lamy5193caa2018-10-12 11:59:423381 GURL url_to_load;
3382 GURL virtual_url;
Anton Bikineevf62d1bf2021-05-15 17:56:073383 absl::optional<url::Origin> origin_to_commit =
3384 frame_entry ? frame_entry->committed_origin() : absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:323385
Camille Lamy2baa8022018-10-19 16:43:173386 // For main frames, rewrite the URL if necessary and compute the virtual URL
3387 // that should be shown in the address bar.
3388 if (node->IsMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423389 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173390 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423391 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423392
Camille Lamy2baa8022018-10-19 16:43:173393 // For DATA loads, override the virtual URL.
3394 if (params.load_type == LOAD_TYPE_DATA)
3395 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423396
Camille Lamy2baa8022018-10-19 16:43:173397 if (virtual_url.is_empty())
3398 virtual_url = url_to_load;
3399
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573400 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283401
Aran Gilman249eb122019-12-02 23:32:463402 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3403 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3404 // return different results, leading to a different URL in the
3405 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3406 // pending NavigationEntry, as we'll only make one call to
3407 // RewriteUrlForNavigation.
3408 VLOG_IF(1, (url_to_load != frame_entry->url()))
3409 << "NavigationRequest and FrameEntry have different URLs: "
3410 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283411
Camille Lamy2baa8022018-10-19 16:43:173412 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423413 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173414 // NavigationRequest.
3415 } else {
3416 url_to_load = params.url;
3417 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243418 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173419 }
Camille Lamy5193caa2018-10-12 11:59:423420
Ehsan Karamad44fc72112019-02-26 18:15:473421 if (node->render_manager()->is_attaching_inner_delegate()) {
3422 // Avoid starting any new navigations since this node is now preparing for
3423 // attaching an inner delegate.
3424 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203425 }
Camille Lamy5193caa2018-10-12 11:59:423426
Camille Lamy5193caa2018-10-12 11:59:423427 if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load))
3428 return nullptr;
3429
Kunihiko Sakamoto346a74e2021-03-10 08:57:483430 if (!DoesURLMatchOriginForNavigation(
3431 url_to_load, origin_to_commit,
3432 frame_entry->subresource_web_bundle_navigation_info())) {
Nasko Oskov03912102019-01-11 00:21:323433 DCHECK(false) << " url:" << url_to_load
3434 << " origin:" << origin_to_commit.value();
3435 return nullptr;
3436 }
3437
Camille Lamy5193caa2018-10-12 11:59:423438 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403439 blink::PreviewsState previews_state =
3440 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
Camille Lamy5193caa2018-10-12 11:59:423441 if (!node->IsMainFrame()) {
3442 // For subframes, use the state of the top-level frame.
3443 previews_state = node->frame_tree()
3444 ->root()
3445 ->current_frame_host()
3446 ->last_navigation_previews_state();
3447 }
3448
Camille Lamy5193caa2018-10-12 11:59:423449 // This will be used to set the Navigation Timing API navigationStart
3450 // parameter for browser navigations in new tabs (intents, tabs opened through
3451 // "Open link in new tab"). If the navigation must wait on the current
3452 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3453 // will be updated when the BeforeUnload ack is received.
3454 base::TimeTicks navigation_start = base::TimeTicks::Now();
3455
danakjd83d706d2020-11-25 22:11:123456 // Look for a pending commit that is to another document in this
3457 // FrameTreeNode. If one exists, then the last committed URL will not be the
3458 // current URL by the time this navigation commits.
3459 bool has_pending_cross_document_commit =
3460 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493461 bool is_currently_error_page = node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123462
danakjb952ef12021-01-14 19:58:493463 mojom::NavigationType navigation_type = GetNavigationType(
3464 /*old_url=*/node->current_url(),
3465 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3466 has_pending_cross_document_commit, is_currently_error_page,
3467 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423468
3469 // Create the NavigationParams based on |params|.
3470
3471 bool is_view_source_mode = virtual_url.SchemeIs(kViewSourceScheme);
Charlie Harrison8c113a32019-01-07 16:08:293472
3473 // Update |download_policy| if the virtual URL is view-source. Why do this
3474 // now? Possibly the URL could be rewritten to a view-source via some URL
3475 // handler.
3476 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253477 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293478
Camille Lamy5193caa2018-10-12 11:59:423479 const GURL& history_url_for_data_url =
3480 params.base_url_for_data_url.is_empty() ? GURL() : virtual_url;
Scott Violet5ae6c42e2020-10-28 02:47:373481 // Don't use |params.transition_type| as calling code may supply a different
3482 // value.
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513483 mojom::CommonNavigationParamsPtr common_params =
3484 mojom::CommonNavigationParams::New(
3485 url_to_load, params.initiator_origin,
3486 blink::mojom::Referrer::New(params.referrer.url,
3487 params.referrer.policy),
Scott Violet5ae6c42e2020-10-28 02:47:373488 transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513489 should_replace_current_entry, params.base_url_for_data_url,
3490 history_url_for_data_url, previews_state, navigation_start,
3491 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
Antonio Sartori2f763d9d2021-04-21 10:04:143492 params.post_data, std::move(source_location),
arthursonzogniaf7c62c52020-02-12 10:49:413493 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273494 false /* has_text_fragment_token */,
3495 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3496 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513497 false /* is_history_navigation_in_new_child_frame */,
Charlie Hu5ffc0152019-12-06 15:59:533498 params.input_start);
Camille Lamy5193caa2018-10-12 11:59:423499
Lucas Furukawa Gadania9c45682019-07-31 22:05:143500 mojom::CommitNavigationParamsPtr commit_params =
3501 mojom::CommitNavigationParams::New(
arthursonzognid5a8d0b2021-03-11 17:36:433502 frame_entry->committed_origin(), network::mojom::WebSandboxFlags(),
3503 override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323504 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143505 std::vector<net::RedirectInfo>(),
3506 std::string() /* post_content_type */, common_params->url,
3507 common_params->method, params.can_load_local_resources,
3508 frame_entry->page_state(), entry->GetUniqueID(),
3509 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3510 -1 /* pending_history_list_offset */,
3511 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3512 params.should_clear_history_list ? 0 : GetEntryCount(),
3513 false /* was_discarded */, is_view_source_mode,
3514 params.should_clear_history_list, mojom::NavigationTiming::New(),
Anton Bikineevf62d1bf2021-05-15 17:56:073515 absl::nullopt /* appcache_host_id */,
Lucas Furukawa Gadania9c45682019-07-31 22:05:143516 mojom::WasActivatedOption::kUnknown,
3517 base::UnguessableToken::Create() /* navigation_token */,
Lucas Furukawa Gadani97ea6382019-08-07 19:14:513518 std::vector<mojom::PrefetchedSignedExchangeInfoPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143519#if defined(OS_ANDROID)
3520 std::string(), /* data_url_as_string */
3521#endif
arthursonzogni14379782020-05-15 09:09:273522 !params.is_renderer_initiated, /* is_browser_initiated */
Sreeja Kamishetty46f762c2021-02-05 07:52:463523 node->frame_tree()->is_prerendering() /* is_prerendering */,
Tsuyoshi Horoe86d7702019-11-29 01:52:473524 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533525 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173526 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073527 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163528 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333529 false /* origin_agent_cluster */,
Maks Orlovichc66745a2020-06-30 17:40:023530 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213531 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Nate Chapind1fe3612021-04-16 20:45:573532 false /* is_cross_browsing_instance */, nullptr /* old_page_info */,
3533 -1 /* http_response_code */,
3534 std::vector<
3535 mojom::AppHistoryEntryPtr>() /* app_history_back_entries */,
3536 std::vector<
3537 mojom::AppHistoryEntryPtr>() /* app_history_forward_entries */);
Camille Lamy5193caa2018-10-12 11:59:423538#if defined(OS_ANDROID)
3539 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143540 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423541 }
3542#endif
3543
Lucas Furukawa Gadania9c45682019-07-31 22:05:143544 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423545
3546 // A form submission may happen here if the navigation is a renderer-initiated
3547 // form submission that took the OpenURL path.
3548 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3549
3550 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3551 std::string extra_headers_crlf;
3552 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093553
3554 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143555 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083556 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453557 params.initiator_frame_token.has_value()
3558 ? &(params.initiator_frame_token.value())
3559 : nullptr,
3560 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3561 request_body,
John Delaney50425f82020-04-07 16:26:213562 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
3563 params.impression);
Yao Xiaodc5ed102019-06-04 19:19:093564 navigation_request->set_from_download_cross_origin_redirect(
3565 params.from_download_cross_origin_redirect);
3566 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423567}
3568
3569std::unique_ptr<NavigationRequest>
3570NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233571 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573572 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233573 FrameNavigationEntry* frame_entry,
3574 ReloadType reload_type,
3575 bool is_same_document_history_load,
Arthur Hemerybee4a752019-05-29 10:50:553576 bool is_history_navigation_in_new_child_frame) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343577 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233578 GURL dest_url = frame_entry->url();
Anton Bikineevf62d1bf2021-05-15 17:56:073579 absl::optional<url::Origin> origin_to_commit =
Nasko Oskov03912102019-01-11 00:21:323580 frame_entry->committed_origin();
3581
clamyea99ea12018-05-28 13:54:233582 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013583 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573584 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233585 // We may have been redirected when navigating to the current URL.
3586 // Use the URL the user originally intended to visit as signaled by the
3587 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013588 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573589 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233590 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323591 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233592 }
3593
Ehsan Karamad44fc72112019-02-26 18:15:473594 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3595 // Avoid starting any new navigations since this node is now preparing for
3596 // attaching an inner delegate.
3597 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203598 }
3599
Camille Lamy5193caa2018-10-12 11:59:423600 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573601 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233602 return nullptr;
3603 }
3604
Kunihiko Sakamoto346a74e2021-03-10 08:57:483605 if (!DoesURLMatchOriginForNavigation(
3606 dest_url, origin_to_commit,
3607 frame_entry->subresource_web_bundle_navigation_info())) {
3608 DCHECK(false) << " url:" << dest_url
3609 << " origin:" << origin_to_commit.value();
Nasko Oskov03912102019-01-11 00:21:323610 return nullptr;
3611 }
3612
clamyea99ea12018-05-28 13:54:233613 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403614 blink::PreviewsState previews_state =
3615 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
clamyea99ea12018-05-28 13:54:233616 if (!frame_tree_node->IsMainFrame()) {
3617 // For subframes, use the state of the top-level frame.
3618 previews_state = frame_tree_node->frame_tree()
3619 ->root()
3620 ->current_frame_host()
3621 ->last_navigation_previews_state();
clamyea99ea12018-05-28 13:54:233622 }
3623
clamyea99ea12018-05-28 13:54:233624 // This will be used to set the Navigation Timing API navigationStart
3625 // parameter for browser navigations in new tabs (intents, tabs opened through
3626 // "Open link in new tab"). If the navigation must wait on the current
3627 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3628 // will be updated when the BeforeUnload ack is received.
3629 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233630
danakjd83d706d2020-11-25 22:11:123631 // Look for a pending commit that is to another document in this
3632 // FrameTreeNode. If one exists, then the last committed URL will not be the
3633 // current URL by the time this navigation commits.
3634 bool has_pending_cross_document_commit =
3635 frame_tree_node->render_manager()
3636 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493637 bool is_currently_error_page =
3638 frame_tree_node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123639
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513640 mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123641 /*old_url=*/frame_tree_node->current_url(),
3642 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493643 has_pending_cross_document_commit, is_currently_error_page,
3644 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423645
3646 // A form submission may happen here if the navigation is a
3647 // back/forward/reload navigation that does a form resubmission.
3648 scoped_refptr<network::ResourceRequestBody> request_body;
3649 std::string post_content_type;
3650 if (frame_entry->method() == "POST") {
3651 request_body = frame_entry->GetPostData(&post_content_type);
3652 // Might have a LF at end.
Peter Kastingb53b81912021-04-28 19:23:303653 post_content_type = std::string(
3654 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL));
Camille Lamy5193caa2018-10-12 11:59:423655 }
3656
3657 // Create the NavigationParams based on |entry| and |frame_entry|.
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513658 mojom::CommonNavigationParamsPtr common_params =
3659 entry->ConstructCommonNavigationParams(
3660 *frame_entry, request_body, dest_url,
3661 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
3662 navigation_type, previews_state, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533663 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513664 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553665 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423666
3667 // TODO(clamy): |intended_as_new_entry| below should always be false once
3668 // Reload no longer leads to this being called for a pending NavigationEntry
3669 // of index -1.
Lucas Furukawa Gadania9c45682019-07-31 22:05:143670 mojom::CommitNavigationParamsPtr commit_params =
3671 entry->ConstructCommitNavigationParams(
3672 *frame_entry, common_params->url, origin_to_commit,
3673 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3674 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533675 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3676 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143677 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423678
clamyea99ea12018-05-28 13:54:233679 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143680 frame_tree_node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083681 !entry->is_renderer_initiated(), false /* was_opener_suppressed */,
3682 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453683 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3684 entry->extra_headers(), frame_entry, entry, request_body,
Anton Bikineevf62d1bf2021-05-15 17:56:073685 nullptr /* navigation_ui_data */, absl::nullopt /* impression */);
clamyea99ea12018-05-28 13:54:233686}
3687
[email protected]d202a7c2012-01-04 07:53:473688void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213689 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273690 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403691
[email protected]2db9bd72012-04-13 20:20:563692 // We need to notify the ssl_manager_ before the web_contents_ so the
[email protected]df1af242009-05-01 00:11:403693 // location bar will have up-to-date information about the security style
3694 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133695 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403696
[email protected]7f924832014-08-09 05:57:223697 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:573698 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463699
[email protected]b0f724c2013-09-05 04:21:133700 // TODO(avi): Remove. http://crbug.com/170921
3701 NotificationDetails notification_details =
3702 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153703 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3704 Source<NavigationController>(this),
3705 notification_details);
initial.commit09911bf2008-07-26 23:55:293706}
3707
initial.commit09911bf2008-07-26 23:55:293708// static
[email protected]d202a7c2012-01-04 07:53:473709size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233710 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153711 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:213712 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233713}
3714
[email protected]d202a7c2012-01-04 07:53:473715void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223716 if (is_active && needs_reload_)
3717 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293718}
3719
[email protected]d202a7c2012-01-04 07:53:473720void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293721 if (!needs_reload_)
3722 return;
3723
Bo Liucdfa4b12018-11-06 00:21:443724 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3725 needs_reload_type_);
3726
initial.commit09911bf2008-07-26 23:55:293727 // Calling Reload() results in ignoring state, and not loading.
3728 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3729 // cached state.
avicc872d7242015-08-19 21:26:343730 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163731 NavigateToExistingPendingEntry(ReloadType::NONE,
3732 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343733 } else if (last_committed_entry_index_ != -1) {
arthursonzogni5c4c202d2017-04-25 23:41:273734 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343735 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163736 NavigateToExistingPendingEntry(ReloadType::NONE,
3737 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343738 } else {
3739 // If there is something to reload, the successful reload will clear the
3740 // |needs_reload_| flag. Otherwise, just do it here.
3741 needs_reload_ = false;
3742 }
initial.commit09911bf2008-07-26 23:55:293743}
3744
Carlos IL42b416592019-10-07 23:10:363745void NavigationControllerImpl::LoadPostCommitErrorPage(
3746 RenderFrameHost* render_frame_host,
3747 const GURL& url,
3748 const std::string& error_page_html,
3749 net::Error error) {
Rakina Zata Amni919b7922020-12-11 09:03:133750 RenderFrameHostImpl* rfhi =
3751 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:583752
3753 // Only active documents can load post-commit error pages:
3754 // - If the document is in pending deletion, the browser already committed to
3755 // destroying this RenderFrameHost so ignore loading the error page.
3756 // - If the document is in back-forward cache, it's not allowed to navigate as
3757 // it should remain frozen. Ignore the request and evict the document from
3758 // back-forward cache.
3759 // - If the document is prerendering, it can navigate but when loading error
3760 // pages, cancel prerendering.
3761 if (rfhi->IsInactiveAndDisallowActivation())
3762 return;
3763
Rakina Zata Amni919b7922020-12-11 09:03:133764 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:413765
3766 mojom::CommonNavigationParamsPtr common_params =
3767 CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:593768 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
3769 // the frame actually committed (e.g. iframe with "src" set to a
3770 // slow-responding URL). We should rewrite the URL to about:blank in this
3771 // case, as the renderer will only think a page is an error page if it has a
3772 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:133773 common_params->url = url.is_empty() ? GURL("about:blank") : url;
John Delaney131ad362019-08-08 21:57:413774 mojom::CommitNavigationParamsPtr commit_params =
3775 CreateCommitNavigationParams();
Antonio Sartori58591c892021-04-21 06:54:333776 commit_params->original_url = common_params->url;
John Delaney131ad362019-08-08 21:57:413777
arthursonzogni70ac7302020-05-28 08:49:053778 // Error pages have a fully permissive FramePolicy.
3779 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
3780 // error pages.
3781 commit_params->frame_policy = blink::FramePolicy();
3782
John Delaney131ad362019-08-08 21:57:413783 std::unique_ptr<NavigationRequest> navigation_request =
3784 NavigationRequest::CreateBrowserInitiated(
3785 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083786 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:193787 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453788 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:063789 "" /* extra_headers */, nullptr /* frame_entry */,
3790 nullptr /* entry */, nullptr /* post_body */,
Anton Bikineevf62d1bf2021-05-15 17:56:073791 nullptr /* navigation_ui_data */, absl::nullopt /* impression */);
Carlos IL42b416592019-10-07 23:10:363792 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:413793 navigation_request->set_net_error(error);
3794 node->CreatedNavigationRequest(std::move(navigation_request));
3795 DCHECK(node->navigation_request());
3796 node->navigation_request()->BeginNavigation();
3797}
3798
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573799void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213800 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093801 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:153802 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143803 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293804}
3805
[email protected]d202a7c2012-01-04 07:53:473806void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363807 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553808 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363809 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293810
initial.commit09911bf2008-07-26 23:55:293811 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293812}
[email protected]765b35502008-08-21 00:51:203813
arthursonzogni69a6a1b2019-09-17 09:23:003814void NavigationControllerImpl::DiscardNonCommittedEntries() {
Michael Thiessen9b14d512019-09-23 21:19:473815 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:103816 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
3817 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:293818 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:473819 return;
Michael Thiessenc5676d22019-09-25 22:32:103820 }
avi45a72532015-04-07 21:01:453821 DiscardPendingEntry(false);
arthursonzogni69a6a1b2019-09-17 09:23:003822 if (delegate_)
3823 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:483824}
3825
avi7c6f35e2015-05-08 17:52:383826int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3827 int nav_entry_id) const {
3828 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3829 if (entries_[i]->GetUniqueID() == nav_entry_id)
3830 return i;
3831 }
3832 return -1;
3833}
3834
[email protected]d202a7c2012-01-04 07:53:473835void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573836 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253837 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573838 DCHECK_LE(max_index, source->GetEntryCount());
[email protected]e1cd5452010-08-26 18:03:253839 for (int i = 0; i < max_index; i++) {
Nasko Oskov923ca482020-12-04 21:20:463840 // TODO(creis): Once we start sharing FrameNavigationEntries between
3841 // NavigationEntries, it will not be safe to share them with another tab.
3842 // Must have a version of Clone that recreates them.
3843 entries_.insert(entries_.begin() + i, source->entries_[i]->Clone());
[email protected]e1cd5452010-08-26 18:03:253844 }
arthursonzogni5c4c202d2017-04-25 23:41:273845 DCHECK(pending_entry_index_ == -1 ||
3846 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:253847}
[email protected]c5b88d82012-10-06 17:03:333848
3849void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:183850 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:333851 get_timestamp_callback_ = get_timestamp_callback;
3852}
[email protected]8ff00d72012-10-23 19:12:213853
Shivani Sharmaffb32b82019-04-09 16:58:473854// History manipulation intervention:
3855void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:473856 bool replace_entry,
3857 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:403858 bool is_renderer_initiated,
3859 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:453860 // Note that for a subframe, previous_document_was_activated is true if the
3861 // gesture happened in any subframe (propagated to main frame) or in the main
3862 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:473863 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:273864 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:473865 if (last_committed_entry_index_ != -1) {
shivanigithube92c33da2020-09-14 13:01:413866 // This histogram always counts when navigating away from an entry,
3867 // irrespective of whether the skippable flag was changed or not, and
3868 // whether this entry is being reused or not.
Shivani Sharmaffb32b82019-04-09 16:58:473869 UMA_HISTOGRAM_BOOLEAN(
shivanigithube92c33da2020-09-14 13:01:413870 "Navigation.BackForward.SetShouldSkipOnBackForwardUI",
3871 GetEntryAtIndex(last_committed_entry_index_)
3872 ->should_skip_on_back_forward_ui());
Shivani Sharmaffb32b82019-04-09 16:58:473873 }
3874 return;
3875 }
3876 if (last_committed_entry_index_ == -1)
3877 return;
3878
Shivani Sharmac4cc8922019-04-18 03:11:173879 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473880 UMA_HISTOGRAM_BOOLEAN("Navigation.BackForward.SetShouldSkipOnBackForwardUI",
3881 true);
3882
Alexander Timine3ec4192020-04-20 16:39:403883 // Log UKM with the URL we are navigating away from.
3884 ukm::builders::HistoryManipulationIntervention(
3885 previous_page_load_ukm_source_id)
3886 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:473887}
3888
Shivani Sharmac4cc8922019-04-18 03:11:173889void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
3890 int reference_index,
3891 bool skippable) {
3892 auto* reference_entry = GetEntryAtIndex(reference_index);
3893 reference_entry->set_should_skip_on_back_forward_ui(skippable);
3894
3895 int64_t document_sequence_number =
3896 reference_entry->root_node()->frame_entry->document_sequence_number();
3897 for (int index = 0; index < GetEntryCount(); index++) {
3898 auto* entry = GetEntryAtIndex(index);
3899 if (entry->root_node()->frame_entry->document_sequence_number() ==
3900 document_sequence_number) {
3901 entry->set_should_skip_on_back_forward_ui(skippable);
3902 }
3903 }
3904}
3905
arthursonzogni66f711c2019-10-08 14:40:363906std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
3907NavigationControllerImpl::ReferencePendingEntry() {
3908 DCHECK(pending_entry_);
3909 auto pending_entry_ref =
3910 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
3911 pending_entry_refs_.insert(pending_entry_ref.get());
3912 return pending_entry_ref;
3913}
3914
3915void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
3916 // Ignore refs that don't correspond to the current pending entry.
3917 auto it = pending_entry_refs_.find(ref);
3918 if (it == pending_entry_refs_.end())
3919 return;
3920 pending_entry_refs_.erase(it);
3921
3922 if (!pending_entry_refs_.empty())
3923 return;
3924
3925 // The pending entry may be deleted before the last PendingEntryRef.
3926 if (!pending_entry_)
3927 return;
3928
3929 // We usually clear the pending entry when the matching NavigationRequest
3930 // fails, so that an arbitrary URL isn't left visible above a committed page.
3931 //
3932 // However, we do preserve the pending entry in some cases, such as on the
3933 // initial navigation of an unmodified blank tab. We also allow the delegate
3934 // to say when it's safe to leave aborted URLs in the omnibox, to let the
3935 // user edit the URL and try again. This may be useful in cases that the
3936 // committed page cannot be attacker-controlled. In these cases, we still
3937 // allow the view to clear the pending entry and typed URL if the user
3938 // requests (e.g., hitting Escape with focus in the address bar).
3939 //
3940 // Do not leave the pending entry visible if it has an invalid URL, since this
3941 // might be formatted in an unexpected or unsafe way.
3942 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:363943 bool should_preserve_entry =
3944 (pending_entry_ == GetVisibleEntry()) &&
3945 pending_entry_->GetURL().is_valid() &&
3946 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
3947 if (should_preserve_entry)
3948 return;
3949
3950 DiscardPendingEntry(true);
3951 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3952}
3953
Titouan Rigoudy6ec70402021-02-02 15:42:193954std::unique_ptr<PolicyContainerPolicies>
3955NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:393956 RenderFrameHostImpl* rfh,
3957 bool is_same_document,
3958 NavigationRequest* request) {
Titouan Rigoudy6ec70402021-02-02 15:42:193959 if (!ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(request))
Antonio Sartori78a749f2020-11-30 12:03:393960 return nullptr;
3961
3962 if (is_same_document) {
Charlie Reis73e356242021-04-02 17:10:313963 // TODO(https://crbug.com/524208): Remove this nullptr check when we can
3964 // ensure we always have a FrameNavigationEntry here.
3965 if (!GetLastCommittedEntry())
3966 return nullptr;
3967
Antonio Sartori78a749f2020-11-30 12:03:393968 FrameNavigationEntry* previous_frame_entry =
3969 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
3970
3971 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
3972 // ensure we always have a FrameNavigationEntry here.
3973 if (!previous_frame_entry)
3974 return nullptr;
3975
Titouan Rigoudy6ec70402021-02-02 15:42:193976 const PolicyContainerPolicies* previous_policies =
3977 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:393978
Titouan Rigoudy6ec70402021-02-02 15:42:193979 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:393980 return nullptr;
3981
3982 // Make a copy of the policy container for the new FrameNavigationEntry.
Antonio Sartori5d09b30f2021-03-02 09:27:163983 return previous_policies->Clone();
Antonio Sartori78a749f2020-11-30 12:03:393984 }
3985
Antonio Sartori4f5373792021-05-31 10:56:473986 return rfh->policy_container_host()->policies().Clone();
Antonio Sartori78a749f2020-11-30 12:03:393987}
3988
Carlos Caballeroede6f8c2021-01-28 11:01:503989void NavigationControllerImpl::SetHistoryOffsetAndLength(int history_offset,
3990 int history_length) {
3991 OPTIONAL_TRACE_EVENT2(
3992 "content", "NavigationControllerImpl::SetHistoryOffsetAndLength",
3993 "history_offset", history_offset, "history_length", history_length);
3994
3995 auto callback = base::BindRepeating(
3996 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
3997 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
3998 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
3999 }
4000 },
4001 history_offset, history_length);
4002 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
4003}
4004
4005void NavigationControllerImpl::DidAccessInitialMainDocument() {
4006 // We may have left a failed browser-initiated navigation in the address bar
4007 // to let the user edit it and try again. Clear it now that content might
4008 // show up underneath it.
4009 if (!frame_tree_.IsLoading() && GetPendingEntry())
4010 DiscardPendingEntry(false);
4011
4012 // Update the URL display.
4013 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
4014}
4015
4016void NavigationControllerImpl::UpdateStateForFrame(
4017 RenderFrameHostImpl* rfhi,
4018 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:564019 OPTIONAL_TRACE_EVENT1("content",
4020 "NavigationControllerImpl::UpdateStateForFrame",
4021 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:504022 // The state update affects the last NavigationEntry associated with the given
4023 // |render_frame_host|. This may not be the last committed NavigationEntry (as
4024 // in the case of an UpdateState from a frame being swapped out). We track
4025 // which entry this is in the RenderFrameHost's nav_entry_id.
4026 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
4027 if (!entry)
4028 return;
4029
4030 FrameNavigationEntry* frame_entry =
4031 entry->GetFrameEntry(rfhi->frame_tree_node());
4032 if (!frame_entry)
4033 return;
4034
4035 // The SiteInstance might not match if we do a cross-process navigation with
4036 // replacement (e.g., auto-subframe), in which case the swap out of the old
4037 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4038 // has already been replaced and destroyed.
4039 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4040 return;
4041
4042 if (page_state == frame_entry->page_state())
4043 return; // Nothing to update.
4044
4045 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4046
4047 // The document_sequence_number and item_sequence_number recorded in the
4048 // FrameNavigationEntry should not differ from the one coming with the update,
4049 // since it must come from the same document. Do not update it if a difference
4050 // is detected, as this indicates that |frame_entry| is not the correct one.
4051 blink::ExplodedPageState exploded_state;
4052 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4053 return;
4054
4055 if (exploded_state.top.document_sequence_number !=
4056 frame_entry->document_sequence_number() ||
4057 exploded_state.top.item_sequence_number !=
4058 frame_entry->item_sequence_number()) {
4059 return;
4060 }
4061
4062 frame_entry->SetPageState(page_state);
4063 NotifyEntryChanged(entry);
4064}
4065
Aaron Colwellb731a0ae2021-03-19 19:14:474066void NavigationControllerImpl::OnStoragePartitionIdAdded(
4067 const StoragePartitionId& partition_id) {
4068 auto it = partition_config_to_id_map_.insert(
4069 std::make_pair(partition_id.config(), partition_id));
4070 bool successful_insert = it.second;
4071 DCHECK(successful_insert);
4072 if (!successful_insert) {
4073 LogStoragePartitionIdCrashKeys(it.first->second, partition_id);
4074 }
4075}
4076
4077void NavigationControllerImpl::LogStoragePartitionIdCrashKeys(
4078 const StoragePartitionId& original_partition_id,
4079 const StoragePartitionId& new_partition_id) {
4080 base::debug::SetCrashKeyString(
4081 base::debug::AllocateCrashKeyString("original_partition_id",
4082 base::debug::CrashKeySize::Size256),
4083 original_partition_id.ToString());
4084
4085 base::debug::SetCrashKeyString(
4086 base::debug::AllocateCrashKeyString("new_partition_id",
4087 base::debug::CrashKeySize::Size256),
4088 new_partition_id.ToString());
4089
4090 base::debug::DumpWithoutCrashing();
4091}
4092
Nate Chapind1fe3612021-04-16 20:45:574093std::vector<mojom::AppHistoryEntryPtr>
4094NavigationControllerImpl::PopulateSingleAppHistoryEntryVector(
4095 Direction direction,
4096 int entry_index,
4097 const url::Origin& pending_origin,
4098 FrameTreeNode* node,
4099 SiteInstance* site_instance,
4100 int64_t previous_item_sequence_number) {
4101 std::vector<mojom::AppHistoryEntryPtr> entries;
4102 int offset = direction == Direction::kForward ? 1 : -1;
4103 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4104 i += offset) {
4105 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
4106 if (!frame_entry || !frame_entry->committed_origin())
4107 break;
4108 if (site_instance != frame_entry->site_instance())
4109 break;
4110 if (!pending_origin.IsSameOriginWith(*frame_entry->committed_origin()))
4111 break;
4112 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4113 continue;
4114 blink::ExplodedPageState exploded_page_state;
4115 if (blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4116 &exploded_page_state)) {
4117 blink::ExplodedFrameState frame_state = exploded_page_state.top;
4118 mojom::AppHistoryEntryPtr entry = mojom::AppHistoryEntry::New(
4119 frame_state.app_history_key.value_or(std::u16string()),
4120 frame_state.app_history_id.value_or(std::u16string()),
4121 frame_state.url_string.value_or(std::u16string()));
4122 DCHECK(pending_origin.CanBeDerivedFrom(GURL(entry->url)));
4123 entries.push_back(std::move(entry));
4124 previous_item_sequence_number = frame_entry->item_sequence_number();
4125 }
4126 }
4127 // If |entries| was constructed by iterating backwards from
4128 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4129 // earliest-at-the-front. Reverse it.
4130 if (direction == Direction::kBack)
4131 std::reverse(entries.begin(), entries.end());
4132 return entries;
4133}
4134
4135void NavigationControllerImpl::PopulateAppHistoryEntryVectors(
4136 NavigationRequest* request) {
4137 url::Origin pending_origin =
4138 request->commit_params().origin_to_commit
4139 ? *request->commit_params().origin_to_commit
4140 : url::Origin::Create(request->common_params().url);
4141
4142 FrameTreeNode* node = request->frame_tree_node();
4143 scoped_refptr<SiteInstance> site_instance =
4144 request->GetRenderFrameHost()->GetSiteInstance();
4145
4146 // NOTE: |entry_index| is an estimate of the index where this entry will
4147 // commit, but it may be wrong in corner cases (e.g., if we are at the max
4148 // entry limit, the earliest entry will be dropped). This is ok because this
4149 // algorithm only uses |entry_index| to walk the entry list as it stands right
4150 // now, and it isn't saved for anything post-commit.
4151 int entry_index = GetPendingEntryIndex();
4152 bool will_create_new_entry = false;
4153 if (NavigationTypeUtils::IsReload(request->common_params().navigation_type) ||
4154 request->common_params().should_replace_current_entry) {
4155 entry_index = GetLastCommittedEntryIndex();
4156 } else if (entry_index == -1) {
4157 will_create_new_entry = true;
4158 entry_index = GetLastCommittedEntryIndex() + 1;
4159 }
4160
4161 int64_t pending_item_sequence_number = 0;
4162 if (auto* pending_entry = GetPendingEntry()) {
4163 if (auto* frame_entry = pending_entry->GetFrameEntry(node))
4164 pending_item_sequence_number = frame_entry->item_sequence_number();
4165 }
4166
4167 request->set_app_history_back_entries(PopulateSingleAppHistoryEntryVector(
4168 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
4169 pending_item_sequence_number));
4170
4171 // Don't populate forward entries if they will be truncated by a new entry.
4172 if (!will_create_new_entry) {
4173 request->set_app_history_forward_entries(
4174 PopulateSingleAppHistoryEntryVector(
4175 Direction::kForward, entry_index, pending_origin, node,
4176 site_instance.get(), pending_item_sequence_number));
4177 }
4178}
4179
[email protected]8ff00d72012-10-23 19:12:214180} // namespace content