blob: 22b32463001b2a27a75a1d63d32b35832ff99c45 [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.
Rakina Zata Amni5d2ef8aa2021-06-25 01:34:23275 if (request->IsLoadDataWithBaseURLAndHasUnreachableURL())
Anton Bikineevf62d1bf2021-05-15 17:56:07276 return absl::nullopt;
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12277
Anton Bikineevf62d1bf2021-05-15 17:56:07278 return absl::make_optional(params.origin);
Nasko Oskov03912102019-01-11 00:21:32279}
280
Camille Lamy5193caa2018-10-12 11:59:42281bool IsValidURLForNavigation(bool is_main_frame,
282 const GURL& virtual_url,
283 const GURL& dest_url) {
284 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
285 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
286 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
287 << virtual_url.possibly_invalid_spec();
288 return false;
289 }
290
291 // Don't attempt to navigate to non-empty invalid URLs.
292 if (!dest_url.is_valid() && !dest_url.is_empty()) {
293 LOG(WARNING) << "Refusing to load invalid URL: "
294 << dest_url.possibly_invalid_spec();
295 return false;
296 }
297
298 // The renderer will reject IPC messages with URLs longer than
299 // this limit, so don't attempt to navigate with a longer URL.
300 if (dest_url.spec().size() > url::kMaxURLChars) {
301 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
302 << " characters.";
303 return false;
304 }
305
Aaron Colwell33109c592020-04-21 21:31:19306 // Reject renderer debug URLs because they should have been handled before
307 // we get to this point. This check handles renderer debug URLs
308 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
309 // provides defense-in-depth if a renderer debug URL manages to get here via
310 // some other path. We want to reject the navigation here so it doesn't
311 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30312 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19313 LOG(WARNING) << "Refusing to load renderer debug URL: "
314 << dest_url.possibly_invalid_spec();
315 return false;
316 }
317
Camille Lamy5193caa2018-10-12 11:59:42318 return true;
319}
320
Mikel Astizba9cf2fd2017-12-17 10:38:10321// See replaced_navigation_entry_data.h for details: this information is meant
322// to ensure |*output_entry| keeps track of its original URL (landing page in
323// case of server redirects) as it gets replaced (e.g. history.replaceState()),
324// without overwriting it later, for main frames.
325void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57326 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10327 NavigationEntryImpl* output_entry) {
328 if (output_entry->GetReplacedEntryData().has_value())
329 return;
330
331 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57332 data.first_committed_url = replaced_entry->GetURL();
333 data.first_timestamp = replaced_entry->GetTimestamp();
334 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29335 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10336}
337
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51338mojom::NavigationType GetNavigationType(const GURL& old_url,
339 const GURL& new_url,
340 ReloadType reload_type,
341 NavigationEntryImpl* entry,
342 const FrameNavigationEntry& frame_entry,
danakjd83d706d2020-11-25 22:11:12343 bool has_pending_cross_document_commit,
danakjb952ef12021-01-14 19:58:49344 bool is_currently_error_page,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51345 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23346 // Reload navigations
347 switch (reload_type) {
348 case ReloadType::NORMAL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51349 return mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23350 case ReloadType::BYPASSING_CACHE:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51351 return mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23352 case ReloadType::ORIGINAL_REQUEST_URL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51353 return mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23354 case ReloadType::NONE:
355 break; // Fall through to rest of function.
356 }
357
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08358 if (entry->IsRestored()) {
Lei Zhang96031532019-10-10 19:05:47359 return entry->GetHasPostData() ? mojom::NavigationType::RESTORE_WITH_POST
360 : mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23361 }
362
danakjb952ef12021-01-14 19:58:49363 const bool can_be_same_document =
364 // A pending cross-document commit means this navigation will not occur in
365 // the current document, as that document would end up being replaced in
366 // the meantime.
367 !has_pending_cross_document_commit &&
368 // If the current document is an error page, we should always treat it as
369 // a different-document navigation so that we'll attempt to load the
370 // document we're navigating to (and not stay in the current error page).
371 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12372
clamyea99ea12018-05-28 13:54:23373 // History navigations.
374 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12375 return can_be_same_document && is_same_document_history_load
Lei Zhang96031532019-10-10 19:05:47376 ? mojom::NavigationType::HISTORY_SAME_DOCUMENT
377 : mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23378 }
379 DCHECK(!is_same_document_history_load);
380
381 // A same-document fragment-navigation happens when the only part of the url
382 // that is modified is after the '#' character.
383 //
384 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12385 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23386 //
387 // Note: this check is only valid for navigations that are not history
388 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
389 // history navigation from 'A#foo' to 'A#bar' is not a same-document
390 // navigation, but a different-document one. This is why history navigation
391 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47392 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
393 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12394
395 // The one case where we do the wrong thing here and incorrectly choose
396 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
397 // the renderer is a frameset. All frameset navigations should be
398 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
399 // navigation would do the right thing, as it would send it to the browser and
400 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
401 // into this method). But since we can't tell that case here for browser-
402 // initiated navigations, we have to get the renderer involved. In that case
403 // the navigation would be restarted due to the renderer spending a reply of
404 // mojom::CommitResult::RestartCrossDocument.
405
406 return can_be_same_document && is_same_doc
407 ? mojom::NavigationType::SAME_DOCUMENT
408 : mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23409}
410
Camille Lamy5193caa2018-10-12 11:59:42411// Adjusts the original input URL if needed, to get the URL to actually load and
412// the virtual URL, which may differ.
413void RewriteUrlForNavigation(const GURL& original_url,
414 BrowserContext* browser_context,
415 GURL* url_to_load,
416 GURL* virtual_url,
417 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42418 // Allow the browser URL handler to rewrite the URL. This will, for example,
419 // remove "view-source:" from the beginning of the URL to get the URL that
420 // will actually be loaded. This real URL won't be shown to the user, just
421 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31422 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42423 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
424 url_to_load, browser_context, reverse_on_redirect);
425}
426
427#if DCHECK_IS_ON()
428// Helper sanity check function used in debug mode.
429void ValidateRequestMatchesEntry(NavigationRequest* request,
430 NavigationEntryImpl* entry) {
431 if (request->frame_tree_node()->IsMainFrame()) {
432 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
433 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
434 request->common_params().transition, entry->GetTransitionType()));
435 }
Nasko Oskovc36327d2019-01-03 23:23:04436 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42437 entry->should_clear_history_list());
438 DCHECK_EQ(request->common_params().has_user_gesture,
439 entry->has_user_gesture());
440 DCHECK_EQ(request->common_params().base_url_for_data_url,
441 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04442 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42443 entry->GetCanLoadLocalResources());
444 DCHECK_EQ(request->common_params().started_from_context_menu,
445 entry->has_started_from_context_menu());
446
447 FrameNavigationEntry* frame_entry =
448 entry->GetFrameEntry(request->frame_tree_node());
449 if (!frame_entry) {
450 NOTREACHED();
451 return;
452 }
453
Camille Lamy5193caa2018-10-12 11:59:42454 DCHECK_EQ(request->common_params().method, frame_entry->method());
455
Nasko Oskovc36327d2019-01-03 23:23:04456 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42457 if (redirect_size == frame_entry->redirect_chain().size()) {
458 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04459 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42460 frame_entry->redirect_chain()[i]);
461 }
462 } else {
463 NOTREACHED();
464 }
465}
466#endif // DCHECK_IS_ON()
467
Dave Tapuska8bfd84c2019-03-26 20:47:16468// Returns whether the session history NavigationRequests in |navigations|
469// would stay within the subtree of the sandboxed iframe in
470// |sandbox_frame_tree_node_id|.
471bool DoesSandboxNavigationStayWithinSubtree(
472 int sandbox_frame_tree_node_id,
473 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
474 for (auto& item : navigations) {
475 bool within_subtree = false;
476 // Check whether this NavigationRequest affects a frame within the
477 // sandboxed frame's subtree by walking up the tree looking for the
478 // sandboxed frame.
479 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03480 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16481 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
482 within_subtree = true;
483 break;
484 }
485 }
486 if (!within_subtree)
487 return false;
488 }
489 return true;
490}
491
Titouan Rigoudy6ec70402021-02-02 15:42:19492bool ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(
Antonio Sartori78a749f2020-11-30 12:03:39493 const NavigationRequest* request) {
494 // For local schemes we need to store the policy container in the
495 // FrameNavigationEntry, so that we can reload it in case of history
496 // navigation.
497 //
498 // TODO(https://crbug.com/1146361 and https://crbug.com/1146362): blob: and
499 // filesystem: should be removed from this list when we have properly
500 // implemented storing their policy container in the respective store.
501 return (request->common_params().url.SchemeIs(url::kAboutScheme) ||
502 request->common_params().url.SchemeIs(url::kDataScheme) ||
503 request->common_params().url.SchemeIsBlob() ||
504 request->common_params().url.SchemeIsFileSystem());
505}
506
[email protected]e9ba4472008-09-14 15:42:43507} // namespace
508
arthursonzogni66f711c2019-10-08 14:40:36509// NavigationControllerImpl::PendingEntryRef------------------------------------
510
511NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
512 base::WeakPtr<NavigationControllerImpl> controller)
513 : controller_(controller) {}
514
515NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
516 if (!controller_) // Can be null with interstitials.
517 return;
518
519 controller_->PendingEntryRefDeleted(this);
520}
521
[email protected]d202a7c2012-01-04 07:53:47522// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29523
[email protected]23a918b2014-07-15 09:51:36524const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23525
[email protected]765b35502008-08-21 00:51:20526// static
[email protected]d202a7c2012-01-04 07:53:47527size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23528 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20529
[email protected]e6fec472013-05-14 05:29:02530// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20531// when testing.
532static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29533
[email protected]71fde352011-12-29 03:29:56534// static
dcheng9bfa5162016-04-09 01:00:57535std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
536 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10537 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07538 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10539 ui::PageTransition transition,
540 bool is_renderer_initiated,
541 const std::string& extra_headers,
542 BrowserContext* browser_context,
543 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
544 return NavigationControllerImpl::CreateNavigationEntry(
545 url, referrer, std::move(initiator_origin),
546 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Hayato Ito303654c2021-06-30 09:07:54547 extra_headers, browser_context, std::move(blob_url_loader_factory));
Lukasz Anforowicz641234d52019-11-07 21:07:10548}
549
550// static
551std::unique_ptr<NavigationEntryImpl>
552NavigationControllerImpl::CreateNavigationEntry(
553 const GURL& url,
554 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07555 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10556 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57557 ui::PageTransition transition,
558 bool is_renderer_initiated,
559 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09560 BrowserContext* browser_context,
Hayato Ito303654c2021-06-30 09:07:54561 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
Camille Lamy5193caa2018-10-12 11:59:42562 GURL url_to_load;
563 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56564 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42565 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
566 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56567
Lukasz Anforowicz641234d52019-11-07 21:07:10568 // Let the NTP override the navigation params and pretend that this is a
569 // browser-initiated, bookmark-like navigation.
570 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violetcf6ea7e2021-06-09 21:09:21571 source_site_instance, &transition, &is_renderer_initiated, &referrer,
572 &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10573
Patrick Monettef507e982019-06-19 20:18:06574 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28575 nullptr, // The site instance for tabs is sent on navigation
576 // (WebContents::GetSiteInstance).
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58577 url_to_load, referrer, initiator_origin, std::u16string(), transition,
Camille Lamy5193caa2018-10-12 11:59:42578 is_renderer_initiated, blob_url_loader_factory);
579 entry->SetVirtualURL(virtual_url);
580 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56581 entry->set_update_virtual_url_with_url(reverse_on_redirect);
582 entry->set_extra_headers(extra_headers);
Patrick Monettef507e982019-06-19 20:18:06583 return entry;
[email protected]71fde352011-12-29 03:29:56584}
585
[email protected]cdcb1dee2012-01-04 00:46:20586// static
587void NavigationController::DisablePromptOnRepost() {
588 g_check_for_repost = false;
589}
590
[email protected]c5b88d82012-10-06 17:03:33591base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
592 base::Time t) {
593 // If |t| is between the water marks, we're in a run of duplicates
594 // or just getting out of it, so increase the high-water mark to get
595 // a time that probably hasn't been used before and return it.
596 if (low_water_mark_ <= t && t <= high_water_mark_) {
597 high_water_mark_ += base::TimeDelta::FromMicroseconds(1);
598 return high_water_mark_;
599 }
600
601 // Otherwise, we're clear of the last duplicate run, so reset the
602 // water marks.
603 low_water_mark_ = high_water_mark_ = t;
604 return t;
605}
606
ckitagawa0faa5e42020-06-17 17:30:54607NavigationControllerImpl::ScopedShowRepostDialogForTesting::
608 ScopedShowRepostDialogForTesting()
609 : was_disallowed_(g_check_for_repost) {
610 g_check_for_repost = true;
611}
612
613NavigationControllerImpl::ScopedShowRepostDialogForTesting::
614 ~ScopedShowRepostDialogForTesting() {
615 g_check_for_repost = was_disallowed_;
616}
617
[email protected]d202a7c2012-01-04 07:53:47618NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00619 BrowserContext* browser_context,
620 FrameTree& frame_tree,
621 NavigationControllerDelegate* delegate)
622 : frame_tree_(frame_tree),
623 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57624 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22625 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47626 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37627 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29628}
629
[email protected]d202a7c2012-01-04 07:53:47630NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00631 // The NavigationControllerImpl might be called inside its delegate
632 // destructor. Calling it is not valid anymore.
633 delegate_ = nullptr;
634 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29635}
636
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57637WebContents* NavigationControllerImpl::GetWebContents() {
[email protected]ec6c05f2013-10-23 18:41:57638 return delegate_->GetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32639}
640
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57641BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55642 return browser_context_;
643}
644
[email protected]d202a7c2012-01-04 07:53:47645void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30646 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36647 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57648 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57649 // Verify that this controller is unused and that the input is valid.
Lei Zhang96031532019-10-10 19:05:47650 DCHECK_EQ(0, GetEntryCount());
651 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57652 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14653 selected_navigation < static_cast<int>(entries->size()));
arthursonzogni5c4c202d2017-04-25 23:41:27654 DCHECK_EQ(-1, pending_entry_index_);
[email protected]ce3fa3c2009-04-20 19:55:57655
[email protected]ce3fa3c2009-04-20 19:55:57656 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44657 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03658 entries_.reserve(entries->size());
659 for (auto& entry : *entries)
dcheng36b6aec92015-12-26 06:16:36660 entries_.push_back(
661 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
avif16f85a72015-11-13 18:25:03662
663 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
664 // cleared out safely.
665 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57666
667 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36668 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57669}
670
toyoshim6142d96f2016-12-19 09:07:25671void NavigationControllerImpl::Reload(ReloadType reload_type,
672 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28673 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28674 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32675 int current_index = -1;
676
Carlos IL42b416592019-10-07 23:10:36677 if (entry_replaced_by_post_commit_error_) {
678 // If there is an entry that was replaced by a currently active post-commit
679 // error navigation, this can't be the initial navigation.
680 DCHECK(!IsInitialNavigation());
681 // If the current entry is a post commit error, we reload the entry it
682 // replaced instead. We leave the error entry in place until a commit
683 // replaces it, but the pending entry points to the original entry in the
684 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
685 // case that pending_entry_ != entries_[pending_entry_index_].
686 entry = entry_replaced_by_post_commit_error_.get();
687 current_index = GetCurrentEntryIndex();
688 } else if (IsInitialNavigation() && pending_entry_) {
689 // If we are reloading the initial navigation, just use the current
690 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32691 entry = pending_entry_;
692 // The pending entry might be in entries_ (e.g., after a Clone), so we
693 // should also update the current_index.
694 current_index = pending_entry_index_;
695 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00696 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32697 current_index = GetCurrentEntryIndex();
698 if (current_index != -1) {
creis3da03872015-02-20 21:12:32699 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32700 }
[email protected]979a4bc2013-04-24 01:27:15701 }
[email protected]241db352013-04-22 18:04:05702
[email protected]59167c22013-06-03 18:07:32703 // If we are no where, then we can't reload. TODO(darin): We should add a
704 // CanReload method.
705 if (!entry)
706 return;
707
Takashi Toyoshimac7df3c22019-06-25 14:18:47708 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26709 entry->set_reload_type(reload_type);
710
Aran Gilman37d11632019-10-08 23:07:15711 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30712 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07713 // they really want to do this. If they do, the dialog will call us back
714 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57715 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02716
[email protected]106a0812010-03-18 00:15:12717 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57718 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47719 return;
initial.commit09911bf2008-07-26 23:55:29720 }
Lei Zhang96031532019-10-10 19:05:47721
722 if (!IsInitialNavigation())
723 DiscardNonCommittedEntries();
724
725 pending_entry_ = entry;
726 pending_entry_index_ = current_index;
727 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
728
729 NavigateToExistingPendingEntry(reload_type,
730 FrameTreeNode::kFrameTreeNodeInvalidId);
initial.commit09911bf2008-07-26 23:55:29731}
732
[email protected]d202a7c2012-01-04 07:53:47733void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48734 DCHECK(pending_reload_ != ReloadType::NONE);
735 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12736}
737
[email protected]d202a7c2012-01-04 07:53:47738void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48739 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12740 NOTREACHED();
741 } else {
toyoshim6142d96f2016-12-19 09:07:25742 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48743 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12744 }
745}
746
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57747bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09748 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11749}
750
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57751bool NavigationControllerImpl::IsInitialBlankNavigation() {
creis10a4ab72015-10-13 17:22:40752 // TODO(creis): Once we create a NavigationEntry for the initial blank page,
toyoshim0df1d3a2016-09-09 09:52:48753 // we'll need to check for entry count 1 and restore_type NONE (to exclude
754 // the cloned tab case).
creis10a4ab72015-10-13 17:22:40755 return IsInitialNavigation() && GetEntryCount() == 0;
756}
757
Aran Gilman37d11632019-10-08 23:07:15758NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
759 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58760 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03761 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58762}
763
W. James MacLean1c40862c2020-04-27 21:05:57764void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
765 const url::Origin& origin) {
766 for (int i = 0; i < GetEntryCount(); i++) {
767 auto* entry = GetEntryAtIndex(i);
768 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
769 }
770 if (entry_replaced_by_post_commit_error_) {
771 // It's possible we could come back to this entry if the error
772 // page/interstitial goes away.
773 entry_replaced_by_post_commit_error_
774 ->RegisterExistingOriginToPreventOptInIsolation(origin);
775 }
776 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
777 // visiting pending_entry_, which lacks a committed origin. This will be done
778 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
779}
780
avi25764702015-06-23 15:43:37781void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57782 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00783 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37784 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27785 DCHECK_EQ(-1, pending_entry_index_);
[email protected]8ff00d72012-10-23 19:12:21786 NotificationService::current()->Notify(
Aran Gilman37d11632019-10-08 23:07:15787 NOTIFICATION_NAV_ENTRY_PENDING, Source<NavigationController>(this),
avi25764702015-06-23 15:43:37788 Details<NavigationEntry>(pending_entry_));
[email protected]765b35502008-08-21 00:51:20789}
790
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57791NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47792 if (pending_entry_)
793 return pending_entry_;
794 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20795}
796
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57797NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32798 // The pending entry is safe to return for new (non-history), browser-
799 // initiated navigations. Most renderer-initiated navigations should not
800 // show the pending entry, to prevent URL spoof attacks.
801 //
802 // We make an exception for renderer-initiated navigations in new tabs, as
803 // long as no other page has tried to access the initial empty document in
804 // the new tab. If another page modifies this blank page, a URL spoof is
805 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32806 bool safe_to_show_pending =
807 pending_entry_ &&
808 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09809 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32810 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46811 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32812
813 // Also allow showing the pending entry for history navigations in a new tab,
814 // such as Ctrl+Back. In this case, no existing page is visible and no one
815 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15816 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
817 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32818 safe_to_show_pending = true;
819
820 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19821 return pending_entry_;
822 return GetLastCommittedEntry();
823}
824
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57825int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20826 if (pending_entry_index_ != -1)
827 return pending_entry_index_;
828 return last_committed_entry_index_;
829}
830
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57831NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20832 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28833 return nullptr;
avif16f85a72015-11-13 18:25:03834 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20835}
836
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57837bool NavigationControllerImpl::CanViewSource() {
Carlos Caballeroede6f8c2021-01-28 11:01:50838 const std::string& mime_type = frame_tree_.root()
Alex Moshchuk2e470ea2021-02-03 06:46:34839 ->current_frame_host()
840 ->render_view_host()
Carlos Caballeroede6f8c2021-01-28 11:01:50841 ->contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53842 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
843 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27844 NavigationEntry* visible_entry = GetVisibleEntry();
845 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39846 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16847}
848
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57849int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27850 // The last committed entry index must always be less than the number of
Carlos IL4dea8902020-05-26 15:14:29851 // entries. If there are no entries, it must be -1.
arthursonzogni5c4c202d2017-04-25 23:41:27852 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
853 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55854 return last_committed_entry_index_;
855}
856
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57857int NavigationControllerImpl::GetEntryCount() {
Carlos IL4dea8902020-05-26 15:14:29858 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55859 return static_cast<int>(entries_.size());
860}
861
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57862NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37863 if (index < 0 || index >= GetEntryCount())
864 return nullptr;
865
avif16f85a72015-11-13 18:25:03866 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25867}
868
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57869NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47870 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20871}
872
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57873int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46874 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03875}
876
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57877bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03878 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
879 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
880 return true;
881 }
882 return false;
[email protected]765b35502008-08-21 00:51:20883}
884
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57885bool NavigationControllerImpl::CanGoForward() {
WangHui74286d52021-03-31 16:17:15886 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
887 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
888 return true;
889 }
890 return false;
[email protected]765b35502008-08-21 00:51:20891}
892
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57893bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03894 int index = GetIndexForOffset(offset);
895 return index >= 0 && index < GetEntryCount();
896}
897
WangHui74286d52021-03-31 16:17:15898#if defined(OS_ANDROID)
899bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
WangHui74286d52021-03-31 16:17:15900 if (offset == 0)
901 return true;
902 int increment = offset > 0 ? 1 : -1;
903 int non_skippable_entries = 0;
904 for (int index = GetIndexForOffset(increment);
905 index >= 0 && index < GetEntryCount(); index += increment) {
906 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
907 non_skippable_entries++;
908
909 if (non_skippable_entries == std::abs(offset))
910 return true;
911 }
912 return false;
913}
914#endif
915
[email protected]d202a7c2012-01-04 07:53:47916void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06917 int target_index = GetIndexForOffset(-1);
918
Elly Fong-Jones40ee8112021-06-23 19:10:52919 // Move the target index past the skippable entries.
Shivani Sharma298d12852019-01-22 20:04:03920 bool all_skippable_entries = true;
Elly Fong-Jones40ee8112021-06-23 19:10:52921 while (target_index >= 0) {
922 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui()) {
Shivani Sharma298d12852019-01-22 20:04:03923 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06924 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16925 }
Elly Fong-Jones40ee8112021-06-23 19:10:52926 target_index--;
shivanisha55201872018-12-13 04:29:06927 }
Miyoung Shin1c565c912021-03-17 12:11:21928
Shivani Sharma298d12852019-01-22 20:04:03929 // Do nothing if all entries are skippable. Normally this path would not
930 // happen as consumers would have already checked it in CanGoBack but a lot of
931 // tests do not do that.
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42932 if (all_skippable_entries)
Shivani Sharma298d12852019-01-22 20:04:03933 return;
shivanisha55201872018-12-13 04:29:06934
shivanisha55201872018-12-13 04:29:06935 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20936}
937
[email protected]d202a7c2012-01-04 07:53:47938void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06939 int target_index = GetIndexForOffset(1);
940
Shivani Sharma2d5b4b6b2019-01-08 16:07:16941 // Note that at least one entry (the last one) will be non-skippable since
942 // entries are marked skippable only when they add another entry because of
943 // redirect or pushState.
Elly Fong-Jones40ee8112021-06-23 19:10:52944 while (target_index < static_cast<int>(entries_.size())) {
945 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui())
shivanisha55201872018-12-13 04:29:06946 break;
Elly Fong-Jones40ee8112021-06-23 19:10:52947 target_index++;
shivanisha55201872018-12-13 04:29:06948 }
shivanisha55201872018-12-13 04:29:06949 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20950}
951
[email protected]d202a7c2012-01-04 07:53:47952void NavigationControllerImpl::GoToIndex(int index) {
Dave Tapuska8bfd84c2019-03-26 20:47:16953 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId);
954}
955
956void NavigationControllerImpl::GoToIndex(int index,
957 int sandbox_frame_tree_node_id) {
sunjian30574a62017-03-21 21:39:44958 TRACE_EVENT0("browser,navigation,benchmark",
959 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:20960 if (index < 0 || index >= static_cast<int>(entries_.size())) {
961 NOTREACHED();
962 return;
963 }
964
[email protected]cbab76d2008-10-13 22:42:47965 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20966
arthursonzogni5c4c202d2017-04-25 23:41:27967 DCHECK_EQ(nullptr, pending_entry_);
968 DCHECK_EQ(-1, pending_entry_index_);
969 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:20970 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:27971 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
972 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Dave Tapuska8bfd84c2019-03-26 20:47:16973 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id);
[email protected]765b35502008-08-21 00:51:20974}
975
[email protected]d202a7c2012-01-04 07:53:47976void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:12977 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:03978 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:20979 return;
980
[email protected]9ba14052012-06-22 23:50:03981 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20982}
983
WangHui74286d52021-03-31 16:17:15984#if defined(OS_ANDROID)
985void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
986 // Note: This is actually reached in unit tests.
987 if (!CanGoToOffsetWithSkipping(offset))
988 return;
989
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42990 if (offset == 0) {
WangHui74286d52021-03-31 16:17:15991 GoToIndex(GetIndexForOffset(offset));
992 return;
993 }
994 int increment = offset > 0 ? 1 : -1;
995 // Find the offset without counting skippable entries.
996 int target_index = GetIndexForOffset(increment);
997 int non_skippable_entries = 0;
998 for (int index = target_index; index >= 0 && index < GetEntryCount();
999 index += increment) {
1000 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1001 non_skippable_entries++;
1002
1003 if (non_skippable_entries == std::abs(offset)) {
1004 target_index = index;
1005 break;
1006 }
1007 }
1008
1009 GoToIndex(target_index);
1010}
1011#endif
1012
[email protected]41374f12013-07-24 02:49:281013bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151014 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281015 return false;
[email protected]6a13a6c2011-12-20 21:47:121016
[email protected]43032342011-03-21 14:10:311017 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281018 return true;
[email protected]cbab76d2008-10-13 22:42:471019}
1020
Michael Thiessen9b14d512019-09-23 21:19:471021void NavigationControllerImpl::PruneForwardEntries() {
1022 DiscardNonCommittedEntries();
1023 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471024 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471025 if (num_removed <= 0)
1026 return;
1027 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1028 NotifyPrunedEntries(this, remove_start_index /* start index */,
1029 num_removed /* count */);
1030}
1031
Aran Gilman37d11632019-10-08 23:07:151032void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1033 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321034 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311035 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511036 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1037 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321038 }
1039}
1040
Harkiran Bolariaba823e42021-05-21 18:30:361041base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL(
1042 const GURL& url,
1043 const Referrer& referrer,
1044 ui::PageTransition transition,
1045 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471046 LoadURLParams params(url);
1047 params.referrer = referrer;
1048 params.transition_type = transition;
1049 params.extra_headers = extra_headers;
Harkiran Bolariaba823e42021-05-21 18:30:361050 return LoadURLWithParams(params);
[email protected]cf002332012-08-14 19:17:471051}
1052
Harkiran Bolariaba823e42021-05-21 18:30:361053base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams(
1054 const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061055 if (params.is_renderer_initiated)
1056 DCHECK(params.initiator_origin.has_value());
1057
naskob8744d22014-08-28 17:07:431058 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151059 "NavigationControllerImpl::LoadURLWithParams", "url",
1060 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291061 bool is_explicit_navigation =
1062 GetContentClient()->browser()->IsExplicitNavigation(
1063 params.transition_type);
1064 if (HandleDebugURL(params.url, params.transition_type,
1065 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431066 // If Telemetry is running, allow the URL load to proceed as if it's
1067 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491068 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431069 cc::switches::kEnableGpuBenchmarking))
Harkiran Bolariaba823e42021-05-21 18:30:361070 return nullptr;
[email protected]47752982014-07-29 08:01:431071 }
[email protected]8bf1048012012-02-08 01:22:181072
[email protected]cf002332012-08-14 19:17:471073 // Checks based on params.load_type.
1074 switch (params.load_type) {
1075 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431076 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471077 break;
1078 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261079 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471080 NOTREACHED() << "Data load must use data scheme.";
Harkiran Bolariaba823e42021-05-21 18:30:361081 return nullptr;
[email protected]cf002332012-08-14 19:17:471082 }
1083 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461084 }
[email protected]e47ae9472011-10-13 19:48:341085
[email protected]e47ae9472011-10-13 19:48:341086 // The user initiated a load, we don't need to reload anymore.
1087 needs_reload_ = false;
1088
Harkiran Bolariaba823e42021-05-21 18:30:361089 return NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441090}
1091
Mohamed Abdelhalim833de902019-09-16 17:41:451092bool NavigationControllerImpl::PendingEntryMatchesRequest(
1093 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191094 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451095 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191096}
1097
[email protected]d202a7c2012-01-04 07:53:471098bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321099 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071100 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331101 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441102 bool is_same_document_navigation,
Nate Chapinc7019dd7d2021-06-25 18:29:251103 bool was_on_initial_empty_document,
Shivani Sharmaffb32b82019-04-09 16:58:471104 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131105 NavigationRequest* navigation_request) {
1106 DCHECK(navigation_request);
[email protected]cd2e15742013-03-08 04:08:311107 is_initial_navigation_ = false;
1108
[email protected]0e8db942008-09-24 21:21:481109 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431110 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481111 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361112 if (entry_replaced_by_post_commit_error_) {
1113 if (is_same_document_navigation) {
1114 // Same document navigations should not be possible on error pages and
1115 // would leave the controller in a weird state. Kill the renderer if
1116 // that happens.
1117 bad_message::ReceivedBadMessage(
1118 rfh->GetProcess(), bad_message::NC_SAME_DOCUMENT_POST_COMMIT_ERROR);
1119 }
1120 // Any commit while a post-commit error page is showing should put the
1121 // original entry back, replacing the error page's entry. This includes
1122 // reloads, where the original entry was used as the pending entry and
1123 // should now be at the correct index at commit time.
1124 entries_[last_committed_entry_index_] =
1125 std::move(entry_replaced_by_post_commit_error_);
1126 }
Fergal Daly8e33cf62020-12-12 01:06:071127 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551128 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431129 if (pending_entry_ &&
1130 pending_entry_->GetIsOverridingUserAgent() !=
1131 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1132 overriding_user_agent_changed = true;
[email protected]0e8db942008-09-24 21:21:481133 } else {
Gang Wu325f03f42021-02-25 20:00:461134 // GetLastCommittedEntry() is null, so this is the first entry.
Fergal Daly8e33cf62020-12-12 01:06:071135 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481136 details->previous_entry_index = -1;
Gang Wu325f03f42021-02-25 20:00:461137 if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) {
1138 // Default setting is NOT override the user agent, so overriding the user
1139 // agent in first entry should be considered as user agent changed as
1140 // well.
1141 overriding_user_agent_changed = true;
1142 }
[email protected]0e8db942008-09-24 21:21:481143 }
[email protected]ecd9d8702008-08-28 22:10:171144
Alexander Timind2f2e4f22019-04-02 20:04:531145 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1146 // implementing back-forward cache.
1147
1148 // Create a new metrics object or reuse the previous one depending on whether
1149 // it's a main frame navigation or not.
1150 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics =
1151 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1152 GetLastCommittedEntry(), !rfh->GetParent(),
1153 params.document_sequence_number);
1154 // Notify the last active entry that we have navigated away.
1155 if (!rfh->GetParent() && !is_same_document_navigation) {
1156 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1157 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Hajime Hoshic7606502021-04-14 02:42:161158 metrics->MainFrameDidNavigateAwayFromDocument(rfh, navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531159 }
1160 }
1161 }
1162
fdegans9caf66a2015-07-30 21:10:421163 // If there is a pending entry at this point, it should have a SiteInstance,
1164 // except for restored entries.
jam48cea9082017-02-15 06:13:291165 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481166 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081167 pending_entry_->IsRestored());
1168 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151169 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291170 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481171 }
[email protected]e9ba4472008-09-14 15:42:431172
Nasko Oskovaee2f862018-06-15 00:05:521173 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1174 // has changed, this must be treated as a new navigation with replacement.
1175 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491176 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521177 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101178 pending_entry_->GetUniqueID() ==
1179 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521180 pending_entry_->site_instance() &&
1181 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1182 DCHECK_NE(-1, pending_entry_index_);
1183 // TODO(nasko,creis): Instead of setting this value here, set
1184 // should_replace_current_entry on the parameters we send to the
1185 // renderer process as part of CommitNavigation. The renderer should
1186 // in turn send it back here as part of |params| and it can be just
1187 // enforced and renderer process terminated on mismatch.
1188 details->did_replace_entry = true;
1189 } else {
1190 // The renderer tells us whether the navigation replaces the current entry.
1191 details->did_replace_entry = params.should_replace_current_entry;
1192 }
[email protected]bcd904482012-02-01 01:54:221193
[email protected]e9ba4472008-09-14 15:42:431194 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amnif6950d552020-11-24 03:26:101195 details->type = ClassifyNavigation(rfh, params, navigation_request);
[email protected]4bf3522c2010-08-19 21:00:201196
eugenebutee08663a2017-04-27 17:43:121197 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441198 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121199
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071200 details->is_prerender_activation =
1201 navigation_request->IsPrerenderedPageActivation();
1202
Peter Boströmd7592132019-01-30 04:50:311203 // Make sure we do not discard the pending entry for a different ongoing
1204 // navigation when a same document commit comes in unexpectedly from the
1205 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1206 // other navigation types. See https://crbug.com/900036.
1207 // TODO(crbug.com/926009): Handle history.pushState() as well.
1208 bool keep_pending_entry = is_same_document_navigation &&
Charlie Reisc0f17d2d2021-01-12 18:52:491209 details->type == NAVIGATION_TYPE_EXISTING_ENTRY &&
Peter Boströmd7592132019-01-30 04:50:311210 pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451211 !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311212
[email protected]0e8db942008-09-24 21:21:481213 switch (details->type) {
Charlie Reisc0f17d2d2021-01-12 18:52:491214 case NAVIGATION_TYPE_NEW_ENTRY:
1215 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051216 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451217 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431218 break;
Charlie Reisc0f17d2d2021-01-12 18:52:491219 case NAVIGATION_TYPE_EXISTING_ENTRY:
1220 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1221 was_restored, navigation_request,
1222 keep_pending_entry);
[email protected]e9ba4472008-09-14 15:42:431223 break;
[email protected]8ff00d72012-10-23 19:12:211224 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471225 RendererDidNavigateNewSubframe(
1226 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451227 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431228 break;
[email protected]8ff00d72012-10-23 19:12:211229 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391230 if (!RendererDidNavigateAutoSubframe(
Nate Chapinc7019dd7d2021-06-25 18:29:251231 rfh, params, details->is_same_document,
1232 was_on_initial_empty_document, navigation_request)) {
creisce0ef3572017-01-26 17:53:081233 // We don't send a notification about auto-subframe PageState during
1234 // UpdateStateForFrame, since it looks like nothing has changed. Send
1235 // it here at commit time instead.
1236 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431237 return false;
creis59d5a47cb2016-08-24 23:57:191238 }
[email protected]e9ba4472008-09-14 15:42:431239 break;
[email protected]8ff00d72012-10-23 19:12:211240 case NAVIGATION_TYPE_NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:491241 // If a pending navigation was in progress, this canceled it. We should
1242 // discard it and make sure it is removed from the URL bar. After that,
1243 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:431244 // the caller that nothing has happened.
arthursonzogni69a6a1b2019-09-17 09:23:001245 if (pending_entry_)
[email protected]20d1c992011-04-12 21:17:491246 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431247 return false;
Aran Gilman37d11632019-10-08 23:07:151248 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431249 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151250 break;
[email protected]765b35502008-08-21 00:51:201251 }
1252
[email protected]688aa65c62012-09-28 04:32:221253 // At this point, we know that the navigation has just completed, so
1254 // record the time.
1255 //
1256 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261257 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331258 base::Time timestamp =
1259 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1260 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131261 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221262
Peter Boströmd7592132019-01-30 04:50:311263 // If we aren't keeping the pending entry, there shouldn't be one at this
1264 // point. Clear it again in case any error cases above forgot to do so.
1265 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1266 // been cleared instead of protecting against it.
1267 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001268 DiscardNonCommittedEntries();
[email protected]f233e4232013-02-23 00:55:141269
[email protected]e9ba4472008-09-14 15:42:431270 // All committed entries should have nonempty content state so WebKit doesn't
1271 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471272 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321273 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221274 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441275 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531276 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1277 // implementing back-forward cache.
1278 if (!active_entry->back_forward_cache_metrics()) {
1279 active_entry->set_back_forward_cache_metrics(
1280 std::move(back_forward_cache_metrics));
1281 }
1282 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
Hajime Hoshi446206e2019-10-18 18:36:251283 navigation_request,
1284 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
naskoc7533512016-05-06 17:01:121285
Charles Reisc0507202017-09-21 00:40:021286 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1287 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1288 // A mismatch can occur if the renderer lies or due to a unique name collision
1289 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121290 FrameNavigationEntry* frame_entry =
1291 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021292 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1293 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031294 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081295 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1296 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031297 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201298
1299 // Remember the bindings the renderer process has at this point, so that
1300 // we do not grant this entry additional bindings if we come back to it.
1301 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301302 }
[email protected]132e281a2012-07-31 18:32:441303
[email protected]97d8f0d2013-10-29 16:49:211304 // Once it is committed, we no longer need to track several pieces of state on
1305 // the entry.
naskoc7533512016-05-06 17:01:121306 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131307
[email protected]49bd30e62011-03-22 20:12:591308 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221309 // TODO(creis): This check won't pass for subframes until we create entries
1310 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001311 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421312 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591313
[email protected]e9ba4472008-09-14 15:42:431314 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001315 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001316 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311317 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531318
arthursonzogni7ddc6542021-04-09 09:16:501319 active_entry->SetIsOverridingUserAgent(
1320 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031321
[email protected]93f230e02011-06-01 14:40:001322 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291323
John Abd-El-Malek380d3c5922017-09-08 00:20:311324 if (active_entry->GetURL().SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121325 !navigation_request->DidEncounterError()) {
John Abd-El-Malek969cdd94e2017-07-10 22:18:161326 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus",
1327 !!active_entry->GetSSL().certificate);
1328 }
1329
aelias100c9192017-01-13 00:01:431330 if (overriding_user_agent_changed)
1331 delegate_->UpdateOverridingUserAgent();
1332
creis03b48002015-11-04 00:54:561333 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1334 // one knows the latest NavigationEntry it is showing (whether it has
1335 // committed anything in this navigation or not). This allows things like
1336 // state and title updates from RenderFrames to apply to the latest relevant
1337 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381338 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001339 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381340 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
Hayato Ito2c8c08d02021-06-23 03:38:431341
1342 if (navigation_request->IsPrerenderedPageActivation()) {
1343 BroadcastHistoryOffsetAndLength();
1344 // TODO(crbug.com/1222893): Broadcasting happens after the prerendered page
1345 // is activated. As a result, a "prerenderingchange" event listener sees the
1346 // history.length which is not updated yet. We should guarantee that
1347 // history's length and offset should be updated before a
1348 // "prerenderingchange" event listener runs. One possible approach is to use
1349 // the same IPC which "prerenderingchange" uses, and propagate history's
1350 // length and offset together with that.
1351 }
1352
[email protected]e9ba4472008-09-14 15:42:431353 return true;
initial.commit09911bf2008-07-26 23:55:291354}
1355
[email protected]8ff00d72012-10-23 19:12:211356NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321357 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101358 const mojom::DidCommitProvisionalLoadParams& params,
1359 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591360 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511361 "ClassifyNavigation");
1362
avi7c6f35e2015-05-08 17:52:381363 if (params.did_create_new_entry) {
Charlie Reisc0f17d2d2021-01-12 18:52:491364 // A new entry. We may or may not have a corresponding pending entry, and
avi7c6f35e2015-05-08 17:52:381365 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001366 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491367 trace_return.set_return_reason("new entry, no parent, new entry");
1368 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381369 }
1370
1371 // When this is a new subframe navigation, we should have a committed page
1372 // in which it's a subframe. This may not be the case when an iframe is
1373 // navigated on a popup navigated to about:blank (the iframe would be
1374 // written into the popup by script on the main page). For these cases,
1375 // there isn't any navigation stuff we can do, so just ignore it.
Nasko Oskovae49e292020-08-13 02:08:511376 if (!GetLastCommittedEntry()) {
1377 trace_return.set_return_reason("new entry, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381378 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511379 }
avi7c6f35e2015-05-08 17:52:381380
1381 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511382 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381383 return NAVIGATION_TYPE_NEW_SUBFRAME;
1384 }
1385
Charlie Reisc0f17d2d2021-01-12 18:52:491386 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381387 DCHECK(!params.history_list_was_cleared);
1388
avi39c1edd32015-06-04 20:06:001389 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381390 // All manual subframes would be did_create_new_entry and handled above, so
1391 // we know this is auto.
Nasko Oskovae49e292020-08-13 02:08:511392 if (GetLastCommittedEntry()) {
1393 trace_return.set_return_reason("subframe, last commmited, auto subframe");
avi7c6f35e2015-05-08 17:52:381394 return NAVIGATION_TYPE_AUTO_SUBFRAME;
Nasko Oskovae49e292020-08-13 02:08:511395 }
Lei Zhang96031532019-10-10 19:05:471396
1397 // We ignore subframes created in non-committed pages; we'd appreciate if
1398 // people stopped doing that.
Nasko Oskovae49e292020-08-13 02:08:511399 trace_return.set_return_reason("subframe, no last commmited, ignore");
Lei Zhang96031532019-10-10 19:05:471400 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381401 }
1402
Rakina Zata Amnif6950d552020-11-24 03:26:101403 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1404 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381405 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1406 // create a new page.
1407
1408 // Just like above in the did_create_new_entry case, it's possible to
1409 // scribble onto an uncommitted page. Again, there isn't any navigation
1410 // stuff that we can do, so ignore it here as well.
avi3a5b8f32015-05-28 17:50:231411 NavigationEntry* last_committed = GetLastCommittedEntry();
Nasko Oskovae49e292020-08-13 02:08:511412 if (!last_committed) {
1413 trace_return.set_return_reason("nav entry 0, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381414 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511415 }
avi7c6f35e2015-05-08 17:52:381416
Charles Reis1378111f2017-11-08 21:44:061417 // This is history.replaceState() or history.reload().
Nasko Oskov332593c2018-08-16 17:21:341418 // TODO(nasko): With error page isolation, reloading an existing session
1419 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491420 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341421 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511422 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491423 "nav entry 0, last committed, existing entry");
1424 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381425 }
1426
Rakina Zata Amnif6950d552020-11-24 03:26:101427 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521428 // If the SiteInstance of the |pending_entry_| does not match the
1429 // SiteInstance that got committed, treat this as a new navigation with
1430 // replacement. This can happen if back/forward/reload encounters a server
1431 // redirect to a different site or an isolated error page gets successfully
1432 // reloaded into a different SiteInstance.
1433 if (pending_entry_->site_instance() &&
1434 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491435 trace_return.set_return_reason("pending matching nav entry, new entry");
1436 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521437 }
creis77c9aa32015-09-25 19:59:421438
Nasko Oskovaee2f862018-06-15 00:05:521439 if (pending_entry_index_ == -1) {
1440 // In this case, we have a pending entry for a load of a new URL but Blink
1441 // didn't do a new navigation (params.did_create_new_entry). First check
1442 // to make sure Blink didn't treat a new cross-process navigation as
1443 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161444 // we must treat it as NEW rather than the converted reload case below,
1445 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521446 if (!GetLastCommittedEntry() ||
1447 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161448 trace_return.set_return_reason("new pending, new entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491449 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521450 }
1451
1452 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161453 // We will create a pending entry, but NavigateWithoutEntry will convert
1454 // it to a reload since it's the same page and not create a new entry for
1455 // it. (The user doesn't want to have a new back/forward entry when they
1456 // do this.) Therefore we want to just ignore the pending entry and go
1457 // back to where we were (the "existing entry").
1458 trace_return.set_return_reason("new pending, existing (same) entry");
1459 return NAVIGATION_TYPE_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521460 }
avi7c6f35e2015-05-08 17:52:381461 }
1462
creis26d22632017-04-21 20:23:561463 // Everything below here is assumed to be an existing entry, but if there is
1464 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511465 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491466 trace_return.set_return_reason("no last committed, new entry");
1467 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511468 }
creis26d22632017-04-21 20:23:561469
avi7c6f35e2015-05-08 17:52:381470 if (params.intended_as_new_entry) {
1471 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491472 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1473 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161474 trace_return.set_return_reason("intended as new entry, existing entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491475 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381476 }
1477
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121478 if (navigation_request->DidEncounterError() &&
1479 failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101480 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381481 // If the renderer was going to a new pending entry that got cleared because
1482 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491483 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381484 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511485 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491486 "unreachable, matching pending, existing entry");
1487 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381488 }
1489
Charlie Reisc0f17d2d2021-01-12 18:52:491490 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101491 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511492 trace_return.traced_value()->SetInteger("existing_entry_index",
1493 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381494 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441495 // The renderer has committed a navigation to an entry that no longer
1496 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491497 trace_return.set_return_reason("existing entry -1, new entry");
1498 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381499 }
1500
avi7c6f35e2015-05-08 17:52:381501 // Since we weeded out "new" navigations above, we know this is an existing
1502 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491503 trace_return.set_return_reason("default return, existing entry");
1504 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381505}
1506
Charlie Reisc0f17d2d2021-01-12 18:52:491507void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321508 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071509 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361510 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441511 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471512 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451513 NavigationRequest* request) {
dcheng9bfa5162016-04-09 01:00:571514 std::unique_ptr<NavigationEntryImpl> new_entry;
creisf49dfc92016-07-26 17:05:181515 bool update_virtual_url = false;
1516
Anton Bikineevf62d1bf2021-05-15 17:56:071517 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451518 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061519
creisf49dfc92016-07-26 17:05:181520 // First check if this is an in-page navigation. If so, clone the current
1521 // entry instead of looking at the pending entry, because the pending entry
1522 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361523 if (is_same_document && GetLastCommittedEntry()) {
Patrick Monette50e8bd82019-06-13 22:40:451524 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481525 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081526 params.document_sequence_number, params.app_history_key,
1527 rfh->GetSiteInstance(), nullptr, params.url,
1528 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091529 Referrer(*params.referrer), initiator_origin,
1530 request->GetRedirectChain(), params.page_state, params.method,
1531 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391532 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481533 request->GetSubresourceWebBundleNavigationInfo(),
Antonio Sartori78a749f2020-11-30 12:03:391534 // We will set the document policies later in this function.
Titouan Rigoudy6ec70402021-02-02 15:42:191535 nullptr /* policy_container_policies */);
Charles Reisf44482022017-10-13 21:15:031536
creisf49dfc92016-07-26 17:05:181537 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Carlos Caballero40b0efd2021-01-26 11:55:001538 frame_entry, true, rfh->frame_tree_node(), frame_tree_.root());
jama78746e2017-02-22 17:21:571539 if (new_entry->GetURL().GetOrigin() != params.url.GetOrigin()) {
1540 // TODO(jam): we had one report of this with a URL that was redirecting to
1541 // only tildes. Until we understand that better, don't copy the cert in
1542 // this case.
1543 new_entry->GetSSL() = SSLStatus();
John Abd-El-Malek9cd697e2017-07-12 21:53:141544
John Abd-El-Malek380d3c5922017-09-08 00:20:311545 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121546 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141547 UMA_HISTOGRAM_BOOLEAN(
1548 "Navigation.SecureSchemeHasSSLStatus.NewPageInPageOriginMismatch",
1549 !!new_entry->GetSSL().certificate);
1550 }
jama78746e2017-02-22 17:21:571551 }
creisf49dfc92016-07-26 17:05:181552
Patrick Monette50e8bd82019-06-13 22:40:451553 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1554 // that |frame_entry| should now have a reference count of exactly 2: one
1555 // due to the local variable |frame_entry|, and another due to |new_entry|
1556 // also retaining one. This should never fail, because it's the main frame.
1557 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181558
1559 update_virtual_url = new_entry->update_virtual_url_with_url();
John Abd-El-Malek9cd697e2017-07-12 21:53:141560
John Abd-El-Malek380d3c5922017-09-08 00:20:311561 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121562 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141563 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus.NewPageInPage",
1564 !!new_entry->GetSSL().certificate);
1565 }
creisf49dfc92016-07-26 17:05:181566 }
1567
Harkiran Bolaria59290d62021-03-17 01:53:011568 // If this is an activation navigation from a prerendered page, transfer the
1569 // new entry from an entry already created and stored in the
1570 // NavigationRequest. |new_entry| will not have been set prior to this as
1571 // |is_same_document| is mutually exclusive with
1572 // |IsPrerenderedPageActivation|.
1573 if (request->IsPrerenderedPageActivation()) {
1574 DCHECK(!is_same_document);
1575 DCHECK(!new_entry);
1576 new_entry = request->TakePrerenderNavigationEntry();
1577 DCHECK(new_entry);
1578 }
1579
Charlie Reisc0f17d2d2021-01-12 18:52:491580 // Only make a copy of the pending entry if it is appropriate for the new
1581 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451582 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041583 // 1. The SiteInstance hasn't been assigned to something else.
1584 // 2. The pending entry was intended as a new entry, rather than being a
1585 // history navigation that was interrupted by an unrelated,
1586 // renderer-initiated navigation.
1587 // TODO(csharrison): Investigate whether we can remove some of the coarser
1588 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451589 if (!new_entry && PendingEntryMatchesRequest(request) &&
1590 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341591 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431592 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351593 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431594
[email protected]f1eb87a2011-05-06 17:49:411595 update_virtual_url = new_entry->update_virtual_url_with_url();
Camille Lamy62b826012019-02-26 09:15:471596 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451597 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141598
John Abd-El-Malek380d3c5922017-09-08 00:20:311599 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121600 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141601 UMA_HISTOGRAM_BOOLEAN(
1602 "Navigation.SecureSchemeHasSSLStatus.NewPagePendingEntryMatches",
1603 !!new_entry->GetSSL().certificate);
1604 }
creisf49dfc92016-07-26 17:05:181605 }
1606
Charlie Reisc0f17d2d2021-01-12 18:52:491607 // For cross-document commits with no matching pending entry, create a new
1608 // entry.
creisf49dfc92016-07-26 17:05:181609 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061610 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071611 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1612 initiator_origin,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:581613 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451614 params.transition, request->IsRendererInitiated(),
Lukasz Anforowicz435bcb582019-07-12 20:50:061615 nullptr); // blob_url_loader_factory
[email protected]f8f93eb2012-09-25 03:06:241616
1617 // Find out whether the new entry needs to update its virtual URL on URL
1618 // change and set up the entry accordingly. This is needed to correctly
1619 // update the virtual URL when replaceState is called after a pushState.
1620 GURL url = params.url;
1621 bool needs_update = false;
[email protected]47752982014-07-29 08:01:431622 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1623 &url, browser_context_, &needs_update);
[email protected]f8f93eb2012-09-25 03:06:241624 new_entry->set_update_virtual_url_with_url(needs_update);
1625
Charlie Reisc0f17d2d2021-01-12 18:52:491626 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411627 // update the virtual URL based on the new URL. For example, this is needed
1628 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1629 // the URL.
[email protected]f8f93eb2012-09-25 03:06:241630 update_virtual_url = needs_update;
Camille Lamy62b826012019-02-26 09:15:471631 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451632 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141633
John Abd-El-Malek380d3c5922017-09-08 00:20:311634 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121635 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141636 UMA_HISTOGRAM_BOOLEAN(
1637 "Navigation.SecureSchemeHasSSLStatus.NewPageNoMatchingEntry",
1638 !!new_entry->GetSSL().certificate);
1639 }
[email protected]e9ba4472008-09-14 15:42:431640 }
1641
Harkiran Bolaria59290d62021-03-17 01:53:011642 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
1643 // for prerendered contents, if any. This is because prerendering/activation
1644 // technically won't be creating a new document. Unlike BFCache, prerendering
1645 // creates a new NavigationEntry rather than using an existing one.
1646 if (!request->IsPrerenderedPageActivation()) {
1647 // Don't use the page type from the pending entry. Some interstitial page
1648 // may have set the type to interstitial. Once we commit, however, the page
1649 // type must always be normal or error.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121650 new_entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1651 : PAGE_TYPE_NORMAL);
Harkiran Bolaria59290d62021-03-17 01:53:011652 new_entry->SetURL(params.url);
1653 if (update_virtual_url)
1654 UpdateVirtualURLToURL(new_entry.get(), params.url);
1655 new_entry->SetReferrer(Referrer(*params.referrer));
1656 new_entry->SetTransitionType(params.transition);
1657 new_entry->set_site_instance(
1658 static_cast<SiteInstanceImpl*>(rfh->GetSiteInstance()));
1659 new_entry->SetOriginalRequestURL(request->GetOriginalRequestURL());
Rakina Zata Amnib597d632020-12-01 00:56:001660
Antonio Sartori4f5373792021-05-31 10:56:471661 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
Harkiran Bolaria59290d62021-03-17 01:53:011662 new_entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
[email protected]e9ba4472008-09-14 15:42:431663
Harkiran Bolaria59290d62021-03-17 01:53:011664 // Update the FrameNavigationEntry for new main frame commits.
1665 FrameNavigationEntry* frame_entry =
1666 new_entry->GetFrameEntry(rfh->frame_tree_node());
1667 frame_entry->set_frame_unique_name(rfh->frame_tree_node()->unique_name());
1668 frame_entry->set_item_sequence_number(params.item_sequence_number);
1669 frame_entry->set_document_sequence_number(params.document_sequence_number);
Nate Chapinfbfe5af2021-06-10 17:22:081670 frame_entry->set_app_history_key(params.app_history_key);
Harkiran Bolaria59290d62021-03-17 01:53:011671 frame_entry->set_redirect_chain(request->GetRedirectChain());
1672 frame_entry->SetPageState(params.page_state);
1673 frame_entry->set_method(params.method);
1674 frame_entry->set_post_id(params.post_id);
1675 frame_entry->set_policy_container_policies(
1676 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1677 request));
Antonio Sartori78a749f2020-11-30 12:03:391678
Anton Bikineevf62d1bf2021-05-15 17:56:071679 if (absl::optional<url::Origin> committed_origin =
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121680 GetCommittedOriginForFrameEntry(params, request)) {
1681 if (committed_origin.has_value())
1682 frame_entry->set_committed_origin(committed_origin.value());
1683 }
Harkiran Bolaria59290d62021-03-17 01:53:011684 if (request->web_bundle_navigation_info()) {
1685 frame_entry->set_web_bundle_navigation_info(
1686 request->web_bundle_navigation_info()->Clone());
1687 }
creis8b5cd4c2015-06-19 00:11:081688
Harkiran Bolaria59290d62021-03-17 01:53:011689 // history.pushState() is classified as a navigation to a new page, but sets
1690 // is_same_document to true. In this case, we already have the title and
1691 // favicon available, so set them immediately.
1692 if (is_same_document && GetLastCommittedEntry()) {
1693 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
1694 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1695 }
[email protected]3a868f212014-08-09 10:41:191696 }
[email protected]ff64b3e2014-05-31 04:07:331697
[email protected]60d6cca2013-04-30 08:47:131698 DCHECK(!params.history_list_was_cleared || !replace_entry);
1699 // The browser requested to clear the session history when it initiated the
1700 // navigation. Now we know that the renderer has updated its state accordingly
1701 // and it is safe to also clear the browser side history.
1702 if (params.history_list_was_cleared) {
arthursonzogni69a6a1b2019-09-17 09:23:001703 DiscardNonCommittedEntries();
[email protected]60d6cca2013-04-30 08:47:131704 entries_.clear();
1705 last_committed_entry_index_ = -1;
1706 }
1707
Nasko Oskovaee2f862018-06-15 00:05:521708 // If this is a new navigation with replacement and there is a
1709 // pending_entry_ which matches the navigation reported by the renderer
1710 // process, then it should be the one replaced, so update the
1711 // last_committed_entry_index_ to use it.
1712 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101713 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521714 last_committed_entry_index_ = pending_entry_index_;
1715 }
1716
Alexander Timine3ec4192020-04-20 16:39:401717 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411718 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401719 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051720
Carlos IL42b416592019-10-07 23:10:361721 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
1722 !request->post_commit_error_page_html().empty());
[email protected]e9ba4472008-09-14 15:42:431723}
1724
Charlie Reisc0f17d2d2021-01-12 18:52:491725void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321726 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071727 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361728 bool is_same_document,
jam48cea9082017-02-15 06:13:291729 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451730 NavigationRequest* request,
Peter Boströmd7592132019-01-30 04:50:311731 bool keep_pending_entry) {
creis26d22632017-04-21 20:23:561732 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1733 << "that a last committed entry exists.";
1734
[email protected]e9ba4472008-09-14 15:42:431735 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001736 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431737
Charlie Reis7e2cb6d2021-01-26 01:27:161738 NavigationEntryImpl* entry = nullptr;
avicbdc4c12015-07-01 16:07:111739 if (params.intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:161740 // We're guaranteed to have a last committed entry if intended_as_new_entry
1741 // is true.
avicbdc4c12015-07-01 16:07:111742 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:161743 DCHECK(entry);
1744
1745 // If the NavigationRequest matches a new pending entry and is classified as
1746 // EXISTING_ENTRY, then it is a navigation to the same URL that was
1747 // converted to a reload, such as a user pressing enter in the omnibox.
1748 if (pending_entry_ && pending_entry_index_ == -1 &&
1749 pending_entry_->GetUniqueID() ==
1750 request->commit_params().nav_entry_id) {
1751 // Note: The pending entry will usually have a real ReloadType here, but
1752 // it can still be ReloadType::NONE in cases that
1753 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
1754
1755 // If we classified this correctly, the SiteInstance should not have
1756 // changed.
1757 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
1758
1759 // For converted reloads, we assign the entry's unique ID to be that of
1760 // the new one. Since this is always the result of a user action, we want
1761 // to dismiss infobars, etc. like a regular user-initiated navigation.
1762 entry->set_unique_id(pending_entry_->GetUniqueID());
1763
1764 // The extra headers may have changed due to reloading with different
1765 // headers.
1766 entry->set_extra_headers(pending_entry_->extra_headers());
1767 }
1768 // Otherwise, this was intended as a new entry but the pending entry was
1769 // lost in the meantime and no new entry was created. We are stuck at the
1770 // last committed entry.
1771
1772 // Even if this is a converted reload from pressing enter in the omnibox,
1773 // the server could redirect, requiring an update to the SSL status. If this
1774 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451775 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:161776 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:471777 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451778 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:161779 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141780
Charlie Reis7e2cb6d2021-01-26 01:27:161781 if (params.url.SchemeIs(url::kHttpsScheme) &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121782 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141783 bool has_cert = !!entry->GetSSL().certificate;
1784 if (is_same_document) {
1785 UMA_HISTOGRAM_BOOLEAN(
1786 "Navigation.SecureSchemeHasSSLStatus."
1787 "ExistingPageSameDocumentIntendedAsNew",
1788 has_cert);
1789 } else {
1790 UMA_HISTOGRAM_BOOLEAN(
1791 "Navigation.SecureSchemeHasSSLStatus."
1792 "ExistingPageDifferentDocumentIntendedAsNew",
1793 has_cert);
1794 }
1795 }
Rakina Zata Amnif6950d552020-11-24 03:26:101796 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:111797 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:101798 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b90ce2016-09-01 16:58:161799
eugenebut604866f2017-05-10 21:35:361800 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451801 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361802 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1803 // this was a restored same document navigation entry, then it won't have
1804 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1805 // navigated it.
jam48cea9082017-02-15 06:13:291806 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
1807 if (entry->GetURL().GetOrigin() == last_entry->GetURL().GetOrigin() &&
1808 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1809 was_restored) {
1810 entry->GetSSL() = last_entry->GetSSL();
1811 }
1812 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451813 // In rapid back/forward navigations |request| sometimes won't have a cert
1814 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191815 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451816 if (request->GetSSLInfo().has_value() &&
1817 request->GetSSLInfo()->is_valid()) {
1818 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
1819 } else if (entry->GetURL().GetOrigin() != request->GetURL().GetOrigin()) {
John Abd-El-Malek3f247082017-12-07 19:02:191820 entry->GetSSL() = SSLStatus();
1821 }
jam48cea9082017-02-15 06:13:291822 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141823
John Abd-El-Malek380d3c5922017-09-08 00:20:311824 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121825 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141826 bool has_cert = !!entry->GetSSL().certificate;
1827 if (is_same_document && was_restored) {
1828 UMA_HISTOGRAM_BOOLEAN(
1829 "Navigation.SecureSchemeHasSSLStatus."
1830 "ExistingPageSameDocumentRestoredBrowserInitiated",
1831 has_cert);
1832 } else if (is_same_document && !was_restored) {
1833 UMA_HISTOGRAM_BOOLEAN(
1834 "Navigation.SecureSchemeHasSSLStatus."
1835 "ExistingPageSameDocumentBrowserInitiated",
1836 has_cert);
1837 } else if (!is_same_document && was_restored) {
1838 UMA_HISTOGRAM_BOOLEAN(
1839 "Navigation.SecureSchemeHasSSLStatus."
1840 "ExistingPageRestoredBrowserInitiated",
1841 has_cert);
1842 } else {
1843 UMA_HISTOGRAM_BOOLEAN(
1844 "Navigation.SecureSchemeHasSSLStatus.ExistingPageBrowserInitiated",
1845 has_cert);
1846 }
1847 }
avicbdc4c12015-07-01 16:07:111848 } else {
1849 // This is renderer-initiated. The only kinds of renderer-initated
Charlie Reisc0f17d2d2021-01-12 18:52:491850 // navigations that are EXISTING_ENTRY are reloads and history.replaceState,
avicbdc4c12015-07-01 16:07:111851 // which land us at the last committed entry.
1852 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101853
Mikel Astizba9cf2fd2017-12-17 10:38:101854 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1855 // to avoid misleading interpretations (e.g. URLs paired with
1856 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1857 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1858 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1859
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571860 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:101861
eugenebut604866f2017-05-10 21:35:361862 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451863 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361864 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471865 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451866 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141867
John Abd-El-Malek380d3c5922017-09-08 00:20:311868 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121869 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141870 bool has_cert = !!entry->GetSSL().certificate;
1871 if (is_same_document) {
1872 UMA_HISTOGRAM_BOOLEAN(
1873 "Navigation.SecureSchemeHasSSLStatus."
1874 "ExistingPageSameDocumentRendererInitiated",
1875 has_cert);
1876 } else {
1877 UMA_HISTOGRAM_BOOLEAN(
1878 "Navigation.SecureSchemeHasSSLStatus."
1879 "ExistingPageDifferentDocumentRendererInitiated",
1880 has_cert);
1881 }
1882 }
avicbdc4c12015-07-01 16:07:111883 }
1884 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:431885
[email protected]5ccd4dc2012-10-24 02:28:141886 // The URL may have changed due to redirects.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121887 entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1888 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041889 entry->SetURL(params.url);
arthursonzogni73fe3212020-11-17 13:24:071890 entry->SetReferrer(Referrer(*params.referrer));
[email protected]38178a42009-12-17 18:58:321891 if (entry->update_virtual_url_with_url())
1892 UpdateVirtualURLToURL(entry, params.url);
[email protected]5ccd4dc2012-10-24 02:28:141893
jam015ba062017-01-06 21:17:001894 // The site instance will normally be the same except
1895 // 1) session restore, when no site instance will be assigned or
1896 // 2) redirect, when the site instance is reset.
Lei Zhang96031532019-10-10 19:05:471897 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
naskoaf182192016-08-11 02:12:011898 entry->site_instance() == rfh->GetSiteInstance());
clamy432acb22016-04-15 19:41:431899
naskoaf182192016-08-11 02:12:011900 // Update the existing FrameNavigationEntry to ensure all of its members
1901 // reflect the parameters coming from the renderer process.
Anton Bikineevf62d1bf2021-05-15 17:56:071902 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451903 request->common_params().initiator_origin;
naskoaf182192016-08-11 02:12:011904 entry->AddOrUpdateFrameEntry(
Nate Chapin9f169072021-06-09 19:32:371905 rfh->frame_tree_node(), NavigationEntryImpl::UpdatePolicy::kUpdate,
1906 params.item_sequence_number, params.document_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081907 params.app_history_key, rfh->GetSiteInstance(), nullptr, params.url,
Nate Chapin9f169072021-06-09 19:32:371908 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091909 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071910 params.page_state, params.method, params.post_id,
1911 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031912 request->web_bundle_navigation_info()
1913 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391914 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481915 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191916 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1917 request));
creis22a7b4c2016-04-28 07:20:301918
[email protected]5ccd4dc2012-10-24 02:28:141919 // The redirected to page should not inherit the favicon from the previous
1920 // page.
eugenebut604866f2017-05-10 21:35:361921 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:481922 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:141923
Peter Boströmd7592132019-01-30 04:50:311924 // We should also usually discard the pending entry if it corresponds to a
1925 // different navigation, since that one is now likely canceled. In rare
1926 // cases, we leave the pending entry for another navigation in place when we
1927 // know it is still ongoing, to avoid a flicker in the omnibox (see
1928 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:431929 //
1930 // Note that we need to use the "internal" version since we don't want to
1931 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:311932 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001933 DiscardNonCommittedEntries();
[email protected]40bcc302009-03-02 20:50:391934
Carlos IL4dea8902020-05-26 15:14:291935 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:111936 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:431937}
1938
[email protected]d202a7c2012-01-04 07:53:471939void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:321940 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071941 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361942 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:471943 bool replace_entry,
1944 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451945 NavigationRequest* request) {
avi25f5f9e2015-07-17 20:08:261946 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1947 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
[email protected]09b8f82f2009-06-16 20:22:111948
[email protected]e9ba4472008-09-14 15:42:431949 // Manual subframe navigations just get the current entry cloned so the user
1950 // can go back or forward to it. The actual subframe information will be
1951 // stored in the page state for each of those entries. This happens out of
1952 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:091953 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1954 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:381955
Mikel Astizba9cf2fd2017-12-17 10:38:101956 // The DCHECK below documents the fact that we don't know of any situation
1957 // where |replace_entry| is true for subframe navigations. This simplifies
1958 // reasoning about the replacement struct for subframes (see
1959 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
1960 DCHECK(!replace_entry);
1961
Patrick Monette50e8bd82019-06-13 22:40:451962 // This FrameNavigationEntry might not end up being used in the
1963 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Anton Bikineevf62d1bf2021-05-15 17:56:071964 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451965 request->common_params().initiator_origin;
Patrick Monette50e8bd82019-06-13 22:40:451966 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481967 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081968 params.document_sequence_number, params.app_history_key,
1969 rfh->GetSiteInstance(), nullptr, params.url,
1970 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091971 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071972 params.page_state, params.method, params.post_id,
1973 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031974 request->web_bundle_navigation_info()
1975 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391976 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481977 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191978 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1979 request));
Charles Reisf44482022017-10-13 21:15:031980
creisce0ef3572017-01-26 17:53:081981 std::unique_ptr<NavigationEntryImpl> new_entry =
1982 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:451983 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:001984 frame_tree_.root());
creise062d542015-08-25 02:01:551985
Alexander Timine3ec4192020-04-20 16:39:401986 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411987 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401988 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:471989
creisce0ef3572017-01-26 17:53:081990 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:451991 // to replace, which can happen due to a unique name change. See
1992 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
1993 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:381994
Carlos IL42b416592019-10-07 23:10:361995 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false);
[email protected]e9ba4472008-09-14 15:42:431996}
1997
[email protected]d202a7c2012-01-04 07:53:471998bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:321999 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072000 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392001 bool is_same_document,
Nate Chapinc7019dd7d2021-06-25 18:29:252002 bool was_on_initial_empty_document,
Mohamed Abdelhalim833de902019-09-16 17:41:452003 NavigationRequest* request) {
avi9f07a0c2015-02-18 22:51:292004 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2005 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2006
[email protected]e9ba4472008-09-14 15:42:432007 // We're guaranteed to have a previously committed entry, and we now need to
2008 // handle navigation inside of a subframe in it without creating a new entry.
2009 DCHECK(GetLastCommittedEntry());
2010
creis913c63ce2016-07-16 19:52:522011 // For newly created subframes, we don't need to send a commit notification.
2012 // This is only necessary for history navigations in subframes.
2013 bool send_commit_notification = false;
2014
Rakina Zata Amnif6950d552020-11-24 03:26:102015 // If |nav_entry_id| is non-zero and matches an existing entry, this
2016 // is a history navigation. Update the last committed index accordingly. If
2017 // we don't recognize the |nav_entry_id|, it might be a recently
2018 // pruned entry. We'll handle it below.
2019 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2020 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472021 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062022 // Make sure that a subframe commit isn't changing the main frame's
2023 // origin. Otherwise the renderer process may be confused, leading to a
2024 // URL spoof. We can't check the path since that may change
2025 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572026 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2027 // about:blank, file, and unique origins are more subtle to get right.
Charlie Reis95fbca442021-05-21 21:38:242028 // We should use checks similar to RenderFrameHostImpl's
2029 // CanCommitUrlAndOrigin on the main frame during subframe commits.
2030 // See https://crbug.com/1209092.
creis37988b92016-06-10 18:03:572031 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2032 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2033 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2034 dest_top_url.SchemeIsHTTPOrHTTPS() &&
2035 current_top_url.GetOrigin() != dest_top_url.GetOrigin()) {
creisfb6eeb62016-05-10 19:01:512036 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2037 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062038 }
creis3cdc3b02015-05-29 23:00:472039
creis913c63ce2016-07-16 19:52:522040 // We only need to discard the pending entry in this history navigation
2041 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062042 last_committed_entry_index_ = entry_index;
arthursonzogni69a6a1b2019-09-17 09:23:002043 DiscardNonCommittedEntries();
creis913c63ce2016-07-16 19:52:522044
2045 // History navigations should send a commit notification.
2046 send_commit_notification = true;
avi98405c22015-05-21 20:47:062047 }
[email protected]e9ba4472008-09-14 15:42:432048 }
[email protected]f233e4232013-02-23 00:55:142049
creisce0ef3572017-01-26 17:53:082050 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2051 // it may be a "history auto" case where we update an existing one.
2052 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Nate Chapin9f169072021-06-09 19:32:372053
2054 // We may want to update |last_committed|'s FrameNavigationEntry (if one
2055 // exists), or we may want to clobber it and create a new one. We update in
2056 // cases where the corresponding FrameNavigationEntry is conceptually similar
2057 // to the document described by the commit params: same-document
2058 // navigations, history traversal to an existing entry, and reloads (including
2059 // a "soft reload" where we navigate to the same url without flagging it as a
2060 // reload). But in the case of a different document that is not logically
2061 // related to the committed FrameNavigationEntry's document (cross-document,
2062 // not same url, not a reload, not a history traversal), we replace rather
2063 // than update.
Nate Chapinc7019dd7d2021-06-25 18:29:252064 //
Nate Chapin9f169072021-06-09 19:32:372065 // In the case where we update, the FrameNavigationEntry will potentially be
2066 // shared across multiple NavigationEntries, and any updates will be reflected
2067 // in all of those NavigationEntries. In the replace case, any existing
2068 // sharing with other NavigationEntries will stop.
Nate Chapinc7019dd7d2021-06-25 18:29:252069 //
2070 // When navigating away from the initial empty document, we also update rather
2071 // than replace. Either update or replace will overwrite the initial empty
2072 // document state for |last_committed|, but if the FrameNavigationEntry for
2073 // the initial empty document is shared across multiple NavigationEntries (due
2074 // to a navigation in another frame), we want to make sure we overwrite the
2075 // initial empty document state everywhere this FrameNavigationEntry is used,
2076 // which is accompished by updating the existing FrameNavigationEntry.
Nate Chapin9f169072021-06-09 19:32:372077 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 &&
Nate Chapinc7019dd7d2021-06-25 18:29:252084 last_committed_frame_entry->url() != params.url &&
2085 !was_on_initial_empty_document) {
Nate Chapin9f169072021-06-09 19:32:372086 update_policy = NavigationEntryImpl::UpdatePolicy::kReplace;
2087 }
2088
Anton Bikineevf62d1bf2021-05-15 17:56:072089 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452090 request->common_params().initiator_origin;
creisce0ef3572017-01-26 17:53:082091 last_committed->AddOrUpdateFrameEntry(
Nate Chapin9f169072021-06-09 19:32:372092 rfh->frame_tree_node(), update_policy, params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:082093 params.document_sequence_number, params.app_history_key,
2094 rfh->GetSiteInstance(), nullptr, params.url,
2095 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:092096 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072097 params.page_state, params.method, params.post_id,
2098 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032099 request->web_bundle_navigation_info()
2100 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392101 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482102 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:192103 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
2104 request));
creis625a0c7d2015-03-24 23:17:122105
creis913c63ce2016-07-16 19:52:522106 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432107}
2108
[email protected]d202a7c2012-01-04 07:53:472109int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232110 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032111 for (size_t i = 0; i < entries_.size(); ++i) {
2112 if (entries_[i].get() == entry)
2113 return i;
2114 }
2115 return -1;
[email protected]765b35502008-08-21 00:51:202116}
2117
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572118void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242119 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572120 NavigationControllerImpl* source =
2121 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572122 // Verify that we look new.
Lei Zhang96031532019-10-10 19:05:472123 DCHECK_EQ(0, GetEntryCount());
2124 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:572125
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572126 if (source->GetEntryCount() == 0)
[email protected]ce3fa3c2009-04-20 19:55:572127 return; // Nothing new to do.
2128
Francois Dorayeaace782017-06-21 16:37:242129 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442130 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572131 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572132
Fergal Dalya1d569972021-03-16 03:24:532133 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302134 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532135 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2136 session_storage_namespace_map_[it.first] = source_namespace->Clone();
Aaron Colwellb731a0ae2021-03-19 19:14:472137 OnStoragePartitionIdAdded(it.first);
[email protected]fdac6ade2013-07-20 01:06:302138 }
[email protected]4e6419c2010-01-15 04:50:342139
Lukasz Anforowicz0de0f452020-12-02 19:57:152140 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572141}
2142
Aran Gilman37d11632019-10-08 23:07:152143void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2144 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162145 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012146 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162147
[email protected]d202a7c2012-01-04 07:53:472148 NavigationControllerImpl* source =
2149 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252150
avi2b177592014-12-10 02:08:022151 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012152 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252153
[email protected]474f8512013-05-31 22:31:162154 // We now have one entry, possibly with a new pending entry. Ensure that
2155 // adding the entries from source won't put us over the limit.
2156 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572157 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572158 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252159
Carlos IL4dea8902020-05-26 15:14:292160 // Insert the entries from source. Ignore any pending entry, since it has not
2161 // committed in source.
[email protected]474f8512013-05-31 22:31:162162 int max_source_index = source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:252163 if (max_source_index == -1)
[email protected]a26023822011-12-29 00:23:552164 max_source_index = source->GetEntryCount();
[email protected]e1cd5452010-08-26 18:03:252165 else
2166 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572167
2168 // Ignore the source's current entry if merging with replacement.
2169 // TODO(davidben): This should preserve entries forward of the current
2170 // too. http://crbug.com/317872
2171 if (replace_entry && max_source_index > 0)
2172 max_source_index--;
2173
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572174 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252175
2176 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552177 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142178
Hayato Ito2c8c08d02021-06-23 03:38:432179 BroadcastHistoryOffsetAndLength();
[email protected]e1cd5452010-08-26 18:03:252180}
2181
[email protected]79368982013-11-13 01:11:012182bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162183 // If there is no last committed entry, we cannot prune. Even if there is a
2184 // pending entry, it may not commit, leaving this WebContents blank, despite
2185 // possibly giving it new entries via CopyStateFromAndPrune.
2186 if (last_committed_entry_index_ == -1)
2187 return false;
[email protected]9350602e2013-02-26 23:27:442188
[email protected]474f8512013-05-31 22:31:162189 // We cannot prune if there is a pending entry at an existing entry index.
2190 // It may not commit, so we have to keep the last committed entry, and thus
2191 // there is no sensible place to keep the pending entry. It is ok to have
2192 // a new pending entry, which can optionally commit as a new navigation.
2193 if (pending_entry_index_ != -1)
2194 return false;
2195
[email protected]474f8512013-05-31 22:31:162196 return true;
2197}
2198
[email protected]79368982013-11-13 01:11:012199void NavigationControllerImpl::PruneAllButLastCommitted() {
2200 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162201
avi2b177592014-12-10 02:08:022202 DCHECK_EQ(0, last_committed_entry_index_);
2203 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442204
Hayato Ito2c8c08d02021-06-23 03:38:432205 BroadcastHistoryOffsetAndLength();
[email protected]9350602e2013-02-26 23:27:442206}
2207
[email protected]79368982013-11-13 01:11:012208void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162209 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012210 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262211
[email protected]474f8512013-05-31 22:31:162212 // Erase all entries but the last committed entry. There may still be a
2213 // new pending entry after this.
2214 entries_.erase(entries_.begin(),
2215 entries_.begin() + last_committed_entry_index_);
2216 entries_.erase(entries_.begin() + 1, entries_.end());
2217 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262218}
2219
Christian Dullweber1af31e62018-02-22 11:49:482220void NavigationControllerImpl::DeleteNavigationEntries(
2221 const DeletionPredicate& deletionPredicate) {
2222 // It is up to callers to check the invariants before calling this.
2223 CHECK(CanPruneAllButLastCommitted());
2224 std::vector<int> delete_indices;
2225 for (size_t i = 0; i < entries_.size(); i++) {
2226 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572227 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482228 delete_indices.push_back(i);
2229 }
2230 }
2231 if (delete_indices.empty())
2232 return;
2233
2234 if (delete_indices.size() == GetEntryCount() - 1U) {
2235 PruneAllButLastCommitted();
2236 } else {
2237 // Do the deletion in reverse to preserve indices.
2238 for (auto it = delete_indices.rbegin(); it != delete_indices.rend(); ++it) {
2239 RemoveEntryAtIndex(*it);
2240 }
Hayato Ito2c8c08d02021-06-23 03:38:432241 BroadcastHistoryOffsetAndLength();
Christian Dullweber1af31e62018-02-22 11:49:482242 }
2243 delegate()->NotifyNavigationEntriesDeleted();
2244}
2245
Shivani Sharma883f5f32019-02-12 18:20:012246bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2247 auto* entry = GetEntryAtIndex(index);
2248 return entry && entry->should_skip_on_back_forward_ui();
2249}
2250
Carlos Caballero35ce710c2019-09-19 10:59:452251BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2252 return back_forward_cache_;
2253}
2254
clamy987a3752018-05-03 17:36:262255void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2256 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2257 // progress, since this will cause a use-after-free. (We only allow this
2258 // when the tab is being destroyed for shutdown, since it won't return to
2259 // NavigateToEntry in that case.) http://crbug.com/347742.
2260 CHECK(!in_navigate_to_pending_entry_ || delegate_->IsBeingDestroyed());
2261
2262 if (was_failure && pending_entry_) {
2263 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2264 } else {
2265 failed_pending_entry_id_ = 0;
2266 }
2267
2268 if (pending_entry_) {
2269 if (pending_entry_index_ == -1)
2270 delete pending_entry_;
2271 pending_entry_index_ = -1;
2272 pending_entry_ = nullptr;
2273 }
arthursonzogni66f711c2019-10-08 14:40:362274
2275 // Ensure any refs to the current pending entry are ignored if they get
2276 // deleted, by clearing the set of known refs. All future pending entries will
2277 // only be affected by new refs.
2278 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262279}
2280
2281void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2282 if (pending_entry_)
2283 pending_entry_->set_ssl_error(error);
2284}
2285
Camille Lamy5193caa2018-10-12 11:59:422286#if defined(OS_ANDROID)
2287// static
2288bool NavigationControllerImpl::ValidateDataURLAsString(
2289 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2290 if (!data_url_as_string)
2291 return false;
2292
2293 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2294 return false;
2295
2296 // The number of characters that is enough for validating a data: URI.
2297 // From the GURL's POV, the only important part here is scheme, it doesn't
2298 // check the actual content. Thus we can take only the prefix of the url, to
2299 // avoid unneeded copying of a potentially long string.
2300 const size_t kDataUriPrefixMaxLen = 64;
2301 GURL data_url(
2302 std::string(data_url_as_string->front_as<char>(),
2303 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2304 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2305 return false;
2306
2307 return true;
2308}
2309#endif
2310
Shivani Sharma194877032019-03-07 17:52:472311void NavigationControllerImpl::NotifyUserActivation() {
2312 // When a user activation occurs, ensure that all adjacent entries for the
2313 // same document clear their skippable bit, so that the history manipulation
2314 // intervention does not apply to them.
Shivani Sharma194877032019-03-07 17:52:472315 auto* last_committed_entry = GetLastCommittedEntry();
2316 if (!last_committed_entry)
2317 return;
Shivani Sharma194877032019-03-07 17:52:472318
shivanigithub99368382021-06-16 18:33:372319 if (base::FeatureList::IsEnabled(
2320 features::kDebugHistoryInterventionNoUserActivation)) {
2321 return;
2322 }
2323
Shivani Sharmac4cc8922019-04-18 03:11:172324 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472325}
2326
clamy987a3752018-05-03 17:36:262327bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2328 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172329 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262330 NavigationEntryImpl* entry =
2331 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2332 if (!entry)
2333 return false;
2334
2335 FrameNavigationEntry* frame_entry =
2336 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2337 if (!frame_entry)
2338 return false;
2339
Camille Lamy5193caa2018-10-12 11:59:422340 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572341 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232342 ReloadType::NONE, false /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422343 true /* is_history_navigation_in_new_child */);
clamyea99ea12018-05-28 13:54:232344
2345 if (!request)
2346 return false;
2347
arthursonzognif046d4a2019-12-12 19:08:102348 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412349
Lukasz Anforowicz9ee83c272020-12-01 20:14:052350 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2351 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232352
2353 return true;
clamy987a3752018-05-03 17:36:262354}
2355
Tsuyoshi Horo52fd08e2020-07-07 07:03:452356bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2357 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2358 if (!entry)
2359 return false;
2360 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2361 if (!frame_entry)
2362 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142363 ReloadType reload_type = ReloadType::NORMAL;
2364 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452365 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142366 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452367 false /* is_same_document_history_load */,
2368 false /* is_history_navigation_in_new_child */);
2369 if (!request)
2370 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052371 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452372 return true;
2373}
2374
Dave Tapuska8bfd84c2019-03-26 20:47:162375void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2376 int offset,
2377 int sandbox_frame_tree_node_id) {
2378 if (!CanGoToOffset(offset))
2379 return;
2380 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id);
2381}
2382
clamy987a3752018-05-03 17:36:262383void NavigationControllerImpl::NavigateFromFrameProxy(
2384 RenderFrameHostImpl* render_frame_host,
2385 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022386 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452387 int initiator_process_id,
Anton Bikineevf62d1bf2021-05-15 17:56:072388 const absl::optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262389 bool is_renderer_initiated,
2390 SiteInstance* source_site_instance,
2391 const Referrer& referrer,
2392 ui::PageTransition page_transition,
2393 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252394 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262395 const std::string& method,
2396 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092397 const std::string& extra_headers,
Antonio Sartori2f763d9d2021-04-21 10:04:142398 network::mojom::SourceLocationPtr source_location,
John Delaney50425f82020-04-07 16:26:212399 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Anton Bikineevf62d1bf2021-05-15 17:56:072400 const absl::optional<blink::Impression>& impression) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582401 if (is_renderer_initiated)
2402 DCHECK(initiator_origin.has_value());
2403
clamy987a3752018-05-03 17:36:262404 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582405
Rakina Zata Amni66992a82020-06-24 03:57:522406 // Don't allow an entry replacement if there is no entry to replace.
2407 // http://crbug.com/457149
2408 if (GetEntryCount() == 0)
2409 should_replace_current_entry = false;
2410
clamy987a3752018-05-03 17:36:262411 // Create a NavigationEntry for the transfer, without making it the pending
2412 // entry. Subframe transfers should have a clone of the last committed entry
2413 // with a FrameNavigationEntry for the target frame. Main frame transfers
2414 // should have a new NavigationEntry.
2415 // TODO(creis): Make this unnecessary by creating (and validating) the params
2416 // directly, passing them to the destination RenderFrameHost. See
2417 // https://crbug.com/536906.
2418 std::unique_ptr<NavigationEntryImpl> entry;
2419 if (!node->IsMainFrame()) {
2420 // Subframe case: create FrameNavigationEntry.
2421 if (GetLastCommittedEntry()) {
2422 entry = GetLastCommittedEntry()->Clone();
2423 entry->set_extra_headers(extra_headers);
2424 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2425 // Renderer-initiated navigation that target a remote frame are currently
2426 // classified as browser-initiated when this one has already navigated.
2427 // See https://crbug.com/722251.
2428 } else {
2429 // If there's no last committed entry, create an entry for about:blank
2430 // with a subframe entry for our destination.
2431 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2432 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:062433 GURL(url::kAboutBlankURL), referrer, initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:102434 source_site_instance, page_transition, is_renderer_initiated,
2435 extra_headers, browser_context_,
Hayato Ito303654c2021-06-30 09:07:542436 nullptr /* blob_url_loader_factory */));
clamy987a3752018-05-03 17:36:262437 }
Nate Chapin9f169072021-06-09 19:32:372438 // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter
2439 // to CreateNavigationRequestFromLoadParams(), so while kReplace might
2440 // remove child FrameNavigationEntries (e.g., if this is a cross-process
2441 // same-document navigation), they will still be present in the
2442 // committed NavigationEntry that will be referenced to construct the new
2443 // FrameNavigationEntry tree when this navigation commits.
clamy987a3752018-05-03 17:36:262444 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:082445 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582446 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072447 absl::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202448 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392449 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482450 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori79d549d2021-02-18 12:59:542451 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262452 } else {
2453 // Main frame case.
2454 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102455 url, referrer, initiator_origin, source_site_instance, page_transition,
2456 is_renderer_initiated, extra_headers, browser_context_,
Hayato Ito303654c2021-06-30 09:07:542457 blob_url_loader_factory));
clamy987a3752018-05-03 17:36:262458 entry->root_node()->frame_entry->set_source_site_instance(
2459 static_cast<SiteInstanceImpl*>(source_site_instance));
2460 entry->root_node()->frame_entry->set_method(method);
2461 }
clamy987a3752018-05-03 17:36:262462
Camille Lamy5193caa2018-10-12 11:59:422463 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262464 if (GetLastCommittedEntry() &&
2465 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2466 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422467 override_user_agent = true;
clamy987a3752018-05-03 17:36:262468 }
2469 // TODO(creis): Set user gesture and intent received timestamp on Android.
2470
2471 // We may not have successfully added the FrameNavigationEntry to |entry|
2472 // above (per https://crbug.com/608402), in which case we create it from
2473 // scratch. This works because we do not depend on |frame_entry| being inside
2474 // |entry| during NavigateToEntry. This will go away when we shortcut this
2475 // further in https://crbug.com/536906.
2476 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2477 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452478 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Nate Chapinfbfe5af2021-06-10 17:22:082479 node->unique_name(), -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582480 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072481 absl::nullopt /* origin */, referrer, initiator_origin,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:122482 std::vector<GURL>(), blink::PageState(), method, -1,
2483 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482484 nullptr /* subresource_web_bundle_navigation_info */,
Titouan Rigoudy6ec70402021-02-02 15:42:192485 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262486 }
2487
Camille Lamy5193caa2018-10-12 11:59:422488 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022489 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452490 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292491 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422492 params.source_site_instance = source_site_instance;
2493 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2494 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032495 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422496 params.referrer = referrer;
2497 /* params.redirect_chain: skip */
2498 params.extra_headers = extra_headers;
2499 params.is_renderer_initiated = is_renderer_initiated;
2500 params.override_user_agent = UA_OVERRIDE_INHERIT;
2501 /* params.base_url_for_data_url: skip */
2502 /* params.virtual_url_for_data_url: skip */
2503 /* params.data_url_as_string: skip */
2504 params.post_data = post_body;
2505 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582506 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422507 /* params.frame_name: skip */
2508 // TODO(clamy): See if user gesture should be propagated to this function.
2509 params.has_user_gesture = false;
2510 params.should_clear_history_list = false;
2511 params.started_from_context_menu = false;
2512 /* params.navigation_ui_data: skip */
2513 /* params.input_start: skip */
Minggang Wangf59db47b2021-06-16 01:56:222514 params.was_activated = blink::mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542515 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212516 params.impression = impression;
Camille Lamy5193caa2018-10-12 11:59:422517
2518 std::unique_ptr<NavigationRequest> request =
2519 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032520 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:142521 false /* has_user_gesture */, std::move(source_location),
Scott Violetcf6ea7e2021-06-09 21:09:212522 download_policy, ReloadType::NONE, entry.get(), frame_entry.get());
clamyea99ea12018-05-28 13:54:232523
2524 if (!request)
2525 return;
2526
Arthur Hemery948742762019-09-18 10:06:242527 // At this stage we are proceeding with this navigation. If this was renderer
2528 // initiated with user gesture, we need to make sure we clear up potential
2529 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2530 DiscardNonCommittedEntries();
2531
Lukasz Anforowicz9ee83c272020-12-01 20:14:052532 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262533}
2534
[email protected]d1198fd2012-08-13 22:50:192535void NavigationControllerImpl::SetSessionStorageNamespace(
Aaron Colwellf3b316e2021-03-11 20:17:052536 const StoragePartitionId& partition_id,
[email protected]8ff00d72012-10-23 19:12:212537 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192538 if (!session_storage_namespace)
2539 return;
2540
2541 // We can't overwrite an existing SessionStorage without violating spec.
2542 // Attempts to do so may give a tab access to another tab's session storage
2543 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152544 bool successful_insert =
2545 session_storage_namespace_map_
Aaron Colwellf3b316e2021-03-11 20:17:052546 .insert(std::make_pair(partition_id,
2547 static_cast<SessionStorageNamespaceImpl*>(
2548 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302549 .second;
2550 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
Aaron Colwellb731a0ae2021-03-19 19:14:472551 OnStoragePartitionIdAdded(partition_id);
[email protected]d1198fd2012-08-13 22:50:192552}
2553
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572554bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Aran Gilman37d11632019-10-08 23:07:152555 return IsInitialNavigation() && !GetLastCommittedEntry() &&
Carlos Caballeroede6f8c2021-01-28 11:01:502556 !frame_tree_.has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462557}
2558
Aran Gilman37d11632019-10-08 23:07:152559SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Aaron Colwell78b4bde2021-03-16 16:16:092560 const SiteInfo& site_info) {
2561 // TODO(acolwell): Remove partition_id logic once we have successfully
2562 // migrated the implementation to be a StoragePartitionConfig. At that point
2563 // |site_info| can be replaced with a StoragePartitionConfig.
2564 const StoragePartitionId partition_id =
2565 site_info.GetStoragePartitionId(browser_context_);
2566 const StoragePartitionConfig partition_config =
2567 site_info.GetStoragePartitionConfig(browser_context_);
[email protected]d1198fd2012-08-13 22:50:192568
[email protected]fdac6ade2013-07-20 01:06:302569 StoragePartition* partition =
Lukasz Anforowiczb9a969a2021-04-29 15:26:252570 browser_context_->GetStoragePartition(partition_config);
michaelnbacbcbd2016-02-09 00:32:032571 DOMStorageContextWrapper* context_wrapper =
2572 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2573
2574 SessionStorageNamespaceMap::const_iterator it =
2575 session_storage_namespace_map_.find(partition_id);
2576 if (it != session_storage_namespace_map_.end()) {
2577 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152578 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2579 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:472580
2581 // Verify that the config we generated now matches the one that
2582 // was generated when the namespace was added to the map.
2583 DCHECK_EQ(partition_config, it->first.config());
2584 if (partition_config != it->first.config()) {
2585 LogStoragePartitionIdCrashKeys(it->first, partition_id);
2586 }
2587
michaelnbacbcbd2016-02-09 00:32:032588 return it->second.get();
2589 }
2590
2591 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102592 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2593 SessionStorageNamespaceImpl::Create(context_wrapper);
2594 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2595 session_storage_namespace.get();
2596 session_storage_namespace_map_[partition_id] =
2597 std::move(session_storage_namespace);
Aaron Colwellb731a0ae2021-03-19 19:14:472598 OnStoragePartitionIdAdded(partition_id);
[email protected]fdac6ade2013-07-20 01:06:302599
Daniel Murphy31bbb8b12018-02-07 21:44:102600 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302601}
2602
2603SessionStorageNamespace*
2604NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Aaron Colwell78b4bde2021-03-16 16:16:092605 return GetSessionStorageNamespace(SiteInfo());
[email protected]fdac6ade2013-07-20 01:06:302606}
2607
2608const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572609NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302610 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552611}
[email protected]d202a7c2012-01-04 07:53:472612
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572613bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562614 return needs_reload_;
2615}
[email protected]a26023822011-12-29 00:23:552616
[email protected]46bb5e9c2013-10-03 22:16:472617void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412618 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2619}
2620
2621void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472622 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412623 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542624
2625 if (last_committed_entry_index_ != -1) {
2626 entries_[last_committed_entry_index_]->SetTransitionType(
2627 ui::PAGE_TRANSITION_RELOAD);
2628 }
[email protected]46bb5e9c2013-10-03 22:16:472629}
2630
[email protected]d202a7c2012-01-04 07:53:472631void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572632 DCHECK_LT(index, GetEntryCount());
2633 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312634 DiscardNonCommittedEntries();
2635
2636 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122637 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312638 last_committed_entry_index_--;
2639}
2640
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572641NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272642 // If there is no pending_entry_, there should be no pending_entry_index_.
2643 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2644
2645 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362646 // at that index. An exception is while a reload of a post commit error page
2647 // is ongoing; in that case pending entry will point to the entry replaced
2648 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272649 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362650 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2651 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272652
[email protected]022af742011-12-28 18:37:252653 return pending_entry_;
2654}
2655
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572656int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272657 // The pending entry index must always be less than the number of entries.
2658 // If there are no entries, it must be exactly -1.
2659 DCHECK_LT(pending_entry_index_, GetEntryCount());
2660 DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:552661 return pending_entry_index_;
2662}
2663
avi25764702015-06-23 15:43:372664void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572665 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362666 bool replace,
2667 bool was_post_commit_error) {
mastiz7eddb5f72016-06-23 09:52:452668 DCHECK(!ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2669 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202670
avi5cad4912015-06-19 05:25:442671 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2672 // need to keep continuity with the pending entry, so copy the pending entry's
2673 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2674 // then the renderer navigated on its own, independent of the pending entry,
2675 // so don't copy anything.
2676 if (pending_entry_ && pending_entry_index_ == -1)
2677 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202678
arthursonzogni69a6a1b2019-09-17 09:23:002679 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:202680
creisee17e932015-07-17 17:56:222681 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362682 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102683 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572684 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362685 // If the new entry is a post-commit error page, we store the current last
2686 // committed entry to the side so that we can put it back when navigating
2687 // away from the error.
2688 if (was_post_commit_error) {
2689 DCHECK(!entry_replaced_by_post_commit_error_);
2690 entry_replaced_by_post_commit_error_ =
2691 std::move(entries_[last_committed_entry_index_]);
2692 }
dcheng36b6aec92015-12-26 06:16:362693 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222694 return;
2695 }
[email protected]765b35502008-08-21 00:51:202696
creis37979a62015-08-04 19:48:182697 // We shouldn't see replace == true when there's no committed entries.
2698 DCHECK(!replace);
2699
Michael Thiessen9b14d512019-09-23 21:19:472700 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202701
Shivani Sharmad8c8d652019-02-13 17:27:572702 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202703
dcheng36b6aec92015-12-26 06:16:362704 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202705 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292706}
2707
Shivani Sharmad8c8d652019-02-13 17:27:572708void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162709 if (entries_.size() < max_entry_count())
2710 return;
2711
2712 DCHECK_EQ(max_entry_count(), entries_.size());
2713 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572714 CHECK_EQ(pending_entry_index_, -1);
2715
2716 int index = 0;
Elly Fong-Jonesccc6d1f2021-06-14 18:32:422717 // Retrieve the oldest skippable entry.
2718 for (; index < GetEntryCount(); index++) {
2719 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2720 break;
Shivani Sharmad8c8d652019-02-13 17:27:572721 }
2722
2723 // If there is no skippable entry or if it is the last committed entry then
2724 // fall back to pruning the oldest entry. It is not safe to prune the last
2725 // committed entry.
2726 if (index == GetEntryCount() || index == last_committed_entry_index_)
2727 index = 0;
2728
2729 bool should_succeed = RemoveEntryAtIndex(index);
2730 DCHECK_EQ(true, should_succeed);
2731
2732 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252733}
2734
clamy3cb9bea92018-07-10 12:42:022735void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162736 ReloadType reload_type,
2737 int sandboxed_source_frame_tree_node_id) {
Alexander Timin3a92df72019-09-20 11:59:502738 TRACE_EVENT0("navigation",
2739 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272740 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022741 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362742 if (pending_entry_index_ != -1) {
2743 // The pending entry may not be in entries_ if a post-commit error page is
2744 // showing.
2745 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2746 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2747 }
Gyuyoung Kim107c2a02021-04-13 01:49:302748 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572749 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:012750 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:002751 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:572752 int nav_entry_id = pending_entry_->GetUniqueID();
2753
[email protected]83c2e232011-10-07 21:36:462754 // If we were navigating to a slow-to-commit page, and the user performs
2755 // a session history navigation to the last committed page, RenderViewHost
2756 // will force the throbber to start, but WebKit will essentially ignore the
2757 // navigation, and won't send a message to stop the throbber. To prevent this
2758 // from happening, we drop the navigation here and stop the slow-to-commit
2759 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022760 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:082761 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:272762 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
[email protected]ec6c05f2013-10-23 18:41:572763 delegate_->Stop();
[email protected]6a13a6c2011-12-20 21:47:122764
[email protected]83c2e232011-10-07 21:36:462765 DiscardNonCommittedEntries();
2766 return;
2767 }
2768
creisce0ef3572017-01-26 17:53:082769 // Compare FrameNavigationEntries to see which frames in the tree need to be
2770 // navigated.
clamy3cb9bea92018-07-10 12:42:022771 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2772 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Alex Moshchuk3a4e77a2020-05-29 21:32:572773 FindFramesToNavigate(root, reload_type, &same_document_loads,
2774 &different_document_loads);
creis4e2ecb72015-06-20 00:46:302775
2776 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572777 // We were unable to match any frames to navigate. This can happen if a
2778 // history navigation targets a subframe that no longer exists
2779 // (https://crbug.com/705550). In this case, we need to update the current
2780 // history entry to the pending one but keep the main document loaded. We
2781 // also need to ensure that observers are informed about the updated
2782 // current history entry (e.g., for greying out back/forward buttons), and
2783 // that renderer processes update their history offsets. The easiest way
2784 // to do all that is to schedule a "redundant" same-document navigation in
2785 // the main frame.
2786 //
2787 // Note that we don't want to remove this history entry, as it might still
2788 // be valid later, since a frame that it's targeting may be recreated.
2789 //
2790 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
2791 // need to repeat history navigations until finding the one that works.
2792 // Consider changing this behavior to keep looking for the first valid
2793 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:022794 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422795 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572796 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:572797 ReloadType::NONE /* reload_type */,
2798 true /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422799 false /* is_history_navigation_in_new_child */);
clamy3cb9bea92018-07-10 12:42:022800 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572801 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:022802 DiscardPendingEntry(false);
2803 return;
2804 }
Alex Moshchuk3a4e77a2020-05-29 21:32:572805 same_document_loads.push_back(std::move(navigation_request));
2806
2807 // Sanity check that we never take this branch for any kinds of reloads,
2808 // as those should've queued a different-document load in the main frame.
2809 DCHECK(!is_forced_reload);
2810 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:302811 }
2812
Dave Tapuska8bfd84c2019-03-26 20:47:162813 // If |sandboxed_source_frame_node_id| is valid, then track whether this
2814 // navigation affects any frame outside the frame's subtree.
2815 if (sandboxed_source_frame_tree_node_id !=
2816 FrameTreeNode::kFrameTreeNodeInvalidId) {
2817 bool navigates_inside_tree =
2818 DoesSandboxNavigationStayWithinSubtree(
2819 sandboxed_source_frame_tree_node_id, same_document_loads) &&
2820 DoesSandboxNavigationStayWithinSubtree(
2821 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:502822 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:162823 // the number of pages that trigger this.
2824 FrameTreeNode* sandbox_source_frame_tree_node =
2825 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
2826 if (sandbox_source_frame_tree_node) {
2827 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
2828 sandbox_source_frame_tree_node->current_frame_host(),
2829 navigates_inside_tree
2830 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
2831 : blink::mojom::WebFeature::
2832 kSandboxBackForwardAffectsFramesOutsideSubtree);
2833 }
Dave Tapuska855c1e12019-08-23 20:45:522834
2835 // If the navigation occurred outside the tree discard it because
2836 // the sandboxed frame didn't have permission to navigate outside
2837 // its tree. If it is possible that the navigation is both inside and
2838 // outside the frame tree and we discard it entirely because we don't
2839 // want to end up in a history state that didn't exist before.
2840 if (base::FeatureList::IsEnabled(
2841 features::kHistoryPreventSandboxedNavigation) &&
2842 !navigates_inside_tree) {
2843 DiscardPendingEntry(false);
2844 return;
2845 }
Dave Tapuska8bfd84c2019-03-26 20:47:162846 }
2847
Carlos Caballero539a421c2020-07-06 10:25:572848 // BackForwardCache:
2849 // Navigate immediately if the document is in the BackForwardCache.
2850 if (back_forward_cache_.GetEntry(nav_entry_id)) {
2851 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
2852 DCHECK_EQ(reload_type, ReloadType::NONE);
2853 auto navigation_request = CreateNavigationRequestFromEntry(
2854 root, pending_entry_, pending_entry_->GetFrameEntry(root),
2855 ReloadType::NONE, false /* is_same_document_history_load */,
2856 false /* is_history_navigation_in_new_child */);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052857 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:572858
2859 return;
2860 }
2861
2862 // History navigation might try to reuse a specific BrowsingInstance, already
2863 // used by a page in the cache. To avoid having two different main frames that
2864 // live in the same BrowsingInstance, evict the all pages with this
2865 // BrowsingInstance from the cache.
2866 //
2867 // For example, take the following scenario:
2868 //
2869 // A1 = Some page on a.com
2870 // A2 = Some other page on a.com
2871 // B3 = An uncacheable page on b.com
2872 //
2873 // Then the following navigations occur:
2874 // A1->A2->B3->A1
2875 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
2876 // take its place) and A1 will be created in the same BrowsingInstance (and
2877 // SiteInstance), as A2.
2878 //
2879 // If we didn't do anything, both A1 and A2 would remain alive in the same
2880 // BrowsingInstance/SiteInstance, which is unsupported by
2881 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
2882 // A2 from the cache.
2883 if (pending_entry_->site_instance()) {
2884 back_forward_cache_.EvictFramesInRelatedSiteInstances(
2885 pending_entry_->site_instance());
2886 }
2887
clamy3cb9bea92018-07-10 12:42:022888 // This call does not support re-entrancy. See http://crbug.com/347742.
2889 CHECK(!in_navigate_to_pending_entry_);
2890 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:302891
arthursonzogni66f711c2019-10-08 14:40:362892 // It is not possible to delete the pending NavigationEntry while navigating
2893 // to it. Grab a reference to delay potential deletion until the end of this
2894 // function.
2895 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
2896
creis4e2ecb72015-06-20 00:46:302897 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:022898 for (auto& item : same_document_loads) {
2899 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052900 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302901 }
clamy3cb9bea92018-07-10 12:42:022902 for (auto& item : different_document_loads) {
2903 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052904 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302905 }
clamy3cb9bea92018-07-10 12:42:022906
2907 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:302908}
2909
Alex Moshchuk3a4e77a2020-05-29 21:32:572910NavigationControllerImpl::HistoryNavigationAction
2911NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:302912 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:572913 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:422914 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:582915 // Only active and prerendered documents are allowed to navigate in their
2916 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422917 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:012918 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:582919 // - If the document is in pending deletion, the browser already committed
2920 // to destroying this RenderFrameHost. See https://crbug.com/930278.
2921 // - If the document is in back-forward cache, it's not allowed to navigate
2922 // as it should remain frozen. Ignore the request and evict the document
2923 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422924 //
Sreeja Kamishettydb8e2892021-03-10 09:30:582925 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:422926 // which should all be inactive as well.
Sreeja Kamishettydb8e2892021-03-10 09:30:582927 if (render_frame_host->IsInactiveAndDisallowActivation())
Sreeja Kamishetty8eacabb2021-03-09 11:45:422928 return HistoryNavigationAction::kStopLooking;
2929 }
arthursonzogni03f76152019-02-12 10:35:202930
Alex Moshchuk3a4e77a2020-05-29 21:32:572931 // If there's no last committed entry, there is no previous history entry to
2932 // compare against, so fall back to a different-document load. Note that we
2933 // should only reach this case for the root frame and not descend further
2934 // into subframes.
2935 if (!GetLastCommittedEntry()) {
2936 DCHECK(frame->IsMainFrame());
2937 return HistoryNavigationAction::kDifferentDocument;
2938 }
2939
2940 // Reloads should result in a different-document load. Note that reloads may
2941 // also happen via the |needs_reload_| mechanism where the reload_type is
2942 // NONE, so detect this by comparing whether we're going to the same
2943 // entry that we're currently on. Similarly to above, only main frames
2944 // should reach this. Note that subframes support reloads, but that's done
2945 // via a different path that doesn't involve FindFramesToNavigate (see
2946 // RenderFrameHost::Reload()).
2947 if (reload_type != ReloadType::NONE ||
2948 pending_entry_index_ == last_committed_entry_index_) {
2949 DCHECK(frame->IsMainFrame());
2950 return HistoryNavigationAction::kDifferentDocument;
2951 }
2952
Alex Moshchuk47d1a4bd2020-06-01 22:15:342953 // If there is no new FrameNavigationEntry for the frame, ignore the
2954 // load. For example, this may happen when going back to an entry before a
2955 // frame was created. Suppose we commit a same-document navigation that also
2956 // results in adding a new subframe somewhere in the tree. If we go back,
2957 // the new subframe will be missing a FrameNavigationEntry in the previous
2958 // NavigationEntry, but we shouldn't delete or change what's loaded in
2959 // it.
2960 //
Alex Moshchuke65c39272020-06-03 17:55:372961 // Note that in this case, there is no need to keep looking for navigations
2962 // in subframes, which would be missing FrameNavigationEntries as well.
2963 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:342964 // It's important to check this before checking |old_item| below, since both
2965 // might be null, and in that case we still shouldn't change what's loaded in
2966 // this frame. Note that scheduling any loads assumes that |new_item| is
2967 // non-null. See https://crbug.com/1088354.
2968 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
2969 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:372970 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:342971
Alex Moshchuk3a4e77a2020-05-29 21:32:572972 // If there is no old FrameNavigationEntry, schedule a different-document
2973 // load.
2974 //
creis225a7432016-06-03 22:56:272975 // TODO(creis): Store the last committed FrameNavigationEntry to use here,
2976 // rather than assuming the NavigationEntry has up to date info on subframes.
creis4e2ecb72015-06-20 00:46:302977 FrameNavigationEntry* old_item =
2978 GetLastCommittedEntry()->GetFrameEntry(frame);
Alex Moshchuk3a4e77a2020-05-29 21:32:572979 if (!old_item)
2980 return HistoryNavigationAction::kDifferentDocument;
2981
Alex Moshchuk3a4e77a2020-05-29 21:32:572982 // If the new item is not in the same SiteInstance, schedule a
2983 // different-document load. Newly restored items may not have a SiteInstance
2984 // yet, in which case it will be assigned on first commit.
2985 if (new_item->site_instance() &&
2986 new_item->site_instance() != old_item->site_instance())
2987 return HistoryNavigationAction::kDifferentDocument;
2988
2989 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:312990 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
2991 // that we do this even if the history navigation would not be modifying this
2992 // frame were it live.
2993 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:572994 return HistoryNavigationAction::kDifferentDocument;
2995
2996 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:312997 // Starting a navigation after a crash early-promotes the speculative
2998 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
2999 // committed yet, so we can not possibly perform a same-document history
3000 // navigation. The frame would need to be reloaded with a cross-document
3001 // navigation.
3002 if (!frame->current_frame_host()->has_committed_any_navigation())
3003 return HistoryNavigationAction::kDifferentDocument;
3004
creis54131692016-08-12 18:32:253005 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:493006 // sequence number but different item sequence number.
3007 if (new_item->document_sequence_number() ==
3008 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573009 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493010 }
avib48cb312016-05-05 21:35:003011
Alex Moshchuk3a4e77a2020-05-29 21:32:573012 // Otherwise, if both item and document sequence numbers differ, this
3013 // should be a different document load.
3014 return HistoryNavigationAction::kDifferentDocument;
3015 }
3016
3017 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373018 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573019 DCHECK_EQ(new_item->document_sequence_number(),
3020 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373021 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573022}
3023
3024void NavigationControllerImpl::FindFramesToNavigate(
3025 FrameTreeNode* frame,
3026 ReloadType reload_type,
3027 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3028 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3029 DCHECK(pending_entry_);
3030 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3031
3032 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3033
3034 if (action == HistoryNavigationAction::kSameDocument) {
3035 std::unique_ptr<NavigationRequest> navigation_request =
3036 CreateNavigationRequestFromEntry(
3037 frame, pending_entry_, new_item, reload_type,
3038 true /* is_same_document_history_load */,
3039 false /* is_history_navigation_in_new_child */);
3040 if (navigation_request) {
3041 // Only add the request if was properly created. It's possible for the
3042 // creation to fail in certain cases, e.g. when the URL is invalid.
3043 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303044 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573045 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473046 std::unique_ptr<NavigationRequest> navigation_request =
3047 CreateNavigationRequestFromEntry(
3048 frame, pending_entry_, new_item, reload_type,
3049 false /* is_same_document_history_load */,
3050 false /* is_history_navigation_in_new_child */);
3051 if (navigation_request) {
3052 // Only add the request if was properly created. It's possible for the
3053 // creation to fail in certain cases, e.g. when the URL is invalid.
3054 different_document_loads->push_back(std::move(navigation_request));
3055 }
3056 // For a different document, the subframes will be destroyed, so there's
3057 // no need to consider them.
3058 return;
Alex Moshchuke65c39272020-06-03 17:55:373059 } else if (action == HistoryNavigationAction::kStopLooking) {
3060 return;
creis4e2ecb72015-06-20 00:46:303061 }
3062
3063 for (size_t i = 0; i < frame->child_count(); i++) {
clamy3cb9bea92018-07-10 12:42:023064 FindFramesToNavigate(frame->child_at(i), reload_type, same_document_loads,
creis4e2ecb72015-06-20 00:46:303065 different_document_loads);
3066 }
3067}
3068
Harkiran Bolariaba823e42021-05-21 18:30:363069base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry(
clamy21718cc22018-06-13 13:34:243070 const LoadURLParams& params) {
3071 // Find the appropriate FrameTreeNode.
3072 FrameTreeNode* node = nullptr;
3073 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3074 !params.frame_name.empty()) {
3075 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003076 ? frame_tree_.FindByID(params.frame_tree_node_id)
3077 : frame_tree_.FindByName(params.frame_name);
clamy21718cc22018-06-13 13:34:243078 }
3079
3080 // If no FrameTreeNode was specified, navigate the main frame.
3081 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003082 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243083
Camille Lamy5193caa2018-10-12 11:59:423084 // Compute overrides to the LoadURLParams for |override_user_agent|,
3085 // |should_replace_current_entry| and |has_user_gesture| that will be used
3086 // both in the creation of the NavigationEntry and the NavigationRequest.
3087 // Ideally, the LoadURLParams themselves would be updated, but since they are
3088 // passed as a const reference, this is not possible.
3089 // TODO(clamy): When we only create a NavigationRequest, move this to
3090 // CreateNavigationRequestFromLoadURLParams.
3091 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3092 GetLastCommittedEntry());
3093
3094 // Don't allow an entry replacement if there is no entry to replace.
3095 // http://crbug.com/457149
3096 bool should_replace_current_entry =
3097 params.should_replace_current_entry && entries_.size();
3098
clamy21718cc22018-06-13 13:34:243099 // Javascript URLs should not create NavigationEntries. All other navigations
3100 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243101 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373102 std::unique_ptr<NavigationEntryImpl> entry =
3103 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3104 should_replace_current_entry,
3105 params.has_user_gesture);
clamy21718cc22018-06-13 13:34:243106 DiscardPendingEntry(false);
3107 SetPendingEntry(std::move(entry));
3108 }
3109
3110 // Renderer-debug URLs are sent to the renderer process immediately for
3111 // processing and don't need to create a NavigationRequest.
3112 // Note: this includes navigations to JavaScript URLs, which are considered
3113 // renderer-debug URLs.
3114 // Note: we intentionally leave the pending entry in place for renderer debug
3115 // URLs, unlike the cases below where we clear it if the navigation doesn't
3116 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303117 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483118 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3119 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153120 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483121 params.url, browser_context_)) {
3122 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363123 return nullptr;
Oleg Davydov2cc0167b2019-02-05 14:32:483124 }
3125
clamy21718cc22018-06-13 13:34:243126 HandleRendererDebugURL(node, params.url);
Harkiran Bolariaba823e42021-05-21 18:30:363127 return nullptr;
clamy21718cc22018-06-13 13:34:243128 }
3129
Antonio Sartori78a749f2020-11-30 12:03:393130 DCHECK(pending_entry_);
3131
clamy21718cc22018-06-13 13:34:243132 // Convert navigations to the current URL to a reload.
3133 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3134 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3135 // DevTools sometimes issues navigations to main frames that they do not
3136 // expect to see treated as reload, and it only works because they pass a
3137 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3138 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543139 ReloadType reload_type = params.reload_type;
3140 if (reload_type == ReloadType::NONE &&
3141 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043142 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243143 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243144 params.load_type ==
3145 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
Rakina Zata Amni312822d72021-06-04 16:13:373146 params.should_replace_current_entry, GetLastCommittedEntry())) {
clamy21718cc22018-06-13 13:34:243147 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473148 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393149
3150 // If this is a reload of an existing FrameNavigationEntry and we had a
3151 // policy container for it, then we should copy it into the pending entry,
3152 // so that it is copied to the navigation request in
3153 // CreateNavigationRequestFromLoadParams later.
3154 if (GetLastCommittedEntry()) {
3155 FrameNavigationEntry* previous_frame_entry =
3156 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193157 if (previous_frame_entry &&
3158 previous_frame_entry->policy_container_policies()) {
3159 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163160 previous_frame_entry->policy_container_policies()->Clone());
Antonio Sartori78a749f2020-11-30 12:03:393161 }
3162 }
3163 }
3164
3165 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
3166 // document (so no last commit), then we should copy the document polices from
3167 // RenderFrameHost's PolicyContainerHost. The NavigationRequest will create a
3168 // new PolicyContainerHost with the document policies from the
3169 // |pending_entry_|, and that PolicyContainerHost will be put in the final
3170 // RenderFrameHost for the navigation. This way, we ensure that we keep
3171 // enforcing the right policies on the initial empty document after the
3172 // reload.
3173 if (!GetLastCommittedEntry() && params.url.IsAboutBlank()) {
3174 if (node->current_frame_host() &&
3175 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193176 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163177 node->current_frame_host()
3178 ->policy_container_host()
3179 ->policies()
3180 .Clone());
Antonio Sartori78a749f2020-11-30 12:03:393181 }
clamy21718cc22018-06-13 13:34:243182 }
3183
3184 // navigation_ui_data should only be present for main frame navigations.
3185 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
3186
Camille Lamy5193caa2018-10-12 11:59:423187 std::unique_ptr<NavigationRequest> request =
3188 CreateNavigationRequestFromLoadParams(
3189 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143190 params.has_user_gesture, network::mojom::SourceLocation::New(),
3191 blink::NavigationDownloadPolicy(), reload_type, pending_entry_,
Scott Violetcf6ea7e2021-06-09 21:09:213192 pending_entry_->GetFrameEntry(node));
clamy21718cc22018-06-13 13:34:243193
3194 // If the navigation couldn't start, return immediately and discard the
3195 // pending NavigationEntry.
3196 if (!request) {
3197 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363198 return nullptr;
clamy21718cc22018-06-13 13:34:243199 }
3200
Camille Lamy5193caa2018-10-12 11:59:423201#if DCHECK_IS_ON()
3202 // Safety check that NavigationRequest and NavigationEntry match.
3203 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3204#endif
3205
clamy21718cc22018-06-13 13:34:243206 // This call does not support re-entrancy. See http://crbug.com/347742.
3207 CHECK(!in_navigate_to_pending_entry_);
3208 in_navigate_to_pending_entry_ = true;
3209
arthursonzogni66f711c2019-10-08 14:40:363210 // It is not possible to delete the pending NavigationEntry while navigating
3211 // to it. Grab a reference to delay potential deletion until the end of this
3212 // function.
3213 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3214
Harkiran Bolariaba823e42021-05-21 18:30:363215 base::WeakPtr<NavigationHandle> created_navigation_handle(
3216 request->GetWeakPtr());
Lukasz Anforowicz9ee83c272020-12-01 20:14:053217 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243218
3219 in_navigate_to_pending_entry_ = false;
Harkiran Bolariaba823e42021-05-21 18:30:363220 return created_navigation_handle;
clamy21718cc22018-06-13 13:34:243221}
3222
clamyea99ea12018-05-28 13:54:233223void NavigationControllerImpl::HandleRendererDebugURL(
3224 FrameTreeNode* frame_tree_node,
3225 const GURL& url) {
3226 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243227 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3228 // the renderer process is not live, unless it is the initial navigation
3229 // of the tab.
clamyea99ea12018-05-28 13:54:233230 if (!IsInitialNavigation()) {
3231 DiscardNonCommittedEntries();
3232 return;
3233 }
Fergal Dalyecd3b0202020-06-25 01:57:373234 // The current frame is always a main frame. If IsInitialNavigation() is
3235 // true then there have been no navigations and any frames of this tab must
3236 // be in the same renderer process. If that has crashed then the only frame
3237 // that can be revived is the main frame.
3238 frame_tree_node->render_manager()
3239 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233240 }
3241 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3242}
3243
clamy21718cc22018-06-13 13:34:243244std::unique_ptr<NavigationEntryImpl>
3245NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3246 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423247 const LoadURLParams& params,
3248 bool override_user_agent,
3249 bool should_replace_current_entry,
3250 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393251 // Browser initiated navigations might not have a blob_url_loader_factory set
3252 // in params even if the navigation is to a blob URL. If that happens, lookup
3253 // the correct url loader factory to use here.
3254 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483255 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473256 // Resolve the blob URL in the storage partition associated with the target
3257 // frame. This is the storage partition the URL will be loaded in, and only
3258 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393259 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473260 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393261 }
3262
clamy21718cc22018-06-13 13:34:243263 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443264 // extra_headers in params are \n separated; navigation entries want \r\n.
3265 std::string extra_headers_crlf;
3266 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243267
3268 // For subframes, create a pending entry with a corresponding frame entry.
3269 if (!node->IsMainFrame()) {
Tommy C. Li03eee77a2019-02-05 02:07:443270 if (GetLastCommittedEntry()) {
Tommy C. Li03eee77a2019-02-05 02:07:443271 entry = GetLastCommittedEntry()->Clone();
3272 } else {
3273 // If there's no last committed entry, create an entry for about:blank
3274 // with a subframe entry for our destination.
3275 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
3276 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063277 GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103278 params.source_site_instance.get(), params.transition_type,
3279 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Hayato Ito303654c2021-06-30 09:07:543280 blob_url_loader_factory));
Tommy C. Li03eee77a2019-02-05 02:07:443281 }
Nasko Oskov18006bc2018-12-06 02:53:583282
clamy21718cc22018-06-13 13:34:243283 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:083284 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
clamy21718cc22018-06-13 13:34:243285 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Anton Bikineevf62d1bf2021-05-15 17:56:073286 params.url, absl::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203287 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393288 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:483289 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori78a749f2020-11-30 12:03:393290 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163291 // a conversion of a new navigation into a reload, we will set the right
3292 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193293 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243294 } else {
3295 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243296 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063297 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103298 params.source_site_instance.get(), params.transition_type,
3299 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Hayato Ito303654c2021-06-30 09:07:543300 blob_url_loader_factory));
clamy21718cc22018-06-13 13:34:243301 entry->set_source_site_instance(
3302 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3303 entry->SetRedirectChain(params.redirect_chain);
3304 }
3305
3306 // Set the FTN ID (only used in non-site-per-process, for tests).
3307 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243308 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423309 entry->SetIsOverridingUserAgent(override_user_agent);
3310 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543311 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243312
clamy21718cc22018-06-13 13:34:243313 switch (params.load_type) {
3314 case LOAD_TYPE_DEFAULT:
3315 break;
3316 case LOAD_TYPE_HTTP_POST:
3317 entry->SetHasPostData(true);
3318 entry->SetPostData(params.post_data);
3319 break;
3320 case LOAD_TYPE_DATA:
3321 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3322 entry->SetVirtualURL(params.virtual_url_for_data_url);
3323#if defined(OS_ANDROID)
3324 entry->SetDataURLAsString(params.data_url_as_string);
3325#endif
3326 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3327 break;
clamy21718cc22018-06-13 13:34:243328 }
3329
3330 // TODO(clamy): NavigationEntry is meant for information that will be kept
3331 // after the navigation ended and therefore is not appropriate for
3332 // started_from_context_menu. Move started_from_context_menu to
3333 // NavigationUIData.
3334 entry->set_started_from_context_menu(params.started_from_context_menu);
3335
3336 return entry;
3337}
3338
clamyea99ea12018-05-28 13:54:233339std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423340NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3341 FrameTreeNode* node,
3342 const LoadURLParams& params,
3343 bool override_user_agent,
3344 bool should_replace_current_entry,
3345 bool has_user_gesture,
Antonio Sartori2f763d9d2021-04-21 10:04:143346 network::mojom::SourceLocationPtr source_location,
Yeunjoo Choi3df791a2021-02-17 07:07:253347 blink::NavigationDownloadPolicy download_policy,
Camille Lamy5193caa2018-10-12 11:59:423348 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573349 NavigationEntryImpl* entry,
Scott Violetcf6ea7e2021-06-09 21:09:213350 FrameNavigationEntry* frame_entry) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573351 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283352 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533353 // All renderer-initiated navigations must have an initiator_origin.
3354 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513355
Camille Lamy5193caa2018-10-12 11:59:423356 GURL url_to_load;
3357 GURL virtual_url;
Anton Bikineevf62d1bf2021-05-15 17:56:073358 absl::optional<url::Origin> origin_to_commit =
3359 frame_entry ? frame_entry->committed_origin() : absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:323360
Camille Lamy2baa8022018-10-19 16:43:173361 // For main frames, rewrite the URL if necessary and compute the virtual URL
3362 // that should be shown in the address bar.
3363 if (node->IsMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423364 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173365 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423366 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423367
Camille Lamy2baa8022018-10-19 16:43:173368 // For DATA loads, override the virtual URL.
3369 if (params.load_type == LOAD_TYPE_DATA)
3370 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423371
Camille Lamy2baa8022018-10-19 16:43:173372 if (virtual_url.is_empty())
3373 virtual_url = url_to_load;
3374
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573375 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283376
Aran Gilman249eb122019-12-02 23:32:463377 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3378 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3379 // return different results, leading to a different URL in the
3380 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3381 // pending NavigationEntry, as we'll only make one call to
3382 // RewriteUrlForNavigation.
3383 VLOG_IF(1, (url_to_load != frame_entry->url()))
3384 << "NavigationRequest and FrameEntry have different URLs: "
3385 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283386
Camille Lamy2baa8022018-10-19 16:43:173387 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423388 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173389 // NavigationRequest.
3390 } else {
3391 url_to_load = params.url;
3392 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243393 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173394 }
Camille Lamy5193caa2018-10-12 11:59:423395
Ehsan Karamad44fc72112019-02-26 18:15:473396 if (node->render_manager()->is_attaching_inner_delegate()) {
3397 // Avoid starting any new navigations since this node is now preparing for
3398 // attaching an inner delegate.
3399 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203400 }
Camille Lamy5193caa2018-10-12 11:59:423401
Camille Lamy5193caa2018-10-12 11:59:423402 if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load))
3403 return nullptr;
3404
Kunihiko Sakamoto346a74e2021-03-10 08:57:483405 if (!DoesURLMatchOriginForNavigation(
3406 url_to_load, origin_to_commit,
3407 frame_entry->subresource_web_bundle_navigation_info())) {
Nasko Oskov03912102019-01-11 00:21:323408 DCHECK(false) << " url:" << url_to_load
3409 << " origin:" << origin_to_commit.value();
3410 return nullptr;
3411 }
3412
Camille Lamy5193caa2018-10-12 11:59:423413 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403414 blink::PreviewsState previews_state =
3415 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
Camille Lamy5193caa2018-10-12 11:59:423416 if (!node->IsMainFrame()) {
3417 // For subframes, use the state of the top-level frame.
3418 previews_state = node->frame_tree()
3419 ->root()
3420 ->current_frame_host()
3421 ->last_navigation_previews_state();
3422 }
3423
Camille Lamy5193caa2018-10-12 11:59:423424 // This will be used to set the Navigation Timing API navigationStart
3425 // parameter for browser navigations in new tabs (intents, tabs opened through
3426 // "Open link in new tab"). If the navigation must wait on the current
3427 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3428 // will be updated when the BeforeUnload ack is received.
3429 base::TimeTicks navigation_start = base::TimeTicks::Now();
3430
danakjd83d706d2020-11-25 22:11:123431 // Look for a pending commit that is to another document in this
3432 // FrameTreeNode. If one exists, then the last committed URL will not be the
3433 // current URL by the time this navigation commits.
3434 bool has_pending_cross_document_commit =
3435 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493436 bool is_currently_error_page = node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123437
danakjb952ef12021-01-14 19:58:493438 mojom::NavigationType navigation_type = GetNavigationType(
3439 /*old_url=*/node->current_url(),
3440 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3441 has_pending_cross_document_commit, is_currently_error_page,
3442 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423443
3444 // Create the NavigationParams based on |params|.
3445
Hiroki Nakagawa4ed61282021-06-18 05:37:233446 bool is_view_source_mode = entry->IsViewSourceMode();
3447 DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme));
Charlie Harrison8c113a32019-01-07 16:08:293448
Hiroki Nakagawa4ed61282021-06-18 05:37:233449 // Update |download_policy| if the virtual URL is view-source.
Charlie Harrison8c113a32019-01-07 16:08:293450 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253451 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293452
Camille Lamy5193caa2018-10-12 11:59:423453 const GURL& history_url_for_data_url =
3454 params.base_url_for_data_url.is_empty() ? GURL() : virtual_url;
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513455 mojom::CommonNavigationParamsPtr common_params =
3456 mojom::CommonNavigationParams::New(
3457 url_to_load, params.initiator_origin,
3458 blink::mojom::Referrer::New(params.referrer.url,
3459 params.referrer.policy),
Scott Violetcf6ea7e2021-06-09 21:09:213460 params.transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513461 should_replace_current_entry, params.base_url_for_data_url,
3462 history_url_for_data_url, previews_state, navigation_start,
3463 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
Antonio Sartori2f763d9d2021-04-21 10:04:143464 params.post_data, std::move(source_location),
arthursonzogniaf7c62c52020-02-12 10:49:413465 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273466 false /* has_text_fragment_token */,
3467 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3468 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513469 false /* is_history_navigation_in_new_child_frame */,
Charlie Hu5ffc0152019-12-06 15:59:533470 params.input_start);
Camille Lamy5193caa2018-10-12 11:59:423471
Lucas Furukawa Gadania9c45682019-07-31 22:05:143472 mojom::CommitNavigationParamsPtr commit_params =
3473 mojom::CommitNavigationParams::New(
arthursonzognid5a8d0b2021-03-11 17:36:433474 frame_entry->committed_origin(), network::mojom::WebSandboxFlags(),
3475 override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323476 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143477 std::vector<net::RedirectInfo>(),
3478 std::string() /* post_content_type */, common_params->url,
3479 common_params->method, params.can_load_local_resources,
3480 frame_entry->page_state(), entry->GetUniqueID(),
3481 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3482 -1 /* pending_history_list_offset */,
3483 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3484 params.should_clear_history_list ? 0 : GetEntryCount(),
3485 false /* was_discarded */, is_view_source_mode,
3486 params.should_clear_history_list, mojom::NavigationTiming::New(),
Anton Bikineevf62d1bf2021-05-15 17:56:073487 absl::nullopt /* appcache_host_id */,
Minggang Wangf59db47b2021-06-16 01:56:223488 blink::mojom::WasActivatedOption::kUnknown,
Lucas Furukawa Gadania9c45682019-07-31 22:05:143489 base::UnguessableToken::Create() /* navigation_token */,
Minggang Wang7ee0c742021-06-16 16:16:513490 std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143491#if defined(OS_ANDROID)
3492 std::string(), /* data_url_as_string */
3493#endif
arthursonzogni14379782020-05-15 09:09:273494 !params.is_renderer_initiated, /* is_browser_initiated */
Sreeja Kamishetty46f762c2021-02-05 07:52:463495 node->frame_tree()->is_prerendering() /* is_prerendering */,
Tsuyoshi Horoe86d7702019-11-29 01:52:473496 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533497 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173498 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073499 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163500 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333501 false /* origin_agent_cluster */,
Maks Orlovichc66745a2020-06-30 17:40:023502 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213503 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Nate Chapind1fe3612021-04-16 20:45:573504 false /* is_cross_browsing_instance */, nullptr /* old_page_info */,
3505 -1 /* http_response_code */,
3506 std::vector<
3507 mojom::AppHistoryEntryPtr>() /* app_history_back_entries */,
3508 std::vector<
Kenichi Ishibashi17ecfb62021-06-21 03:31:153509 mojom::AppHistoryEntryPtr>() /* app_history_forward_entries */,
3510 std::vector<GURL>() /* early_hints_preloaded_resources */);
Camille Lamy5193caa2018-10-12 11:59:423511#if defined(OS_ANDROID)
3512 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143513 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423514 }
3515#endif
3516
Lucas Furukawa Gadania9c45682019-07-31 22:05:143517 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423518
3519 // A form submission may happen here if the navigation is a renderer-initiated
3520 // form submission that took the OpenURL path.
3521 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3522
3523 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3524 std::string extra_headers_crlf;
3525 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093526
3527 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143528 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083529 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453530 params.initiator_frame_token.has_value()
3531 ? &(params.initiator_frame_token.value())
3532 : nullptr,
3533 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3534 request_body,
John Delaney50425f82020-04-07 16:26:213535 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
3536 params.impression);
Yao Xiaodc5ed102019-06-04 19:19:093537 navigation_request->set_from_download_cross_origin_redirect(
3538 params.from_download_cross_origin_redirect);
3539 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423540}
3541
3542std::unique_ptr<NavigationRequest>
3543NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233544 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573545 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233546 FrameNavigationEntry* frame_entry,
3547 ReloadType reload_type,
3548 bool is_same_document_history_load,
Arthur Hemerybee4a752019-05-29 10:50:553549 bool is_history_navigation_in_new_child_frame) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343550 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233551 GURL dest_url = frame_entry->url();
Anton Bikineevf62d1bf2021-05-15 17:56:073552 absl::optional<url::Origin> origin_to_commit =
Nasko Oskov03912102019-01-11 00:21:323553 frame_entry->committed_origin();
3554
clamyea99ea12018-05-28 13:54:233555 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013556 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573557 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233558 // We may have been redirected when navigating to the current URL.
3559 // Use the URL the user originally intended to visit as signaled by the
3560 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013561 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573562 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233563 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323564 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233565 }
3566
Ehsan Karamad44fc72112019-02-26 18:15:473567 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3568 // Avoid starting any new navigations since this node is now preparing for
3569 // attaching an inner delegate.
3570 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203571 }
3572
Camille Lamy5193caa2018-10-12 11:59:423573 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573574 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233575 return nullptr;
3576 }
3577
Kunihiko Sakamoto346a74e2021-03-10 08:57:483578 if (!DoesURLMatchOriginForNavigation(
3579 dest_url, origin_to_commit,
3580 frame_entry->subresource_web_bundle_navigation_info())) {
3581 DCHECK(false) << " url:" << dest_url
3582 << " origin:" << origin_to_commit.value();
Nasko Oskov03912102019-01-11 00:21:323583 return nullptr;
3584 }
3585
clamyea99ea12018-05-28 13:54:233586 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403587 blink::PreviewsState previews_state =
3588 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
clamyea99ea12018-05-28 13:54:233589 if (!frame_tree_node->IsMainFrame()) {
3590 // For subframes, use the state of the top-level frame.
3591 previews_state = frame_tree_node->frame_tree()
3592 ->root()
3593 ->current_frame_host()
3594 ->last_navigation_previews_state();
clamyea99ea12018-05-28 13:54:233595 }
3596
clamyea99ea12018-05-28 13:54:233597 // This will be used to set the Navigation Timing API navigationStart
3598 // parameter for browser navigations in new tabs (intents, tabs opened through
3599 // "Open link in new tab"). If the navigation must wait on the current
3600 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3601 // will be updated when the BeforeUnload ack is received.
3602 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233603
danakjd83d706d2020-11-25 22:11:123604 // Look for a pending commit that is to another document in this
3605 // FrameTreeNode. If one exists, then the last committed URL will not be the
3606 // current URL by the time this navigation commits.
3607 bool has_pending_cross_document_commit =
3608 frame_tree_node->render_manager()
3609 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493610 bool is_currently_error_page =
3611 frame_tree_node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123612
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513613 mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123614 /*old_url=*/frame_tree_node->current_url(),
3615 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493616 has_pending_cross_document_commit, is_currently_error_page,
3617 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423618
3619 // A form submission may happen here if the navigation is a
3620 // back/forward/reload navigation that does a form resubmission.
3621 scoped_refptr<network::ResourceRequestBody> request_body;
3622 std::string post_content_type;
3623 if (frame_entry->method() == "POST") {
3624 request_body = frame_entry->GetPostData(&post_content_type);
3625 // Might have a LF at end.
Peter Kastingb53b81912021-04-28 19:23:303626 post_content_type = std::string(
3627 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL));
Camille Lamy5193caa2018-10-12 11:59:423628 }
3629
3630 // Create the NavigationParams based on |entry| and |frame_entry|.
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513631 mojom::CommonNavigationParamsPtr common_params =
3632 entry->ConstructCommonNavigationParams(
3633 *frame_entry, request_body, dest_url,
3634 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
3635 navigation_type, previews_state, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533636 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513637 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553638 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423639
3640 // TODO(clamy): |intended_as_new_entry| below should always be false once
3641 // Reload no longer leads to this being called for a pending NavigationEntry
3642 // of index -1.
Lucas Furukawa Gadania9c45682019-07-31 22:05:143643 mojom::CommitNavigationParamsPtr commit_params =
3644 entry->ConstructCommitNavigationParams(
3645 *frame_entry, common_params->url, origin_to_commit,
3646 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3647 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533648 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3649 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143650 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423651
clamyea99ea12018-05-28 13:54:233652 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143653 frame_tree_node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083654 !entry->is_renderer_initiated(), false /* was_opener_suppressed */,
3655 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453656 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3657 entry->extra_headers(), frame_entry, entry, request_body,
Anton Bikineevf62d1bf2021-05-15 17:56:073658 nullptr /* navigation_ui_data */, absl::nullopt /* impression */);
clamyea99ea12018-05-28 13:54:233659}
3660
[email protected]d202a7c2012-01-04 07:53:473661void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213662 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273663 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403664
[email protected]2db9bd72012-04-13 20:20:563665 // We need to notify the ssl_manager_ before the web_contents_ so the
[email protected]df1af242009-05-01 00:11:403666 // location bar will have up-to-date information about the security style
3667 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133668 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403669
[email protected]7f924832014-08-09 05:57:223670 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:573671 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463672
[email protected]b0f724c2013-09-05 04:21:133673 // TODO(avi): Remove. http://crbug.com/170921
3674 NotificationDetails notification_details =
3675 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153676 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3677 Source<NavigationController>(this),
3678 notification_details);
initial.commit09911bf2008-07-26 23:55:293679}
3680
initial.commit09911bf2008-07-26 23:55:293681// static
[email protected]d202a7c2012-01-04 07:53:473682size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233683 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153684 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:213685 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233686}
3687
[email protected]d202a7c2012-01-04 07:53:473688void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223689 if (is_active && needs_reload_)
3690 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293691}
3692
[email protected]d202a7c2012-01-04 07:53:473693void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293694 if (!needs_reload_)
3695 return;
3696
Bo Liucdfa4b12018-11-06 00:21:443697 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3698 needs_reload_type_);
3699
initial.commit09911bf2008-07-26 23:55:293700 // Calling Reload() results in ignoring state, and not loading.
3701 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3702 // cached state.
avicc872d7242015-08-19 21:26:343703 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163704 NavigateToExistingPendingEntry(ReloadType::NONE,
3705 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343706 } else if (last_committed_entry_index_ != -1) {
arthursonzogni5c4c202d2017-04-25 23:41:273707 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343708 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163709 NavigateToExistingPendingEntry(ReloadType::NONE,
3710 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343711 } else {
3712 // If there is something to reload, the successful reload will clear the
3713 // |needs_reload_| flag. Otherwise, just do it here.
3714 needs_reload_ = false;
3715 }
initial.commit09911bf2008-07-26 23:55:293716}
3717
Carlos IL42b416592019-10-07 23:10:363718void NavigationControllerImpl::LoadPostCommitErrorPage(
3719 RenderFrameHost* render_frame_host,
3720 const GURL& url,
3721 const std::string& error_page_html,
3722 net::Error error) {
Rakina Zata Amni919b7922020-12-11 09:03:133723 RenderFrameHostImpl* rfhi =
3724 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:583725
3726 // Only active documents can load post-commit error pages:
3727 // - If the document is in pending deletion, the browser already committed to
3728 // destroying this RenderFrameHost so ignore loading the error page.
3729 // - If the document is in back-forward cache, it's not allowed to navigate as
3730 // it should remain frozen. Ignore the request and evict the document from
3731 // back-forward cache.
3732 // - If the document is prerendering, it can navigate but when loading error
3733 // pages, cancel prerendering.
3734 if (rfhi->IsInactiveAndDisallowActivation())
3735 return;
3736
Rakina Zata Amni919b7922020-12-11 09:03:133737 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:413738
3739 mojom::CommonNavigationParamsPtr common_params =
3740 CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:593741 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
3742 // the frame actually committed (e.g. iframe with "src" set to a
3743 // slow-responding URL). We should rewrite the URL to about:blank in this
3744 // case, as the renderer will only think a page is an error page if it has a
3745 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:133746 common_params->url = url.is_empty() ? GURL("about:blank") : url;
John Delaney131ad362019-08-08 21:57:413747 mojom::CommitNavigationParamsPtr commit_params =
3748 CreateCommitNavigationParams();
Antonio Sartori58591c892021-04-21 06:54:333749 commit_params->original_url = common_params->url;
John Delaney131ad362019-08-08 21:57:413750
arthursonzogni70ac7302020-05-28 08:49:053751 // Error pages have a fully permissive FramePolicy.
3752 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
3753 // error pages.
3754 commit_params->frame_policy = blink::FramePolicy();
3755
John Delaney131ad362019-08-08 21:57:413756 std::unique_ptr<NavigationRequest> navigation_request =
3757 NavigationRequest::CreateBrowserInitiated(
3758 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083759 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:193760 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453761 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:063762 "" /* extra_headers */, nullptr /* frame_entry */,
3763 nullptr /* entry */, nullptr /* post_body */,
Anton Bikineevf62d1bf2021-05-15 17:56:073764 nullptr /* navigation_ui_data */, absl::nullopt /* impression */);
Carlos IL42b416592019-10-07 23:10:363765 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:413766 navigation_request->set_net_error(error);
3767 node->CreatedNavigationRequest(std::move(navigation_request));
3768 DCHECK(node->navigation_request());
3769 node->navigation_request()->BeginNavigation();
3770}
3771
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573772void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213773 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093774 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:153775 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143776 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293777}
3778
[email protected]d202a7c2012-01-04 07:53:473779void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363780 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553781 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363782 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293783
initial.commit09911bf2008-07-26 23:55:293784 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293785}
[email protected]765b35502008-08-21 00:51:203786
arthursonzogni69a6a1b2019-09-17 09:23:003787void NavigationControllerImpl::DiscardNonCommittedEntries() {
Michael Thiessen9b14d512019-09-23 21:19:473788 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:103789 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
3790 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:293791 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:473792 return;
Michael Thiessenc5676d22019-09-25 22:32:103793 }
avi45a72532015-04-07 21:01:453794 DiscardPendingEntry(false);
arthursonzogni69a6a1b2019-09-17 09:23:003795 if (delegate_)
3796 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:483797}
3798
avi7c6f35e2015-05-08 17:52:383799int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3800 int nav_entry_id) const {
3801 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3802 if (entries_[i]->GetUniqueID() == nav_entry_id)
3803 return i;
3804 }
3805 return -1;
3806}
3807
[email protected]d202a7c2012-01-04 07:53:473808void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573809 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253810 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573811 DCHECK_LE(max_index, source->GetEntryCount());
Nate Chapin214a86a2021-06-21 20:35:573812 std::unique_ptr<NavigationEntryRestoreContextImpl> context =
3813 std::make_unique<NavigationEntryRestoreContextImpl>();
[email protected]e1cd5452010-08-26 18:03:253814 for (int i = 0; i < max_index; i++) {
Nate Chapin9f169072021-06-09 19:32:373815 // Normally, cloning a NavigationEntryImpl results in sharing
3816 // FrameNavigationEntries between the original and the clone. However, when
3817 // cloning from a different NavigationControllerImpl, we want to fork the
3818 // FrameNavigationEntries.
Nate Chapin9f169072021-06-09 19:32:373819 entries_.insert(entries_.begin() + i,
Nate Chapin214a86a2021-06-21 20:35:573820 source->entries_[i]->CloneWithoutSharing(context.get()));
[email protected]e1cd5452010-08-26 18:03:253821 }
arthursonzogni5c4c202d2017-04-25 23:41:273822 DCHECK(pending_entry_index_ == -1 ||
3823 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:253824}
[email protected]c5b88d82012-10-06 17:03:333825
3826void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:183827 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:333828 get_timestamp_callback_ = get_timestamp_callback;
3829}
[email protected]8ff00d72012-10-23 19:12:213830
Shivani Sharmaffb32b82019-04-09 16:58:473831// History manipulation intervention:
3832void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:473833 bool replace_entry,
3834 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:403835 bool is_renderer_initiated,
3836 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:453837 // Note that for a subframe, previous_document_was_activated is true if the
3838 // gesture happened in any subframe (propagated to main frame) or in the main
3839 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:473840 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:273841 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:473842 return;
3843 }
3844 if (last_committed_entry_index_ == -1)
3845 return;
3846
Shivani Sharmac4cc8922019-04-18 03:11:173847 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473848
Alexander Timine3ec4192020-04-20 16:39:403849 // Log UKM with the URL we are navigating away from.
3850 ukm::builders::HistoryManipulationIntervention(
3851 previous_page_load_ukm_source_id)
3852 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:473853}
3854
Shivani Sharmac4cc8922019-04-18 03:11:173855void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
3856 int reference_index,
3857 bool skippable) {
3858 auto* reference_entry = GetEntryAtIndex(reference_index);
3859 reference_entry->set_should_skip_on_back_forward_ui(skippable);
3860
3861 int64_t document_sequence_number =
3862 reference_entry->root_node()->frame_entry->document_sequence_number();
3863 for (int index = 0; index < GetEntryCount(); index++) {
3864 auto* entry = GetEntryAtIndex(index);
3865 if (entry->root_node()->frame_entry->document_sequence_number() ==
3866 document_sequence_number) {
3867 entry->set_should_skip_on_back_forward_ui(skippable);
3868 }
3869 }
3870}
3871
arthursonzogni66f711c2019-10-08 14:40:363872std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
3873NavigationControllerImpl::ReferencePendingEntry() {
3874 DCHECK(pending_entry_);
3875 auto pending_entry_ref =
3876 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
3877 pending_entry_refs_.insert(pending_entry_ref.get());
3878 return pending_entry_ref;
3879}
3880
3881void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
3882 // Ignore refs that don't correspond to the current pending entry.
3883 auto it = pending_entry_refs_.find(ref);
3884 if (it == pending_entry_refs_.end())
3885 return;
3886 pending_entry_refs_.erase(it);
3887
3888 if (!pending_entry_refs_.empty())
3889 return;
3890
3891 // The pending entry may be deleted before the last PendingEntryRef.
3892 if (!pending_entry_)
3893 return;
3894
3895 // We usually clear the pending entry when the matching NavigationRequest
3896 // fails, so that an arbitrary URL isn't left visible above a committed page.
3897 //
3898 // However, we do preserve the pending entry in some cases, such as on the
3899 // initial navigation of an unmodified blank tab. We also allow the delegate
3900 // to say when it's safe to leave aborted URLs in the omnibox, to let the
3901 // user edit the URL and try again. This may be useful in cases that the
3902 // committed page cannot be attacker-controlled. In these cases, we still
3903 // allow the view to clear the pending entry and typed URL if the user
3904 // requests (e.g., hitting Escape with focus in the address bar).
3905 //
3906 // Do not leave the pending entry visible if it has an invalid URL, since this
3907 // might be formatted in an unexpected or unsafe way.
3908 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:363909 bool should_preserve_entry =
3910 (pending_entry_ == GetVisibleEntry()) &&
3911 pending_entry_->GetURL().is_valid() &&
3912 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
3913 if (should_preserve_entry)
3914 return;
3915
3916 DiscardPendingEntry(true);
3917 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3918}
3919
Titouan Rigoudy6ec70402021-02-02 15:42:193920std::unique_ptr<PolicyContainerPolicies>
3921NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:393922 RenderFrameHostImpl* rfh,
3923 bool is_same_document,
3924 NavigationRequest* request) {
Titouan Rigoudy6ec70402021-02-02 15:42:193925 if (!ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(request))
Antonio Sartori78a749f2020-11-30 12:03:393926 return nullptr;
3927
3928 if (is_same_document) {
Charlie Reis73e356242021-04-02 17:10:313929 // TODO(https://crbug.com/524208): Remove this nullptr check when we can
3930 // ensure we always have a FrameNavigationEntry here.
3931 if (!GetLastCommittedEntry())
3932 return nullptr;
3933
Antonio Sartori78a749f2020-11-30 12:03:393934 FrameNavigationEntry* previous_frame_entry =
3935 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
3936
3937 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
3938 // ensure we always have a FrameNavigationEntry here.
3939 if (!previous_frame_entry)
3940 return nullptr;
3941
Titouan Rigoudy6ec70402021-02-02 15:42:193942 const PolicyContainerPolicies* previous_policies =
3943 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:393944
Titouan Rigoudy6ec70402021-02-02 15:42:193945 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:393946 return nullptr;
3947
3948 // Make a copy of the policy container for the new FrameNavigationEntry.
Antonio Sartori5d09b30f2021-03-02 09:27:163949 return previous_policies->Clone();
Antonio Sartori78a749f2020-11-30 12:03:393950 }
3951
Antonio Sartori4f5373792021-05-31 10:56:473952 return rfh->policy_container_host()->policies().Clone();
Antonio Sartori78a749f2020-11-30 12:03:393953}
3954
Hayato Ito2c8c08d02021-06-23 03:38:433955void NavigationControllerImpl::BroadcastHistoryOffsetAndLength() {
Carlos Caballeroede6f8c2021-01-28 11:01:503956 OPTIONAL_TRACE_EVENT2(
Hayato Ito2c8c08d02021-06-23 03:38:433957 "content", "NavigationControllerImpl::BroadcastHistoryOffsetAndLength",
3958 "history_offset", GetLastCommittedEntryIndex(), "history_length",
3959 GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:503960
3961 auto callback = base::BindRepeating(
3962 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
3963 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
3964 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
3965 }
3966 },
Hayato Ito2c8c08d02021-06-23 03:38:433967 GetLastCommittedEntryIndex(), GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:503968 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
3969}
3970
3971void NavigationControllerImpl::DidAccessInitialMainDocument() {
3972 // We may have left a failed browser-initiated navigation in the address bar
3973 // to let the user edit it and try again. Clear it now that content might
3974 // show up underneath it.
3975 if (!frame_tree_.IsLoading() && GetPendingEntry())
3976 DiscardPendingEntry(false);
3977
3978 // Update the URL display.
3979 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3980}
3981
3982void NavigationControllerImpl::UpdateStateForFrame(
3983 RenderFrameHostImpl* rfhi,
3984 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:563985 OPTIONAL_TRACE_EVENT1("content",
3986 "NavigationControllerImpl::UpdateStateForFrame",
3987 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:503988 // The state update affects the last NavigationEntry associated with the given
3989 // |render_frame_host|. This may not be the last committed NavigationEntry (as
3990 // in the case of an UpdateState from a frame being swapped out). We track
3991 // which entry this is in the RenderFrameHost's nav_entry_id.
3992 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
3993 if (!entry)
3994 return;
3995
3996 FrameNavigationEntry* frame_entry =
3997 entry->GetFrameEntry(rfhi->frame_tree_node());
3998 if (!frame_entry)
3999 return;
4000
4001 // The SiteInstance might not match if we do a cross-process navigation with
4002 // replacement (e.g., auto-subframe), in which case the swap out of the old
4003 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4004 // has already been replaced and destroyed.
4005 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4006 return;
4007
4008 if (page_state == frame_entry->page_state())
4009 return; // Nothing to update.
4010
4011 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4012
4013 // The document_sequence_number and item_sequence_number recorded in the
4014 // FrameNavigationEntry should not differ from the one coming with the update,
4015 // since it must come from the same document. Do not update it if a difference
4016 // is detected, as this indicates that |frame_entry| is not the correct one.
4017 blink::ExplodedPageState exploded_state;
4018 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4019 return;
4020
4021 if (exploded_state.top.document_sequence_number !=
4022 frame_entry->document_sequence_number() ||
4023 exploded_state.top.item_sequence_number !=
4024 frame_entry->item_sequence_number()) {
4025 return;
4026 }
4027
4028 frame_entry->SetPageState(page_state);
4029 NotifyEntryChanged(entry);
4030}
4031
Aaron Colwellb731a0ae2021-03-19 19:14:474032void NavigationControllerImpl::OnStoragePartitionIdAdded(
4033 const StoragePartitionId& partition_id) {
4034 auto it = partition_config_to_id_map_.insert(
4035 std::make_pair(partition_id.config(), partition_id));
4036 bool successful_insert = it.second;
4037 DCHECK(successful_insert);
4038 if (!successful_insert) {
4039 LogStoragePartitionIdCrashKeys(it.first->second, partition_id);
4040 }
4041}
4042
4043void NavigationControllerImpl::LogStoragePartitionIdCrashKeys(
4044 const StoragePartitionId& original_partition_id,
4045 const StoragePartitionId& new_partition_id) {
4046 base::debug::SetCrashKeyString(
4047 base::debug::AllocateCrashKeyString("original_partition_id",
4048 base::debug::CrashKeySize::Size256),
4049 original_partition_id.ToString());
4050
4051 base::debug::SetCrashKeyString(
4052 base::debug::AllocateCrashKeyString("new_partition_id",
4053 base::debug::CrashKeySize::Size256),
4054 new_partition_id.ToString());
4055
4056 base::debug::DumpWithoutCrashing();
4057}
4058
Nate Chapind1fe3612021-04-16 20:45:574059std::vector<mojom::AppHistoryEntryPtr>
4060NavigationControllerImpl::PopulateSingleAppHistoryEntryVector(
4061 Direction direction,
4062 int entry_index,
4063 const url::Origin& pending_origin,
4064 FrameTreeNode* node,
4065 SiteInstance* site_instance,
4066 int64_t previous_item_sequence_number) {
4067 std::vector<mojom::AppHistoryEntryPtr> entries;
4068 int offset = direction == Direction::kForward ? 1 : -1;
4069 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4070 i += offset) {
4071 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
4072 if (!frame_entry || !frame_entry->committed_origin())
4073 break;
4074 if (site_instance != frame_entry->site_instance())
4075 break;
4076 if (!pending_origin.IsSameOriginWith(*frame_entry->committed_origin()))
4077 break;
4078 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4079 continue;
4080 blink::ExplodedPageState exploded_page_state;
4081 if (blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4082 &exploded_page_state)) {
4083 blink::ExplodedFrameState frame_state = exploded_page_state.top;
4084 mojom::AppHistoryEntryPtr entry = mojom::AppHistoryEntry::New(
4085 frame_state.app_history_key.value_or(std::u16string()),
4086 frame_state.app_history_id.value_or(std::u16string()),
4087 frame_state.url_string.value_or(std::u16string()));
4088 DCHECK(pending_origin.CanBeDerivedFrom(GURL(entry->url)));
4089 entries.push_back(std::move(entry));
4090 previous_item_sequence_number = frame_entry->item_sequence_number();
4091 }
4092 }
4093 // If |entries| was constructed by iterating backwards from
4094 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4095 // earliest-at-the-front. Reverse it.
4096 if (direction == Direction::kBack)
4097 std::reverse(entries.begin(), entries.end());
4098 return entries;
4099}
4100
4101void NavigationControllerImpl::PopulateAppHistoryEntryVectors(
4102 NavigationRequest* request) {
4103 url::Origin pending_origin =
4104 request->commit_params().origin_to_commit
4105 ? *request->commit_params().origin_to_commit
4106 : url::Origin::Create(request->common_params().url);
4107
4108 FrameTreeNode* node = request->frame_tree_node();
4109 scoped_refptr<SiteInstance> site_instance =
4110 request->GetRenderFrameHost()->GetSiteInstance();
4111
4112 // NOTE: |entry_index| is an estimate of the index where this entry will
4113 // commit, but it may be wrong in corner cases (e.g., if we are at the max
4114 // entry limit, the earliest entry will be dropped). This is ok because this
4115 // algorithm only uses |entry_index| to walk the entry list as it stands right
4116 // now, and it isn't saved for anything post-commit.
4117 int entry_index = GetPendingEntryIndex();
4118 bool will_create_new_entry = false;
4119 if (NavigationTypeUtils::IsReload(request->common_params().navigation_type) ||
4120 request->common_params().should_replace_current_entry) {
4121 entry_index = GetLastCommittedEntryIndex();
4122 } else if (entry_index == -1) {
4123 will_create_new_entry = true;
4124 entry_index = GetLastCommittedEntryIndex() + 1;
4125 }
4126
4127 int64_t pending_item_sequence_number = 0;
4128 if (auto* pending_entry = GetPendingEntry()) {
4129 if (auto* frame_entry = pending_entry->GetFrameEntry(node))
4130 pending_item_sequence_number = frame_entry->item_sequence_number();
4131 }
4132
4133 request->set_app_history_back_entries(PopulateSingleAppHistoryEntryVector(
4134 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
4135 pending_item_sequence_number));
4136
4137 // Don't populate forward entries if they will be truncated by a new entry.
4138 if (!will_create_new_entry) {
4139 request->set_app_history_forward_entries(
4140 PopulateSingleAppHistoryEntryVector(
4141 Direction::kForward, entry_index, pending_origin, node,
4142 site_instance.get(), pending_item_sequence_number));
4143 }
4144}
4145
Nate Chapinfbfe5af2021-06-10 17:22:084146NavigationControllerImpl::HistoryNavigationAction
4147NavigationControllerImpl::ShouldNavigateToEntryForAppHistoryKey(
4148 FrameNavigationEntry* current_entry,
4149 FrameNavigationEntry* target_entry,
4150 const std::string& app_history_key) {
4151 if (!target_entry || !target_entry->committed_origin())
4152 return HistoryNavigationAction::kStopLooking;
4153 if (current_entry->site_instance() != target_entry->site_instance())
4154 return HistoryNavigationAction::kStopLooking;
4155 if (!current_entry->committed_origin()->IsSameOriginWith(
4156 *target_entry->committed_origin())) {
4157 return HistoryNavigationAction::kStopLooking;
4158 }
4159
4160 // NOTE: We don't actually care between kSameDocument and
4161 // kDifferentDocument, so always use kDifferentDocument by convention.
4162 if (target_entry->app_history_key() == app_history_key)
4163 return HistoryNavigationAction::kDifferentDocument;
4164 return HistoryNavigationAction::kKeepLooking;
4165}
4166
4167void NavigationControllerImpl::NavigateToAppHistoryKey(FrameTreeNode* node,
4168 const std::string& key) {
4169 FrameNavigationEntry* current_entry =
4170 GetLastCommittedEntry()->GetFrameEntry(node);
4171 if (!current_entry)
4172 return;
4173
4174 // We want to find the nearest matching entry that is contiguously
4175 // same-instance and same-origin. Check back first, then forward.
4176 // TODO(japhet): Link spec here once it exists.
4177 for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) {
4178 auto result = ShouldNavigateToEntryForAppHistoryKey(
4179 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4180 if (result == HistoryNavigationAction::kStopLooking)
4181 break;
4182 if (result != HistoryNavigationAction::kKeepLooking) {
4183 GoToIndex(i);
4184 return;
4185 }
4186 }
4187 for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) {
4188 auto result = ShouldNavigateToEntryForAppHistoryKey(
4189 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4190 if (result == HistoryNavigationAction::kStopLooking)
4191 break;
4192 if (result != HistoryNavigationAction::kKeepLooking) {
4193 GoToIndex(i);
4194 return;
4195 }
4196 }
4197}
4198
[email protected]8ff00d72012-10-23 19:12:214199} // namespace content