blob: 22661e9e4156a35ed82a837cd137a114dcd1b2a6 [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"
[email protected]b6583592012-01-25 19:52:3369#include "content/browser/site_instance_impl.h"
Kunihiko Sakamoto346a74e2021-03-10 08:57:4870#include "content/browser/web_package/subresource_web_bundle_navigation_info.h"
Tsuyoshi Horo37493af2019-11-21 23:43:1471#include "content/browser/web_package/web_bundle_navigation_info.h"
Camille Lamy5193caa2018-10-12 11:59:4272#include "content/common/content_constants_internal.h"
Nate Chapind1fe3612021-04-16 20:45:5773#include "content/common/navigation_params_utils.h"
Nasko Oskovae49e292020-08-13 02:08:5174#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1175#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1976#include "content/public/browser/content_browser_client.h"
[email protected]d9083482012-01-06 00:38:4677#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0078#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3879#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1680#include "content/public/browser/notification_types.h"
Lei Zhang96031532019-10-10 19:05:4781#include "content/public/browser/render_view_host.h"
[email protected]9677a3c2012-12-22 04:18:5882#include "content/public/browser/render_widget_host.h"
83#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1084#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3485#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:1986#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:3887#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:1088#include "content/public/common/content_features.h"
Lei Zhang96031532019-10-10 19:05:4789#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4390#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:5991#include "media/base/mime_util.h"
[email protected]a23de8572009-06-03 02:16:3292#include "net/base/escape.h"
Arthur Sonzogni620cec62018-12-13 13:08:5793#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:1894#include "services/metrics/public/cpp/ukm_builders.h"
95#include "services/metrics/public/cpp/ukm_recorder.h"
Yue Ru Sun128804932020-09-30 22:19:1796#include "services/metrics/public/cpp/ukm_source_id.h"
[email protected]9677a3c2012-12-22 04:18:5897#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3998#include "third_party/blink/public/common/blob/blob_utils.h"
Gyuyoung Kim107c2a02021-04-13 01:49:3099#include "third_party/blink/public/common/chrome_debug_urls.h"
Miyoung Shin1c565c912021-03-17 12:11:21100#include "third_party/blink/public/common/history/session_history_constants.h"
Blink Reformata30d4232018-04-07 15:31:06101#include "third_party/blink/public/common/mime_util/mime_util.h"
Carlos Caballeroede6f8c2021-01-28 11:01:50102#include "third_party/blink/public/common/page_state/page_state_serialization.h"
Minggang Wang7ee0c742021-06-16 16:16:51103#include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h"
[email protected]cca6f392014-05-28 21:32:26104#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:29105
[email protected]8ff00d72012-10-23 19:12:21106namespace content {
[email protected]e9ba4472008-09-14 15:42:43107namespace {
108
109// Invoked when entries have been pruned, or removed. For example, if the
110// current entries are [google, digg, yahoo], with the current entry google,
111// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47112void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50113 int index,
[email protected]c12bf1a12008-09-17 16:28:49114 int count) {
[email protected]8ff00d72012-10-23 19:12:21115 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50116 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49117 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14118 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43119}
120
[email protected]e9ba4472008-09-14 15:42:43121// Configure all the NavigationEntries in entries for restore. This resets
122// the transition type to reload and makes sure the content state isn't empty.
123void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57124 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48125 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47126 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43127 // Use a transition type of reload so that we don't incorrectly increase
128 // the typed count.
Lei Zhang96031532019-10-10 19:05:47129 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
130 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43131 }
132}
133
[email protected]bf70edce2012-06-20 22:32:22134// Determines whether or not we should be carrying over a user agent override
135// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57136bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22137 return last_entry && last_entry->GetIsOverridingUserAgent();
138}
139
Camille Lamy5193caa2018-10-12 11:59:42140// Determines whether to override user agent for a navigation.
141bool ShouldOverrideUserAgent(
142 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57143 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42144 switch (override_user_agent) {
145 case NavigationController::UA_OVERRIDE_INHERIT:
146 return ShouldKeepOverride(last_committed_entry);
147 case NavigationController::UA_OVERRIDE_TRUE:
148 return true;
149 case NavigationController::UA_OVERRIDE_FALSE:
150 return false;
Camille Lamy5193caa2018-10-12 11:59:42151 }
152 NOTREACHED();
153 return false;
154}
155
Rakina Zata Amni312822d72021-06-04 16:13:37156// Returns true if this navigation should be treated as a reload. For e.g.
clamy0a656e42018-02-06 18:18:28157// navigating to the last committed url via the address bar or clicking on a
Rakina Zata Amni312822d72021-06-04 16:13:37158// link which results in a navigation to the last committed URL (but wasn't
159// converted to do a replacement navigation in the renderer), etc.
Fergal Daly766177d2020-07-07 07:54:04160// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
161// |base_url_for_data_url|, |transition_type| correspond to the new navigation
162// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
163// navigation that committed.
164bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
165 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57166 const GURL& virtual_url,
167 const GURL& base_url_for_data_url,
168 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57169 bool is_post,
Rakina Zata Amni312822d72021-06-04 16:13:37170 bool should_replace_current_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57171 NavigationEntryImpl* last_committed_entry) {
Rakina Zata Amni312822d72021-06-04 16:13:37172 // Navigations intended to do a replacement shouldn't be converted to do a
173 // reload.
174 if (should_replace_current_entry)
clamy0a656e42018-02-06 18:18:28175 return false;
clamy0a656e42018-02-06 18:18:28176 // Only convert to reload if at least one navigation committed.
177 if (!last_committed_entry)
ananta3bdd8ae2016-12-22 17:11:55178 return false;
179
arthursonzogni7a8243682017-12-14 16:41:42180 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28181 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42182 return false;
183
ananta3bdd8ae2016-12-22 17:11:55184 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
185 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
186 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28187 bool transition_type_can_be_converted = false;
188 if (ui::PageTransitionCoreTypeIs(transition_type,
189 ui::PAGE_TRANSITION_RELOAD) &&
190 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
191 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55192 }
clamy0a656e42018-02-06 18:18:28193 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55194 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28195 transition_type_can_be_converted = true;
196 }
197 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
198 transition_type_can_be_converted = true;
199 if (!transition_type_can_be_converted)
200 return false;
201
202 // This check is required for cases like view-source:, etc. Here the URL of
203 // the navigation entry would contain the url of the page, while the virtual
204 // URL contains the full URL including the view-source prefix.
205 if (virtual_url != last_committed_entry->GetVirtualURL())
206 return false;
207
Fergal Daly766177d2020-07-07 07:54:04208 // Check that the URLs match.
209 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
210 // If there's no frame entry then by definition the URLs don't match.
211 if (!frame_entry)
212 return false;
213
214 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28215 return false;
216
217 // This check is required for Android WebView loadDataWithBaseURL. Apps
218 // can pass in anything in the base URL and we need to ensure that these
219 // match before classifying it as a reload.
220 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
221 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
222 return false;
ananta3bdd8ae2016-12-22 17:11:55223 }
224
clamy0a656e42018-02-06 18:18:28225 // Skip entries with SSL errors.
226 if (last_committed_entry->ssl_error())
227 return false;
228
229 // Don't convert to a reload when the last navigation was a POST or the new
230 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04231 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28232 return false;
233
234 return true;
ananta3bdd8ae2016-12-22 17:11:55235}
236
Nasko Oskov03912102019-01-11 00:21:32237bool DoesURLMatchOriginForNavigation(
238 const GURL& url,
Anton Bikineevf62d1bf2021-05-15 17:56:07239 const absl::optional<url::Origin>& origin,
Kunihiko Sakamoto346a74e2021-03-10 08:57:48240 SubresourceWebBundleNavigationInfo*
241 subresource_web_bundle_navigation_info) {
Nasko Oskov03912102019-01-11 00:21:32242 // If there is no origin supplied there is nothing to match. This can happen
243 // for navigations to a pending entry and therefore it should be allowed.
244 if (!origin)
245 return true;
246
Kunihiko Sakamoto346a74e2021-03-10 08:57:48247 if (url.SchemeIs(url::kUrnScheme) && subresource_web_bundle_navigation_info) {
248 // Urn: subframe from WebBundle has an opaque origin derived from the
249 // Bundle's origin.
250 return origin->CanBeDerivedFrom(
251 subresource_web_bundle_navigation_info->bundle_url());
252 }
253
Nasko Oskov03912102019-01-11 00:21:32254 return origin->CanBeDerivedFrom(url);
255}
256
Anton Bikineevf62d1bf2021-05-15 17:56:07257absl::optional<url::Origin> GetCommittedOriginForFrameEntry(
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12258 const mojom::DidCommitProvisionalLoadParams& params,
259 NavigationRequest* request) {
Nasko Oskov03912102019-01-11 00:21:32260 // Error pages commit in an opaque origin, yet have the real URL that resulted
261 // in an error as the |params.url|. Since successful reload of an error page
262 // should commit in the correct origin, setting the opaque origin on the
263 // FrameNavigationEntry will be incorrect.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12264 if (request->DidEncounterError())
Anton Bikineevf62d1bf2021-05-15 17:56:07265 return absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:32266
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12267 // We also currently don't save committed origins for loadDataWithBaseURL
268 // navigations (probably accidentally). Without this check, navigations to
269 // the FrameNavigationEntry might fail the DoesURLMatchOriginForNavigation()
270 // check since the origin will be based on the base URL instead of the data:
271 // URL used for the navigation.
272 // TODO(https://crbug.com/1198406): Save committed origin in
273 // FrameNavigationEntry for this case too.
Rakina Zata Amni5d2ef8aa2021-06-25 01:34:23274 if (request->IsLoadDataWithBaseURLAndHasUnreachableURL())
Anton Bikineevf62d1bf2021-05-15 17:56:07275 return absl::nullopt;
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12276
Anton Bikineevf62d1bf2021-05-15 17:56:07277 return absl::make_optional(params.origin);
Nasko Oskov03912102019-01-11 00:21:32278}
279
Camille Lamy5193caa2018-10-12 11:59:42280bool IsValidURLForNavigation(bool is_main_frame,
281 const GURL& virtual_url,
282 const GURL& dest_url) {
283 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
284 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
285 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
286 << virtual_url.possibly_invalid_spec();
287 return false;
288 }
289
290 // Don't attempt to navigate to non-empty invalid URLs.
291 if (!dest_url.is_valid() && !dest_url.is_empty()) {
292 LOG(WARNING) << "Refusing to load invalid URL: "
293 << dest_url.possibly_invalid_spec();
294 return false;
295 }
296
297 // The renderer will reject IPC messages with URLs longer than
298 // this limit, so don't attempt to navigate with a longer URL.
299 if (dest_url.spec().size() > url::kMaxURLChars) {
300 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
301 << " characters.";
302 return false;
303 }
304
Aaron Colwell33109c592020-04-21 21:31:19305 // Reject renderer debug URLs because they should have been handled before
306 // we get to this point. This check handles renderer debug URLs
307 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
308 // provides defense-in-depth if a renderer debug URL manages to get here via
309 // some other path. We want to reject the navigation here so it doesn't
310 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30311 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19312 LOG(WARNING) << "Refusing to load renderer debug URL: "
313 << dest_url.possibly_invalid_spec();
314 return false;
315 }
316
Camille Lamy5193caa2018-10-12 11:59:42317 return true;
318}
319
Mikel Astizba9cf2fd2017-12-17 10:38:10320// See replaced_navigation_entry_data.h for details: this information is meant
321// to ensure |*output_entry| keeps track of its original URL (landing page in
322// case of server redirects) as it gets replaced (e.g. history.replaceState()),
323// without overwriting it later, for main frames.
324void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57325 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10326 NavigationEntryImpl* output_entry) {
327 if (output_entry->GetReplacedEntryData().has_value())
328 return;
329
330 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57331 data.first_committed_url = replaced_entry->GetURL();
332 data.first_timestamp = replaced_entry->GetTimestamp();
333 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29334 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10335}
336
Anatoliy Potapchuk27278cce22021-06-30 09:45:31337mojom::NavigationType GetNavigationType(const GURL& old_url,
338 const GURL& new_url,
339 ReloadType reload_type,
340 NavigationEntryImpl* entry,
341 const FrameNavigationEntry& frame_entry,
342 bool has_pending_cross_document_commit,
343 bool is_currently_error_page,
344 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23345 // Reload navigations
346 switch (reload_type) {
347 case ReloadType::NORMAL:
Anatoliy Potapchuk27278cce22021-06-30 09:45:31348 return mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23349 case ReloadType::BYPASSING_CACHE:
Anatoliy Potapchuk27278cce22021-06-30 09:45:31350 return mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23351 case ReloadType::ORIGINAL_REQUEST_URL:
Anatoliy Potapchuk27278cce22021-06-30 09:45:31352 return mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23353 case ReloadType::NONE:
354 break; // Fall through to rest of function.
355 }
356
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08357 if (entry->IsRestored()) {
Anatoliy Potapchuk27278cce22021-06-30 09:45:31358 return entry->GetHasPostData() ? mojom::NavigationType::RESTORE_WITH_POST
359 : mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23360 }
361
danakjb952ef12021-01-14 19:58:49362 const bool can_be_same_document =
363 // A pending cross-document commit means this navigation will not occur in
364 // the current document, as that document would end up being replaced in
365 // the meantime.
366 !has_pending_cross_document_commit &&
367 // If the current document is an error page, we should always treat it as
368 // a different-document navigation so that we'll attempt to load the
369 // document we're navigating to (and not stay in the current error page).
370 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12371
clamyea99ea12018-05-28 13:54:23372 // History navigations.
373 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12374 return can_be_same_document && is_same_document_history_load
Anatoliy Potapchuk27278cce22021-06-30 09:45:31375 ? mojom::NavigationType::HISTORY_SAME_DOCUMENT
376 : mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23377 }
378 DCHECK(!is_same_document_history_load);
379
380 // A same-document fragment-navigation happens when the only part of the url
381 // that is modified is after the '#' character.
382 //
383 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12384 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23385 //
386 // Note: this check is only valid for navigations that are not history
387 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
388 // history navigation from 'A#foo' to 'A#bar' is not a same-document
389 // navigation, but a different-document one. This is why history navigation
390 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47391 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
392 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12393
394 // The one case where we do the wrong thing here and incorrectly choose
395 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
396 // the renderer is a frameset. All frameset navigations should be
397 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
398 // navigation would do the right thing, as it would send it to the browser and
399 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
400 // into this method). But since we can't tell that case here for browser-
401 // initiated navigations, we have to get the renderer involved. In that case
402 // the navigation would be restarted due to the renderer spending a reply of
403 // mojom::CommitResult::RestartCrossDocument.
404
405 return can_be_same_document && is_same_doc
Anatoliy Potapchuk27278cce22021-06-30 09:45:31406 ? mojom::NavigationType::SAME_DOCUMENT
407 : mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23408}
409
Camille Lamy5193caa2018-10-12 11:59:42410// Adjusts the original input URL if needed, to get the URL to actually load and
411// the virtual URL, which may differ.
412void RewriteUrlForNavigation(const GURL& original_url,
413 BrowserContext* browser_context,
414 GURL* url_to_load,
415 GURL* virtual_url,
416 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42417 // Allow the browser URL handler to rewrite the URL. This will, for example,
418 // remove "view-source:" from the beginning of the URL to get the URL that
419 // will actually be loaded. This real URL won't be shown to the user, just
420 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31421 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42422 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
423 url_to_load, browser_context, reverse_on_redirect);
424}
425
426#if DCHECK_IS_ON()
427// Helper sanity check function used in debug mode.
428void ValidateRequestMatchesEntry(NavigationRequest* request,
429 NavigationEntryImpl* entry) {
430 if (request->frame_tree_node()->IsMainFrame()) {
431 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
432 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
Anatoliy Potapchuk27278cce22021-06-30 09:45:31433 request->common_params().transition, entry->GetTransitionType()));
Camille Lamy5193caa2018-10-12 11:59:42434 }
Nasko Oskovc36327d2019-01-03 23:23:04435 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42436 entry->should_clear_history_list());
437 DCHECK_EQ(request->common_params().has_user_gesture,
438 entry->has_user_gesture());
439 DCHECK_EQ(request->common_params().base_url_for_data_url,
440 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04441 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42442 entry->GetCanLoadLocalResources());
443 DCHECK_EQ(request->common_params().started_from_context_menu,
444 entry->has_started_from_context_menu());
445
446 FrameNavigationEntry* frame_entry =
447 entry->GetFrameEntry(request->frame_tree_node());
448 if (!frame_entry) {
449 NOTREACHED();
450 return;
451 }
452
Camille Lamy5193caa2018-10-12 11:59:42453 DCHECK_EQ(request->common_params().method, frame_entry->method());
454
Nasko Oskovc36327d2019-01-03 23:23:04455 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42456 if (redirect_size == frame_entry->redirect_chain().size()) {
457 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04458 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42459 frame_entry->redirect_chain()[i]);
460 }
461 } else {
462 NOTREACHED();
463 }
464}
465#endif // DCHECK_IS_ON()
466
Dave Tapuska8bfd84c2019-03-26 20:47:16467// Returns whether the session history NavigationRequests in |navigations|
468// would stay within the subtree of the sandboxed iframe in
469// |sandbox_frame_tree_node_id|.
470bool DoesSandboxNavigationStayWithinSubtree(
471 int sandbox_frame_tree_node_id,
472 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
473 for (auto& item : navigations) {
474 bool within_subtree = false;
475 // Check whether this NavigationRequest affects a frame within the
476 // sandboxed frame's subtree by walking up the tree looking for the
477 // sandboxed frame.
478 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03479 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16480 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
481 within_subtree = true;
482 break;
483 }
484 }
485 if (!within_subtree)
486 return false;
487 }
488 return true;
489}
490
Titouan Rigoudy6ec70402021-02-02 15:42:19491bool ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(
Antonio Sartori78a749f2020-11-30 12:03:39492 const NavigationRequest* request) {
493 // For local schemes we need to store the policy container in the
494 // FrameNavigationEntry, so that we can reload it in case of history
495 // navigation.
496 //
497 // TODO(https://crbug.com/1146361 and https://crbug.com/1146362): blob: and
498 // filesystem: should be removed from this list when we have properly
499 // implemented storing their policy container in the respective store.
500 return (request->common_params().url.SchemeIs(url::kAboutScheme) ||
501 request->common_params().url.SchemeIs(url::kDataScheme) ||
502 request->common_params().url.SchemeIsBlob() ||
503 request->common_params().url.SchemeIsFileSystem());
504}
505
[email protected]e9ba4472008-09-14 15:42:43506} // namespace
507
arthursonzogni66f711c2019-10-08 14:40:36508// NavigationControllerImpl::PendingEntryRef------------------------------------
509
510NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
511 base::WeakPtr<NavigationControllerImpl> controller)
512 : controller_(controller) {}
513
514NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
515 if (!controller_) // Can be null with interstitials.
516 return;
517
518 controller_->PendingEntryRefDeleted(this);
519}
520
[email protected]d202a7c2012-01-04 07:53:47521// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29522
[email protected]23a918b2014-07-15 09:51:36523const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23524
[email protected]765b35502008-08-21 00:51:20525// static
[email protected]d202a7c2012-01-04 07:53:47526size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23527 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20528
[email protected]e6fec472013-05-14 05:29:02529// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20530// when testing.
531static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29532
[email protected]71fde352011-12-29 03:29:56533// static
dcheng9bfa5162016-04-09 01:00:57534std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
535 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10536 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07537 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10538 ui::PageTransition transition,
539 bool is_renderer_initiated,
540 const std::string& extra_headers,
541 BrowserContext* browser_context,
542 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
543 return NavigationControllerImpl::CreateNavigationEntry(
544 url, referrer, std::move(initiator_origin),
545 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Hayato Ito303654c2021-06-30 09:07:54546 extra_headers, browser_context, std::move(blob_url_loader_factory));
Lukasz Anforowicz641234d52019-11-07 21:07:10547}
548
549// static
550std::unique_ptr<NavigationEntryImpl>
551NavigationControllerImpl::CreateNavigationEntry(
552 const GURL& url,
553 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07554 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10555 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57556 ui::PageTransition transition,
557 bool is_renderer_initiated,
558 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09559 BrowserContext* browser_context,
Hayato Ito303654c2021-06-30 09:07:54560 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
Camille Lamy5193caa2018-10-12 11:59:42561 GURL url_to_load;
562 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56563 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42564 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
565 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56566
Lukasz Anforowicz641234d52019-11-07 21:07:10567 // Let the NTP override the navigation params and pretend that this is a
568 // browser-initiated, bookmark-like navigation.
569 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violetcf6ea7e2021-06-09 21:09:21570 source_site_instance, &transition, &is_renderer_initiated, &referrer,
571 &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10572
Patrick Monettef507e982019-06-19 20:18:06573 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28574 nullptr, // The site instance for tabs is sent on navigation
575 // (WebContents::GetSiteInstance).
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58576 url_to_load, referrer, initiator_origin, std::u16string(), transition,
Camille Lamy5193caa2018-10-12 11:59:42577 is_renderer_initiated, blob_url_loader_factory);
578 entry->SetVirtualURL(virtual_url);
579 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56580 entry->set_update_virtual_url_with_url(reverse_on_redirect);
581 entry->set_extra_headers(extra_headers);
Patrick Monettef507e982019-06-19 20:18:06582 return entry;
[email protected]71fde352011-12-29 03:29:56583}
584
[email protected]cdcb1dee2012-01-04 00:46:20585// static
586void NavigationController::DisablePromptOnRepost() {
587 g_check_for_repost = false;
588}
589
[email protected]c5b88d82012-10-06 17:03:33590base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
591 base::Time t) {
592 // If |t| is between the water marks, we're in a run of duplicates
593 // or just getting out of it, so increase the high-water mark to get
594 // a time that probably hasn't been used before and return it.
595 if (low_water_mark_ <= t && t <= high_water_mark_) {
596 high_water_mark_ += base::TimeDelta::FromMicroseconds(1);
597 return high_water_mark_;
598 }
599
600 // Otherwise, we're clear of the last duplicate run, so reset the
601 // water marks.
602 low_water_mark_ = high_water_mark_ = t;
603 return t;
604}
605
ckitagawa0faa5e42020-06-17 17:30:54606NavigationControllerImpl::ScopedShowRepostDialogForTesting::
607 ScopedShowRepostDialogForTesting()
608 : was_disallowed_(g_check_for_repost) {
609 g_check_for_repost = true;
610}
611
612NavigationControllerImpl::ScopedShowRepostDialogForTesting::
613 ~ScopedShowRepostDialogForTesting() {
614 g_check_for_repost = was_disallowed_;
615}
616
[email protected]d202a7c2012-01-04 07:53:47617NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00618 BrowserContext* browser_context,
619 FrameTree& frame_tree,
620 NavigationControllerDelegate* delegate)
621 : frame_tree_(frame_tree),
622 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57623 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22624 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47625 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37626 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29627}
628
[email protected]d202a7c2012-01-04 07:53:47629NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00630 // The NavigationControllerImpl might be called inside its delegate
631 // destructor. Calling it is not valid anymore.
632 delegate_ = nullptr;
633 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29634}
635
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57636WebContents* NavigationControllerImpl::GetWebContents() {
[email protected]ec6c05f2013-10-23 18:41:57637 return delegate_->GetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32638}
639
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57640BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55641 return browser_context_;
642}
643
[email protected]d202a7c2012-01-04 07:53:47644void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30645 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36646 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57647 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57648 // Verify that this controller is unused and that the input is valid.
Lei Zhang96031532019-10-10 19:05:47649 DCHECK_EQ(0, GetEntryCount());
650 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57651 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14652 selected_navigation < static_cast<int>(entries->size()));
arthursonzogni5c4c202d2017-04-25 23:41:27653 DCHECK_EQ(-1, pending_entry_index_);
[email protected]ce3fa3c2009-04-20 19:55:57654
[email protected]ce3fa3c2009-04-20 19:55:57655 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44656 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03657 entries_.reserve(entries->size());
658 for (auto& entry : *entries)
dcheng36b6aec92015-12-26 06:16:36659 entries_.push_back(
660 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
avif16f85a72015-11-13 18:25:03661
662 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
663 // cleared out safely.
664 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57665
666 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36667 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57668}
669
toyoshim6142d96f2016-12-19 09:07:25670void NavigationControllerImpl::Reload(ReloadType reload_type,
671 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28672 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28673 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32674 int current_index = -1;
675
Carlos IL42b416592019-10-07 23:10:36676 if (entry_replaced_by_post_commit_error_) {
677 // If there is an entry that was replaced by a currently active post-commit
678 // error navigation, this can't be the initial navigation.
679 DCHECK(!IsInitialNavigation());
680 // If the current entry is a post commit error, we reload the entry it
681 // replaced instead. We leave the error entry in place until a commit
682 // replaces it, but the pending entry points to the original entry in the
683 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
684 // case that pending_entry_ != entries_[pending_entry_index_].
685 entry = entry_replaced_by_post_commit_error_.get();
686 current_index = GetCurrentEntryIndex();
687 } else if (IsInitialNavigation() && pending_entry_) {
688 // If we are reloading the initial navigation, just use the current
689 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32690 entry = pending_entry_;
691 // The pending entry might be in entries_ (e.g., after a Clone), so we
692 // should also update the current_index.
693 current_index = pending_entry_index_;
694 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00695 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32696 current_index = GetCurrentEntryIndex();
697 if (current_index != -1) {
creis3da03872015-02-20 21:12:32698 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32699 }
[email protected]979a4bc2013-04-24 01:27:15700 }
[email protected]241db352013-04-22 18:04:05701
[email protected]59167c22013-06-03 18:07:32702 // If we are no where, then we can't reload. TODO(darin): We should add a
703 // CanReload method.
704 if (!entry)
705 return;
706
Takashi Toyoshimac7df3c22019-06-25 14:18:47707 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26708 entry->set_reload_type(reload_type);
709
Aran Gilman37d11632019-10-08 23:07:15710 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30711 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07712 // they really want to do this. If they do, the dialog will call us back
713 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57714 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02715
[email protected]106a0812010-03-18 00:15:12716 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57717 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47718 return;
initial.commit09911bf2008-07-26 23:55:29719 }
Lei Zhang96031532019-10-10 19:05:47720
721 if (!IsInitialNavigation())
722 DiscardNonCommittedEntries();
723
724 pending_entry_ = entry;
725 pending_entry_index_ = current_index;
726 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
727
728 NavigateToExistingPendingEntry(reload_type,
729 FrameTreeNode::kFrameTreeNodeInvalidId);
initial.commit09911bf2008-07-26 23:55:29730}
731
[email protected]d202a7c2012-01-04 07:53:47732void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48733 DCHECK(pending_reload_ != ReloadType::NONE);
734 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12735}
736
[email protected]d202a7c2012-01-04 07:53:47737void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48738 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12739 NOTREACHED();
740 } else {
toyoshim6142d96f2016-12-19 09:07:25741 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48742 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12743 }
744}
745
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57746bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09747 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11748}
749
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57750bool NavigationControllerImpl::IsInitialBlankNavigation() {
creis10a4ab72015-10-13 17:22:40751 // TODO(creis): Once we create a NavigationEntry for the initial blank page,
toyoshim0df1d3a2016-09-09 09:52:48752 // we'll need to check for entry count 1 and restore_type NONE (to exclude
753 // the cloned tab case).
creis10a4ab72015-10-13 17:22:40754 return IsInitialNavigation() && GetEntryCount() == 0;
755}
756
Aran Gilman37d11632019-10-08 23:07:15757NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
758 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58759 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03760 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58761}
762
W. James MacLean1c40862c2020-04-27 21:05:57763void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
764 const url::Origin& origin) {
765 for (int i = 0; i < GetEntryCount(); i++) {
766 auto* entry = GetEntryAtIndex(i);
767 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
768 }
769 if (entry_replaced_by_post_commit_error_) {
770 // It's possible we could come back to this entry if the error
771 // page/interstitial goes away.
772 entry_replaced_by_post_commit_error_
773 ->RegisterExistingOriginToPreventOptInIsolation(origin);
774 }
775 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
776 // visiting pending_entry_, which lacks a committed origin. This will be done
777 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
778}
779
avi25764702015-06-23 15:43:37780void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57781 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00782 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37783 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27784 DCHECK_EQ(-1, pending_entry_index_);
[email protected]8ff00d72012-10-23 19:12:21785 NotificationService::current()->Notify(
Aran Gilman37d11632019-10-08 23:07:15786 NOTIFICATION_NAV_ENTRY_PENDING, Source<NavigationController>(this),
avi25764702015-06-23 15:43:37787 Details<NavigationEntry>(pending_entry_));
[email protected]765b35502008-08-21 00:51:20788}
789
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57790NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47791 if (pending_entry_)
792 return pending_entry_;
793 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20794}
795
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57796NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32797 // The pending entry is safe to return for new (non-history), browser-
798 // initiated navigations. Most renderer-initiated navigations should not
799 // show the pending entry, to prevent URL spoof attacks.
800 //
801 // We make an exception for renderer-initiated navigations in new tabs, as
802 // long as no other page has tried to access the initial empty document in
803 // the new tab. If another page modifies this blank page, a URL spoof is
804 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32805 bool safe_to_show_pending =
806 pending_entry_ &&
807 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09808 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32809 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46810 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32811
812 // Also allow showing the pending entry for history navigations in a new tab,
813 // such as Ctrl+Back. In this case, no existing page is visible and no one
814 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15815 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
816 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32817 safe_to_show_pending = true;
818
819 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19820 return pending_entry_;
821 return GetLastCommittedEntry();
822}
823
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57824int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20825 if (pending_entry_index_ != -1)
826 return pending_entry_index_;
827 return last_committed_entry_index_;
828}
829
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57830NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20831 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28832 return nullptr;
avif16f85a72015-11-13 18:25:03833 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20834}
835
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57836bool NavigationControllerImpl::CanViewSource() {
Carlos Caballeroede6f8c2021-01-28 11:01:50837 const std::string& mime_type = frame_tree_.root()
Alex Moshchuk2e470ea2021-02-03 06:46:34838 ->current_frame_host()
839 ->render_view_host()
Carlos Caballeroede6f8c2021-01-28 11:01:50840 ->contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53841 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
842 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27843 NavigationEntry* visible_entry = GetVisibleEntry();
844 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39845 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16846}
847
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57848int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27849 // The last committed entry index must always be less than the number of
Carlos IL4dea8902020-05-26 15:14:29850 // entries. If there are no entries, it must be -1.
arthursonzogni5c4c202d2017-04-25 23:41:27851 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
852 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55853 return last_committed_entry_index_;
854}
855
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57856int NavigationControllerImpl::GetEntryCount() {
Carlos IL4dea8902020-05-26 15:14:29857 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55858 return static_cast<int>(entries_.size());
859}
860
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57861NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37862 if (index < 0 || index >= GetEntryCount())
863 return nullptr;
864
avif16f85a72015-11-13 18:25:03865 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25866}
867
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57868NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47869 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20870}
871
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57872int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46873 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03874}
875
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57876bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03877 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
878 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
879 return true;
880 }
881 return false;
[email protected]765b35502008-08-21 00:51:20882}
883
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57884bool NavigationControllerImpl::CanGoForward() {
WangHui74286d52021-03-31 16:17:15885 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
886 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
887 return true;
888 }
889 return false;
[email protected]765b35502008-08-21 00:51:20890}
891
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57892bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03893 int index = GetIndexForOffset(offset);
894 return index >= 0 && index < GetEntryCount();
895}
896
WangHui74286d52021-03-31 16:17:15897#if defined(OS_ANDROID)
898bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
WangHui74286d52021-03-31 16:17:15899 if (offset == 0)
900 return true;
901 int increment = offset > 0 ? 1 : -1;
902 int non_skippable_entries = 0;
903 for (int index = GetIndexForOffset(increment);
904 index >= 0 && index < GetEntryCount(); index += increment) {
905 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
906 non_skippable_entries++;
907
908 if (non_skippable_entries == std::abs(offset))
909 return true;
910 }
911 return false;
912}
913#endif
914
[email protected]d202a7c2012-01-04 07:53:47915void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06916 int target_index = GetIndexForOffset(-1);
917
Elly Fong-Jones40ee8112021-06-23 19:10:52918 // Move the target index past the skippable entries.
Shivani Sharma298d12852019-01-22 20:04:03919 bool all_skippable_entries = true;
Elly Fong-Jones40ee8112021-06-23 19:10:52920 while (target_index >= 0) {
921 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui()) {
Shivani Sharma298d12852019-01-22 20:04:03922 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06923 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16924 }
Elly Fong-Jones40ee8112021-06-23 19:10:52925 target_index--;
shivanisha55201872018-12-13 04:29:06926 }
Miyoung Shin1c565c912021-03-17 12:11:21927
Shivani Sharma298d12852019-01-22 20:04:03928 // Do nothing if all entries are skippable. Normally this path would not
929 // happen as consumers would have already checked it in CanGoBack but a lot of
930 // tests do not do that.
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42931 if (all_skippable_entries)
Shivani Sharma298d12852019-01-22 20:04:03932 return;
shivanisha55201872018-12-13 04:29:06933
shivanisha55201872018-12-13 04:29:06934 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20935}
936
[email protected]d202a7c2012-01-04 07:53:47937void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06938 int target_index = GetIndexForOffset(1);
939
Shivani Sharma2d5b4b6b2019-01-08 16:07:16940 // Note that at least one entry (the last one) will be non-skippable since
941 // entries are marked skippable only when they add another entry because of
942 // redirect or pushState.
Elly Fong-Jones40ee8112021-06-23 19:10:52943 while (target_index < static_cast<int>(entries_.size())) {
944 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui())
shivanisha55201872018-12-13 04:29:06945 break;
Elly Fong-Jones40ee8112021-06-23 19:10:52946 target_index++;
shivanisha55201872018-12-13 04:29:06947 }
shivanisha55201872018-12-13 04:29:06948 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20949}
950
[email protected]d202a7c2012-01-04 07:53:47951void NavigationControllerImpl::GoToIndex(int index) {
Dave Tapuska8bfd84c2019-03-26 20:47:16952 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId);
953}
954
955void NavigationControllerImpl::GoToIndex(int index,
956 int sandbox_frame_tree_node_id) {
sunjian30574a62017-03-21 21:39:44957 TRACE_EVENT0("browser,navigation,benchmark",
958 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:20959 if (index < 0 || index >= static_cast<int>(entries_.size())) {
960 NOTREACHED();
961 return;
962 }
963
[email protected]cbab76d2008-10-13 22:42:47964 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20965
arthursonzogni5c4c202d2017-04-25 23:41:27966 DCHECK_EQ(nullptr, pending_entry_);
967 DCHECK_EQ(-1, pending_entry_index_);
968 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:20969 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:27970 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
971 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Dave Tapuska8bfd84c2019-03-26 20:47:16972 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id);
[email protected]765b35502008-08-21 00:51:20973}
974
[email protected]d202a7c2012-01-04 07:53:47975void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:12976 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:03977 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:20978 return;
979
[email protected]9ba14052012-06-22 23:50:03980 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20981}
982
WangHui74286d52021-03-31 16:17:15983#if defined(OS_ANDROID)
984void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
985 // Note: This is actually reached in unit tests.
986 if (!CanGoToOffsetWithSkipping(offset))
987 return;
988
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42989 if (offset == 0) {
WangHui74286d52021-03-31 16:17:15990 GoToIndex(GetIndexForOffset(offset));
991 return;
992 }
993 int increment = offset > 0 ? 1 : -1;
994 // Find the offset without counting skippable entries.
995 int target_index = GetIndexForOffset(increment);
996 int non_skippable_entries = 0;
997 for (int index = target_index; index >= 0 && index < GetEntryCount();
998 index += increment) {
999 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1000 non_skippable_entries++;
1001
1002 if (non_skippable_entries == std::abs(offset)) {
1003 target_index = index;
1004 break;
1005 }
1006 }
1007
1008 GoToIndex(target_index);
1009}
1010#endif
1011
[email protected]41374f12013-07-24 02:49:281012bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151013 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281014 return false;
[email protected]6a13a6c2011-12-20 21:47:121015
[email protected]43032342011-03-21 14:10:311016 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281017 return true;
[email protected]cbab76d2008-10-13 22:42:471018}
1019
Michael Thiessen9b14d512019-09-23 21:19:471020void NavigationControllerImpl::PruneForwardEntries() {
1021 DiscardNonCommittedEntries();
1022 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471023 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471024 if (num_removed <= 0)
1025 return;
1026 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1027 NotifyPrunedEntries(this, remove_start_index /* start index */,
1028 num_removed /* count */);
1029}
1030
Aran Gilman37d11632019-10-08 23:07:151031void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1032 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321033 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311034 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511035 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1036 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321037 }
1038}
1039
Harkiran Bolariaba823e42021-05-21 18:30:361040base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL(
1041 const GURL& url,
1042 const Referrer& referrer,
1043 ui::PageTransition transition,
1044 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471045 LoadURLParams params(url);
1046 params.referrer = referrer;
1047 params.transition_type = transition;
1048 params.extra_headers = extra_headers;
Harkiran Bolariaba823e42021-05-21 18:30:361049 return LoadURLWithParams(params);
[email protected]cf002332012-08-14 19:17:471050}
1051
Harkiran Bolariaba823e42021-05-21 18:30:361052base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams(
1053 const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061054 if (params.is_renderer_initiated)
1055 DCHECK(params.initiator_origin.has_value());
1056
naskob8744d22014-08-28 17:07:431057 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151058 "NavigationControllerImpl::LoadURLWithParams", "url",
1059 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291060 bool is_explicit_navigation =
1061 GetContentClient()->browser()->IsExplicitNavigation(
1062 params.transition_type);
1063 if (HandleDebugURL(params.url, params.transition_type,
1064 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431065 // If Telemetry is running, allow the URL load to proceed as if it's
1066 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491067 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431068 cc::switches::kEnableGpuBenchmarking))
Harkiran Bolariaba823e42021-05-21 18:30:361069 return nullptr;
[email protected]47752982014-07-29 08:01:431070 }
[email protected]8bf1048012012-02-08 01:22:181071
[email protected]cf002332012-08-14 19:17:471072 // Checks based on params.load_type.
1073 switch (params.load_type) {
1074 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431075 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471076 break;
1077 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261078 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471079 NOTREACHED() << "Data load must use data scheme.";
Harkiran Bolariaba823e42021-05-21 18:30:361080 return nullptr;
[email protected]cf002332012-08-14 19:17:471081 }
1082 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461083 }
[email protected]e47ae9472011-10-13 19:48:341084
[email protected]e47ae9472011-10-13 19:48:341085 // The user initiated a load, we don't need to reload anymore.
1086 needs_reload_ = false;
1087
Harkiran Bolariaba823e42021-05-21 18:30:361088 return NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441089}
1090
Mohamed Abdelhalim833de902019-09-16 17:41:451091bool NavigationControllerImpl::PendingEntryMatchesRequest(
1092 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191093 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451094 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191095}
1096
[email protected]d202a7c2012-01-04 07:53:471097bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321098 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071099 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331100 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441101 bool is_same_document_navigation,
Nate Chapinc7019dd7d2021-06-25 18:29:251102 bool was_on_initial_empty_document,
Shivani Sharmaffb32b82019-04-09 16:58:471103 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131104 NavigationRequest* navigation_request) {
1105 DCHECK(navigation_request);
[email protected]cd2e15742013-03-08 04:08:311106 is_initial_navigation_ = false;
1107
[email protected]0e8db942008-09-24 21:21:481108 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431109 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481110 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361111 if (entry_replaced_by_post_commit_error_) {
1112 if (is_same_document_navigation) {
1113 // Same document navigations should not be possible on error pages and
1114 // would leave the controller in a weird state. Kill the renderer if
1115 // that happens.
1116 bad_message::ReceivedBadMessage(
1117 rfh->GetProcess(), bad_message::NC_SAME_DOCUMENT_POST_COMMIT_ERROR);
1118 }
1119 // Any commit while a post-commit error page is showing should put the
1120 // original entry back, replacing the error page's entry. This includes
1121 // reloads, where the original entry was used as the pending entry and
1122 // should now be at the correct index at commit time.
1123 entries_[last_committed_entry_index_] =
1124 std::move(entry_replaced_by_post_commit_error_);
1125 }
Fergal Daly8e33cf62020-12-12 01:06:071126 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551127 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431128 if (pending_entry_ &&
1129 pending_entry_->GetIsOverridingUserAgent() !=
1130 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1131 overriding_user_agent_changed = true;
[email protected]0e8db942008-09-24 21:21:481132 } else {
Gang Wu325f03f42021-02-25 20:00:461133 // GetLastCommittedEntry() is null, so this is the first entry.
Fergal Daly8e33cf62020-12-12 01:06:071134 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481135 details->previous_entry_index = -1;
Gang Wu325f03f42021-02-25 20:00:461136 if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) {
1137 // Default setting is NOT override the user agent, so overriding the user
1138 // agent in first entry should be considered as user agent changed as
1139 // well.
1140 overriding_user_agent_changed = true;
1141 }
[email protected]0e8db942008-09-24 21:21:481142 }
[email protected]ecd9d8702008-08-28 22:10:171143
Alexander Timind2f2e4f22019-04-02 20:04:531144 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1145 // implementing back-forward cache.
1146
1147 // Create a new metrics object or reuse the previous one depending on whether
1148 // it's a main frame navigation or not.
1149 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics =
1150 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1151 GetLastCommittedEntry(), !rfh->GetParent(),
1152 params.document_sequence_number);
1153 // Notify the last active entry that we have navigated away.
1154 if (!rfh->GetParent() && !is_same_document_navigation) {
1155 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1156 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Hajime Hoshic7606502021-04-14 02:42:161157 metrics->MainFrameDidNavigateAwayFromDocument(rfh, navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531158 }
1159 }
1160 }
1161
fdegans9caf66a2015-07-30 21:10:421162 // If there is a pending entry at this point, it should have a SiteInstance,
1163 // except for restored entries.
jam48cea9082017-02-15 06:13:291164 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481165 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081166 pending_entry_->IsRestored());
1167 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151168 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291169 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481170 }
[email protected]e9ba4472008-09-14 15:42:431171
Nasko Oskovaee2f862018-06-15 00:05:521172 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1173 // has changed, this must be treated as a new navigation with replacement.
1174 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491175 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521176 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101177 pending_entry_->GetUniqueID() ==
1178 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521179 pending_entry_->site_instance() &&
1180 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1181 DCHECK_NE(-1, pending_entry_index_);
1182 // TODO(nasko,creis): Instead of setting this value here, set
1183 // should_replace_current_entry on the parameters we send to the
1184 // renderer process as part of CommitNavigation. The renderer should
1185 // in turn send it back here as part of |params| and it can be just
1186 // enforced and renderer process terminated on mismatch.
1187 details->did_replace_entry = true;
1188 } else {
1189 // The renderer tells us whether the navigation replaces the current entry.
1190 details->did_replace_entry = params.should_replace_current_entry;
1191 }
[email protected]bcd904482012-02-01 01:54:221192
[email protected]e9ba4472008-09-14 15:42:431193 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amnif6950d552020-11-24 03:26:101194 details->type = ClassifyNavigation(rfh, params, navigation_request);
[email protected]4bf3522c2010-08-19 21:00:201195
eugenebutee08663a2017-04-27 17:43:121196 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441197 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121198
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071199 details->is_prerender_activation =
1200 navigation_request->IsPrerenderedPageActivation();
1201
Peter Boströmd7592132019-01-30 04:50:311202 // Make sure we do not discard the pending entry for a different ongoing
1203 // navigation when a same document commit comes in unexpectedly from the
1204 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1205 // other navigation types. See https://crbug.com/900036.
1206 // TODO(crbug.com/926009): Handle history.pushState() as well.
1207 bool keep_pending_entry = is_same_document_navigation &&
Charlie Reisc0f17d2d2021-01-12 18:52:491208 details->type == NAVIGATION_TYPE_EXISTING_ENTRY &&
Peter Boströmd7592132019-01-30 04:50:311209 pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451210 !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311211
[email protected]0e8db942008-09-24 21:21:481212 switch (details->type) {
Charlie Reisc0f17d2d2021-01-12 18:52:491213 case NAVIGATION_TYPE_NEW_ENTRY:
1214 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051215 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451216 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431217 break;
Charlie Reisc0f17d2d2021-01-12 18:52:491218 case NAVIGATION_TYPE_EXISTING_ENTRY:
1219 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1220 was_restored, navigation_request,
1221 keep_pending_entry);
[email protected]e9ba4472008-09-14 15:42:431222 break;
[email protected]8ff00d72012-10-23 19:12:211223 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471224 RendererDidNavigateNewSubframe(
1225 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451226 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431227 break;
[email protected]8ff00d72012-10-23 19:12:211228 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391229 if (!RendererDidNavigateAutoSubframe(
Nate Chapinc7019dd7d2021-06-25 18:29:251230 rfh, params, details->is_same_document,
1231 was_on_initial_empty_document, navigation_request)) {
creisce0ef3572017-01-26 17:53:081232 // We don't send a notification about auto-subframe PageState during
1233 // UpdateStateForFrame, since it looks like nothing has changed. Send
1234 // it here at commit time instead.
1235 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431236 return false;
creis59d5a47cb2016-08-24 23:57:191237 }
[email protected]e9ba4472008-09-14 15:42:431238 break;
[email protected]8ff00d72012-10-23 19:12:211239 case NAVIGATION_TYPE_NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:491240 // If a pending navigation was in progress, this canceled it. We should
1241 // discard it and make sure it is removed from the URL bar. After that,
1242 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:431243 // the caller that nothing has happened.
arthursonzogni69a6a1b2019-09-17 09:23:001244 if (pending_entry_)
[email protected]20d1c992011-04-12 21:17:491245 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431246 return false;
Aran Gilman37d11632019-10-08 23:07:151247 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431248 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151249 break;
[email protected]765b35502008-08-21 00:51:201250 }
1251
[email protected]688aa65c62012-09-28 04:32:221252 // At this point, we know that the navigation has just completed, so
1253 // record the time.
1254 //
1255 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261256 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331257 base::Time timestamp =
1258 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1259 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131260 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221261
Peter Boströmd7592132019-01-30 04:50:311262 // If we aren't keeping the pending entry, there shouldn't be one at this
1263 // point. Clear it again in case any error cases above forgot to do so.
1264 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1265 // been cleared instead of protecting against it.
1266 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001267 DiscardNonCommittedEntries();
[email protected]f233e4232013-02-23 00:55:141268
[email protected]e9ba4472008-09-14 15:42:431269 // All committed entries should have nonempty content state so WebKit doesn't
1270 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471271 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321272 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221273 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441274 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531275 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1276 // implementing back-forward cache.
1277 if (!active_entry->back_forward_cache_metrics()) {
1278 active_entry->set_back_forward_cache_metrics(
1279 std::move(back_forward_cache_metrics));
1280 }
1281 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
Hajime Hoshi446206e2019-10-18 18:36:251282 navigation_request,
1283 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
naskoc7533512016-05-06 17:01:121284
Charles Reisc0507202017-09-21 00:40:021285 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1286 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1287 // A mismatch can occur if the renderer lies or due to a unique name collision
1288 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121289 FrameNavigationEntry* frame_entry =
1290 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021291 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1292 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031293 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081294 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1295 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031296 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201297
1298 // Remember the bindings the renderer process has at this point, so that
1299 // we do not grant this entry additional bindings if we come back to it.
1300 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301301 }
[email protected]132e281a2012-07-31 18:32:441302
[email protected]97d8f0d2013-10-29 16:49:211303 // Once it is committed, we no longer need to track several pieces of state on
1304 // the entry.
naskoc7533512016-05-06 17:01:121305 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131306
[email protected]49bd30e62011-03-22 20:12:591307 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221308 // TODO(creis): This check won't pass for subframes until we create entries
1309 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001310 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421311 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591312
[email protected]e9ba4472008-09-14 15:42:431313 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001314 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001315 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311316 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531317
arthursonzogni7ddc6542021-04-09 09:16:501318 active_entry->SetIsOverridingUserAgent(
1319 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031320
[email protected]93f230e02011-06-01 14:40:001321 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291322
John Abd-El-Malek380d3c5922017-09-08 00:20:311323 if (active_entry->GetURL().SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121324 !navigation_request->DidEncounterError()) {
John Abd-El-Malek969cdd94e2017-07-10 22:18:161325 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus",
1326 !!active_entry->GetSSL().certificate);
1327 }
1328
aelias100c9192017-01-13 00:01:431329 if (overriding_user_agent_changed)
1330 delegate_->UpdateOverridingUserAgent();
1331
creis03b48002015-11-04 00:54:561332 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1333 // one knows the latest NavigationEntry it is showing (whether it has
1334 // committed anything in this navigation or not). This allows things like
1335 // state and title updates from RenderFrames to apply to the latest relevant
1336 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381337 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001338 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381339 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
Hayato Ito2c8c08d02021-06-23 03:38:431340
1341 if (navigation_request->IsPrerenderedPageActivation()) {
1342 BroadcastHistoryOffsetAndLength();
1343 // TODO(crbug.com/1222893): Broadcasting happens after the prerendered page
1344 // is activated. As a result, a "prerenderingchange" event listener sees the
1345 // history.length which is not updated yet. We should guarantee that
1346 // history's length and offset should be updated before a
1347 // "prerenderingchange" event listener runs. One possible approach is to use
1348 // the same IPC which "prerenderingchange" uses, and propagate history's
1349 // length and offset together with that.
1350 }
1351
[email protected]e9ba4472008-09-14 15:42:431352 return true;
initial.commit09911bf2008-07-26 23:55:291353}
1354
[email protected]8ff00d72012-10-23 19:12:211355NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321356 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101357 const mojom::DidCommitProvisionalLoadParams& params,
1358 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591359 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511360 "ClassifyNavigation");
1361
avi7c6f35e2015-05-08 17:52:381362 if (params.did_create_new_entry) {
Charlie Reisc0f17d2d2021-01-12 18:52:491363 // A new entry. We may or may not have a corresponding pending entry, and
avi7c6f35e2015-05-08 17:52:381364 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001365 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491366 trace_return.set_return_reason("new entry, no parent, new entry");
1367 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381368 }
1369
1370 // When this is a new subframe navigation, we should have a committed page
1371 // in which it's a subframe. This may not be the case when an iframe is
1372 // navigated on a popup navigated to about:blank (the iframe would be
1373 // written into the popup by script on the main page). For these cases,
1374 // there isn't any navigation stuff we can do, so just ignore it.
Nasko Oskovae49e292020-08-13 02:08:511375 if (!GetLastCommittedEntry()) {
1376 trace_return.set_return_reason("new entry, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381377 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511378 }
avi7c6f35e2015-05-08 17:52:381379
1380 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511381 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381382 return NAVIGATION_TYPE_NEW_SUBFRAME;
1383 }
1384
Charlie Reisc0f17d2d2021-01-12 18:52:491385 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381386 DCHECK(!params.history_list_was_cleared);
1387
avi39c1edd32015-06-04 20:06:001388 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381389 // All manual subframes would be did_create_new_entry and handled above, so
1390 // we know this is auto.
Nasko Oskovae49e292020-08-13 02:08:511391 if (GetLastCommittedEntry()) {
1392 trace_return.set_return_reason("subframe, last commmited, auto subframe");
avi7c6f35e2015-05-08 17:52:381393 return NAVIGATION_TYPE_AUTO_SUBFRAME;
Nasko Oskovae49e292020-08-13 02:08:511394 }
Lei Zhang96031532019-10-10 19:05:471395
1396 // We ignore subframes created in non-committed pages; we'd appreciate if
1397 // people stopped doing that.
Nasko Oskovae49e292020-08-13 02:08:511398 trace_return.set_return_reason("subframe, no last commmited, ignore");
Lei Zhang96031532019-10-10 19:05:471399 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381400 }
1401
Rakina Zata Amnif6950d552020-11-24 03:26:101402 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1403 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381404 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1405 // create a new page.
1406
1407 // Just like above in the did_create_new_entry case, it's possible to
1408 // scribble onto an uncommitted page. Again, there isn't any navigation
1409 // stuff that we can do, so ignore it here as well.
avi3a5b8f32015-05-28 17:50:231410 NavigationEntry* last_committed = GetLastCommittedEntry();
Nasko Oskovae49e292020-08-13 02:08:511411 if (!last_committed) {
1412 trace_return.set_return_reason("nav entry 0, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381413 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511414 }
avi7c6f35e2015-05-08 17:52:381415
Charles Reis1378111f2017-11-08 21:44:061416 // This is history.replaceState() or history.reload().
Nasko Oskov332593c2018-08-16 17:21:341417 // TODO(nasko): With error page isolation, reloading an existing session
1418 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491419 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341420 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511421 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491422 "nav entry 0, last committed, existing entry");
1423 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381424 }
1425
Rakina Zata Amnif6950d552020-11-24 03:26:101426 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521427 // If the SiteInstance of the |pending_entry_| does not match the
1428 // SiteInstance that got committed, treat this as a new navigation with
1429 // replacement. This can happen if back/forward/reload encounters a server
1430 // redirect to a different site or an isolated error page gets successfully
1431 // reloaded into a different SiteInstance.
1432 if (pending_entry_->site_instance() &&
1433 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491434 trace_return.set_return_reason("pending matching nav entry, new entry");
1435 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521436 }
creis77c9aa32015-09-25 19:59:421437
Nasko Oskovaee2f862018-06-15 00:05:521438 if (pending_entry_index_ == -1) {
1439 // In this case, we have a pending entry for a load of a new URL but Blink
1440 // didn't do a new navigation (params.did_create_new_entry). First check
1441 // to make sure Blink didn't treat a new cross-process navigation as
1442 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161443 // we must treat it as NEW rather than the converted reload case below,
1444 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521445 if (!GetLastCommittedEntry() ||
1446 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161447 trace_return.set_return_reason("new pending, new entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491448 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521449 }
1450
1451 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161452 // We will create a pending entry, but NavigateWithoutEntry will convert
1453 // it to a reload since it's the same page and not create a new entry for
1454 // it. (The user doesn't want to have a new back/forward entry when they
1455 // do this.) Therefore we want to just ignore the pending entry and go
1456 // back to where we were (the "existing entry").
1457 trace_return.set_return_reason("new pending, existing (same) entry");
1458 return NAVIGATION_TYPE_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521459 }
avi7c6f35e2015-05-08 17:52:381460 }
1461
creis26d22632017-04-21 20:23:561462 // Everything below here is assumed to be an existing entry, but if there is
1463 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511464 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491465 trace_return.set_return_reason("no last committed, new entry");
1466 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511467 }
creis26d22632017-04-21 20:23:561468
avi7c6f35e2015-05-08 17:52:381469 if (params.intended_as_new_entry) {
1470 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491471 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1472 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161473 trace_return.set_return_reason("intended as new entry, existing entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491474 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381475 }
1476
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121477 if (navigation_request->DidEncounterError() &&
1478 failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101479 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381480 // If the renderer was going to a new pending entry that got cleared because
1481 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491482 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381483 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511484 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491485 "unreachable, matching pending, existing entry");
1486 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381487 }
1488
Charlie Reisc0f17d2d2021-01-12 18:52:491489 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101490 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511491 trace_return.traced_value()->SetInteger("existing_entry_index",
1492 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381493 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441494 // The renderer has committed a navigation to an entry that no longer
1495 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491496 trace_return.set_return_reason("existing entry -1, new entry");
1497 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381498 }
1499
avi7c6f35e2015-05-08 17:52:381500 // Since we weeded out "new" navigations above, we know this is an existing
1501 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491502 trace_return.set_return_reason("default return, existing entry");
1503 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381504}
1505
Charlie Reisc0f17d2d2021-01-12 18:52:491506void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321507 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071508 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361509 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441510 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471511 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451512 NavigationRequest* request) {
dcheng9bfa5162016-04-09 01:00:571513 std::unique_ptr<NavigationEntryImpl> new_entry;
creisf49dfc92016-07-26 17:05:181514 bool update_virtual_url = false;
1515
Anton Bikineevf62d1bf2021-05-15 17:56:071516 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451517 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061518
creisf49dfc92016-07-26 17:05:181519 // First check if this is an in-page navigation. If so, clone the current
1520 // entry instead of looking at the pending entry, because the pending entry
1521 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361522 if (is_same_document && GetLastCommittedEntry()) {
Patrick Monette50e8bd82019-06-13 22:40:451523 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481524 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081525 params.document_sequence_number, params.app_history_key,
1526 rfh->GetSiteInstance(), nullptr, params.url,
1527 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091528 Referrer(*params.referrer), initiator_origin,
1529 request->GetRedirectChain(), params.page_state, params.method,
1530 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391531 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481532 request->GetSubresourceWebBundleNavigationInfo(),
Antonio Sartori78a749f2020-11-30 12:03:391533 // We will set the document policies later in this function.
Titouan Rigoudy6ec70402021-02-02 15:42:191534 nullptr /* policy_container_policies */);
Charles Reisf44482022017-10-13 21:15:031535
creisf49dfc92016-07-26 17:05:181536 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Carlos Caballero40b0efd2021-01-26 11:55:001537 frame_entry, true, rfh->frame_tree_node(), frame_tree_.root());
jama78746e2017-02-22 17:21:571538 if (new_entry->GetURL().GetOrigin() != params.url.GetOrigin()) {
1539 // TODO(jam): we had one report of this with a URL that was redirecting to
1540 // only tildes. Until we understand that better, don't copy the cert in
1541 // this case.
1542 new_entry->GetSSL() = SSLStatus();
John Abd-El-Malek9cd697e2017-07-12 21:53:141543
John Abd-El-Malek380d3c5922017-09-08 00:20:311544 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121545 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141546 UMA_HISTOGRAM_BOOLEAN(
1547 "Navigation.SecureSchemeHasSSLStatus.NewPageInPageOriginMismatch",
1548 !!new_entry->GetSSL().certificate);
1549 }
jama78746e2017-02-22 17:21:571550 }
creisf49dfc92016-07-26 17:05:181551
Patrick Monette50e8bd82019-06-13 22:40:451552 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1553 // that |frame_entry| should now have a reference count of exactly 2: one
1554 // due to the local variable |frame_entry|, and another due to |new_entry|
1555 // also retaining one. This should never fail, because it's the main frame.
1556 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181557
1558 update_virtual_url = new_entry->update_virtual_url_with_url();
John Abd-El-Malek9cd697e2017-07-12 21:53:141559
John Abd-El-Malek380d3c5922017-09-08 00:20:311560 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121561 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141562 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus.NewPageInPage",
1563 !!new_entry->GetSSL().certificate);
1564 }
creisf49dfc92016-07-26 17:05:181565 }
1566
Harkiran Bolaria59290d62021-03-17 01:53:011567 // If this is an activation navigation from a prerendered page, transfer the
1568 // new entry from an entry already created and stored in the
1569 // NavigationRequest. |new_entry| will not have been set prior to this as
1570 // |is_same_document| is mutually exclusive with
1571 // |IsPrerenderedPageActivation|.
1572 if (request->IsPrerenderedPageActivation()) {
1573 DCHECK(!is_same_document);
1574 DCHECK(!new_entry);
1575 new_entry = request->TakePrerenderNavigationEntry();
1576 DCHECK(new_entry);
1577 }
1578
Charlie Reisc0f17d2d2021-01-12 18:52:491579 // Only make a copy of the pending entry if it is appropriate for the new
1580 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451581 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041582 // 1. The SiteInstance hasn't been assigned to something else.
1583 // 2. The pending entry was intended as a new entry, rather than being a
1584 // history navigation that was interrupted by an unrelated,
1585 // renderer-initiated navigation.
1586 // TODO(csharrison): Investigate whether we can remove some of the coarser
1587 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451588 if (!new_entry && PendingEntryMatchesRequest(request) &&
1589 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341590 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431591 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351592 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431593
[email protected]f1eb87a2011-05-06 17:49:411594 update_virtual_url = new_entry->update_virtual_url_with_url();
Camille Lamy62b826012019-02-26 09:15:471595 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451596 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141597
John Abd-El-Malek380d3c5922017-09-08 00:20:311598 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121599 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141600 UMA_HISTOGRAM_BOOLEAN(
1601 "Navigation.SecureSchemeHasSSLStatus.NewPagePendingEntryMatches",
1602 !!new_entry->GetSSL().certificate);
1603 }
creisf49dfc92016-07-26 17:05:181604 }
1605
Charlie Reisc0f17d2d2021-01-12 18:52:491606 // For cross-document commits with no matching pending entry, create a new
1607 // entry.
creisf49dfc92016-07-26 17:05:181608 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061609 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071610 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1611 initiator_origin,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:581612 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451613 params.transition, request->IsRendererInitiated(),
Lukasz Anforowicz435bcb582019-07-12 20:50:061614 nullptr); // blob_url_loader_factory
[email protected]f8f93eb2012-09-25 03:06:241615
1616 // Find out whether the new entry needs to update its virtual URL on URL
1617 // change and set up the entry accordingly. This is needed to correctly
1618 // update the virtual URL when replaceState is called after a pushState.
1619 GURL url = params.url;
1620 bool needs_update = false;
[email protected]47752982014-07-29 08:01:431621 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1622 &url, browser_context_, &needs_update);
[email protected]f8f93eb2012-09-25 03:06:241623 new_entry->set_update_virtual_url_with_url(needs_update);
1624
Charlie Reisc0f17d2d2021-01-12 18:52:491625 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411626 // update the virtual URL based on the new URL. For example, this is needed
1627 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1628 // the URL.
[email protected]f8f93eb2012-09-25 03:06:241629 update_virtual_url = needs_update;
Camille Lamy62b826012019-02-26 09:15:471630 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451631 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141632
John Abd-El-Malek380d3c5922017-09-08 00:20:311633 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121634 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141635 UMA_HISTOGRAM_BOOLEAN(
1636 "Navigation.SecureSchemeHasSSLStatus.NewPageNoMatchingEntry",
1637 !!new_entry->GetSSL().certificate);
1638 }
[email protected]e9ba4472008-09-14 15:42:431639 }
1640
Harkiran Bolaria59290d62021-03-17 01:53:011641 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
1642 // for prerendered contents, if any. This is because prerendering/activation
1643 // technically won't be creating a new document. Unlike BFCache, prerendering
1644 // creates a new NavigationEntry rather than using an existing one.
1645 if (!request->IsPrerenderedPageActivation()) {
1646 // Don't use the page type from the pending entry. Some interstitial page
1647 // may have set the type to interstitial. Once we commit, however, the page
1648 // type must always be normal or error.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121649 new_entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1650 : PAGE_TYPE_NORMAL);
Harkiran Bolaria59290d62021-03-17 01:53:011651 new_entry->SetURL(params.url);
1652 if (update_virtual_url)
1653 UpdateVirtualURLToURL(new_entry.get(), params.url);
1654 new_entry->SetReferrer(Referrer(*params.referrer));
1655 new_entry->SetTransitionType(params.transition);
1656 new_entry->set_site_instance(
1657 static_cast<SiteInstanceImpl*>(rfh->GetSiteInstance()));
1658 new_entry->SetOriginalRequestURL(request->GetOriginalRequestURL());
Rakina Zata Amnib597d632020-12-01 00:56:001659
Antonio Sartori4f5373792021-05-31 10:56:471660 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
Harkiran Bolaria59290d62021-03-17 01:53:011661 new_entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
[email protected]e9ba4472008-09-14 15:42:431662
Harkiran Bolaria59290d62021-03-17 01:53:011663 // Update the FrameNavigationEntry for new main frame commits.
1664 FrameNavigationEntry* frame_entry =
1665 new_entry->GetFrameEntry(rfh->frame_tree_node());
1666 frame_entry->set_frame_unique_name(rfh->frame_tree_node()->unique_name());
1667 frame_entry->set_item_sequence_number(params.item_sequence_number);
1668 frame_entry->set_document_sequence_number(params.document_sequence_number);
Nate Chapinfbfe5af2021-06-10 17:22:081669 frame_entry->set_app_history_key(params.app_history_key);
Harkiran Bolaria59290d62021-03-17 01:53:011670 frame_entry->set_redirect_chain(request->GetRedirectChain());
1671 frame_entry->SetPageState(params.page_state);
1672 frame_entry->set_method(params.method);
1673 frame_entry->set_post_id(params.post_id);
1674 frame_entry->set_policy_container_policies(
1675 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1676 request));
Antonio Sartori78a749f2020-11-30 12:03:391677
Anton Bikineevf62d1bf2021-05-15 17:56:071678 if (absl::optional<url::Origin> committed_origin =
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121679 GetCommittedOriginForFrameEntry(params, request)) {
1680 if (committed_origin.has_value())
1681 frame_entry->set_committed_origin(committed_origin.value());
1682 }
Harkiran Bolaria59290d62021-03-17 01:53:011683 if (request->web_bundle_navigation_info()) {
1684 frame_entry->set_web_bundle_navigation_info(
1685 request->web_bundle_navigation_info()->Clone());
1686 }
creis8b5cd4c2015-06-19 00:11:081687
Harkiran Bolaria59290d62021-03-17 01:53:011688 // history.pushState() is classified as a navigation to a new page, but sets
1689 // is_same_document to true. In this case, we already have the title and
1690 // favicon available, so set them immediately.
1691 if (is_same_document && GetLastCommittedEntry()) {
1692 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
1693 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1694 }
[email protected]3a868f212014-08-09 10:41:191695 }
[email protected]ff64b3e2014-05-31 04:07:331696
[email protected]60d6cca2013-04-30 08:47:131697 DCHECK(!params.history_list_was_cleared || !replace_entry);
1698 // The browser requested to clear the session history when it initiated the
1699 // navigation. Now we know that the renderer has updated its state accordingly
1700 // and it is safe to also clear the browser side history.
1701 if (params.history_list_was_cleared) {
arthursonzogni69a6a1b2019-09-17 09:23:001702 DiscardNonCommittedEntries();
[email protected]60d6cca2013-04-30 08:47:131703 entries_.clear();
1704 last_committed_entry_index_ = -1;
1705 }
1706
Nasko Oskovaee2f862018-06-15 00:05:521707 // If this is a new navigation with replacement and there is a
1708 // pending_entry_ which matches the navigation reported by the renderer
1709 // process, then it should be the one replaced, so update the
1710 // last_committed_entry_index_ to use it.
1711 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101712 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521713 last_committed_entry_index_ = pending_entry_index_;
1714 }
1715
Alexander Timine3ec4192020-04-20 16:39:401716 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411717 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401718 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051719
Carlos IL42b416592019-10-07 23:10:361720 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
1721 !request->post_commit_error_page_html().empty());
[email protected]e9ba4472008-09-14 15:42:431722}
1723
Charlie Reisc0f17d2d2021-01-12 18:52:491724void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321725 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071726 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361727 bool is_same_document,
jam48cea9082017-02-15 06:13:291728 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451729 NavigationRequest* request,
Peter Boströmd7592132019-01-30 04:50:311730 bool keep_pending_entry) {
creis26d22632017-04-21 20:23:561731 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1732 << "that a last committed entry exists.";
1733
[email protected]e9ba4472008-09-14 15:42:431734 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001735 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431736
Charlie Reis7e2cb6d2021-01-26 01:27:161737 NavigationEntryImpl* entry = nullptr;
avicbdc4c12015-07-01 16:07:111738 if (params.intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:161739 // We're guaranteed to have a last committed entry if intended_as_new_entry
1740 // is true.
avicbdc4c12015-07-01 16:07:111741 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:161742 DCHECK(entry);
1743
1744 // If the NavigationRequest matches a new pending entry and is classified as
1745 // EXISTING_ENTRY, then it is a navigation to the same URL that was
1746 // converted to a reload, such as a user pressing enter in the omnibox.
1747 if (pending_entry_ && pending_entry_index_ == -1 &&
1748 pending_entry_->GetUniqueID() ==
1749 request->commit_params().nav_entry_id) {
1750 // Note: The pending entry will usually have a real ReloadType here, but
1751 // it can still be ReloadType::NONE in cases that
1752 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
1753
1754 // If we classified this correctly, the SiteInstance should not have
1755 // changed.
1756 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
1757
1758 // For converted reloads, we assign the entry's unique ID to be that of
1759 // the new one. Since this is always the result of a user action, we want
1760 // to dismiss infobars, etc. like a regular user-initiated navigation.
1761 entry->set_unique_id(pending_entry_->GetUniqueID());
1762
1763 // The extra headers may have changed due to reloading with different
1764 // headers.
1765 entry->set_extra_headers(pending_entry_->extra_headers());
1766 }
1767 // Otherwise, this was intended as a new entry but the pending entry was
1768 // lost in the meantime and no new entry was created. We are stuck at the
1769 // last committed entry.
1770
1771 // Even if this is a converted reload from pressing enter in the omnibox,
1772 // the server could redirect, requiring an update to the SSL status. If this
1773 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451774 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:161775 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:471776 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451777 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:161778 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141779
Charlie Reis7e2cb6d2021-01-26 01:27:161780 if (params.url.SchemeIs(url::kHttpsScheme) &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121781 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141782 bool has_cert = !!entry->GetSSL().certificate;
1783 if (is_same_document) {
1784 UMA_HISTOGRAM_BOOLEAN(
1785 "Navigation.SecureSchemeHasSSLStatus."
1786 "ExistingPageSameDocumentIntendedAsNew",
1787 has_cert);
1788 } else {
1789 UMA_HISTOGRAM_BOOLEAN(
1790 "Navigation.SecureSchemeHasSSLStatus."
1791 "ExistingPageDifferentDocumentIntendedAsNew",
1792 has_cert);
1793 }
1794 }
Rakina Zata Amnif6950d552020-11-24 03:26:101795 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:111796 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:101797 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b902016-09-01 16:58:161798
eugenebut604866f2017-05-10 21:35:361799 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451800 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361801 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1802 // this was a restored same document navigation entry, then it won't have
1803 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1804 // navigated it.
jam48cea9082017-02-15 06:13:291805 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
1806 if (entry->GetURL().GetOrigin() == last_entry->GetURL().GetOrigin() &&
1807 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1808 was_restored) {
1809 entry->GetSSL() = last_entry->GetSSL();
1810 }
1811 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451812 // In rapid back/forward navigations |request| sometimes won't have a cert
1813 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191814 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451815 if (request->GetSSLInfo().has_value() &&
1816 request->GetSSLInfo()->is_valid()) {
1817 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
1818 } else if (entry->GetURL().GetOrigin() != request->GetURL().GetOrigin()) {
John Abd-El-Malek3f247082017-12-07 19:02:191819 entry->GetSSL() = SSLStatus();
1820 }
jam48cea9082017-02-15 06:13:291821 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141822
John Abd-El-Malek380d3c5922017-09-08 00:20:311823 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121824 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141825 bool has_cert = !!entry->GetSSL().certificate;
1826 if (is_same_document && was_restored) {
1827 UMA_HISTOGRAM_BOOLEAN(
1828 "Navigation.SecureSchemeHasSSLStatus."
1829 "ExistingPageSameDocumentRestoredBrowserInitiated",
1830 has_cert);
1831 } else if (is_same_document && !was_restored) {
1832 UMA_HISTOGRAM_BOOLEAN(
1833 "Navigation.SecureSchemeHasSSLStatus."
1834 "ExistingPageSameDocumentBrowserInitiated",
1835 has_cert);
1836 } else if (!is_same_document && was_restored) {
1837 UMA_HISTOGRAM_BOOLEAN(
1838 "Navigation.SecureSchemeHasSSLStatus."
1839 "ExistingPageRestoredBrowserInitiated",
1840 has_cert);
1841 } else {
1842 UMA_HISTOGRAM_BOOLEAN(
1843 "Navigation.SecureSchemeHasSSLStatus.ExistingPageBrowserInitiated",
1844 has_cert);
1845 }
1846 }
avicbdc4c12015-07-01 16:07:111847 } else {
1848 // This is renderer-initiated. The only kinds of renderer-initated
Charlie Reisc0f17d2d2021-01-12 18:52:491849 // navigations that are EXISTING_ENTRY are reloads and history.replaceState,
avicbdc4c12015-07-01 16:07:111850 // which land us at the last committed entry.
1851 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101852
Mikel Astizba9cf2fd2017-12-17 10:38:101853 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1854 // to avoid misleading interpretations (e.g. URLs paired with
1855 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1856 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1857 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1858
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571859 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:101860
eugenebut604866f2017-05-10 21:35:361861 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451862 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361863 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471864 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451865 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141866
John Abd-El-Malek380d3c5922017-09-08 00:20:311867 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121868 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141869 bool has_cert = !!entry->GetSSL().certificate;
1870 if (is_same_document) {
1871 UMA_HISTOGRAM_BOOLEAN(
1872 "Navigation.SecureSchemeHasSSLStatus."
1873 "ExistingPageSameDocumentRendererInitiated",
1874 has_cert);
1875 } else {
1876 UMA_HISTOGRAM_BOOLEAN(
1877 "Navigation.SecureSchemeHasSSLStatus."
1878 "ExistingPageDifferentDocumentRendererInitiated",
1879 has_cert);
1880 }
1881 }
avicbdc4c12015-07-01 16:07:111882 }
1883 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:431884
[email protected]5ccd4dc2012-10-24 02:28:141885 // The URL may have changed due to redirects.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121886 entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1887 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041888 entry->SetURL(params.url);
arthursonzogni73fe3212020-11-17 13:24:071889 entry->SetReferrer(Referrer(*params.referrer));
[email protected]38178a42009-12-17 18:58:321890 if (entry->update_virtual_url_with_url())
1891 UpdateVirtualURLToURL(entry, params.url);
[email protected]5ccd4dc2012-10-24 02:28:141892
jam015ba062017-01-06 21:17:001893 // The site instance will normally be the same except
1894 // 1) session restore, when no site instance will be assigned or
1895 // 2) redirect, when the site instance is reset.
Lei Zhang96031532019-10-10 19:05:471896 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
naskoaf182192016-08-11 02:12:011897 entry->site_instance() == rfh->GetSiteInstance());
clamy432acb22016-04-15 19:41:431898
naskoaf182192016-08-11 02:12:011899 // Update the existing FrameNavigationEntry to ensure all of its members
1900 // reflect the parameters coming from the renderer process.
Anton Bikineevf62d1bf2021-05-15 17:56:071901 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451902 request->common_params().initiator_origin;
naskoaf182192016-08-11 02:12:011903 entry->AddOrUpdateFrameEntry(
Nate Chapin9f169072021-06-09 19:32:371904 rfh->frame_tree_node(), NavigationEntryImpl::UpdatePolicy::kUpdate,
1905 params.item_sequence_number, params.document_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081906 params.app_history_key, rfh->GetSiteInstance(), nullptr, params.url,
Nate Chapin9f169072021-06-09 19:32:371907 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091908 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071909 params.page_state, params.method, params.post_id,
1910 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031911 request->web_bundle_navigation_info()
1912 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391913 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481914 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191915 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1916 request));
creis22a7b4c2016-04-28 07:20:301917
[email protected]5ccd4dc2012-10-24 02:28:141918 // The redirected to page should not inherit the favicon from the previous
1919 // page.
eugenebut604866f2017-05-10 21:35:361920 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:481921 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:141922
Peter Boströmd7592132019-01-30 04:50:311923 // We should also usually discard the pending entry if it corresponds to a
1924 // different navigation, since that one is now likely canceled. In rare
1925 // cases, we leave the pending entry for another navigation in place when we
1926 // know it is still ongoing, to avoid a flicker in the omnibox (see
1927 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:431928 //
1929 // Note that we need to use the "internal" version since we don't want to
1930 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:311931 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001932 DiscardNonCommittedEntries();
[email protected]40bcc302009-03-02 20:50:391933
Carlos IL4dea8902020-05-26 15:14:291934 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:111935 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:431936}
1937
[email protected]d202a7c2012-01-04 07:53:471938void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:321939 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071940 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361941 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:471942 bool replace_entry,
1943 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451944 NavigationRequest* request) {
avi25f5f9e2015-07-17 20:08:261945 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1946 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
[email protected]09b8f82f2009-06-16 20:22:111947
[email protected]e9ba4472008-09-14 15:42:431948 // Manual subframe navigations just get the current entry cloned so the user
1949 // can go back or forward to it. The actual subframe information will be
1950 // stored in the page state for each of those entries. This happens out of
1951 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:091952 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1953 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:381954
Mikel Astizba9cf2fd2017-12-17 10:38:101955 // The DCHECK below documents the fact that we don't know of any situation
1956 // where |replace_entry| is true for subframe navigations. This simplifies
1957 // reasoning about the replacement struct for subframes (see
1958 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
1959 DCHECK(!replace_entry);
1960
Patrick Monette50e8bd82019-06-13 22:40:451961 // This FrameNavigationEntry might not end up being used in the
1962 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Anton Bikineevf62d1bf2021-05-15 17:56:071963 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451964 request->common_params().initiator_origin;
Patrick Monette50e8bd82019-06-13 22:40:451965 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481966 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081967 params.document_sequence_number, params.app_history_key,
1968 rfh->GetSiteInstance(), nullptr, params.url,
1969 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091970 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071971 params.page_state, params.method, params.post_id,
1972 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031973 request->web_bundle_navigation_info()
1974 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391975 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481976 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191977 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1978 request));
Charles Reisf44482022017-10-13 21:15:031979
creisce0ef3572017-01-26 17:53:081980 std::unique_ptr<NavigationEntryImpl> new_entry =
1981 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:451982 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:001983 frame_tree_.root());
creise062d542015-08-25 02:01:551984
Alexander Timine3ec4192020-04-20 16:39:401985 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411986 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401987 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:471988
creisce0ef3572017-01-26 17:53:081989 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:451990 // to replace, which can happen due to a unique name change. See
1991 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
1992 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:381993
Carlos IL42b416592019-10-07 23:10:361994 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false);
[email protected]e9ba4472008-09-14 15:42:431995}
1996
[email protected]d202a7c2012-01-04 07:53:471997bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:321998 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071999 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392000 bool is_same_document,
Nate Chapinc7019dd7d2021-06-25 18:29:252001 bool was_on_initial_empty_document,
Mohamed Abdelhalim833de902019-09-16 17:41:452002 NavigationRequest* request) {
avi9f07a0c2015-02-18 22:51:292003 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2004 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2005
[email protected]e9ba4472008-09-14 15:42:432006 // We're guaranteed to have a previously committed entry, and we now need to
2007 // handle navigation inside of a subframe in it without creating a new entry.
2008 DCHECK(GetLastCommittedEntry());
2009
creis913c63ce2016-07-16 19:52:522010 // For newly created subframes, we don't need to send a commit notification.
2011 // This is only necessary for history navigations in subframes.
2012 bool send_commit_notification = false;
2013
Rakina Zata Amnif6950d552020-11-24 03:26:102014 // If |nav_entry_id| is non-zero and matches an existing entry, this
2015 // is a history navigation. Update the last committed index accordingly. If
2016 // we don't recognize the |nav_entry_id|, it might be a recently
2017 // pruned entry. We'll handle it below.
2018 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2019 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472020 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062021 // Make sure that a subframe commit isn't changing the main frame's
2022 // origin. Otherwise the renderer process may be confused, leading to a
2023 // URL spoof. We can't check the path since that may change
2024 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572025 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2026 // about:blank, file, and unique origins are more subtle to get right.
Charlie Reis95fbca442021-05-21 21:38:242027 // We should use checks similar to RenderFrameHostImpl's
2028 // CanCommitUrlAndOrigin on the main frame during subframe commits.
2029 // See https://crbug.com/1209092.
creis37988b92016-06-10 18:03:572030 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2031 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2032 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2033 dest_top_url.SchemeIsHTTPOrHTTPS() &&
2034 current_top_url.GetOrigin() != dest_top_url.GetOrigin()) {
creisfb6eeb62016-05-10 19:01:512035 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2036 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062037 }
creis3cdc3b02015-05-29 23:00:472038
creis913c63ce2016-07-16 19:52:522039 // We only need to discard the pending entry in this history navigation
2040 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062041 last_committed_entry_index_ = entry_index;
arthursonzogni69a6a1b2019-09-17 09:23:002042 DiscardNonCommittedEntries();
creis913c63ce2016-07-16 19:52:522043
2044 // History navigations should send a commit notification.
2045 send_commit_notification = true;
avi98405c22015-05-21 20:47:062046 }
[email protected]e9ba4472008-09-14 15:42:432047 }
[email protected]f233e4232013-02-23 00:55:142048
creisce0ef3572017-01-26 17:53:082049 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2050 // it may be a "history auto" case where we update an existing one.
2051 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Nate Chapin9f169072021-06-09 19:32:372052
2053 // We may want to update |last_committed|'s FrameNavigationEntry (if one
2054 // exists), or we may want to clobber it and create a new one. We update in
2055 // cases where the corresponding FrameNavigationEntry is conceptually similar
2056 // to the document described by the commit params: same-document
2057 // navigations, history traversal to an existing entry, and reloads (including
2058 // a "soft reload" where we navigate to the same url without flagging it as a
2059 // reload). But in the case of a different document that is not logically
2060 // related to the committed FrameNavigationEntry's document (cross-document,
2061 // not same url, not a reload, not a history traversal), we replace rather
2062 // than update.
Nate Chapinc7019dd7d2021-06-25 18:29:252063 //
Nate Chapin9f169072021-06-09 19:32:372064 // In the case where we update, the FrameNavigationEntry will potentially be
2065 // shared across multiple NavigationEntries, and any updates will be reflected
2066 // in all of those NavigationEntries. In the replace case, any existing
2067 // sharing with other NavigationEntries will stop.
Nate Chapinc7019dd7d2021-06-25 18:29:252068 //
2069 // When navigating away from the initial empty document, we also update rather
2070 // than replace. Either update or replace will overwrite the initial empty
2071 // document state for |last_committed|, but if the FrameNavigationEntry for
2072 // the initial empty document is shared across multiple NavigationEntries (due
2073 // to a navigation in another frame), we want to make sure we overwrite the
2074 // initial empty document state everywhere this FrameNavigationEntry is used,
2075 // which is accompished by updating the existing FrameNavigationEntry.
Nate Chapin9f169072021-06-09 19:32:372076 FrameNavigationEntry* last_committed_frame_entry =
2077 last_committed->GetFrameEntry(rfh->frame_tree_node());
2078 NavigationEntryImpl::UpdatePolicy update_policy =
2079 NavigationEntryImpl::UpdatePolicy::kUpdate;
2080 if (request->common_params().navigation_type ==
Anatoliy Potapchuk27278cce22021-06-30 09:45:312081 mojom::NavigationType::DIFFERENT_DOCUMENT &&
Nate Chapin9f169072021-06-09 19:32:372082 last_committed_frame_entry &&
Nate Chapinc7019dd7d2021-06-25 18:29:252083 last_committed_frame_entry->url() != params.url &&
2084 !was_on_initial_empty_document) {
Nate Chapin9f169072021-06-09 19:32:372085 update_policy = NavigationEntryImpl::UpdatePolicy::kReplace;
2086 }
2087
Anton Bikineevf62d1bf2021-05-15 17:56:072088 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452089 request->common_params().initiator_origin;
creisce0ef3572017-01-26 17:53:082090 last_committed->AddOrUpdateFrameEntry(
Nate Chapin9f169072021-06-09 19:32:372091 rfh->frame_tree_node(), update_policy, params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:082092 params.document_sequence_number, params.app_history_key,
2093 rfh->GetSiteInstance(), nullptr, params.url,
2094 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:092095 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072096 params.page_state, params.method, params.post_id,
2097 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032098 request->web_bundle_navigation_info()
2099 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392100 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482101 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:192102 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
2103 request));
creis625a0c7d2015-03-24 23:17:122104
creis913c63ce2016-07-16 19:52:522105 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432106}
2107
[email protected]d202a7c2012-01-04 07:53:472108int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232109 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032110 for (size_t i = 0; i < entries_.size(); ++i) {
2111 if (entries_[i].get() == entry)
2112 return i;
2113 }
2114 return -1;
[email protected]765b35502008-08-21 00:51:202115}
2116
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572117void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242118 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572119 NavigationControllerImpl* source =
2120 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572121 // Verify that we look new.
Lei Zhang96031532019-10-10 19:05:472122 DCHECK_EQ(0, GetEntryCount());
2123 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:572124
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572125 if (source->GetEntryCount() == 0)
[email protected]ce3fa3c2009-04-20 19:55:572126 return; // Nothing new to do.
2127
Francois Dorayeaace782017-06-21 16:37:242128 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442129 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572130 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572131
Fergal Dalya1d569972021-03-16 03:24:532132 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302133 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532134 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2135 session_storage_namespace_map_[it.first] = source_namespace->Clone();
Aaron Colwellb731a0ae2021-03-19 19:14:472136 OnStoragePartitionIdAdded(it.first);
[email protected]fdac6ade2013-07-20 01:06:302137 }
[email protected]4e6419c2010-01-15 04:50:342138
Lukasz Anforowicz0de0f452020-12-02 19:57:152139 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572140}
2141
Aran Gilman37d11632019-10-08 23:07:152142void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2143 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162144 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012145 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162146
[email protected]d202a7c2012-01-04 07:53:472147 NavigationControllerImpl* source =
2148 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252149
avi2b177592014-12-10 02:08:022150 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012151 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252152
[email protected]474f8512013-05-31 22:31:162153 // We now have one entry, possibly with a new pending entry. Ensure that
2154 // adding the entries from source won't put us over the limit.
2155 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572156 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572157 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252158
Carlos IL4dea8902020-05-26 15:14:292159 // Insert the entries from source. Ignore any pending entry, since it has not
2160 // committed in source.
[email protected]474f8512013-05-31 22:31:162161 int max_source_index = source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:252162 if (max_source_index == -1)
[email protected]a26023822011-12-29 00:23:552163 max_source_index = source->GetEntryCount();
[email protected]e1cd5452010-08-26 18:03:252164 else
2165 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572166
2167 // Ignore the source's current entry if merging with replacement.
2168 // TODO(davidben): This should preserve entries forward of the current
2169 // too. http://crbug.com/317872
2170 if (replace_entry && max_source_index > 0)
2171 max_source_index--;
2172
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572173 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252174
2175 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552176 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142177
Hayato Ito2c8c08d02021-06-23 03:38:432178 BroadcastHistoryOffsetAndLength();
[email protected]e1cd5452010-08-26 18:03:252179}
2180
[email protected]79368982013-11-13 01:11:012181bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162182 // If there is no last committed entry, we cannot prune. Even if there is a
2183 // pending entry, it may not commit, leaving this WebContents blank, despite
2184 // possibly giving it new entries via CopyStateFromAndPrune.
2185 if (last_committed_entry_index_ == -1)
2186 return false;
[email protected]9350602e2013-02-26 23:27:442187
[email protected]474f8512013-05-31 22:31:162188 // We cannot prune if there is a pending entry at an existing entry index.
2189 // It may not commit, so we have to keep the last committed entry, and thus
2190 // there is no sensible place to keep the pending entry. It is ok to have
2191 // a new pending entry, which can optionally commit as a new navigation.
2192 if (pending_entry_index_ != -1)
2193 return false;
2194
[email protected]474f8512013-05-31 22:31:162195 return true;
2196}
2197
[email protected]79368982013-11-13 01:11:012198void NavigationControllerImpl::PruneAllButLastCommitted() {
2199 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162200
avi2b177592014-12-10 02:08:022201 DCHECK_EQ(0, last_committed_entry_index_);
2202 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442203
Hayato Ito2c8c08d02021-06-23 03:38:432204 BroadcastHistoryOffsetAndLength();
[email protected]9350602e2013-02-26 23:27:442205}
2206
[email protected]79368982013-11-13 01:11:012207void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162208 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012209 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262210
[email protected]474f8512013-05-31 22:31:162211 // Erase all entries but the last committed entry. There may still be a
2212 // new pending entry after this.
2213 entries_.erase(entries_.begin(),
2214 entries_.begin() + last_committed_entry_index_);
2215 entries_.erase(entries_.begin() + 1, entries_.end());
2216 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262217}
2218
Christian Dullweber1af31e62018-02-22 11:49:482219void NavigationControllerImpl::DeleteNavigationEntries(
2220 const DeletionPredicate& deletionPredicate) {
2221 // It is up to callers to check the invariants before calling this.
2222 CHECK(CanPruneAllButLastCommitted());
2223 std::vector<int> delete_indices;
2224 for (size_t i = 0; i < entries_.size(); i++) {
2225 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572226 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482227 delete_indices.push_back(i);
2228 }
2229 }
2230 if (delete_indices.empty())
2231 return;
2232
2233 if (delete_indices.size() == GetEntryCount() - 1U) {
2234 PruneAllButLastCommitted();
2235 } else {
2236 // Do the deletion in reverse to preserve indices.
2237 for (auto it = delete_indices.rbegin(); it != delete_indices.rend(); ++it) {
2238 RemoveEntryAtIndex(*it);
2239 }
Hayato Ito2c8c08d02021-06-23 03:38:432240 BroadcastHistoryOffsetAndLength();
Christian Dullweber1af31e62018-02-22 11:49:482241 }
2242 delegate()->NotifyNavigationEntriesDeleted();
2243}
2244
Shivani Sharma883f5f32019-02-12 18:20:012245bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2246 auto* entry = GetEntryAtIndex(index);
2247 return entry && entry->should_skip_on_back_forward_ui();
2248}
2249
Carlos Caballero35ce710c2019-09-19 10:59:452250BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2251 return back_forward_cache_;
2252}
2253
clamy987a3752018-05-03 17:36:262254void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2255 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2256 // progress, since this will cause a use-after-free. (We only allow this
2257 // when the tab is being destroyed for shutdown, since it won't return to
2258 // NavigateToEntry in that case.) http://crbug.com/347742.
2259 CHECK(!in_navigate_to_pending_entry_ || delegate_->IsBeingDestroyed());
2260
2261 if (was_failure && pending_entry_) {
2262 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2263 } else {
2264 failed_pending_entry_id_ = 0;
2265 }
2266
2267 if (pending_entry_) {
2268 if (pending_entry_index_ == -1)
2269 delete pending_entry_;
2270 pending_entry_index_ = -1;
2271 pending_entry_ = nullptr;
2272 }
arthursonzogni66f711c2019-10-08 14:40:362273
2274 // Ensure any refs to the current pending entry are ignored if they get
2275 // deleted, by clearing the set of known refs. All future pending entries will
2276 // only be affected by new refs.
2277 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262278}
2279
2280void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2281 if (pending_entry_)
2282 pending_entry_->set_ssl_error(error);
2283}
2284
Camille Lamy5193caa2018-10-12 11:59:422285#if defined(OS_ANDROID)
2286// static
2287bool NavigationControllerImpl::ValidateDataURLAsString(
2288 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2289 if (!data_url_as_string)
2290 return false;
2291
2292 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2293 return false;
2294
2295 // The number of characters that is enough for validating a data: URI.
2296 // From the GURL's POV, the only important part here is scheme, it doesn't
2297 // check the actual content. Thus we can take only the prefix of the url, to
2298 // avoid unneeded copying of a potentially long string.
2299 const size_t kDataUriPrefixMaxLen = 64;
2300 GURL data_url(
2301 std::string(data_url_as_string->front_as<char>(),
2302 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2303 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2304 return false;
2305
2306 return true;
2307}
2308#endif
2309
Shivani Sharma194877032019-03-07 17:52:472310void NavigationControllerImpl::NotifyUserActivation() {
2311 // When a user activation occurs, ensure that all adjacent entries for the
2312 // same document clear their skippable bit, so that the history manipulation
2313 // intervention does not apply to them.
Shivani Sharma194877032019-03-07 17:52:472314 auto* last_committed_entry = GetLastCommittedEntry();
2315 if (!last_committed_entry)
2316 return;
Shivani Sharma194877032019-03-07 17:52:472317
shivanigithub99368382021-06-16 18:33:372318 if (base::FeatureList::IsEnabled(
2319 features::kDebugHistoryInterventionNoUserActivation)) {
2320 return;
2321 }
2322
Shivani Sharmac4cc8922019-04-18 03:11:172323 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472324}
2325
clamy987a3752018-05-03 17:36:262326bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2327 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172328 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262329 NavigationEntryImpl* entry =
2330 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2331 if (!entry)
2332 return false;
2333
2334 FrameNavigationEntry* frame_entry =
2335 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2336 if (!frame_entry)
2337 return false;
2338
Camille Lamy5193caa2018-10-12 11:59:422339 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572340 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232341 ReloadType::NONE, false /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422342 true /* is_history_navigation_in_new_child */);
clamyea99ea12018-05-28 13:54:232343
2344 if (!request)
2345 return false;
2346
arthursonzognif046d4a2019-12-12 19:08:102347 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412348
Lukasz Anforowicz9ee83c272020-12-01 20:14:052349 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2350 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232351
2352 return true;
clamy987a3752018-05-03 17:36:262353}
2354
Tsuyoshi Horo52fd08e2020-07-07 07:03:452355bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2356 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2357 if (!entry)
2358 return false;
2359 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2360 if (!frame_entry)
2361 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142362 ReloadType reload_type = ReloadType::NORMAL;
2363 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452364 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142365 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452366 false /* is_same_document_history_load */,
2367 false /* is_history_navigation_in_new_child */);
2368 if (!request)
2369 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052370 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452371 return true;
2372}
2373
Dave Tapuska8bfd84c2019-03-26 20:47:162374void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2375 int offset,
2376 int sandbox_frame_tree_node_id) {
2377 if (!CanGoToOffset(offset))
2378 return;
2379 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id);
2380}
2381
clamy987a3752018-05-03 17:36:262382void NavigationControllerImpl::NavigateFromFrameProxy(
2383 RenderFrameHostImpl* render_frame_host,
2384 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022385 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452386 int initiator_process_id,
Anton Bikineevf62d1bf2021-05-15 17:56:072387 const absl::optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262388 bool is_renderer_initiated,
2389 SiteInstance* source_site_instance,
2390 const Referrer& referrer,
2391 ui::PageTransition page_transition,
2392 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252393 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262394 const std::string& method,
2395 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092396 const std::string& extra_headers,
Antonio Sartori2f763d9d2021-04-21 10:04:142397 network::mojom::SourceLocationPtr source_location,
John Delaney50425f82020-04-07 16:26:212398 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Anton Bikineevf62d1bf2021-05-15 17:56:072399 const absl::optional<blink::Impression>& impression) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582400 if (is_renderer_initiated)
2401 DCHECK(initiator_origin.has_value());
2402
clamy987a3752018-05-03 17:36:262403 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582404
Rakina Zata Amni66992a82020-06-24 03:57:522405 // Don't allow an entry replacement if there is no entry to replace.
2406 // http://crbug.com/457149
2407 if (GetEntryCount() == 0)
2408 should_replace_current_entry = false;
2409
clamy987a3752018-05-03 17:36:262410 // Create a NavigationEntry for the transfer, without making it the pending
2411 // entry. Subframe transfers should have a clone of the last committed entry
2412 // with a FrameNavigationEntry for the target frame. Main frame transfers
2413 // should have a new NavigationEntry.
2414 // TODO(creis): Make this unnecessary by creating (and validating) the params
2415 // directly, passing them to the destination RenderFrameHost. See
2416 // https://crbug.com/536906.
2417 std::unique_ptr<NavigationEntryImpl> entry;
2418 if (!node->IsMainFrame()) {
2419 // Subframe case: create FrameNavigationEntry.
2420 if (GetLastCommittedEntry()) {
2421 entry = GetLastCommittedEntry()->Clone();
2422 entry->set_extra_headers(extra_headers);
2423 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2424 // Renderer-initiated navigation that target a remote frame are currently
2425 // classified as browser-initiated when this one has already navigated.
2426 // See https://crbug.com/722251.
2427 } else {
2428 // If there's no last committed entry, create an entry for about:blank
2429 // with a subframe entry for our destination.
2430 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2431 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:062432 GURL(url::kAboutBlankURL), referrer, initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:102433 source_site_instance, page_transition, is_renderer_initiated,
2434 extra_headers, browser_context_,
Hayato Ito303654c2021-06-30 09:07:542435 nullptr /* blob_url_loader_factory */));
clamy987a3752018-05-03 17:36:262436 }
Nate Chapin9f169072021-06-09 19:32:372437 // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter
2438 // to CreateNavigationRequestFromLoadParams(), so while kReplace might
2439 // remove child FrameNavigationEntries (e.g., if this is a cross-process
2440 // same-document navigation), they will still be present in the
2441 // committed NavigationEntry that will be referenced to construct the new
2442 // FrameNavigationEntry tree when this navigation commits.
clamy987a3752018-05-03 17:36:262443 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:082444 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582445 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072446 absl::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202447 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392448 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482449 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori79d549d2021-02-18 12:59:542450 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262451 } else {
2452 // Main frame case.
2453 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102454 url, referrer, initiator_origin, source_site_instance, page_transition,
2455 is_renderer_initiated, extra_headers, browser_context_,
Hayato Ito303654c2021-06-30 09:07:542456 blob_url_loader_factory));
clamy987a3752018-05-03 17:36:262457 entry->root_node()->frame_entry->set_source_site_instance(
2458 static_cast<SiteInstanceImpl*>(source_site_instance));
2459 entry->root_node()->frame_entry->set_method(method);
2460 }
clamy987a3752018-05-03 17:36:262461
Camille Lamy5193caa2018-10-12 11:59:422462 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262463 if (GetLastCommittedEntry() &&
2464 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2465 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422466 override_user_agent = true;
clamy987a3752018-05-03 17:36:262467 }
2468 // TODO(creis): Set user gesture and intent received timestamp on Android.
2469
2470 // We may not have successfully added the FrameNavigationEntry to |entry|
2471 // above (per https://crbug.com/608402), in which case we create it from
2472 // scratch. This works because we do not depend on |frame_entry| being inside
2473 // |entry| during NavigateToEntry. This will go away when we shortcut this
2474 // further in https://crbug.com/536906.
2475 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2476 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452477 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Nate Chapinfbfe5af2021-06-10 17:22:082478 node->unique_name(), -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582479 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072480 absl::nullopt /* origin */, referrer, initiator_origin,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:122481 std::vector<GURL>(), blink::PageState(), method, -1,
2482 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482483 nullptr /* subresource_web_bundle_navigation_info */,
Titouan Rigoudy6ec70402021-02-02 15:42:192484 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262485 }
2486
Camille Lamy5193caa2018-10-12 11:59:422487 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022488 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452489 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292490 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422491 params.source_site_instance = source_site_instance;
2492 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2493 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032494 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422495 params.referrer = referrer;
2496 /* params.redirect_chain: skip */
2497 params.extra_headers = extra_headers;
2498 params.is_renderer_initiated = is_renderer_initiated;
2499 params.override_user_agent = UA_OVERRIDE_INHERIT;
2500 /* params.base_url_for_data_url: skip */
2501 /* params.virtual_url_for_data_url: skip */
2502 /* params.data_url_as_string: skip */
2503 params.post_data = post_body;
2504 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582505 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422506 /* params.frame_name: skip */
2507 // TODO(clamy): See if user gesture should be propagated to this function.
2508 params.has_user_gesture = false;
2509 params.should_clear_history_list = false;
2510 params.started_from_context_menu = false;
2511 /* params.navigation_ui_data: skip */
2512 /* params.input_start: skip */
Minggang Wangf59db47b2021-06-16 01:56:222513 params.was_activated = blink::mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542514 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212515 params.impression = impression;
Camille Lamy5193caa2018-10-12 11:59:422516
2517 std::unique_ptr<NavigationRequest> request =
2518 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032519 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:142520 false /* has_user_gesture */, std::move(source_location),
Scott Violetcf6ea7e2021-06-09 21:09:212521 download_policy, ReloadType::NONE, entry.get(), frame_entry.get());
clamyea99ea12018-05-28 13:54:232522
2523 if (!request)
2524 return;
2525
Arthur Hemery948742762019-09-18 10:06:242526 // At this stage we are proceeding with this navigation. If this was renderer
2527 // initiated with user gesture, we need to make sure we clear up potential
2528 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2529 DiscardNonCommittedEntries();
2530
Lukasz Anforowicz9ee83c272020-12-01 20:14:052531 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262532}
2533
[email protected]d1198fd2012-08-13 22:50:192534void NavigationControllerImpl::SetSessionStorageNamespace(
Aaron Colwellf3b316e2021-03-11 20:17:052535 const StoragePartitionId& partition_id,
[email protected]8ff00d72012-10-23 19:12:212536 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192537 if (!session_storage_namespace)
2538 return;
2539
2540 // We can't overwrite an existing SessionStorage without violating spec.
2541 // Attempts to do so may give a tab access to another tab's session storage
2542 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152543 bool successful_insert =
2544 session_storage_namespace_map_
Aaron Colwellf3b316e2021-03-11 20:17:052545 .insert(std::make_pair(partition_id,
2546 static_cast<SessionStorageNamespaceImpl*>(
2547 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302548 .second;
2549 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
Aaron Colwellb731a0ae2021-03-19 19:14:472550 OnStoragePartitionIdAdded(partition_id);
[email protected]d1198fd2012-08-13 22:50:192551}
2552
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572553bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Aran Gilman37d11632019-10-08 23:07:152554 return IsInitialNavigation() && !GetLastCommittedEntry() &&
Carlos Caballeroede6f8c2021-01-28 11:01:502555 !frame_tree_.has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462556}
2557
Aran Gilman37d11632019-10-08 23:07:152558SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Aaron Colwell78b4bde2021-03-16 16:16:092559 const SiteInfo& site_info) {
2560 // TODO(acolwell): Remove partition_id logic once we have successfully
2561 // migrated the implementation to be a StoragePartitionConfig. At that point
2562 // |site_info| can be replaced with a StoragePartitionConfig.
2563 const StoragePartitionId partition_id =
2564 site_info.GetStoragePartitionId(browser_context_);
2565 const StoragePartitionConfig partition_config =
2566 site_info.GetStoragePartitionConfig(browser_context_);
[email protected]d1198fd2012-08-13 22:50:192567
[email protected]fdac6ade2013-07-20 01:06:302568 StoragePartition* partition =
Lukasz Anforowiczb9a969a2021-04-29 15:26:252569 browser_context_->GetStoragePartition(partition_config);
michaelnbacbcbd2016-02-09 00:32:032570 DOMStorageContextWrapper* context_wrapper =
2571 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2572
2573 SessionStorageNamespaceMap::const_iterator it =
2574 session_storage_namespace_map_.find(partition_id);
2575 if (it != session_storage_namespace_map_.end()) {
2576 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152577 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2578 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:472579
2580 // Verify that the config we generated now matches the one that
2581 // was generated when the namespace was added to the map.
Aaron Colwellb731a0ae2021-03-19 19:14:472582 if (partition_config != it->first.config()) {
2583 LogStoragePartitionIdCrashKeys(it->first, partition_id);
2584 }
Sharon Yangd4f48792021-06-30 17:02:532585 CHECK_EQ(partition_config, it->first.config());
Aaron Colwellb731a0ae2021-03-19 19:14:472586
michaelnbacbcbd2016-02-09 00:32:032587 return it->second.get();
2588 }
2589
2590 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102591 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2592 SessionStorageNamespaceImpl::Create(context_wrapper);
2593 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2594 session_storage_namespace.get();
2595 session_storage_namespace_map_[partition_id] =
2596 std::move(session_storage_namespace);
Aaron Colwellb731a0ae2021-03-19 19:14:472597 OnStoragePartitionIdAdded(partition_id);
[email protected]fdac6ade2013-07-20 01:06:302598
Daniel Murphy31bbb8b12018-02-07 21:44:102599 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302600}
2601
2602SessionStorageNamespace*
2603NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Aaron Colwell78b4bde2021-03-16 16:16:092604 return GetSessionStorageNamespace(SiteInfo());
[email protected]fdac6ade2013-07-20 01:06:302605}
2606
2607const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572608NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302609 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552610}
[email protected]d202a7c2012-01-04 07:53:472611
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572612bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562613 return needs_reload_;
2614}
[email protected]a26023822011-12-29 00:23:552615
[email protected]46bb5e9c2013-10-03 22:16:472616void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412617 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2618}
2619
2620void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472621 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412622 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542623
2624 if (last_committed_entry_index_ != -1) {
2625 entries_[last_committed_entry_index_]->SetTransitionType(
2626 ui::PAGE_TRANSITION_RELOAD);
2627 }
[email protected]46bb5e9c2013-10-03 22:16:472628}
2629
[email protected]d202a7c2012-01-04 07:53:472630void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572631 DCHECK_LT(index, GetEntryCount());
2632 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312633 DiscardNonCommittedEntries();
2634
2635 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122636 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312637 last_committed_entry_index_--;
2638}
2639
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572640NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272641 // If there is no pending_entry_, there should be no pending_entry_index_.
2642 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2643
2644 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362645 // at that index. An exception is while a reload of a post commit error page
2646 // is ongoing; in that case pending entry will point to the entry replaced
2647 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272648 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362649 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2650 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272651
[email protected]022af742011-12-28 18:37:252652 return pending_entry_;
2653}
2654
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572655int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272656 // The pending entry index must always be less than the number of entries.
2657 // If there are no entries, it must be exactly -1.
2658 DCHECK_LT(pending_entry_index_, GetEntryCount());
2659 DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:552660 return pending_entry_index_;
2661}
2662
avi25764702015-06-23 15:43:372663void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572664 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362665 bool replace,
2666 bool was_post_commit_error) {
mastiz7eddb5f72016-06-23 09:52:452667 DCHECK(!ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2668 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202669
avi5cad4912015-06-19 05:25:442670 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2671 // need to keep continuity with the pending entry, so copy the pending entry's
2672 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2673 // then the renderer navigated on its own, independent of the pending entry,
2674 // so don't copy anything.
2675 if (pending_entry_ && pending_entry_index_ == -1)
2676 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202677
arthursonzogni69a6a1b2019-09-17 09:23:002678 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:202679
creisee17e932015-07-17 17:56:222680 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362681 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102682 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572683 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362684 // If the new entry is a post-commit error page, we store the current last
2685 // committed entry to the side so that we can put it back when navigating
2686 // away from the error.
2687 if (was_post_commit_error) {
2688 DCHECK(!entry_replaced_by_post_commit_error_);
2689 entry_replaced_by_post_commit_error_ =
2690 std::move(entries_[last_committed_entry_index_]);
2691 }
dcheng36b6aec92015-12-26 06:16:362692 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222693 return;
2694 }
[email protected]765b35502008-08-21 00:51:202695
creis37979a62015-08-04 19:48:182696 // We shouldn't see replace == true when there's no committed entries.
2697 DCHECK(!replace);
2698
Michael Thiessen9b14d512019-09-23 21:19:472699 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202700
Shivani Sharmad8c8d652019-02-13 17:27:572701 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202702
dcheng36b6aec92015-12-26 06:16:362703 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202704 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292705}
2706
Shivani Sharmad8c8d652019-02-13 17:27:572707void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162708 if (entries_.size() < max_entry_count())
2709 return;
2710
2711 DCHECK_EQ(max_entry_count(), entries_.size());
2712 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572713 CHECK_EQ(pending_entry_index_, -1);
2714
2715 int index = 0;
Elly Fong-Jonesccc6d1f2021-06-14 18:32:422716 // Retrieve the oldest skippable entry.
2717 for (; index < GetEntryCount(); index++) {
2718 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2719 break;
Shivani Sharmad8c8d652019-02-13 17:27:572720 }
2721
2722 // If there is no skippable entry or if it is the last committed entry then
2723 // fall back to pruning the oldest entry. It is not safe to prune the last
2724 // committed entry.
2725 if (index == GetEntryCount() || index == last_committed_entry_index_)
2726 index = 0;
2727
2728 bool should_succeed = RemoveEntryAtIndex(index);
2729 DCHECK_EQ(true, should_succeed);
2730
2731 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252732}
2733
clamy3cb9bea92018-07-10 12:42:022734void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162735 ReloadType reload_type,
2736 int sandboxed_source_frame_tree_node_id) {
Alexander Timin3a92df72019-09-20 11:59:502737 TRACE_EVENT0("navigation",
2738 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272739 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022740 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362741 if (pending_entry_index_ != -1) {
2742 // The pending entry may not be in entries_ if a post-commit error page is
2743 // showing.
2744 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2745 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2746 }
Gyuyoung Kim107c2a02021-04-13 01:49:302747 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572748 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:012749 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:002750 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:572751 int nav_entry_id = pending_entry_->GetUniqueID();
2752
[email protected]83c2e232011-10-07 21:36:462753 // If we were navigating to a slow-to-commit page, and the user performs
2754 // a session history navigation to the last committed page, RenderViewHost
2755 // will force the throbber to start, but WebKit will essentially ignore the
2756 // navigation, and won't send a message to stop the throbber. To prevent this
2757 // from happening, we drop the navigation here and stop the slow-to-commit
2758 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022759 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:082760 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:272761 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
Takashi Toyoshimabf549472021-07-01 07:47:202762 frame_tree_.StopLoading();
[email protected]6a13a6c2011-12-20 21:47:122763
[email protected]83c2e232011-10-07 21:36:462764 DiscardNonCommittedEntries();
2765 return;
2766 }
2767
creisce0ef3572017-01-26 17:53:082768 // Compare FrameNavigationEntries to see which frames in the tree need to be
2769 // navigated.
clamy3cb9bea92018-07-10 12:42:022770 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2771 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Alex Moshchuk3a4e77a2020-05-29 21:32:572772 FindFramesToNavigate(root, reload_type, &same_document_loads,
2773 &different_document_loads);
creis4e2ecb72015-06-20 00:46:302774
2775 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572776 // We were unable to match any frames to navigate. This can happen if a
2777 // history navigation targets a subframe that no longer exists
2778 // (https://crbug.com/705550). In this case, we need to update the current
2779 // history entry to the pending one but keep the main document loaded. We
2780 // also need to ensure that observers are informed about the updated
2781 // current history entry (e.g., for greying out back/forward buttons), and
2782 // that renderer processes update their history offsets. The easiest way
2783 // to do all that is to schedule a "redundant" same-document navigation in
2784 // the main frame.
2785 //
2786 // Note that we don't want to remove this history entry, as it might still
2787 // be valid later, since a frame that it's targeting may be recreated.
2788 //
2789 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
2790 // need to repeat history navigations until finding the one that works.
2791 // Consider changing this behavior to keep looking for the first valid
2792 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:022793 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422794 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572795 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:572796 ReloadType::NONE /* reload_type */,
2797 true /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422798 false /* is_history_navigation_in_new_child */);
clamy3cb9bea92018-07-10 12:42:022799 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572800 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:022801 DiscardPendingEntry(false);
2802 return;
2803 }
Alex Moshchuk3a4e77a2020-05-29 21:32:572804 same_document_loads.push_back(std::move(navigation_request));
2805
2806 // Sanity check that we never take this branch for any kinds of reloads,
2807 // as those should've queued a different-document load in the main frame.
2808 DCHECK(!is_forced_reload);
2809 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:302810 }
2811
Dave Tapuska8bfd84c2019-03-26 20:47:162812 // If |sandboxed_source_frame_node_id| is valid, then track whether this
2813 // navigation affects any frame outside the frame's subtree.
2814 if (sandboxed_source_frame_tree_node_id !=
2815 FrameTreeNode::kFrameTreeNodeInvalidId) {
2816 bool navigates_inside_tree =
2817 DoesSandboxNavigationStayWithinSubtree(
2818 sandboxed_source_frame_tree_node_id, same_document_loads) &&
2819 DoesSandboxNavigationStayWithinSubtree(
2820 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:502821 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:162822 // the number of pages that trigger this.
2823 FrameTreeNode* sandbox_source_frame_tree_node =
2824 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
2825 if (sandbox_source_frame_tree_node) {
2826 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
2827 sandbox_source_frame_tree_node->current_frame_host(),
2828 navigates_inside_tree
2829 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
2830 : blink::mojom::WebFeature::
2831 kSandboxBackForwardAffectsFramesOutsideSubtree);
2832 }
Dave Tapuska855c1e12019-08-23 20:45:522833
2834 // If the navigation occurred outside the tree discard it because
2835 // the sandboxed frame didn't have permission to navigate outside
2836 // its tree. If it is possible that the navigation is both inside and
2837 // outside the frame tree and we discard it entirely because we don't
2838 // want to end up in a history state that didn't exist before.
2839 if (base::FeatureList::IsEnabled(
2840 features::kHistoryPreventSandboxedNavigation) &&
2841 !navigates_inside_tree) {
2842 DiscardPendingEntry(false);
2843 return;
2844 }
Dave Tapuska8bfd84c2019-03-26 20:47:162845 }
2846
Carlos Caballero539a421c2020-07-06 10:25:572847 // BackForwardCache:
2848 // Navigate immediately if the document is in the BackForwardCache.
2849 if (back_forward_cache_.GetEntry(nav_entry_id)) {
2850 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
2851 DCHECK_EQ(reload_type, ReloadType::NONE);
2852 auto navigation_request = CreateNavigationRequestFromEntry(
2853 root, pending_entry_, pending_entry_->GetFrameEntry(root),
2854 ReloadType::NONE, false /* is_same_document_history_load */,
2855 false /* is_history_navigation_in_new_child */);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052856 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:572857
2858 return;
2859 }
2860
2861 // History navigation might try to reuse a specific BrowsingInstance, already
2862 // used by a page in the cache. To avoid having two different main frames that
2863 // live in the same BrowsingInstance, evict the all pages with this
2864 // BrowsingInstance from the cache.
2865 //
2866 // For example, take the following scenario:
2867 //
2868 // A1 = Some page on a.com
2869 // A2 = Some other page on a.com
2870 // B3 = An uncacheable page on b.com
2871 //
2872 // Then the following navigations occur:
2873 // A1->A2->B3->A1
2874 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
2875 // take its place) and A1 will be created in the same BrowsingInstance (and
2876 // SiteInstance), as A2.
2877 //
2878 // If we didn't do anything, both A1 and A2 would remain alive in the same
2879 // BrowsingInstance/SiteInstance, which is unsupported by
2880 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
2881 // A2 from the cache.
2882 if (pending_entry_->site_instance()) {
2883 back_forward_cache_.EvictFramesInRelatedSiteInstances(
2884 pending_entry_->site_instance());
2885 }
2886
clamy3cb9bea92018-07-10 12:42:022887 // This call does not support re-entrancy. See http://crbug.com/347742.
2888 CHECK(!in_navigate_to_pending_entry_);
2889 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:302890
arthursonzogni66f711c2019-10-08 14:40:362891 // It is not possible to delete the pending NavigationEntry while navigating
2892 // to it. Grab a reference to delay potential deletion until the end of this
2893 // function.
2894 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
2895
creis4e2ecb72015-06-20 00:46:302896 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:022897 for (auto& item : same_document_loads) {
2898 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052899 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302900 }
clamy3cb9bea92018-07-10 12:42:022901 for (auto& item : different_document_loads) {
2902 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052903 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302904 }
clamy3cb9bea92018-07-10 12:42:022905
2906 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:302907}
2908
Alex Moshchuk3a4e77a2020-05-29 21:32:572909NavigationControllerImpl::HistoryNavigationAction
2910NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:302911 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:572912 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:422913 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:582914 // Only active and prerendered documents are allowed to navigate in their
2915 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422916 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:012917 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:582918 // - If the document is in pending deletion, the browser already committed
2919 // to destroying this RenderFrameHost. See https://crbug.com/930278.
2920 // - If the document is in back-forward cache, it's not allowed to navigate
2921 // as it should remain frozen. Ignore the request and evict the document
2922 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422923 //
Sreeja Kamishettydb8e2892021-03-10 09:30:582924 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:422925 // which should all be inactive as well.
Sreeja Kamishettydb8e2892021-03-10 09:30:582926 if (render_frame_host->IsInactiveAndDisallowActivation())
Sreeja Kamishetty8eacabb2021-03-09 11:45:422927 return HistoryNavigationAction::kStopLooking;
2928 }
arthursonzogni03f76152019-02-12 10:35:202929
Alex Moshchuk3a4e77a2020-05-29 21:32:572930 // If there's no last committed entry, there is no previous history entry to
2931 // compare against, so fall back to a different-document load. Note that we
2932 // should only reach this case for the root frame and not descend further
2933 // into subframes.
2934 if (!GetLastCommittedEntry()) {
2935 DCHECK(frame->IsMainFrame());
2936 return HistoryNavigationAction::kDifferentDocument;
2937 }
2938
2939 // Reloads should result in a different-document load. Note that reloads may
2940 // also happen via the |needs_reload_| mechanism where the reload_type is
2941 // NONE, so detect this by comparing whether we're going to the same
2942 // entry that we're currently on. Similarly to above, only main frames
2943 // should reach this. Note that subframes support reloads, but that's done
2944 // via a different path that doesn't involve FindFramesToNavigate (see
2945 // RenderFrameHost::Reload()).
2946 if (reload_type != ReloadType::NONE ||
2947 pending_entry_index_ == last_committed_entry_index_) {
2948 DCHECK(frame->IsMainFrame());
2949 return HistoryNavigationAction::kDifferentDocument;
2950 }
2951
Alex Moshchuk47d1a4bd2020-06-01 22:15:342952 // If there is no new FrameNavigationEntry for the frame, ignore the
2953 // load. For example, this may happen when going back to an entry before a
2954 // frame was created. Suppose we commit a same-document navigation that also
2955 // results in adding a new subframe somewhere in the tree. If we go back,
2956 // the new subframe will be missing a FrameNavigationEntry in the previous
2957 // NavigationEntry, but we shouldn't delete or change what's loaded in
2958 // it.
2959 //
Alex Moshchuke65c39272020-06-03 17:55:372960 // Note that in this case, there is no need to keep looking for navigations
2961 // in subframes, which would be missing FrameNavigationEntries as well.
2962 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:342963 // It's important to check this before checking |old_item| below, since both
2964 // might be null, and in that case we still shouldn't change what's loaded in
2965 // this frame. Note that scheduling any loads assumes that |new_item| is
2966 // non-null. See https://crbug.com/1088354.
2967 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
2968 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:372969 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:342970
Alex Moshchuk3a4e77a2020-05-29 21:32:572971 // If there is no old FrameNavigationEntry, schedule a different-document
2972 // load.
2973 //
creis225a7432016-06-03 22:56:272974 // TODO(creis): Store the last committed FrameNavigationEntry to use here,
2975 // rather than assuming the NavigationEntry has up to date info on subframes.
creis4e2ecb72015-06-20 00:46:302976 FrameNavigationEntry* old_item =
2977 GetLastCommittedEntry()->GetFrameEntry(frame);
Alex Moshchuk3a4e77a2020-05-29 21:32:572978 if (!old_item)
2979 return HistoryNavigationAction::kDifferentDocument;
2980
Alex Moshchuk3a4e77a2020-05-29 21:32:572981 // If the new item is not in the same SiteInstance, schedule a
2982 // different-document load. Newly restored items may not have a SiteInstance
2983 // yet, in which case it will be assigned on first commit.
2984 if (new_item->site_instance() &&
2985 new_item->site_instance() != old_item->site_instance())
2986 return HistoryNavigationAction::kDifferentDocument;
2987
2988 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:312989 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
2990 // that we do this even if the history navigation would not be modifying this
2991 // frame were it live.
2992 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:572993 return HistoryNavigationAction::kDifferentDocument;
2994
2995 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:312996 // Starting a navigation after a crash early-promotes the speculative
2997 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
2998 // committed yet, so we can not possibly perform a same-document history
2999 // navigation. The frame would need to be reloaded with a cross-document
3000 // navigation.
3001 if (!frame->current_frame_host()->has_committed_any_navigation())
3002 return HistoryNavigationAction::kDifferentDocument;
3003
creis54131692016-08-12 18:32:253004 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:493005 // sequence number but different item sequence number.
3006 if (new_item->document_sequence_number() ==
3007 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573008 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493009 }
avib48cb312016-05-05 21:35:003010
Alex Moshchuk3a4e77a2020-05-29 21:32:573011 // Otherwise, if both item and document sequence numbers differ, this
3012 // should be a different document load.
3013 return HistoryNavigationAction::kDifferentDocument;
3014 }
3015
3016 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373017 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573018 DCHECK_EQ(new_item->document_sequence_number(),
3019 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373020 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573021}
3022
3023void NavigationControllerImpl::FindFramesToNavigate(
3024 FrameTreeNode* frame,
3025 ReloadType reload_type,
3026 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3027 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3028 DCHECK(pending_entry_);
3029 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3030
3031 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3032
3033 if (action == HistoryNavigationAction::kSameDocument) {
3034 std::unique_ptr<NavigationRequest> navigation_request =
3035 CreateNavigationRequestFromEntry(
3036 frame, pending_entry_, new_item, reload_type,
3037 true /* is_same_document_history_load */,
3038 false /* is_history_navigation_in_new_child */);
3039 if (navigation_request) {
3040 // Only add the request if was properly created. It's possible for the
3041 // creation to fail in certain cases, e.g. when the URL is invalid.
3042 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303043 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573044 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473045 std::unique_ptr<NavigationRequest> navigation_request =
3046 CreateNavigationRequestFromEntry(
3047 frame, pending_entry_, new_item, reload_type,
3048 false /* is_same_document_history_load */,
3049 false /* is_history_navigation_in_new_child */);
3050 if (navigation_request) {
3051 // Only add the request if was properly created. It's possible for the
3052 // creation to fail in certain cases, e.g. when the URL is invalid.
3053 different_document_loads->push_back(std::move(navigation_request));
3054 }
3055 // For a different document, the subframes will be destroyed, so there's
3056 // no need to consider them.
3057 return;
Alex Moshchuke65c39272020-06-03 17:55:373058 } else if (action == HistoryNavigationAction::kStopLooking) {
3059 return;
creis4e2ecb72015-06-20 00:46:303060 }
3061
3062 for (size_t i = 0; i < frame->child_count(); i++) {
clamy3cb9bea92018-07-10 12:42:023063 FindFramesToNavigate(frame->child_at(i), reload_type, same_document_loads,
creis4e2ecb72015-06-20 00:46:303064 different_document_loads);
3065 }
3066}
3067
Harkiran Bolariaba823e42021-05-21 18:30:363068base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry(
clamy21718cc22018-06-13 13:34:243069 const LoadURLParams& params) {
3070 // Find the appropriate FrameTreeNode.
3071 FrameTreeNode* node = nullptr;
3072 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3073 !params.frame_name.empty()) {
3074 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003075 ? frame_tree_.FindByID(params.frame_tree_node_id)
3076 : frame_tree_.FindByName(params.frame_name);
clamy21718cc22018-06-13 13:34:243077 }
3078
3079 // If no FrameTreeNode was specified, navigate the main frame.
3080 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003081 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243082
Camille Lamy5193caa2018-10-12 11:59:423083 // Compute overrides to the LoadURLParams for |override_user_agent|,
3084 // |should_replace_current_entry| and |has_user_gesture| that will be used
3085 // both in the creation of the NavigationEntry and the NavigationRequest.
3086 // Ideally, the LoadURLParams themselves would be updated, but since they are
3087 // passed as a const reference, this is not possible.
3088 // TODO(clamy): When we only create a NavigationRequest, move this to
3089 // CreateNavigationRequestFromLoadURLParams.
3090 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3091 GetLastCommittedEntry());
3092
3093 // Don't allow an entry replacement if there is no entry to replace.
3094 // http://crbug.com/457149
3095 bool should_replace_current_entry =
3096 params.should_replace_current_entry && entries_.size();
3097
clamy21718cc22018-06-13 13:34:243098 // Javascript URLs should not create NavigationEntries. All other navigations
3099 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243100 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373101 std::unique_ptr<NavigationEntryImpl> entry =
3102 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3103 should_replace_current_entry,
3104 params.has_user_gesture);
clamy21718cc22018-06-13 13:34:243105 DiscardPendingEntry(false);
3106 SetPendingEntry(std::move(entry));
3107 }
3108
3109 // Renderer-debug URLs are sent to the renderer process immediately for
3110 // processing and don't need to create a NavigationRequest.
3111 // Note: this includes navigations to JavaScript URLs, which are considered
3112 // renderer-debug URLs.
3113 // Note: we intentionally leave the pending entry in place for renderer debug
3114 // URLs, unlike the cases below where we clear it if the navigation doesn't
3115 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303116 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483117 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3118 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153119 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483120 params.url, browser_context_)) {
3121 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363122 return nullptr;
Oleg Davydov2cc0167b2019-02-05 14:32:483123 }
3124
clamy21718cc22018-06-13 13:34:243125 HandleRendererDebugURL(node, params.url);
Harkiran Bolariaba823e42021-05-21 18:30:363126 return nullptr;
clamy21718cc22018-06-13 13:34:243127 }
3128
Antonio Sartori78a749f2020-11-30 12:03:393129 DCHECK(pending_entry_);
3130
clamy21718cc22018-06-13 13:34:243131 // Convert navigations to the current URL to a reload.
3132 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3133 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3134 // DevTools sometimes issues navigations to main frames that they do not
3135 // expect to see treated as reload, and it only works because they pass a
3136 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3137 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543138 ReloadType reload_type = params.reload_type;
3139 if (reload_type == ReloadType::NONE &&
3140 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043141 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243142 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243143 params.load_type ==
3144 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
Rakina Zata Amni312822d72021-06-04 16:13:373145 params.should_replace_current_entry, GetLastCommittedEntry())) {
clamy21718cc22018-06-13 13:34:243146 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473147 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393148
3149 // If this is a reload of an existing FrameNavigationEntry and we had a
3150 // policy container for it, then we should copy it into the pending entry,
3151 // so that it is copied to the navigation request in
3152 // CreateNavigationRequestFromLoadParams later.
3153 if (GetLastCommittedEntry()) {
3154 FrameNavigationEntry* previous_frame_entry =
3155 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193156 if (previous_frame_entry &&
3157 previous_frame_entry->policy_container_policies()) {
3158 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163159 previous_frame_entry->policy_container_policies()->Clone());
Antonio Sartori78a749f2020-11-30 12:03:393160 }
3161 }
3162 }
3163
3164 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
3165 // document (so no last commit), then we should copy the document polices from
3166 // RenderFrameHost's PolicyContainerHost. The NavigationRequest will create a
3167 // new PolicyContainerHost with the document policies from the
3168 // |pending_entry_|, and that PolicyContainerHost will be put in the final
3169 // RenderFrameHost for the navigation. This way, we ensure that we keep
3170 // enforcing the right policies on the initial empty document after the
3171 // reload.
3172 if (!GetLastCommittedEntry() && params.url.IsAboutBlank()) {
3173 if (node->current_frame_host() &&
3174 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193175 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163176 node->current_frame_host()
3177 ->policy_container_host()
3178 ->policies()
3179 .Clone());
Antonio Sartori78a749f2020-11-30 12:03:393180 }
clamy21718cc22018-06-13 13:34:243181 }
3182
3183 // navigation_ui_data should only be present for main frame navigations.
3184 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
3185
Camille Lamy5193caa2018-10-12 11:59:423186 std::unique_ptr<NavigationRequest> request =
3187 CreateNavigationRequestFromLoadParams(
3188 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143189 params.has_user_gesture, network::mojom::SourceLocation::New(),
3190 blink::NavigationDownloadPolicy(), reload_type, pending_entry_,
Scott Violetcf6ea7e2021-06-09 21:09:213191 pending_entry_->GetFrameEntry(node));
clamy21718cc22018-06-13 13:34:243192
3193 // If the navigation couldn't start, return immediately and discard the
3194 // pending NavigationEntry.
3195 if (!request) {
3196 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363197 return nullptr;
clamy21718cc22018-06-13 13:34:243198 }
3199
Camille Lamy5193caa2018-10-12 11:59:423200#if DCHECK_IS_ON()
3201 // Safety check that NavigationRequest and NavigationEntry match.
3202 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3203#endif
3204
clamy21718cc22018-06-13 13:34:243205 // This call does not support re-entrancy. See http://crbug.com/347742.
3206 CHECK(!in_navigate_to_pending_entry_);
3207 in_navigate_to_pending_entry_ = true;
3208
arthursonzogni66f711c2019-10-08 14:40:363209 // It is not possible to delete the pending NavigationEntry while navigating
3210 // to it. Grab a reference to delay potential deletion until the end of this
3211 // function.
3212 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3213
Harkiran Bolariaba823e42021-05-21 18:30:363214 base::WeakPtr<NavigationHandle> created_navigation_handle(
3215 request->GetWeakPtr());
Lukasz Anforowicz9ee83c272020-12-01 20:14:053216 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243217
3218 in_navigate_to_pending_entry_ = false;
Harkiran Bolariaba823e42021-05-21 18:30:363219 return created_navigation_handle;
clamy21718cc22018-06-13 13:34:243220}
3221
clamyea99ea12018-05-28 13:54:233222void NavigationControllerImpl::HandleRendererDebugURL(
3223 FrameTreeNode* frame_tree_node,
3224 const GURL& url) {
3225 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243226 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3227 // the renderer process is not live, unless it is the initial navigation
3228 // of the tab.
clamyea99ea12018-05-28 13:54:233229 if (!IsInitialNavigation()) {
3230 DiscardNonCommittedEntries();
3231 return;
3232 }
Fergal Dalyecd3b0202020-06-25 01:57:373233 // The current frame is always a main frame. If IsInitialNavigation() is
3234 // true then there have been no navigations and any frames of this tab must
3235 // be in the same renderer process. If that has crashed then the only frame
3236 // that can be revived is the main frame.
3237 frame_tree_node->render_manager()
3238 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233239 }
3240 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3241}
3242
clamy21718cc22018-06-13 13:34:243243std::unique_ptr<NavigationEntryImpl>
3244NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3245 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423246 const LoadURLParams& params,
3247 bool override_user_agent,
3248 bool should_replace_current_entry,
3249 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393250 // Browser initiated navigations might not have a blob_url_loader_factory set
3251 // in params even if the navigation is to a blob URL. If that happens, lookup
3252 // the correct url loader factory to use here.
3253 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483254 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473255 // Resolve the blob URL in the storage partition associated with the target
3256 // frame. This is the storage partition the URL will be loaded in, and only
3257 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393258 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473259 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393260 }
3261
clamy21718cc22018-06-13 13:34:243262 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443263 // extra_headers in params are \n separated; navigation entries want \r\n.
3264 std::string extra_headers_crlf;
3265 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243266
3267 // For subframes, create a pending entry with a corresponding frame entry.
3268 if (!node->IsMainFrame()) {
Tommy C. Li03eee77a2019-02-05 02:07:443269 if (GetLastCommittedEntry()) {
Tommy C. Li03eee77a2019-02-05 02:07:443270 entry = GetLastCommittedEntry()->Clone();
3271 } else {
3272 // If there's no last committed entry, create an entry for about:blank
3273 // with a subframe entry for our destination.
3274 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
3275 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063276 GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103277 params.source_site_instance.get(), params.transition_type,
3278 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Hayato Ito303654c2021-06-30 09:07:543279 blob_url_loader_factory));
Tommy C. Li03eee77a2019-02-05 02:07:443280 }
Nasko Oskov18006bc2018-12-06 02:53:583281
clamy21718cc22018-06-13 13:34:243282 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:083283 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
clamy21718cc22018-06-13 13:34:243284 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Anton Bikineevf62d1bf2021-05-15 17:56:073285 params.url, absl::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203286 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393287 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:483288 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori78a749f2020-11-30 12:03:393289 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163290 // a conversion of a new navigation into a reload, we will set the right
3291 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193292 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243293 } else {
3294 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243295 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063296 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103297 params.source_site_instance.get(), params.transition_type,
3298 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Hayato Ito303654c2021-06-30 09:07:543299 blob_url_loader_factory));
clamy21718cc22018-06-13 13:34:243300 entry->set_source_site_instance(
3301 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3302 entry->SetRedirectChain(params.redirect_chain);
3303 }
3304
3305 // Set the FTN ID (only used in non-site-per-process, for tests).
3306 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243307 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423308 entry->SetIsOverridingUserAgent(override_user_agent);
3309 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543310 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243311
clamy21718cc22018-06-13 13:34:243312 switch (params.load_type) {
3313 case LOAD_TYPE_DEFAULT:
3314 break;
3315 case LOAD_TYPE_HTTP_POST:
3316 entry->SetHasPostData(true);
3317 entry->SetPostData(params.post_data);
3318 break;
3319 case LOAD_TYPE_DATA:
3320 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3321 entry->SetVirtualURL(params.virtual_url_for_data_url);
3322#if defined(OS_ANDROID)
3323 entry->SetDataURLAsString(params.data_url_as_string);
3324#endif
3325 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3326 break;
clamy21718cc22018-06-13 13:34:243327 }
3328
3329 // TODO(clamy): NavigationEntry is meant for information that will be kept
3330 // after the navigation ended and therefore is not appropriate for
3331 // started_from_context_menu. Move started_from_context_menu to
3332 // NavigationUIData.
3333 entry->set_started_from_context_menu(params.started_from_context_menu);
3334
3335 return entry;
3336}
3337
clamyea99ea12018-05-28 13:54:233338std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423339NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3340 FrameTreeNode* node,
3341 const LoadURLParams& params,
3342 bool override_user_agent,
3343 bool should_replace_current_entry,
3344 bool has_user_gesture,
Antonio Sartori2f763d9d2021-04-21 10:04:143345 network::mojom::SourceLocationPtr source_location,
Yeunjoo Choi3df791a2021-02-17 07:07:253346 blink::NavigationDownloadPolicy download_policy,
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();
danakjb952ef12021-01-14 19:58:493435 bool is_currently_error_page = node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123436
Anatoliy Potapchuk27278cce22021-06-30 09:45:313437 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
Hiroki Nakagawa4ed61282021-06-18 05:37:233448 // Update |download_policy| if the virtual URL is view-source.
Charlie Harrison8c113a32019-01-07 16:08:293449 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253450 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293451
Camille Lamy5193caa2018-10-12 11:59:423452 const GURL& history_url_for_data_url =
3453 params.base_url_for_data_url.is_empty() ? GURL() : virtual_url;
Anatoliy Potapchuk27278cce22021-06-30 09:45:313454 mojom::CommonNavigationParamsPtr common_params =
3455 mojom::CommonNavigationParams::New(
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513456 url_to_load, params.initiator_origin,
3457 blink::mojom::Referrer::New(params.referrer.url,
3458 params.referrer.policy),
Scott Violetcf6ea7e2021-06-09 21:09:213459 params.transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513460 should_replace_current_entry, params.base_url_for_data_url,
3461 history_url_for_data_url, previews_state, navigation_start,
3462 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
Antonio Sartori2f763d9d2021-04-21 10:04:143463 params.post_data, std::move(source_location),
arthursonzogniaf7c62c52020-02-12 10:49:413464 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273465 false /* has_text_fragment_token */,
3466 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3467 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513468 false /* is_history_navigation_in_new_child_frame */,
Charlie Hu5ffc0152019-12-06 15:59:533469 params.input_start);
Camille Lamy5193caa2018-10-12 11:59:423470
Anatoliy Potapchuk27278cce22021-06-30 09:45:313471 mojom::CommitNavigationParamsPtr commit_params =
3472 mojom::CommitNavigationParams::New(
arthursonzognid5a8d0b2021-03-11 17:36:433473 frame_entry->committed_origin(), network::mojom::WebSandboxFlags(),
3474 override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323475 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143476 std::vector<net::RedirectInfo>(),
3477 std::string() /* post_content_type */, common_params->url,
3478 common_params->method, params.can_load_local_resources,
Anatoliy Potapchuk27278cce22021-06-30 09:45:313479 frame_entry->page_state(), entry->GetUniqueID(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143480 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3481 -1 /* pending_history_list_offset */,
3482 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3483 params.should_clear_history_list ? 0 : GetEntryCount(),
3484 false /* was_discarded */, is_view_source_mode,
Anatoliy Potapchuk27278cce22021-06-30 09:45:313485 params.should_clear_history_list, mojom::NavigationTiming::New(),
Anton Bikineevf62d1bf2021-05-15 17:56:073486 absl::nullopt /* appcache_host_id */,
Minggang Wangf59db47b2021-06-16 01:56:223487 blink::mojom::WasActivatedOption::kUnknown,
Lucas Furukawa Gadania9c45682019-07-31 22:05:143488 base::UnguessableToken::Create() /* navigation_token */,
Minggang Wang7ee0c742021-06-16 16:16:513489 std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143490#if defined(OS_ANDROID)
3491 std::string(), /* data_url_as_string */
3492#endif
arthursonzogni14379782020-05-15 09:09:273493 !params.is_renderer_initiated, /* is_browser_initiated */
Sreeja Kamishetty46f762c2021-02-05 07:52:463494 node->frame_tree()->is_prerendering() /* is_prerendering */,
Tsuyoshi Horoe86d7702019-11-29 01:52:473495 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533496 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173497 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073498 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163499 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333500 false /* origin_agent_cluster */,
Maks Orlovichc66745a2020-06-30 17:40:023501 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213502 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Nate Chapind1fe3612021-04-16 20:45:573503 false /* is_cross_browsing_instance */, nullptr /* old_page_info */,
3504 -1 /* http_response_code */,
3505 std::vector<
Anatoliy Potapchuk27278cce22021-06-30 09:45:313506 mojom::AppHistoryEntryPtr>() /* app_history_back_entries */,
3507 std::vector<
3508 mojom::AppHistoryEntryPtr>() /* app_history_forward_entries */,
Kenichi Ishibashi17ecfb62021-06-21 03:31:153509 std::vector<GURL>() /* early_hints_preloaded_resources */);
Camille Lamy5193caa2018-10-12 11:59:423510#if defined(OS_ANDROID)
3511 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143512 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423513 }
3514#endif
3515
Lucas Furukawa Gadania9c45682019-07-31 22:05:143516 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423517
3518 // A form submission may happen here if the navigation is a renderer-initiated
3519 // form submission that took the OpenURL path.
3520 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3521
3522 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3523 std::string extra_headers_crlf;
3524 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093525
3526 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143527 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083528 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453529 params.initiator_frame_token.has_value()
3530 ? &(params.initiator_frame_token.value())
3531 : nullptr,
3532 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3533 request_body,
John Delaney50425f82020-04-07 16:26:213534 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
3535 params.impression);
Yao Xiaodc5ed102019-06-04 19:19:093536 navigation_request->set_from_download_cross_origin_redirect(
3537 params.from_download_cross_origin_redirect);
3538 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423539}
3540
3541std::unique_ptr<NavigationRequest>
3542NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233543 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573544 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233545 FrameNavigationEntry* frame_entry,
3546 ReloadType reload_type,
3547 bool is_same_document_history_load,
Arthur Hemerybee4a752019-05-29 10:50:553548 bool is_history_navigation_in_new_child_frame) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343549 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233550 GURL dest_url = frame_entry->url();
Anton Bikineevf62d1bf2021-05-15 17:56:073551 absl::optional<url::Origin> origin_to_commit =
Nasko Oskov03912102019-01-11 00:21:323552 frame_entry->committed_origin();
3553
clamyea99ea12018-05-28 13:54:233554 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013555 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573556 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233557 // We may have been redirected when navigating to the current URL.
3558 // Use the URL the user originally intended to visit as signaled by the
3559 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013560 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573561 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233562 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323563 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233564 }
3565
Ehsan Karamad44fc72112019-02-26 18:15:473566 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3567 // Avoid starting any new navigations since this node is now preparing for
3568 // attaching an inner delegate.
3569 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203570 }
3571
Camille Lamy5193caa2018-10-12 11:59:423572 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573573 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233574 return nullptr;
3575 }
3576
Kunihiko Sakamoto346a74e2021-03-10 08:57:483577 if (!DoesURLMatchOriginForNavigation(
3578 dest_url, origin_to_commit,
3579 frame_entry->subresource_web_bundle_navigation_info())) {
3580 DCHECK(false) << " url:" << dest_url
3581 << " origin:" << origin_to_commit.value();
Nasko Oskov03912102019-01-11 00:21:323582 return nullptr;
3583 }
3584
clamyea99ea12018-05-28 13:54:233585 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403586 blink::PreviewsState previews_state =
3587 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
clamyea99ea12018-05-28 13:54:233588 if (!frame_tree_node->IsMainFrame()) {
3589 // For subframes, use the state of the top-level frame.
3590 previews_state = frame_tree_node->frame_tree()
3591 ->root()
3592 ->current_frame_host()
3593 ->last_navigation_previews_state();
clamyea99ea12018-05-28 13:54:233594 }
3595
clamyea99ea12018-05-28 13:54:233596 // This will be used to set the Navigation Timing API navigationStart
3597 // parameter for browser navigations in new tabs (intents, tabs opened through
3598 // "Open link in new tab"). If the navigation must wait on the current
3599 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3600 // will be updated when the BeforeUnload ack is received.
3601 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233602
danakjd83d706d2020-11-25 22:11:123603 // Look for a pending commit that is to another document in this
3604 // FrameTreeNode. If one exists, then the last committed URL will not be the
3605 // current URL by the time this navigation commits.
3606 bool has_pending_cross_document_commit =
3607 frame_tree_node->render_manager()
3608 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493609 bool is_currently_error_page =
3610 frame_tree_node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123611
Anatoliy Potapchuk27278cce22021-06-30 09:45:313612 mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123613 /*old_url=*/frame_tree_node->current_url(),
3614 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493615 has_pending_cross_document_commit, is_currently_error_page,
3616 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423617
3618 // A form submission may happen here if the navigation is a
3619 // back/forward/reload navigation that does a form resubmission.
3620 scoped_refptr<network::ResourceRequestBody> request_body;
3621 std::string post_content_type;
3622 if (frame_entry->method() == "POST") {
3623 request_body = frame_entry->GetPostData(&post_content_type);
3624 // Might have a LF at end.
Peter Kastingb53b81912021-04-28 19:23:303625 post_content_type = std::string(
3626 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL));
Camille Lamy5193caa2018-10-12 11:59:423627 }
3628
3629 // Create the NavigationParams based on |entry| and |frame_entry|.
Anatoliy Potapchuk27278cce22021-06-30 09:45:313630 mojom::CommonNavigationParamsPtr common_params =
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513631 entry->ConstructCommonNavigationParams(
3632 *frame_entry, request_body, dest_url,
3633 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
3634 navigation_type, previews_state, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533635 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513636 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553637 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423638
3639 // TODO(clamy): |intended_as_new_entry| below should always be false once
3640 // Reload no longer leads to this being called for a pending NavigationEntry
3641 // of index -1.
Anatoliy Potapchuk27278cce22021-06-30 09:45:313642 mojom::CommitNavigationParamsPtr commit_params =
Lucas Furukawa Gadania9c45682019-07-31 22:05:143643 entry->ConstructCommitNavigationParams(
3644 *frame_entry, common_params->url, origin_to_commit,
3645 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3646 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533647 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3648 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143649 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423650
clamyea99ea12018-05-28 13:54:233651 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143652 frame_tree_node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083653 !entry->is_renderer_initiated(), false /* was_opener_suppressed */,
3654 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453655 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3656 entry->extra_headers(), frame_entry, entry, request_body,
Anton Bikineevf62d1bf2021-05-15 17:56:073657 nullptr /* navigation_ui_data */, absl::nullopt /* impression */);
clamyea99ea12018-05-28 13:54:233658}
3659
[email protected]d202a7c2012-01-04 07:53:473660void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213661 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273662 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403663
[email protected]2db9bd72012-04-13 20:20:563664 // We need to notify the ssl_manager_ before the web_contents_ so the
[email protected]df1af242009-05-01 00:11:403665 // location bar will have up-to-date information about the security style
3666 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133667 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403668
[email protected]7f924832014-08-09 05:57:223669 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:573670 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463671
[email protected]b0f724c2013-09-05 04:21:133672 // TODO(avi): Remove. http://crbug.com/170921
3673 NotificationDetails notification_details =
3674 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153675 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3676 Source<NavigationController>(this),
3677 notification_details);
initial.commit09911bf2008-07-26 23:55:293678}
3679
initial.commit09911bf2008-07-26 23:55:293680// static
[email protected]d202a7c2012-01-04 07:53:473681size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233682 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153683 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:213684 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233685}
3686
[email protected]d202a7c2012-01-04 07:53:473687void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223688 if (is_active && needs_reload_)
3689 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293690}
3691
[email protected]d202a7c2012-01-04 07:53:473692void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293693 if (!needs_reload_)
3694 return;
3695
Bo Liucdfa4b12018-11-06 00:21:443696 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3697 needs_reload_type_);
3698
initial.commit09911bf2008-07-26 23:55:293699 // Calling Reload() results in ignoring state, and not loading.
3700 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3701 // cached state.
avicc872d7242015-08-19 21:26:343702 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163703 NavigateToExistingPendingEntry(ReloadType::NONE,
3704 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343705 } else if (last_committed_entry_index_ != -1) {
arthursonzogni5c4c202d2017-04-25 23:41:273706 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343707 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163708 NavigateToExistingPendingEntry(ReloadType::NONE,
3709 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343710 } else {
3711 // If there is something to reload, the successful reload will clear the
3712 // |needs_reload_| flag. Otherwise, just do it here.
3713 needs_reload_ = false;
3714 }
initial.commit09911bf2008-07-26 23:55:293715}
3716
Carlos IL42b416592019-10-07 23:10:363717void NavigationControllerImpl::LoadPostCommitErrorPage(
3718 RenderFrameHost* render_frame_host,
3719 const GURL& url,
3720 const std::string& error_page_html,
3721 net::Error error) {
Rakina Zata Amni919b7922020-12-11 09:03:133722 RenderFrameHostImpl* rfhi =
3723 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:583724
3725 // Only active documents can load post-commit error pages:
3726 // - If the document is in pending deletion, the browser already committed to
3727 // destroying this RenderFrameHost so ignore loading the error page.
3728 // - If the document is in back-forward cache, it's not allowed to navigate as
3729 // it should remain frozen. Ignore the request and evict the document from
3730 // back-forward cache.
3731 // - If the document is prerendering, it can navigate but when loading error
3732 // pages, cancel prerendering.
3733 if (rfhi->IsInactiveAndDisallowActivation())
3734 return;
3735
Rakina Zata Amni919b7922020-12-11 09:03:133736 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:413737
Anatoliy Potapchuk27278cce22021-06-30 09:45:313738 mojom::CommonNavigationParamsPtr common_params =
Anatoliy Potapchuk64e86fd2021-06-30 09:44:323739 CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:593740 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
3741 // the frame actually committed (e.g. iframe with "src" set to a
3742 // slow-responding URL). We should rewrite the URL to about:blank in this
3743 // case, as the renderer will only think a page is an error page if it has a
3744 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:133745 common_params->url = url.is_empty() ? GURL("about:blank") : url;
Anatoliy Potapchuk27278cce22021-06-30 09:45:313746 mojom::CommitNavigationParamsPtr commit_params =
Anatoliy Potapchuk64e86fd2021-06-30 09:44:323747 CreateCommitNavigationParams();
Antonio Sartori58591c892021-04-21 06:54:333748 commit_params->original_url = common_params->url;
John Delaney131ad362019-08-08 21:57:413749
arthursonzogni70ac7302020-05-28 08:49:053750 // Error pages have a fully permissive FramePolicy.
3751 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
3752 // error pages.
3753 commit_params->frame_policy = blink::FramePolicy();
3754
John Delaney131ad362019-08-08 21:57:413755 std::unique_ptr<NavigationRequest> navigation_request =
3756 NavigationRequest::CreateBrowserInitiated(
3757 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083758 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:193759 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453760 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:063761 "" /* extra_headers */, nullptr /* frame_entry */,
3762 nullptr /* entry */, nullptr /* post_body */,
Anton Bikineevf62d1bf2021-05-15 17:56:073763 nullptr /* navigation_ui_data */, absl::nullopt /* impression */);
Carlos IL42b416592019-10-07 23:10:363764 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:413765 navigation_request->set_net_error(error);
3766 node->CreatedNavigationRequest(std::move(navigation_request));
3767 DCHECK(node->navigation_request());
3768 node->navigation_request()->BeginNavigation();
3769}
3770
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573771void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213772 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093773 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:153774 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143775 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293776}
3777
[email protected]d202a7c2012-01-04 07:53:473778void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363779 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553780 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363781 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293782
initial.commit09911bf2008-07-26 23:55:293783 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293784}
[email protected]765b35502008-08-21 00:51:203785
arthursonzogni69a6a1b2019-09-17 09:23:003786void NavigationControllerImpl::DiscardNonCommittedEntries() {
Michael Thiessen9b14d512019-09-23 21:19:473787 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:103788 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
3789 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:293790 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:473791 return;
Michael Thiessenc5676d22019-09-25 22:32:103792 }
avi45a72532015-04-07 21:01:453793 DiscardPendingEntry(false);
arthursonzogni69a6a1b2019-09-17 09:23:003794 if (delegate_)
3795 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:483796}
3797
avi7c6f35e2015-05-08 17:52:383798int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3799 int nav_entry_id) const {
3800 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3801 if (entries_[i]->GetUniqueID() == nav_entry_id)
3802 return i;
3803 }
3804 return -1;
3805}
3806
[email protected]d202a7c2012-01-04 07:53:473807void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573808 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253809 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573810 DCHECK_LE(max_index, source->GetEntryCount());
Nate Chapin214a86a2021-06-21 20:35:573811 std::unique_ptr<NavigationEntryRestoreContextImpl> context =
3812 std::make_unique<NavigationEntryRestoreContextImpl>();
[email protected]e1cd5452010-08-26 18:03:253813 for (int i = 0; i < max_index; i++) {
Nate Chapin9f169072021-06-09 19:32:373814 // Normally, cloning a NavigationEntryImpl results in sharing
3815 // FrameNavigationEntries between the original and the clone. However, when
3816 // cloning from a different NavigationControllerImpl, we want to fork the
3817 // FrameNavigationEntries.
Nate Chapin9f169072021-06-09 19:32:373818 entries_.insert(entries_.begin() + i,
Nate Chapin214a86a2021-06-21 20:35:573819 source->entries_[i]->CloneWithoutSharing(context.get()));
[email protected]e1cd5452010-08-26 18:03:253820 }
arthursonzogni5c4c202d2017-04-25 23:41:273821 DCHECK(pending_entry_index_ == -1 ||
3822 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:253823}
[email protected]c5b88d82012-10-06 17:03:333824
3825void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:183826 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:333827 get_timestamp_callback_ = get_timestamp_callback;
3828}
[email protected]8ff00d72012-10-23 19:12:213829
Shivani Sharmaffb32b82019-04-09 16:58:473830// History manipulation intervention:
3831void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:473832 bool replace_entry,
3833 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:403834 bool is_renderer_initiated,
3835 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:453836 // Note that for a subframe, previous_document_was_activated is true if the
3837 // gesture happened in any subframe (propagated to main frame) or in the main
3838 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:473839 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:273840 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:473841 return;
3842 }
3843 if (last_committed_entry_index_ == -1)
3844 return;
3845
Shivani Sharmac4cc8922019-04-18 03:11:173846 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473847
Alexander Timine3ec4192020-04-20 16:39:403848 // Log UKM with the URL we are navigating away from.
3849 ukm::builders::HistoryManipulationIntervention(
3850 previous_page_load_ukm_source_id)
3851 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:473852}
3853
Shivani Sharmac4cc8922019-04-18 03:11:173854void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
3855 int reference_index,
3856 bool skippable) {
3857 auto* reference_entry = GetEntryAtIndex(reference_index);
3858 reference_entry->set_should_skip_on_back_forward_ui(skippable);
3859
3860 int64_t document_sequence_number =
3861 reference_entry->root_node()->frame_entry->document_sequence_number();
3862 for (int index = 0; index < GetEntryCount(); index++) {
3863 auto* entry = GetEntryAtIndex(index);
3864 if (entry->root_node()->frame_entry->document_sequence_number() ==
3865 document_sequence_number) {
3866 entry->set_should_skip_on_back_forward_ui(skippable);
3867 }
3868 }
3869}
3870
arthursonzogni66f711c2019-10-08 14:40:363871std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
3872NavigationControllerImpl::ReferencePendingEntry() {
3873 DCHECK(pending_entry_);
3874 auto pending_entry_ref =
3875 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
3876 pending_entry_refs_.insert(pending_entry_ref.get());
3877 return pending_entry_ref;
3878}
3879
3880void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
3881 // Ignore refs that don't correspond to the current pending entry.
3882 auto it = pending_entry_refs_.find(ref);
3883 if (it == pending_entry_refs_.end())
3884 return;
3885 pending_entry_refs_.erase(it);
3886
3887 if (!pending_entry_refs_.empty())
3888 return;
3889
3890 // The pending entry may be deleted before the last PendingEntryRef.
3891 if (!pending_entry_)
3892 return;
3893
3894 // We usually clear the pending entry when the matching NavigationRequest
3895 // fails, so that an arbitrary URL isn't left visible above a committed page.
3896 //
3897 // However, we do preserve the pending entry in some cases, such as on the
3898 // initial navigation of an unmodified blank tab. We also allow the delegate
3899 // to say when it's safe to leave aborted URLs in the omnibox, to let the
3900 // user edit the URL and try again. This may be useful in cases that the
3901 // committed page cannot be attacker-controlled. In these cases, we still
3902 // allow the view to clear the pending entry and typed URL if the user
3903 // requests (e.g., hitting Escape with focus in the address bar).
3904 //
3905 // Do not leave the pending entry visible if it has an invalid URL, since this
3906 // might be formatted in an unexpected or unsafe way.
3907 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:363908 bool should_preserve_entry =
3909 (pending_entry_ == GetVisibleEntry()) &&
3910 pending_entry_->GetURL().is_valid() &&
3911 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
3912 if (should_preserve_entry)
3913 return;
3914
3915 DiscardPendingEntry(true);
3916 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3917}
3918
Titouan Rigoudy6ec70402021-02-02 15:42:193919std::unique_ptr<PolicyContainerPolicies>
3920NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:393921 RenderFrameHostImpl* rfh,
3922 bool is_same_document,
3923 NavigationRequest* request) {
Titouan Rigoudy6ec70402021-02-02 15:42:193924 if (!ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(request))
Antonio Sartori78a749f2020-11-30 12:03:393925 return nullptr;
3926
3927 if (is_same_document) {
Charlie Reis73e356242021-04-02 17:10:313928 // TODO(https://crbug.com/524208): Remove this nullptr check when we can
3929 // ensure we always have a FrameNavigationEntry here.
3930 if (!GetLastCommittedEntry())
3931 return nullptr;
3932
Antonio Sartori78a749f2020-11-30 12:03:393933 FrameNavigationEntry* previous_frame_entry =
3934 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
3935
3936 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
3937 // ensure we always have a FrameNavigationEntry here.
3938 if (!previous_frame_entry)
3939 return nullptr;
3940
Titouan Rigoudy6ec70402021-02-02 15:42:193941 const PolicyContainerPolicies* previous_policies =
3942 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:393943
Titouan Rigoudy6ec70402021-02-02 15:42:193944 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:393945 return nullptr;
3946
3947 // Make a copy of the policy container for the new FrameNavigationEntry.
Antonio Sartori5d09b30f2021-03-02 09:27:163948 return previous_policies->Clone();
Antonio Sartori78a749f2020-11-30 12:03:393949 }
3950
Antonio Sartori4f5373792021-05-31 10:56:473951 return rfh->policy_container_host()->policies().Clone();
Antonio Sartori78a749f2020-11-30 12:03:393952}
3953
Hayato Ito2c8c08d02021-06-23 03:38:433954void NavigationControllerImpl::BroadcastHistoryOffsetAndLength() {
Carlos Caballeroede6f8c2021-01-28 11:01:503955 OPTIONAL_TRACE_EVENT2(
Hayato Ito2c8c08d02021-06-23 03:38:433956 "content", "NavigationControllerImpl::BroadcastHistoryOffsetAndLength",
3957 "history_offset", GetLastCommittedEntryIndex(), "history_length",
3958 GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:503959
3960 auto callback = base::BindRepeating(
3961 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
3962 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
3963 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
3964 }
3965 },
Hayato Ito2c8c08d02021-06-23 03:38:433966 GetLastCommittedEntryIndex(), GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:503967 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
3968}
3969
3970void NavigationControllerImpl::DidAccessInitialMainDocument() {
3971 // We may have left a failed browser-initiated navigation in the address bar
3972 // to let the user edit it and try again. Clear it now that content might
3973 // show up underneath it.
3974 if (!frame_tree_.IsLoading() && GetPendingEntry())
3975 DiscardPendingEntry(false);
3976
3977 // Update the URL display.
3978 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3979}
3980
3981void NavigationControllerImpl::UpdateStateForFrame(
3982 RenderFrameHostImpl* rfhi,
3983 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:563984 OPTIONAL_TRACE_EVENT1("content",
3985 "NavigationControllerImpl::UpdateStateForFrame",
3986 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:503987 // The state update affects the last NavigationEntry associated with the given
3988 // |render_frame_host|. This may not be the last committed NavigationEntry (as
3989 // in the case of an UpdateState from a frame being swapped out). We track
3990 // which entry this is in the RenderFrameHost's nav_entry_id.
3991 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
3992 if (!entry)
3993 return;
3994
3995 FrameNavigationEntry* frame_entry =
3996 entry->GetFrameEntry(rfhi->frame_tree_node());
3997 if (!frame_entry)
3998 return;
3999
4000 // The SiteInstance might not match if we do a cross-process navigation with
4001 // replacement (e.g., auto-subframe), in which case the swap out of the old
4002 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4003 // has already been replaced and destroyed.
4004 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4005 return;
4006
4007 if (page_state == frame_entry->page_state())
4008 return; // Nothing to update.
4009
4010 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4011
4012 // The document_sequence_number and item_sequence_number recorded in the
4013 // FrameNavigationEntry should not differ from the one coming with the update,
4014 // since it must come from the same document. Do not update it if a difference
4015 // is detected, as this indicates that |frame_entry| is not the correct one.
4016 blink::ExplodedPageState exploded_state;
4017 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4018 return;
4019
4020 if (exploded_state.top.document_sequence_number !=
4021 frame_entry->document_sequence_number() ||
4022 exploded_state.top.item_sequence_number !=
4023 frame_entry->item_sequence_number()) {
4024 return;
4025 }
4026
4027 frame_entry->SetPageState(page_state);
4028 NotifyEntryChanged(entry);
4029}
4030
Aaron Colwellb731a0ae2021-03-19 19:14:474031void NavigationControllerImpl::OnStoragePartitionIdAdded(
4032 const StoragePartitionId& partition_id) {
4033 auto it = partition_config_to_id_map_.insert(
4034 std::make_pair(partition_id.config(), partition_id));
4035 bool successful_insert = it.second;
Aaron Colwellb731a0ae2021-03-19 19:14:474036 if (!successful_insert) {
4037 LogStoragePartitionIdCrashKeys(it.first->second, partition_id);
4038 }
Sharon Yangd4f48792021-06-30 17:02:534039 CHECK(successful_insert);
Aaron Colwellb731a0ae2021-03-19 19:14:474040}
4041
4042void NavigationControllerImpl::LogStoragePartitionIdCrashKeys(
4043 const StoragePartitionId& original_partition_id,
4044 const StoragePartitionId& new_partition_id) {
4045 base::debug::SetCrashKeyString(
4046 base::debug::AllocateCrashKeyString("original_partition_id",
4047 base::debug::CrashKeySize::Size256),
4048 original_partition_id.ToString());
4049
4050 base::debug::SetCrashKeyString(
4051 base::debug::AllocateCrashKeyString("new_partition_id",
4052 base::debug::CrashKeySize::Size256),
4053 new_partition_id.ToString());
Aaron Colwellb731a0ae2021-03-19 19:14:474054}
4055
Anatoliy Potapchuk27278cce22021-06-30 09:45:314056std::vector<mojom::AppHistoryEntryPtr>
Nate Chapind1fe3612021-04-16 20:45:574057NavigationControllerImpl::PopulateSingleAppHistoryEntryVector(
4058 Direction direction,
4059 int entry_index,
4060 const url::Origin& pending_origin,
4061 FrameTreeNode* node,
4062 SiteInstance* site_instance,
4063 int64_t previous_item_sequence_number) {
Anatoliy Potapchuk27278cce22021-06-30 09:45:314064 std::vector<mojom::AppHistoryEntryPtr> entries;
Nate Chapind1fe3612021-04-16 20:45:574065 int offset = direction == Direction::kForward ? 1 : -1;
4066 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4067 i += offset) {
4068 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
4069 if (!frame_entry || !frame_entry->committed_origin())
4070 break;
4071 if (site_instance != frame_entry->site_instance())
4072 break;
4073 if (!pending_origin.IsSameOriginWith(*frame_entry->committed_origin()))
4074 break;
4075 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4076 continue;
4077 blink::ExplodedPageState exploded_page_state;
4078 if (blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4079 &exploded_page_state)) {
4080 blink::ExplodedFrameState frame_state = exploded_page_state.top;
Anatoliy Potapchuk27278cce22021-06-30 09:45:314081 mojom::AppHistoryEntryPtr entry = mojom::AppHistoryEntry::New(
4082 frame_state.app_history_key.value_or(std::u16string()),
4083 frame_state.app_history_id.value_or(std::u16string()),
4084 frame_state.url_string.value_or(std::u16string()));
Nate Chapind1fe3612021-04-16 20:45:574085 DCHECK(pending_origin.CanBeDerivedFrom(GURL(entry->url)));
4086 entries.push_back(std::move(entry));
4087 previous_item_sequence_number = frame_entry->item_sequence_number();
4088 }
4089 }
4090 // If |entries| was constructed by iterating backwards from
4091 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4092 // earliest-at-the-front. Reverse it.
4093 if (direction == Direction::kBack)
4094 std::reverse(entries.begin(), entries.end());
4095 return entries;
4096}
4097
4098void NavigationControllerImpl::PopulateAppHistoryEntryVectors(
4099 NavigationRequest* request) {
4100 url::Origin pending_origin =
4101 request->commit_params().origin_to_commit
4102 ? *request->commit_params().origin_to_commit
4103 : url::Origin::Create(request->common_params().url);
4104
4105 FrameTreeNode* node = request->frame_tree_node();
4106 scoped_refptr<SiteInstance> site_instance =
4107 request->GetRenderFrameHost()->GetSiteInstance();
4108
4109 // NOTE: |entry_index| is an estimate of the index where this entry will
4110 // commit, but it may be wrong in corner cases (e.g., if we are at the max
4111 // entry limit, the earliest entry will be dropped). This is ok because this
4112 // algorithm only uses |entry_index| to walk the entry list as it stands right
4113 // now, and it isn't saved for anything post-commit.
4114 int entry_index = GetPendingEntryIndex();
4115 bool will_create_new_entry = false;
4116 if (NavigationTypeUtils::IsReload(request->common_params().navigation_type) ||
4117 request->common_params().should_replace_current_entry) {
4118 entry_index = GetLastCommittedEntryIndex();
4119 } else if (entry_index == -1) {
4120 will_create_new_entry = true;
4121 entry_index = GetLastCommittedEntryIndex() + 1;
4122 }
4123
4124 int64_t pending_item_sequence_number = 0;
4125 if (auto* pending_entry = GetPendingEntry()) {
4126 if (auto* frame_entry = pending_entry->GetFrameEntry(node))
4127 pending_item_sequence_number = frame_entry->item_sequence_number();
4128 }
4129
4130 request->set_app_history_back_entries(PopulateSingleAppHistoryEntryVector(
4131 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
4132 pending_item_sequence_number));
4133
4134 // Don't populate forward entries if they will be truncated by a new entry.
4135 if (!will_create_new_entry) {
4136 request->set_app_history_forward_entries(
4137 PopulateSingleAppHistoryEntryVector(
4138 Direction::kForward, entry_index, pending_origin, node,
4139 site_instance.get(), pending_item_sequence_number));
4140 }
4141}
4142
Nate Chapinfbfe5af2021-06-10 17:22:084143NavigationControllerImpl::HistoryNavigationAction
4144NavigationControllerImpl::ShouldNavigateToEntryForAppHistoryKey(
4145 FrameNavigationEntry* current_entry,
4146 FrameNavigationEntry* target_entry,
4147 const std::string& app_history_key) {
4148 if (!target_entry || !target_entry->committed_origin())
4149 return HistoryNavigationAction::kStopLooking;
4150 if (current_entry->site_instance() != target_entry->site_instance())
4151 return HistoryNavigationAction::kStopLooking;
4152 if (!current_entry->committed_origin()->IsSameOriginWith(
4153 *target_entry->committed_origin())) {
4154 return HistoryNavigationAction::kStopLooking;
4155 }
4156
4157 // NOTE: We don't actually care between kSameDocument and
4158 // kDifferentDocument, so always use kDifferentDocument by convention.
4159 if (target_entry->app_history_key() == app_history_key)
4160 return HistoryNavigationAction::kDifferentDocument;
4161 return HistoryNavigationAction::kKeepLooking;
4162}
4163
4164void NavigationControllerImpl::NavigateToAppHistoryKey(FrameTreeNode* node,
4165 const std::string& key) {
4166 FrameNavigationEntry* current_entry =
4167 GetLastCommittedEntry()->GetFrameEntry(node);
4168 if (!current_entry)
4169 return;
4170
4171 // We want to find the nearest matching entry that is contiguously
4172 // same-instance and same-origin. Check back first, then forward.
4173 // TODO(japhet): Link spec here once it exists.
4174 for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) {
4175 auto result = ShouldNavigateToEntryForAppHistoryKey(
4176 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4177 if (result == HistoryNavigationAction::kStopLooking)
4178 break;
4179 if (result != HistoryNavigationAction::kKeepLooking) {
4180 GoToIndex(i);
4181 return;
4182 }
4183 }
4184 for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) {
4185 auto result = ShouldNavigateToEntryForAppHistoryKey(
4186 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4187 if (result == HistoryNavigationAction::kStopLooking)
4188 break;
4189 if (result != HistoryNavigationAction::kKeepLooking) {
4190 GoToIndex(i);
4191 return;
4192 }
4193 }
4194}
4195
[email protected]8ff00d72012-10-23 19:12:214196} // namespace content