blob: d36f5b076a1b7d57b7ccdc20431f8936c6ce00a3 [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"
[email protected]348fbaac2013-06-11 06:31:5147#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0048#include "base/strings/utf_string_conversions.h"
[email protected]a43858f2013-06-28 15:18:3749#include "base/time/time.h"
Carlos Caballero40b0efd2021-01-26 11:55:0050#include "base/trace_event/optional_trace_event.h"
Carlos Caballeroede6f8c2021-01-28 11:01:5051#include "base/trace_event/trace_conversion_helper.h"
ssid3e765612015-01-28 04:03:4252#include "base/trace_event/trace_event.h"
servolkf3955532015-05-16 00:01:5953#include "build/build_config.h"
[email protected]eabfe1912014-05-12 10:07:2854#include "cc/base/switches.h"
jamescookda2505812015-03-20 18:01:1855#include "content/browser/bad_message.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3956#include "content/browser/blob_storage/chrome_blob_storage_context.h"
[email protected]825b1662012-03-12 19:07:3157#include "content/browser/browser_url_handler_impl.h"
[email protected]5f2aa722013-08-07 16:59:4158#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0459#include "content/browser/dom_storage/session_storage_namespace_impl.h"
danakjc492bf82020-09-09 20:02:4460#include "content/browser/renderer_host/debug_urls.h"
61#include "content/browser/renderer_host/frame_tree.h"
Carlos Caballero40b0efd2021-01-26 11:55:0062#include "content/browser/renderer_host/frame_tree_node.h"
danakjc492bf82020-09-09 20:02:4463#include "content/browser/renderer_host/navigation_entry_impl.h"
64#include "content/browser/renderer_host/navigation_request.h"
65#include "content/browser/renderer_host/navigator.h"
66#include "content/browser/renderer_host/render_frame_host_delegate.h"
Carlos Caballero40b0efd2021-01-26 11:55:0067#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]b6583592012-01-25 19:52:3368#include "content/browser/site_instance_impl.h"
Kunihiko Sakamoto346a74e2021-03-10 08:57:4869#include "content/browser/web_package/subresource_web_bundle_navigation_info.h"
Tsuyoshi Horo37493af2019-11-21 23:43:1470#include "content/browser/web_package/web_bundle_navigation_info.h"
Camille Lamy5193caa2018-10-12 11:59:4271#include "content/common/content_constants_internal.h"
[email protected]37567b432014-02-12 01:12:2272#include "content/common/frame_messages.h"
Nate Chapinb2189ba2021-04-13 23:46:5473#include "content/common/navigation_params_utils.h"
Nasko Oskovae49e292020-08-13 02:08:5174#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1175#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1976#include "content/public/browser/content_browser_client.h"
[email protected]d9083482012-01-06 00:38:4677#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0078#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3879#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1680#include "content/public/browser/notification_types.h"
Lei Zhang96031532019-10-10 19:05:4781#include "content/public/browser/render_view_host.h"
[email protected]9677a3c2012-12-22 04:18:5882#include "content/public/browser/render_widget_host.h"
83#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1084#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3485#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:1986#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:3887#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:1088#include "content/public/common/content_features.h"
Lei Zhang96031532019-10-10 19:05:4789#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4390#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:5991#include "media/base/mime_util.h"
[email protected]a23de8572009-06-03 02:16:3292#include "net/base/escape.h"
Arthur Sonzogni620cec62018-12-13 13:08:5793#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:1894#include "services/metrics/public/cpp/ukm_builders.h"
95#include "services/metrics/public/cpp/ukm_recorder.h"
Yue Ru Sun128804932020-09-30 22:19:1796#include "services/metrics/public/cpp/ukm_source_id.h"
[email protected]9677a3c2012-12-22 04:18:5897#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3998#include "third_party/blink/public/common/blob/blob_utils.h"
Gyuyoung Kim107c2a02021-04-13 01:49:3099#include "third_party/blink/public/common/chrome_debug_urls.h"
Miyoung Shin1c565c912021-03-17 12:11:21100#include "third_party/blink/public/common/history/session_history_constants.h"
Blink Reformata30d4232018-04-07 15:31:06101#include "third_party/blink/public/common/mime_util/mime_util.h"
Carlos Caballeroede6f8c2021-01-28 11:01:50102#include "third_party/blink/public/common/page_state/page_state_serialization.h"
[email protected]cca6f392014-05-28 21:32:26103#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:29104
[email protected]8ff00d72012-10-23 19:12:21105namespace content {
[email protected]e9ba4472008-09-14 15:42:43106namespace {
107
108// Invoked when entries have been pruned, or removed. For example, if the
109// current entries are [google, digg, yahoo], with the current entry google,
110// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47111void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50112 int index,
[email protected]c12bf1a12008-09-17 16:28:49113 int count) {
[email protected]8ff00d72012-10-23 19:12:21114 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50115 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49116 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14117 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43118}
119
[email protected]e9ba4472008-09-14 15:42:43120// Configure all the NavigationEntries in entries for restore. This resets
121// the transition type to reload and makes sure the content state isn't empty.
122void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57123 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48124 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47125 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43126 // Use a transition type of reload so that we don't incorrectly increase
127 // the typed count.
Lei Zhang96031532019-10-10 19:05:47128 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
129 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43130 }
131}
132
[email protected]bf70edce2012-06-20 22:32:22133// Determines whether or not we should be carrying over a user agent override
134// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57135bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22136 return last_entry && last_entry->GetIsOverridingUserAgent();
137}
138
Camille Lamy5193caa2018-10-12 11:59:42139// Determines whether to override user agent for a navigation.
140bool ShouldOverrideUserAgent(
141 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57142 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42143 switch (override_user_agent) {
144 case NavigationController::UA_OVERRIDE_INHERIT:
145 return ShouldKeepOverride(last_committed_entry);
146 case NavigationController::UA_OVERRIDE_TRUE:
147 return true;
148 case NavigationController::UA_OVERRIDE_FALSE:
149 return false;
Camille Lamy5193caa2018-10-12 11:59:42150 }
151 NOTREACHED();
152 return false;
153}
154
clamy0a656e42018-02-06 18:18:28155// Returns true this navigation should be treated as a reload. For e.g.
156// navigating to the last committed url via the address bar or clicking on a
157// link which results in a navigation to the last committed or pending
158// navigation, etc.
Fergal Daly766177d2020-07-07 07:54:04159// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
160// |base_url_for_data_url|, |transition_type| correspond to the new navigation
161// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
162// navigation that committed.
163bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
164 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57165 const GURL& virtual_url,
166 const GURL& base_url_for_data_url,
167 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57168 bool is_post,
169 bool is_reload,
170 bool is_navigation_to_existing_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57171 NavigationEntryImpl* last_committed_entry) {
Fergal Daly766177d2020-07-07 07:54:04172 if (is_reload || is_navigation_to_existing_entry)
clamy0a656e42018-02-06 18:18:28173 return false;
clamy0a656e42018-02-06 18:18:28174 // Only convert to reload if at least one navigation committed.
175 if (!last_committed_entry)
ananta3bdd8ae2016-12-22 17:11:55176 return false;
177
arthursonzogni7a8243682017-12-14 16:41:42178 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28179 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42180 return false;
181
ananta3bdd8ae2016-12-22 17:11:55182 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
183 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
184 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28185 bool transition_type_can_be_converted = false;
186 if (ui::PageTransitionCoreTypeIs(transition_type,
187 ui::PAGE_TRANSITION_RELOAD) &&
188 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
189 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55190 }
clamy0a656e42018-02-06 18:18:28191 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55192 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28193 transition_type_can_be_converted = true;
194 }
195 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
196 transition_type_can_be_converted = true;
197 if (!transition_type_can_be_converted)
198 return false;
199
200 // This check is required for cases like view-source:, etc. Here the URL of
201 // the navigation entry would contain the url of the page, while the virtual
202 // URL contains the full URL including the view-source prefix.
203 if (virtual_url != last_committed_entry->GetVirtualURL())
204 return false;
205
Fergal Daly766177d2020-07-07 07:54:04206 // Check that the URLs match.
207 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
208 // If there's no frame entry then by definition the URLs don't match.
209 if (!frame_entry)
210 return false;
211
212 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28213 return false;
214
215 // This check is required for Android WebView loadDataWithBaseURL. Apps
216 // can pass in anything in the base URL and we need to ensure that these
217 // match before classifying it as a reload.
218 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
219 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
220 return false;
ananta3bdd8ae2016-12-22 17:11:55221 }
222
clamy0a656e42018-02-06 18:18:28223 // Skip entries with SSL errors.
224 if (last_committed_entry->ssl_error())
225 return false;
226
227 // Don't convert to a reload when the last navigation was a POST or the new
228 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04229 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28230 return false;
231
232 return true;
ananta3bdd8ae2016-12-22 17:11:55233}
234
Nasko Oskov03912102019-01-11 00:21:32235bool DoesURLMatchOriginForNavigation(
236 const GURL& url,
Kunihiko Sakamoto346a74e2021-03-10 08:57:48237 const base::Optional<url::Origin>& origin,
238 SubresourceWebBundleNavigationInfo*
239 subresource_web_bundle_navigation_info) {
Nasko Oskov03912102019-01-11 00:21:32240 // If there is no origin supplied there is nothing to match. This can happen
241 // for navigations to a pending entry and therefore it should be allowed.
242 if (!origin)
243 return true;
244
Kunihiko Sakamoto346a74e2021-03-10 08:57:48245 if (url.SchemeIs(url::kUrnScheme) && subresource_web_bundle_navigation_info) {
246 // Urn: subframe from WebBundle has an opaque origin derived from the
247 // Bundle's origin.
248 return origin->CanBeDerivedFrom(
249 subresource_web_bundle_navigation_info->bundle_url());
250 }
251
Nasko Oskov03912102019-01-11 00:21:32252 return origin->CanBeDerivedFrom(url);
253}
254
255base::Optional<url::Origin> GetCommittedOriginForFrameEntry(
arthursonzogni73fe3212020-11-17 13:24:07256 const mojom::DidCommitProvisionalLoadParams& params) {
Nasko Oskov03912102019-01-11 00:21:32257 // Error pages commit in an opaque origin, yet have the real URL that resulted
258 // in an error as the |params.url|. Since successful reload of an error page
259 // should commit in the correct origin, setting the opaque origin on the
260 // FrameNavigationEntry will be incorrect.
261 if (params.url_is_unreachable)
262 return base::nullopt;
263
264 return base::make_optional(params.origin);
265}
266
Camille Lamy5193caa2018-10-12 11:59:42267bool IsValidURLForNavigation(bool is_main_frame,
268 const GURL& virtual_url,
269 const GURL& dest_url) {
270 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
271 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
272 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
273 << virtual_url.possibly_invalid_spec();
274 return false;
275 }
276
277 // Don't attempt to navigate to non-empty invalid URLs.
278 if (!dest_url.is_valid() && !dest_url.is_empty()) {
279 LOG(WARNING) << "Refusing to load invalid URL: "
280 << dest_url.possibly_invalid_spec();
281 return false;
282 }
283
284 // The renderer will reject IPC messages with URLs longer than
285 // this limit, so don't attempt to navigate with a longer URL.
286 if (dest_url.spec().size() > url::kMaxURLChars) {
287 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
288 << " characters.";
289 return false;
290 }
291
Aaron Colwell33109c592020-04-21 21:31:19292 // Reject renderer debug URLs because they should have been handled before
293 // we get to this point. This check handles renderer debug URLs
294 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
295 // provides defense-in-depth if a renderer debug URL manages to get here via
296 // some other path. We want to reject the navigation here so it doesn't
297 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30298 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19299 LOG(WARNING) << "Refusing to load renderer debug URL: "
300 << dest_url.possibly_invalid_spec();
301 return false;
302 }
303
Camille Lamy5193caa2018-10-12 11:59:42304 return true;
305}
306
Mikel Astizba9cf2fd2017-12-17 10:38:10307// See replaced_navigation_entry_data.h for details: this information is meant
308// to ensure |*output_entry| keeps track of its original URL (landing page in
309// case of server redirects) as it gets replaced (e.g. history.replaceState()),
310// without overwriting it later, for main frames.
311void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57312 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10313 NavigationEntryImpl* output_entry) {
314 if (output_entry->GetReplacedEntryData().has_value())
315 return;
316
317 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57318 data.first_committed_url = replaced_entry->GetURL();
319 data.first_timestamp = replaced_entry->GetTimestamp();
320 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29321 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10322}
323
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51324mojom::NavigationType GetNavigationType(const GURL& old_url,
325 const GURL& new_url,
326 ReloadType reload_type,
327 NavigationEntryImpl* entry,
328 const FrameNavigationEntry& frame_entry,
danakjd83d706d2020-11-25 22:11:12329 bool has_pending_cross_document_commit,
danakjb952ef12021-01-14 19:58:49330 bool is_currently_error_page,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51331 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23332 // Reload navigations
333 switch (reload_type) {
334 case ReloadType::NORMAL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51335 return mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23336 case ReloadType::BYPASSING_CACHE:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51337 return mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23338 case ReloadType::ORIGINAL_REQUEST_URL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51339 return mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23340 case ReloadType::NONE:
341 break; // Fall through to rest of function.
342 }
343
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08344 if (entry->IsRestored()) {
Lei Zhang96031532019-10-10 19:05:47345 return entry->GetHasPostData() ? mojom::NavigationType::RESTORE_WITH_POST
346 : mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23347 }
348
danakjb952ef12021-01-14 19:58:49349 const bool can_be_same_document =
350 // A pending cross-document commit means this navigation will not occur in
351 // the current document, as that document would end up being replaced in
352 // the meantime.
353 !has_pending_cross_document_commit &&
354 // If the current document is an error page, we should always treat it as
355 // a different-document navigation so that we'll attempt to load the
356 // document we're navigating to (and not stay in the current error page).
357 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12358
clamyea99ea12018-05-28 13:54:23359 // History navigations.
360 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12361 return can_be_same_document && is_same_document_history_load
Lei Zhang96031532019-10-10 19:05:47362 ? mojom::NavigationType::HISTORY_SAME_DOCUMENT
363 : mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23364 }
365 DCHECK(!is_same_document_history_load);
366
367 // A same-document fragment-navigation happens when the only part of the url
368 // that is modified is after the '#' character.
369 //
370 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12371 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23372 //
373 // Note: this check is only valid for navigations that are not history
374 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
375 // history navigation from 'A#foo' to 'A#bar' is not a same-document
376 // navigation, but a different-document one. This is why history navigation
377 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47378 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
379 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12380
381 // The one case where we do the wrong thing here and incorrectly choose
382 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
383 // the renderer is a frameset. All frameset navigations should be
384 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
385 // navigation would do the right thing, as it would send it to the browser and
386 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
387 // into this method). But since we can't tell that case here for browser-
388 // initiated navigations, we have to get the renderer involved. In that case
389 // the navigation would be restarted due to the renderer spending a reply of
390 // mojom::CommitResult::RestartCrossDocument.
391
392 return can_be_same_document && is_same_doc
393 ? mojom::NavigationType::SAME_DOCUMENT
394 : mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23395}
396
Camille Lamy5193caa2018-10-12 11:59:42397// Adjusts the original input URL if needed, to get the URL to actually load and
398// the virtual URL, which may differ.
399void RewriteUrlForNavigation(const GURL& original_url,
400 BrowserContext* browser_context,
401 GURL* url_to_load,
402 GURL* virtual_url,
403 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42404 // Allow the browser URL handler to rewrite the URL. This will, for example,
405 // remove "view-source:" from the beginning of the URL to get the URL that
406 // will actually be loaded. This real URL won't be shown to the user, just
407 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31408 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42409 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
410 url_to_load, browser_context, reverse_on_redirect);
411}
412
413#if DCHECK_IS_ON()
414// Helper sanity check function used in debug mode.
415void ValidateRequestMatchesEntry(NavigationRequest* request,
416 NavigationEntryImpl* entry) {
417 if (request->frame_tree_node()->IsMainFrame()) {
418 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
419 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
420 request->common_params().transition, entry->GetTransitionType()));
421 }
422 DCHECK_EQ(request->common_params().should_replace_current_entry,
423 entry->should_replace_entry());
Nasko Oskovc36327d2019-01-03 23:23:04424 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42425 entry->should_clear_history_list());
426 DCHECK_EQ(request->common_params().has_user_gesture,
427 entry->has_user_gesture());
428 DCHECK_EQ(request->common_params().base_url_for_data_url,
429 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04430 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42431 entry->GetCanLoadLocalResources());
432 DCHECK_EQ(request->common_params().started_from_context_menu,
433 entry->has_started_from_context_menu());
434
435 FrameNavigationEntry* frame_entry =
436 entry->GetFrameEntry(request->frame_tree_node());
437 if (!frame_entry) {
438 NOTREACHED();
439 return;
440 }
441
Camille Lamy5193caa2018-10-12 11:59:42442 DCHECK_EQ(request->common_params().method, frame_entry->method());
443
Nasko Oskovc36327d2019-01-03 23:23:04444 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42445 if (redirect_size == frame_entry->redirect_chain().size()) {
446 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04447 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42448 frame_entry->redirect_chain()[i]);
449 }
450 } else {
451 NOTREACHED();
452 }
453}
454#endif // DCHECK_IS_ON()
455
Dave Tapuska8bfd84c2019-03-26 20:47:16456// Returns whether the session history NavigationRequests in |navigations|
457// would stay within the subtree of the sandboxed iframe in
458// |sandbox_frame_tree_node_id|.
459bool DoesSandboxNavigationStayWithinSubtree(
460 int sandbox_frame_tree_node_id,
461 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
462 for (auto& item : navigations) {
463 bool within_subtree = false;
464 // Check whether this NavigationRequest affects a frame within the
465 // sandboxed frame's subtree by walking up the tree looking for the
466 // sandboxed frame.
467 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03468 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16469 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
470 within_subtree = true;
471 break;
472 }
473 }
474 if (!within_subtree)
475 return false;
476 }
477 return true;
478}
479
Titouan Rigoudy6ec70402021-02-02 15:42:19480bool ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(
Antonio Sartori78a749f2020-11-30 12:03:39481 const NavigationRequest* request) {
482 // For local schemes we need to store the policy container in the
483 // FrameNavigationEntry, so that we can reload it in case of history
484 // navigation.
485 //
486 // TODO(https://crbug.com/1146361 and https://crbug.com/1146362): blob: and
487 // filesystem: should be removed from this list when we have properly
488 // implemented storing their policy container in the respective store.
489 return (request->common_params().url.SchemeIs(url::kAboutScheme) ||
490 request->common_params().url.SchemeIs(url::kDataScheme) ||
491 request->common_params().url.SchemeIsBlob() ||
492 request->common_params().url.SchemeIsFileSystem());
493}
494
[email protected]e9ba4472008-09-14 15:42:43495} // namespace
496
arthursonzogni66f711c2019-10-08 14:40:36497// NavigationControllerImpl::PendingEntryRef------------------------------------
498
499NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
500 base::WeakPtr<NavigationControllerImpl> controller)
501 : controller_(controller) {}
502
503NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
504 if (!controller_) // Can be null with interstitials.
505 return;
506
507 controller_->PendingEntryRefDeleted(this);
508}
509
[email protected]d202a7c2012-01-04 07:53:47510// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29511
[email protected]23a918b2014-07-15 09:51:36512const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23513
[email protected]765b35502008-08-21 00:51:20514// static
[email protected]d202a7c2012-01-04 07:53:47515size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23516 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20517
[email protected]e6fec472013-05-14 05:29:02518// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20519// when testing.
520static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29521
[email protected]71fde352011-12-29 03:29:56522// static
dcheng9bfa5162016-04-09 01:00:57523std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
524 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10525 Referrer referrer,
526 base::Optional<url::Origin> initiator_origin,
527 ui::PageTransition transition,
528 bool is_renderer_initiated,
529 const std::string& extra_headers,
530 BrowserContext* browser_context,
531 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
532 return NavigationControllerImpl::CreateNavigationEntry(
533 url, referrer, std::move(initiator_origin),
534 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Rakina Zata Amni66992a82020-06-24 03:57:52535 extra_headers, browser_context, std::move(blob_url_loader_factory),
Scott Violet5ae6c42e2020-10-28 02:47:37536 false /* should_replace_entry */, nullptr /* web_contents */);
Lukasz Anforowicz641234d52019-11-07 21:07:10537}
538
539// static
540std::unique_ptr<NavigationEntryImpl>
541NavigationControllerImpl::CreateNavigationEntry(
542 const GURL& url,
543 Referrer referrer,
544 base::Optional<url::Origin> initiator_origin,
545 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57546 ui::PageTransition transition,
547 bool is_renderer_initiated,
548 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09549 BrowserContext* browser_context,
Rakina Zata Amni66992a82020-06-24 03:57:52550 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Scott Violet5ae6c42e2020-10-28 02:47:37551 bool should_replace_entry,
552 WebContents* web_contents) {
Camille Lamy5193caa2018-10-12 11:59:42553 GURL url_to_load;
554 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56555 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42556 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
557 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56558
Lukasz Anforowicz641234d52019-11-07 21:07:10559 // Let the NTP override the navigation params and pretend that this is a
560 // browser-initiated, bookmark-like navigation.
561 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violet5ae6c42e2020-10-28 02:47:37562 web_contents, source_site_instance, &transition, &is_renderer_initiated,
563 &referrer, &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10564
Patrick Monettef507e982019-06-19 20:18:06565 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28566 nullptr, // The site instance for tabs is sent on navigation
567 // (WebContents::GetSiteInstance).
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58568 url_to_load, referrer, initiator_origin, std::u16string(), transition,
Camille Lamy5193caa2018-10-12 11:59:42569 is_renderer_initiated, blob_url_loader_factory);
570 entry->SetVirtualURL(virtual_url);
571 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56572 entry->set_update_virtual_url_with_url(reverse_on_redirect);
573 entry->set_extra_headers(extra_headers);
Rakina Zata Amni66992a82020-06-24 03:57:52574 entry->set_should_replace_entry(should_replace_entry);
Patrick Monettef507e982019-06-19 20:18:06575 return entry;
[email protected]71fde352011-12-29 03:29:56576}
577
[email protected]cdcb1dee2012-01-04 00:46:20578// static
579void NavigationController::DisablePromptOnRepost() {
580 g_check_for_repost = false;
581}
582
[email protected]c5b88d82012-10-06 17:03:33583base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
584 base::Time t) {
585 // If |t| is between the water marks, we're in a run of duplicates
586 // or just getting out of it, so increase the high-water mark to get
587 // a time that probably hasn't been used before and return it.
588 if (low_water_mark_ <= t && t <= high_water_mark_) {
589 high_water_mark_ += base::TimeDelta::FromMicroseconds(1);
590 return high_water_mark_;
591 }
592
593 // Otherwise, we're clear of the last duplicate run, so reset the
594 // water marks.
595 low_water_mark_ = high_water_mark_ = t;
596 return t;
597}
598
ckitagawa0faa5e42020-06-17 17:30:54599NavigationControllerImpl::ScopedShowRepostDialogForTesting::
600 ScopedShowRepostDialogForTesting()
601 : was_disallowed_(g_check_for_repost) {
602 g_check_for_repost = true;
603}
604
605NavigationControllerImpl::ScopedShowRepostDialogForTesting::
606 ~ScopedShowRepostDialogForTesting() {
607 g_check_for_repost = was_disallowed_;
608}
609
[email protected]d202a7c2012-01-04 07:53:47610NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00611 BrowserContext* browser_context,
612 FrameTree& frame_tree,
613 NavigationControllerDelegate* delegate)
614 : frame_tree_(frame_tree),
615 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57616 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22617 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47618 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37619 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29620}
621
[email protected]d202a7c2012-01-04 07:53:47622NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00623 // The NavigationControllerImpl might be called inside its delegate
624 // destructor. Calling it is not valid anymore.
625 delegate_ = nullptr;
626 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29627}
628
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57629WebContents* NavigationControllerImpl::GetWebContents() {
[email protected]ec6c05f2013-10-23 18:41:57630 return delegate_->GetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32631}
632
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57633BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55634 return browser_context_;
635}
636
[email protected]d202a7c2012-01-04 07:53:47637void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30638 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36639 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57640 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57641 // Verify that this controller is unused and that the input is valid.
Lei Zhang96031532019-10-10 19:05:47642 DCHECK_EQ(0, GetEntryCount());
643 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57644 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14645 selected_navigation < static_cast<int>(entries->size()));
arthursonzogni5c4c202d2017-04-25 23:41:27646 DCHECK_EQ(-1, pending_entry_index_);
[email protected]ce3fa3c2009-04-20 19:55:57647
[email protected]ce3fa3c2009-04-20 19:55:57648 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44649 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03650 entries_.reserve(entries->size());
651 for (auto& entry : *entries)
dcheng36b6aec92015-12-26 06:16:36652 entries_.push_back(
653 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
avif16f85a72015-11-13 18:25:03654
655 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
656 // cleared out safely.
657 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57658
659 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36660 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57661}
662
toyoshim6142d96f2016-12-19 09:07:25663void NavigationControllerImpl::Reload(ReloadType reload_type,
664 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28665 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28666 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32667 int current_index = -1;
668
Carlos IL42b416592019-10-07 23:10:36669 if (entry_replaced_by_post_commit_error_) {
670 // If there is an entry that was replaced by a currently active post-commit
671 // error navigation, this can't be the initial navigation.
672 DCHECK(!IsInitialNavigation());
673 // If the current entry is a post commit error, we reload the entry it
674 // replaced instead. We leave the error entry in place until a commit
675 // replaces it, but the pending entry points to the original entry in the
676 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
677 // case that pending_entry_ != entries_[pending_entry_index_].
678 entry = entry_replaced_by_post_commit_error_.get();
679 current_index = GetCurrentEntryIndex();
680 } else if (IsInitialNavigation() && pending_entry_) {
681 // If we are reloading the initial navigation, just use the current
682 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32683 entry = pending_entry_;
684 // The pending entry might be in entries_ (e.g., after a Clone), so we
685 // should also update the current_index.
686 current_index = pending_entry_index_;
687 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00688 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32689 current_index = GetCurrentEntryIndex();
690 if (current_index != -1) {
creis3da03872015-02-20 21:12:32691 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32692 }
[email protected]979a4bc2013-04-24 01:27:15693 }
[email protected]241db352013-04-22 18:04:05694
[email protected]59167c22013-06-03 18:07:32695 // If we are no where, then we can't reload. TODO(darin): We should add a
696 // CanReload method.
697 if (!entry)
698 return;
699
Takashi Toyoshimac7df3c22019-06-25 14:18:47700 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26701 entry->set_reload_type(reload_type);
702
Aran Gilman37d11632019-10-08 23:07:15703 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30704 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07705 // they really want to do this. If they do, the dialog will call us back
706 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57707 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02708
[email protected]106a0812010-03-18 00:15:12709 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57710 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47711 return;
initial.commit09911bf2008-07-26 23:55:29712 }
Lei Zhang96031532019-10-10 19:05:47713
714 if (!IsInitialNavigation())
715 DiscardNonCommittedEntries();
716
717 pending_entry_ = entry;
718 pending_entry_index_ = current_index;
719 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
720
721 NavigateToExistingPendingEntry(reload_type,
722 FrameTreeNode::kFrameTreeNodeInvalidId);
initial.commit09911bf2008-07-26 23:55:29723}
724
[email protected]d202a7c2012-01-04 07:53:47725void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48726 DCHECK(pending_reload_ != ReloadType::NONE);
727 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12728}
729
[email protected]d202a7c2012-01-04 07:53:47730void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48731 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12732 NOTREACHED();
733 } else {
toyoshim6142d96f2016-12-19 09:07:25734 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48735 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12736 }
737}
738
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57739bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09740 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11741}
742
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57743bool NavigationControllerImpl::IsInitialBlankNavigation() {
creis10a4ab72015-10-13 17:22:40744 // TODO(creis): Once we create a NavigationEntry for the initial blank page,
toyoshim0df1d3a2016-09-09 09:52:48745 // we'll need to check for entry count 1 and restore_type NONE (to exclude
746 // the cloned tab case).
creis10a4ab72015-10-13 17:22:40747 return IsInitialNavigation() && GetEntryCount() == 0;
748}
749
Aran Gilman37d11632019-10-08 23:07:15750NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
751 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58752 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03753 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58754}
755
W. James MacLean1c40862c2020-04-27 21:05:57756void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
757 const url::Origin& origin) {
758 for (int i = 0; i < GetEntryCount(); i++) {
759 auto* entry = GetEntryAtIndex(i);
760 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
761 }
762 if (entry_replaced_by_post_commit_error_) {
763 // It's possible we could come back to this entry if the error
764 // page/interstitial goes away.
765 entry_replaced_by_post_commit_error_
766 ->RegisterExistingOriginToPreventOptInIsolation(origin);
767 }
768 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
769 // visiting pending_entry_, which lacks a committed origin. This will be done
770 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
771}
772
avi25764702015-06-23 15:43:37773void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57774 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00775 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37776 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27777 DCHECK_EQ(-1, pending_entry_index_);
[email protected]8ff00d72012-10-23 19:12:21778 NotificationService::current()->Notify(
Aran Gilman37d11632019-10-08 23:07:15779 NOTIFICATION_NAV_ENTRY_PENDING, Source<NavigationController>(this),
avi25764702015-06-23 15:43:37780 Details<NavigationEntry>(pending_entry_));
[email protected]765b35502008-08-21 00:51:20781}
782
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57783NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47784 if (pending_entry_)
785 return pending_entry_;
786 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20787}
788
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57789NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32790 // The pending entry is safe to return for new (non-history), browser-
791 // initiated navigations. Most renderer-initiated navigations should not
792 // show the pending entry, to prevent URL spoof attacks.
793 //
794 // We make an exception for renderer-initiated navigations in new tabs, as
795 // long as no other page has tried to access the initial empty document in
796 // the new tab. If another page modifies this blank page, a URL spoof is
797 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32798 bool safe_to_show_pending =
799 pending_entry_ &&
800 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09801 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32802 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46803 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32804
805 // Also allow showing the pending entry for history navigations in a new tab,
806 // such as Ctrl+Back. In this case, no existing page is visible and no one
807 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15808 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
809 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32810 safe_to_show_pending = true;
811
812 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19813 return pending_entry_;
814 return GetLastCommittedEntry();
815}
816
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57817int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20818 if (pending_entry_index_ != -1)
819 return pending_entry_index_;
820 return last_committed_entry_index_;
821}
822
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57823NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20824 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28825 return nullptr;
avif16f85a72015-11-13 18:25:03826 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20827}
828
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57829bool NavigationControllerImpl::CanViewSource() {
Carlos Caballeroede6f8c2021-01-28 11:01:50830 const std::string& mime_type = frame_tree_.root()
Alex Moshchuk2e470ea2021-02-03 06:46:34831 ->current_frame_host()
832 ->render_view_host()
Carlos Caballeroede6f8c2021-01-28 11:01:50833 ->contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53834 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
835 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27836 NavigationEntry* visible_entry = GetVisibleEntry();
837 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39838 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16839}
840
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57841int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27842 // The last committed entry index must always be less than the number of
Carlos IL4dea8902020-05-26 15:14:29843 // entries. If there are no entries, it must be -1.
arthursonzogni5c4c202d2017-04-25 23:41:27844 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
845 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55846 return last_committed_entry_index_;
847}
848
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57849int NavigationControllerImpl::GetEntryCount() {
Carlos IL4dea8902020-05-26 15:14:29850 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55851 return static_cast<int>(entries_.size());
852}
853
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57854NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37855 if (index < 0 || index >= GetEntryCount())
856 return nullptr;
857
avif16f85a72015-11-13 18:25:03858 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25859}
860
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57861NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47862 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20863}
864
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57865int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46866 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03867}
868
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57869bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03870 if (!base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention))
871 return CanGoToOffset(-1);
872
873 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
874 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
875 return true;
876 }
877 return false;
[email protected]765b35502008-08-21 00:51:20878}
879
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57880bool NavigationControllerImpl::CanGoForward() {
WangHui74286d52021-03-31 16:17:15881 if (!base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention))
882 return CanGoToOffset(1);
883
884 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
885 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
886 return true;
887 }
888 return false;
[email protected]765b35502008-08-21 00:51:20889}
890
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57891bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03892 int index = GetIndexForOffset(offset);
893 return index >= 0 && index < GetEntryCount();
894}
895
WangHui74286d52021-03-31 16:17:15896#if defined(OS_ANDROID)
897bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
898 if (!base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention))
899 return CanGoToOffset(offset);
900
901 if (offset == 0)
902 return true;
903 int increment = offset > 0 ? 1 : -1;
904 int non_skippable_entries = 0;
905 for (int index = GetIndexForOffset(increment);
906 index >= 0 && index < GetEntryCount(); index += increment) {
907 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
908 non_skippable_entries++;
909
910 if (non_skippable_entries == std::abs(offset))
911 return true;
912 }
913 return false;
914}
915#endif
916
[email protected]d202a7c2012-01-04 07:53:47917void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06918 int target_index = GetIndexForOffset(-1);
919
920 // Log metrics for the number of entries that are eligible for skipping on
Shivani Sharma2d5b4b6b2019-01-08 16:07:16921 // back button and move the target index past the skippable entries, if
922 // history intervention is enabled.
shivanisha55201872018-12-13 04:29:06923 int count_entries_skipped = 0;
Shivani Sharma298d12852019-01-22 20:04:03924 bool all_skippable_entries = true;
Aran Gilman37d11632019-10-08 23:07:15925 bool history_intervention_enabled =
926 base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention);
shivanisha55201872018-12-13 04:29:06927 for (int index = target_index; index >= 0; index--) {
Shivani Sharma2d5b4b6b2019-01-08 16:07:16928 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
shivanisha55201872018-12-13 04:29:06929 count_entries_skipped++;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16930 } else {
931 if (history_intervention_enabled)
932 target_index = index;
Shivani Sharma298d12852019-01-22 20:04:03933 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06934 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16935 }
shivanisha55201872018-12-13 04:29:06936 }
Miyoung Shin1c565c912021-03-17 12:11:21937
shivanisha55201872018-12-13 04:29:06938 UMA_HISTOGRAM_ENUMERATION("Navigation.BackForward.BackTargetSkipped",
Miyoung Shin1c565c912021-03-17 12:11:21939 count_entries_skipped,
940 blink::kMaxSessionHistoryEntries);
Shivani Sharma298d12852019-01-22 20:04:03941 UMA_HISTOGRAM_BOOLEAN("Navigation.BackForward.AllBackTargetsSkippable",
942 all_skippable_entries);
943
944 // Do nothing if all entries are skippable. Normally this path would not
945 // happen as consumers would have already checked it in CanGoBack but a lot of
946 // tests do not do that.
947 if (history_intervention_enabled && all_skippable_entries)
948 return;
shivanisha55201872018-12-13 04:29:06949
shivanisha55201872018-12-13 04:29:06950 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20951}
952
[email protected]d202a7c2012-01-04 07:53:47953void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06954 int target_index = GetIndexForOffset(1);
955
956 // Log metrics for the number of entries that are eligible for skipping on
Shivani Sharma2d5b4b6b2019-01-08 16:07:16957 // forward button and move the target index past the skippable entries, if
958 // history intervention is enabled.
959 // Note that at least one entry (the last one) will be non-skippable since
960 // entries are marked skippable only when they add another entry because of
961 // redirect or pushState.
shivanisha55201872018-12-13 04:29:06962 int count_entries_skipped = 0;
Aran Gilman37d11632019-10-08 23:07:15963 bool history_intervention_enabled =
964 base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention);
shivanisha55201872018-12-13 04:29:06965 for (size_t index = target_index; index < entries_.size(); index++) {
Shivani Sharma2d5b4b6b2019-01-08 16:07:16966 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
shivanisha55201872018-12-13 04:29:06967 count_entries_skipped++;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16968 } else {
969 if (history_intervention_enabled)
970 target_index = index;
shivanisha55201872018-12-13 04:29:06971 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16972 }
shivanisha55201872018-12-13 04:29:06973 }
974 UMA_HISTOGRAM_ENUMERATION("Navigation.BackForward.ForwardTargetSkipped",
Miyoung Shin1c565c912021-03-17 12:11:21975 count_entries_skipped,
976 blink::kMaxSessionHistoryEntries);
shivanisha55201872018-12-13 04:29:06977
shivanisha55201872018-12-13 04:29:06978 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20979}
980
[email protected]d202a7c2012-01-04 07:53:47981void NavigationControllerImpl::GoToIndex(int index) {
Dave Tapuska8bfd84c2019-03-26 20:47:16982 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId);
983}
984
985void NavigationControllerImpl::GoToIndex(int index,
986 int sandbox_frame_tree_node_id) {
sunjian30574a62017-03-21 21:39:44987 TRACE_EVENT0("browser,navigation,benchmark",
988 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:20989 if (index < 0 || index >= static_cast<int>(entries_.size())) {
990 NOTREACHED();
991 return;
992 }
993
[email protected]cbab76d2008-10-13 22:42:47994 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20995
arthursonzogni5c4c202d2017-04-25 23:41:27996 DCHECK_EQ(nullptr, pending_entry_);
997 DCHECK_EQ(-1, pending_entry_index_);
998 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:20999 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:271000 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
1001 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Dave Tapuska8bfd84c2019-03-26 20:47:161002 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id);
[email protected]765b35502008-08-21 00:51:201003}
1004
[email protected]d202a7c2012-01-04 07:53:471005void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:121006 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:031007 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:201008 return;
1009
[email protected]9ba14052012-06-22 23:50:031010 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:201011}
1012
WangHui74286d52021-03-31 16:17:151013#if defined(OS_ANDROID)
1014void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
1015 // Note: This is actually reached in unit tests.
1016 if (!CanGoToOffsetWithSkipping(offset))
1017 return;
1018
1019 bool history_intervention_enabled =
1020 base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention);
1021 if (offset == 0 || !history_intervention_enabled) {
1022 GoToIndex(GetIndexForOffset(offset));
1023 return;
1024 }
1025 int increment = offset > 0 ? 1 : -1;
1026 // Find the offset without counting skippable entries.
1027 int target_index = GetIndexForOffset(increment);
1028 int non_skippable_entries = 0;
1029 for (int index = target_index; index >= 0 && index < GetEntryCount();
1030 index += increment) {
1031 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1032 non_skippable_entries++;
1033
1034 if (non_skippable_entries == std::abs(offset)) {
1035 target_index = index;
1036 break;
1037 }
1038 }
1039
1040 GoToIndex(target_index);
1041}
1042#endif
1043
[email protected]41374f12013-07-24 02:49:281044bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151045 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281046 return false;
[email protected]6a13a6c2011-12-20 21:47:121047
[email protected]43032342011-03-21 14:10:311048 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281049 return true;
[email protected]cbab76d2008-10-13 22:42:471050}
1051
Michael Thiessen9b14d512019-09-23 21:19:471052void NavigationControllerImpl::PruneForwardEntries() {
1053 DiscardNonCommittedEntries();
1054 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471055 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471056 if (num_removed <= 0)
1057 return;
1058 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1059 NotifyPrunedEntries(this, remove_start_index /* start index */,
1060 num_removed /* count */);
1061}
1062
Aran Gilman37d11632019-10-08 23:07:151063void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1064 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321065 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311066 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511067 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1068 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321069 }
1070}
1071
Aran Gilman37d11632019-10-08 23:07:151072void NavigationControllerImpl::LoadURL(const GURL& url,
1073 const Referrer& referrer,
1074 ui::PageTransition transition,
1075 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471076 LoadURLParams params(url);
1077 params.referrer = referrer;
1078 params.transition_type = transition;
1079 params.extra_headers = extra_headers;
1080 LoadURLWithParams(params);
1081}
1082
1083void NavigationControllerImpl::LoadURLWithParams(const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061084 if (params.is_renderer_initiated)
1085 DCHECK(params.initiator_origin.has_value());
1086
naskob8744d22014-08-28 17:07:431087 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151088 "NavigationControllerImpl::LoadURLWithParams", "url",
1089 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291090 bool is_explicit_navigation =
1091 GetContentClient()->browser()->IsExplicitNavigation(
1092 params.transition_type);
1093 if (HandleDebugURL(params.url, params.transition_type,
1094 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431095 // If Telemetry is running, allow the URL load to proceed as if it's
1096 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491097 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431098 cc::switches::kEnableGpuBenchmarking))
1099 return;
1100 }
[email protected]8bf1048012012-02-08 01:22:181101
[email protected]cf002332012-08-14 19:17:471102 // Checks based on params.load_type.
1103 switch (params.load_type) {
1104 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431105 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471106 break;
1107 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261108 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471109 NOTREACHED() << "Data load must use data scheme.";
1110 return;
1111 }
1112 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461113 }
[email protected]e47ae9472011-10-13 19:48:341114
[email protected]e47ae9472011-10-13 19:48:341115 // The user initiated a load, we don't need to reload anymore.
1116 needs_reload_ = false;
1117
clamy21718cc22018-06-13 13:34:241118 NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441119}
1120
Mohamed Abdelhalim833de902019-09-16 17:41:451121bool NavigationControllerImpl::PendingEntryMatchesRequest(
1122 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191123 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451124 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191125}
1126
[email protected]d202a7c2012-01-04 07:53:471127bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321128 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071129 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331130 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441131 bool is_same_document_navigation,
Shivani Sharmaffb32b82019-04-09 16:58:471132 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131133 NavigationRequest* navigation_request) {
1134 DCHECK(navigation_request);
[email protected]cd2e15742013-03-08 04:08:311135 is_initial_navigation_ = false;
1136
[email protected]0e8db942008-09-24 21:21:481137 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431138 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481139 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361140 if (entry_replaced_by_post_commit_error_) {
1141 if (is_same_document_navigation) {
1142 // Same document navigations should not be possible on error pages and
1143 // would leave the controller in a weird state. Kill the renderer if
1144 // that happens.
1145 bad_message::ReceivedBadMessage(
1146 rfh->GetProcess(), bad_message::NC_SAME_DOCUMENT_POST_COMMIT_ERROR);
1147 }
1148 // Any commit while a post-commit error page is showing should put the
1149 // original entry back, replacing the error page's entry. This includes
1150 // reloads, where the original entry was used as the pending entry and
1151 // should now be at the correct index at commit time.
1152 entries_[last_committed_entry_index_] =
1153 std::move(entry_replaced_by_post_commit_error_);
1154 }
Fergal Daly8e33cf62020-12-12 01:06:071155 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551156 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431157 if (pending_entry_ &&
1158 pending_entry_->GetIsOverridingUserAgent() !=
1159 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1160 overriding_user_agent_changed = true;
[email protected]0e8db942008-09-24 21:21:481161 } else {
Gang Wu325f03f42021-02-25 20:00:461162 // GetLastCommittedEntry() is null, so this is the first entry.
Fergal Daly8e33cf62020-12-12 01:06:071163 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481164 details->previous_entry_index = -1;
Gang Wu325f03f42021-02-25 20:00:461165 if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) {
1166 // Default setting is NOT override the user agent, so overriding the user
1167 // agent in first entry should be considered as user agent changed as
1168 // well.
1169 overriding_user_agent_changed = true;
1170 }
[email protected]0e8db942008-09-24 21:21:481171 }
[email protected]ecd9d8702008-08-28 22:10:171172
Alexander Timind2f2e4f22019-04-02 20:04:531173 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1174 // implementing back-forward cache.
1175
1176 // Create a new metrics object or reuse the previous one depending on whether
1177 // it's a main frame navigation or not.
1178 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics =
1179 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1180 GetLastCommittedEntry(), !rfh->GetParent(),
1181 params.document_sequence_number);
1182 // Notify the last active entry that we have navigated away.
1183 if (!rfh->GetParent() && !is_same_document_navigation) {
1184 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1185 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Alexander Timinde527cd2019-12-02 09:41:121186 metrics->MainFrameDidNavigateAwayFromDocument(rfh, details,
1187 navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531188 }
1189 }
1190 }
1191
fdegans9caf66a2015-07-30 21:10:421192 // If there is a pending entry at this point, it should have a SiteInstance,
1193 // except for restored entries.
jam48cea9082017-02-15 06:13:291194 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481195 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081196 pending_entry_->IsRestored());
1197 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151198 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291199 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481200 }
[email protected]e9ba4472008-09-14 15:42:431201
Nasko Oskovaee2f862018-06-15 00:05:521202 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1203 // has changed, this must be treated as a new navigation with replacement.
1204 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491205 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521206 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101207 pending_entry_->GetUniqueID() ==
1208 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521209 pending_entry_->site_instance() &&
1210 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1211 DCHECK_NE(-1, pending_entry_index_);
1212 // TODO(nasko,creis): Instead of setting this value here, set
1213 // should_replace_current_entry on the parameters we send to the
1214 // renderer process as part of CommitNavigation. The renderer should
1215 // in turn send it back here as part of |params| and it can be just
1216 // enforced and renderer process terminated on mismatch.
1217 details->did_replace_entry = true;
1218 } else {
1219 // The renderer tells us whether the navigation replaces the current entry.
1220 details->did_replace_entry = params.should_replace_current_entry;
1221 }
[email protected]bcd904482012-02-01 01:54:221222
[email protected]e9ba4472008-09-14 15:42:431223 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amnif6950d552020-11-24 03:26:101224 details->type = ClassifyNavigation(rfh, params, navigation_request);
[email protected]4bf3522c2010-08-19 21:00:201225
eugenebutee08663a2017-04-27 17:43:121226 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441227 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121228
Peter Boströmd7592132019-01-30 04:50:311229 // Make sure we do not discard the pending entry for a different ongoing
1230 // navigation when a same document commit comes in unexpectedly from the
1231 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1232 // other navigation types. See https://crbug.com/900036.
1233 // TODO(crbug.com/926009): Handle history.pushState() as well.
1234 bool keep_pending_entry = is_same_document_navigation &&
Charlie Reisc0f17d2d2021-01-12 18:52:491235 details->type == NAVIGATION_TYPE_EXISTING_ENTRY &&
Peter Boströmd7592132019-01-30 04:50:311236 pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451237 !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311238
[email protected]0e8db942008-09-24 21:21:481239 switch (details->type) {
Charlie Reisc0f17d2d2021-01-12 18:52:491240 case NAVIGATION_TYPE_NEW_ENTRY:
1241 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051242 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451243 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431244 break;
Charlie Reisc0f17d2d2021-01-12 18:52:491245 case NAVIGATION_TYPE_EXISTING_ENTRY:
1246 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1247 was_restored, navigation_request,
1248 keep_pending_entry);
[email protected]e9ba4472008-09-14 15:42:431249 break;
[email protected]8ff00d72012-10-23 19:12:211250 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471251 RendererDidNavigateNewSubframe(
1252 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451253 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431254 break;
[email protected]8ff00d72012-10-23 19:12:211255 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391256 if (!RendererDidNavigateAutoSubframe(
1257 rfh, params, details->is_same_document, navigation_request)) {
creisce0ef3572017-01-26 17:53:081258 // We don't send a notification about auto-subframe PageState during
1259 // UpdateStateForFrame, since it looks like nothing has changed. Send
1260 // it here at commit time instead.
1261 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431262 return false;
creis59d5a47cb2016-08-24 23:57:191263 }
[email protected]e9ba4472008-09-14 15:42:431264 break;
[email protected]8ff00d72012-10-23 19:12:211265 case NAVIGATION_TYPE_NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:491266 // If a pending navigation was in progress, this canceled it. We should
1267 // discard it and make sure it is removed from the URL bar. After that,
1268 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:431269 // the caller that nothing has happened.
arthursonzogni69a6a1b2019-09-17 09:23:001270 if (pending_entry_)
[email protected]20d1c992011-04-12 21:17:491271 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431272 return false;
Aran Gilman37d11632019-10-08 23:07:151273 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431274 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151275 break;
[email protected]765b35502008-08-21 00:51:201276 }
1277
[email protected]688aa65c62012-09-28 04:32:221278 // At this point, we know that the navigation has just completed, so
1279 // record the time.
1280 //
1281 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261282 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331283 base::Time timestamp =
1284 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1285 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131286 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221287
Peter Boströmd7592132019-01-30 04:50:311288 // If we aren't keeping the pending entry, there shouldn't be one at this
1289 // point. Clear it again in case any error cases above forgot to do so.
1290 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1291 // been cleared instead of protecting against it.
1292 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001293 DiscardNonCommittedEntries();
[email protected]f233e4232013-02-23 00:55:141294
[email protected]e9ba4472008-09-14 15:42:431295 // All committed entries should have nonempty content state so WebKit doesn't
1296 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471297 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321298 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221299 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441300 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531301 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1302 // implementing back-forward cache.
1303 if (!active_entry->back_forward_cache_metrics()) {
1304 active_entry->set_back_forward_cache_metrics(
1305 std::move(back_forward_cache_metrics));
1306 }
1307 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
Hajime Hoshi446206e2019-10-18 18:36:251308 navigation_request,
1309 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
naskoc7533512016-05-06 17:01:121310
Charles Reisc0507202017-09-21 00:40:021311 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1312 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1313 // A mismatch can occur if the renderer lies or due to a unique name collision
1314 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121315 FrameNavigationEntry* frame_entry =
1316 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021317 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1318 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031319 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081320 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1321 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031322 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201323
1324 // Remember the bindings the renderer process has at this point, so that
1325 // we do not grant this entry additional bindings if we come back to it.
1326 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301327 }
[email protected]132e281a2012-07-31 18:32:441328
[email protected]97d8f0d2013-10-29 16:49:211329 // Once it is committed, we no longer need to track several pieces of state on
1330 // the entry.
naskoc7533512016-05-06 17:01:121331 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131332
[email protected]49bd30e62011-03-22 20:12:591333 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221334 // TODO(creis): This check won't pass for subframes until we create entries
1335 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001336 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421337 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591338
[email protected]e9ba4472008-09-14 15:42:431339 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001340 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001341 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311342 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531343
arthursonzogni7ddc6542021-04-09 09:16:501344 active_entry->SetIsOverridingUserAgent(
1345 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031346
[email protected]93f230e02011-06-01 14:40:001347 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291348
John Abd-El-Malek380d3c5922017-09-08 00:20:311349 if (active_entry->GetURL().SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451350 navigation_request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek969cdd94e2017-07-10 22:18:161351 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus",
1352 !!active_entry->GetSSL().certificate);
1353 }
1354
aelias100c9192017-01-13 00:01:431355 if (overriding_user_agent_changed)
1356 delegate_->UpdateOverridingUserAgent();
1357
creis03b48002015-11-04 00:54:561358 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1359 // one knows the latest NavigationEntry it is showing (whether it has
1360 // committed anything in this navigation or not). This allows things like
1361 // state and title updates from RenderFrames to apply to the latest relevant
1362 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381363 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001364 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381365 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
[email protected]e9ba4472008-09-14 15:42:431366 return true;
initial.commit09911bf2008-07-26 23:55:291367}
1368
[email protected]8ff00d72012-10-23 19:12:211369NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321370 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101371 const mojom::DidCommitProvisionalLoadParams& params,
1372 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591373 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511374 "ClassifyNavigation");
1375
avi7c6f35e2015-05-08 17:52:381376 if (params.did_create_new_entry) {
Charlie Reisc0f17d2d2021-01-12 18:52:491377 // A new entry. We may or may not have a corresponding pending entry, and
avi7c6f35e2015-05-08 17:52:381378 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001379 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491380 trace_return.set_return_reason("new entry, no parent, new entry");
1381 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381382 }
1383
1384 // When this is a new subframe navigation, we should have a committed page
1385 // in which it's a subframe. This may not be the case when an iframe is
1386 // navigated on a popup navigated to about:blank (the iframe would be
1387 // written into the popup by script on the main page). For these cases,
1388 // there isn't any navigation stuff we can do, so just ignore it.
Nasko Oskovae49e292020-08-13 02:08:511389 if (!GetLastCommittedEntry()) {
1390 trace_return.set_return_reason("new entry, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381391 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511392 }
avi7c6f35e2015-05-08 17:52:381393
1394 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511395 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381396 return NAVIGATION_TYPE_NEW_SUBFRAME;
1397 }
1398
Charlie Reisc0f17d2d2021-01-12 18:52:491399 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381400 DCHECK(!params.history_list_was_cleared);
1401
avi39c1edd32015-06-04 20:06:001402 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381403 // All manual subframes would be did_create_new_entry and handled above, so
1404 // we know this is auto.
Nasko Oskovae49e292020-08-13 02:08:511405 if (GetLastCommittedEntry()) {
1406 trace_return.set_return_reason("subframe, last commmited, auto subframe");
avi7c6f35e2015-05-08 17:52:381407 return NAVIGATION_TYPE_AUTO_SUBFRAME;
Nasko Oskovae49e292020-08-13 02:08:511408 }
Lei Zhang96031532019-10-10 19:05:471409
1410 // We ignore subframes created in non-committed pages; we'd appreciate if
1411 // people stopped doing that.
Nasko Oskovae49e292020-08-13 02:08:511412 trace_return.set_return_reason("subframe, no last commmited, ignore");
Lei Zhang96031532019-10-10 19:05:471413 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381414 }
1415
Rakina Zata Amnif6950d552020-11-24 03:26:101416 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1417 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381418 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1419 // create a new page.
1420
1421 // Just like above in the did_create_new_entry case, it's possible to
1422 // scribble onto an uncommitted page. Again, there isn't any navigation
1423 // stuff that we can do, so ignore it here as well.
avi3a5b8f32015-05-28 17:50:231424 NavigationEntry* last_committed = GetLastCommittedEntry();
Nasko Oskovae49e292020-08-13 02:08:511425 if (!last_committed) {
1426 trace_return.set_return_reason("nav entry 0, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381427 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511428 }
avi7c6f35e2015-05-08 17:52:381429
Charles Reis1378111f2017-11-08 21:44:061430 // This is history.replaceState() or history.reload().
Nasko Oskov332593c2018-08-16 17:21:341431 // TODO(nasko): With error page isolation, reloading an existing session
1432 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491433 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341434 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511435 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491436 "nav entry 0, last committed, existing entry");
1437 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381438 }
1439
Rakina Zata Amnif6950d552020-11-24 03:26:101440 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521441 // If the SiteInstance of the |pending_entry_| does not match the
1442 // SiteInstance that got committed, treat this as a new navigation with
1443 // replacement. This can happen if back/forward/reload encounters a server
1444 // redirect to a different site or an isolated error page gets successfully
1445 // reloaded into a different SiteInstance.
1446 if (pending_entry_->site_instance() &&
1447 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491448 trace_return.set_return_reason("pending matching nav entry, new entry");
1449 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521450 }
creis77c9aa32015-09-25 19:59:421451
Nasko Oskovaee2f862018-06-15 00:05:521452 if (pending_entry_index_ == -1) {
1453 // In this case, we have a pending entry for a load of a new URL but Blink
1454 // didn't do a new navigation (params.did_create_new_entry). First check
1455 // to make sure Blink didn't treat a new cross-process navigation as
1456 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161457 // we must treat it as NEW rather than the converted reload case below,
1458 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521459 if (!GetLastCommittedEntry() ||
1460 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161461 trace_return.set_return_reason("new pending, new entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491462 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521463 }
1464
1465 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161466 // We will create a pending entry, but NavigateWithoutEntry will convert
1467 // it to a reload since it's the same page and not create a new entry for
1468 // it. (The user doesn't want to have a new back/forward entry when they
1469 // do this.) Therefore we want to just ignore the pending entry and go
1470 // back to where we were (the "existing entry").
1471 trace_return.set_return_reason("new pending, existing (same) entry");
1472 return NAVIGATION_TYPE_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521473 }
avi7c6f35e2015-05-08 17:52:381474 }
1475
creis26d22632017-04-21 20:23:561476 // Everything below here is assumed to be an existing entry, but if there is
1477 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511478 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491479 trace_return.set_return_reason("no last committed, new entry");
1480 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511481 }
creis26d22632017-04-21 20:23:561482
avi7c6f35e2015-05-08 17:52:381483 if (params.intended_as_new_entry) {
1484 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491485 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1486 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161487 trace_return.set_return_reason("intended as new entry, existing entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491488 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381489 }
1490
1491 if (params.url_is_unreachable && failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101492 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381493 // If the renderer was going to a new pending entry that got cleared because
1494 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491495 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381496 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511497 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491498 "unreachable, matching pending, existing entry");
1499 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381500 }
1501
Charlie Reisc0f17d2d2021-01-12 18:52:491502 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101503 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511504 trace_return.traced_value()->SetInteger("existing_entry_index",
1505 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381506 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441507 // The renderer has committed a navigation to an entry that no longer
1508 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491509 trace_return.set_return_reason("existing entry -1, new entry");
1510 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381511 }
1512
avi7c6f35e2015-05-08 17:52:381513 // Since we weeded out "new" navigations above, we know this is an existing
1514 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491515 trace_return.set_return_reason("default return, existing entry");
1516 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381517}
1518
Charlie Reisc0f17d2d2021-01-12 18:52:491519void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321520 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071521 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361522 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441523 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471524 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451525 NavigationRequest* request) {
dcheng9bfa5162016-04-09 01:00:571526 std::unique_ptr<NavigationEntryImpl> new_entry;
creisf49dfc92016-07-26 17:05:181527 bool update_virtual_url = false;
1528
Lukasz Anforowicz435bcb582019-07-12 20:50:061529 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451530 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061531
creisf49dfc92016-07-26 17:05:181532 // First check if this is an in-page navigation. If so, clone the current
1533 // entry instead of looking at the pending entry, because the pending entry
1534 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361535 if (is_same_document && GetLastCommittedEntry()) {
Patrick Monette50e8bd82019-06-13 22:40:451536 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481537 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
creisf49dfc92016-07-26 17:05:181538 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Nasko Oskov03912102019-01-11 00:21:321539 params.url, (params.url_is_unreachable) ? nullptr : &params.origin,
Rakina Zata Amni82fafba2021-03-11 07:07:091540 Referrer(*params.referrer), initiator_origin,
1541 request->GetRedirectChain(), params.page_state, params.method,
1542 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391543 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481544 request->GetSubresourceWebBundleNavigationInfo(),
Antonio Sartori78a749f2020-11-30 12:03:391545 // We will set the document policies later in this function.
Titouan Rigoudy6ec70402021-02-02 15:42:191546 nullptr /* policy_container_policies */);
Charles Reisf44482022017-10-13 21:15:031547
creisf49dfc92016-07-26 17:05:181548 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Carlos Caballero40b0efd2021-01-26 11:55:001549 frame_entry, true, rfh->frame_tree_node(), frame_tree_.root());
jama78746e2017-02-22 17:21:571550 if (new_entry->GetURL().GetOrigin() != params.url.GetOrigin()) {
1551 // TODO(jam): we had one report of this with a URL that was redirecting to
1552 // only tildes. Until we understand that better, don't copy the cert in
1553 // this case.
1554 new_entry->GetSSL() = SSLStatus();
John Abd-El-Malek9cd697e2017-07-12 21:53:141555
John Abd-El-Malek380d3c5922017-09-08 00:20:311556 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451557 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141558 UMA_HISTOGRAM_BOOLEAN(
1559 "Navigation.SecureSchemeHasSSLStatus.NewPageInPageOriginMismatch",
1560 !!new_entry->GetSSL().certificate);
1561 }
jama78746e2017-02-22 17:21:571562 }
creisf49dfc92016-07-26 17:05:181563
Patrick Monette50e8bd82019-06-13 22:40:451564 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1565 // that |frame_entry| should now have a reference count of exactly 2: one
1566 // due to the local variable |frame_entry|, and another due to |new_entry|
1567 // also retaining one. This should never fail, because it's the main frame.
1568 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181569
1570 update_virtual_url = new_entry->update_virtual_url_with_url();
John Abd-El-Malek9cd697e2017-07-12 21:53:141571
John Abd-El-Malek380d3c5922017-09-08 00:20:311572 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451573 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141574 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus.NewPageInPage",
1575 !!new_entry->GetSSL().certificate);
1576 }
creisf49dfc92016-07-26 17:05:181577 }
1578
Harkiran Bolaria59290d62021-03-17 01:53:011579 // If this is an activation navigation from a prerendered page, transfer the
1580 // new entry from an entry already created and stored in the
1581 // NavigationRequest. |new_entry| will not have been set prior to this as
1582 // |is_same_document| is mutually exclusive with
1583 // |IsPrerenderedPageActivation|.
1584 if (request->IsPrerenderedPageActivation()) {
1585 DCHECK(!is_same_document);
1586 DCHECK(!new_entry);
1587 new_entry = request->TakePrerenderNavigationEntry();
1588 DCHECK(new_entry);
1589 }
1590
Charlie Reisc0f17d2d2021-01-12 18:52:491591 // Only make a copy of the pending entry if it is appropriate for the new
1592 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451593 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041594 // 1. The SiteInstance hasn't been assigned to something else.
1595 // 2. The pending entry was intended as a new entry, rather than being a
1596 // history navigation that was interrupted by an unrelated,
1597 // renderer-initiated navigation.
1598 // TODO(csharrison): Investigate whether we can remove some of the coarser
1599 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451600 if (!new_entry && PendingEntryMatchesRequest(request) &&
1601 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341602 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431603 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351604 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431605
[email protected]f1eb87a2011-05-06 17:49:411606 update_virtual_url = new_entry->update_virtual_url_with_url();
Camille Lamy62b826012019-02-26 09:15:471607 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451608 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141609
John Abd-El-Malek380d3c5922017-09-08 00:20:311610 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451611 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141612 UMA_HISTOGRAM_BOOLEAN(
1613 "Navigation.SecureSchemeHasSSLStatus.NewPagePendingEntryMatches",
1614 !!new_entry->GetSSL().certificate);
1615 }
creisf49dfc92016-07-26 17:05:181616 }
1617
Charlie Reisc0f17d2d2021-01-12 18:52:491618 // For cross-document commits with no matching pending entry, create a new
1619 // entry.
creisf49dfc92016-07-26 17:05:181620 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061621 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071622 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1623 initiator_origin,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:581624 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451625 params.transition, request->IsRendererInitiated(),
Lukasz Anforowicz435bcb582019-07-12 20:50:061626 nullptr); // blob_url_loader_factory
[email protected]f8f93eb2012-09-25 03:06:241627
1628 // Find out whether the new entry needs to update its virtual URL on URL
1629 // change and set up the entry accordingly. This is needed to correctly
1630 // update the virtual URL when replaceState is called after a pushState.
1631 GURL url = params.url;
1632 bool needs_update = false;
[email protected]47752982014-07-29 08:01:431633 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1634 &url, browser_context_, &needs_update);
[email protected]f8f93eb2012-09-25 03:06:241635 new_entry->set_update_virtual_url_with_url(needs_update);
1636
Charlie Reisc0f17d2d2021-01-12 18:52:491637 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411638 // update the virtual URL based on the new URL. For example, this is needed
1639 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1640 // the URL.
[email protected]f8f93eb2012-09-25 03:06:241641 update_virtual_url = needs_update;
Camille Lamy62b826012019-02-26 09:15:471642 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451643 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141644
John Abd-El-Malek380d3c5922017-09-08 00:20:311645 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451646 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141647 UMA_HISTOGRAM_BOOLEAN(
1648 "Navigation.SecureSchemeHasSSLStatus.NewPageNoMatchingEntry",
1649 !!new_entry->GetSSL().certificate);
1650 }
[email protected]e9ba4472008-09-14 15:42:431651 }
1652
Harkiran Bolaria59290d62021-03-17 01:53:011653 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
1654 // for prerendered contents, if any. This is because prerendering/activation
1655 // technically won't be creating a new document. Unlike BFCache, prerendering
1656 // creates a new NavigationEntry rather than using an existing one.
1657 if (!request->IsPrerenderedPageActivation()) {
1658 // Don't use the page type from the pending entry. Some interstitial page
1659 // may have set the type to interstitial. Once we commit, however, the page
1660 // type must always be normal or error.
1661 new_entry->set_page_type(params.url_is_unreachable ? PAGE_TYPE_ERROR
1662 : PAGE_TYPE_NORMAL);
1663 new_entry->SetURL(params.url);
1664 if (update_virtual_url)
1665 UpdateVirtualURLToURL(new_entry.get(), params.url);
1666 new_entry->SetReferrer(Referrer(*params.referrer));
1667 new_entry->SetTransitionType(params.transition);
1668 new_entry->set_site_instance(
1669 static_cast<SiteInstanceImpl*>(rfh->GetSiteInstance()));
1670 new_entry->SetOriginalRequestURL(request->GetOriginalRequestURL());
Rakina Zata Amnib597d632020-12-01 00:56:001671
Harkiran Bolaria59290d62021-03-17 01:53:011672 if (!is_same_document) {
arthursonzogni7ddc6542021-04-09 09:16:501673 DCHECK_EQ(request->is_overriding_user_agent() && !rfh->GetParent(),
Harkiran Bolaria59290d62021-03-17 01:53:011674 params.is_overriding_user_agent);
1675 } else {
1676 DCHECK_EQ(rfh->is_overriding_user_agent(),
1677 params.is_overriding_user_agent);
1678 }
1679 new_entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
[email protected]e9ba4472008-09-14 15:42:431680
Harkiran Bolaria59290d62021-03-17 01:53:011681 // Update the FrameNavigationEntry for new main frame commits.
1682 FrameNavigationEntry* frame_entry =
1683 new_entry->GetFrameEntry(rfh->frame_tree_node());
1684 frame_entry->set_frame_unique_name(rfh->frame_tree_node()->unique_name());
1685 frame_entry->set_item_sequence_number(params.item_sequence_number);
1686 frame_entry->set_document_sequence_number(params.document_sequence_number);
1687 frame_entry->set_redirect_chain(request->GetRedirectChain());
1688 frame_entry->SetPageState(params.page_state);
1689 frame_entry->set_method(params.method);
1690 frame_entry->set_post_id(params.post_id);
1691 frame_entry->set_policy_container_policies(
1692 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1693 request));
Antonio Sartori78a749f2020-11-30 12:03:391694
Harkiran Bolaria59290d62021-03-17 01:53:011695 if (!params.url_is_unreachable)
1696 frame_entry->set_committed_origin(params.origin);
1697 if (request->web_bundle_navigation_info()) {
1698 frame_entry->set_web_bundle_navigation_info(
1699 request->web_bundle_navigation_info()->Clone());
1700 }
creis8b5cd4c2015-06-19 00:11:081701
Harkiran Bolaria59290d62021-03-17 01:53:011702 // history.pushState() is classified as a navigation to a new page, but sets
1703 // is_same_document to true. In this case, we already have the title and
1704 // favicon available, so set them immediately.
1705 if (is_same_document && GetLastCommittedEntry()) {
1706 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
1707 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1708 }
[email protected]3a868f212014-08-09 10:41:191709 }
[email protected]ff64b3e2014-05-31 04:07:331710
[email protected]60d6cca2013-04-30 08:47:131711 DCHECK(!params.history_list_was_cleared || !replace_entry);
1712 // The browser requested to clear the session history when it initiated the
1713 // navigation. Now we know that the renderer has updated its state accordingly
1714 // and it is safe to also clear the browser side history.
1715 if (params.history_list_was_cleared) {
arthursonzogni69a6a1b2019-09-17 09:23:001716 DiscardNonCommittedEntries();
[email protected]60d6cca2013-04-30 08:47:131717 entries_.clear();
1718 last_committed_entry_index_ = -1;
1719 }
1720
Nasko Oskovaee2f862018-06-15 00:05:521721 // If this is a new navigation with replacement and there is a
1722 // pending_entry_ which matches the navigation reported by the renderer
1723 // process, then it should be the one replaced, so update the
1724 // last_committed_entry_index_ to use it.
1725 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101726 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521727 last_committed_entry_index_ = pending_entry_index_;
1728 }
1729
Alexander Timine3ec4192020-04-20 16:39:401730 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411731 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401732 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051733
Carlos IL42b416592019-10-07 23:10:361734 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
1735 !request->post_commit_error_page_html().empty());
[email protected]e9ba4472008-09-14 15:42:431736}
1737
Charlie Reisc0f17d2d2021-01-12 18:52:491738void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321739 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071740 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361741 bool is_same_document,
jam48cea9082017-02-15 06:13:291742 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451743 NavigationRequest* request,
Peter Boströmd7592132019-01-30 04:50:311744 bool keep_pending_entry) {
creis26d22632017-04-21 20:23:561745 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1746 << "that a last committed entry exists.";
1747
[email protected]e9ba4472008-09-14 15:42:431748 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001749 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431750
Charlie Reis7e2cb6d2021-01-26 01:27:161751 NavigationEntryImpl* entry = nullptr;
avicbdc4c12015-07-01 16:07:111752 if (params.intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:161753 // We're guaranteed to have a last committed entry if intended_as_new_entry
1754 // is true.
avicbdc4c12015-07-01 16:07:111755 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:161756 DCHECK(entry);
1757
1758 // If the NavigationRequest matches a new pending entry and is classified as
1759 // EXISTING_ENTRY, then it is a navigation to the same URL that was
1760 // converted to a reload, such as a user pressing enter in the omnibox.
1761 if (pending_entry_ && pending_entry_index_ == -1 &&
1762 pending_entry_->GetUniqueID() ==
1763 request->commit_params().nav_entry_id) {
1764 // Note: The pending entry will usually have a real ReloadType here, but
1765 // it can still be ReloadType::NONE in cases that
1766 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
1767
1768 // If we classified this correctly, the SiteInstance should not have
1769 // changed.
1770 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
1771
1772 // For converted reloads, we assign the entry's unique ID to be that of
1773 // the new one. Since this is always the result of a user action, we want
1774 // to dismiss infobars, etc. like a regular user-initiated navigation.
1775 entry->set_unique_id(pending_entry_->GetUniqueID());
1776
1777 // The extra headers may have changed due to reloading with different
1778 // headers.
1779 entry->set_extra_headers(pending_entry_->extra_headers());
1780 }
1781 // Otherwise, this was intended as a new entry but the pending entry was
1782 // lost in the meantime and no new entry was created. We are stuck at the
1783 // last committed entry.
1784
1785 // Even if this is a converted reload from pressing enter in the omnibox,
1786 // the server could redirect, requiring an update to the SSL status. If this
1787 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451788 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:161789 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:471790 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451791 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:161792 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141793
Charlie Reis7e2cb6d2021-01-26 01:27:161794 if (params.url.SchemeIs(url::kHttpsScheme) &&
Mohamed Abdelhalim833de902019-09-16 17:41:451795 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141796 bool has_cert = !!entry->GetSSL().certificate;
1797 if (is_same_document) {
1798 UMA_HISTOGRAM_BOOLEAN(
1799 "Navigation.SecureSchemeHasSSLStatus."
1800 "ExistingPageSameDocumentIntendedAsNew",
1801 has_cert);
1802 } else {
1803 UMA_HISTOGRAM_BOOLEAN(
1804 "Navigation.SecureSchemeHasSSLStatus."
1805 "ExistingPageDifferentDocumentIntendedAsNew",
1806 has_cert);
1807 }
1808 }
Rakina Zata Amnif6950d552020-11-24 03:26:101809 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:111810 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:101811 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b90ce2016-09-01 16:58:161812
eugenebut604866f2017-05-10 21:35:361813 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451814 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361815 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1816 // this was a restored same document navigation entry, then it won't have
1817 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1818 // navigated it.
jam48cea9082017-02-15 06:13:291819 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
1820 if (entry->GetURL().GetOrigin() == last_entry->GetURL().GetOrigin() &&
1821 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1822 was_restored) {
1823 entry->GetSSL() = last_entry->GetSSL();
1824 }
1825 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451826 // In rapid back/forward navigations |request| sometimes won't have a cert
1827 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191828 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451829 if (request->GetSSLInfo().has_value() &&
1830 request->GetSSLInfo()->is_valid()) {
1831 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
1832 } else if (entry->GetURL().GetOrigin() != request->GetURL().GetOrigin()) {
John Abd-El-Malek3f247082017-12-07 19:02:191833 entry->GetSSL() = SSLStatus();
1834 }
jam48cea9082017-02-15 06:13:291835 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141836
John Abd-El-Malek380d3c5922017-09-08 00:20:311837 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451838 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141839 bool has_cert = !!entry->GetSSL().certificate;
1840 if (is_same_document && was_restored) {
1841 UMA_HISTOGRAM_BOOLEAN(
1842 "Navigation.SecureSchemeHasSSLStatus."
1843 "ExistingPageSameDocumentRestoredBrowserInitiated",
1844 has_cert);
1845 } else if (is_same_document && !was_restored) {
1846 UMA_HISTOGRAM_BOOLEAN(
1847 "Navigation.SecureSchemeHasSSLStatus."
1848 "ExistingPageSameDocumentBrowserInitiated",
1849 has_cert);
1850 } else if (!is_same_document && was_restored) {
1851 UMA_HISTOGRAM_BOOLEAN(
1852 "Navigation.SecureSchemeHasSSLStatus."
1853 "ExistingPageRestoredBrowserInitiated",
1854 has_cert);
1855 } else {
1856 UMA_HISTOGRAM_BOOLEAN(
1857 "Navigation.SecureSchemeHasSSLStatus.ExistingPageBrowserInitiated",
1858 has_cert);
1859 }
1860 }
avicbdc4c12015-07-01 16:07:111861 } else {
1862 // This is renderer-initiated. The only kinds of renderer-initated
Charlie Reisc0f17d2d2021-01-12 18:52:491863 // navigations that are EXISTING_ENTRY are reloads and history.replaceState,
avicbdc4c12015-07-01 16:07:111864 // which land us at the last committed entry.
1865 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101866
Mikel Astizba9cf2fd2017-12-17 10:38:101867 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1868 // to avoid misleading interpretations (e.g. URLs paired with
1869 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1870 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1871 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1872
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571873 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:101874
eugenebut604866f2017-05-10 21:35:361875 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451876 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361877 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471878 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451879 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141880
John Abd-El-Malek380d3c5922017-09-08 00:20:311881 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451882 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141883 bool has_cert = !!entry->GetSSL().certificate;
1884 if (is_same_document) {
1885 UMA_HISTOGRAM_BOOLEAN(
1886 "Navigation.SecureSchemeHasSSLStatus."
1887 "ExistingPageSameDocumentRendererInitiated",
1888 has_cert);
1889 } else {
1890 UMA_HISTOGRAM_BOOLEAN(
1891 "Navigation.SecureSchemeHasSSLStatus."
1892 "ExistingPageDifferentDocumentRendererInitiated",
1893 has_cert);
1894 }
1895 }
avicbdc4c12015-07-01 16:07:111896 }
1897 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:431898
[email protected]5ccd4dc2012-10-24 02:28:141899 // The URL may have changed due to redirects.
wjmaclean7431bb22015-02-19 14:53:431900 entry->set_page_type(params.url_is_unreachable ? PAGE_TYPE_ERROR
1901 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041902 entry->SetURL(params.url);
arthursonzogni73fe3212020-11-17 13:24:071903 entry->SetReferrer(Referrer(*params.referrer));
[email protected]38178a42009-12-17 18:58:321904 if (entry->update_virtual_url_with_url())
1905 UpdateVirtualURLToURL(entry, params.url);
[email protected]5ccd4dc2012-10-24 02:28:141906
jam015ba062017-01-06 21:17:001907 // The site instance will normally be the same except
1908 // 1) session restore, when no site instance will be assigned or
1909 // 2) redirect, when the site instance is reset.
Lei Zhang96031532019-10-10 19:05:471910 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
naskoaf182192016-08-11 02:12:011911 entry->site_instance() == rfh->GetSiteInstance());
clamy432acb22016-04-15 19:41:431912
naskoaf182192016-08-11 02:12:011913 // Update the existing FrameNavigationEntry to ensure all of its members
1914 // reflect the parameters coming from the renderer process.
Lukasz Anforowicz435bcb582019-07-12 20:50:061915 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451916 request->common_params().initiator_origin;
naskoaf182192016-08-11 02:12:011917 entry->AddOrUpdateFrameEntry(
1918 rfh->frame_tree_node(), params.item_sequence_number,
1919 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
arthursonzogni73fe3212020-11-17 13:24:071920 params.url, GetCommittedOriginForFrameEntry(params),
Rakina Zata Amni82fafba2021-03-11 07:07:091921 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071922 params.page_state, params.method, params.post_id,
1923 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031924 request->web_bundle_navigation_info()
1925 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391926 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481927 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191928 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1929 request));
creis22a7b4c2016-04-28 07:20:301930
[email protected]5ccd4dc2012-10-24 02:28:141931 // The redirected to page should not inherit the favicon from the previous
1932 // page.
eugenebut604866f2017-05-10 21:35:361933 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:481934 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:141935
Peter Boströmd7592132019-01-30 04:50:311936 // We should also usually discard the pending entry if it corresponds to a
1937 // different navigation, since that one is now likely canceled. In rare
1938 // cases, we leave the pending entry for another navigation in place when we
1939 // know it is still ongoing, to avoid a flicker in the omnibox (see
1940 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:431941 //
1942 // Note that we need to use the "internal" version since we don't want to
1943 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:311944 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001945 DiscardNonCommittedEntries();
[email protected]40bcc302009-03-02 20:50:391946
Carlos IL4dea8902020-05-26 15:14:291947 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:111948 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:431949}
1950
[email protected]d202a7c2012-01-04 07:53:471951void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:321952 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071953 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361954 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:471955 bool replace_entry,
1956 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451957 NavigationRequest* request) {
avi25f5f9e2015-07-17 20:08:261958 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1959 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
[email protected]09b8f82f2009-06-16 20:22:111960
[email protected]e9ba4472008-09-14 15:42:431961 // Manual subframe navigations just get the current entry cloned so the user
1962 // can go back or forward to it. The actual subframe information will be
1963 // stored in the page state for each of those entries. This happens out of
1964 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:091965 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1966 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:381967
Mikel Astizba9cf2fd2017-12-17 10:38:101968 // The DCHECK below documents the fact that we don't know of any situation
1969 // where |replace_entry| is true for subframe navigations. This simplifies
1970 // reasoning about the replacement struct for subframes (see
1971 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
1972 DCHECK(!replace_entry);
1973
Patrick Monette50e8bd82019-06-13 22:40:451974 // This FrameNavigationEntry might not end up being used in the
1975 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Lukasz Anforowicz435bcb582019-07-12 20:50:061976 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451977 request->common_params().initiator_origin;
Patrick Monette50e8bd82019-06-13 22:40:451978 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481979 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
creisce0ef3572017-01-26 17:53:081980 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Nasko Oskov03912102019-01-11 00:21:321981 params.url, (params.url_is_unreachable) ? nullptr : &params.origin,
Rakina Zata Amni82fafba2021-03-11 07:07:091982 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071983 params.page_state, params.method, params.post_id,
1984 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031985 request->web_bundle_navigation_info()
1986 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391987 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481988 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191989 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1990 request));
Charles Reisf44482022017-10-13 21:15:031991
creisce0ef3572017-01-26 17:53:081992 std::unique_ptr<NavigationEntryImpl> new_entry =
1993 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:451994 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:001995 frame_tree_.root());
creise062d542015-08-25 02:01:551996
Alexander Timine3ec4192020-04-20 16:39:401997 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411998 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401999 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:472000
creisce0ef3572017-01-26 17:53:082001 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:452002 // to replace, which can happen due to a unique name change. See
2003 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
2004 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:382005
Carlos IL42b416592019-10-07 23:10:362006 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false);
[email protected]e9ba4472008-09-14 15:42:432007}
2008
[email protected]d202a7c2012-01-04 07:53:472009bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:322010 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072011 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392012 bool is_same_document,
Mohamed Abdelhalim833de902019-09-16 17:41:452013 NavigationRequest* request) {
avi9f07a0c2015-02-18 22:51:292014 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2015 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2016
[email protected]e9ba4472008-09-14 15:42:432017 // We're guaranteed to have a previously committed entry, and we now need to
2018 // handle navigation inside of a subframe in it without creating a new entry.
2019 DCHECK(GetLastCommittedEntry());
2020
creis913c63ce2016-07-16 19:52:522021 // For newly created subframes, we don't need to send a commit notification.
2022 // This is only necessary for history navigations in subframes.
2023 bool send_commit_notification = false;
2024
Rakina Zata Amnif6950d552020-11-24 03:26:102025 // If |nav_entry_id| is non-zero and matches an existing entry, this
2026 // is a history navigation. Update the last committed index accordingly. If
2027 // we don't recognize the |nav_entry_id|, it might be a recently
2028 // pruned entry. We'll handle it below.
2029 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2030 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472031 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062032 // Make sure that a subframe commit isn't changing the main frame's
2033 // origin. Otherwise the renderer process may be confused, leading to a
2034 // URL spoof. We can't check the path since that may change
2035 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572036 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2037 // about:blank, file, and unique origins are more subtle to get right.
Eugene But9fb98d12018-05-22 18:28:332038 // We'll abstract out the relevant checks from IsURLSameDocumentNavigation
2039 // and share them here. See https://crbug.com/618104.
creis37988b92016-06-10 18:03:572040 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2041 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2042 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2043 dest_top_url.SchemeIsHTTPOrHTTPS() &&
2044 current_top_url.GetOrigin() != dest_top_url.GetOrigin()) {
creisfb6eeb62016-05-10 19:01:512045 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2046 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062047 }
creis3cdc3b02015-05-29 23:00:472048
creis913c63ce2016-07-16 19:52:522049 // We only need to discard the pending entry in this history navigation
2050 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062051 last_committed_entry_index_ = entry_index;
arthursonzogni69a6a1b2019-09-17 09:23:002052 DiscardNonCommittedEntries();
creis913c63ce2016-07-16 19:52:522053
2054 // History navigations should send a commit notification.
2055 send_commit_notification = true;
avi98405c22015-05-21 20:47:062056 }
[email protected]e9ba4472008-09-14 15:42:432057 }
[email protected]f233e4232013-02-23 00:55:142058
creisce0ef3572017-01-26 17:53:082059 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2060 // it may be a "history auto" case where we update an existing one.
2061 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Lukasz Anforowicz435bcb582019-07-12 20:50:062062 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452063 request->common_params().initiator_origin;
creisce0ef3572017-01-26 17:53:082064 last_committed->AddOrUpdateFrameEntry(
2065 rfh->frame_tree_node(), params.item_sequence_number,
2066 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
arthursonzogni73fe3212020-11-17 13:24:072067 params.url, GetCommittedOriginForFrameEntry(params),
Rakina Zata Amni82fafba2021-03-11 07:07:092068 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072069 params.page_state, params.method, params.post_id,
2070 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032071 request->web_bundle_navigation_info()
2072 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392073 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482074 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:192075 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
2076 request));
creis625a0c7d2015-03-24 23:17:122077
creis913c63ce2016-07-16 19:52:522078 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432079}
2080
[email protected]d202a7c2012-01-04 07:53:472081int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232082 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032083 for (size_t i = 0; i < entries_.size(); ++i) {
2084 if (entries_[i].get() == entry)
2085 return i;
2086 }
2087 return -1;
[email protected]765b35502008-08-21 00:51:202088}
2089
Eugene But7cc259d2017-10-09 23:52:492090// There are two general cases where a navigation is "same-document":
avidb7d1d22015-06-08 21:21:502091// 1. A fragment navigation, in which the url is kept the same except for the
2092// reference fragment.
2093// 2. A history API navigation (pushState and replaceState). This case is
Eugene But7cc259d2017-10-09 23:52:492094// always same-document, but the urls are not guaranteed to match excluding
2095// the fragment. The relevant spec allows pushState/replaceState to any URL
2096// on the same origin.
avidb7d1d22015-06-08 21:21:502097// However, due to reloads, even identical urls are *not* guaranteed to be
Eugene But7cc259d2017-10-09 23:52:492098// same-document navigations, we have to trust the renderer almost entirely.
avidb7d1d22015-06-08 21:21:502099// The one thing we do know is that cross-origin navigations will *never* be
Eugene But7cc259d2017-10-09 23:52:492100// same-document. Therefore, trust the renderer if the URLs are on the same
2101// origin, and assume the renderer is malicious if a cross-origin navigation
2102// claims to be same-document.
creisf164daa2016-06-07 00:17:052103//
2104// TODO(creis): Clean up and simplify the about:blank and origin checks below,
2105// which are likely redundant with each other. Be careful about data URLs vs
2106// about:blank, both of which are unique origins and thus not considered equal.
Eugene But7cc259d2017-10-09 23:52:492107bool NavigationControllerImpl::IsURLSameDocumentNavigation(
[email protected]b9d4dfdc2013-08-08 00:25:122108 const GURL& url,
creisf164daa2016-06-07 00:17:052109 const url::Origin& origin,
Eugene But7cc259d2017-10-09 23:52:492110 bool renderer_says_same_document,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572111 RenderFrameHost* rfh) {
creis225a7432016-06-03 22:56:272112 RenderFrameHostImpl* rfhi = static_cast<RenderFrameHostImpl*>(rfh);
avidb7d1d22015-06-08 21:21:502113 GURL last_committed_url;
Charlie Reis73e356242021-04-02 17:10:312114 // For cases that can't compare against the main frame's URL in the last
2115 // committed entry, use the FrameTreeNode's current_url(). Note that it is
2116 // possible to get same-document commits in the initial empty document when
2117 // there is no last committed entry (e.g., about:blank#foo).
2118 // TODO(creis): It would be simpler to always get the URL from the FTN rather
2119 // than ever looking at GetLastCommittedEntry here. We're limiting the change
2120 // in behavior for a merge and will clean it up further afterward.
2121 if (rfh->GetParent() || !GetLastCommittedEntry()) {
creis225a7432016-06-03 22:56:272122 // Use the FrameTreeNode's current_url and not rfh->GetLastCommittedURL(),
2123 // which might be empty in a new RenderFrameHost after a process swap.
2124 // Here, we care about the last committed URL in the FrameTreeNode,
2125 // regardless of which process it is in.
2126 last_committed_url = rfhi->frame_tree_node()->current_url();
avidb7d1d22015-06-08 21:21:502127 } else {
Charlie Reis73e356242021-04-02 17:10:312128 last_committed_url = GetLastCommittedEntry()->GetURL();
avidb7d1d22015-06-08 21:21:502129 }
2130
Gyuyoung Kim1ac4ca782020-09-11 03:32:512131 auto prefs = rfhi->GetOrCreateWebPreferences();
creis225a7432016-06-03 22:56:272132 const url::Origin& committed_origin =
2133 rfhi->frame_tree_node()->current_origin();
avidb7d1d22015-06-08 21:21:502134 bool is_same_origin = last_committed_url.is_empty() ||
2135 // TODO(japhet): We should only permit navigations
2136 // originating from about:blank to be in-page if the
2137 // about:blank is the first document that frame loaded.
2138 // We don't have sufficient information to identify
2139 // that case at the moment, so always allow about:blank
2140 // for now.
csharrisona3bd0b32016-10-19 18:40:482141 last_committed_url == url::kAboutBlankURL ||
avidb7d1d22015-06-08 21:21:502142 last_committed_url.GetOrigin() == url.GetOrigin() ||
creisf164daa2016-06-07 00:17:052143 committed_origin == origin ||
avidb7d1d22015-06-08 21:21:502144 !prefs.web_security_enabled ||
2145 (prefs.allow_universal_access_from_file_urls &&
creisdc004e02015-11-20 21:28:472146 committed_origin.scheme() == url::kFileScheme);
Eugene But7cc259d2017-10-09 23:52:492147 if (!is_same_origin && renderer_says_same_document) {
avidb7d1d22015-06-08 21:21:502148 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2149 bad_message::NC_IN_PAGE_NAVIGATION);
2150 }
Eugene But7cc259d2017-10-09 23:52:492151 return is_same_origin && renderer_says_same_document;
[email protected]e9ba4472008-09-14 15:42:432152}
2153
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572154void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242155 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572156 NavigationControllerImpl* source =
2157 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572158 // Verify that we look new.
Lei Zhang96031532019-10-10 19:05:472159 DCHECK_EQ(0, GetEntryCount());
2160 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:572161
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572162 if (source->GetEntryCount() == 0)
[email protected]ce3fa3c2009-04-20 19:55:572163 return; // Nothing new to do.
2164
Francois Dorayeaace782017-06-21 16:37:242165 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442166 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572167 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572168
Fergal Dalya1d569972021-03-16 03:24:532169 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302170 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532171 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2172 session_storage_namespace_map_[it.first] = source_namespace->Clone();
Aaron Colwellb731a0ae2021-03-19 19:14:472173 OnStoragePartitionIdAdded(it.first);
[email protected]fdac6ade2013-07-20 01:06:302174 }
[email protected]4e6419c2010-01-15 04:50:342175
Lukasz Anforowicz0de0f452020-12-02 19:57:152176 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572177}
2178
Aran Gilman37d11632019-10-08 23:07:152179void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2180 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162181 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012182 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162183
[email protected]d202a7c2012-01-04 07:53:472184 NavigationControllerImpl* source =
2185 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252186
avi2b177592014-12-10 02:08:022187 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012188 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252189
[email protected]474f8512013-05-31 22:31:162190 // We now have one entry, possibly with a new pending entry. Ensure that
2191 // adding the entries from source won't put us over the limit.
2192 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572193 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572194 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252195
Carlos IL4dea8902020-05-26 15:14:292196 // Insert the entries from source. Ignore any pending entry, since it has not
2197 // committed in source.
[email protected]474f8512013-05-31 22:31:162198 int max_source_index = source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:252199 if (max_source_index == -1)
[email protected]a26023822011-12-29 00:23:552200 max_source_index = source->GetEntryCount();
[email protected]e1cd5452010-08-26 18:03:252201 else
2202 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572203
2204 // Ignore the source's current entry if merging with replacement.
2205 // TODO(davidben): This should preserve entries forward of the current
2206 // too. http://crbug.com/317872
2207 if (replace_entry && max_source_index > 0)
2208 max_source_index--;
2209
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572210 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252211
2212 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552213 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142214
Carlos Caballeroede6f8c2021-01-28 11:01:502215 SetHistoryOffsetAndLength(last_committed_entry_index_, GetEntryCount());
[email protected]e1cd5452010-08-26 18:03:252216}
2217
[email protected]79368982013-11-13 01:11:012218bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162219 // If there is no last committed entry, we cannot prune. Even if there is a
2220 // pending entry, it may not commit, leaving this WebContents blank, despite
2221 // possibly giving it new entries via CopyStateFromAndPrune.
2222 if (last_committed_entry_index_ == -1)
2223 return false;
[email protected]9350602e2013-02-26 23:27:442224
[email protected]474f8512013-05-31 22:31:162225 // We cannot prune if there is a pending entry at an existing entry index.
2226 // It may not commit, so we have to keep the last committed entry, and thus
2227 // there is no sensible place to keep the pending entry. It is ok to have
2228 // a new pending entry, which can optionally commit as a new navigation.
2229 if (pending_entry_index_ != -1)
2230 return false;
2231
[email protected]474f8512013-05-31 22:31:162232 return true;
2233}
2234
[email protected]79368982013-11-13 01:11:012235void NavigationControllerImpl::PruneAllButLastCommitted() {
2236 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162237
avi2b177592014-12-10 02:08:022238 DCHECK_EQ(0, last_committed_entry_index_);
2239 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442240
Carlos Caballeroede6f8c2021-01-28 11:01:502241 SetHistoryOffsetAndLength(last_committed_entry_index_, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442242}
2243
[email protected]79368982013-11-13 01:11:012244void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162245 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012246 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262247
[email protected]474f8512013-05-31 22:31:162248 // Erase all entries but the last committed entry. There may still be a
2249 // new pending entry after this.
2250 entries_.erase(entries_.begin(),
2251 entries_.begin() + last_committed_entry_index_);
2252 entries_.erase(entries_.begin() + 1, entries_.end());
2253 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262254}
2255
Christian Dullweber1af31e62018-02-22 11:49:482256void NavigationControllerImpl::DeleteNavigationEntries(
2257 const DeletionPredicate& deletionPredicate) {
2258 // It is up to callers to check the invariants before calling this.
2259 CHECK(CanPruneAllButLastCommitted());
2260 std::vector<int> delete_indices;
2261 for (size_t i = 0; i < entries_.size(); i++) {
2262 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572263 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482264 delete_indices.push_back(i);
2265 }
2266 }
2267 if (delete_indices.empty())
2268 return;
2269
2270 if (delete_indices.size() == GetEntryCount() - 1U) {
2271 PruneAllButLastCommitted();
2272 } else {
2273 // Do the deletion in reverse to preserve indices.
2274 for (auto it = delete_indices.rbegin(); it != delete_indices.rend(); ++it) {
2275 RemoveEntryAtIndex(*it);
2276 }
Carlos Caballeroede6f8c2021-01-28 11:01:502277 SetHistoryOffsetAndLength(last_committed_entry_index_, GetEntryCount());
Christian Dullweber1af31e62018-02-22 11:49:482278 }
2279 delegate()->NotifyNavigationEntriesDeleted();
2280}
2281
Shivani Sharma883f5f32019-02-12 18:20:012282bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2283 auto* entry = GetEntryAtIndex(index);
2284 return entry && entry->should_skip_on_back_forward_ui();
2285}
2286
Carlos Caballero35ce710c2019-09-19 10:59:452287BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2288 return back_forward_cache_;
2289}
2290
clamy987a3752018-05-03 17:36:262291void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2292 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2293 // progress, since this will cause a use-after-free. (We only allow this
2294 // when the tab is being destroyed for shutdown, since it won't return to
2295 // NavigateToEntry in that case.) http://crbug.com/347742.
2296 CHECK(!in_navigate_to_pending_entry_ || delegate_->IsBeingDestroyed());
2297
2298 if (was_failure && pending_entry_) {
2299 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2300 } else {
2301 failed_pending_entry_id_ = 0;
2302 }
2303
2304 if (pending_entry_) {
2305 if (pending_entry_index_ == -1)
2306 delete pending_entry_;
2307 pending_entry_index_ = -1;
2308 pending_entry_ = nullptr;
2309 }
arthursonzogni66f711c2019-10-08 14:40:362310
2311 // Ensure any refs to the current pending entry are ignored if they get
2312 // deleted, by clearing the set of known refs. All future pending entries will
2313 // only be affected by new refs.
2314 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262315}
2316
2317void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2318 if (pending_entry_)
2319 pending_entry_->set_ssl_error(error);
2320}
2321
Camille Lamy5193caa2018-10-12 11:59:422322#if defined(OS_ANDROID)
2323// static
2324bool NavigationControllerImpl::ValidateDataURLAsString(
2325 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2326 if (!data_url_as_string)
2327 return false;
2328
2329 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2330 return false;
2331
2332 // The number of characters that is enough for validating a data: URI.
2333 // From the GURL's POV, the only important part here is scheme, it doesn't
2334 // check the actual content. Thus we can take only the prefix of the url, to
2335 // avoid unneeded copying of a potentially long string.
2336 const size_t kDataUriPrefixMaxLen = 64;
2337 GURL data_url(
2338 std::string(data_url_as_string->front_as<char>(),
2339 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2340 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2341 return false;
2342
2343 return true;
2344}
2345#endif
2346
Shivani Sharma194877032019-03-07 17:52:472347void NavigationControllerImpl::NotifyUserActivation() {
2348 // When a user activation occurs, ensure that all adjacent entries for the
2349 // same document clear their skippable bit, so that the history manipulation
2350 // intervention does not apply to them.
Shivani Sharmaffb32b82019-04-09 16:58:472351 // TODO(crbug.com/949279) in case it becomes necessary for resetting based on
2352 // which frame created an entry and which frame has the user gesture.
Shivani Sharma194877032019-03-07 17:52:472353 auto* last_committed_entry = GetLastCommittedEntry();
2354 if (!last_committed_entry)
2355 return;
Shivani Sharma194877032019-03-07 17:52:472356
Shivani Sharmac4cc8922019-04-18 03:11:172357 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472358}
2359
clamy987a3752018-05-03 17:36:262360bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2361 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172362 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262363 NavigationEntryImpl* entry =
2364 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2365 if (!entry)
2366 return false;
2367
2368 FrameNavigationEntry* frame_entry =
2369 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2370 if (!frame_entry)
2371 return false;
2372
Camille Lamy5193caa2018-10-12 11:59:422373 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572374 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232375 ReloadType::NONE, false /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422376 true /* is_history_navigation_in_new_child */);
clamyea99ea12018-05-28 13:54:232377
2378 if (!request)
2379 return false;
2380
arthursonzognif046d4a2019-12-12 19:08:102381 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412382
Lukasz Anforowicz9ee83c272020-12-01 20:14:052383 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2384 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232385
2386 return true;
clamy987a3752018-05-03 17:36:262387}
2388
Tsuyoshi Horo52fd08e2020-07-07 07:03:452389bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2390 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2391 if (!entry)
2392 return false;
2393 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2394 if (!frame_entry)
2395 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142396 ReloadType reload_type = ReloadType::NORMAL;
2397 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452398 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142399 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452400 false /* is_same_document_history_load */,
2401 false /* is_history_navigation_in_new_child */);
2402 if (!request)
2403 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052404 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452405 return true;
2406}
2407
Dave Tapuska8bfd84c2019-03-26 20:47:162408void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2409 int offset,
2410 int sandbox_frame_tree_node_id) {
2411 if (!CanGoToOffset(offset))
2412 return;
2413 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id);
2414}
2415
clamy987a3752018-05-03 17:36:262416void NavigationControllerImpl::NavigateFromFrameProxy(
2417 RenderFrameHostImpl* render_frame_host,
2418 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022419 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452420 int initiator_process_id,
Lukasz Anforowicz63f3b9432019-05-30 05:42:582421 const base::Optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262422 bool is_renderer_initiated,
2423 SiteInstance* source_site_instance,
2424 const Referrer& referrer,
2425 ui::PageTransition page_transition,
2426 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252427 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262428 const std::string& method,
2429 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092430 const std::string& extra_headers,
John Delaney50425f82020-04-07 16:26:212431 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Gyuyoung Kim38e63632021-01-14 13:11:392432 const base::Optional<blink::Impression>& impression) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582433 if (is_renderer_initiated)
2434 DCHECK(initiator_origin.has_value());
2435
clamy987a3752018-05-03 17:36:262436 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582437
Rakina Zata Amni66992a82020-06-24 03:57:522438 // Don't allow an entry replacement if there is no entry to replace.
2439 // http://crbug.com/457149
2440 if (GetEntryCount() == 0)
2441 should_replace_current_entry = false;
2442
clamy987a3752018-05-03 17:36:262443 // Create a NavigationEntry for the transfer, without making it the pending
2444 // entry. Subframe transfers should have a clone of the last committed entry
2445 // with a FrameNavigationEntry for the target frame. Main frame transfers
2446 // should have a new NavigationEntry.
2447 // TODO(creis): Make this unnecessary by creating (and validating) the params
2448 // directly, passing them to the destination RenderFrameHost. See
2449 // https://crbug.com/536906.
2450 std::unique_ptr<NavigationEntryImpl> entry;
2451 if (!node->IsMainFrame()) {
2452 // Subframe case: create FrameNavigationEntry.
2453 if (GetLastCommittedEntry()) {
2454 entry = GetLastCommittedEntry()->Clone();
2455 entry->set_extra_headers(extra_headers);
2456 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2457 // Renderer-initiated navigation that target a remote frame are currently
2458 // classified as browser-initiated when this one has already navigated.
2459 // See https://crbug.com/722251.
2460 } else {
2461 // If there's no last committed entry, create an entry for about:blank
2462 // with a subframe entry for our destination.
2463 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2464 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:062465 GURL(url::kAboutBlankURL), referrer, initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:102466 source_site_instance, page_transition, is_renderer_initiated,
2467 extra_headers, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:372468 nullptr /* blob_url_loader_factory */, should_replace_current_entry,
2469 GetWebContents()));
2470 // CreateNavigationEntry() may have changed the transition type.
2471 page_transition = entry->GetTransitionType();
clamy987a3752018-05-03 17:36:262472 }
clamy987a3752018-05-03 17:36:262473 entry->AddOrUpdateFrameEntry(
2474 node, -1, -1, nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582475 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Lukasz Anforowicz435bcb582019-07-12 20:50:062476 base::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202477 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392478 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482479 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori79d549d2021-02-18 12:59:542480 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262481 } else {
2482 // Main frame case.
2483 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102484 url, referrer, initiator_origin, source_site_instance, page_transition,
2485 is_renderer_initiated, extra_headers, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:372486 blob_url_loader_factory, should_replace_current_entry,
2487 GetWebContents()));
2488 // CreateNavigationEntry() may have changed the transition type.
2489 page_transition = entry->GetTransitionType();
clamy987a3752018-05-03 17:36:262490 entry->root_node()->frame_entry->set_source_site_instance(
2491 static_cast<SiteInstanceImpl*>(source_site_instance));
2492 entry->root_node()->frame_entry->set_method(method);
2493 }
clamy987a3752018-05-03 17:36:262494
Camille Lamy5193caa2018-10-12 11:59:422495 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262496 if (GetLastCommittedEntry() &&
2497 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2498 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422499 override_user_agent = true;
clamy987a3752018-05-03 17:36:262500 }
2501 // TODO(creis): Set user gesture and intent received timestamp on Android.
2502
2503 // We may not have successfully added the FrameNavigationEntry to |entry|
2504 // above (per https://crbug.com/608402), in which case we create it from
2505 // scratch. This works because we do not depend on |frame_entry| being inside
2506 // |entry| during NavigateToEntry. This will go away when we shortcut this
2507 // further in https://crbug.com/536906.
2508 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2509 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452510 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
clamy987a3752018-05-03 17:36:262511 node->unique_name(), -1, -1, nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582512 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Lukasz Anforowicz435bcb582019-07-12 20:50:062513 nullptr /* origin */, referrer, initiator_origin, std::vector<GURL>(),
Miyoung Shin5d77f72072020-10-09 15:14:202514 blink::PageState(), method, -1, blob_url_loader_factory,
Antonio Sartori78a749f2020-11-30 12:03:392515 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482516 nullptr /* subresource_web_bundle_navigation_info */,
Titouan Rigoudy6ec70402021-02-02 15:42:192517 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262518 }
2519
Camille Lamy5193caa2018-10-12 11:59:422520 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022521 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452522 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292523 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422524 params.source_site_instance = source_site_instance;
2525 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2526 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032527 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422528 params.referrer = referrer;
2529 /* params.redirect_chain: skip */
2530 params.extra_headers = extra_headers;
2531 params.is_renderer_initiated = is_renderer_initiated;
2532 params.override_user_agent = UA_OVERRIDE_INHERIT;
2533 /* params.base_url_for_data_url: skip */
2534 /* params.virtual_url_for_data_url: skip */
2535 /* params.data_url_as_string: skip */
2536 params.post_data = post_body;
2537 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582538 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422539 /* params.frame_name: skip */
2540 // TODO(clamy): See if user gesture should be propagated to this function.
2541 params.has_user_gesture = false;
2542 params.should_clear_history_list = false;
2543 params.started_from_context_menu = false;
2544 /* params.navigation_ui_data: skip */
2545 /* params.input_start: skip */
Lucas Furukawa Gadania9c45682019-07-31 22:05:142546 params.was_activated = mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542547 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212548 params.impression = impression;
Camille Lamy5193caa2018-10-12 11:59:422549
2550 std::unique_ptr<NavigationRequest> request =
2551 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032552 node, params, override_user_agent, should_replace_current_entry,
2553 false /* has_user_gesture */, download_policy, ReloadType::NONE,
Scott Violet5ae6c42e2020-10-28 02:47:372554 entry.get(), frame_entry.get(), params.transition_type);
clamyea99ea12018-05-28 13:54:232555
2556 if (!request)
2557 return;
2558
Arthur Hemery948742762019-09-18 10:06:242559 // At this stage we are proceeding with this navigation. If this was renderer
2560 // initiated with user gesture, we need to make sure we clear up potential
2561 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2562 DiscardNonCommittedEntries();
2563
Lukasz Anforowicz9ee83c272020-12-01 20:14:052564 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262565}
2566
[email protected]d1198fd2012-08-13 22:50:192567void NavigationControllerImpl::SetSessionStorageNamespace(
Aaron Colwellf3b316e2021-03-11 20:17:052568 const StoragePartitionId& partition_id,
[email protected]8ff00d72012-10-23 19:12:212569 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192570 if (!session_storage_namespace)
2571 return;
2572
2573 // We can't overwrite an existing SessionStorage without violating spec.
2574 // Attempts to do so may give a tab access to another tab's session storage
2575 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152576 bool successful_insert =
2577 session_storage_namespace_map_
Aaron Colwellf3b316e2021-03-11 20:17:052578 .insert(std::make_pair(partition_id,
2579 static_cast<SessionStorageNamespaceImpl*>(
2580 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302581 .second;
2582 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
Aaron Colwellb731a0ae2021-03-19 19:14:472583 OnStoragePartitionIdAdded(partition_id);
[email protected]d1198fd2012-08-13 22:50:192584}
2585
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572586bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Aran Gilman37d11632019-10-08 23:07:152587 return IsInitialNavigation() && !GetLastCommittedEntry() &&
Carlos Caballeroede6f8c2021-01-28 11:01:502588 !frame_tree_.has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462589}
2590
Aran Gilman37d11632019-10-08 23:07:152591SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Aaron Colwell78b4bde2021-03-16 16:16:092592 const SiteInfo& site_info) {
2593 // TODO(acolwell): Remove partition_id logic once we have successfully
2594 // migrated the implementation to be a StoragePartitionConfig. At that point
2595 // |site_info| can be replaced with a StoragePartitionConfig.
2596 const StoragePartitionId partition_id =
2597 site_info.GetStoragePartitionId(browser_context_);
2598 const StoragePartitionConfig partition_config =
2599 site_info.GetStoragePartitionConfig(browser_context_);
[email protected]d1198fd2012-08-13 22:50:192600
[email protected]fdac6ade2013-07-20 01:06:302601 StoragePartition* partition =
Aaron Colwell78b4bde2021-03-16 16:16:092602 BrowserContext::GetStoragePartition(browser_context_, partition_config);
michaelnbacbcbd2016-02-09 00:32:032603 DOMStorageContextWrapper* context_wrapper =
2604 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2605
2606 SessionStorageNamespaceMap::const_iterator it =
2607 session_storage_namespace_map_.find(partition_id);
2608 if (it != session_storage_namespace_map_.end()) {
2609 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152610 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2611 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:472612
2613 // Verify that the config we generated now matches the one that
2614 // was generated when the namespace was added to the map.
2615 DCHECK_EQ(partition_config, it->first.config());
2616 if (partition_config != it->first.config()) {
2617 LogStoragePartitionIdCrashKeys(it->first, partition_id);
2618 }
2619
michaelnbacbcbd2016-02-09 00:32:032620 return it->second.get();
2621 }
2622
2623 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102624 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2625 SessionStorageNamespaceImpl::Create(context_wrapper);
2626 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2627 session_storage_namespace.get();
2628 session_storage_namespace_map_[partition_id] =
2629 std::move(session_storage_namespace);
Aaron Colwellb731a0ae2021-03-19 19:14:472630 OnStoragePartitionIdAdded(partition_id);
[email protected]fdac6ade2013-07-20 01:06:302631
Daniel Murphy31bbb8b12018-02-07 21:44:102632 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302633}
2634
2635SessionStorageNamespace*
2636NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Aaron Colwell78b4bde2021-03-16 16:16:092637 return GetSessionStorageNamespace(SiteInfo());
[email protected]fdac6ade2013-07-20 01:06:302638}
2639
2640const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572641NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302642 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552643}
[email protected]d202a7c2012-01-04 07:53:472644
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572645bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562646 return needs_reload_;
2647}
[email protected]a26023822011-12-29 00:23:552648
[email protected]46bb5e9c2013-10-03 22:16:472649void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412650 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2651}
2652
2653void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472654 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412655 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542656
2657 if (last_committed_entry_index_ != -1) {
2658 entries_[last_committed_entry_index_]->SetTransitionType(
2659 ui::PAGE_TRANSITION_RELOAD);
2660 }
[email protected]46bb5e9c2013-10-03 22:16:472661}
2662
[email protected]d202a7c2012-01-04 07:53:472663void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572664 DCHECK_LT(index, GetEntryCount());
2665 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312666 DiscardNonCommittedEntries();
2667
2668 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122669 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312670 last_committed_entry_index_--;
2671}
2672
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572673NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272674 // If there is no pending_entry_, there should be no pending_entry_index_.
2675 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2676
2677 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362678 // at that index. An exception is while a reload of a post commit error page
2679 // is ongoing; in that case pending entry will point to the entry replaced
2680 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272681 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362682 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2683 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272684
[email protected]022af742011-12-28 18:37:252685 return pending_entry_;
2686}
2687
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572688int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272689 // The pending entry index must always be less than the number of entries.
2690 // If there are no entries, it must be exactly -1.
2691 DCHECK_LT(pending_entry_index_, GetEntryCount());
2692 DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:552693 return pending_entry_index_;
2694}
2695
avi25764702015-06-23 15:43:372696void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572697 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362698 bool replace,
2699 bool was_post_commit_error) {
mastiz7eddb5f72016-06-23 09:52:452700 DCHECK(!ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2701 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202702
avi5cad4912015-06-19 05:25:442703 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2704 // need to keep continuity with the pending entry, so copy the pending entry's
2705 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2706 // then the renderer navigated on its own, independent of the pending entry,
2707 // so don't copy anything.
2708 if (pending_entry_ && pending_entry_index_ == -1)
2709 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202710
arthursonzogni69a6a1b2019-09-17 09:23:002711 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:202712
creisee17e932015-07-17 17:56:222713 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362714 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102715 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572716 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362717 // If the new entry is a post-commit error page, we store the current last
2718 // committed entry to the side so that we can put it back when navigating
2719 // away from the error.
2720 if (was_post_commit_error) {
2721 DCHECK(!entry_replaced_by_post_commit_error_);
2722 entry_replaced_by_post_commit_error_ =
2723 std::move(entries_[last_committed_entry_index_]);
2724 }
dcheng36b6aec92015-12-26 06:16:362725 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222726 return;
2727 }
[email protected]765b35502008-08-21 00:51:202728
creis37979a62015-08-04 19:48:182729 // We shouldn't see replace == true when there's no committed entries.
2730 DCHECK(!replace);
2731
Michael Thiessen9b14d512019-09-23 21:19:472732 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202733
Shivani Sharmad8c8d652019-02-13 17:27:572734 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202735
dcheng36b6aec92015-12-26 06:16:362736 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202737 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292738}
2739
Shivani Sharmad8c8d652019-02-13 17:27:572740void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162741 if (entries_.size() < max_entry_count())
2742 return;
2743
2744 DCHECK_EQ(max_entry_count(), entries_.size());
2745 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572746 CHECK_EQ(pending_entry_index_, -1);
2747
2748 int index = 0;
2749 if (base::FeatureList::IsEnabled(
2750 features::kHistoryManipulationIntervention)) {
2751 // Retrieve the oldest skippable entry.
2752 for (; index < GetEntryCount(); index++) {
2753 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2754 break;
2755 }
2756 }
2757
2758 // If there is no skippable entry or if it is the last committed entry then
2759 // fall back to pruning the oldest entry. It is not safe to prune the last
2760 // committed entry.
2761 if (index == GetEntryCount() || index == last_committed_entry_index_)
2762 index = 0;
2763
2764 bool should_succeed = RemoveEntryAtIndex(index);
2765 DCHECK_EQ(true, should_succeed);
2766
2767 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252768}
2769
clamy3cb9bea92018-07-10 12:42:022770void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162771 ReloadType reload_type,
2772 int sandboxed_source_frame_tree_node_id) {
Alexander Timin3a92df72019-09-20 11:59:502773 TRACE_EVENT0("navigation",
2774 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272775 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022776 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362777 if (pending_entry_index_ != -1) {
2778 // The pending entry may not be in entries_ if a post-commit error page is
2779 // showing.
2780 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2781 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2782 }
Gyuyoung Kim107c2a02021-04-13 01:49:302783 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572784 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:012785 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:002786 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:572787 int nav_entry_id = pending_entry_->GetUniqueID();
2788
[email protected]83c2e232011-10-07 21:36:462789 // If we were navigating to a slow-to-commit page, and the user performs
2790 // a session history navigation to the last committed page, RenderViewHost
2791 // will force the throbber to start, but WebKit will essentially ignore the
2792 // navigation, and won't send a message to stop the throbber. To prevent this
2793 // from happening, we drop the navigation here and stop the slow-to-commit
2794 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022795 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:082796 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:272797 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
[email protected]ec6c05f2013-10-23 18:41:572798 delegate_->Stop();
[email protected]6a13a6c2011-12-20 21:47:122799
[email protected]83c2e232011-10-07 21:36:462800 DiscardNonCommittedEntries();
2801 return;
2802 }
2803
creisce0ef3572017-01-26 17:53:082804 // Compare FrameNavigationEntries to see which frames in the tree need to be
2805 // navigated.
clamy3cb9bea92018-07-10 12:42:022806 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2807 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Alex Moshchuk3a4e77a2020-05-29 21:32:572808 FindFramesToNavigate(root, reload_type, &same_document_loads,
2809 &different_document_loads);
creis4e2ecb72015-06-20 00:46:302810
2811 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572812 // We were unable to match any frames to navigate. This can happen if a
2813 // history navigation targets a subframe that no longer exists
2814 // (https://crbug.com/705550). In this case, we need to update the current
2815 // history entry to the pending one but keep the main document loaded. We
2816 // also need to ensure that observers are informed about the updated
2817 // current history entry (e.g., for greying out back/forward buttons), and
2818 // that renderer processes update their history offsets. The easiest way
2819 // to do all that is to schedule a "redundant" same-document navigation in
2820 // the main frame.
2821 //
2822 // Note that we don't want to remove this history entry, as it might still
2823 // be valid later, since a frame that it's targeting may be recreated.
2824 //
2825 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
2826 // need to repeat history navigations until finding the one that works.
2827 // Consider changing this behavior to keep looking for the first valid
2828 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:022829 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422830 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572831 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:572832 ReloadType::NONE /* reload_type */,
2833 true /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422834 false /* is_history_navigation_in_new_child */);
clamy3cb9bea92018-07-10 12:42:022835 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572836 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:022837 DiscardPendingEntry(false);
2838 return;
2839 }
Alex Moshchuk3a4e77a2020-05-29 21:32:572840 same_document_loads.push_back(std::move(navigation_request));
2841
2842 // Sanity check that we never take this branch for any kinds of reloads,
2843 // as those should've queued a different-document load in the main frame.
2844 DCHECK(!is_forced_reload);
2845 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:302846 }
2847
Dave Tapuska8bfd84c2019-03-26 20:47:162848 // If |sandboxed_source_frame_node_id| is valid, then track whether this
2849 // navigation affects any frame outside the frame's subtree.
2850 if (sandboxed_source_frame_tree_node_id !=
2851 FrameTreeNode::kFrameTreeNodeInvalidId) {
2852 bool navigates_inside_tree =
2853 DoesSandboxNavigationStayWithinSubtree(
2854 sandboxed_source_frame_tree_node_id, same_document_loads) &&
2855 DoesSandboxNavigationStayWithinSubtree(
2856 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:502857 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:162858 // the number of pages that trigger this.
2859 FrameTreeNode* sandbox_source_frame_tree_node =
2860 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
2861 if (sandbox_source_frame_tree_node) {
2862 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
2863 sandbox_source_frame_tree_node->current_frame_host(),
2864 navigates_inside_tree
2865 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
2866 : blink::mojom::WebFeature::
2867 kSandboxBackForwardAffectsFramesOutsideSubtree);
2868 }
Dave Tapuska855c1e12019-08-23 20:45:522869
2870 // If the navigation occurred outside the tree discard it because
2871 // the sandboxed frame didn't have permission to navigate outside
2872 // its tree. If it is possible that the navigation is both inside and
2873 // outside the frame tree and we discard it entirely because we don't
2874 // want to end up in a history state that didn't exist before.
2875 if (base::FeatureList::IsEnabled(
2876 features::kHistoryPreventSandboxedNavigation) &&
2877 !navigates_inside_tree) {
2878 DiscardPendingEntry(false);
2879 return;
2880 }
Dave Tapuska8bfd84c2019-03-26 20:47:162881 }
2882
Carlos Caballero539a421c2020-07-06 10:25:572883 // BackForwardCache:
2884 // Navigate immediately if the document is in the BackForwardCache.
2885 if (back_forward_cache_.GetEntry(nav_entry_id)) {
2886 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
2887 DCHECK_EQ(reload_type, ReloadType::NONE);
2888 auto navigation_request = CreateNavigationRequestFromEntry(
2889 root, pending_entry_, pending_entry_->GetFrameEntry(root),
2890 ReloadType::NONE, false /* is_same_document_history_load */,
2891 false /* is_history_navigation_in_new_child */);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052892 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:572893
2894 return;
2895 }
2896
2897 // History navigation might try to reuse a specific BrowsingInstance, already
2898 // used by a page in the cache. To avoid having two different main frames that
2899 // live in the same BrowsingInstance, evict the all pages with this
2900 // BrowsingInstance from the cache.
2901 //
2902 // For example, take the following scenario:
2903 //
2904 // A1 = Some page on a.com
2905 // A2 = Some other page on a.com
2906 // B3 = An uncacheable page on b.com
2907 //
2908 // Then the following navigations occur:
2909 // A1->A2->B3->A1
2910 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
2911 // take its place) and A1 will be created in the same BrowsingInstance (and
2912 // SiteInstance), as A2.
2913 //
2914 // If we didn't do anything, both A1 and A2 would remain alive in the same
2915 // BrowsingInstance/SiteInstance, which is unsupported by
2916 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
2917 // A2 from the cache.
2918 if (pending_entry_->site_instance()) {
2919 back_forward_cache_.EvictFramesInRelatedSiteInstances(
2920 pending_entry_->site_instance());
2921 }
2922
clamy3cb9bea92018-07-10 12:42:022923 // This call does not support re-entrancy. See http://crbug.com/347742.
2924 CHECK(!in_navigate_to_pending_entry_);
2925 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:302926
arthursonzogni66f711c2019-10-08 14:40:362927 // It is not possible to delete the pending NavigationEntry while navigating
2928 // to it. Grab a reference to delay potential deletion until the end of this
2929 // function.
2930 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
2931
creis4e2ecb72015-06-20 00:46:302932 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:022933 for (auto& item : same_document_loads) {
2934 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052935 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302936 }
clamy3cb9bea92018-07-10 12:42:022937 for (auto& item : different_document_loads) {
2938 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052939 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302940 }
clamy3cb9bea92018-07-10 12:42:022941
2942 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:302943}
2944
Alex Moshchuk3a4e77a2020-05-29 21:32:572945NavigationControllerImpl::HistoryNavigationAction
2946NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:302947 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:572948 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:422949 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:582950 // Only active and prerendered documents are allowed to navigate in their
2951 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422952 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:012953 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:582954 // - If the document is in pending deletion, the browser already committed
2955 // to destroying this RenderFrameHost. See https://crbug.com/930278.
2956 // - If the document is in back-forward cache, it's not allowed to navigate
2957 // as it should remain frozen. Ignore the request and evict the document
2958 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422959 //
Sreeja Kamishettydb8e2892021-03-10 09:30:582960 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:422961 // which should all be inactive as well.
Sreeja Kamishettydb8e2892021-03-10 09:30:582962 if (render_frame_host->IsInactiveAndDisallowActivation())
Sreeja Kamishetty8eacabb2021-03-09 11:45:422963 return HistoryNavigationAction::kStopLooking;
2964 }
arthursonzogni03f76152019-02-12 10:35:202965
Alex Moshchuk3a4e77a2020-05-29 21:32:572966 // If there's no last committed entry, there is no previous history entry to
2967 // compare against, so fall back to a different-document load. Note that we
2968 // should only reach this case for the root frame and not descend further
2969 // into subframes.
2970 if (!GetLastCommittedEntry()) {
2971 DCHECK(frame->IsMainFrame());
2972 return HistoryNavigationAction::kDifferentDocument;
2973 }
2974
2975 // Reloads should result in a different-document load. Note that reloads may
2976 // also happen via the |needs_reload_| mechanism where the reload_type is
2977 // NONE, so detect this by comparing whether we're going to the same
2978 // entry that we're currently on. Similarly to above, only main frames
2979 // should reach this. Note that subframes support reloads, but that's done
2980 // via a different path that doesn't involve FindFramesToNavigate (see
2981 // RenderFrameHost::Reload()).
2982 if (reload_type != ReloadType::NONE ||
2983 pending_entry_index_ == last_committed_entry_index_) {
2984 DCHECK(frame->IsMainFrame());
2985 return HistoryNavigationAction::kDifferentDocument;
2986 }
2987
Alex Moshchuk47d1a4bd2020-06-01 22:15:342988 // If there is no new FrameNavigationEntry for the frame, ignore the
2989 // load. For example, this may happen when going back to an entry before a
2990 // frame was created. Suppose we commit a same-document navigation that also
2991 // results in adding a new subframe somewhere in the tree. If we go back,
2992 // the new subframe will be missing a FrameNavigationEntry in the previous
2993 // NavigationEntry, but we shouldn't delete or change what's loaded in
2994 // it.
2995 //
Alex Moshchuke65c39272020-06-03 17:55:372996 // Note that in this case, there is no need to keep looking for navigations
2997 // in subframes, which would be missing FrameNavigationEntries as well.
2998 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:342999 // It's important to check this before checking |old_item| below, since both
3000 // might be null, and in that case we still shouldn't change what's loaded in
3001 // this frame. Note that scheduling any loads assumes that |new_item| is
3002 // non-null. See https://crbug.com/1088354.
3003 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3004 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:373005 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:343006
Alex Moshchuk3a4e77a2020-05-29 21:32:573007 // If there is no old FrameNavigationEntry, schedule a different-document
3008 // load.
3009 //
creis225a7432016-06-03 22:56:273010 // TODO(creis): Store the last committed FrameNavigationEntry to use here,
3011 // rather than assuming the NavigationEntry has up to date info on subframes.
Alex Moshchuk3a4e77a2020-05-29 21:32:573012 // Note that this may require sharing FrameNavigationEntries between
3013 // NavigationEntries (https://crbug.com/373041) as a prerequisite, since
3014 // otherwise the stored FrameNavigationEntry might get stale (e.g., after
3015 // subframe navigations, or in the case where we don't find any frames to
3016 // navigate and ignore a back/forward navigation while moving to a different
3017 // NavigationEntry, as in https://crbug.com/705550).
creis4e2ecb72015-06-20 00:46:303018 FrameNavigationEntry* old_item =
3019 GetLastCommittedEntry()->GetFrameEntry(frame);
Alex Moshchuk3a4e77a2020-05-29 21:32:573020 if (!old_item)
3021 return HistoryNavigationAction::kDifferentDocument;
3022
Alex Moshchuk3a4e77a2020-05-29 21:32:573023 // If the new item is not in the same SiteInstance, schedule a
3024 // different-document load. Newly restored items may not have a SiteInstance
3025 // yet, in which case it will be assigned on first commit.
3026 if (new_item->site_instance() &&
3027 new_item->site_instance() != old_item->site_instance())
3028 return HistoryNavigationAction::kDifferentDocument;
3029
3030 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:313031 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
3032 // that we do this even if the history navigation would not be modifying this
3033 // frame were it live.
3034 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:573035 return HistoryNavigationAction::kDifferentDocument;
3036
3037 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:313038 // Starting a navigation after a crash early-promotes the speculative
3039 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
3040 // committed yet, so we can not possibly perform a same-document history
3041 // navigation. The frame would need to be reloaded with a cross-document
3042 // navigation.
3043 if (!frame->current_frame_host()->has_committed_any_navigation())
3044 return HistoryNavigationAction::kDifferentDocument;
3045
creis54131692016-08-12 18:32:253046 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:493047 // sequence number but different item sequence number.
3048 if (new_item->document_sequence_number() ==
3049 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573050 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493051 }
avib48cb312016-05-05 21:35:003052
Alex Moshchuk3a4e77a2020-05-29 21:32:573053 // Otherwise, if both item and document sequence numbers differ, this
3054 // should be a different document load.
3055 return HistoryNavigationAction::kDifferentDocument;
3056 }
3057
3058 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373059 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573060 DCHECK_EQ(new_item->document_sequence_number(),
3061 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373062 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573063}
3064
3065void NavigationControllerImpl::FindFramesToNavigate(
3066 FrameTreeNode* frame,
3067 ReloadType reload_type,
3068 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3069 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3070 DCHECK(pending_entry_);
3071 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3072
3073 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3074
3075 if (action == HistoryNavigationAction::kSameDocument) {
3076 std::unique_ptr<NavigationRequest> navigation_request =
3077 CreateNavigationRequestFromEntry(
3078 frame, pending_entry_, new_item, reload_type,
3079 true /* is_same_document_history_load */,
3080 false /* is_history_navigation_in_new_child */);
3081 if (navigation_request) {
3082 // Only add the request if was properly created. It's possible for the
3083 // creation to fail in certain cases, e.g. when the URL is invalid.
3084 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303085 }
Lei Zhang96031532019-10-10 19:05:473086
Alex Moshchuk3a4e77a2020-05-29 21:32:573087 // TODO(avi, creis): This is a bug; we should not return here. Rather, we
3088 // should continue on and navigate all child frames which have also
3089 // changed. This bug is the cause of <https://crbug.com/542299>, which is
3090 // a NC_IN_PAGE_NAVIGATION renderer kill.
3091 //
3092 // However, this bug is a bandaid over a deeper and worse problem. Doing a
3093 // pushState immediately after loading a subframe is a race, one that no
3094 // web page author expects. If we fix this bug, many large websites break.
3095 // For example, see <https://crbug.com/598043> and the spec discussion at
3096 // <https://github.com/whatwg/html/issues/1191>.
3097 //
3098 // For now, we accept this bug, and hope to resolve the race in a
3099 // different way that will one day allow us to fix this.
3100 return;
3101 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473102 std::unique_ptr<NavigationRequest> navigation_request =
3103 CreateNavigationRequestFromEntry(
3104 frame, pending_entry_, new_item, reload_type,
3105 false /* is_same_document_history_load */,
3106 false /* is_history_navigation_in_new_child */);
3107 if (navigation_request) {
3108 // Only add the request if was properly created. It's possible for the
3109 // creation to fail in certain cases, e.g. when the URL is invalid.
3110 different_document_loads->push_back(std::move(navigation_request));
3111 }
3112 // For a different document, the subframes will be destroyed, so there's
3113 // no need to consider them.
3114 return;
Alex Moshchuke65c39272020-06-03 17:55:373115 } else if (action == HistoryNavigationAction::kStopLooking) {
3116 return;
creis4e2ecb72015-06-20 00:46:303117 }
3118
3119 for (size_t i = 0; i < frame->child_count(); i++) {
clamy3cb9bea92018-07-10 12:42:023120 FindFramesToNavigate(frame->child_at(i), reload_type, same_document_loads,
creis4e2ecb72015-06-20 00:46:303121 different_document_loads);
3122 }
3123}
3124
clamy21718cc22018-06-13 13:34:243125void NavigationControllerImpl::NavigateWithoutEntry(
3126 const LoadURLParams& params) {
3127 // Find the appropriate FrameTreeNode.
3128 FrameTreeNode* node = nullptr;
3129 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3130 !params.frame_name.empty()) {
3131 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003132 ? frame_tree_.FindByID(params.frame_tree_node_id)
3133 : frame_tree_.FindByName(params.frame_name);
clamy21718cc22018-06-13 13:34:243134 }
3135
3136 // If no FrameTreeNode was specified, navigate the main frame.
3137 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003138 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243139
Camille Lamy5193caa2018-10-12 11:59:423140 // Compute overrides to the LoadURLParams for |override_user_agent|,
3141 // |should_replace_current_entry| and |has_user_gesture| that will be used
3142 // both in the creation of the NavigationEntry and the NavigationRequest.
3143 // Ideally, the LoadURLParams themselves would be updated, but since they are
3144 // passed as a const reference, this is not possible.
3145 // TODO(clamy): When we only create a NavigationRequest, move this to
3146 // CreateNavigationRequestFromLoadURLParams.
3147 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3148 GetLastCommittedEntry());
3149
3150 // Don't allow an entry replacement if there is no entry to replace.
3151 // http://crbug.com/457149
3152 bool should_replace_current_entry =
3153 params.should_replace_current_entry && entries_.size();
3154
Scott Violet5ae6c42e2020-10-28 02:47:373155 ui::PageTransition transition_type = params.transition_type;
3156
clamy21718cc22018-06-13 13:34:243157 // Javascript URLs should not create NavigationEntries. All other navigations
3158 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243159 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373160 std::unique_ptr<NavigationEntryImpl> entry =
3161 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3162 should_replace_current_entry,
3163 params.has_user_gesture);
3164 // CreateNavigationEntryFromLoadParams() may modify the transition type.
3165 transition_type = entry->GetTransitionType();
clamy21718cc22018-06-13 13:34:243166 DiscardPendingEntry(false);
3167 SetPendingEntry(std::move(entry));
3168 }
3169
3170 // Renderer-debug URLs are sent to the renderer process immediately for
3171 // processing and don't need to create a NavigationRequest.
3172 // Note: this includes navigations to JavaScript URLs, which are considered
3173 // renderer-debug URLs.
3174 // Note: we intentionally leave the pending entry in place for renderer debug
3175 // URLs, unlike the cases below where we clear it if the navigation doesn't
3176 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303177 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483178 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3179 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153180 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483181 params.url, browser_context_)) {
3182 DiscardPendingEntry(false);
3183 return;
3184 }
3185
clamy21718cc22018-06-13 13:34:243186 HandleRendererDebugURL(node, params.url);
3187 return;
3188 }
3189
Antonio Sartori78a749f2020-11-30 12:03:393190 DCHECK(pending_entry_);
3191
clamy21718cc22018-06-13 13:34:243192 // Convert navigations to the current URL to a reload.
3193 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3194 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3195 // DevTools sometimes issues navigations to main frames that they do not
3196 // expect to see treated as reload, and it only works because they pass a
3197 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3198 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543199 ReloadType reload_type = params.reload_type;
3200 if (reload_type == ReloadType::NONE &&
3201 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043202 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243203 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243204 params.load_type ==
3205 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
3206 false /* is_reload */, false /* is_navigation_to_existing_entry */,
clamy21718cc22018-06-13 13:34:243207 GetLastCommittedEntry())) {
3208 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473209 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393210
3211 // If this is a reload of an existing FrameNavigationEntry and we had a
3212 // policy container for it, then we should copy it into the pending entry,
3213 // so that it is copied to the navigation request in
3214 // CreateNavigationRequestFromLoadParams later.
3215 if (GetLastCommittedEntry()) {
3216 FrameNavigationEntry* previous_frame_entry =
3217 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193218 if (previous_frame_entry &&
3219 previous_frame_entry->policy_container_policies()) {
3220 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163221 previous_frame_entry->policy_container_policies()->Clone());
Antonio Sartori78a749f2020-11-30 12:03:393222 }
3223 }
3224 }
3225
3226 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
3227 // document (so no last commit), then we should copy the document polices from
3228 // RenderFrameHost's PolicyContainerHost. The NavigationRequest will create a
3229 // new PolicyContainerHost with the document policies from the
3230 // |pending_entry_|, and that PolicyContainerHost will be put in the final
3231 // RenderFrameHost for the navigation. This way, we ensure that we keep
3232 // enforcing the right policies on the initial empty document after the
3233 // reload.
3234 if (!GetLastCommittedEntry() && params.url.IsAboutBlank()) {
3235 if (node->current_frame_host() &&
3236 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193237 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163238 node->current_frame_host()
3239 ->policy_container_host()
3240 ->policies()
3241 .Clone());
Antonio Sartori78a749f2020-11-30 12:03:393242 }
clamy21718cc22018-06-13 13:34:243243 }
3244
3245 // navigation_ui_data should only be present for main frame navigations.
3246 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
3247
Camille Lamy5193caa2018-10-12 11:59:423248 std::unique_ptr<NavigationRequest> request =
3249 CreateNavigationRequestFromLoadParams(
3250 node, params, override_user_agent, should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:253251 params.has_user_gesture, blink::NavigationDownloadPolicy(),
3252 reload_type, pending_entry_, pending_entry_->GetFrameEntry(node),
3253 transition_type);
clamy21718cc22018-06-13 13:34:243254
3255 // If the navigation couldn't start, return immediately and discard the
3256 // pending NavigationEntry.
3257 if (!request) {
3258 DiscardPendingEntry(false);
3259 return;
3260 }
3261
Camille Lamy5193caa2018-10-12 11:59:423262#if DCHECK_IS_ON()
3263 // Safety check that NavigationRequest and NavigationEntry match.
3264 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3265#endif
3266
clamy21718cc22018-06-13 13:34:243267 // This call does not support re-entrancy. See http://crbug.com/347742.
3268 CHECK(!in_navigate_to_pending_entry_);
3269 in_navigate_to_pending_entry_ = true;
3270
arthursonzogni66f711c2019-10-08 14:40:363271 // It is not possible to delete the pending NavigationEntry while navigating
3272 // to it. Grab a reference to delay potential deletion until the end of this
3273 // function.
3274 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3275
Lukasz Anforowicz9ee83c272020-12-01 20:14:053276 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243277
3278 in_navigate_to_pending_entry_ = false;
3279}
3280
clamyea99ea12018-05-28 13:54:233281void NavigationControllerImpl::HandleRendererDebugURL(
3282 FrameTreeNode* frame_tree_node,
3283 const GURL& url) {
3284 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243285 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3286 // the renderer process is not live, unless it is the initial navigation
3287 // of the tab.
clamyea99ea12018-05-28 13:54:233288 if (!IsInitialNavigation()) {
3289 DiscardNonCommittedEntries();
3290 return;
3291 }
Fergal Dalyecd3b0202020-06-25 01:57:373292 // The current frame is always a main frame. If IsInitialNavigation() is
3293 // true then there have been no navigations and any frames of this tab must
3294 // be in the same renderer process. If that has crashed then the only frame
3295 // that can be revived is the main frame.
3296 frame_tree_node->render_manager()
3297 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233298 }
3299 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3300}
3301
clamy21718cc22018-06-13 13:34:243302std::unique_ptr<NavigationEntryImpl>
3303NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3304 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423305 const LoadURLParams& params,
3306 bool override_user_agent,
3307 bool should_replace_current_entry,
3308 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393309 // Browser initiated navigations might not have a blob_url_loader_factory set
3310 // in params even if the navigation is to a blob URL. If that happens, lookup
3311 // the correct url loader factory to use here.
3312 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483313 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473314 // Resolve the blob URL in the storage partition associated with the target
3315 // frame. This is the storage partition the URL will be loaded in, and only
3316 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393317 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473318 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393319 }
3320
clamy21718cc22018-06-13 13:34:243321 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443322 // extra_headers in params are \n separated; navigation entries want \r\n.
3323 std::string extra_headers_crlf;
3324 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243325
3326 // For subframes, create a pending entry with a corresponding frame entry.
3327 if (!node->IsMainFrame()) {
Tommy C. Li03eee77a2019-02-05 02:07:443328 if (GetLastCommittedEntry()) {
3329 // Create an identical NavigationEntry with a new FrameNavigationEntry for
3330 // the target subframe.
3331 entry = GetLastCommittedEntry()->Clone();
3332 } else {
3333 // If there's no last committed entry, create an entry for about:blank
3334 // with a subframe entry for our destination.
3335 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
3336 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063337 GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103338 params.source_site_instance.get(), params.transition_type,
3339 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:373340 blob_url_loader_factory, should_replace_current_entry,
3341 GetWebContents()));
Tommy C. Li03eee77a2019-02-05 02:07:443342 }
Nasko Oskov18006bc2018-12-06 02:53:583343
clamy21718cc22018-06-13 13:34:243344 entry->AddOrUpdateFrameEntry(
3345 node, -1, -1, nullptr,
3346 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Lukasz Anforowicz435bcb582019-07-12 20:50:063347 params.url, base::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203348 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393349 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:483350 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori78a749f2020-11-30 12:03:393351 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163352 // a conversion of a new navigation into a reload, we will set the right
3353 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193354 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243355 } else {
3356 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243357 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063358 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103359 params.source_site_instance.get(), params.transition_type,
3360 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:373361 blob_url_loader_factory, should_replace_current_entry,
3362 GetWebContents()));
clamy21718cc22018-06-13 13:34:243363 entry->set_source_site_instance(
3364 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3365 entry->SetRedirectChain(params.redirect_chain);
3366 }
3367
3368 // Set the FTN ID (only used in non-site-per-process, for tests).
3369 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243370 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423371 entry->SetIsOverridingUserAgent(override_user_agent);
3372 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543373 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243374
clamy21718cc22018-06-13 13:34:243375 switch (params.load_type) {
3376 case LOAD_TYPE_DEFAULT:
3377 break;
3378 case LOAD_TYPE_HTTP_POST:
3379 entry->SetHasPostData(true);
3380 entry->SetPostData(params.post_data);
3381 break;
3382 case LOAD_TYPE_DATA:
3383 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3384 entry->SetVirtualURL(params.virtual_url_for_data_url);
3385#if defined(OS_ANDROID)
3386 entry->SetDataURLAsString(params.data_url_as_string);
3387#endif
3388 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3389 break;
clamy21718cc22018-06-13 13:34:243390 }
3391
3392 // TODO(clamy): NavigationEntry is meant for information that will be kept
3393 // after the navigation ended and therefore is not appropriate for
3394 // started_from_context_menu. Move started_from_context_menu to
3395 // NavigationUIData.
3396 entry->set_started_from_context_menu(params.started_from_context_menu);
3397
3398 return entry;
3399}
3400
clamyea99ea12018-05-28 13:54:233401std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423402NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3403 FrameTreeNode* node,
3404 const LoadURLParams& params,
3405 bool override_user_agent,
3406 bool should_replace_current_entry,
3407 bool has_user_gesture,
Yeunjoo Choi3df791a2021-02-17 07:07:253408 blink::NavigationDownloadPolicy download_policy,
Camille Lamy5193caa2018-10-12 11:59:423409 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573410 NavigationEntryImpl* entry,
Scott Violet5ae6c42e2020-10-28 02:47:373411 FrameNavigationEntry* frame_entry,
3412 ui::PageTransition transition_type) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573413 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283414 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533415 // All renderer-initiated navigations must have an initiator_origin.
3416 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513417
Camille Lamy5193caa2018-10-12 11:59:423418 GURL url_to_load;
3419 GURL virtual_url;
Nasko Oskov03912102019-01-11 00:21:323420 base::Optional<url::Origin> origin_to_commit =
3421 frame_entry ? frame_entry->committed_origin() : base::nullopt;
3422
Camille Lamy2baa8022018-10-19 16:43:173423 // For main frames, rewrite the URL if necessary and compute the virtual URL
3424 // that should be shown in the address bar.
3425 if (node->IsMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423426 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173427 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423428 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423429
Camille Lamy2baa8022018-10-19 16:43:173430 // For DATA loads, override the virtual URL.
3431 if (params.load_type == LOAD_TYPE_DATA)
3432 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423433
Camille Lamy2baa8022018-10-19 16:43:173434 if (virtual_url.is_empty())
3435 virtual_url = url_to_load;
3436
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573437 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283438
Aran Gilman249eb122019-12-02 23:32:463439 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3440 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3441 // return different results, leading to a different URL in the
3442 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3443 // pending NavigationEntry, as we'll only make one call to
3444 // RewriteUrlForNavigation.
3445 VLOG_IF(1, (url_to_load != frame_entry->url()))
3446 << "NavigationRequest and FrameEntry have different URLs: "
3447 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283448
Camille Lamy2baa8022018-10-19 16:43:173449 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423450 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173451 // NavigationRequest.
3452 } else {
3453 url_to_load = params.url;
3454 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243455 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173456 }
Camille Lamy5193caa2018-10-12 11:59:423457
Ehsan Karamad44fc72112019-02-26 18:15:473458 if (node->render_manager()->is_attaching_inner_delegate()) {
3459 // Avoid starting any new navigations since this node is now preparing for
3460 // attaching an inner delegate.
3461 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203462 }
Camille Lamy5193caa2018-10-12 11:59:423463
Camille Lamy5193caa2018-10-12 11:59:423464 if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load))
3465 return nullptr;
3466
Kunihiko Sakamoto346a74e2021-03-10 08:57:483467 if (!DoesURLMatchOriginForNavigation(
3468 url_to_load, origin_to_commit,
3469 frame_entry->subresource_web_bundle_navigation_info())) {
Nasko Oskov03912102019-01-11 00:21:323470 DCHECK(false) << " url:" << url_to_load
3471 << " origin:" << origin_to_commit.value();
3472 return nullptr;
3473 }
3474
Camille Lamy5193caa2018-10-12 11:59:423475 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403476 blink::PreviewsState previews_state =
3477 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
Camille Lamy5193caa2018-10-12 11:59:423478 if (!node->IsMainFrame()) {
3479 // For subframes, use the state of the top-level frame.
3480 previews_state = node->frame_tree()
3481 ->root()
3482 ->current_frame_host()
3483 ->last_navigation_previews_state();
3484 }
3485
Camille Lamy5193caa2018-10-12 11:59:423486 // This will be used to set the Navigation Timing API navigationStart
3487 // parameter for browser navigations in new tabs (intents, tabs opened through
3488 // "Open link in new tab"). If the navigation must wait on the current
3489 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3490 // will be updated when the BeforeUnload ack is received.
3491 base::TimeTicks navigation_start = base::TimeTicks::Now();
3492
danakjd83d706d2020-11-25 22:11:123493 // Look for a pending commit that is to another document in this
3494 // FrameTreeNode. If one exists, then the last committed URL will not be the
3495 // current URL by the time this navigation commits.
3496 bool has_pending_cross_document_commit =
3497 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493498 bool is_currently_error_page = node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123499
danakjb952ef12021-01-14 19:58:493500 mojom::NavigationType navigation_type = GetNavigationType(
3501 /*old_url=*/node->current_url(),
3502 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3503 has_pending_cross_document_commit, is_currently_error_page,
3504 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423505
3506 // Create the NavigationParams based on |params|.
3507
3508 bool is_view_source_mode = virtual_url.SchemeIs(kViewSourceScheme);
Charlie Harrison8c113a32019-01-07 16:08:293509
3510 // Update |download_policy| if the virtual URL is view-source. Why do this
3511 // now? Possibly the URL could be rewritten to a view-source via some URL
3512 // handler.
3513 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253514 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293515
Camille Lamy5193caa2018-10-12 11:59:423516 const GURL& history_url_for_data_url =
3517 params.base_url_for_data_url.is_empty() ? GURL() : virtual_url;
Scott Violet5ae6c42e2020-10-28 02:47:373518 // Don't use |params.transition_type| as calling code may supply a different
3519 // value.
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513520 mojom::CommonNavigationParamsPtr common_params =
3521 mojom::CommonNavigationParams::New(
3522 url_to_load, params.initiator_origin,
3523 blink::mojom::Referrer::New(params.referrer.url,
3524 params.referrer.policy),
Scott Violet5ae6c42e2020-10-28 02:47:373525 transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513526 should_replace_current_entry, params.base_url_for_data_url,
3527 history_url_for_data_url, previews_state, navigation_start,
3528 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
arthursonzogniaf7c62c52020-02-12 10:49:413529 params.post_data, network::mojom::SourceLocation::New(),
3530 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273531 false /* has_text_fragment_token */,
3532 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3533 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513534 false /* is_history_navigation_in_new_child_frame */,
Charlie Hu5ffc0152019-12-06 15:59:533535 params.input_start);
Camille Lamy5193caa2018-10-12 11:59:423536
Lucas Furukawa Gadania9c45682019-07-31 22:05:143537 mojom::CommitNavigationParamsPtr commit_params =
3538 mojom::CommitNavigationParams::New(
arthursonzognid5a8d0b2021-03-11 17:36:433539 frame_entry->committed_origin(), network::mojom::WebSandboxFlags(),
3540 override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323541 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143542 std::vector<net::RedirectInfo>(),
3543 std::string() /* post_content_type */, common_params->url,
3544 common_params->method, params.can_load_local_resources,
3545 frame_entry->page_state(), entry->GetUniqueID(),
3546 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3547 -1 /* pending_history_list_offset */,
3548 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3549 params.should_clear_history_list ? 0 : GetEntryCount(),
3550 false /* was_discarded */, is_view_source_mode,
3551 params.should_clear_history_list, mojom::NavigationTiming::New(),
3552 base::nullopt /* appcache_host_id */,
3553 mojom::WasActivatedOption::kUnknown,
3554 base::UnguessableToken::Create() /* navigation_token */,
Lucas Furukawa Gadani97ea6382019-08-07 19:14:513555 std::vector<mojom::PrefetchedSignedExchangeInfoPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143556#if defined(OS_ANDROID)
3557 std::string(), /* data_url_as_string */
3558#endif
arthursonzogni14379782020-05-15 09:09:273559 !params.is_renderer_initiated, /* is_browser_initiated */
Sreeja Kamishetty46f762c2021-02-05 07:52:463560 node->frame_tree()->is_prerendering() /* is_prerendering */,
Tsuyoshi Horoe86d7702019-11-29 01:52:473561 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533562 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173563 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073564 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163565 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333566 false /* origin_agent_cluster */,
Maks Orlovichc66745a2020-06-30 17:40:023567 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213568 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Nate Chapinb2189ba2021-04-13 23:46:543569 false /* is_cross_browsing_instance */, nullptr /* old_page_info */,
3570 -1 /* http_response_code */,
3571 std::vector<
3572 mojom::AppHistoryEntryPtr>() /* app_history_back_entries */,
3573 std::vector<
3574 mojom::AppHistoryEntryPtr>() /* app_history_forward_entries */);
Camille Lamy5193caa2018-10-12 11:59:423575#if defined(OS_ANDROID)
3576 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143577 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423578 }
3579#endif
3580
Lucas Furukawa Gadania9c45682019-07-31 22:05:143581 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423582
3583 // A form submission may happen here if the navigation is a renderer-initiated
3584 // form submission that took the OpenURL path.
3585 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3586
3587 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3588 std::string extra_headers_crlf;
3589 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093590
3591 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143592 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083593 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453594 params.initiator_frame_token.has_value()
3595 ? &(params.initiator_frame_token.value())
3596 : nullptr,
3597 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3598 request_body,
John Delaney50425f82020-04-07 16:26:213599 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
3600 params.impression);
Yao Xiaodc5ed102019-06-04 19:19:093601 navigation_request->set_from_download_cross_origin_redirect(
3602 params.from_download_cross_origin_redirect);
3603 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423604}
3605
3606std::unique_ptr<NavigationRequest>
3607NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233608 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573609 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233610 FrameNavigationEntry* frame_entry,
3611 ReloadType reload_type,
3612 bool is_same_document_history_load,
Arthur Hemerybee4a752019-05-29 10:50:553613 bool is_history_navigation_in_new_child_frame) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343614 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233615 GURL dest_url = frame_entry->url();
Nasko Oskov03912102019-01-11 00:21:323616 base::Optional<url::Origin> origin_to_commit =
3617 frame_entry->committed_origin();
3618
clamyea99ea12018-05-28 13:54:233619 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013620 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573621 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233622 // We may have been redirected when navigating to the current URL.
3623 // Use the URL the user originally intended to visit as signaled by the
3624 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013625 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573626 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233627 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323628 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233629 }
3630
Ehsan Karamad44fc72112019-02-26 18:15:473631 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3632 // Avoid starting any new navigations since this node is now preparing for
3633 // attaching an inner delegate.
3634 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203635 }
3636
Camille Lamy5193caa2018-10-12 11:59:423637 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573638 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233639 return nullptr;
3640 }
3641
Kunihiko Sakamoto346a74e2021-03-10 08:57:483642 if (!DoesURLMatchOriginForNavigation(
3643 dest_url, origin_to_commit,
3644 frame_entry->subresource_web_bundle_navigation_info())) {
3645 DCHECK(false) << " url:" << dest_url
3646 << " origin:" << origin_to_commit.value();
Nasko Oskov03912102019-01-11 00:21:323647 return nullptr;
3648 }
3649
clamyea99ea12018-05-28 13:54:233650 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403651 blink::PreviewsState previews_state =
3652 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
clamyea99ea12018-05-28 13:54:233653 if (!frame_tree_node->IsMainFrame()) {
3654 // For subframes, use the state of the top-level frame.
3655 previews_state = frame_tree_node->frame_tree()
3656 ->root()
3657 ->current_frame_host()
3658 ->last_navigation_previews_state();
clamyea99ea12018-05-28 13:54:233659 }
3660
clamyea99ea12018-05-28 13:54:233661 // This will be used to set the Navigation Timing API navigationStart
3662 // parameter for browser navigations in new tabs (intents, tabs opened through
3663 // "Open link in new tab"). If the navigation must wait on the current
3664 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3665 // will be updated when the BeforeUnload ack is received.
3666 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233667
danakjd83d706d2020-11-25 22:11:123668 // Look for a pending commit that is to another document in this
3669 // FrameTreeNode. If one exists, then the last committed URL will not be the
3670 // current URL by the time this navigation commits.
3671 bool has_pending_cross_document_commit =
3672 frame_tree_node->render_manager()
3673 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493674 bool is_currently_error_page =
3675 frame_tree_node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123676
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513677 mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123678 /*old_url=*/frame_tree_node->current_url(),
3679 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493680 has_pending_cross_document_commit, is_currently_error_page,
3681 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423682
3683 // A form submission may happen here if the navigation is a
3684 // back/forward/reload navigation that does a form resubmission.
3685 scoped_refptr<network::ResourceRequestBody> request_body;
3686 std::string post_content_type;
3687 if (frame_entry->method() == "POST") {
3688 request_body = frame_entry->GetPostData(&post_content_type);
3689 // Might have a LF at end.
3690 post_content_type =
3691 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL)
3692 .as_string();
3693 }
3694
3695 // Create the NavigationParams based on |entry| and |frame_entry|.
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513696 mojom::CommonNavigationParamsPtr common_params =
3697 entry->ConstructCommonNavigationParams(
3698 *frame_entry, request_body, dest_url,
3699 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
3700 navigation_type, previews_state, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533701 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513702 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553703 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423704
3705 // TODO(clamy): |intended_as_new_entry| below should always be false once
3706 // Reload no longer leads to this being called for a pending NavigationEntry
3707 // of index -1.
Lucas Furukawa Gadania9c45682019-07-31 22:05:143708 mojom::CommitNavigationParamsPtr commit_params =
3709 entry->ConstructCommitNavigationParams(
3710 *frame_entry, common_params->url, origin_to_commit,
3711 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3712 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533713 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3714 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143715 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423716
clamyea99ea12018-05-28 13:54:233717 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143718 frame_tree_node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083719 !entry->is_renderer_initiated(), false /* was_opener_suppressed */,
3720 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453721 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3722 entry->extra_headers(), frame_entry, entry, request_body,
3723 nullptr /* navigation_ui_data */, base::nullopt /* impression */);
clamyea99ea12018-05-28 13:54:233724}
3725
[email protected]d202a7c2012-01-04 07:53:473726void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213727 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273728 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403729
[email protected]2db9bd72012-04-13 20:20:563730 // We need to notify the ssl_manager_ before the web_contents_ so the
[email protected]df1af242009-05-01 00:11:403731 // location bar will have up-to-date information about the security style
3732 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133733 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403734
[email protected]7f924832014-08-09 05:57:223735 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:573736 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463737
[email protected]b0f724c2013-09-05 04:21:133738 // TODO(avi): Remove. http://crbug.com/170921
3739 NotificationDetails notification_details =
3740 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153741 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3742 Source<NavigationController>(this),
3743 notification_details);
initial.commit09911bf2008-07-26 23:55:293744}
3745
initial.commit09911bf2008-07-26 23:55:293746// static
[email protected]d202a7c2012-01-04 07:53:473747size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233748 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153749 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:213750 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233751}
3752
[email protected]d202a7c2012-01-04 07:53:473753void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223754 if (is_active && needs_reload_)
3755 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293756}
3757
[email protected]d202a7c2012-01-04 07:53:473758void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293759 if (!needs_reload_)
3760 return;
3761
Bo Liucdfa4b12018-11-06 00:21:443762 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3763 needs_reload_type_);
3764
initial.commit09911bf2008-07-26 23:55:293765 // Calling Reload() results in ignoring state, and not loading.
3766 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3767 // cached state.
avicc872d7242015-08-19 21:26:343768 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163769 NavigateToExistingPendingEntry(ReloadType::NONE,
3770 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343771 } else if (last_committed_entry_index_ != -1) {
arthursonzogni5c4c202d2017-04-25 23:41:273772 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343773 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163774 NavigateToExistingPendingEntry(ReloadType::NONE,
3775 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343776 } else {
3777 // If there is something to reload, the successful reload will clear the
3778 // |needs_reload_| flag. Otherwise, just do it here.
3779 needs_reload_ = false;
3780 }
initial.commit09911bf2008-07-26 23:55:293781}
3782
Carlos IL42b416592019-10-07 23:10:363783void NavigationControllerImpl::LoadPostCommitErrorPage(
3784 RenderFrameHost* render_frame_host,
3785 const GURL& url,
3786 const std::string& error_page_html,
3787 net::Error error) {
Rakina Zata Amni919b7922020-12-11 09:03:133788 RenderFrameHostImpl* rfhi =
3789 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:583790
3791 // Only active documents can load post-commit error pages:
3792 // - If the document is in pending deletion, the browser already committed to
3793 // destroying this RenderFrameHost so ignore loading the error page.
3794 // - If the document is in back-forward cache, it's not allowed to navigate as
3795 // it should remain frozen. Ignore the request and evict the document from
3796 // back-forward cache.
3797 // - If the document is prerendering, it can navigate but when loading error
3798 // pages, cancel prerendering.
3799 if (rfhi->IsInactiveAndDisallowActivation())
3800 return;
3801
Rakina Zata Amni919b7922020-12-11 09:03:133802 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:413803
3804 mojom::CommonNavigationParamsPtr common_params =
3805 CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:593806 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
3807 // the frame actually committed (e.g. iframe with "src" set to a
3808 // slow-responding URL). We should rewrite the URL to about:blank in this
3809 // case, as the renderer will only think a page is an error page if it has a
3810 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:133811 common_params->url = url.is_empty() ? GURL("about:blank") : url;
John Delaney131ad362019-08-08 21:57:413812 mojom::CommitNavigationParamsPtr commit_params =
3813 CreateCommitNavigationParams();
3814
arthursonzogni70ac7302020-05-28 08:49:053815 // Error pages have a fully permissive FramePolicy.
3816 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
3817 // error pages.
3818 commit_params->frame_policy = blink::FramePolicy();
3819
John Delaney131ad362019-08-08 21:57:413820 std::unique_ptr<NavigationRequest> navigation_request =
3821 NavigationRequest::CreateBrowserInitiated(
3822 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083823 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:193824 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453825 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:063826 "" /* extra_headers */, nullptr /* frame_entry */,
3827 nullptr /* entry */, nullptr /* post_body */,
3828 nullptr /* navigation_ui_data */, base::nullopt /* impression */);
Carlos IL42b416592019-10-07 23:10:363829 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:413830 navigation_request->set_net_error(error);
3831 node->CreatedNavigationRequest(std::move(navigation_request));
3832 DCHECK(node->navigation_request());
3833 node->navigation_request()->BeginNavigation();
3834}
3835
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573836void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213837 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093838 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:153839 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143840 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293841}
3842
[email protected]d202a7c2012-01-04 07:53:473843void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363844 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553845 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363846 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293847
initial.commit09911bf2008-07-26 23:55:293848 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293849}
[email protected]765b35502008-08-21 00:51:203850
arthursonzogni69a6a1b2019-09-17 09:23:003851void NavigationControllerImpl::DiscardNonCommittedEntries() {
Michael Thiessen9b14d512019-09-23 21:19:473852 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:103853 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
3854 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:293855 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:473856 return;
Michael Thiessenc5676d22019-09-25 22:32:103857 }
avi45a72532015-04-07 21:01:453858 DiscardPendingEntry(false);
arthursonzogni69a6a1b2019-09-17 09:23:003859 if (delegate_)
3860 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:483861}
3862
avi7c6f35e2015-05-08 17:52:383863int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3864 int nav_entry_id) const {
3865 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3866 if (entries_[i]->GetUniqueID() == nav_entry_id)
3867 return i;
3868 }
3869 return -1;
3870}
3871
[email protected]d202a7c2012-01-04 07:53:473872void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573873 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253874 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573875 DCHECK_LE(max_index, source->GetEntryCount());
[email protected]e1cd5452010-08-26 18:03:253876 for (int i = 0; i < max_index; i++) {
Nasko Oskov923ca482020-12-04 21:20:463877 // TODO(creis): Once we start sharing FrameNavigationEntries between
3878 // NavigationEntries, it will not be safe to share them with another tab.
3879 // Must have a version of Clone that recreates them.
3880 entries_.insert(entries_.begin() + i, source->entries_[i]->Clone());
[email protected]e1cd5452010-08-26 18:03:253881 }
arthursonzogni5c4c202d2017-04-25 23:41:273882 DCHECK(pending_entry_index_ == -1 ||
3883 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:253884}
[email protected]c5b88d82012-10-06 17:03:333885
3886void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:183887 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:333888 get_timestamp_callback_ = get_timestamp_callback;
3889}
[email protected]8ff00d72012-10-23 19:12:213890
Shivani Sharmaffb32b82019-04-09 16:58:473891// History manipulation intervention:
3892void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:473893 bool replace_entry,
3894 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:403895 bool is_renderer_initiated,
3896 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:453897 // Note that for a subframe, previous_document_was_activated is true if the
3898 // gesture happened in any subframe (propagated to main frame) or in the main
3899 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:473900 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:273901 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:473902 if (last_committed_entry_index_ != -1) {
shivanigithube92c33da2020-09-14 13:01:413903 // This histogram always counts when navigating away from an entry,
3904 // irrespective of whether the skippable flag was changed or not, and
3905 // whether this entry is being reused or not.
Shivani Sharmaffb32b82019-04-09 16:58:473906 UMA_HISTOGRAM_BOOLEAN(
shivanigithube92c33da2020-09-14 13:01:413907 "Navigation.BackForward.SetShouldSkipOnBackForwardUI",
3908 GetEntryAtIndex(last_committed_entry_index_)
3909 ->should_skip_on_back_forward_ui());
Shivani Sharmaffb32b82019-04-09 16:58:473910 }
3911 return;
3912 }
3913 if (last_committed_entry_index_ == -1)
3914 return;
3915
Shivani Sharmac4cc8922019-04-18 03:11:173916 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473917 UMA_HISTOGRAM_BOOLEAN("Navigation.BackForward.SetShouldSkipOnBackForwardUI",
3918 true);
3919
Alexander Timine3ec4192020-04-20 16:39:403920 // Log UKM with the URL we are navigating away from.
3921 ukm::builders::HistoryManipulationIntervention(
3922 previous_page_load_ukm_source_id)
3923 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:473924}
3925
Shivani Sharmac4cc8922019-04-18 03:11:173926void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
3927 int reference_index,
3928 bool skippable) {
3929 auto* reference_entry = GetEntryAtIndex(reference_index);
3930 reference_entry->set_should_skip_on_back_forward_ui(skippable);
3931
3932 int64_t document_sequence_number =
3933 reference_entry->root_node()->frame_entry->document_sequence_number();
3934 for (int index = 0; index < GetEntryCount(); index++) {
3935 auto* entry = GetEntryAtIndex(index);
3936 if (entry->root_node()->frame_entry->document_sequence_number() ==
3937 document_sequence_number) {
3938 entry->set_should_skip_on_back_forward_ui(skippable);
3939 }
3940 }
3941}
3942
arthursonzogni66f711c2019-10-08 14:40:363943std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
3944NavigationControllerImpl::ReferencePendingEntry() {
3945 DCHECK(pending_entry_);
3946 auto pending_entry_ref =
3947 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
3948 pending_entry_refs_.insert(pending_entry_ref.get());
3949 return pending_entry_ref;
3950}
3951
3952void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
3953 // Ignore refs that don't correspond to the current pending entry.
3954 auto it = pending_entry_refs_.find(ref);
3955 if (it == pending_entry_refs_.end())
3956 return;
3957 pending_entry_refs_.erase(it);
3958
3959 if (!pending_entry_refs_.empty())
3960 return;
3961
3962 // The pending entry may be deleted before the last PendingEntryRef.
3963 if (!pending_entry_)
3964 return;
3965
3966 // We usually clear the pending entry when the matching NavigationRequest
3967 // fails, so that an arbitrary URL isn't left visible above a committed page.
3968 //
3969 // However, we do preserve the pending entry in some cases, such as on the
3970 // initial navigation of an unmodified blank tab. We also allow the delegate
3971 // to say when it's safe to leave aborted URLs in the omnibox, to let the
3972 // user edit the URL and try again. This may be useful in cases that the
3973 // committed page cannot be attacker-controlled. In these cases, we still
3974 // allow the view to clear the pending entry and typed URL if the user
3975 // requests (e.g., hitting Escape with focus in the address bar).
3976 //
3977 // Do not leave the pending entry visible if it has an invalid URL, since this
3978 // might be formatted in an unexpected or unsafe way.
3979 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:363980 bool should_preserve_entry =
3981 (pending_entry_ == GetVisibleEntry()) &&
3982 pending_entry_->GetURL().is_valid() &&
3983 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
3984 if (should_preserve_entry)
3985 return;
3986
3987 DiscardPendingEntry(true);
3988 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3989}
3990
Titouan Rigoudy6ec70402021-02-02 15:42:193991std::unique_ptr<PolicyContainerPolicies>
3992NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:393993 RenderFrameHostImpl* rfh,
3994 bool is_same_document,
3995 NavigationRequest* request) {
Titouan Rigoudy6ec70402021-02-02 15:42:193996 if (!ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(request))
Antonio Sartori78a749f2020-11-30 12:03:393997 return nullptr;
3998
3999 if (is_same_document) {
Charlie Reis73e356242021-04-02 17:10:314000 // TODO(https://crbug.com/524208): Remove this nullptr check when we can
4001 // ensure we always have a FrameNavigationEntry here.
4002 if (!GetLastCommittedEntry())
4003 return nullptr;
4004
Antonio Sartori78a749f2020-11-30 12:03:394005 FrameNavigationEntry* previous_frame_entry =
4006 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
4007
4008 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
4009 // ensure we always have a FrameNavigationEntry here.
4010 if (!previous_frame_entry)
4011 return nullptr;
4012
Titouan Rigoudy6ec70402021-02-02 15:42:194013 const PolicyContainerPolicies* previous_policies =
4014 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:394015
Titouan Rigoudy6ec70402021-02-02 15:42:194016 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:394017 return nullptr;
4018
4019 // Make a copy of the policy container for the new FrameNavigationEntry.
Antonio Sartori5d09b30f2021-03-02 09:27:164020 return previous_policies->Clone();
Antonio Sartori78a749f2020-11-30 12:03:394021 }
4022
4023 if (!request->IsWaitingToCommit()) {
4024 // This is the initial, "fake" navigation to about:blank. The
4025 // NavigationRequest contains a dummy policy container, while the
4026 // RenderFrameHost already inherited the policy container from the
4027 // creator, so let's take the policies from there.
Antonio Sartori5d09b30f2021-03-02 09:27:164028 return rfh->policy_container_host()->policies().Clone();
Antonio Sartori78a749f2020-11-30 12:03:394029 }
4030
4031 // Take the policy container from the request since we did not move it
4032 // into the RFH yet.
Antonio Sartori5d09b30f2021-03-02 09:27:164033 return request->GetPolicyContainerPolicies().Clone();
Antonio Sartori78a749f2020-11-30 12:03:394034}
4035
Carlos Caballeroede6f8c2021-01-28 11:01:504036void NavigationControllerImpl::SetHistoryOffsetAndLength(int history_offset,
4037 int history_length) {
4038 OPTIONAL_TRACE_EVENT2(
4039 "content", "NavigationControllerImpl::SetHistoryOffsetAndLength",
4040 "history_offset", history_offset, "history_length", history_length);
4041
4042 auto callback = base::BindRepeating(
4043 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
4044 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
4045 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
4046 }
4047 },
4048 history_offset, history_length);
4049 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
4050}
4051
4052void NavigationControllerImpl::DidAccessInitialMainDocument() {
4053 // We may have left a failed browser-initiated navigation in the address bar
4054 // to let the user edit it and try again. Clear it now that content might
4055 // show up underneath it.
4056 if (!frame_tree_.IsLoading() && GetPendingEntry())
4057 DiscardPendingEntry(false);
4058
4059 // Update the URL display.
4060 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
4061}
4062
4063void NavigationControllerImpl::UpdateStateForFrame(
4064 RenderFrameHostImpl* rfhi,
4065 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:564066 OPTIONAL_TRACE_EVENT1("content",
4067 "NavigationControllerImpl::UpdateStateForFrame",
4068 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:504069 // The state update affects the last NavigationEntry associated with the given
4070 // |render_frame_host|. This may not be the last committed NavigationEntry (as
4071 // in the case of an UpdateState from a frame being swapped out). We track
4072 // which entry this is in the RenderFrameHost's nav_entry_id.
4073 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
4074 if (!entry)
4075 return;
4076
4077 FrameNavigationEntry* frame_entry =
4078 entry->GetFrameEntry(rfhi->frame_tree_node());
4079 if (!frame_entry)
4080 return;
4081
4082 // The SiteInstance might not match if we do a cross-process navigation with
4083 // replacement (e.g., auto-subframe), in which case the swap out of the old
4084 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4085 // has already been replaced and destroyed.
4086 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4087 return;
4088
4089 if (page_state == frame_entry->page_state())
4090 return; // Nothing to update.
4091
4092 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4093
4094 // The document_sequence_number and item_sequence_number recorded in the
4095 // FrameNavigationEntry should not differ from the one coming with the update,
4096 // since it must come from the same document. Do not update it if a difference
4097 // is detected, as this indicates that |frame_entry| is not the correct one.
4098 blink::ExplodedPageState exploded_state;
4099 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4100 return;
4101
4102 if (exploded_state.top.document_sequence_number !=
4103 frame_entry->document_sequence_number() ||
4104 exploded_state.top.item_sequence_number !=
4105 frame_entry->item_sequence_number()) {
4106 return;
4107 }
4108
4109 frame_entry->SetPageState(page_state);
4110 NotifyEntryChanged(entry);
4111}
4112
Aaron Colwellb731a0ae2021-03-19 19:14:474113void NavigationControllerImpl::OnStoragePartitionIdAdded(
4114 const StoragePartitionId& partition_id) {
4115 auto it = partition_config_to_id_map_.insert(
4116 std::make_pair(partition_id.config(), partition_id));
4117 bool successful_insert = it.second;
4118 DCHECK(successful_insert);
4119 if (!successful_insert) {
4120 LogStoragePartitionIdCrashKeys(it.first->second, partition_id);
4121 }
4122}
4123
4124void NavigationControllerImpl::LogStoragePartitionIdCrashKeys(
4125 const StoragePartitionId& original_partition_id,
4126 const StoragePartitionId& new_partition_id) {
4127 base::debug::SetCrashKeyString(
4128 base::debug::AllocateCrashKeyString("original_partition_id",
4129 base::debug::CrashKeySize::Size256),
4130 original_partition_id.ToString());
4131
4132 base::debug::SetCrashKeyString(
4133 base::debug::AllocateCrashKeyString("new_partition_id",
4134 base::debug::CrashKeySize::Size256),
4135 new_partition_id.ToString());
4136
4137 base::debug::DumpWithoutCrashing();
4138}
4139
Nate Chapinb2189ba2021-04-13 23:46:544140std::vector<mojom::AppHistoryEntryPtr>
4141NavigationControllerImpl::PopulateSingleAppHistoryEntryVector(
4142 Direction direction,
4143 int entry_index,
4144 const url::Origin& pending_origin,
4145 FrameTreeNode* node,
4146 SiteInstance* site_instance,
4147 int64_t previous_item_sequence_number) {
4148 std::vector<mojom::AppHistoryEntryPtr> entries;
4149 int offset = direction == Direction::kForward ? 1 : -1;
4150 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4151 i += offset) {
4152 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
4153 if (!frame_entry || !frame_entry->committed_origin())
4154 break;
4155 if (site_instance != frame_entry->site_instance())
4156 break;
4157 if (!pending_origin.IsSameOriginWith(*frame_entry->committed_origin()))
4158 break;
4159 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4160 continue;
4161 blink::ExplodedPageState exploded_page_state;
4162 if (blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4163 &exploded_page_state)) {
4164 blink::ExplodedFrameState frame_state = exploded_page_state.top;
4165 mojom::AppHistoryEntryPtr entry = mojom::AppHistoryEntry::New(
4166 frame_state.app_history_key.value_or(std::u16string()),
4167 frame_state.app_history_id.value_or(std::u16string()),
4168 frame_state.url_string.value_or(std::u16string()));
4169 DCHECK(pending_origin.CanBeDerivedFrom(GURL(entry->url)));
4170 entries.push_back(std::move(entry));
4171 previous_item_sequence_number = frame_entry->item_sequence_number();
4172 }
4173 }
4174 // If |entries| was constructed by iterating backwards from
4175 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4176 // earliest-at-the-front. Reverse it.
4177 if (direction == Direction::kBack)
4178 std::reverse(entries.begin(), entries.end());
4179 return entries;
4180}
4181
4182void NavigationControllerImpl::PopulateAppHistoryEntryVectors(
4183 NavigationRequest* request) {
4184 url::Origin pending_origin =
4185 request->commit_params().origin_to_commit
4186 ? *request->commit_params().origin_to_commit
4187 : url::Origin::Create(request->common_params().url);
4188
4189 FrameTreeNode* node = request->frame_tree_node();
4190 scoped_refptr<SiteInstance> site_instance =
4191 request->GetRenderFrameHost()->GetSiteInstance();
4192
4193 // NOTE: |entry_index| is an estimate of the index where this entry will
4194 // commit, but it may be wrong in corner cases (e.g., if we are at the max
4195 // entry limit, the earliest entry will be dropped). This is ok because this
4196 // algorithm only uses |entry_index| to walk the entry list as it stands right
4197 // now, and it isn't saved for anything post-commit.
4198 int entry_index = GetPendingEntryIndex();
4199 bool will_create_new_entry = false;
4200 if (NavigationTypeUtils::IsReload(request->common_params().navigation_type) ||
4201 request->common_params().should_replace_current_entry) {
4202 entry_index = GetLastCommittedEntryIndex();
4203 } else if (entry_index == -1) {
4204 will_create_new_entry = true;
4205 entry_index = GetLastCommittedEntryIndex() + 1;
4206 }
4207
4208 int64_t pending_item_sequence_number = 0;
4209 if (auto* pending_entry = GetPendingEntry()) {
4210 if (auto* frame_entry = pending_entry->GetFrameEntry(node))
4211 pending_item_sequence_number = frame_entry->item_sequence_number();
4212 }
4213
4214 request->set_app_history_back_entries(PopulateSingleAppHistoryEntryVector(
4215 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
4216 pending_item_sequence_number));
4217
4218 // Don't populate forward entries if they will be truncated by a new entry.
4219 if (!will_create_new_entry) {
4220 request->set_app_history_forward_entries(
4221 PopulateSingleAppHistoryEntryVector(
4222 Direction::kForward, entry_index, pending_origin, node,
4223 site_instance.get(), pending_item_sequence_number));
4224 }
4225}
4226
[email protected]8ff00d72012-10-23 19:12:214227} // namespace content