blob: ab290c0b81a2df114a8c6e722a4fb109260b2137 [file] [log] [blame]
[email protected]d4a8ca482013-10-30 21:06:401// Copyright 2013 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
creis4e2ecb72015-06-20 00:46:305/*
6 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
7 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved.
9 * (http://www.torchmobile.com/)
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
21 * its contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
25 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
28 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
danakjc492bf82020-09-09 20:02:4436#include "content/browser/renderer_host/navigation_controller_impl.h"
initial.commit09911bf2008-07-26 23:55:2937
Lei Zhang96031532019-10-10 19:05:4738#include <algorithm>
dcheng36b6aec92015-12-26 06:16:3639#include <utility>
40
[email protected]c5b88d82012-10-06 17:03:3341#include "base/bind.h"
[email protected]eabfe1912014-05-12 10:07:2842#include "base/command_line.h"
initial.commit09911bf2008-07-26 23:55:2943#include "base/logging.h"
asvitkine30330812016-08-30 04:01:0844#include "base/metrics/histogram_macros.h"
Chris Hamilton83272dc2021-02-23 00:24:0245#include "base/stl_util.h"
Peter Kastingb53b81912021-04-28 19:23:3046#include "base/strings/string_piece.h"
[email protected]348fbaac2013-06-11 06:31:5147#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0048#include "base/strings/utf_string_conversions.h"
[email protected]a43858f2013-06-28 15:18:3749#include "base/time/time.h"
Carlos Caballero40b0efd2021-01-26 11:55:0050#include "base/trace_event/optional_trace_event.h"
Carlos Caballeroede6f8c2021-01-28 11:01:5051#include "base/trace_event/trace_conversion_helper.h"
ssid3e765612015-01-28 04:03:4252#include "base/trace_event/trace_event.h"
servolkf3955532015-05-16 00:01:5953#include "build/build_config.h"
[email protected]eabfe1912014-05-12 10:07:2854#include "cc/base/switches.h"
jamescookda2505812015-03-20 18:01:1855#include "content/browser/bad_message.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3956#include "content/browser/blob_storage/chrome_blob_storage_context.h"
[email protected]825b1662012-03-12 19:07:3157#include "content/browser/browser_url_handler_impl.h"
[email protected]5f2aa722013-08-07 16:59:4158#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0459#include "content/browser/dom_storage/session_storage_namespace_impl.h"
danakjc492bf82020-09-09 20:02:4460#include "content/browser/renderer_host/debug_urls.h"
61#include "content/browser/renderer_host/frame_tree.h"
Carlos Caballero40b0efd2021-01-26 11:55:0062#include "content/browser/renderer_host/frame_tree_node.h"
danakjc492bf82020-09-09 20:02:4463#include "content/browser/renderer_host/navigation_entry_impl.h"
Nate Chapin214a86a2021-06-21 20:35:5764#include "content/browser/renderer_host/navigation_entry_restore_context_impl.h"
danakjc492bf82020-09-09 20:02:4465#include "content/browser/renderer_host/navigation_request.h"
66#include "content/browser/renderer_host/navigator.h"
67#include "content/browser/renderer_host/render_frame_host_delegate.h"
Carlos Caballero40b0efd2021-01-26 11:55:0068#include "content/browser/renderer_host/render_view_host_impl.h"
Sharon Yangd70a5392021-10-26 23:06:3269#include "content/browser/site_info.h"
[email protected]b6583592012-01-25 19:52:3370#include "content/browser/site_instance_impl.h"
Kunihiko Sakamoto346a74e2021-03-10 08:57:4871#include "content/browser/web_package/subresource_web_bundle_navigation_info.h"
Tsuyoshi Horo37493af2019-11-21 23:43:1472#include "content/browser/web_package/web_bundle_navigation_info.h"
Camille Lamy5193caa2018-10-12 11:59:4273#include "content/common/content_constants_internal.h"
Nate Chapind1fe3612021-04-16 20:45:5774#include "content/common/navigation_params_utils.h"
Nasko Oskovae49e292020-08-13 02:08:5175#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1176#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1977#include "content/public/browser/content_browser_client.h"
Fergal Daly1336ac642021-09-14 15:13:1178#include "content/public/browser/disallow_activation_reason.h"
[email protected]d9083482012-01-06 00:38:4679#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0080#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3881#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1682#include "content/public/browser/notification_types.h"
Lei Zhang96031532019-10-10 19:05:4783#include "content/public/browser/render_view_host.h"
[email protected]9677a3c2012-12-22 04:18:5884#include "content/public/browser/render_widget_host.h"
85#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1086#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3487#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:1988#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:3889#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:1090#include "content/public/common/content_features.h"
Lei Zhang96031532019-10-10 19:05:4791#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4392#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:5993#include "media/base/mime_util.h"
[email protected]a23de8572009-06-03 02:16:3294#include "net/base/escape.h"
Arthur Sonzogni620cec62018-12-13 13:08:5795#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:1896#include "services/metrics/public/cpp/ukm_builders.h"
97#include "services/metrics/public/cpp/ukm_recorder.h"
Yue Ru Sun128804932020-09-30 22:19:1798#include "services/metrics/public/cpp/ukm_source_id.h"
Nan Lind91c8152021-10-21 16:22:3799#include "services/network/public/mojom/fetch_api.mojom.h"
[email protected]9677a3c2012-12-22 04:18:58100#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:39101#include "third_party/blink/public/common/blob/blob_utils.h"
Gyuyoung Kim107c2a02021-04-13 01:49:30102#include "third_party/blink/public/common/chrome_debug_urls.h"
Miyoung Shin1c565c912021-03-17 12:11:21103#include "third_party/blink/public/common/history/session_history_constants.h"
Blink Reformata30d4232018-04-07 15:31:06104#include "third_party/blink/public/common/mime_util/mime_util.h"
Minggang Wanga13c796e2021-07-02 05:54:43105#include "third_party/blink/public/common/navigation/navigation_params.h"
Carlos Caballeroede6f8c2021-01-28 11:01:50106#include "third_party/blink/public/common/page_state/page_state_serialization.h"
Minggang Wangb9f3fa92021-07-01 15:30:31107#include "third_party/blink/public/mojom/navigation/navigation_params.mojom.h"
Minggang Wang7ee0c742021-06-16 16:16:51108#include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h"
[email protected]cca6f392014-05-28 21:32:26109#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:29110
[email protected]8ff00d72012-10-23 19:12:21111namespace content {
[email protected]e9ba4472008-09-14 15:42:43112namespace {
113
114// Invoked when entries have been pruned, or removed. For example, if the
115// current entries are [google, digg, yahoo], with the current entry google,
116// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47117void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50118 int index,
[email protected]c12bf1a12008-09-17 16:28:49119 int count) {
[email protected]8ff00d72012-10-23 19:12:21120 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50121 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49122 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14123 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43124}
125
[email protected]e9ba4472008-09-14 15:42:43126// Configure all the NavigationEntries in entries for restore. This resets
127// the transition type to reload and makes sure the content state isn't empty.
128void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57129 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48130 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47131 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43132 // Use a transition type of reload so that we don't incorrectly increase
133 // the typed count.
Lei Zhang96031532019-10-10 19:05:47134 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
135 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43136 }
137}
138
[email protected]bf70edce2012-06-20 22:32:22139// Determines whether or not we should be carrying over a user agent override
140// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57141bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22142 return last_entry && last_entry->GetIsOverridingUserAgent();
143}
144
Camille Lamy5193caa2018-10-12 11:59:42145// Determines whether to override user agent for a navigation.
146bool ShouldOverrideUserAgent(
147 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57148 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42149 switch (override_user_agent) {
150 case NavigationController::UA_OVERRIDE_INHERIT:
151 return ShouldKeepOverride(last_committed_entry);
152 case NavigationController::UA_OVERRIDE_TRUE:
153 return true;
154 case NavigationController::UA_OVERRIDE_FALSE:
155 return false;
Camille Lamy5193caa2018-10-12 11:59:42156 }
157 NOTREACHED();
158 return false;
159}
160
Rakina Zata Amni312822d72021-06-04 16:13:37161// Returns true if this navigation should be treated as a reload. For e.g.
clamy0a656e42018-02-06 18:18:28162// navigating to the last committed url via the address bar or clicking on a
Rakina Zata Amni312822d72021-06-04 16:13:37163// link which results in a navigation to the last committed URL (but wasn't
164// converted to do a replacement navigation in the renderer), etc.
Fergal Daly766177d2020-07-07 07:54:04165// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
166// |base_url_for_data_url|, |transition_type| correspond to the new navigation
167// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
168// navigation that committed.
169bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
170 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57171 const GURL& virtual_url,
172 const GURL& base_url_for_data_url,
173 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57174 bool is_post,
Rakina Zata Amni312822d72021-06-04 16:13:37175 bool should_replace_current_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57176 NavigationEntryImpl* last_committed_entry) {
Rakina Zata Amni312822d72021-06-04 16:13:37177 // Navigations intended to do a replacement shouldn't be converted to do a
178 // reload.
179 if (should_replace_current_entry)
clamy0a656e42018-02-06 18:18:28180 return false;
clamy0a656e42018-02-06 18:18:28181 // Only convert to reload if at least one navigation committed.
182 if (!last_committed_entry)
ananta3bdd8ae2016-12-22 17:11:55183 return false;
184
arthursonzogni7a8243682017-12-14 16:41:42185 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28186 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42187 return false;
188
ananta3bdd8ae2016-12-22 17:11:55189 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
190 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
191 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28192 bool transition_type_can_be_converted = false;
193 if (ui::PageTransitionCoreTypeIs(transition_type,
194 ui::PAGE_TRANSITION_RELOAD) &&
195 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
196 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55197 }
clamy0a656e42018-02-06 18:18:28198 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55199 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28200 transition_type_can_be_converted = true;
201 }
202 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
203 transition_type_can_be_converted = true;
204 if (!transition_type_can_be_converted)
205 return false;
206
207 // This check is required for cases like view-source:, etc. Here the URL of
208 // the navigation entry would contain the url of the page, while the virtual
209 // URL contains the full URL including the view-source prefix.
210 if (virtual_url != last_committed_entry->GetVirtualURL())
211 return false;
212
Fergal Daly766177d2020-07-07 07:54:04213 // Check that the URLs match.
214 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
215 // If there's no frame entry then by definition the URLs don't match.
216 if (!frame_entry)
217 return false;
218
219 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28220 return false;
221
222 // This check is required for Android WebView loadDataWithBaseURL. Apps
223 // can pass in anything in the base URL and we need to ensure that these
224 // match before classifying it as a reload.
225 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
226 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
227 return false;
ananta3bdd8ae2016-12-22 17:11:55228 }
229
clamy0a656e42018-02-06 18:18:28230 // Skip entries with SSL errors.
231 if (last_committed_entry->ssl_error())
232 return false;
233
234 // Don't convert to a reload when the last navigation was a POST or the new
235 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04236 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28237 return false;
238
239 return true;
ananta3bdd8ae2016-12-22 17:11:55240}
241
Nasko Oskov03912102019-01-11 00:21:32242bool DoesURLMatchOriginForNavigation(
243 const GURL& url,
Anton Bikineevf62d1bf2021-05-15 17:56:07244 const absl::optional<url::Origin>& origin,
Kunihiko Sakamoto346a74e2021-03-10 08:57:48245 SubresourceWebBundleNavigationInfo*
246 subresource_web_bundle_navigation_info) {
Nasko Oskov03912102019-01-11 00:21:32247 // If there is no origin supplied there is nothing to match. This can happen
248 // for navigations to a pending entry and therefore it should be allowed.
249 if (!origin)
250 return true;
251
Kunihiko Sakamoto260ee0a2021-10-15 01:44:08252 // Urn: and uuid-in-package: subframe from WebBundle has an opaque origin
253 // derived from the Bundle's origin.
254 // TODO(https://crbug.com/1257045): Remove urn: scheme support.
255 if ((url.SchemeIs(url::kUrnScheme) ||
256 url.SchemeIs(url::kUuidInPackageScheme)) &&
257 subresource_web_bundle_navigation_info) {
Kunihiko Sakamoto346a74e2021-03-10 08:57:48258 return origin->CanBeDerivedFrom(
259 subresource_web_bundle_navigation_info->bundle_url());
260 }
261
Nasko Oskov03912102019-01-11 00:21:32262 return origin->CanBeDerivedFrom(url);
263}
264
Anton Bikineevf62d1bf2021-05-15 17:56:07265absl::optional<url::Origin> GetCommittedOriginForFrameEntry(
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12266 const mojom::DidCommitProvisionalLoadParams& params,
267 NavigationRequest* request) {
Nasko Oskov03912102019-01-11 00:21:32268 // Error pages commit in an opaque origin, yet have the real URL that resulted
269 // in an error as the |params.url|. Since successful reload of an error page
270 // should commit in the correct origin, setting the opaque origin on the
271 // FrameNavigationEntry will be incorrect.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12272 if (request->DidEncounterError())
Anton Bikineevf62d1bf2021-05-15 17:56:07273 return absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:32274
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12275 // We also currently don't save committed origins for loadDataWithBaseURL
276 // navigations (probably accidentally). Without this check, navigations to
277 // the FrameNavigationEntry might fail the DoesURLMatchOriginForNavigation()
278 // check since the origin will be based on the base URL instead of the data:
279 // URL used for the navigation.
280 // TODO(https://crbug.com/1198406): Save committed origin in
281 // FrameNavigationEntry for this case too.
Rakina Zata Amni8a1c6b32021-09-29 06:41:29282 if (request->IsLoadDataWithBaseURL())
Anton Bikineevf62d1bf2021-05-15 17:56:07283 return absl::nullopt;
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12284
Anton Bikineevf62d1bf2021-05-15 17:56:07285 return absl::make_optional(params.origin);
Nasko Oskov03912102019-01-11 00:21:32286}
287
Camille Lamy5193caa2018-10-12 11:59:42288bool IsValidURLForNavigation(bool is_main_frame,
289 const GURL& virtual_url,
290 const GURL& dest_url) {
291 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
292 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
293 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
294 << virtual_url.possibly_invalid_spec();
295 return false;
296 }
297
298 // Don't attempt to navigate to non-empty invalid URLs.
299 if (!dest_url.is_valid() && !dest_url.is_empty()) {
300 LOG(WARNING) << "Refusing to load invalid URL: "
301 << dest_url.possibly_invalid_spec();
302 return false;
303 }
304
305 // The renderer will reject IPC messages with URLs longer than
306 // this limit, so don't attempt to navigate with a longer URL.
307 if (dest_url.spec().size() > url::kMaxURLChars) {
308 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
309 << " characters.";
310 return false;
311 }
312
Aaron Colwell33109c592020-04-21 21:31:19313 // Reject renderer debug URLs because they should have been handled before
314 // we get to this point. This check handles renderer debug URLs
315 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
316 // provides defense-in-depth if a renderer debug URL manages to get here via
317 // some other path. We want to reject the navigation here so it doesn't
318 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30319 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19320 LOG(WARNING) << "Refusing to load renderer debug URL: "
321 << dest_url.possibly_invalid_spec();
322 return false;
323 }
324
Camille Lamy5193caa2018-10-12 11:59:42325 return true;
326}
327
Mikel Astizba9cf2fd2017-12-17 10:38:10328// See replaced_navigation_entry_data.h for details: this information is meant
329// to ensure |*output_entry| keeps track of its original URL (landing page in
330// case of server redirects) as it gets replaced (e.g. history.replaceState()),
331// without overwriting it later, for main frames.
332void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57333 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10334 NavigationEntryImpl* output_entry) {
335 if (output_entry->GetReplacedEntryData().has_value())
336 return;
337
338 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57339 data.first_committed_url = replaced_entry->GetURL();
340 data.first_timestamp = replaced_entry->GetTimestamp();
341 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29342 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10343}
344
Minggang Wangb9f3fa92021-07-01 15:30:31345blink::mojom::NavigationType GetNavigationType(
346 const GURL& old_url,
347 const GURL& new_url,
348 ReloadType reload_type,
349 NavigationEntryImpl* entry,
350 const FrameNavigationEntry& frame_entry,
351 bool has_pending_cross_document_commit,
352 bool is_currently_error_page,
353 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23354 // Reload navigations
355 switch (reload_type) {
356 case ReloadType::NORMAL:
Minggang Wangb9f3fa92021-07-01 15:30:31357 return blink::mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23358 case ReloadType::BYPASSING_CACHE:
Minggang Wangb9f3fa92021-07-01 15:30:31359 return blink::mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23360 case ReloadType::ORIGINAL_REQUEST_URL:
Minggang Wangb9f3fa92021-07-01 15:30:31361 return blink::mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23362 case ReloadType::NONE:
363 break; // Fall through to rest of function.
364 }
365
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08366 if (entry->IsRestored()) {
Minggang Wangb9f3fa92021-07-01 15:30:31367 return entry->GetHasPostData()
368 ? blink::mojom::NavigationType::RESTORE_WITH_POST
369 : blink::mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23370 }
371
danakjb952ef12021-01-14 19:58:49372 const bool can_be_same_document =
373 // A pending cross-document commit means this navigation will not occur in
374 // the current document, as that document would end up being replaced in
375 // the meantime.
376 !has_pending_cross_document_commit &&
377 // If the current document is an error page, we should always treat it as
378 // a different-document navigation so that we'll attempt to load the
379 // document we're navigating to (and not stay in the current error page).
380 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12381
clamyea99ea12018-05-28 13:54:23382 // History navigations.
383 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12384 return can_be_same_document && is_same_document_history_load
Minggang Wangb9f3fa92021-07-01 15:30:31385 ? blink::mojom::NavigationType::HISTORY_SAME_DOCUMENT
386 : blink::mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23387 }
388 DCHECK(!is_same_document_history_load);
389
390 // A same-document fragment-navigation happens when the only part of the url
391 // that is modified is after the '#' character.
392 //
393 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12394 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23395 //
396 // Note: this check is only valid for navigations that are not history
397 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
398 // history navigation from 'A#foo' to 'A#bar' is not a same-document
399 // navigation, but a different-document one. This is why history navigation
400 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47401 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
402 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12403
404 // The one case where we do the wrong thing here and incorrectly choose
405 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
406 // the renderer is a frameset. All frameset navigations should be
407 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
408 // navigation would do the right thing, as it would send it to the browser and
409 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
410 // into this method). But since we can't tell that case here for browser-
411 // initiated navigations, we have to get the renderer involved. In that case
412 // the navigation would be restarted due to the renderer spending a reply of
413 // mojom::CommitResult::RestartCrossDocument.
414
415 return can_be_same_document && is_same_doc
Minggang Wangb9f3fa92021-07-01 15:30:31416 ? blink::mojom::NavigationType::SAME_DOCUMENT
417 : blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23418}
419
Camille Lamy5193caa2018-10-12 11:59:42420// Adjusts the original input URL if needed, to get the URL to actually load and
421// the virtual URL, which may differ.
422void RewriteUrlForNavigation(const GURL& original_url,
423 BrowserContext* browser_context,
424 GURL* url_to_load,
425 GURL* virtual_url,
426 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42427 // Allow the browser URL handler to rewrite the URL. This will, for example,
428 // remove "view-source:" from the beginning of the URL to get the URL that
429 // will actually be loaded. This real URL won't be shown to the user, just
430 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31431 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42432 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
433 url_to_load, browser_context, reverse_on_redirect);
434}
435
436#if DCHECK_IS_ON()
437// Helper sanity check function used in debug mode.
438void ValidateRequestMatchesEntry(NavigationRequest* request,
439 NavigationEntryImpl* entry) {
440 if (request->frame_tree_node()->IsMainFrame()) {
441 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
442 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
Minggang Wangb9f3fa92021-07-01 15:30:31443 ui::PageTransitionFromInt(request->common_params().transition),
444 entry->GetTransitionType()));
Camille Lamy5193caa2018-10-12 11:59:42445 }
Nasko Oskovc36327d2019-01-03 23:23:04446 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42447 entry->should_clear_history_list());
448 DCHECK_EQ(request->common_params().has_user_gesture,
449 entry->has_user_gesture());
450 DCHECK_EQ(request->common_params().base_url_for_data_url,
451 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04452 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42453 entry->GetCanLoadLocalResources());
454 DCHECK_EQ(request->common_params().started_from_context_menu,
455 entry->has_started_from_context_menu());
456
457 FrameNavigationEntry* frame_entry =
458 entry->GetFrameEntry(request->frame_tree_node());
459 if (!frame_entry) {
460 NOTREACHED();
461 return;
462 }
463
Camille Lamy5193caa2018-10-12 11:59:42464 DCHECK_EQ(request->common_params().method, frame_entry->method());
465
Nasko Oskovc36327d2019-01-03 23:23:04466 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42467 if (redirect_size == frame_entry->redirect_chain().size()) {
468 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04469 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42470 frame_entry->redirect_chain()[i]);
471 }
472 } else {
473 NOTREACHED();
474 }
475}
476#endif // DCHECK_IS_ON()
477
Dave Tapuska8bfd84c2019-03-26 20:47:16478// Returns whether the session history NavigationRequests in |navigations|
479// would stay within the subtree of the sandboxed iframe in
480// |sandbox_frame_tree_node_id|.
481bool DoesSandboxNavigationStayWithinSubtree(
482 int sandbox_frame_tree_node_id,
483 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
484 for (auto& item : navigations) {
485 bool within_subtree = false;
486 // Check whether this NavigationRequest affects a frame within the
487 // sandboxed frame's subtree by walking up the tree looking for the
488 // sandboxed frame.
489 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03490 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16491 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
492 within_subtree = true;
493 break;
494 }
495 }
496 if (!within_subtree)
497 return false;
498 }
499 return true;
500}
501
Titouan Rigoudy6ec70402021-02-02 15:42:19502bool ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(
Antonio Sartori78a749f2020-11-30 12:03:39503 const NavigationRequest* request) {
504 // For local schemes we need to store the policy container in the
505 // FrameNavigationEntry, so that we can reload it in case of history
506 // navigation.
507 //
508 // TODO(https://crbug.com/1146361 and https://crbug.com/1146362): blob: and
509 // filesystem: should be removed from this list when we have properly
510 // implemented storing their policy container in the respective store.
511 return (request->common_params().url.SchemeIs(url::kAboutScheme) ||
512 request->common_params().url.SchemeIs(url::kDataScheme) ||
513 request->common_params().url.SchemeIsBlob() ||
514 request->common_params().url.SchemeIsFileSystem());
515}
516
[email protected]e9ba4472008-09-14 15:42:43517} // namespace
518
arthursonzogni66f711c2019-10-08 14:40:36519// NavigationControllerImpl::PendingEntryRef------------------------------------
520
521NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
522 base::WeakPtr<NavigationControllerImpl> controller)
523 : controller_(controller) {}
524
525NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
526 if (!controller_) // Can be null with interstitials.
527 return;
528
529 controller_->PendingEntryRefDeleted(this);
530}
531
[email protected]d202a7c2012-01-04 07:53:47532// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29533
[email protected]23a918b2014-07-15 09:51:36534const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23535
[email protected]765b35502008-08-21 00:51:20536// static
[email protected]d202a7c2012-01-04 07:53:47537size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23538 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20539
[email protected]e6fec472013-05-14 05:29:02540// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20541// when testing.
542static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29543
[email protected]71fde352011-12-29 03:29:56544// static
dcheng9bfa5162016-04-09 01:00:57545std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
546 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10547 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07548 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10549 ui::PageTransition transition,
550 bool is_renderer_initiated,
551 const std::string& extra_headers,
552 BrowserContext* browser_context,
553 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
554 return NavigationControllerImpl::CreateNavigationEntry(
555 url, referrer, std::move(initiator_origin),
556 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Hayato Ito303654c2021-06-30 09:07:54557 extra_headers, browser_context, std::move(blob_url_loader_factory));
Lukasz Anforowicz641234d52019-11-07 21:07:10558}
559
560// static
561std::unique_ptr<NavigationEntryImpl>
562NavigationControllerImpl::CreateNavigationEntry(
563 const GURL& url,
564 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07565 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10566 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57567 ui::PageTransition transition,
568 bool is_renderer_initiated,
569 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09570 BrowserContext* browser_context,
Hayato Ito303654c2021-06-30 09:07:54571 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
Camille Lamy5193caa2018-10-12 11:59:42572 GURL url_to_load;
573 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56574 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42575 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
576 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56577
Lukasz Anforowicz641234d52019-11-07 21:07:10578 // Let the NTP override the navigation params and pretend that this is a
579 // browser-initiated, bookmark-like navigation.
580 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violetcf6ea7e2021-06-09 21:09:21581 source_site_instance, &transition, &is_renderer_initiated, &referrer,
582 &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10583
Patrick Monettef507e982019-06-19 20:18:06584 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28585 nullptr, // The site instance for tabs is sent on navigation
586 // (WebContents::GetSiteInstance).
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58587 url_to_load, referrer, initiator_origin, std::u16string(), transition,
Camille Lamy5193caa2018-10-12 11:59:42588 is_renderer_initiated, blob_url_loader_factory);
589 entry->SetVirtualURL(virtual_url);
590 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56591 entry->set_update_virtual_url_with_url(reverse_on_redirect);
592 entry->set_extra_headers(extra_headers);
Patrick Monettef507e982019-06-19 20:18:06593 return entry;
[email protected]71fde352011-12-29 03:29:56594}
595
[email protected]cdcb1dee2012-01-04 00:46:20596// static
597void NavigationController::DisablePromptOnRepost() {
598 g_check_for_repost = false;
599}
600
[email protected]c5b88d82012-10-06 17:03:33601base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
602 base::Time t) {
603 // If |t| is between the water marks, we're in a run of duplicates
604 // or just getting out of it, so increase the high-water mark to get
605 // a time that probably hasn't been used before and return it.
606 if (low_water_mark_ <= t && t <= high_water_mark_) {
Peter Kastinge5a38ed2021-10-02 03:06:35607 high_water_mark_ += base::Microseconds(1);
[email protected]c5b88d82012-10-06 17:03:33608 return high_water_mark_;
609 }
610
611 // Otherwise, we're clear of the last duplicate run, so reset the
612 // water marks.
613 low_water_mark_ = high_water_mark_ = t;
614 return t;
615}
616
ckitagawa0faa5e42020-06-17 17:30:54617NavigationControllerImpl::ScopedShowRepostDialogForTesting::
618 ScopedShowRepostDialogForTesting()
619 : was_disallowed_(g_check_for_repost) {
620 g_check_for_repost = true;
621}
622
623NavigationControllerImpl::ScopedShowRepostDialogForTesting::
624 ~ScopedShowRepostDialogForTesting() {
625 g_check_for_repost = was_disallowed_;
626}
627
[email protected]d202a7c2012-01-04 07:53:47628NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00629 BrowserContext* browser_context,
630 FrameTree& frame_tree,
631 NavigationControllerDelegate* delegate)
632 : frame_tree_(frame_tree),
633 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57634 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22635 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47636 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37637 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29638}
639
[email protected]d202a7c2012-01-04 07:53:47640NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00641 // The NavigationControllerImpl might be called inside its delegate
642 // destructor. Calling it is not valid anymore.
643 delegate_ = nullptr;
644 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29645}
646
Matt Falkenhagen548ed1562021-07-06 01:38:26647WebContents* NavigationControllerImpl::DeprecatedGetWebContents() {
648 return delegate_->DeprecatedGetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32649}
650
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57651BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55652 return browser_context_;
653}
654
[email protected]d202a7c2012-01-04 07:53:47655void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30656 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36657 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57658 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57659 // Verify that this controller is unused and that the input is valid.
Lei Zhang96031532019-10-10 19:05:47660 DCHECK_EQ(0, GetEntryCount());
661 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57662 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14663 selected_navigation < static_cast<int>(entries->size()));
arthursonzogni5c4c202d2017-04-25 23:41:27664 DCHECK_EQ(-1, pending_entry_index_);
[email protected]ce3fa3c2009-04-20 19:55:57665
[email protected]ce3fa3c2009-04-20 19:55:57666 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44667 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03668 entries_.reserve(entries->size());
669 for (auto& entry : *entries)
dcheng36b6aec92015-12-26 06:16:36670 entries_.push_back(
671 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
avif16f85a72015-11-13 18:25:03672
673 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
674 // cleared out safely.
675 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57676
677 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36678 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57679}
680
toyoshim6142d96f2016-12-19 09:07:25681void NavigationControllerImpl::Reload(ReloadType reload_type,
682 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28683 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28684 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32685 int current_index = -1;
686
Carlos IL42b416592019-10-07 23:10:36687 if (entry_replaced_by_post_commit_error_) {
688 // If there is an entry that was replaced by a currently active post-commit
689 // error navigation, this can't be the initial navigation.
690 DCHECK(!IsInitialNavigation());
691 // If the current entry is a post commit error, we reload the entry it
692 // replaced instead. We leave the error entry in place until a commit
693 // replaces it, but the pending entry points to the original entry in the
694 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
695 // case that pending_entry_ != entries_[pending_entry_index_].
696 entry = entry_replaced_by_post_commit_error_.get();
697 current_index = GetCurrentEntryIndex();
698 } else if (IsInitialNavigation() && pending_entry_) {
699 // If we are reloading the initial navigation, just use the current
700 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32701 entry = pending_entry_;
702 // The pending entry might be in entries_ (e.g., after a Clone), so we
703 // should also update the current_index.
704 current_index = pending_entry_index_;
705 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00706 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32707 current_index = GetCurrentEntryIndex();
708 if (current_index != -1) {
creis3da03872015-02-20 21:12:32709 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32710 }
[email protected]979a4bc2013-04-24 01:27:15711 }
[email protected]241db352013-04-22 18:04:05712
[email protected]59167c22013-06-03 18:07:32713 // If we are no where, then we can't reload. TODO(darin): We should add a
714 // CanReload method.
715 if (!entry)
716 return;
717
Takashi Toyoshimac7df3c22019-06-25 14:18:47718 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26719 entry->set_reload_type(reload_type);
720
Aran Gilman37d11632019-10-08 23:07:15721 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30722 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07723 // they really want to do this. If they do, the dialog will call us back
724 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57725 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02726
[email protected]106a0812010-03-18 00:15:12727 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57728 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47729 return;
initial.commit09911bf2008-07-26 23:55:29730 }
Lei Zhang96031532019-10-10 19:05:47731
732 if (!IsInitialNavigation())
733 DiscardNonCommittedEntries();
734
735 pending_entry_ = entry;
736 pending_entry_index_ = current_index;
737 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
738
Nate Chapin45f620582021-09-30 17:45:43739 // location.reload() goes through BeginNavigation, so all reloads triggered
740 // via this codepath are browser initiated.
Lei Zhang96031532019-10-10 19:05:47741 NavigateToExistingPendingEntry(reload_type,
Nate Chapin45f620582021-09-30 17:45:43742 FrameTreeNode::kFrameTreeNodeInvalidId,
743 true /* is_browser_initiated */);
initial.commit09911bf2008-07-26 23:55:29744}
745
[email protected]d202a7c2012-01-04 07:53:47746void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48747 DCHECK(pending_reload_ != ReloadType::NONE);
748 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12749}
750
[email protected]d202a7c2012-01-04 07:53:47751void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48752 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12753 NOTREACHED();
754 } else {
toyoshim6142d96f2016-12-19 09:07:25755 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48756 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12757 }
758}
759
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57760bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09761 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11762}
763
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57764bool NavigationControllerImpl::IsInitialBlankNavigation() {
creis10a4ab72015-10-13 17:22:40765 // TODO(creis): Once we create a NavigationEntry for the initial blank page,
toyoshim0df1d3a2016-09-09 09:52:48766 // we'll need to check for entry count 1 and restore_type NONE (to exclude
767 // the cloned tab case).
creis10a4ab72015-10-13 17:22:40768 return IsInitialNavigation() && GetEntryCount() == 0;
769}
770
Aran Gilman37d11632019-10-08 23:07:15771NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
772 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58773 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03774 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58775}
776
Adithya Srinivasan9b0c99c2021-08-10 15:19:45777NavigationEntryImpl*
778NavigationControllerImpl::GetEntryWithUniqueIDIncludingPending(
779 int nav_entry_id) const {
780 NavigationEntryImpl* entry = GetEntryWithUniqueID(nav_entry_id);
781 if (entry)
782 return entry;
783 return pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id
784 ? pending_entry_
785 : nullptr;
786}
787
W. James MacLean1c40862c2020-04-27 21:05:57788void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
789 const url::Origin& origin) {
790 for (int i = 0; i < GetEntryCount(); i++) {
791 auto* entry = GetEntryAtIndex(i);
792 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
793 }
794 if (entry_replaced_by_post_commit_error_) {
795 // It's possible we could come back to this entry if the error
796 // page/interstitial goes away.
797 entry_replaced_by_post_commit_error_
798 ->RegisterExistingOriginToPreventOptInIsolation(origin);
799 }
800 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
801 // visiting pending_entry_, which lacks a committed origin. This will be done
802 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
803}
804
avi25764702015-06-23 15:43:37805void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57806 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00807 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37808 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27809 DCHECK_EQ(-1, pending_entry_index_);
[email protected]765b35502008-08-21 00:51:20810}
811
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57812NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47813 if (pending_entry_)
814 return pending_entry_;
815 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20816}
817
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57818NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32819 // The pending entry is safe to return for new (non-history), browser-
820 // initiated navigations. Most renderer-initiated navigations should not
821 // show the pending entry, to prevent URL spoof attacks.
822 //
823 // We make an exception for renderer-initiated navigations in new tabs, as
824 // long as no other page has tried to access the initial empty document in
825 // the new tab. If another page modifies this blank page, a URL spoof is
826 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32827 bool safe_to_show_pending =
828 pending_entry_ &&
829 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09830 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32831 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46832 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32833
834 // Also allow showing the pending entry for history navigations in a new tab,
835 // such as Ctrl+Back. In this case, no existing page is visible and no one
836 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15837 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
838 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32839 safe_to_show_pending = true;
840
841 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19842 return pending_entry_;
843 return GetLastCommittedEntry();
844}
845
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57846int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20847 if (pending_entry_index_ != -1)
848 return pending_entry_index_;
849 return last_committed_entry_index_;
850}
851
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57852NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20853 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28854 return nullptr;
avif16f85a72015-11-13 18:25:03855 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20856}
857
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57858bool NavigationControllerImpl::CanViewSource() {
Jeremy Roman2d8dfe132021-07-06 20:51:26859 const std::string& mime_type =
860 frame_tree_.root()->current_frame_host()->GetPage().contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53861 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
862 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27863 NavigationEntry* visible_entry = GetVisibleEntry();
864 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39865 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16866}
867
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57868int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27869 // The last committed entry index must always be less than the number of
Carlos IL4dea8902020-05-26 15:14:29870 // entries. If there are no entries, it must be -1.
arthursonzogni5c4c202d2017-04-25 23:41:27871 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
872 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55873 return last_committed_entry_index_;
874}
875
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57876int NavigationControllerImpl::GetEntryCount() {
Carlos IL4dea8902020-05-26 15:14:29877 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55878 return static_cast<int>(entries_.size());
879}
880
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57881NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37882 if (index < 0 || index >= GetEntryCount())
883 return nullptr;
884
avif16f85a72015-11-13 18:25:03885 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25886}
887
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57888NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47889 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20890}
891
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57892int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46893 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03894}
895
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57896bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03897 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
898 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
899 return true;
900 }
901 return false;
[email protected]765b35502008-08-21 00:51:20902}
903
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57904bool NavigationControllerImpl::CanGoForward() {
WangHui74286d52021-03-31 16:17:15905 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
906 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
907 return true;
908 }
909 return false;
[email protected]765b35502008-08-21 00:51:20910}
911
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57912bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03913 int index = GetIndexForOffset(offset);
914 return index >= 0 && index < GetEntryCount();
915}
916
WangHui74286d52021-03-31 16:17:15917#if defined(OS_ANDROID)
918bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
WangHui74286d52021-03-31 16:17:15919 if (offset == 0)
920 return true;
921 int increment = offset > 0 ? 1 : -1;
922 int non_skippable_entries = 0;
923 for (int index = GetIndexForOffset(increment);
924 index >= 0 && index < GetEntryCount(); index += increment) {
925 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
926 non_skippable_entries++;
927
928 if (non_skippable_entries == std::abs(offset))
929 return true;
930 }
931 return false;
932}
933#endif
934
[email protected]d202a7c2012-01-04 07:53:47935void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06936 int target_index = GetIndexForOffset(-1);
937
Elly Fong-Jones40ee8112021-06-23 19:10:52938 // Move the target index past the skippable entries.
Shivani Sharma298d12852019-01-22 20:04:03939 bool all_skippable_entries = true;
Elly Fong-Jones40ee8112021-06-23 19:10:52940 while (target_index >= 0) {
941 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui()) {
Shivani Sharma298d12852019-01-22 20:04:03942 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06943 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16944 }
Elly Fong-Jones40ee8112021-06-23 19:10:52945 target_index--;
shivanisha55201872018-12-13 04:29:06946 }
Miyoung Shin1c565c912021-03-17 12:11:21947
Shivani Sharma298d12852019-01-22 20:04:03948 // Do nothing if all entries are skippable. Normally this path would not
949 // happen as consumers would have already checked it in CanGoBack but a lot of
950 // tests do not do that.
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42951 if (all_skippable_entries)
Shivani Sharma298d12852019-01-22 20:04:03952 return;
shivanisha55201872018-12-13 04:29:06953
shivanisha55201872018-12-13 04:29:06954 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20955}
956
[email protected]d202a7c2012-01-04 07:53:47957void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06958 int target_index = GetIndexForOffset(1);
959
Shivani Sharma2d5b4b6b2019-01-08 16:07:16960 // Note that at least one entry (the last one) will be non-skippable since
961 // entries are marked skippable only when they add another entry because of
962 // redirect or pushState.
Elly Fong-Jones40ee8112021-06-23 19:10:52963 while (target_index < static_cast<int>(entries_.size())) {
964 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui())
shivanisha55201872018-12-13 04:29:06965 break;
Elly Fong-Jones40ee8112021-06-23 19:10:52966 target_index++;
shivanisha55201872018-12-13 04:29:06967 }
shivanisha55201872018-12-13 04:29:06968 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20969}
970
[email protected]d202a7c2012-01-04 07:53:47971void NavigationControllerImpl::GoToIndex(int index) {
Nate Chapin45f620582021-09-30 17:45:43972 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId,
973 true /* is_browser_initiated */);
Dave Tapuska8bfd84c2019-03-26 20:47:16974}
975
976void NavigationControllerImpl::GoToIndex(int index,
Nate Chapin45f620582021-09-30 17:45:43977 int sandbox_frame_tree_node_id,
978 bool is_browser_initiated) {
979 DCHECK(sandbox_frame_tree_node_id == FrameTreeNode::kFrameTreeNodeInvalidId ||
980 !is_browser_initiated);
sunjian30574a62017-03-21 21:39:44981 TRACE_EVENT0("browser,navigation,benchmark",
982 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:20983 if (index < 0 || index >= static_cast<int>(entries_.size())) {
984 NOTREACHED();
985 return;
986 }
987
[email protected]cbab76d2008-10-13 22:42:47988 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20989
arthursonzogni5c4c202d2017-04-25 23:41:27990 DCHECK_EQ(nullptr, pending_entry_);
991 DCHECK_EQ(-1, pending_entry_index_);
992 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:20993 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:27994 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
995 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Nate Chapin45f620582021-09-30 17:45:43996 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id,
997 is_browser_initiated);
[email protected]765b35502008-08-21 00:51:20998}
999
[email protected]d202a7c2012-01-04 07:53:471000void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:121001 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:031002 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:201003 return;
1004
[email protected]9ba14052012-06-22 23:50:031005 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:201006}
1007
Nate Chapin45f620582021-09-30 17:45:431008void NavigationControllerImpl::GoToOffsetFromRenderer(int offset) {
1009 // Note: This is actually reached in unit tests.
1010 if (!CanGoToOffset(offset))
1011 return;
1012
1013 GoToIndex(GetIndexForOffset(offset), FrameTreeNode::kFrameTreeNodeInvalidId,
1014 false /* is_browser_initiated */);
1015}
1016
WangHui74286d52021-03-31 16:17:151017#if defined(OS_ANDROID)
1018void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
1019 // Note: This is actually reached in unit tests.
1020 if (!CanGoToOffsetWithSkipping(offset))
1021 return;
1022
Elly Fong-Jonesccc6d1f2021-06-14 18:32:421023 if (offset == 0) {
WangHui74286d52021-03-31 16:17:151024 GoToIndex(GetIndexForOffset(offset));
1025 return;
1026 }
1027 int increment = offset > 0 ? 1 : -1;
1028 // Find the offset without counting skippable entries.
1029 int target_index = GetIndexForOffset(increment);
1030 int non_skippable_entries = 0;
1031 for (int index = target_index; index >= 0 && index < GetEntryCount();
1032 index += increment) {
1033 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1034 non_skippable_entries++;
1035
1036 if (non_skippable_entries == std::abs(offset)) {
1037 target_index = index;
1038 break;
1039 }
1040 }
1041
1042 GoToIndex(target_index);
1043}
1044#endif
1045
[email protected]41374f12013-07-24 02:49:281046bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151047 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281048 return false;
[email protected]6a13a6c2011-12-20 21:47:121049
[email protected]43032342011-03-21 14:10:311050 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281051 return true;
[email protected]cbab76d2008-10-13 22:42:471052}
1053
Michael Thiessen9b14d512019-09-23 21:19:471054void NavigationControllerImpl::PruneForwardEntries() {
1055 DiscardNonCommittedEntries();
1056 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471057 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471058 if (num_removed <= 0)
1059 return;
1060 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1061 NotifyPrunedEntries(this, remove_start_index /* start index */,
1062 num_removed /* count */);
1063}
1064
Aran Gilman37d11632019-10-08 23:07:151065void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1066 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321067 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311068 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511069 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1070 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321071 }
1072}
1073
Harkiran Bolariaba823e42021-05-21 18:30:361074base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL(
1075 const GURL& url,
1076 const Referrer& referrer,
1077 ui::PageTransition transition,
1078 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471079 LoadURLParams params(url);
1080 params.referrer = referrer;
1081 params.transition_type = transition;
1082 params.extra_headers = extra_headers;
Harkiran Bolariaba823e42021-05-21 18:30:361083 return LoadURLWithParams(params);
[email protected]cf002332012-08-14 19:17:471084}
1085
Harkiran Bolariaba823e42021-05-21 18:30:361086base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams(
1087 const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061088 if (params.is_renderer_initiated)
1089 DCHECK(params.initiator_origin.has_value());
1090
naskob8744d22014-08-28 17:07:431091 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151092 "NavigationControllerImpl::LoadURLWithParams", "url",
1093 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291094 bool is_explicit_navigation =
1095 GetContentClient()->browser()->IsExplicitNavigation(
1096 params.transition_type);
1097 if (HandleDebugURL(params.url, params.transition_type,
1098 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431099 // If Telemetry is running, allow the URL load to proceed as if it's
1100 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491101 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431102 cc::switches::kEnableGpuBenchmarking))
Harkiran Bolariaba823e42021-05-21 18:30:361103 return nullptr;
[email protected]47752982014-07-29 08:01:431104 }
[email protected]8bf1048012012-02-08 01:22:181105
[email protected]cf002332012-08-14 19:17:471106 // Checks based on params.load_type.
1107 switch (params.load_type) {
1108 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431109 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471110 break;
1111 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261112 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471113 NOTREACHED() << "Data load must use data scheme.";
Harkiran Bolariaba823e42021-05-21 18:30:361114 return nullptr;
[email protected]cf002332012-08-14 19:17:471115 }
1116 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461117 }
[email protected]e47ae9472011-10-13 19:48:341118
[email protected]e47ae9472011-10-13 19:48:341119 // The user initiated a load, we don't need to reload anymore.
1120 needs_reload_ = false;
1121
Harkiran Bolariaba823e42021-05-21 18:30:361122 return NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441123}
1124
Mohamed Abdelhalim833de902019-09-16 17:41:451125bool NavigationControllerImpl::PendingEntryMatchesRequest(
1126 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191127 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451128 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191129}
1130
[email protected]d202a7c2012-01-04 07:53:471131bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321132 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071133 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331134 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441135 bool is_same_document_navigation,
Nate Chapinc7019dd7d2021-06-25 18:29:251136 bool was_on_initial_empty_document,
Shivani Sharmaffb32b82019-04-09 16:58:471137 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131138 NavigationRequest* navigation_request) {
1139 DCHECK(navigation_request);
shivanigithubf405bf0d2021-11-05 17:58:331140 if (ShouldMaintainTrivialSessionHistory(rfh->frame_tree_node()) &&
1141 GetLastCommittedEntry()) {
Takashi Toyoshimaa9f66802021-08-18 02:40:171142 // Ensure that this navigation does not add a navigation entry, since
1143 // ShouldMaintainTrivialSessionHistory() means we should not add an entry
1144 // beyond the last committed one. Therefore, `should_replace_current_entry`
1145 // should be set, which replaces the current entry, or this should be a
1146 // reload, which does not create a new entry.
1147 DCHECK(params.should_replace_current_entry ||
1148 navigation_request->GetReloadType() != ReloadType::NONE);
1149 }
[email protected]cd2e15742013-03-08 04:08:311150 is_initial_navigation_ = false;
1151
[email protected]0e8db942008-09-24 21:21:481152 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431153 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481154 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361155 if (entry_replaced_by_post_commit_error_) {
1156 if (is_same_document_navigation) {
1157 // Same document navigations should not be possible on error pages and
1158 // would leave the controller in a weird state. Kill the renderer if
1159 // that happens.
1160 bad_message::ReceivedBadMessage(
1161 rfh->GetProcess(), bad_message::NC_SAME_DOCUMENT_POST_COMMIT_ERROR);
1162 }
1163 // Any commit while a post-commit error page is showing should put the
1164 // original entry back, replacing the error page's entry. This includes
1165 // reloads, where the original entry was used as the pending entry and
1166 // should now be at the correct index at commit time.
1167 entries_[last_committed_entry_index_] =
1168 std::move(entry_replaced_by_post_commit_error_);
1169 }
Fergal Daly8e33cf62020-12-12 01:06:071170 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551171 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431172 if (pending_entry_ &&
1173 pending_entry_->GetIsOverridingUserAgent() !=
1174 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1175 overriding_user_agent_changed = true;
Shu Yang5756fa422021-11-05 23:02:541176#if defined(OS_ANDROID)
1177 // TODO(crbug.com/1266277): Clean up the logic of setting
1178 // |overriding_user_agent_changed| post-launch.
Shu Yang9eabbb902021-11-17 00:45:031179 if (base::FeatureList::IsEnabled(features::kRequestDesktopSiteExceptions)) {
Shu Yang5756fa422021-11-05 23:02:541180 // Must honor user agent overrides in the |navigation_request|, such as
1181 // from things like RequestDesktopSiteWebContentsObserverAndroid. As a
1182 // result, besides comparing |pending_entry_|'s user agent against
1183 // LastCommittedEntry's, also need to compare |navigation_request|'s user
1184 // agent against LastCommittedEntry's.
1185 if (navigation_request->is_overriding_user_agent() !=
1186 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
1187 overriding_user_agent_changed = true;
1188 }
1189 }
1190#endif // defined(OS_ANDROID)
[email protected]0e8db942008-09-24 21:21:481191 } else {
Gang Wu325f03f42021-02-25 20:00:461192 // GetLastCommittedEntry() is null, so this is the first entry.
Fergal Daly8e33cf62020-12-12 01:06:071193 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481194 details->previous_entry_index = -1;
Gang Wu325f03f42021-02-25 20:00:461195 if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) {
1196 // Default setting is NOT override the user agent, so overriding the user
1197 // agent in first entry should be considered as user agent changed as
1198 // well.
1199 overriding_user_agent_changed = true;
1200 }
Shu Yang5756fa422021-11-05 23:02:541201#if defined(OS_ANDROID)
1202 // TODO(crbug.com/1266277): Clean up the logic of setting
1203 // |overriding_user_agent_changed| post-launch.
Shu Yang9eabbb902021-11-17 00:45:031204 if (base::FeatureList::IsEnabled(features::kRequestDesktopSiteExceptions)) {
Shu Yang5756fa422021-11-05 23:02:541205 // Must honor user agent overrides in the |navigation_request|, such as
1206 // from things like RequestDesktopSiteWebContentsObserverAndroid. As a
1207 // result, besides checking |pending_entry_|'s user agent, also need to
1208 // check |navigation_request|'s.
1209 if (navigation_request->is_overriding_user_agent()) {
1210 overriding_user_agent_changed = true;
1211 }
1212 }
1213#endif // defined(OS_ANDROID)
[email protected]0e8db942008-09-24 21:21:481214 }
[email protected]ecd9d8702008-08-28 22:10:171215
Dave Tapuskaa2ab4f252021-07-08 21:31:281216 bool is_main_frame_navigation = !rfh->GetParent();
1217
Alexander Timind2f2e4f22019-04-02 20:04:531218 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1219 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281220 // For primary frame tree navigations, choose an appropriate
1221 // BackForwardCacheMetrics to be associated with the NavigationEntry, by
1222 // either creating a new object or reusing the previous one depending on
1223 // whether it's a main frame navigation or not.
1224 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics;
1225 if (navigation_request->frame_tree_node()->frame_tree()->type() ==
1226 FrameTree::Type::kPrimary) {
1227 back_forward_cache_metrics =
1228 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1229 GetLastCommittedEntry(), is_main_frame_navigation,
1230 params.document_sequence_number);
1231 }
Alexander Timind2f2e4f22019-04-02 20:04:531232 // Notify the last active entry that we have navigated away.
Dave Tapuskaa2ab4f252021-07-08 21:31:281233 if (is_main_frame_navigation && !is_same_document_navigation) {
Alexander Timind2f2e4f22019-04-02 20:04:531234 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1235 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Hajime Hoshic7606502021-04-14 02:42:161236 metrics->MainFrameDidNavigateAwayFromDocument(rfh, navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531237 }
1238 }
1239 }
1240
fdegans9caf66a2015-07-30 21:10:421241 // If there is a pending entry at this point, it should have a SiteInstance,
1242 // except for restored entries.
jam48cea9082017-02-15 06:13:291243 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481244 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081245 pending_entry_->IsRestored());
1246 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151247 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291248 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481249 }
[email protected]e9ba4472008-09-14 15:42:431250
Nasko Oskovaee2f862018-06-15 00:05:521251 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1252 // has changed, this must be treated as a new navigation with replacement.
1253 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491254 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521255 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101256 pending_entry_->GetUniqueID() ==
1257 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521258 pending_entry_->site_instance() &&
1259 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1260 DCHECK_NE(-1, pending_entry_index_);
1261 // TODO(nasko,creis): Instead of setting this value here, set
1262 // should_replace_current_entry on the parameters we send to the
1263 // renderer process as part of CommitNavigation. The renderer should
1264 // in turn send it back here as part of |params| and it can be just
1265 // enforced and renderer process terminated on mismatch.
1266 details->did_replace_entry = true;
1267 } else {
1268 // The renderer tells us whether the navigation replaces the current entry.
1269 details->did_replace_entry = params.should_replace_current_entry;
1270 }
[email protected]bcd904482012-02-01 01:54:221271
[email protected]e9ba4472008-09-14 15:42:431272 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amnif6950d552020-11-24 03:26:101273 details->type = ClassifyNavigation(rfh, params, navigation_request);
[email protected]4bf3522c2010-08-19 21:00:201274
eugenebutee08663a2017-04-27 17:43:121275 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441276 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121277
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071278 details->is_prerender_activation =
1279 navigation_request->IsPrerenderedPageActivation();
1280
Peter Boströmd7592132019-01-30 04:50:311281 // Make sure we do not discard the pending entry for a different ongoing
1282 // navigation when a same document commit comes in unexpectedly from the
1283 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1284 // other navigation types. See https://crbug.com/900036.
1285 // TODO(crbug.com/926009): Handle history.pushState() as well.
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061286 bool keep_pending_entry =
1287 is_same_document_navigation &&
1288 details->type == NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY &&
1289 pending_entry_ && !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311290
[email protected]0e8db942008-09-24 21:21:481291 switch (details->type) {
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061292 case NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY:
Charlie Reisc0f17d2d2021-01-12 18:52:491293 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051294 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451295 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431296 break;
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061297 case NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY:
Charlie Reisc0f17d2d2021-01-12 18:52:491298 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1299 was_restored, navigation_request,
1300 keep_pending_entry);
[email protected]e9ba4472008-09-14 15:42:431301 break;
[email protected]8ff00d72012-10-23 19:12:211302 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471303 RendererDidNavigateNewSubframe(
1304 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451305 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431306 break;
[email protected]8ff00d72012-10-23 19:12:211307 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391308 if (!RendererDidNavigateAutoSubframe(
Nate Chapinc7019dd7d2021-06-25 18:29:251309 rfh, params, details->is_same_document,
1310 was_on_initial_empty_document, navigation_request)) {
creisce0ef3572017-01-26 17:53:081311 // We don't send a notification about auto-subframe PageState during
1312 // UpdateStateForFrame, since it looks like nothing has changed. Send
1313 // it here at commit time instead.
1314 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431315 return false;
creis59d5a47cb2016-08-24 23:57:191316 }
[email protected]e9ba4472008-09-14 15:42:431317 break;
[email protected]8ff00d72012-10-23 19:12:211318 case NAVIGATION_TYPE_NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:491319 // If a pending navigation was in progress, this canceled it. We should
1320 // discard it and make sure it is removed from the URL bar. After that,
1321 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:431322 // the caller that nothing has happened.
arthursonzogni69a6a1b2019-09-17 09:23:001323 if (pending_entry_)
[email protected]20d1c992011-04-12 21:17:491324 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431325 return false;
Aran Gilman37d11632019-10-08 23:07:151326 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431327 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151328 break;
[email protected]765b35502008-08-21 00:51:201329 }
1330
[email protected]688aa65c62012-09-28 04:32:221331 // At this point, we know that the navigation has just completed, so
1332 // record the time.
1333 //
1334 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261335 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331336 base::Time timestamp =
1337 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1338 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131339 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221340
Peter Boströmd7592132019-01-30 04:50:311341 // If we aren't keeping the pending entry, there shouldn't be one at this
1342 // point. Clear it again in case any error cases above forgot to do so.
1343 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1344 // been cleared instead of protecting against it.
1345 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001346 DiscardNonCommittedEntries();
[email protected]f233e4232013-02-23 00:55:141347
[email protected]e9ba4472008-09-14 15:42:431348 // All committed entries should have nonempty content state so WebKit doesn't
1349 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471350 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321351 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221352 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441353 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531354 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1355 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281356 if (back_forward_cache_metrics &&
1357 !active_entry->back_forward_cache_metrics()) {
Alexander Timind2f2e4f22019-04-02 20:04:531358 active_entry->set_back_forward_cache_metrics(
1359 std::move(back_forward_cache_metrics));
1360 }
Dave Tapuskaa2ab4f252021-07-08 21:31:281361
1362 // `back_forward_cache_metrics()` may return null as we do not record
1363 // back-forward cache metrics for navigations in non-primary frame trees.
1364 if (active_entry->back_forward_cache_metrics()) {
1365 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
1366 navigation_request,
1367 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
1368 }
naskoc7533512016-05-06 17:01:121369
Charles Reisc0507202017-09-21 00:40:021370 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1371 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1372 // A mismatch can occur if the renderer lies or due to a unique name collision
1373 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121374 FrameNavigationEntry* frame_entry =
1375 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021376 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1377 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031378 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081379 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1380 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031381 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201382
1383 // Remember the bindings the renderer process has at this point, so that
1384 // we do not grant this entry additional bindings if we come back to it.
1385 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301386 }
[email protected]132e281a2012-07-31 18:32:441387
[email protected]97d8f0d2013-10-29 16:49:211388 // Once it is committed, we no longer need to track several pieces of state on
1389 // the entry.
naskoc7533512016-05-06 17:01:121390 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131391
[email protected]49bd30e62011-03-22 20:12:591392 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221393 // TODO(creis): This check won't pass for subframes until we create entries
1394 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001395 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421396 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591397
[email protected]e9ba4472008-09-14 15:42:431398 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001399 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001400 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311401 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531402
arthursonzogni7ddc6542021-04-09 09:16:501403 active_entry->SetIsOverridingUserAgent(
1404 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031405
[email protected]93f230e02011-06-01 14:40:001406 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291407
aelias100c9192017-01-13 00:01:431408 if (overriding_user_agent_changed)
1409 delegate_->UpdateOverridingUserAgent();
1410
creis03b48002015-11-04 00:54:561411 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1412 // one knows the latest NavigationEntry it is showing (whether it has
1413 // committed anything in this navigation or not). This allows things like
1414 // state and title updates from RenderFrames to apply to the latest relevant
1415 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381416 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001417 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381418 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
Hayato Ito2c8c08d02021-06-23 03:38:431419
1420 if (navigation_request->IsPrerenderedPageActivation()) {
1421 BroadcastHistoryOffsetAndLength();
1422 // TODO(crbug.com/1222893): Broadcasting happens after the prerendered page
1423 // is activated. As a result, a "prerenderingchange" event listener sees the
1424 // history.length which is not updated yet. We should guarantee that
1425 // history's length and offset should be updated before a
1426 // "prerenderingchange" event listener runs. One possible approach is to use
1427 // the same IPC which "prerenderingchange" uses, and propagate history's
1428 // length and offset together with that.
1429 }
1430
[email protected]e9ba4472008-09-14 15:42:431431 return true;
initial.commit09911bf2008-07-26 23:55:291432}
1433
[email protected]8ff00d72012-10-23 19:12:211434NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321435 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101436 const mojom::DidCommitProvisionalLoadParams& params,
1437 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591438 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511439 "ClassifyNavigation");
1440
avi7c6f35e2015-05-08 17:52:381441 if (params.did_create_new_entry) {
Charlie Reisc0f17d2d2021-01-12 18:52:491442 // A new entry. We may or may not have a corresponding pending entry, and
avi7c6f35e2015-05-08 17:52:381443 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001444 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491445 trace_return.set_return_reason("new entry, no parent, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061446 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381447 }
1448
1449 // When this is a new subframe navigation, we should have a committed page
1450 // in which it's a subframe. This may not be the case when an iframe is
1451 // navigated on a popup navigated to about:blank (the iframe would be
1452 // written into the popup by script on the main page). For these cases,
1453 // there isn't any navigation stuff we can do, so just ignore it.
Nasko Oskovae49e292020-08-13 02:08:511454 if (!GetLastCommittedEntry()) {
1455 trace_return.set_return_reason("new entry, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381456 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511457 }
avi7c6f35e2015-05-08 17:52:381458
1459 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511460 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381461 return NAVIGATION_TYPE_NEW_SUBFRAME;
1462 }
1463
Charlie Reisc0f17d2d2021-01-12 18:52:491464 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381465 DCHECK(!params.history_list_was_cleared);
1466
avi39c1edd32015-06-04 20:06:001467 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381468 // All manual subframes would be did_create_new_entry and handled above, so
1469 // we know this is auto.
Nasko Oskovae49e292020-08-13 02:08:511470 if (GetLastCommittedEntry()) {
1471 trace_return.set_return_reason("subframe, last commmited, auto subframe");
avi7c6f35e2015-05-08 17:52:381472 return NAVIGATION_TYPE_AUTO_SUBFRAME;
Nasko Oskovae49e292020-08-13 02:08:511473 }
Lei Zhang96031532019-10-10 19:05:471474
1475 // We ignore subframes created in non-committed pages; we'd appreciate if
1476 // people stopped doing that.
Nasko Oskovae49e292020-08-13 02:08:511477 trace_return.set_return_reason("subframe, no last commmited, ignore");
Lei Zhang96031532019-10-10 19:05:471478 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381479 }
1480
Rakina Zata Amnif6950d552020-11-24 03:26:101481 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1482 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381483 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1484 // create a new page.
1485
1486 // Just like above in the did_create_new_entry case, it's possible to
1487 // scribble onto an uncommitted page. Again, there isn't any navigation
1488 // stuff that we can do, so ignore it here as well.
avi3a5b8f32015-05-28 17:50:231489 NavigationEntry* last_committed = GetLastCommittedEntry();
Nasko Oskovae49e292020-08-13 02:08:511490 if (!last_committed) {
1491 trace_return.set_return_reason("nav entry 0, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381492 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511493 }
avi7c6f35e2015-05-08 17:52:381494
Hayato Ito2ae49442021-07-02 02:59:251495 // This main frame navigation is not a history navigation (since
1496 // nav_entry_id is 0), but didn't create a new entry. So this must be a
1497 // reload or a replacement navigation, which will modify the existing entry.
1498 //
Nasko Oskov332593c2018-08-16 17:21:341499 // TODO(nasko): With error page isolation, reloading an existing session
1500 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491501 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341502 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511503 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491504 "nav entry 0, last committed, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061505 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381506 }
1507
Rakina Zata Amnif6950d552020-11-24 03:26:101508 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521509 // If the SiteInstance of the |pending_entry_| does not match the
1510 // SiteInstance that got committed, treat this as a new navigation with
1511 // replacement. This can happen if back/forward/reload encounters a server
1512 // redirect to a different site or an isolated error page gets successfully
1513 // reloaded into a different SiteInstance.
1514 if (pending_entry_->site_instance() &&
1515 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491516 trace_return.set_return_reason("pending matching nav entry, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061517 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521518 }
creis77c9aa32015-09-25 19:59:421519
Nasko Oskovaee2f862018-06-15 00:05:521520 if (pending_entry_index_ == -1) {
1521 // In this case, we have a pending entry for a load of a new URL but Blink
1522 // didn't do a new navigation (params.did_create_new_entry). First check
1523 // to make sure Blink didn't treat a new cross-process navigation as
1524 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161525 // we must treat it as NEW rather than the converted reload case below,
1526 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521527 if (!GetLastCommittedEntry() ||
1528 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161529 trace_return.set_return_reason("new pending, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061530 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521531 }
1532
1533 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161534 // We will create a pending entry, but NavigateWithoutEntry will convert
1535 // it to a reload since it's the same page and not create a new entry for
1536 // it. (The user doesn't want to have a new back/forward entry when they
1537 // do this.) Therefore we want to just ignore the pending entry and go
1538 // back to where we were (the "existing entry").
1539 trace_return.set_return_reason("new pending, existing (same) entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061540 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521541 }
avi7c6f35e2015-05-08 17:52:381542 }
1543
creis26d22632017-04-21 20:23:561544 // Everything below here is assumed to be an existing entry, but if there is
1545 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511546 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491547 trace_return.set_return_reason("no last committed, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061548 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511549 }
creis26d22632017-04-21 20:23:561550
Rakina Zata Amni153d5702021-09-13 22:48:001551 if (navigation_request->commit_params().intended_as_new_entry) {
avi7c6f35e2015-05-08 17:52:381552 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491553 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1554 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161555 trace_return.set_return_reason("intended as new entry, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061556 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381557 }
1558
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121559 if (navigation_request->DidEncounterError() &&
1560 failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101561 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381562 // If the renderer was going to a new pending entry that got cleared because
1563 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491564 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381565 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511566 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491567 "unreachable, matching pending, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061568 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381569 }
1570
Charlie Reisc0f17d2d2021-01-12 18:52:491571 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101572 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511573 trace_return.traced_value()->SetInteger("existing_entry_index",
1574 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381575 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441576 // The renderer has committed a navigation to an entry that no longer
1577 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491578 trace_return.set_return_reason("existing entry -1, new entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061579 return NAVIGATION_TYPE_MAIN_FRAME_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381580 }
1581
avi7c6f35e2015-05-08 17:52:381582 // Since we weeded out "new" navigations above, we know this is an existing
1583 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491584 trace_return.set_return_reason("default return, existing entry");
Yoshisato Yanagisawa2ccba602021-11-17 08:24:061585 return NAVIGATION_TYPE_MAIN_FRAME_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381586}
1587
Rakina Zata Amni3460d382021-10-29 00:43:371588void NavigationControllerImpl::UpdateNavigationEntryDetails(
1589 NavigationEntryImpl* entry,
1590 RenderFrameHostImpl* rfh,
1591 const mojom::DidCommitProvisionalLoadParams& params,
1592 NavigationRequest* request,
1593 NavigationEntryImpl::UpdatePolicy update_policy,
1594 bool is_new_entry) {
1595 // Update the FrameNavigationEntry.
1596 entry->AddOrUpdateFrameEntry(
1597 rfh->frame_tree_node(), update_policy, params.item_sequence_number,
1598 params.document_sequence_number, params.app_history_key,
1599 rfh->GetSiteInstance(), nullptr, params.url,
1600 GetCommittedOriginForFrameEntry(params, request),
1601 Referrer(*params.referrer), request->common_params().initiator_origin,
1602 request->GetRedirectChain(), params.page_state, params.method,
1603 params.post_id, nullptr /* blob_url_loader_factory */,
1604 request->web_bundle_navigation_info()
1605 ? request->web_bundle_navigation_info()->Clone()
1606 : nullptr,
1607 request->GetSubresourceWebBundleNavigationInfo(),
1608 ComputePolicyContainerPoliciesForFrameEntry(
1609 rfh, request->IsSameDocument(), request));
1610
1611 if (rfh->GetParent()) {
1612 // Only modify the NavigationEntry for main frame navigations.
1613 return;
1614 }
1615 if (entry->update_virtual_url_with_url())
1616 UpdateVirtualURLToURL(entry, params.url);
1617 // Don't use the page type from the pending entry. Some interstitial page
1618 // may have set the type to interstitial. Once we commit, however, the page
1619 // type must always be normal or error.
1620 entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1621 : PAGE_TYPE_NORMAL);
1622 if (is_new_entry) {
1623 // Some properties of the NavigationEntry are only set when the entry is
1624 // new (we aren't reusing it).
1625 entry->SetTransitionType(params.transition);
1626 entry->SetOriginalRequestURL(request->GetOriginalRequestURL());
1627 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
1628 entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
1629 } else {
1630 // We are reusing the NavigationEntry. The site instance will normally be
1631 // the same except for a few cases:
1632 // 1) session restore, when no site instance will be assigned or
1633 // 2) redirect, when the site instance is reset.
1634 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
1635 entry->site_instance() == rfh->GetSiteInstance());
1636 }
1637}
1638
Charlie Reisc0f17d2d2021-01-12 18:52:491639void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321640 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071641 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361642 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441643 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471644 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451645 NavigationRequest* request) {
dcheng9bfa5162016-04-09 01:00:571646 std::unique_ptr<NavigationEntryImpl> new_entry;
Anton Bikineevf62d1bf2021-05-15 17:56:071647 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451648 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061649
creisf49dfc92016-07-26 17:05:181650 // First check if this is an in-page navigation. If so, clone the current
1651 // entry instead of looking at the pending entry, because the pending entry
1652 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361653 if (is_same_document && GetLastCommittedEntry()) {
Patrick Monette50e8bd82019-06-13 22:40:451654 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481655 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081656 params.document_sequence_number, params.app_history_key,
1657 rfh->GetSiteInstance(), nullptr, params.url,
1658 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091659 Referrer(*params.referrer), initiator_origin,
1660 request->GetRedirectChain(), params.page_state, params.method,
1661 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391662 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481663 request->GetSubresourceWebBundleNavigationInfo(),
Antonio Sartori78a749f2020-11-30 12:03:391664 // We will set the document policies later in this function.
Titouan Rigoudy6ec70402021-02-02 15:42:191665 nullptr /* policy_container_policies */);
Charles Reisf44482022017-10-13 21:15:031666
creisf49dfc92016-07-26 17:05:181667 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Harkiran Bolaria081c5c22021-09-13 08:44:531668 frame_entry, true, request->frame_tree_node(), frame_tree_.root());
Mike West800532c2021-10-14 09:26:521669 if (new_entry->GetURL().DeprecatedGetOriginAsURL() !=
1670 params.url.DeprecatedGetOriginAsURL()) {
jama78746e2017-02-22 17:21:571671 // TODO(jam): we had one report of this with a URL that was redirecting to
1672 // only tildes. Until we understand that better, don't copy the cert in
1673 // this case.
1674 new_entry->GetSSL() = SSLStatus();
jama78746e2017-02-22 17:21:571675 }
creisf49dfc92016-07-26 17:05:181676
Patrick Monette50e8bd82019-06-13 22:40:451677 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1678 // that |frame_entry| should now have a reference count of exactly 2: one
1679 // due to the local variable |frame_entry|, and another due to |new_entry|
1680 // also retaining one. This should never fail, because it's the main frame.
1681 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181682 }
1683
Harkiran Bolaria59290d62021-03-17 01:53:011684 // If this is an activation navigation from a prerendered page, transfer the
1685 // new entry from an entry already created and stored in the
1686 // NavigationRequest. |new_entry| will not have been set prior to this as
1687 // |is_same_document| is mutually exclusive with
1688 // |IsPrerenderedPageActivation|.
1689 if (request->IsPrerenderedPageActivation()) {
1690 DCHECK(!is_same_document);
1691 DCHECK(!new_entry);
1692 new_entry = request->TakePrerenderNavigationEntry();
1693 DCHECK(new_entry);
1694 }
1695
Charlie Reisc0f17d2d2021-01-12 18:52:491696 // Only make a copy of the pending entry if it is appropriate for the new
1697 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451698 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041699 // 1. The SiteInstance hasn't been assigned to something else.
1700 // 2. The pending entry was intended as a new entry, rather than being a
1701 // history navigation that was interrupted by an unrelated,
1702 // renderer-initiated navigation.
1703 // TODO(csharrison): Investigate whether we can remove some of the coarser
1704 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451705 if (!new_entry && PendingEntryMatchesRequest(request) &&
1706 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341707 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431708 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351709 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431710
Camille Lamy62b826012019-02-26 09:15:471711 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451712 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
creisf49dfc92016-07-26 17:05:181713 }
1714
Charlie Reisc0f17d2d2021-01-12 18:52:491715 // For cross-document commits with no matching pending entry, create a new
1716 // entry.
creisf49dfc92016-07-26 17:05:181717 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061718 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071719 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1720 initiator_origin,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:581721 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451722 params.transition, request->IsRendererInitiated(),
Lukasz Anforowicz435bcb582019-07-12 20:50:061723 nullptr); // blob_url_loader_factory
[email protected]f8f93eb2012-09-25 03:06:241724
1725 // Find out whether the new entry needs to update its virtual URL on URL
1726 // change and set up the entry accordingly. This is needed to correctly
1727 // update the virtual URL when replaceState is called after a pushState.
1728 GURL url = params.url;
1729 bool needs_update = false;
Charlie Reisc0f17d2d2021-01-12 18:52:491730 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411731 // update the virtual URL based on the new URL. For example, this is needed
1732 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1733 // the URL.
Rakina Zata Amni3460d382021-10-29 00:43:371734 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1735 &url, browser_context_, &needs_update);
1736 new_entry->set_update_virtual_url_with_url(needs_update);
1737
Camille Lamy62b826012019-02-26 09:15:471738 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451739 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
[email protected]e9ba4472008-09-14 15:42:431740 }
1741
Harkiran Bolaria59290d62021-03-17 01:53:011742 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
1743 // for prerendered contents, if any. This is because prerendering/activation
1744 // technically won't be creating a new document. Unlike BFCache, prerendering
1745 // creates a new NavigationEntry rather than using an existing one.
1746 if (!request->IsPrerenderedPageActivation()) {
Rakina Zata Amni3460d382021-10-29 00:43:371747 UpdateNavigationEntryDetails(new_entry.get(), rfh, params, request,
1748 NavigationEntryImpl::UpdatePolicy::kUpdate,
1749 true /* is_new_entry */);
creis8b5cd4c2015-06-19 00:11:081750
Harkiran Bolaria59290d62021-03-17 01:53:011751 // history.pushState() is classified as a navigation to a new page, but sets
1752 // is_same_document to true. In this case, we already have the title and
1753 // favicon available, so set them immediately.
1754 if (is_same_document && GetLastCommittedEntry()) {
1755 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
1756 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1757 }
[email protected]3a868f212014-08-09 10:41:191758 }
[email protected]ff64b3e2014-05-31 04:07:331759
[email protected]60d6cca2013-04-30 08:47:131760 DCHECK(!params.history_list_was_cleared || !replace_entry);
1761 // The browser requested to clear the session history when it initiated the
1762 // navigation. Now we know that the renderer has updated its state accordingly
1763 // and it is safe to also clear the browser side history.
1764 if (params.history_list_was_cleared) {
arthursonzogni69a6a1b2019-09-17 09:23:001765 DiscardNonCommittedEntries();
[email protected]60d6cca2013-04-30 08:47:131766 entries_.clear();
1767 last_committed_entry_index_ = -1;
1768 }
1769
Nasko Oskovaee2f862018-06-15 00:05:521770 // If this is a new navigation with replacement and there is a
1771 // pending_entry_ which matches the navigation reported by the renderer
1772 // process, then it should be the one replaced, so update the
1773 // last_committed_entry_index_ to use it.
1774 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101775 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521776 last_committed_entry_index_ = pending_entry_index_;
1777 }
1778
Alexander Timine3ec4192020-04-20 16:39:401779 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411780 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401781 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051782
Carlos IL42b416592019-10-07 23:10:361783 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
Dave Tapuska87696ae2021-11-18 18:48:311784 !request->post_commit_error_page_html().empty(),
1785 rfh->frame_tree_node()->IsInFencedFrameTree());
[email protected]e9ba4472008-09-14 15:42:431786}
1787
Charlie Reisc0f17d2d2021-01-12 18:52:491788void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321789 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071790 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361791 bool is_same_document,
jam48cea9082017-02-15 06:13:291792 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451793 NavigationRequest* request,
Peter Boströmd7592132019-01-30 04:50:311794 bool keep_pending_entry) {
creis26d22632017-04-21 20:23:561795 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1796 << "that a last committed entry exists.";
1797
[email protected]e9ba4472008-09-14 15:42:431798 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001799 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431800
Charlie Reis7e2cb6d2021-01-26 01:27:161801 NavigationEntryImpl* entry = nullptr;
Rakina Zata Amni153d5702021-09-13 22:48:001802 if (request->commit_params().intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:161803 // We're guaranteed to have a last committed entry if intended_as_new_entry
1804 // is true.
avicbdc4c12015-07-01 16:07:111805 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:161806 DCHECK(entry);
1807
1808 // If the NavigationRequest matches a new pending entry and is classified as
1809 // EXISTING_ENTRY, then it is a navigation to the same URL that was
1810 // converted to a reload, such as a user pressing enter in the omnibox.
1811 if (pending_entry_ && pending_entry_index_ == -1 &&
1812 pending_entry_->GetUniqueID() ==
1813 request->commit_params().nav_entry_id) {
1814 // Note: The pending entry will usually have a real ReloadType here, but
1815 // it can still be ReloadType::NONE in cases that
1816 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
1817
1818 // If we classified this correctly, the SiteInstance should not have
1819 // changed.
1820 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
1821
1822 // For converted reloads, we assign the entry's unique ID to be that of
1823 // the new one. Since this is always the result of a user action, we want
1824 // to dismiss infobars, etc. like a regular user-initiated navigation.
1825 entry->set_unique_id(pending_entry_->GetUniqueID());
1826
1827 // The extra headers may have changed due to reloading with different
1828 // headers.
1829 entry->set_extra_headers(pending_entry_->extra_headers());
1830 }
1831 // Otherwise, this was intended as a new entry but the pending entry was
1832 // lost in the meantime and no new entry was created. We are stuck at the
1833 // last committed entry.
1834
1835 // Even if this is a converted reload from pressing enter in the omnibox,
1836 // the server could redirect, requiring an update to the SSL status. If this
1837 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451838 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:161839 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:471840 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451841 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:161842 }
Rakina Zata Amnif6950d552020-11-24 03:26:101843 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:111844 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:101845 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b90ce2016-09-01 16:58:161846
eugenebut604866f2017-05-10 21:35:361847 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451848 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361849 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1850 // this was a restored same document navigation entry, then it won't have
1851 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1852 // navigated it.
jam48cea9082017-02-15 06:13:291853 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
Mike West800532c2021-10-14 09:26:521854 if (entry->GetURL().DeprecatedGetOriginAsURL() ==
1855 last_entry->GetURL().DeprecatedGetOriginAsURL() &&
jam48cea9082017-02-15 06:13:291856 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1857 was_restored) {
1858 entry->GetSSL() = last_entry->GetSSL();
1859 }
1860 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451861 // In rapid back/forward navigations |request| sometimes won't have a cert
1862 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191863 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451864 if (request->GetSSLInfo().has_value() &&
1865 request->GetSSLInfo()->is_valid()) {
1866 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
Mike West800532c2021-10-14 09:26:521867 } else if (entry->GetURL().DeprecatedGetOriginAsURL() !=
1868 request->GetURL().DeprecatedGetOriginAsURL()) {
John Abd-El-Malek3f247082017-12-07 19:02:191869 entry->GetSSL() = SSLStatus();
1870 }
jam48cea9082017-02-15 06:13:291871 }
avicbdc4c12015-07-01 16:07:111872 } else {
1873 // This is renderer-initiated. The only kinds of renderer-initated
Rakina Zata Amni557afb92021-07-17 04:39:571874 // navigations that are EXISTING_ENTRY are same-document navigations that
1875 // result in replacement (e.g. history.replaceState(), location.replace(),
1876 // forced replacements for trivial session history contexts). For these
1877 // cases, we reuse the last committed entry.
avicbdc4c12015-07-01 16:07:111878 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101879
Mikel Astizba9cf2fd2017-12-17 10:38:101880 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1881 // to avoid misleading interpretations (e.g. URLs paired with
1882 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1883 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1884 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1885
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571886 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:101887
eugenebut604866f2017-05-10 21:35:361888 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451889 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361890 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471891 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451892 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
avicbdc4c12015-07-01 16:07:111893 }
1894 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:431895
Rakina Zata Amni3460d382021-10-29 00:43:371896 UpdateNavigationEntryDetails(entry, rfh, params, request,
1897 NavigationEntryImpl::UpdatePolicy::kUpdate,
1898 false /* is_new_entry */);
creis22a7b4c2016-04-28 07:20:301899
[email protected]5ccd4dc2012-10-24 02:28:141900 // The redirected to page should not inherit the favicon from the previous
1901 // page.
eugenebut604866f2017-05-10 21:35:361902 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:481903 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:141904
Peter Boströmd7592132019-01-30 04:50:311905 // We should also usually discard the pending entry if it corresponds to a
1906 // different navigation, since that one is now likely canceled. In rare
1907 // cases, we leave the pending entry for another navigation in place when we
1908 // know it is still ongoing, to avoid a flicker in the omnibox (see
1909 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:431910 //
1911 // Note that we need to use the "internal" version since we don't want to
1912 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:311913 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001914 DiscardNonCommittedEntries();
[email protected]40bcc302009-03-02 20:50:391915
Carlos IL4dea8902020-05-26 15:14:291916 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:111917 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:431918}
1919
[email protected]d202a7c2012-01-04 07:53:471920void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:321921 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071922 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361923 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:471924 bool replace_entry,
1925 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451926 NavigationRequest* request) {
avi25f5f9e2015-07-17 20:08:261927 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1928 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
[email protected]09b8f82f2009-06-16 20:22:111929
[email protected]e9ba4472008-09-14 15:42:431930 // Manual subframe navigations just get the current entry cloned so the user
1931 // can go back or forward to it. The actual subframe information will be
1932 // stored in the page state for each of those entries. This happens out of
1933 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:091934 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1935 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:381936
Mikel Astizba9cf2fd2017-12-17 10:38:101937 // The DCHECK below documents the fact that we don't know of any situation
1938 // where |replace_entry| is true for subframe navigations. This simplifies
1939 // reasoning about the replacement struct for subframes (see
1940 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
1941 DCHECK(!replace_entry);
1942
Patrick Monette50e8bd82019-06-13 22:40:451943 // This FrameNavigationEntry might not end up being used in the
1944 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Anton Bikineevf62d1bf2021-05-15 17:56:071945 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451946 request->common_params().initiator_origin;
Patrick Monette50e8bd82019-06-13 22:40:451947 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481948 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081949 params.document_sequence_number, params.app_history_key,
1950 rfh->GetSiteInstance(), nullptr, params.url,
1951 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091952 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071953 params.page_state, params.method, params.post_id,
1954 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031955 request->web_bundle_navigation_info()
1956 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391957 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481958 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191959 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1960 request));
Charles Reisf44482022017-10-13 21:15:031961
creisce0ef3572017-01-26 17:53:081962 std::unique_ptr<NavigationEntryImpl> new_entry =
1963 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:451964 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:001965 frame_tree_.root());
creise062d542015-08-25 02:01:551966
Alexander Timine3ec4192020-04-20 16:39:401967 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411968 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401969 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:471970
creisce0ef3572017-01-26 17:53:081971 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:451972 // to replace, which can happen due to a unique name change. See
1973 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
1974 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:381975
Dave Tapuska87696ae2021-11-18 18:48:311976 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false,
1977 rfh->frame_tree_node()->IsInFencedFrameTree());
[email protected]e9ba4472008-09-14 15:42:431978}
1979
[email protected]d202a7c2012-01-04 07:53:471980bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:321981 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071982 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:391983 bool is_same_document,
Nate Chapinc7019dd7d2021-06-25 18:29:251984 bool was_on_initial_empty_document,
Mohamed Abdelhalim833de902019-09-16 17:41:451985 NavigationRequest* request) {
avi9f07a0c2015-02-18 22:51:291986 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1987 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
1988
[email protected]e9ba4472008-09-14 15:42:431989 // We're guaranteed to have a previously committed entry, and we now need to
1990 // handle navigation inside of a subframe in it without creating a new entry.
1991 DCHECK(GetLastCommittedEntry());
1992
creis913c63ce2016-07-16 19:52:521993 // For newly created subframes, we don't need to send a commit notification.
1994 // This is only necessary for history navigations in subframes.
1995 bool send_commit_notification = false;
1996
Rakina Zata Amnif6950d552020-11-24 03:26:101997 // If |nav_entry_id| is non-zero and matches an existing entry, this
1998 // is a history navigation. Update the last committed index accordingly. If
1999 // we don't recognize the |nav_entry_id|, it might be a recently
2000 // pruned entry. We'll handle it below.
2001 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2002 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472003 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062004 // Make sure that a subframe commit isn't changing the main frame's
2005 // origin. Otherwise the renderer process may be confused, leading to a
2006 // URL spoof. We can't check the path since that may change
2007 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572008 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2009 // about:blank, file, and unique origins are more subtle to get right.
Charlie Reis95fbca442021-05-21 21:38:242010 // We should use checks similar to RenderFrameHostImpl's
2011 // CanCommitUrlAndOrigin on the main frame during subframe commits.
2012 // See https://crbug.com/1209092.
creis37988b92016-06-10 18:03:572013 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2014 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2015 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2016 dest_top_url.SchemeIsHTTPOrHTTPS() &&
Mike West800532c2021-10-14 09:26:522017 current_top_url.DeprecatedGetOriginAsURL() !=
2018 dest_top_url.DeprecatedGetOriginAsURL()) {
creisfb6eeb62016-05-10 19:01:512019 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2020 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062021 }
creis3cdc3b02015-05-29 23:00:472022
creis913c63ce2016-07-16 19:52:522023 // We only need to discard the pending entry in this history navigation
2024 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062025 last_committed_entry_index_ = entry_index;
arthursonzogni69a6a1b2019-09-17 09:23:002026 DiscardNonCommittedEntries();
creis913c63ce2016-07-16 19:52:522027
2028 // History navigations should send a commit notification.
2029 send_commit_notification = true;
avi98405c22015-05-21 20:47:062030 }
[email protected]e9ba4472008-09-14 15:42:432031 }
[email protected]f233e4232013-02-23 00:55:142032
creisce0ef3572017-01-26 17:53:082033 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2034 // it may be a "history auto" case where we update an existing one.
2035 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Nate Chapin9f169072021-06-09 19:32:372036
2037 // We may want to update |last_committed|'s FrameNavigationEntry (if one
2038 // exists), or we may want to clobber it and create a new one. We update in
2039 // cases where the corresponding FrameNavigationEntry is conceptually similar
2040 // to the document described by the commit params: same-document
2041 // navigations, history traversal to an existing entry, and reloads (including
2042 // a "soft reload" where we navigate to the same url without flagging it as a
2043 // reload). But in the case of a different document that is not logically
2044 // related to the committed FrameNavigationEntry's document (cross-document,
2045 // not same url, not a reload, not a history traversal), we replace rather
2046 // than update.
Nate Chapinc7019dd7d2021-06-25 18:29:252047 //
Nate Chapin9f169072021-06-09 19:32:372048 // In the case where we update, the FrameNavigationEntry will potentially be
2049 // shared across multiple NavigationEntries, and any updates will be reflected
2050 // in all of those NavigationEntries. In the replace case, any existing
2051 // sharing with other NavigationEntries will stop.
Nate Chapinc7019dd7d2021-06-25 18:29:252052 //
2053 // When navigating away from the initial empty document, we also update rather
2054 // than replace. Either update or replace will overwrite the initial empty
2055 // document state for |last_committed|, but if the FrameNavigationEntry for
2056 // the initial empty document is shared across multiple NavigationEntries (due
2057 // to a navigation in another frame), we want to make sure we overwrite the
2058 // initial empty document state everywhere this FrameNavigationEntry is used,
2059 // which is accompished by updating the existing FrameNavigationEntry.
Nate Chapin9f169072021-06-09 19:32:372060 FrameNavigationEntry* last_committed_frame_entry =
2061 last_committed->GetFrameEntry(rfh->frame_tree_node());
2062 NavigationEntryImpl::UpdatePolicy update_policy =
2063 NavigationEntryImpl::UpdatePolicy::kUpdate;
2064 if (request->common_params().navigation_type ==
Minggang Wangb9f3fa92021-07-01 15:30:312065 blink::mojom::NavigationType::DIFFERENT_DOCUMENT &&
Nate Chapin9f169072021-06-09 19:32:372066 last_committed_frame_entry &&
Nate Chapinc7019dd7d2021-06-25 18:29:252067 last_committed_frame_entry->url() != params.url &&
2068 !was_on_initial_empty_document) {
Nate Chapin9f169072021-06-09 19:32:372069 update_policy = NavigationEntryImpl::UpdatePolicy::kReplace;
2070 }
2071
Rakina Zata Amni3460d382021-10-29 00:43:372072 UpdateNavigationEntryDetails(last_committed, rfh, params, request,
2073 update_policy, false /* is_new_entry */);
creis625a0c7d2015-03-24 23:17:122074
creis913c63ce2016-07-16 19:52:522075 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432076}
2077
[email protected]d202a7c2012-01-04 07:53:472078int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232079 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032080 for (size_t i = 0; i < entries_.size(); ++i) {
2081 if (entries_[i].get() == entry)
2082 return i;
2083 }
2084 return -1;
[email protected]765b35502008-08-21 00:51:202085}
2086
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572087void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242088 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572089 NavigationControllerImpl* source =
2090 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572091 // Verify that we look new.
Lei Zhang96031532019-10-10 19:05:472092 DCHECK_EQ(0, GetEntryCount());
2093 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:572094
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572095 if (source->GetEntryCount() == 0)
[email protected]ce3fa3c2009-04-20 19:55:572096 return; // Nothing new to do.
2097
Francois Dorayeaace782017-06-21 16:37:242098 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442099 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572100 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572101
Fergal Dalya1d569972021-03-16 03:24:532102 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302103 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532104 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2105 session_storage_namespace_map_[it.first] = source_namespace->Clone();
Aaron Colwellb731a0ae2021-03-19 19:14:472106 OnStoragePartitionIdAdded(it.first);
[email protected]fdac6ade2013-07-20 01:06:302107 }
[email protected]4e6419c2010-01-15 04:50:342108
Lukasz Anforowicz0de0f452020-12-02 19:57:152109 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572110}
2111
Aran Gilman37d11632019-10-08 23:07:152112void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2113 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162114 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012115 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162116
[email protected]d202a7c2012-01-04 07:53:472117 NavigationControllerImpl* source =
2118 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252119
avi2b177592014-12-10 02:08:022120 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012121 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252122
[email protected]474f8512013-05-31 22:31:162123 // We now have one entry, possibly with a new pending entry. Ensure that
2124 // adding the entries from source won't put us over the limit.
2125 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572126 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572127 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252128
Carlos IL4dea8902020-05-26 15:14:292129 // Insert the entries from source. Ignore any pending entry, since it has not
2130 // committed in source.
[email protected]474f8512013-05-31 22:31:162131 int max_source_index = source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:252132 if (max_source_index == -1)
[email protected]a26023822011-12-29 00:23:552133 max_source_index = source->GetEntryCount();
[email protected]e1cd5452010-08-26 18:03:252134 else
2135 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572136
2137 // Ignore the source's current entry if merging with replacement.
2138 // TODO(davidben): This should preserve entries forward of the current
2139 // too. http://crbug.com/317872
2140 if (replace_entry && max_source_index > 0)
2141 max_source_index--;
2142
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572143 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252144
2145 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552146 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142147
Hayato Ito2c8c08d02021-06-23 03:38:432148 BroadcastHistoryOffsetAndLength();
[email protected]e1cd5452010-08-26 18:03:252149}
2150
[email protected]79368982013-11-13 01:11:012151bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162152 // If there is no last committed entry, we cannot prune. Even if there is a
2153 // pending entry, it may not commit, leaving this WebContents blank, despite
2154 // possibly giving it new entries via CopyStateFromAndPrune.
2155 if (last_committed_entry_index_ == -1)
2156 return false;
[email protected]9350602e2013-02-26 23:27:442157
[email protected]474f8512013-05-31 22:31:162158 // We cannot prune if there is a pending entry at an existing entry index.
2159 // It may not commit, so we have to keep the last committed entry, and thus
2160 // there is no sensible place to keep the pending entry. It is ok to have
2161 // a new pending entry, which can optionally commit as a new navigation.
2162 if (pending_entry_index_ != -1)
2163 return false;
2164
[email protected]474f8512013-05-31 22:31:162165 return true;
2166}
2167
[email protected]79368982013-11-13 01:11:012168void NavigationControllerImpl::PruneAllButLastCommitted() {
2169 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162170
avi2b177592014-12-10 02:08:022171 DCHECK_EQ(0, last_committed_entry_index_);
2172 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442173
Hayato Ito2c8c08d02021-06-23 03:38:432174 BroadcastHistoryOffsetAndLength();
[email protected]9350602e2013-02-26 23:27:442175}
2176
[email protected]79368982013-11-13 01:11:012177void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162178 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012179 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262180
[email protected]474f8512013-05-31 22:31:162181 // Erase all entries but the last committed entry. There may still be a
2182 // new pending entry after this.
2183 entries_.erase(entries_.begin(),
2184 entries_.begin() + last_committed_entry_index_);
2185 entries_.erase(entries_.begin() + 1, entries_.end());
2186 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262187}
2188
Christian Dullweber1af31e62018-02-22 11:49:482189void NavigationControllerImpl::DeleteNavigationEntries(
2190 const DeletionPredicate& deletionPredicate) {
2191 // It is up to callers to check the invariants before calling this.
2192 CHECK(CanPruneAllButLastCommitted());
2193 std::vector<int> delete_indices;
2194 for (size_t i = 0; i < entries_.size(); i++) {
2195 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572196 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482197 delete_indices.push_back(i);
2198 }
2199 }
2200 if (delete_indices.empty())
2201 return;
2202
2203 if (delete_indices.size() == GetEntryCount() - 1U) {
2204 PruneAllButLastCommitted();
2205 } else {
2206 // Do the deletion in reverse to preserve indices.
2207 for (auto it = delete_indices.rbegin(); it != delete_indices.rend(); ++it) {
2208 RemoveEntryAtIndex(*it);
2209 }
Hayato Ito2c8c08d02021-06-23 03:38:432210 BroadcastHistoryOffsetAndLength();
Christian Dullweber1af31e62018-02-22 11:49:482211 }
2212 delegate()->NotifyNavigationEntriesDeleted();
2213}
2214
Shivani Sharma883f5f32019-02-12 18:20:012215bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2216 auto* entry = GetEntryAtIndex(index);
2217 return entry && entry->should_skip_on_back_forward_ui();
2218}
2219
Carlos Caballero35ce710c2019-09-19 10:59:452220BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2221 return back_forward_cache_;
2222}
2223
clamy987a3752018-05-03 17:36:262224void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2225 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2226 // progress, since this will cause a use-after-free. (We only allow this
2227 // when the tab is being destroyed for shutdown, since it won't return to
2228 // NavigateToEntry in that case.) http://crbug.com/347742.
Takashi Toyoshimaea534ef22021-07-21 03:27:592229 CHECK(!in_navigate_to_pending_entry_ || frame_tree_.IsBeingDestroyed());
clamy987a3752018-05-03 17:36:262230
2231 if (was_failure && pending_entry_) {
2232 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2233 } else {
2234 failed_pending_entry_id_ = 0;
2235 }
2236
2237 if (pending_entry_) {
2238 if (pending_entry_index_ == -1)
2239 delete pending_entry_;
2240 pending_entry_index_ = -1;
2241 pending_entry_ = nullptr;
2242 }
arthursonzogni66f711c2019-10-08 14:40:362243
2244 // Ensure any refs to the current pending entry are ignored if they get
2245 // deleted, by clearing the set of known refs. All future pending entries will
2246 // only be affected by new refs.
2247 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262248}
2249
2250void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2251 if (pending_entry_)
2252 pending_entry_->set_ssl_error(error);
2253}
2254
Camille Lamy5193caa2018-10-12 11:59:422255#if defined(OS_ANDROID)
2256// static
2257bool NavigationControllerImpl::ValidateDataURLAsString(
2258 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2259 if (!data_url_as_string)
2260 return false;
2261
2262 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2263 return false;
2264
2265 // The number of characters that is enough for validating a data: URI.
2266 // From the GURL's POV, the only important part here is scheme, it doesn't
2267 // check the actual content. Thus we can take only the prefix of the url, to
2268 // avoid unneeded copying of a potentially long string.
2269 const size_t kDataUriPrefixMaxLen = 64;
2270 GURL data_url(
2271 std::string(data_url_as_string->front_as<char>(),
2272 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2273 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2274 return false;
2275
2276 return true;
2277}
2278#endif
2279
Shivani Sharma194877032019-03-07 17:52:472280void NavigationControllerImpl::NotifyUserActivation() {
2281 // When a user activation occurs, ensure that all adjacent entries for the
2282 // same document clear their skippable bit, so that the history manipulation
2283 // intervention does not apply to them.
2284 auto* last_committed_entry = GetLastCommittedEntry();
2285 if (!last_committed_entry)
2286 return;
Shivani Sharma194877032019-03-07 17:52:472287
shivanigithub99368382021-06-16 18:33:372288 if (base::FeatureList::IsEnabled(
2289 features::kDebugHistoryInterventionNoUserActivation)) {
2290 return;
2291 }
2292
Shivani Sharmac4cc8922019-04-18 03:11:172293 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472294}
2295
clamy987a3752018-05-03 17:36:262296bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2297 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172298 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262299 NavigationEntryImpl* entry =
2300 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2301 if (!entry)
2302 return false;
2303
2304 FrameNavigationEntry* frame_entry =
2305 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2306 if (!frame_entry)
2307 return false;
2308
Nate Chapin45f620582021-09-30 17:45:432309 // |is_browser_initiated| is false here because a navigation in a new subframe
2310 // always begins with renderer action (i.e., an HTML element being inserted
2311 // into the DOM), so it is always renderer-initiated.
Camille Lamy5193caa2018-10-12 11:59:422312 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572313 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232314 ReloadType::NONE, false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432315 true /* is_history_navigation_in_new_child */,
2316 false /* is_browser_initiated */);
clamyea99ea12018-05-28 13:54:232317
2318 if (!request)
2319 return false;
2320
arthursonzognif046d4a2019-12-12 19:08:102321 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412322
Lukasz Anforowicz9ee83c272020-12-01 20:14:052323 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2324 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232325
2326 return true;
clamy987a3752018-05-03 17:36:262327}
2328
Tsuyoshi Horo52fd08e2020-07-07 07:03:452329bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2330 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2331 if (!entry)
2332 return false;
2333 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2334 if (!frame_entry)
2335 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142336 ReloadType reload_type = ReloadType::NORMAL;
2337 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452338 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142339 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452340 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432341 false /* is_history_navigation_in_new_child */,
2342 true /* is_browser_initiated */);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452343 if (!request)
2344 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052345 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452346 return true;
2347}
2348
Dave Tapuska8bfd84c2019-03-26 20:47:162349void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2350 int offset,
2351 int sandbox_frame_tree_node_id) {
2352 if (!CanGoToOffset(offset))
2353 return;
Nate Chapin45f620582021-09-30 17:45:432354 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id,
2355 false /* is_browser_initiated */);
Dave Tapuska8bfd84c2019-03-26 20:47:162356}
2357
clamy987a3752018-05-03 17:36:262358void NavigationControllerImpl::NavigateFromFrameProxy(
2359 RenderFrameHostImpl* render_frame_host,
2360 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022361 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452362 int initiator_process_id,
Anton Bikineevf62d1bf2021-05-15 17:56:072363 const absl::optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262364 bool is_renderer_initiated,
2365 SiteInstance* source_site_instance,
2366 const Referrer& referrer,
2367 ui::PageTransition page_transition,
2368 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252369 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262370 const std::string& method,
2371 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092372 const std::string& extra_headers,
Antonio Sartori2f763d9d2021-04-21 10:04:142373 network::mojom::SourceLocationPtr source_location,
John Delaney50425f82020-04-07 16:26:212374 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Anton Bikineevf62d1bf2021-05-15 17:56:072375 const absl::optional<blink::Impression>& impression) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582376 if (is_renderer_initiated)
2377 DCHECK(initiator_origin.has_value());
2378
clamy987a3752018-05-03 17:36:262379 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582380
Rakina Zata Amni66992a82020-06-24 03:57:522381 // Don't allow an entry replacement if there is no entry to replace.
2382 // http://crbug.com/457149
2383 if (GetEntryCount() == 0)
2384 should_replace_current_entry = false;
2385
clamy987a3752018-05-03 17:36:262386 // Create a NavigationEntry for the transfer, without making it the pending
2387 // entry. Subframe transfers should have a clone of the last committed entry
2388 // with a FrameNavigationEntry for the target frame. Main frame transfers
2389 // should have a new NavigationEntry.
2390 // TODO(creis): Make this unnecessary by creating (and validating) the params
2391 // directly, passing them to the destination RenderFrameHost. See
2392 // https://crbug.com/536906.
2393 std::unique_ptr<NavigationEntryImpl> entry;
Harkiran Bolariae1b5158b2021-09-16 19:03:262394 if (!render_frame_host->is_main_frame()) {
clamy987a3752018-05-03 17:36:262395 // Subframe case: create FrameNavigationEntry.
2396 if (GetLastCommittedEntry()) {
2397 entry = GetLastCommittedEntry()->Clone();
2398 entry->set_extra_headers(extra_headers);
2399 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2400 // Renderer-initiated navigation that target a remote frame are currently
2401 // classified as browser-initiated when this one has already navigated.
2402 // See https://crbug.com/722251.
2403 } else {
2404 // If there's no last committed entry, create an entry for about:blank
2405 // with a subframe entry for our destination.
2406 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2407 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:062408 GURL(url::kAboutBlankURL), referrer, initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:102409 source_site_instance, page_transition, is_renderer_initiated,
2410 extra_headers, browser_context_,
Hayato Ito303654c2021-06-30 09:07:542411 nullptr /* blob_url_loader_factory */));
clamy987a3752018-05-03 17:36:262412 }
Nate Chapin9f169072021-06-09 19:32:372413 // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter
2414 // to CreateNavigationRequestFromLoadParams(), so while kReplace might
2415 // remove child FrameNavigationEntries (e.g., if this is a cross-process
2416 // same-document navigation), they will still be present in the
2417 // committed NavigationEntry that will be referenced to construct the new
2418 // FrameNavigationEntry tree when this navigation commits.
clamy987a3752018-05-03 17:36:262419 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:082420 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582421 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072422 absl::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202423 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392424 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482425 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori79d549d2021-02-18 12:59:542426 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262427 } else {
2428 // Main frame case.
2429 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102430 url, referrer, initiator_origin, source_site_instance, page_transition,
2431 is_renderer_initiated, extra_headers, browser_context_,
Hayato Ito303654c2021-06-30 09:07:542432 blob_url_loader_factory));
clamy987a3752018-05-03 17:36:262433 entry->root_node()->frame_entry->set_source_site_instance(
2434 static_cast<SiteInstanceImpl*>(source_site_instance));
2435 entry->root_node()->frame_entry->set_method(method);
2436 }
clamy987a3752018-05-03 17:36:262437
Camille Lamy5193caa2018-10-12 11:59:422438 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262439 if (GetLastCommittedEntry() &&
2440 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2441 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422442 override_user_agent = true;
clamy987a3752018-05-03 17:36:262443 }
2444 // TODO(creis): Set user gesture and intent received timestamp on Android.
2445
2446 // We may not have successfully added the FrameNavigationEntry to |entry|
2447 // above (per https://crbug.com/608402), in which case we create it from
2448 // scratch. This works because we do not depend on |frame_entry| being inside
2449 // |entry| during NavigateToEntry. This will go away when we shortcut this
2450 // further in https://crbug.com/536906.
2451 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2452 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452453 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Nate Chapinfbfe5af2021-06-10 17:22:082454 node->unique_name(), -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582455 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072456 absl::nullopt /* origin */, referrer, initiator_origin,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:122457 std::vector<GURL>(), blink::PageState(), method, -1,
2458 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482459 nullptr /* subresource_web_bundle_navigation_info */,
Titouan Rigoudy6ec70402021-02-02 15:42:192460 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262461 }
2462
Camille Lamy5193caa2018-10-12 11:59:422463 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022464 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452465 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292466 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422467 params.source_site_instance = source_site_instance;
2468 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2469 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032470 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422471 params.referrer = referrer;
2472 /* params.redirect_chain: skip */
2473 params.extra_headers = extra_headers;
2474 params.is_renderer_initiated = is_renderer_initiated;
2475 params.override_user_agent = UA_OVERRIDE_INHERIT;
2476 /* params.base_url_for_data_url: skip */
2477 /* params.virtual_url_for_data_url: skip */
2478 /* params.data_url_as_string: skip */
2479 params.post_data = post_body;
2480 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582481 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422482 /* params.frame_name: skip */
2483 // TODO(clamy): See if user gesture should be propagated to this function.
2484 params.has_user_gesture = false;
2485 params.should_clear_history_list = false;
2486 params.started_from_context_menu = false;
2487 /* params.navigation_ui_data: skip */
2488 /* params.input_start: skip */
Minggang Wangf59db47b2021-06-16 01:56:222489 params.was_activated = blink::mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542490 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212491 params.impression = impression;
Antonio Sartori6984c742021-08-26 08:03:412492 params.download_policy = std::move(download_policy);
Camille Lamy5193caa2018-10-12 11:59:422493
2494 std::unique_ptr<NavigationRequest> request =
2495 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032496 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:142497 false /* has_user_gesture */, std::move(source_location),
Antonio Sartori6984c742021-08-26 08:03:412498 ReloadType::NONE, entry.get(), frame_entry.get());
clamyea99ea12018-05-28 13:54:232499
2500 if (!request)
2501 return;
2502
Arthur Hemery948742762019-09-18 10:06:242503 // At this stage we are proceeding with this navigation. If this was renderer
2504 // initiated with user gesture, we need to make sure we clear up potential
2505 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2506 DiscardNonCommittedEntries();
2507
Lukasz Anforowicz9ee83c272020-12-01 20:14:052508 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262509}
2510
[email protected]d1198fd2012-08-13 22:50:192511void NavigationControllerImpl::SetSessionStorageNamespace(
Aaron Colwellf3b316e2021-03-11 20:17:052512 const StoragePartitionId& partition_id,
[email protected]8ff00d72012-10-23 19:12:212513 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192514 if (!session_storage_namespace)
2515 return;
2516
2517 // We can't overwrite an existing SessionStorage without violating spec.
2518 // Attempts to do so may give a tab access to another tab's session storage
2519 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152520 bool successful_insert =
2521 session_storage_namespace_map_
Aaron Colwellf3b316e2021-03-11 20:17:052522 .insert(std::make_pair(partition_id,
2523 static_cast<SessionStorageNamespaceImpl*>(
2524 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302525 .second;
2526 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
Aaron Colwellb731a0ae2021-03-19 19:14:472527 OnStoragePartitionIdAdded(partition_id);
[email protected]d1198fd2012-08-13 22:50:192528}
2529
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572530bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Aran Gilman37d11632019-10-08 23:07:152531 return IsInitialNavigation() && !GetLastCommittedEntry() &&
Carlos Caballeroede6f8c2021-01-28 11:01:502532 !frame_tree_.has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462533}
2534
Aran Gilman37d11632019-10-08 23:07:152535SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Aaron Colwell78b4bde2021-03-16 16:16:092536 const SiteInfo& site_info) {
2537 // TODO(acolwell): Remove partition_id logic once we have successfully
2538 // migrated the implementation to be a StoragePartitionConfig. At that point
2539 // |site_info| can be replaced with a StoragePartitionConfig.
2540 const StoragePartitionId partition_id =
2541 site_info.GetStoragePartitionId(browser_context_);
2542 const StoragePartitionConfig partition_config =
Robbie McElrath7d4bd852021-07-24 04:02:192543 site_info.storage_partition_config();
[email protected]d1198fd2012-08-13 22:50:192544
[email protected]fdac6ade2013-07-20 01:06:302545 StoragePartition* partition =
Lukasz Anforowiczb9a969a2021-04-29 15:26:252546 browser_context_->GetStoragePartition(partition_config);
michaelnbacbcbd2016-02-09 00:32:032547 DOMStorageContextWrapper* context_wrapper =
2548 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2549
2550 SessionStorageNamespaceMap::const_iterator it =
2551 session_storage_namespace_map_.find(partition_id);
2552 if (it != session_storage_namespace_map_.end()) {
2553 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152554 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2555 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:472556
2557 // Verify that the config we generated now matches the one that
2558 // was generated when the namespace was added to the map.
Aaron Colwellb731a0ae2021-03-19 19:14:472559 if (partition_config != it->first.config()) {
2560 LogStoragePartitionIdCrashKeys(it->first, partition_id);
2561 }
Sharon Yangd4f48792021-06-30 17:02:532562 CHECK_EQ(partition_config, it->first.config());
Aaron Colwellb731a0ae2021-03-19 19:14:472563
michaelnbacbcbd2016-02-09 00:32:032564 return it->second.get();
2565 }
2566
2567 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102568 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2569 SessionStorageNamespaceImpl::Create(context_wrapper);
2570 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2571 session_storage_namespace.get();
2572 session_storage_namespace_map_[partition_id] =
2573 std::move(session_storage_namespace);
Aaron Colwellb731a0ae2021-03-19 19:14:472574 OnStoragePartitionIdAdded(partition_id);
[email protected]fdac6ade2013-07-20 01:06:302575
Daniel Murphy31bbb8b12018-02-07 21:44:102576 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302577}
2578
2579SessionStorageNamespace*
2580NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Robbie McElrath7d4bd852021-07-24 04:02:192581 return GetSessionStorageNamespace(SiteInfo(GetBrowserContext()));
[email protected]fdac6ade2013-07-20 01:06:302582}
2583
2584const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572585NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302586 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552587}
[email protected]d202a7c2012-01-04 07:53:472588
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572589bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562590 return needs_reload_;
2591}
[email protected]a26023822011-12-29 00:23:552592
[email protected]46bb5e9c2013-10-03 22:16:472593void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412594 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2595}
2596
2597void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472598 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412599 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542600
2601 if (last_committed_entry_index_ != -1) {
2602 entries_[last_committed_entry_index_]->SetTransitionType(
2603 ui::PAGE_TRANSITION_RELOAD);
2604 }
[email protected]46bb5e9c2013-10-03 22:16:472605}
2606
[email protected]d202a7c2012-01-04 07:53:472607void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572608 DCHECK_LT(index, GetEntryCount());
2609 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312610 DiscardNonCommittedEntries();
2611
2612 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122613 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312614 last_committed_entry_index_--;
2615}
2616
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572617NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272618 // If there is no pending_entry_, there should be no pending_entry_index_.
2619 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2620
2621 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362622 // at that index. An exception is while a reload of a post commit error page
2623 // is ongoing; in that case pending entry will point to the entry replaced
2624 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272625 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362626 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2627 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272628
[email protected]022af742011-12-28 18:37:252629 return pending_entry_;
2630}
2631
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572632int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272633 // The pending entry index must always be less than the number of entries.
2634 // If there are no entries, it must be exactly -1.
2635 DCHECK_LT(pending_entry_index_, GetEntryCount());
2636 DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:552637 return pending_entry_index_;
2638}
2639
avi25764702015-06-23 15:43:372640void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572641 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362642 bool replace,
Dave Tapuska87696ae2021-11-18 18:48:312643 bool was_post_commit_error,
2644 bool in_fenced_frame_tree) {
2645 // Fenced frame trees should always have `ui::PAGE_TRANSITION_AUTO_SUBFRAME`
2646 // set because:
2647 // 1) They don't influence the history of the outer page.
2648 // 2) They are always replace only navigation (there is always only one entry
2649 // in their history stack).
2650 // 3) Are not top level navigations and appear similar to iframes.
2651 // Navigations of the fenced frame might create a new NavigationEntry, which
2652 // will call this function. Non fenced frame navigations will never have
2653 // `ui::PAGE_TRANSITION_AUTO_SUBFRAME` because they won't call
2654 // InsertOrReplaceEntry.
2655 DCHECK_EQ(in_fenced_frame_tree,
2656 ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2657 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202658
avi5cad4912015-06-19 05:25:442659 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2660 // need to keep continuity with the pending entry, so copy the pending entry's
2661 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2662 // then the renderer navigated on its own, independent of the pending entry,
2663 // so don't copy anything.
2664 if (pending_entry_ && pending_entry_index_ == -1)
2665 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202666
arthursonzogni69a6a1b2019-09-17 09:23:002667 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:202668
creisee17e932015-07-17 17:56:222669 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362670 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102671 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572672 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362673 // If the new entry is a post-commit error page, we store the current last
2674 // committed entry to the side so that we can put it back when navigating
2675 // away from the error.
2676 if (was_post_commit_error) {
2677 DCHECK(!entry_replaced_by_post_commit_error_);
2678 entry_replaced_by_post_commit_error_ =
2679 std::move(entries_[last_committed_entry_index_]);
2680 }
dcheng36b6aec92015-12-26 06:16:362681 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222682 return;
2683 }
[email protected]765b35502008-08-21 00:51:202684
creis37979a62015-08-04 19:48:182685 // We shouldn't see replace == true when there's no committed entries.
2686 DCHECK(!replace);
2687
Michael Thiessen9b14d512019-09-23 21:19:472688 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202689
Shivani Sharmad8c8d652019-02-13 17:27:572690 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202691
dcheng36b6aec92015-12-26 06:16:362692 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202693 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292694}
2695
Shivani Sharmad8c8d652019-02-13 17:27:572696void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162697 if (entries_.size() < max_entry_count())
2698 return;
2699
2700 DCHECK_EQ(max_entry_count(), entries_.size());
2701 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572702 CHECK_EQ(pending_entry_index_, -1);
2703
2704 int index = 0;
Elly Fong-Jonesccc6d1f2021-06-14 18:32:422705 // Retrieve the oldest skippable entry.
2706 for (; index < GetEntryCount(); index++) {
2707 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2708 break;
Shivani Sharmad8c8d652019-02-13 17:27:572709 }
2710
2711 // If there is no skippable entry or if it is the last committed entry then
2712 // fall back to pruning the oldest entry. It is not safe to prune the last
2713 // committed entry.
2714 if (index == GetEntryCount() || index == last_committed_entry_index_)
2715 index = 0;
2716
2717 bool should_succeed = RemoveEntryAtIndex(index);
2718 DCHECK_EQ(true, should_succeed);
2719
2720 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252721}
2722
clamy3cb9bea92018-07-10 12:42:022723void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162724 ReloadType reload_type,
Nate Chapin45f620582021-09-30 17:45:432725 int sandboxed_source_frame_tree_node_id,
2726 bool is_browser_initiated) {
Alexander Timin3a92df72019-09-20 11:59:502727 TRACE_EVENT0("navigation",
2728 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272729 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022730 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362731 if (pending_entry_index_ != -1) {
2732 // The pending entry may not be in entries_ if a post-commit error page is
2733 // showing.
2734 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2735 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2736 }
Gyuyoung Kim107c2a02021-04-13 01:49:302737 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572738 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:012739 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:002740 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:572741 int nav_entry_id = pending_entry_->GetUniqueID();
2742
[email protected]83c2e232011-10-07 21:36:462743 // If we were navigating to a slow-to-commit page, and the user performs
2744 // a session history navigation to the last committed page, RenderViewHost
2745 // will force the throbber to start, but WebKit will essentially ignore the
2746 // navigation, and won't send a message to stop the throbber. To prevent this
2747 // from happening, we drop the navigation here and stop the slow-to-commit
2748 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022749 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:082750 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:272751 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
Takashi Toyoshimabf549472021-07-01 07:47:202752 frame_tree_.StopLoading();
[email protected]6a13a6c2011-12-20 21:47:122753
[email protected]83c2e232011-10-07 21:36:462754 DiscardNonCommittedEntries();
2755 return;
2756 }
2757
creisce0ef3572017-01-26 17:53:082758 // Compare FrameNavigationEntries to see which frames in the tree need to be
2759 // navigated.
clamy3cb9bea92018-07-10 12:42:022760 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2761 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Nate Chapin45f620582021-09-30 17:45:432762 FindFramesToNavigate(root, reload_type, is_browser_initiated,
2763 &same_document_loads, &different_document_loads);
creis4e2ecb72015-06-20 00:46:302764
2765 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572766 // We were unable to match any frames to navigate. This can happen if a
2767 // history navigation targets a subframe that no longer exists
2768 // (https://crbug.com/705550). In this case, we need to update the current
2769 // history entry to the pending one but keep the main document loaded. We
2770 // also need to ensure that observers are informed about the updated
2771 // current history entry (e.g., for greying out back/forward buttons), and
2772 // that renderer processes update their history offsets. The easiest way
2773 // to do all that is to schedule a "redundant" same-document navigation in
2774 // the main frame.
2775 //
2776 // Note that we don't want to remove this history entry, as it might still
2777 // be valid later, since a frame that it's targeting may be recreated.
2778 //
2779 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
2780 // need to repeat history navigations until finding the one that works.
2781 // Consider changing this behavior to keep looking for the first valid
2782 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:022783 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422784 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572785 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:572786 ReloadType::NONE /* reload_type */,
2787 true /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432788 false /* is_history_navigation_in_new_child */,
2789 is_browser_initiated);
clamy3cb9bea92018-07-10 12:42:022790 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572791 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:022792 DiscardPendingEntry(false);
2793 return;
2794 }
Alex Moshchuk3a4e77a2020-05-29 21:32:572795 same_document_loads.push_back(std::move(navigation_request));
2796
2797 // Sanity check that we never take this branch for any kinds of reloads,
2798 // as those should've queued a different-document load in the main frame.
2799 DCHECK(!is_forced_reload);
2800 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:302801 }
2802
Dave Tapuska8bfd84c2019-03-26 20:47:162803 // If |sandboxed_source_frame_node_id| is valid, then track whether this
2804 // navigation affects any frame outside the frame's subtree.
2805 if (sandboxed_source_frame_tree_node_id !=
2806 FrameTreeNode::kFrameTreeNodeInvalidId) {
2807 bool navigates_inside_tree =
2808 DoesSandboxNavigationStayWithinSubtree(
2809 sandboxed_source_frame_tree_node_id, same_document_loads) &&
2810 DoesSandboxNavigationStayWithinSubtree(
2811 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:502812 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:162813 // the number of pages that trigger this.
2814 FrameTreeNode* sandbox_source_frame_tree_node =
2815 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
2816 if (sandbox_source_frame_tree_node) {
2817 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
2818 sandbox_source_frame_tree_node->current_frame_host(),
2819 navigates_inside_tree
2820 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
2821 : blink::mojom::WebFeature::
2822 kSandboxBackForwardAffectsFramesOutsideSubtree);
2823 }
Dave Tapuska855c1e12019-08-23 20:45:522824
2825 // If the navigation occurred outside the tree discard it because
2826 // the sandboxed frame didn't have permission to navigate outside
2827 // its tree. If it is possible that the navigation is both inside and
2828 // outside the frame tree and we discard it entirely because we don't
2829 // want to end up in a history state that didn't exist before.
2830 if (base::FeatureList::IsEnabled(
2831 features::kHistoryPreventSandboxedNavigation) &&
2832 !navigates_inside_tree) {
2833 DiscardPendingEntry(false);
2834 return;
2835 }
Dave Tapuska8bfd84c2019-03-26 20:47:162836 }
2837
Carlos Caballero539a421c2020-07-06 10:25:572838 // BackForwardCache:
2839 // Navigate immediately if the document is in the BackForwardCache.
2840 if (back_forward_cache_.GetEntry(nav_entry_id)) {
2841 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
2842 DCHECK_EQ(reload_type, ReloadType::NONE);
2843 auto navigation_request = CreateNavigationRequestFromEntry(
2844 root, pending_entry_, pending_entry_->GetFrameEntry(root),
2845 ReloadType::NONE, false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432846 false /* is_history_navigation_in_new_child */, is_browser_initiated);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052847 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:572848
2849 return;
2850 }
2851
2852 // History navigation might try to reuse a specific BrowsingInstance, already
2853 // used by a page in the cache. To avoid having two different main frames that
2854 // live in the same BrowsingInstance, evict the all pages with this
2855 // BrowsingInstance from the cache.
2856 //
2857 // For example, take the following scenario:
2858 //
2859 // A1 = Some page on a.com
2860 // A2 = Some other page on a.com
2861 // B3 = An uncacheable page on b.com
2862 //
2863 // Then the following navigations occur:
2864 // A1->A2->B3->A1
2865 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
2866 // take its place) and A1 will be created in the same BrowsingInstance (and
2867 // SiteInstance), as A2.
2868 //
2869 // If we didn't do anything, both A1 and A2 would remain alive in the same
2870 // BrowsingInstance/SiteInstance, which is unsupported by
2871 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
2872 // A2 from the cache.
2873 if (pending_entry_->site_instance()) {
2874 back_forward_cache_.EvictFramesInRelatedSiteInstances(
2875 pending_entry_->site_instance());
2876 }
2877
clamy3cb9bea92018-07-10 12:42:022878 // This call does not support re-entrancy. See http://crbug.com/347742.
2879 CHECK(!in_navigate_to_pending_entry_);
2880 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:302881
arthursonzogni66f711c2019-10-08 14:40:362882 // It is not possible to delete the pending NavigationEntry while navigating
2883 // to it. Grab a reference to delay potential deletion until the end of this
2884 // function.
2885 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
2886
creis4e2ecb72015-06-20 00:46:302887 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:022888 for (auto& item : same_document_loads) {
2889 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052890 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302891 }
clamy3cb9bea92018-07-10 12:42:022892 for (auto& item : different_document_loads) {
2893 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052894 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302895 }
clamy3cb9bea92018-07-10 12:42:022896
2897 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:302898}
2899
Alex Moshchuk3a4e77a2020-05-29 21:32:572900NavigationControllerImpl::HistoryNavigationAction
2901NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:302902 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:572903 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:422904 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:582905 // Only active and prerendered documents are allowed to navigate in their
2906 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422907 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:012908 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:582909 // - If the document is in pending deletion, the browser already committed
2910 // to destroying this RenderFrameHost. See https://crbug.com/930278.
2911 // - If the document is in back-forward cache, it's not allowed to navigate
2912 // as it should remain frozen. Ignore the request and evict the document
2913 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422914 //
Sreeja Kamishettydb8e2892021-03-10 09:30:582915 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:422916 // which should all be inactive as well.
Fergal Daly1336ac642021-09-14 15:13:112917 if (frame->current_frame_host()->IsInactiveAndDisallowActivation(
2918 DisallowActivationReasonId::kDetermineActionForHistoryNavigation)) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:422919 return HistoryNavigationAction::kStopLooking;
Fergal Daly1336ac642021-09-14 15:13:112920 }
Sreeja Kamishetty8eacabb2021-03-09 11:45:422921 }
arthursonzogni03f76152019-02-12 10:35:202922
Alex Moshchuk3a4e77a2020-05-29 21:32:572923 // If there's no last committed entry, there is no previous history entry to
2924 // compare against, so fall back to a different-document load. Note that we
2925 // should only reach this case for the root frame and not descend further
2926 // into subframes.
2927 if (!GetLastCommittedEntry()) {
2928 DCHECK(frame->IsMainFrame());
2929 return HistoryNavigationAction::kDifferentDocument;
2930 }
2931
2932 // Reloads should result in a different-document load. Note that reloads may
2933 // also happen via the |needs_reload_| mechanism where the reload_type is
2934 // NONE, so detect this by comparing whether we're going to the same
2935 // entry that we're currently on. Similarly to above, only main frames
2936 // should reach this. Note that subframes support reloads, but that's done
2937 // via a different path that doesn't involve FindFramesToNavigate (see
2938 // RenderFrameHost::Reload()).
2939 if (reload_type != ReloadType::NONE ||
2940 pending_entry_index_ == last_committed_entry_index_) {
2941 DCHECK(frame->IsMainFrame());
2942 return HistoryNavigationAction::kDifferentDocument;
2943 }
2944
Alex Moshchuk47d1a4bd2020-06-01 22:15:342945 // If there is no new FrameNavigationEntry for the frame, ignore the
2946 // load. For example, this may happen when going back to an entry before a
2947 // frame was created. Suppose we commit a same-document navigation that also
2948 // results in adding a new subframe somewhere in the tree. If we go back,
2949 // the new subframe will be missing a FrameNavigationEntry in the previous
2950 // NavigationEntry, but we shouldn't delete or change what's loaded in
2951 // it.
2952 //
Alex Moshchuke65c39272020-06-03 17:55:372953 // Note that in this case, there is no need to keep looking for navigations
2954 // in subframes, which would be missing FrameNavigationEntries as well.
2955 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:342956 // It's important to check this before checking |old_item| below, since both
2957 // might be null, and in that case we still shouldn't change what's loaded in
2958 // this frame. Note that scheduling any loads assumes that |new_item| is
2959 // non-null. See https://crbug.com/1088354.
2960 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
2961 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:372962 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:342963
Alex Moshchuk3a4e77a2020-05-29 21:32:572964 // If there is no old FrameNavigationEntry, schedule a different-document
2965 // load.
2966 //
creis225a7432016-06-03 22:56:272967 // TODO(creis): Store the last committed FrameNavigationEntry to use here,
2968 // rather than assuming the NavigationEntry has up to date info on subframes.
creis4e2ecb72015-06-20 00:46:302969 FrameNavigationEntry* old_item =
2970 GetLastCommittedEntry()->GetFrameEntry(frame);
Alex Moshchuk3a4e77a2020-05-29 21:32:572971 if (!old_item)
2972 return HistoryNavigationAction::kDifferentDocument;
2973
Alex Moshchuk3a4e77a2020-05-29 21:32:572974 // If the new item is not in the same SiteInstance, schedule a
2975 // different-document load. Newly restored items may not have a SiteInstance
2976 // yet, in which case it will be assigned on first commit.
2977 if (new_item->site_instance() &&
2978 new_item->site_instance() != old_item->site_instance())
2979 return HistoryNavigationAction::kDifferentDocument;
2980
2981 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:312982 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
2983 // that we do this even if the history navigation would not be modifying this
2984 // frame were it live.
2985 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:572986 return HistoryNavigationAction::kDifferentDocument;
2987
2988 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:312989 // Starting a navigation after a crash early-promotes the speculative
2990 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
2991 // committed yet, so we can not possibly perform a same-document history
2992 // navigation. The frame would need to be reloaded with a cross-document
2993 // navigation.
2994 if (!frame->current_frame_host()->has_committed_any_navigation())
2995 return HistoryNavigationAction::kDifferentDocument;
2996
creis54131692016-08-12 18:32:252997 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:492998 // sequence number but different item sequence number.
2999 if (new_item->document_sequence_number() ==
3000 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573001 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493002 }
avib48cb312016-05-05 21:35:003003
Alex Moshchuk3a4e77a2020-05-29 21:32:573004 // Otherwise, if both item and document sequence numbers differ, this
3005 // should be a different document load.
3006 return HistoryNavigationAction::kDifferentDocument;
3007 }
3008
3009 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373010 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573011 DCHECK_EQ(new_item->document_sequence_number(),
3012 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373013 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573014}
3015
3016void NavigationControllerImpl::FindFramesToNavigate(
3017 FrameTreeNode* frame,
3018 ReloadType reload_type,
Nate Chapin45f620582021-09-30 17:45:433019 bool is_browser_initiated,
Alex Moshchuk3a4e77a2020-05-29 21:32:573020 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3021 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3022 DCHECK(pending_entry_);
3023 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3024
3025 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3026
3027 if (action == HistoryNavigationAction::kSameDocument) {
3028 std::unique_ptr<NavigationRequest> navigation_request =
3029 CreateNavigationRequestFromEntry(
3030 frame, pending_entry_, new_item, reload_type,
3031 true /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433032 false /* is_history_navigation_in_new_child */,
3033 is_browser_initiated);
Alex Moshchuk3a4e77a2020-05-29 21:32:573034 if (navigation_request) {
3035 // Only add the request if was properly created. It's possible for the
3036 // creation to fail in certain cases, e.g. when the URL is invalid.
3037 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303038 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573039 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473040 std::unique_ptr<NavigationRequest> navigation_request =
3041 CreateNavigationRequestFromEntry(
3042 frame, pending_entry_, new_item, reload_type,
3043 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433044 false /* is_history_navigation_in_new_child */,
3045 is_browser_initiated);
Lei Zhang96031532019-10-10 19:05:473046 if (navigation_request) {
3047 // Only add the request if was properly created. It's possible for the
3048 // creation to fail in certain cases, e.g. when the URL is invalid.
3049 different_document_loads->push_back(std::move(navigation_request));
3050 }
3051 // For a different document, the subframes will be destroyed, so there's
3052 // no need to consider them.
3053 return;
Alex Moshchuke65c39272020-06-03 17:55:373054 } else if (action == HistoryNavigationAction::kStopLooking) {
3055 return;
creis4e2ecb72015-06-20 00:46:303056 }
3057
3058 for (size_t i = 0; i < frame->child_count(); i++) {
Nate Chapin45f620582021-09-30 17:45:433059 FindFramesToNavigate(frame->child_at(i), reload_type, is_browser_initiated,
3060 same_document_loads, different_document_loads);
creis4e2ecb72015-06-20 00:46:303061 }
3062}
3063
Harkiran Bolariaba823e42021-05-21 18:30:363064base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry(
clamy21718cc22018-06-13 13:34:243065 const LoadURLParams& params) {
3066 // Find the appropriate FrameTreeNode.
3067 FrameTreeNode* node = nullptr;
3068 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3069 !params.frame_name.empty()) {
3070 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003071 ? frame_tree_.FindByID(params.frame_tree_node_id)
3072 : frame_tree_.FindByName(params.frame_name);
Hayato Ito7a80db42021-07-05 06:18:543073 DCHECK(!node || node->frame_tree() == &frame_tree_);
clamy21718cc22018-06-13 13:34:243074 }
3075
3076 // If no FrameTreeNode was specified, navigate the main frame.
3077 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003078 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243079
Camille Lamy5193caa2018-10-12 11:59:423080 // Compute overrides to the LoadURLParams for |override_user_agent|,
3081 // |should_replace_current_entry| and |has_user_gesture| that will be used
3082 // both in the creation of the NavigationEntry and the NavigationRequest.
3083 // Ideally, the LoadURLParams themselves would be updated, but since they are
3084 // passed as a const reference, this is not possible.
3085 // TODO(clamy): When we only create a NavigationRequest, move this to
3086 // CreateNavigationRequestFromLoadURLParams.
3087 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3088 GetLastCommittedEntry());
3089
3090 // Don't allow an entry replacement if there is no entry to replace.
3091 // http://crbug.com/457149
Hayato Ito7a80db42021-07-05 06:18:543092 //
3093 // If there is an entry, an entry replacement must happen if the current
3094 // browsing context should maintain a trivial session history.
shivanigithubf405bf0d2021-11-05 17:58:333095 bool should_replace_current_entry =
3096 (params.should_replace_current_entry ||
3097 ShouldMaintainTrivialSessionHistory(node)) &&
3098 entries_.size();
Camille Lamy5193caa2018-10-12 11:59:423099
clamy21718cc22018-06-13 13:34:243100 // Javascript URLs should not create NavigationEntries. All other navigations
3101 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243102 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373103 std::unique_ptr<NavigationEntryImpl> entry =
3104 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3105 should_replace_current_entry,
3106 params.has_user_gesture);
clamy21718cc22018-06-13 13:34:243107 DiscardPendingEntry(false);
3108 SetPendingEntry(std::move(entry));
3109 }
3110
3111 // Renderer-debug URLs are sent to the renderer process immediately for
3112 // processing and don't need to create a NavigationRequest.
3113 // Note: this includes navigations to JavaScript URLs, which are considered
3114 // renderer-debug URLs.
3115 // Note: we intentionally leave the pending entry in place for renderer debug
3116 // URLs, unlike the cases below where we clear it if the navigation doesn't
3117 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303118 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483119 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3120 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153121 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483122 params.url, browser_context_)) {
3123 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363124 return nullptr;
Oleg Davydov2cc0167b2019-02-05 14:32:483125 }
3126
clamy21718cc22018-06-13 13:34:243127 HandleRendererDebugURL(node, params.url);
Harkiran Bolariaba823e42021-05-21 18:30:363128 return nullptr;
clamy21718cc22018-06-13 13:34:243129 }
3130
Antonio Sartori78a749f2020-11-30 12:03:393131 DCHECK(pending_entry_);
3132
clamy21718cc22018-06-13 13:34:243133 // Convert navigations to the current URL to a reload.
3134 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3135 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3136 // DevTools sometimes issues navigations to main frames that they do not
3137 // expect to see treated as reload, and it only works because they pass a
3138 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3139 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543140 ReloadType reload_type = params.reload_type;
3141 if (reload_type == ReloadType::NONE &&
3142 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043143 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243144 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243145 params.load_type ==
3146 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
Hayato Ito7a80db42021-07-05 06:18:543147 should_replace_current_entry, GetLastCommittedEntry())) {
clamy21718cc22018-06-13 13:34:243148 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473149 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393150
3151 // If this is a reload of an existing FrameNavigationEntry and we had a
3152 // policy container for it, then we should copy it into the pending entry,
3153 // so that it is copied to the navigation request in
3154 // CreateNavigationRequestFromLoadParams later.
3155 if (GetLastCommittedEntry()) {
3156 FrameNavigationEntry* previous_frame_entry =
3157 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193158 if (previous_frame_entry &&
3159 previous_frame_entry->policy_container_policies()) {
3160 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163161 previous_frame_entry->policy_container_policies()->Clone());
Antonio Sartori78a749f2020-11-30 12:03:393162 }
3163 }
3164 }
3165
3166 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
3167 // document (so no last commit), then we should copy the document polices from
3168 // RenderFrameHost's PolicyContainerHost. The NavigationRequest will create a
3169 // new PolicyContainerHost with the document policies from the
3170 // |pending_entry_|, and that PolicyContainerHost will be put in the final
3171 // RenderFrameHost for the navigation. This way, we ensure that we keep
3172 // enforcing the right policies on the initial empty document after the
3173 // reload.
3174 if (!GetLastCommittedEntry() && params.url.IsAboutBlank()) {
3175 if (node->current_frame_host() &&
3176 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193177 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163178 node->current_frame_host()
3179 ->policy_container_host()
3180 ->policies()
3181 .Clone());
Antonio Sartori78a749f2020-11-30 12:03:393182 }
clamy21718cc22018-06-13 13:34:243183 }
3184
3185 // navigation_ui_data should only be present for main frame navigations.
3186 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
3187
Camille Lamy5193caa2018-10-12 11:59:423188 std::unique_ptr<NavigationRequest> request =
3189 CreateNavigationRequestFromLoadParams(
3190 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143191 params.has_user_gesture, network::mojom::SourceLocation::New(),
Antonio Sartori6984c742021-08-26 08:03:413192 reload_type, pending_entry_, pending_entry_->GetFrameEntry(node));
clamy21718cc22018-06-13 13:34:243193
3194 // If the navigation couldn't start, return immediately and discard the
3195 // pending NavigationEntry.
3196 if (!request) {
3197 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363198 return nullptr;
clamy21718cc22018-06-13 13:34:243199 }
3200
Camille Lamy5193caa2018-10-12 11:59:423201#if DCHECK_IS_ON()
3202 // Safety check that NavigationRequest and NavigationEntry match.
3203 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3204#endif
3205
clamy21718cc22018-06-13 13:34:243206 // This call does not support re-entrancy. See http://crbug.com/347742.
3207 CHECK(!in_navigate_to_pending_entry_);
3208 in_navigate_to_pending_entry_ = true;
3209
arthursonzogni66f711c2019-10-08 14:40:363210 // It is not possible to delete the pending NavigationEntry while navigating
3211 // to it. Grab a reference to delay potential deletion until the end of this
3212 // function.
3213 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3214
Harkiran Bolariaba823e42021-05-21 18:30:363215 base::WeakPtr<NavigationHandle> created_navigation_handle(
3216 request->GetWeakPtr());
Lukasz Anforowicz9ee83c272020-12-01 20:14:053217 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243218
3219 in_navigate_to_pending_entry_ = false;
Harkiran Bolariaba823e42021-05-21 18:30:363220 return created_navigation_handle;
clamy21718cc22018-06-13 13:34:243221}
3222
clamyea99ea12018-05-28 13:54:233223void NavigationControllerImpl::HandleRendererDebugURL(
3224 FrameTreeNode* frame_tree_node,
3225 const GURL& url) {
3226 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243227 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3228 // the renderer process is not live, unless it is the initial navigation
3229 // of the tab.
clamyea99ea12018-05-28 13:54:233230 if (!IsInitialNavigation()) {
3231 DiscardNonCommittedEntries();
3232 return;
3233 }
Fergal Dalyecd3b0202020-06-25 01:57:373234 // The current frame is always a main frame. If IsInitialNavigation() is
3235 // true then there have been no navigations and any frames of this tab must
3236 // be in the same renderer process. If that has crashed then the only frame
3237 // that can be revived is the main frame.
3238 frame_tree_node->render_manager()
3239 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233240 }
3241 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3242}
3243
clamy21718cc22018-06-13 13:34:243244std::unique_ptr<NavigationEntryImpl>
3245NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3246 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423247 const LoadURLParams& params,
3248 bool override_user_agent,
3249 bool should_replace_current_entry,
3250 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393251 // Browser initiated navigations might not have a blob_url_loader_factory set
3252 // in params even if the navigation is to a blob URL. If that happens, lookup
3253 // the correct url loader factory to use here.
3254 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483255 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473256 // Resolve the blob URL in the storage partition associated with the target
3257 // frame. This is the storage partition the URL will be loaded in, and only
3258 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393259 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473260 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393261 }
3262
clamy21718cc22018-06-13 13:34:243263 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443264 // extra_headers in params are \n separated; navigation entries want \r\n.
3265 std::string extra_headers_crlf;
3266 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243267
3268 // For subframes, create a pending entry with a corresponding frame entry.
3269 if (!node->IsMainFrame()) {
Tommy C. Li03eee77a2019-02-05 02:07:443270 if (GetLastCommittedEntry()) {
Tommy C. Li03eee77a2019-02-05 02:07:443271 entry = GetLastCommittedEntry()->Clone();
3272 } else {
3273 // If there's no last committed entry, create an entry for about:blank
3274 // with a subframe entry for our destination.
3275 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
3276 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063277 GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103278 params.source_site_instance.get(), params.transition_type,
3279 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Hayato Ito303654c2021-06-30 09:07:543280 blob_url_loader_factory));
Tommy C. Li03eee77a2019-02-05 02:07:443281 }
Nasko Oskov18006bc2018-12-06 02:53:583282
clamy21718cc22018-06-13 13:34:243283 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:083284 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
clamy21718cc22018-06-13 13:34:243285 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Anton Bikineevf62d1bf2021-05-15 17:56:073286 params.url, absl::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203287 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393288 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:483289 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori78a749f2020-11-30 12:03:393290 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163291 // a conversion of a new navigation into a reload, we will set the right
3292 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193293 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243294 } else {
3295 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243296 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063297 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103298 params.source_site_instance.get(), params.transition_type,
3299 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Hayato Ito303654c2021-06-30 09:07:543300 blob_url_loader_factory));
clamy21718cc22018-06-13 13:34:243301 entry->set_source_site_instance(
3302 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3303 entry->SetRedirectChain(params.redirect_chain);
3304 }
3305
3306 // Set the FTN ID (only used in non-site-per-process, for tests).
3307 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243308 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423309 entry->SetIsOverridingUserAgent(override_user_agent);
3310 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543311 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243312
clamy21718cc22018-06-13 13:34:243313 switch (params.load_type) {
3314 case LOAD_TYPE_DEFAULT:
3315 break;
3316 case LOAD_TYPE_HTTP_POST:
3317 entry->SetHasPostData(true);
3318 entry->SetPostData(params.post_data);
3319 break;
3320 case LOAD_TYPE_DATA:
3321 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3322 entry->SetVirtualURL(params.virtual_url_for_data_url);
3323#if defined(OS_ANDROID)
3324 entry->SetDataURLAsString(params.data_url_as_string);
3325#endif
3326 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3327 break;
clamy21718cc22018-06-13 13:34:243328 }
3329
3330 // TODO(clamy): NavigationEntry is meant for information that will be kept
3331 // after the navigation ended and therefore is not appropriate for
3332 // started_from_context_menu. Move started_from_context_menu to
3333 // NavigationUIData.
3334 entry->set_started_from_context_menu(params.started_from_context_menu);
3335
3336 return entry;
3337}
3338
clamyea99ea12018-05-28 13:54:233339std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423340NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3341 FrameTreeNode* node,
3342 const LoadURLParams& params,
3343 bool override_user_agent,
3344 bool should_replace_current_entry,
3345 bool has_user_gesture,
Antonio Sartori2f763d9d2021-04-21 10:04:143346 network::mojom::SourceLocationPtr source_location,
Camille Lamy5193caa2018-10-12 11:59:423347 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573348 NavigationEntryImpl* entry,
Scott Violetcf6ea7e2021-06-09 21:09:213349 FrameNavigationEntry* frame_entry) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573350 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283351 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533352 // All renderer-initiated navigations must have an initiator_origin.
3353 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513354
Camille Lamy5193caa2018-10-12 11:59:423355 GURL url_to_load;
3356 GURL virtual_url;
Anton Bikineevf62d1bf2021-05-15 17:56:073357 absl::optional<url::Origin> origin_to_commit =
3358 frame_entry ? frame_entry->committed_origin() : absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:323359
Camille Lamy2baa8022018-10-19 16:43:173360 // For main frames, rewrite the URL if necessary and compute the virtual URL
3361 // that should be shown in the address bar.
3362 if (node->IsMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423363 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173364 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423365 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423366
Camille Lamy2baa8022018-10-19 16:43:173367 // For DATA loads, override the virtual URL.
3368 if (params.load_type == LOAD_TYPE_DATA)
3369 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423370
Camille Lamy2baa8022018-10-19 16:43:173371 if (virtual_url.is_empty())
3372 virtual_url = url_to_load;
3373
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573374 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283375
Aran Gilman249eb122019-12-02 23:32:463376 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3377 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3378 // return different results, leading to a different URL in the
3379 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3380 // pending NavigationEntry, as we'll only make one call to
3381 // RewriteUrlForNavigation.
3382 VLOG_IF(1, (url_to_load != frame_entry->url()))
3383 << "NavigationRequest and FrameEntry have different URLs: "
3384 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283385
Camille Lamy2baa8022018-10-19 16:43:173386 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423387 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173388 // NavigationRequest.
3389 } else {
3390 url_to_load = params.url;
3391 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243392 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173393 }
Camille Lamy5193caa2018-10-12 11:59:423394
Ehsan Karamad44fc72112019-02-26 18:15:473395 if (node->render_manager()->is_attaching_inner_delegate()) {
3396 // Avoid starting any new navigations since this node is now preparing for
3397 // attaching an inner delegate.
3398 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203399 }
Camille Lamy5193caa2018-10-12 11:59:423400
Camille Lamy5193caa2018-10-12 11:59:423401 if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load))
3402 return nullptr;
3403
Kunihiko Sakamoto346a74e2021-03-10 08:57:483404 if (!DoesURLMatchOriginForNavigation(
3405 url_to_load, origin_to_commit,
3406 frame_entry->subresource_web_bundle_navigation_info())) {
Nasko Oskov03912102019-01-11 00:21:323407 DCHECK(false) << " url:" << url_to_load
3408 << " origin:" << origin_to_commit.value();
3409 return nullptr;
3410 }
3411
Camille Lamy5193caa2018-10-12 11:59:423412 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403413 blink::PreviewsState previews_state =
3414 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
Camille Lamy5193caa2018-10-12 11:59:423415 if (!node->IsMainFrame()) {
3416 // For subframes, use the state of the top-level frame.
3417 previews_state = node->frame_tree()
3418 ->root()
3419 ->current_frame_host()
3420 ->last_navigation_previews_state();
3421 }
3422
Camille Lamy5193caa2018-10-12 11:59:423423 // This will be used to set the Navigation Timing API navigationStart
3424 // parameter for browser navigations in new tabs (intents, tabs opened through
3425 // "Open link in new tab"). If the navigation must wait on the current
3426 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3427 // will be updated when the BeforeUnload ack is received.
3428 base::TimeTicks navigation_start = base::TimeTicks::Now();
3429
danakjd83d706d2020-11-25 22:11:123430 // Look for a pending commit that is to another document in this
3431 // FrameTreeNode. If one exists, then the last committed URL will not be the
3432 // current URL by the time this navigation commits.
3433 bool has_pending_cross_document_commit =
3434 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
Miyoung Shina2dd6a42021-10-07 12:19:213435 bool is_currently_error_page = node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:123436
Minggang Wangb9f3fa92021-07-01 15:30:313437 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjb952ef12021-01-14 19:58:493438 /*old_url=*/node->current_url(),
3439 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3440 has_pending_cross_document_commit, is_currently_error_page,
3441 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423442
3443 // Create the NavigationParams based on |params|.
3444
Hiroki Nakagawa4ed61282021-06-18 05:37:233445 bool is_view_source_mode = entry->IsViewSourceMode();
3446 DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme));
Charlie Harrison8c113a32019-01-07 16:08:293447
Antonio Sartori6984c742021-08-26 08:03:413448 blink::NavigationDownloadPolicy download_policy = params.download_policy;
Hiroki Nakagawa4ed61282021-06-18 05:37:233449 // Update |download_policy| if the virtual URL is view-source.
Charlie Harrison8c113a32019-01-07 16:08:293450 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253451 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293452
Minggang Wangb9f3fa92021-07-01 15:30:313453 blink::mojom::CommonNavigationParamsPtr common_params =
3454 blink::mojom::CommonNavigationParams::New(
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513455 url_to_load, params.initiator_origin,
3456 blink::mojom::Referrer::New(params.referrer.url,
3457 params.referrer.policy),
Scott Violetcf6ea7e2021-06-09 21:09:213458 params.transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513459 should_replace_current_entry, params.base_url_for_data_url,
Rakina Zata Amni8a1c6b32021-09-29 06:41:293460 previews_state, navigation_start,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513461 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
Antonio Sartori2f763d9d2021-04-21 10:04:143462 params.post_data, std::move(source_location),
arthursonzogniaf7c62c52020-02-12 10:49:413463 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273464 false /* has_text_fragment_token */,
3465 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3466 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513467 false /* is_history_navigation_in_new_child_frame */,
Nan Lind91c8152021-10-21 16:22:373468 params.input_start, network::mojom::RequestDestination::kEmpty);
Camille Lamy5193caa2018-10-12 11:59:423469
Minggang Wangb9f3fa92021-07-01 15:30:313470 blink::mojom::CommitNavigationParamsPtr commit_params =
3471 blink::mojom::CommitNavigationParams::New(
Antonio Sartori3e8de6d2021-07-26 10:28:413472 frame_entry->committed_origin(),
3473 // The correct storage key will be computed before committing the
3474 // navigation.
3475 blink::StorageKey(), network::mojom::WebSandboxFlags(),
arthursonzognid5a8d0b2021-03-11 17:36:433476 override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323477 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143478 std::vector<net::RedirectInfo>(),
3479 std::string() /* post_content_type */, common_params->url,
3480 common_params->method, params.can_load_local_resources,
Minggang Wangb9f3fa92021-07-01 15:30:313481 frame_entry->page_state().ToEncodedData(), entry->GetUniqueID(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143482 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3483 -1 /* pending_history_list_offset */,
3484 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3485 params.should_clear_history_list ? 0 : GetEntryCount(),
3486 false /* was_discarded */, is_view_source_mode,
Minggang Wangb9f3fa92021-07-01 15:30:313487 params.should_clear_history_list,
3488 blink::mojom::NavigationTiming::New(),
Minggang Wangf59db47b2021-06-16 01:56:223489 blink::mojom::WasActivatedOption::kUnknown,
Lucas Furukawa Gadania9c45682019-07-31 22:05:143490 base::UnguessableToken::Create() /* navigation_token */,
Minggang Wang7ee0c742021-06-16 16:16:513491 std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143492#if defined(OS_ANDROID)
3493 std::string(), /* data_url_as_string */
3494#endif
arthursonzogni14379782020-05-15 09:09:273495 !params.is_renderer_initiated, /* is_browser_initiated */
Tsuyoshi Horoe86d7702019-11-29 01:52:473496 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533497 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173498 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073499 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163500 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333501 false /* origin_agent_cluster */,
Maks Orlovichc66745a2020-06-30 17:40:023502 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213503 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Nate Chapind1fe3612021-04-16 20:45:573504 false /* is_cross_browsing_instance */, nullptr /* old_page_info */,
3505 -1 /* http_response_code */,
Minggang Wangb9f3fa92021-07-01 15:30:313506 std::vector<blink::mojom::
3507 AppHistoryEntryPtr>() /* app_history_back_entries */,
Nate Chapind1fe3612021-04-16 20:45:573508 std::vector<
Minggang Wangb9f3fa92021-07-01 15:30:313509 blink::mojom::
3510 AppHistoryEntryPtr>() /* app_history_forward_entries */,
Matt Menke4e209082021-11-09 04:59:313511 std::vector<GURL>() /* early_hints_preloaded_resources */,
Clark DuVall8ee487a22021-11-10 02:25:583512 absl::nullopt /* ad_auction_components */,
3513 // This timestamp will be populated when the commit IPC is sent.
3514 base::TimeTicks() /* commit_sent */);
Camille Lamy5193caa2018-10-12 11:59:423515#if defined(OS_ANDROID)
3516 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143517 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423518 }
3519#endif
3520
Lucas Furukawa Gadania9c45682019-07-31 22:05:143521 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423522
3523 // A form submission may happen here if the navigation is a renderer-initiated
3524 // form submission that took the OpenURL path.
3525 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3526
3527 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3528 std::string extra_headers_crlf;
3529 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093530
3531 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143532 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083533 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453534 params.initiator_frame_token.has_value()
3535 ? &(params.initiator_frame_token.value())
3536 : nullptr,
3537 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3538 request_body,
John Delaney50425f82020-04-07 16:26:213539 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473540 params.impression, params.is_pdf);
Yao Xiaodc5ed102019-06-04 19:19:093541 navigation_request->set_from_download_cross_origin_redirect(
3542 params.from_download_cross_origin_redirect);
3543 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423544}
3545
3546std::unique_ptr<NavigationRequest>
3547NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233548 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573549 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233550 FrameNavigationEntry* frame_entry,
3551 ReloadType reload_type,
3552 bool is_same_document_history_load,
Nate Chapin45f620582021-09-30 17:45:433553 bool is_history_navigation_in_new_child_frame,
3554 bool is_browser_initiated) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343555 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233556 GURL dest_url = frame_entry->url();
Anton Bikineevf62d1bf2021-05-15 17:56:073557 absl::optional<url::Origin> origin_to_commit =
Nasko Oskov03912102019-01-11 00:21:323558 frame_entry->committed_origin();
3559
clamyea99ea12018-05-28 13:54:233560 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013561 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573562 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233563 // We may have been redirected when navigating to the current URL.
3564 // Use the URL the user originally intended to visit as signaled by the
3565 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013566 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573567 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233568 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323569 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233570 }
3571
Ehsan Karamad44fc72112019-02-26 18:15:473572 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3573 // Avoid starting any new navigations since this node is now preparing for
3574 // attaching an inner delegate.
3575 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203576 }
3577
Camille Lamy5193caa2018-10-12 11:59:423578 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573579 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233580 return nullptr;
3581 }
3582
Kunihiko Sakamoto346a74e2021-03-10 08:57:483583 if (!DoesURLMatchOriginForNavigation(
3584 dest_url, origin_to_commit,
3585 frame_entry->subresource_web_bundle_navigation_info())) {
3586 DCHECK(false) << " url:" << dest_url
3587 << " origin:" << origin_to_commit.value();
Nasko Oskov03912102019-01-11 00:21:323588 return nullptr;
3589 }
3590
clamyea99ea12018-05-28 13:54:233591 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403592 blink::PreviewsState previews_state =
3593 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
clamyea99ea12018-05-28 13:54:233594 if (!frame_tree_node->IsMainFrame()) {
3595 // For subframes, use the state of the top-level frame.
3596 previews_state = frame_tree_node->frame_tree()
3597 ->root()
3598 ->current_frame_host()
3599 ->last_navigation_previews_state();
clamyea99ea12018-05-28 13:54:233600 }
3601
clamyea99ea12018-05-28 13:54:233602 // This will be used to set the Navigation Timing API navigationStart
3603 // parameter for browser navigations in new tabs (intents, tabs opened through
3604 // "Open link in new tab"). If the navigation must wait on the current
3605 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3606 // will be updated when the BeforeUnload ack is received.
3607 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233608
danakjd83d706d2020-11-25 22:11:123609 // Look for a pending commit that is to another document in this
3610 // FrameTreeNode. If one exists, then the last committed URL will not be the
3611 // current URL by the time this navigation commits.
3612 bool has_pending_cross_document_commit =
3613 frame_tree_node->render_manager()
3614 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493615 bool is_currently_error_page =
Miyoung Shina2dd6a42021-10-07 12:19:213616 frame_tree_node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:123617
Minggang Wangb9f3fa92021-07-01 15:30:313618 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123619 /*old_url=*/frame_tree_node->current_url(),
3620 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493621 has_pending_cross_document_commit, is_currently_error_page,
3622 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423623
3624 // A form submission may happen here if the navigation is a
3625 // back/forward/reload navigation that does a form resubmission.
3626 scoped_refptr<network::ResourceRequestBody> request_body;
3627 std::string post_content_type;
3628 if (frame_entry->method() == "POST") {
3629 request_body = frame_entry->GetPostData(&post_content_type);
3630 // Might have a LF at end.
Peter Kastingb53b81912021-04-28 19:23:303631 post_content_type = std::string(
3632 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL));
Camille Lamy5193caa2018-10-12 11:59:423633 }
3634
3635 // Create the NavigationParams based on |entry| and |frame_entry|.
Minggang Wangb9f3fa92021-07-01 15:30:313636 blink::mojom::CommonNavigationParamsPtr common_params =
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513637 entry->ConstructCommonNavigationParams(
3638 *frame_entry, request_body, dest_url,
3639 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
3640 navigation_type, previews_state, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533641 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513642 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553643 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423644
3645 // TODO(clamy): |intended_as_new_entry| below should always be false once
3646 // Reload no longer leads to this being called for a pending NavigationEntry
3647 // of index -1.
Minggang Wangb9f3fa92021-07-01 15:30:313648 blink::mojom::CommitNavigationParamsPtr commit_params =
Lucas Furukawa Gadania9c45682019-07-31 22:05:143649 entry->ConstructCommitNavigationParams(
3650 *frame_entry, common_params->url, origin_to_commit,
3651 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3652 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533653 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3654 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143655 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423656
clamyea99ea12018-05-28 13:54:233657 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143658 frame_tree_node, std::move(common_params), std::move(commit_params),
Nate Chapin45f620582021-09-30 17:45:433659 is_browser_initiated, false /* was_opener_suppressed */,
Takashi Toyoshimae87b7be2021-01-22 11:51:083660 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453661 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3662 entry->extra_headers(), frame_entry, entry, request_body,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473663 nullptr /* navigation_ui_data */, absl::nullopt /* impression */,
3664 false /* is_pdf */);
clamyea99ea12018-05-28 13:54:233665}
3666
[email protected]d202a7c2012-01-04 07:53:473667void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213668 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273669 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403670
Takashi Toyoshimaea534ef22021-07-21 03:27:593671 // We need to notify the ssl_manager_ before the WebContents so the
[email protected]df1af242009-05-01 00:11:403672 // location bar will have up-to-date information about the security style
3673 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133674 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403675
[email protected]7f924832014-08-09 05:57:223676 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:573677 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463678
[email protected]b0f724c2013-09-05 04:21:133679 // TODO(avi): Remove. http://crbug.com/170921
3680 NotificationDetails notification_details =
3681 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153682 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3683 Source<NavigationController>(this),
3684 notification_details);
initial.commit09911bf2008-07-26 23:55:293685}
3686
initial.commit09911bf2008-07-26 23:55:293687// static
[email protected]d202a7c2012-01-04 07:53:473688size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233689 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153690 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:213691 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233692}
3693
[email protected]d202a7c2012-01-04 07:53:473694void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223695 if (is_active && needs_reload_)
3696 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293697}
3698
[email protected]d202a7c2012-01-04 07:53:473699void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293700 if (!needs_reload_)
3701 return;
3702
Bo Liucdfa4b12018-11-06 00:21:443703 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3704 needs_reload_type_);
3705
initial.commit09911bf2008-07-26 23:55:293706 // Calling Reload() results in ignoring state, and not loading.
3707 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3708 // cached state.
avicc872d7242015-08-19 21:26:343709 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163710 NavigateToExistingPendingEntry(ReloadType::NONE,
Nate Chapin45f620582021-09-30 17:45:433711 FrameTreeNode::kFrameTreeNodeInvalidId,
3712 true /* is_browser_initiated */);
avicc872d7242015-08-19 21:26:343713 } else if (last_committed_entry_index_ != -1) {
arthursonzogni5c4c202d2017-04-25 23:41:273714 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343715 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163716 NavigateToExistingPendingEntry(ReloadType::NONE,
Nate Chapin45f620582021-09-30 17:45:433717 FrameTreeNode::kFrameTreeNodeInvalidId,
3718 true /* is_browser_initiated */);
avicc872d7242015-08-19 21:26:343719 } else {
3720 // If there is something to reload, the successful reload will clear the
3721 // |needs_reload_| flag. Otherwise, just do it here.
3722 needs_reload_ = false;
3723 }
initial.commit09911bf2008-07-26 23:55:293724}
3725
Kevin McNeeccca6172021-10-19 17:11:143726base::WeakPtr<NavigationHandle>
3727NavigationControllerImpl::LoadPostCommitErrorPage(
Carlos IL42b416592019-10-07 23:10:363728 RenderFrameHost* render_frame_host,
3729 const GURL& url,
3730 const std::string& error_page_html,
3731 net::Error error) {
Rakina Zata Amni919b7922020-12-11 09:03:133732 RenderFrameHostImpl* rfhi =
3733 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:583734
3735 // Only active documents can load post-commit error pages:
3736 // - If the document is in pending deletion, the browser already committed to
3737 // destroying this RenderFrameHost so ignore loading the error page.
3738 // - If the document is in back-forward cache, it's not allowed to navigate as
3739 // it should remain frozen. Ignore the request and evict the document from
3740 // back-forward cache.
3741 // - If the document is prerendering, it can navigate but when loading error
3742 // pages, cancel prerendering.
Fergal Daly1336ac642021-09-14 15:13:113743 if (rfhi->IsInactiveAndDisallowActivation(
3744 DisallowActivationReasonId::kLoadPostCommitErrorPage)) {
Kevin McNeeccca6172021-10-19 17:11:143745 return nullptr;
Fergal Daly1336ac642021-09-14 15:13:113746 }
Sreeja Kamishettydb8e2892021-03-10 09:30:583747
Rakina Zata Amni919b7922020-12-11 09:03:133748 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:413749
Minggang Wangb9f3fa92021-07-01 15:30:313750 blink::mojom::CommonNavigationParamsPtr common_params =
Minggang Wanga13c796e2021-07-02 05:54:433751 blink::CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:593752 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
3753 // the frame actually committed (e.g. iframe with "src" set to a
3754 // slow-responding URL). We should rewrite the URL to about:blank in this
3755 // case, as the renderer will only think a page is an error page if it has a
3756 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:133757 common_params->url = url.is_empty() ? GURL("about:blank") : url;
Minggang Wangb9f3fa92021-07-01 15:30:313758 blink::mojom::CommitNavigationParamsPtr commit_params =
Minggang Wanga13c796e2021-07-02 05:54:433759 blink::CreateCommitNavigationParams();
Antonio Sartori58591c892021-04-21 06:54:333760 commit_params->original_url = common_params->url;
John Delaney131ad362019-08-08 21:57:413761
arthursonzogni70ac7302020-05-28 08:49:053762 // Error pages have a fully permissive FramePolicy.
3763 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
3764 // error pages.
3765 commit_params->frame_policy = blink::FramePolicy();
3766
John Delaney131ad362019-08-08 21:57:413767 std::unique_ptr<NavigationRequest> navigation_request =
3768 NavigationRequest::CreateBrowserInitiated(
3769 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083770 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:193771 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453772 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:063773 "" /* extra_headers */, nullptr /* frame_entry */,
3774 nullptr /* entry */, nullptr /* post_body */,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473775 nullptr /* navigation_ui_data */, absl::nullopt /* impression */,
3776 false /* is_pdf */);
Carlos IL42b416592019-10-07 23:10:363777 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:413778 navigation_request->set_net_error(error);
3779 node->CreatedNavigationRequest(std::move(navigation_request));
3780 DCHECK(node->navigation_request());
Kevin McNeeccca6172021-10-19 17:11:143781
3782 // Calling BeginNavigation may destroy the NavigationRequest.
3783 base::WeakPtr<NavigationRequest> created_navigation_request(
3784 node->navigation_request()->GetWeakPtr());
John Delaney131ad362019-08-08 21:57:413785 node->navigation_request()->BeginNavigation();
Kevin McNeeccca6172021-10-19 17:11:143786 return created_navigation_request;
John Delaney131ad362019-08-08 21:57:413787}
3788
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573789void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213790 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093791 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:153792 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143793 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293794}
3795
[email protected]d202a7c2012-01-04 07:53:473796void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363797 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553798 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363799 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293800
initial.commit09911bf2008-07-26 23:55:293801 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293802}
[email protected]765b35502008-08-21 00:51:203803
arthursonzogni69a6a1b2019-09-17 09:23:003804void NavigationControllerImpl::DiscardNonCommittedEntries() {
Michael Thiessen9b14d512019-09-23 21:19:473805 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:103806 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
3807 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:293808 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:473809 return;
Michael Thiessenc5676d22019-09-25 22:32:103810 }
avi45a72532015-04-07 21:01:453811 DiscardPendingEntry(false);
arthursonzogni69a6a1b2019-09-17 09:23:003812 if (delegate_)
3813 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:483814}
3815
avi7c6f35e2015-05-08 17:52:383816int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3817 int nav_entry_id) const {
3818 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3819 if (entries_[i]->GetUniqueID() == nav_entry_id)
3820 return i;
3821 }
3822 return -1;
3823}
3824
[email protected]d202a7c2012-01-04 07:53:473825void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573826 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253827 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573828 DCHECK_LE(max_index, source->GetEntryCount());
Nate Chapin214a86a2021-06-21 20:35:573829 std::unique_ptr<NavigationEntryRestoreContextImpl> context =
3830 std::make_unique<NavigationEntryRestoreContextImpl>();
[email protected]e1cd5452010-08-26 18:03:253831 for (int i = 0; i < max_index; i++) {
Nate Chapin9f169072021-06-09 19:32:373832 // Normally, cloning a NavigationEntryImpl results in sharing
3833 // FrameNavigationEntries between the original and the clone. However, when
3834 // cloning from a different NavigationControllerImpl, we want to fork the
3835 // FrameNavigationEntries.
Nate Chapin9f169072021-06-09 19:32:373836 entries_.insert(entries_.begin() + i,
Nate Chapin214a86a2021-06-21 20:35:573837 source->entries_[i]->CloneWithoutSharing(context.get()));
[email protected]e1cd5452010-08-26 18:03:253838 }
arthursonzogni5c4c202d2017-04-25 23:41:273839 DCHECK(pending_entry_index_ == -1 ||
3840 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:253841}
[email protected]c5b88d82012-10-06 17:03:333842
3843void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:183844 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:333845 get_timestamp_callback_ = get_timestamp_callback;
3846}
[email protected]8ff00d72012-10-23 19:12:213847
Shivani Sharmaffb32b82019-04-09 16:58:473848// History manipulation intervention:
3849void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:473850 bool replace_entry,
3851 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:403852 bool is_renderer_initiated,
3853 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:453854 // Note that for a subframe, previous_document_was_activated is true if the
3855 // gesture happened in any subframe (propagated to main frame) or in the main
3856 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:473857 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:273858 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:473859 return;
3860 }
3861 if (last_committed_entry_index_ == -1)
3862 return;
3863
Shivani Sharmac4cc8922019-04-18 03:11:173864 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473865
Alexander Timine3ec4192020-04-20 16:39:403866 // Log UKM with the URL we are navigating away from.
3867 ukm::builders::HistoryManipulationIntervention(
3868 previous_page_load_ukm_source_id)
3869 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:473870}
3871
Shivani Sharmac4cc8922019-04-18 03:11:173872void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
3873 int reference_index,
3874 bool skippable) {
3875 auto* reference_entry = GetEntryAtIndex(reference_index);
3876 reference_entry->set_should_skip_on_back_forward_ui(skippable);
3877
3878 int64_t document_sequence_number =
3879 reference_entry->root_node()->frame_entry->document_sequence_number();
3880 for (int index = 0; index < GetEntryCount(); index++) {
3881 auto* entry = GetEntryAtIndex(index);
3882 if (entry->root_node()->frame_entry->document_sequence_number() ==
3883 document_sequence_number) {
3884 entry->set_should_skip_on_back_forward_ui(skippable);
3885 }
3886 }
3887}
3888
arthursonzogni66f711c2019-10-08 14:40:363889std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
3890NavigationControllerImpl::ReferencePendingEntry() {
3891 DCHECK(pending_entry_);
3892 auto pending_entry_ref =
3893 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
3894 pending_entry_refs_.insert(pending_entry_ref.get());
3895 return pending_entry_ref;
3896}
3897
3898void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
3899 // Ignore refs that don't correspond to the current pending entry.
3900 auto it = pending_entry_refs_.find(ref);
3901 if (it == pending_entry_refs_.end())
3902 return;
3903 pending_entry_refs_.erase(it);
3904
3905 if (!pending_entry_refs_.empty())
3906 return;
3907
3908 // The pending entry may be deleted before the last PendingEntryRef.
3909 if (!pending_entry_)
3910 return;
3911
3912 // We usually clear the pending entry when the matching NavigationRequest
3913 // fails, so that an arbitrary URL isn't left visible above a committed page.
3914 //
3915 // However, we do preserve the pending entry in some cases, such as on the
3916 // initial navigation of an unmodified blank tab. We also allow the delegate
3917 // to say when it's safe to leave aborted URLs in the omnibox, to let the
3918 // user edit the URL and try again. This may be useful in cases that the
3919 // committed page cannot be attacker-controlled. In these cases, we still
3920 // allow the view to clear the pending entry and typed URL if the user
3921 // requests (e.g., hitting Escape with focus in the address bar).
3922 //
3923 // Do not leave the pending entry visible if it has an invalid URL, since this
3924 // might be formatted in an unexpected or unsafe way.
3925 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:363926 bool should_preserve_entry =
3927 (pending_entry_ == GetVisibleEntry()) &&
3928 pending_entry_->GetURL().is_valid() &&
3929 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
3930 if (should_preserve_entry)
3931 return;
3932
3933 DiscardPendingEntry(true);
3934 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3935}
3936
Titouan Rigoudy6ec70402021-02-02 15:42:193937std::unique_ptr<PolicyContainerPolicies>
3938NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:393939 RenderFrameHostImpl* rfh,
3940 bool is_same_document,
3941 NavigationRequest* request) {
Titouan Rigoudy6ec70402021-02-02 15:42:193942 if (!ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(request))
Antonio Sartori78a749f2020-11-30 12:03:393943 return nullptr;
3944
3945 if (is_same_document) {
Charlie Reis73e356242021-04-02 17:10:313946 // TODO(https://crbug.com/524208): Remove this nullptr check when we can
3947 // ensure we always have a FrameNavigationEntry here.
3948 if (!GetLastCommittedEntry())
3949 return nullptr;
3950
Antonio Sartori78a749f2020-11-30 12:03:393951 FrameNavigationEntry* previous_frame_entry =
3952 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
3953
3954 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
3955 // ensure we always have a FrameNavigationEntry here.
3956 if (!previous_frame_entry)
3957 return nullptr;
3958
Titouan Rigoudy6ec70402021-02-02 15:42:193959 const PolicyContainerPolicies* previous_policies =
3960 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:393961
Titouan Rigoudy6ec70402021-02-02 15:42:193962 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:393963 return nullptr;
3964
3965 // Make a copy of the policy container for the new FrameNavigationEntry.
Antonio Sartori5d09b30f2021-03-02 09:27:163966 return previous_policies->Clone();
Antonio Sartori78a749f2020-11-30 12:03:393967 }
3968
Antonio Sartori4f5373792021-05-31 10:56:473969 return rfh->policy_container_host()->policies().Clone();
Antonio Sartori78a749f2020-11-30 12:03:393970}
3971
Hayato Ito2c8c08d02021-06-23 03:38:433972void NavigationControllerImpl::BroadcastHistoryOffsetAndLength() {
Carlos Caballeroede6f8c2021-01-28 11:01:503973 OPTIONAL_TRACE_EVENT2(
Hayato Ito2c8c08d02021-06-23 03:38:433974 "content", "NavigationControllerImpl::BroadcastHistoryOffsetAndLength",
3975 "history_offset", GetLastCommittedEntryIndex(), "history_length",
3976 GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:503977
3978 auto callback = base::BindRepeating(
3979 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
3980 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
3981 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
3982 }
3983 },
Hayato Ito2c8c08d02021-06-23 03:38:433984 GetLastCommittedEntryIndex(), GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:503985 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
3986}
3987
3988void NavigationControllerImpl::DidAccessInitialMainDocument() {
3989 // We may have left a failed browser-initiated navigation in the address bar
3990 // to let the user edit it and try again. Clear it now that content might
3991 // show up underneath it.
3992 if (!frame_tree_.IsLoading() && GetPendingEntry())
3993 DiscardPendingEntry(false);
3994
3995 // Update the URL display.
3996 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3997}
3998
3999void NavigationControllerImpl::UpdateStateForFrame(
4000 RenderFrameHostImpl* rfhi,
4001 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:564002 OPTIONAL_TRACE_EVENT1("content",
4003 "NavigationControllerImpl::UpdateStateForFrame",
4004 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:504005 // The state update affects the last NavigationEntry associated with the given
4006 // |render_frame_host|. This may not be the last committed NavigationEntry (as
4007 // in the case of an UpdateState from a frame being swapped out). We track
4008 // which entry this is in the RenderFrameHost's nav_entry_id.
4009 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
4010 if (!entry)
4011 return;
4012
4013 FrameNavigationEntry* frame_entry =
4014 entry->GetFrameEntry(rfhi->frame_tree_node());
4015 if (!frame_entry)
4016 return;
4017
4018 // The SiteInstance might not match if we do a cross-process navigation with
4019 // replacement (e.g., auto-subframe), in which case the swap out of the old
4020 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4021 // has already been replaced and destroyed.
4022 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4023 return;
4024
4025 if (page_state == frame_entry->page_state())
4026 return; // Nothing to update.
4027
4028 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4029
4030 // The document_sequence_number and item_sequence_number recorded in the
4031 // FrameNavigationEntry should not differ from the one coming with the update,
4032 // since it must come from the same document. Do not update it if a difference
4033 // is detected, as this indicates that |frame_entry| is not the correct one.
4034 blink::ExplodedPageState exploded_state;
4035 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4036 return;
4037
4038 if (exploded_state.top.document_sequence_number !=
4039 frame_entry->document_sequence_number() ||
4040 exploded_state.top.item_sequence_number !=
4041 frame_entry->item_sequence_number()) {
4042 return;
4043 }
4044
4045 frame_entry->SetPageState(page_state);
4046 NotifyEntryChanged(entry);
4047}
4048
Aaron Colwellb731a0ae2021-03-19 19:14:474049void NavigationControllerImpl::OnStoragePartitionIdAdded(
4050 const StoragePartitionId& partition_id) {
4051 auto it = partition_config_to_id_map_.insert(
4052 std::make_pair(partition_id.config(), partition_id));
4053 bool successful_insert = it.second;
Aaron Colwellb731a0ae2021-03-19 19:14:474054 if (!successful_insert) {
4055 LogStoragePartitionIdCrashKeys(it.first->second, partition_id);
4056 }
Sharon Yangd4f48792021-06-30 17:02:534057 CHECK(successful_insert);
Aaron Colwellb731a0ae2021-03-19 19:14:474058}
4059
4060void NavigationControllerImpl::LogStoragePartitionIdCrashKeys(
4061 const StoragePartitionId& original_partition_id,
4062 const StoragePartitionId& new_partition_id) {
4063 base::debug::SetCrashKeyString(
4064 base::debug::AllocateCrashKeyString("original_partition_id",
4065 base::debug::CrashKeySize::Size256),
4066 original_partition_id.ToString());
4067
4068 base::debug::SetCrashKeyString(
4069 base::debug::AllocateCrashKeyString("new_partition_id",
4070 base::debug::CrashKeySize::Size256),
4071 new_partition_id.ToString());
Aaron Colwellb731a0ae2021-03-19 19:14:474072}
4073
Minggang Wangb9f3fa92021-07-01 15:30:314074std::vector<blink::mojom::AppHistoryEntryPtr>
Nate Chapind1fe3612021-04-16 20:45:574075NavigationControllerImpl::PopulateSingleAppHistoryEntryVector(
4076 Direction direction,
4077 int entry_index,
4078 const url::Origin& pending_origin,
4079 FrameTreeNode* node,
4080 SiteInstance* site_instance,
4081 int64_t previous_item_sequence_number) {
Minggang Wangb9f3fa92021-07-01 15:30:314082 std::vector<blink::mojom::AppHistoryEntryPtr> entries;
Nate Chapind1fe3612021-04-16 20:45:574083 int offset = direction == Direction::kForward ? 1 : -1;
4084 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4085 i += offset) {
4086 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
4087 if (!frame_entry || !frame_entry->committed_origin())
4088 break;
4089 if (site_instance != frame_entry->site_instance())
4090 break;
4091 if (!pending_origin.IsSameOriginWith(*frame_entry->committed_origin()))
4092 break;
4093 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4094 continue;
4095 blink::ExplodedPageState exploded_page_state;
4096 if (blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4097 &exploded_page_state)) {
4098 blink::ExplodedFrameState frame_state = exploded_page_state.top;
Minggang Wangb9f3fa92021-07-01 15:30:314099 blink::mojom::AppHistoryEntryPtr entry =
4100 blink::mojom::AppHistoryEntry::New(
4101 frame_state.app_history_key.value_or(std::u16string()),
4102 frame_state.app_history_id.value_or(std::u16string()),
Nate Chapinab5c3a712021-11-18 22:17:094103 frame_state.url_string.value_or(std::u16string()),
4104 frame_state.item_sequence_number,
4105 frame_state.document_sequence_number,
4106 frame_state.app_history_state.value_or(std::u16string()));
Nate Chapind1fe3612021-04-16 20:45:574107 DCHECK(pending_origin.CanBeDerivedFrom(GURL(entry->url)));
4108 entries.push_back(std::move(entry));
4109 previous_item_sequence_number = frame_entry->item_sequence_number();
4110 }
4111 }
4112 // If |entries| was constructed by iterating backwards from
4113 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4114 // earliest-at-the-front. Reverse it.
4115 if (direction == Direction::kBack)
4116 std::reverse(entries.begin(), entries.end());
4117 return entries;
4118}
4119
4120void NavigationControllerImpl::PopulateAppHistoryEntryVectors(
4121 NavigationRequest* request) {
4122 url::Origin pending_origin =
4123 request->commit_params().origin_to_commit
4124 ? *request->commit_params().origin_to_commit
4125 : url::Origin::Create(request->common_params().url);
4126
4127 FrameTreeNode* node = request->frame_tree_node();
4128 scoped_refptr<SiteInstance> site_instance =
4129 request->GetRenderFrameHost()->GetSiteInstance();
4130
4131 // NOTE: |entry_index| is an estimate of the index where this entry will
4132 // commit, but it may be wrong in corner cases (e.g., if we are at the max
4133 // entry limit, the earliest entry will be dropped). This is ok because this
4134 // algorithm only uses |entry_index| to walk the entry list as it stands right
4135 // now, and it isn't saved for anything post-commit.
4136 int entry_index = GetPendingEntryIndex();
4137 bool will_create_new_entry = false;
4138 if (NavigationTypeUtils::IsReload(request->common_params().navigation_type) ||
4139 request->common_params().should_replace_current_entry) {
4140 entry_index = GetLastCommittedEntryIndex();
4141 } else if (entry_index == -1) {
4142 will_create_new_entry = true;
4143 entry_index = GetLastCommittedEntryIndex() + 1;
4144 }
4145
4146 int64_t pending_item_sequence_number = 0;
4147 if (auto* pending_entry = GetPendingEntry()) {
4148 if (auto* frame_entry = pending_entry->GetFrameEntry(node))
4149 pending_item_sequence_number = frame_entry->item_sequence_number();
4150 }
4151
4152 request->set_app_history_back_entries(PopulateSingleAppHistoryEntryVector(
4153 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
4154 pending_item_sequence_number));
4155
4156 // Don't populate forward entries if they will be truncated by a new entry.
4157 if (!will_create_new_entry) {
4158 request->set_app_history_forward_entries(
4159 PopulateSingleAppHistoryEntryVector(
4160 Direction::kForward, entry_index, pending_origin, node,
4161 site_instance.get(), pending_item_sequence_number));
4162 }
4163}
4164
Nate Chapinfbfe5af2021-06-10 17:22:084165NavigationControllerImpl::HistoryNavigationAction
4166NavigationControllerImpl::ShouldNavigateToEntryForAppHistoryKey(
4167 FrameNavigationEntry* current_entry,
4168 FrameNavigationEntry* target_entry,
4169 const std::string& app_history_key) {
4170 if (!target_entry || !target_entry->committed_origin())
4171 return HistoryNavigationAction::kStopLooking;
4172 if (current_entry->site_instance() != target_entry->site_instance())
4173 return HistoryNavigationAction::kStopLooking;
4174 if (!current_entry->committed_origin()->IsSameOriginWith(
4175 *target_entry->committed_origin())) {
4176 return HistoryNavigationAction::kStopLooking;
4177 }
4178
4179 // NOTE: We don't actually care between kSameDocument and
4180 // kDifferentDocument, so always use kDifferentDocument by convention.
4181 if (target_entry->app_history_key() == app_history_key)
4182 return HistoryNavigationAction::kDifferentDocument;
4183 return HistoryNavigationAction::kKeepLooking;
4184}
4185
4186void NavigationControllerImpl::NavigateToAppHistoryKey(FrameTreeNode* node,
4187 const std::string& key) {
4188 FrameNavigationEntry* current_entry =
4189 GetLastCommittedEntry()->GetFrameEntry(node);
4190 if (!current_entry)
4191 return;
4192
4193 // We want to find the nearest matching entry that is contiguously
4194 // same-instance and same-origin. Check back first, then forward.
4195 // TODO(japhet): Link spec here once it exists.
4196 for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) {
4197 auto result = ShouldNavigateToEntryForAppHistoryKey(
4198 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4199 if (result == HistoryNavigationAction::kStopLooking)
4200 break;
4201 if (result != HistoryNavigationAction::kKeepLooking) {
Nate Chapin2892b37c2021-10-08 17:33:114202 GoToIndex(i, FrameTreeNode::kFrameTreeNodeInvalidId,
4203 false /* is_browser_initiated*/);
Nate Chapinfbfe5af2021-06-10 17:22:084204 return;
4205 }
4206 }
4207 for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) {
4208 auto result = ShouldNavigateToEntryForAppHistoryKey(
4209 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4210 if (result == HistoryNavigationAction::kStopLooking)
4211 break;
4212 if (result != HistoryNavigationAction::kKeepLooking) {
Nate Chapin2892b37c2021-10-08 17:33:114213 GoToIndex(i, FrameTreeNode::kFrameTreeNodeInvalidId,
4214 false /* is_browser_initiated*/);
Nate Chapinfbfe5af2021-06-10 17:22:084215 return;
4216 }
4217 }
4218}
4219
shivanigithubf405bf0d2021-11-05 17:58:334220bool NavigationControllerImpl::ShouldMaintainTrivialSessionHistory(
4221 const FrameTreeNode* frame_tree_node) const {
4222 // TODO(https://crbug.com/1197384): We may have to add portals in addition to
4223 // prerender and fenced frames. This should be kept in sync with
Hayato Ito7a80db42021-07-05 06:18:544224 // LocalFrame version, LocalFrame::ShouldMaintainTrivialSessionHistory.
shivanigithubf405bf0d2021-11-05 17:58:334225 return frame_tree_.is_prerendering() ||
4226 frame_tree_node->IsInFencedFrameTree();
Hayato Ito7a80db42021-07-05 06:18:544227}
4228
[email protected]8ff00d72012-10-23 19:12:214229} // namespace content