blob: 08835ae2d998ccc0c97ab2ea121021a039b5db62 [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"
Nasko Oskovae49e292020-08-13 02:08:5173#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1174#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1975#include "content/public/browser/content_browser_client.h"
[email protected]d9083482012-01-06 00:38:4676#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0077#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3878#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1679#include "content/public/browser/notification_types.h"
Lei Zhang96031532019-10-10 19:05:4780#include "content/public/browser/render_view_host.h"
[email protected]9677a3c2012-12-22 04:18:5881#include "content/public/browser/render_widget_host.h"
82#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1083#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3484#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:1985#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:3886#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:1087#include "content/public/common/content_features.h"
Lei Zhang96031532019-10-10 19:05:4788#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4389#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:5990#include "media/base/mime_util.h"
[email protected]a23de8572009-06-03 02:16:3291#include "net/base/escape.h"
Arthur Sonzogni620cec62018-12-13 13:08:5792#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:1893#include "services/metrics/public/cpp/ukm_builders.h"
94#include "services/metrics/public/cpp/ukm_recorder.h"
Yue Ru Sun128804932020-09-30 22:19:1795#include "services/metrics/public/cpp/ukm_source_id.h"
[email protected]9677a3c2012-12-22 04:18:5896#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3997#include "third_party/blink/public/common/blob/blob_utils.h"
Gyuyoung Kim107c2a02021-04-13 01:49:3098#include "third_party/blink/public/common/chrome_debug_urls.h"
Miyoung Shin1c565c912021-03-17 12:11:2199#include "third_party/blink/public/common/history/session_history_constants.h"
Blink Reformata30d4232018-04-07 15:31:06100#include "third_party/blink/public/common/mime_util/mime_util.h"
Carlos Caballeroede6f8c2021-01-28 11:01:50101#include "third_party/blink/public/common/page_state/page_state_serialization.h"
[email protected]cca6f392014-05-28 21:32:26102#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:29103
[email protected]8ff00d72012-10-23 19:12:21104namespace content {
[email protected]e9ba4472008-09-14 15:42:43105namespace {
106
107// Invoked when entries have been pruned, or removed. For example, if the
108// current entries are [google, digg, yahoo], with the current entry google,
109// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47110void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50111 int index,
[email protected]c12bf1a12008-09-17 16:28:49112 int count) {
[email protected]8ff00d72012-10-23 19:12:21113 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50114 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49115 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14116 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43117}
118
[email protected]e9ba4472008-09-14 15:42:43119// Configure all the NavigationEntries in entries for restore. This resets
120// the transition type to reload and makes sure the content state isn't empty.
121void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57122 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48123 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47124 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43125 // Use a transition type of reload so that we don't incorrectly increase
126 // the typed count.
Lei Zhang96031532019-10-10 19:05:47127 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
128 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43129 }
130}
131
[email protected]bf70edce2012-06-20 22:32:22132// Determines whether or not we should be carrying over a user agent override
133// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57134bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22135 return last_entry && last_entry->GetIsOverridingUserAgent();
136}
137
Camille Lamy5193caa2018-10-12 11:59:42138// Determines whether to override user agent for a navigation.
139bool ShouldOverrideUserAgent(
140 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57141 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42142 switch (override_user_agent) {
143 case NavigationController::UA_OVERRIDE_INHERIT:
144 return ShouldKeepOverride(last_committed_entry);
145 case NavigationController::UA_OVERRIDE_TRUE:
146 return true;
147 case NavigationController::UA_OVERRIDE_FALSE:
148 return false;
Camille Lamy5193caa2018-10-12 11:59:42149 }
150 NOTREACHED();
151 return false;
152}
153
clamy0a656e42018-02-06 18:18:28154// Returns true this navigation should be treated as a reload. For e.g.
155// navigating to the last committed url via the address bar or clicking on a
156// link which results in a navigation to the last committed or pending
157// navigation, etc.
Fergal Daly766177d2020-07-07 07:54:04158// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
159// |base_url_for_data_url|, |transition_type| correspond to the new navigation
160// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
161// navigation that committed.
162bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
163 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57164 const GURL& virtual_url,
165 const GURL& base_url_for_data_url,
166 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57167 bool is_post,
168 bool is_reload,
169 bool is_navigation_to_existing_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57170 NavigationEntryImpl* last_committed_entry) {
Fergal Daly766177d2020-07-07 07:54:04171 if (is_reload || is_navigation_to_existing_entry)
clamy0a656e42018-02-06 18:18:28172 return false;
clamy0a656e42018-02-06 18:18:28173 // Only convert to reload if at least one navigation committed.
174 if (!last_committed_entry)
ananta3bdd8ae2016-12-22 17:11:55175 return false;
176
arthursonzogni7a8243682017-12-14 16:41:42177 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28178 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42179 return false;
180
ananta3bdd8ae2016-12-22 17:11:55181 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
182 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
183 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28184 bool transition_type_can_be_converted = false;
185 if (ui::PageTransitionCoreTypeIs(transition_type,
186 ui::PAGE_TRANSITION_RELOAD) &&
187 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
188 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55189 }
clamy0a656e42018-02-06 18:18:28190 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55191 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28192 transition_type_can_be_converted = true;
193 }
194 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
195 transition_type_can_be_converted = true;
196 if (!transition_type_can_be_converted)
197 return false;
198
199 // This check is required for cases like view-source:, etc. Here the URL of
200 // the navigation entry would contain the url of the page, while the virtual
201 // URL contains the full URL including the view-source prefix.
202 if (virtual_url != last_committed_entry->GetVirtualURL())
203 return false;
204
Fergal Daly766177d2020-07-07 07:54:04205 // Check that the URLs match.
206 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
207 // If there's no frame entry then by definition the URLs don't match.
208 if (!frame_entry)
209 return false;
210
211 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28212 return false;
213
214 // This check is required for Android WebView loadDataWithBaseURL. Apps
215 // can pass in anything in the base URL and we need to ensure that these
216 // match before classifying it as a reload.
217 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
218 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
219 return false;
ananta3bdd8ae2016-12-22 17:11:55220 }
221
clamy0a656e42018-02-06 18:18:28222 // Skip entries with SSL errors.
223 if (last_committed_entry->ssl_error())
224 return false;
225
226 // Don't convert to a reload when the last navigation was a POST or the new
227 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04228 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28229 return false;
230
231 return true;
ananta3bdd8ae2016-12-22 17:11:55232}
233
Nasko Oskov03912102019-01-11 00:21:32234bool DoesURLMatchOriginForNavigation(
235 const GURL& url,
Kunihiko Sakamoto346a74e2021-03-10 08:57:48236 const base::Optional<url::Origin>& origin,
237 SubresourceWebBundleNavigationInfo*
238 subresource_web_bundle_navigation_info) {
Nasko Oskov03912102019-01-11 00:21:32239 // If there is no origin supplied there is nothing to match. This can happen
240 // for navigations to a pending entry and therefore it should be allowed.
241 if (!origin)
242 return true;
243
Kunihiko Sakamoto346a74e2021-03-10 08:57:48244 if (url.SchemeIs(url::kUrnScheme) && subresource_web_bundle_navigation_info) {
245 // Urn: subframe from WebBundle has an opaque origin derived from the
246 // Bundle's origin.
247 return origin->CanBeDerivedFrom(
248 subresource_web_bundle_navigation_info->bundle_url());
249 }
250
Nasko Oskov03912102019-01-11 00:21:32251 return origin->CanBeDerivedFrom(url);
252}
253
254base::Optional<url::Origin> GetCommittedOriginForFrameEntry(
arthursonzogni73fe3212020-11-17 13:24:07255 const mojom::DidCommitProvisionalLoadParams& params) {
Nasko Oskov03912102019-01-11 00:21:32256 // Error pages commit in an opaque origin, yet have the real URL that resulted
257 // in an error as the |params.url|. Since successful reload of an error page
258 // should commit in the correct origin, setting the opaque origin on the
259 // FrameNavigationEntry will be incorrect.
260 if (params.url_is_unreachable)
261 return base::nullopt;
262
263 return base::make_optional(params.origin);
264}
265
Camille Lamy5193caa2018-10-12 11:59:42266bool IsValidURLForNavigation(bool is_main_frame,
267 const GURL& virtual_url,
268 const GURL& dest_url) {
269 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
270 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
271 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
272 << virtual_url.possibly_invalid_spec();
273 return false;
274 }
275
276 // Don't attempt to navigate to non-empty invalid URLs.
277 if (!dest_url.is_valid() && !dest_url.is_empty()) {
278 LOG(WARNING) << "Refusing to load invalid URL: "
279 << dest_url.possibly_invalid_spec();
280 return false;
281 }
282
283 // The renderer will reject IPC messages with URLs longer than
284 // this limit, so don't attempt to navigate with a longer URL.
285 if (dest_url.spec().size() > url::kMaxURLChars) {
286 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
287 << " characters.";
288 return false;
289 }
290
Aaron Colwell33109c592020-04-21 21:31:19291 // Reject renderer debug URLs because they should have been handled before
292 // we get to this point. This check handles renderer debug URLs
293 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
294 // provides defense-in-depth if a renderer debug URL manages to get here via
295 // some other path. We want to reject the navigation here so it doesn't
296 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30297 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19298 LOG(WARNING) << "Refusing to load renderer debug URL: "
299 << dest_url.possibly_invalid_spec();
300 return false;
301 }
302
Camille Lamy5193caa2018-10-12 11:59:42303 return true;
304}
305
Mikel Astizba9cf2fd2017-12-17 10:38:10306// See replaced_navigation_entry_data.h for details: this information is meant
307// to ensure |*output_entry| keeps track of its original URL (landing page in
308// case of server redirects) as it gets replaced (e.g. history.replaceState()),
309// without overwriting it later, for main frames.
310void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57311 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10312 NavigationEntryImpl* output_entry) {
313 if (output_entry->GetReplacedEntryData().has_value())
314 return;
315
316 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57317 data.first_committed_url = replaced_entry->GetURL();
318 data.first_timestamp = replaced_entry->GetTimestamp();
319 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29320 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10321}
322
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51323mojom::NavigationType GetNavigationType(const GURL& old_url,
324 const GURL& new_url,
325 ReloadType reload_type,
326 NavigationEntryImpl* entry,
327 const FrameNavigationEntry& frame_entry,
danakjd83d706d2020-11-25 22:11:12328 bool has_pending_cross_document_commit,
danakjb952ef12021-01-14 19:58:49329 bool is_currently_error_page,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51330 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23331 // Reload navigations
332 switch (reload_type) {
333 case ReloadType::NORMAL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51334 return mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23335 case ReloadType::BYPASSING_CACHE:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51336 return mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23337 case ReloadType::ORIGINAL_REQUEST_URL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51338 return mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23339 case ReloadType::NONE:
340 break; // Fall through to rest of function.
341 }
342
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08343 if (entry->IsRestored()) {
Lei Zhang96031532019-10-10 19:05:47344 return entry->GetHasPostData() ? mojom::NavigationType::RESTORE_WITH_POST
345 : mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23346 }
347
danakjb952ef12021-01-14 19:58:49348 const bool can_be_same_document =
349 // A pending cross-document commit means this navigation will not occur in
350 // the current document, as that document would end up being replaced in
351 // the meantime.
352 !has_pending_cross_document_commit &&
353 // If the current document is an error page, we should always treat it as
354 // a different-document navigation so that we'll attempt to load the
355 // document we're navigating to (and not stay in the current error page).
356 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12357
clamyea99ea12018-05-28 13:54:23358 // History navigations.
359 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12360 return can_be_same_document && is_same_document_history_load
Lei Zhang96031532019-10-10 19:05:47361 ? mojom::NavigationType::HISTORY_SAME_DOCUMENT
362 : mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23363 }
364 DCHECK(!is_same_document_history_load);
365
366 // A same-document fragment-navigation happens when the only part of the url
367 // that is modified is after the '#' character.
368 //
369 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12370 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23371 //
372 // Note: this check is only valid for navigations that are not history
373 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
374 // history navigation from 'A#foo' to 'A#bar' is not a same-document
375 // navigation, but a different-document one. This is why history navigation
376 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47377 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
378 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12379
380 // The one case where we do the wrong thing here and incorrectly choose
381 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
382 // the renderer is a frameset. All frameset navigations should be
383 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
384 // navigation would do the right thing, as it would send it to the browser and
385 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
386 // into this method). But since we can't tell that case here for browser-
387 // initiated navigations, we have to get the renderer involved. In that case
388 // the navigation would be restarted due to the renderer spending a reply of
389 // mojom::CommitResult::RestartCrossDocument.
390
391 return can_be_same_document && is_same_doc
392 ? mojom::NavigationType::SAME_DOCUMENT
393 : mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23394}
395
Camille Lamy5193caa2018-10-12 11:59:42396// Adjusts the original input URL if needed, to get the URL to actually load and
397// the virtual URL, which may differ.
398void RewriteUrlForNavigation(const GURL& original_url,
399 BrowserContext* browser_context,
400 GURL* url_to_load,
401 GURL* virtual_url,
402 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42403 // Allow the browser URL handler to rewrite the URL. This will, for example,
404 // remove "view-source:" from the beginning of the URL to get the URL that
405 // will actually be loaded. This real URL won't be shown to the user, just
406 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31407 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42408 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
409 url_to_load, browser_context, reverse_on_redirect);
410}
411
412#if DCHECK_IS_ON()
413// Helper sanity check function used in debug mode.
414void ValidateRequestMatchesEntry(NavigationRequest* request,
415 NavigationEntryImpl* entry) {
416 if (request->frame_tree_node()->IsMainFrame()) {
417 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
418 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
419 request->common_params().transition, entry->GetTransitionType()));
420 }
421 DCHECK_EQ(request->common_params().should_replace_current_entry,
422 entry->should_replace_entry());
Nasko Oskovc36327d2019-01-03 23:23:04423 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42424 entry->should_clear_history_list());
425 DCHECK_EQ(request->common_params().has_user_gesture,
426 entry->has_user_gesture());
427 DCHECK_EQ(request->common_params().base_url_for_data_url,
428 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04429 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42430 entry->GetCanLoadLocalResources());
431 DCHECK_EQ(request->common_params().started_from_context_menu,
432 entry->has_started_from_context_menu());
433
434 FrameNavigationEntry* frame_entry =
435 entry->GetFrameEntry(request->frame_tree_node());
436 if (!frame_entry) {
437 NOTREACHED();
438 return;
439 }
440
Camille Lamy5193caa2018-10-12 11:59:42441 DCHECK_EQ(request->common_params().method, frame_entry->method());
442
Nasko Oskovc36327d2019-01-03 23:23:04443 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42444 if (redirect_size == frame_entry->redirect_chain().size()) {
445 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04446 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42447 frame_entry->redirect_chain()[i]);
448 }
449 } else {
450 NOTREACHED();
451 }
452}
453#endif // DCHECK_IS_ON()
454
Dave Tapuska8bfd84c2019-03-26 20:47:16455// Returns whether the session history NavigationRequests in |navigations|
456// would stay within the subtree of the sandboxed iframe in
457// |sandbox_frame_tree_node_id|.
458bool DoesSandboxNavigationStayWithinSubtree(
459 int sandbox_frame_tree_node_id,
460 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
461 for (auto& item : navigations) {
462 bool within_subtree = false;
463 // Check whether this NavigationRequest affects a frame within the
464 // sandboxed frame's subtree by walking up the tree looking for the
465 // sandboxed frame.
466 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03467 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16468 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
469 within_subtree = true;
470 break;
471 }
472 }
473 if (!within_subtree)
474 return false;
475 }
476 return true;
477}
478
Titouan Rigoudy6ec70402021-02-02 15:42:19479bool ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(
Antonio Sartori78a749f2020-11-30 12:03:39480 const NavigationRequest* request) {
481 // For local schemes we need to store the policy container in the
482 // FrameNavigationEntry, so that we can reload it in case of history
483 // navigation.
484 //
485 // TODO(https://crbug.com/1146361 and https://crbug.com/1146362): blob: and
486 // filesystem: should be removed from this list when we have properly
487 // implemented storing their policy container in the respective store.
488 return (request->common_params().url.SchemeIs(url::kAboutScheme) ||
489 request->common_params().url.SchemeIs(url::kDataScheme) ||
490 request->common_params().url.SchemeIsBlob() ||
491 request->common_params().url.SchemeIsFileSystem());
492}
493
[email protected]e9ba4472008-09-14 15:42:43494} // namespace
495
arthursonzogni66f711c2019-10-08 14:40:36496// NavigationControllerImpl::PendingEntryRef------------------------------------
497
498NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
499 base::WeakPtr<NavigationControllerImpl> controller)
500 : controller_(controller) {}
501
502NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
503 if (!controller_) // Can be null with interstitials.
504 return;
505
506 controller_->PendingEntryRefDeleted(this);
507}
508
[email protected]d202a7c2012-01-04 07:53:47509// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29510
[email protected]23a918b2014-07-15 09:51:36511const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23512
[email protected]765b35502008-08-21 00:51:20513// static
[email protected]d202a7c2012-01-04 07:53:47514size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23515 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20516
[email protected]e6fec472013-05-14 05:29:02517// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20518// when testing.
519static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29520
[email protected]71fde352011-12-29 03:29:56521// static
dcheng9bfa5162016-04-09 01:00:57522std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
523 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10524 Referrer referrer,
525 base::Optional<url::Origin> initiator_origin,
526 ui::PageTransition transition,
527 bool is_renderer_initiated,
528 const std::string& extra_headers,
529 BrowserContext* browser_context,
530 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
531 return NavigationControllerImpl::CreateNavigationEntry(
532 url, referrer, std::move(initiator_origin),
533 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Rakina Zata Amni66992a82020-06-24 03:57:52534 extra_headers, browser_context, std::move(blob_url_loader_factory),
Scott Violet5ae6c42e2020-10-28 02:47:37535 false /* should_replace_entry */, nullptr /* web_contents */);
Lukasz Anforowicz641234d52019-11-07 21:07:10536}
537
538// static
539std::unique_ptr<NavigationEntryImpl>
540NavigationControllerImpl::CreateNavigationEntry(
541 const GURL& url,
542 Referrer referrer,
543 base::Optional<url::Origin> initiator_origin,
544 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57545 ui::PageTransition transition,
546 bool is_renderer_initiated,
547 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09548 BrowserContext* browser_context,
Rakina Zata Amni66992a82020-06-24 03:57:52549 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Scott Violet5ae6c42e2020-10-28 02:47:37550 bool should_replace_entry,
551 WebContents* web_contents) {
Camille Lamy5193caa2018-10-12 11:59:42552 GURL url_to_load;
553 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56554 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42555 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
556 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56557
Lukasz Anforowicz641234d52019-11-07 21:07:10558 // Let the NTP override the navigation params and pretend that this is a
559 // browser-initiated, bookmark-like navigation.
560 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violet5ae6c42e2020-10-28 02:47:37561 web_contents, source_site_instance, &transition, &is_renderer_initiated,
562 &referrer, &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10563
Patrick Monettef507e982019-06-19 20:18:06564 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28565 nullptr, // The site instance for tabs is sent on navigation
566 // (WebContents::GetSiteInstance).
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58567 url_to_load, referrer, initiator_origin, std::u16string(), transition,
Camille Lamy5193caa2018-10-12 11:59:42568 is_renderer_initiated, blob_url_loader_factory);
569 entry->SetVirtualURL(virtual_url);
570 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56571 entry->set_update_virtual_url_with_url(reverse_on_redirect);
572 entry->set_extra_headers(extra_headers);
Rakina Zata Amni66992a82020-06-24 03:57:52573 entry->set_should_replace_entry(should_replace_entry);
Patrick Monettef507e982019-06-19 20:18:06574 return entry;
[email protected]71fde352011-12-29 03:29:56575}
576
[email protected]cdcb1dee2012-01-04 00:46:20577// static
578void NavigationController::DisablePromptOnRepost() {
579 g_check_for_repost = false;
580}
581
[email protected]c5b88d82012-10-06 17:03:33582base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
583 base::Time t) {
584 // If |t| is between the water marks, we're in a run of duplicates
585 // or just getting out of it, so increase the high-water mark to get
586 // a time that probably hasn't been used before and return it.
587 if (low_water_mark_ <= t && t <= high_water_mark_) {
588 high_water_mark_ += base::TimeDelta::FromMicroseconds(1);
589 return high_water_mark_;
590 }
591
592 // Otherwise, we're clear of the last duplicate run, so reset the
593 // water marks.
594 low_water_mark_ = high_water_mark_ = t;
595 return t;
596}
597
ckitagawa0faa5e42020-06-17 17:30:54598NavigationControllerImpl::ScopedShowRepostDialogForTesting::
599 ScopedShowRepostDialogForTesting()
600 : was_disallowed_(g_check_for_repost) {
601 g_check_for_repost = true;
602}
603
604NavigationControllerImpl::ScopedShowRepostDialogForTesting::
605 ~ScopedShowRepostDialogForTesting() {
606 g_check_for_repost = was_disallowed_;
607}
608
[email protected]d202a7c2012-01-04 07:53:47609NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00610 BrowserContext* browser_context,
611 FrameTree& frame_tree,
612 NavigationControllerDelegate* delegate)
613 : frame_tree_(frame_tree),
614 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57615 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22616 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47617 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37618 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29619}
620
[email protected]d202a7c2012-01-04 07:53:47621NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00622 // The NavigationControllerImpl might be called inside its delegate
623 // destructor. Calling it is not valid anymore.
624 delegate_ = nullptr;
625 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29626}
627
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57628WebContents* NavigationControllerImpl::GetWebContents() {
[email protected]ec6c05f2013-10-23 18:41:57629 return delegate_->GetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32630}
631
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57632BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55633 return browser_context_;
634}
635
[email protected]d202a7c2012-01-04 07:53:47636void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30637 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36638 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57639 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57640 // Verify that this controller is unused and that the input is valid.
Lei Zhang96031532019-10-10 19:05:47641 DCHECK_EQ(0, GetEntryCount());
642 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57643 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14644 selected_navigation < static_cast<int>(entries->size()));
arthursonzogni5c4c202d2017-04-25 23:41:27645 DCHECK_EQ(-1, pending_entry_index_);
[email protected]ce3fa3c2009-04-20 19:55:57646
[email protected]ce3fa3c2009-04-20 19:55:57647 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44648 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03649 entries_.reserve(entries->size());
650 for (auto& entry : *entries)
dcheng36b6aec92015-12-26 06:16:36651 entries_.push_back(
652 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
avif16f85a72015-11-13 18:25:03653
654 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
655 // cleared out safely.
656 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57657
658 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36659 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57660}
661
toyoshim6142d96f2016-12-19 09:07:25662void NavigationControllerImpl::Reload(ReloadType reload_type,
663 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28664 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28665 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32666 int current_index = -1;
667
Carlos IL42b416592019-10-07 23:10:36668 if (entry_replaced_by_post_commit_error_) {
669 // If there is an entry that was replaced by a currently active post-commit
670 // error navigation, this can't be the initial navigation.
671 DCHECK(!IsInitialNavigation());
672 // If the current entry is a post commit error, we reload the entry it
673 // replaced instead. We leave the error entry in place until a commit
674 // replaces it, but the pending entry points to the original entry in the
675 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
676 // case that pending_entry_ != entries_[pending_entry_index_].
677 entry = entry_replaced_by_post_commit_error_.get();
678 current_index = GetCurrentEntryIndex();
679 } else if (IsInitialNavigation() && pending_entry_) {
680 // If we are reloading the initial navigation, just use the current
681 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32682 entry = pending_entry_;
683 // The pending entry might be in entries_ (e.g., after a Clone), so we
684 // should also update the current_index.
685 current_index = pending_entry_index_;
686 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00687 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32688 current_index = GetCurrentEntryIndex();
689 if (current_index != -1) {
creis3da03872015-02-20 21:12:32690 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32691 }
[email protected]979a4bc2013-04-24 01:27:15692 }
[email protected]241db352013-04-22 18:04:05693
[email protected]59167c22013-06-03 18:07:32694 // If we are no where, then we can't reload. TODO(darin): We should add a
695 // CanReload method.
696 if (!entry)
697 return;
698
Takashi Toyoshimac7df3c22019-06-25 14:18:47699 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26700 entry->set_reload_type(reload_type);
701
Aran Gilman37d11632019-10-08 23:07:15702 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30703 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07704 // they really want to do this. If they do, the dialog will call us back
705 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57706 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02707
[email protected]106a0812010-03-18 00:15:12708 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57709 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47710 return;
initial.commit09911bf2008-07-26 23:55:29711 }
Lei Zhang96031532019-10-10 19:05:47712
713 if (!IsInitialNavigation())
714 DiscardNonCommittedEntries();
715
716 pending_entry_ = entry;
717 pending_entry_index_ = current_index;
718 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
719
720 NavigateToExistingPendingEntry(reload_type,
721 FrameTreeNode::kFrameTreeNodeInvalidId);
initial.commit09911bf2008-07-26 23:55:29722}
723
[email protected]d202a7c2012-01-04 07:53:47724void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48725 DCHECK(pending_reload_ != ReloadType::NONE);
726 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12727}
728
[email protected]d202a7c2012-01-04 07:53:47729void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48730 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12731 NOTREACHED();
732 } else {
toyoshim6142d96f2016-12-19 09:07:25733 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48734 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12735 }
736}
737
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57738bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09739 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11740}
741
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57742bool NavigationControllerImpl::IsInitialBlankNavigation() {
creis10a4ab72015-10-13 17:22:40743 // TODO(creis): Once we create a NavigationEntry for the initial blank page,
toyoshim0df1d3a2016-09-09 09:52:48744 // we'll need to check for entry count 1 and restore_type NONE (to exclude
745 // the cloned tab case).
creis10a4ab72015-10-13 17:22:40746 return IsInitialNavigation() && GetEntryCount() == 0;
747}
748
Aran Gilman37d11632019-10-08 23:07:15749NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
750 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58751 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03752 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58753}
754
W. James MacLean1c40862c2020-04-27 21:05:57755void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
756 const url::Origin& origin) {
757 for (int i = 0; i < GetEntryCount(); i++) {
758 auto* entry = GetEntryAtIndex(i);
759 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
760 }
761 if (entry_replaced_by_post_commit_error_) {
762 // It's possible we could come back to this entry if the error
763 // page/interstitial goes away.
764 entry_replaced_by_post_commit_error_
765 ->RegisterExistingOriginToPreventOptInIsolation(origin);
766 }
767 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
768 // visiting pending_entry_, which lacks a committed origin. This will be done
769 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
770}
771
avi25764702015-06-23 15:43:37772void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57773 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00774 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37775 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27776 DCHECK_EQ(-1, pending_entry_index_);
[email protected]8ff00d72012-10-23 19:12:21777 NotificationService::current()->Notify(
Aran Gilman37d11632019-10-08 23:07:15778 NOTIFICATION_NAV_ENTRY_PENDING, Source<NavigationController>(this),
avi25764702015-06-23 15:43:37779 Details<NavigationEntry>(pending_entry_));
[email protected]765b35502008-08-21 00:51:20780}
781
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57782NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47783 if (pending_entry_)
784 return pending_entry_;
785 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20786}
787
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57788NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32789 // The pending entry is safe to return for new (non-history), browser-
790 // initiated navigations. Most renderer-initiated navigations should not
791 // show the pending entry, to prevent URL spoof attacks.
792 //
793 // We make an exception for renderer-initiated navigations in new tabs, as
794 // long as no other page has tried to access the initial empty document in
795 // the new tab. If another page modifies this blank page, a URL spoof is
796 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32797 bool safe_to_show_pending =
798 pending_entry_ &&
799 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09800 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32801 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46802 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32803
804 // Also allow showing the pending entry for history navigations in a new tab,
805 // such as Ctrl+Back. In this case, no existing page is visible and no one
806 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15807 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
808 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32809 safe_to_show_pending = true;
810
811 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19812 return pending_entry_;
813 return GetLastCommittedEntry();
814}
815
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57816int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20817 if (pending_entry_index_ != -1)
818 return pending_entry_index_;
819 return last_committed_entry_index_;
820}
821
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57822NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20823 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28824 return nullptr;
avif16f85a72015-11-13 18:25:03825 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20826}
827
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57828bool NavigationControllerImpl::CanViewSource() {
Carlos Caballeroede6f8c2021-01-28 11:01:50829 const std::string& mime_type = frame_tree_.root()
Alex Moshchuk2e470ea2021-02-03 06:46:34830 ->current_frame_host()
831 ->render_view_host()
Carlos Caballeroede6f8c2021-01-28 11:01:50832 ->contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53833 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
834 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27835 NavigationEntry* visible_entry = GetVisibleEntry();
836 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39837 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16838}
839
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57840int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27841 // The last committed entry index must always be less than the number of
Carlos IL4dea8902020-05-26 15:14:29842 // entries. If there are no entries, it must be -1.
arthursonzogni5c4c202d2017-04-25 23:41:27843 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
844 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55845 return last_committed_entry_index_;
846}
847
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57848int NavigationControllerImpl::GetEntryCount() {
Carlos IL4dea8902020-05-26 15:14:29849 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55850 return static_cast<int>(entries_.size());
851}
852
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57853NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37854 if (index < 0 || index >= GetEntryCount())
855 return nullptr;
856
avif16f85a72015-11-13 18:25:03857 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25858}
859
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57860NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47861 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20862}
863
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57864int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46865 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03866}
867
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57868bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03869 if (!base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention))
870 return CanGoToOffset(-1);
871
872 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
873 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
874 return true;
875 }
876 return false;
[email protected]765b35502008-08-21 00:51:20877}
878
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57879bool NavigationControllerImpl::CanGoForward() {
WangHui74286d52021-03-31 16:17:15880 if (!base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention))
881 return CanGoToOffset(1);
882
883 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
884 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
885 return true;
886 }
887 return false;
[email protected]765b35502008-08-21 00:51:20888}
889
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57890bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03891 int index = GetIndexForOffset(offset);
892 return index >= 0 && index < GetEntryCount();
893}
894
WangHui74286d52021-03-31 16:17:15895#if defined(OS_ANDROID)
896bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
897 if (!base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention))
898 return CanGoToOffset(offset);
899
900 if (offset == 0)
901 return true;
902 int increment = offset > 0 ? 1 : -1;
903 int non_skippable_entries = 0;
904 for (int index = GetIndexForOffset(increment);
905 index >= 0 && index < GetEntryCount(); index += increment) {
906 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
907 non_skippable_entries++;
908
909 if (non_skippable_entries == std::abs(offset))
910 return true;
911 }
912 return false;
913}
914#endif
915
[email protected]d202a7c2012-01-04 07:53:47916void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06917 int target_index = GetIndexForOffset(-1);
918
919 // Log metrics for the number of entries that are eligible for skipping on
Shivani Sharma2d5b4b6b2019-01-08 16:07:16920 // back button and move the target index past the skippable entries, if
921 // history intervention is enabled.
shivanisha55201872018-12-13 04:29:06922 int count_entries_skipped = 0;
Shivani Sharma298d12852019-01-22 20:04:03923 bool all_skippable_entries = true;
Aran Gilman37d11632019-10-08 23:07:15924 bool history_intervention_enabled =
925 base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention);
shivanisha55201872018-12-13 04:29:06926 for (int index = target_index; index >= 0; index--) {
Shivani Sharma2d5b4b6b2019-01-08 16:07:16927 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
shivanisha55201872018-12-13 04:29:06928 count_entries_skipped++;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16929 } else {
930 if (history_intervention_enabled)
931 target_index = index;
Shivani Sharma298d12852019-01-22 20:04:03932 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06933 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16934 }
shivanisha55201872018-12-13 04:29:06935 }
Miyoung Shin1c565c912021-03-17 12:11:21936
shivanisha55201872018-12-13 04:29:06937 UMA_HISTOGRAM_ENUMERATION("Navigation.BackForward.BackTargetSkipped",
Miyoung Shin1c565c912021-03-17 12:11:21938 count_entries_skipped,
939 blink::kMaxSessionHistoryEntries);
Shivani Sharma298d12852019-01-22 20:04:03940 UMA_HISTOGRAM_BOOLEAN("Navigation.BackForward.AllBackTargetsSkippable",
941 all_skippable_entries);
942
943 // Do nothing if all entries are skippable. Normally this path would not
944 // happen as consumers would have already checked it in CanGoBack but a lot of
945 // tests do not do that.
946 if (history_intervention_enabled && all_skippable_entries)
947 return;
shivanisha55201872018-12-13 04:29:06948
shivanisha55201872018-12-13 04:29:06949 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20950}
951
[email protected]d202a7c2012-01-04 07:53:47952void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06953 int target_index = GetIndexForOffset(1);
954
955 // Log metrics for the number of entries that are eligible for skipping on
Shivani Sharma2d5b4b6b2019-01-08 16:07:16956 // forward button and move the target index past the skippable entries, if
957 // history intervention is enabled.
958 // Note that at least one entry (the last one) will be non-skippable since
959 // entries are marked skippable only when they add another entry because of
960 // redirect or pushState.
shivanisha55201872018-12-13 04:29:06961 int count_entries_skipped = 0;
Aran Gilman37d11632019-10-08 23:07:15962 bool history_intervention_enabled =
963 base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention);
shivanisha55201872018-12-13 04:29:06964 for (size_t index = target_index; index < entries_.size(); index++) {
Shivani Sharma2d5b4b6b2019-01-08 16:07:16965 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
shivanisha55201872018-12-13 04:29:06966 count_entries_skipped++;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16967 } else {
968 if (history_intervention_enabled)
969 target_index = index;
shivanisha55201872018-12-13 04:29:06970 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16971 }
shivanisha55201872018-12-13 04:29:06972 }
973 UMA_HISTOGRAM_ENUMERATION("Navigation.BackForward.ForwardTargetSkipped",
Miyoung Shin1c565c912021-03-17 12:11:21974 count_entries_skipped,
975 blink::kMaxSessionHistoryEntries);
shivanisha55201872018-12-13 04:29:06976
shivanisha55201872018-12-13 04:29:06977 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20978}
979
[email protected]d202a7c2012-01-04 07:53:47980void NavigationControllerImpl::GoToIndex(int index) {
Dave Tapuska8bfd84c2019-03-26 20:47:16981 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId);
982}
983
984void NavigationControllerImpl::GoToIndex(int index,
985 int sandbox_frame_tree_node_id) {
sunjian30574a62017-03-21 21:39:44986 TRACE_EVENT0("browser,navigation,benchmark",
987 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:20988 if (index < 0 || index >= static_cast<int>(entries_.size())) {
989 NOTREACHED();
990 return;
991 }
992
[email protected]cbab76d2008-10-13 22:42:47993 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20994
arthursonzogni5c4c202d2017-04-25 23:41:27995 DCHECK_EQ(nullptr, pending_entry_);
996 DCHECK_EQ(-1, pending_entry_index_);
997 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:20998 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:27999 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
1000 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Dave Tapuska8bfd84c2019-03-26 20:47:161001 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id);
[email protected]765b35502008-08-21 00:51:201002}
1003
[email protected]d202a7c2012-01-04 07:53:471004void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:121005 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:031006 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:201007 return;
1008
[email protected]9ba14052012-06-22 23:50:031009 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:201010}
1011
WangHui74286d52021-03-31 16:17:151012#if defined(OS_ANDROID)
1013void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
1014 // Note: This is actually reached in unit tests.
1015 if (!CanGoToOffsetWithSkipping(offset))
1016 return;
1017
1018 bool history_intervention_enabled =
1019 base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention);
1020 if (offset == 0 || !history_intervention_enabled) {
1021 GoToIndex(GetIndexForOffset(offset));
1022 return;
1023 }
1024 int increment = offset > 0 ? 1 : -1;
1025 // Find the offset without counting skippable entries.
1026 int target_index = GetIndexForOffset(increment);
1027 int non_skippable_entries = 0;
1028 for (int index = target_index; index >= 0 && index < GetEntryCount();
1029 index += increment) {
1030 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1031 non_skippable_entries++;
1032
1033 if (non_skippable_entries == std::abs(offset)) {
1034 target_index = index;
1035 break;
1036 }
1037 }
1038
1039 GoToIndex(target_index);
1040}
1041#endif
1042
[email protected]41374f12013-07-24 02:49:281043bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151044 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281045 return false;
[email protected]6a13a6c2011-12-20 21:47:121046
[email protected]43032342011-03-21 14:10:311047 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281048 return true;
[email protected]cbab76d2008-10-13 22:42:471049}
1050
Michael Thiessen9b14d512019-09-23 21:19:471051void NavigationControllerImpl::PruneForwardEntries() {
1052 DiscardNonCommittedEntries();
1053 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471054 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471055 if (num_removed <= 0)
1056 return;
1057 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1058 NotifyPrunedEntries(this, remove_start_index /* start index */,
1059 num_removed /* count */);
1060}
1061
Aran Gilman37d11632019-10-08 23:07:151062void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1063 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321064 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311065 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511066 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1067 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321068 }
1069}
1070
Aran Gilman37d11632019-10-08 23:07:151071void NavigationControllerImpl::LoadURL(const GURL& url,
1072 const Referrer& referrer,
1073 ui::PageTransition transition,
1074 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471075 LoadURLParams params(url);
1076 params.referrer = referrer;
1077 params.transition_type = transition;
1078 params.extra_headers = extra_headers;
1079 LoadURLWithParams(params);
1080}
1081
1082void NavigationControllerImpl::LoadURLWithParams(const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061083 if (params.is_renderer_initiated)
1084 DCHECK(params.initiator_origin.has_value());
1085
naskob8744d22014-08-28 17:07:431086 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151087 "NavigationControllerImpl::LoadURLWithParams", "url",
1088 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291089 bool is_explicit_navigation =
1090 GetContentClient()->browser()->IsExplicitNavigation(
1091 params.transition_type);
1092 if (HandleDebugURL(params.url, params.transition_type,
1093 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431094 // If Telemetry is running, allow the URL load to proceed as if it's
1095 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491096 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431097 cc::switches::kEnableGpuBenchmarking))
1098 return;
1099 }
[email protected]8bf1048012012-02-08 01:22:181100
[email protected]cf002332012-08-14 19:17:471101 // Checks based on params.load_type.
1102 switch (params.load_type) {
1103 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431104 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471105 break;
1106 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261107 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471108 NOTREACHED() << "Data load must use data scheme.";
1109 return;
1110 }
1111 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461112 }
[email protected]e47ae9472011-10-13 19:48:341113
[email protected]e47ae9472011-10-13 19:48:341114 // The user initiated a load, we don't need to reload anymore.
1115 needs_reload_ = false;
1116
clamy21718cc22018-06-13 13:34:241117 NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441118}
1119
Mohamed Abdelhalim833de902019-09-16 17:41:451120bool NavigationControllerImpl::PendingEntryMatchesRequest(
1121 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191122 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451123 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191124}
1125
[email protected]d202a7c2012-01-04 07:53:471126bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321127 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071128 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331129 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441130 bool is_same_document_navigation,
Shivani Sharmaffb32b82019-04-09 16:58:471131 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131132 NavigationRequest* navigation_request) {
1133 DCHECK(navigation_request);
[email protected]cd2e15742013-03-08 04:08:311134 is_initial_navigation_ = false;
1135
[email protected]0e8db942008-09-24 21:21:481136 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431137 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481138 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361139 if (entry_replaced_by_post_commit_error_) {
1140 if (is_same_document_navigation) {
1141 // Same document navigations should not be possible on error pages and
1142 // would leave the controller in a weird state. Kill the renderer if
1143 // that happens.
1144 bad_message::ReceivedBadMessage(
1145 rfh->GetProcess(), bad_message::NC_SAME_DOCUMENT_POST_COMMIT_ERROR);
1146 }
1147 // Any commit while a post-commit error page is showing should put the
1148 // original entry back, replacing the error page's entry. This includes
1149 // reloads, where the original entry was used as the pending entry and
1150 // should now be at the correct index at commit time.
1151 entries_[last_committed_entry_index_] =
1152 std::move(entry_replaced_by_post_commit_error_);
1153 }
Fergal Daly8e33cf62020-12-12 01:06:071154 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551155 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431156 if (pending_entry_ &&
1157 pending_entry_->GetIsOverridingUserAgent() !=
1158 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1159 overriding_user_agent_changed = true;
[email protected]0e8db942008-09-24 21:21:481160 } else {
Gang Wu325f03f42021-02-25 20:00:461161 // GetLastCommittedEntry() is null, so this is the first entry.
Fergal Daly8e33cf62020-12-12 01:06:071162 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481163 details->previous_entry_index = -1;
Gang Wu325f03f42021-02-25 20:00:461164 if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) {
1165 // Default setting is NOT override the user agent, so overriding the user
1166 // agent in first entry should be considered as user agent changed as
1167 // well.
1168 overriding_user_agent_changed = true;
1169 }
[email protected]0e8db942008-09-24 21:21:481170 }
[email protected]ecd9d8702008-08-28 22:10:171171
Alexander Timind2f2e4f22019-04-02 20:04:531172 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1173 // implementing back-forward cache.
1174
1175 // Create a new metrics object or reuse the previous one depending on whether
1176 // it's a main frame navigation or not.
1177 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics =
1178 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1179 GetLastCommittedEntry(), !rfh->GetParent(),
1180 params.document_sequence_number);
1181 // Notify the last active entry that we have navigated away.
1182 if (!rfh->GetParent() && !is_same_document_navigation) {
1183 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1184 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Alexander Timinde527cd2019-12-02 09:41:121185 metrics->MainFrameDidNavigateAwayFromDocument(rfh, details,
1186 navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531187 }
1188 }
1189 }
1190
fdegans9caf66a2015-07-30 21:10:421191 // If there is a pending entry at this point, it should have a SiteInstance,
1192 // except for restored entries.
jam48cea9082017-02-15 06:13:291193 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481194 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081195 pending_entry_->IsRestored());
1196 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151197 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291198 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481199 }
[email protected]e9ba4472008-09-14 15:42:431200
Nasko Oskovaee2f862018-06-15 00:05:521201 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1202 // has changed, this must be treated as a new navigation with replacement.
1203 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491204 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521205 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101206 pending_entry_->GetUniqueID() ==
1207 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521208 pending_entry_->site_instance() &&
1209 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1210 DCHECK_NE(-1, pending_entry_index_);
1211 // TODO(nasko,creis): Instead of setting this value here, set
1212 // should_replace_current_entry on the parameters we send to the
1213 // renderer process as part of CommitNavigation. The renderer should
1214 // in turn send it back here as part of |params| and it can be just
1215 // enforced and renderer process terminated on mismatch.
1216 details->did_replace_entry = true;
1217 } else {
1218 // The renderer tells us whether the navigation replaces the current entry.
1219 details->did_replace_entry = params.should_replace_current_entry;
1220 }
[email protected]bcd904482012-02-01 01:54:221221
[email protected]e9ba4472008-09-14 15:42:431222 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amnif6950d552020-11-24 03:26:101223 details->type = ClassifyNavigation(rfh, params, navigation_request);
[email protected]4bf3522c2010-08-19 21:00:201224
eugenebutee08663a2017-04-27 17:43:121225 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441226 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121227
Peter Boströmd7592132019-01-30 04:50:311228 // Make sure we do not discard the pending entry for a different ongoing
1229 // navigation when a same document commit comes in unexpectedly from the
1230 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1231 // other navigation types. See https://crbug.com/900036.
1232 // TODO(crbug.com/926009): Handle history.pushState() as well.
1233 bool keep_pending_entry = is_same_document_navigation &&
Charlie Reisc0f17d2d2021-01-12 18:52:491234 details->type == NAVIGATION_TYPE_EXISTING_ENTRY &&
Peter Boströmd7592132019-01-30 04:50:311235 pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451236 !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311237
[email protected]0e8db942008-09-24 21:21:481238 switch (details->type) {
Charlie Reisc0f17d2d2021-01-12 18:52:491239 case NAVIGATION_TYPE_NEW_ENTRY:
1240 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051241 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451242 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431243 break;
Charlie Reisc0f17d2d2021-01-12 18:52:491244 case NAVIGATION_TYPE_EXISTING_ENTRY:
1245 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1246 was_restored, navigation_request,
1247 keep_pending_entry);
[email protected]e9ba4472008-09-14 15:42:431248 break;
[email protected]8ff00d72012-10-23 19:12:211249 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471250 RendererDidNavigateNewSubframe(
1251 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451252 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431253 break;
[email protected]8ff00d72012-10-23 19:12:211254 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391255 if (!RendererDidNavigateAutoSubframe(
1256 rfh, params, details->is_same_document, navigation_request)) {
creisce0ef3572017-01-26 17:53:081257 // We don't send a notification about auto-subframe PageState during
1258 // UpdateStateForFrame, since it looks like nothing has changed. Send
1259 // it here at commit time instead.
1260 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431261 return false;
creis59d5a47cb2016-08-24 23:57:191262 }
[email protected]e9ba4472008-09-14 15:42:431263 break;
[email protected]8ff00d72012-10-23 19:12:211264 case NAVIGATION_TYPE_NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:491265 // If a pending navigation was in progress, this canceled it. We should
1266 // discard it and make sure it is removed from the URL bar. After that,
1267 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:431268 // the caller that nothing has happened.
arthursonzogni69a6a1b2019-09-17 09:23:001269 if (pending_entry_)
[email protected]20d1c992011-04-12 21:17:491270 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431271 return false;
Aran Gilman37d11632019-10-08 23:07:151272 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431273 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151274 break;
[email protected]765b35502008-08-21 00:51:201275 }
1276
[email protected]688aa65c62012-09-28 04:32:221277 // At this point, we know that the navigation has just completed, so
1278 // record the time.
1279 //
1280 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261281 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331282 base::Time timestamp =
1283 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1284 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131285 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221286
Peter Boströmd7592132019-01-30 04:50:311287 // If we aren't keeping the pending entry, there shouldn't be one at this
1288 // point. Clear it again in case any error cases above forgot to do so.
1289 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1290 // been cleared instead of protecting against it.
1291 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001292 DiscardNonCommittedEntries();
[email protected]f233e4232013-02-23 00:55:141293
[email protected]e9ba4472008-09-14 15:42:431294 // All committed entries should have nonempty content state so WebKit doesn't
1295 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471296 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321297 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221298 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441299 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531300 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1301 // implementing back-forward cache.
1302 if (!active_entry->back_forward_cache_metrics()) {
1303 active_entry->set_back_forward_cache_metrics(
1304 std::move(back_forward_cache_metrics));
1305 }
1306 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
Hajime Hoshi446206e2019-10-18 18:36:251307 navigation_request,
1308 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
naskoc7533512016-05-06 17:01:121309
Charles Reisc0507202017-09-21 00:40:021310 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1311 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1312 // A mismatch can occur if the renderer lies or due to a unique name collision
1313 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121314 FrameNavigationEntry* frame_entry =
1315 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021316 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1317 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031318 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081319 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1320 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031321 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201322
1323 // Remember the bindings the renderer process has at this point, so that
1324 // we do not grant this entry additional bindings if we come back to it.
1325 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301326 }
[email protected]132e281a2012-07-31 18:32:441327
[email protected]97d8f0d2013-10-29 16:49:211328 // Once it is committed, we no longer need to track several pieces of state on
1329 // the entry.
naskoc7533512016-05-06 17:01:121330 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131331
[email protected]49bd30e62011-03-22 20:12:591332 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221333 // TODO(creis): This check won't pass for subframes until we create entries
1334 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001335 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421336 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591337
[email protected]e9ba4472008-09-14 15:42:431338 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001339 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001340 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311341 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531342
arthursonzogni7ddc6542021-04-09 09:16:501343 active_entry->SetIsOverridingUserAgent(
1344 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031345
[email protected]93f230e02011-06-01 14:40:001346 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291347
John Abd-El-Malek380d3c5922017-09-08 00:20:311348 if (active_entry->GetURL().SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451349 navigation_request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek969cdd94e2017-07-10 22:18:161350 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus",
1351 !!active_entry->GetSSL().certificate);
1352 }
1353
aelias100c9192017-01-13 00:01:431354 if (overriding_user_agent_changed)
1355 delegate_->UpdateOverridingUserAgent();
1356
creis03b48002015-11-04 00:54:561357 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1358 // one knows the latest NavigationEntry it is showing (whether it has
1359 // committed anything in this navigation or not). This allows things like
1360 // state and title updates from RenderFrames to apply to the latest relevant
1361 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381362 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001363 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381364 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
[email protected]e9ba4472008-09-14 15:42:431365 return true;
initial.commit09911bf2008-07-26 23:55:291366}
1367
[email protected]8ff00d72012-10-23 19:12:211368NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321369 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101370 const mojom::DidCommitProvisionalLoadParams& params,
1371 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591372 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511373 "ClassifyNavigation");
1374
avi7c6f35e2015-05-08 17:52:381375 if (params.did_create_new_entry) {
Charlie Reisc0f17d2d2021-01-12 18:52:491376 // A new entry. We may or may not have a corresponding pending entry, and
avi7c6f35e2015-05-08 17:52:381377 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001378 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491379 trace_return.set_return_reason("new entry, no parent, new entry");
1380 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381381 }
1382
1383 // When this is a new subframe navigation, we should have a committed page
1384 // in which it's a subframe. This may not be the case when an iframe is
1385 // navigated on a popup navigated to about:blank (the iframe would be
1386 // written into the popup by script on the main page). For these cases,
1387 // there isn't any navigation stuff we can do, so just ignore it.
Nasko Oskovae49e292020-08-13 02:08:511388 if (!GetLastCommittedEntry()) {
1389 trace_return.set_return_reason("new entry, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381390 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511391 }
avi7c6f35e2015-05-08 17:52:381392
1393 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511394 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381395 return NAVIGATION_TYPE_NEW_SUBFRAME;
1396 }
1397
Charlie Reisc0f17d2d2021-01-12 18:52:491398 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381399 DCHECK(!params.history_list_was_cleared);
1400
avi39c1edd32015-06-04 20:06:001401 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381402 // All manual subframes would be did_create_new_entry and handled above, so
1403 // we know this is auto.
Nasko Oskovae49e292020-08-13 02:08:511404 if (GetLastCommittedEntry()) {
1405 trace_return.set_return_reason("subframe, last commmited, auto subframe");
avi7c6f35e2015-05-08 17:52:381406 return NAVIGATION_TYPE_AUTO_SUBFRAME;
Nasko Oskovae49e292020-08-13 02:08:511407 }
Lei Zhang96031532019-10-10 19:05:471408
1409 // We ignore subframes created in non-committed pages; we'd appreciate if
1410 // people stopped doing that.
Nasko Oskovae49e292020-08-13 02:08:511411 trace_return.set_return_reason("subframe, no last commmited, ignore");
Lei Zhang96031532019-10-10 19:05:471412 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381413 }
1414
Rakina Zata Amnif6950d552020-11-24 03:26:101415 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1416 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381417 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1418 // create a new page.
1419
1420 // Just like above in the did_create_new_entry case, it's possible to
1421 // scribble onto an uncommitted page. Again, there isn't any navigation
1422 // stuff that we can do, so ignore it here as well.
avi3a5b8f32015-05-28 17:50:231423 NavigationEntry* last_committed = GetLastCommittedEntry();
Nasko Oskovae49e292020-08-13 02:08:511424 if (!last_committed) {
1425 trace_return.set_return_reason("nav entry 0, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381426 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511427 }
avi7c6f35e2015-05-08 17:52:381428
Charles Reis1378111f2017-11-08 21:44:061429 // This is history.replaceState() or history.reload().
Nasko Oskov332593c2018-08-16 17:21:341430 // TODO(nasko): With error page isolation, reloading an existing session
1431 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491432 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341433 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511434 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491435 "nav entry 0, last committed, existing entry");
1436 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381437 }
1438
Rakina Zata Amnif6950d552020-11-24 03:26:101439 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521440 // If the SiteInstance of the |pending_entry_| does not match the
1441 // SiteInstance that got committed, treat this as a new navigation with
1442 // replacement. This can happen if back/forward/reload encounters a server
1443 // redirect to a different site or an isolated error page gets successfully
1444 // reloaded into a different SiteInstance.
1445 if (pending_entry_->site_instance() &&
1446 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491447 trace_return.set_return_reason("pending matching nav entry, new entry");
1448 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521449 }
creis77c9aa32015-09-25 19:59:421450
Nasko Oskovaee2f862018-06-15 00:05:521451 if (pending_entry_index_ == -1) {
1452 // In this case, we have a pending entry for a load of a new URL but Blink
1453 // didn't do a new navigation (params.did_create_new_entry). First check
1454 // to make sure Blink didn't treat a new cross-process navigation as
1455 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161456 // we must treat it as NEW rather than the converted reload case below,
1457 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521458 if (!GetLastCommittedEntry() ||
1459 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161460 trace_return.set_return_reason("new pending, new entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491461 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521462 }
1463
1464 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161465 // We will create a pending entry, but NavigateWithoutEntry will convert
1466 // it to a reload since it's the same page and not create a new entry for
1467 // it. (The user doesn't want to have a new back/forward entry when they
1468 // do this.) Therefore we want to just ignore the pending entry and go
1469 // back to where we were (the "existing entry").
1470 trace_return.set_return_reason("new pending, existing (same) entry");
1471 return NAVIGATION_TYPE_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521472 }
avi7c6f35e2015-05-08 17:52:381473 }
1474
creis26d22632017-04-21 20:23:561475 // Everything below here is assumed to be an existing entry, but if there is
1476 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511477 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491478 trace_return.set_return_reason("no last committed, new entry");
1479 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511480 }
creis26d22632017-04-21 20:23:561481
avi7c6f35e2015-05-08 17:52:381482 if (params.intended_as_new_entry) {
1483 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491484 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1485 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161486 trace_return.set_return_reason("intended as new entry, existing entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491487 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381488 }
1489
1490 if (params.url_is_unreachable && failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101491 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381492 // If the renderer was going to a new pending entry that got cleared because
1493 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491494 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381495 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511496 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491497 "unreachable, matching pending, existing entry");
1498 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381499 }
1500
Charlie Reisc0f17d2d2021-01-12 18:52:491501 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101502 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511503 trace_return.traced_value()->SetInteger("existing_entry_index",
1504 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381505 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441506 // The renderer has committed a navigation to an entry that no longer
1507 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491508 trace_return.set_return_reason("existing entry -1, new entry");
1509 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381510 }
1511
avi7c6f35e2015-05-08 17:52:381512 // Since we weeded out "new" navigations above, we know this is an existing
1513 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491514 trace_return.set_return_reason("default return, existing entry");
1515 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381516}
1517
Charlie Reisc0f17d2d2021-01-12 18:52:491518void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321519 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071520 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361521 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441522 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471523 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451524 NavigationRequest* request) {
dcheng9bfa5162016-04-09 01:00:571525 std::unique_ptr<NavigationEntryImpl> new_entry;
creisf49dfc92016-07-26 17:05:181526 bool update_virtual_url = false;
1527
Lukasz Anforowicz435bcb582019-07-12 20:50:061528 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451529 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061530
creisf49dfc92016-07-26 17:05:181531 // First check if this is an in-page navigation. If so, clone the current
1532 // entry instead of looking at the pending entry, because the pending entry
1533 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361534 if (is_same_document && GetLastCommittedEntry()) {
Patrick Monette50e8bd82019-06-13 22:40:451535 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481536 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
creisf49dfc92016-07-26 17:05:181537 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Nasko Oskov03912102019-01-11 00:21:321538 params.url, (params.url_is_unreachable) ? nullptr : &params.origin,
Rakina Zata Amni82fafba2021-03-11 07:07:091539 Referrer(*params.referrer), initiator_origin,
1540 request->GetRedirectChain(), params.page_state, params.method,
1541 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391542 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481543 request->GetSubresourceWebBundleNavigationInfo(),
Antonio Sartori78a749f2020-11-30 12:03:391544 // We will set the document policies later in this function.
Titouan Rigoudy6ec70402021-02-02 15:42:191545 nullptr /* policy_container_policies */);
Charles Reisf44482022017-10-13 21:15:031546
creisf49dfc92016-07-26 17:05:181547 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Carlos Caballero40b0efd2021-01-26 11:55:001548 frame_entry, true, rfh->frame_tree_node(), frame_tree_.root());
jama78746e2017-02-22 17:21:571549 if (new_entry->GetURL().GetOrigin() != params.url.GetOrigin()) {
1550 // TODO(jam): we had one report of this with a URL that was redirecting to
1551 // only tildes. Until we understand that better, don't copy the cert in
1552 // this case.
1553 new_entry->GetSSL() = SSLStatus();
John Abd-El-Malek9cd697e2017-07-12 21:53:141554
John Abd-El-Malek380d3c5922017-09-08 00:20:311555 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451556 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141557 UMA_HISTOGRAM_BOOLEAN(
1558 "Navigation.SecureSchemeHasSSLStatus.NewPageInPageOriginMismatch",
1559 !!new_entry->GetSSL().certificate);
1560 }
jama78746e2017-02-22 17:21:571561 }
creisf49dfc92016-07-26 17:05:181562
Patrick Monette50e8bd82019-06-13 22:40:451563 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1564 // that |frame_entry| should now have a reference count of exactly 2: one
1565 // due to the local variable |frame_entry|, and another due to |new_entry|
1566 // also retaining one. This should never fail, because it's the main frame.
1567 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181568
1569 update_virtual_url = new_entry->update_virtual_url_with_url();
John Abd-El-Malek9cd697e2017-07-12 21:53:141570
John Abd-El-Malek380d3c5922017-09-08 00:20:311571 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451572 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141573 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus.NewPageInPage",
1574 !!new_entry->GetSSL().certificate);
1575 }
creisf49dfc92016-07-26 17:05:181576 }
1577
Harkiran Bolaria59290d62021-03-17 01:53:011578 // If this is an activation navigation from a prerendered page, transfer the
1579 // new entry from an entry already created and stored in the
1580 // NavigationRequest. |new_entry| will not have been set prior to this as
1581 // |is_same_document| is mutually exclusive with
1582 // |IsPrerenderedPageActivation|.
1583 if (request->IsPrerenderedPageActivation()) {
1584 DCHECK(!is_same_document);
1585 DCHECK(!new_entry);
1586 new_entry = request->TakePrerenderNavigationEntry();
1587 DCHECK(new_entry);
1588 }
1589
Charlie Reisc0f17d2d2021-01-12 18:52:491590 // Only make a copy of the pending entry if it is appropriate for the new
1591 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451592 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041593 // 1. The SiteInstance hasn't been assigned to something else.
1594 // 2. The pending entry was intended as a new entry, rather than being a
1595 // history navigation that was interrupted by an unrelated,
1596 // renderer-initiated navigation.
1597 // TODO(csharrison): Investigate whether we can remove some of the coarser
1598 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451599 if (!new_entry && PendingEntryMatchesRequest(request) &&
1600 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341601 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431602 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351603 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431604
[email protected]f1eb87a2011-05-06 17:49:411605 update_virtual_url = new_entry->update_virtual_url_with_url();
Camille Lamy62b826012019-02-26 09:15:471606 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451607 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141608
John Abd-El-Malek380d3c5922017-09-08 00:20:311609 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451610 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141611 UMA_HISTOGRAM_BOOLEAN(
1612 "Navigation.SecureSchemeHasSSLStatus.NewPagePendingEntryMatches",
1613 !!new_entry->GetSSL().certificate);
1614 }
creisf49dfc92016-07-26 17:05:181615 }
1616
Charlie Reisc0f17d2d2021-01-12 18:52:491617 // For cross-document commits with no matching pending entry, create a new
1618 // entry.
creisf49dfc92016-07-26 17:05:181619 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061620 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071621 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1622 initiator_origin,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:581623 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451624 params.transition, request->IsRendererInitiated(),
Lukasz Anforowicz435bcb582019-07-12 20:50:061625 nullptr); // blob_url_loader_factory
[email protected]f8f93eb2012-09-25 03:06:241626
1627 // Find out whether the new entry needs to update its virtual URL on URL
1628 // change and set up the entry accordingly. This is needed to correctly
1629 // update the virtual URL when replaceState is called after a pushState.
1630 GURL url = params.url;
1631 bool needs_update = false;
[email protected]47752982014-07-29 08:01:431632 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1633 &url, browser_context_, &needs_update);
[email protected]f8f93eb2012-09-25 03:06:241634 new_entry->set_update_virtual_url_with_url(needs_update);
1635
Charlie Reisc0f17d2d2021-01-12 18:52:491636 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411637 // update the virtual URL based on the new URL. For example, this is needed
1638 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1639 // the URL.
[email protected]f8f93eb2012-09-25 03:06:241640 update_virtual_url = needs_update;
Camille Lamy62b826012019-02-26 09:15:471641 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451642 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141643
John Abd-El-Malek380d3c5922017-09-08 00:20:311644 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451645 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141646 UMA_HISTOGRAM_BOOLEAN(
1647 "Navigation.SecureSchemeHasSSLStatus.NewPageNoMatchingEntry",
1648 !!new_entry->GetSSL().certificate);
1649 }
[email protected]e9ba4472008-09-14 15:42:431650 }
1651
Harkiran Bolaria59290d62021-03-17 01:53:011652 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
1653 // for prerendered contents, if any. This is because prerendering/activation
1654 // technically won't be creating a new document. Unlike BFCache, prerendering
1655 // creates a new NavigationEntry rather than using an existing one.
1656 if (!request->IsPrerenderedPageActivation()) {
1657 // Don't use the page type from the pending entry. Some interstitial page
1658 // may have set the type to interstitial. Once we commit, however, the page
1659 // type must always be normal or error.
1660 new_entry->set_page_type(params.url_is_unreachable ? PAGE_TYPE_ERROR
1661 : PAGE_TYPE_NORMAL);
1662 new_entry->SetURL(params.url);
1663 if (update_virtual_url)
1664 UpdateVirtualURLToURL(new_entry.get(), params.url);
1665 new_entry->SetReferrer(Referrer(*params.referrer));
1666 new_entry->SetTransitionType(params.transition);
1667 new_entry->set_site_instance(
1668 static_cast<SiteInstanceImpl*>(rfh->GetSiteInstance()));
1669 new_entry->SetOriginalRequestURL(request->GetOriginalRequestURL());
Rakina Zata Amnib597d632020-12-01 00:56:001670
Harkiran Bolaria59290d62021-03-17 01:53:011671 if (!is_same_document) {
arthursonzogni7ddc6542021-04-09 09:16:501672 DCHECK_EQ(request->is_overriding_user_agent() && !rfh->GetParent(),
Harkiran Bolaria59290d62021-03-17 01:53:011673 params.is_overriding_user_agent);
1674 } else {
1675 DCHECK_EQ(rfh->is_overriding_user_agent(),
1676 params.is_overriding_user_agent);
1677 }
1678 new_entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
[email protected]e9ba4472008-09-14 15:42:431679
Harkiran Bolaria59290d62021-03-17 01:53:011680 // Update the FrameNavigationEntry for new main frame commits.
1681 FrameNavigationEntry* frame_entry =
1682 new_entry->GetFrameEntry(rfh->frame_tree_node());
1683 frame_entry->set_frame_unique_name(rfh->frame_tree_node()->unique_name());
1684 frame_entry->set_item_sequence_number(params.item_sequence_number);
1685 frame_entry->set_document_sequence_number(params.document_sequence_number);
1686 frame_entry->set_redirect_chain(request->GetRedirectChain());
1687 frame_entry->SetPageState(params.page_state);
1688 frame_entry->set_method(params.method);
1689 frame_entry->set_post_id(params.post_id);
1690 frame_entry->set_policy_container_policies(
1691 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1692 request));
Antonio Sartori78a749f2020-11-30 12:03:391693
Harkiran Bolaria59290d62021-03-17 01:53:011694 if (!params.url_is_unreachable)
1695 frame_entry->set_committed_origin(params.origin);
1696 if (request->web_bundle_navigation_info()) {
1697 frame_entry->set_web_bundle_navigation_info(
1698 request->web_bundle_navigation_info()->Clone());
1699 }
creis8b5cd4c2015-06-19 00:11:081700
Harkiran Bolaria59290d62021-03-17 01:53:011701 // history.pushState() is classified as a navigation to a new page, but sets
1702 // is_same_document to true. In this case, we already have the title and
1703 // favicon available, so set them immediately.
1704 if (is_same_document && GetLastCommittedEntry()) {
1705 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
1706 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1707 }
[email protected]3a868f212014-08-09 10:41:191708 }
[email protected]ff64b3e2014-05-31 04:07:331709
[email protected]60d6cca2013-04-30 08:47:131710 DCHECK(!params.history_list_was_cleared || !replace_entry);
1711 // The browser requested to clear the session history when it initiated the
1712 // navigation. Now we know that the renderer has updated its state accordingly
1713 // and it is safe to also clear the browser side history.
1714 if (params.history_list_was_cleared) {
arthursonzogni69a6a1b2019-09-17 09:23:001715 DiscardNonCommittedEntries();
[email protected]60d6cca2013-04-30 08:47:131716 entries_.clear();
1717 last_committed_entry_index_ = -1;
1718 }
1719
Nasko Oskovaee2f862018-06-15 00:05:521720 // If this is a new navigation with replacement and there is a
1721 // pending_entry_ which matches the navigation reported by the renderer
1722 // process, then it should be the one replaced, so update the
1723 // last_committed_entry_index_ to use it.
1724 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101725 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521726 last_committed_entry_index_ = pending_entry_index_;
1727 }
1728
Alexander Timine3ec4192020-04-20 16:39:401729 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411730 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401731 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051732
Carlos IL42b416592019-10-07 23:10:361733 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
1734 !request->post_commit_error_page_html().empty());
[email protected]e9ba4472008-09-14 15:42:431735}
1736
Charlie Reisc0f17d2d2021-01-12 18:52:491737void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321738 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071739 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361740 bool is_same_document,
jam48cea9082017-02-15 06:13:291741 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451742 NavigationRequest* request,
Peter Boströmd7592132019-01-30 04:50:311743 bool keep_pending_entry) {
creis26d22632017-04-21 20:23:561744 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1745 << "that a last committed entry exists.";
1746
[email protected]e9ba4472008-09-14 15:42:431747 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001748 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431749
Charlie Reis7e2cb6d2021-01-26 01:27:161750 NavigationEntryImpl* entry = nullptr;
avicbdc4c12015-07-01 16:07:111751 if (params.intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:161752 // We're guaranteed to have a last committed entry if intended_as_new_entry
1753 // is true.
avicbdc4c12015-07-01 16:07:111754 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:161755 DCHECK(entry);
1756
1757 // If the NavigationRequest matches a new pending entry and is classified as
1758 // EXISTING_ENTRY, then it is a navigation to the same URL that was
1759 // converted to a reload, such as a user pressing enter in the omnibox.
1760 if (pending_entry_ && pending_entry_index_ == -1 &&
1761 pending_entry_->GetUniqueID() ==
1762 request->commit_params().nav_entry_id) {
1763 // Note: The pending entry will usually have a real ReloadType here, but
1764 // it can still be ReloadType::NONE in cases that
1765 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
1766
1767 // If we classified this correctly, the SiteInstance should not have
1768 // changed.
1769 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
1770
1771 // For converted reloads, we assign the entry's unique ID to be that of
1772 // the new one. Since this is always the result of a user action, we want
1773 // to dismiss infobars, etc. like a regular user-initiated navigation.
1774 entry->set_unique_id(pending_entry_->GetUniqueID());
1775
1776 // The extra headers may have changed due to reloading with different
1777 // headers.
1778 entry->set_extra_headers(pending_entry_->extra_headers());
1779 }
1780 // Otherwise, this was intended as a new entry but the pending entry was
1781 // lost in the meantime and no new entry was created. We are stuck at the
1782 // last committed entry.
1783
1784 // Even if this is a converted reload from pressing enter in the omnibox,
1785 // the server could redirect, requiring an update to the SSL status. If this
1786 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451787 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:161788 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:471789 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451790 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:161791 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141792
Charlie Reis7e2cb6d2021-01-26 01:27:161793 if (params.url.SchemeIs(url::kHttpsScheme) &&
Mohamed Abdelhalim833de902019-09-16 17:41:451794 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141795 bool has_cert = !!entry->GetSSL().certificate;
1796 if (is_same_document) {
1797 UMA_HISTOGRAM_BOOLEAN(
1798 "Navigation.SecureSchemeHasSSLStatus."
1799 "ExistingPageSameDocumentIntendedAsNew",
1800 has_cert);
1801 } else {
1802 UMA_HISTOGRAM_BOOLEAN(
1803 "Navigation.SecureSchemeHasSSLStatus."
1804 "ExistingPageDifferentDocumentIntendedAsNew",
1805 has_cert);
1806 }
1807 }
Rakina Zata Amnif6950d552020-11-24 03:26:101808 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:111809 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:101810 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b902016-09-01 16:58:161811
eugenebut604866f2017-05-10 21:35:361812 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451813 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361814 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1815 // this was a restored same document navigation entry, then it won't have
1816 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1817 // navigated it.
jam48cea9082017-02-15 06:13:291818 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
1819 if (entry->GetURL().GetOrigin() == last_entry->GetURL().GetOrigin() &&
1820 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1821 was_restored) {
1822 entry->GetSSL() = last_entry->GetSSL();
1823 }
1824 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451825 // In rapid back/forward navigations |request| sometimes won't have a cert
1826 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191827 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451828 if (request->GetSSLInfo().has_value() &&
1829 request->GetSSLInfo()->is_valid()) {
1830 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
1831 } else if (entry->GetURL().GetOrigin() != request->GetURL().GetOrigin()) {
John Abd-El-Malek3f247082017-12-07 19:02:191832 entry->GetSSL() = SSLStatus();
1833 }
jam48cea9082017-02-15 06:13:291834 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141835
John Abd-El-Malek380d3c5922017-09-08 00:20:311836 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451837 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141838 bool has_cert = !!entry->GetSSL().certificate;
1839 if (is_same_document && was_restored) {
1840 UMA_HISTOGRAM_BOOLEAN(
1841 "Navigation.SecureSchemeHasSSLStatus."
1842 "ExistingPageSameDocumentRestoredBrowserInitiated",
1843 has_cert);
1844 } else if (is_same_document && !was_restored) {
1845 UMA_HISTOGRAM_BOOLEAN(
1846 "Navigation.SecureSchemeHasSSLStatus."
1847 "ExistingPageSameDocumentBrowserInitiated",
1848 has_cert);
1849 } else if (!is_same_document && was_restored) {
1850 UMA_HISTOGRAM_BOOLEAN(
1851 "Navigation.SecureSchemeHasSSLStatus."
1852 "ExistingPageRestoredBrowserInitiated",
1853 has_cert);
1854 } else {
1855 UMA_HISTOGRAM_BOOLEAN(
1856 "Navigation.SecureSchemeHasSSLStatus.ExistingPageBrowserInitiated",
1857 has_cert);
1858 }
1859 }
avicbdc4c12015-07-01 16:07:111860 } else {
1861 // This is renderer-initiated. The only kinds of renderer-initated
Charlie Reisc0f17d2d2021-01-12 18:52:491862 // navigations that are EXISTING_ENTRY are reloads and history.replaceState,
avicbdc4c12015-07-01 16:07:111863 // which land us at the last committed entry.
1864 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101865
Mikel Astizba9cf2fd2017-12-17 10:38:101866 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1867 // to avoid misleading interpretations (e.g. URLs paired with
1868 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1869 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1870 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1871
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571872 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:101873
eugenebut604866f2017-05-10 21:35:361874 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451875 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361876 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471877 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451878 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141879
John Abd-El-Malek380d3c5922017-09-08 00:20:311880 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451881 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141882 bool has_cert = !!entry->GetSSL().certificate;
1883 if (is_same_document) {
1884 UMA_HISTOGRAM_BOOLEAN(
1885 "Navigation.SecureSchemeHasSSLStatus."
1886 "ExistingPageSameDocumentRendererInitiated",
1887 has_cert);
1888 } else {
1889 UMA_HISTOGRAM_BOOLEAN(
1890 "Navigation.SecureSchemeHasSSLStatus."
1891 "ExistingPageDifferentDocumentRendererInitiated",
1892 has_cert);
1893 }
1894 }
avicbdc4c12015-07-01 16:07:111895 }
1896 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:431897
[email protected]5ccd4dc2012-10-24 02:28:141898 // The URL may have changed due to redirects.
wjmaclean7431bb22015-02-19 14:53:431899 entry->set_page_type(params.url_is_unreachable ? PAGE_TYPE_ERROR
1900 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041901 entry->SetURL(params.url);
arthursonzogni73fe3212020-11-17 13:24:071902 entry->SetReferrer(Referrer(*params.referrer));
[email protected]38178a42009-12-17 18:58:321903 if (entry->update_virtual_url_with_url())
1904 UpdateVirtualURLToURL(entry, params.url);
[email protected]5ccd4dc2012-10-24 02:28:141905
jam015ba062017-01-06 21:17:001906 // The site instance will normally be the same except
1907 // 1) session restore, when no site instance will be assigned or
1908 // 2) redirect, when the site instance is reset.
Lei Zhang96031532019-10-10 19:05:471909 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
naskoaf182192016-08-11 02:12:011910 entry->site_instance() == rfh->GetSiteInstance());
clamy432acb22016-04-15 19:41:431911
naskoaf182192016-08-11 02:12:011912 // Update the existing FrameNavigationEntry to ensure all of its members
1913 // reflect the parameters coming from the renderer process.
Lukasz Anforowicz435bcb582019-07-12 20:50:061914 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451915 request->common_params().initiator_origin;
naskoaf182192016-08-11 02:12:011916 entry->AddOrUpdateFrameEntry(
1917 rfh->frame_tree_node(), params.item_sequence_number,
1918 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
arthursonzogni73fe3212020-11-17 13:24:071919 params.url, GetCommittedOriginForFrameEntry(params),
Rakina Zata Amni82fafba2021-03-11 07:07:091920 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071921 params.page_state, params.method, params.post_id,
1922 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031923 request->web_bundle_navigation_info()
1924 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391925 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481926 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191927 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1928 request));
creis22a7b4c2016-04-28 07:20:301929
[email protected]5ccd4dc2012-10-24 02:28:141930 // The redirected to page should not inherit the favicon from the previous
1931 // page.
eugenebut604866f2017-05-10 21:35:361932 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:481933 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:141934
Peter Boströmd7592132019-01-30 04:50:311935 // We should also usually discard the pending entry if it corresponds to a
1936 // different navigation, since that one is now likely canceled. In rare
1937 // cases, we leave the pending entry for another navigation in place when we
1938 // know it is still ongoing, to avoid a flicker in the omnibox (see
1939 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:431940 //
1941 // Note that we need to use the "internal" version since we don't want to
1942 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:311943 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001944 DiscardNonCommittedEntries();
[email protected]40bcc302009-03-02 20:50:391945
Carlos IL4dea8902020-05-26 15:14:291946 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:111947 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:431948}
1949
[email protected]d202a7c2012-01-04 07:53:471950void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:321951 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071952 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361953 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:471954 bool replace_entry,
1955 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451956 NavigationRequest* request) {
avi25f5f9e2015-07-17 20:08:261957 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1958 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
[email protected]09b8f82f2009-06-16 20:22:111959
[email protected]e9ba4472008-09-14 15:42:431960 // Manual subframe navigations just get the current entry cloned so the user
1961 // can go back or forward to it. The actual subframe information will be
1962 // stored in the page state for each of those entries. This happens out of
1963 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:091964 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1965 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:381966
Mikel Astizba9cf2fd2017-12-17 10:38:101967 // The DCHECK below documents the fact that we don't know of any situation
1968 // where |replace_entry| is true for subframe navigations. This simplifies
1969 // reasoning about the replacement struct for subframes (see
1970 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
1971 DCHECK(!replace_entry);
1972
Patrick Monette50e8bd82019-06-13 22:40:451973 // This FrameNavigationEntry might not end up being used in the
1974 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Lukasz Anforowicz435bcb582019-07-12 20:50:061975 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451976 request->common_params().initiator_origin;
Patrick Monette50e8bd82019-06-13 22:40:451977 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481978 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
creisce0ef3572017-01-26 17:53:081979 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Nasko Oskov03912102019-01-11 00:21:321980 params.url, (params.url_is_unreachable) ? nullptr : &params.origin,
Rakina Zata Amni82fafba2021-03-11 07:07:091981 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071982 params.page_state, params.method, params.post_id,
1983 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031984 request->web_bundle_navigation_info()
1985 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391986 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481987 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191988 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1989 request));
Charles Reisf44482022017-10-13 21:15:031990
creisce0ef3572017-01-26 17:53:081991 std::unique_ptr<NavigationEntryImpl> new_entry =
1992 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:451993 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:001994 frame_tree_.root());
creise062d542015-08-25 02:01:551995
Alexander Timine3ec4192020-04-20 16:39:401996 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411997 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401998 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:471999
creisce0ef3572017-01-26 17:53:082000 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:452001 // to replace, which can happen due to a unique name change. See
2002 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
2003 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:382004
Carlos IL42b416592019-10-07 23:10:362005 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false);
[email protected]e9ba4472008-09-14 15:42:432006}
2007
[email protected]d202a7c2012-01-04 07:53:472008bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:322009 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072010 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392011 bool is_same_document,
Mohamed Abdelhalim833de902019-09-16 17:41:452012 NavigationRequest* request) {
avi9f07a0c2015-02-18 22:51:292013 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2014 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2015
[email protected]e9ba4472008-09-14 15:42:432016 // We're guaranteed to have a previously committed entry, and we now need to
2017 // handle navigation inside of a subframe in it without creating a new entry.
2018 DCHECK(GetLastCommittedEntry());
2019
creis913c63ce2016-07-16 19:52:522020 // For newly created subframes, we don't need to send a commit notification.
2021 // This is only necessary for history navigations in subframes.
2022 bool send_commit_notification = false;
2023
Rakina Zata Amnif6950d552020-11-24 03:26:102024 // If |nav_entry_id| is non-zero and matches an existing entry, this
2025 // is a history navigation. Update the last committed index accordingly. If
2026 // we don't recognize the |nav_entry_id|, it might be a recently
2027 // pruned entry. We'll handle it below.
2028 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2029 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472030 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062031 // Make sure that a subframe commit isn't changing the main frame's
2032 // origin. Otherwise the renderer process may be confused, leading to a
2033 // URL spoof. We can't check the path since that may change
2034 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572035 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2036 // about:blank, file, and unique origins are more subtle to get right.
Eugene But9fb98d12018-05-22 18:28:332037 // We'll abstract out the relevant checks from IsURLSameDocumentNavigation
2038 // and share them here. See https://crbug.com/618104.
creis37988b92016-06-10 18:03:572039 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2040 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2041 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2042 dest_top_url.SchemeIsHTTPOrHTTPS() &&
2043 current_top_url.GetOrigin() != dest_top_url.GetOrigin()) {
creisfb6eeb62016-05-10 19:01:512044 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2045 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062046 }
creis3cdc3b02015-05-29 23:00:472047
creis913c63ce2016-07-16 19:52:522048 // We only need to discard the pending entry in this history navigation
2049 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062050 last_committed_entry_index_ = entry_index;
arthursonzogni69a6a1b2019-09-17 09:23:002051 DiscardNonCommittedEntries();
creis913c63ce2016-07-16 19:52:522052
2053 // History navigations should send a commit notification.
2054 send_commit_notification = true;
avi98405c22015-05-21 20:47:062055 }
[email protected]e9ba4472008-09-14 15:42:432056 }
[email protected]f233e4232013-02-23 00:55:142057
creisce0ef3572017-01-26 17:53:082058 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2059 // it may be a "history auto" case where we update an existing one.
2060 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Lukasz Anforowicz435bcb582019-07-12 20:50:062061 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452062 request->common_params().initiator_origin;
creisce0ef3572017-01-26 17:53:082063 last_committed->AddOrUpdateFrameEntry(
2064 rfh->frame_tree_node(), params.item_sequence_number,
2065 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
arthursonzogni73fe3212020-11-17 13:24:072066 params.url, GetCommittedOriginForFrameEntry(params),
Rakina Zata Amni82fafba2021-03-11 07:07:092067 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072068 params.page_state, params.method, params.post_id,
2069 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032070 request->web_bundle_navigation_info()
2071 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392072 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482073 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:192074 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
2075 request));
creis625a0c7d2015-03-24 23:17:122076
creis913c63ce2016-07-16 19:52:522077 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432078}
2079
[email protected]d202a7c2012-01-04 07:53:472080int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232081 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032082 for (size_t i = 0; i < entries_.size(); ++i) {
2083 if (entries_[i].get() == entry)
2084 return i;
2085 }
2086 return -1;
[email protected]765b35502008-08-21 00:51:202087}
2088
Eugene But7cc259d2017-10-09 23:52:492089// There are two general cases where a navigation is "same-document":
avidb7d1d22015-06-08 21:21:502090// 1. A fragment navigation, in which the url is kept the same except for the
2091// reference fragment.
2092// 2. A history API navigation (pushState and replaceState). This case is
Eugene But7cc259d2017-10-09 23:52:492093// always same-document, but the urls are not guaranteed to match excluding
2094// the fragment. The relevant spec allows pushState/replaceState to any URL
2095// on the same origin.
avidb7d1d22015-06-08 21:21:502096// However, due to reloads, even identical urls are *not* guaranteed to be
Eugene But7cc259d2017-10-09 23:52:492097// same-document navigations, we have to trust the renderer almost entirely.
avidb7d1d22015-06-08 21:21:502098// The one thing we do know is that cross-origin navigations will *never* be
Eugene But7cc259d2017-10-09 23:52:492099// same-document. Therefore, trust the renderer if the URLs are on the same
2100// origin, and assume the renderer is malicious if a cross-origin navigation
2101// claims to be same-document.
creisf164daa2016-06-07 00:17:052102//
2103// TODO(creis): Clean up and simplify the about:blank and origin checks below,
2104// which are likely redundant with each other. Be careful about data URLs vs
2105// about:blank, both of which are unique origins and thus not considered equal.
Eugene But7cc259d2017-10-09 23:52:492106bool NavigationControllerImpl::IsURLSameDocumentNavigation(
[email protected]b9d4dfdc2013-08-08 00:25:122107 const GURL& url,
creisf164daa2016-06-07 00:17:052108 const url::Origin& origin,
Eugene But7cc259d2017-10-09 23:52:492109 bool renderer_says_same_document,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572110 RenderFrameHost* rfh) {
creis225a7432016-06-03 22:56:272111 RenderFrameHostImpl* rfhi = static_cast<RenderFrameHostImpl*>(rfh);
avidb7d1d22015-06-08 21:21:502112 GURL last_committed_url;
Charlie Reis73e356242021-04-02 17:10:312113 // For cases that can't compare against the main frame's URL in the last
2114 // committed entry, use the FrameTreeNode's current_url(). Note that it is
2115 // possible to get same-document commits in the initial empty document when
2116 // there is no last committed entry (e.g., about:blank#foo).
2117 // TODO(creis): It would be simpler to always get the URL from the FTN rather
2118 // than ever looking at GetLastCommittedEntry here. We're limiting the change
2119 // in behavior for a merge and will clean it up further afterward.
2120 if (rfh->GetParent() || !GetLastCommittedEntry()) {
creis225a7432016-06-03 22:56:272121 // Use the FrameTreeNode's current_url and not rfh->GetLastCommittedURL(),
2122 // which might be empty in a new RenderFrameHost after a process swap.
2123 // Here, we care about the last committed URL in the FrameTreeNode,
2124 // regardless of which process it is in.
2125 last_committed_url = rfhi->frame_tree_node()->current_url();
avidb7d1d22015-06-08 21:21:502126 } else {
Charlie Reis73e356242021-04-02 17:10:312127 last_committed_url = GetLastCommittedEntry()->GetURL();
avidb7d1d22015-06-08 21:21:502128 }
2129
Gyuyoung Kim1ac4ca782020-09-11 03:32:512130 auto prefs = rfhi->GetOrCreateWebPreferences();
creis225a7432016-06-03 22:56:272131 const url::Origin& committed_origin =
2132 rfhi->frame_tree_node()->current_origin();
avidb7d1d22015-06-08 21:21:502133 bool is_same_origin = last_committed_url.is_empty() ||
2134 // TODO(japhet): We should only permit navigations
2135 // originating from about:blank to be in-page if the
2136 // about:blank is the first document that frame loaded.
2137 // We don't have sufficient information to identify
2138 // that case at the moment, so always allow about:blank
2139 // for now.
csharrisona3bd0b32016-10-19 18:40:482140 last_committed_url == url::kAboutBlankURL ||
avidb7d1d22015-06-08 21:21:502141 last_committed_url.GetOrigin() == url.GetOrigin() ||
creisf164daa2016-06-07 00:17:052142 committed_origin == origin ||
avidb7d1d22015-06-08 21:21:502143 !prefs.web_security_enabled ||
2144 (prefs.allow_universal_access_from_file_urls &&
creisdc004e02015-11-20 21:28:472145 committed_origin.scheme() == url::kFileScheme);
Eugene But7cc259d2017-10-09 23:52:492146 if (!is_same_origin && renderer_says_same_document) {
avidb7d1d22015-06-08 21:21:502147 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2148 bad_message::NC_IN_PAGE_NAVIGATION);
2149 }
Eugene But7cc259d2017-10-09 23:52:492150 return is_same_origin && renderer_says_same_document;
[email protected]e9ba4472008-09-14 15:42:432151}
2152
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572153void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242154 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572155 NavigationControllerImpl* source =
2156 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572157 // Verify that we look new.
Lei Zhang96031532019-10-10 19:05:472158 DCHECK_EQ(0, GetEntryCount());
2159 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:572160
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572161 if (source->GetEntryCount() == 0)
[email protected]ce3fa3c2009-04-20 19:55:572162 return; // Nothing new to do.
2163
Francois Dorayeaace782017-06-21 16:37:242164 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442165 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572166 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572167
Fergal Dalya1d569972021-03-16 03:24:532168 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302169 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532170 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2171 session_storage_namespace_map_[it.first] = source_namespace->Clone();
Aaron Colwellb731a0ae2021-03-19 19:14:472172 OnStoragePartitionIdAdded(it.first);
[email protected]fdac6ade2013-07-20 01:06:302173 }
[email protected]4e6419c2010-01-15 04:50:342174
Lukasz Anforowicz0de0f452020-12-02 19:57:152175 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572176}
2177
Aran Gilman37d11632019-10-08 23:07:152178void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2179 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162180 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012181 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162182
[email protected]d202a7c2012-01-04 07:53:472183 NavigationControllerImpl* source =
2184 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252185
avi2b177592014-12-10 02:08:022186 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012187 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252188
[email protected]474f8512013-05-31 22:31:162189 // We now have one entry, possibly with a new pending entry. Ensure that
2190 // adding the entries from source won't put us over the limit.
2191 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572192 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572193 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252194
Carlos IL4dea8902020-05-26 15:14:292195 // Insert the entries from source. Ignore any pending entry, since it has not
2196 // committed in source.
[email protected]474f8512013-05-31 22:31:162197 int max_source_index = source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:252198 if (max_source_index == -1)
[email protected]a26023822011-12-29 00:23:552199 max_source_index = source->GetEntryCount();
[email protected]e1cd5452010-08-26 18:03:252200 else
2201 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572202
2203 // Ignore the source's current entry if merging with replacement.
2204 // TODO(davidben): This should preserve entries forward of the current
2205 // too. http://crbug.com/317872
2206 if (replace_entry && max_source_index > 0)
2207 max_source_index--;
2208
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572209 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252210
2211 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552212 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142213
Carlos Caballeroede6f8c2021-01-28 11:01:502214 SetHistoryOffsetAndLength(last_committed_entry_index_, GetEntryCount());
[email protected]e1cd5452010-08-26 18:03:252215}
2216
[email protected]79368982013-11-13 01:11:012217bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162218 // If there is no last committed entry, we cannot prune. Even if there is a
2219 // pending entry, it may not commit, leaving this WebContents blank, despite
2220 // possibly giving it new entries via CopyStateFromAndPrune.
2221 if (last_committed_entry_index_ == -1)
2222 return false;
[email protected]9350602e2013-02-26 23:27:442223
[email protected]474f8512013-05-31 22:31:162224 // We cannot prune if there is a pending entry at an existing entry index.
2225 // It may not commit, so we have to keep the last committed entry, and thus
2226 // there is no sensible place to keep the pending entry. It is ok to have
2227 // a new pending entry, which can optionally commit as a new navigation.
2228 if (pending_entry_index_ != -1)
2229 return false;
2230
[email protected]474f8512013-05-31 22:31:162231 return true;
2232}
2233
[email protected]79368982013-11-13 01:11:012234void NavigationControllerImpl::PruneAllButLastCommitted() {
2235 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162236
avi2b177592014-12-10 02:08:022237 DCHECK_EQ(0, last_committed_entry_index_);
2238 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442239
Carlos Caballeroede6f8c2021-01-28 11:01:502240 SetHistoryOffsetAndLength(last_committed_entry_index_, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442241}
2242
[email protected]79368982013-11-13 01:11:012243void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162244 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012245 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262246
[email protected]474f8512013-05-31 22:31:162247 // Erase all entries but the last committed entry. There may still be a
2248 // new pending entry after this.
2249 entries_.erase(entries_.begin(),
2250 entries_.begin() + last_committed_entry_index_);
2251 entries_.erase(entries_.begin() + 1, entries_.end());
2252 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262253}
2254
Christian Dullweber1af31e62018-02-22 11:49:482255void NavigationControllerImpl::DeleteNavigationEntries(
2256 const DeletionPredicate& deletionPredicate) {
2257 // It is up to callers to check the invariants before calling this.
2258 CHECK(CanPruneAllButLastCommitted());
2259 std::vector<int> delete_indices;
2260 for (size_t i = 0; i < entries_.size(); i++) {
2261 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572262 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482263 delete_indices.push_back(i);
2264 }
2265 }
2266 if (delete_indices.empty())
2267 return;
2268
2269 if (delete_indices.size() == GetEntryCount() - 1U) {
2270 PruneAllButLastCommitted();
2271 } else {
2272 // Do the deletion in reverse to preserve indices.
2273 for (auto it = delete_indices.rbegin(); it != delete_indices.rend(); ++it) {
2274 RemoveEntryAtIndex(*it);
2275 }
Carlos Caballeroede6f8c2021-01-28 11:01:502276 SetHistoryOffsetAndLength(last_committed_entry_index_, GetEntryCount());
Christian Dullweber1af31e62018-02-22 11:49:482277 }
2278 delegate()->NotifyNavigationEntriesDeleted();
2279}
2280
Shivani Sharma883f5f32019-02-12 18:20:012281bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2282 auto* entry = GetEntryAtIndex(index);
2283 return entry && entry->should_skip_on_back_forward_ui();
2284}
2285
Carlos Caballero35ce710c2019-09-19 10:59:452286BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2287 return back_forward_cache_;
2288}
2289
clamy987a3752018-05-03 17:36:262290void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2291 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2292 // progress, since this will cause a use-after-free. (We only allow this
2293 // when the tab is being destroyed for shutdown, since it won't return to
2294 // NavigateToEntry in that case.) http://crbug.com/347742.
2295 CHECK(!in_navigate_to_pending_entry_ || delegate_->IsBeingDestroyed());
2296
2297 if (was_failure && pending_entry_) {
2298 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2299 } else {
2300 failed_pending_entry_id_ = 0;
2301 }
2302
2303 if (pending_entry_) {
2304 if (pending_entry_index_ == -1)
2305 delete pending_entry_;
2306 pending_entry_index_ = -1;
2307 pending_entry_ = nullptr;
2308 }
arthursonzogni66f711c2019-10-08 14:40:362309
2310 // Ensure any refs to the current pending entry are ignored if they get
2311 // deleted, by clearing the set of known refs. All future pending entries will
2312 // only be affected by new refs.
2313 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262314}
2315
2316void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2317 if (pending_entry_)
2318 pending_entry_->set_ssl_error(error);
2319}
2320
Camille Lamy5193caa2018-10-12 11:59:422321#if defined(OS_ANDROID)
2322// static
2323bool NavigationControllerImpl::ValidateDataURLAsString(
2324 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2325 if (!data_url_as_string)
2326 return false;
2327
2328 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2329 return false;
2330
2331 // The number of characters that is enough for validating a data: URI.
2332 // From the GURL's POV, the only important part here is scheme, it doesn't
2333 // check the actual content. Thus we can take only the prefix of the url, to
2334 // avoid unneeded copying of a potentially long string.
2335 const size_t kDataUriPrefixMaxLen = 64;
2336 GURL data_url(
2337 std::string(data_url_as_string->front_as<char>(),
2338 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2339 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2340 return false;
2341
2342 return true;
2343}
2344#endif
2345
Shivani Sharma194877032019-03-07 17:52:472346void NavigationControllerImpl::NotifyUserActivation() {
2347 // When a user activation occurs, ensure that all adjacent entries for the
2348 // same document clear their skippable bit, so that the history manipulation
2349 // intervention does not apply to them.
Shivani Sharmaffb32b82019-04-09 16:58:472350 // TODO(crbug.com/949279) in case it becomes necessary for resetting based on
2351 // which frame created an entry and which frame has the user gesture.
Shivani Sharma194877032019-03-07 17:52:472352 auto* last_committed_entry = GetLastCommittedEntry();
2353 if (!last_committed_entry)
2354 return;
Shivani Sharma194877032019-03-07 17:52:472355
Shivani Sharmac4cc8922019-04-18 03:11:172356 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472357}
2358
clamy987a3752018-05-03 17:36:262359bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2360 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172361 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262362 NavigationEntryImpl* entry =
2363 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2364 if (!entry)
2365 return false;
2366
2367 FrameNavigationEntry* frame_entry =
2368 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2369 if (!frame_entry)
2370 return false;
2371
Camille Lamy5193caa2018-10-12 11:59:422372 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572373 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232374 ReloadType::NONE, false /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422375 true /* is_history_navigation_in_new_child */);
clamyea99ea12018-05-28 13:54:232376
2377 if (!request)
2378 return false;
2379
arthursonzognif046d4a2019-12-12 19:08:102380 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412381
Lukasz Anforowicz9ee83c272020-12-01 20:14:052382 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2383 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232384
2385 return true;
clamy987a3752018-05-03 17:36:262386}
2387
Tsuyoshi Horo52fd08e2020-07-07 07:03:452388bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2389 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2390 if (!entry)
2391 return false;
2392 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2393 if (!frame_entry)
2394 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142395 ReloadType reload_type = ReloadType::NORMAL;
2396 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452397 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142398 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452399 false /* is_same_document_history_load */,
2400 false /* is_history_navigation_in_new_child */);
2401 if (!request)
2402 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052403 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452404 return true;
2405}
2406
Dave Tapuska8bfd84c2019-03-26 20:47:162407void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2408 int offset,
2409 int sandbox_frame_tree_node_id) {
2410 if (!CanGoToOffset(offset))
2411 return;
2412 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id);
2413}
2414
clamy987a3752018-05-03 17:36:262415void NavigationControllerImpl::NavigateFromFrameProxy(
2416 RenderFrameHostImpl* render_frame_host,
2417 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022418 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452419 int initiator_process_id,
Lukasz Anforowicz63f3b9432019-05-30 05:42:582420 const base::Optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262421 bool is_renderer_initiated,
2422 SiteInstance* source_site_instance,
2423 const Referrer& referrer,
2424 ui::PageTransition page_transition,
2425 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252426 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262427 const std::string& method,
2428 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092429 const std::string& extra_headers,
John Delaney50425f82020-04-07 16:26:212430 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Gyuyoung Kim38e63632021-01-14 13:11:392431 const base::Optional<blink::Impression>& impression) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582432 if (is_renderer_initiated)
2433 DCHECK(initiator_origin.has_value());
2434
clamy987a3752018-05-03 17:36:262435 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582436
Rakina Zata Amni66992a82020-06-24 03:57:522437 // Don't allow an entry replacement if there is no entry to replace.
2438 // http://crbug.com/457149
2439 if (GetEntryCount() == 0)
2440 should_replace_current_entry = false;
2441
clamy987a3752018-05-03 17:36:262442 // Create a NavigationEntry for the transfer, without making it the pending
2443 // entry. Subframe transfers should have a clone of the last committed entry
2444 // with a FrameNavigationEntry for the target frame. Main frame transfers
2445 // should have a new NavigationEntry.
2446 // TODO(creis): Make this unnecessary by creating (and validating) the params
2447 // directly, passing them to the destination RenderFrameHost. See
2448 // https://crbug.com/536906.
2449 std::unique_ptr<NavigationEntryImpl> entry;
2450 if (!node->IsMainFrame()) {
2451 // Subframe case: create FrameNavigationEntry.
2452 if (GetLastCommittedEntry()) {
2453 entry = GetLastCommittedEntry()->Clone();
2454 entry->set_extra_headers(extra_headers);
2455 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2456 // Renderer-initiated navigation that target a remote frame are currently
2457 // classified as browser-initiated when this one has already navigated.
2458 // See https://crbug.com/722251.
2459 } else {
2460 // If there's no last committed entry, create an entry for about:blank
2461 // with a subframe entry for our destination.
2462 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2463 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:062464 GURL(url::kAboutBlankURL), referrer, initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:102465 source_site_instance, page_transition, is_renderer_initiated,
2466 extra_headers, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:372467 nullptr /* blob_url_loader_factory */, should_replace_current_entry,
2468 GetWebContents()));
2469 // CreateNavigationEntry() may have changed the transition type.
2470 page_transition = entry->GetTransitionType();
clamy987a3752018-05-03 17:36:262471 }
clamy987a3752018-05-03 17:36:262472 entry->AddOrUpdateFrameEntry(
2473 node, -1, -1, nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582474 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Lukasz Anforowicz435bcb582019-07-12 20:50:062475 base::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202476 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392477 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482478 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori79d549d2021-02-18 12:59:542479 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262480 } else {
2481 // Main frame case.
2482 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102483 url, referrer, initiator_origin, source_site_instance, page_transition,
2484 is_renderer_initiated, extra_headers, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:372485 blob_url_loader_factory, should_replace_current_entry,
2486 GetWebContents()));
2487 // CreateNavigationEntry() may have changed the transition type.
2488 page_transition = entry->GetTransitionType();
clamy987a3752018-05-03 17:36:262489 entry->root_node()->frame_entry->set_source_site_instance(
2490 static_cast<SiteInstanceImpl*>(source_site_instance));
2491 entry->root_node()->frame_entry->set_method(method);
2492 }
clamy987a3752018-05-03 17:36:262493
Camille Lamy5193caa2018-10-12 11:59:422494 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262495 if (GetLastCommittedEntry() &&
2496 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2497 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422498 override_user_agent = true;
clamy987a3752018-05-03 17:36:262499 }
2500 // TODO(creis): Set user gesture and intent received timestamp on Android.
2501
2502 // We may not have successfully added the FrameNavigationEntry to |entry|
2503 // above (per https://crbug.com/608402), in which case we create it from
2504 // scratch. This works because we do not depend on |frame_entry| being inside
2505 // |entry| during NavigateToEntry. This will go away when we shortcut this
2506 // further in https://crbug.com/536906.
2507 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2508 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452509 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
clamy987a3752018-05-03 17:36:262510 node->unique_name(), -1, -1, nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582511 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Lukasz Anforowicz435bcb582019-07-12 20:50:062512 nullptr /* origin */, referrer, initiator_origin, std::vector<GURL>(),
Miyoung Shin5d77f72072020-10-09 15:14:202513 blink::PageState(), method, -1, blob_url_loader_factory,
Antonio Sartori78a749f2020-11-30 12:03:392514 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482515 nullptr /* subresource_web_bundle_navigation_info */,
Titouan Rigoudy6ec70402021-02-02 15:42:192516 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262517 }
2518
Camille Lamy5193caa2018-10-12 11:59:422519 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022520 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452521 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292522 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422523 params.source_site_instance = source_site_instance;
2524 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2525 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032526 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422527 params.referrer = referrer;
2528 /* params.redirect_chain: skip */
2529 params.extra_headers = extra_headers;
2530 params.is_renderer_initiated = is_renderer_initiated;
2531 params.override_user_agent = UA_OVERRIDE_INHERIT;
2532 /* params.base_url_for_data_url: skip */
2533 /* params.virtual_url_for_data_url: skip */
2534 /* params.data_url_as_string: skip */
2535 params.post_data = post_body;
2536 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582537 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422538 /* params.frame_name: skip */
2539 // TODO(clamy): See if user gesture should be propagated to this function.
2540 params.has_user_gesture = false;
2541 params.should_clear_history_list = false;
2542 params.started_from_context_menu = false;
2543 /* params.navigation_ui_data: skip */
2544 /* params.input_start: skip */
Lucas Furukawa Gadania9c45682019-07-31 22:05:142545 params.was_activated = mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542546 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212547 params.impression = impression;
Camille Lamy5193caa2018-10-12 11:59:422548
2549 std::unique_ptr<NavigationRequest> request =
2550 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032551 node, params, override_user_agent, should_replace_current_entry,
2552 false /* has_user_gesture */, download_policy, ReloadType::NONE,
Scott Violet5ae6c42e2020-10-28 02:47:372553 entry.get(), frame_entry.get(), params.transition_type);
clamyea99ea12018-05-28 13:54:232554
2555 if (!request)
2556 return;
2557
Arthur Hemery948742762019-09-18 10:06:242558 // At this stage we are proceeding with this navigation. If this was renderer
2559 // initiated with user gesture, we need to make sure we clear up potential
2560 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2561 DiscardNonCommittedEntries();
2562
Lukasz Anforowicz9ee83c272020-12-01 20:14:052563 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262564}
2565
[email protected]d1198fd2012-08-13 22:50:192566void NavigationControllerImpl::SetSessionStorageNamespace(
Aaron Colwellf3b316e2021-03-11 20:17:052567 const StoragePartitionId& partition_id,
[email protected]8ff00d72012-10-23 19:12:212568 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192569 if (!session_storage_namespace)
2570 return;
2571
2572 // We can't overwrite an existing SessionStorage without violating spec.
2573 // Attempts to do so may give a tab access to another tab's session storage
2574 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152575 bool successful_insert =
2576 session_storage_namespace_map_
Aaron Colwellf3b316e2021-03-11 20:17:052577 .insert(std::make_pair(partition_id,
2578 static_cast<SessionStorageNamespaceImpl*>(
2579 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302580 .second;
2581 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
Aaron Colwellb731a0ae2021-03-19 19:14:472582 OnStoragePartitionIdAdded(partition_id);
[email protected]d1198fd2012-08-13 22:50:192583}
2584
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572585bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Aran Gilman37d11632019-10-08 23:07:152586 return IsInitialNavigation() && !GetLastCommittedEntry() &&
Carlos Caballeroede6f8c2021-01-28 11:01:502587 !frame_tree_.has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462588}
2589
Aran Gilman37d11632019-10-08 23:07:152590SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Aaron Colwell78b4bde2021-03-16 16:16:092591 const SiteInfo& site_info) {
2592 // TODO(acolwell): Remove partition_id logic once we have successfully
2593 // migrated the implementation to be a StoragePartitionConfig. At that point
2594 // |site_info| can be replaced with a StoragePartitionConfig.
2595 const StoragePartitionId partition_id =
2596 site_info.GetStoragePartitionId(browser_context_);
2597 const StoragePartitionConfig partition_config =
2598 site_info.GetStoragePartitionConfig(browser_context_);
[email protected]d1198fd2012-08-13 22:50:192599
[email protected]fdac6ade2013-07-20 01:06:302600 StoragePartition* partition =
Aaron Colwell78b4bde2021-03-16 16:16:092601 BrowserContext::GetStoragePartition(browser_context_, partition_config);
michaelnbacbcbd2016-02-09 00:32:032602 DOMStorageContextWrapper* context_wrapper =
2603 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2604
2605 SessionStorageNamespaceMap::const_iterator it =
2606 session_storage_namespace_map_.find(partition_id);
2607 if (it != session_storage_namespace_map_.end()) {
2608 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152609 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2610 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:472611
2612 // Verify that the config we generated now matches the one that
2613 // was generated when the namespace was added to the map.
2614 DCHECK_EQ(partition_config, it->first.config());
2615 if (partition_config != it->first.config()) {
2616 LogStoragePartitionIdCrashKeys(it->first, partition_id);
2617 }
2618
michaelnbacbcbd2016-02-09 00:32:032619 return it->second.get();
2620 }
2621
2622 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102623 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2624 SessionStorageNamespaceImpl::Create(context_wrapper);
2625 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2626 session_storage_namespace.get();
2627 session_storage_namespace_map_[partition_id] =
2628 std::move(session_storage_namespace);
Aaron Colwellb731a0ae2021-03-19 19:14:472629 OnStoragePartitionIdAdded(partition_id);
[email protected]fdac6ade2013-07-20 01:06:302630
Daniel Murphy31bbb8b12018-02-07 21:44:102631 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302632}
2633
2634SessionStorageNamespace*
2635NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Aaron Colwell78b4bde2021-03-16 16:16:092636 return GetSessionStorageNamespace(SiteInfo());
[email protected]fdac6ade2013-07-20 01:06:302637}
2638
2639const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572640NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302641 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552642}
[email protected]d202a7c2012-01-04 07:53:472643
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572644bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562645 return needs_reload_;
2646}
[email protected]a26023822011-12-29 00:23:552647
[email protected]46bb5e9c2013-10-03 22:16:472648void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412649 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2650}
2651
2652void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472653 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412654 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542655
2656 if (last_committed_entry_index_ != -1) {
2657 entries_[last_committed_entry_index_]->SetTransitionType(
2658 ui::PAGE_TRANSITION_RELOAD);
2659 }
[email protected]46bb5e9c2013-10-03 22:16:472660}
2661
[email protected]d202a7c2012-01-04 07:53:472662void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572663 DCHECK_LT(index, GetEntryCount());
2664 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312665 DiscardNonCommittedEntries();
2666
2667 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122668 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312669 last_committed_entry_index_--;
2670}
2671
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572672NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272673 // If there is no pending_entry_, there should be no pending_entry_index_.
2674 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2675
2676 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362677 // at that index. An exception is while a reload of a post commit error page
2678 // is ongoing; in that case pending entry will point to the entry replaced
2679 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272680 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362681 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2682 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272683
[email protected]022af742011-12-28 18:37:252684 return pending_entry_;
2685}
2686
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572687int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272688 // The pending entry index must always be less than the number of entries.
2689 // If there are no entries, it must be exactly -1.
2690 DCHECK_LT(pending_entry_index_, GetEntryCount());
2691 DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:552692 return pending_entry_index_;
2693}
2694
avi25764702015-06-23 15:43:372695void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572696 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362697 bool replace,
2698 bool was_post_commit_error) {
mastiz7eddb5f72016-06-23 09:52:452699 DCHECK(!ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2700 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202701
avi5cad4912015-06-19 05:25:442702 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2703 // need to keep continuity with the pending entry, so copy the pending entry's
2704 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2705 // then the renderer navigated on its own, independent of the pending entry,
2706 // so don't copy anything.
2707 if (pending_entry_ && pending_entry_index_ == -1)
2708 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202709
arthursonzogni69a6a1b2019-09-17 09:23:002710 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:202711
creisee17e932015-07-17 17:56:222712 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362713 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102714 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572715 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362716 // If the new entry is a post-commit error page, we store the current last
2717 // committed entry to the side so that we can put it back when navigating
2718 // away from the error.
2719 if (was_post_commit_error) {
2720 DCHECK(!entry_replaced_by_post_commit_error_);
2721 entry_replaced_by_post_commit_error_ =
2722 std::move(entries_[last_committed_entry_index_]);
2723 }
dcheng36b6aec92015-12-26 06:16:362724 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222725 return;
2726 }
[email protected]765b35502008-08-21 00:51:202727
creis37979a62015-08-04 19:48:182728 // We shouldn't see replace == true when there's no committed entries.
2729 DCHECK(!replace);
2730
Michael Thiessen9b14d512019-09-23 21:19:472731 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202732
Shivani Sharmad8c8d652019-02-13 17:27:572733 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202734
dcheng36b6aec92015-12-26 06:16:362735 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202736 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292737}
2738
Shivani Sharmad8c8d652019-02-13 17:27:572739void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162740 if (entries_.size() < max_entry_count())
2741 return;
2742
2743 DCHECK_EQ(max_entry_count(), entries_.size());
2744 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572745 CHECK_EQ(pending_entry_index_, -1);
2746
2747 int index = 0;
2748 if (base::FeatureList::IsEnabled(
2749 features::kHistoryManipulationIntervention)) {
2750 // Retrieve the oldest skippable entry.
2751 for (; index < GetEntryCount(); index++) {
2752 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2753 break;
2754 }
2755 }
2756
2757 // If there is no skippable entry or if it is the last committed entry then
2758 // fall back to pruning the oldest entry. It is not safe to prune the last
2759 // committed entry.
2760 if (index == GetEntryCount() || index == last_committed_entry_index_)
2761 index = 0;
2762
2763 bool should_succeed = RemoveEntryAtIndex(index);
2764 DCHECK_EQ(true, should_succeed);
2765
2766 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252767}
2768
clamy3cb9bea92018-07-10 12:42:022769void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162770 ReloadType reload_type,
2771 int sandboxed_source_frame_tree_node_id) {
Alexander Timin3a92df72019-09-20 11:59:502772 TRACE_EVENT0("navigation",
2773 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272774 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022775 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362776 if (pending_entry_index_ != -1) {
2777 // The pending entry may not be in entries_ if a post-commit error page is
2778 // showing.
2779 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2780 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2781 }
Gyuyoung Kim107c2a02021-04-13 01:49:302782 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572783 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:012784 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:002785 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:572786 int nav_entry_id = pending_entry_->GetUniqueID();
2787
[email protected]83c2e232011-10-07 21:36:462788 // If we were navigating to a slow-to-commit page, and the user performs
2789 // a session history navigation to the last committed page, RenderViewHost
2790 // will force the throbber to start, but WebKit will essentially ignore the
2791 // navigation, and won't send a message to stop the throbber. To prevent this
2792 // from happening, we drop the navigation here and stop the slow-to-commit
2793 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022794 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:082795 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:272796 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
[email protected]ec6c05f2013-10-23 18:41:572797 delegate_->Stop();
[email protected]6a13a6c2011-12-20 21:47:122798
[email protected]83c2e232011-10-07 21:36:462799 DiscardNonCommittedEntries();
2800 return;
2801 }
2802
creisce0ef3572017-01-26 17:53:082803 // Compare FrameNavigationEntries to see which frames in the tree need to be
2804 // navigated.
clamy3cb9bea92018-07-10 12:42:022805 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2806 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Alex Moshchuk3a4e77a2020-05-29 21:32:572807 FindFramesToNavigate(root, reload_type, &same_document_loads,
2808 &different_document_loads);
creis4e2ecb72015-06-20 00:46:302809
2810 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572811 // We were unable to match any frames to navigate. This can happen if a
2812 // history navigation targets a subframe that no longer exists
2813 // (https://crbug.com/705550). In this case, we need to update the current
2814 // history entry to the pending one but keep the main document loaded. We
2815 // also need to ensure that observers are informed about the updated
2816 // current history entry (e.g., for greying out back/forward buttons), and
2817 // that renderer processes update their history offsets. The easiest way
2818 // to do all that is to schedule a "redundant" same-document navigation in
2819 // the main frame.
2820 //
2821 // Note that we don't want to remove this history entry, as it might still
2822 // be valid later, since a frame that it's targeting may be recreated.
2823 //
2824 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
2825 // need to repeat history navigations until finding the one that works.
2826 // Consider changing this behavior to keep looking for the first valid
2827 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:022828 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422829 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572830 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:572831 ReloadType::NONE /* reload_type */,
2832 true /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422833 false /* is_history_navigation_in_new_child */);
clamy3cb9bea92018-07-10 12:42:022834 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572835 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:022836 DiscardPendingEntry(false);
2837 return;
2838 }
Alex Moshchuk3a4e77a2020-05-29 21:32:572839 same_document_loads.push_back(std::move(navigation_request));
2840
2841 // Sanity check that we never take this branch for any kinds of reloads,
2842 // as those should've queued a different-document load in the main frame.
2843 DCHECK(!is_forced_reload);
2844 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:302845 }
2846
Dave Tapuska8bfd84c2019-03-26 20:47:162847 // If |sandboxed_source_frame_node_id| is valid, then track whether this
2848 // navigation affects any frame outside the frame's subtree.
2849 if (sandboxed_source_frame_tree_node_id !=
2850 FrameTreeNode::kFrameTreeNodeInvalidId) {
2851 bool navigates_inside_tree =
2852 DoesSandboxNavigationStayWithinSubtree(
2853 sandboxed_source_frame_tree_node_id, same_document_loads) &&
2854 DoesSandboxNavigationStayWithinSubtree(
2855 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:502856 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:162857 // the number of pages that trigger this.
2858 FrameTreeNode* sandbox_source_frame_tree_node =
2859 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
2860 if (sandbox_source_frame_tree_node) {
2861 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
2862 sandbox_source_frame_tree_node->current_frame_host(),
2863 navigates_inside_tree
2864 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
2865 : blink::mojom::WebFeature::
2866 kSandboxBackForwardAffectsFramesOutsideSubtree);
2867 }
Dave Tapuska855c1e12019-08-23 20:45:522868
2869 // If the navigation occurred outside the tree discard it because
2870 // the sandboxed frame didn't have permission to navigate outside
2871 // its tree. If it is possible that the navigation is both inside and
2872 // outside the frame tree and we discard it entirely because we don't
2873 // want to end up in a history state that didn't exist before.
2874 if (base::FeatureList::IsEnabled(
2875 features::kHistoryPreventSandboxedNavigation) &&
2876 !navigates_inside_tree) {
2877 DiscardPendingEntry(false);
2878 return;
2879 }
Dave Tapuska8bfd84c2019-03-26 20:47:162880 }
2881
Carlos Caballero539a421c2020-07-06 10:25:572882 // BackForwardCache:
2883 // Navigate immediately if the document is in the BackForwardCache.
2884 if (back_forward_cache_.GetEntry(nav_entry_id)) {
2885 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
2886 DCHECK_EQ(reload_type, ReloadType::NONE);
2887 auto navigation_request = CreateNavigationRequestFromEntry(
2888 root, pending_entry_, pending_entry_->GetFrameEntry(root),
2889 ReloadType::NONE, false /* is_same_document_history_load */,
2890 false /* is_history_navigation_in_new_child */);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052891 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:572892
2893 return;
2894 }
2895
2896 // History navigation might try to reuse a specific BrowsingInstance, already
2897 // used by a page in the cache. To avoid having two different main frames that
2898 // live in the same BrowsingInstance, evict the all pages with this
2899 // BrowsingInstance from the cache.
2900 //
2901 // For example, take the following scenario:
2902 //
2903 // A1 = Some page on a.com
2904 // A2 = Some other page on a.com
2905 // B3 = An uncacheable page on b.com
2906 //
2907 // Then the following navigations occur:
2908 // A1->A2->B3->A1
2909 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
2910 // take its place) and A1 will be created in the same BrowsingInstance (and
2911 // SiteInstance), as A2.
2912 //
2913 // If we didn't do anything, both A1 and A2 would remain alive in the same
2914 // BrowsingInstance/SiteInstance, which is unsupported by
2915 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
2916 // A2 from the cache.
2917 if (pending_entry_->site_instance()) {
2918 back_forward_cache_.EvictFramesInRelatedSiteInstances(
2919 pending_entry_->site_instance());
2920 }
2921
clamy3cb9bea92018-07-10 12:42:022922 // This call does not support re-entrancy. See http://crbug.com/347742.
2923 CHECK(!in_navigate_to_pending_entry_);
2924 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:302925
arthursonzogni66f711c2019-10-08 14:40:362926 // It is not possible to delete the pending NavigationEntry while navigating
2927 // to it. Grab a reference to delay potential deletion until the end of this
2928 // function.
2929 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
2930
creis4e2ecb72015-06-20 00:46:302931 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:022932 for (auto& item : same_document_loads) {
2933 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052934 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302935 }
clamy3cb9bea92018-07-10 12:42:022936 for (auto& item : different_document_loads) {
2937 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052938 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302939 }
clamy3cb9bea92018-07-10 12:42:022940
2941 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:302942}
2943
Alex Moshchuk3a4e77a2020-05-29 21:32:572944NavigationControllerImpl::HistoryNavigationAction
2945NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:302946 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:572947 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:422948 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:582949 // Only active and prerendered documents are allowed to navigate in their
2950 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422951 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:012952 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:582953 // - If the document is in pending deletion, the browser already committed
2954 // to destroying this RenderFrameHost. See https://crbug.com/930278.
2955 // - If the document is in back-forward cache, it's not allowed to navigate
2956 // as it should remain frozen. Ignore the request and evict the document
2957 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422958 //
Sreeja Kamishettydb8e2892021-03-10 09:30:582959 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:422960 // which should all be inactive as well.
Sreeja Kamishettydb8e2892021-03-10 09:30:582961 if (render_frame_host->IsInactiveAndDisallowActivation())
Sreeja Kamishetty8eacabb2021-03-09 11:45:422962 return HistoryNavigationAction::kStopLooking;
2963 }
arthursonzogni03f76152019-02-12 10:35:202964
Alex Moshchuk3a4e77a2020-05-29 21:32:572965 // If there's no last committed entry, there is no previous history entry to
2966 // compare against, so fall back to a different-document load. Note that we
2967 // should only reach this case for the root frame and not descend further
2968 // into subframes.
2969 if (!GetLastCommittedEntry()) {
2970 DCHECK(frame->IsMainFrame());
2971 return HistoryNavigationAction::kDifferentDocument;
2972 }
2973
2974 // Reloads should result in a different-document load. Note that reloads may
2975 // also happen via the |needs_reload_| mechanism where the reload_type is
2976 // NONE, so detect this by comparing whether we're going to the same
2977 // entry that we're currently on. Similarly to above, only main frames
2978 // should reach this. Note that subframes support reloads, but that's done
2979 // via a different path that doesn't involve FindFramesToNavigate (see
2980 // RenderFrameHost::Reload()).
2981 if (reload_type != ReloadType::NONE ||
2982 pending_entry_index_ == last_committed_entry_index_) {
2983 DCHECK(frame->IsMainFrame());
2984 return HistoryNavigationAction::kDifferentDocument;
2985 }
2986
Alex Moshchuk47d1a4bd2020-06-01 22:15:342987 // If there is no new FrameNavigationEntry for the frame, ignore the
2988 // load. For example, this may happen when going back to an entry before a
2989 // frame was created. Suppose we commit a same-document navigation that also
2990 // results in adding a new subframe somewhere in the tree. If we go back,
2991 // the new subframe will be missing a FrameNavigationEntry in the previous
2992 // NavigationEntry, but we shouldn't delete or change what's loaded in
2993 // it.
2994 //
Alex Moshchuke65c39272020-06-03 17:55:372995 // Note that in this case, there is no need to keep looking for navigations
2996 // in subframes, which would be missing FrameNavigationEntries as well.
2997 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:342998 // It's important to check this before checking |old_item| below, since both
2999 // might be null, and in that case we still shouldn't change what's loaded in
3000 // this frame. Note that scheduling any loads assumes that |new_item| is
3001 // non-null. See https://crbug.com/1088354.
3002 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3003 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:373004 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:343005
Alex Moshchuk3a4e77a2020-05-29 21:32:573006 // If there is no old FrameNavigationEntry, schedule a different-document
3007 // load.
3008 //
creis225a7432016-06-03 22:56:273009 // TODO(creis): Store the last committed FrameNavigationEntry to use here,
3010 // rather than assuming the NavigationEntry has up to date info on subframes.
Alex Moshchuk3a4e77a2020-05-29 21:32:573011 // Note that this may require sharing FrameNavigationEntries between
3012 // NavigationEntries (https://crbug.com/373041) as a prerequisite, since
3013 // otherwise the stored FrameNavigationEntry might get stale (e.g., after
3014 // subframe navigations, or in the case where we don't find any frames to
3015 // navigate and ignore a back/forward navigation while moving to a different
3016 // NavigationEntry, as in https://crbug.com/705550).
creis4e2ecb72015-06-20 00:46:303017 FrameNavigationEntry* old_item =
3018 GetLastCommittedEntry()->GetFrameEntry(frame);
Alex Moshchuk3a4e77a2020-05-29 21:32:573019 if (!old_item)
3020 return HistoryNavigationAction::kDifferentDocument;
3021
Alex Moshchuk3a4e77a2020-05-29 21:32:573022 // If the new item is not in the same SiteInstance, schedule a
3023 // different-document load. Newly restored items may not have a SiteInstance
3024 // yet, in which case it will be assigned on first commit.
3025 if (new_item->site_instance() &&
3026 new_item->site_instance() != old_item->site_instance())
3027 return HistoryNavigationAction::kDifferentDocument;
3028
3029 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:313030 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
3031 // that we do this even if the history navigation would not be modifying this
3032 // frame were it live.
3033 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:573034 return HistoryNavigationAction::kDifferentDocument;
3035
3036 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:313037 // Starting a navigation after a crash early-promotes the speculative
3038 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
3039 // committed yet, so we can not possibly perform a same-document history
3040 // navigation. The frame would need to be reloaded with a cross-document
3041 // navigation.
3042 if (!frame->current_frame_host()->has_committed_any_navigation())
3043 return HistoryNavigationAction::kDifferentDocument;
3044
creis54131692016-08-12 18:32:253045 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:493046 // sequence number but different item sequence number.
3047 if (new_item->document_sequence_number() ==
3048 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573049 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493050 }
avib48cb312016-05-05 21:35:003051
Alex Moshchuk3a4e77a2020-05-29 21:32:573052 // Otherwise, if both item and document sequence numbers differ, this
3053 // should be a different document load.
3054 return HistoryNavigationAction::kDifferentDocument;
3055 }
3056
3057 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373058 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573059 DCHECK_EQ(new_item->document_sequence_number(),
3060 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373061 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573062}
3063
3064void NavigationControllerImpl::FindFramesToNavigate(
3065 FrameTreeNode* frame,
3066 ReloadType reload_type,
3067 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3068 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3069 DCHECK(pending_entry_);
3070 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3071
3072 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3073
3074 if (action == HistoryNavigationAction::kSameDocument) {
3075 std::unique_ptr<NavigationRequest> navigation_request =
3076 CreateNavigationRequestFromEntry(
3077 frame, pending_entry_, new_item, reload_type,
3078 true /* is_same_document_history_load */,
3079 false /* is_history_navigation_in_new_child */);
3080 if (navigation_request) {
3081 // Only add the request if was properly created. It's possible for the
3082 // creation to fail in certain cases, e.g. when the URL is invalid.
3083 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303084 }
Lei Zhang96031532019-10-10 19:05:473085
Alex Moshchuk3a4e77a2020-05-29 21:32:573086 // TODO(avi, creis): This is a bug; we should not return here. Rather, we
3087 // should continue on and navigate all child frames which have also
3088 // changed. This bug is the cause of <https://crbug.com/542299>, which is
3089 // a NC_IN_PAGE_NAVIGATION renderer kill.
3090 //
3091 // However, this bug is a bandaid over a deeper and worse problem. Doing a
3092 // pushState immediately after loading a subframe is a race, one that no
3093 // web page author expects. If we fix this bug, many large websites break.
3094 // For example, see <https://crbug.com/598043> and the spec discussion at
3095 // <https://github.com/whatwg/html/issues/1191>.
3096 //
3097 // For now, we accept this bug, and hope to resolve the race in a
3098 // different way that will one day allow us to fix this.
3099 return;
3100 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473101 std::unique_ptr<NavigationRequest> navigation_request =
3102 CreateNavigationRequestFromEntry(
3103 frame, pending_entry_, new_item, reload_type,
3104 false /* is_same_document_history_load */,
3105 false /* is_history_navigation_in_new_child */);
3106 if (navigation_request) {
3107 // Only add the request if was properly created. It's possible for the
3108 // creation to fail in certain cases, e.g. when the URL is invalid.
3109 different_document_loads->push_back(std::move(navigation_request));
3110 }
3111 // For a different document, the subframes will be destroyed, so there's
3112 // no need to consider them.
3113 return;
Alex Moshchuke65c39272020-06-03 17:55:373114 } else if (action == HistoryNavigationAction::kStopLooking) {
3115 return;
creis4e2ecb72015-06-20 00:46:303116 }
3117
3118 for (size_t i = 0; i < frame->child_count(); i++) {
clamy3cb9bea92018-07-10 12:42:023119 FindFramesToNavigate(frame->child_at(i), reload_type, same_document_loads,
creis4e2ecb72015-06-20 00:46:303120 different_document_loads);
3121 }
3122}
3123
clamy21718cc22018-06-13 13:34:243124void NavigationControllerImpl::NavigateWithoutEntry(
3125 const LoadURLParams& params) {
3126 // Find the appropriate FrameTreeNode.
3127 FrameTreeNode* node = nullptr;
3128 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3129 !params.frame_name.empty()) {
3130 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003131 ? frame_tree_.FindByID(params.frame_tree_node_id)
3132 : frame_tree_.FindByName(params.frame_name);
clamy21718cc22018-06-13 13:34:243133 }
3134
3135 // If no FrameTreeNode was specified, navigate the main frame.
3136 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003137 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243138
Camille Lamy5193caa2018-10-12 11:59:423139 // Compute overrides to the LoadURLParams for |override_user_agent|,
3140 // |should_replace_current_entry| and |has_user_gesture| that will be used
3141 // both in the creation of the NavigationEntry and the NavigationRequest.
3142 // Ideally, the LoadURLParams themselves would be updated, but since they are
3143 // passed as a const reference, this is not possible.
3144 // TODO(clamy): When we only create a NavigationRequest, move this to
3145 // CreateNavigationRequestFromLoadURLParams.
3146 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3147 GetLastCommittedEntry());
3148
3149 // Don't allow an entry replacement if there is no entry to replace.
3150 // http://crbug.com/457149
3151 bool should_replace_current_entry =
3152 params.should_replace_current_entry && entries_.size();
3153
Scott Violet5ae6c42e2020-10-28 02:47:373154 ui::PageTransition transition_type = params.transition_type;
3155
clamy21718cc22018-06-13 13:34:243156 // Javascript URLs should not create NavigationEntries. All other navigations
3157 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243158 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373159 std::unique_ptr<NavigationEntryImpl> entry =
3160 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3161 should_replace_current_entry,
3162 params.has_user_gesture);
3163 // CreateNavigationEntryFromLoadParams() may modify the transition type.
3164 transition_type = entry->GetTransitionType();
clamy21718cc22018-06-13 13:34:243165 DiscardPendingEntry(false);
3166 SetPendingEntry(std::move(entry));
3167 }
3168
3169 // Renderer-debug URLs are sent to the renderer process immediately for
3170 // processing and don't need to create a NavigationRequest.
3171 // Note: this includes navigations to JavaScript URLs, which are considered
3172 // renderer-debug URLs.
3173 // Note: we intentionally leave the pending entry in place for renderer debug
3174 // URLs, unlike the cases below where we clear it if the navigation doesn't
3175 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303176 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483177 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3178 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153179 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483180 params.url, browser_context_)) {
3181 DiscardPendingEntry(false);
3182 return;
3183 }
3184
clamy21718cc22018-06-13 13:34:243185 HandleRendererDebugURL(node, params.url);
3186 return;
3187 }
3188
Antonio Sartori78a749f2020-11-30 12:03:393189 DCHECK(pending_entry_);
3190
clamy21718cc22018-06-13 13:34:243191 // Convert navigations to the current URL to a reload.
3192 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3193 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3194 // DevTools sometimes issues navigations to main frames that they do not
3195 // expect to see treated as reload, and it only works because they pass a
3196 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3197 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543198 ReloadType reload_type = params.reload_type;
3199 if (reload_type == ReloadType::NONE &&
3200 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043201 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243202 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243203 params.load_type ==
3204 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
3205 false /* is_reload */, false /* is_navigation_to_existing_entry */,
clamy21718cc22018-06-13 13:34:243206 GetLastCommittedEntry())) {
3207 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473208 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393209
3210 // If this is a reload of an existing FrameNavigationEntry and we had a
3211 // policy container for it, then we should copy it into the pending entry,
3212 // so that it is copied to the navigation request in
3213 // CreateNavigationRequestFromLoadParams later.
3214 if (GetLastCommittedEntry()) {
3215 FrameNavigationEntry* previous_frame_entry =
3216 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193217 if (previous_frame_entry &&
3218 previous_frame_entry->policy_container_policies()) {
3219 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163220 previous_frame_entry->policy_container_policies()->Clone());
Antonio Sartori78a749f2020-11-30 12:03:393221 }
3222 }
3223 }
3224
3225 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
3226 // document (so no last commit), then we should copy the document polices from
3227 // RenderFrameHost's PolicyContainerHost. The NavigationRequest will create a
3228 // new PolicyContainerHost with the document policies from the
3229 // |pending_entry_|, and that PolicyContainerHost will be put in the final
3230 // RenderFrameHost for the navigation. This way, we ensure that we keep
3231 // enforcing the right policies on the initial empty document after the
3232 // reload.
3233 if (!GetLastCommittedEntry() && params.url.IsAboutBlank()) {
3234 if (node->current_frame_host() &&
3235 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193236 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163237 node->current_frame_host()
3238 ->policy_container_host()
3239 ->policies()
3240 .Clone());
Antonio Sartori78a749f2020-11-30 12:03:393241 }
clamy21718cc22018-06-13 13:34:243242 }
3243
3244 // navigation_ui_data should only be present for main frame navigations.
3245 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
3246
Camille Lamy5193caa2018-10-12 11:59:423247 std::unique_ptr<NavigationRequest> request =
3248 CreateNavigationRequestFromLoadParams(
3249 node, params, override_user_agent, should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:253250 params.has_user_gesture, blink::NavigationDownloadPolicy(),
3251 reload_type, pending_entry_, pending_entry_->GetFrameEntry(node),
3252 transition_type);
clamy21718cc22018-06-13 13:34:243253
3254 // If the navigation couldn't start, return immediately and discard the
3255 // pending NavigationEntry.
3256 if (!request) {
3257 DiscardPendingEntry(false);
3258 return;
3259 }
3260
Camille Lamy5193caa2018-10-12 11:59:423261#if DCHECK_IS_ON()
3262 // Safety check that NavigationRequest and NavigationEntry match.
3263 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3264#endif
3265
clamy21718cc22018-06-13 13:34:243266 // This call does not support re-entrancy. See http://crbug.com/347742.
3267 CHECK(!in_navigate_to_pending_entry_);
3268 in_navigate_to_pending_entry_ = true;
3269
arthursonzogni66f711c2019-10-08 14:40:363270 // It is not possible to delete the pending NavigationEntry while navigating
3271 // to it. Grab a reference to delay potential deletion until the end of this
3272 // function.
3273 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3274
Lukasz Anforowicz9ee83c272020-12-01 20:14:053275 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243276
3277 in_navigate_to_pending_entry_ = false;
3278}
3279
clamyea99ea12018-05-28 13:54:233280void NavigationControllerImpl::HandleRendererDebugURL(
3281 FrameTreeNode* frame_tree_node,
3282 const GURL& url) {
3283 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243284 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3285 // the renderer process is not live, unless it is the initial navigation
3286 // of the tab.
clamyea99ea12018-05-28 13:54:233287 if (!IsInitialNavigation()) {
3288 DiscardNonCommittedEntries();
3289 return;
3290 }
Fergal Dalyecd3b0202020-06-25 01:57:373291 // The current frame is always a main frame. If IsInitialNavigation() is
3292 // true then there have been no navigations and any frames of this tab must
3293 // be in the same renderer process. If that has crashed then the only frame
3294 // that can be revived is the main frame.
3295 frame_tree_node->render_manager()
3296 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233297 }
3298 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3299}
3300
clamy21718cc22018-06-13 13:34:243301std::unique_ptr<NavigationEntryImpl>
3302NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3303 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423304 const LoadURLParams& params,
3305 bool override_user_agent,
3306 bool should_replace_current_entry,
3307 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393308 // Browser initiated navigations might not have a blob_url_loader_factory set
3309 // in params even if the navigation is to a blob URL. If that happens, lookup
3310 // the correct url loader factory to use here.
3311 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483312 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473313 // Resolve the blob URL in the storage partition associated with the target
3314 // frame. This is the storage partition the URL will be loaded in, and only
3315 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393316 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473317 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393318 }
3319
clamy21718cc22018-06-13 13:34:243320 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443321 // extra_headers in params are \n separated; navigation entries want \r\n.
3322 std::string extra_headers_crlf;
3323 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243324
3325 // For subframes, create a pending entry with a corresponding frame entry.
3326 if (!node->IsMainFrame()) {
Tommy C. Li03eee77a2019-02-05 02:07:443327 if (GetLastCommittedEntry()) {
3328 // Create an identical NavigationEntry with a new FrameNavigationEntry for
3329 // the target subframe.
3330 entry = GetLastCommittedEntry()->Clone();
3331 } else {
3332 // If there's no last committed entry, create an entry for about:blank
3333 // with a subframe entry for our destination.
3334 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
3335 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063336 GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103337 params.source_site_instance.get(), params.transition_type,
3338 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:373339 blob_url_loader_factory, should_replace_current_entry,
3340 GetWebContents()));
Tommy C. Li03eee77a2019-02-05 02:07:443341 }
Nasko Oskov18006bc2018-12-06 02:53:583342
clamy21718cc22018-06-13 13:34:243343 entry->AddOrUpdateFrameEntry(
3344 node, -1, -1, nullptr,
3345 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Lukasz Anforowicz435bcb582019-07-12 20:50:063346 params.url, base::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203347 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393348 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:483349 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori78a749f2020-11-30 12:03:393350 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163351 // a conversion of a new navigation into a reload, we will set the right
3352 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193353 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243354 } else {
3355 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243356 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063357 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103358 params.source_site_instance.get(), params.transition_type,
3359 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:373360 blob_url_loader_factory, should_replace_current_entry,
3361 GetWebContents()));
clamy21718cc22018-06-13 13:34:243362 entry->set_source_site_instance(
3363 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3364 entry->SetRedirectChain(params.redirect_chain);
3365 }
3366
3367 // Set the FTN ID (only used in non-site-per-process, for tests).
3368 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243369 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423370 entry->SetIsOverridingUserAgent(override_user_agent);
3371 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543372 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243373
clamy21718cc22018-06-13 13:34:243374 switch (params.load_type) {
3375 case LOAD_TYPE_DEFAULT:
3376 break;
3377 case LOAD_TYPE_HTTP_POST:
3378 entry->SetHasPostData(true);
3379 entry->SetPostData(params.post_data);
3380 break;
3381 case LOAD_TYPE_DATA:
3382 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3383 entry->SetVirtualURL(params.virtual_url_for_data_url);
3384#if defined(OS_ANDROID)
3385 entry->SetDataURLAsString(params.data_url_as_string);
3386#endif
3387 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3388 break;
clamy21718cc22018-06-13 13:34:243389 }
3390
3391 // TODO(clamy): NavigationEntry is meant for information that will be kept
3392 // after the navigation ended and therefore is not appropriate for
3393 // started_from_context_menu. Move started_from_context_menu to
3394 // NavigationUIData.
3395 entry->set_started_from_context_menu(params.started_from_context_menu);
3396
3397 return entry;
3398}
3399
clamyea99ea12018-05-28 13:54:233400std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423401NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3402 FrameTreeNode* node,
3403 const LoadURLParams& params,
3404 bool override_user_agent,
3405 bool should_replace_current_entry,
3406 bool has_user_gesture,
Yeunjoo Choi3df791a2021-02-17 07:07:253407 blink::NavigationDownloadPolicy download_policy,
Camille Lamy5193caa2018-10-12 11:59:423408 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573409 NavigationEntryImpl* entry,
Scott Violet5ae6c42e2020-10-28 02:47:373410 FrameNavigationEntry* frame_entry,
3411 ui::PageTransition transition_type) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573412 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283413 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533414 // All renderer-initiated navigations must have an initiator_origin.
3415 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513416
Camille Lamy5193caa2018-10-12 11:59:423417 GURL url_to_load;
3418 GURL virtual_url;
Nasko Oskov03912102019-01-11 00:21:323419 base::Optional<url::Origin> origin_to_commit =
3420 frame_entry ? frame_entry->committed_origin() : base::nullopt;
3421
Camille Lamy2baa8022018-10-19 16:43:173422 // For main frames, rewrite the URL if necessary and compute the virtual URL
3423 // that should be shown in the address bar.
3424 if (node->IsMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423425 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173426 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423427 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423428
Camille Lamy2baa8022018-10-19 16:43:173429 // For DATA loads, override the virtual URL.
3430 if (params.load_type == LOAD_TYPE_DATA)
3431 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423432
Camille Lamy2baa8022018-10-19 16:43:173433 if (virtual_url.is_empty())
3434 virtual_url = url_to_load;
3435
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573436 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283437
Aran Gilman249eb122019-12-02 23:32:463438 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3439 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3440 // return different results, leading to a different URL in the
3441 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3442 // pending NavigationEntry, as we'll only make one call to
3443 // RewriteUrlForNavigation.
3444 VLOG_IF(1, (url_to_load != frame_entry->url()))
3445 << "NavigationRequest and FrameEntry have different URLs: "
3446 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283447
Camille Lamy2baa8022018-10-19 16:43:173448 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423449 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173450 // NavigationRequest.
3451 } else {
3452 url_to_load = params.url;
3453 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243454 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173455 }
Camille Lamy5193caa2018-10-12 11:59:423456
Ehsan Karamad44fc72112019-02-26 18:15:473457 if (node->render_manager()->is_attaching_inner_delegate()) {
3458 // Avoid starting any new navigations since this node is now preparing for
3459 // attaching an inner delegate.
3460 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203461 }
Camille Lamy5193caa2018-10-12 11:59:423462
Camille Lamy5193caa2018-10-12 11:59:423463 if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load))
3464 return nullptr;
3465
Kunihiko Sakamoto346a74e2021-03-10 08:57:483466 if (!DoesURLMatchOriginForNavigation(
3467 url_to_load, origin_to_commit,
3468 frame_entry->subresource_web_bundle_navigation_info())) {
Nasko Oskov03912102019-01-11 00:21:323469 DCHECK(false) << " url:" << url_to_load
3470 << " origin:" << origin_to_commit.value();
3471 return nullptr;
3472 }
3473
Camille Lamy5193caa2018-10-12 11:59:423474 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403475 blink::PreviewsState previews_state =
3476 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
Camille Lamy5193caa2018-10-12 11:59:423477 if (!node->IsMainFrame()) {
3478 // For subframes, use the state of the top-level frame.
3479 previews_state = node->frame_tree()
3480 ->root()
3481 ->current_frame_host()
3482 ->last_navigation_previews_state();
3483 }
3484
Camille Lamy5193caa2018-10-12 11:59:423485 // This will be used to set the Navigation Timing API navigationStart
3486 // parameter for browser navigations in new tabs (intents, tabs opened through
3487 // "Open link in new tab"). If the navigation must wait on the current
3488 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3489 // will be updated when the BeforeUnload ack is received.
3490 base::TimeTicks navigation_start = base::TimeTicks::Now();
3491
danakjd83d706d2020-11-25 22:11:123492 // Look for a pending commit that is to another document in this
3493 // FrameTreeNode. If one exists, then the last committed URL will not be the
3494 // current URL by the time this navigation commits.
3495 bool has_pending_cross_document_commit =
3496 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493497 bool is_currently_error_page = node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123498
danakjb952ef12021-01-14 19:58:493499 mojom::NavigationType navigation_type = GetNavigationType(
3500 /*old_url=*/node->current_url(),
3501 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3502 has_pending_cross_document_commit, is_currently_error_page,
3503 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423504
3505 // Create the NavigationParams based on |params|.
3506
3507 bool is_view_source_mode = virtual_url.SchemeIs(kViewSourceScheme);
Charlie Harrison8c113a32019-01-07 16:08:293508
3509 // Update |download_policy| if the virtual URL is view-source. Why do this
3510 // now? Possibly the URL could be rewritten to a view-source via some URL
3511 // handler.
3512 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253513 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293514
Camille Lamy5193caa2018-10-12 11:59:423515 const GURL& history_url_for_data_url =
3516 params.base_url_for_data_url.is_empty() ? GURL() : virtual_url;
Scott Violet5ae6c42e2020-10-28 02:47:373517 // Don't use |params.transition_type| as calling code may supply a different
3518 // value.
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513519 mojom::CommonNavigationParamsPtr common_params =
3520 mojom::CommonNavigationParams::New(
3521 url_to_load, params.initiator_origin,
3522 blink::mojom::Referrer::New(params.referrer.url,
3523 params.referrer.policy),
Scott Violet5ae6c42e2020-10-28 02:47:373524 transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513525 should_replace_current_entry, params.base_url_for_data_url,
3526 history_url_for_data_url, previews_state, navigation_start,
3527 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
arthursonzogniaf7c62c52020-02-12 10:49:413528 params.post_data, network::mojom::SourceLocation::New(),
3529 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273530 false /* has_text_fragment_token */,
3531 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3532 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513533 false /* is_history_navigation_in_new_child_frame */,
Charlie Hu5ffc0152019-12-06 15:59:533534 params.input_start);
Camille Lamy5193caa2018-10-12 11:59:423535
Lucas Furukawa Gadania9c45682019-07-31 22:05:143536 mojom::CommitNavigationParamsPtr commit_params =
3537 mojom::CommitNavigationParams::New(
arthursonzognid5a8d0b2021-03-11 17:36:433538 frame_entry->committed_origin(), network::mojom::WebSandboxFlags(),
3539 override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323540 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143541 std::vector<net::RedirectInfo>(),
3542 std::string() /* post_content_type */, common_params->url,
3543 common_params->method, params.can_load_local_resources,
3544 frame_entry->page_state(), entry->GetUniqueID(),
3545 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3546 -1 /* pending_history_list_offset */,
3547 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3548 params.should_clear_history_list ? 0 : GetEntryCount(),
3549 false /* was_discarded */, is_view_source_mode,
3550 params.should_clear_history_list, mojom::NavigationTiming::New(),
3551 base::nullopt /* appcache_host_id */,
3552 mojom::WasActivatedOption::kUnknown,
3553 base::UnguessableToken::Create() /* navigation_token */,
Lucas Furukawa Gadani97ea6382019-08-07 19:14:513554 std::vector<mojom::PrefetchedSignedExchangeInfoPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143555#if defined(OS_ANDROID)
3556 std::string(), /* data_url_as_string */
3557#endif
arthursonzogni14379782020-05-15 09:09:273558 !params.is_renderer_initiated, /* is_browser_initiated */
Sreeja Kamishetty46f762c2021-02-05 07:52:463559 node->frame_tree()->is_prerendering() /* is_prerendering */,
Tsuyoshi Horoe86d7702019-11-29 01:52:473560 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533561 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173562 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073563 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163564 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333565 false /* origin_agent_cluster */,
Maks Orlovichc66745a2020-06-30 17:40:023566 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213567 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Antonio Sartori1e09fd7b2020-08-10 14:16:173568 false /* is_cross_browsing_instance */,
Rakina Zata Amniba3eecb2020-11-02 10:12:283569 nullptr /* old_page_info */, -1 /* http_response_code */);
Camille Lamy5193caa2018-10-12 11:59:423570#if defined(OS_ANDROID)
3571 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143572 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423573 }
3574#endif
3575
Lucas Furukawa Gadania9c45682019-07-31 22:05:143576 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423577
3578 // A form submission may happen here if the navigation is a renderer-initiated
3579 // form submission that took the OpenURL path.
3580 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3581
3582 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3583 std::string extra_headers_crlf;
3584 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093585
3586 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143587 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083588 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453589 params.initiator_frame_token.has_value()
3590 ? &(params.initiator_frame_token.value())
3591 : nullptr,
3592 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3593 request_body,
John Delaney50425f82020-04-07 16:26:213594 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
3595 params.impression);
Yao Xiaodc5ed102019-06-04 19:19:093596 navigation_request->set_from_download_cross_origin_redirect(
3597 params.from_download_cross_origin_redirect);
3598 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423599}
3600
3601std::unique_ptr<NavigationRequest>
3602NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233603 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573604 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233605 FrameNavigationEntry* frame_entry,
3606 ReloadType reload_type,
3607 bool is_same_document_history_load,
Arthur Hemerybee4a752019-05-29 10:50:553608 bool is_history_navigation_in_new_child_frame) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343609 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233610 GURL dest_url = frame_entry->url();
Nasko Oskov03912102019-01-11 00:21:323611 base::Optional<url::Origin> origin_to_commit =
3612 frame_entry->committed_origin();
3613
clamyea99ea12018-05-28 13:54:233614 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013615 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573616 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233617 // We may have been redirected when navigating to the current URL.
3618 // Use the URL the user originally intended to visit as signaled by the
3619 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013620 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573621 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233622 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323623 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233624 }
3625
Ehsan Karamad44fc72112019-02-26 18:15:473626 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3627 // Avoid starting any new navigations since this node is now preparing for
3628 // attaching an inner delegate.
3629 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203630 }
3631
Camille Lamy5193caa2018-10-12 11:59:423632 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573633 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233634 return nullptr;
3635 }
3636
Kunihiko Sakamoto346a74e2021-03-10 08:57:483637 if (!DoesURLMatchOriginForNavigation(
3638 dest_url, origin_to_commit,
3639 frame_entry->subresource_web_bundle_navigation_info())) {
3640 DCHECK(false) << " url:" << dest_url
3641 << " origin:" << origin_to_commit.value();
Nasko Oskov03912102019-01-11 00:21:323642 return nullptr;
3643 }
3644
clamyea99ea12018-05-28 13:54:233645 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403646 blink::PreviewsState previews_state =
3647 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
clamyea99ea12018-05-28 13:54:233648 if (!frame_tree_node->IsMainFrame()) {
3649 // For subframes, use the state of the top-level frame.
3650 previews_state = frame_tree_node->frame_tree()
3651 ->root()
3652 ->current_frame_host()
3653 ->last_navigation_previews_state();
clamyea99ea12018-05-28 13:54:233654 }
3655
clamyea99ea12018-05-28 13:54:233656 // This will be used to set the Navigation Timing API navigationStart
3657 // parameter for browser navigations in new tabs (intents, tabs opened through
3658 // "Open link in new tab"). If the navigation must wait on the current
3659 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3660 // will be updated when the BeforeUnload ack is received.
3661 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233662
danakjd83d706d2020-11-25 22:11:123663 // Look for a pending commit that is to another document in this
3664 // FrameTreeNode. If one exists, then the last committed URL will not be the
3665 // current URL by the time this navigation commits.
3666 bool has_pending_cross_document_commit =
3667 frame_tree_node->render_manager()
3668 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493669 bool is_currently_error_page =
3670 frame_tree_node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123671
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513672 mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123673 /*old_url=*/frame_tree_node->current_url(),
3674 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493675 has_pending_cross_document_commit, is_currently_error_page,
3676 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423677
3678 // A form submission may happen here if the navigation is a
3679 // back/forward/reload navigation that does a form resubmission.
3680 scoped_refptr<network::ResourceRequestBody> request_body;
3681 std::string post_content_type;
3682 if (frame_entry->method() == "POST") {
3683 request_body = frame_entry->GetPostData(&post_content_type);
3684 // Might have a LF at end.
3685 post_content_type =
3686 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL)
3687 .as_string();
3688 }
3689
3690 // Create the NavigationParams based on |entry| and |frame_entry|.
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513691 mojom::CommonNavigationParamsPtr common_params =
3692 entry->ConstructCommonNavigationParams(
3693 *frame_entry, request_body, dest_url,
3694 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
3695 navigation_type, previews_state, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533696 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513697 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553698 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423699
3700 // TODO(clamy): |intended_as_new_entry| below should always be false once
3701 // Reload no longer leads to this being called for a pending NavigationEntry
3702 // of index -1.
Lucas Furukawa Gadania9c45682019-07-31 22:05:143703 mojom::CommitNavigationParamsPtr commit_params =
3704 entry->ConstructCommitNavigationParams(
3705 *frame_entry, common_params->url, origin_to_commit,
3706 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3707 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533708 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3709 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143710 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423711
clamyea99ea12018-05-28 13:54:233712 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143713 frame_tree_node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083714 !entry->is_renderer_initiated(), false /* was_opener_suppressed */,
3715 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453716 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3717 entry->extra_headers(), frame_entry, entry, request_body,
3718 nullptr /* navigation_ui_data */, base::nullopt /* impression */);
clamyea99ea12018-05-28 13:54:233719}
3720
[email protected]d202a7c2012-01-04 07:53:473721void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213722 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273723 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403724
[email protected]2db9bd72012-04-13 20:20:563725 // We need to notify the ssl_manager_ before the web_contents_ so the
[email protected]df1af242009-05-01 00:11:403726 // location bar will have up-to-date information about the security style
3727 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133728 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403729
[email protected]7f924832014-08-09 05:57:223730 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:573731 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463732
[email protected]b0f724c2013-09-05 04:21:133733 // TODO(avi): Remove. http://crbug.com/170921
3734 NotificationDetails notification_details =
3735 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153736 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3737 Source<NavigationController>(this),
3738 notification_details);
initial.commit09911bf2008-07-26 23:55:293739}
3740
initial.commit09911bf2008-07-26 23:55:293741// static
[email protected]d202a7c2012-01-04 07:53:473742size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233743 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153744 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:213745 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233746}
3747
[email protected]d202a7c2012-01-04 07:53:473748void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223749 if (is_active && needs_reload_)
3750 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293751}
3752
[email protected]d202a7c2012-01-04 07:53:473753void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293754 if (!needs_reload_)
3755 return;
3756
Bo Liucdfa4b12018-11-06 00:21:443757 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3758 needs_reload_type_);
3759
initial.commit09911bf2008-07-26 23:55:293760 // Calling Reload() results in ignoring state, and not loading.
3761 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3762 // cached state.
avicc872d7242015-08-19 21:26:343763 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163764 NavigateToExistingPendingEntry(ReloadType::NONE,
3765 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343766 } else if (last_committed_entry_index_ != -1) {
arthursonzogni5c4c202d2017-04-25 23:41:273767 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343768 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163769 NavigateToExistingPendingEntry(ReloadType::NONE,
3770 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343771 } else {
3772 // If there is something to reload, the successful reload will clear the
3773 // |needs_reload_| flag. Otherwise, just do it here.
3774 needs_reload_ = false;
3775 }
initial.commit09911bf2008-07-26 23:55:293776}
3777
Carlos IL42b416592019-10-07 23:10:363778void NavigationControllerImpl::LoadPostCommitErrorPage(
3779 RenderFrameHost* render_frame_host,
3780 const GURL& url,
3781 const std::string& error_page_html,
3782 net::Error error) {
Rakina Zata Amni919b7922020-12-11 09:03:133783 RenderFrameHostImpl* rfhi =
3784 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:583785
3786 // Only active documents can load post-commit error pages:
3787 // - If the document is in pending deletion, the browser already committed to
3788 // destroying this RenderFrameHost so ignore loading the error page.
3789 // - If the document is in back-forward cache, it's not allowed to navigate as
3790 // it should remain frozen. Ignore the request and evict the document from
3791 // back-forward cache.
3792 // - If the document is prerendering, it can navigate but when loading error
3793 // pages, cancel prerendering.
3794 if (rfhi->IsInactiveAndDisallowActivation())
3795 return;
3796
Rakina Zata Amni919b7922020-12-11 09:03:133797 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:413798
3799 mojom::CommonNavigationParamsPtr common_params =
3800 CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:593801 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
3802 // the frame actually committed (e.g. iframe with "src" set to a
3803 // slow-responding URL). We should rewrite the URL to about:blank in this
3804 // case, as the renderer will only think a page is an error page if it has a
3805 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:133806 common_params->url = url.is_empty() ? GURL("about:blank") : url;
John Delaney131ad362019-08-08 21:57:413807 mojom::CommitNavigationParamsPtr commit_params =
3808 CreateCommitNavigationParams();
3809
arthursonzogni70ac7302020-05-28 08:49:053810 // Error pages have a fully permissive FramePolicy.
3811 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
3812 // error pages.
3813 commit_params->frame_policy = blink::FramePolicy();
3814
John Delaney131ad362019-08-08 21:57:413815 std::unique_ptr<NavigationRequest> navigation_request =
3816 NavigationRequest::CreateBrowserInitiated(
3817 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083818 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:193819 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453820 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:063821 "" /* extra_headers */, nullptr /* frame_entry */,
3822 nullptr /* entry */, nullptr /* post_body */,
3823 nullptr /* navigation_ui_data */, base::nullopt /* impression */);
Carlos IL42b416592019-10-07 23:10:363824 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:413825 navigation_request->set_net_error(error);
3826 node->CreatedNavigationRequest(std::move(navigation_request));
3827 DCHECK(node->navigation_request());
3828 node->navigation_request()->BeginNavigation();
3829}
3830
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573831void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213832 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093833 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:153834 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143835 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293836}
3837
[email protected]d202a7c2012-01-04 07:53:473838void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363839 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553840 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363841 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293842
initial.commit09911bf2008-07-26 23:55:293843 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293844}
[email protected]765b35502008-08-21 00:51:203845
arthursonzogni69a6a1b2019-09-17 09:23:003846void NavigationControllerImpl::DiscardNonCommittedEntries() {
Michael Thiessen9b14d512019-09-23 21:19:473847 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:103848 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
3849 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:293850 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:473851 return;
Michael Thiessenc5676d22019-09-25 22:32:103852 }
avi45a72532015-04-07 21:01:453853 DiscardPendingEntry(false);
arthursonzogni69a6a1b2019-09-17 09:23:003854 if (delegate_)
3855 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:483856}
3857
avi7c6f35e2015-05-08 17:52:383858int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3859 int nav_entry_id) const {
3860 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3861 if (entries_[i]->GetUniqueID() == nav_entry_id)
3862 return i;
3863 }
3864 return -1;
3865}
3866
[email protected]d202a7c2012-01-04 07:53:473867void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573868 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253869 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573870 DCHECK_LE(max_index, source->GetEntryCount());
[email protected]e1cd5452010-08-26 18:03:253871 for (int i = 0; i < max_index; i++) {
Nasko Oskov923ca482020-12-04 21:20:463872 // TODO(creis): Once we start sharing FrameNavigationEntries between
3873 // NavigationEntries, it will not be safe to share them with another tab.
3874 // Must have a version of Clone that recreates them.
3875 entries_.insert(entries_.begin() + i, source->entries_[i]->Clone());
[email protected]e1cd5452010-08-26 18:03:253876 }
arthursonzogni5c4c202d2017-04-25 23:41:273877 DCHECK(pending_entry_index_ == -1 ||
3878 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:253879}
[email protected]c5b88d82012-10-06 17:03:333880
3881void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:183882 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:333883 get_timestamp_callback_ = get_timestamp_callback;
3884}
[email protected]8ff00d72012-10-23 19:12:213885
Shivani Sharmaffb32b82019-04-09 16:58:473886// History manipulation intervention:
3887void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:473888 bool replace_entry,
3889 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:403890 bool is_renderer_initiated,
3891 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:453892 // Note that for a subframe, previous_document_was_activated is true if the
3893 // gesture happened in any subframe (propagated to main frame) or in the main
3894 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:473895 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:273896 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:473897 if (last_committed_entry_index_ != -1) {
shivanigithube92c33da2020-09-14 13:01:413898 // This histogram always counts when navigating away from an entry,
3899 // irrespective of whether the skippable flag was changed or not, and
3900 // whether this entry is being reused or not.
Shivani Sharmaffb32b82019-04-09 16:58:473901 UMA_HISTOGRAM_BOOLEAN(
shivanigithube92c33da2020-09-14 13:01:413902 "Navigation.BackForward.SetShouldSkipOnBackForwardUI",
3903 GetEntryAtIndex(last_committed_entry_index_)
3904 ->should_skip_on_back_forward_ui());
Shivani Sharmaffb32b82019-04-09 16:58:473905 }
3906 return;
3907 }
3908 if (last_committed_entry_index_ == -1)
3909 return;
3910
Shivani Sharmac4cc8922019-04-18 03:11:173911 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473912 UMA_HISTOGRAM_BOOLEAN("Navigation.BackForward.SetShouldSkipOnBackForwardUI",
3913 true);
3914
Alexander Timine3ec4192020-04-20 16:39:403915 // Log UKM with the URL we are navigating away from.
3916 ukm::builders::HistoryManipulationIntervention(
3917 previous_page_load_ukm_source_id)
3918 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:473919}
3920
Shivani Sharmac4cc8922019-04-18 03:11:173921void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
3922 int reference_index,
3923 bool skippable) {
3924 auto* reference_entry = GetEntryAtIndex(reference_index);
3925 reference_entry->set_should_skip_on_back_forward_ui(skippable);
3926
3927 int64_t document_sequence_number =
3928 reference_entry->root_node()->frame_entry->document_sequence_number();
3929 for (int index = 0; index < GetEntryCount(); index++) {
3930 auto* entry = GetEntryAtIndex(index);
3931 if (entry->root_node()->frame_entry->document_sequence_number() ==
3932 document_sequence_number) {
3933 entry->set_should_skip_on_back_forward_ui(skippable);
3934 }
3935 }
3936}
3937
arthursonzogni66f711c2019-10-08 14:40:363938std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
3939NavigationControllerImpl::ReferencePendingEntry() {
3940 DCHECK(pending_entry_);
3941 auto pending_entry_ref =
3942 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
3943 pending_entry_refs_.insert(pending_entry_ref.get());
3944 return pending_entry_ref;
3945}
3946
3947void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
3948 // Ignore refs that don't correspond to the current pending entry.
3949 auto it = pending_entry_refs_.find(ref);
3950 if (it == pending_entry_refs_.end())
3951 return;
3952 pending_entry_refs_.erase(it);
3953
3954 if (!pending_entry_refs_.empty())
3955 return;
3956
3957 // The pending entry may be deleted before the last PendingEntryRef.
3958 if (!pending_entry_)
3959 return;
3960
3961 // We usually clear the pending entry when the matching NavigationRequest
3962 // fails, so that an arbitrary URL isn't left visible above a committed page.
3963 //
3964 // However, we do preserve the pending entry in some cases, such as on the
3965 // initial navigation of an unmodified blank tab. We also allow the delegate
3966 // to say when it's safe to leave aborted URLs in the omnibox, to let the
3967 // user edit the URL and try again. This may be useful in cases that the
3968 // committed page cannot be attacker-controlled. In these cases, we still
3969 // allow the view to clear the pending entry and typed URL if the user
3970 // requests (e.g., hitting Escape with focus in the address bar).
3971 //
3972 // Do not leave the pending entry visible if it has an invalid URL, since this
3973 // might be formatted in an unexpected or unsafe way.
3974 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:363975 bool should_preserve_entry =
3976 (pending_entry_ == GetVisibleEntry()) &&
3977 pending_entry_->GetURL().is_valid() &&
3978 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
3979 if (should_preserve_entry)
3980 return;
3981
3982 DiscardPendingEntry(true);
3983 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3984}
3985
Titouan Rigoudy6ec70402021-02-02 15:42:193986std::unique_ptr<PolicyContainerPolicies>
3987NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:393988 RenderFrameHostImpl* rfh,
3989 bool is_same_document,
3990 NavigationRequest* request) {
Titouan Rigoudy6ec70402021-02-02 15:42:193991 if (!ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(request))
Antonio Sartori78a749f2020-11-30 12:03:393992 return nullptr;
3993
3994 if (is_same_document) {
Charlie Reis73e356242021-04-02 17:10:313995 // TODO(https://crbug.com/524208): Remove this nullptr check when we can
3996 // ensure we always have a FrameNavigationEntry here.
3997 if (!GetLastCommittedEntry())
3998 return nullptr;
3999
Antonio Sartori78a749f2020-11-30 12:03:394000 FrameNavigationEntry* previous_frame_entry =
4001 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
4002
4003 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
4004 // ensure we always have a FrameNavigationEntry here.
4005 if (!previous_frame_entry)
4006 return nullptr;
4007
Titouan Rigoudy6ec70402021-02-02 15:42:194008 const PolicyContainerPolicies* previous_policies =
4009 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:394010
Titouan Rigoudy6ec70402021-02-02 15:42:194011 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:394012 return nullptr;
4013
4014 // Make a copy of the policy container for the new FrameNavigationEntry.
Antonio Sartori5d09b30f2021-03-02 09:27:164015 return previous_policies->Clone();
Antonio Sartori78a749f2020-11-30 12:03:394016 }
4017
4018 if (!request->IsWaitingToCommit()) {
4019 // This is the initial, "fake" navigation to about:blank. The
4020 // NavigationRequest contains a dummy policy container, while the
4021 // RenderFrameHost already inherited the policy container from the
4022 // creator, so let's take the policies from there.
Antonio Sartori5d09b30f2021-03-02 09:27:164023 return rfh->policy_container_host()->policies().Clone();
Antonio Sartori78a749f2020-11-30 12:03:394024 }
4025
4026 // Take the policy container from the request since we did not move it
4027 // into the RFH yet.
Antonio Sartori5d09b30f2021-03-02 09:27:164028 return request->GetPolicyContainerPolicies().Clone();
Antonio Sartori78a749f2020-11-30 12:03:394029}
4030
Carlos Caballeroede6f8c2021-01-28 11:01:504031void NavigationControllerImpl::SetHistoryOffsetAndLength(int history_offset,
4032 int history_length) {
4033 OPTIONAL_TRACE_EVENT2(
4034 "content", "NavigationControllerImpl::SetHistoryOffsetAndLength",
4035 "history_offset", history_offset, "history_length", history_length);
4036
4037 auto callback = base::BindRepeating(
4038 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
4039 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
4040 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
4041 }
4042 },
4043 history_offset, history_length);
4044 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
4045}
4046
4047void NavigationControllerImpl::DidAccessInitialMainDocument() {
4048 // We may have left a failed browser-initiated navigation in the address bar
4049 // to let the user edit it and try again. Clear it now that content might
4050 // show up underneath it.
4051 if (!frame_tree_.IsLoading() && GetPendingEntry())
4052 DiscardPendingEntry(false);
4053
4054 // Update the URL display.
4055 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
4056}
4057
4058void NavigationControllerImpl::UpdateStateForFrame(
4059 RenderFrameHostImpl* rfhi,
4060 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:564061 OPTIONAL_TRACE_EVENT1("content",
4062 "NavigationControllerImpl::UpdateStateForFrame",
4063 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:504064 // The state update affects the last NavigationEntry associated with the given
4065 // |render_frame_host|. This may not be the last committed NavigationEntry (as
4066 // in the case of an UpdateState from a frame being swapped out). We track
4067 // which entry this is in the RenderFrameHost's nav_entry_id.
4068 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
4069 if (!entry)
4070 return;
4071
4072 FrameNavigationEntry* frame_entry =
4073 entry->GetFrameEntry(rfhi->frame_tree_node());
4074 if (!frame_entry)
4075 return;
4076
4077 // The SiteInstance might not match if we do a cross-process navigation with
4078 // replacement (e.g., auto-subframe), in which case the swap out of the old
4079 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4080 // has already been replaced and destroyed.
4081 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4082 return;
4083
4084 if (page_state == frame_entry->page_state())
4085 return; // Nothing to update.
4086
4087 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4088
4089 // The document_sequence_number and item_sequence_number recorded in the
4090 // FrameNavigationEntry should not differ from the one coming with the update,
4091 // since it must come from the same document. Do not update it if a difference
4092 // is detected, as this indicates that |frame_entry| is not the correct one.
4093 blink::ExplodedPageState exploded_state;
4094 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4095 return;
4096
4097 if (exploded_state.top.document_sequence_number !=
4098 frame_entry->document_sequence_number() ||
4099 exploded_state.top.item_sequence_number !=
4100 frame_entry->item_sequence_number()) {
4101 return;
4102 }
4103
4104 frame_entry->SetPageState(page_state);
4105 NotifyEntryChanged(entry);
4106}
4107
Aaron Colwellb731a0ae2021-03-19 19:14:474108void NavigationControllerImpl::OnStoragePartitionIdAdded(
4109 const StoragePartitionId& partition_id) {
4110 auto it = partition_config_to_id_map_.insert(
4111 std::make_pair(partition_id.config(), partition_id));
4112 bool successful_insert = it.second;
4113 DCHECK(successful_insert);
4114 if (!successful_insert) {
4115 LogStoragePartitionIdCrashKeys(it.first->second, partition_id);
4116 }
4117}
4118
4119void NavigationControllerImpl::LogStoragePartitionIdCrashKeys(
4120 const StoragePartitionId& original_partition_id,
4121 const StoragePartitionId& new_partition_id) {
4122 base::debug::SetCrashKeyString(
4123 base::debug::AllocateCrashKeyString("original_partition_id",
4124 base::debug::CrashKeySize::Size256),
4125 original_partition_id.ToString());
4126
4127 base::debug::SetCrashKeyString(
4128 base::debug::AllocateCrashKeyString("new_partition_id",
4129 base::debug::CrashKeySize::Size256),
4130 new_partition_id.ToString());
4131
4132 base::debug::DumpWithoutCrashing();
4133}
4134
[email protected]8ff00d72012-10-23 19:12:214135} // namespace content