blob: 194670c21f7365542fe095f7a7e57fe0b65b134b [file] [log] [blame]
[email protected]d4a8ca482013-10-30 21:06:401// Copyright 2013 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
creis4e2ecb72015-06-20 00:46:305/*
6 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
7 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved.
9 * (http://www.torchmobile.com/)
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
21 * its contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
25 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
28 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
danakjc492bf82020-09-09 20:02:4436#include "content/browser/renderer_host/navigation_controller_impl.h"
initial.commit09911bf2008-07-26 23:55:2937
Lei Zhang96031532019-10-10 19:05:4738#include <algorithm>
dcheng36b6aec92015-12-26 06:16:3639#include <utility>
40
[email protected]c5b88d82012-10-06 17:03:3341#include "base/bind.h"
[email protected]eabfe1912014-05-12 10:07:2842#include "base/command_line.h"
initial.commit09911bf2008-07-26 23:55:2943#include "base/logging.h"
asvitkine30330812016-08-30 04:01:0844#include "base/metrics/histogram_macros.h"
Chris Hamilton83272dc2021-02-23 00:24:0245#include "base/stl_util.h"
Peter Kastingb53b81912021-04-28 19:23:3046#include "base/strings/string_piece.h"
[email protected]348fbaac2013-06-11 06:31:5147#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0048#include "base/strings/utf_string_conversions.h"
[email protected]a43858f2013-06-28 15:18:3749#include "base/time/time.h"
Carlos Caballero40b0efd2021-01-26 11:55:0050#include "base/trace_event/optional_trace_event.h"
Carlos Caballeroede6f8c2021-01-28 11:01:5051#include "base/trace_event/trace_conversion_helper.h"
ssid3e765612015-01-28 04:03:4252#include "base/trace_event/trace_event.h"
servolkf3955532015-05-16 00:01:5953#include "build/build_config.h"
[email protected]eabfe1912014-05-12 10:07:2854#include "cc/base/switches.h"
jamescookda2505812015-03-20 18:01:1855#include "content/browser/bad_message.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3956#include "content/browser/blob_storage/chrome_blob_storage_context.h"
[email protected]825b1662012-03-12 19:07:3157#include "content/browser/browser_url_handler_impl.h"
[email protected]5f2aa722013-08-07 16:59:4158#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0459#include "content/browser/dom_storage/session_storage_namespace_impl.h"
danakjc492bf82020-09-09 20:02:4460#include "content/browser/renderer_host/debug_urls.h"
61#include "content/browser/renderer_host/frame_tree.h"
Carlos Caballero40b0efd2021-01-26 11:55:0062#include "content/browser/renderer_host/frame_tree_node.h"
danakjc492bf82020-09-09 20:02:4463#include "content/browser/renderer_host/navigation_entry_impl.h"
Nate Chapin214a86a2021-06-21 20:35:5764#include "content/browser/renderer_host/navigation_entry_restore_context_impl.h"
danakjc492bf82020-09-09 20:02:4465#include "content/browser/renderer_host/navigation_request.h"
66#include "content/browser/renderer_host/navigator.h"
67#include "content/browser/renderer_host/render_frame_host_delegate.h"
Carlos Caballero40b0efd2021-01-26 11:55:0068#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]b6583592012-01-25 19:52:3369#include "content/browser/site_instance_impl.h"
Kunihiko Sakamoto346a74e2021-03-10 08:57:4870#include "content/browser/web_package/subresource_web_bundle_navigation_info.h"
Tsuyoshi Horo37493af2019-11-21 23:43:1471#include "content/browser/web_package/web_bundle_navigation_info.h"
Camille Lamy5193caa2018-10-12 11:59:4272#include "content/common/content_constants_internal.h"
Nate Chapind1fe3612021-04-16 20:45:5773#include "content/common/navigation_params_utils.h"
Nasko Oskovae49e292020-08-13 02:08:5174#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1175#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1976#include "content/public/browser/content_browser_client.h"
Fergal Daly1336ac642021-09-14 15:13:1177#include "content/public/browser/disallow_activation_reason.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"
Minggang Wanga13c796e2021-07-02 05:54:43103#include "third_party/blink/public/common/navigation/navigation_params.h"
Carlos Caballeroede6f8c2021-01-28 11:01:50104#include "third_party/blink/public/common/page_state/page_state_serialization.h"
Minggang Wangb9f3fa92021-07-01 15:30:31105#include "third_party/blink/public/mojom/navigation/navigation_params.mojom.h"
Minggang Wang7ee0c742021-06-16 16:16:51106#include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h"
[email protected]cca6f392014-05-28 21:32:26107#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:29108
[email protected]8ff00d72012-10-23 19:12:21109namespace content {
[email protected]e9ba4472008-09-14 15:42:43110namespace {
111
112// Invoked when entries have been pruned, or removed. For example, if the
113// current entries are [google, digg, yahoo], with the current entry google,
114// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47115void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50116 int index,
[email protected]c12bf1a12008-09-17 16:28:49117 int count) {
[email protected]8ff00d72012-10-23 19:12:21118 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50119 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49120 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14121 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43122}
123
[email protected]e9ba4472008-09-14 15:42:43124// Configure all the NavigationEntries in entries for restore. This resets
125// the transition type to reload and makes sure the content state isn't empty.
126void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57127 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48128 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47129 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43130 // Use a transition type of reload so that we don't incorrectly increase
131 // the typed count.
Lei Zhang96031532019-10-10 19:05:47132 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
133 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43134 }
135}
136
[email protected]bf70edce2012-06-20 22:32:22137// Determines whether or not we should be carrying over a user agent override
138// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57139bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22140 return last_entry && last_entry->GetIsOverridingUserAgent();
141}
142
Camille Lamy5193caa2018-10-12 11:59:42143// Determines whether to override user agent for a navigation.
144bool ShouldOverrideUserAgent(
145 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57146 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42147 switch (override_user_agent) {
148 case NavigationController::UA_OVERRIDE_INHERIT:
149 return ShouldKeepOverride(last_committed_entry);
150 case NavigationController::UA_OVERRIDE_TRUE:
151 return true;
152 case NavigationController::UA_OVERRIDE_FALSE:
153 return false;
Camille Lamy5193caa2018-10-12 11:59:42154 }
155 NOTREACHED();
156 return false;
157}
158
Rakina Zata Amni312822d72021-06-04 16:13:37159// Returns true if this navigation should be treated as a reload. For e.g.
clamy0a656e42018-02-06 18:18:28160// navigating to the last committed url via the address bar or clicking on a
Rakina Zata Amni312822d72021-06-04 16:13:37161// link which results in a navigation to the last committed URL (but wasn't
162// converted to do a replacement navigation in the renderer), etc.
Fergal Daly766177d2020-07-07 07:54:04163// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
164// |base_url_for_data_url|, |transition_type| correspond to the new navigation
165// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
166// navigation that committed.
167bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
168 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57169 const GURL& virtual_url,
170 const GURL& base_url_for_data_url,
171 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57172 bool is_post,
Rakina Zata Amni312822d72021-06-04 16:13:37173 bool should_replace_current_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57174 NavigationEntryImpl* last_committed_entry) {
Rakina Zata Amni312822d72021-06-04 16:13:37175 // Navigations intended to do a replacement shouldn't be converted to do a
176 // reload.
177 if (should_replace_current_entry)
clamy0a656e42018-02-06 18:18:28178 return false;
clamy0a656e42018-02-06 18:18:28179 // Only convert to reload if at least one navigation committed.
180 if (!last_committed_entry)
ananta3bdd8ae2016-12-22 17:11:55181 return false;
182
arthursonzogni7a8243682017-12-14 16:41:42183 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28184 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42185 return false;
186
ananta3bdd8ae2016-12-22 17:11:55187 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
188 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
189 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28190 bool transition_type_can_be_converted = false;
191 if (ui::PageTransitionCoreTypeIs(transition_type,
192 ui::PAGE_TRANSITION_RELOAD) &&
193 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
194 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55195 }
clamy0a656e42018-02-06 18:18:28196 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55197 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28198 transition_type_can_be_converted = true;
199 }
200 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
201 transition_type_can_be_converted = true;
202 if (!transition_type_can_be_converted)
203 return false;
204
205 // This check is required for cases like view-source:, etc. Here the URL of
206 // the navigation entry would contain the url of the page, while the virtual
207 // URL contains the full URL including the view-source prefix.
208 if (virtual_url != last_committed_entry->GetVirtualURL())
209 return false;
210
Fergal Daly766177d2020-07-07 07:54:04211 // Check that the URLs match.
212 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
213 // If there's no frame entry then by definition the URLs don't match.
214 if (!frame_entry)
215 return false;
216
217 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28218 return false;
219
220 // This check is required for Android WebView loadDataWithBaseURL. Apps
221 // can pass in anything in the base URL and we need to ensure that these
222 // match before classifying it as a reload.
223 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
224 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
225 return false;
ananta3bdd8ae2016-12-22 17:11:55226 }
227
clamy0a656e42018-02-06 18:18:28228 // Skip entries with SSL errors.
229 if (last_committed_entry->ssl_error())
230 return false;
231
232 // Don't convert to a reload when the last navigation was a POST or the new
233 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04234 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28235 return false;
236
237 return true;
ananta3bdd8ae2016-12-22 17:11:55238}
239
Nasko Oskov03912102019-01-11 00:21:32240bool DoesURLMatchOriginForNavigation(
241 const GURL& url,
Anton Bikineevf62d1bf2021-05-15 17:56:07242 const absl::optional<url::Origin>& origin,
Kunihiko Sakamoto346a74e2021-03-10 08:57:48243 SubresourceWebBundleNavigationInfo*
244 subresource_web_bundle_navigation_info) {
Nasko Oskov03912102019-01-11 00:21:32245 // If there is no origin supplied there is nothing to match. This can happen
246 // for navigations to a pending entry and therefore it should be allowed.
247 if (!origin)
248 return true;
249
Kunihiko Sakamoto346a74e2021-03-10 08:57:48250 if (url.SchemeIs(url::kUrnScheme) && subresource_web_bundle_navigation_info) {
251 // Urn: subframe from WebBundle has an opaque origin derived from the
252 // Bundle's origin.
253 return origin->CanBeDerivedFrom(
254 subresource_web_bundle_navigation_info->bundle_url());
255 }
256
Nasko Oskov03912102019-01-11 00:21:32257 return origin->CanBeDerivedFrom(url);
258}
259
Anton Bikineevf62d1bf2021-05-15 17:56:07260absl::optional<url::Origin> GetCommittedOriginForFrameEntry(
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12261 const mojom::DidCommitProvisionalLoadParams& params,
262 NavigationRequest* request) {
Nasko Oskov03912102019-01-11 00:21:32263 // Error pages commit in an opaque origin, yet have the real URL that resulted
264 // in an error as the |params.url|. Since successful reload of an error page
265 // should commit in the correct origin, setting the opaque origin on the
266 // FrameNavigationEntry will be incorrect.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12267 if (request->DidEncounterError())
Anton Bikineevf62d1bf2021-05-15 17:56:07268 return absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:32269
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12270 // We also currently don't save committed origins for loadDataWithBaseURL
271 // navigations (probably accidentally). Without this check, navigations to
272 // the FrameNavigationEntry might fail the DoesURLMatchOriginForNavigation()
273 // check since the origin will be based on the base URL instead of the data:
274 // URL used for the navigation.
275 // TODO(https://crbug.com/1198406): Save committed origin in
276 // FrameNavigationEntry for this case too.
Rakina Zata Amni8a1c6b32021-09-29 06:41:29277 if (request->IsLoadDataWithBaseURL())
Anton Bikineevf62d1bf2021-05-15 17:56:07278 return absl::nullopt;
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12279
Anton Bikineevf62d1bf2021-05-15 17:56:07280 return absl::make_optional(params.origin);
Nasko Oskov03912102019-01-11 00:21:32281}
282
Camille Lamy5193caa2018-10-12 11:59:42283bool IsValidURLForNavigation(bool is_main_frame,
284 const GURL& virtual_url,
285 const GURL& dest_url) {
286 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
287 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
288 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
289 << virtual_url.possibly_invalid_spec();
290 return false;
291 }
292
293 // Don't attempt to navigate to non-empty invalid URLs.
294 if (!dest_url.is_valid() && !dest_url.is_empty()) {
295 LOG(WARNING) << "Refusing to load invalid URL: "
296 << dest_url.possibly_invalid_spec();
297 return false;
298 }
299
300 // The renderer will reject IPC messages with URLs longer than
301 // this limit, so don't attempt to navigate with a longer URL.
302 if (dest_url.spec().size() > url::kMaxURLChars) {
303 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
304 << " characters.";
305 return false;
306 }
307
Aaron Colwell33109c592020-04-21 21:31:19308 // Reject renderer debug URLs because they should have been handled before
309 // we get to this point. This check handles renderer debug URLs
310 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
311 // provides defense-in-depth if a renderer debug URL manages to get here via
312 // some other path. We want to reject the navigation here so it doesn't
313 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30314 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19315 LOG(WARNING) << "Refusing to load renderer debug URL: "
316 << dest_url.possibly_invalid_spec();
317 return false;
318 }
319
Camille Lamy5193caa2018-10-12 11:59:42320 return true;
321}
322
Mikel Astizba9cf2fd2017-12-17 10:38:10323// See replaced_navigation_entry_data.h for details: this information is meant
324// to ensure |*output_entry| keeps track of its original URL (landing page in
325// case of server redirects) as it gets replaced (e.g. history.replaceState()),
326// without overwriting it later, for main frames.
327void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57328 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10329 NavigationEntryImpl* output_entry) {
330 if (output_entry->GetReplacedEntryData().has_value())
331 return;
332
333 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57334 data.first_committed_url = replaced_entry->GetURL();
335 data.first_timestamp = replaced_entry->GetTimestamp();
336 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29337 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10338}
339
Minggang Wangb9f3fa92021-07-01 15:30:31340blink::mojom::NavigationType GetNavigationType(
341 const GURL& old_url,
342 const GURL& new_url,
343 ReloadType reload_type,
344 NavigationEntryImpl* entry,
345 const FrameNavigationEntry& frame_entry,
346 bool has_pending_cross_document_commit,
347 bool is_currently_error_page,
348 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23349 // Reload navigations
350 switch (reload_type) {
351 case ReloadType::NORMAL:
Minggang Wangb9f3fa92021-07-01 15:30:31352 return blink::mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23353 case ReloadType::BYPASSING_CACHE:
Minggang Wangb9f3fa92021-07-01 15:30:31354 return blink::mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23355 case ReloadType::ORIGINAL_REQUEST_URL:
Minggang Wangb9f3fa92021-07-01 15:30:31356 return blink::mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23357 case ReloadType::NONE:
358 break; // Fall through to rest of function.
359 }
360
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08361 if (entry->IsRestored()) {
Minggang Wangb9f3fa92021-07-01 15:30:31362 return entry->GetHasPostData()
363 ? blink::mojom::NavigationType::RESTORE_WITH_POST
364 : blink::mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23365 }
366
danakjb952ef12021-01-14 19:58:49367 const bool can_be_same_document =
368 // A pending cross-document commit means this navigation will not occur in
369 // the current document, as that document would end up being replaced in
370 // the meantime.
371 !has_pending_cross_document_commit &&
372 // If the current document is an error page, we should always treat it as
373 // a different-document navigation so that we'll attempt to load the
374 // document we're navigating to (and not stay in the current error page).
375 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12376
clamyea99ea12018-05-28 13:54:23377 // History navigations.
378 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12379 return can_be_same_document && is_same_document_history_load
Minggang Wangb9f3fa92021-07-01 15:30:31380 ? blink::mojom::NavigationType::HISTORY_SAME_DOCUMENT
381 : blink::mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23382 }
383 DCHECK(!is_same_document_history_load);
384
385 // A same-document fragment-navigation happens when the only part of the url
386 // that is modified is after the '#' character.
387 //
388 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12389 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23390 //
391 // Note: this check is only valid for navigations that are not history
392 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
393 // history navigation from 'A#foo' to 'A#bar' is not a same-document
394 // navigation, but a different-document one. This is why history navigation
395 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47396 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
397 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12398
399 // The one case where we do the wrong thing here and incorrectly choose
400 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
401 // the renderer is a frameset. All frameset navigations should be
402 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
403 // navigation would do the right thing, as it would send it to the browser and
404 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
405 // into this method). But since we can't tell that case here for browser-
406 // initiated navigations, we have to get the renderer involved. In that case
407 // the navigation would be restarted due to the renderer spending a reply of
408 // mojom::CommitResult::RestartCrossDocument.
409
410 return can_be_same_document && is_same_doc
Minggang Wangb9f3fa92021-07-01 15:30:31411 ? blink::mojom::NavigationType::SAME_DOCUMENT
412 : blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23413}
414
Camille Lamy5193caa2018-10-12 11:59:42415// Adjusts the original input URL if needed, to get the URL to actually load and
416// the virtual URL, which may differ.
417void RewriteUrlForNavigation(const GURL& original_url,
418 BrowserContext* browser_context,
419 GURL* url_to_load,
420 GURL* virtual_url,
421 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42422 // Allow the browser URL handler to rewrite the URL. This will, for example,
423 // remove "view-source:" from the beginning of the URL to get the URL that
424 // will actually be loaded. This real URL won't be shown to the user, just
425 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31426 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42427 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
428 url_to_load, browser_context, reverse_on_redirect);
429}
430
431#if DCHECK_IS_ON()
432// Helper sanity check function used in debug mode.
433void ValidateRequestMatchesEntry(NavigationRequest* request,
434 NavigationEntryImpl* entry) {
435 if (request->frame_tree_node()->IsMainFrame()) {
436 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
437 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
Minggang Wangb9f3fa92021-07-01 15:30:31438 ui::PageTransitionFromInt(request->common_params().transition),
439 entry->GetTransitionType()));
Camille Lamy5193caa2018-10-12 11:59:42440 }
Nasko Oskovc36327d2019-01-03 23:23:04441 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42442 entry->should_clear_history_list());
443 DCHECK_EQ(request->common_params().has_user_gesture,
444 entry->has_user_gesture());
445 DCHECK_EQ(request->common_params().base_url_for_data_url,
446 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04447 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42448 entry->GetCanLoadLocalResources());
449 DCHECK_EQ(request->common_params().started_from_context_menu,
450 entry->has_started_from_context_menu());
451
452 FrameNavigationEntry* frame_entry =
453 entry->GetFrameEntry(request->frame_tree_node());
454 if (!frame_entry) {
455 NOTREACHED();
456 return;
457 }
458
Camille Lamy5193caa2018-10-12 11:59:42459 DCHECK_EQ(request->common_params().method, frame_entry->method());
460
Nasko Oskovc36327d2019-01-03 23:23:04461 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42462 if (redirect_size == frame_entry->redirect_chain().size()) {
463 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04464 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42465 frame_entry->redirect_chain()[i]);
466 }
467 } else {
468 NOTREACHED();
469 }
470}
471#endif // DCHECK_IS_ON()
472
Dave Tapuska8bfd84c2019-03-26 20:47:16473// Returns whether the session history NavigationRequests in |navigations|
474// would stay within the subtree of the sandboxed iframe in
475// |sandbox_frame_tree_node_id|.
476bool DoesSandboxNavigationStayWithinSubtree(
477 int sandbox_frame_tree_node_id,
478 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
479 for (auto& item : navigations) {
480 bool within_subtree = false;
481 // Check whether this NavigationRequest affects a frame within the
482 // sandboxed frame's subtree by walking up the tree looking for the
483 // sandboxed frame.
484 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03485 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16486 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
487 within_subtree = true;
488 break;
489 }
490 }
491 if (!within_subtree)
492 return false;
493 }
494 return true;
495}
496
Titouan Rigoudy6ec70402021-02-02 15:42:19497bool ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(
Antonio Sartori78a749f2020-11-30 12:03:39498 const NavigationRequest* request) {
499 // For local schemes we need to store the policy container in the
500 // FrameNavigationEntry, so that we can reload it in case of history
501 // navigation.
502 //
503 // TODO(https://crbug.com/1146361 and https://crbug.com/1146362): blob: and
504 // filesystem: should be removed from this list when we have properly
505 // implemented storing their policy container in the respective store.
506 return (request->common_params().url.SchemeIs(url::kAboutScheme) ||
507 request->common_params().url.SchemeIs(url::kDataScheme) ||
508 request->common_params().url.SchemeIsBlob() ||
509 request->common_params().url.SchemeIsFileSystem());
510}
511
[email protected]e9ba4472008-09-14 15:42:43512} // namespace
513
arthursonzogni66f711c2019-10-08 14:40:36514// NavigationControllerImpl::PendingEntryRef------------------------------------
515
516NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
517 base::WeakPtr<NavigationControllerImpl> controller)
518 : controller_(controller) {}
519
520NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
521 if (!controller_) // Can be null with interstitials.
522 return;
523
524 controller_->PendingEntryRefDeleted(this);
525}
526
[email protected]d202a7c2012-01-04 07:53:47527// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29528
[email protected]23a918b2014-07-15 09:51:36529const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23530
[email protected]765b35502008-08-21 00:51:20531// static
[email protected]d202a7c2012-01-04 07:53:47532size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23533 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20534
[email protected]e6fec472013-05-14 05:29:02535// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20536// when testing.
537static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29538
[email protected]71fde352011-12-29 03:29:56539// static
dcheng9bfa5162016-04-09 01:00:57540std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
541 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10542 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07543 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10544 ui::PageTransition transition,
545 bool is_renderer_initiated,
546 const std::string& extra_headers,
547 BrowserContext* browser_context,
548 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
549 return NavigationControllerImpl::CreateNavigationEntry(
550 url, referrer, std::move(initiator_origin),
551 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Hayato Ito303654c2021-06-30 09:07:54552 extra_headers, browser_context, std::move(blob_url_loader_factory));
Lukasz Anforowicz641234d52019-11-07 21:07:10553}
554
555// static
556std::unique_ptr<NavigationEntryImpl>
557NavigationControllerImpl::CreateNavigationEntry(
558 const GURL& url,
559 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07560 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10561 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57562 ui::PageTransition transition,
563 bool is_renderer_initiated,
564 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09565 BrowserContext* browser_context,
Hayato Ito303654c2021-06-30 09:07:54566 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
Camille Lamy5193caa2018-10-12 11:59:42567 GURL url_to_load;
568 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56569 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42570 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
571 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56572
Lukasz Anforowicz641234d52019-11-07 21:07:10573 // Let the NTP override the navigation params and pretend that this is a
574 // browser-initiated, bookmark-like navigation.
575 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violetcf6ea7e2021-06-09 21:09:21576 source_site_instance, &transition, &is_renderer_initiated, &referrer,
577 &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10578
Patrick Monettef507e982019-06-19 20:18:06579 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28580 nullptr, // The site instance for tabs is sent on navigation
581 // (WebContents::GetSiteInstance).
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58582 url_to_load, referrer, initiator_origin, std::u16string(), transition,
Camille Lamy5193caa2018-10-12 11:59:42583 is_renderer_initiated, blob_url_loader_factory);
584 entry->SetVirtualURL(virtual_url);
585 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56586 entry->set_update_virtual_url_with_url(reverse_on_redirect);
587 entry->set_extra_headers(extra_headers);
Patrick Monettef507e982019-06-19 20:18:06588 return entry;
[email protected]71fde352011-12-29 03:29:56589}
590
[email protected]cdcb1dee2012-01-04 00:46:20591// static
592void NavigationController::DisablePromptOnRepost() {
593 g_check_for_repost = false;
594}
595
[email protected]c5b88d82012-10-06 17:03:33596base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
597 base::Time t) {
598 // If |t| is between the water marks, we're in a run of duplicates
599 // or just getting out of it, so increase the high-water mark to get
600 // a time that probably hasn't been used before and return it.
601 if (low_water_mark_ <= t && t <= high_water_mark_) {
Peter Kastinge5a38ed2021-10-02 03:06:35602 high_water_mark_ += base::Microseconds(1);
[email protected]c5b88d82012-10-06 17:03:33603 return high_water_mark_;
604 }
605
606 // Otherwise, we're clear of the last duplicate run, so reset the
607 // water marks.
608 low_water_mark_ = high_water_mark_ = t;
609 return t;
610}
611
ckitagawa0faa5e42020-06-17 17:30:54612NavigationControllerImpl::ScopedShowRepostDialogForTesting::
613 ScopedShowRepostDialogForTesting()
614 : was_disallowed_(g_check_for_repost) {
615 g_check_for_repost = true;
616}
617
618NavigationControllerImpl::ScopedShowRepostDialogForTesting::
619 ~ScopedShowRepostDialogForTesting() {
620 g_check_for_repost = was_disallowed_;
621}
622
[email protected]d202a7c2012-01-04 07:53:47623NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00624 BrowserContext* browser_context,
625 FrameTree& frame_tree,
626 NavigationControllerDelegate* delegate)
627 : frame_tree_(frame_tree),
628 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57629 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22630 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47631 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37632 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29633}
634
[email protected]d202a7c2012-01-04 07:53:47635NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00636 // The NavigationControllerImpl might be called inside its delegate
637 // destructor. Calling it is not valid anymore.
638 delegate_ = nullptr;
639 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29640}
641
Matt Falkenhagen548ed1562021-07-06 01:38:26642WebContents* NavigationControllerImpl::DeprecatedGetWebContents() {
643 return delegate_->DeprecatedGetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32644}
645
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57646BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55647 return browser_context_;
648}
649
[email protected]d202a7c2012-01-04 07:53:47650void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30651 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36652 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57653 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57654 // Verify that this controller is unused and that the input is valid.
Lei Zhang96031532019-10-10 19:05:47655 DCHECK_EQ(0, GetEntryCount());
656 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57657 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14658 selected_navigation < static_cast<int>(entries->size()));
arthursonzogni5c4c202d2017-04-25 23:41:27659 DCHECK_EQ(-1, pending_entry_index_);
[email protected]ce3fa3c2009-04-20 19:55:57660
[email protected]ce3fa3c2009-04-20 19:55:57661 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44662 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03663 entries_.reserve(entries->size());
664 for (auto& entry : *entries)
dcheng36b6aec92015-12-26 06:16:36665 entries_.push_back(
666 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
avif16f85a72015-11-13 18:25:03667
668 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
669 // cleared out safely.
670 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57671
672 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36673 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57674}
675
toyoshim6142d96f2016-12-19 09:07:25676void NavigationControllerImpl::Reload(ReloadType reload_type,
677 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28678 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28679 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32680 int current_index = -1;
681
Carlos IL42b416592019-10-07 23:10:36682 if (entry_replaced_by_post_commit_error_) {
683 // If there is an entry that was replaced by a currently active post-commit
684 // error navigation, this can't be the initial navigation.
685 DCHECK(!IsInitialNavigation());
686 // If the current entry is a post commit error, we reload the entry it
687 // replaced instead. We leave the error entry in place until a commit
688 // replaces it, but the pending entry points to the original entry in the
689 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
690 // case that pending_entry_ != entries_[pending_entry_index_].
691 entry = entry_replaced_by_post_commit_error_.get();
692 current_index = GetCurrentEntryIndex();
693 } else if (IsInitialNavigation() && pending_entry_) {
694 // If we are reloading the initial navigation, just use the current
695 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32696 entry = pending_entry_;
697 // The pending entry might be in entries_ (e.g., after a Clone), so we
698 // should also update the current_index.
699 current_index = pending_entry_index_;
700 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00701 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32702 current_index = GetCurrentEntryIndex();
703 if (current_index != -1) {
creis3da03872015-02-20 21:12:32704 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32705 }
[email protected]979a4bc2013-04-24 01:27:15706 }
[email protected]241db352013-04-22 18:04:05707
[email protected]59167c22013-06-03 18:07:32708 // If we are no where, then we can't reload. TODO(darin): We should add a
709 // CanReload method.
710 if (!entry)
711 return;
712
Takashi Toyoshimac7df3c22019-06-25 14:18:47713 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26714 entry->set_reload_type(reload_type);
715
Aran Gilman37d11632019-10-08 23:07:15716 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30717 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07718 // they really want to do this. If they do, the dialog will call us back
719 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57720 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02721
[email protected]106a0812010-03-18 00:15:12722 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57723 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47724 return;
initial.commit09911bf2008-07-26 23:55:29725 }
Lei Zhang96031532019-10-10 19:05:47726
727 if (!IsInitialNavigation())
728 DiscardNonCommittedEntries();
729
730 pending_entry_ = entry;
731 pending_entry_index_ = current_index;
732 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
733
Nate Chapin45f620582021-09-30 17:45:43734 // location.reload() goes through BeginNavigation, so all reloads triggered
735 // via this codepath are browser initiated.
Lei Zhang96031532019-10-10 19:05:47736 NavigateToExistingPendingEntry(reload_type,
Nate Chapin45f620582021-09-30 17:45:43737 FrameTreeNode::kFrameTreeNodeInvalidId,
738 true /* is_browser_initiated */);
initial.commit09911bf2008-07-26 23:55:29739}
740
[email protected]d202a7c2012-01-04 07:53:47741void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48742 DCHECK(pending_reload_ != ReloadType::NONE);
743 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12744}
745
[email protected]d202a7c2012-01-04 07:53:47746void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48747 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12748 NOTREACHED();
749 } else {
toyoshim6142d96f2016-12-19 09:07:25750 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48751 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12752 }
753}
754
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57755bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09756 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11757}
758
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57759bool NavigationControllerImpl::IsInitialBlankNavigation() {
creis10a4ab72015-10-13 17:22:40760 // TODO(creis): Once we create a NavigationEntry for the initial blank page,
toyoshim0df1d3a2016-09-09 09:52:48761 // we'll need to check for entry count 1 and restore_type NONE (to exclude
762 // the cloned tab case).
creis10a4ab72015-10-13 17:22:40763 return IsInitialNavigation() && GetEntryCount() == 0;
764}
765
Aran Gilman37d11632019-10-08 23:07:15766NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
767 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58768 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03769 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58770}
771
Adithya Srinivasan9b0c99c2021-08-10 15:19:45772NavigationEntryImpl*
773NavigationControllerImpl::GetEntryWithUniqueIDIncludingPending(
774 int nav_entry_id) const {
775 NavigationEntryImpl* entry = GetEntryWithUniqueID(nav_entry_id);
776 if (entry)
777 return entry;
778 return pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id
779 ? pending_entry_
780 : nullptr;
781}
782
W. James MacLean1c40862c2020-04-27 21:05:57783void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
784 const url::Origin& origin) {
785 for (int i = 0; i < GetEntryCount(); i++) {
786 auto* entry = GetEntryAtIndex(i);
787 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
788 }
789 if (entry_replaced_by_post_commit_error_) {
790 // It's possible we could come back to this entry if the error
791 // page/interstitial goes away.
792 entry_replaced_by_post_commit_error_
793 ->RegisterExistingOriginToPreventOptInIsolation(origin);
794 }
795 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
796 // visiting pending_entry_, which lacks a committed origin. This will be done
797 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
798}
799
avi25764702015-06-23 15:43:37800void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57801 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00802 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37803 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27804 DCHECK_EQ(-1, pending_entry_index_);
[email protected]765b35502008-08-21 00:51:20805}
806
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57807NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47808 if (pending_entry_)
809 return pending_entry_;
810 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20811}
812
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57813NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32814 // The pending entry is safe to return for new (non-history), browser-
815 // initiated navigations. Most renderer-initiated navigations should not
816 // show the pending entry, to prevent URL spoof attacks.
817 //
818 // We make an exception for renderer-initiated navigations in new tabs, as
819 // long as no other page has tried to access the initial empty document in
820 // the new tab. If another page modifies this blank page, a URL spoof is
821 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32822 bool safe_to_show_pending =
823 pending_entry_ &&
824 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09825 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32826 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46827 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32828
829 // Also allow showing the pending entry for history navigations in a new tab,
830 // such as Ctrl+Back. In this case, no existing page is visible and no one
831 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15832 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
833 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32834 safe_to_show_pending = true;
835
836 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19837 return pending_entry_;
838 return GetLastCommittedEntry();
839}
840
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57841int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20842 if (pending_entry_index_ != -1)
843 return pending_entry_index_;
844 return last_committed_entry_index_;
845}
846
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57847NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20848 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28849 return nullptr;
avif16f85a72015-11-13 18:25:03850 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20851}
852
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57853bool NavigationControllerImpl::CanViewSource() {
Jeremy Roman2d8dfe132021-07-06 20:51:26854 const std::string& mime_type =
855 frame_tree_.root()->current_frame_host()->GetPage().contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53856 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
857 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27858 NavigationEntry* visible_entry = GetVisibleEntry();
859 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39860 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16861}
862
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57863int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27864 // The last committed entry index must always be less than the number of
Carlos IL4dea8902020-05-26 15:14:29865 // entries. If there are no entries, it must be -1.
arthursonzogni5c4c202d2017-04-25 23:41:27866 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
867 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55868 return last_committed_entry_index_;
869}
870
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57871int NavigationControllerImpl::GetEntryCount() {
Carlos IL4dea8902020-05-26 15:14:29872 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55873 return static_cast<int>(entries_.size());
874}
875
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57876NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37877 if (index < 0 || index >= GetEntryCount())
878 return nullptr;
879
avif16f85a72015-11-13 18:25:03880 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25881}
882
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57883NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47884 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20885}
886
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57887int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46888 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03889}
890
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57891bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03892 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
893 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
894 return true;
895 }
896 return false;
[email protected]765b35502008-08-21 00:51:20897}
898
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57899bool NavigationControllerImpl::CanGoForward() {
WangHui74286d52021-03-31 16:17:15900 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
901 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
902 return true;
903 }
904 return false;
[email protected]765b35502008-08-21 00:51:20905}
906
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57907bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03908 int index = GetIndexForOffset(offset);
909 return index >= 0 && index < GetEntryCount();
910}
911
WangHui74286d52021-03-31 16:17:15912#if defined(OS_ANDROID)
913bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
WangHui74286d52021-03-31 16:17:15914 if (offset == 0)
915 return true;
916 int increment = offset > 0 ? 1 : -1;
917 int non_skippable_entries = 0;
918 for (int index = GetIndexForOffset(increment);
919 index >= 0 && index < GetEntryCount(); index += increment) {
920 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
921 non_skippable_entries++;
922
923 if (non_skippable_entries == std::abs(offset))
924 return true;
925 }
926 return false;
927}
928#endif
929
[email protected]d202a7c2012-01-04 07:53:47930void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06931 int target_index = GetIndexForOffset(-1);
932
Elly Fong-Jones40ee8112021-06-23 19:10:52933 // Move the target index past the skippable entries.
Shivani Sharma298d12852019-01-22 20:04:03934 bool all_skippable_entries = true;
Elly Fong-Jones40ee8112021-06-23 19:10:52935 while (target_index >= 0) {
936 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui()) {
Shivani Sharma298d12852019-01-22 20:04:03937 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06938 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16939 }
Elly Fong-Jones40ee8112021-06-23 19:10:52940 target_index--;
shivanisha55201872018-12-13 04:29:06941 }
Miyoung Shin1c565c912021-03-17 12:11:21942
Shivani Sharma298d12852019-01-22 20:04:03943 // Do nothing if all entries are skippable. Normally this path would not
944 // happen as consumers would have already checked it in CanGoBack but a lot of
945 // tests do not do that.
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42946 if (all_skippable_entries)
Shivani Sharma298d12852019-01-22 20:04:03947 return;
shivanisha55201872018-12-13 04:29:06948
shivanisha55201872018-12-13 04:29:06949 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20950}
951
[email protected]d202a7c2012-01-04 07:53:47952void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06953 int target_index = GetIndexForOffset(1);
954
Shivani Sharma2d5b4b6b2019-01-08 16:07:16955 // Note that at least one entry (the last one) will be non-skippable since
956 // entries are marked skippable only when they add another entry because of
957 // redirect or pushState.
Elly Fong-Jones40ee8112021-06-23 19:10:52958 while (target_index < static_cast<int>(entries_.size())) {
959 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui())
shivanisha55201872018-12-13 04:29:06960 break;
Elly Fong-Jones40ee8112021-06-23 19:10:52961 target_index++;
shivanisha55201872018-12-13 04:29:06962 }
shivanisha55201872018-12-13 04:29:06963 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20964}
965
[email protected]d202a7c2012-01-04 07:53:47966void NavigationControllerImpl::GoToIndex(int index) {
Nate Chapin45f620582021-09-30 17:45:43967 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId,
968 true /* is_browser_initiated */);
Dave Tapuska8bfd84c2019-03-26 20:47:16969}
970
971void NavigationControllerImpl::GoToIndex(int index,
Nate Chapin45f620582021-09-30 17:45:43972 int sandbox_frame_tree_node_id,
973 bool is_browser_initiated) {
974 DCHECK(sandbox_frame_tree_node_id == FrameTreeNode::kFrameTreeNodeInvalidId ||
975 !is_browser_initiated);
sunjian30574a62017-03-21 21:39:44976 TRACE_EVENT0("browser,navigation,benchmark",
977 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:20978 if (index < 0 || index >= static_cast<int>(entries_.size())) {
979 NOTREACHED();
980 return;
981 }
982
[email protected]cbab76d2008-10-13 22:42:47983 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20984
arthursonzogni5c4c202d2017-04-25 23:41:27985 DCHECK_EQ(nullptr, pending_entry_);
986 DCHECK_EQ(-1, pending_entry_index_);
987 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:20988 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:27989 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
990 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Nate Chapin45f620582021-09-30 17:45:43991 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id,
992 is_browser_initiated);
[email protected]765b35502008-08-21 00:51:20993}
994
[email protected]d202a7c2012-01-04 07:53:47995void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:12996 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:03997 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:20998 return;
999
[email protected]9ba14052012-06-22 23:50:031000 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:201001}
1002
Nate Chapin45f620582021-09-30 17:45:431003void NavigationControllerImpl::GoToOffsetFromRenderer(int offset) {
1004 // Note: This is actually reached in unit tests.
1005 if (!CanGoToOffset(offset))
1006 return;
1007
1008 GoToIndex(GetIndexForOffset(offset), FrameTreeNode::kFrameTreeNodeInvalidId,
1009 false /* is_browser_initiated */);
1010}
1011
WangHui74286d52021-03-31 16:17:151012#if defined(OS_ANDROID)
1013void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
1014 // Note: This is actually reached in unit tests.
1015 if (!CanGoToOffsetWithSkipping(offset))
1016 return;
1017
Elly Fong-Jonesccc6d1f2021-06-14 18:32:421018 if (offset == 0) {
WangHui74286d52021-03-31 16:17:151019 GoToIndex(GetIndexForOffset(offset));
1020 return;
1021 }
1022 int increment = offset > 0 ? 1 : -1;
1023 // Find the offset without counting skippable entries.
1024 int target_index = GetIndexForOffset(increment);
1025 int non_skippable_entries = 0;
1026 for (int index = target_index; index >= 0 && index < GetEntryCount();
1027 index += increment) {
1028 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1029 non_skippable_entries++;
1030
1031 if (non_skippable_entries == std::abs(offset)) {
1032 target_index = index;
1033 break;
1034 }
1035 }
1036
1037 GoToIndex(target_index);
1038}
1039#endif
1040
[email protected]41374f12013-07-24 02:49:281041bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151042 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281043 return false;
[email protected]6a13a6c2011-12-20 21:47:121044
[email protected]43032342011-03-21 14:10:311045 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281046 return true;
[email protected]cbab76d2008-10-13 22:42:471047}
1048
Michael Thiessen9b14d512019-09-23 21:19:471049void NavigationControllerImpl::PruneForwardEntries() {
1050 DiscardNonCommittedEntries();
1051 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471052 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471053 if (num_removed <= 0)
1054 return;
1055 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1056 NotifyPrunedEntries(this, remove_start_index /* start index */,
1057 num_removed /* count */);
1058}
1059
Aran Gilman37d11632019-10-08 23:07:151060void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1061 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321062 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311063 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511064 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1065 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321066 }
1067}
1068
Harkiran Bolariaba823e42021-05-21 18:30:361069base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL(
1070 const GURL& url,
1071 const Referrer& referrer,
1072 ui::PageTransition transition,
1073 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471074 LoadURLParams params(url);
1075 params.referrer = referrer;
1076 params.transition_type = transition;
1077 params.extra_headers = extra_headers;
Harkiran Bolariaba823e42021-05-21 18:30:361078 return LoadURLWithParams(params);
[email protected]cf002332012-08-14 19:17:471079}
1080
Harkiran Bolariaba823e42021-05-21 18:30:361081base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams(
1082 const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061083 if (params.is_renderer_initiated)
1084 DCHECK(params.initiator_origin.has_value());
1085
naskob8744d22014-08-28 17:07:431086 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151087 "NavigationControllerImpl::LoadURLWithParams", "url",
1088 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291089 bool is_explicit_navigation =
1090 GetContentClient()->browser()->IsExplicitNavigation(
1091 params.transition_type);
1092 if (HandleDebugURL(params.url, params.transition_type,
1093 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431094 // If Telemetry is running, allow the URL load to proceed as if it's
1095 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491096 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431097 cc::switches::kEnableGpuBenchmarking))
Harkiran Bolariaba823e42021-05-21 18:30:361098 return nullptr;
[email protected]47752982014-07-29 08:01:431099 }
[email protected]8bf1048012012-02-08 01:22:181100
[email protected]cf002332012-08-14 19:17:471101 // Checks based on params.load_type.
1102 switch (params.load_type) {
1103 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431104 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471105 break;
1106 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261107 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471108 NOTREACHED() << "Data load must use data scheme.";
Harkiran Bolariaba823e42021-05-21 18:30:361109 return nullptr;
[email protected]cf002332012-08-14 19:17:471110 }
1111 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461112 }
[email protected]e47ae9472011-10-13 19:48:341113
[email protected]e47ae9472011-10-13 19:48:341114 // The user initiated a load, we don't need to reload anymore.
1115 needs_reload_ = false;
1116
Harkiran Bolariaba823e42021-05-21 18:30:361117 return NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441118}
1119
Mohamed Abdelhalim833de902019-09-16 17:41:451120bool NavigationControllerImpl::PendingEntryMatchesRequest(
1121 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191122 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451123 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191124}
1125
[email protected]d202a7c2012-01-04 07:53:471126bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321127 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071128 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331129 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441130 bool is_same_document_navigation,
Nate Chapinc7019dd7d2021-06-25 18:29:251131 bool was_on_initial_empty_document,
Shivani Sharmaffb32b82019-04-09 16:58:471132 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131133 NavigationRequest* navigation_request) {
1134 DCHECK(navigation_request);
Takashi Toyoshimaa9f66802021-08-18 02:40:171135 if (ShouldMaintainTrivialSessionHistory() && GetLastCommittedEntry()) {
1136 // Ensure that this navigation does not add a navigation entry, since
1137 // ShouldMaintainTrivialSessionHistory() means we should not add an entry
1138 // beyond the last committed one. Therefore, `should_replace_current_entry`
1139 // should be set, which replaces the current entry, or this should be a
1140 // reload, which does not create a new entry.
1141 DCHECK(params.should_replace_current_entry ||
1142 navigation_request->GetReloadType() != ReloadType::NONE);
1143 }
[email protected]cd2e15742013-03-08 04:08:311144 is_initial_navigation_ = false;
1145
[email protected]0e8db942008-09-24 21:21:481146 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431147 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481148 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361149 if (entry_replaced_by_post_commit_error_) {
1150 if (is_same_document_navigation) {
1151 // Same document navigations should not be possible on error pages and
1152 // would leave the controller in a weird state. Kill the renderer if
1153 // that happens.
1154 bad_message::ReceivedBadMessage(
1155 rfh->GetProcess(), bad_message::NC_SAME_DOCUMENT_POST_COMMIT_ERROR);
1156 }
1157 // Any commit while a post-commit error page is showing should put the
1158 // original entry back, replacing the error page's entry. This includes
1159 // reloads, where the original entry was used as the pending entry and
1160 // should now be at the correct index at commit time.
1161 entries_[last_committed_entry_index_] =
1162 std::move(entry_replaced_by_post_commit_error_);
1163 }
Fergal Daly8e33cf62020-12-12 01:06:071164 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551165 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431166 if (pending_entry_ &&
1167 pending_entry_->GetIsOverridingUserAgent() !=
1168 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1169 overriding_user_agent_changed = true;
[email protected]0e8db942008-09-24 21:21:481170 } else {
Gang Wu325f03f42021-02-25 20:00:461171 // GetLastCommittedEntry() is null, so this is the first entry.
Fergal Daly8e33cf62020-12-12 01:06:071172 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481173 details->previous_entry_index = -1;
Gang Wu325f03f42021-02-25 20:00:461174 if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) {
1175 // Default setting is NOT override the user agent, so overriding the user
1176 // agent in first entry should be considered as user agent changed as
1177 // well.
1178 overriding_user_agent_changed = true;
1179 }
[email protected]0e8db942008-09-24 21:21:481180 }
[email protected]ecd9d8702008-08-28 22:10:171181
Dave Tapuskaa2ab4f252021-07-08 21:31:281182 bool is_main_frame_navigation = !rfh->GetParent();
1183
Alexander Timind2f2e4f22019-04-02 20:04:531184 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1185 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281186 // For primary frame tree navigations, choose an appropriate
1187 // BackForwardCacheMetrics to be associated with the NavigationEntry, by
1188 // either creating a new object or reusing the previous one depending on
1189 // whether it's a main frame navigation or not.
1190 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics;
1191 if (navigation_request->frame_tree_node()->frame_tree()->type() ==
1192 FrameTree::Type::kPrimary) {
1193 back_forward_cache_metrics =
1194 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1195 GetLastCommittedEntry(), is_main_frame_navigation,
1196 params.document_sequence_number);
1197 }
Alexander Timind2f2e4f22019-04-02 20:04:531198 // Notify the last active entry that we have navigated away.
Dave Tapuskaa2ab4f252021-07-08 21:31:281199 if (is_main_frame_navigation && !is_same_document_navigation) {
Alexander Timind2f2e4f22019-04-02 20:04:531200 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1201 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Hajime Hoshic7606502021-04-14 02:42:161202 metrics->MainFrameDidNavigateAwayFromDocument(rfh, navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531203 }
1204 }
1205 }
1206
fdegans9caf66a2015-07-30 21:10:421207 // If there is a pending entry at this point, it should have a SiteInstance,
1208 // except for restored entries.
jam48cea9082017-02-15 06:13:291209 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481210 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081211 pending_entry_->IsRestored());
1212 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151213 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291214 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481215 }
[email protected]e9ba4472008-09-14 15:42:431216
Nasko Oskovaee2f862018-06-15 00:05:521217 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1218 // has changed, this must be treated as a new navigation with replacement.
1219 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491220 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521221 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101222 pending_entry_->GetUniqueID() ==
1223 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521224 pending_entry_->site_instance() &&
1225 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1226 DCHECK_NE(-1, pending_entry_index_);
1227 // TODO(nasko,creis): Instead of setting this value here, set
1228 // should_replace_current_entry on the parameters we send to the
1229 // renderer process as part of CommitNavigation. The renderer should
1230 // in turn send it back here as part of |params| and it can be just
1231 // enforced and renderer process terminated on mismatch.
1232 details->did_replace_entry = true;
1233 } else {
1234 // The renderer tells us whether the navigation replaces the current entry.
1235 details->did_replace_entry = params.should_replace_current_entry;
1236 }
[email protected]bcd904482012-02-01 01:54:221237
[email protected]e9ba4472008-09-14 15:42:431238 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amnif6950d552020-11-24 03:26:101239 details->type = ClassifyNavigation(rfh, params, navigation_request);
[email protected]4bf3522c2010-08-19 21:00:201240
eugenebutee08663a2017-04-27 17:43:121241 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441242 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121243
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071244 details->is_prerender_activation =
1245 navigation_request->IsPrerenderedPageActivation();
1246
Peter Boströmd7592132019-01-30 04:50:311247 // Make sure we do not discard the pending entry for a different ongoing
1248 // navigation when a same document commit comes in unexpectedly from the
1249 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1250 // other navigation types. See https://crbug.com/900036.
1251 // TODO(crbug.com/926009): Handle history.pushState() as well.
1252 bool keep_pending_entry = is_same_document_navigation &&
Charlie Reisc0f17d2d2021-01-12 18:52:491253 details->type == NAVIGATION_TYPE_EXISTING_ENTRY &&
Peter Boströmd7592132019-01-30 04:50:311254 pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451255 !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311256
[email protected]0e8db942008-09-24 21:21:481257 switch (details->type) {
Charlie Reisc0f17d2d2021-01-12 18:52:491258 case NAVIGATION_TYPE_NEW_ENTRY:
1259 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051260 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451261 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431262 break;
Charlie Reisc0f17d2d2021-01-12 18:52:491263 case NAVIGATION_TYPE_EXISTING_ENTRY:
1264 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1265 was_restored, navigation_request,
1266 keep_pending_entry);
[email protected]e9ba4472008-09-14 15:42:431267 break;
[email protected]8ff00d72012-10-23 19:12:211268 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471269 RendererDidNavigateNewSubframe(
1270 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451271 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431272 break;
[email protected]8ff00d72012-10-23 19:12:211273 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391274 if (!RendererDidNavigateAutoSubframe(
Nate Chapinc7019dd7d2021-06-25 18:29:251275 rfh, params, details->is_same_document,
1276 was_on_initial_empty_document, navigation_request)) {
creisce0ef3572017-01-26 17:53:081277 // We don't send a notification about auto-subframe PageState during
1278 // UpdateStateForFrame, since it looks like nothing has changed. Send
1279 // it here at commit time instead.
1280 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431281 return false;
creis59d5a47cb2016-08-24 23:57:191282 }
[email protected]e9ba4472008-09-14 15:42:431283 break;
[email protected]8ff00d72012-10-23 19:12:211284 case NAVIGATION_TYPE_NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:491285 // If a pending navigation was in progress, this canceled it. We should
1286 // discard it and make sure it is removed from the URL bar. After that,
1287 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:431288 // the caller that nothing has happened.
arthursonzogni69a6a1b2019-09-17 09:23:001289 if (pending_entry_)
[email protected]20d1c992011-04-12 21:17:491290 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431291 return false;
Aran Gilman37d11632019-10-08 23:07:151292 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431293 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151294 break;
[email protected]765b35502008-08-21 00:51:201295 }
1296
[email protected]688aa65c62012-09-28 04:32:221297 // At this point, we know that the navigation has just completed, so
1298 // record the time.
1299 //
1300 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261301 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331302 base::Time timestamp =
1303 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1304 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131305 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221306
Peter Boströmd7592132019-01-30 04:50:311307 // If we aren't keeping the pending entry, there shouldn't be one at this
1308 // point. Clear it again in case any error cases above forgot to do so.
1309 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1310 // been cleared instead of protecting against it.
1311 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001312 DiscardNonCommittedEntries();
[email protected]f233e4232013-02-23 00:55:141313
[email protected]e9ba4472008-09-14 15:42:431314 // All committed entries should have nonempty content state so WebKit doesn't
1315 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471316 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321317 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221318 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441319 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531320 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1321 // implementing back-forward cache.
Dave Tapuskaa2ab4f252021-07-08 21:31:281322 if (back_forward_cache_metrics &&
1323 !active_entry->back_forward_cache_metrics()) {
Alexander Timind2f2e4f22019-04-02 20:04:531324 active_entry->set_back_forward_cache_metrics(
1325 std::move(back_forward_cache_metrics));
1326 }
Dave Tapuskaa2ab4f252021-07-08 21:31:281327
1328 // `back_forward_cache_metrics()` may return null as we do not record
1329 // back-forward cache metrics for navigations in non-primary frame trees.
1330 if (active_entry->back_forward_cache_metrics()) {
1331 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
1332 navigation_request,
1333 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
1334 }
naskoc7533512016-05-06 17:01:121335
Charles Reisc0507202017-09-21 00:40:021336 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1337 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1338 // A mismatch can occur if the renderer lies or due to a unique name collision
1339 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121340 FrameNavigationEntry* frame_entry =
1341 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021342 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1343 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031344 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081345 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1346 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031347 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201348
1349 // Remember the bindings the renderer process has at this point, so that
1350 // we do not grant this entry additional bindings if we come back to it.
1351 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301352 }
[email protected]132e281a2012-07-31 18:32:441353
[email protected]97d8f0d2013-10-29 16:49:211354 // Once it is committed, we no longer need to track several pieces of state on
1355 // the entry.
naskoc7533512016-05-06 17:01:121356 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131357
[email protected]49bd30e62011-03-22 20:12:591358 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221359 // TODO(creis): This check won't pass for subframes until we create entries
1360 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001361 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421362 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591363
[email protected]e9ba4472008-09-14 15:42:431364 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001365 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001366 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311367 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531368
arthursonzogni7ddc6542021-04-09 09:16:501369 active_entry->SetIsOverridingUserAgent(
1370 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031371
[email protected]93f230e02011-06-01 14:40:001372 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291373
John Abd-El-Malek380d3c5922017-09-08 00:20:311374 if (active_entry->GetURL().SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121375 !navigation_request->DidEncounterError()) {
John Abd-El-Malek969cdd94e2017-07-10 22:18:161376 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus",
1377 !!active_entry->GetSSL().certificate);
1378 }
1379
aelias100c9192017-01-13 00:01:431380 if (overriding_user_agent_changed)
1381 delegate_->UpdateOverridingUserAgent();
1382
creis03b48002015-11-04 00:54:561383 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1384 // one knows the latest NavigationEntry it is showing (whether it has
1385 // committed anything in this navigation or not). This allows things like
1386 // state and title updates from RenderFrames to apply to the latest relevant
1387 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381388 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001389 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381390 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
Hayato Ito2c8c08d02021-06-23 03:38:431391
1392 if (navigation_request->IsPrerenderedPageActivation()) {
1393 BroadcastHistoryOffsetAndLength();
1394 // TODO(crbug.com/1222893): Broadcasting happens after the prerendered page
1395 // is activated. As a result, a "prerenderingchange" event listener sees the
1396 // history.length which is not updated yet. We should guarantee that
1397 // history's length and offset should be updated before a
1398 // "prerenderingchange" event listener runs. One possible approach is to use
1399 // the same IPC which "prerenderingchange" uses, and propagate history's
1400 // length and offset together with that.
1401 }
1402
[email protected]e9ba4472008-09-14 15:42:431403 return true;
initial.commit09911bf2008-07-26 23:55:291404}
1405
[email protected]8ff00d72012-10-23 19:12:211406NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321407 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101408 const mojom::DidCommitProvisionalLoadParams& params,
1409 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591410 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511411 "ClassifyNavigation");
1412
avi7c6f35e2015-05-08 17:52:381413 if (params.did_create_new_entry) {
Charlie Reisc0f17d2d2021-01-12 18:52:491414 // A new entry. We may or may not have a corresponding pending entry, and
avi7c6f35e2015-05-08 17:52:381415 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001416 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491417 trace_return.set_return_reason("new entry, no parent, new entry");
1418 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381419 }
1420
1421 // When this is a new subframe navigation, we should have a committed page
1422 // in which it's a subframe. This may not be the case when an iframe is
1423 // navigated on a popup navigated to about:blank (the iframe would be
1424 // written into the popup by script on the main page). For these cases,
1425 // there isn't any navigation stuff we can do, so just ignore it.
Nasko Oskovae49e292020-08-13 02:08:511426 if (!GetLastCommittedEntry()) {
1427 trace_return.set_return_reason("new entry, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381428 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511429 }
avi7c6f35e2015-05-08 17:52:381430
1431 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511432 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381433 return NAVIGATION_TYPE_NEW_SUBFRAME;
1434 }
1435
Charlie Reisc0f17d2d2021-01-12 18:52:491436 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381437 DCHECK(!params.history_list_was_cleared);
1438
avi39c1edd32015-06-04 20:06:001439 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381440 // All manual subframes would be did_create_new_entry and handled above, so
1441 // we know this is auto.
Nasko Oskovae49e292020-08-13 02:08:511442 if (GetLastCommittedEntry()) {
1443 trace_return.set_return_reason("subframe, last commmited, auto subframe");
avi7c6f35e2015-05-08 17:52:381444 return NAVIGATION_TYPE_AUTO_SUBFRAME;
Nasko Oskovae49e292020-08-13 02:08:511445 }
Lei Zhang96031532019-10-10 19:05:471446
1447 // We ignore subframes created in non-committed pages; we'd appreciate if
1448 // people stopped doing that.
Nasko Oskovae49e292020-08-13 02:08:511449 trace_return.set_return_reason("subframe, no last commmited, ignore");
Lei Zhang96031532019-10-10 19:05:471450 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381451 }
1452
Rakina Zata Amnif6950d552020-11-24 03:26:101453 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1454 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381455 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1456 // create a new page.
1457
1458 // Just like above in the did_create_new_entry case, it's possible to
1459 // scribble onto an uncommitted page. Again, there isn't any navigation
1460 // stuff that we can do, so ignore it here as well.
avi3a5b8f32015-05-28 17:50:231461 NavigationEntry* last_committed = GetLastCommittedEntry();
Nasko Oskovae49e292020-08-13 02:08:511462 if (!last_committed) {
1463 trace_return.set_return_reason("nav entry 0, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381464 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511465 }
avi7c6f35e2015-05-08 17:52:381466
Hayato Ito2ae49442021-07-02 02:59:251467 // This main frame navigation is not a history navigation (since
1468 // nav_entry_id is 0), but didn't create a new entry. So this must be a
1469 // reload or a replacement navigation, which will modify the existing entry.
1470 //
Nasko Oskov332593c2018-08-16 17:21:341471 // TODO(nasko): With error page isolation, reloading an existing session
1472 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491473 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341474 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511475 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491476 "nav entry 0, last committed, existing entry");
1477 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381478 }
1479
Rakina Zata Amnif6950d552020-11-24 03:26:101480 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521481 // If the SiteInstance of the |pending_entry_| does not match the
1482 // SiteInstance that got committed, treat this as a new navigation with
1483 // replacement. This can happen if back/forward/reload encounters a server
1484 // redirect to a different site or an isolated error page gets successfully
1485 // reloaded into a different SiteInstance.
1486 if (pending_entry_->site_instance() &&
1487 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491488 trace_return.set_return_reason("pending matching nav entry, new entry");
1489 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521490 }
creis77c9aa32015-09-25 19:59:421491
Nasko Oskovaee2f862018-06-15 00:05:521492 if (pending_entry_index_ == -1) {
1493 // In this case, we have a pending entry for a load of a new URL but Blink
1494 // didn't do a new navigation (params.did_create_new_entry). First check
1495 // to make sure Blink didn't treat a new cross-process navigation as
1496 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161497 // we must treat it as NEW rather than the converted reload case below,
1498 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521499 if (!GetLastCommittedEntry() ||
1500 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161501 trace_return.set_return_reason("new pending, new entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491502 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521503 }
1504
1505 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161506 // We will create a pending entry, but NavigateWithoutEntry will convert
1507 // it to a reload since it's the same page and not create a new entry for
1508 // it. (The user doesn't want to have a new back/forward entry when they
1509 // do this.) Therefore we want to just ignore the pending entry and go
1510 // back to where we were (the "existing entry").
1511 trace_return.set_return_reason("new pending, existing (same) entry");
1512 return NAVIGATION_TYPE_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521513 }
avi7c6f35e2015-05-08 17:52:381514 }
1515
creis26d22632017-04-21 20:23:561516 // Everything below here is assumed to be an existing entry, but if there is
1517 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511518 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491519 trace_return.set_return_reason("no last committed, new entry");
1520 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511521 }
creis26d22632017-04-21 20:23:561522
Rakina Zata Amni153d5702021-09-13 22:48:001523 if (navigation_request->commit_params().intended_as_new_entry) {
avi7c6f35e2015-05-08 17:52:381524 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491525 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1526 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161527 trace_return.set_return_reason("intended as new entry, existing entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491528 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381529 }
1530
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121531 if (navigation_request->DidEncounterError() &&
1532 failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101533 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381534 // If the renderer was going to a new pending entry that got cleared because
1535 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491536 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381537 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511538 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491539 "unreachable, matching pending, existing entry");
1540 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381541 }
1542
Charlie Reisc0f17d2d2021-01-12 18:52:491543 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101544 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511545 trace_return.traced_value()->SetInteger("existing_entry_index",
1546 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381547 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441548 // The renderer has committed a navigation to an entry that no longer
1549 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491550 trace_return.set_return_reason("existing entry -1, new entry");
1551 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381552 }
1553
avi7c6f35e2015-05-08 17:52:381554 // Since we weeded out "new" navigations above, we know this is an existing
1555 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491556 trace_return.set_return_reason("default return, existing entry");
1557 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381558}
1559
Charlie Reisc0f17d2d2021-01-12 18:52:491560void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321561 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071562 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361563 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441564 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471565 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451566 NavigationRequest* request) {
dcheng9bfa5162016-04-09 01:00:571567 std::unique_ptr<NavigationEntryImpl> new_entry;
creisf49dfc92016-07-26 17:05:181568 bool update_virtual_url = false;
1569
Anton Bikineevf62d1bf2021-05-15 17:56:071570 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451571 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061572
creisf49dfc92016-07-26 17:05:181573 // First check if this is an in-page navigation. If so, clone the current
1574 // entry instead of looking at the pending entry, because the pending entry
1575 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361576 if (is_same_document && GetLastCommittedEntry()) {
Patrick Monette50e8bd82019-06-13 22:40:451577 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481578 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081579 params.document_sequence_number, params.app_history_key,
1580 rfh->GetSiteInstance(), nullptr, params.url,
1581 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091582 Referrer(*params.referrer), initiator_origin,
1583 request->GetRedirectChain(), params.page_state, params.method,
1584 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391585 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481586 request->GetSubresourceWebBundleNavigationInfo(),
Antonio Sartori78a749f2020-11-30 12:03:391587 // We will set the document policies later in this function.
Titouan Rigoudy6ec70402021-02-02 15:42:191588 nullptr /* policy_container_policies */);
Charles Reisf44482022017-10-13 21:15:031589
creisf49dfc92016-07-26 17:05:181590 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Harkiran Bolaria081c5c22021-09-13 08:44:531591 frame_entry, true, request->frame_tree_node(), frame_tree_.root());
Mike West800532c2021-10-14 09:26:521592 if (new_entry->GetURL().DeprecatedGetOriginAsURL() !=
1593 params.url.DeprecatedGetOriginAsURL()) {
jama78746e2017-02-22 17:21:571594 // TODO(jam): we had one report of this with a URL that was redirecting to
1595 // only tildes. Until we understand that better, don't copy the cert in
1596 // this case.
1597 new_entry->GetSSL() = SSLStatus();
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.NewPageInPageOriginMismatch",
1603 !!new_entry->GetSSL().certificate);
1604 }
jama78746e2017-02-22 17:21:571605 }
creisf49dfc92016-07-26 17:05:181606
Patrick Monette50e8bd82019-06-13 22:40:451607 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1608 // that |frame_entry| should now have a reference count of exactly 2: one
1609 // due to the local variable |frame_entry|, and another due to |new_entry|
1610 // also retaining one. This should never fail, because it's the main frame.
1611 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181612
1613 update_virtual_url = new_entry->update_virtual_url_with_url();
John Abd-El-Malek9cd697e2017-07-12 21:53:141614
John Abd-El-Malek380d3c5922017-09-08 00:20:311615 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121616 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141617 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus.NewPageInPage",
1618 !!new_entry->GetSSL().certificate);
1619 }
creisf49dfc92016-07-26 17:05:181620 }
1621
Harkiran Bolaria59290d62021-03-17 01:53:011622 // If this is an activation navigation from a prerendered page, transfer the
1623 // new entry from an entry already created and stored in the
1624 // NavigationRequest. |new_entry| will not have been set prior to this as
1625 // |is_same_document| is mutually exclusive with
1626 // |IsPrerenderedPageActivation|.
1627 if (request->IsPrerenderedPageActivation()) {
1628 DCHECK(!is_same_document);
1629 DCHECK(!new_entry);
1630 new_entry = request->TakePrerenderNavigationEntry();
1631 DCHECK(new_entry);
1632 }
1633
Charlie Reisc0f17d2d2021-01-12 18:52:491634 // Only make a copy of the pending entry if it is appropriate for the new
1635 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451636 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041637 // 1. The SiteInstance hasn't been assigned to something else.
1638 // 2. The pending entry was intended as a new entry, rather than being a
1639 // history navigation that was interrupted by an unrelated,
1640 // renderer-initiated navigation.
1641 // TODO(csharrison): Investigate whether we can remove some of the coarser
1642 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451643 if (!new_entry && PendingEntryMatchesRequest(request) &&
1644 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341645 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431646 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351647 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431648
[email protected]f1eb87a2011-05-06 17:49:411649 update_virtual_url = new_entry->update_virtual_url_with_url();
Camille Lamy62b826012019-02-26 09:15:471650 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451651 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141652
John Abd-El-Malek380d3c5922017-09-08 00:20:311653 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121654 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141655 UMA_HISTOGRAM_BOOLEAN(
1656 "Navigation.SecureSchemeHasSSLStatus.NewPagePendingEntryMatches",
1657 !!new_entry->GetSSL().certificate);
1658 }
creisf49dfc92016-07-26 17:05:181659 }
1660
Charlie Reisc0f17d2d2021-01-12 18:52:491661 // For cross-document commits with no matching pending entry, create a new
1662 // entry.
creisf49dfc92016-07-26 17:05:181663 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061664 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071665 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1666 initiator_origin,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:581667 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451668 params.transition, request->IsRendererInitiated(),
Lukasz Anforowicz435bcb582019-07-12 20:50:061669 nullptr); // blob_url_loader_factory
[email protected]f8f93eb2012-09-25 03:06:241670
1671 // Find out whether the new entry needs to update its virtual URL on URL
1672 // change and set up the entry accordingly. This is needed to correctly
1673 // update the virtual URL when replaceState is called after a pushState.
1674 GURL url = params.url;
1675 bool needs_update = false;
[email protected]47752982014-07-29 08:01:431676 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1677 &url, browser_context_, &needs_update);
[email protected]f8f93eb2012-09-25 03:06:241678 new_entry->set_update_virtual_url_with_url(needs_update);
1679
Charlie Reisc0f17d2d2021-01-12 18:52:491680 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411681 // update the virtual URL based on the new URL. For example, this is needed
1682 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1683 // the URL.
[email protected]f8f93eb2012-09-25 03:06:241684 update_virtual_url = needs_update;
Camille Lamy62b826012019-02-26 09:15:471685 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451686 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141687
John Abd-El-Malek380d3c5922017-09-08 00:20:311688 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121689 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141690 UMA_HISTOGRAM_BOOLEAN(
1691 "Navigation.SecureSchemeHasSSLStatus.NewPageNoMatchingEntry",
1692 !!new_entry->GetSSL().certificate);
1693 }
[email protected]e9ba4472008-09-14 15:42:431694 }
1695
Harkiran Bolaria59290d62021-03-17 01:53:011696 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
1697 // for prerendered contents, if any. This is because prerendering/activation
1698 // technically won't be creating a new document. Unlike BFCache, prerendering
1699 // creates a new NavigationEntry rather than using an existing one.
1700 if (!request->IsPrerenderedPageActivation()) {
1701 // Don't use the page type from the pending entry. Some interstitial page
1702 // may have set the type to interstitial. Once we commit, however, the page
1703 // type must always be normal or error.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121704 new_entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1705 : PAGE_TYPE_NORMAL);
Harkiran Bolaria59290d62021-03-17 01:53:011706 new_entry->SetURL(params.url);
1707 if (update_virtual_url)
1708 UpdateVirtualURLToURL(new_entry.get(), params.url);
1709 new_entry->SetReferrer(Referrer(*params.referrer));
1710 new_entry->SetTransitionType(params.transition);
1711 new_entry->set_site_instance(
1712 static_cast<SiteInstanceImpl*>(rfh->GetSiteInstance()));
1713 new_entry->SetOriginalRequestURL(request->GetOriginalRequestURL());
Rakina Zata Amnib597d632020-12-01 00:56:001714
Antonio Sartori4f5373792021-05-31 10:56:471715 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
Harkiran Bolaria59290d62021-03-17 01:53:011716 new_entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
[email protected]e9ba4472008-09-14 15:42:431717
Harkiran Bolaria59290d62021-03-17 01:53:011718 // Update the FrameNavigationEntry for new main frame commits.
1719 FrameNavigationEntry* frame_entry =
1720 new_entry->GetFrameEntry(rfh->frame_tree_node());
1721 frame_entry->set_frame_unique_name(rfh->frame_tree_node()->unique_name());
1722 frame_entry->set_item_sequence_number(params.item_sequence_number);
1723 frame_entry->set_document_sequence_number(params.document_sequence_number);
Nate Chapinfbfe5af2021-06-10 17:22:081724 frame_entry->set_app_history_key(params.app_history_key);
Harkiran Bolaria59290d62021-03-17 01:53:011725 frame_entry->set_redirect_chain(request->GetRedirectChain());
1726 frame_entry->SetPageState(params.page_state);
1727 frame_entry->set_method(params.method);
1728 frame_entry->set_post_id(params.post_id);
1729 frame_entry->set_policy_container_policies(
1730 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1731 request));
Antonio Sartori78a749f2020-11-30 12:03:391732
Anton Bikineevf62d1bf2021-05-15 17:56:071733 if (absl::optional<url::Origin> committed_origin =
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121734 GetCommittedOriginForFrameEntry(params, request)) {
1735 if (committed_origin.has_value())
1736 frame_entry->set_committed_origin(committed_origin.value());
1737 }
Harkiran Bolaria59290d62021-03-17 01:53:011738 if (request->web_bundle_navigation_info()) {
1739 frame_entry->set_web_bundle_navigation_info(
1740 request->web_bundle_navigation_info()->Clone());
1741 }
creis8b5cd4c2015-06-19 00:11:081742
Harkiran Bolaria59290d62021-03-17 01:53:011743 // history.pushState() is classified as a navigation to a new page, but sets
1744 // is_same_document to true. In this case, we already have the title and
1745 // favicon available, so set them immediately.
1746 if (is_same_document && GetLastCommittedEntry()) {
1747 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
1748 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1749 }
[email protected]3a868f212014-08-09 10:41:191750 }
[email protected]ff64b3e2014-05-31 04:07:331751
[email protected]60d6cca2013-04-30 08:47:131752 DCHECK(!params.history_list_was_cleared || !replace_entry);
1753 // The browser requested to clear the session history when it initiated the
1754 // navigation. Now we know that the renderer has updated its state accordingly
1755 // and it is safe to also clear the browser side history.
1756 if (params.history_list_was_cleared) {
arthursonzogni69a6a1b2019-09-17 09:23:001757 DiscardNonCommittedEntries();
[email protected]60d6cca2013-04-30 08:47:131758 entries_.clear();
1759 last_committed_entry_index_ = -1;
1760 }
1761
Nasko Oskovaee2f862018-06-15 00:05:521762 // If this is a new navigation with replacement and there is a
1763 // pending_entry_ which matches the navigation reported by the renderer
1764 // process, then it should be the one replaced, so update the
1765 // last_committed_entry_index_ to use it.
1766 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101767 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521768 last_committed_entry_index_ = pending_entry_index_;
1769 }
1770
Alexander Timine3ec4192020-04-20 16:39:401771 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411772 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401773 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051774
Carlos IL42b416592019-10-07 23:10:361775 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
1776 !request->post_commit_error_page_html().empty());
[email protected]e9ba4472008-09-14 15:42:431777}
1778
Charlie Reisc0f17d2d2021-01-12 18:52:491779void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321780 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071781 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361782 bool is_same_document,
jam48cea9082017-02-15 06:13:291783 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451784 NavigationRequest* request,
Peter Boströmd7592132019-01-30 04:50:311785 bool keep_pending_entry) {
creis26d22632017-04-21 20:23:561786 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1787 << "that a last committed entry exists.";
1788
[email protected]e9ba4472008-09-14 15:42:431789 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001790 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431791
Charlie Reis7e2cb6d2021-01-26 01:27:161792 NavigationEntryImpl* entry = nullptr;
Rakina Zata Amni153d5702021-09-13 22:48:001793 if (request->commit_params().intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:161794 // We're guaranteed to have a last committed entry if intended_as_new_entry
1795 // is true.
avicbdc4c12015-07-01 16:07:111796 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:161797 DCHECK(entry);
1798
1799 // If the NavigationRequest matches a new pending entry and is classified as
1800 // EXISTING_ENTRY, then it is a navigation to the same URL that was
1801 // converted to a reload, such as a user pressing enter in the omnibox.
1802 if (pending_entry_ && pending_entry_index_ == -1 &&
1803 pending_entry_->GetUniqueID() ==
1804 request->commit_params().nav_entry_id) {
1805 // Note: The pending entry will usually have a real ReloadType here, but
1806 // it can still be ReloadType::NONE in cases that
1807 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
1808
1809 // If we classified this correctly, the SiteInstance should not have
1810 // changed.
1811 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
1812
1813 // For converted reloads, we assign the entry's unique ID to be that of
1814 // the new one. Since this is always the result of a user action, we want
1815 // to dismiss infobars, etc. like a regular user-initiated navigation.
1816 entry->set_unique_id(pending_entry_->GetUniqueID());
1817
1818 // The extra headers may have changed due to reloading with different
1819 // headers.
1820 entry->set_extra_headers(pending_entry_->extra_headers());
1821 }
1822 // Otherwise, this was intended as a new entry but the pending entry was
1823 // lost in the meantime and no new entry was created. We are stuck at the
1824 // last committed entry.
1825
1826 // Even if this is a converted reload from pressing enter in the omnibox,
1827 // the server could redirect, requiring an update to the SSL status. If this
1828 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451829 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:161830 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:471831 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451832 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:161833 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141834
Charlie Reis7e2cb6d2021-01-26 01:27:161835 if (params.url.SchemeIs(url::kHttpsScheme) &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121836 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141837 bool has_cert = !!entry->GetSSL().certificate;
1838 if (is_same_document) {
1839 UMA_HISTOGRAM_BOOLEAN(
1840 "Navigation.SecureSchemeHasSSLStatus."
1841 "ExistingPageSameDocumentIntendedAsNew",
1842 has_cert);
1843 } else {
1844 UMA_HISTOGRAM_BOOLEAN(
1845 "Navigation.SecureSchemeHasSSLStatus."
1846 "ExistingPageDifferentDocumentIntendedAsNew",
1847 has_cert);
1848 }
1849 }
Rakina Zata Amnif6950d552020-11-24 03:26:101850 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:111851 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:101852 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b902016-09-01 16:58:161853
eugenebut604866f2017-05-10 21:35:361854 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451855 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361856 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1857 // this was a restored same document navigation entry, then it won't have
1858 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1859 // navigated it.
jam48cea9082017-02-15 06:13:291860 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
Mike West800532c2021-10-14 09:26:521861 if (entry->GetURL().DeprecatedGetOriginAsURL() ==
1862 last_entry->GetURL().DeprecatedGetOriginAsURL() &&
jam48cea9082017-02-15 06:13:291863 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1864 was_restored) {
1865 entry->GetSSL() = last_entry->GetSSL();
1866 }
1867 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451868 // In rapid back/forward navigations |request| sometimes won't have a cert
1869 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191870 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451871 if (request->GetSSLInfo().has_value() &&
1872 request->GetSSLInfo()->is_valid()) {
1873 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
Mike West800532c2021-10-14 09:26:521874 } else if (entry->GetURL().DeprecatedGetOriginAsURL() !=
1875 request->GetURL().DeprecatedGetOriginAsURL()) {
John Abd-El-Malek3f247082017-12-07 19:02:191876 entry->GetSSL() = SSLStatus();
1877 }
jam48cea9082017-02-15 06:13:291878 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141879
John Abd-El-Malek380d3c5922017-09-08 00:20:311880 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121881 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141882 bool has_cert = !!entry->GetSSL().certificate;
1883 if (is_same_document && was_restored) {
1884 UMA_HISTOGRAM_BOOLEAN(
1885 "Navigation.SecureSchemeHasSSLStatus."
1886 "ExistingPageSameDocumentRestoredBrowserInitiated",
1887 has_cert);
1888 } else if (is_same_document && !was_restored) {
1889 UMA_HISTOGRAM_BOOLEAN(
1890 "Navigation.SecureSchemeHasSSLStatus."
1891 "ExistingPageSameDocumentBrowserInitiated",
1892 has_cert);
1893 } else if (!is_same_document && was_restored) {
1894 UMA_HISTOGRAM_BOOLEAN(
1895 "Navigation.SecureSchemeHasSSLStatus."
1896 "ExistingPageRestoredBrowserInitiated",
1897 has_cert);
1898 } else {
1899 UMA_HISTOGRAM_BOOLEAN(
1900 "Navigation.SecureSchemeHasSSLStatus.ExistingPageBrowserInitiated",
1901 has_cert);
1902 }
1903 }
avicbdc4c12015-07-01 16:07:111904 } else {
1905 // This is renderer-initiated. The only kinds of renderer-initated
Rakina Zata Amni557afb92021-07-17 04:39:571906 // navigations that are EXISTING_ENTRY are same-document navigations that
1907 // result in replacement (e.g. history.replaceState(), location.replace(),
1908 // forced replacements for trivial session history contexts). For these
1909 // cases, we reuse the last committed entry.
avicbdc4c12015-07-01 16:07:111910 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101911
Mikel Astizba9cf2fd2017-12-17 10:38:101912 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1913 // to avoid misleading interpretations (e.g. URLs paired with
1914 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1915 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1916 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1917
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571918 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:101919
eugenebut604866f2017-05-10 21:35:361920 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451921 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361922 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471923 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451924 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141925
John Abd-El-Malek380d3c5922017-09-08 00:20:311926 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121927 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141928 bool has_cert = !!entry->GetSSL().certificate;
1929 if (is_same_document) {
1930 UMA_HISTOGRAM_BOOLEAN(
1931 "Navigation.SecureSchemeHasSSLStatus."
1932 "ExistingPageSameDocumentRendererInitiated",
1933 has_cert);
1934 } else {
1935 UMA_HISTOGRAM_BOOLEAN(
1936 "Navigation.SecureSchemeHasSSLStatus."
1937 "ExistingPageDifferentDocumentRendererInitiated",
1938 has_cert);
1939 }
1940 }
avicbdc4c12015-07-01 16:07:111941 }
1942 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:431943
[email protected]5ccd4dc2012-10-24 02:28:141944 // The URL may have changed due to redirects.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121945 entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1946 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041947 entry->SetURL(params.url);
arthursonzogni73fe3212020-11-17 13:24:071948 entry->SetReferrer(Referrer(*params.referrer));
[email protected]38178a42009-12-17 18:58:321949 if (entry->update_virtual_url_with_url())
1950 UpdateVirtualURLToURL(entry, params.url);
[email protected]5ccd4dc2012-10-24 02:28:141951
jam015ba062017-01-06 21:17:001952 // The site instance will normally be the same except
1953 // 1) session restore, when no site instance will be assigned or
1954 // 2) redirect, when the site instance is reset.
Lei Zhang96031532019-10-10 19:05:471955 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
naskoaf182192016-08-11 02:12:011956 entry->site_instance() == rfh->GetSiteInstance());
clamy432acb22016-04-15 19:41:431957
naskoaf182192016-08-11 02:12:011958 // Update the existing FrameNavigationEntry to ensure all of its members
1959 // reflect the parameters coming from the renderer process.
Anton Bikineevf62d1bf2021-05-15 17:56:071960 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451961 request->common_params().initiator_origin;
naskoaf182192016-08-11 02:12:011962 entry->AddOrUpdateFrameEntry(
Nate Chapin9f169072021-06-09 19:32:371963 rfh->frame_tree_node(), NavigationEntryImpl::UpdatePolicy::kUpdate,
1964 params.item_sequence_number, params.document_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081965 params.app_history_key, rfh->GetSiteInstance(), nullptr, params.url,
Nate Chapin9f169072021-06-09 19:32:371966 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091967 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071968 params.page_state, params.method, params.post_id,
1969 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031970 request->web_bundle_navigation_info()
1971 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391972 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481973 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191974 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1975 request));
creis22a7b4c2016-04-28 07:20:301976
[email protected]5ccd4dc2012-10-24 02:28:141977 // The redirected to page should not inherit the favicon from the previous
1978 // page.
eugenebut604866f2017-05-10 21:35:361979 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:481980 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:141981
Peter Boströmd7592132019-01-30 04:50:311982 // We should also usually discard the pending entry if it corresponds to a
1983 // different navigation, since that one is now likely canceled. In rare
1984 // cases, we leave the pending entry for another navigation in place when we
1985 // know it is still ongoing, to avoid a flicker in the omnibox (see
1986 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:431987 //
1988 // Note that we need to use the "internal" version since we don't want to
1989 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:311990 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001991 DiscardNonCommittedEntries();
[email protected]40bcc302009-03-02 20:50:391992
Carlos IL4dea8902020-05-26 15:14:291993 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:111994 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:431995}
1996
[email protected]d202a7c2012-01-04 07:53:471997void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:321998 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071999 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:362000 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:472001 bool replace_entry,
2002 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:452003 NavigationRequest* request) {
avi25f5f9e2015-07-17 20:08:262004 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2005 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
[email protected]09b8f82f2009-06-16 20:22:112006
[email protected]e9ba4472008-09-14 15:42:432007 // Manual subframe navigations just get the current entry cloned so the user
2008 // can go back or forward to it. The actual subframe information will be
2009 // stored in the page state for each of those entries. This happens out of
2010 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:092011 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
2012 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:382013
Mikel Astizba9cf2fd2017-12-17 10:38:102014 // The DCHECK below documents the fact that we don't know of any situation
2015 // where |replace_entry| is true for subframe navigations. This simplifies
2016 // reasoning about the replacement struct for subframes (see
2017 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
2018 DCHECK(!replace_entry);
2019
Patrick Monette50e8bd82019-06-13 22:40:452020 // This FrameNavigationEntry might not end up being used in the
2021 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Anton Bikineevf62d1bf2021-05-15 17:56:072022 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452023 request->common_params().initiator_origin;
Patrick Monette50e8bd82019-06-13 22:40:452024 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:482025 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:082026 params.document_sequence_number, params.app_history_key,
2027 rfh->GetSiteInstance(), nullptr, params.url,
2028 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:092029 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072030 params.page_state, params.method, params.post_id,
2031 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032032 request->web_bundle_navigation_info()
2033 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392034 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482035 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:192036 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
2037 request));
Charles Reisf44482022017-10-13 21:15:032038
creisce0ef3572017-01-26 17:53:082039 std::unique_ptr<NavigationEntryImpl> new_entry =
2040 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:452041 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:002042 frame_tree_.root());
creise062d542015-08-25 02:01:552043
Alexander Timine3ec4192020-04-20 16:39:402044 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:412045 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:402046 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:472047
creisce0ef3572017-01-26 17:53:082048 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:452049 // to replace, which can happen due to a unique name change. See
2050 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
2051 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:382052
Carlos IL42b416592019-10-07 23:10:362053 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false);
[email protected]e9ba4472008-09-14 15:42:432054}
2055
[email protected]d202a7c2012-01-04 07:53:472056bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:322057 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072058 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392059 bool is_same_document,
Nate Chapinc7019dd7d2021-06-25 18:29:252060 bool was_on_initial_empty_document,
Mohamed Abdelhalim833de902019-09-16 17:41:452061 NavigationRequest* request) {
avi9f07a0c2015-02-18 22:51:292062 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2063 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2064
[email protected]e9ba4472008-09-14 15:42:432065 // We're guaranteed to have a previously committed entry, and we now need to
2066 // handle navigation inside of a subframe in it without creating a new entry.
2067 DCHECK(GetLastCommittedEntry());
2068
creis913c63ce2016-07-16 19:52:522069 // For newly created subframes, we don't need to send a commit notification.
2070 // This is only necessary for history navigations in subframes.
2071 bool send_commit_notification = false;
2072
Rakina Zata Amnif6950d552020-11-24 03:26:102073 // If |nav_entry_id| is non-zero and matches an existing entry, this
2074 // is a history navigation. Update the last committed index accordingly. If
2075 // we don't recognize the |nav_entry_id|, it might be a recently
2076 // pruned entry. We'll handle it below.
2077 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2078 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472079 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062080 // Make sure that a subframe commit isn't changing the main frame's
2081 // origin. Otherwise the renderer process may be confused, leading to a
2082 // URL spoof. We can't check the path since that may change
2083 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572084 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2085 // about:blank, file, and unique origins are more subtle to get right.
Charlie Reis95fbca442021-05-21 21:38:242086 // We should use checks similar to RenderFrameHostImpl's
2087 // CanCommitUrlAndOrigin on the main frame during subframe commits.
2088 // See https://crbug.com/1209092.
creis37988b92016-06-10 18:03:572089 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2090 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2091 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2092 dest_top_url.SchemeIsHTTPOrHTTPS() &&
Mike West800532c2021-10-14 09:26:522093 current_top_url.DeprecatedGetOriginAsURL() !=
2094 dest_top_url.DeprecatedGetOriginAsURL()) {
creisfb6eeb62016-05-10 19:01:512095 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2096 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062097 }
creis3cdc3b02015-05-29 23:00:472098
creis913c63ce2016-07-16 19:52:522099 // We only need to discard the pending entry in this history navigation
2100 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062101 last_committed_entry_index_ = entry_index;
arthursonzogni69a6a1b2019-09-17 09:23:002102 DiscardNonCommittedEntries();
creis913c63ce2016-07-16 19:52:522103
2104 // History navigations should send a commit notification.
2105 send_commit_notification = true;
avi98405c22015-05-21 20:47:062106 }
[email protected]e9ba4472008-09-14 15:42:432107 }
[email protected]f233e4232013-02-23 00:55:142108
creisce0ef3572017-01-26 17:53:082109 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2110 // it may be a "history auto" case where we update an existing one.
2111 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Nate Chapin9f169072021-06-09 19:32:372112
2113 // We may want to update |last_committed|'s FrameNavigationEntry (if one
2114 // exists), or we may want to clobber it and create a new one. We update in
2115 // cases where the corresponding FrameNavigationEntry is conceptually similar
2116 // to the document described by the commit params: same-document
2117 // navigations, history traversal to an existing entry, and reloads (including
2118 // a "soft reload" where we navigate to the same url without flagging it as a
2119 // reload). But in the case of a different document that is not logically
2120 // related to the committed FrameNavigationEntry's document (cross-document,
2121 // not same url, not a reload, not a history traversal), we replace rather
2122 // than update.
Nate Chapinc7019dd7d2021-06-25 18:29:252123 //
Nate Chapin9f169072021-06-09 19:32:372124 // In the case where we update, the FrameNavigationEntry will potentially be
2125 // shared across multiple NavigationEntries, and any updates will be reflected
2126 // in all of those NavigationEntries. In the replace case, any existing
2127 // sharing with other NavigationEntries will stop.
Nate Chapinc7019dd7d2021-06-25 18:29:252128 //
2129 // When navigating away from the initial empty document, we also update rather
2130 // than replace. Either update or replace will overwrite the initial empty
2131 // document state for |last_committed|, but if the FrameNavigationEntry for
2132 // the initial empty document is shared across multiple NavigationEntries (due
2133 // to a navigation in another frame), we want to make sure we overwrite the
2134 // initial empty document state everywhere this FrameNavigationEntry is used,
2135 // which is accompished by updating the existing FrameNavigationEntry.
Nate Chapin9f169072021-06-09 19:32:372136 FrameNavigationEntry* last_committed_frame_entry =
2137 last_committed->GetFrameEntry(rfh->frame_tree_node());
2138 NavigationEntryImpl::UpdatePolicy update_policy =
2139 NavigationEntryImpl::UpdatePolicy::kUpdate;
2140 if (request->common_params().navigation_type ==
Minggang Wangb9f3fa92021-07-01 15:30:312141 blink::mojom::NavigationType::DIFFERENT_DOCUMENT &&
Nate Chapin9f169072021-06-09 19:32:372142 last_committed_frame_entry &&
Nate Chapinc7019dd7d2021-06-25 18:29:252143 last_committed_frame_entry->url() != params.url &&
2144 !was_on_initial_empty_document) {
Nate Chapin9f169072021-06-09 19:32:372145 update_policy = NavigationEntryImpl::UpdatePolicy::kReplace;
2146 }
2147
Anton Bikineevf62d1bf2021-05-15 17:56:072148 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452149 request->common_params().initiator_origin;
creisce0ef3572017-01-26 17:53:082150 last_committed->AddOrUpdateFrameEntry(
Nate Chapin9f169072021-06-09 19:32:372151 rfh->frame_tree_node(), update_policy, params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:082152 params.document_sequence_number, params.app_history_key,
2153 rfh->GetSiteInstance(), nullptr, params.url,
2154 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:092155 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072156 params.page_state, params.method, params.post_id,
2157 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032158 request->web_bundle_navigation_info()
2159 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392160 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482161 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:192162 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
2163 request));
creis625a0c7d2015-03-24 23:17:122164
creis913c63ce2016-07-16 19:52:522165 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432166}
2167
[email protected]d202a7c2012-01-04 07:53:472168int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232169 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032170 for (size_t i = 0; i < entries_.size(); ++i) {
2171 if (entries_[i].get() == entry)
2172 return i;
2173 }
2174 return -1;
[email protected]765b35502008-08-21 00:51:202175}
2176
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572177void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242178 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572179 NavigationControllerImpl* source =
2180 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572181 // Verify that we look new.
Lei Zhang96031532019-10-10 19:05:472182 DCHECK_EQ(0, GetEntryCount());
2183 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:572184
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572185 if (source->GetEntryCount() == 0)
[email protected]ce3fa3c2009-04-20 19:55:572186 return; // Nothing new to do.
2187
Francois Dorayeaace782017-06-21 16:37:242188 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442189 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572190 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572191
Fergal Dalya1d569972021-03-16 03:24:532192 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302193 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532194 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2195 session_storage_namespace_map_[it.first] = source_namespace->Clone();
Aaron Colwellb731a0ae2021-03-19 19:14:472196 OnStoragePartitionIdAdded(it.first);
[email protected]fdac6ade2013-07-20 01:06:302197 }
[email protected]4e6419c2010-01-15 04:50:342198
Lukasz Anforowicz0de0f452020-12-02 19:57:152199 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572200}
2201
Aran Gilman37d11632019-10-08 23:07:152202void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2203 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162204 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012205 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162206
[email protected]d202a7c2012-01-04 07:53:472207 NavigationControllerImpl* source =
2208 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252209
avi2b177592014-12-10 02:08:022210 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012211 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252212
[email protected]474f8512013-05-31 22:31:162213 // We now have one entry, possibly with a new pending entry. Ensure that
2214 // adding the entries from source won't put us over the limit.
2215 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572216 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572217 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252218
Carlos IL4dea8902020-05-26 15:14:292219 // Insert the entries from source. Ignore any pending entry, since it has not
2220 // committed in source.
[email protected]474f8512013-05-31 22:31:162221 int max_source_index = source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:252222 if (max_source_index == -1)
[email protected]a26023822011-12-29 00:23:552223 max_source_index = source->GetEntryCount();
[email protected]e1cd5452010-08-26 18:03:252224 else
2225 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572226
2227 // Ignore the source's current entry if merging with replacement.
2228 // TODO(davidben): This should preserve entries forward of the current
2229 // too. http://crbug.com/317872
2230 if (replace_entry && max_source_index > 0)
2231 max_source_index--;
2232
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572233 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252234
2235 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552236 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142237
Hayato Ito2c8c08d02021-06-23 03:38:432238 BroadcastHistoryOffsetAndLength();
[email protected]e1cd5452010-08-26 18:03:252239}
2240
[email protected]79368982013-11-13 01:11:012241bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162242 // If there is no last committed entry, we cannot prune. Even if there is a
2243 // pending entry, it may not commit, leaving this WebContents blank, despite
2244 // possibly giving it new entries via CopyStateFromAndPrune.
2245 if (last_committed_entry_index_ == -1)
2246 return false;
[email protected]9350602e2013-02-26 23:27:442247
[email protected]474f8512013-05-31 22:31:162248 // We cannot prune if there is a pending entry at an existing entry index.
2249 // It may not commit, so we have to keep the last committed entry, and thus
2250 // there is no sensible place to keep the pending entry. It is ok to have
2251 // a new pending entry, which can optionally commit as a new navigation.
2252 if (pending_entry_index_ != -1)
2253 return false;
2254
[email protected]474f8512013-05-31 22:31:162255 return true;
2256}
2257
[email protected]79368982013-11-13 01:11:012258void NavigationControllerImpl::PruneAllButLastCommitted() {
2259 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162260
avi2b177592014-12-10 02:08:022261 DCHECK_EQ(0, last_committed_entry_index_);
2262 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442263
Hayato Ito2c8c08d02021-06-23 03:38:432264 BroadcastHistoryOffsetAndLength();
[email protected]9350602e2013-02-26 23:27:442265}
2266
[email protected]79368982013-11-13 01:11:012267void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162268 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012269 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262270
[email protected]474f8512013-05-31 22:31:162271 // Erase all entries but the last committed entry. There may still be a
2272 // new pending entry after this.
2273 entries_.erase(entries_.begin(),
2274 entries_.begin() + last_committed_entry_index_);
2275 entries_.erase(entries_.begin() + 1, entries_.end());
2276 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262277}
2278
Christian Dullweber1af31e62018-02-22 11:49:482279void NavigationControllerImpl::DeleteNavigationEntries(
2280 const DeletionPredicate& deletionPredicate) {
2281 // It is up to callers to check the invariants before calling this.
2282 CHECK(CanPruneAllButLastCommitted());
2283 std::vector<int> delete_indices;
2284 for (size_t i = 0; i < entries_.size(); i++) {
2285 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572286 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482287 delete_indices.push_back(i);
2288 }
2289 }
2290 if (delete_indices.empty())
2291 return;
2292
2293 if (delete_indices.size() == GetEntryCount() - 1U) {
2294 PruneAllButLastCommitted();
2295 } else {
2296 // Do the deletion in reverse to preserve indices.
2297 for (auto it = delete_indices.rbegin(); it != delete_indices.rend(); ++it) {
2298 RemoveEntryAtIndex(*it);
2299 }
Hayato Ito2c8c08d02021-06-23 03:38:432300 BroadcastHistoryOffsetAndLength();
Christian Dullweber1af31e62018-02-22 11:49:482301 }
2302 delegate()->NotifyNavigationEntriesDeleted();
2303}
2304
Shivani Sharma883f5f32019-02-12 18:20:012305bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2306 auto* entry = GetEntryAtIndex(index);
2307 return entry && entry->should_skip_on_back_forward_ui();
2308}
2309
Carlos Caballero35ce710c2019-09-19 10:59:452310BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2311 return back_forward_cache_;
2312}
2313
clamy987a3752018-05-03 17:36:262314void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2315 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2316 // progress, since this will cause a use-after-free. (We only allow this
2317 // when the tab is being destroyed for shutdown, since it won't return to
2318 // NavigateToEntry in that case.) http://crbug.com/347742.
Takashi Toyoshimaea534ef22021-07-21 03:27:592319 CHECK(!in_navigate_to_pending_entry_ || frame_tree_.IsBeingDestroyed());
clamy987a3752018-05-03 17:36:262320
2321 if (was_failure && pending_entry_) {
2322 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2323 } else {
2324 failed_pending_entry_id_ = 0;
2325 }
2326
2327 if (pending_entry_) {
2328 if (pending_entry_index_ == -1)
2329 delete pending_entry_;
2330 pending_entry_index_ = -1;
2331 pending_entry_ = nullptr;
2332 }
arthursonzogni66f711c2019-10-08 14:40:362333
2334 // Ensure any refs to the current pending entry are ignored if they get
2335 // deleted, by clearing the set of known refs. All future pending entries will
2336 // only be affected by new refs.
2337 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262338}
2339
2340void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2341 if (pending_entry_)
2342 pending_entry_->set_ssl_error(error);
2343}
2344
Camille Lamy5193caa2018-10-12 11:59:422345#if defined(OS_ANDROID)
2346// static
2347bool NavigationControllerImpl::ValidateDataURLAsString(
2348 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2349 if (!data_url_as_string)
2350 return false;
2351
2352 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2353 return false;
2354
2355 // The number of characters that is enough for validating a data: URI.
2356 // From the GURL's POV, the only important part here is scheme, it doesn't
2357 // check the actual content. Thus we can take only the prefix of the url, to
2358 // avoid unneeded copying of a potentially long string.
2359 const size_t kDataUriPrefixMaxLen = 64;
2360 GURL data_url(
2361 std::string(data_url_as_string->front_as<char>(),
2362 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2363 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2364 return false;
2365
2366 return true;
2367}
2368#endif
2369
Shivani Sharma194877032019-03-07 17:52:472370void NavigationControllerImpl::NotifyUserActivation() {
2371 // When a user activation occurs, ensure that all adjacent entries for the
2372 // same document clear their skippable bit, so that the history manipulation
2373 // intervention does not apply to them.
2374 auto* last_committed_entry = GetLastCommittedEntry();
2375 if (!last_committed_entry)
2376 return;
Shivani Sharma194877032019-03-07 17:52:472377
shivanigithub99368382021-06-16 18:33:372378 if (base::FeatureList::IsEnabled(
2379 features::kDebugHistoryInterventionNoUserActivation)) {
2380 return;
2381 }
2382
Shivani Sharmac4cc8922019-04-18 03:11:172383 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472384}
2385
clamy987a3752018-05-03 17:36:262386bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2387 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172388 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262389 NavigationEntryImpl* entry =
2390 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2391 if (!entry)
2392 return false;
2393
2394 FrameNavigationEntry* frame_entry =
2395 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2396 if (!frame_entry)
2397 return false;
2398
Nate Chapin45f620582021-09-30 17:45:432399 // |is_browser_initiated| is false here because a navigation in a new subframe
2400 // always begins with renderer action (i.e., an HTML element being inserted
2401 // into the DOM), so it is always renderer-initiated.
Camille Lamy5193caa2018-10-12 11:59:422402 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572403 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232404 ReloadType::NONE, false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432405 true /* is_history_navigation_in_new_child */,
2406 false /* is_browser_initiated */);
clamyea99ea12018-05-28 13:54:232407
2408 if (!request)
2409 return false;
2410
arthursonzognif046d4a2019-12-12 19:08:102411 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412412
Lukasz Anforowicz9ee83c272020-12-01 20:14:052413 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2414 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232415
2416 return true;
clamy987a3752018-05-03 17:36:262417}
2418
Tsuyoshi Horo52fd08e2020-07-07 07:03:452419bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2420 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2421 if (!entry)
2422 return false;
2423 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2424 if (!frame_entry)
2425 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142426 ReloadType reload_type = ReloadType::NORMAL;
2427 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452428 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142429 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452430 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432431 false /* is_history_navigation_in_new_child */,
2432 true /* is_browser_initiated */);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452433 if (!request)
2434 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052435 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452436 return true;
2437}
2438
Dave Tapuska8bfd84c2019-03-26 20:47:162439void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2440 int offset,
2441 int sandbox_frame_tree_node_id) {
2442 if (!CanGoToOffset(offset))
2443 return;
Nate Chapin45f620582021-09-30 17:45:432444 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id,
2445 false /* is_browser_initiated */);
Dave Tapuska8bfd84c2019-03-26 20:47:162446}
2447
clamy987a3752018-05-03 17:36:262448void NavigationControllerImpl::NavigateFromFrameProxy(
2449 RenderFrameHostImpl* render_frame_host,
2450 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022451 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452452 int initiator_process_id,
Anton Bikineevf62d1bf2021-05-15 17:56:072453 const absl::optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262454 bool is_renderer_initiated,
2455 SiteInstance* source_site_instance,
2456 const Referrer& referrer,
2457 ui::PageTransition page_transition,
2458 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252459 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262460 const std::string& method,
2461 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092462 const std::string& extra_headers,
Antonio Sartori2f763d9d2021-04-21 10:04:142463 network::mojom::SourceLocationPtr source_location,
John Delaney50425f82020-04-07 16:26:212464 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Anton Bikineevf62d1bf2021-05-15 17:56:072465 const absl::optional<blink::Impression>& impression) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582466 if (is_renderer_initiated)
2467 DCHECK(initiator_origin.has_value());
2468
clamy987a3752018-05-03 17:36:262469 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582470
Rakina Zata Amni66992a82020-06-24 03:57:522471 // Don't allow an entry replacement if there is no entry to replace.
2472 // http://crbug.com/457149
2473 if (GetEntryCount() == 0)
2474 should_replace_current_entry = false;
2475
clamy987a3752018-05-03 17:36:262476 // Create a NavigationEntry for the transfer, without making it the pending
2477 // entry. Subframe transfers should have a clone of the last committed entry
2478 // with a FrameNavigationEntry for the target frame. Main frame transfers
2479 // should have a new NavigationEntry.
2480 // TODO(creis): Make this unnecessary by creating (and validating) the params
2481 // directly, passing them to the destination RenderFrameHost. See
2482 // https://crbug.com/536906.
2483 std::unique_ptr<NavigationEntryImpl> entry;
Harkiran Bolariae1b5158b2021-09-16 19:03:262484 if (!render_frame_host->is_main_frame()) {
clamy987a3752018-05-03 17:36:262485 // Subframe case: create FrameNavigationEntry.
2486 if (GetLastCommittedEntry()) {
2487 entry = GetLastCommittedEntry()->Clone();
2488 entry->set_extra_headers(extra_headers);
2489 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2490 // Renderer-initiated navigation that target a remote frame are currently
2491 // classified as browser-initiated when this one has already navigated.
2492 // See https://crbug.com/722251.
2493 } else {
2494 // If there's no last committed entry, create an entry for about:blank
2495 // with a subframe entry for our destination.
2496 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2497 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:062498 GURL(url::kAboutBlankURL), referrer, initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:102499 source_site_instance, page_transition, is_renderer_initiated,
2500 extra_headers, browser_context_,
Hayato Ito303654c2021-06-30 09:07:542501 nullptr /* blob_url_loader_factory */));
clamy987a3752018-05-03 17:36:262502 }
Nate Chapin9f169072021-06-09 19:32:372503 // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter
2504 // to CreateNavigationRequestFromLoadParams(), so while kReplace might
2505 // remove child FrameNavigationEntries (e.g., if this is a cross-process
2506 // same-document navigation), they will still be present in the
2507 // committed NavigationEntry that will be referenced to construct the new
2508 // FrameNavigationEntry tree when this navigation commits.
clamy987a3752018-05-03 17:36:262509 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:082510 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582511 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072512 absl::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202513 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392514 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482515 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori79d549d2021-02-18 12:59:542516 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262517 } else {
2518 // Main frame case.
2519 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102520 url, referrer, initiator_origin, source_site_instance, page_transition,
2521 is_renderer_initiated, extra_headers, browser_context_,
Hayato Ito303654c2021-06-30 09:07:542522 blob_url_loader_factory));
clamy987a3752018-05-03 17:36:262523 entry->root_node()->frame_entry->set_source_site_instance(
2524 static_cast<SiteInstanceImpl*>(source_site_instance));
2525 entry->root_node()->frame_entry->set_method(method);
2526 }
clamy987a3752018-05-03 17:36:262527
Camille Lamy5193caa2018-10-12 11:59:422528 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262529 if (GetLastCommittedEntry() &&
2530 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2531 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422532 override_user_agent = true;
clamy987a3752018-05-03 17:36:262533 }
2534 // TODO(creis): Set user gesture and intent received timestamp on Android.
2535
2536 // We may not have successfully added the FrameNavigationEntry to |entry|
2537 // above (per https://crbug.com/608402), in which case we create it from
2538 // scratch. This works because we do not depend on |frame_entry| being inside
2539 // |entry| during NavigateToEntry. This will go away when we shortcut this
2540 // further in https://crbug.com/536906.
2541 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2542 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452543 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Nate Chapinfbfe5af2021-06-10 17:22:082544 node->unique_name(), -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582545 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072546 absl::nullopt /* origin */, referrer, initiator_origin,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:122547 std::vector<GURL>(), blink::PageState(), method, -1,
2548 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482549 nullptr /* subresource_web_bundle_navigation_info */,
Titouan Rigoudy6ec70402021-02-02 15:42:192550 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262551 }
2552
Camille Lamy5193caa2018-10-12 11:59:422553 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022554 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452555 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292556 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422557 params.source_site_instance = source_site_instance;
2558 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2559 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032560 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422561 params.referrer = referrer;
2562 /* params.redirect_chain: skip */
2563 params.extra_headers = extra_headers;
2564 params.is_renderer_initiated = is_renderer_initiated;
2565 params.override_user_agent = UA_OVERRIDE_INHERIT;
2566 /* params.base_url_for_data_url: skip */
2567 /* params.virtual_url_for_data_url: skip */
2568 /* params.data_url_as_string: skip */
2569 params.post_data = post_body;
2570 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582571 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422572 /* params.frame_name: skip */
2573 // TODO(clamy): See if user gesture should be propagated to this function.
2574 params.has_user_gesture = false;
2575 params.should_clear_history_list = false;
2576 params.started_from_context_menu = false;
2577 /* params.navigation_ui_data: skip */
2578 /* params.input_start: skip */
Minggang Wangf59db47b2021-06-16 01:56:222579 params.was_activated = blink::mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542580 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212581 params.impression = impression;
Antonio Sartori6984c742021-08-26 08:03:412582 params.download_policy = std::move(download_policy);
Camille Lamy5193caa2018-10-12 11:59:422583
2584 std::unique_ptr<NavigationRequest> request =
2585 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032586 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:142587 false /* has_user_gesture */, std::move(source_location),
Antonio Sartori6984c742021-08-26 08:03:412588 ReloadType::NONE, entry.get(), frame_entry.get());
clamyea99ea12018-05-28 13:54:232589
2590 if (!request)
2591 return;
2592
Arthur Hemery948742762019-09-18 10:06:242593 // At this stage we are proceeding with this navigation. If this was renderer
2594 // initiated with user gesture, we need to make sure we clear up potential
2595 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2596 DiscardNonCommittedEntries();
2597
Lukasz Anforowicz9ee83c272020-12-01 20:14:052598 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262599}
2600
[email protected]d1198fd2012-08-13 22:50:192601void NavigationControllerImpl::SetSessionStorageNamespace(
Aaron Colwellf3b316e2021-03-11 20:17:052602 const StoragePartitionId& partition_id,
[email protected]8ff00d72012-10-23 19:12:212603 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192604 if (!session_storage_namespace)
2605 return;
2606
2607 // We can't overwrite an existing SessionStorage without violating spec.
2608 // Attempts to do so may give a tab access to another tab's session storage
2609 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152610 bool successful_insert =
2611 session_storage_namespace_map_
Aaron Colwellf3b316e2021-03-11 20:17:052612 .insert(std::make_pair(partition_id,
2613 static_cast<SessionStorageNamespaceImpl*>(
2614 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302615 .second;
2616 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
Aaron Colwellb731a0ae2021-03-19 19:14:472617 OnStoragePartitionIdAdded(partition_id);
[email protected]d1198fd2012-08-13 22:50:192618}
2619
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572620bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Aran Gilman37d11632019-10-08 23:07:152621 return IsInitialNavigation() && !GetLastCommittedEntry() &&
Carlos Caballeroede6f8c2021-01-28 11:01:502622 !frame_tree_.has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462623}
2624
Aran Gilman37d11632019-10-08 23:07:152625SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Aaron Colwell78b4bde2021-03-16 16:16:092626 const SiteInfo& site_info) {
2627 // TODO(acolwell): Remove partition_id logic once we have successfully
2628 // migrated the implementation to be a StoragePartitionConfig. At that point
2629 // |site_info| can be replaced with a StoragePartitionConfig.
2630 const StoragePartitionId partition_id =
2631 site_info.GetStoragePartitionId(browser_context_);
2632 const StoragePartitionConfig partition_config =
Robbie McElrath7d4bd852021-07-24 04:02:192633 site_info.storage_partition_config();
[email protected]d1198fd2012-08-13 22:50:192634
[email protected]fdac6ade2013-07-20 01:06:302635 StoragePartition* partition =
Lukasz Anforowiczb9a969a2021-04-29 15:26:252636 browser_context_->GetStoragePartition(partition_config);
michaelnbacbcbd2016-02-09 00:32:032637 DOMStorageContextWrapper* context_wrapper =
2638 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2639
2640 SessionStorageNamespaceMap::const_iterator it =
2641 session_storage_namespace_map_.find(partition_id);
2642 if (it != session_storage_namespace_map_.end()) {
2643 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152644 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2645 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:472646
2647 // Verify that the config we generated now matches the one that
2648 // was generated when the namespace was added to the map.
Aaron Colwellb731a0ae2021-03-19 19:14:472649 if (partition_config != it->first.config()) {
2650 LogStoragePartitionIdCrashKeys(it->first, partition_id);
2651 }
Sharon Yangd4f48792021-06-30 17:02:532652 CHECK_EQ(partition_config, it->first.config());
Aaron Colwellb731a0ae2021-03-19 19:14:472653
michaelnbacbcbd2016-02-09 00:32:032654 return it->second.get();
2655 }
2656
2657 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102658 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2659 SessionStorageNamespaceImpl::Create(context_wrapper);
2660 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2661 session_storage_namespace.get();
2662 session_storage_namespace_map_[partition_id] =
2663 std::move(session_storage_namespace);
Aaron Colwellb731a0ae2021-03-19 19:14:472664 OnStoragePartitionIdAdded(partition_id);
[email protected]fdac6ade2013-07-20 01:06:302665
Daniel Murphy31bbb8b12018-02-07 21:44:102666 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302667}
2668
2669SessionStorageNamespace*
2670NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Robbie McElrath7d4bd852021-07-24 04:02:192671 return GetSessionStorageNamespace(SiteInfo(GetBrowserContext()));
[email protected]fdac6ade2013-07-20 01:06:302672}
2673
2674const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572675NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302676 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552677}
[email protected]d202a7c2012-01-04 07:53:472678
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572679bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562680 return needs_reload_;
2681}
[email protected]a26023822011-12-29 00:23:552682
[email protected]46bb5e9c2013-10-03 22:16:472683void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412684 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2685}
2686
2687void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472688 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412689 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542690
2691 if (last_committed_entry_index_ != -1) {
2692 entries_[last_committed_entry_index_]->SetTransitionType(
2693 ui::PAGE_TRANSITION_RELOAD);
2694 }
[email protected]46bb5e9c2013-10-03 22:16:472695}
2696
[email protected]d202a7c2012-01-04 07:53:472697void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572698 DCHECK_LT(index, GetEntryCount());
2699 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312700 DiscardNonCommittedEntries();
2701
2702 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122703 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312704 last_committed_entry_index_--;
2705}
2706
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572707NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272708 // If there is no pending_entry_, there should be no pending_entry_index_.
2709 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2710
2711 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362712 // at that index. An exception is while a reload of a post commit error page
2713 // is ongoing; in that case pending entry will point to the entry replaced
2714 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272715 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362716 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2717 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272718
[email protected]022af742011-12-28 18:37:252719 return pending_entry_;
2720}
2721
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572722int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272723 // The pending entry index must always be less than the number of entries.
2724 // If there are no entries, it must be exactly -1.
2725 DCHECK_LT(pending_entry_index_, GetEntryCount());
2726 DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:552727 return pending_entry_index_;
2728}
2729
avi25764702015-06-23 15:43:372730void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572731 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362732 bool replace,
2733 bool was_post_commit_error) {
mastiz7eddb5f72016-06-23 09:52:452734 DCHECK(!ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2735 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202736
avi5cad4912015-06-19 05:25:442737 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2738 // need to keep continuity with the pending entry, so copy the pending entry's
2739 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2740 // then the renderer navigated on its own, independent of the pending entry,
2741 // so don't copy anything.
2742 if (pending_entry_ && pending_entry_index_ == -1)
2743 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202744
arthursonzogni69a6a1b2019-09-17 09:23:002745 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:202746
creisee17e932015-07-17 17:56:222747 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362748 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102749 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572750 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362751 // If the new entry is a post-commit error page, we store the current last
2752 // committed entry to the side so that we can put it back when navigating
2753 // away from the error.
2754 if (was_post_commit_error) {
2755 DCHECK(!entry_replaced_by_post_commit_error_);
2756 entry_replaced_by_post_commit_error_ =
2757 std::move(entries_[last_committed_entry_index_]);
2758 }
dcheng36b6aec92015-12-26 06:16:362759 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222760 return;
2761 }
[email protected]765b35502008-08-21 00:51:202762
creis37979a62015-08-04 19:48:182763 // We shouldn't see replace == true when there's no committed entries.
2764 DCHECK(!replace);
2765
Michael Thiessen9b14d512019-09-23 21:19:472766 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202767
Shivani Sharmad8c8d652019-02-13 17:27:572768 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202769
dcheng36b6aec92015-12-26 06:16:362770 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202771 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292772}
2773
Shivani Sharmad8c8d652019-02-13 17:27:572774void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162775 if (entries_.size() < max_entry_count())
2776 return;
2777
2778 DCHECK_EQ(max_entry_count(), entries_.size());
2779 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572780 CHECK_EQ(pending_entry_index_, -1);
2781
2782 int index = 0;
Elly Fong-Jonesccc6d1f2021-06-14 18:32:422783 // Retrieve the oldest skippable entry.
2784 for (; index < GetEntryCount(); index++) {
2785 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2786 break;
Shivani Sharmad8c8d652019-02-13 17:27:572787 }
2788
2789 // If there is no skippable entry or if it is the last committed entry then
2790 // fall back to pruning the oldest entry. It is not safe to prune the last
2791 // committed entry.
2792 if (index == GetEntryCount() || index == last_committed_entry_index_)
2793 index = 0;
2794
2795 bool should_succeed = RemoveEntryAtIndex(index);
2796 DCHECK_EQ(true, should_succeed);
2797
2798 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252799}
2800
clamy3cb9bea92018-07-10 12:42:022801void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162802 ReloadType reload_type,
Nate Chapin45f620582021-09-30 17:45:432803 int sandboxed_source_frame_tree_node_id,
2804 bool is_browser_initiated) {
Alexander Timin3a92df72019-09-20 11:59:502805 TRACE_EVENT0("navigation",
2806 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272807 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022808 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362809 if (pending_entry_index_ != -1) {
2810 // The pending entry may not be in entries_ if a post-commit error page is
2811 // showing.
2812 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2813 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2814 }
Gyuyoung Kim107c2a02021-04-13 01:49:302815 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572816 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:012817 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:002818 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:572819 int nav_entry_id = pending_entry_->GetUniqueID();
2820
[email protected]83c2e232011-10-07 21:36:462821 // If we were navigating to a slow-to-commit page, and the user performs
2822 // a session history navigation to the last committed page, RenderViewHost
2823 // will force the throbber to start, but WebKit will essentially ignore the
2824 // navigation, and won't send a message to stop the throbber. To prevent this
2825 // from happening, we drop the navigation here and stop the slow-to-commit
2826 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022827 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:082828 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:272829 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
Takashi Toyoshimabf549472021-07-01 07:47:202830 frame_tree_.StopLoading();
[email protected]6a13a6c2011-12-20 21:47:122831
[email protected]83c2e232011-10-07 21:36:462832 DiscardNonCommittedEntries();
2833 return;
2834 }
2835
creisce0ef3572017-01-26 17:53:082836 // Compare FrameNavigationEntries to see which frames in the tree need to be
2837 // navigated.
clamy3cb9bea92018-07-10 12:42:022838 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2839 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Nate Chapin45f620582021-09-30 17:45:432840 FindFramesToNavigate(root, reload_type, is_browser_initiated,
2841 &same_document_loads, &different_document_loads);
creis4e2ecb72015-06-20 00:46:302842
2843 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572844 // We were unable to match any frames to navigate. This can happen if a
2845 // history navigation targets a subframe that no longer exists
2846 // (https://crbug.com/705550). In this case, we need to update the current
2847 // history entry to the pending one but keep the main document loaded. We
2848 // also need to ensure that observers are informed about the updated
2849 // current history entry (e.g., for greying out back/forward buttons), and
2850 // that renderer processes update their history offsets. The easiest way
2851 // to do all that is to schedule a "redundant" same-document navigation in
2852 // the main frame.
2853 //
2854 // Note that we don't want to remove this history entry, as it might still
2855 // be valid later, since a frame that it's targeting may be recreated.
2856 //
2857 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
2858 // need to repeat history navigations until finding the one that works.
2859 // Consider changing this behavior to keep looking for the first valid
2860 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:022861 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422862 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572863 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:572864 ReloadType::NONE /* reload_type */,
2865 true /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432866 false /* is_history_navigation_in_new_child */,
2867 is_browser_initiated);
clamy3cb9bea92018-07-10 12:42:022868 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572869 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:022870 DiscardPendingEntry(false);
2871 return;
2872 }
Alex Moshchuk3a4e77a2020-05-29 21:32:572873 same_document_loads.push_back(std::move(navigation_request));
2874
2875 // Sanity check that we never take this branch for any kinds of reloads,
2876 // as those should've queued a different-document load in the main frame.
2877 DCHECK(!is_forced_reload);
2878 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:302879 }
2880
Dave Tapuska8bfd84c2019-03-26 20:47:162881 // If |sandboxed_source_frame_node_id| is valid, then track whether this
2882 // navigation affects any frame outside the frame's subtree.
2883 if (sandboxed_source_frame_tree_node_id !=
2884 FrameTreeNode::kFrameTreeNodeInvalidId) {
2885 bool navigates_inside_tree =
2886 DoesSandboxNavigationStayWithinSubtree(
2887 sandboxed_source_frame_tree_node_id, same_document_loads) &&
2888 DoesSandboxNavigationStayWithinSubtree(
2889 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:502890 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:162891 // the number of pages that trigger this.
2892 FrameTreeNode* sandbox_source_frame_tree_node =
2893 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
2894 if (sandbox_source_frame_tree_node) {
2895 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
2896 sandbox_source_frame_tree_node->current_frame_host(),
2897 navigates_inside_tree
2898 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
2899 : blink::mojom::WebFeature::
2900 kSandboxBackForwardAffectsFramesOutsideSubtree);
2901 }
Dave Tapuska855c1e12019-08-23 20:45:522902
2903 // If the navigation occurred outside the tree discard it because
2904 // the sandboxed frame didn't have permission to navigate outside
2905 // its tree. If it is possible that the navigation is both inside and
2906 // outside the frame tree and we discard it entirely because we don't
2907 // want to end up in a history state that didn't exist before.
2908 if (base::FeatureList::IsEnabled(
2909 features::kHistoryPreventSandboxedNavigation) &&
2910 !navigates_inside_tree) {
2911 DiscardPendingEntry(false);
2912 return;
2913 }
Dave Tapuska8bfd84c2019-03-26 20:47:162914 }
2915
Carlos Caballero539a421c2020-07-06 10:25:572916 // BackForwardCache:
2917 // Navigate immediately if the document is in the BackForwardCache.
2918 if (back_forward_cache_.GetEntry(nav_entry_id)) {
2919 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
2920 DCHECK_EQ(reload_type, ReloadType::NONE);
2921 auto navigation_request = CreateNavigationRequestFromEntry(
2922 root, pending_entry_, pending_entry_->GetFrameEntry(root),
2923 ReloadType::NONE, false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:432924 false /* is_history_navigation_in_new_child */, is_browser_initiated);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052925 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:572926
2927 return;
2928 }
2929
2930 // History navigation might try to reuse a specific BrowsingInstance, already
2931 // used by a page in the cache. To avoid having two different main frames that
2932 // live in the same BrowsingInstance, evict the all pages with this
2933 // BrowsingInstance from the cache.
2934 //
2935 // For example, take the following scenario:
2936 //
2937 // A1 = Some page on a.com
2938 // A2 = Some other page on a.com
2939 // B3 = An uncacheable page on b.com
2940 //
2941 // Then the following navigations occur:
2942 // A1->A2->B3->A1
2943 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
2944 // take its place) and A1 will be created in the same BrowsingInstance (and
2945 // SiteInstance), as A2.
2946 //
2947 // If we didn't do anything, both A1 and A2 would remain alive in the same
2948 // BrowsingInstance/SiteInstance, which is unsupported by
2949 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
2950 // A2 from the cache.
2951 if (pending_entry_->site_instance()) {
2952 back_forward_cache_.EvictFramesInRelatedSiteInstances(
2953 pending_entry_->site_instance());
2954 }
2955
clamy3cb9bea92018-07-10 12:42:022956 // This call does not support re-entrancy. See http://crbug.com/347742.
2957 CHECK(!in_navigate_to_pending_entry_);
2958 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:302959
arthursonzogni66f711c2019-10-08 14:40:362960 // It is not possible to delete the pending NavigationEntry while navigating
2961 // to it. Grab a reference to delay potential deletion until the end of this
2962 // function.
2963 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
2964
creis4e2ecb72015-06-20 00:46:302965 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:022966 for (auto& item : same_document_loads) {
2967 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052968 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302969 }
clamy3cb9bea92018-07-10 12:42:022970 for (auto& item : different_document_loads) {
2971 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052972 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302973 }
clamy3cb9bea92018-07-10 12:42:022974
2975 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:302976}
2977
Alex Moshchuk3a4e77a2020-05-29 21:32:572978NavigationControllerImpl::HistoryNavigationAction
2979NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:302980 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:572981 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:422982 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:582983 // Only active and prerendered documents are allowed to navigate in their
2984 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422985 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:012986 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:582987 // - If the document is in pending deletion, the browser already committed
2988 // to destroying this RenderFrameHost. See https://crbug.com/930278.
2989 // - If the document is in back-forward cache, it's not allowed to navigate
2990 // as it should remain frozen. Ignore the request and evict the document
2991 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422992 //
Sreeja Kamishettydb8e2892021-03-10 09:30:582993 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:422994 // which should all be inactive as well.
Fergal Daly1336ac642021-09-14 15:13:112995 if (frame->current_frame_host()->IsInactiveAndDisallowActivation(
2996 DisallowActivationReasonId::kDetermineActionForHistoryNavigation)) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:422997 return HistoryNavigationAction::kStopLooking;
Fergal Daly1336ac642021-09-14 15:13:112998 }
Sreeja Kamishetty8eacabb2021-03-09 11:45:422999 }
arthursonzogni03f76152019-02-12 10:35:203000
Alex Moshchuk3a4e77a2020-05-29 21:32:573001 // If there's no last committed entry, there is no previous history entry to
3002 // compare against, so fall back to a different-document load. Note that we
3003 // should only reach this case for the root frame and not descend further
3004 // into subframes.
3005 if (!GetLastCommittedEntry()) {
3006 DCHECK(frame->IsMainFrame());
3007 return HistoryNavigationAction::kDifferentDocument;
3008 }
3009
3010 // Reloads should result in a different-document load. Note that reloads may
3011 // also happen via the |needs_reload_| mechanism where the reload_type is
3012 // NONE, so detect this by comparing whether we're going to the same
3013 // entry that we're currently on. Similarly to above, only main frames
3014 // should reach this. Note that subframes support reloads, but that's done
3015 // via a different path that doesn't involve FindFramesToNavigate (see
3016 // RenderFrameHost::Reload()).
3017 if (reload_type != ReloadType::NONE ||
3018 pending_entry_index_ == last_committed_entry_index_) {
3019 DCHECK(frame->IsMainFrame());
3020 return HistoryNavigationAction::kDifferentDocument;
3021 }
3022
Alex Moshchuk47d1a4bd2020-06-01 22:15:343023 // If there is no new FrameNavigationEntry for the frame, ignore the
3024 // load. For example, this may happen when going back to an entry before a
3025 // frame was created. Suppose we commit a same-document navigation that also
3026 // results in adding a new subframe somewhere in the tree. If we go back,
3027 // the new subframe will be missing a FrameNavigationEntry in the previous
3028 // NavigationEntry, but we shouldn't delete or change what's loaded in
3029 // it.
3030 //
Alex Moshchuke65c39272020-06-03 17:55:373031 // Note that in this case, there is no need to keep looking for navigations
3032 // in subframes, which would be missing FrameNavigationEntries as well.
3033 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:343034 // It's important to check this before checking |old_item| below, since both
3035 // might be null, and in that case we still shouldn't change what's loaded in
3036 // this frame. Note that scheduling any loads assumes that |new_item| is
3037 // non-null. See https://crbug.com/1088354.
3038 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3039 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:373040 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:343041
Alex Moshchuk3a4e77a2020-05-29 21:32:573042 // If there is no old FrameNavigationEntry, schedule a different-document
3043 // load.
3044 //
creis225a7432016-06-03 22:56:273045 // TODO(creis): Store the last committed FrameNavigationEntry to use here,
3046 // rather than assuming the NavigationEntry has up to date info on subframes.
creis4e2ecb72015-06-20 00:46:303047 FrameNavigationEntry* old_item =
3048 GetLastCommittedEntry()->GetFrameEntry(frame);
Alex Moshchuk3a4e77a2020-05-29 21:32:573049 if (!old_item)
3050 return HistoryNavigationAction::kDifferentDocument;
3051
Alex Moshchuk3a4e77a2020-05-29 21:32:573052 // If the new item is not in the same SiteInstance, schedule a
3053 // different-document load. Newly restored items may not have a SiteInstance
3054 // yet, in which case it will be assigned on first commit.
3055 if (new_item->site_instance() &&
3056 new_item->site_instance() != old_item->site_instance())
3057 return HistoryNavigationAction::kDifferentDocument;
3058
3059 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:313060 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
3061 // that we do this even if the history navigation would not be modifying this
3062 // frame were it live.
3063 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:573064 return HistoryNavigationAction::kDifferentDocument;
3065
3066 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:313067 // Starting a navigation after a crash early-promotes the speculative
3068 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
3069 // committed yet, so we can not possibly perform a same-document history
3070 // navigation. The frame would need to be reloaded with a cross-document
3071 // navigation.
3072 if (!frame->current_frame_host()->has_committed_any_navigation())
3073 return HistoryNavigationAction::kDifferentDocument;
3074
creis54131692016-08-12 18:32:253075 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:493076 // sequence number but different item sequence number.
3077 if (new_item->document_sequence_number() ==
3078 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573079 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493080 }
avib48cb312016-05-05 21:35:003081
Alex Moshchuk3a4e77a2020-05-29 21:32:573082 // Otherwise, if both item and document sequence numbers differ, this
3083 // should be a different document load.
3084 return HistoryNavigationAction::kDifferentDocument;
3085 }
3086
3087 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373088 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573089 DCHECK_EQ(new_item->document_sequence_number(),
3090 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373091 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573092}
3093
3094void NavigationControllerImpl::FindFramesToNavigate(
3095 FrameTreeNode* frame,
3096 ReloadType reload_type,
Nate Chapin45f620582021-09-30 17:45:433097 bool is_browser_initiated,
Alex Moshchuk3a4e77a2020-05-29 21:32:573098 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3099 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3100 DCHECK(pending_entry_);
3101 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3102
3103 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3104
3105 if (action == HistoryNavigationAction::kSameDocument) {
3106 std::unique_ptr<NavigationRequest> navigation_request =
3107 CreateNavigationRequestFromEntry(
3108 frame, pending_entry_, new_item, reload_type,
3109 true /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433110 false /* is_history_navigation_in_new_child */,
3111 is_browser_initiated);
Alex Moshchuk3a4e77a2020-05-29 21:32:573112 if (navigation_request) {
3113 // Only add the request if was properly created. It's possible for the
3114 // creation to fail in certain cases, e.g. when the URL is invalid.
3115 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303116 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573117 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473118 std::unique_ptr<NavigationRequest> navigation_request =
3119 CreateNavigationRequestFromEntry(
3120 frame, pending_entry_, new_item, reload_type,
3121 false /* is_same_document_history_load */,
Nate Chapin45f620582021-09-30 17:45:433122 false /* is_history_navigation_in_new_child */,
3123 is_browser_initiated);
Lei Zhang96031532019-10-10 19:05:473124 if (navigation_request) {
3125 // Only add the request if was properly created. It's possible for the
3126 // creation to fail in certain cases, e.g. when the URL is invalid.
3127 different_document_loads->push_back(std::move(navigation_request));
3128 }
3129 // For a different document, the subframes will be destroyed, so there's
3130 // no need to consider them.
3131 return;
Alex Moshchuke65c39272020-06-03 17:55:373132 } else if (action == HistoryNavigationAction::kStopLooking) {
3133 return;
creis4e2ecb72015-06-20 00:46:303134 }
3135
3136 for (size_t i = 0; i < frame->child_count(); i++) {
Nate Chapin45f620582021-09-30 17:45:433137 FindFramesToNavigate(frame->child_at(i), reload_type, is_browser_initiated,
3138 same_document_loads, different_document_loads);
creis4e2ecb72015-06-20 00:46:303139 }
3140}
3141
Harkiran Bolariaba823e42021-05-21 18:30:363142base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry(
clamy21718cc22018-06-13 13:34:243143 const LoadURLParams& params) {
3144 // Find the appropriate FrameTreeNode.
3145 FrameTreeNode* node = nullptr;
3146 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3147 !params.frame_name.empty()) {
3148 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003149 ? frame_tree_.FindByID(params.frame_tree_node_id)
3150 : frame_tree_.FindByName(params.frame_name);
Hayato Ito7a80db42021-07-05 06:18:543151 DCHECK(!node || node->frame_tree() == &frame_tree_);
clamy21718cc22018-06-13 13:34:243152 }
3153
3154 // If no FrameTreeNode was specified, navigate the main frame.
3155 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003156 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243157
Camille Lamy5193caa2018-10-12 11:59:423158 // Compute overrides to the LoadURLParams for |override_user_agent|,
3159 // |should_replace_current_entry| and |has_user_gesture| that will be used
3160 // both in the creation of the NavigationEntry and the NavigationRequest.
3161 // Ideally, the LoadURLParams themselves would be updated, but since they are
3162 // passed as a const reference, this is not possible.
3163 // TODO(clamy): When we only create a NavigationRequest, move this to
3164 // CreateNavigationRequestFromLoadURLParams.
3165 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3166 GetLastCommittedEntry());
3167
3168 // Don't allow an entry replacement if there is no entry to replace.
3169 // http://crbug.com/457149
Hayato Ito7a80db42021-07-05 06:18:543170 //
3171 // If there is an entry, an entry replacement must happen if the current
3172 // browsing context should maintain a trivial session history.
3173 bool should_replace_current_entry = (params.should_replace_current_entry ||
3174 ShouldMaintainTrivialSessionHistory()) &&
3175 entries_.size();
Camille Lamy5193caa2018-10-12 11:59:423176
clamy21718cc22018-06-13 13:34:243177 // Javascript URLs should not create NavigationEntries. All other navigations
3178 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243179 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373180 std::unique_ptr<NavigationEntryImpl> entry =
3181 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3182 should_replace_current_entry,
3183 params.has_user_gesture);
clamy21718cc22018-06-13 13:34:243184 DiscardPendingEntry(false);
3185 SetPendingEntry(std::move(entry));
3186 }
3187
3188 // Renderer-debug URLs are sent to the renderer process immediately for
3189 // processing and don't need to create a NavigationRequest.
3190 // Note: this includes navigations to JavaScript URLs, which are considered
3191 // renderer-debug URLs.
3192 // Note: we intentionally leave the pending entry in place for renderer debug
3193 // URLs, unlike the cases below where we clear it if the navigation doesn't
3194 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303195 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483196 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3197 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153198 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483199 params.url, browser_context_)) {
3200 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363201 return nullptr;
Oleg Davydov2cc0167b2019-02-05 14:32:483202 }
3203
clamy21718cc22018-06-13 13:34:243204 HandleRendererDebugURL(node, params.url);
Harkiran Bolariaba823e42021-05-21 18:30:363205 return nullptr;
clamy21718cc22018-06-13 13:34:243206 }
3207
Antonio Sartori78a749f2020-11-30 12:03:393208 DCHECK(pending_entry_);
3209
clamy21718cc22018-06-13 13:34:243210 // Convert navigations to the current URL to a reload.
3211 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3212 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3213 // DevTools sometimes issues navigations to main frames that they do not
3214 // expect to see treated as reload, and it only works because they pass a
3215 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3216 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543217 ReloadType reload_type = params.reload_type;
3218 if (reload_type == ReloadType::NONE &&
3219 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043220 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243221 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243222 params.load_type ==
3223 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
Hayato Ito7a80db42021-07-05 06:18:543224 should_replace_current_entry, GetLastCommittedEntry())) {
clamy21718cc22018-06-13 13:34:243225 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473226 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393227
3228 // If this is a reload of an existing FrameNavigationEntry and we had a
3229 // policy container for it, then we should copy it into the pending entry,
3230 // so that it is copied to the navigation request in
3231 // CreateNavigationRequestFromLoadParams later.
3232 if (GetLastCommittedEntry()) {
3233 FrameNavigationEntry* previous_frame_entry =
3234 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193235 if (previous_frame_entry &&
3236 previous_frame_entry->policy_container_policies()) {
3237 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163238 previous_frame_entry->policy_container_policies()->Clone());
Antonio Sartori78a749f2020-11-30 12:03:393239 }
3240 }
3241 }
3242
3243 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
3244 // document (so no last commit), then we should copy the document polices from
3245 // RenderFrameHost's PolicyContainerHost. The NavigationRequest will create a
3246 // new PolicyContainerHost with the document policies from the
3247 // |pending_entry_|, and that PolicyContainerHost will be put in the final
3248 // RenderFrameHost for the navigation. This way, we ensure that we keep
3249 // enforcing the right policies on the initial empty document after the
3250 // reload.
3251 if (!GetLastCommittedEntry() && params.url.IsAboutBlank()) {
3252 if (node->current_frame_host() &&
3253 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193254 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163255 node->current_frame_host()
3256 ->policy_container_host()
3257 ->policies()
3258 .Clone());
Antonio Sartori78a749f2020-11-30 12:03:393259 }
clamy21718cc22018-06-13 13:34:243260 }
3261
3262 // navigation_ui_data should only be present for main frame navigations.
3263 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
3264
Camille Lamy5193caa2018-10-12 11:59:423265 std::unique_ptr<NavigationRequest> request =
3266 CreateNavigationRequestFromLoadParams(
3267 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143268 params.has_user_gesture, network::mojom::SourceLocation::New(),
Antonio Sartori6984c742021-08-26 08:03:413269 reload_type, pending_entry_, pending_entry_->GetFrameEntry(node));
clamy21718cc22018-06-13 13:34:243270
3271 // If the navigation couldn't start, return immediately and discard the
3272 // pending NavigationEntry.
3273 if (!request) {
3274 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363275 return nullptr;
clamy21718cc22018-06-13 13:34:243276 }
3277
Camille Lamy5193caa2018-10-12 11:59:423278#if DCHECK_IS_ON()
3279 // Safety check that NavigationRequest and NavigationEntry match.
3280 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3281#endif
3282
clamy21718cc22018-06-13 13:34:243283 // This call does not support re-entrancy. See http://crbug.com/347742.
3284 CHECK(!in_navigate_to_pending_entry_);
3285 in_navigate_to_pending_entry_ = true;
3286
arthursonzogni66f711c2019-10-08 14:40:363287 // It is not possible to delete the pending NavigationEntry while navigating
3288 // to it. Grab a reference to delay potential deletion until the end of this
3289 // function.
3290 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3291
Harkiran Bolariaba823e42021-05-21 18:30:363292 base::WeakPtr<NavigationHandle> created_navigation_handle(
3293 request->GetWeakPtr());
Lukasz Anforowicz9ee83c272020-12-01 20:14:053294 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243295
3296 in_navigate_to_pending_entry_ = false;
Harkiran Bolariaba823e42021-05-21 18:30:363297 return created_navigation_handle;
clamy21718cc22018-06-13 13:34:243298}
3299
clamyea99ea12018-05-28 13:54:233300void NavigationControllerImpl::HandleRendererDebugURL(
3301 FrameTreeNode* frame_tree_node,
3302 const GURL& url) {
3303 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243304 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3305 // the renderer process is not live, unless it is the initial navigation
3306 // of the tab.
clamyea99ea12018-05-28 13:54:233307 if (!IsInitialNavigation()) {
3308 DiscardNonCommittedEntries();
3309 return;
3310 }
Fergal Dalyecd3b0202020-06-25 01:57:373311 // The current frame is always a main frame. If IsInitialNavigation() is
3312 // true then there have been no navigations and any frames of this tab must
3313 // be in the same renderer process. If that has crashed then the only frame
3314 // that can be revived is the main frame.
3315 frame_tree_node->render_manager()
3316 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233317 }
3318 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3319}
3320
clamy21718cc22018-06-13 13:34:243321std::unique_ptr<NavigationEntryImpl>
3322NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3323 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423324 const LoadURLParams& params,
3325 bool override_user_agent,
3326 bool should_replace_current_entry,
3327 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393328 // Browser initiated navigations might not have a blob_url_loader_factory set
3329 // in params even if the navigation is to a blob URL. If that happens, lookup
3330 // the correct url loader factory to use here.
3331 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483332 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473333 // Resolve the blob URL in the storage partition associated with the target
3334 // frame. This is the storage partition the URL will be loaded in, and only
3335 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393336 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473337 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393338 }
3339
clamy21718cc22018-06-13 13:34:243340 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443341 // extra_headers in params are \n separated; navigation entries want \r\n.
3342 std::string extra_headers_crlf;
3343 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243344
3345 // For subframes, create a pending entry with a corresponding frame entry.
3346 if (!node->IsMainFrame()) {
Tommy C. Li03eee77a2019-02-05 02:07:443347 if (GetLastCommittedEntry()) {
Tommy C. Li03eee77a2019-02-05 02:07:443348 entry = GetLastCommittedEntry()->Clone();
3349 } else {
3350 // If there's no last committed entry, create an entry for about:blank
3351 // with a subframe entry for our destination.
3352 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
3353 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063354 GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103355 params.source_site_instance.get(), params.transition_type,
3356 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Hayato Ito303654c2021-06-30 09:07:543357 blob_url_loader_factory));
Tommy C. Li03eee77a2019-02-05 02:07:443358 }
Nasko Oskov18006bc2018-12-06 02:53:583359
clamy21718cc22018-06-13 13:34:243360 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:083361 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
clamy21718cc22018-06-13 13:34:243362 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Anton Bikineevf62d1bf2021-05-15 17:56:073363 params.url, absl::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203364 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393365 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:483366 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori78a749f2020-11-30 12:03:393367 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163368 // a conversion of a new navigation into a reload, we will set the right
3369 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193370 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243371 } else {
3372 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243373 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063374 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103375 params.source_site_instance.get(), params.transition_type,
3376 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Hayato Ito303654c2021-06-30 09:07:543377 blob_url_loader_factory));
clamy21718cc22018-06-13 13:34:243378 entry->set_source_site_instance(
3379 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3380 entry->SetRedirectChain(params.redirect_chain);
3381 }
3382
3383 // Set the FTN ID (only used in non-site-per-process, for tests).
3384 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243385 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423386 entry->SetIsOverridingUserAgent(override_user_agent);
3387 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543388 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243389
clamy21718cc22018-06-13 13:34:243390 switch (params.load_type) {
3391 case LOAD_TYPE_DEFAULT:
3392 break;
3393 case LOAD_TYPE_HTTP_POST:
3394 entry->SetHasPostData(true);
3395 entry->SetPostData(params.post_data);
3396 break;
3397 case LOAD_TYPE_DATA:
3398 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3399 entry->SetVirtualURL(params.virtual_url_for_data_url);
3400#if defined(OS_ANDROID)
3401 entry->SetDataURLAsString(params.data_url_as_string);
3402#endif
3403 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3404 break;
clamy21718cc22018-06-13 13:34:243405 }
3406
3407 // TODO(clamy): NavigationEntry is meant for information that will be kept
3408 // after the navigation ended and therefore is not appropriate for
3409 // started_from_context_menu. Move started_from_context_menu to
3410 // NavigationUIData.
3411 entry->set_started_from_context_menu(params.started_from_context_menu);
3412
3413 return entry;
3414}
3415
clamyea99ea12018-05-28 13:54:233416std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423417NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3418 FrameTreeNode* node,
3419 const LoadURLParams& params,
3420 bool override_user_agent,
3421 bool should_replace_current_entry,
3422 bool has_user_gesture,
Antonio Sartori2f763d9d2021-04-21 10:04:143423 network::mojom::SourceLocationPtr source_location,
Camille Lamy5193caa2018-10-12 11:59:423424 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573425 NavigationEntryImpl* entry,
Scott Violetcf6ea7e2021-06-09 21:09:213426 FrameNavigationEntry* frame_entry) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573427 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283428 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533429 // All renderer-initiated navigations must have an initiator_origin.
3430 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513431
Camille Lamy5193caa2018-10-12 11:59:423432 GURL url_to_load;
3433 GURL virtual_url;
Anton Bikineevf62d1bf2021-05-15 17:56:073434 absl::optional<url::Origin> origin_to_commit =
3435 frame_entry ? frame_entry->committed_origin() : absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:323436
Camille Lamy2baa8022018-10-19 16:43:173437 // For main frames, rewrite the URL if necessary and compute the virtual URL
3438 // that should be shown in the address bar.
3439 if (node->IsMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423440 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173441 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423442 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423443
Camille Lamy2baa8022018-10-19 16:43:173444 // For DATA loads, override the virtual URL.
3445 if (params.load_type == LOAD_TYPE_DATA)
3446 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423447
Camille Lamy2baa8022018-10-19 16:43:173448 if (virtual_url.is_empty())
3449 virtual_url = url_to_load;
3450
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573451 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283452
Aran Gilman249eb122019-12-02 23:32:463453 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3454 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3455 // return different results, leading to a different URL in the
3456 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3457 // pending NavigationEntry, as we'll only make one call to
3458 // RewriteUrlForNavigation.
3459 VLOG_IF(1, (url_to_load != frame_entry->url()))
3460 << "NavigationRequest and FrameEntry have different URLs: "
3461 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283462
Camille Lamy2baa8022018-10-19 16:43:173463 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423464 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173465 // NavigationRequest.
3466 } else {
3467 url_to_load = params.url;
3468 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243469 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173470 }
Camille Lamy5193caa2018-10-12 11:59:423471
Ehsan Karamad44fc72112019-02-26 18:15:473472 if (node->render_manager()->is_attaching_inner_delegate()) {
3473 // Avoid starting any new navigations since this node is now preparing for
3474 // attaching an inner delegate.
3475 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203476 }
Camille Lamy5193caa2018-10-12 11:59:423477
Camille Lamy5193caa2018-10-12 11:59:423478 if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load))
3479 return nullptr;
3480
Kunihiko Sakamoto346a74e2021-03-10 08:57:483481 if (!DoesURLMatchOriginForNavigation(
3482 url_to_load, origin_to_commit,
3483 frame_entry->subresource_web_bundle_navigation_info())) {
Nasko Oskov03912102019-01-11 00:21:323484 DCHECK(false) << " url:" << url_to_load
3485 << " origin:" << origin_to_commit.value();
3486 return nullptr;
3487 }
3488
Camille Lamy5193caa2018-10-12 11:59:423489 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403490 blink::PreviewsState previews_state =
3491 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
Camille Lamy5193caa2018-10-12 11:59:423492 if (!node->IsMainFrame()) {
3493 // For subframes, use the state of the top-level frame.
3494 previews_state = node->frame_tree()
3495 ->root()
3496 ->current_frame_host()
3497 ->last_navigation_previews_state();
3498 }
3499
Camille Lamy5193caa2018-10-12 11:59:423500 // This will be used to set the Navigation Timing API navigationStart
3501 // parameter for browser navigations in new tabs (intents, tabs opened through
3502 // "Open link in new tab"). If the navigation must wait on the current
3503 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3504 // will be updated when the BeforeUnload ack is received.
3505 base::TimeTicks navigation_start = base::TimeTicks::Now();
3506
danakjd83d706d2020-11-25 22:11:123507 // Look for a pending commit that is to another document in this
3508 // FrameTreeNode. If one exists, then the last committed URL will not be the
3509 // current URL by the time this navigation commits.
3510 bool has_pending_cross_document_commit =
3511 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
Miyoung Shina2dd6a42021-10-07 12:19:213512 bool is_currently_error_page = node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:123513
Minggang Wangb9f3fa92021-07-01 15:30:313514 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjb952ef12021-01-14 19:58:493515 /*old_url=*/node->current_url(),
3516 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3517 has_pending_cross_document_commit, is_currently_error_page,
3518 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423519
3520 // Create the NavigationParams based on |params|.
3521
Hiroki Nakagawa4ed61282021-06-18 05:37:233522 bool is_view_source_mode = entry->IsViewSourceMode();
3523 DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme));
Charlie Harrison8c113a32019-01-07 16:08:293524
Antonio Sartori6984c742021-08-26 08:03:413525 blink::NavigationDownloadPolicy download_policy = params.download_policy;
Hiroki Nakagawa4ed61282021-06-18 05:37:233526 // Update |download_policy| if the virtual URL is view-source.
Charlie Harrison8c113a32019-01-07 16:08:293527 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253528 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293529
Minggang Wangb9f3fa92021-07-01 15:30:313530 blink::mojom::CommonNavigationParamsPtr common_params =
3531 blink::mojom::CommonNavigationParams::New(
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513532 url_to_load, params.initiator_origin,
3533 blink::mojom::Referrer::New(params.referrer.url,
3534 params.referrer.policy),
Scott Violetcf6ea7e2021-06-09 21:09:213535 params.transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513536 should_replace_current_entry, params.base_url_for_data_url,
Rakina Zata Amni8a1c6b32021-09-29 06:41:293537 previews_state, navigation_start,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513538 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
Antonio Sartori2f763d9d2021-04-21 10:04:143539 params.post_data, std::move(source_location),
arthursonzogniaf7c62c52020-02-12 10:49:413540 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273541 false /* has_text_fragment_token */,
3542 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3543 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513544 false /* is_history_navigation_in_new_child_frame */,
Charlie Hu5ffc0152019-12-06 15:59:533545 params.input_start);
Camille Lamy5193caa2018-10-12 11:59:423546
Minggang Wangb9f3fa92021-07-01 15:30:313547 blink::mojom::CommitNavigationParamsPtr commit_params =
3548 blink::mojom::CommitNavigationParams::New(
Antonio Sartori3e8de6d2021-07-26 10:28:413549 frame_entry->committed_origin(),
3550 // The correct storage key will be computed before committing the
3551 // navigation.
3552 blink::StorageKey(), network::mojom::WebSandboxFlags(),
arthursonzognid5a8d0b2021-03-11 17:36:433553 override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323554 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143555 std::vector<net::RedirectInfo>(),
3556 std::string() /* post_content_type */, common_params->url,
3557 common_params->method, params.can_load_local_resources,
Minggang Wangb9f3fa92021-07-01 15:30:313558 frame_entry->page_state().ToEncodedData(), entry->GetUniqueID(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143559 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3560 -1 /* pending_history_list_offset */,
3561 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3562 params.should_clear_history_list ? 0 : GetEntryCount(),
3563 false /* was_discarded */, is_view_source_mode,
Minggang Wangb9f3fa92021-07-01 15:30:313564 params.should_clear_history_list,
3565 blink::mojom::NavigationTiming::New(),
Anton Bikineevf62d1bf2021-05-15 17:56:073566 absl::nullopt /* appcache_host_id */,
Minggang Wangf59db47b2021-06-16 01:56:223567 blink::mojom::WasActivatedOption::kUnknown,
Lucas Furukawa Gadania9c45682019-07-31 22:05:143568 base::UnguessableToken::Create() /* navigation_token */,
Minggang Wang7ee0c742021-06-16 16:16:513569 std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143570#if defined(OS_ANDROID)
3571 std::string(), /* data_url_as_string */
3572#endif
arthursonzogni14379782020-05-15 09:09:273573 !params.is_renderer_initiated, /* is_browser_initiated */
Tsuyoshi Horoe86d7702019-11-29 01:52:473574 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533575 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173576 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073577 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163578 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333579 false /* origin_agent_cluster */,
Maks Orlovichc66745a2020-06-30 17:40:023580 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213581 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Nate Chapind1fe3612021-04-16 20:45:573582 false /* is_cross_browsing_instance */, nullptr /* old_page_info */,
3583 -1 /* http_response_code */,
Minggang Wangb9f3fa92021-07-01 15:30:313584 std::vector<blink::mojom::
3585 AppHistoryEntryPtr>() /* app_history_back_entries */,
Nate Chapind1fe3612021-04-16 20:45:573586 std::vector<
Minggang Wangb9f3fa92021-07-01 15:30:313587 blink::mojom::
3588 AppHistoryEntryPtr>() /* app_history_forward_entries */,
Kenichi Ishibashi17ecfb62021-06-21 03:31:153589 std::vector<GURL>() /* early_hints_preloaded_resources */);
Camille Lamy5193caa2018-10-12 11:59:423590#if defined(OS_ANDROID)
3591 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143592 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423593 }
3594#endif
3595
Lucas Furukawa Gadania9c45682019-07-31 22:05:143596 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423597
3598 // A form submission may happen here if the navigation is a renderer-initiated
3599 // form submission that took the OpenURL path.
3600 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3601
3602 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3603 std::string extra_headers_crlf;
3604 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093605
3606 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143607 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083608 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453609 params.initiator_frame_token.has_value()
3610 ? &(params.initiator_frame_token.value())
3611 : nullptr,
3612 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3613 request_body,
John Delaney50425f82020-04-07 16:26:213614 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473615 params.impression, params.is_pdf);
Yao Xiaodc5ed102019-06-04 19:19:093616 navigation_request->set_from_download_cross_origin_redirect(
3617 params.from_download_cross_origin_redirect);
3618 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423619}
3620
3621std::unique_ptr<NavigationRequest>
3622NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233623 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573624 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233625 FrameNavigationEntry* frame_entry,
3626 ReloadType reload_type,
3627 bool is_same_document_history_load,
Nate Chapin45f620582021-09-30 17:45:433628 bool is_history_navigation_in_new_child_frame,
3629 bool is_browser_initiated) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343630 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233631 GURL dest_url = frame_entry->url();
Anton Bikineevf62d1bf2021-05-15 17:56:073632 absl::optional<url::Origin> origin_to_commit =
Nasko Oskov03912102019-01-11 00:21:323633 frame_entry->committed_origin();
3634
clamyea99ea12018-05-28 13:54:233635 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013636 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573637 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233638 // We may have been redirected when navigating to the current URL.
3639 // Use the URL the user originally intended to visit as signaled by the
3640 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013641 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573642 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233643 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323644 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233645 }
3646
Ehsan Karamad44fc72112019-02-26 18:15:473647 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3648 // Avoid starting any new navigations since this node is now preparing for
3649 // attaching an inner delegate.
3650 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203651 }
3652
Camille Lamy5193caa2018-10-12 11:59:423653 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573654 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233655 return nullptr;
3656 }
3657
Kunihiko Sakamoto346a74e2021-03-10 08:57:483658 if (!DoesURLMatchOriginForNavigation(
3659 dest_url, origin_to_commit,
3660 frame_entry->subresource_web_bundle_navigation_info())) {
3661 DCHECK(false) << " url:" << dest_url
3662 << " origin:" << origin_to_commit.value();
Nasko Oskov03912102019-01-11 00:21:323663 return nullptr;
3664 }
3665
clamyea99ea12018-05-28 13:54:233666 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403667 blink::PreviewsState previews_state =
3668 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
clamyea99ea12018-05-28 13:54:233669 if (!frame_tree_node->IsMainFrame()) {
3670 // For subframes, use the state of the top-level frame.
3671 previews_state = frame_tree_node->frame_tree()
3672 ->root()
3673 ->current_frame_host()
3674 ->last_navigation_previews_state();
clamyea99ea12018-05-28 13:54:233675 }
3676
clamyea99ea12018-05-28 13:54:233677 // This will be used to set the Navigation Timing API navigationStart
3678 // parameter for browser navigations in new tabs (intents, tabs opened through
3679 // "Open link in new tab"). If the navigation must wait on the current
3680 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3681 // will be updated when the BeforeUnload ack is received.
3682 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233683
danakjd83d706d2020-11-25 22:11:123684 // Look for a pending commit that is to another document in this
3685 // FrameTreeNode. If one exists, then the last committed URL will not be the
3686 // current URL by the time this navigation commits.
3687 bool has_pending_cross_document_commit =
3688 frame_tree_node->render_manager()
3689 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493690 bool is_currently_error_page =
Miyoung Shina2dd6a42021-10-07 12:19:213691 frame_tree_node->current_frame_host()->IsErrorDocument();
danakjd83d706d2020-11-25 22:11:123692
Minggang Wangb9f3fa92021-07-01 15:30:313693 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123694 /*old_url=*/frame_tree_node->current_url(),
3695 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493696 has_pending_cross_document_commit, is_currently_error_page,
3697 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423698
3699 // A form submission may happen here if the navigation is a
3700 // back/forward/reload navigation that does a form resubmission.
3701 scoped_refptr<network::ResourceRequestBody> request_body;
3702 std::string post_content_type;
3703 if (frame_entry->method() == "POST") {
3704 request_body = frame_entry->GetPostData(&post_content_type);
3705 // Might have a LF at end.
Peter Kastingb53b81912021-04-28 19:23:303706 post_content_type = std::string(
3707 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL));
Camille Lamy5193caa2018-10-12 11:59:423708 }
3709
3710 // Create the NavigationParams based on |entry| and |frame_entry|.
Minggang Wangb9f3fa92021-07-01 15:30:313711 blink::mojom::CommonNavigationParamsPtr common_params =
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513712 entry->ConstructCommonNavigationParams(
3713 *frame_entry, request_body, dest_url,
3714 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
3715 navigation_type, previews_state, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533716 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513717 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553718 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423719
3720 // TODO(clamy): |intended_as_new_entry| below should always be false once
3721 // Reload no longer leads to this being called for a pending NavigationEntry
3722 // of index -1.
Minggang Wangb9f3fa92021-07-01 15:30:313723 blink::mojom::CommitNavigationParamsPtr commit_params =
Lucas Furukawa Gadania9c45682019-07-31 22:05:143724 entry->ConstructCommitNavigationParams(
3725 *frame_entry, common_params->url, origin_to_commit,
3726 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3727 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533728 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3729 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143730 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423731
clamyea99ea12018-05-28 13:54:233732 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143733 frame_tree_node, std::move(common_params), std::move(commit_params),
Nate Chapin45f620582021-09-30 17:45:433734 is_browser_initiated, false /* was_opener_suppressed */,
Takashi Toyoshimae87b7be2021-01-22 11:51:083735 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453736 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3737 entry->extra_headers(), frame_entry, entry, request_body,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473738 nullptr /* navigation_ui_data */, absl::nullopt /* impression */,
3739 false /* is_pdf */);
clamyea99ea12018-05-28 13:54:233740}
3741
[email protected]d202a7c2012-01-04 07:53:473742void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213743 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273744 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403745
Takashi Toyoshimaea534ef22021-07-21 03:27:593746 // We need to notify the ssl_manager_ before the WebContents so the
[email protected]df1af242009-05-01 00:11:403747 // location bar will have up-to-date information about the security style
3748 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133749 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403750
[email protected]7f924832014-08-09 05:57:223751 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:573752 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463753
[email protected]b0f724c2013-09-05 04:21:133754 // TODO(avi): Remove. http://crbug.com/170921
3755 NotificationDetails notification_details =
3756 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153757 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3758 Source<NavigationController>(this),
3759 notification_details);
initial.commit09911bf2008-07-26 23:55:293760}
3761
initial.commit09911bf2008-07-26 23:55:293762// static
[email protected]d202a7c2012-01-04 07:53:473763size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233764 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153765 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:213766 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233767}
3768
[email protected]d202a7c2012-01-04 07:53:473769void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223770 if (is_active && needs_reload_)
3771 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293772}
3773
[email protected]d202a7c2012-01-04 07:53:473774void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293775 if (!needs_reload_)
3776 return;
3777
Bo Liucdfa4b12018-11-06 00:21:443778 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3779 needs_reload_type_);
3780
initial.commit09911bf2008-07-26 23:55:293781 // Calling Reload() results in ignoring state, and not loading.
3782 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3783 // cached state.
avicc872d7242015-08-19 21:26:343784 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163785 NavigateToExistingPendingEntry(ReloadType::NONE,
Nate Chapin45f620582021-09-30 17:45:433786 FrameTreeNode::kFrameTreeNodeInvalidId,
3787 true /* is_browser_initiated */);
avicc872d7242015-08-19 21:26:343788 } else if (last_committed_entry_index_ != -1) {
arthursonzogni5c4c202d2017-04-25 23:41:273789 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343790 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163791 NavigateToExistingPendingEntry(ReloadType::NONE,
Nate Chapin45f620582021-09-30 17:45:433792 FrameTreeNode::kFrameTreeNodeInvalidId,
3793 true /* is_browser_initiated */);
avicc872d7242015-08-19 21:26:343794 } else {
3795 // If there is something to reload, the successful reload will clear the
3796 // |needs_reload_| flag. Otherwise, just do it here.
3797 needs_reload_ = false;
3798 }
initial.commit09911bf2008-07-26 23:55:293799}
3800
Carlos IL42b416592019-10-07 23:10:363801void NavigationControllerImpl::LoadPostCommitErrorPage(
3802 RenderFrameHost* render_frame_host,
3803 const GURL& url,
3804 const std::string& error_page_html,
3805 net::Error error) {
Rakina Zata Amni919b7922020-12-11 09:03:133806 RenderFrameHostImpl* rfhi =
3807 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:583808
3809 // Only active documents can load post-commit error pages:
3810 // - If the document is in pending deletion, the browser already committed to
3811 // destroying this RenderFrameHost so ignore loading the error page.
3812 // - If the document is in back-forward cache, it's not allowed to navigate as
3813 // it should remain frozen. Ignore the request and evict the document from
3814 // back-forward cache.
3815 // - If the document is prerendering, it can navigate but when loading error
3816 // pages, cancel prerendering.
Fergal Daly1336ac642021-09-14 15:13:113817 if (rfhi->IsInactiveAndDisallowActivation(
3818 DisallowActivationReasonId::kLoadPostCommitErrorPage)) {
Sreeja Kamishettydb8e2892021-03-10 09:30:583819 return;
Fergal Daly1336ac642021-09-14 15:13:113820 }
Sreeja Kamishettydb8e2892021-03-10 09:30:583821
Rakina Zata Amni919b7922020-12-11 09:03:133822 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:413823
Minggang Wangb9f3fa92021-07-01 15:30:313824 blink::mojom::CommonNavigationParamsPtr common_params =
Minggang Wanga13c796e2021-07-02 05:54:433825 blink::CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:593826 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
3827 // the frame actually committed (e.g. iframe with "src" set to a
3828 // slow-responding URL). We should rewrite the URL to about:blank in this
3829 // case, as the renderer will only think a page is an error page if it has a
3830 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:133831 common_params->url = url.is_empty() ? GURL("about:blank") : url;
Minggang Wangb9f3fa92021-07-01 15:30:313832 blink::mojom::CommitNavigationParamsPtr commit_params =
Minggang Wanga13c796e2021-07-02 05:54:433833 blink::CreateCommitNavigationParams();
Antonio Sartori58591c892021-04-21 06:54:333834 commit_params->original_url = common_params->url;
John Delaney131ad362019-08-08 21:57:413835
arthursonzogni70ac7302020-05-28 08:49:053836 // Error pages have a fully permissive FramePolicy.
3837 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
3838 // error pages.
3839 commit_params->frame_policy = blink::FramePolicy();
3840
John Delaney131ad362019-08-08 21:57:413841 std::unique_ptr<NavigationRequest> navigation_request =
3842 NavigationRequest::CreateBrowserInitiated(
3843 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083844 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:193845 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453846 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:063847 "" /* extra_headers */, nullptr /* frame_entry */,
3848 nullptr /* entry */, nullptr /* post_body */,
Daniel Hosseinianf0fbfb42021-09-08 02:20:473849 nullptr /* navigation_ui_data */, absl::nullopt /* impression */,
3850 false /* is_pdf */);
Carlos IL42b416592019-10-07 23:10:363851 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:413852 navigation_request->set_net_error(error);
3853 node->CreatedNavigationRequest(std::move(navigation_request));
3854 DCHECK(node->navigation_request());
3855 node->navigation_request()->BeginNavigation();
3856}
3857
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573858void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213859 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093860 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:153861 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143862 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293863}
3864
[email protected]d202a7c2012-01-04 07:53:473865void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363866 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553867 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363868 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293869
initial.commit09911bf2008-07-26 23:55:293870 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293871}
[email protected]765b35502008-08-21 00:51:203872
arthursonzogni69a6a1b2019-09-17 09:23:003873void NavigationControllerImpl::DiscardNonCommittedEntries() {
Michael Thiessen9b14d512019-09-23 21:19:473874 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:103875 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
3876 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:293877 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:473878 return;
Michael Thiessenc5676d22019-09-25 22:32:103879 }
avi45a72532015-04-07 21:01:453880 DiscardPendingEntry(false);
arthursonzogni69a6a1b2019-09-17 09:23:003881 if (delegate_)
3882 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:483883}
3884
avi7c6f35e2015-05-08 17:52:383885int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3886 int nav_entry_id) const {
3887 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3888 if (entries_[i]->GetUniqueID() == nav_entry_id)
3889 return i;
3890 }
3891 return -1;
3892}
3893
[email protected]d202a7c2012-01-04 07:53:473894void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573895 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253896 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573897 DCHECK_LE(max_index, source->GetEntryCount());
Nate Chapin214a86a2021-06-21 20:35:573898 std::unique_ptr<NavigationEntryRestoreContextImpl> context =
3899 std::make_unique<NavigationEntryRestoreContextImpl>();
[email protected]e1cd5452010-08-26 18:03:253900 for (int i = 0; i < max_index; i++) {
Nate Chapin9f169072021-06-09 19:32:373901 // Normally, cloning a NavigationEntryImpl results in sharing
3902 // FrameNavigationEntries between the original and the clone. However, when
3903 // cloning from a different NavigationControllerImpl, we want to fork the
3904 // FrameNavigationEntries.
Nate Chapin9f169072021-06-09 19:32:373905 entries_.insert(entries_.begin() + i,
Nate Chapin214a86a2021-06-21 20:35:573906 source->entries_[i]->CloneWithoutSharing(context.get()));
[email protected]e1cd5452010-08-26 18:03:253907 }
arthursonzogni5c4c202d2017-04-25 23:41:273908 DCHECK(pending_entry_index_ == -1 ||
3909 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:253910}
[email protected]c5b88d82012-10-06 17:03:333911
3912void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:183913 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:333914 get_timestamp_callback_ = get_timestamp_callback;
3915}
[email protected]8ff00d72012-10-23 19:12:213916
Shivani Sharmaffb32b82019-04-09 16:58:473917// History manipulation intervention:
3918void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:473919 bool replace_entry,
3920 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:403921 bool is_renderer_initiated,
3922 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:453923 // Note that for a subframe, previous_document_was_activated is true if the
3924 // gesture happened in any subframe (propagated to main frame) or in the main
3925 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:473926 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:273927 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:473928 return;
3929 }
3930 if (last_committed_entry_index_ == -1)
3931 return;
3932
Shivani Sharmac4cc8922019-04-18 03:11:173933 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473934
Alexander Timine3ec4192020-04-20 16:39:403935 // Log UKM with the URL we are navigating away from.
3936 ukm::builders::HistoryManipulationIntervention(
3937 previous_page_load_ukm_source_id)
3938 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:473939}
3940
Shivani Sharmac4cc8922019-04-18 03:11:173941void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
3942 int reference_index,
3943 bool skippable) {
3944 auto* reference_entry = GetEntryAtIndex(reference_index);
3945 reference_entry->set_should_skip_on_back_forward_ui(skippable);
3946
3947 int64_t document_sequence_number =
3948 reference_entry->root_node()->frame_entry->document_sequence_number();
3949 for (int index = 0; index < GetEntryCount(); index++) {
3950 auto* entry = GetEntryAtIndex(index);
3951 if (entry->root_node()->frame_entry->document_sequence_number() ==
3952 document_sequence_number) {
3953 entry->set_should_skip_on_back_forward_ui(skippable);
3954 }
3955 }
3956}
3957
arthursonzogni66f711c2019-10-08 14:40:363958std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
3959NavigationControllerImpl::ReferencePendingEntry() {
3960 DCHECK(pending_entry_);
3961 auto pending_entry_ref =
3962 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
3963 pending_entry_refs_.insert(pending_entry_ref.get());
3964 return pending_entry_ref;
3965}
3966
3967void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
3968 // Ignore refs that don't correspond to the current pending entry.
3969 auto it = pending_entry_refs_.find(ref);
3970 if (it == pending_entry_refs_.end())
3971 return;
3972 pending_entry_refs_.erase(it);
3973
3974 if (!pending_entry_refs_.empty())
3975 return;
3976
3977 // The pending entry may be deleted before the last PendingEntryRef.
3978 if (!pending_entry_)
3979 return;
3980
3981 // We usually clear the pending entry when the matching NavigationRequest
3982 // fails, so that an arbitrary URL isn't left visible above a committed page.
3983 //
3984 // However, we do preserve the pending entry in some cases, such as on the
3985 // initial navigation of an unmodified blank tab. We also allow the delegate
3986 // to say when it's safe to leave aborted URLs in the omnibox, to let the
3987 // user edit the URL and try again. This may be useful in cases that the
3988 // committed page cannot be attacker-controlled. In these cases, we still
3989 // allow the view to clear the pending entry and typed URL if the user
3990 // requests (e.g., hitting Escape with focus in the address bar).
3991 //
3992 // Do not leave the pending entry visible if it has an invalid URL, since this
3993 // might be formatted in an unexpected or unsafe way.
3994 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:363995 bool should_preserve_entry =
3996 (pending_entry_ == GetVisibleEntry()) &&
3997 pending_entry_->GetURL().is_valid() &&
3998 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
3999 if (should_preserve_entry)
4000 return;
4001
4002 DiscardPendingEntry(true);
4003 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
4004}
4005
Titouan Rigoudy6ec70402021-02-02 15:42:194006std::unique_ptr<PolicyContainerPolicies>
4007NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:394008 RenderFrameHostImpl* rfh,
4009 bool is_same_document,
4010 NavigationRequest* request) {
Titouan Rigoudy6ec70402021-02-02 15:42:194011 if (!ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(request))
Antonio Sartori78a749f2020-11-30 12:03:394012 return nullptr;
4013
4014 if (is_same_document) {
Charlie Reis73e356242021-04-02 17:10:314015 // TODO(https://crbug.com/524208): Remove this nullptr check when we can
4016 // ensure we always have a FrameNavigationEntry here.
4017 if (!GetLastCommittedEntry())
4018 return nullptr;
4019
Antonio Sartori78a749f2020-11-30 12:03:394020 FrameNavigationEntry* previous_frame_entry =
4021 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
4022
4023 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
4024 // ensure we always have a FrameNavigationEntry here.
4025 if (!previous_frame_entry)
4026 return nullptr;
4027
Titouan Rigoudy6ec70402021-02-02 15:42:194028 const PolicyContainerPolicies* previous_policies =
4029 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:394030
Titouan Rigoudy6ec70402021-02-02 15:42:194031 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:394032 return nullptr;
4033
4034 // Make a copy of the policy container for the new FrameNavigationEntry.
Antonio Sartori5d09b30f2021-03-02 09:27:164035 return previous_policies->Clone();
Antonio Sartori78a749f2020-11-30 12:03:394036 }
4037
Antonio Sartori4f5373792021-05-31 10:56:474038 return rfh->policy_container_host()->policies().Clone();
Antonio Sartori78a749f2020-11-30 12:03:394039}
4040
Hayato Ito2c8c08d02021-06-23 03:38:434041void NavigationControllerImpl::BroadcastHistoryOffsetAndLength() {
Carlos Caballeroede6f8c2021-01-28 11:01:504042 OPTIONAL_TRACE_EVENT2(
Hayato Ito2c8c08d02021-06-23 03:38:434043 "content", "NavigationControllerImpl::BroadcastHistoryOffsetAndLength",
4044 "history_offset", GetLastCommittedEntryIndex(), "history_length",
4045 GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:504046
4047 auto callback = base::BindRepeating(
4048 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
4049 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
4050 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
4051 }
4052 },
Hayato Ito2c8c08d02021-06-23 03:38:434053 GetLastCommittedEntryIndex(), GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:504054 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
4055}
4056
4057void NavigationControllerImpl::DidAccessInitialMainDocument() {
4058 // We may have left a failed browser-initiated navigation in the address bar
4059 // to let the user edit it and try again. Clear it now that content might
4060 // show up underneath it.
4061 if (!frame_tree_.IsLoading() && GetPendingEntry())
4062 DiscardPendingEntry(false);
4063
4064 // Update the URL display.
4065 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
4066}
4067
4068void NavigationControllerImpl::UpdateStateForFrame(
4069 RenderFrameHostImpl* rfhi,
4070 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:564071 OPTIONAL_TRACE_EVENT1("content",
4072 "NavigationControllerImpl::UpdateStateForFrame",
4073 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:504074 // The state update affects the last NavigationEntry associated with the given
4075 // |render_frame_host|. This may not be the last committed NavigationEntry (as
4076 // in the case of an UpdateState from a frame being swapped out). We track
4077 // which entry this is in the RenderFrameHost's nav_entry_id.
4078 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
4079 if (!entry)
4080 return;
4081
4082 FrameNavigationEntry* frame_entry =
4083 entry->GetFrameEntry(rfhi->frame_tree_node());
4084 if (!frame_entry)
4085 return;
4086
4087 // The SiteInstance might not match if we do a cross-process navigation with
4088 // replacement (e.g., auto-subframe), in which case the swap out of the old
4089 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4090 // has already been replaced and destroyed.
4091 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4092 return;
4093
4094 if (page_state == frame_entry->page_state())
4095 return; // Nothing to update.
4096
4097 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4098
4099 // The document_sequence_number and item_sequence_number recorded in the
4100 // FrameNavigationEntry should not differ from the one coming with the update,
4101 // since it must come from the same document. Do not update it if a difference
4102 // is detected, as this indicates that |frame_entry| is not the correct one.
4103 blink::ExplodedPageState exploded_state;
4104 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4105 return;
4106
4107 if (exploded_state.top.document_sequence_number !=
4108 frame_entry->document_sequence_number() ||
4109 exploded_state.top.item_sequence_number !=
4110 frame_entry->item_sequence_number()) {
4111 return;
4112 }
4113
4114 frame_entry->SetPageState(page_state);
4115 NotifyEntryChanged(entry);
4116}
4117
Aaron Colwellb731a0ae2021-03-19 19:14:474118void NavigationControllerImpl::OnStoragePartitionIdAdded(
4119 const StoragePartitionId& partition_id) {
4120 auto it = partition_config_to_id_map_.insert(
4121 std::make_pair(partition_id.config(), partition_id));
4122 bool successful_insert = it.second;
Aaron Colwellb731a0ae2021-03-19 19:14:474123 if (!successful_insert) {
4124 LogStoragePartitionIdCrashKeys(it.first->second, partition_id);
4125 }
Sharon Yangd4f48792021-06-30 17:02:534126 CHECK(successful_insert);
Aaron Colwellb731a0ae2021-03-19 19:14:474127}
4128
4129void NavigationControllerImpl::LogStoragePartitionIdCrashKeys(
4130 const StoragePartitionId& original_partition_id,
4131 const StoragePartitionId& new_partition_id) {
4132 base::debug::SetCrashKeyString(
4133 base::debug::AllocateCrashKeyString("original_partition_id",
4134 base::debug::CrashKeySize::Size256),
4135 original_partition_id.ToString());
4136
4137 base::debug::SetCrashKeyString(
4138 base::debug::AllocateCrashKeyString("new_partition_id",
4139 base::debug::CrashKeySize::Size256),
4140 new_partition_id.ToString());
Aaron Colwellb731a0ae2021-03-19 19:14:474141}
4142
Minggang Wangb9f3fa92021-07-01 15:30:314143std::vector<blink::mojom::AppHistoryEntryPtr>
Nate Chapind1fe3612021-04-16 20:45:574144NavigationControllerImpl::PopulateSingleAppHistoryEntryVector(
4145 Direction direction,
4146 int entry_index,
4147 const url::Origin& pending_origin,
4148 FrameTreeNode* node,
4149 SiteInstance* site_instance,
4150 int64_t previous_item_sequence_number) {
Minggang Wangb9f3fa92021-07-01 15:30:314151 std::vector<blink::mojom::AppHistoryEntryPtr> entries;
Nate Chapind1fe3612021-04-16 20:45:574152 int offset = direction == Direction::kForward ? 1 : -1;
4153 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4154 i += offset) {
4155 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
4156 if (!frame_entry || !frame_entry->committed_origin())
4157 break;
4158 if (site_instance != frame_entry->site_instance())
4159 break;
4160 if (!pending_origin.IsSameOriginWith(*frame_entry->committed_origin()))
4161 break;
4162 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4163 continue;
4164 blink::ExplodedPageState exploded_page_state;
4165 if (blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4166 &exploded_page_state)) {
4167 blink::ExplodedFrameState frame_state = exploded_page_state.top;
Minggang Wangb9f3fa92021-07-01 15:30:314168 blink::mojom::AppHistoryEntryPtr entry =
4169 blink::mojom::AppHistoryEntry::New(
4170 frame_state.app_history_key.value_or(std::u16string()),
4171 frame_state.app_history_id.value_or(std::u16string()),
4172 frame_state.url_string.value_or(std::u16string()));
Nate Chapind1fe3612021-04-16 20:45:574173 DCHECK(pending_origin.CanBeDerivedFrom(GURL(entry->url)));
4174 entries.push_back(std::move(entry));
4175 previous_item_sequence_number = frame_entry->item_sequence_number();
4176 }
4177 }
4178 // If |entries| was constructed by iterating backwards from
4179 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4180 // earliest-at-the-front. Reverse it.
4181 if (direction == Direction::kBack)
4182 std::reverse(entries.begin(), entries.end());
4183 return entries;
4184}
4185
4186void NavigationControllerImpl::PopulateAppHistoryEntryVectors(
4187 NavigationRequest* request) {
4188 url::Origin pending_origin =
4189 request->commit_params().origin_to_commit
4190 ? *request->commit_params().origin_to_commit
4191 : url::Origin::Create(request->common_params().url);
4192
4193 FrameTreeNode* node = request->frame_tree_node();
4194 scoped_refptr<SiteInstance> site_instance =
4195 request->GetRenderFrameHost()->GetSiteInstance();
4196
4197 // NOTE: |entry_index| is an estimate of the index where this entry will
4198 // commit, but it may be wrong in corner cases (e.g., if we are at the max
4199 // entry limit, the earliest entry will be dropped). This is ok because this
4200 // algorithm only uses |entry_index| to walk the entry list as it stands right
4201 // now, and it isn't saved for anything post-commit.
4202 int entry_index = GetPendingEntryIndex();
4203 bool will_create_new_entry = false;
4204 if (NavigationTypeUtils::IsReload(request->common_params().navigation_type) ||
4205 request->common_params().should_replace_current_entry) {
4206 entry_index = GetLastCommittedEntryIndex();
4207 } else if (entry_index == -1) {
4208 will_create_new_entry = true;
4209 entry_index = GetLastCommittedEntryIndex() + 1;
4210 }
4211
4212 int64_t pending_item_sequence_number = 0;
4213 if (auto* pending_entry = GetPendingEntry()) {
4214 if (auto* frame_entry = pending_entry->GetFrameEntry(node))
4215 pending_item_sequence_number = frame_entry->item_sequence_number();
4216 }
4217
4218 request->set_app_history_back_entries(PopulateSingleAppHistoryEntryVector(
4219 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
4220 pending_item_sequence_number));
4221
4222 // Don't populate forward entries if they will be truncated by a new entry.
4223 if (!will_create_new_entry) {
4224 request->set_app_history_forward_entries(
4225 PopulateSingleAppHistoryEntryVector(
4226 Direction::kForward, entry_index, pending_origin, node,
4227 site_instance.get(), pending_item_sequence_number));
4228 }
4229}
4230
Nate Chapinfbfe5af2021-06-10 17:22:084231NavigationControllerImpl::HistoryNavigationAction
4232NavigationControllerImpl::ShouldNavigateToEntryForAppHistoryKey(
4233 FrameNavigationEntry* current_entry,
4234 FrameNavigationEntry* target_entry,
4235 const std::string& app_history_key) {
4236 if (!target_entry || !target_entry->committed_origin())
4237 return HistoryNavigationAction::kStopLooking;
4238 if (current_entry->site_instance() != target_entry->site_instance())
4239 return HistoryNavigationAction::kStopLooking;
4240 if (!current_entry->committed_origin()->IsSameOriginWith(
4241 *target_entry->committed_origin())) {
4242 return HistoryNavigationAction::kStopLooking;
4243 }
4244
4245 // NOTE: We don't actually care between kSameDocument and
4246 // kDifferentDocument, so always use kDifferentDocument by convention.
4247 if (target_entry->app_history_key() == app_history_key)
4248 return HistoryNavigationAction::kDifferentDocument;
4249 return HistoryNavigationAction::kKeepLooking;
4250}
4251
4252void NavigationControllerImpl::NavigateToAppHistoryKey(FrameTreeNode* node,
4253 const std::string& key) {
4254 FrameNavigationEntry* current_entry =
4255 GetLastCommittedEntry()->GetFrameEntry(node);
4256 if (!current_entry)
4257 return;
4258
4259 // We want to find the nearest matching entry that is contiguously
4260 // same-instance and same-origin. Check back first, then forward.
4261 // TODO(japhet): Link spec here once it exists.
4262 for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) {
4263 auto result = ShouldNavigateToEntryForAppHistoryKey(
4264 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4265 if (result == HistoryNavigationAction::kStopLooking)
4266 break;
4267 if (result != HistoryNavigationAction::kKeepLooking) {
Nate Chapin2892b37c2021-10-08 17:33:114268 GoToIndex(i, FrameTreeNode::kFrameTreeNodeInvalidId,
4269 false /* is_browser_initiated*/);
Nate Chapinfbfe5af2021-06-10 17:22:084270 return;
4271 }
4272 }
4273 for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) {
4274 auto result = ShouldNavigateToEntryForAppHistoryKey(
4275 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4276 if (result == HistoryNavigationAction::kStopLooking)
4277 break;
4278 if (result != HistoryNavigationAction::kKeepLooking) {
Nate Chapin2892b37c2021-10-08 17:33:114279 GoToIndex(i, FrameTreeNode::kFrameTreeNodeInvalidId,
4280 false /* is_browser_initiated*/);
Nate Chapinfbfe5af2021-06-10 17:22:084281 return;
4282 }
4283 }
4284}
4285
Hayato Ito7a80db42021-07-05 06:18:544286bool NavigationControllerImpl::ShouldMaintainTrivialSessionHistory() const {
4287 // TODO(https://crbug.com/1197384): We may have to add portals and fenced
4288 // frames in addition to prerender. This should be kept in sync with
4289 // LocalFrame version, LocalFrame::ShouldMaintainTrivialSessionHistory.
4290 return frame_tree_.is_prerendering();
4291}
4292
[email protected]8ff00d72012-10-23 19:12:214293} // namespace content