blob: 4c22be27380459e0a001243ceb0f9ee233a9e111 [file] [log] [blame]
[email protected]d4a8ca482013-10-30 21:06:401// Copyright 2013 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
creis4e2ecb72015-06-20 00:46:305/*
6 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
7 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved.
9 * (http://www.torchmobile.com/)
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
21 * its contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
25 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
28 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
danakjc492bf82020-09-09 20:02:4436#include "content/browser/renderer_host/navigation_controller_impl.h"
initial.commit09911bf2008-07-26 23:55:2937
Lei Zhang96031532019-10-10 19:05:4738#include <algorithm>
dcheng36b6aec92015-12-26 06:16:3639#include <utility>
40
[email protected]c5b88d82012-10-06 17:03:3341#include "base/bind.h"
[email protected]eabfe1912014-05-12 10:07:2842#include "base/command_line.h"
Aaron Colwellb731a0ae2021-03-19 19:14:4743#include "base/debug/dump_without_crashing.h"
initial.commit09911bf2008-07-26 23:55:2944#include "base/logging.h"
asvitkine30330812016-08-30 04:01:0845#include "base/metrics/histogram_macros.h"
Chris Hamilton83272dc2021-02-23 00:24:0246#include "base/stl_util.h"
Peter Kastingb53b81912021-04-28 19:23:3047#include "base/strings/string_piece.h"
[email protected]348fbaac2013-06-11 06:31:5148#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0049#include "base/strings/utf_string_conversions.h"
[email protected]a43858f2013-06-28 15:18:3750#include "base/time/time.h"
Carlos Caballero40b0efd2021-01-26 11:55:0051#include "base/trace_event/optional_trace_event.h"
Carlos Caballeroede6f8c2021-01-28 11:01:5052#include "base/trace_event/trace_conversion_helper.h"
ssid3e765612015-01-28 04:03:4253#include "base/trace_event/trace_event.h"
servolkf3955532015-05-16 00:01:5954#include "build/build_config.h"
[email protected]eabfe1912014-05-12 10:07:2855#include "cc/base/switches.h"
jamescookda2505812015-03-20 18:01:1856#include "content/browser/bad_message.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3957#include "content/browser/blob_storage/chrome_blob_storage_context.h"
[email protected]825b1662012-03-12 19:07:3158#include "content/browser/browser_url_handler_impl.h"
[email protected]5f2aa722013-08-07 16:59:4159#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0460#include "content/browser/dom_storage/session_storage_namespace_impl.h"
danakjc492bf82020-09-09 20:02:4461#include "content/browser/renderer_host/debug_urls.h"
62#include "content/browser/renderer_host/frame_tree.h"
Carlos Caballero40b0efd2021-01-26 11:55:0063#include "content/browser/renderer_host/frame_tree_node.h"
danakjc492bf82020-09-09 20:02:4464#include "content/browser/renderer_host/navigation_entry_impl.h"
Nate Chapin214a86a2021-06-21 20:35:5765#include "content/browser/renderer_host/navigation_entry_restore_context_impl.h"
danakjc492bf82020-09-09 20:02:4466#include "content/browser/renderer_host/navigation_request.h"
67#include "content/browser/renderer_host/navigator.h"
68#include "content/browser/renderer_host/render_frame_host_delegate.h"
Carlos Caballero40b0efd2021-01-26 11:55:0069#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]b6583592012-01-25 19:52:3370#include "content/browser/site_instance_impl.h"
Kunihiko Sakamoto346a74e2021-03-10 08:57:4871#include "content/browser/web_package/subresource_web_bundle_navigation_info.h"
Tsuyoshi Horo37493af2019-11-21 23:43:1472#include "content/browser/web_package/web_bundle_navigation_info.h"
Camille Lamy5193caa2018-10-12 11:59:4273#include "content/common/content_constants_internal.h"
Nate Chapind1fe3612021-04-16 20:45:5774#include "content/common/navigation_params_utils.h"
Nasko Oskovae49e292020-08-13 02:08:5175#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1176#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1977#include "content/public/browser/content_browser_client.h"
[email protected]d9083482012-01-06 00:38:4678#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0079#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3880#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1681#include "content/public/browser/notification_types.h"
Lei Zhang96031532019-10-10 19:05:4782#include "content/public/browser/render_view_host.h"
[email protected]9677a3c2012-12-22 04:18:5883#include "content/public/browser/render_widget_host.h"
84#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1085#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3486#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:1987#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:3888#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:1089#include "content/public/common/content_features.h"
Lei Zhang96031532019-10-10 19:05:4790#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4391#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:5992#include "media/base/mime_util.h"
[email protected]a23de8572009-06-03 02:16:3293#include "net/base/escape.h"
Arthur Sonzogni620cec62018-12-13 13:08:5794#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:1895#include "services/metrics/public/cpp/ukm_builders.h"
96#include "services/metrics/public/cpp/ukm_recorder.h"
Yue Ru Sun128804932020-09-30 22:19:1797#include "services/metrics/public/cpp/ukm_source_id.h"
[email protected]9677a3c2012-12-22 04:18:5898#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3999#include "third_party/blink/public/common/blob/blob_utils.h"
Gyuyoung Kim107c2a02021-04-13 01:49:30100#include "third_party/blink/public/common/chrome_debug_urls.h"
Miyoung Shin1c565c912021-03-17 12:11:21101#include "third_party/blink/public/common/history/session_history_constants.h"
Blink Reformata30d4232018-04-07 15:31:06102#include "third_party/blink/public/common/mime_util/mime_util.h"
Carlos Caballeroede6f8c2021-01-28 11:01:50103#include "third_party/blink/public/common/page_state/page_state_serialization.h"
Minggang Wang7ee0c742021-06-16 16:16:51104#include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h"
[email protected]cca6f392014-05-28 21:32:26105#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:29106
[email protected]8ff00d72012-10-23 19:12:21107namespace content {
[email protected]e9ba4472008-09-14 15:42:43108namespace {
109
110// Invoked when entries have been pruned, or removed. For example, if the
111// current entries are [google, digg, yahoo], with the current entry google,
112// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47113void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50114 int index,
[email protected]c12bf1a12008-09-17 16:28:49115 int count) {
[email protected]8ff00d72012-10-23 19:12:21116 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50117 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49118 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14119 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43120}
121
[email protected]e9ba4472008-09-14 15:42:43122// Configure all the NavigationEntries in entries for restore. This resets
123// the transition type to reload and makes sure the content state isn't empty.
124void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57125 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48126 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47127 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43128 // Use a transition type of reload so that we don't incorrectly increase
129 // the typed count.
Lei Zhang96031532019-10-10 19:05:47130 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
131 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43132 }
133}
134
[email protected]bf70edce2012-06-20 22:32:22135// Determines whether or not we should be carrying over a user agent override
136// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57137bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22138 return last_entry && last_entry->GetIsOverridingUserAgent();
139}
140
Camille Lamy5193caa2018-10-12 11:59:42141// Determines whether to override user agent for a navigation.
142bool ShouldOverrideUserAgent(
143 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57144 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42145 switch (override_user_agent) {
146 case NavigationController::UA_OVERRIDE_INHERIT:
147 return ShouldKeepOverride(last_committed_entry);
148 case NavigationController::UA_OVERRIDE_TRUE:
149 return true;
150 case NavigationController::UA_OVERRIDE_FALSE:
151 return false;
Camille Lamy5193caa2018-10-12 11:59:42152 }
153 NOTREACHED();
154 return false;
155}
156
Rakina Zata Amni312822d72021-06-04 16:13:37157// Returns true if this navigation should be treated as a reload. For e.g.
clamy0a656e42018-02-06 18:18:28158// navigating to the last committed url via the address bar or clicking on a
Rakina Zata Amni312822d72021-06-04 16:13:37159// link which results in a navigation to the last committed URL (but wasn't
160// converted to do a replacement navigation in the renderer), etc.
Fergal Daly766177d2020-07-07 07:54:04161// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
162// |base_url_for_data_url|, |transition_type| correspond to the new navigation
163// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
164// navigation that committed.
165bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
166 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57167 const GURL& virtual_url,
168 const GURL& base_url_for_data_url,
169 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57170 bool is_post,
Rakina Zata Amni312822d72021-06-04 16:13:37171 bool should_replace_current_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57172 NavigationEntryImpl* last_committed_entry) {
Rakina Zata Amni312822d72021-06-04 16:13:37173 // Navigations intended to do a replacement shouldn't be converted to do a
174 // reload.
175 if (should_replace_current_entry)
clamy0a656e42018-02-06 18:18:28176 return false;
clamy0a656e42018-02-06 18:18:28177 // Only convert to reload if at least one navigation committed.
178 if (!last_committed_entry)
ananta3bdd8ae2016-12-22 17:11:55179 return false;
180
arthursonzogni7a8243682017-12-14 16:41:42181 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28182 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42183 return false;
184
ananta3bdd8ae2016-12-22 17:11:55185 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
186 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
187 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28188 bool transition_type_can_be_converted = false;
189 if (ui::PageTransitionCoreTypeIs(transition_type,
190 ui::PAGE_TRANSITION_RELOAD) &&
191 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
192 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55193 }
clamy0a656e42018-02-06 18:18:28194 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55195 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28196 transition_type_can_be_converted = true;
197 }
198 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
199 transition_type_can_be_converted = true;
200 if (!transition_type_can_be_converted)
201 return false;
202
203 // This check is required for cases like view-source:, etc. Here the URL of
204 // the navigation entry would contain the url of the page, while the virtual
205 // URL contains the full URL including the view-source prefix.
206 if (virtual_url != last_committed_entry->GetVirtualURL())
207 return false;
208
Fergal Daly766177d2020-07-07 07:54:04209 // Check that the URLs match.
210 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
211 // If there's no frame entry then by definition the URLs don't match.
212 if (!frame_entry)
213 return false;
214
215 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28216 return false;
217
218 // This check is required for Android WebView loadDataWithBaseURL. Apps
219 // can pass in anything in the base URL and we need to ensure that these
220 // match before classifying it as a reload.
221 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
222 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
223 return false;
ananta3bdd8ae2016-12-22 17:11:55224 }
225
clamy0a656e42018-02-06 18:18:28226 // Skip entries with SSL errors.
227 if (last_committed_entry->ssl_error())
228 return false;
229
230 // Don't convert to a reload when the last navigation was a POST or the new
231 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04232 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28233 return false;
234
235 return true;
ananta3bdd8ae2016-12-22 17:11:55236}
237
Nasko Oskov03912102019-01-11 00:21:32238bool DoesURLMatchOriginForNavigation(
239 const GURL& url,
Anton Bikineevf62d1bf2021-05-15 17:56:07240 const absl::optional<url::Origin>& origin,
Kunihiko Sakamoto346a74e2021-03-10 08:57:48241 SubresourceWebBundleNavigationInfo*
242 subresource_web_bundle_navigation_info) {
Nasko Oskov03912102019-01-11 00:21:32243 // If there is no origin supplied there is nothing to match. This can happen
244 // for navigations to a pending entry and therefore it should be allowed.
245 if (!origin)
246 return true;
247
Kunihiko Sakamoto346a74e2021-03-10 08:57:48248 if (url.SchemeIs(url::kUrnScheme) && subresource_web_bundle_navigation_info) {
249 // Urn: subframe from WebBundle has an opaque origin derived from the
250 // Bundle's origin.
251 return origin->CanBeDerivedFrom(
252 subresource_web_bundle_navigation_info->bundle_url());
253 }
254
Nasko Oskov03912102019-01-11 00:21:32255 return origin->CanBeDerivedFrom(url);
256}
257
Anton Bikineevf62d1bf2021-05-15 17:56:07258absl::optional<url::Origin> GetCommittedOriginForFrameEntry(
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12259 const mojom::DidCommitProvisionalLoadParams& params,
260 NavigationRequest* request) {
Nasko Oskov03912102019-01-11 00:21:32261 // Error pages commit in an opaque origin, yet have the real URL that resulted
262 // in an error as the |params.url|. Since successful reload of an error page
263 // should commit in the correct origin, setting the opaque origin on the
264 // FrameNavigationEntry will be incorrect.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12265 if (request->DidEncounterError())
Anton Bikineevf62d1bf2021-05-15 17:56:07266 return absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:32267
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12268 // We also currently don't save committed origins for loadDataWithBaseURL
269 // navigations (probably accidentally). Without this check, navigations to
270 // the FrameNavigationEntry might fail the DoesURLMatchOriginForNavigation()
271 // check since the origin will be based on the base URL instead of the data:
272 // URL used for the navigation.
273 // TODO(https://crbug.com/1198406): Save committed origin in
274 // FrameNavigationEntry for this case too.
Anton Bikineevf62d1bf2021-05-15 17:56:07275 absl::optional<std::string> data_url_as_string;
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12276#if defined(OS_ANDROID)
277 data_url_as_string = request->commit_params().data_url_as_string;
278#endif
279 if (NavigationRequest::IsLoadDataWithBaseURLAndUnreachableURL(
280 request->IsInMainFrame(), request->common_params(),
281 data_url_as_string)) {
Anton Bikineevf62d1bf2021-05-15 17:56:07282 return absl::nullopt;
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12283 }
284
Anton Bikineevf62d1bf2021-05-15 17:56:07285 return absl::make_optional(params.origin);
Nasko Oskov03912102019-01-11 00:21:32286}
287
Camille Lamy5193caa2018-10-12 11:59:42288bool IsValidURLForNavigation(bool is_main_frame,
289 const GURL& virtual_url,
290 const GURL& dest_url) {
291 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
292 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
293 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
294 << virtual_url.possibly_invalid_spec();
295 return false;
296 }
297
298 // Don't attempt to navigate to non-empty invalid URLs.
299 if (!dest_url.is_valid() && !dest_url.is_empty()) {
300 LOG(WARNING) << "Refusing to load invalid URL: "
301 << dest_url.possibly_invalid_spec();
302 return false;
303 }
304
305 // The renderer will reject IPC messages with URLs longer than
306 // this limit, so don't attempt to navigate with a longer URL.
307 if (dest_url.spec().size() > url::kMaxURLChars) {
308 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
309 << " characters.";
310 return false;
311 }
312
Aaron Colwell33109c592020-04-21 21:31:19313 // Reject renderer debug URLs because they should have been handled before
314 // we get to this point. This check handles renderer debug URLs
315 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
316 // provides defense-in-depth if a renderer debug URL manages to get here via
317 // some other path. We want to reject the navigation here so it doesn't
318 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30319 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19320 LOG(WARNING) << "Refusing to load renderer debug URL: "
321 << dest_url.possibly_invalid_spec();
322 return false;
323 }
324
Camille Lamy5193caa2018-10-12 11:59:42325 return true;
326}
327
Mikel Astizba9cf2fd2017-12-17 10:38:10328// See replaced_navigation_entry_data.h for details: this information is meant
329// to ensure |*output_entry| keeps track of its original URL (landing page in
330// case of server redirects) as it gets replaced (e.g. history.replaceState()),
331// without overwriting it later, for main frames.
332void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57333 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10334 NavigationEntryImpl* output_entry) {
335 if (output_entry->GetReplacedEntryData().has_value())
336 return;
337
338 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57339 data.first_committed_url = replaced_entry->GetURL();
340 data.first_timestamp = replaced_entry->GetTimestamp();
341 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29342 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10343}
344
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51345mojom::NavigationType GetNavigationType(const GURL& old_url,
346 const GURL& new_url,
347 ReloadType reload_type,
348 NavigationEntryImpl* entry,
349 const FrameNavigationEntry& frame_entry,
danakjd83d706d2020-11-25 22:11:12350 bool has_pending_cross_document_commit,
danakjb952ef12021-01-14 19:58:49351 bool is_currently_error_page,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51352 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23353 // Reload navigations
354 switch (reload_type) {
355 case ReloadType::NORMAL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51356 return mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23357 case ReloadType::BYPASSING_CACHE:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51358 return mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23359 case ReloadType::ORIGINAL_REQUEST_URL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51360 return mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23361 case ReloadType::NONE:
362 break; // Fall through to rest of function.
363 }
364
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08365 if (entry->IsRestored()) {
Lei Zhang96031532019-10-10 19:05:47366 return entry->GetHasPostData() ? mojom::NavigationType::RESTORE_WITH_POST
367 : mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23368 }
369
danakjb952ef12021-01-14 19:58:49370 const bool can_be_same_document =
371 // A pending cross-document commit means this navigation will not occur in
372 // the current document, as that document would end up being replaced in
373 // the meantime.
374 !has_pending_cross_document_commit &&
375 // If the current document is an error page, we should always treat it as
376 // a different-document navigation so that we'll attempt to load the
377 // document we're navigating to (and not stay in the current error page).
378 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12379
clamyea99ea12018-05-28 13:54:23380 // History navigations.
381 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12382 return can_be_same_document && is_same_document_history_load
Lei Zhang96031532019-10-10 19:05:47383 ? mojom::NavigationType::HISTORY_SAME_DOCUMENT
384 : mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23385 }
386 DCHECK(!is_same_document_history_load);
387
388 // A same-document fragment-navigation happens when the only part of the url
389 // that is modified is after the '#' character.
390 //
391 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12392 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23393 //
394 // Note: this check is only valid for navigations that are not history
395 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
396 // history navigation from 'A#foo' to 'A#bar' is not a same-document
397 // navigation, but a different-document one. This is why history navigation
398 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47399 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
400 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12401
402 // The one case where we do the wrong thing here and incorrectly choose
403 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
404 // the renderer is a frameset. All frameset navigations should be
405 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
406 // navigation would do the right thing, as it would send it to the browser and
407 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
408 // into this method). But since we can't tell that case here for browser-
409 // initiated navigations, we have to get the renderer involved. In that case
410 // the navigation would be restarted due to the renderer spending a reply of
411 // mojom::CommitResult::RestartCrossDocument.
412
413 return can_be_same_document && is_same_doc
414 ? mojom::NavigationType::SAME_DOCUMENT
415 : mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23416}
417
Camille Lamy5193caa2018-10-12 11:59:42418// Adjusts the original input URL if needed, to get the URL to actually load and
419// the virtual URL, which may differ.
420void RewriteUrlForNavigation(const GURL& original_url,
421 BrowserContext* browser_context,
422 GURL* url_to_load,
423 GURL* virtual_url,
424 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42425 // Allow the browser URL handler to rewrite the URL. This will, for example,
426 // remove "view-source:" from the beginning of the URL to get the URL that
427 // will actually be loaded. This real URL won't be shown to the user, just
428 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31429 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42430 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
431 url_to_load, browser_context, reverse_on_redirect);
432}
433
434#if DCHECK_IS_ON()
435// Helper sanity check function used in debug mode.
436void ValidateRequestMatchesEntry(NavigationRequest* request,
437 NavigationEntryImpl* entry) {
438 if (request->frame_tree_node()->IsMainFrame()) {
439 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
440 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
441 request->common_params().transition, entry->GetTransitionType()));
442 }
443 DCHECK_EQ(request->common_params().should_replace_current_entry,
444 entry->should_replace_entry());
Nasko Oskovc36327d2019-01-03 23:23:04445 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42446 entry->should_clear_history_list());
447 DCHECK_EQ(request->common_params().has_user_gesture,
448 entry->has_user_gesture());
449 DCHECK_EQ(request->common_params().base_url_for_data_url,
450 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04451 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42452 entry->GetCanLoadLocalResources());
453 DCHECK_EQ(request->common_params().started_from_context_menu,
454 entry->has_started_from_context_menu());
455
456 FrameNavigationEntry* frame_entry =
457 entry->GetFrameEntry(request->frame_tree_node());
458 if (!frame_entry) {
459 NOTREACHED();
460 return;
461 }
462
Camille Lamy5193caa2018-10-12 11:59:42463 DCHECK_EQ(request->common_params().method, frame_entry->method());
464
Nasko Oskovc36327d2019-01-03 23:23:04465 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42466 if (redirect_size == frame_entry->redirect_chain().size()) {
467 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04468 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42469 frame_entry->redirect_chain()[i]);
470 }
471 } else {
472 NOTREACHED();
473 }
474}
475#endif // DCHECK_IS_ON()
476
Dave Tapuska8bfd84c2019-03-26 20:47:16477// Returns whether the session history NavigationRequests in |navigations|
478// would stay within the subtree of the sandboxed iframe in
479// |sandbox_frame_tree_node_id|.
480bool DoesSandboxNavigationStayWithinSubtree(
481 int sandbox_frame_tree_node_id,
482 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
483 for (auto& item : navigations) {
484 bool within_subtree = false;
485 // Check whether this NavigationRequest affects a frame within the
486 // sandboxed frame's subtree by walking up the tree looking for the
487 // sandboxed frame.
488 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03489 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16490 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
491 within_subtree = true;
492 break;
493 }
494 }
495 if (!within_subtree)
496 return false;
497 }
498 return true;
499}
500
Titouan Rigoudy6ec70402021-02-02 15:42:19501bool ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(
Antonio Sartori78a749f2020-11-30 12:03:39502 const NavigationRequest* request) {
503 // For local schemes we need to store the policy container in the
504 // FrameNavigationEntry, so that we can reload it in case of history
505 // navigation.
506 //
507 // TODO(https://crbug.com/1146361 and https://crbug.com/1146362): blob: and
508 // filesystem: should be removed from this list when we have properly
509 // implemented storing their policy container in the respective store.
510 return (request->common_params().url.SchemeIs(url::kAboutScheme) ||
511 request->common_params().url.SchemeIs(url::kDataScheme) ||
512 request->common_params().url.SchemeIsBlob() ||
513 request->common_params().url.SchemeIsFileSystem());
514}
515
[email protected]e9ba4472008-09-14 15:42:43516} // namespace
517
arthursonzogni66f711c2019-10-08 14:40:36518// NavigationControllerImpl::PendingEntryRef------------------------------------
519
520NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
521 base::WeakPtr<NavigationControllerImpl> controller)
522 : controller_(controller) {}
523
524NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
525 if (!controller_) // Can be null with interstitials.
526 return;
527
528 controller_->PendingEntryRefDeleted(this);
529}
530
[email protected]d202a7c2012-01-04 07:53:47531// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29532
[email protected]23a918b2014-07-15 09:51:36533const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23534
[email protected]765b35502008-08-21 00:51:20535// static
[email protected]d202a7c2012-01-04 07:53:47536size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23537 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20538
[email protected]e6fec472013-05-14 05:29:02539// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20540// when testing.
541static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29542
[email protected]71fde352011-12-29 03:29:56543// static
dcheng9bfa5162016-04-09 01:00:57544std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
545 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10546 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07547 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10548 ui::PageTransition transition,
549 bool is_renderer_initiated,
550 const std::string& extra_headers,
551 BrowserContext* browser_context,
552 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
553 return NavigationControllerImpl::CreateNavigationEntry(
554 url, referrer, std::move(initiator_origin),
555 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Rakina Zata Amni66992a82020-06-24 03:57:52556 extra_headers, browser_context, std::move(blob_url_loader_factory),
Scott Violetcf6ea7e2021-06-09 21:09:21557 false /* should_replace_entry */);
Lukasz Anforowicz641234d52019-11-07 21:07:10558}
559
560// static
561std::unique_ptr<NavigationEntryImpl>
562NavigationControllerImpl::CreateNavigationEntry(
563 const GURL& url,
564 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07565 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10566 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57567 ui::PageTransition transition,
568 bool is_renderer_initiated,
569 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09570 BrowserContext* browser_context,
Rakina Zata Amni66992a82020-06-24 03:57:52571 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Scott Violetcf6ea7e2021-06-09 21:09:21572 bool should_replace_entry) {
Camille Lamy5193caa2018-10-12 11:59:42573 GURL url_to_load;
574 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56575 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42576 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
577 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56578
Lukasz Anforowicz641234d52019-11-07 21:07:10579 // Let the NTP override the navigation params and pretend that this is a
580 // browser-initiated, bookmark-like navigation.
581 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violetcf6ea7e2021-06-09 21:09:21582 source_site_instance, &transition, &is_renderer_initiated, &referrer,
583 &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10584
Patrick Monettef507e982019-06-19 20:18:06585 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28586 nullptr, // The site instance for tabs is sent on navigation
587 // (WebContents::GetSiteInstance).
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58588 url_to_load, referrer, initiator_origin, std::u16string(), transition,
Camille Lamy5193caa2018-10-12 11:59:42589 is_renderer_initiated, blob_url_loader_factory);
590 entry->SetVirtualURL(virtual_url);
591 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56592 entry->set_update_virtual_url_with_url(reverse_on_redirect);
593 entry->set_extra_headers(extra_headers);
Rakina Zata Amni66992a82020-06-24 03:57:52594 entry->set_should_replace_entry(should_replace_entry);
Patrick Monettef507e982019-06-19 20:18:06595 return entry;
[email protected]71fde352011-12-29 03:29:56596}
597
[email protected]cdcb1dee2012-01-04 00:46:20598// static
599void NavigationController::DisablePromptOnRepost() {
600 g_check_for_repost = false;
601}
602
[email protected]c5b88d82012-10-06 17:03:33603base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
604 base::Time t) {
605 // If |t| is between the water marks, we're in a run of duplicates
606 // or just getting out of it, so increase the high-water mark to get
607 // a time that probably hasn't been used before and return it.
608 if (low_water_mark_ <= t && t <= high_water_mark_) {
609 high_water_mark_ += base::TimeDelta::FromMicroseconds(1);
610 return high_water_mark_;
611 }
612
613 // Otherwise, we're clear of the last duplicate run, so reset the
614 // water marks.
615 low_water_mark_ = high_water_mark_ = t;
616 return t;
617}
618
ckitagawa0faa5e42020-06-17 17:30:54619NavigationControllerImpl::ScopedShowRepostDialogForTesting::
620 ScopedShowRepostDialogForTesting()
621 : was_disallowed_(g_check_for_repost) {
622 g_check_for_repost = true;
623}
624
625NavigationControllerImpl::ScopedShowRepostDialogForTesting::
626 ~ScopedShowRepostDialogForTesting() {
627 g_check_for_repost = was_disallowed_;
628}
629
[email protected]d202a7c2012-01-04 07:53:47630NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00631 BrowserContext* browser_context,
632 FrameTree& frame_tree,
633 NavigationControllerDelegate* delegate)
634 : frame_tree_(frame_tree),
635 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57636 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22637 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47638 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37639 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29640}
641
[email protected]d202a7c2012-01-04 07:53:47642NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00643 // The NavigationControllerImpl might be called inside its delegate
644 // destructor. Calling it is not valid anymore.
645 delegate_ = nullptr;
646 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29647}
648
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57649WebContents* NavigationControllerImpl::GetWebContents() {
[email protected]ec6c05f2013-10-23 18:41:57650 return delegate_->GetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32651}
652
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57653BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55654 return browser_context_;
655}
656
[email protected]d202a7c2012-01-04 07:53:47657void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30658 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36659 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57660 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57661 // Verify that this controller is unused and that the input is valid.
Lei Zhang96031532019-10-10 19:05:47662 DCHECK_EQ(0, GetEntryCount());
663 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57664 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14665 selected_navigation < static_cast<int>(entries->size()));
arthursonzogni5c4c202d2017-04-25 23:41:27666 DCHECK_EQ(-1, pending_entry_index_);
[email protected]ce3fa3c2009-04-20 19:55:57667
[email protected]ce3fa3c2009-04-20 19:55:57668 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44669 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03670 entries_.reserve(entries->size());
671 for (auto& entry : *entries)
dcheng36b6aec92015-12-26 06:16:36672 entries_.push_back(
673 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
avif16f85a72015-11-13 18:25:03674
675 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
676 // cleared out safely.
677 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57678
679 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36680 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57681}
682
toyoshim6142d96f2016-12-19 09:07:25683void NavigationControllerImpl::Reload(ReloadType reload_type,
684 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28685 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28686 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32687 int current_index = -1;
688
Carlos IL42b416592019-10-07 23:10:36689 if (entry_replaced_by_post_commit_error_) {
690 // If there is an entry that was replaced by a currently active post-commit
691 // error navigation, this can't be the initial navigation.
692 DCHECK(!IsInitialNavigation());
693 // If the current entry is a post commit error, we reload the entry it
694 // replaced instead. We leave the error entry in place until a commit
695 // replaces it, but the pending entry points to the original entry in the
696 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
697 // case that pending_entry_ != entries_[pending_entry_index_].
698 entry = entry_replaced_by_post_commit_error_.get();
699 current_index = GetCurrentEntryIndex();
700 } else if (IsInitialNavigation() && pending_entry_) {
701 // If we are reloading the initial navigation, just use the current
702 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32703 entry = pending_entry_;
704 // The pending entry might be in entries_ (e.g., after a Clone), so we
705 // should also update the current_index.
706 current_index = pending_entry_index_;
707 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00708 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32709 current_index = GetCurrentEntryIndex();
710 if (current_index != -1) {
creis3da03872015-02-20 21:12:32711 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32712 }
[email protected]979a4bc2013-04-24 01:27:15713 }
[email protected]241db352013-04-22 18:04:05714
[email protected]59167c22013-06-03 18:07:32715 // If we are no where, then we can't reload. TODO(darin): We should add a
716 // CanReload method.
717 if (!entry)
718 return;
719
Takashi Toyoshimac7df3c22019-06-25 14:18:47720 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26721 entry->set_reload_type(reload_type);
722
Aran Gilman37d11632019-10-08 23:07:15723 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30724 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07725 // they really want to do this. If they do, the dialog will call us back
726 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57727 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02728
[email protected]106a0812010-03-18 00:15:12729 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57730 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47731 return;
initial.commit09911bf2008-07-26 23:55:29732 }
Lei Zhang96031532019-10-10 19:05:47733
734 if (!IsInitialNavigation())
735 DiscardNonCommittedEntries();
736
737 pending_entry_ = entry;
738 pending_entry_index_ = current_index;
739 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
740
741 NavigateToExistingPendingEntry(reload_type,
742 FrameTreeNode::kFrameTreeNodeInvalidId);
initial.commit09911bf2008-07-26 23:55:29743}
744
[email protected]d202a7c2012-01-04 07:53:47745void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48746 DCHECK(pending_reload_ != ReloadType::NONE);
747 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12748}
749
[email protected]d202a7c2012-01-04 07:53:47750void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48751 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12752 NOTREACHED();
753 } else {
toyoshim6142d96f2016-12-19 09:07:25754 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48755 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12756 }
757}
758
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57759bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09760 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11761}
762
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57763bool NavigationControllerImpl::IsInitialBlankNavigation() {
creis10a4ab72015-10-13 17:22:40764 // TODO(creis): Once we create a NavigationEntry for the initial blank page,
toyoshim0df1d3a2016-09-09 09:52:48765 // we'll need to check for entry count 1 and restore_type NONE (to exclude
766 // the cloned tab case).
creis10a4ab72015-10-13 17:22:40767 return IsInitialNavigation() && GetEntryCount() == 0;
768}
769
Aran Gilman37d11632019-10-08 23:07:15770NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
771 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58772 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03773 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58774}
775
W. James MacLean1c40862c2020-04-27 21:05:57776void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
777 const url::Origin& origin) {
778 for (int i = 0; i < GetEntryCount(); i++) {
779 auto* entry = GetEntryAtIndex(i);
780 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
781 }
782 if (entry_replaced_by_post_commit_error_) {
783 // It's possible we could come back to this entry if the error
784 // page/interstitial goes away.
785 entry_replaced_by_post_commit_error_
786 ->RegisterExistingOriginToPreventOptInIsolation(origin);
787 }
788 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
789 // visiting pending_entry_, which lacks a committed origin. This will be done
790 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
791}
792
avi25764702015-06-23 15:43:37793void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57794 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00795 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37796 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27797 DCHECK_EQ(-1, pending_entry_index_);
[email protected]8ff00d72012-10-23 19:12:21798 NotificationService::current()->Notify(
Aran Gilman37d11632019-10-08 23:07:15799 NOTIFICATION_NAV_ENTRY_PENDING, Source<NavigationController>(this),
avi25764702015-06-23 15:43:37800 Details<NavigationEntry>(pending_entry_));
[email protected]765b35502008-08-21 00:51:20801}
802
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57803NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47804 if (pending_entry_)
805 return pending_entry_;
806 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20807}
808
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57809NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32810 // The pending entry is safe to return for new (non-history), browser-
811 // initiated navigations. Most renderer-initiated navigations should not
812 // show the pending entry, to prevent URL spoof attacks.
813 //
814 // We make an exception for renderer-initiated navigations in new tabs, as
815 // long as no other page has tried to access the initial empty document in
816 // the new tab. If another page modifies this blank page, a URL spoof is
817 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32818 bool safe_to_show_pending =
819 pending_entry_ &&
820 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09821 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32822 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46823 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32824
825 // Also allow showing the pending entry for history navigations in a new tab,
826 // such as Ctrl+Back. In this case, no existing page is visible and no one
827 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15828 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
829 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32830 safe_to_show_pending = true;
831
832 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19833 return pending_entry_;
834 return GetLastCommittedEntry();
835}
836
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57837int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20838 if (pending_entry_index_ != -1)
839 return pending_entry_index_;
840 return last_committed_entry_index_;
841}
842
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57843NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20844 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28845 return nullptr;
avif16f85a72015-11-13 18:25:03846 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20847}
848
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57849bool NavigationControllerImpl::CanViewSource() {
Carlos Caballeroede6f8c2021-01-28 11:01:50850 const std::string& mime_type = frame_tree_.root()
Alex Moshchuk2e470ea2021-02-03 06:46:34851 ->current_frame_host()
852 ->render_view_host()
Carlos Caballeroede6f8c2021-01-28 11:01:50853 ->contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53854 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
855 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27856 NavigationEntry* visible_entry = GetVisibleEntry();
857 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39858 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16859}
860
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57861int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27862 // The last committed entry index must always be less than the number of
Carlos IL4dea8902020-05-26 15:14:29863 // entries. If there are no entries, it must be -1.
arthursonzogni5c4c202d2017-04-25 23:41:27864 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
865 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55866 return last_committed_entry_index_;
867}
868
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57869int NavigationControllerImpl::GetEntryCount() {
Carlos IL4dea8902020-05-26 15:14:29870 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55871 return static_cast<int>(entries_.size());
872}
873
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57874NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37875 if (index < 0 || index >= GetEntryCount())
876 return nullptr;
877
avif16f85a72015-11-13 18:25:03878 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25879}
880
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57881NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47882 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20883}
884
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57885int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46886 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03887}
888
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57889bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03890 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
891 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
892 return true;
893 }
894 return false;
[email protected]765b35502008-08-21 00:51:20895}
896
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57897bool NavigationControllerImpl::CanGoForward() {
WangHui74286d52021-03-31 16:17:15898 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
899 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
900 return true;
901 }
902 return false;
[email protected]765b35502008-08-21 00:51:20903}
904
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57905bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03906 int index = GetIndexForOffset(offset);
907 return index >= 0 && index < GetEntryCount();
908}
909
WangHui74286d52021-03-31 16:17:15910#if defined(OS_ANDROID)
911bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
WangHui74286d52021-03-31 16:17:15912 if (offset == 0)
913 return true;
914 int increment = offset > 0 ? 1 : -1;
915 int non_skippable_entries = 0;
916 for (int index = GetIndexForOffset(increment);
917 index >= 0 && index < GetEntryCount(); index += increment) {
918 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
919 non_skippable_entries++;
920
921 if (non_skippable_entries == std::abs(offset))
922 return true;
923 }
924 return false;
925}
926#endif
927
[email protected]d202a7c2012-01-04 07:53:47928void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06929 int target_index = GetIndexForOffset(-1);
930
Elly Fong-Jones40ee8112021-06-23 19:10:52931 // Move the target index past the skippable entries.
Shivani Sharma298d12852019-01-22 20:04:03932 bool all_skippable_entries = true;
Elly Fong-Jones40ee8112021-06-23 19:10:52933 while (target_index >= 0) {
934 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui()) {
Shivani Sharma298d12852019-01-22 20:04:03935 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06936 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16937 }
Elly Fong-Jones40ee8112021-06-23 19:10:52938 target_index--;
shivanisha55201872018-12-13 04:29:06939 }
Miyoung Shin1c565c912021-03-17 12:11:21940
Shivani Sharma298d12852019-01-22 20:04:03941 // Do nothing if all entries are skippable. Normally this path would not
942 // happen as consumers would have already checked it in CanGoBack but a lot of
943 // tests do not do that.
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42944 if (all_skippable_entries)
Shivani Sharma298d12852019-01-22 20:04:03945 return;
shivanisha55201872018-12-13 04:29:06946
shivanisha55201872018-12-13 04:29:06947 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20948}
949
[email protected]d202a7c2012-01-04 07:53:47950void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06951 int target_index = GetIndexForOffset(1);
952
Shivani Sharma2d5b4b6b2019-01-08 16:07:16953 // Note that at least one entry (the last one) will be non-skippable since
954 // entries are marked skippable only when they add another entry because of
955 // redirect or pushState.
Elly Fong-Jones40ee8112021-06-23 19:10:52956 while (target_index < static_cast<int>(entries_.size())) {
957 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui())
shivanisha55201872018-12-13 04:29:06958 break;
Elly Fong-Jones40ee8112021-06-23 19:10:52959 target_index++;
shivanisha55201872018-12-13 04:29:06960 }
shivanisha55201872018-12-13 04:29:06961 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20962}
963
[email protected]d202a7c2012-01-04 07:53:47964void NavigationControllerImpl::GoToIndex(int index) {
Dave Tapuska8bfd84c2019-03-26 20:47:16965 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId);
966}
967
968void NavigationControllerImpl::GoToIndex(int index,
969 int sandbox_frame_tree_node_id) {
sunjian30574a62017-03-21 21:39:44970 TRACE_EVENT0("browser,navigation,benchmark",
971 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:20972 if (index < 0 || index >= static_cast<int>(entries_.size())) {
973 NOTREACHED();
974 return;
975 }
976
[email protected]cbab76d2008-10-13 22:42:47977 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20978
arthursonzogni5c4c202d2017-04-25 23:41:27979 DCHECK_EQ(nullptr, pending_entry_);
980 DCHECK_EQ(-1, pending_entry_index_);
981 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:20982 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:27983 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
984 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Dave Tapuska8bfd84c2019-03-26 20:47:16985 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id);
[email protected]765b35502008-08-21 00:51:20986}
987
[email protected]d202a7c2012-01-04 07:53:47988void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:12989 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:03990 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:20991 return;
992
[email protected]9ba14052012-06-22 23:50:03993 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20994}
995
WangHui74286d52021-03-31 16:17:15996#if defined(OS_ANDROID)
997void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
998 // Note: This is actually reached in unit tests.
999 if (!CanGoToOffsetWithSkipping(offset))
1000 return;
1001
Elly Fong-Jonesccc6d1f2021-06-14 18:32:421002 if (offset == 0) {
WangHui74286d52021-03-31 16:17:151003 GoToIndex(GetIndexForOffset(offset));
1004 return;
1005 }
1006 int increment = offset > 0 ? 1 : -1;
1007 // Find the offset without counting skippable entries.
1008 int target_index = GetIndexForOffset(increment);
1009 int non_skippable_entries = 0;
1010 for (int index = target_index; index >= 0 && index < GetEntryCount();
1011 index += increment) {
1012 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1013 non_skippable_entries++;
1014
1015 if (non_skippable_entries == std::abs(offset)) {
1016 target_index = index;
1017 break;
1018 }
1019 }
1020
1021 GoToIndex(target_index);
1022}
1023#endif
1024
[email protected]41374f12013-07-24 02:49:281025bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151026 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281027 return false;
[email protected]6a13a6c2011-12-20 21:47:121028
[email protected]43032342011-03-21 14:10:311029 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281030 return true;
[email protected]cbab76d2008-10-13 22:42:471031}
1032
Michael Thiessen9b14d512019-09-23 21:19:471033void NavigationControllerImpl::PruneForwardEntries() {
1034 DiscardNonCommittedEntries();
1035 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471036 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471037 if (num_removed <= 0)
1038 return;
1039 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1040 NotifyPrunedEntries(this, remove_start_index /* start index */,
1041 num_removed /* count */);
1042}
1043
Aran Gilman37d11632019-10-08 23:07:151044void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1045 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321046 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311047 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511048 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1049 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321050 }
1051}
1052
Harkiran Bolariaba823e42021-05-21 18:30:361053base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL(
1054 const GURL& url,
1055 const Referrer& referrer,
1056 ui::PageTransition transition,
1057 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471058 LoadURLParams params(url);
1059 params.referrer = referrer;
1060 params.transition_type = transition;
1061 params.extra_headers = extra_headers;
Harkiran Bolariaba823e42021-05-21 18:30:361062 return LoadURLWithParams(params);
[email protected]cf002332012-08-14 19:17:471063}
1064
Harkiran Bolariaba823e42021-05-21 18:30:361065base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams(
1066 const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061067 if (params.is_renderer_initiated)
1068 DCHECK(params.initiator_origin.has_value());
1069
naskob8744d22014-08-28 17:07:431070 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151071 "NavigationControllerImpl::LoadURLWithParams", "url",
1072 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291073 bool is_explicit_navigation =
1074 GetContentClient()->browser()->IsExplicitNavigation(
1075 params.transition_type);
1076 if (HandleDebugURL(params.url, params.transition_type,
1077 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431078 // If Telemetry is running, allow the URL load to proceed as if it's
1079 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491080 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431081 cc::switches::kEnableGpuBenchmarking))
Harkiran Bolariaba823e42021-05-21 18:30:361082 return nullptr;
[email protected]47752982014-07-29 08:01:431083 }
[email protected]8bf1048012012-02-08 01:22:181084
[email protected]cf002332012-08-14 19:17:471085 // Checks based on params.load_type.
1086 switch (params.load_type) {
1087 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431088 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471089 break;
1090 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261091 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471092 NOTREACHED() << "Data load must use data scheme.";
Harkiran Bolariaba823e42021-05-21 18:30:361093 return nullptr;
[email protected]cf002332012-08-14 19:17:471094 }
1095 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461096 }
[email protected]e47ae9472011-10-13 19:48:341097
[email protected]e47ae9472011-10-13 19:48:341098 // The user initiated a load, we don't need to reload anymore.
1099 needs_reload_ = false;
1100
Harkiran Bolariaba823e42021-05-21 18:30:361101 return NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441102}
1103
Mohamed Abdelhalim833de902019-09-16 17:41:451104bool NavigationControllerImpl::PendingEntryMatchesRequest(
1105 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191106 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451107 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191108}
1109
[email protected]d202a7c2012-01-04 07:53:471110bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321111 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071112 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331113 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441114 bool is_same_document_navigation,
Shivani Sharmaffb32b82019-04-09 16:58:471115 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131116 NavigationRequest* navigation_request) {
1117 DCHECK(navigation_request);
[email protected]cd2e15742013-03-08 04:08:311118 is_initial_navigation_ = false;
1119
[email protected]0e8db942008-09-24 21:21:481120 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431121 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481122 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361123 if (entry_replaced_by_post_commit_error_) {
1124 if (is_same_document_navigation) {
1125 // Same document navigations should not be possible on error pages and
1126 // would leave the controller in a weird state. Kill the renderer if
1127 // that happens.
1128 bad_message::ReceivedBadMessage(
1129 rfh->GetProcess(), bad_message::NC_SAME_DOCUMENT_POST_COMMIT_ERROR);
1130 }
1131 // Any commit while a post-commit error page is showing should put the
1132 // original entry back, replacing the error page's entry. This includes
1133 // reloads, where the original entry was used as the pending entry and
1134 // should now be at the correct index at commit time.
1135 entries_[last_committed_entry_index_] =
1136 std::move(entry_replaced_by_post_commit_error_);
1137 }
Fergal Daly8e33cf62020-12-12 01:06:071138 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551139 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431140 if (pending_entry_ &&
1141 pending_entry_->GetIsOverridingUserAgent() !=
1142 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1143 overriding_user_agent_changed = true;
[email protected]0e8db942008-09-24 21:21:481144 } else {
Gang Wu325f03f42021-02-25 20:00:461145 // GetLastCommittedEntry() is null, so this is the first entry.
Fergal Daly8e33cf62020-12-12 01:06:071146 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481147 details->previous_entry_index = -1;
Gang Wu325f03f42021-02-25 20:00:461148 if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) {
1149 // Default setting is NOT override the user agent, so overriding the user
1150 // agent in first entry should be considered as user agent changed as
1151 // well.
1152 overriding_user_agent_changed = true;
1153 }
[email protected]0e8db942008-09-24 21:21:481154 }
[email protected]ecd9d8702008-08-28 22:10:171155
Alexander Timind2f2e4f22019-04-02 20:04:531156 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1157 // implementing back-forward cache.
1158
1159 // Create a new metrics object or reuse the previous one depending on whether
1160 // it's a main frame navigation or not.
1161 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics =
1162 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1163 GetLastCommittedEntry(), !rfh->GetParent(),
1164 params.document_sequence_number);
1165 // Notify the last active entry that we have navigated away.
1166 if (!rfh->GetParent() && !is_same_document_navigation) {
1167 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1168 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Hajime Hoshic7606502021-04-14 02:42:161169 metrics->MainFrameDidNavigateAwayFromDocument(rfh, navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531170 }
1171 }
1172 }
1173
fdegans9caf66a2015-07-30 21:10:421174 // If there is a pending entry at this point, it should have a SiteInstance,
1175 // except for restored entries.
jam48cea9082017-02-15 06:13:291176 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481177 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081178 pending_entry_->IsRestored());
1179 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151180 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291181 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481182 }
[email protected]e9ba4472008-09-14 15:42:431183
Nasko Oskovaee2f862018-06-15 00:05:521184 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1185 // has changed, this must be treated as a new navigation with replacement.
1186 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491187 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521188 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101189 pending_entry_->GetUniqueID() ==
1190 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521191 pending_entry_->site_instance() &&
1192 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1193 DCHECK_NE(-1, pending_entry_index_);
1194 // TODO(nasko,creis): Instead of setting this value here, set
1195 // should_replace_current_entry on the parameters we send to the
1196 // renderer process as part of CommitNavigation. The renderer should
1197 // in turn send it back here as part of |params| and it can be just
1198 // enforced and renderer process terminated on mismatch.
1199 details->did_replace_entry = true;
1200 } else {
1201 // The renderer tells us whether the navigation replaces the current entry.
1202 details->did_replace_entry = params.should_replace_current_entry;
1203 }
[email protected]bcd904482012-02-01 01:54:221204
[email protected]e9ba4472008-09-14 15:42:431205 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amnif6950d552020-11-24 03:26:101206 details->type = ClassifyNavigation(rfh, params, navigation_request);
[email protected]4bf3522c2010-08-19 21:00:201207
eugenebutee08663a2017-04-27 17:43:121208 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441209 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121210
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071211 details->is_prerender_activation =
1212 navigation_request->IsPrerenderedPageActivation();
1213
Peter Boströmd7592132019-01-30 04:50:311214 // Make sure we do not discard the pending entry for a different ongoing
1215 // navigation when a same document commit comes in unexpectedly from the
1216 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1217 // other navigation types. See https://crbug.com/900036.
1218 // TODO(crbug.com/926009): Handle history.pushState() as well.
1219 bool keep_pending_entry = is_same_document_navigation &&
Charlie Reisc0f17d2d2021-01-12 18:52:491220 details->type == NAVIGATION_TYPE_EXISTING_ENTRY &&
Peter Boströmd7592132019-01-30 04:50:311221 pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451222 !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311223
[email protected]0e8db942008-09-24 21:21:481224 switch (details->type) {
Charlie Reisc0f17d2d2021-01-12 18:52:491225 case NAVIGATION_TYPE_NEW_ENTRY:
1226 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051227 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451228 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431229 break;
Charlie Reisc0f17d2d2021-01-12 18:52:491230 case NAVIGATION_TYPE_EXISTING_ENTRY:
1231 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1232 was_restored, navigation_request,
1233 keep_pending_entry);
[email protected]e9ba4472008-09-14 15:42:431234 break;
[email protected]8ff00d72012-10-23 19:12:211235 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471236 RendererDidNavigateNewSubframe(
1237 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451238 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431239 break;
[email protected]8ff00d72012-10-23 19:12:211240 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391241 if (!RendererDidNavigateAutoSubframe(
1242 rfh, params, details->is_same_document, navigation_request)) {
creisce0ef3572017-01-26 17:53:081243 // We don't send a notification about auto-subframe PageState during
1244 // UpdateStateForFrame, since it looks like nothing has changed. Send
1245 // it here at commit time instead.
1246 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431247 return false;
creis59d5a47cb2016-08-24 23:57:191248 }
[email protected]e9ba4472008-09-14 15:42:431249 break;
[email protected]8ff00d72012-10-23 19:12:211250 case NAVIGATION_TYPE_NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:491251 // If a pending navigation was in progress, this canceled it. We should
1252 // discard it and make sure it is removed from the URL bar. After that,
1253 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:431254 // the caller that nothing has happened.
arthursonzogni69a6a1b2019-09-17 09:23:001255 if (pending_entry_)
[email protected]20d1c992011-04-12 21:17:491256 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431257 return false;
Aran Gilman37d11632019-10-08 23:07:151258 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431259 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151260 break;
[email protected]765b35502008-08-21 00:51:201261 }
1262
[email protected]688aa65c62012-09-28 04:32:221263 // At this point, we know that the navigation has just completed, so
1264 // record the time.
1265 //
1266 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261267 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331268 base::Time timestamp =
1269 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1270 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131271 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221272
Peter Boströmd7592132019-01-30 04:50:311273 // If we aren't keeping the pending entry, there shouldn't be one at this
1274 // point. Clear it again in case any error cases above forgot to do so.
1275 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1276 // been cleared instead of protecting against it.
1277 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001278 DiscardNonCommittedEntries();
[email protected]f233e4232013-02-23 00:55:141279
[email protected]e9ba4472008-09-14 15:42:431280 // All committed entries should have nonempty content state so WebKit doesn't
1281 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471282 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321283 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221284 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441285 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531286 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1287 // implementing back-forward cache.
1288 if (!active_entry->back_forward_cache_metrics()) {
1289 active_entry->set_back_forward_cache_metrics(
1290 std::move(back_forward_cache_metrics));
1291 }
1292 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
Hajime Hoshi446206e2019-10-18 18:36:251293 navigation_request,
1294 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
naskoc7533512016-05-06 17:01:121295
Charles Reisc0507202017-09-21 00:40:021296 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1297 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1298 // A mismatch can occur if the renderer lies or due to a unique name collision
1299 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121300 FrameNavigationEntry* frame_entry =
1301 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021302 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1303 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031304 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081305 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1306 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031307 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201308
1309 // Remember the bindings the renderer process has at this point, so that
1310 // we do not grant this entry additional bindings if we come back to it.
1311 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301312 }
[email protected]132e281a2012-07-31 18:32:441313
[email protected]97d8f0d2013-10-29 16:49:211314 // Once it is committed, we no longer need to track several pieces of state on
1315 // the entry.
naskoc7533512016-05-06 17:01:121316 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131317
[email protected]49bd30e62011-03-22 20:12:591318 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221319 // TODO(creis): This check won't pass for subframes until we create entries
1320 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001321 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421322 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591323
[email protected]e9ba4472008-09-14 15:42:431324 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001325 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001326 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311327 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531328
arthursonzogni7ddc6542021-04-09 09:16:501329 active_entry->SetIsOverridingUserAgent(
1330 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031331
[email protected]93f230e02011-06-01 14:40:001332 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291333
John Abd-El-Malek380d3c5922017-09-08 00:20:311334 if (active_entry->GetURL().SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121335 !navigation_request->DidEncounterError()) {
John Abd-El-Malek969cdd94e2017-07-10 22:18:161336 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus",
1337 !!active_entry->GetSSL().certificate);
1338 }
1339
aelias100c9192017-01-13 00:01:431340 if (overriding_user_agent_changed)
1341 delegate_->UpdateOverridingUserAgent();
1342
creis03b48002015-11-04 00:54:561343 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1344 // one knows the latest NavigationEntry it is showing (whether it has
1345 // committed anything in this navigation or not). This allows things like
1346 // state and title updates from RenderFrames to apply to the latest relevant
1347 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381348 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001349 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381350 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
Hayato Ito2c8c08d02021-06-23 03:38:431351
1352 if (navigation_request->IsPrerenderedPageActivation()) {
1353 BroadcastHistoryOffsetAndLength();
1354 // TODO(crbug.com/1222893): Broadcasting happens after the prerendered page
1355 // is activated. As a result, a "prerenderingchange" event listener sees the
1356 // history.length which is not updated yet. We should guarantee that
1357 // history's length and offset should be updated before a
1358 // "prerenderingchange" event listener runs. One possible approach is to use
1359 // the same IPC which "prerenderingchange" uses, and propagate history's
1360 // length and offset together with that.
1361 }
1362
[email protected]e9ba4472008-09-14 15:42:431363 return true;
initial.commit09911bf2008-07-26 23:55:291364}
1365
[email protected]8ff00d72012-10-23 19:12:211366NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321367 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101368 const mojom::DidCommitProvisionalLoadParams& params,
1369 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591370 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511371 "ClassifyNavigation");
1372
avi7c6f35e2015-05-08 17:52:381373 if (params.did_create_new_entry) {
Charlie Reisc0f17d2d2021-01-12 18:52:491374 // A new entry. We may or may not have a corresponding pending entry, and
avi7c6f35e2015-05-08 17:52:381375 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001376 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491377 trace_return.set_return_reason("new entry, no parent, new entry");
1378 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381379 }
1380
1381 // When this is a new subframe navigation, we should have a committed page
1382 // in which it's a subframe. This may not be the case when an iframe is
1383 // navigated on a popup navigated to about:blank (the iframe would be
1384 // written into the popup by script on the main page). For these cases,
1385 // there isn't any navigation stuff we can do, so just ignore it.
Nasko Oskovae49e292020-08-13 02:08:511386 if (!GetLastCommittedEntry()) {
1387 trace_return.set_return_reason("new entry, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381388 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511389 }
avi7c6f35e2015-05-08 17:52:381390
1391 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511392 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381393 return NAVIGATION_TYPE_NEW_SUBFRAME;
1394 }
1395
Charlie Reisc0f17d2d2021-01-12 18:52:491396 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381397 DCHECK(!params.history_list_was_cleared);
1398
avi39c1edd32015-06-04 20:06:001399 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381400 // All manual subframes would be did_create_new_entry and handled above, so
1401 // we know this is auto.
Nasko Oskovae49e292020-08-13 02:08:511402 if (GetLastCommittedEntry()) {
1403 trace_return.set_return_reason("subframe, last commmited, auto subframe");
avi7c6f35e2015-05-08 17:52:381404 return NAVIGATION_TYPE_AUTO_SUBFRAME;
Nasko Oskovae49e292020-08-13 02:08:511405 }
Lei Zhang96031532019-10-10 19:05:471406
1407 // We ignore subframes created in non-committed pages; we'd appreciate if
1408 // people stopped doing that.
Nasko Oskovae49e292020-08-13 02:08:511409 trace_return.set_return_reason("subframe, no last commmited, ignore");
Lei Zhang96031532019-10-10 19:05:471410 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381411 }
1412
Rakina Zata Amnif6950d552020-11-24 03:26:101413 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1414 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381415 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1416 // create a new page.
1417
1418 // Just like above in the did_create_new_entry case, it's possible to
1419 // scribble onto an uncommitted page. Again, there isn't any navigation
1420 // stuff that we can do, so ignore it here as well.
avi3a5b8f32015-05-28 17:50:231421 NavigationEntry* last_committed = GetLastCommittedEntry();
Nasko Oskovae49e292020-08-13 02:08:511422 if (!last_committed) {
1423 trace_return.set_return_reason("nav entry 0, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381424 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511425 }
avi7c6f35e2015-05-08 17:52:381426
Charles Reis1378111f2017-11-08 21:44:061427 // This is history.replaceState() or history.reload().
Nasko Oskov332593c2018-08-16 17:21:341428 // TODO(nasko): With error page isolation, reloading an existing session
1429 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491430 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341431 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511432 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491433 "nav entry 0, last committed, existing entry");
1434 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381435 }
1436
Rakina Zata Amnif6950d552020-11-24 03:26:101437 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521438 // If the SiteInstance of the |pending_entry_| does not match the
1439 // SiteInstance that got committed, treat this as a new navigation with
1440 // replacement. This can happen if back/forward/reload encounters a server
1441 // redirect to a different site or an isolated error page gets successfully
1442 // reloaded into a different SiteInstance.
1443 if (pending_entry_->site_instance() &&
1444 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491445 trace_return.set_return_reason("pending matching nav entry, new entry");
1446 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521447 }
creis77c9aa32015-09-25 19:59:421448
Nasko Oskovaee2f862018-06-15 00:05:521449 if (pending_entry_index_ == -1) {
1450 // In this case, we have a pending entry for a load of a new URL but Blink
1451 // didn't do a new navigation (params.did_create_new_entry). First check
1452 // to make sure Blink didn't treat a new cross-process navigation as
1453 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161454 // we must treat it as NEW rather than the converted reload case below,
1455 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521456 if (!GetLastCommittedEntry() ||
1457 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161458 trace_return.set_return_reason("new pending, new entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491459 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521460 }
1461
1462 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161463 // We will create a pending entry, but NavigateWithoutEntry will convert
1464 // it to a reload since it's the same page and not create a new entry for
1465 // it. (The user doesn't want to have a new back/forward entry when they
1466 // do this.) Therefore we want to just ignore the pending entry and go
1467 // back to where we were (the "existing entry").
1468 trace_return.set_return_reason("new pending, existing (same) entry");
1469 return NAVIGATION_TYPE_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521470 }
avi7c6f35e2015-05-08 17:52:381471 }
1472
creis26d22632017-04-21 20:23:561473 // Everything below here is assumed to be an existing entry, but if there is
1474 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511475 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491476 trace_return.set_return_reason("no last committed, new entry");
1477 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511478 }
creis26d22632017-04-21 20:23:561479
avi7c6f35e2015-05-08 17:52:381480 if (params.intended_as_new_entry) {
1481 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491482 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1483 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161484 trace_return.set_return_reason("intended as new entry, existing entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491485 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381486 }
1487
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121488 if (navigation_request->DidEncounterError() &&
1489 failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101490 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381491 // If the renderer was going to a new pending entry that got cleared because
1492 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491493 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381494 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511495 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491496 "unreachable, matching pending, existing entry");
1497 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381498 }
1499
Charlie Reisc0f17d2d2021-01-12 18:52:491500 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101501 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511502 trace_return.traced_value()->SetInteger("existing_entry_index",
1503 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381504 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441505 // The renderer has committed a navigation to an entry that no longer
1506 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491507 trace_return.set_return_reason("existing entry -1, new entry");
1508 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381509 }
1510
avi7c6f35e2015-05-08 17:52:381511 // Since we weeded out "new" navigations above, we know this is an existing
1512 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491513 trace_return.set_return_reason("default return, existing entry");
1514 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381515}
1516
Charlie Reisc0f17d2d2021-01-12 18:52:491517void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321518 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071519 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361520 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441521 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471522 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451523 NavigationRequest* request) {
dcheng9bfa5162016-04-09 01:00:571524 std::unique_ptr<NavigationEntryImpl> new_entry;
creisf49dfc92016-07-26 17:05:181525 bool update_virtual_url = false;
1526
Anton Bikineevf62d1bf2021-05-15 17:56:071527 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451528 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061529
creisf49dfc92016-07-26 17:05:181530 // First check if this is an in-page navigation. If so, clone the current
1531 // entry instead of looking at the pending entry, because the pending entry
1532 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361533 if (is_same_document && GetLastCommittedEntry()) {
Patrick Monette50e8bd82019-06-13 22:40:451534 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481535 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081536 params.document_sequence_number, params.app_history_key,
1537 rfh->GetSiteInstance(), nullptr, params.url,
1538 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091539 Referrer(*params.referrer), initiator_origin,
1540 request->GetRedirectChain(), params.page_state, params.method,
1541 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391542 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481543 request->GetSubresourceWebBundleNavigationInfo(),
Antonio Sartori78a749f2020-11-30 12:03:391544 // We will set the document policies later in this function.
Titouan Rigoudy6ec70402021-02-02 15:42:191545 nullptr /* policy_container_policies */);
Charles Reisf44482022017-10-13 21:15:031546
creisf49dfc92016-07-26 17:05:181547 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Carlos Caballero40b0efd2021-01-26 11:55:001548 frame_entry, true, rfh->frame_tree_node(), frame_tree_.root());
jama78746e2017-02-22 17:21:571549 if (new_entry->GetURL().GetOrigin() != params.url.GetOrigin()) {
1550 // TODO(jam): we had one report of this with a URL that was redirecting to
1551 // only tildes. Until we understand that better, don't copy the cert in
1552 // this case.
1553 new_entry->GetSSL() = SSLStatus();
John Abd-El-Malek9cd697e2017-07-12 21:53:141554
John Abd-El-Malek380d3c5922017-09-08 00:20:311555 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121556 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141557 UMA_HISTOGRAM_BOOLEAN(
1558 "Navigation.SecureSchemeHasSSLStatus.NewPageInPageOriginMismatch",
1559 !!new_entry->GetSSL().certificate);
1560 }
jama78746e2017-02-22 17:21:571561 }
creisf49dfc92016-07-26 17:05:181562
Patrick Monette50e8bd82019-06-13 22:40:451563 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1564 // that |frame_entry| should now have a reference count of exactly 2: one
1565 // due to the local variable |frame_entry|, and another due to |new_entry|
1566 // also retaining one. This should never fail, because it's the main frame.
1567 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181568
1569 update_virtual_url = new_entry->update_virtual_url_with_url();
John Abd-El-Malek9cd697e2017-07-12 21:53:141570
John Abd-El-Malek380d3c5922017-09-08 00:20:311571 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121572 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141573 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus.NewPageInPage",
1574 !!new_entry->GetSSL().certificate);
1575 }
creisf49dfc92016-07-26 17:05:181576 }
1577
Harkiran Bolaria59290d62021-03-17 01:53:011578 // If this is an activation navigation from a prerendered page, transfer the
1579 // new entry from an entry already created and stored in the
1580 // NavigationRequest. |new_entry| will not have been set prior to this as
1581 // |is_same_document| is mutually exclusive with
1582 // |IsPrerenderedPageActivation|.
1583 if (request->IsPrerenderedPageActivation()) {
1584 DCHECK(!is_same_document);
1585 DCHECK(!new_entry);
1586 new_entry = request->TakePrerenderNavigationEntry();
1587 DCHECK(new_entry);
1588 }
1589
Charlie Reisc0f17d2d2021-01-12 18:52:491590 // Only make a copy of the pending entry if it is appropriate for the new
1591 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451592 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041593 // 1. The SiteInstance hasn't been assigned to something else.
1594 // 2. The pending entry was intended as a new entry, rather than being a
1595 // history navigation that was interrupted by an unrelated,
1596 // renderer-initiated navigation.
1597 // TODO(csharrison): Investigate whether we can remove some of the coarser
1598 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451599 if (!new_entry && PendingEntryMatchesRequest(request) &&
1600 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341601 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431602 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351603 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431604
[email protected]f1eb87a2011-05-06 17:49:411605 update_virtual_url = new_entry->update_virtual_url_with_url();
Camille Lamy62b826012019-02-26 09:15:471606 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451607 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141608
John Abd-El-Malek380d3c5922017-09-08 00:20:311609 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121610 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141611 UMA_HISTOGRAM_BOOLEAN(
1612 "Navigation.SecureSchemeHasSSLStatus.NewPagePendingEntryMatches",
1613 !!new_entry->GetSSL().certificate);
1614 }
creisf49dfc92016-07-26 17:05:181615 }
1616
Charlie Reisc0f17d2d2021-01-12 18:52:491617 // For cross-document commits with no matching pending entry, create a new
1618 // entry.
creisf49dfc92016-07-26 17:05:181619 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061620 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071621 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1622 initiator_origin,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:581623 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451624 params.transition, request->IsRendererInitiated(),
Lukasz Anforowicz435bcb582019-07-12 20:50:061625 nullptr); // blob_url_loader_factory
[email protected]f8f93eb2012-09-25 03:06:241626
1627 // Find out whether the new entry needs to update its virtual URL on URL
1628 // change and set up the entry accordingly. This is needed to correctly
1629 // update the virtual URL when replaceState is called after a pushState.
1630 GURL url = params.url;
1631 bool needs_update = false;
[email protected]47752982014-07-29 08:01:431632 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1633 &url, browser_context_, &needs_update);
[email protected]f8f93eb2012-09-25 03:06:241634 new_entry->set_update_virtual_url_with_url(needs_update);
1635
Charlie Reisc0f17d2d2021-01-12 18:52:491636 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411637 // update the virtual URL based on the new URL. For example, this is needed
1638 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1639 // the URL.
[email protected]f8f93eb2012-09-25 03:06:241640 update_virtual_url = needs_update;
Camille Lamy62b826012019-02-26 09:15:471641 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451642 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141643
John Abd-El-Malek380d3c5922017-09-08 00:20:311644 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121645 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141646 UMA_HISTOGRAM_BOOLEAN(
1647 "Navigation.SecureSchemeHasSSLStatus.NewPageNoMatchingEntry",
1648 !!new_entry->GetSSL().certificate);
1649 }
[email protected]e9ba4472008-09-14 15:42:431650 }
1651
Harkiran Bolaria59290d62021-03-17 01:53:011652 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
1653 // for prerendered contents, if any. This is because prerendering/activation
1654 // technically won't be creating a new document. Unlike BFCache, prerendering
1655 // creates a new NavigationEntry rather than using an existing one.
1656 if (!request->IsPrerenderedPageActivation()) {
1657 // Don't use the page type from the pending entry. Some interstitial page
1658 // may have set the type to interstitial. Once we commit, however, the page
1659 // type must always be normal or error.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121660 new_entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1661 : PAGE_TYPE_NORMAL);
Harkiran Bolaria59290d62021-03-17 01:53:011662 new_entry->SetURL(params.url);
1663 if (update_virtual_url)
1664 UpdateVirtualURLToURL(new_entry.get(), params.url);
1665 new_entry->SetReferrer(Referrer(*params.referrer));
1666 new_entry->SetTransitionType(params.transition);
1667 new_entry->set_site_instance(
1668 static_cast<SiteInstanceImpl*>(rfh->GetSiteInstance()));
1669 new_entry->SetOriginalRequestURL(request->GetOriginalRequestURL());
Rakina Zata Amnib597d632020-12-01 00:56:001670
Antonio Sartori4f5373792021-05-31 10:56:471671 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
Harkiran Bolaria59290d62021-03-17 01:53:011672 new_entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
[email protected]e9ba4472008-09-14 15:42:431673
Harkiran Bolaria59290d62021-03-17 01:53:011674 // Update the FrameNavigationEntry for new main frame commits.
1675 FrameNavigationEntry* frame_entry =
1676 new_entry->GetFrameEntry(rfh->frame_tree_node());
1677 frame_entry->set_frame_unique_name(rfh->frame_tree_node()->unique_name());
1678 frame_entry->set_item_sequence_number(params.item_sequence_number);
1679 frame_entry->set_document_sequence_number(params.document_sequence_number);
Nate Chapinfbfe5af2021-06-10 17:22:081680 frame_entry->set_app_history_key(params.app_history_key);
Harkiran Bolaria59290d62021-03-17 01:53:011681 frame_entry->set_redirect_chain(request->GetRedirectChain());
1682 frame_entry->SetPageState(params.page_state);
1683 frame_entry->set_method(params.method);
1684 frame_entry->set_post_id(params.post_id);
1685 frame_entry->set_policy_container_policies(
1686 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1687 request));
Antonio Sartori78a749f2020-11-30 12:03:391688
Anton Bikineevf62d1bf2021-05-15 17:56:071689 if (absl::optional<url::Origin> committed_origin =
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121690 GetCommittedOriginForFrameEntry(params, request)) {
1691 if (committed_origin.has_value())
1692 frame_entry->set_committed_origin(committed_origin.value());
1693 }
Harkiran Bolaria59290d62021-03-17 01:53:011694 if (request->web_bundle_navigation_info()) {
1695 frame_entry->set_web_bundle_navigation_info(
1696 request->web_bundle_navigation_info()->Clone());
1697 }
creis8b5cd4c2015-06-19 00:11:081698
Harkiran Bolaria59290d62021-03-17 01:53:011699 // history.pushState() is classified as a navigation to a new page, but sets
1700 // is_same_document to true. In this case, we already have the title and
1701 // favicon available, so set them immediately.
1702 if (is_same_document && GetLastCommittedEntry()) {
1703 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
1704 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1705 }
[email protected]3a868f212014-08-09 10:41:191706 }
[email protected]ff64b3e2014-05-31 04:07:331707
[email protected]60d6cca2013-04-30 08:47:131708 DCHECK(!params.history_list_was_cleared || !replace_entry);
1709 // The browser requested to clear the session history when it initiated the
1710 // navigation. Now we know that the renderer has updated its state accordingly
1711 // and it is safe to also clear the browser side history.
1712 if (params.history_list_was_cleared) {
arthursonzogni69a6a1b2019-09-17 09:23:001713 DiscardNonCommittedEntries();
[email protected]60d6cca2013-04-30 08:47:131714 entries_.clear();
1715 last_committed_entry_index_ = -1;
1716 }
1717
Nasko Oskovaee2f862018-06-15 00:05:521718 // If this is a new navigation with replacement and there is a
1719 // pending_entry_ which matches the navigation reported by the renderer
1720 // process, then it should be the one replaced, so update the
1721 // last_committed_entry_index_ to use it.
1722 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101723 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521724 last_committed_entry_index_ = pending_entry_index_;
1725 }
1726
Alexander Timine3ec4192020-04-20 16:39:401727 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411728 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401729 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051730
Carlos IL42b416592019-10-07 23:10:361731 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
1732 !request->post_commit_error_page_html().empty());
[email protected]e9ba4472008-09-14 15:42:431733}
1734
Charlie Reisc0f17d2d2021-01-12 18:52:491735void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321736 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071737 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361738 bool is_same_document,
jam48cea9082017-02-15 06:13:291739 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451740 NavigationRequest* request,
Peter Boströmd7592132019-01-30 04:50:311741 bool keep_pending_entry) {
creis26d22632017-04-21 20:23:561742 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1743 << "that a last committed entry exists.";
1744
[email protected]e9ba4472008-09-14 15:42:431745 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001746 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431747
Charlie Reis7e2cb6d2021-01-26 01:27:161748 NavigationEntryImpl* entry = nullptr;
avicbdc4c12015-07-01 16:07:111749 if (params.intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:161750 // We're guaranteed to have a last committed entry if intended_as_new_entry
1751 // is true.
avicbdc4c12015-07-01 16:07:111752 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:161753 DCHECK(entry);
1754
1755 // If the NavigationRequest matches a new pending entry and is classified as
1756 // EXISTING_ENTRY, then it is a navigation to the same URL that was
1757 // converted to a reload, such as a user pressing enter in the omnibox.
1758 if (pending_entry_ && pending_entry_index_ == -1 &&
1759 pending_entry_->GetUniqueID() ==
1760 request->commit_params().nav_entry_id) {
1761 // Note: The pending entry will usually have a real ReloadType here, but
1762 // it can still be ReloadType::NONE in cases that
1763 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
1764
1765 // If we classified this correctly, the SiteInstance should not have
1766 // changed.
1767 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
1768
1769 // For converted reloads, we assign the entry's unique ID to be that of
1770 // the new one. Since this is always the result of a user action, we want
1771 // to dismiss infobars, etc. like a regular user-initiated navigation.
1772 entry->set_unique_id(pending_entry_->GetUniqueID());
1773
1774 // The extra headers may have changed due to reloading with different
1775 // headers.
1776 entry->set_extra_headers(pending_entry_->extra_headers());
1777 }
1778 // Otherwise, this was intended as a new entry but the pending entry was
1779 // lost in the meantime and no new entry was created. We are stuck at the
1780 // last committed entry.
1781
1782 // Even if this is a converted reload from pressing enter in the omnibox,
1783 // the server could redirect, requiring an update to the SSL status. If this
1784 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451785 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:161786 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:471787 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451788 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:161789 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141790
Charlie Reis7e2cb6d2021-01-26 01:27:161791 if (params.url.SchemeIs(url::kHttpsScheme) &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121792 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141793 bool has_cert = !!entry->GetSSL().certificate;
1794 if (is_same_document) {
1795 UMA_HISTOGRAM_BOOLEAN(
1796 "Navigation.SecureSchemeHasSSLStatus."
1797 "ExistingPageSameDocumentIntendedAsNew",
1798 has_cert);
1799 } else {
1800 UMA_HISTOGRAM_BOOLEAN(
1801 "Navigation.SecureSchemeHasSSLStatus."
1802 "ExistingPageDifferentDocumentIntendedAsNew",
1803 has_cert);
1804 }
1805 }
Rakina Zata Amnif6950d552020-11-24 03:26:101806 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:111807 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:101808 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b902016-09-01 16:58:161809
eugenebut604866f2017-05-10 21:35:361810 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451811 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361812 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1813 // this was a restored same document navigation entry, then it won't have
1814 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1815 // navigated it.
jam48cea9082017-02-15 06:13:291816 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
1817 if (entry->GetURL().GetOrigin() == last_entry->GetURL().GetOrigin() &&
1818 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1819 was_restored) {
1820 entry->GetSSL() = last_entry->GetSSL();
1821 }
1822 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451823 // In rapid back/forward navigations |request| sometimes won't have a cert
1824 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191825 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451826 if (request->GetSSLInfo().has_value() &&
1827 request->GetSSLInfo()->is_valid()) {
1828 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
1829 } else if (entry->GetURL().GetOrigin() != request->GetURL().GetOrigin()) {
John Abd-El-Malek3f247082017-12-07 19:02:191830 entry->GetSSL() = SSLStatus();
1831 }
jam48cea9082017-02-15 06:13:291832 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141833
John Abd-El-Malek380d3c5922017-09-08 00:20:311834 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121835 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141836 bool has_cert = !!entry->GetSSL().certificate;
1837 if (is_same_document && was_restored) {
1838 UMA_HISTOGRAM_BOOLEAN(
1839 "Navigation.SecureSchemeHasSSLStatus."
1840 "ExistingPageSameDocumentRestoredBrowserInitiated",
1841 has_cert);
1842 } else if (is_same_document && !was_restored) {
1843 UMA_HISTOGRAM_BOOLEAN(
1844 "Navigation.SecureSchemeHasSSLStatus."
1845 "ExistingPageSameDocumentBrowserInitiated",
1846 has_cert);
1847 } else if (!is_same_document && was_restored) {
1848 UMA_HISTOGRAM_BOOLEAN(
1849 "Navigation.SecureSchemeHasSSLStatus."
1850 "ExistingPageRestoredBrowserInitiated",
1851 has_cert);
1852 } else {
1853 UMA_HISTOGRAM_BOOLEAN(
1854 "Navigation.SecureSchemeHasSSLStatus.ExistingPageBrowserInitiated",
1855 has_cert);
1856 }
1857 }
avicbdc4c12015-07-01 16:07:111858 } else {
1859 // This is renderer-initiated. The only kinds of renderer-initated
Charlie Reisc0f17d2d2021-01-12 18:52:491860 // navigations that are EXISTING_ENTRY are reloads and history.replaceState,
avicbdc4c12015-07-01 16:07:111861 // which land us at the last committed entry.
1862 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101863
Mikel Astizba9cf2fd2017-12-17 10:38:101864 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1865 // to avoid misleading interpretations (e.g. URLs paired with
1866 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1867 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1868 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1869
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571870 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:101871
eugenebut604866f2017-05-10 21:35:361872 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451873 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361874 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471875 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451876 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141877
John Abd-El-Malek380d3c5922017-09-08 00:20:311878 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121879 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141880 bool has_cert = !!entry->GetSSL().certificate;
1881 if (is_same_document) {
1882 UMA_HISTOGRAM_BOOLEAN(
1883 "Navigation.SecureSchemeHasSSLStatus."
1884 "ExistingPageSameDocumentRendererInitiated",
1885 has_cert);
1886 } else {
1887 UMA_HISTOGRAM_BOOLEAN(
1888 "Navigation.SecureSchemeHasSSLStatus."
1889 "ExistingPageDifferentDocumentRendererInitiated",
1890 has_cert);
1891 }
1892 }
avicbdc4c12015-07-01 16:07:111893 }
1894 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:431895
[email protected]5ccd4dc2012-10-24 02:28:141896 // The URL may have changed due to redirects.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121897 entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1898 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041899 entry->SetURL(params.url);
arthursonzogni73fe3212020-11-17 13:24:071900 entry->SetReferrer(Referrer(*params.referrer));
[email protected]38178a42009-12-17 18:58:321901 if (entry->update_virtual_url_with_url())
1902 UpdateVirtualURLToURL(entry, params.url);
[email protected]5ccd4dc2012-10-24 02:28:141903
jam015ba062017-01-06 21:17:001904 // The site instance will normally be the same except
1905 // 1) session restore, when no site instance will be assigned or
1906 // 2) redirect, when the site instance is reset.
Lei Zhang96031532019-10-10 19:05:471907 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
naskoaf182192016-08-11 02:12:011908 entry->site_instance() == rfh->GetSiteInstance());
clamy432acb22016-04-15 19:41:431909
naskoaf182192016-08-11 02:12:011910 // Update the existing FrameNavigationEntry to ensure all of its members
1911 // reflect the parameters coming from the renderer process.
Anton Bikineevf62d1bf2021-05-15 17:56:071912 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451913 request->common_params().initiator_origin;
naskoaf182192016-08-11 02:12:011914 entry->AddOrUpdateFrameEntry(
Nate Chapin9f169072021-06-09 19:32:371915 rfh->frame_tree_node(), NavigationEntryImpl::UpdatePolicy::kUpdate,
1916 params.item_sequence_number, params.document_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081917 params.app_history_key, rfh->GetSiteInstance(), nullptr, params.url,
Nate Chapin9f169072021-06-09 19:32:371918 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091919 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071920 params.page_state, params.method, params.post_id,
1921 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031922 request->web_bundle_navigation_info()
1923 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391924 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481925 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191926 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1927 request));
creis22a7b4c2016-04-28 07:20:301928
[email protected]5ccd4dc2012-10-24 02:28:141929 // The redirected to page should not inherit the favicon from the previous
1930 // page.
eugenebut604866f2017-05-10 21:35:361931 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:481932 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:141933
Peter Boströmd7592132019-01-30 04:50:311934 // We should also usually discard the pending entry if it corresponds to a
1935 // different navigation, since that one is now likely canceled. In rare
1936 // cases, we leave the pending entry for another navigation in place when we
1937 // know it is still ongoing, to avoid a flicker in the omnibox (see
1938 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:431939 //
1940 // Note that we need to use the "internal" version since we don't want to
1941 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:311942 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001943 DiscardNonCommittedEntries();
[email protected]40bcc302009-03-02 20:50:391944
Carlos IL4dea8902020-05-26 15:14:291945 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:111946 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:431947}
1948
[email protected]d202a7c2012-01-04 07:53:471949void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:321950 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071951 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361952 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:471953 bool replace_entry,
1954 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451955 NavigationRequest* request) {
avi25f5f9e2015-07-17 20:08:261956 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1957 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
[email protected]09b8f82f2009-06-16 20:22:111958
[email protected]e9ba4472008-09-14 15:42:431959 // Manual subframe navigations just get the current entry cloned so the user
1960 // can go back or forward to it. The actual subframe information will be
1961 // stored in the page state for each of those entries. This happens out of
1962 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:091963 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1964 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:381965
Mikel Astizba9cf2fd2017-12-17 10:38:101966 // The DCHECK below documents the fact that we don't know of any situation
1967 // where |replace_entry| is true for subframe navigations. This simplifies
1968 // reasoning about the replacement struct for subframes (see
1969 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
1970 DCHECK(!replace_entry);
1971
Patrick Monette50e8bd82019-06-13 22:40:451972 // This FrameNavigationEntry might not end up being used in the
1973 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Anton Bikineevf62d1bf2021-05-15 17:56:071974 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451975 request->common_params().initiator_origin;
Patrick Monette50e8bd82019-06-13 22:40:451976 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481977 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081978 params.document_sequence_number, params.app_history_key,
1979 rfh->GetSiteInstance(), nullptr, params.url,
1980 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091981 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071982 params.page_state, params.method, params.post_id,
1983 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031984 request->web_bundle_navigation_info()
1985 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391986 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481987 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191988 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1989 request));
Charles Reisf44482022017-10-13 21:15:031990
creisce0ef3572017-01-26 17:53:081991 std::unique_ptr<NavigationEntryImpl> new_entry =
1992 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:451993 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:001994 frame_tree_.root());
creise062d542015-08-25 02:01:551995
Alexander Timine3ec4192020-04-20 16:39:401996 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411997 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401998 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:471999
creisce0ef3572017-01-26 17:53:082000 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:452001 // to replace, which can happen due to a unique name change. See
2002 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
2003 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:382004
Carlos IL42b416592019-10-07 23:10:362005 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false);
[email protected]e9ba4472008-09-14 15:42:432006}
2007
[email protected]d202a7c2012-01-04 07:53:472008bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:322009 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072010 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392011 bool is_same_document,
Mohamed Abdelhalim833de902019-09-16 17:41:452012 NavigationRequest* request) {
avi9f07a0c2015-02-18 22:51:292013 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2014 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2015
[email protected]e9ba4472008-09-14 15:42:432016 // We're guaranteed to have a previously committed entry, and we now need to
2017 // handle navigation inside of a subframe in it without creating a new entry.
2018 DCHECK(GetLastCommittedEntry());
2019
creis913c63ce2016-07-16 19:52:522020 // For newly created subframes, we don't need to send a commit notification.
2021 // This is only necessary for history navigations in subframes.
2022 bool send_commit_notification = false;
2023
Rakina Zata Amnif6950d552020-11-24 03:26:102024 // If |nav_entry_id| is non-zero and matches an existing entry, this
2025 // is a history navigation. Update the last committed index accordingly. If
2026 // we don't recognize the |nav_entry_id|, it might be a recently
2027 // pruned entry. We'll handle it below.
2028 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2029 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472030 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062031 // Make sure that a subframe commit isn't changing the main frame's
2032 // origin. Otherwise the renderer process may be confused, leading to a
2033 // URL spoof. We can't check the path since that may change
2034 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572035 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2036 // about:blank, file, and unique origins are more subtle to get right.
Charlie Reis95fbca442021-05-21 21:38:242037 // We should use checks similar to RenderFrameHostImpl's
2038 // CanCommitUrlAndOrigin on the main frame during subframe commits.
2039 // See https://crbug.com/1209092.
creis37988b92016-06-10 18:03:572040 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2041 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2042 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2043 dest_top_url.SchemeIsHTTPOrHTTPS() &&
2044 current_top_url.GetOrigin() != dest_top_url.GetOrigin()) {
creisfb6eeb62016-05-10 19:01:512045 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2046 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062047 }
creis3cdc3b02015-05-29 23:00:472048
creis913c63ce2016-07-16 19:52:522049 // We only need to discard the pending entry in this history navigation
2050 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062051 last_committed_entry_index_ = entry_index;
arthursonzogni69a6a1b2019-09-17 09:23:002052 DiscardNonCommittedEntries();
creis913c63ce2016-07-16 19:52:522053
2054 // History navigations should send a commit notification.
2055 send_commit_notification = true;
avi98405c22015-05-21 20:47:062056 }
[email protected]e9ba4472008-09-14 15:42:432057 }
[email protected]f233e4232013-02-23 00:55:142058
creisce0ef3572017-01-26 17:53:082059 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2060 // it may be a "history auto" case where we update an existing one.
2061 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Nate Chapin9f169072021-06-09 19:32:372062
2063 // We may want to update |last_committed|'s FrameNavigationEntry (if one
2064 // exists), or we may want to clobber it and create a new one. We update in
2065 // cases where the corresponding FrameNavigationEntry is conceptually similar
2066 // to the document described by the commit params: same-document
2067 // navigations, history traversal to an existing entry, and reloads (including
2068 // a "soft reload" where we navigate to the same url without flagging it as a
2069 // reload). But in the case of a different document that is not logically
2070 // related to the committed FrameNavigationEntry's document (cross-document,
2071 // not same url, not a reload, not a history traversal), we replace rather
2072 // than update.
2073 // In the case where we update, the FrameNavigationEntry will potentially be
2074 // shared across multiple NavigationEntries, and any updates will be reflected
2075 // in all of those NavigationEntries. In the replace case, any existing
2076 // sharing with other NavigationEntries will stop.
2077 FrameNavigationEntry* last_committed_frame_entry =
2078 last_committed->GetFrameEntry(rfh->frame_tree_node());
2079 NavigationEntryImpl::UpdatePolicy update_policy =
2080 NavigationEntryImpl::UpdatePolicy::kUpdate;
2081 if (request->common_params().navigation_type ==
2082 mojom::NavigationType::DIFFERENT_DOCUMENT &&
2083 last_committed_frame_entry &&
2084 last_committed_frame_entry->url() != params.url) {
2085 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_,
Scott Violetcf6ea7e2021-06-09 21:09:212435 nullptr /* blob_url_loader_factory */, should_replace_current_entry));
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_,
Scott Violetcf6ea7e2021-06-09 21:09:212456 blob_url_loader_factory, should_replace_current_entry));
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.
2582 DCHECK_EQ(partition_config, it->first.config());
2583 if (partition_config != it->first.config()) {
2584 LogStoragePartitionIdCrashKeys(it->first, partition_id);
2585 }
2586
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) {
[email protected]ec6c05f2013-10-23 18:41:572762 delegate_->Stop();
[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 }
Lei Zhang96031532019-10-10 19:05:473044
Alex Moshchuk3a4e77a2020-05-29 21:32:573045 // TODO(avi, creis): This is a bug; we should not return here. Rather, we
3046 // should continue on and navigate all child frames which have also
3047 // changed. This bug is the cause of <https://crbug.com/542299>, which is
3048 // a NC_IN_PAGE_NAVIGATION renderer kill.
3049 //
3050 // However, this bug is a bandaid over a deeper and worse problem. Doing a
3051 // pushState immediately after loading a subframe is a race, one that no
3052 // web page author expects. If we fix this bug, many large websites break.
3053 // For example, see <https://crbug.com/598043> and the spec discussion at
3054 // <https://github.com/whatwg/html/issues/1191>.
3055 //
3056 // For now, we accept this bug, and hope to resolve the race in a
3057 // different way that will one day allow us to fix this.
3058 return;
3059 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473060 std::unique_ptr<NavigationRequest> navigation_request =
3061 CreateNavigationRequestFromEntry(
3062 frame, pending_entry_, new_item, reload_type,
3063 false /* is_same_document_history_load */,
3064 false /* is_history_navigation_in_new_child */);
3065 if (navigation_request) {
3066 // Only add the request if was properly created. It's possible for the
3067 // creation to fail in certain cases, e.g. when the URL is invalid.
3068 different_document_loads->push_back(std::move(navigation_request));
3069 }
3070 // For a different document, the subframes will be destroyed, so there's
3071 // no need to consider them.
3072 return;
Alex Moshchuke65c39272020-06-03 17:55:373073 } else if (action == HistoryNavigationAction::kStopLooking) {
3074 return;
creis4e2ecb72015-06-20 00:46:303075 }
3076
3077 for (size_t i = 0; i < frame->child_count(); i++) {
clamy3cb9bea92018-07-10 12:42:023078 FindFramesToNavigate(frame->child_at(i), reload_type, same_document_loads,
creis4e2ecb72015-06-20 00:46:303079 different_document_loads);
3080 }
3081}
3082
Harkiran Bolariaba823e42021-05-21 18:30:363083base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry(
clamy21718cc22018-06-13 13:34:243084 const LoadURLParams& params) {
3085 // Find the appropriate FrameTreeNode.
3086 FrameTreeNode* node = nullptr;
3087 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3088 !params.frame_name.empty()) {
3089 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003090 ? frame_tree_.FindByID(params.frame_tree_node_id)
3091 : frame_tree_.FindByName(params.frame_name);
clamy21718cc22018-06-13 13:34:243092 }
3093
3094 // If no FrameTreeNode was specified, navigate the main frame.
3095 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003096 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243097
Camille Lamy5193caa2018-10-12 11:59:423098 // Compute overrides to the LoadURLParams for |override_user_agent|,
3099 // |should_replace_current_entry| and |has_user_gesture| that will be used
3100 // both in the creation of the NavigationEntry and the NavigationRequest.
3101 // Ideally, the LoadURLParams themselves would be updated, but since they are
3102 // passed as a const reference, this is not possible.
3103 // TODO(clamy): When we only create a NavigationRequest, move this to
3104 // CreateNavigationRequestFromLoadURLParams.
3105 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3106 GetLastCommittedEntry());
3107
3108 // Don't allow an entry replacement if there is no entry to replace.
3109 // http://crbug.com/457149
3110 bool should_replace_current_entry =
3111 params.should_replace_current_entry && entries_.size();
3112
clamy21718cc22018-06-13 13:34:243113 // Javascript URLs should not create NavigationEntries. All other navigations
3114 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243115 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373116 std::unique_ptr<NavigationEntryImpl> entry =
3117 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3118 should_replace_current_entry,
3119 params.has_user_gesture);
clamy21718cc22018-06-13 13:34:243120 DiscardPendingEntry(false);
3121 SetPendingEntry(std::move(entry));
3122 }
3123
3124 // Renderer-debug URLs are sent to the renderer process immediately for
3125 // processing and don't need to create a NavigationRequest.
3126 // Note: this includes navigations to JavaScript URLs, which are considered
3127 // renderer-debug URLs.
3128 // Note: we intentionally leave the pending entry in place for renderer debug
3129 // URLs, unlike the cases below where we clear it if the navigation doesn't
3130 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303131 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483132 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3133 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153134 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483135 params.url, browser_context_)) {
3136 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363137 return nullptr;
Oleg Davydov2cc0167b2019-02-05 14:32:483138 }
3139
clamy21718cc22018-06-13 13:34:243140 HandleRendererDebugURL(node, params.url);
Harkiran Bolariaba823e42021-05-21 18:30:363141 return nullptr;
clamy21718cc22018-06-13 13:34:243142 }
3143
Antonio Sartori78a749f2020-11-30 12:03:393144 DCHECK(pending_entry_);
3145
clamy21718cc22018-06-13 13:34:243146 // Convert navigations to the current URL to a reload.
3147 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3148 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3149 // DevTools sometimes issues navigations to main frames that they do not
3150 // expect to see treated as reload, and it only works because they pass a
3151 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3152 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543153 ReloadType reload_type = params.reload_type;
3154 if (reload_type == ReloadType::NONE &&
3155 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043156 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243157 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243158 params.load_type ==
3159 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
Rakina Zata Amni312822d72021-06-04 16:13:373160 params.should_replace_current_entry, GetLastCommittedEntry())) {
clamy21718cc22018-06-13 13:34:243161 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473162 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393163
3164 // If this is a reload of an existing FrameNavigationEntry and we had a
3165 // policy container for it, then we should copy it into the pending entry,
3166 // so that it is copied to the navigation request in
3167 // CreateNavigationRequestFromLoadParams later.
3168 if (GetLastCommittedEntry()) {
3169 FrameNavigationEntry* previous_frame_entry =
3170 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193171 if (previous_frame_entry &&
3172 previous_frame_entry->policy_container_policies()) {
3173 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163174 previous_frame_entry->policy_container_policies()->Clone());
Antonio Sartori78a749f2020-11-30 12:03:393175 }
3176 }
3177 }
3178
3179 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
3180 // document (so no last commit), then we should copy the document polices from
3181 // RenderFrameHost's PolicyContainerHost. The NavigationRequest will create a
3182 // new PolicyContainerHost with the document policies from the
3183 // |pending_entry_|, and that PolicyContainerHost will be put in the final
3184 // RenderFrameHost for the navigation. This way, we ensure that we keep
3185 // enforcing the right policies on the initial empty document after the
3186 // reload.
3187 if (!GetLastCommittedEntry() && params.url.IsAboutBlank()) {
3188 if (node->current_frame_host() &&
3189 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193190 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163191 node->current_frame_host()
3192 ->policy_container_host()
3193 ->policies()
3194 .Clone());
Antonio Sartori78a749f2020-11-30 12:03:393195 }
clamy21718cc22018-06-13 13:34:243196 }
3197
3198 // navigation_ui_data should only be present for main frame navigations.
3199 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
3200
Camille Lamy5193caa2018-10-12 11:59:423201 std::unique_ptr<NavigationRequest> request =
3202 CreateNavigationRequestFromLoadParams(
3203 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143204 params.has_user_gesture, network::mojom::SourceLocation::New(),
3205 blink::NavigationDownloadPolicy(), reload_type, pending_entry_,
Scott Violetcf6ea7e2021-06-09 21:09:213206 pending_entry_->GetFrameEntry(node));
clamy21718cc22018-06-13 13:34:243207
3208 // If the navigation couldn't start, return immediately and discard the
3209 // pending NavigationEntry.
3210 if (!request) {
3211 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363212 return nullptr;
clamy21718cc22018-06-13 13:34:243213 }
3214
Camille Lamy5193caa2018-10-12 11:59:423215#if DCHECK_IS_ON()
3216 // Safety check that NavigationRequest and NavigationEntry match.
3217 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3218#endif
3219
clamy21718cc22018-06-13 13:34:243220 // This call does not support re-entrancy. See http://crbug.com/347742.
3221 CHECK(!in_navigate_to_pending_entry_);
3222 in_navigate_to_pending_entry_ = true;
3223
arthursonzogni66f711c2019-10-08 14:40:363224 // It is not possible to delete the pending NavigationEntry while navigating
3225 // to it. Grab a reference to delay potential deletion until the end of this
3226 // function.
3227 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3228
Harkiran Bolariaba823e42021-05-21 18:30:363229 base::WeakPtr<NavigationHandle> created_navigation_handle(
3230 request->GetWeakPtr());
Lukasz Anforowicz9ee83c272020-12-01 20:14:053231 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243232
3233 in_navigate_to_pending_entry_ = false;
Harkiran Bolariaba823e42021-05-21 18:30:363234 return created_navigation_handle;
clamy21718cc22018-06-13 13:34:243235}
3236
clamyea99ea12018-05-28 13:54:233237void NavigationControllerImpl::HandleRendererDebugURL(
3238 FrameTreeNode* frame_tree_node,
3239 const GURL& url) {
3240 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243241 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3242 // the renderer process is not live, unless it is the initial navigation
3243 // of the tab.
clamyea99ea12018-05-28 13:54:233244 if (!IsInitialNavigation()) {
3245 DiscardNonCommittedEntries();
3246 return;
3247 }
Fergal Dalyecd3b0202020-06-25 01:57:373248 // The current frame is always a main frame. If IsInitialNavigation() is
3249 // true then there have been no navigations and any frames of this tab must
3250 // be in the same renderer process. If that has crashed then the only frame
3251 // that can be revived is the main frame.
3252 frame_tree_node->render_manager()
3253 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233254 }
3255 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3256}
3257
clamy21718cc22018-06-13 13:34:243258std::unique_ptr<NavigationEntryImpl>
3259NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3260 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423261 const LoadURLParams& params,
3262 bool override_user_agent,
3263 bool should_replace_current_entry,
3264 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393265 // Browser initiated navigations might not have a blob_url_loader_factory set
3266 // in params even if the navigation is to a blob URL. If that happens, lookup
3267 // the correct url loader factory to use here.
3268 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483269 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473270 // Resolve the blob URL in the storage partition associated with the target
3271 // frame. This is the storage partition the URL will be loaded in, and only
3272 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393273 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473274 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393275 }
3276
clamy21718cc22018-06-13 13:34:243277 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443278 // extra_headers in params are \n separated; navigation entries want \r\n.
3279 std::string extra_headers_crlf;
3280 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243281
3282 // For subframes, create a pending entry with a corresponding frame entry.
3283 if (!node->IsMainFrame()) {
Tommy C. Li03eee77a2019-02-05 02:07:443284 if (GetLastCommittedEntry()) {
Tommy C. Li03eee77a2019-02-05 02:07:443285 entry = GetLastCommittedEntry()->Clone();
3286 } else {
3287 // If there's no last committed entry, create an entry for about:blank
3288 // with a subframe entry for our destination.
3289 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
3290 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063291 GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103292 params.source_site_instance.get(), params.transition_type,
3293 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Scott Violetcf6ea7e2021-06-09 21:09:213294 blob_url_loader_factory, should_replace_current_entry));
Tommy C. Li03eee77a2019-02-05 02:07:443295 }
Nasko Oskov18006bc2018-12-06 02:53:583296
clamy21718cc22018-06-13 13:34:243297 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:083298 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
clamy21718cc22018-06-13 13:34:243299 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Anton Bikineevf62d1bf2021-05-15 17:56:073300 params.url, absl::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203301 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393302 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:483303 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori78a749f2020-11-30 12:03:393304 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163305 // a conversion of a new navigation into a reload, we will set the right
3306 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193307 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243308 } else {
3309 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243310 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063311 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103312 params.source_site_instance.get(), params.transition_type,
3313 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Scott Violetcf6ea7e2021-06-09 21:09:213314 blob_url_loader_factory, should_replace_current_entry));
clamy21718cc22018-06-13 13:34:243315 entry->set_source_site_instance(
3316 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3317 entry->SetRedirectChain(params.redirect_chain);
3318 }
3319
3320 // Set the FTN ID (only used in non-site-per-process, for tests).
3321 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243322 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423323 entry->SetIsOverridingUserAgent(override_user_agent);
3324 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543325 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243326
clamy21718cc22018-06-13 13:34:243327 switch (params.load_type) {
3328 case LOAD_TYPE_DEFAULT:
3329 break;
3330 case LOAD_TYPE_HTTP_POST:
3331 entry->SetHasPostData(true);
3332 entry->SetPostData(params.post_data);
3333 break;
3334 case LOAD_TYPE_DATA:
3335 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3336 entry->SetVirtualURL(params.virtual_url_for_data_url);
3337#if defined(OS_ANDROID)
3338 entry->SetDataURLAsString(params.data_url_as_string);
3339#endif
3340 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3341 break;
clamy21718cc22018-06-13 13:34:243342 }
3343
3344 // TODO(clamy): NavigationEntry is meant for information that will be kept
3345 // after the navigation ended and therefore is not appropriate for
3346 // started_from_context_menu. Move started_from_context_menu to
3347 // NavigationUIData.
3348 entry->set_started_from_context_menu(params.started_from_context_menu);
3349
3350 return entry;
3351}
3352
clamyea99ea12018-05-28 13:54:233353std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423354NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3355 FrameTreeNode* node,
3356 const LoadURLParams& params,
3357 bool override_user_agent,
3358 bool should_replace_current_entry,
3359 bool has_user_gesture,
Antonio Sartori2f763d9d2021-04-21 10:04:143360 network::mojom::SourceLocationPtr source_location,
Yeunjoo Choi3df791a2021-02-17 07:07:253361 blink::NavigationDownloadPolicy download_policy,
Camille Lamy5193caa2018-10-12 11:59:423362 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573363 NavigationEntryImpl* entry,
Scott Violetcf6ea7e2021-06-09 21:09:213364 FrameNavigationEntry* frame_entry) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573365 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283366 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533367 // All renderer-initiated navigations must have an initiator_origin.
3368 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513369
Camille Lamy5193caa2018-10-12 11:59:423370 GURL url_to_load;
3371 GURL virtual_url;
Anton Bikineevf62d1bf2021-05-15 17:56:073372 absl::optional<url::Origin> origin_to_commit =
3373 frame_entry ? frame_entry->committed_origin() : absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:323374
Camille Lamy2baa8022018-10-19 16:43:173375 // For main frames, rewrite the URL if necessary and compute the virtual URL
3376 // that should be shown in the address bar.
3377 if (node->IsMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423378 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173379 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423380 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423381
Camille Lamy2baa8022018-10-19 16:43:173382 // For DATA loads, override the virtual URL.
3383 if (params.load_type == LOAD_TYPE_DATA)
3384 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423385
Camille Lamy2baa8022018-10-19 16:43:173386 if (virtual_url.is_empty())
3387 virtual_url = url_to_load;
3388
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573389 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283390
Aran Gilman249eb122019-12-02 23:32:463391 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3392 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3393 // return different results, leading to a different URL in the
3394 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3395 // pending NavigationEntry, as we'll only make one call to
3396 // RewriteUrlForNavigation.
3397 VLOG_IF(1, (url_to_load != frame_entry->url()))
3398 << "NavigationRequest and FrameEntry have different URLs: "
3399 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283400
Camille Lamy2baa8022018-10-19 16:43:173401 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423402 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173403 // NavigationRequest.
3404 } else {
3405 url_to_load = params.url;
3406 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243407 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173408 }
Camille Lamy5193caa2018-10-12 11:59:423409
Ehsan Karamad44fc72112019-02-26 18:15:473410 if (node->render_manager()->is_attaching_inner_delegate()) {
3411 // Avoid starting any new navigations since this node is now preparing for
3412 // attaching an inner delegate.
3413 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203414 }
Camille Lamy5193caa2018-10-12 11:59:423415
Camille Lamy5193caa2018-10-12 11:59:423416 if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load))
3417 return nullptr;
3418
Kunihiko Sakamoto346a74e2021-03-10 08:57:483419 if (!DoesURLMatchOriginForNavigation(
3420 url_to_load, origin_to_commit,
3421 frame_entry->subresource_web_bundle_navigation_info())) {
Nasko Oskov03912102019-01-11 00:21:323422 DCHECK(false) << " url:" << url_to_load
3423 << " origin:" << origin_to_commit.value();
3424 return nullptr;
3425 }
3426
Camille Lamy5193caa2018-10-12 11:59:423427 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403428 blink::PreviewsState previews_state =
3429 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
Camille Lamy5193caa2018-10-12 11:59:423430 if (!node->IsMainFrame()) {
3431 // For subframes, use the state of the top-level frame.
3432 previews_state = node->frame_tree()
3433 ->root()
3434 ->current_frame_host()
3435 ->last_navigation_previews_state();
3436 }
3437
Camille Lamy5193caa2018-10-12 11:59:423438 // This will be used to set the Navigation Timing API navigationStart
3439 // parameter for browser navigations in new tabs (intents, tabs opened through
3440 // "Open link in new tab"). If the navigation must wait on the current
3441 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3442 // will be updated when the BeforeUnload ack is received.
3443 base::TimeTicks navigation_start = base::TimeTicks::Now();
3444
danakjd83d706d2020-11-25 22:11:123445 // Look for a pending commit that is to another document in this
3446 // FrameTreeNode. If one exists, then the last committed URL will not be the
3447 // current URL by the time this navigation commits.
3448 bool has_pending_cross_document_commit =
3449 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493450 bool is_currently_error_page = node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123451
danakjb952ef12021-01-14 19:58:493452 mojom::NavigationType navigation_type = GetNavigationType(
3453 /*old_url=*/node->current_url(),
3454 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3455 has_pending_cross_document_commit, is_currently_error_page,
3456 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423457
3458 // Create the NavigationParams based on |params|.
3459
Hiroki Nakagawa4ed61282021-06-18 05:37:233460 bool is_view_source_mode = entry->IsViewSourceMode();
3461 DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme));
Charlie Harrison8c113a32019-01-07 16:08:293462
Hiroki Nakagawa4ed61282021-06-18 05:37:233463 // Update |download_policy| if the virtual URL is view-source.
Charlie Harrison8c113a32019-01-07 16:08:293464 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253465 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293466
Camille Lamy5193caa2018-10-12 11:59:423467 const GURL& history_url_for_data_url =
3468 params.base_url_for_data_url.is_empty() ? GURL() : virtual_url;
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513469 mojom::CommonNavigationParamsPtr common_params =
3470 mojom::CommonNavigationParams::New(
3471 url_to_load, params.initiator_origin,
3472 blink::mojom::Referrer::New(params.referrer.url,
3473 params.referrer.policy),
Scott Violetcf6ea7e2021-06-09 21:09:213474 params.transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513475 should_replace_current_entry, params.base_url_for_data_url,
3476 history_url_for_data_url, previews_state, navigation_start,
3477 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
Antonio Sartori2f763d9d2021-04-21 10:04:143478 params.post_data, std::move(source_location),
arthursonzogniaf7c62c52020-02-12 10:49:413479 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273480 false /* has_text_fragment_token */,
3481 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3482 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513483 false /* is_history_navigation_in_new_child_frame */,
Charlie Hu5ffc0152019-12-06 15:59:533484 params.input_start);
Camille Lamy5193caa2018-10-12 11:59:423485
Lucas Furukawa Gadania9c45682019-07-31 22:05:143486 mojom::CommitNavigationParamsPtr commit_params =
3487 mojom::CommitNavigationParams::New(
arthursonzognid5a8d0b2021-03-11 17:36:433488 frame_entry->committed_origin(), network::mojom::WebSandboxFlags(),
3489 override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323490 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143491 std::vector<net::RedirectInfo>(),
3492 std::string() /* post_content_type */, common_params->url,
3493 common_params->method, params.can_load_local_resources,
3494 frame_entry->page_state(), entry->GetUniqueID(),
3495 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3496 -1 /* pending_history_list_offset */,
3497 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3498 params.should_clear_history_list ? 0 : GetEntryCount(),
3499 false /* was_discarded */, is_view_source_mode,
3500 params.should_clear_history_list, mojom::NavigationTiming::New(),
Anton Bikineevf62d1bf2021-05-15 17:56:073501 absl::nullopt /* appcache_host_id */,
Minggang Wangf59db47b2021-06-16 01:56:223502 blink::mojom::WasActivatedOption::kUnknown,
Lucas Furukawa Gadania9c45682019-07-31 22:05:143503 base::UnguessableToken::Create() /* navigation_token */,
Minggang Wang7ee0c742021-06-16 16:16:513504 std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143505#if defined(OS_ANDROID)
3506 std::string(), /* data_url_as_string */
3507#endif
arthursonzogni14379782020-05-15 09:09:273508 !params.is_renderer_initiated, /* is_browser_initiated */
Sreeja Kamishetty46f762c2021-02-05 07:52:463509 node->frame_tree()->is_prerendering() /* is_prerendering */,
Tsuyoshi Horoe86d7702019-11-29 01:52:473510 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533511 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173512 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073513 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163514 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333515 false /* origin_agent_cluster */,
Maks Orlovichc66745a2020-06-30 17:40:023516 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213517 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Nate Chapind1fe3612021-04-16 20:45:573518 false /* is_cross_browsing_instance */, nullptr /* old_page_info */,
3519 -1 /* http_response_code */,
3520 std::vector<
3521 mojom::AppHistoryEntryPtr>() /* app_history_back_entries */,
3522 std::vector<
Kenichi Ishibashi17ecfb62021-06-21 03:31:153523 mojom::AppHistoryEntryPtr>() /* app_history_forward_entries */,
3524 std::vector<GURL>() /* early_hints_preloaded_resources */);
Camille Lamy5193caa2018-10-12 11:59:423525#if defined(OS_ANDROID)
3526 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143527 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423528 }
3529#endif
3530
Lucas Furukawa Gadania9c45682019-07-31 22:05:143531 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423532
3533 // A form submission may happen here if the navigation is a renderer-initiated
3534 // form submission that took the OpenURL path.
3535 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3536
3537 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3538 std::string extra_headers_crlf;
3539 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093540
3541 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143542 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083543 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453544 params.initiator_frame_token.has_value()
3545 ? &(params.initiator_frame_token.value())
3546 : nullptr,
3547 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3548 request_body,
John Delaney50425f82020-04-07 16:26:213549 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
3550 params.impression);
Yao Xiaodc5ed102019-06-04 19:19:093551 navigation_request->set_from_download_cross_origin_redirect(
3552 params.from_download_cross_origin_redirect);
3553 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423554}
3555
3556std::unique_ptr<NavigationRequest>
3557NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233558 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573559 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233560 FrameNavigationEntry* frame_entry,
3561 ReloadType reload_type,
3562 bool is_same_document_history_load,
Arthur Hemerybee4a752019-05-29 10:50:553563 bool is_history_navigation_in_new_child_frame) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343564 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233565 GURL dest_url = frame_entry->url();
Anton Bikineevf62d1bf2021-05-15 17:56:073566 absl::optional<url::Origin> origin_to_commit =
Nasko Oskov03912102019-01-11 00:21:323567 frame_entry->committed_origin();
3568
clamyea99ea12018-05-28 13:54:233569 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013570 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573571 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233572 // We may have been redirected when navigating to the current URL.
3573 // Use the URL the user originally intended to visit as signaled by the
3574 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013575 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573576 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233577 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323578 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233579 }
3580
Ehsan Karamad44fc72112019-02-26 18:15:473581 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3582 // Avoid starting any new navigations since this node is now preparing for
3583 // attaching an inner delegate.
3584 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203585 }
3586
Camille Lamy5193caa2018-10-12 11:59:423587 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573588 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233589 return nullptr;
3590 }
3591
Kunihiko Sakamoto346a74e2021-03-10 08:57:483592 if (!DoesURLMatchOriginForNavigation(
3593 dest_url, origin_to_commit,
3594 frame_entry->subresource_web_bundle_navigation_info())) {
3595 DCHECK(false) << " url:" << dest_url
3596 << " origin:" << origin_to_commit.value();
Nasko Oskov03912102019-01-11 00:21:323597 return nullptr;
3598 }
3599
clamyea99ea12018-05-28 13:54:233600 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403601 blink::PreviewsState previews_state =
3602 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
clamyea99ea12018-05-28 13:54:233603 if (!frame_tree_node->IsMainFrame()) {
3604 // For subframes, use the state of the top-level frame.
3605 previews_state = frame_tree_node->frame_tree()
3606 ->root()
3607 ->current_frame_host()
3608 ->last_navigation_previews_state();
clamyea99ea12018-05-28 13:54:233609 }
3610
clamyea99ea12018-05-28 13:54:233611 // This will be used to set the Navigation Timing API navigationStart
3612 // parameter for browser navigations in new tabs (intents, tabs opened through
3613 // "Open link in new tab"). If the navigation must wait on the current
3614 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3615 // will be updated when the BeforeUnload ack is received.
3616 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233617
danakjd83d706d2020-11-25 22:11:123618 // Look for a pending commit that is to another document in this
3619 // FrameTreeNode. If one exists, then the last committed URL will not be the
3620 // current URL by the time this navigation commits.
3621 bool has_pending_cross_document_commit =
3622 frame_tree_node->render_manager()
3623 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493624 bool is_currently_error_page =
3625 frame_tree_node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123626
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513627 mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123628 /*old_url=*/frame_tree_node->current_url(),
3629 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493630 has_pending_cross_document_commit, is_currently_error_page,
3631 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423632
3633 // A form submission may happen here if the navigation is a
3634 // back/forward/reload navigation that does a form resubmission.
3635 scoped_refptr<network::ResourceRequestBody> request_body;
3636 std::string post_content_type;
3637 if (frame_entry->method() == "POST") {
3638 request_body = frame_entry->GetPostData(&post_content_type);
3639 // Might have a LF at end.
Peter Kastingb53b81912021-04-28 19:23:303640 post_content_type = std::string(
3641 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL));
Camille Lamy5193caa2018-10-12 11:59:423642 }
3643
3644 // Create the NavigationParams based on |entry| and |frame_entry|.
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513645 mojom::CommonNavigationParamsPtr common_params =
3646 entry->ConstructCommonNavigationParams(
3647 *frame_entry, request_body, dest_url,
3648 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
3649 navigation_type, previews_state, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533650 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513651 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553652 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423653
3654 // TODO(clamy): |intended_as_new_entry| below should always be false once
3655 // Reload no longer leads to this being called for a pending NavigationEntry
3656 // of index -1.
Lucas Furukawa Gadania9c45682019-07-31 22:05:143657 mojom::CommitNavigationParamsPtr commit_params =
3658 entry->ConstructCommitNavigationParams(
3659 *frame_entry, common_params->url, origin_to_commit,
3660 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3661 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533662 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3663 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143664 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423665
clamyea99ea12018-05-28 13:54:233666 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143667 frame_tree_node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083668 !entry->is_renderer_initiated(), false /* was_opener_suppressed */,
3669 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453670 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3671 entry->extra_headers(), frame_entry, entry, request_body,
Anton Bikineevf62d1bf2021-05-15 17:56:073672 nullptr /* navigation_ui_data */, absl::nullopt /* impression */);
clamyea99ea12018-05-28 13:54:233673}
3674
[email protected]d202a7c2012-01-04 07:53:473675void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213676 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273677 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403678
[email protected]2db9bd72012-04-13 20:20:563679 // We need to notify the ssl_manager_ before the web_contents_ so the
[email protected]df1af242009-05-01 00:11:403680 // location bar will have up-to-date information about the security style
3681 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133682 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403683
[email protected]7f924832014-08-09 05:57:223684 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:573685 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463686
[email protected]b0f724c2013-09-05 04:21:133687 // TODO(avi): Remove. http://crbug.com/170921
3688 NotificationDetails notification_details =
3689 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153690 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3691 Source<NavigationController>(this),
3692 notification_details);
initial.commit09911bf2008-07-26 23:55:293693}
3694
initial.commit09911bf2008-07-26 23:55:293695// static
[email protected]d202a7c2012-01-04 07:53:473696size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233697 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153698 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:213699 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233700}
3701
[email protected]d202a7c2012-01-04 07:53:473702void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223703 if (is_active && needs_reload_)
3704 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293705}
3706
[email protected]d202a7c2012-01-04 07:53:473707void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293708 if (!needs_reload_)
3709 return;
3710
Bo Liucdfa4b12018-11-06 00:21:443711 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3712 needs_reload_type_);
3713
initial.commit09911bf2008-07-26 23:55:293714 // Calling Reload() results in ignoring state, and not loading.
3715 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3716 // cached state.
avicc872d7242015-08-19 21:26:343717 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163718 NavigateToExistingPendingEntry(ReloadType::NONE,
3719 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343720 } else if (last_committed_entry_index_ != -1) {
arthursonzogni5c4c202d2017-04-25 23:41:273721 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343722 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163723 NavigateToExistingPendingEntry(ReloadType::NONE,
3724 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343725 } else {
3726 // If there is something to reload, the successful reload will clear the
3727 // |needs_reload_| flag. Otherwise, just do it here.
3728 needs_reload_ = false;
3729 }
initial.commit09911bf2008-07-26 23:55:293730}
3731
Carlos IL42b416592019-10-07 23:10:363732void NavigationControllerImpl::LoadPostCommitErrorPage(
3733 RenderFrameHost* render_frame_host,
3734 const GURL& url,
3735 const std::string& error_page_html,
3736 net::Error error) {
Rakina Zata Amni919b7922020-12-11 09:03:133737 RenderFrameHostImpl* rfhi =
3738 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:583739
3740 // Only active documents can load post-commit error pages:
3741 // - If the document is in pending deletion, the browser already committed to
3742 // destroying this RenderFrameHost so ignore loading the error page.
3743 // - If the document is in back-forward cache, it's not allowed to navigate as
3744 // it should remain frozen. Ignore the request and evict the document from
3745 // back-forward cache.
3746 // - If the document is prerendering, it can navigate but when loading error
3747 // pages, cancel prerendering.
3748 if (rfhi->IsInactiveAndDisallowActivation())
3749 return;
3750
Rakina Zata Amni919b7922020-12-11 09:03:133751 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:413752
3753 mojom::CommonNavigationParamsPtr common_params =
3754 CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:593755 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
3756 // the frame actually committed (e.g. iframe with "src" set to a
3757 // slow-responding URL). We should rewrite the URL to about:blank in this
3758 // case, as the renderer will only think a page is an error page if it has a
3759 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:133760 common_params->url = url.is_empty() ? GURL("about:blank") : url;
John Delaney131ad362019-08-08 21:57:413761 mojom::CommitNavigationParamsPtr commit_params =
3762 CreateCommitNavigationParams();
Antonio Sartori58591c892021-04-21 06:54:333763 commit_params->original_url = common_params->url;
John Delaney131ad362019-08-08 21:57:413764
arthursonzogni70ac7302020-05-28 08:49:053765 // Error pages have a fully permissive FramePolicy.
3766 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
3767 // error pages.
3768 commit_params->frame_policy = blink::FramePolicy();
3769
John Delaney131ad362019-08-08 21:57:413770 std::unique_ptr<NavigationRequest> navigation_request =
3771 NavigationRequest::CreateBrowserInitiated(
3772 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083773 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:193774 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453775 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:063776 "" /* extra_headers */, nullptr /* frame_entry */,
3777 nullptr /* entry */, nullptr /* post_body */,
Anton Bikineevf62d1bf2021-05-15 17:56:073778 nullptr /* navigation_ui_data */, absl::nullopt /* impression */);
Carlos IL42b416592019-10-07 23:10:363779 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:413780 navigation_request->set_net_error(error);
3781 node->CreatedNavigationRequest(std::move(navigation_request));
3782 DCHECK(node->navigation_request());
3783 node->navigation_request()->BeginNavigation();
3784}
3785
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573786void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213787 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093788 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:153789 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143790 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293791}
3792
[email protected]d202a7c2012-01-04 07:53:473793void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363794 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553795 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363796 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293797
initial.commit09911bf2008-07-26 23:55:293798 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293799}
[email protected]765b35502008-08-21 00:51:203800
arthursonzogni69a6a1b2019-09-17 09:23:003801void NavigationControllerImpl::DiscardNonCommittedEntries() {
Michael Thiessen9b14d512019-09-23 21:19:473802 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:103803 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
3804 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:293805 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:473806 return;
Michael Thiessenc5676d22019-09-25 22:32:103807 }
avi45a72532015-04-07 21:01:453808 DiscardPendingEntry(false);
arthursonzogni69a6a1b2019-09-17 09:23:003809 if (delegate_)
3810 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:483811}
3812
avi7c6f35e2015-05-08 17:52:383813int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3814 int nav_entry_id) const {
3815 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3816 if (entries_[i]->GetUniqueID() == nav_entry_id)
3817 return i;
3818 }
3819 return -1;
3820}
3821
[email protected]d202a7c2012-01-04 07:53:473822void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573823 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253824 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573825 DCHECK_LE(max_index, source->GetEntryCount());
Nate Chapin214a86a2021-06-21 20:35:573826 std::unique_ptr<NavigationEntryRestoreContextImpl> context =
3827 std::make_unique<NavigationEntryRestoreContextImpl>();
[email protected]e1cd5452010-08-26 18:03:253828 for (int i = 0; i < max_index; i++) {
Nate Chapin9f169072021-06-09 19:32:373829 // Normally, cloning a NavigationEntryImpl results in sharing
3830 // FrameNavigationEntries between the original and the clone. However, when
3831 // cloning from a different NavigationControllerImpl, we want to fork the
3832 // FrameNavigationEntries.
Nate Chapin9f169072021-06-09 19:32:373833 entries_.insert(entries_.begin() + i,
Nate Chapin214a86a2021-06-21 20:35:573834 source->entries_[i]->CloneWithoutSharing(context.get()));
[email protected]e1cd5452010-08-26 18:03:253835 }
arthursonzogni5c4c202d2017-04-25 23:41:273836 DCHECK(pending_entry_index_ == -1 ||
3837 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:253838}
[email protected]c5b88d82012-10-06 17:03:333839
3840void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:183841 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:333842 get_timestamp_callback_ = get_timestamp_callback;
3843}
[email protected]8ff00d72012-10-23 19:12:213844
Shivani Sharmaffb32b82019-04-09 16:58:473845// History manipulation intervention:
3846void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:473847 bool replace_entry,
3848 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:403849 bool is_renderer_initiated,
3850 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:453851 // Note that for a subframe, previous_document_was_activated is true if the
3852 // gesture happened in any subframe (propagated to main frame) or in the main
3853 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:473854 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:273855 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:473856 return;
3857 }
3858 if (last_committed_entry_index_ == -1)
3859 return;
3860
Shivani Sharmac4cc8922019-04-18 03:11:173861 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473862
Alexander Timine3ec4192020-04-20 16:39:403863 // Log UKM with the URL we are navigating away from.
3864 ukm::builders::HistoryManipulationIntervention(
3865 previous_page_load_ukm_source_id)
3866 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:473867}
3868
Shivani Sharmac4cc8922019-04-18 03:11:173869void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
3870 int reference_index,
3871 bool skippable) {
3872 auto* reference_entry = GetEntryAtIndex(reference_index);
3873 reference_entry->set_should_skip_on_back_forward_ui(skippable);
3874
3875 int64_t document_sequence_number =
3876 reference_entry->root_node()->frame_entry->document_sequence_number();
3877 for (int index = 0; index < GetEntryCount(); index++) {
3878 auto* entry = GetEntryAtIndex(index);
3879 if (entry->root_node()->frame_entry->document_sequence_number() ==
3880 document_sequence_number) {
3881 entry->set_should_skip_on_back_forward_ui(skippable);
3882 }
3883 }
3884}
3885
arthursonzogni66f711c2019-10-08 14:40:363886std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
3887NavigationControllerImpl::ReferencePendingEntry() {
3888 DCHECK(pending_entry_);
3889 auto pending_entry_ref =
3890 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
3891 pending_entry_refs_.insert(pending_entry_ref.get());
3892 return pending_entry_ref;
3893}
3894
3895void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
3896 // Ignore refs that don't correspond to the current pending entry.
3897 auto it = pending_entry_refs_.find(ref);
3898 if (it == pending_entry_refs_.end())
3899 return;
3900 pending_entry_refs_.erase(it);
3901
3902 if (!pending_entry_refs_.empty())
3903 return;
3904
3905 // The pending entry may be deleted before the last PendingEntryRef.
3906 if (!pending_entry_)
3907 return;
3908
3909 // We usually clear the pending entry when the matching NavigationRequest
3910 // fails, so that an arbitrary URL isn't left visible above a committed page.
3911 //
3912 // However, we do preserve the pending entry in some cases, such as on the
3913 // initial navigation of an unmodified blank tab. We also allow the delegate
3914 // to say when it's safe to leave aborted URLs in the omnibox, to let the
3915 // user edit the URL and try again. This may be useful in cases that the
3916 // committed page cannot be attacker-controlled. In these cases, we still
3917 // allow the view to clear the pending entry and typed URL if the user
3918 // requests (e.g., hitting Escape with focus in the address bar).
3919 //
3920 // Do not leave the pending entry visible if it has an invalid URL, since this
3921 // might be formatted in an unexpected or unsafe way.
3922 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:363923 bool should_preserve_entry =
3924 (pending_entry_ == GetVisibleEntry()) &&
3925 pending_entry_->GetURL().is_valid() &&
3926 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
3927 if (should_preserve_entry)
3928 return;
3929
3930 DiscardPendingEntry(true);
3931 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3932}
3933
Titouan Rigoudy6ec70402021-02-02 15:42:193934std::unique_ptr<PolicyContainerPolicies>
3935NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:393936 RenderFrameHostImpl* rfh,
3937 bool is_same_document,
3938 NavigationRequest* request) {
Titouan Rigoudy6ec70402021-02-02 15:42:193939 if (!ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(request))
Antonio Sartori78a749f2020-11-30 12:03:393940 return nullptr;
3941
3942 if (is_same_document) {
Charlie Reis73e356242021-04-02 17:10:313943 // TODO(https://crbug.com/524208): Remove this nullptr check when we can
3944 // ensure we always have a FrameNavigationEntry here.
3945 if (!GetLastCommittedEntry())
3946 return nullptr;
3947
Antonio Sartori78a749f2020-11-30 12:03:393948 FrameNavigationEntry* previous_frame_entry =
3949 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
3950
3951 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
3952 // ensure we always have a FrameNavigationEntry here.
3953 if (!previous_frame_entry)
3954 return nullptr;
3955
Titouan Rigoudy6ec70402021-02-02 15:42:193956 const PolicyContainerPolicies* previous_policies =
3957 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:393958
Titouan Rigoudy6ec70402021-02-02 15:42:193959 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:393960 return nullptr;
3961
3962 // Make a copy of the policy container for the new FrameNavigationEntry.
Antonio Sartori5d09b30f2021-03-02 09:27:163963 return previous_policies->Clone();
Antonio Sartori78a749f2020-11-30 12:03:393964 }
3965
Antonio Sartori4f5373792021-05-31 10:56:473966 return rfh->policy_container_host()->policies().Clone();
Antonio Sartori78a749f2020-11-30 12:03:393967}
3968
Hayato Ito2c8c08d02021-06-23 03:38:433969void NavigationControllerImpl::BroadcastHistoryOffsetAndLength() {
Carlos Caballeroede6f8c2021-01-28 11:01:503970 OPTIONAL_TRACE_EVENT2(
Hayato Ito2c8c08d02021-06-23 03:38:433971 "content", "NavigationControllerImpl::BroadcastHistoryOffsetAndLength",
3972 "history_offset", GetLastCommittedEntryIndex(), "history_length",
3973 GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:503974
3975 auto callback = base::BindRepeating(
3976 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
3977 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
3978 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
3979 }
3980 },
Hayato Ito2c8c08d02021-06-23 03:38:433981 GetLastCommittedEntryIndex(), GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:503982 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
3983}
3984
3985void NavigationControllerImpl::DidAccessInitialMainDocument() {
3986 // We may have left a failed browser-initiated navigation in the address bar
3987 // to let the user edit it and try again. Clear it now that content might
3988 // show up underneath it.
3989 if (!frame_tree_.IsLoading() && GetPendingEntry())
3990 DiscardPendingEntry(false);
3991
3992 // Update the URL display.
3993 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3994}
3995
3996void NavigationControllerImpl::UpdateStateForFrame(
3997 RenderFrameHostImpl* rfhi,
3998 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:563999 OPTIONAL_TRACE_EVENT1("content",
4000 "NavigationControllerImpl::UpdateStateForFrame",
4001 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:504002 // The state update affects the last NavigationEntry associated with the given
4003 // |render_frame_host|. This may not be the last committed NavigationEntry (as
4004 // in the case of an UpdateState from a frame being swapped out). We track
4005 // which entry this is in the RenderFrameHost's nav_entry_id.
4006 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
4007 if (!entry)
4008 return;
4009
4010 FrameNavigationEntry* frame_entry =
4011 entry->GetFrameEntry(rfhi->frame_tree_node());
4012 if (!frame_entry)
4013 return;
4014
4015 // The SiteInstance might not match if we do a cross-process navigation with
4016 // replacement (e.g., auto-subframe), in which case the swap out of the old
4017 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4018 // has already been replaced and destroyed.
4019 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4020 return;
4021
4022 if (page_state == frame_entry->page_state())
4023 return; // Nothing to update.
4024
4025 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4026
4027 // The document_sequence_number and item_sequence_number recorded in the
4028 // FrameNavigationEntry should not differ from the one coming with the update,
4029 // since it must come from the same document. Do not update it if a difference
4030 // is detected, as this indicates that |frame_entry| is not the correct one.
4031 blink::ExplodedPageState exploded_state;
4032 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4033 return;
4034
4035 if (exploded_state.top.document_sequence_number !=
4036 frame_entry->document_sequence_number() ||
4037 exploded_state.top.item_sequence_number !=
4038 frame_entry->item_sequence_number()) {
4039 return;
4040 }
4041
4042 frame_entry->SetPageState(page_state);
4043 NotifyEntryChanged(entry);
4044}
4045
Aaron Colwellb731a0ae2021-03-19 19:14:474046void NavigationControllerImpl::OnStoragePartitionIdAdded(
4047 const StoragePartitionId& partition_id) {
4048 auto it = partition_config_to_id_map_.insert(
4049 std::make_pair(partition_id.config(), partition_id));
4050 bool successful_insert = it.second;
4051 DCHECK(successful_insert);
4052 if (!successful_insert) {
4053 LogStoragePartitionIdCrashKeys(it.first->second, partition_id);
4054 }
4055}
4056
4057void NavigationControllerImpl::LogStoragePartitionIdCrashKeys(
4058 const StoragePartitionId& original_partition_id,
4059 const StoragePartitionId& new_partition_id) {
4060 base::debug::SetCrashKeyString(
4061 base::debug::AllocateCrashKeyString("original_partition_id",
4062 base::debug::CrashKeySize::Size256),
4063 original_partition_id.ToString());
4064
4065 base::debug::SetCrashKeyString(
4066 base::debug::AllocateCrashKeyString("new_partition_id",
4067 base::debug::CrashKeySize::Size256),
4068 new_partition_id.ToString());
4069
4070 base::debug::DumpWithoutCrashing();
4071}
4072
Nate Chapind1fe3612021-04-16 20:45:574073std::vector<mojom::AppHistoryEntryPtr>
4074NavigationControllerImpl::PopulateSingleAppHistoryEntryVector(
4075 Direction direction,
4076 int entry_index,
4077 const url::Origin& pending_origin,
4078 FrameTreeNode* node,
4079 SiteInstance* site_instance,
4080 int64_t previous_item_sequence_number) {
4081 std::vector<mojom::AppHistoryEntryPtr> entries;
4082 int offset = direction == Direction::kForward ? 1 : -1;
4083 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4084 i += offset) {
4085 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
4086 if (!frame_entry || !frame_entry->committed_origin())
4087 break;
4088 if (site_instance != frame_entry->site_instance())
4089 break;
4090 if (!pending_origin.IsSameOriginWith(*frame_entry->committed_origin()))
4091 break;
4092 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4093 continue;
4094 blink::ExplodedPageState exploded_page_state;
4095 if (blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4096 &exploded_page_state)) {
4097 blink::ExplodedFrameState frame_state = exploded_page_state.top;
4098 mojom::AppHistoryEntryPtr entry = mojom::AppHistoryEntry::New(
4099 frame_state.app_history_key.value_or(std::u16string()),
4100 frame_state.app_history_id.value_or(std::u16string()),
4101 frame_state.url_string.value_or(std::u16string()));
4102 DCHECK(pending_origin.CanBeDerivedFrom(GURL(entry->url)));
4103 entries.push_back(std::move(entry));
4104 previous_item_sequence_number = frame_entry->item_sequence_number();
4105 }
4106 }
4107 // If |entries| was constructed by iterating backwards from
4108 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4109 // earliest-at-the-front. Reverse it.
4110 if (direction == Direction::kBack)
4111 std::reverse(entries.begin(), entries.end());
4112 return entries;
4113}
4114
4115void NavigationControllerImpl::PopulateAppHistoryEntryVectors(
4116 NavigationRequest* request) {
4117 url::Origin pending_origin =
4118 request->commit_params().origin_to_commit
4119 ? *request->commit_params().origin_to_commit
4120 : url::Origin::Create(request->common_params().url);
4121
4122 FrameTreeNode* node = request->frame_tree_node();
4123 scoped_refptr<SiteInstance> site_instance =
4124 request->GetRenderFrameHost()->GetSiteInstance();
4125
4126 // NOTE: |entry_index| is an estimate of the index where this entry will
4127 // commit, but it may be wrong in corner cases (e.g., if we are at the max
4128 // entry limit, the earliest entry will be dropped). This is ok because this
4129 // algorithm only uses |entry_index| to walk the entry list as it stands right
4130 // now, and it isn't saved for anything post-commit.
4131 int entry_index = GetPendingEntryIndex();
4132 bool will_create_new_entry = false;
4133 if (NavigationTypeUtils::IsReload(request->common_params().navigation_type) ||
4134 request->common_params().should_replace_current_entry) {
4135 entry_index = GetLastCommittedEntryIndex();
4136 } else if (entry_index == -1) {
4137 will_create_new_entry = true;
4138 entry_index = GetLastCommittedEntryIndex() + 1;
4139 }
4140
4141 int64_t pending_item_sequence_number = 0;
4142 if (auto* pending_entry = GetPendingEntry()) {
4143 if (auto* frame_entry = pending_entry->GetFrameEntry(node))
4144 pending_item_sequence_number = frame_entry->item_sequence_number();
4145 }
4146
4147 request->set_app_history_back_entries(PopulateSingleAppHistoryEntryVector(
4148 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
4149 pending_item_sequence_number));
4150
4151 // Don't populate forward entries if they will be truncated by a new entry.
4152 if (!will_create_new_entry) {
4153 request->set_app_history_forward_entries(
4154 PopulateSingleAppHistoryEntryVector(
4155 Direction::kForward, entry_index, pending_origin, node,
4156 site_instance.get(), pending_item_sequence_number));
4157 }
4158}
4159
Nate Chapinfbfe5af2021-06-10 17:22:084160NavigationControllerImpl::HistoryNavigationAction
4161NavigationControllerImpl::ShouldNavigateToEntryForAppHistoryKey(
4162 FrameNavigationEntry* current_entry,
4163 FrameNavigationEntry* target_entry,
4164 const std::string& app_history_key) {
4165 if (!target_entry || !target_entry->committed_origin())
4166 return HistoryNavigationAction::kStopLooking;
4167 if (current_entry->site_instance() != target_entry->site_instance())
4168 return HistoryNavigationAction::kStopLooking;
4169 if (!current_entry->committed_origin()->IsSameOriginWith(
4170 *target_entry->committed_origin())) {
4171 return HistoryNavigationAction::kStopLooking;
4172 }
4173
4174 // NOTE: We don't actually care between kSameDocument and
4175 // kDifferentDocument, so always use kDifferentDocument by convention.
4176 if (target_entry->app_history_key() == app_history_key)
4177 return HistoryNavigationAction::kDifferentDocument;
4178 return HistoryNavigationAction::kKeepLooking;
4179}
4180
4181void NavigationControllerImpl::NavigateToAppHistoryKey(FrameTreeNode* node,
4182 const std::string& key) {
4183 FrameNavigationEntry* current_entry =
4184 GetLastCommittedEntry()->GetFrameEntry(node);
4185 if (!current_entry)
4186 return;
4187
4188 // We want to find the nearest matching entry that is contiguously
4189 // same-instance and same-origin. Check back first, then forward.
4190 // TODO(japhet): Link spec here once it exists.
4191 for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) {
4192 auto result = ShouldNavigateToEntryForAppHistoryKey(
4193 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4194 if (result == HistoryNavigationAction::kStopLooking)
4195 break;
4196 if (result != HistoryNavigationAction::kKeepLooking) {
4197 GoToIndex(i);
4198 return;
4199 }
4200 }
4201 for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) {
4202 auto result = ShouldNavigateToEntryForAppHistoryKey(
4203 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4204 if (result == HistoryNavigationAction::kStopLooking)
4205 break;
4206 if (result != HistoryNavigationAction::kKeepLooking) {
4207 GoToIndex(i);
4208 return;
4209 }
4210 }
4211}
4212
[email protected]8ff00d72012-10-23 19:12:214213} // namespace content