blob: fb118bd70df304374f6b6baedfaca2b2c243ecfa [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"
Minggang Wang7ee0c742021-06-16 16:16:51103#include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h"
[email protected]cca6f392014-05-28 21:32:26104#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:29105
[email protected]8ff00d72012-10-23 19:12:21106namespace content {
[email protected]e9ba4472008-09-14 15:42:43107namespace {
108
109// Invoked when entries have been pruned, or removed. For example, if the
110// current entries are [google, digg, yahoo], with the current entry google,
111// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47112void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50113 int index,
[email protected]c12bf1a12008-09-17 16:28:49114 int count) {
[email protected]8ff00d72012-10-23 19:12:21115 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50116 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49117 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14118 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43119}
120
[email protected]e9ba4472008-09-14 15:42:43121// Configure all the NavigationEntries in entries for restore. This resets
122// the transition type to reload and makes sure the content state isn't empty.
123void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57124 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48125 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47126 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43127 // Use a transition type of reload so that we don't incorrectly increase
128 // the typed count.
Lei Zhang96031532019-10-10 19:05:47129 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
130 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43131 }
132}
133
[email protected]bf70edce2012-06-20 22:32:22134// Determines whether or not we should be carrying over a user agent override
135// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57136bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22137 return last_entry && last_entry->GetIsOverridingUserAgent();
138}
139
Camille Lamy5193caa2018-10-12 11:59:42140// Determines whether to override user agent for a navigation.
141bool ShouldOverrideUserAgent(
142 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57143 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42144 switch (override_user_agent) {
145 case NavigationController::UA_OVERRIDE_INHERIT:
146 return ShouldKeepOverride(last_committed_entry);
147 case NavigationController::UA_OVERRIDE_TRUE:
148 return true;
149 case NavigationController::UA_OVERRIDE_FALSE:
150 return false;
Camille Lamy5193caa2018-10-12 11:59:42151 }
152 NOTREACHED();
153 return false;
154}
155
Rakina Zata Amni312822d72021-06-04 16:13:37156// Returns true if this navigation should be treated as a reload. For e.g.
clamy0a656e42018-02-06 18:18:28157// navigating to the last committed url via the address bar or clicking on a
Rakina Zata Amni312822d72021-06-04 16:13:37158// link which results in a navigation to the last committed URL (but wasn't
159// converted to do a replacement navigation in the renderer), etc.
Fergal Daly766177d2020-07-07 07:54:04160// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
161// |base_url_for_data_url|, |transition_type| correspond to the new navigation
162// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
163// navigation that committed.
164bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
165 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57166 const GURL& virtual_url,
167 const GURL& base_url_for_data_url,
168 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57169 bool is_post,
Rakina Zata Amni312822d72021-06-04 16:13:37170 bool should_replace_current_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57171 NavigationEntryImpl* last_committed_entry) {
Rakina Zata Amni312822d72021-06-04 16:13:37172 // Navigations intended to do a replacement shouldn't be converted to do a
173 // reload.
174 if (should_replace_current_entry)
clamy0a656e42018-02-06 18:18:28175 return false;
clamy0a656e42018-02-06 18:18:28176 // Only convert to reload if at least one navigation committed.
177 if (!last_committed_entry)
ananta3bdd8ae2016-12-22 17:11:55178 return false;
179
arthursonzogni7a8243682017-12-14 16:41:42180 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28181 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42182 return false;
183
ananta3bdd8ae2016-12-22 17:11:55184 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
185 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
186 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28187 bool transition_type_can_be_converted = false;
188 if (ui::PageTransitionCoreTypeIs(transition_type,
189 ui::PAGE_TRANSITION_RELOAD) &&
190 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
191 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55192 }
clamy0a656e42018-02-06 18:18:28193 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55194 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28195 transition_type_can_be_converted = true;
196 }
197 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
198 transition_type_can_be_converted = true;
199 if (!transition_type_can_be_converted)
200 return false;
201
202 // This check is required for cases like view-source:, etc. Here the URL of
203 // the navigation entry would contain the url of the page, while the virtual
204 // URL contains the full URL including the view-source prefix.
205 if (virtual_url != last_committed_entry->GetVirtualURL())
206 return false;
207
Fergal Daly766177d2020-07-07 07:54:04208 // Check that the URLs match.
209 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
210 // If there's no frame entry then by definition the URLs don't match.
211 if (!frame_entry)
212 return false;
213
214 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28215 return false;
216
217 // This check is required for Android WebView loadDataWithBaseURL. Apps
218 // can pass in anything in the base URL and we need to ensure that these
219 // match before classifying it as a reload.
220 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
221 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
222 return false;
ananta3bdd8ae2016-12-22 17:11:55223 }
224
clamy0a656e42018-02-06 18:18:28225 // Skip entries with SSL errors.
226 if (last_committed_entry->ssl_error())
227 return false;
228
229 // Don't convert to a reload when the last navigation was a POST or the new
230 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04231 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28232 return false;
233
234 return true;
ananta3bdd8ae2016-12-22 17:11:55235}
236
Nasko Oskov03912102019-01-11 00:21:32237bool DoesURLMatchOriginForNavigation(
238 const GURL& url,
Anton Bikineevf62d1bf2021-05-15 17:56:07239 const absl::optional<url::Origin>& origin,
Kunihiko Sakamoto346a74e2021-03-10 08:57:48240 SubresourceWebBundleNavigationInfo*
241 subresource_web_bundle_navigation_info) {
Nasko Oskov03912102019-01-11 00:21:32242 // If there is no origin supplied there is nothing to match. This can happen
243 // for navigations to a pending entry and therefore it should be allowed.
244 if (!origin)
245 return true;
246
Kunihiko Sakamoto346a74e2021-03-10 08:57:48247 if (url.SchemeIs(url::kUrnScheme) && subresource_web_bundle_navigation_info) {
248 // Urn: subframe from WebBundle has an opaque origin derived from the
249 // Bundle's origin.
250 return origin->CanBeDerivedFrom(
251 subresource_web_bundle_navigation_info->bundle_url());
252 }
253
Nasko Oskov03912102019-01-11 00:21:32254 return origin->CanBeDerivedFrom(url);
255}
256
Anton Bikineevf62d1bf2021-05-15 17:56:07257absl::optional<url::Origin> GetCommittedOriginForFrameEntry(
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12258 const mojom::DidCommitProvisionalLoadParams& params,
259 NavigationRequest* request) {
Nasko Oskov03912102019-01-11 00:21:32260 // Error pages commit in an opaque origin, yet have the real URL that resulted
261 // in an error as the |params.url|. Since successful reload of an error page
262 // should commit in the correct origin, setting the opaque origin on the
263 // FrameNavigationEntry will be incorrect.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12264 if (request->DidEncounterError())
Anton Bikineevf62d1bf2021-05-15 17:56:07265 return absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:32266
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12267 // We also currently don't save committed origins for loadDataWithBaseURL
268 // navigations (probably accidentally). Without this check, navigations to
269 // the FrameNavigationEntry might fail the DoesURLMatchOriginForNavigation()
270 // check since the origin will be based on the base URL instead of the data:
271 // URL used for the navigation.
272 // TODO(https://crbug.com/1198406): Save committed origin in
273 // FrameNavigationEntry for this case too.
Anton Bikineevf62d1bf2021-05-15 17:56:07274 absl::optional<std::string> data_url_as_string;
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12275#if defined(OS_ANDROID)
276 data_url_as_string = request->commit_params().data_url_as_string;
277#endif
278 if (NavigationRequest::IsLoadDataWithBaseURLAndUnreachableURL(
279 request->IsInMainFrame(), request->common_params(),
280 data_url_as_string)) {
Anton Bikineevf62d1bf2021-05-15 17:56:07281 return absl::nullopt;
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12282 }
283
Anton Bikineevf62d1bf2021-05-15 17:56:07284 return absl::make_optional(params.origin);
Nasko Oskov03912102019-01-11 00:21:32285}
286
Camille Lamy5193caa2018-10-12 11:59:42287bool IsValidURLForNavigation(bool is_main_frame,
288 const GURL& virtual_url,
289 const GURL& dest_url) {
290 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
291 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
292 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
293 << virtual_url.possibly_invalid_spec();
294 return false;
295 }
296
297 // Don't attempt to navigate to non-empty invalid URLs.
298 if (!dest_url.is_valid() && !dest_url.is_empty()) {
299 LOG(WARNING) << "Refusing to load invalid URL: "
300 << dest_url.possibly_invalid_spec();
301 return false;
302 }
303
304 // The renderer will reject IPC messages with URLs longer than
305 // this limit, so don't attempt to navigate with a longer URL.
306 if (dest_url.spec().size() > url::kMaxURLChars) {
307 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
308 << " characters.";
309 return false;
310 }
311
Aaron Colwell33109c592020-04-21 21:31:19312 // Reject renderer debug URLs because they should have been handled before
313 // we get to this point. This check handles renderer debug URLs
314 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
315 // provides defense-in-depth if a renderer debug URL manages to get here via
316 // some other path. We want to reject the navigation here so it doesn't
317 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30318 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19319 LOG(WARNING) << "Refusing to load renderer debug URL: "
320 << dest_url.possibly_invalid_spec();
321 return false;
322 }
323
Camille Lamy5193caa2018-10-12 11:59:42324 return true;
325}
326
Mikel Astizba9cf2fd2017-12-17 10:38:10327// See replaced_navigation_entry_data.h for details: this information is meant
328// to ensure |*output_entry| keeps track of its original URL (landing page in
329// case of server redirects) as it gets replaced (e.g. history.replaceState()),
330// without overwriting it later, for main frames.
331void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57332 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10333 NavigationEntryImpl* output_entry) {
334 if (output_entry->GetReplacedEntryData().has_value())
335 return;
336
337 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57338 data.first_committed_url = replaced_entry->GetURL();
339 data.first_timestamp = replaced_entry->GetTimestamp();
340 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29341 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10342}
343
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51344mojom::NavigationType GetNavigationType(const GURL& old_url,
345 const GURL& new_url,
346 ReloadType reload_type,
347 NavigationEntryImpl* entry,
348 const FrameNavigationEntry& frame_entry,
danakjd83d706d2020-11-25 22:11:12349 bool has_pending_cross_document_commit,
danakjb952ef12021-01-14 19:58:49350 bool is_currently_error_page,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51351 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23352 // Reload navigations
353 switch (reload_type) {
354 case ReloadType::NORMAL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51355 return mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23356 case ReloadType::BYPASSING_CACHE:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51357 return mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23358 case ReloadType::ORIGINAL_REQUEST_URL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51359 return mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23360 case ReloadType::NONE:
361 break; // Fall through to rest of function.
362 }
363
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08364 if (entry->IsRestored()) {
Lei Zhang96031532019-10-10 19:05:47365 return entry->GetHasPostData() ? mojom::NavigationType::RESTORE_WITH_POST
366 : mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23367 }
368
danakjb952ef12021-01-14 19:58:49369 const bool can_be_same_document =
370 // A pending cross-document commit means this navigation will not occur in
371 // the current document, as that document would end up being replaced in
372 // the meantime.
373 !has_pending_cross_document_commit &&
374 // If the current document is an error page, we should always treat it as
375 // a different-document navigation so that we'll attempt to load the
376 // document we're navigating to (and not stay in the current error page).
377 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12378
clamyea99ea12018-05-28 13:54:23379 // History navigations.
380 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12381 return can_be_same_document && is_same_document_history_load
Lei Zhang96031532019-10-10 19:05:47382 ? mojom::NavigationType::HISTORY_SAME_DOCUMENT
383 : mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23384 }
385 DCHECK(!is_same_document_history_load);
386
387 // A same-document fragment-navigation happens when the only part of the url
388 // that is modified is after the '#' character.
389 //
390 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12391 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23392 //
393 // Note: this check is only valid for navigations that are not history
394 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
395 // history navigation from 'A#foo' to 'A#bar' is not a same-document
396 // navigation, but a different-document one. This is why history navigation
397 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47398 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
399 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12400
401 // The one case where we do the wrong thing here and incorrectly choose
402 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
403 // the renderer is a frameset. All frameset navigations should be
404 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
405 // navigation would do the right thing, as it would send it to the browser and
406 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
407 // into this method). But since we can't tell that case here for browser-
408 // initiated navigations, we have to get the renderer involved. In that case
409 // the navigation would be restarted due to the renderer spending a reply of
410 // mojom::CommitResult::RestartCrossDocument.
411
412 return can_be_same_document && is_same_doc
413 ? mojom::NavigationType::SAME_DOCUMENT
414 : mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23415}
416
Camille Lamy5193caa2018-10-12 11:59:42417// Adjusts the original input URL if needed, to get the URL to actually load and
418// the virtual URL, which may differ.
419void RewriteUrlForNavigation(const GURL& original_url,
420 BrowserContext* browser_context,
421 GURL* url_to_load,
422 GURL* virtual_url,
423 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42424 // Allow the browser URL handler to rewrite the URL. This will, for example,
425 // remove "view-source:" from the beginning of the URL to get the URL that
426 // will actually be loaded. This real URL won't be shown to the user, just
427 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31428 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42429 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
430 url_to_load, browser_context, reverse_on_redirect);
431}
432
433#if DCHECK_IS_ON()
434// Helper sanity check function used in debug mode.
435void ValidateRequestMatchesEntry(NavigationRequest* request,
436 NavigationEntryImpl* entry) {
437 if (request->frame_tree_node()->IsMainFrame()) {
438 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
439 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
440 request->common_params().transition, entry->GetTransitionType()));
441 }
442 DCHECK_EQ(request->common_params().should_replace_current_entry,
443 entry->should_replace_entry());
Nasko Oskovc36327d2019-01-03 23:23:04444 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42445 entry->should_clear_history_list());
446 DCHECK_EQ(request->common_params().has_user_gesture,
447 entry->has_user_gesture());
448 DCHECK_EQ(request->common_params().base_url_for_data_url,
449 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04450 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42451 entry->GetCanLoadLocalResources());
452 DCHECK_EQ(request->common_params().started_from_context_menu,
453 entry->has_started_from_context_menu());
454
455 FrameNavigationEntry* frame_entry =
456 entry->GetFrameEntry(request->frame_tree_node());
457 if (!frame_entry) {
458 NOTREACHED();
459 return;
460 }
461
Camille Lamy5193caa2018-10-12 11:59:42462 DCHECK_EQ(request->common_params().method, frame_entry->method());
463
Nasko Oskovc36327d2019-01-03 23:23:04464 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42465 if (redirect_size == frame_entry->redirect_chain().size()) {
466 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04467 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42468 frame_entry->redirect_chain()[i]);
469 }
470 } else {
471 NOTREACHED();
472 }
473}
474#endif // DCHECK_IS_ON()
475
Dave Tapuska8bfd84c2019-03-26 20:47:16476// Returns whether the session history NavigationRequests in |navigations|
477// would stay within the subtree of the sandboxed iframe in
478// |sandbox_frame_tree_node_id|.
479bool DoesSandboxNavigationStayWithinSubtree(
480 int sandbox_frame_tree_node_id,
481 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
482 for (auto& item : navigations) {
483 bool within_subtree = false;
484 // Check whether this NavigationRequest affects a frame within the
485 // sandboxed frame's subtree by walking up the tree looking for the
486 // sandboxed frame.
487 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03488 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16489 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
490 within_subtree = true;
491 break;
492 }
493 }
494 if (!within_subtree)
495 return false;
496 }
497 return true;
498}
499
Titouan Rigoudy6ec70402021-02-02 15:42:19500bool ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(
Antonio Sartori78a749f2020-11-30 12:03:39501 const NavigationRequest* request) {
502 // For local schemes we need to store the policy container in the
503 // FrameNavigationEntry, so that we can reload it in case of history
504 // navigation.
505 //
506 // TODO(https://crbug.com/1146361 and https://crbug.com/1146362): blob: and
507 // filesystem: should be removed from this list when we have properly
508 // implemented storing their policy container in the respective store.
509 return (request->common_params().url.SchemeIs(url::kAboutScheme) ||
510 request->common_params().url.SchemeIs(url::kDataScheme) ||
511 request->common_params().url.SchemeIsBlob() ||
512 request->common_params().url.SchemeIsFileSystem());
513}
514
[email protected]e9ba4472008-09-14 15:42:43515} // namespace
516
arthursonzogni66f711c2019-10-08 14:40:36517// NavigationControllerImpl::PendingEntryRef------------------------------------
518
519NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
520 base::WeakPtr<NavigationControllerImpl> controller)
521 : controller_(controller) {}
522
523NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
524 if (!controller_) // Can be null with interstitials.
525 return;
526
527 controller_->PendingEntryRefDeleted(this);
528}
529
[email protected]d202a7c2012-01-04 07:53:47530// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29531
[email protected]23a918b2014-07-15 09:51:36532const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23533
[email protected]765b35502008-08-21 00:51:20534// static
[email protected]d202a7c2012-01-04 07:53:47535size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23536 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20537
[email protected]e6fec472013-05-14 05:29:02538// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20539// when testing.
540static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29541
[email protected]71fde352011-12-29 03:29:56542// static
dcheng9bfa5162016-04-09 01:00:57543std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
544 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10545 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07546 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10547 ui::PageTransition transition,
548 bool is_renderer_initiated,
549 const std::string& extra_headers,
550 BrowserContext* browser_context,
551 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
552 return NavigationControllerImpl::CreateNavigationEntry(
553 url, referrer, std::move(initiator_origin),
554 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Rakina Zata Amni66992a82020-06-24 03:57:52555 extra_headers, browser_context, std::move(blob_url_loader_factory),
Scott Violetcf6ea7e2021-06-09 21:09:21556 false /* should_replace_entry */);
Lukasz Anforowicz641234d52019-11-07 21:07:10557}
558
559// static
560std::unique_ptr<NavigationEntryImpl>
561NavigationControllerImpl::CreateNavigationEntry(
562 const GURL& url,
563 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07564 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10565 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57566 ui::PageTransition transition,
567 bool is_renderer_initiated,
568 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09569 BrowserContext* browser_context,
Rakina Zata Amni66992a82020-06-24 03:57:52570 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Scott Violetcf6ea7e2021-06-09 21:09:21571 bool should_replace_entry) {
Camille Lamy5193caa2018-10-12 11:59:42572 GURL url_to_load;
573 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56574 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42575 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
576 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56577
Lukasz Anforowicz641234d52019-11-07 21:07:10578 // Let the NTP override the navigation params and pretend that this is a
579 // browser-initiated, bookmark-like navigation.
580 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violetcf6ea7e2021-06-09 21:09:21581 source_site_instance, &transition, &is_renderer_initiated, &referrer,
582 &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10583
Patrick Monettef507e982019-06-19 20:18:06584 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28585 nullptr, // The site instance for tabs is sent on navigation
586 // (WebContents::GetSiteInstance).
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58587 url_to_load, referrer, initiator_origin, std::u16string(), transition,
Camille Lamy5193caa2018-10-12 11:59:42588 is_renderer_initiated, blob_url_loader_factory);
589 entry->SetVirtualURL(virtual_url);
590 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56591 entry->set_update_virtual_url_with_url(reverse_on_redirect);
592 entry->set_extra_headers(extra_headers);
Rakina Zata Amni66992a82020-06-24 03:57:52593 entry->set_should_replace_entry(should_replace_entry);
Patrick Monettef507e982019-06-19 20:18:06594 return entry;
[email protected]71fde352011-12-29 03:29:56595}
596
[email protected]cdcb1dee2012-01-04 00:46:20597// static
598void NavigationController::DisablePromptOnRepost() {
599 g_check_for_repost = false;
600}
601
[email protected]c5b88d82012-10-06 17:03:33602base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
603 base::Time t) {
604 // If |t| is between the water marks, we're in a run of duplicates
605 // or just getting out of it, so increase the high-water mark to get
606 // a time that probably hasn't been used before and return it.
607 if (low_water_mark_ <= t && t <= high_water_mark_) {
608 high_water_mark_ += base::TimeDelta::FromMicroseconds(1);
609 return high_water_mark_;
610 }
611
612 // Otherwise, we're clear of the last duplicate run, so reset the
613 // water marks.
614 low_water_mark_ = high_water_mark_ = t;
615 return t;
616}
617
ckitagawa0faa5e42020-06-17 17:30:54618NavigationControllerImpl::ScopedShowRepostDialogForTesting::
619 ScopedShowRepostDialogForTesting()
620 : was_disallowed_(g_check_for_repost) {
621 g_check_for_repost = true;
622}
623
624NavigationControllerImpl::ScopedShowRepostDialogForTesting::
625 ~ScopedShowRepostDialogForTesting() {
626 g_check_for_repost = was_disallowed_;
627}
628
[email protected]d202a7c2012-01-04 07:53:47629NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00630 BrowserContext* browser_context,
631 FrameTree& frame_tree,
632 NavigationControllerDelegate* delegate)
633 : frame_tree_(frame_tree),
634 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57635 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22636 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47637 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37638 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29639}
640
[email protected]d202a7c2012-01-04 07:53:47641NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00642 // The NavigationControllerImpl might be called inside its delegate
643 // destructor. Calling it is not valid anymore.
644 delegate_ = nullptr;
645 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29646}
647
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57648WebContents* NavigationControllerImpl::GetWebContents() {
[email protected]ec6c05f2013-10-23 18:41:57649 return delegate_->GetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32650}
651
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57652BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55653 return browser_context_;
654}
655
[email protected]d202a7c2012-01-04 07:53:47656void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30657 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36658 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57659 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57660 // Verify that this controller is unused and that the input is valid.
Lei Zhang96031532019-10-10 19:05:47661 DCHECK_EQ(0, GetEntryCount());
662 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57663 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14664 selected_navigation < static_cast<int>(entries->size()));
arthursonzogni5c4c202d2017-04-25 23:41:27665 DCHECK_EQ(-1, pending_entry_index_);
[email protected]ce3fa3c2009-04-20 19:55:57666
[email protected]ce3fa3c2009-04-20 19:55:57667 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44668 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03669 entries_.reserve(entries->size());
670 for (auto& entry : *entries)
dcheng36b6aec92015-12-26 06:16:36671 entries_.push_back(
672 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
avif16f85a72015-11-13 18:25:03673
674 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
675 // cleared out safely.
676 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57677
678 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36679 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57680}
681
toyoshim6142d96f2016-12-19 09:07:25682void NavigationControllerImpl::Reload(ReloadType reload_type,
683 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28684 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28685 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32686 int current_index = -1;
687
Carlos IL42b416592019-10-07 23:10:36688 if (entry_replaced_by_post_commit_error_) {
689 // If there is an entry that was replaced by a currently active post-commit
690 // error navigation, this can't be the initial navigation.
691 DCHECK(!IsInitialNavigation());
692 // If the current entry is a post commit error, we reload the entry it
693 // replaced instead. We leave the error entry in place until a commit
694 // replaces it, but the pending entry points to the original entry in the
695 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
696 // case that pending_entry_ != entries_[pending_entry_index_].
697 entry = entry_replaced_by_post_commit_error_.get();
698 current_index = GetCurrentEntryIndex();
699 } else if (IsInitialNavigation() && pending_entry_) {
700 // If we are reloading the initial navigation, just use the current
701 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32702 entry = pending_entry_;
703 // The pending entry might be in entries_ (e.g., after a Clone), so we
704 // should also update the current_index.
705 current_index = pending_entry_index_;
706 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00707 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32708 current_index = GetCurrentEntryIndex();
709 if (current_index != -1) {
creis3da03872015-02-20 21:12:32710 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32711 }
[email protected]979a4bc2013-04-24 01:27:15712 }
[email protected]241db352013-04-22 18:04:05713
[email protected]59167c22013-06-03 18:07:32714 // If we are no where, then we can't reload. TODO(darin): We should add a
715 // CanReload method.
716 if (!entry)
717 return;
718
Takashi Toyoshimac7df3c22019-06-25 14:18:47719 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26720 entry->set_reload_type(reload_type);
721
Aran Gilman37d11632019-10-08 23:07:15722 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30723 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07724 // they really want to do this. If they do, the dialog will call us back
725 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57726 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02727
[email protected]106a0812010-03-18 00:15:12728 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57729 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47730 return;
initial.commit09911bf2008-07-26 23:55:29731 }
Lei Zhang96031532019-10-10 19:05:47732
733 if (!IsInitialNavigation())
734 DiscardNonCommittedEntries();
735
736 pending_entry_ = entry;
737 pending_entry_index_ = current_index;
738 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
739
740 NavigateToExistingPendingEntry(reload_type,
741 FrameTreeNode::kFrameTreeNodeInvalidId);
initial.commit09911bf2008-07-26 23:55:29742}
743
[email protected]d202a7c2012-01-04 07:53:47744void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48745 DCHECK(pending_reload_ != ReloadType::NONE);
746 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12747}
748
[email protected]d202a7c2012-01-04 07:53:47749void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48750 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12751 NOTREACHED();
752 } else {
toyoshim6142d96f2016-12-19 09:07:25753 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48754 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12755 }
756}
757
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57758bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09759 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11760}
761
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57762bool NavigationControllerImpl::IsInitialBlankNavigation() {
creis10a4ab72015-10-13 17:22:40763 // TODO(creis): Once we create a NavigationEntry for the initial blank page,
toyoshim0df1d3a2016-09-09 09:52:48764 // we'll need to check for entry count 1 and restore_type NONE (to exclude
765 // the cloned tab case).
creis10a4ab72015-10-13 17:22:40766 return IsInitialNavigation() && GetEntryCount() == 0;
767}
768
Aran Gilman37d11632019-10-08 23:07:15769NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
770 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58771 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03772 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58773}
774
W. James MacLean1c40862c2020-04-27 21:05:57775void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
776 const url::Origin& origin) {
777 for (int i = 0; i < GetEntryCount(); i++) {
778 auto* entry = GetEntryAtIndex(i);
779 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
780 }
781 if (entry_replaced_by_post_commit_error_) {
782 // It's possible we could come back to this entry if the error
783 // page/interstitial goes away.
784 entry_replaced_by_post_commit_error_
785 ->RegisterExistingOriginToPreventOptInIsolation(origin);
786 }
787 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
788 // visiting pending_entry_, which lacks a committed origin. This will be done
789 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
790}
791
avi25764702015-06-23 15:43:37792void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57793 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00794 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37795 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27796 DCHECK_EQ(-1, pending_entry_index_);
[email protected]8ff00d72012-10-23 19:12:21797 NotificationService::current()->Notify(
Aran Gilman37d11632019-10-08 23:07:15798 NOTIFICATION_NAV_ENTRY_PENDING, Source<NavigationController>(this),
avi25764702015-06-23 15:43:37799 Details<NavigationEntry>(pending_entry_));
[email protected]765b35502008-08-21 00:51:20800}
801
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57802NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47803 if (pending_entry_)
804 return pending_entry_;
805 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20806}
807
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57808NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32809 // The pending entry is safe to return for new (non-history), browser-
810 // initiated navigations. Most renderer-initiated navigations should not
811 // show the pending entry, to prevent URL spoof attacks.
812 //
813 // We make an exception for renderer-initiated navigations in new tabs, as
814 // long as no other page has tried to access the initial empty document in
815 // the new tab. If another page modifies this blank page, a URL spoof is
816 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32817 bool safe_to_show_pending =
818 pending_entry_ &&
819 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09820 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32821 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46822 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32823
824 // Also allow showing the pending entry for history navigations in a new tab,
825 // such as Ctrl+Back. In this case, no existing page is visible and no one
826 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15827 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
828 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32829 safe_to_show_pending = true;
830
831 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19832 return pending_entry_;
833 return GetLastCommittedEntry();
834}
835
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57836int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20837 if (pending_entry_index_ != -1)
838 return pending_entry_index_;
839 return last_committed_entry_index_;
840}
841
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57842NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20843 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28844 return nullptr;
avif16f85a72015-11-13 18:25:03845 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20846}
847
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57848bool NavigationControllerImpl::CanViewSource() {
Carlos Caballeroede6f8c2021-01-28 11:01:50849 const std::string& mime_type = frame_tree_.root()
Alex Moshchuk2e470ea2021-02-03 06:46:34850 ->current_frame_host()
851 ->render_view_host()
Carlos Caballeroede6f8c2021-01-28 11:01:50852 ->contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53853 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
854 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27855 NavigationEntry* visible_entry = GetVisibleEntry();
856 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39857 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16858}
859
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57860int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27861 // The last committed entry index must always be less than the number of
Carlos IL4dea8902020-05-26 15:14:29862 // entries. If there are no entries, it must be -1.
arthursonzogni5c4c202d2017-04-25 23:41:27863 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
864 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55865 return last_committed_entry_index_;
866}
867
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57868int NavigationControllerImpl::GetEntryCount() {
Carlos IL4dea8902020-05-26 15:14:29869 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55870 return static_cast<int>(entries_.size());
871}
872
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57873NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37874 if (index < 0 || index >= GetEntryCount())
875 return nullptr;
876
avif16f85a72015-11-13 18:25:03877 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25878}
879
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57880NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47881 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20882}
883
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57884int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46885 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03886}
887
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57888bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03889 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
890 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
891 return true;
892 }
893 return false;
[email protected]765b35502008-08-21 00:51:20894}
895
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57896bool NavigationControllerImpl::CanGoForward() {
WangHui74286d52021-03-31 16:17:15897 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
898 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
899 return true;
900 }
901 return false;
[email protected]765b35502008-08-21 00:51:20902}
903
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57904bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03905 int index = GetIndexForOffset(offset);
906 return index >= 0 && index < GetEntryCount();
907}
908
WangHui74286d52021-03-31 16:17:15909#if defined(OS_ANDROID)
910bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
WangHui74286d52021-03-31 16:17:15911 if (offset == 0)
912 return true;
913 int increment = offset > 0 ? 1 : -1;
914 int non_skippable_entries = 0;
915 for (int index = GetIndexForOffset(increment);
916 index >= 0 && index < GetEntryCount(); index += increment) {
917 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
918 non_skippable_entries++;
919
920 if (non_skippable_entries == std::abs(offset))
921 return true;
922 }
923 return false;
924}
925#endif
926
[email protected]d202a7c2012-01-04 07:53:47927void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06928 int target_index = GetIndexForOffset(-1);
929
930 // Log metrics for the number of entries that are eligible for skipping on
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42931 // back button and move the target index past the skippable entries.
shivanisha55201872018-12-13 04:29:06932 int count_entries_skipped = 0;
Shivani Sharma298d12852019-01-22 20:04:03933 bool all_skippable_entries = true;
shivanisha55201872018-12-13 04:29:06934 for (int index = target_index; index >= 0; index--) {
Shivani Sharma2d5b4b6b2019-01-08 16:07:16935 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
shivanisha55201872018-12-13 04:29:06936 count_entries_skipped++;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16937 } else {
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42938 target_index = index;
Shivani Sharma298d12852019-01-22 20:04:03939 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06940 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16941 }
shivanisha55201872018-12-13 04:29:06942 }
Miyoung Shin1c565c912021-03-17 12:11:21943
shivanisha55201872018-12-13 04:29:06944 UMA_HISTOGRAM_ENUMERATION("Navigation.BackForward.BackTargetSkipped",
Miyoung Shin1c565c912021-03-17 12:11:21945 count_entries_skipped,
946 blink::kMaxSessionHistoryEntries);
Shivani Sharma298d12852019-01-22 20:04:03947 UMA_HISTOGRAM_BOOLEAN("Navigation.BackForward.AllBackTargetsSkippable",
948 all_skippable_entries);
949
950 // Do nothing if all entries are skippable. Normally this path would not
951 // happen as consumers would have already checked it in CanGoBack but a lot of
952 // tests do not do that.
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42953 if (all_skippable_entries)
Shivani Sharma298d12852019-01-22 20:04:03954 return;
shivanisha55201872018-12-13 04:29:06955
shivanisha55201872018-12-13 04:29:06956 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20957}
958
[email protected]d202a7c2012-01-04 07:53:47959void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06960 int target_index = GetIndexForOffset(1);
961
962 // Log metrics for the number of entries that are eligible for skipping on
Shivani Sharma2d5b4b6b2019-01-08 16:07:16963 // forward button and move the target index past the skippable entries, if
964 // history intervention is enabled.
965 // Note that at least one entry (the last one) will be non-skippable since
966 // entries are marked skippable only when they add another entry because of
967 // redirect or pushState.
shivanisha55201872018-12-13 04:29:06968 int count_entries_skipped = 0;
shivanisha55201872018-12-13 04:29:06969 for (size_t index = target_index; index < entries_.size(); index++) {
Shivani Sharma2d5b4b6b2019-01-08 16:07:16970 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
shivanisha55201872018-12-13 04:29:06971 count_entries_skipped++;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16972 } else {
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42973 target_index = index;
shivanisha55201872018-12-13 04:29:06974 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16975 }
shivanisha55201872018-12-13 04:29:06976 }
977 UMA_HISTOGRAM_ENUMERATION("Navigation.BackForward.ForwardTargetSkipped",
Miyoung Shin1c565c912021-03-17 12:11:21978 count_entries_skipped,
979 blink::kMaxSessionHistoryEntries);
shivanisha55201872018-12-13 04:29:06980
shivanisha55201872018-12-13 04:29:06981 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20982}
983
[email protected]d202a7c2012-01-04 07:53:47984void NavigationControllerImpl::GoToIndex(int index) {
Dave Tapuska8bfd84c2019-03-26 20:47:16985 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId);
986}
987
988void NavigationControllerImpl::GoToIndex(int index,
989 int sandbox_frame_tree_node_id) {
sunjian30574a62017-03-21 21:39:44990 TRACE_EVENT0("browser,navigation,benchmark",
991 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:20992 if (index < 0 || index >= static_cast<int>(entries_.size())) {
993 NOTREACHED();
994 return;
995 }
996
[email protected]cbab76d2008-10-13 22:42:47997 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20998
arthursonzogni5c4c202d2017-04-25 23:41:27999 DCHECK_EQ(nullptr, pending_entry_);
1000 DCHECK_EQ(-1, pending_entry_index_);
1001 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:201002 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:271003 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
1004 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Dave Tapuska8bfd84c2019-03-26 20:47:161005 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id);
[email protected]765b35502008-08-21 00:51:201006}
1007
[email protected]d202a7c2012-01-04 07:53:471008void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:121009 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:031010 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:201011 return;
1012
[email protected]9ba14052012-06-22 23:50:031013 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:201014}
1015
WangHui74286d52021-03-31 16:17:151016#if defined(OS_ANDROID)
1017void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
1018 // Note: This is actually reached in unit tests.
1019 if (!CanGoToOffsetWithSkipping(offset))
1020 return;
1021
Elly Fong-Jonesccc6d1f2021-06-14 18:32:421022 if (offset == 0) {
WangHui74286d52021-03-31 16:17:151023 GoToIndex(GetIndexForOffset(offset));
1024 return;
1025 }
1026 int increment = offset > 0 ? 1 : -1;
1027 // Find the offset without counting skippable entries.
1028 int target_index = GetIndexForOffset(increment);
1029 int non_skippable_entries = 0;
1030 for (int index = target_index; index >= 0 && index < GetEntryCount();
1031 index += increment) {
1032 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1033 non_skippable_entries++;
1034
1035 if (non_skippable_entries == std::abs(offset)) {
1036 target_index = index;
1037 break;
1038 }
1039 }
1040
1041 GoToIndex(target_index);
1042}
1043#endif
1044
[email protected]41374f12013-07-24 02:49:281045bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151046 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281047 return false;
[email protected]6a13a6c2011-12-20 21:47:121048
[email protected]43032342011-03-21 14:10:311049 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281050 return true;
[email protected]cbab76d2008-10-13 22:42:471051}
1052
Michael Thiessen9b14d512019-09-23 21:19:471053void NavigationControllerImpl::PruneForwardEntries() {
1054 DiscardNonCommittedEntries();
1055 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471056 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471057 if (num_removed <= 0)
1058 return;
1059 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1060 NotifyPrunedEntries(this, remove_start_index /* start index */,
1061 num_removed /* count */);
1062}
1063
Aran Gilman37d11632019-10-08 23:07:151064void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1065 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321066 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311067 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511068 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1069 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321070 }
1071}
1072
Harkiran Bolariaba823e42021-05-21 18:30:361073base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL(
1074 const GURL& url,
1075 const Referrer& referrer,
1076 ui::PageTransition transition,
1077 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471078 LoadURLParams params(url);
1079 params.referrer = referrer;
1080 params.transition_type = transition;
1081 params.extra_headers = extra_headers;
Harkiran Bolariaba823e42021-05-21 18:30:361082 return LoadURLWithParams(params);
[email protected]cf002332012-08-14 19:17:471083}
1084
Harkiran Bolariaba823e42021-05-21 18:30:361085base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams(
1086 const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061087 if (params.is_renderer_initiated)
1088 DCHECK(params.initiator_origin.has_value());
1089
naskob8744d22014-08-28 17:07:431090 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151091 "NavigationControllerImpl::LoadURLWithParams", "url",
1092 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291093 bool is_explicit_navigation =
1094 GetContentClient()->browser()->IsExplicitNavigation(
1095 params.transition_type);
1096 if (HandleDebugURL(params.url, params.transition_type,
1097 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431098 // If Telemetry is running, allow the URL load to proceed as if it's
1099 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491100 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431101 cc::switches::kEnableGpuBenchmarking))
Harkiran Bolariaba823e42021-05-21 18:30:361102 return nullptr;
[email protected]47752982014-07-29 08:01:431103 }
[email protected]8bf1048012012-02-08 01:22:181104
[email protected]cf002332012-08-14 19:17:471105 // Checks based on params.load_type.
1106 switch (params.load_type) {
1107 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431108 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471109 break;
1110 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261111 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471112 NOTREACHED() << "Data load must use data scheme.";
Harkiran Bolariaba823e42021-05-21 18:30:361113 return nullptr;
[email protected]cf002332012-08-14 19:17:471114 }
1115 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461116 }
[email protected]e47ae9472011-10-13 19:48:341117
[email protected]e47ae9472011-10-13 19:48:341118 // The user initiated a load, we don't need to reload anymore.
1119 needs_reload_ = false;
1120
Harkiran Bolariaba823e42021-05-21 18:30:361121 return NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441122}
1123
Mohamed Abdelhalim833de902019-09-16 17:41:451124bool NavigationControllerImpl::PendingEntryMatchesRequest(
1125 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191126 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451127 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191128}
1129
[email protected]d202a7c2012-01-04 07:53:471130bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321131 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071132 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331133 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441134 bool is_same_document_navigation,
Shivani Sharmaffb32b82019-04-09 16:58:471135 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131136 NavigationRequest* navigation_request) {
1137 DCHECK(navigation_request);
[email protected]cd2e15742013-03-08 04:08:311138 is_initial_navigation_ = false;
1139
[email protected]0e8db942008-09-24 21:21:481140 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431141 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481142 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361143 if (entry_replaced_by_post_commit_error_) {
1144 if (is_same_document_navigation) {
1145 // Same document navigations should not be possible on error pages and
1146 // would leave the controller in a weird state. Kill the renderer if
1147 // that happens.
1148 bad_message::ReceivedBadMessage(
1149 rfh->GetProcess(), bad_message::NC_SAME_DOCUMENT_POST_COMMIT_ERROR);
1150 }
1151 // Any commit while a post-commit error page is showing should put the
1152 // original entry back, replacing the error page's entry. This includes
1153 // reloads, where the original entry was used as the pending entry and
1154 // should now be at the correct index at commit time.
1155 entries_[last_committed_entry_index_] =
1156 std::move(entry_replaced_by_post_commit_error_);
1157 }
Fergal Daly8e33cf62020-12-12 01:06:071158 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551159 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431160 if (pending_entry_ &&
1161 pending_entry_->GetIsOverridingUserAgent() !=
1162 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1163 overriding_user_agent_changed = true;
[email protected]0e8db942008-09-24 21:21:481164 } else {
Gang Wu325f03f42021-02-25 20:00:461165 // GetLastCommittedEntry() is null, so this is the first entry.
Fergal Daly8e33cf62020-12-12 01:06:071166 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481167 details->previous_entry_index = -1;
Gang Wu325f03f42021-02-25 20:00:461168 if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) {
1169 // Default setting is NOT override the user agent, so overriding the user
1170 // agent in first entry should be considered as user agent changed as
1171 // well.
1172 overriding_user_agent_changed = true;
1173 }
[email protected]0e8db942008-09-24 21:21:481174 }
[email protected]ecd9d8702008-08-28 22:10:171175
Alexander Timind2f2e4f22019-04-02 20:04:531176 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1177 // implementing back-forward cache.
1178
1179 // Create a new metrics object or reuse the previous one depending on whether
1180 // it's a main frame navigation or not.
1181 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics =
1182 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1183 GetLastCommittedEntry(), !rfh->GetParent(),
1184 params.document_sequence_number);
1185 // Notify the last active entry that we have navigated away.
1186 if (!rfh->GetParent() && !is_same_document_navigation) {
1187 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1188 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Hajime Hoshic7606502021-04-14 02:42:161189 metrics->MainFrameDidNavigateAwayFromDocument(rfh, navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531190 }
1191 }
1192 }
1193
fdegans9caf66a2015-07-30 21:10:421194 // If there is a pending entry at this point, it should have a SiteInstance,
1195 // except for restored entries.
jam48cea9082017-02-15 06:13:291196 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481197 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081198 pending_entry_->IsRestored());
1199 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151200 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291201 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481202 }
[email protected]e9ba4472008-09-14 15:42:431203
Nasko Oskovaee2f862018-06-15 00:05:521204 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1205 // has changed, this must be treated as a new navigation with replacement.
1206 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491207 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521208 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101209 pending_entry_->GetUniqueID() ==
1210 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521211 pending_entry_->site_instance() &&
1212 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1213 DCHECK_NE(-1, pending_entry_index_);
1214 // TODO(nasko,creis): Instead of setting this value here, set
1215 // should_replace_current_entry on the parameters we send to the
1216 // renderer process as part of CommitNavigation. The renderer should
1217 // in turn send it back here as part of |params| and it can be just
1218 // enforced and renderer process terminated on mismatch.
1219 details->did_replace_entry = true;
1220 } else {
1221 // The renderer tells us whether the navigation replaces the current entry.
1222 details->did_replace_entry = params.should_replace_current_entry;
1223 }
[email protected]bcd904482012-02-01 01:54:221224
[email protected]e9ba4472008-09-14 15:42:431225 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amnif6950d552020-11-24 03:26:101226 details->type = ClassifyNavigation(rfh, params, navigation_request);
[email protected]4bf3522c2010-08-19 21:00:201227
eugenebutee08663a2017-04-27 17:43:121228 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441229 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121230
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071231 details->is_prerender_activation =
1232 navigation_request->IsPrerenderedPageActivation();
1233
Peter Boströmd7592132019-01-30 04:50:311234 // Make sure we do not discard the pending entry for a different ongoing
1235 // navigation when a same document commit comes in unexpectedly from the
1236 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1237 // other navigation types. See https://crbug.com/900036.
1238 // TODO(crbug.com/926009): Handle history.pushState() as well.
1239 bool keep_pending_entry = is_same_document_navigation &&
Charlie Reisc0f17d2d2021-01-12 18:52:491240 details->type == NAVIGATION_TYPE_EXISTING_ENTRY &&
Peter Boströmd7592132019-01-30 04:50:311241 pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451242 !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311243
[email protected]0e8db942008-09-24 21:21:481244 switch (details->type) {
Charlie Reisc0f17d2d2021-01-12 18:52:491245 case NAVIGATION_TYPE_NEW_ENTRY:
1246 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051247 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451248 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431249 break;
Charlie Reisc0f17d2d2021-01-12 18:52:491250 case NAVIGATION_TYPE_EXISTING_ENTRY:
1251 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1252 was_restored, navigation_request,
1253 keep_pending_entry);
[email protected]e9ba4472008-09-14 15:42:431254 break;
[email protected]8ff00d72012-10-23 19:12:211255 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471256 RendererDidNavigateNewSubframe(
1257 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451258 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431259 break;
[email protected]8ff00d72012-10-23 19:12:211260 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391261 if (!RendererDidNavigateAutoSubframe(
1262 rfh, params, details->is_same_document, navigation_request)) {
creisce0ef3572017-01-26 17:53:081263 // We don't send a notification about auto-subframe PageState during
1264 // UpdateStateForFrame, since it looks like nothing has changed. Send
1265 // it here at commit time instead.
1266 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431267 return false;
creis59d5a47cb2016-08-24 23:57:191268 }
[email protected]e9ba4472008-09-14 15:42:431269 break;
[email protected]8ff00d72012-10-23 19:12:211270 case NAVIGATION_TYPE_NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:491271 // If a pending navigation was in progress, this canceled it. We should
1272 // discard it and make sure it is removed from the URL bar. After that,
1273 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:431274 // the caller that nothing has happened.
arthursonzogni69a6a1b2019-09-17 09:23:001275 if (pending_entry_)
[email protected]20d1c992011-04-12 21:17:491276 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431277 return false;
Aran Gilman37d11632019-10-08 23:07:151278 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431279 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151280 break;
[email protected]765b35502008-08-21 00:51:201281 }
1282
[email protected]688aa65c62012-09-28 04:32:221283 // At this point, we know that the navigation has just completed, so
1284 // record the time.
1285 //
1286 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261287 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331288 base::Time timestamp =
1289 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1290 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131291 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221292
Peter Boströmd7592132019-01-30 04:50:311293 // If we aren't keeping the pending entry, there shouldn't be one at this
1294 // point. Clear it again in case any error cases above forgot to do so.
1295 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1296 // been cleared instead of protecting against it.
1297 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001298 DiscardNonCommittedEntries();
[email protected]f233e4232013-02-23 00:55:141299
[email protected]e9ba4472008-09-14 15:42:431300 // All committed entries should have nonempty content state so WebKit doesn't
1301 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471302 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321303 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221304 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441305 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531306 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1307 // implementing back-forward cache.
1308 if (!active_entry->back_forward_cache_metrics()) {
1309 active_entry->set_back_forward_cache_metrics(
1310 std::move(back_forward_cache_metrics));
1311 }
1312 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
Hajime Hoshi446206e2019-10-18 18:36:251313 navigation_request,
1314 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
naskoc7533512016-05-06 17:01:121315
Charles Reisc0507202017-09-21 00:40:021316 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1317 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1318 // A mismatch can occur if the renderer lies or due to a unique name collision
1319 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121320 FrameNavigationEntry* frame_entry =
1321 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021322 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1323 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031324 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081325 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1326 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031327 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201328
1329 // Remember the bindings the renderer process has at this point, so that
1330 // we do not grant this entry additional bindings if we come back to it.
1331 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301332 }
[email protected]132e281a2012-07-31 18:32:441333
[email protected]97d8f0d2013-10-29 16:49:211334 // Once it is committed, we no longer need to track several pieces of state on
1335 // the entry.
naskoc7533512016-05-06 17:01:121336 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131337
[email protected]49bd30e62011-03-22 20:12:591338 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221339 // TODO(creis): This check won't pass for subframes until we create entries
1340 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001341 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421342 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591343
[email protected]e9ba4472008-09-14 15:42:431344 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001345 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001346 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311347 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531348
arthursonzogni7ddc6542021-04-09 09:16:501349 active_entry->SetIsOverridingUserAgent(
1350 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031351
[email protected]93f230e02011-06-01 14:40:001352 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291353
John Abd-El-Malek380d3c5922017-09-08 00:20:311354 if (active_entry->GetURL().SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121355 !navigation_request->DidEncounterError()) {
John Abd-El-Malek969cdd94e2017-07-10 22:18:161356 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus",
1357 !!active_entry->GetSSL().certificate);
1358 }
1359
aelias100c9192017-01-13 00:01:431360 if (overriding_user_agent_changed)
1361 delegate_->UpdateOverridingUserAgent();
1362
creis03b48002015-11-04 00:54:561363 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1364 // one knows the latest NavigationEntry it is showing (whether it has
1365 // committed anything in this navigation or not). This allows things like
1366 // state and title updates from RenderFrames to apply to the latest relevant
1367 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381368 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001369 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381370 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
[email protected]e9ba4472008-09-14 15:42:431371 return true;
initial.commit09911bf2008-07-26 23:55:291372}
1373
[email protected]8ff00d72012-10-23 19:12:211374NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321375 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101376 const mojom::DidCommitProvisionalLoadParams& params,
1377 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591378 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511379 "ClassifyNavigation");
1380
avi7c6f35e2015-05-08 17:52:381381 if (params.did_create_new_entry) {
Charlie Reisc0f17d2d2021-01-12 18:52:491382 // A new entry. We may or may not have a corresponding pending entry, and
avi7c6f35e2015-05-08 17:52:381383 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001384 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491385 trace_return.set_return_reason("new entry, no parent, new entry");
1386 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381387 }
1388
1389 // When this is a new subframe navigation, we should have a committed page
1390 // in which it's a subframe. This may not be the case when an iframe is
1391 // navigated on a popup navigated to about:blank (the iframe would be
1392 // written into the popup by script on the main page). For these cases,
1393 // there isn't any navigation stuff we can do, so just ignore it.
Nasko Oskovae49e292020-08-13 02:08:511394 if (!GetLastCommittedEntry()) {
1395 trace_return.set_return_reason("new entry, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381396 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511397 }
avi7c6f35e2015-05-08 17:52:381398
1399 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511400 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381401 return NAVIGATION_TYPE_NEW_SUBFRAME;
1402 }
1403
Charlie Reisc0f17d2d2021-01-12 18:52:491404 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381405 DCHECK(!params.history_list_was_cleared);
1406
avi39c1edd32015-06-04 20:06:001407 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381408 // All manual subframes would be did_create_new_entry and handled above, so
1409 // we know this is auto.
Nasko Oskovae49e292020-08-13 02:08:511410 if (GetLastCommittedEntry()) {
1411 trace_return.set_return_reason("subframe, last commmited, auto subframe");
avi7c6f35e2015-05-08 17:52:381412 return NAVIGATION_TYPE_AUTO_SUBFRAME;
Nasko Oskovae49e292020-08-13 02:08:511413 }
Lei Zhang96031532019-10-10 19:05:471414
1415 // We ignore subframes created in non-committed pages; we'd appreciate if
1416 // people stopped doing that.
Nasko Oskovae49e292020-08-13 02:08:511417 trace_return.set_return_reason("subframe, no last commmited, ignore");
Lei Zhang96031532019-10-10 19:05:471418 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381419 }
1420
Rakina Zata Amnif6950d552020-11-24 03:26:101421 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1422 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381423 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1424 // create a new page.
1425
1426 // Just like above in the did_create_new_entry case, it's possible to
1427 // scribble onto an uncommitted page. Again, there isn't any navigation
1428 // stuff that we can do, so ignore it here as well.
avi3a5b8f32015-05-28 17:50:231429 NavigationEntry* last_committed = GetLastCommittedEntry();
Nasko Oskovae49e292020-08-13 02:08:511430 if (!last_committed) {
1431 trace_return.set_return_reason("nav entry 0, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381432 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511433 }
avi7c6f35e2015-05-08 17:52:381434
Charles Reis1378111f2017-11-08 21:44:061435 // This is history.replaceState() or history.reload().
Nasko Oskov332593c2018-08-16 17:21:341436 // TODO(nasko): With error page isolation, reloading an existing session
1437 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491438 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341439 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511440 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491441 "nav entry 0, last committed, existing entry");
1442 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381443 }
1444
Rakina Zata Amnif6950d552020-11-24 03:26:101445 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521446 // If the SiteInstance of the |pending_entry_| does not match the
1447 // SiteInstance that got committed, treat this as a new navigation with
1448 // replacement. This can happen if back/forward/reload encounters a server
1449 // redirect to a different site or an isolated error page gets successfully
1450 // reloaded into a different SiteInstance.
1451 if (pending_entry_->site_instance() &&
1452 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491453 trace_return.set_return_reason("pending matching nav entry, new entry");
1454 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521455 }
creis77c9aa32015-09-25 19:59:421456
Nasko Oskovaee2f862018-06-15 00:05:521457 if (pending_entry_index_ == -1) {
1458 // In this case, we have a pending entry for a load of a new URL but Blink
1459 // didn't do a new navigation (params.did_create_new_entry). First check
1460 // to make sure Blink didn't treat a new cross-process navigation as
1461 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161462 // we must treat it as NEW rather than the converted reload case below,
1463 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521464 if (!GetLastCommittedEntry() ||
1465 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161466 trace_return.set_return_reason("new pending, new entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491467 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521468 }
1469
1470 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161471 // We will create a pending entry, but NavigateWithoutEntry will convert
1472 // it to a reload since it's the same page and not create a new entry for
1473 // it. (The user doesn't want to have a new back/forward entry when they
1474 // do this.) Therefore we want to just ignore the pending entry and go
1475 // back to where we were (the "existing entry").
1476 trace_return.set_return_reason("new pending, existing (same) entry");
1477 return NAVIGATION_TYPE_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521478 }
avi7c6f35e2015-05-08 17:52:381479 }
1480
creis26d22632017-04-21 20:23:561481 // Everything below here is assumed to be an existing entry, but if there is
1482 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511483 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491484 trace_return.set_return_reason("no last committed, new entry");
1485 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511486 }
creis26d22632017-04-21 20:23:561487
avi7c6f35e2015-05-08 17:52:381488 if (params.intended_as_new_entry) {
1489 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491490 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1491 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161492 trace_return.set_return_reason("intended as new entry, existing entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491493 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381494 }
1495
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121496 if (navigation_request->DidEncounterError() &&
1497 failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101498 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381499 // If the renderer was going to a new pending entry that got cleared because
1500 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491501 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381502 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511503 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491504 "unreachable, matching pending, existing entry");
1505 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381506 }
1507
Charlie Reisc0f17d2d2021-01-12 18:52:491508 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101509 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511510 trace_return.traced_value()->SetInteger("existing_entry_index",
1511 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381512 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441513 // The renderer has committed a navigation to an entry that no longer
1514 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491515 trace_return.set_return_reason("existing entry -1, new entry");
1516 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381517 }
1518
avi7c6f35e2015-05-08 17:52:381519 // Since we weeded out "new" navigations above, we know this is an existing
1520 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491521 trace_return.set_return_reason("default return, existing entry");
1522 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381523}
1524
Charlie Reisc0f17d2d2021-01-12 18:52:491525void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321526 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071527 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361528 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441529 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471530 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451531 NavigationRequest* request) {
dcheng9bfa5162016-04-09 01:00:571532 std::unique_ptr<NavigationEntryImpl> new_entry;
creisf49dfc92016-07-26 17:05:181533 bool update_virtual_url = false;
1534
Anton Bikineevf62d1bf2021-05-15 17:56:071535 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451536 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061537
creisf49dfc92016-07-26 17:05:181538 // First check if this is an in-page navigation. If so, clone the current
1539 // entry instead of looking at the pending entry, because the pending entry
1540 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361541 if (is_same_document && GetLastCommittedEntry()) {
Patrick Monette50e8bd82019-06-13 22:40:451542 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481543 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081544 params.document_sequence_number, params.app_history_key,
1545 rfh->GetSiteInstance(), nullptr, params.url,
1546 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091547 Referrer(*params.referrer), initiator_origin,
1548 request->GetRedirectChain(), params.page_state, params.method,
1549 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391550 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481551 request->GetSubresourceWebBundleNavigationInfo(),
Antonio Sartori78a749f2020-11-30 12:03:391552 // We will set the document policies later in this function.
Titouan Rigoudy6ec70402021-02-02 15:42:191553 nullptr /* policy_container_policies */);
Charles Reisf44482022017-10-13 21:15:031554
creisf49dfc92016-07-26 17:05:181555 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Carlos Caballero40b0efd2021-01-26 11:55:001556 frame_entry, true, rfh->frame_tree_node(), frame_tree_.root());
jama78746e2017-02-22 17:21:571557 if (new_entry->GetURL().GetOrigin() != params.url.GetOrigin()) {
1558 // TODO(jam): we had one report of this with a URL that was redirecting to
1559 // only tildes. Until we understand that better, don't copy the cert in
1560 // this case.
1561 new_entry->GetSSL() = SSLStatus();
John Abd-El-Malek9cd697e2017-07-12 21:53:141562
John Abd-El-Malek380d3c5922017-09-08 00:20:311563 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121564 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141565 UMA_HISTOGRAM_BOOLEAN(
1566 "Navigation.SecureSchemeHasSSLStatus.NewPageInPageOriginMismatch",
1567 !!new_entry->GetSSL().certificate);
1568 }
jama78746e2017-02-22 17:21:571569 }
creisf49dfc92016-07-26 17:05:181570
Patrick Monette50e8bd82019-06-13 22:40:451571 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1572 // that |frame_entry| should now have a reference count of exactly 2: one
1573 // due to the local variable |frame_entry|, and another due to |new_entry|
1574 // also retaining one. This should never fail, because it's the main frame.
1575 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181576
1577 update_virtual_url = new_entry->update_virtual_url_with_url();
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("Navigation.SecureSchemeHasSSLStatus.NewPageInPage",
1582 !!new_entry->GetSSL().certificate);
1583 }
creisf49dfc92016-07-26 17:05:181584 }
1585
Harkiran Bolaria59290d62021-03-17 01:53:011586 // If this is an activation navigation from a prerendered page, transfer the
1587 // new entry from an entry already created and stored in the
1588 // NavigationRequest. |new_entry| will not have been set prior to this as
1589 // |is_same_document| is mutually exclusive with
1590 // |IsPrerenderedPageActivation|.
1591 if (request->IsPrerenderedPageActivation()) {
1592 DCHECK(!is_same_document);
1593 DCHECK(!new_entry);
1594 new_entry = request->TakePrerenderNavigationEntry();
1595 DCHECK(new_entry);
1596 }
1597
Charlie Reisc0f17d2d2021-01-12 18:52:491598 // Only make a copy of the pending entry if it is appropriate for the new
1599 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451600 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041601 // 1. The SiteInstance hasn't been assigned to something else.
1602 // 2. The pending entry was intended as a new entry, rather than being a
1603 // history navigation that was interrupted by an unrelated,
1604 // renderer-initiated navigation.
1605 // TODO(csharrison): Investigate whether we can remove some of the coarser
1606 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451607 if (!new_entry && PendingEntryMatchesRequest(request) &&
1608 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341609 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431610 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351611 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431612
[email protected]f1eb87a2011-05-06 17:49:411613 update_virtual_url = new_entry->update_virtual_url_with_url();
Camille Lamy62b826012019-02-26 09:15:471614 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451615 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141616
John Abd-El-Malek380d3c5922017-09-08 00:20:311617 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121618 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141619 UMA_HISTOGRAM_BOOLEAN(
1620 "Navigation.SecureSchemeHasSSLStatus.NewPagePendingEntryMatches",
1621 !!new_entry->GetSSL().certificate);
1622 }
creisf49dfc92016-07-26 17:05:181623 }
1624
Charlie Reisc0f17d2d2021-01-12 18:52:491625 // For cross-document commits with no matching pending entry, create a new
1626 // entry.
creisf49dfc92016-07-26 17:05:181627 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061628 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071629 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1630 initiator_origin,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:581631 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451632 params.transition, request->IsRendererInitiated(),
Lukasz Anforowicz435bcb582019-07-12 20:50:061633 nullptr); // blob_url_loader_factory
[email protected]f8f93eb2012-09-25 03:06:241634
1635 // Find out whether the new entry needs to update its virtual URL on URL
1636 // change and set up the entry accordingly. This is needed to correctly
1637 // update the virtual URL when replaceState is called after a pushState.
1638 GURL url = params.url;
1639 bool needs_update = false;
[email protected]47752982014-07-29 08:01:431640 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1641 &url, browser_context_, &needs_update);
[email protected]f8f93eb2012-09-25 03:06:241642 new_entry->set_update_virtual_url_with_url(needs_update);
1643
Charlie Reisc0f17d2d2021-01-12 18:52:491644 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411645 // update the virtual URL based on the new URL. For example, this is needed
1646 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1647 // the URL.
[email protected]f8f93eb2012-09-25 03:06:241648 update_virtual_url = needs_update;
Camille Lamy62b826012019-02-26 09:15:471649 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451650 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141651
John Abd-El-Malek380d3c5922017-09-08 00:20:311652 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121653 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141654 UMA_HISTOGRAM_BOOLEAN(
1655 "Navigation.SecureSchemeHasSSLStatus.NewPageNoMatchingEntry",
1656 !!new_entry->GetSSL().certificate);
1657 }
[email protected]e9ba4472008-09-14 15:42:431658 }
1659
Harkiran Bolaria59290d62021-03-17 01:53:011660 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
1661 // for prerendered contents, if any. This is because prerendering/activation
1662 // technically won't be creating a new document. Unlike BFCache, prerendering
1663 // creates a new NavigationEntry rather than using an existing one.
1664 if (!request->IsPrerenderedPageActivation()) {
1665 // Don't use the page type from the pending entry. Some interstitial page
1666 // may have set the type to interstitial. Once we commit, however, the page
1667 // type must always be normal or error.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121668 new_entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1669 : PAGE_TYPE_NORMAL);
Harkiran Bolaria59290d62021-03-17 01:53:011670 new_entry->SetURL(params.url);
1671 if (update_virtual_url)
1672 UpdateVirtualURLToURL(new_entry.get(), params.url);
1673 new_entry->SetReferrer(Referrer(*params.referrer));
1674 new_entry->SetTransitionType(params.transition);
1675 new_entry->set_site_instance(
1676 static_cast<SiteInstanceImpl*>(rfh->GetSiteInstance()));
1677 new_entry->SetOriginalRequestURL(request->GetOriginalRequestURL());
Rakina Zata Amnib597d632020-12-01 00:56:001678
Antonio Sartori4f5373792021-05-31 10:56:471679 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
Harkiran Bolaria59290d62021-03-17 01:53:011680 new_entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
[email protected]e9ba4472008-09-14 15:42:431681
Harkiran Bolaria59290d62021-03-17 01:53:011682 // Update the FrameNavigationEntry for new main frame commits.
1683 FrameNavigationEntry* frame_entry =
1684 new_entry->GetFrameEntry(rfh->frame_tree_node());
1685 frame_entry->set_frame_unique_name(rfh->frame_tree_node()->unique_name());
1686 frame_entry->set_item_sequence_number(params.item_sequence_number);
1687 frame_entry->set_document_sequence_number(params.document_sequence_number);
Nate Chapinfbfe5af2021-06-10 17:22:081688 frame_entry->set_app_history_key(params.app_history_key);
Harkiran Bolaria59290d62021-03-17 01:53:011689 frame_entry->set_redirect_chain(request->GetRedirectChain());
1690 frame_entry->SetPageState(params.page_state);
1691 frame_entry->set_method(params.method);
1692 frame_entry->set_post_id(params.post_id);
1693 frame_entry->set_policy_container_policies(
1694 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1695 request));
Antonio Sartori78a749f2020-11-30 12:03:391696
Anton Bikineevf62d1bf2021-05-15 17:56:071697 if (absl::optional<url::Origin> committed_origin =
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121698 GetCommittedOriginForFrameEntry(params, request)) {
1699 if (committed_origin.has_value())
1700 frame_entry->set_committed_origin(committed_origin.value());
1701 }
Harkiran Bolaria59290d62021-03-17 01:53:011702 if (request->web_bundle_navigation_info()) {
1703 frame_entry->set_web_bundle_navigation_info(
1704 request->web_bundle_navigation_info()->Clone());
1705 }
creis8b5cd4c2015-06-19 00:11:081706
Harkiran Bolaria59290d62021-03-17 01:53:011707 // history.pushState() is classified as a navigation to a new page, but sets
1708 // is_same_document to true. In this case, we already have the title and
1709 // favicon available, so set them immediately.
1710 if (is_same_document && GetLastCommittedEntry()) {
1711 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
1712 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1713 }
[email protected]3a868f212014-08-09 10:41:191714 }
[email protected]ff64b3e2014-05-31 04:07:331715
[email protected]60d6cca2013-04-30 08:47:131716 DCHECK(!params.history_list_was_cleared || !replace_entry);
1717 // The browser requested to clear the session history when it initiated the
1718 // navigation. Now we know that the renderer has updated its state accordingly
1719 // and it is safe to also clear the browser side history.
1720 if (params.history_list_was_cleared) {
arthursonzogni69a6a1b2019-09-17 09:23:001721 DiscardNonCommittedEntries();
[email protected]60d6cca2013-04-30 08:47:131722 entries_.clear();
1723 last_committed_entry_index_ = -1;
1724 }
1725
Nasko Oskovaee2f862018-06-15 00:05:521726 // If this is a new navigation with replacement and there is a
1727 // pending_entry_ which matches the navigation reported by the renderer
1728 // process, then it should be the one replaced, so update the
1729 // last_committed_entry_index_ to use it.
1730 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101731 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521732 last_committed_entry_index_ = pending_entry_index_;
1733 }
1734
Alexander Timine3ec4192020-04-20 16:39:401735 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411736 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401737 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051738
Carlos IL42b416592019-10-07 23:10:361739 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
1740 !request->post_commit_error_page_html().empty());
[email protected]e9ba4472008-09-14 15:42:431741}
1742
Charlie Reisc0f17d2d2021-01-12 18:52:491743void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321744 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071745 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361746 bool is_same_document,
jam48cea9082017-02-15 06:13:291747 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451748 NavigationRequest* request,
Peter Boströmd7592132019-01-30 04:50:311749 bool keep_pending_entry) {
creis26d22632017-04-21 20:23:561750 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1751 << "that a last committed entry exists.";
1752
[email protected]e9ba4472008-09-14 15:42:431753 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001754 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431755
Charlie Reis7e2cb6d2021-01-26 01:27:161756 NavigationEntryImpl* entry = nullptr;
avicbdc4c12015-07-01 16:07:111757 if (params.intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:161758 // We're guaranteed to have a last committed entry if intended_as_new_entry
1759 // is true.
avicbdc4c12015-07-01 16:07:111760 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:161761 DCHECK(entry);
1762
1763 // If the NavigationRequest matches a new pending entry and is classified as
1764 // EXISTING_ENTRY, then it is a navigation to the same URL that was
1765 // converted to a reload, such as a user pressing enter in the omnibox.
1766 if (pending_entry_ && pending_entry_index_ == -1 &&
1767 pending_entry_->GetUniqueID() ==
1768 request->commit_params().nav_entry_id) {
1769 // Note: The pending entry will usually have a real ReloadType here, but
1770 // it can still be ReloadType::NONE in cases that
1771 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
1772
1773 // If we classified this correctly, the SiteInstance should not have
1774 // changed.
1775 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
1776
1777 // For converted reloads, we assign the entry's unique ID to be that of
1778 // the new one. Since this is always the result of a user action, we want
1779 // to dismiss infobars, etc. like a regular user-initiated navigation.
1780 entry->set_unique_id(pending_entry_->GetUniqueID());
1781
1782 // The extra headers may have changed due to reloading with different
1783 // headers.
1784 entry->set_extra_headers(pending_entry_->extra_headers());
1785 }
1786 // Otherwise, this was intended as a new entry but the pending entry was
1787 // lost in the meantime and no new entry was created. We are stuck at the
1788 // last committed entry.
1789
1790 // Even if this is a converted reload from pressing enter in the omnibox,
1791 // the server could redirect, requiring an update to the SSL status. If this
1792 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451793 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:161794 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:471795 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451796 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:161797 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141798
Charlie Reis7e2cb6d2021-01-26 01:27:161799 if (params.url.SchemeIs(url::kHttpsScheme) &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121800 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141801 bool has_cert = !!entry->GetSSL().certificate;
1802 if (is_same_document) {
1803 UMA_HISTOGRAM_BOOLEAN(
1804 "Navigation.SecureSchemeHasSSLStatus."
1805 "ExistingPageSameDocumentIntendedAsNew",
1806 has_cert);
1807 } else {
1808 UMA_HISTOGRAM_BOOLEAN(
1809 "Navigation.SecureSchemeHasSSLStatus."
1810 "ExistingPageDifferentDocumentIntendedAsNew",
1811 has_cert);
1812 }
1813 }
Rakina Zata Amnif6950d552020-11-24 03:26:101814 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:111815 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:101816 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b90ce2016-09-01 16:58:161817
eugenebut604866f2017-05-10 21:35:361818 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451819 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361820 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1821 // this was a restored same document navigation entry, then it won't have
1822 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1823 // navigated it.
jam48cea9082017-02-15 06:13:291824 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
1825 if (entry->GetURL().GetOrigin() == last_entry->GetURL().GetOrigin() &&
1826 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1827 was_restored) {
1828 entry->GetSSL() = last_entry->GetSSL();
1829 }
1830 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451831 // In rapid back/forward navigations |request| sometimes won't have a cert
1832 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191833 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451834 if (request->GetSSLInfo().has_value() &&
1835 request->GetSSLInfo()->is_valid()) {
1836 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
1837 } else if (entry->GetURL().GetOrigin() != request->GetURL().GetOrigin()) {
John Abd-El-Malek3f247082017-12-07 19:02:191838 entry->GetSSL() = SSLStatus();
1839 }
jam48cea9082017-02-15 06:13:291840 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141841
John Abd-El-Malek380d3c5922017-09-08 00:20:311842 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121843 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141844 bool has_cert = !!entry->GetSSL().certificate;
1845 if (is_same_document && was_restored) {
1846 UMA_HISTOGRAM_BOOLEAN(
1847 "Navigation.SecureSchemeHasSSLStatus."
1848 "ExistingPageSameDocumentRestoredBrowserInitiated",
1849 has_cert);
1850 } else if (is_same_document && !was_restored) {
1851 UMA_HISTOGRAM_BOOLEAN(
1852 "Navigation.SecureSchemeHasSSLStatus."
1853 "ExistingPageSameDocumentBrowserInitiated",
1854 has_cert);
1855 } else if (!is_same_document && was_restored) {
1856 UMA_HISTOGRAM_BOOLEAN(
1857 "Navigation.SecureSchemeHasSSLStatus."
1858 "ExistingPageRestoredBrowserInitiated",
1859 has_cert);
1860 } else {
1861 UMA_HISTOGRAM_BOOLEAN(
1862 "Navigation.SecureSchemeHasSSLStatus.ExistingPageBrowserInitiated",
1863 has_cert);
1864 }
1865 }
avicbdc4c12015-07-01 16:07:111866 } else {
1867 // This is renderer-initiated. The only kinds of renderer-initated
Charlie Reisc0f17d2d2021-01-12 18:52:491868 // navigations that are EXISTING_ENTRY are reloads and history.replaceState,
avicbdc4c12015-07-01 16:07:111869 // which land us at the last committed entry.
1870 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101871
Mikel Astizba9cf2fd2017-12-17 10:38:101872 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1873 // to avoid misleading interpretations (e.g. URLs paired with
1874 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1875 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1876 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1877
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571878 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:101879
eugenebut604866f2017-05-10 21:35:361880 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451881 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361882 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471883 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451884 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141885
John Abd-El-Malek380d3c5922017-09-08 00:20:311886 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121887 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141888 bool has_cert = !!entry->GetSSL().certificate;
1889 if (is_same_document) {
1890 UMA_HISTOGRAM_BOOLEAN(
1891 "Navigation.SecureSchemeHasSSLStatus."
1892 "ExistingPageSameDocumentRendererInitiated",
1893 has_cert);
1894 } else {
1895 UMA_HISTOGRAM_BOOLEAN(
1896 "Navigation.SecureSchemeHasSSLStatus."
1897 "ExistingPageDifferentDocumentRendererInitiated",
1898 has_cert);
1899 }
1900 }
avicbdc4c12015-07-01 16:07:111901 }
1902 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:431903
[email protected]5ccd4dc2012-10-24 02:28:141904 // The URL may have changed due to redirects.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121905 entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1906 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041907 entry->SetURL(params.url);
arthursonzogni73fe3212020-11-17 13:24:071908 entry->SetReferrer(Referrer(*params.referrer));
[email protected]38178a42009-12-17 18:58:321909 if (entry->update_virtual_url_with_url())
1910 UpdateVirtualURLToURL(entry, params.url);
[email protected]5ccd4dc2012-10-24 02:28:141911
jam015ba062017-01-06 21:17:001912 // The site instance will normally be the same except
1913 // 1) session restore, when no site instance will be assigned or
1914 // 2) redirect, when the site instance is reset.
Lei Zhang96031532019-10-10 19:05:471915 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
naskoaf182192016-08-11 02:12:011916 entry->site_instance() == rfh->GetSiteInstance());
clamy432acb22016-04-15 19:41:431917
naskoaf182192016-08-11 02:12:011918 // Update the existing FrameNavigationEntry to ensure all of its members
1919 // reflect the parameters coming from the renderer process.
Anton Bikineevf62d1bf2021-05-15 17:56:071920 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451921 request->common_params().initiator_origin;
naskoaf182192016-08-11 02:12:011922 entry->AddOrUpdateFrameEntry(
Nate Chapin9f169072021-06-09 19:32:371923 rfh->frame_tree_node(), NavigationEntryImpl::UpdatePolicy::kUpdate,
1924 params.item_sequence_number, params.document_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081925 params.app_history_key, rfh->GetSiteInstance(), nullptr, params.url,
Nate Chapin9f169072021-06-09 19:32:371926 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091927 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071928 params.page_state, params.method, params.post_id,
1929 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031930 request->web_bundle_navigation_info()
1931 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391932 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481933 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191934 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1935 request));
creis22a7b4c2016-04-28 07:20:301936
[email protected]5ccd4dc2012-10-24 02:28:141937 // The redirected to page should not inherit the favicon from the previous
1938 // page.
eugenebut604866f2017-05-10 21:35:361939 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:481940 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:141941
Peter Boströmd7592132019-01-30 04:50:311942 // We should also usually discard the pending entry if it corresponds to a
1943 // different navigation, since that one is now likely canceled. In rare
1944 // cases, we leave the pending entry for another navigation in place when we
1945 // know it is still ongoing, to avoid a flicker in the omnibox (see
1946 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:431947 //
1948 // Note that we need to use the "internal" version since we don't want to
1949 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:311950 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001951 DiscardNonCommittedEntries();
[email protected]40bcc302009-03-02 20:50:391952
Carlos IL4dea8902020-05-26 15:14:291953 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:111954 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:431955}
1956
[email protected]d202a7c2012-01-04 07:53:471957void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:321958 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071959 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361960 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:471961 bool replace_entry,
1962 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451963 NavigationRequest* request) {
avi25f5f9e2015-07-17 20:08:261964 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1965 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
[email protected]09b8f82f2009-06-16 20:22:111966
[email protected]e9ba4472008-09-14 15:42:431967 // Manual subframe navigations just get the current entry cloned so the user
1968 // can go back or forward to it. The actual subframe information will be
1969 // stored in the page state for each of those entries. This happens out of
1970 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:091971 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1972 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:381973
Mikel Astizba9cf2fd2017-12-17 10:38:101974 // The DCHECK below documents the fact that we don't know of any situation
1975 // where |replace_entry| is true for subframe navigations. This simplifies
1976 // reasoning about the replacement struct for subframes (see
1977 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
1978 DCHECK(!replace_entry);
1979
Patrick Monette50e8bd82019-06-13 22:40:451980 // This FrameNavigationEntry might not end up being used in the
1981 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Anton Bikineevf62d1bf2021-05-15 17:56:071982 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451983 request->common_params().initiator_origin;
Patrick Monette50e8bd82019-06-13 22:40:451984 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481985 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081986 params.document_sequence_number, params.app_history_key,
1987 rfh->GetSiteInstance(), nullptr, params.url,
1988 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091989 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071990 params.page_state, params.method, params.post_id,
1991 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031992 request->web_bundle_navigation_info()
1993 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391994 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481995 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191996 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1997 request));
Charles Reisf44482022017-10-13 21:15:031998
creisce0ef3572017-01-26 17:53:081999 std::unique_ptr<NavigationEntryImpl> new_entry =
2000 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:452001 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:002002 frame_tree_.root());
creise062d542015-08-25 02:01:552003
Alexander Timine3ec4192020-04-20 16:39:402004 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:412005 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:402006 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:472007
creisce0ef3572017-01-26 17:53:082008 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:452009 // to replace, which can happen due to a unique name change. See
2010 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
2011 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:382012
Carlos IL42b416592019-10-07 23:10:362013 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false);
[email protected]e9ba4472008-09-14 15:42:432014}
2015
[email protected]d202a7c2012-01-04 07:53:472016bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:322017 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072018 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392019 bool is_same_document,
Mohamed Abdelhalim833de902019-09-16 17:41:452020 NavigationRequest* request) {
avi9f07a0c2015-02-18 22:51:292021 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2022 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2023
[email protected]e9ba4472008-09-14 15:42:432024 // We're guaranteed to have a previously committed entry, and we now need to
2025 // handle navigation inside of a subframe in it without creating a new entry.
2026 DCHECK(GetLastCommittedEntry());
2027
creis913c63ce2016-07-16 19:52:522028 // For newly created subframes, we don't need to send a commit notification.
2029 // This is only necessary for history navigations in subframes.
2030 bool send_commit_notification = false;
2031
Rakina Zata Amnif6950d552020-11-24 03:26:102032 // If |nav_entry_id| is non-zero and matches an existing entry, this
2033 // is a history navigation. Update the last committed index accordingly. If
2034 // we don't recognize the |nav_entry_id|, it might be a recently
2035 // pruned entry. We'll handle it below.
2036 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2037 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472038 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062039 // Make sure that a subframe commit isn't changing the main frame's
2040 // origin. Otherwise the renderer process may be confused, leading to a
2041 // URL spoof. We can't check the path since that may change
2042 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572043 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2044 // about:blank, file, and unique origins are more subtle to get right.
Charlie Reis95fbca442021-05-21 21:38:242045 // We should use checks similar to RenderFrameHostImpl's
2046 // CanCommitUrlAndOrigin on the main frame during subframe commits.
2047 // See https://crbug.com/1209092.
creis37988b92016-06-10 18:03:572048 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2049 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2050 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2051 dest_top_url.SchemeIsHTTPOrHTTPS() &&
2052 current_top_url.GetOrigin() != dest_top_url.GetOrigin()) {
creisfb6eeb62016-05-10 19:01:512053 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2054 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062055 }
creis3cdc3b02015-05-29 23:00:472056
creis913c63ce2016-07-16 19:52:522057 // We only need to discard the pending entry in this history navigation
2058 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062059 last_committed_entry_index_ = entry_index;
arthursonzogni69a6a1b2019-09-17 09:23:002060 DiscardNonCommittedEntries();
creis913c63ce2016-07-16 19:52:522061
2062 // History navigations should send a commit notification.
2063 send_commit_notification = true;
avi98405c22015-05-21 20:47:062064 }
[email protected]e9ba4472008-09-14 15:42:432065 }
[email protected]f233e4232013-02-23 00:55:142066
creisce0ef3572017-01-26 17:53:082067 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2068 // it may be a "history auto" case where we update an existing one.
2069 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Nate Chapin9f169072021-06-09 19:32:372070
2071 // We may want to update |last_committed|'s FrameNavigationEntry (if one
2072 // exists), or we may want to clobber it and create a new one. We update in
2073 // cases where the corresponding FrameNavigationEntry is conceptually similar
2074 // to the document described by the commit params: same-document
2075 // navigations, history traversal to an existing entry, and reloads (including
2076 // a "soft reload" where we navigate to the same url without flagging it as a
2077 // reload). But in the case of a different document that is not logically
2078 // related to the committed FrameNavigationEntry's document (cross-document,
2079 // not same url, not a reload, not a history traversal), we replace rather
2080 // than update.
2081 // In the case where we update, the FrameNavigationEntry will potentially be
2082 // shared across multiple NavigationEntries, and any updates will be reflected
2083 // in all of those NavigationEntries. In the replace case, any existing
2084 // sharing with other NavigationEntries will stop.
2085 FrameNavigationEntry* last_committed_frame_entry =
2086 last_committed->GetFrameEntry(rfh->frame_tree_node());
2087 NavigationEntryImpl::UpdatePolicy update_policy =
2088 NavigationEntryImpl::UpdatePolicy::kUpdate;
2089 if (request->common_params().navigation_type ==
2090 mojom::NavigationType::DIFFERENT_DOCUMENT &&
2091 last_committed_frame_entry &&
2092 last_committed_frame_entry->url() != params.url) {
2093 update_policy = NavigationEntryImpl::UpdatePolicy::kReplace;
2094 }
2095
Anton Bikineevf62d1bf2021-05-15 17:56:072096 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452097 request->common_params().initiator_origin;
creisce0ef3572017-01-26 17:53:082098 last_committed->AddOrUpdateFrameEntry(
Nate Chapin9f169072021-06-09 19:32:372099 rfh->frame_tree_node(), update_policy, params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:082100 params.document_sequence_number, params.app_history_key,
2101 rfh->GetSiteInstance(), nullptr, params.url,
2102 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:092103 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072104 params.page_state, params.method, params.post_id,
2105 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032106 request->web_bundle_navigation_info()
2107 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392108 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482109 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:192110 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
2111 request));
creis625a0c7d2015-03-24 23:17:122112
creis913c63ce2016-07-16 19:52:522113 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432114}
2115
[email protected]d202a7c2012-01-04 07:53:472116int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232117 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032118 for (size_t i = 0; i < entries_.size(); ++i) {
2119 if (entries_[i].get() == entry)
2120 return i;
2121 }
2122 return -1;
[email protected]765b35502008-08-21 00:51:202123}
2124
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572125void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242126 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572127 NavigationControllerImpl* source =
2128 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572129 // Verify that we look new.
Lei Zhang96031532019-10-10 19:05:472130 DCHECK_EQ(0, GetEntryCount());
2131 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:572132
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572133 if (source->GetEntryCount() == 0)
[email protected]ce3fa3c2009-04-20 19:55:572134 return; // Nothing new to do.
2135
Francois Dorayeaace782017-06-21 16:37:242136 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442137 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572138 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572139
Fergal Dalya1d569972021-03-16 03:24:532140 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302141 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532142 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2143 session_storage_namespace_map_[it.first] = source_namespace->Clone();
Aaron Colwellb731a0ae2021-03-19 19:14:472144 OnStoragePartitionIdAdded(it.first);
[email protected]fdac6ade2013-07-20 01:06:302145 }
[email protected]4e6419c2010-01-15 04:50:342146
Lukasz Anforowicz0de0f452020-12-02 19:57:152147 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572148}
2149
Aran Gilman37d11632019-10-08 23:07:152150void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2151 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162152 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012153 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162154
[email protected]d202a7c2012-01-04 07:53:472155 NavigationControllerImpl* source =
2156 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252157
avi2b177592014-12-10 02:08:022158 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012159 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252160
[email protected]474f8512013-05-31 22:31:162161 // We now have one entry, possibly with a new pending entry. Ensure that
2162 // adding the entries from source won't put us over the limit.
2163 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572164 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572165 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252166
Carlos IL4dea8902020-05-26 15:14:292167 // Insert the entries from source. Ignore any pending entry, since it has not
2168 // committed in source.
[email protected]474f8512013-05-31 22:31:162169 int max_source_index = source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:252170 if (max_source_index == -1)
[email protected]a26023822011-12-29 00:23:552171 max_source_index = source->GetEntryCount();
[email protected]e1cd5452010-08-26 18:03:252172 else
2173 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572174
2175 // Ignore the source's current entry if merging with replacement.
2176 // TODO(davidben): This should preserve entries forward of the current
2177 // too. http://crbug.com/317872
2178 if (replace_entry && max_source_index > 0)
2179 max_source_index--;
2180
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572181 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252182
2183 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552184 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142185
Carlos Caballeroede6f8c2021-01-28 11:01:502186 SetHistoryOffsetAndLength(last_committed_entry_index_, GetEntryCount());
[email protected]e1cd5452010-08-26 18:03:252187}
2188
[email protected]79368982013-11-13 01:11:012189bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162190 // If there is no last committed entry, we cannot prune. Even if there is a
2191 // pending entry, it may not commit, leaving this WebContents blank, despite
2192 // possibly giving it new entries via CopyStateFromAndPrune.
2193 if (last_committed_entry_index_ == -1)
2194 return false;
[email protected]9350602e2013-02-26 23:27:442195
[email protected]474f8512013-05-31 22:31:162196 // We cannot prune if there is a pending entry at an existing entry index.
2197 // It may not commit, so we have to keep the last committed entry, and thus
2198 // there is no sensible place to keep the pending entry. It is ok to have
2199 // a new pending entry, which can optionally commit as a new navigation.
2200 if (pending_entry_index_ != -1)
2201 return false;
2202
[email protected]474f8512013-05-31 22:31:162203 return true;
2204}
2205
[email protected]79368982013-11-13 01:11:012206void NavigationControllerImpl::PruneAllButLastCommitted() {
2207 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162208
avi2b177592014-12-10 02:08:022209 DCHECK_EQ(0, last_committed_entry_index_);
2210 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442211
Carlos Caballeroede6f8c2021-01-28 11:01:502212 SetHistoryOffsetAndLength(last_committed_entry_index_, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442213}
2214
[email protected]79368982013-11-13 01:11:012215void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162216 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012217 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262218
[email protected]474f8512013-05-31 22:31:162219 // Erase all entries but the last committed entry. There may still be a
2220 // new pending entry after this.
2221 entries_.erase(entries_.begin(),
2222 entries_.begin() + last_committed_entry_index_);
2223 entries_.erase(entries_.begin() + 1, entries_.end());
2224 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262225}
2226
Christian Dullweber1af31e62018-02-22 11:49:482227void NavigationControllerImpl::DeleteNavigationEntries(
2228 const DeletionPredicate& deletionPredicate) {
2229 // It is up to callers to check the invariants before calling this.
2230 CHECK(CanPruneAllButLastCommitted());
2231 std::vector<int> delete_indices;
2232 for (size_t i = 0; i < entries_.size(); i++) {
2233 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572234 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482235 delete_indices.push_back(i);
2236 }
2237 }
2238 if (delete_indices.empty())
2239 return;
2240
2241 if (delete_indices.size() == GetEntryCount() - 1U) {
2242 PruneAllButLastCommitted();
2243 } else {
2244 // Do the deletion in reverse to preserve indices.
2245 for (auto it = delete_indices.rbegin(); it != delete_indices.rend(); ++it) {
2246 RemoveEntryAtIndex(*it);
2247 }
Carlos Caballeroede6f8c2021-01-28 11:01:502248 SetHistoryOffsetAndLength(last_committed_entry_index_, GetEntryCount());
Christian Dullweber1af31e62018-02-22 11:49:482249 }
2250 delegate()->NotifyNavigationEntriesDeleted();
2251}
2252
Shivani Sharma883f5f32019-02-12 18:20:012253bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2254 auto* entry = GetEntryAtIndex(index);
2255 return entry && entry->should_skip_on_back_forward_ui();
2256}
2257
Carlos Caballero35ce710c2019-09-19 10:59:452258BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2259 return back_forward_cache_;
2260}
2261
clamy987a3752018-05-03 17:36:262262void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2263 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2264 // progress, since this will cause a use-after-free. (We only allow this
2265 // when the tab is being destroyed for shutdown, since it won't return to
2266 // NavigateToEntry in that case.) http://crbug.com/347742.
2267 CHECK(!in_navigate_to_pending_entry_ || delegate_->IsBeingDestroyed());
2268
2269 if (was_failure && pending_entry_) {
2270 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2271 } else {
2272 failed_pending_entry_id_ = 0;
2273 }
2274
2275 if (pending_entry_) {
2276 if (pending_entry_index_ == -1)
2277 delete pending_entry_;
2278 pending_entry_index_ = -1;
2279 pending_entry_ = nullptr;
2280 }
arthursonzogni66f711c2019-10-08 14:40:362281
2282 // Ensure any refs to the current pending entry are ignored if they get
2283 // deleted, by clearing the set of known refs. All future pending entries will
2284 // only be affected by new refs.
2285 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262286}
2287
2288void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2289 if (pending_entry_)
2290 pending_entry_->set_ssl_error(error);
2291}
2292
Camille Lamy5193caa2018-10-12 11:59:422293#if defined(OS_ANDROID)
2294// static
2295bool NavigationControllerImpl::ValidateDataURLAsString(
2296 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2297 if (!data_url_as_string)
2298 return false;
2299
2300 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2301 return false;
2302
2303 // The number of characters that is enough for validating a data: URI.
2304 // From the GURL's POV, the only important part here is scheme, it doesn't
2305 // check the actual content. Thus we can take only the prefix of the url, to
2306 // avoid unneeded copying of a potentially long string.
2307 const size_t kDataUriPrefixMaxLen = 64;
2308 GURL data_url(
2309 std::string(data_url_as_string->front_as<char>(),
2310 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2311 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2312 return false;
2313
2314 return true;
2315}
2316#endif
2317
Shivani Sharma194877032019-03-07 17:52:472318void NavigationControllerImpl::NotifyUserActivation() {
2319 // When a user activation occurs, ensure that all adjacent entries for the
2320 // same document clear their skippable bit, so that the history manipulation
2321 // intervention does not apply to them.
Shivani Sharma194877032019-03-07 17:52:472322 auto* last_committed_entry = GetLastCommittedEntry();
2323 if (!last_committed_entry)
2324 return;
Shivani Sharma194877032019-03-07 17:52:472325
shivanigithub99368382021-06-16 18:33:372326 if (base::FeatureList::IsEnabled(
2327 features::kDebugHistoryInterventionNoUserActivation)) {
2328 return;
2329 }
2330
Shivani Sharmac4cc8922019-04-18 03:11:172331 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472332}
2333
clamy987a3752018-05-03 17:36:262334bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2335 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172336 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262337 NavigationEntryImpl* entry =
2338 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2339 if (!entry)
2340 return false;
2341
2342 FrameNavigationEntry* frame_entry =
2343 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2344 if (!frame_entry)
2345 return false;
2346
Camille Lamy5193caa2018-10-12 11:59:422347 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572348 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232349 ReloadType::NONE, false /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422350 true /* is_history_navigation_in_new_child */);
clamyea99ea12018-05-28 13:54:232351
2352 if (!request)
2353 return false;
2354
arthursonzognif046d4a2019-12-12 19:08:102355 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412356
Lukasz Anforowicz9ee83c272020-12-01 20:14:052357 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2358 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232359
2360 return true;
clamy987a3752018-05-03 17:36:262361}
2362
Tsuyoshi Horo52fd08e2020-07-07 07:03:452363bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2364 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2365 if (!entry)
2366 return false;
2367 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2368 if (!frame_entry)
2369 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142370 ReloadType reload_type = ReloadType::NORMAL;
2371 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452372 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142373 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452374 false /* is_same_document_history_load */,
2375 false /* is_history_navigation_in_new_child */);
2376 if (!request)
2377 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052378 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452379 return true;
2380}
2381
Dave Tapuska8bfd84c2019-03-26 20:47:162382void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2383 int offset,
2384 int sandbox_frame_tree_node_id) {
2385 if (!CanGoToOffset(offset))
2386 return;
2387 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id);
2388}
2389
clamy987a3752018-05-03 17:36:262390void NavigationControllerImpl::NavigateFromFrameProxy(
2391 RenderFrameHostImpl* render_frame_host,
2392 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022393 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452394 int initiator_process_id,
Anton Bikineevf62d1bf2021-05-15 17:56:072395 const absl::optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262396 bool is_renderer_initiated,
2397 SiteInstance* source_site_instance,
2398 const Referrer& referrer,
2399 ui::PageTransition page_transition,
2400 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252401 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262402 const std::string& method,
2403 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092404 const std::string& extra_headers,
Antonio Sartori2f763d9d2021-04-21 10:04:142405 network::mojom::SourceLocationPtr source_location,
John Delaney50425f82020-04-07 16:26:212406 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Anton Bikineevf62d1bf2021-05-15 17:56:072407 const absl::optional<blink::Impression>& impression) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582408 if (is_renderer_initiated)
2409 DCHECK(initiator_origin.has_value());
2410
clamy987a3752018-05-03 17:36:262411 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582412
Rakina Zata Amni66992a82020-06-24 03:57:522413 // Don't allow an entry replacement if there is no entry to replace.
2414 // http://crbug.com/457149
2415 if (GetEntryCount() == 0)
2416 should_replace_current_entry = false;
2417
clamy987a3752018-05-03 17:36:262418 // Create a NavigationEntry for the transfer, without making it the pending
2419 // entry. Subframe transfers should have a clone of the last committed entry
2420 // with a FrameNavigationEntry for the target frame. Main frame transfers
2421 // should have a new NavigationEntry.
2422 // TODO(creis): Make this unnecessary by creating (and validating) the params
2423 // directly, passing them to the destination RenderFrameHost. See
2424 // https://crbug.com/536906.
2425 std::unique_ptr<NavigationEntryImpl> entry;
2426 if (!node->IsMainFrame()) {
2427 // Subframe case: create FrameNavigationEntry.
2428 if (GetLastCommittedEntry()) {
2429 entry = GetLastCommittedEntry()->Clone();
2430 entry->set_extra_headers(extra_headers);
2431 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2432 // Renderer-initiated navigation that target a remote frame are currently
2433 // classified as browser-initiated when this one has already navigated.
2434 // See https://crbug.com/722251.
2435 } else {
2436 // If there's no last committed entry, create an entry for about:blank
2437 // with a subframe entry for our destination.
2438 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2439 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:062440 GURL(url::kAboutBlankURL), referrer, initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:102441 source_site_instance, page_transition, is_renderer_initiated,
2442 extra_headers, browser_context_,
Scott Violetcf6ea7e2021-06-09 21:09:212443 nullptr /* blob_url_loader_factory */, should_replace_current_entry));
clamy987a3752018-05-03 17:36:262444 }
Nate Chapin9f169072021-06-09 19:32:372445 // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter
2446 // to CreateNavigationRequestFromLoadParams(), so while kReplace might
2447 // remove child FrameNavigationEntries (e.g., if this is a cross-process
2448 // same-document navigation), they will still be present in the
2449 // committed NavigationEntry that will be referenced to construct the new
2450 // FrameNavigationEntry tree when this navigation commits.
clamy987a3752018-05-03 17:36:262451 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:082452 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582453 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072454 absl::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202455 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392456 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482457 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori79d549d2021-02-18 12:59:542458 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262459 } else {
2460 // Main frame case.
2461 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102462 url, referrer, initiator_origin, source_site_instance, page_transition,
2463 is_renderer_initiated, extra_headers, browser_context_,
Scott Violetcf6ea7e2021-06-09 21:09:212464 blob_url_loader_factory, should_replace_current_entry));
clamy987a3752018-05-03 17:36:262465 entry->root_node()->frame_entry->set_source_site_instance(
2466 static_cast<SiteInstanceImpl*>(source_site_instance));
2467 entry->root_node()->frame_entry->set_method(method);
2468 }
clamy987a3752018-05-03 17:36:262469
Camille Lamy5193caa2018-10-12 11:59:422470 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262471 if (GetLastCommittedEntry() &&
2472 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2473 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422474 override_user_agent = true;
clamy987a3752018-05-03 17:36:262475 }
2476 // TODO(creis): Set user gesture and intent received timestamp on Android.
2477
2478 // We may not have successfully added the FrameNavigationEntry to |entry|
2479 // above (per https://crbug.com/608402), in which case we create it from
2480 // scratch. This works because we do not depend on |frame_entry| being inside
2481 // |entry| during NavigateToEntry. This will go away when we shortcut this
2482 // further in https://crbug.com/536906.
2483 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2484 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452485 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Nate Chapinfbfe5af2021-06-10 17:22:082486 node->unique_name(), -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582487 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072488 absl::nullopt /* origin */, referrer, initiator_origin,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:122489 std::vector<GURL>(), blink::PageState(), method, -1,
2490 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482491 nullptr /* subresource_web_bundle_navigation_info */,
Titouan Rigoudy6ec70402021-02-02 15:42:192492 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262493 }
2494
Camille Lamy5193caa2018-10-12 11:59:422495 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022496 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452497 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292498 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422499 params.source_site_instance = source_site_instance;
2500 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2501 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032502 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422503 params.referrer = referrer;
2504 /* params.redirect_chain: skip */
2505 params.extra_headers = extra_headers;
2506 params.is_renderer_initiated = is_renderer_initiated;
2507 params.override_user_agent = UA_OVERRIDE_INHERIT;
2508 /* params.base_url_for_data_url: skip */
2509 /* params.virtual_url_for_data_url: skip */
2510 /* params.data_url_as_string: skip */
2511 params.post_data = post_body;
2512 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582513 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422514 /* params.frame_name: skip */
2515 // TODO(clamy): See if user gesture should be propagated to this function.
2516 params.has_user_gesture = false;
2517 params.should_clear_history_list = false;
2518 params.started_from_context_menu = false;
2519 /* params.navigation_ui_data: skip */
2520 /* params.input_start: skip */
Minggang Wangf59db47b2021-06-16 01:56:222521 params.was_activated = blink::mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542522 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212523 params.impression = impression;
Camille Lamy5193caa2018-10-12 11:59:422524
2525 std::unique_ptr<NavigationRequest> request =
2526 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032527 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:142528 false /* has_user_gesture */, std::move(source_location),
Scott Violetcf6ea7e2021-06-09 21:09:212529 download_policy, ReloadType::NONE, entry.get(), frame_entry.get());
clamyea99ea12018-05-28 13:54:232530
2531 if (!request)
2532 return;
2533
Arthur Hemery948742762019-09-18 10:06:242534 // At this stage we are proceeding with this navigation. If this was renderer
2535 // initiated with user gesture, we need to make sure we clear up potential
2536 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2537 DiscardNonCommittedEntries();
2538
Lukasz Anforowicz9ee83c272020-12-01 20:14:052539 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262540}
2541
[email protected]d1198fd2012-08-13 22:50:192542void NavigationControllerImpl::SetSessionStorageNamespace(
Aaron Colwellf3b316e2021-03-11 20:17:052543 const StoragePartitionId& partition_id,
[email protected]8ff00d72012-10-23 19:12:212544 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192545 if (!session_storage_namespace)
2546 return;
2547
2548 // We can't overwrite an existing SessionStorage without violating spec.
2549 // Attempts to do so may give a tab access to another tab's session storage
2550 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152551 bool successful_insert =
2552 session_storage_namespace_map_
Aaron Colwellf3b316e2021-03-11 20:17:052553 .insert(std::make_pair(partition_id,
2554 static_cast<SessionStorageNamespaceImpl*>(
2555 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302556 .second;
2557 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
Aaron Colwellb731a0ae2021-03-19 19:14:472558 OnStoragePartitionIdAdded(partition_id);
[email protected]d1198fd2012-08-13 22:50:192559}
2560
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572561bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Aran Gilman37d11632019-10-08 23:07:152562 return IsInitialNavigation() && !GetLastCommittedEntry() &&
Carlos Caballeroede6f8c2021-01-28 11:01:502563 !frame_tree_.has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462564}
2565
Aran Gilman37d11632019-10-08 23:07:152566SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Aaron Colwell78b4bde2021-03-16 16:16:092567 const SiteInfo& site_info) {
2568 // TODO(acolwell): Remove partition_id logic once we have successfully
2569 // migrated the implementation to be a StoragePartitionConfig. At that point
2570 // |site_info| can be replaced with a StoragePartitionConfig.
2571 const StoragePartitionId partition_id =
2572 site_info.GetStoragePartitionId(browser_context_);
2573 const StoragePartitionConfig partition_config =
2574 site_info.GetStoragePartitionConfig(browser_context_);
[email protected]d1198fd2012-08-13 22:50:192575
[email protected]fdac6ade2013-07-20 01:06:302576 StoragePartition* partition =
Lukasz Anforowiczb9a969a2021-04-29 15:26:252577 browser_context_->GetStoragePartition(partition_config);
michaelnbacbcbd2016-02-09 00:32:032578 DOMStorageContextWrapper* context_wrapper =
2579 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2580
2581 SessionStorageNamespaceMap::const_iterator it =
2582 session_storage_namespace_map_.find(partition_id);
2583 if (it != session_storage_namespace_map_.end()) {
2584 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152585 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2586 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:472587
2588 // Verify that the config we generated now matches the one that
2589 // was generated when the namespace was added to the map.
2590 DCHECK_EQ(partition_config, it->first.config());
2591 if (partition_config != it->first.config()) {
2592 LogStoragePartitionIdCrashKeys(it->first, partition_id);
2593 }
2594
michaelnbacbcbd2016-02-09 00:32:032595 return it->second.get();
2596 }
2597
2598 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102599 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2600 SessionStorageNamespaceImpl::Create(context_wrapper);
2601 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2602 session_storage_namespace.get();
2603 session_storage_namespace_map_[partition_id] =
2604 std::move(session_storage_namespace);
Aaron Colwellb731a0ae2021-03-19 19:14:472605 OnStoragePartitionIdAdded(partition_id);
[email protected]fdac6ade2013-07-20 01:06:302606
Daniel Murphy31bbb8b12018-02-07 21:44:102607 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302608}
2609
2610SessionStorageNamespace*
2611NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Aaron Colwell78b4bde2021-03-16 16:16:092612 return GetSessionStorageNamespace(SiteInfo());
[email protected]fdac6ade2013-07-20 01:06:302613}
2614
2615const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572616NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302617 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552618}
[email protected]d202a7c2012-01-04 07:53:472619
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572620bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562621 return needs_reload_;
2622}
[email protected]a26023822011-12-29 00:23:552623
[email protected]46bb5e9c2013-10-03 22:16:472624void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412625 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2626}
2627
2628void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472629 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412630 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542631
2632 if (last_committed_entry_index_ != -1) {
2633 entries_[last_committed_entry_index_]->SetTransitionType(
2634 ui::PAGE_TRANSITION_RELOAD);
2635 }
[email protected]46bb5e9c2013-10-03 22:16:472636}
2637
[email protected]d202a7c2012-01-04 07:53:472638void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572639 DCHECK_LT(index, GetEntryCount());
2640 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312641 DiscardNonCommittedEntries();
2642
2643 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122644 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312645 last_committed_entry_index_--;
2646}
2647
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572648NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272649 // If there is no pending_entry_, there should be no pending_entry_index_.
2650 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2651
2652 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362653 // at that index. An exception is while a reload of a post commit error page
2654 // is ongoing; in that case pending entry will point to the entry replaced
2655 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272656 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362657 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2658 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272659
[email protected]022af742011-12-28 18:37:252660 return pending_entry_;
2661}
2662
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572663int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272664 // The pending entry index must always be less than the number of entries.
2665 // If there are no entries, it must be exactly -1.
2666 DCHECK_LT(pending_entry_index_, GetEntryCount());
2667 DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:552668 return pending_entry_index_;
2669}
2670
avi25764702015-06-23 15:43:372671void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572672 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362673 bool replace,
2674 bool was_post_commit_error) {
mastiz7eddb5f72016-06-23 09:52:452675 DCHECK(!ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2676 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202677
avi5cad4912015-06-19 05:25:442678 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2679 // need to keep continuity with the pending entry, so copy the pending entry's
2680 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2681 // then the renderer navigated on its own, independent of the pending entry,
2682 // so don't copy anything.
2683 if (pending_entry_ && pending_entry_index_ == -1)
2684 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202685
arthursonzogni69a6a1b2019-09-17 09:23:002686 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:202687
creisee17e932015-07-17 17:56:222688 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362689 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102690 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572691 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362692 // If the new entry is a post-commit error page, we store the current last
2693 // committed entry to the side so that we can put it back when navigating
2694 // away from the error.
2695 if (was_post_commit_error) {
2696 DCHECK(!entry_replaced_by_post_commit_error_);
2697 entry_replaced_by_post_commit_error_ =
2698 std::move(entries_[last_committed_entry_index_]);
2699 }
dcheng36b6aec92015-12-26 06:16:362700 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222701 return;
2702 }
[email protected]765b35502008-08-21 00:51:202703
creis37979a62015-08-04 19:48:182704 // We shouldn't see replace == true when there's no committed entries.
2705 DCHECK(!replace);
2706
Michael Thiessen9b14d512019-09-23 21:19:472707 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202708
Shivani Sharmad8c8d652019-02-13 17:27:572709 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202710
dcheng36b6aec92015-12-26 06:16:362711 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202712 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292713}
2714
Shivani Sharmad8c8d652019-02-13 17:27:572715void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162716 if (entries_.size() < max_entry_count())
2717 return;
2718
2719 DCHECK_EQ(max_entry_count(), entries_.size());
2720 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572721 CHECK_EQ(pending_entry_index_, -1);
2722
2723 int index = 0;
Elly Fong-Jonesccc6d1f2021-06-14 18:32:422724 // Retrieve the oldest skippable entry.
2725 for (; index < GetEntryCount(); index++) {
2726 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2727 break;
Shivani Sharmad8c8d652019-02-13 17:27:572728 }
2729
2730 // If there is no skippable entry or if it is the last committed entry then
2731 // fall back to pruning the oldest entry. It is not safe to prune the last
2732 // committed entry.
2733 if (index == GetEntryCount() || index == last_committed_entry_index_)
2734 index = 0;
2735
2736 bool should_succeed = RemoveEntryAtIndex(index);
2737 DCHECK_EQ(true, should_succeed);
2738
2739 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252740}
2741
clamy3cb9bea92018-07-10 12:42:022742void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162743 ReloadType reload_type,
2744 int sandboxed_source_frame_tree_node_id) {
Alexander Timin3a92df72019-09-20 11:59:502745 TRACE_EVENT0("navigation",
2746 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272747 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022748 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362749 if (pending_entry_index_ != -1) {
2750 // The pending entry may not be in entries_ if a post-commit error page is
2751 // showing.
2752 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2753 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2754 }
Gyuyoung Kim107c2a02021-04-13 01:49:302755 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572756 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:012757 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:002758 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:572759 int nav_entry_id = pending_entry_->GetUniqueID();
2760
[email protected]83c2e232011-10-07 21:36:462761 // If we were navigating to a slow-to-commit page, and the user performs
2762 // a session history navigation to the last committed page, RenderViewHost
2763 // will force the throbber to start, but WebKit will essentially ignore the
2764 // navigation, and won't send a message to stop the throbber. To prevent this
2765 // from happening, we drop the navigation here and stop the slow-to-commit
2766 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022767 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:082768 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:272769 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
[email protected]ec6c05f2013-10-23 18:41:572770 delegate_->Stop();
[email protected]6a13a6c2011-12-20 21:47:122771
[email protected]83c2e232011-10-07 21:36:462772 DiscardNonCommittedEntries();
2773 return;
2774 }
2775
creisce0ef3572017-01-26 17:53:082776 // Compare FrameNavigationEntries to see which frames in the tree need to be
2777 // navigated.
clamy3cb9bea92018-07-10 12:42:022778 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2779 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Alex Moshchuk3a4e77a2020-05-29 21:32:572780 FindFramesToNavigate(root, reload_type, &same_document_loads,
2781 &different_document_loads);
creis4e2ecb72015-06-20 00:46:302782
2783 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572784 // We were unable to match any frames to navigate. This can happen if a
2785 // history navigation targets a subframe that no longer exists
2786 // (https://crbug.com/705550). In this case, we need to update the current
2787 // history entry to the pending one but keep the main document loaded. We
2788 // also need to ensure that observers are informed about the updated
2789 // current history entry (e.g., for greying out back/forward buttons), and
2790 // that renderer processes update their history offsets. The easiest way
2791 // to do all that is to schedule a "redundant" same-document navigation in
2792 // the main frame.
2793 //
2794 // Note that we don't want to remove this history entry, as it might still
2795 // be valid later, since a frame that it's targeting may be recreated.
2796 //
2797 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
2798 // need to repeat history navigations until finding the one that works.
2799 // Consider changing this behavior to keep looking for the first valid
2800 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:022801 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422802 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572803 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:572804 ReloadType::NONE /* reload_type */,
2805 true /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422806 false /* is_history_navigation_in_new_child */);
clamy3cb9bea92018-07-10 12:42:022807 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572808 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:022809 DiscardPendingEntry(false);
2810 return;
2811 }
Alex Moshchuk3a4e77a2020-05-29 21:32:572812 same_document_loads.push_back(std::move(navigation_request));
2813
2814 // Sanity check that we never take this branch for any kinds of reloads,
2815 // as those should've queued a different-document load in the main frame.
2816 DCHECK(!is_forced_reload);
2817 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:302818 }
2819
Dave Tapuska8bfd84c2019-03-26 20:47:162820 // If |sandboxed_source_frame_node_id| is valid, then track whether this
2821 // navigation affects any frame outside the frame's subtree.
2822 if (sandboxed_source_frame_tree_node_id !=
2823 FrameTreeNode::kFrameTreeNodeInvalidId) {
2824 bool navigates_inside_tree =
2825 DoesSandboxNavigationStayWithinSubtree(
2826 sandboxed_source_frame_tree_node_id, same_document_loads) &&
2827 DoesSandboxNavigationStayWithinSubtree(
2828 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:502829 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:162830 // the number of pages that trigger this.
2831 FrameTreeNode* sandbox_source_frame_tree_node =
2832 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
2833 if (sandbox_source_frame_tree_node) {
2834 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
2835 sandbox_source_frame_tree_node->current_frame_host(),
2836 navigates_inside_tree
2837 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
2838 : blink::mojom::WebFeature::
2839 kSandboxBackForwardAffectsFramesOutsideSubtree);
2840 }
Dave Tapuska855c1e12019-08-23 20:45:522841
2842 // If the navigation occurred outside the tree discard it because
2843 // the sandboxed frame didn't have permission to navigate outside
2844 // its tree. If it is possible that the navigation is both inside and
2845 // outside the frame tree and we discard it entirely because we don't
2846 // want to end up in a history state that didn't exist before.
2847 if (base::FeatureList::IsEnabled(
2848 features::kHistoryPreventSandboxedNavigation) &&
2849 !navigates_inside_tree) {
2850 DiscardPendingEntry(false);
2851 return;
2852 }
Dave Tapuska8bfd84c2019-03-26 20:47:162853 }
2854
Carlos Caballero539a421c2020-07-06 10:25:572855 // BackForwardCache:
2856 // Navigate immediately if the document is in the BackForwardCache.
2857 if (back_forward_cache_.GetEntry(nav_entry_id)) {
2858 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
2859 DCHECK_EQ(reload_type, ReloadType::NONE);
2860 auto navigation_request = CreateNavigationRequestFromEntry(
2861 root, pending_entry_, pending_entry_->GetFrameEntry(root),
2862 ReloadType::NONE, false /* is_same_document_history_load */,
2863 false /* is_history_navigation_in_new_child */);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052864 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:572865
2866 return;
2867 }
2868
2869 // History navigation might try to reuse a specific BrowsingInstance, already
2870 // used by a page in the cache. To avoid having two different main frames that
2871 // live in the same BrowsingInstance, evict the all pages with this
2872 // BrowsingInstance from the cache.
2873 //
2874 // For example, take the following scenario:
2875 //
2876 // A1 = Some page on a.com
2877 // A2 = Some other page on a.com
2878 // B3 = An uncacheable page on b.com
2879 //
2880 // Then the following navigations occur:
2881 // A1->A2->B3->A1
2882 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
2883 // take its place) and A1 will be created in the same BrowsingInstance (and
2884 // SiteInstance), as A2.
2885 //
2886 // If we didn't do anything, both A1 and A2 would remain alive in the same
2887 // BrowsingInstance/SiteInstance, which is unsupported by
2888 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
2889 // A2 from the cache.
2890 if (pending_entry_->site_instance()) {
2891 back_forward_cache_.EvictFramesInRelatedSiteInstances(
2892 pending_entry_->site_instance());
2893 }
2894
clamy3cb9bea92018-07-10 12:42:022895 // This call does not support re-entrancy. See http://crbug.com/347742.
2896 CHECK(!in_navigate_to_pending_entry_);
2897 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:302898
arthursonzogni66f711c2019-10-08 14:40:362899 // It is not possible to delete the pending NavigationEntry while navigating
2900 // to it. Grab a reference to delay potential deletion until the end of this
2901 // function.
2902 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
2903
creis4e2ecb72015-06-20 00:46:302904 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:022905 for (auto& item : same_document_loads) {
2906 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052907 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302908 }
clamy3cb9bea92018-07-10 12:42:022909 for (auto& item : different_document_loads) {
2910 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052911 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302912 }
clamy3cb9bea92018-07-10 12:42:022913
2914 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:302915}
2916
Alex Moshchuk3a4e77a2020-05-29 21:32:572917NavigationControllerImpl::HistoryNavigationAction
2918NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:302919 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:572920 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:422921 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:582922 // Only active and prerendered documents are allowed to navigate in their
2923 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422924 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:012925 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:582926 // - If the document is in pending deletion, the browser already committed
2927 // to destroying this RenderFrameHost. See https://crbug.com/930278.
2928 // - If the document is in back-forward cache, it's not allowed to navigate
2929 // as it should remain frozen. Ignore the request and evict the document
2930 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422931 //
Sreeja Kamishettydb8e2892021-03-10 09:30:582932 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:422933 // which should all be inactive as well.
Sreeja Kamishettydb8e2892021-03-10 09:30:582934 if (render_frame_host->IsInactiveAndDisallowActivation())
Sreeja Kamishetty8eacabb2021-03-09 11:45:422935 return HistoryNavigationAction::kStopLooking;
2936 }
arthursonzogni03f76152019-02-12 10:35:202937
Alex Moshchuk3a4e77a2020-05-29 21:32:572938 // If there's no last committed entry, there is no previous history entry to
2939 // compare against, so fall back to a different-document load. Note that we
2940 // should only reach this case for the root frame and not descend further
2941 // into subframes.
2942 if (!GetLastCommittedEntry()) {
2943 DCHECK(frame->IsMainFrame());
2944 return HistoryNavigationAction::kDifferentDocument;
2945 }
2946
2947 // Reloads should result in a different-document load. Note that reloads may
2948 // also happen via the |needs_reload_| mechanism where the reload_type is
2949 // NONE, so detect this by comparing whether we're going to the same
2950 // entry that we're currently on. Similarly to above, only main frames
2951 // should reach this. Note that subframes support reloads, but that's done
2952 // via a different path that doesn't involve FindFramesToNavigate (see
2953 // RenderFrameHost::Reload()).
2954 if (reload_type != ReloadType::NONE ||
2955 pending_entry_index_ == last_committed_entry_index_) {
2956 DCHECK(frame->IsMainFrame());
2957 return HistoryNavigationAction::kDifferentDocument;
2958 }
2959
Alex Moshchuk47d1a4bd2020-06-01 22:15:342960 // If there is no new FrameNavigationEntry for the frame, ignore the
2961 // load. For example, this may happen when going back to an entry before a
2962 // frame was created. Suppose we commit a same-document navigation that also
2963 // results in adding a new subframe somewhere in the tree. If we go back,
2964 // the new subframe will be missing a FrameNavigationEntry in the previous
2965 // NavigationEntry, but we shouldn't delete or change what's loaded in
2966 // it.
2967 //
Alex Moshchuke65c39272020-06-03 17:55:372968 // Note that in this case, there is no need to keep looking for navigations
2969 // in subframes, which would be missing FrameNavigationEntries as well.
2970 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:342971 // It's important to check this before checking |old_item| below, since both
2972 // might be null, and in that case we still shouldn't change what's loaded in
2973 // this frame. Note that scheduling any loads assumes that |new_item| is
2974 // non-null. See https://crbug.com/1088354.
2975 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
2976 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:372977 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:342978
Alex Moshchuk3a4e77a2020-05-29 21:32:572979 // If there is no old FrameNavigationEntry, schedule a different-document
2980 // load.
2981 //
creis225a7432016-06-03 22:56:272982 // TODO(creis): Store the last committed FrameNavigationEntry to use here,
2983 // rather than assuming the NavigationEntry has up to date info on subframes.
creis4e2ecb72015-06-20 00:46:302984 FrameNavigationEntry* old_item =
2985 GetLastCommittedEntry()->GetFrameEntry(frame);
Alex Moshchuk3a4e77a2020-05-29 21:32:572986 if (!old_item)
2987 return HistoryNavigationAction::kDifferentDocument;
2988
Alex Moshchuk3a4e77a2020-05-29 21:32:572989 // If the new item is not in the same SiteInstance, schedule a
2990 // different-document load. Newly restored items may not have a SiteInstance
2991 // yet, in which case it will be assigned on first commit.
2992 if (new_item->site_instance() &&
2993 new_item->site_instance() != old_item->site_instance())
2994 return HistoryNavigationAction::kDifferentDocument;
2995
2996 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:312997 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
2998 // that we do this even if the history navigation would not be modifying this
2999 // frame were it live.
3000 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:573001 return HistoryNavigationAction::kDifferentDocument;
3002
3003 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:313004 // Starting a navigation after a crash early-promotes the speculative
3005 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
3006 // committed yet, so we can not possibly perform a same-document history
3007 // navigation. The frame would need to be reloaded with a cross-document
3008 // navigation.
3009 if (!frame->current_frame_host()->has_committed_any_navigation())
3010 return HistoryNavigationAction::kDifferentDocument;
3011
creis54131692016-08-12 18:32:253012 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:493013 // sequence number but different item sequence number.
3014 if (new_item->document_sequence_number() ==
3015 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573016 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493017 }
avib48cb312016-05-05 21:35:003018
Alex Moshchuk3a4e77a2020-05-29 21:32:573019 // Otherwise, if both item and document sequence numbers differ, this
3020 // should be a different document load.
3021 return HistoryNavigationAction::kDifferentDocument;
3022 }
3023
3024 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373025 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573026 DCHECK_EQ(new_item->document_sequence_number(),
3027 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373028 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573029}
3030
3031void NavigationControllerImpl::FindFramesToNavigate(
3032 FrameTreeNode* frame,
3033 ReloadType reload_type,
3034 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3035 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3036 DCHECK(pending_entry_);
3037 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3038
3039 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3040
3041 if (action == HistoryNavigationAction::kSameDocument) {
3042 std::unique_ptr<NavigationRequest> navigation_request =
3043 CreateNavigationRequestFromEntry(
3044 frame, pending_entry_, new_item, reload_type,
3045 true /* is_same_document_history_load */,
3046 false /* is_history_navigation_in_new_child */);
3047 if (navigation_request) {
3048 // Only add the request if was properly created. It's possible for the
3049 // creation to fail in certain cases, e.g. when the URL is invalid.
3050 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303051 }
Lei Zhang96031532019-10-10 19:05:473052
Alex Moshchuk3a4e77a2020-05-29 21:32:573053 // TODO(avi, creis): This is a bug; we should not return here. Rather, we
3054 // should continue on and navigate all child frames which have also
3055 // changed. This bug is the cause of <https://crbug.com/542299>, which is
3056 // a NC_IN_PAGE_NAVIGATION renderer kill.
3057 //
3058 // However, this bug is a bandaid over a deeper and worse problem. Doing a
3059 // pushState immediately after loading a subframe is a race, one that no
3060 // web page author expects. If we fix this bug, many large websites break.
3061 // For example, see <https://crbug.com/598043> and the spec discussion at
3062 // <https://github.com/whatwg/html/issues/1191>.
3063 //
3064 // For now, we accept this bug, and hope to resolve the race in a
3065 // different way that will one day allow us to fix this.
3066 return;
3067 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473068 std::unique_ptr<NavigationRequest> navigation_request =
3069 CreateNavigationRequestFromEntry(
3070 frame, pending_entry_, new_item, reload_type,
3071 false /* is_same_document_history_load */,
3072 false /* is_history_navigation_in_new_child */);
3073 if (navigation_request) {
3074 // Only add the request if was properly created. It's possible for the
3075 // creation to fail in certain cases, e.g. when the URL is invalid.
3076 different_document_loads->push_back(std::move(navigation_request));
3077 }
3078 // For a different document, the subframes will be destroyed, so there's
3079 // no need to consider them.
3080 return;
Alex Moshchuke65c39272020-06-03 17:55:373081 } else if (action == HistoryNavigationAction::kStopLooking) {
3082 return;
creis4e2ecb72015-06-20 00:46:303083 }
3084
3085 for (size_t i = 0; i < frame->child_count(); i++) {
clamy3cb9bea92018-07-10 12:42:023086 FindFramesToNavigate(frame->child_at(i), reload_type, same_document_loads,
creis4e2ecb72015-06-20 00:46:303087 different_document_loads);
3088 }
3089}
3090
Harkiran Bolariaba823e42021-05-21 18:30:363091base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry(
clamy21718cc22018-06-13 13:34:243092 const LoadURLParams& params) {
3093 // Find the appropriate FrameTreeNode.
3094 FrameTreeNode* node = nullptr;
3095 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3096 !params.frame_name.empty()) {
3097 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003098 ? frame_tree_.FindByID(params.frame_tree_node_id)
3099 : frame_tree_.FindByName(params.frame_name);
clamy21718cc22018-06-13 13:34:243100 }
3101
3102 // If no FrameTreeNode was specified, navigate the main frame.
3103 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003104 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243105
Camille Lamy5193caa2018-10-12 11:59:423106 // Compute overrides to the LoadURLParams for |override_user_agent|,
3107 // |should_replace_current_entry| and |has_user_gesture| that will be used
3108 // both in the creation of the NavigationEntry and the NavigationRequest.
3109 // Ideally, the LoadURLParams themselves would be updated, but since they are
3110 // passed as a const reference, this is not possible.
3111 // TODO(clamy): When we only create a NavigationRequest, move this to
3112 // CreateNavigationRequestFromLoadURLParams.
3113 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3114 GetLastCommittedEntry());
3115
3116 // Don't allow an entry replacement if there is no entry to replace.
3117 // http://crbug.com/457149
3118 bool should_replace_current_entry =
3119 params.should_replace_current_entry && entries_.size();
3120
clamy21718cc22018-06-13 13:34:243121 // Javascript URLs should not create NavigationEntries. All other navigations
3122 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243123 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373124 std::unique_ptr<NavigationEntryImpl> entry =
3125 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3126 should_replace_current_entry,
3127 params.has_user_gesture);
clamy21718cc22018-06-13 13:34:243128 DiscardPendingEntry(false);
3129 SetPendingEntry(std::move(entry));
3130 }
3131
3132 // Renderer-debug URLs are sent to the renderer process immediately for
3133 // processing and don't need to create a NavigationRequest.
3134 // Note: this includes navigations to JavaScript URLs, which are considered
3135 // renderer-debug URLs.
3136 // Note: we intentionally leave the pending entry in place for renderer debug
3137 // URLs, unlike the cases below where we clear it if the navigation doesn't
3138 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303139 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483140 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3141 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153142 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483143 params.url, browser_context_)) {
3144 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363145 return nullptr;
Oleg Davydov2cc0167b2019-02-05 14:32:483146 }
3147
clamy21718cc22018-06-13 13:34:243148 HandleRendererDebugURL(node, params.url);
Harkiran Bolariaba823e42021-05-21 18:30:363149 return nullptr;
clamy21718cc22018-06-13 13:34:243150 }
3151
Antonio Sartori78a749f2020-11-30 12:03:393152 DCHECK(pending_entry_);
3153
clamy21718cc22018-06-13 13:34:243154 // Convert navigations to the current URL to a reload.
3155 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3156 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3157 // DevTools sometimes issues navigations to main frames that they do not
3158 // expect to see treated as reload, and it only works because they pass a
3159 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3160 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543161 ReloadType reload_type = params.reload_type;
3162 if (reload_type == ReloadType::NONE &&
3163 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043164 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243165 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243166 params.load_type ==
3167 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
Rakina Zata Amni312822d72021-06-04 16:13:373168 params.should_replace_current_entry, GetLastCommittedEntry())) {
clamy21718cc22018-06-13 13:34:243169 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473170 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393171
3172 // If this is a reload of an existing FrameNavigationEntry and we had a
3173 // policy container for it, then we should copy it into the pending entry,
3174 // so that it is copied to the navigation request in
3175 // CreateNavigationRequestFromLoadParams later.
3176 if (GetLastCommittedEntry()) {
3177 FrameNavigationEntry* previous_frame_entry =
3178 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193179 if (previous_frame_entry &&
3180 previous_frame_entry->policy_container_policies()) {
3181 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163182 previous_frame_entry->policy_container_policies()->Clone());
Antonio Sartori78a749f2020-11-30 12:03:393183 }
3184 }
3185 }
3186
3187 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
3188 // document (so no last commit), then we should copy the document polices from
3189 // RenderFrameHost's PolicyContainerHost. The NavigationRequest will create a
3190 // new PolicyContainerHost with the document policies from the
3191 // |pending_entry_|, and that PolicyContainerHost will be put in the final
3192 // RenderFrameHost for the navigation. This way, we ensure that we keep
3193 // enforcing the right policies on the initial empty document after the
3194 // reload.
3195 if (!GetLastCommittedEntry() && params.url.IsAboutBlank()) {
3196 if (node->current_frame_host() &&
3197 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193198 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163199 node->current_frame_host()
3200 ->policy_container_host()
3201 ->policies()
3202 .Clone());
Antonio Sartori78a749f2020-11-30 12:03:393203 }
clamy21718cc22018-06-13 13:34:243204 }
3205
3206 // navigation_ui_data should only be present for main frame navigations.
3207 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
3208
Camille Lamy5193caa2018-10-12 11:59:423209 std::unique_ptr<NavigationRequest> request =
3210 CreateNavigationRequestFromLoadParams(
3211 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143212 params.has_user_gesture, network::mojom::SourceLocation::New(),
3213 blink::NavigationDownloadPolicy(), reload_type, pending_entry_,
Scott Violetcf6ea7e2021-06-09 21:09:213214 pending_entry_->GetFrameEntry(node));
clamy21718cc22018-06-13 13:34:243215
3216 // If the navigation couldn't start, return immediately and discard the
3217 // pending NavigationEntry.
3218 if (!request) {
3219 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363220 return nullptr;
clamy21718cc22018-06-13 13:34:243221 }
3222
Camille Lamy5193caa2018-10-12 11:59:423223#if DCHECK_IS_ON()
3224 // Safety check that NavigationRequest and NavigationEntry match.
3225 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3226#endif
3227
clamy21718cc22018-06-13 13:34:243228 // This call does not support re-entrancy. See http://crbug.com/347742.
3229 CHECK(!in_navigate_to_pending_entry_);
3230 in_navigate_to_pending_entry_ = true;
3231
arthursonzogni66f711c2019-10-08 14:40:363232 // It is not possible to delete the pending NavigationEntry while navigating
3233 // to it. Grab a reference to delay potential deletion until the end of this
3234 // function.
3235 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3236
Harkiran Bolariaba823e42021-05-21 18:30:363237 base::WeakPtr<NavigationHandle> created_navigation_handle(
3238 request->GetWeakPtr());
Lukasz Anforowicz9ee83c272020-12-01 20:14:053239 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243240
3241 in_navigate_to_pending_entry_ = false;
Harkiran Bolariaba823e42021-05-21 18:30:363242 return created_navigation_handle;
clamy21718cc22018-06-13 13:34:243243}
3244
clamyea99ea12018-05-28 13:54:233245void NavigationControllerImpl::HandleRendererDebugURL(
3246 FrameTreeNode* frame_tree_node,
3247 const GURL& url) {
3248 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243249 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3250 // the renderer process is not live, unless it is the initial navigation
3251 // of the tab.
clamyea99ea12018-05-28 13:54:233252 if (!IsInitialNavigation()) {
3253 DiscardNonCommittedEntries();
3254 return;
3255 }
Fergal Dalyecd3b0202020-06-25 01:57:373256 // The current frame is always a main frame. If IsInitialNavigation() is
3257 // true then there have been no navigations and any frames of this tab must
3258 // be in the same renderer process. If that has crashed then the only frame
3259 // that can be revived is the main frame.
3260 frame_tree_node->render_manager()
3261 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233262 }
3263 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3264}
3265
clamy21718cc22018-06-13 13:34:243266std::unique_ptr<NavigationEntryImpl>
3267NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3268 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423269 const LoadURLParams& params,
3270 bool override_user_agent,
3271 bool should_replace_current_entry,
3272 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393273 // Browser initiated navigations might not have a blob_url_loader_factory set
3274 // in params even if the navigation is to a blob URL. If that happens, lookup
3275 // the correct url loader factory to use here.
3276 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483277 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473278 // Resolve the blob URL in the storage partition associated with the target
3279 // frame. This is the storage partition the URL will be loaded in, and only
3280 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393281 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473282 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393283 }
3284
clamy21718cc22018-06-13 13:34:243285 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443286 // extra_headers in params are \n separated; navigation entries want \r\n.
3287 std::string extra_headers_crlf;
3288 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243289
3290 // For subframes, create a pending entry with a corresponding frame entry.
3291 if (!node->IsMainFrame()) {
Tommy C. Li03eee77a2019-02-05 02:07:443292 if (GetLastCommittedEntry()) {
Tommy C. Li03eee77a2019-02-05 02:07:443293 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 Violetcf6ea7e2021-06-09 21:09:213302 blob_url_loader_factory, should_replace_current_entry));
Tommy C. Li03eee77a2019-02-05 02:07:443303 }
Nasko Oskov18006bc2018-12-06 02:53:583304
clamy21718cc22018-06-13 13:34:243305 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:083306 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
clamy21718cc22018-06-13 13:34:243307 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Anton Bikineevf62d1bf2021-05-15 17:56:073308 params.url, absl::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203309 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393310 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:483311 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori78a749f2020-11-30 12:03:393312 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163313 // a conversion of a new navigation into a reload, we will set the right
3314 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193315 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243316 } else {
3317 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243318 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063319 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103320 params.source_site_instance.get(), params.transition_type,
3321 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Scott Violetcf6ea7e2021-06-09 21:09:213322 blob_url_loader_factory, should_replace_current_entry));
clamy21718cc22018-06-13 13:34:243323 entry->set_source_site_instance(
3324 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3325 entry->SetRedirectChain(params.redirect_chain);
3326 }
3327
3328 // Set the FTN ID (only used in non-site-per-process, for tests).
3329 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243330 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423331 entry->SetIsOverridingUserAgent(override_user_agent);
3332 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543333 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243334
clamy21718cc22018-06-13 13:34:243335 switch (params.load_type) {
3336 case LOAD_TYPE_DEFAULT:
3337 break;
3338 case LOAD_TYPE_HTTP_POST:
3339 entry->SetHasPostData(true);
3340 entry->SetPostData(params.post_data);
3341 break;
3342 case LOAD_TYPE_DATA:
3343 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3344 entry->SetVirtualURL(params.virtual_url_for_data_url);
3345#if defined(OS_ANDROID)
3346 entry->SetDataURLAsString(params.data_url_as_string);
3347#endif
3348 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3349 break;
clamy21718cc22018-06-13 13:34:243350 }
3351
3352 // TODO(clamy): NavigationEntry is meant for information that will be kept
3353 // after the navigation ended and therefore is not appropriate for
3354 // started_from_context_menu. Move started_from_context_menu to
3355 // NavigationUIData.
3356 entry->set_started_from_context_menu(params.started_from_context_menu);
3357
3358 return entry;
3359}
3360
clamyea99ea12018-05-28 13:54:233361std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423362NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3363 FrameTreeNode* node,
3364 const LoadURLParams& params,
3365 bool override_user_agent,
3366 bool should_replace_current_entry,
3367 bool has_user_gesture,
Antonio Sartori2f763d9d2021-04-21 10:04:143368 network::mojom::SourceLocationPtr source_location,
Yeunjoo Choi3df791a2021-02-17 07:07:253369 blink::NavigationDownloadPolicy download_policy,
Camille Lamy5193caa2018-10-12 11:59:423370 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573371 NavigationEntryImpl* entry,
Scott Violetcf6ea7e2021-06-09 21:09:213372 FrameNavigationEntry* frame_entry) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573373 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283374 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533375 // All renderer-initiated navigations must have an initiator_origin.
3376 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513377
Camille Lamy5193caa2018-10-12 11:59:423378 GURL url_to_load;
3379 GURL virtual_url;
Anton Bikineevf62d1bf2021-05-15 17:56:073380 absl::optional<url::Origin> origin_to_commit =
3381 frame_entry ? frame_entry->committed_origin() : absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:323382
Camille Lamy2baa8022018-10-19 16:43:173383 // For main frames, rewrite the URL if necessary and compute the virtual URL
3384 // that should be shown in the address bar.
3385 if (node->IsMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423386 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173387 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423388 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423389
Camille Lamy2baa8022018-10-19 16:43:173390 // For DATA loads, override the virtual URL.
3391 if (params.load_type == LOAD_TYPE_DATA)
3392 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423393
Camille Lamy2baa8022018-10-19 16:43:173394 if (virtual_url.is_empty())
3395 virtual_url = url_to_load;
3396
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573397 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283398
Aran Gilman249eb122019-12-02 23:32:463399 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3400 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3401 // return different results, leading to a different URL in the
3402 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3403 // pending NavigationEntry, as we'll only make one call to
3404 // RewriteUrlForNavigation.
3405 VLOG_IF(1, (url_to_load != frame_entry->url()))
3406 << "NavigationRequest and FrameEntry have different URLs: "
3407 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283408
Camille Lamy2baa8022018-10-19 16:43:173409 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423410 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173411 // NavigationRequest.
3412 } else {
3413 url_to_load = params.url;
3414 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243415 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173416 }
Camille Lamy5193caa2018-10-12 11:59:423417
Ehsan Karamad44fc72112019-02-26 18:15:473418 if (node->render_manager()->is_attaching_inner_delegate()) {
3419 // Avoid starting any new navigations since this node is now preparing for
3420 // attaching an inner delegate.
3421 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203422 }
Camille Lamy5193caa2018-10-12 11:59:423423
Camille Lamy5193caa2018-10-12 11:59:423424 if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load))
3425 return nullptr;
3426
Kunihiko Sakamoto346a74e2021-03-10 08:57:483427 if (!DoesURLMatchOriginForNavigation(
3428 url_to_load, origin_to_commit,
3429 frame_entry->subresource_web_bundle_navigation_info())) {
Nasko Oskov03912102019-01-11 00:21:323430 DCHECK(false) << " url:" << url_to_load
3431 << " origin:" << origin_to_commit.value();
3432 return nullptr;
3433 }
3434
Camille Lamy5193caa2018-10-12 11:59:423435 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403436 blink::PreviewsState previews_state =
3437 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
Camille Lamy5193caa2018-10-12 11:59:423438 if (!node->IsMainFrame()) {
3439 // For subframes, use the state of the top-level frame.
3440 previews_state = node->frame_tree()
3441 ->root()
3442 ->current_frame_host()
3443 ->last_navigation_previews_state();
3444 }
3445
Camille Lamy5193caa2018-10-12 11:59:423446 // This will be used to set the Navigation Timing API navigationStart
3447 // parameter for browser navigations in new tabs (intents, tabs opened through
3448 // "Open link in new tab"). If the navigation must wait on the current
3449 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3450 // will be updated when the BeforeUnload ack is received.
3451 base::TimeTicks navigation_start = base::TimeTicks::Now();
3452
danakjd83d706d2020-11-25 22:11:123453 // Look for a pending commit that is to another document in this
3454 // FrameTreeNode. If one exists, then the last committed URL will not be the
3455 // current URL by the time this navigation commits.
3456 bool has_pending_cross_document_commit =
3457 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493458 bool is_currently_error_page = node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123459
danakjb952ef12021-01-14 19:58:493460 mojom::NavigationType navigation_type = GetNavigationType(
3461 /*old_url=*/node->current_url(),
3462 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3463 has_pending_cross_document_commit, is_currently_error_page,
3464 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423465
3466 // Create the NavigationParams based on |params|.
3467
Hiroki Nakagawa4ed61282021-06-18 05:37:233468 bool is_view_source_mode = entry->IsViewSourceMode();
3469 DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme));
Charlie Harrison8c113a32019-01-07 16:08:293470
Hiroki Nakagawa4ed61282021-06-18 05:37:233471 // Update |download_policy| if the virtual URL is view-source.
Charlie Harrison8c113a32019-01-07 16:08:293472 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253473 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293474
Camille Lamy5193caa2018-10-12 11:59:423475 const GURL& history_url_for_data_url =
3476 params.base_url_for_data_url.is_empty() ? GURL() : virtual_url;
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513477 mojom::CommonNavigationParamsPtr common_params =
3478 mojom::CommonNavigationParams::New(
3479 url_to_load, params.initiator_origin,
3480 blink::mojom::Referrer::New(params.referrer.url,
3481 params.referrer.policy),
Scott Violetcf6ea7e2021-06-09 21:09:213482 params.transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513483 should_replace_current_entry, params.base_url_for_data_url,
3484 history_url_for_data_url, previews_state, navigation_start,
3485 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
Antonio Sartori2f763d9d2021-04-21 10:04:143486 params.post_data, std::move(source_location),
arthursonzogniaf7c62c52020-02-12 10:49:413487 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273488 false /* has_text_fragment_token */,
3489 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3490 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513491 false /* is_history_navigation_in_new_child_frame */,
Charlie Hu5ffc0152019-12-06 15:59:533492 params.input_start);
Camille Lamy5193caa2018-10-12 11:59:423493
Lucas Furukawa Gadania9c45682019-07-31 22:05:143494 mojom::CommitNavigationParamsPtr commit_params =
3495 mojom::CommitNavigationParams::New(
arthursonzognid5a8d0b2021-03-11 17:36:433496 frame_entry->committed_origin(), network::mojom::WebSandboxFlags(),
3497 override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323498 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143499 std::vector<net::RedirectInfo>(),
3500 std::string() /* post_content_type */, common_params->url,
3501 common_params->method, params.can_load_local_resources,
3502 frame_entry->page_state(), entry->GetUniqueID(),
3503 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3504 -1 /* pending_history_list_offset */,
3505 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3506 params.should_clear_history_list ? 0 : GetEntryCount(),
3507 false /* was_discarded */, is_view_source_mode,
3508 params.should_clear_history_list, mojom::NavigationTiming::New(),
Anton Bikineevf62d1bf2021-05-15 17:56:073509 absl::nullopt /* appcache_host_id */,
Minggang Wangf59db47b2021-06-16 01:56:223510 blink::mojom::WasActivatedOption::kUnknown,
Lucas Furukawa Gadania9c45682019-07-31 22:05:143511 base::UnguessableToken::Create() /* navigation_token */,
Minggang Wang7ee0c742021-06-16 16:16:513512 std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143513#if defined(OS_ANDROID)
3514 std::string(), /* data_url_as_string */
3515#endif
arthursonzogni14379782020-05-15 09:09:273516 !params.is_renderer_initiated, /* is_browser_initiated */
Sreeja Kamishetty46f762c2021-02-05 07:52:463517 node->frame_tree()->is_prerendering() /* is_prerendering */,
Tsuyoshi Horoe86d7702019-11-29 01:52:473518 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533519 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173520 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073521 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163522 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333523 false /* origin_agent_cluster */,
Maks Orlovichc66745a2020-06-30 17:40:023524 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213525 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Nate Chapind1fe3612021-04-16 20:45:573526 false /* is_cross_browsing_instance */, nullptr /* old_page_info */,
3527 -1 /* http_response_code */,
3528 std::vector<
3529 mojom::AppHistoryEntryPtr>() /* app_history_back_entries */,
3530 std::vector<
Amr Aboelkherd3319f72021-06-17 10:32:243531 mojom::AppHistoryEntryPtr>() /* app_history_forward_entries */);
Camille Lamy5193caa2018-10-12 11:59:423532#if defined(OS_ANDROID)
3533 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143534 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423535 }
3536#endif
3537
Lucas Furukawa Gadania9c45682019-07-31 22:05:143538 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423539
3540 // A form submission may happen here if the navigation is a renderer-initiated
3541 // form submission that took the OpenURL path.
3542 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3543
3544 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3545 std::string extra_headers_crlf;
3546 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093547
3548 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143549 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083550 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453551 params.initiator_frame_token.has_value()
3552 ? &(params.initiator_frame_token.value())
3553 : nullptr,
3554 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3555 request_body,
John Delaney50425f82020-04-07 16:26:213556 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
3557 params.impression);
Yao Xiaodc5ed102019-06-04 19:19:093558 navigation_request->set_from_download_cross_origin_redirect(
3559 params.from_download_cross_origin_redirect);
3560 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423561}
3562
3563std::unique_ptr<NavigationRequest>
3564NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233565 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573566 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233567 FrameNavigationEntry* frame_entry,
3568 ReloadType reload_type,
3569 bool is_same_document_history_load,
Arthur Hemerybee4a752019-05-29 10:50:553570 bool is_history_navigation_in_new_child_frame) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343571 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233572 GURL dest_url = frame_entry->url();
Anton Bikineevf62d1bf2021-05-15 17:56:073573 absl::optional<url::Origin> origin_to_commit =
Nasko Oskov03912102019-01-11 00:21:323574 frame_entry->committed_origin();
3575
clamyea99ea12018-05-28 13:54:233576 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013577 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573578 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233579 // We may have been redirected when navigating to the current URL.
3580 // Use the URL the user originally intended to visit as signaled by the
3581 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013582 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573583 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233584 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323585 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233586 }
3587
Ehsan Karamad44fc72112019-02-26 18:15:473588 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3589 // Avoid starting any new navigations since this node is now preparing for
3590 // attaching an inner delegate.
3591 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203592 }
3593
Camille Lamy5193caa2018-10-12 11:59:423594 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573595 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233596 return nullptr;
3597 }
3598
Kunihiko Sakamoto346a74e2021-03-10 08:57:483599 if (!DoesURLMatchOriginForNavigation(
3600 dest_url, origin_to_commit,
3601 frame_entry->subresource_web_bundle_navigation_info())) {
3602 DCHECK(false) << " url:" << dest_url
3603 << " origin:" << origin_to_commit.value();
Nasko Oskov03912102019-01-11 00:21:323604 return nullptr;
3605 }
3606
clamyea99ea12018-05-28 13:54:233607 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403608 blink::PreviewsState previews_state =
3609 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
clamyea99ea12018-05-28 13:54:233610 if (!frame_tree_node->IsMainFrame()) {
3611 // For subframes, use the state of the top-level frame.
3612 previews_state = frame_tree_node->frame_tree()
3613 ->root()
3614 ->current_frame_host()
3615 ->last_navigation_previews_state();
clamyea99ea12018-05-28 13:54:233616 }
3617
clamyea99ea12018-05-28 13:54:233618 // This will be used to set the Navigation Timing API navigationStart
3619 // parameter for browser navigations in new tabs (intents, tabs opened through
3620 // "Open link in new tab"). If the navigation must wait on the current
3621 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3622 // will be updated when the BeforeUnload ack is received.
3623 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233624
danakjd83d706d2020-11-25 22:11:123625 // Look for a pending commit that is to another document in this
3626 // FrameTreeNode. If one exists, then the last committed URL will not be the
3627 // current URL by the time this navigation commits.
3628 bool has_pending_cross_document_commit =
3629 frame_tree_node->render_manager()
3630 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493631 bool is_currently_error_page =
3632 frame_tree_node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123633
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513634 mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123635 /*old_url=*/frame_tree_node->current_url(),
3636 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493637 has_pending_cross_document_commit, is_currently_error_page,
3638 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423639
3640 // A form submission may happen here if the navigation is a
3641 // back/forward/reload navigation that does a form resubmission.
3642 scoped_refptr<network::ResourceRequestBody> request_body;
3643 std::string post_content_type;
3644 if (frame_entry->method() == "POST") {
3645 request_body = frame_entry->GetPostData(&post_content_type);
3646 // Might have a LF at end.
Peter Kastingb53b81912021-04-28 19:23:303647 post_content_type = std::string(
3648 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL));
Camille Lamy5193caa2018-10-12 11:59:423649 }
3650
3651 // Create the NavigationParams based on |entry| and |frame_entry|.
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513652 mojom::CommonNavigationParamsPtr common_params =
3653 entry->ConstructCommonNavigationParams(
3654 *frame_entry, request_body, dest_url,
3655 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
3656 navigation_type, previews_state, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533657 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513658 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553659 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423660
3661 // TODO(clamy): |intended_as_new_entry| below should always be false once
3662 // Reload no longer leads to this being called for a pending NavigationEntry
3663 // of index -1.
Lucas Furukawa Gadania9c45682019-07-31 22:05:143664 mojom::CommitNavigationParamsPtr commit_params =
3665 entry->ConstructCommitNavigationParams(
3666 *frame_entry, common_params->url, origin_to_commit,
3667 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3668 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533669 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3670 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143671 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423672
clamyea99ea12018-05-28 13:54:233673 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143674 frame_tree_node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083675 !entry->is_renderer_initiated(), false /* was_opener_suppressed */,
3676 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453677 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3678 entry->extra_headers(), frame_entry, entry, request_body,
Anton Bikineevf62d1bf2021-05-15 17:56:073679 nullptr /* navigation_ui_data */, absl::nullopt /* impression */);
clamyea99ea12018-05-28 13:54:233680}
3681
[email protected]d202a7c2012-01-04 07:53:473682void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213683 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273684 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403685
[email protected]2db9bd72012-04-13 20:20:563686 // We need to notify the ssl_manager_ before the web_contents_ so the
[email protected]df1af242009-05-01 00:11:403687 // location bar will have up-to-date information about the security style
3688 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133689 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403690
[email protected]7f924832014-08-09 05:57:223691 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:573692 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463693
[email protected]b0f724c2013-09-05 04:21:133694 // TODO(avi): Remove. http://crbug.com/170921
3695 NotificationDetails notification_details =
3696 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153697 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3698 Source<NavigationController>(this),
3699 notification_details);
initial.commit09911bf2008-07-26 23:55:293700}
3701
initial.commit09911bf2008-07-26 23:55:293702// static
[email protected]d202a7c2012-01-04 07:53:473703size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233704 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153705 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:213706 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233707}
3708
[email protected]d202a7c2012-01-04 07:53:473709void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223710 if (is_active && needs_reload_)
3711 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293712}
3713
[email protected]d202a7c2012-01-04 07:53:473714void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293715 if (!needs_reload_)
3716 return;
3717
Bo Liucdfa4b12018-11-06 00:21:443718 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3719 needs_reload_type_);
3720
initial.commit09911bf2008-07-26 23:55:293721 // Calling Reload() results in ignoring state, and not loading.
3722 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3723 // cached state.
avicc872d7242015-08-19 21:26:343724 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163725 NavigateToExistingPendingEntry(ReloadType::NONE,
3726 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343727 } else if (last_committed_entry_index_ != -1) {
arthursonzogni5c4c202d2017-04-25 23:41:273728 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343729 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163730 NavigateToExistingPendingEntry(ReloadType::NONE,
3731 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343732 } else {
3733 // If there is something to reload, the successful reload will clear the
3734 // |needs_reload_| flag. Otherwise, just do it here.
3735 needs_reload_ = false;
3736 }
initial.commit09911bf2008-07-26 23:55:293737}
3738
Carlos IL42b416592019-10-07 23:10:363739void NavigationControllerImpl::LoadPostCommitErrorPage(
3740 RenderFrameHost* render_frame_host,
3741 const GURL& url,
3742 const std::string& error_page_html,
3743 net::Error error) {
Rakina Zata Amni919b7922020-12-11 09:03:133744 RenderFrameHostImpl* rfhi =
3745 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:583746
3747 // Only active documents can load post-commit error pages:
3748 // - If the document is in pending deletion, the browser already committed to
3749 // destroying this RenderFrameHost so ignore loading the error page.
3750 // - If the document is in back-forward cache, it's not allowed to navigate as
3751 // it should remain frozen. Ignore the request and evict the document from
3752 // back-forward cache.
3753 // - If the document is prerendering, it can navigate but when loading error
3754 // pages, cancel prerendering.
3755 if (rfhi->IsInactiveAndDisallowActivation())
3756 return;
3757
Rakina Zata Amni919b7922020-12-11 09:03:133758 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:413759
3760 mojom::CommonNavigationParamsPtr common_params =
3761 CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:593762 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
3763 // the frame actually committed (e.g. iframe with "src" set to a
3764 // slow-responding URL). We should rewrite the URL to about:blank in this
3765 // case, as the renderer will only think a page is an error page if it has a
3766 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:133767 common_params->url = url.is_empty() ? GURL("about:blank") : url;
John Delaney131ad362019-08-08 21:57:413768 mojom::CommitNavigationParamsPtr commit_params =
3769 CreateCommitNavigationParams();
Antonio Sartori58591c892021-04-21 06:54:333770 commit_params->original_url = common_params->url;
John Delaney131ad362019-08-08 21:57:413771
arthursonzogni70ac7302020-05-28 08:49:053772 // Error pages have a fully permissive FramePolicy.
3773 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
3774 // error pages.
3775 commit_params->frame_policy = blink::FramePolicy();
3776
John Delaney131ad362019-08-08 21:57:413777 std::unique_ptr<NavigationRequest> navigation_request =
3778 NavigationRequest::CreateBrowserInitiated(
3779 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083780 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:193781 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453782 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:063783 "" /* extra_headers */, nullptr /* frame_entry */,
3784 nullptr /* entry */, nullptr /* post_body */,
Anton Bikineevf62d1bf2021-05-15 17:56:073785 nullptr /* navigation_ui_data */, absl::nullopt /* impression */);
Carlos IL42b416592019-10-07 23:10:363786 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:413787 navigation_request->set_net_error(error);
3788 node->CreatedNavigationRequest(std::move(navigation_request));
3789 DCHECK(node->navigation_request());
3790 node->navigation_request()->BeginNavigation();
3791}
3792
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573793void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213794 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093795 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:153796 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143797 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293798}
3799
[email protected]d202a7c2012-01-04 07:53:473800void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363801 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553802 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363803 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293804
initial.commit09911bf2008-07-26 23:55:293805 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293806}
[email protected]765b35502008-08-21 00:51:203807
arthursonzogni69a6a1b2019-09-17 09:23:003808void NavigationControllerImpl::DiscardNonCommittedEntries() {
Michael Thiessen9b14d512019-09-23 21:19:473809 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:103810 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
3811 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:293812 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:473813 return;
Michael Thiessenc5676d22019-09-25 22:32:103814 }
avi45a72532015-04-07 21:01:453815 DiscardPendingEntry(false);
arthursonzogni69a6a1b2019-09-17 09:23:003816 if (delegate_)
3817 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:483818}
3819
avi7c6f35e2015-05-08 17:52:383820int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3821 int nav_entry_id) const {
3822 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3823 if (entries_[i]->GetUniqueID() == nav_entry_id)
3824 return i;
3825 }
3826 return -1;
3827}
3828
[email protected]d202a7c2012-01-04 07:53:473829void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573830 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253831 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573832 DCHECK_LE(max_index, source->GetEntryCount());
[email protected]e1cd5452010-08-26 18:03:253833 for (int i = 0; i < max_index; i++) {
Nate Chapin9f169072021-06-09 19:32:373834 // Normally, cloning a NavigationEntryImpl results in sharing
3835 // FrameNavigationEntries between the original and the clone. However, when
3836 // cloning from a different NavigationControllerImpl, we want to fork the
3837 // FrameNavigationEntries.
3838 // TODO(japhet): FNEs should not be shared between original and clone, but
3839 // we should mirror any sharing of FNEs within the |source->entries_|. We
3840 // don't currently. https://crbug.com/1211683
3841 entries_.insert(entries_.begin() + i,
3842 source->entries_[i]->CloneWithoutSharing());
[email protected]e1cd5452010-08-26 18:03:253843 }
arthursonzogni5c4c202d2017-04-25 23:41:273844 DCHECK(pending_entry_index_ == -1 ||
3845 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:253846}
[email protected]c5b88d82012-10-06 17:03:333847
3848void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:183849 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:333850 get_timestamp_callback_ = get_timestamp_callback;
3851}
[email protected]8ff00d72012-10-23 19:12:213852
Shivani Sharmaffb32b82019-04-09 16:58:473853// History manipulation intervention:
3854void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:473855 bool replace_entry,
3856 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:403857 bool is_renderer_initiated,
3858 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:453859 // Note that for a subframe, previous_document_was_activated is true if the
3860 // gesture happened in any subframe (propagated to main frame) or in the main
3861 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:473862 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:273863 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:473864 if (last_committed_entry_index_ != -1) {
shivanigithube92c33da2020-09-14 13:01:413865 // This histogram always counts when navigating away from an entry,
3866 // irrespective of whether the skippable flag was changed or not, and
3867 // whether this entry is being reused or not.
Shivani Sharmaffb32b82019-04-09 16:58:473868 UMA_HISTOGRAM_BOOLEAN(
shivanigithube92c33da2020-09-14 13:01:413869 "Navigation.BackForward.SetShouldSkipOnBackForwardUI",
3870 GetEntryAtIndex(last_committed_entry_index_)
3871 ->should_skip_on_back_forward_ui());
Shivani Sharmaffb32b82019-04-09 16:58:473872 }
3873 return;
3874 }
3875 if (last_committed_entry_index_ == -1)
3876 return;
3877
Shivani Sharmac4cc8922019-04-18 03:11:173878 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473879 UMA_HISTOGRAM_BOOLEAN("Navigation.BackForward.SetShouldSkipOnBackForwardUI",
3880 true);
3881
Alexander Timine3ec4192020-04-20 16:39:403882 // Log UKM with the URL we are navigating away from.
3883 ukm::builders::HistoryManipulationIntervention(
3884 previous_page_load_ukm_source_id)
3885 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:473886}
3887
Shivani Sharmac4cc8922019-04-18 03:11:173888void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
3889 int reference_index,
3890 bool skippable) {
3891 auto* reference_entry = GetEntryAtIndex(reference_index);
3892 reference_entry->set_should_skip_on_back_forward_ui(skippable);
3893
3894 int64_t document_sequence_number =
3895 reference_entry->root_node()->frame_entry->document_sequence_number();
3896 for (int index = 0; index < GetEntryCount(); index++) {
3897 auto* entry = GetEntryAtIndex(index);
3898 if (entry->root_node()->frame_entry->document_sequence_number() ==
3899 document_sequence_number) {
3900 entry->set_should_skip_on_back_forward_ui(skippable);
3901 }
3902 }
3903}
3904
arthursonzogni66f711c2019-10-08 14:40:363905std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
3906NavigationControllerImpl::ReferencePendingEntry() {
3907 DCHECK(pending_entry_);
3908 auto pending_entry_ref =
3909 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
3910 pending_entry_refs_.insert(pending_entry_ref.get());
3911 return pending_entry_ref;
3912}
3913
3914void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
3915 // Ignore refs that don't correspond to the current pending entry.
3916 auto it = pending_entry_refs_.find(ref);
3917 if (it == pending_entry_refs_.end())
3918 return;
3919 pending_entry_refs_.erase(it);
3920
3921 if (!pending_entry_refs_.empty())
3922 return;
3923
3924 // The pending entry may be deleted before the last PendingEntryRef.
3925 if (!pending_entry_)
3926 return;
3927
3928 // We usually clear the pending entry when the matching NavigationRequest
3929 // fails, so that an arbitrary URL isn't left visible above a committed page.
3930 //
3931 // However, we do preserve the pending entry in some cases, such as on the
3932 // initial navigation of an unmodified blank tab. We also allow the delegate
3933 // to say when it's safe to leave aborted URLs in the omnibox, to let the
3934 // user edit the URL and try again. This may be useful in cases that the
3935 // committed page cannot be attacker-controlled. In these cases, we still
3936 // allow the view to clear the pending entry and typed URL if the user
3937 // requests (e.g., hitting Escape with focus in the address bar).
3938 //
3939 // Do not leave the pending entry visible if it has an invalid URL, since this
3940 // might be formatted in an unexpected or unsafe way.
3941 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:363942 bool should_preserve_entry =
3943 (pending_entry_ == GetVisibleEntry()) &&
3944 pending_entry_->GetURL().is_valid() &&
3945 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
3946 if (should_preserve_entry)
3947 return;
3948
3949 DiscardPendingEntry(true);
3950 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3951}
3952
Titouan Rigoudy6ec70402021-02-02 15:42:193953std::unique_ptr<PolicyContainerPolicies>
3954NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:393955 RenderFrameHostImpl* rfh,
3956 bool is_same_document,
3957 NavigationRequest* request) {
Titouan Rigoudy6ec70402021-02-02 15:42:193958 if (!ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(request))
Antonio Sartori78a749f2020-11-30 12:03:393959 return nullptr;
3960
3961 if (is_same_document) {
Charlie Reis73e356242021-04-02 17:10:313962 // TODO(https://crbug.com/524208): Remove this nullptr check when we can
3963 // ensure we always have a FrameNavigationEntry here.
3964 if (!GetLastCommittedEntry())
3965 return nullptr;
3966
Antonio Sartori78a749f2020-11-30 12:03:393967 FrameNavigationEntry* previous_frame_entry =
3968 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
3969
3970 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
3971 // ensure we always have a FrameNavigationEntry here.
3972 if (!previous_frame_entry)
3973 return nullptr;
3974
Titouan Rigoudy6ec70402021-02-02 15:42:193975 const PolicyContainerPolicies* previous_policies =
3976 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:393977
Titouan Rigoudy6ec70402021-02-02 15:42:193978 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:393979 return nullptr;
3980
3981 // Make a copy of the policy container for the new FrameNavigationEntry.
Antonio Sartori5d09b30f2021-03-02 09:27:163982 return previous_policies->Clone();
Antonio Sartori78a749f2020-11-30 12:03:393983 }
3984
Antonio Sartori4f5373792021-05-31 10:56:473985 return rfh->policy_container_host()->policies().Clone();
Antonio Sartori78a749f2020-11-30 12:03:393986}
3987
Carlos Caballeroede6f8c2021-01-28 11:01:503988void NavigationControllerImpl::SetHistoryOffsetAndLength(int history_offset,
3989 int history_length) {
3990 OPTIONAL_TRACE_EVENT2(
3991 "content", "NavigationControllerImpl::SetHistoryOffsetAndLength",
3992 "history_offset", history_offset, "history_length", history_length);
3993
3994 auto callback = base::BindRepeating(
3995 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
3996 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
3997 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
3998 }
3999 },
4000 history_offset, history_length);
4001 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
4002}
4003
4004void NavigationControllerImpl::DidAccessInitialMainDocument() {
4005 // We may have left a failed browser-initiated navigation in the address bar
4006 // to let the user edit it and try again. Clear it now that content might
4007 // show up underneath it.
4008 if (!frame_tree_.IsLoading() && GetPendingEntry())
4009 DiscardPendingEntry(false);
4010
4011 // Update the URL display.
4012 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
4013}
4014
4015void NavigationControllerImpl::UpdateStateForFrame(
4016 RenderFrameHostImpl* rfhi,
4017 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:564018 OPTIONAL_TRACE_EVENT1("content",
4019 "NavigationControllerImpl::UpdateStateForFrame",
4020 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:504021 // The state update affects the last NavigationEntry associated with the given
4022 // |render_frame_host|. This may not be the last committed NavigationEntry (as
4023 // in the case of an UpdateState from a frame being swapped out). We track
4024 // which entry this is in the RenderFrameHost's nav_entry_id.
4025 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
4026 if (!entry)
4027 return;
4028
4029 FrameNavigationEntry* frame_entry =
4030 entry->GetFrameEntry(rfhi->frame_tree_node());
4031 if (!frame_entry)
4032 return;
4033
4034 // The SiteInstance might not match if we do a cross-process navigation with
4035 // replacement (e.g., auto-subframe), in which case the swap out of the old
4036 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4037 // has already been replaced and destroyed.
4038 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4039 return;
4040
4041 if (page_state == frame_entry->page_state())
4042 return; // Nothing to update.
4043
4044 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4045
4046 // The document_sequence_number and item_sequence_number recorded in the
4047 // FrameNavigationEntry should not differ from the one coming with the update,
4048 // since it must come from the same document. Do not update it if a difference
4049 // is detected, as this indicates that |frame_entry| is not the correct one.
4050 blink::ExplodedPageState exploded_state;
4051 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4052 return;
4053
4054 if (exploded_state.top.document_sequence_number !=
4055 frame_entry->document_sequence_number() ||
4056 exploded_state.top.item_sequence_number !=
4057 frame_entry->item_sequence_number()) {
4058 return;
4059 }
4060
4061 frame_entry->SetPageState(page_state);
4062 NotifyEntryChanged(entry);
4063}
4064
Aaron Colwellb731a0ae2021-03-19 19:14:474065void NavigationControllerImpl::OnStoragePartitionIdAdded(
4066 const StoragePartitionId& partition_id) {
4067 auto it = partition_config_to_id_map_.insert(
4068 std::make_pair(partition_id.config(), partition_id));
4069 bool successful_insert = it.second;
4070 DCHECK(successful_insert);
4071 if (!successful_insert) {
4072 LogStoragePartitionIdCrashKeys(it.first->second, partition_id);
4073 }
4074}
4075
4076void NavigationControllerImpl::LogStoragePartitionIdCrashKeys(
4077 const StoragePartitionId& original_partition_id,
4078 const StoragePartitionId& new_partition_id) {
4079 base::debug::SetCrashKeyString(
4080 base::debug::AllocateCrashKeyString("original_partition_id",
4081 base::debug::CrashKeySize::Size256),
4082 original_partition_id.ToString());
4083
4084 base::debug::SetCrashKeyString(
4085 base::debug::AllocateCrashKeyString("new_partition_id",
4086 base::debug::CrashKeySize::Size256),
4087 new_partition_id.ToString());
4088
4089 base::debug::DumpWithoutCrashing();
4090}
4091
Nate Chapind1fe3612021-04-16 20:45:574092std::vector<mojom::AppHistoryEntryPtr>
4093NavigationControllerImpl::PopulateSingleAppHistoryEntryVector(
4094 Direction direction,
4095 int entry_index,
4096 const url::Origin& pending_origin,
4097 FrameTreeNode* node,
4098 SiteInstance* site_instance,
4099 int64_t previous_item_sequence_number) {
4100 std::vector<mojom::AppHistoryEntryPtr> entries;
4101 int offset = direction == Direction::kForward ? 1 : -1;
4102 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4103 i += offset) {
4104 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
4105 if (!frame_entry || !frame_entry->committed_origin())
4106 break;
4107 if (site_instance != frame_entry->site_instance())
4108 break;
4109 if (!pending_origin.IsSameOriginWith(*frame_entry->committed_origin()))
4110 break;
4111 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4112 continue;
4113 blink::ExplodedPageState exploded_page_state;
4114 if (blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4115 &exploded_page_state)) {
4116 blink::ExplodedFrameState frame_state = exploded_page_state.top;
4117 mojom::AppHistoryEntryPtr entry = mojom::AppHistoryEntry::New(
4118 frame_state.app_history_key.value_or(std::u16string()),
4119 frame_state.app_history_id.value_or(std::u16string()),
4120 frame_state.url_string.value_or(std::u16string()));
4121 DCHECK(pending_origin.CanBeDerivedFrom(GURL(entry->url)));
4122 entries.push_back(std::move(entry));
4123 previous_item_sequence_number = frame_entry->item_sequence_number();
4124 }
4125 }
4126 // If |entries| was constructed by iterating backwards from
4127 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4128 // earliest-at-the-front. Reverse it.
4129 if (direction == Direction::kBack)
4130 std::reverse(entries.begin(), entries.end());
4131 return entries;
4132}
4133
4134void NavigationControllerImpl::PopulateAppHistoryEntryVectors(
4135 NavigationRequest* request) {
4136 url::Origin pending_origin =
4137 request->commit_params().origin_to_commit
4138 ? *request->commit_params().origin_to_commit
4139 : url::Origin::Create(request->common_params().url);
4140
4141 FrameTreeNode* node = request->frame_tree_node();
4142 scoped_refptr<SiteInstance> site_instance =
4143 request->GetRenderFrameHost()->GetSiteInstance();
4144
4145 // NOTE: |entry_index| is an estimate of the index where this entry will
4146 // commit, but it may be wrong in corner cases (e.g., if we are at the max
4147 // entry limit, the earliest entry will be dropped). This is ok because this
4148 // algorithm only uses |entry_index| to walk the entry list as it stands right
4149 // now, and it isn't saved for anything post-commit.
4150 int entry_index = GetPendingEntryIndex();
4151 bool will_create_new_entry = false;
4152 if (NavigationTypeUtils::IsReload(request->common_params().navigation_type) ||
4153 request->common_params().should_replace_current_entry) {
4154 entry_index = GetLastCommittedEntryIndex();
4155 } else if (entry_index == -1) {
4156 will_create_new_entry = true;
4157 entry_index = GetLastCommittedEntryIndex() + 1;
4158 }
4159
4160 int64_t pending_item_sequence_number = 0;
4161 if (auto* pending_entry = GetPendingEntry()) {
4162 if (auto* frame_entry = pending_entry->GetFrameEntry(node))
4163 pending_item_sequence_number = frame_entry->item_sequence_number();
4164 }
4165
4166 request->set_app_history_back_entries(PopulateSingleAppHistoryEntryVector(
4167 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
4168 pending_item_sequence_number));
4169
4170 // Don't populate forward entries if they will be truncated by a new entry.
4171 if (!will_create_new_entry) {
4172 request->set_app_history_forward_entries(
4173 PopulateSingleAppHistoryEntryVector(
4174 Direction::kForward, entry_index, pending_origin, node,
4175 site_instance.get(), pending_item_sequence_number));
4176 }
4177}
4178
Nate Chapinfbfe5af2021-06-10 17:22:084179NavigationControllerImpl::HistoryNavigationAction
4180NavigationControllerImpl::ShouldNavigateToEntryForAppHistoryKey(
4181 FrameNavigationEntry* current_entry,
4182 FrameNavigationEntry* target_entry,
4183 const std::string& app_history_key) {
4184 if (!target_entry || !target_entry->committed_origin())
4185 return HistoryNavigationAction::kStopLooking;
4186 if (current_entry->site_instance() != target_entry->site_instance())
4187 return HistoryNavigationAction::kStopLooking;
4188 if (!current_entry->committed_origin()->IsSameOriginWith(
4189 *target_entry->committed_origin())) {
4190 return HistoryNavigationAction::kStopLooking;
4191 }
4192
4193 // NOTE: We don't actually care between kSameDocument and
4194 // kDifferentDocument, so always use kDifferentDocument by convention.
4195 if (target_entry->app_history_key() == app_history_key)
4196 return HistoryNavigationAction::kDifferentDocument;
4197 return HistoryNavigationAction::kKeepLooking;
4198}
4199
4200void NavigationControllerImpl::NavigateToAppHistoryKey(FrameTreeNode* node,
4201 const std::string& key) {
4202 FrameNavigationEntry* current_entry =
4203 GetLastCommittedEntry()->GetFrameEntry(node);
4204 if (!current_entry)
4205 return;
4206
4207 // We want to find the nearest matching entry that is contiguously
4208 // same-instance and same-origin. Check back first, then forward.
4209 // TODO(japhet): Link spec here once it exists.
4210 for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) {
4211 auto result = ShouldNavigateToEntryForAppHistoryKey(
4212 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4213 if (result == HistoryNavigationAction::kStopLooking)
4214 break;
4215 if (result != HistoryNavigationAction::kKeepLooking) {
4216 GoToIndex(i);
4217 return;
4218 }
4219 }
4220 for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) {
4221 auto result = ShouldNavigateToEntryForAppHistoryKey(
4222 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4223 if (result == HistoryNavigationAction::kStopLooking)
4224 break;
4225 if (result != HistoryNavigationAction::kKeepLooking) {
4226 GoToIndex(i);
4227 return;
4228 }
4229 }
4230}
4231
[email protected]8ff00d72012-10-23 19:12:214232} // namespace content