blob: f4caae060099ac98e96b8f30408ecf9244540378 [file] [log] [blame]
[email protected]d4a8ca482013-10-30 21:06:401// Copyright 2013 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
creis4e2ecb72015-06-20 00:46:305/*
6 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
7 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved.
9 * (http://www.torchmobile.com/)
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
21 * its contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
25 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
28 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
danakjc492bf82020-09-09 20:02:4436#include "content/browser/renderer_host/navigation_controller_impl.h"
initial.commit09911bf2008-07-26 23:55:2937
Lei Zhang96031532019-10-10 19:05:4738#include <algorithm>
dcheng36b6aec92015-12-26 06:16:3639#include <utility>
40
[email protected]c5b88d82012-10-06 17:03:3341#include "base/bind.h"
[email protected]eabfe1912014-05-12 10:07:2842#include "base/command_line.h"
initial.commit09911bf2008-07-26 23:55:2943#include "base/logging.h"
asvitkine30330812016-08-30 04:01:0844#include "base/metrics/histogram_macros.h"
Chris Hamilton83272dc2021-02-23 00:24:0245#include "base/stl_util.h"
Peter Kastingb53b81912021-04-28 19:23:3046#include "base/strings/string_piece.h"
[email protected]348fbaac2013-06-11 06:31:5147#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0048#include "base/strings/utf_string_conversions.h"
[email protected]a43858f2013-06-28 15:18:3749#include "base/time/time.h"
Carlos Caballero40b0efd2021-01-26 11:55:0050#include "base/trace_event/optional_trace_event.h"
Carlos Caballeroede6f8c2021-01-28 11:01:5051#include "base/trace_event/trace_conversion_helper.h"
ssid3e765612015-01-28 04:03:4252#include "base/trace_event/trace_event.h"
servolkf3955532015-05-16 00:01:5953#include "build/build_config.h"
[email protected]eabfe1912014-05-12 10:07:2854#include "cc/base/switches.h"
jamescookda2505812015-03-20 18:01:1855#include "content/browser/bad_message.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3956#include "content/browser/blob_storage/chrome_blob_storage_context.h"
[email protected]825b1662012-03-12 19:07:3157#include "content/browser/browser_url_handler_impl.h"
[email protected]5f2aa722013-08-07 16:59:4158#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0459#include "content/browser/dom_storage/session_storage_namespace_impl.h"
danakjc492bf82020-09-09 20:02:4460#include "content/browser/renderer_host/debug_urls.h"
61#include "content/browser/renderer_host/frame_tree.h"
Carlos Caballero40b0efd2021-01-26 11:55:0062#include "content/browser/renderer_host/frame_tree_node.h"
danakjc492bf82020-09-09 20:02:4463#include "content/browser/renderer_host/navigation_entry_impl.h"
Nate Chapin214a86a2021-06-21 20:35:5764#include "content/browser/renderer_host/navigation_entry_restore_context_impl.h"
danakjc492bf82020-09-09 20:02:4465#include "content/browser/renderer_host/navigation_request.h"
66#include "content/browser/renderer_host/navigator.h"
67#include "content/browser/renderer_host/render_frame_host_delegate.h"
Carlos Caballero40b0efd2021-01-26 11:55:0068#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]b6583592012-01-25 19:52:3369#include "content/browser/site_instance_impl.h"
Kunihiko Sakamoto346a74e2021-03-10 08:57:4870#include "content/browser/web_package/subresource_web_bundle_navigation_info.h"
Tsuyoshi Horo37493af2019-11-21 23:43:1471#include "content/browser/web_package/web_bundle_navigation_info.h"
Camille Lamy5193caa2018-10-12 11:59:4272#include "content/common/content_constants_internal.h"
Nate Chapind1fe3612021-04-16 20:45:5773#include "content/common/navigation_params_utils.h"
Nasko Oskovae49e292020-08-13 02:08:5174#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1175#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1976#include "content/public/browser/content_browser_client.h"
[email protected]d9083482012-01-06 00:38:4677#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0078#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3879#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1680#include "content/public/browser/notification_types.h"
Lei Zhang96031532019-10-10 19:05:4781#include "content/public/browser/render_view_host.h"
[email protected]9677a3c2012-12-22 04:18:5882#include "content/public/browser/render_widget_host.h"
83#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1084#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3485#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:1986#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:3887#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:1088#include "content/public/common/content_features.h"
Lei Zhang96031532019-10-10 19:05:4789#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4390#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:5991#include "media/base/mime_util.h"
[email protected]a23de8572009-06-03 02:16:3292#include "net/base/escape.h"
Arthur Sonzogni620cec62018-12-13 13:08:5793#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:1894#include "services/metrics/public/cpp/ukm_builders.h"
95#include "services/metrics/public/cpp/ukm_recorder.h"
Yue Ru Sun128804932020-09-30 22:19:1796#include "services/metrics/public/cpp/ukm_source_id.h"
[email protected]9677a3c2012-12-22 04:18:5897#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3998#include "third_party/blink/public/common/blob/blob_utils.h"
Gyuyoung Kim107c2a02021-04-13 01:49:3099#include "third_party/blink/public/common/chrome_debug_urls.h"
Miyoung Shin1c565c912021-03-17 12:11:21100#include "third_party/blink/public/common/history/session_history_constants.h"
Blink Reformata30d4232018-04-07 15:31:06101#include "third_party/blink/public/common/mime_util/mime_util.h"
Carlos Caballeroede6f8c2021-01-28 11:01:50102#include "third_party/blink/public/common/page_state/page_state_serialization.h"
Minggang Wangb9f3fa92021-07-01 15:30:31103#include "third_party/blink/public/mojom/navigation/navigation_params.mojom.h"
Minggang Wang7ee0c742021-06-16 16:16:51104#include "third_party/blink/public/mojom/navigation/prefetched_signed_exchange_info.mojom.h"
[email protected]cca6f392014-05-28 21:32:26105#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:29106
[email protected]8ff00d72012-10-23 19:12:21107namespace content {
[email protected]e9ba4472008-09-14 15:42:43108namespace {
109
110// Invoked when entries have been pruned, or removed. For example, if the
111// current entries are [google, digg, yahoo], with the current entry google,
112// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47113void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50114 int index,
[email protected]c12bf1a12008-09-17 16:28:49115 int count) {
[email protected]8ff00d72012-10-23 19:12:21116 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50117 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49118 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14119 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43120}
121
[email protected]e9ba4472008-09-14 15:42:43122// Configure all the NavigationEntries in entries for restore. This resets
123// the transition type to reload and makes sure the content state isn't empty.
124void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57125 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48126 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47127 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43128 // Use a transition type of reload so that we don't incorrectly increase
129 // the typed count.
Lei Zhang96031532019-10-10 19:05:47130 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
131 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43132 }
133}
134
[email protected]bf70edce2012-06-20 22:32:22135// Determines whether or not we should be carrying over a user agent override
136// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57137bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22138 return last_entry && last_entry->GetIsOverridingUserAgent();
139}
140
Camille Lamy5193caa2018-10-12 11:59:42141// Determines whether to override user agent for a navigation.
142bool ShouldOverrideUserAgent(
143 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57144 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42145 switch (override_user_agent) {
146 case NavigationController::UA_OVERRIDE_INHERIT:
147 return ShouldKeepOverride(last_committed_entry);
148 case NavigationController::UA_OVERRIDE_TRUE:
149 return true;
150 case NavigationController::UA_OVERRIDE_FALSE:
151 return false;
Camille Lamy5193caa2018-10-12 11:59:42152 }
153 NOTREACHED();
154 return false;
155}
156
Rakina Zata Amni312822d72021-06-04 16:13:37157// Returns true if this navigation should be treated as a reload. For e.g.
clamy0a656e42018-02-06 18:18:28158// navigating to the last committed url via the address bar or clicking on a
Rakina Zata Amni312822d72021-06-04 16:13:37159// link which results in a navigation to the last committed URL (but wasn't
160// converted to do a replacement navigation in the renderer), etc.
Fergal Daly766177d2020-07-07 07:54:04161// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
162// |base_url_for_data_url|, |transition_type| correspond to the new navigation
163// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
164// navigation that committed.
165bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
166 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57167 const GURL& virtual_url,
168 const GURL& base_url_for_data_url,
169 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57170 bool is_post,
Rakina Zata Amni312822d72021-06-04 16:13:37171 bool should_replace_current_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57172 NavigationEntryImpl* last_committed_entry) {
Rakina Zata Amni312822d72021-06-04 16:13:37173 // Navigations intended to do a replacement shouldn't be converted to do a
174 // reload.
175 if (should_replace_current_entry)
clamy0a656e42018-02-06 18:18:28176 return false;
clamy0a656e42018-02-06 18:18:28177 // Only convert to reload if at least one navigation committed.
178 if (!last_committed_entry)
ananta3bdd8ae2016-12-22 17:11:55179 return false;
180
arthursonzogni7a8243682017-12-14 16:41:42181 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28182 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42183 return false;
184
ananta3bdd8ae2016-12-22 17:11:55185 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
186 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
187 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28188 bool transition_type_can_be_converted = false;
189 if (ui::PageTransitionCoreTypeIs(transition_type,
190 ui::PAGE_TRANSITION_RELOAD) &&
191 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
192 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55193 }
clamy0a656e42018-02-06 18:18:28194 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55195 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28196 transition_type_can_be_converted = true;
197 }
198 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
199 transition_type_can_be_converted = true;
200 if (!transition_type_can_be_converted)
201 return false;
202
203 // This check is required for cases like view-source:, etc. Here the URL of
204 // the navigation entry would contain the url of the page, while the virtual
205 // URL contains the full URL including the view-source prefix.
206 if (virtual_url != last_committed_entry->GetVirtualURL())
207 return false;
208
Fergal Daly766177d2020-07-07 07:54:04209 // Check that the URLs match.
210 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
211 // If there's no frame entry then by definition the URLs don't match.
212 if (!frame_entry)
213 return false;
214
215 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28216 return false;
217
218 // This check is required for Android WebView loadDataWithBaseURL. Apps
219 // can pass in anything in the base URL and we need to ensure that these
220 // match before classifying it as a reload.
221 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
222 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
223 return false;
ananta3bdd8ae2016-12-22 17:11:55224 }
225
clamy0a656e42018-02-06 18:18:28226 // Skip entries with SSL errors.
227 if (last_committed_entry->ssl_error())
228 return false;
229
230 // Don't convert to a reload when the last navigation was a POST or the new
231 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04232 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28233 return false;
234
235 return true;
ananta3bdd8ae2016-12-22 17:11:55236}
237
Nasko Oskov03912102019-01-11 00:21:32238bool DoesURLMatchOriginForNavigation(
239 const GURL& url,
Anton Bikineevf62d1bf2021-05-15 17:56:07240 const absl::optional<url::Origin>& origin,
Kunihiko Sakamoto346a74e2021-03-10 08:57:48241 SubresourceWebBundleNavigationInfo*
242 subresource_web_bundle_navigation_info) {
Nasko Oskov03912102019-01-11 00:21:32243 // If there is no origin supplied there is nothing to match. This can happen
244 // for navigations to a pending entry and therefore it should be allowed.
245 if (!origin)
246 return true;
247
Kunihiko Sakamoto346a74e2021-03-10 08:57:48248 if (url.SchemeIs(url::kUrnScheme) && subresource_web_bundle_navigation_info) {
249 // Urn: subframe from WebBundle has an opaque origin derived from the
250 // Bundle's origin.
251 return origin->CanBeDerivedFrom(
252 subresource_web_bundle_navigation_info->bundle_url());
253 }
254
Nasko Oskov03912102019-01-11 00:21:32255 return origin->CanBeDerivedFrom(url);
256}
257
Anton Bikineevf62d1bf2021-05-15 17:56:07258absl::optional<url::Origin> GetCommittedOriginForFrameEntry(
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12259 const mojom::DidCommitProvisionalLoadParams& params,
260 NavigationRequest* request) {
Nasko Oskov03912102019-01-11 00:21:32261 // Error pages commit in an opaque origin, yet have the real URL that resulted
262 // in an error as the |params.url|. Since successful reload of an error page
263 // should commit in the correct origin, setting the opaque origin on the
264 // FrameNavigationEntry will be incorrect.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12265 if (request->DidEncounterError())
Anton Bikineevf62d1bf2021-05-15 17:56:07266 return absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:32267
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12268 // We also currently don't save committed origins for loadDataWithBaseURL
269 // navigations (probably accidentally). Without this check, navigations to
270 // the FrameNavigationEntry might fail the DoesURLMatchOriginForNavigation()
271 // check since the origin will be based on the base URL instead of the data:
272 // URL used for the navigation.
273 // TODO(https://crbug.com/1198406): Save committed origin in
274 // FrameNavigationEntry for this case too.
Rakina Zata Amni5d2ef8aa2021-06-25 01:34:23275 if (request->IsLoadDataWithBaseURLAndHasUnreachableURL())
Anton Bikineevf62d1bf2021-05-15 17:56:07276 return absl::nullopt;
Rakina Zata Amni3a1c0ec2021-04-15 03:35:12277
Anton Bikineevf62d1bf2021-05-15 17:56:07278 return absl::make_optional(params.origin);
Nasko Oskov03912102019-01-11 00:21:32279}
280
Camille Lamy5193caa2018-10-12 11:59:42281bool IsValidURLForNavigation(bool is_main_frame,
282 const GURL& virtual_url,
283 const GURL& dest_url) {
284 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
285 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
286 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
287 << virtual_url.possibly_invalid_spec();
288 return false;
289 }
290
291 // Don't attempt to navigate to non-empty invalid URLs.
292 if (!dest_url.is_valid() && !dest_url.is_empty()) {
293 LOG(WARNING) << "Refusing to load invalid URL: "
294 << dest_url.possibly_invalid_spec();
295 return false;
296 }
297
298 // The renderer will reject IPC messages with URLs longer than
299 // this limit, so don't attempt to navigate with a longer URL.
300 if (dest_url.spec().size() > url::kMaxURLChars) {
301 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
302 << " characters.";
303 return false;
304 }
305
Aaron Colwell33109c592020-04-21 21:31:19306 // Reject renderer debug URLs because they should have been handled before
307 // we get to this point. This check handles renderer debug URLs
308 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
309 // provides defense-in-depth if a renderer debug URL manages to get here via
310 // some other path. We want to reject the navigation here so it doesn't
311 // violate assumptions in downstream code.
Gyuyoung Kim107c2a02021-04-13 01:49:30312 if (blink::IsRendererDebugURL(dest_url)) {
Aaron Colwell33109c592020-04-21 21:31:19313 LOG(WARNING) << "Refusing to load renderer debug URL: "
314 << dest_url.possibly_invalid_spec();
315 return false;
316 }
317
Camille Lamy5193caa2018-10-12 11:59:42318 return true;
319}
320
Mikel Astizba9cf2fd2017-12-17 10:38:10321// See replaced_navigation_entry_data.h for details: this information is meant
322// to ensure |*output_entry| keeps track of its original URL (landing page in
323// case of server redirects) as it gets replaced (e.g. history.replaceState()),
324// without overwriting it later, for main frames.
325void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57326 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10327 NavigationEntryImpl* output_entry) {
328 if (output_entry->GetReplacedEntryData().has_value())
329 return;
330
331 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57332 data.first_committed_url = replaced_entry->GetURL();
333 data.first_timestamp = replaced_entry->GetTimestamp();
334 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29335 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10336}
337
Minggang Wangb9f3fa92021-07-01 15:30:31338blink::mojom::NavigationType GetNavigationType(
339 const GURL& old_url,
340 const GURL& new_url,
341 ReloadType reload_type,
342 NavigationEntryImpl* entry,
343 const FrameNavigationEntry& frame_entry,
344 bool has_pending_cross_document_commit,
345 bool is_currently_error_page,
346 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23347 // Reload navigations
348 switch (reload_type) {
349 case ReloadType::NORMAL:
Minggang Wangb9f3fa92021-07-01 15:30:31350 return blink::mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23351 case ReloadType::BYPASSING_CACHE:
Minggang Wangb9f3fa92021-07-01 15:30:31352 return blink::mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23353 case ReloadType::ORIGINAL_REQUEST_URL:
Minggang Wangb9f3fa92021-07-01 15:30:31354 return blink::mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23355 case ReloadType::NONE:
356 break; // Fall through to rest of function.
357 }
358
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08359 if (entry->IsRestored()) {
Minggang Wangb9f3fa92021-07-01 15:30:31360 return entry->GetHasPostData()
361 ? blink::mojom::NavigationType::RESTORE_WITH_POST
362 : blink::mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23363 }
364
danakjb952ef12021-01-14 19:58:49365 const bool can_be_same_document =
366 // A pending cross-document commit means this navigation will not occur in
367 // the current document, as that document would end up being replaced in
368 // the meantime.
369 !has_pending_cross_document_commit &&
370 // If the current document is an error page, we should always treat it as
371 // a different-document navigation so that we'll attempt to load the
372 // document we're navigating to (and not stay in the current error page).
373 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12374
clamyea99ea12018-05-28 13:54:23375 // History navigations.
376 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12377 return can_be_same_document && is_same_document_history_load
Minggang Wangb9f3fa92021-07-01 15:30:31378 ? blink::mojom::NavigationType::HISTORY_SAME_DOCUMENT
379 : blink::mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23380 }
381 DCHECK(!is_same_document_history_load);
382
383 // A same-document fragment-navigation happens when the only part of the url
384 // that is modified is after the '#' character.
385 //
386 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12387 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23388 //
389 // Note: this check is only valid for navigations that are not history
390 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
391 // history navigation from 'A#foo' to 'A#bar' is not a same-document
392 // navigation, but a different-document one. This is why history navigation
393 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47394 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
395 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12396
397 // The one case where we do the wrong thing here and incorrectly choose
398 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
399 // the renderer is a frameset. All frameset navigations should be
400 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
401 // navigation would do the right thing, as it would send it to the browser and
402 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
403 // into this method). But since we can't tell that case here for browser-
404 // initiated navigations, we have to get the renderer involved. In that case
405 // the navigation would be restarted due to the renderer spending a reply of
406 // mojom::CommitResult::RestartCrossDocument.
407
408 return can_be_same_document && is_same_doc
Minggang Wangb9f3fa92021-07-01 15:30:31409 ? blink::mojom::NavigationType::SAME_DOCUMENT
410 : blink::mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23411}
412
Camille Lamy5193caa2018-10-12 11:59:42413// Adjusts the original input URL if needed, to get the URL to actually load and
414// the virtual URL, which may differ.
415void RewriteUrlForNavigation(const GURL& original_url,
416 BrowserContext* browser_context,
417 GURL* url_to_load,
418 GURL* virtual_url,
419 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42420 // Allow the browser URL handler to rewrite the URL. This will, for example,
421 // remove "view-source:" from the beginning of the URL to get the URL that
422 // will actually be loaded. This real URL won't be shown to the user, just
423 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31424 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42425 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
426 url_to_load, browser_context, reverse_on_redirect);
427}
428
429#if DCHECK_IS_ON()
430// Helper sanity check function used in debug mode.
431void ValidateRequestMatchesEntry(NavigationRequest* request,
432 NavigationEntryImpl* entry) {
433 if (request->frame_tree_node()->IsMainFrame()) {
434 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
435 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
Minggang Wangb9f3fa92021-07-01 15:30:31436 ui::PageTransitionFromInt(request->common_params().transition),
437 entry->GetTransitionType()));
Camille Lamy5193caa2018-10-12 11:59:42438 }
Nasko Oskovc36327d2019-01-03 23:23:04439 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42440 entry->should_clear_history_list());
441 DCHECK_EQ(request->common_params().has_user_gesture,
442 entry->has_user_gesture());
443 DCHECK_EQ(request->common_params().base_url_for_data_url,
444 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04445 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42446 entry->GetCanLoadLocalResources());
447 DCHECK_EQ(request->common_params().started_from_context_menu,
448 entry->has_started_from_context_menu());
449
450 FrameNavigationEntry* frame_entry =
451 entry->GetFrameEntry(request->frame_tree_node());
452 if (!frame_entry) {
453 NOTREACHED();
454 return;
455 }
456
Camille Lamy5193caa2018-10-12 11:59:42457 DCHECK_EQ(request->common_params().method, frame_entry->method());
458
Nasko Oskovc36327d2019-01-03 23:23:04459 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42460 if (redirect_size == frame_entry->redirect_chain().size()) {
461 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04462 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42463 frame_entry->redirect_chain()[i]);
464 }
465 } else {
466 NOTREACHED();
467 }
468}
469#endif // DCHECK_IS_ON()
470
Dave Tapuska8bfd84c2019-03-26 20:47:16471// Returns whether the session history NavigationRequests in |navigations|
472// would stay within the subtree of the sandboxed iframe in
473// |sandbox_frame_tree_node_id|.
474bool DoesSandboxNavigationStayWithinSubtree(
475 int sandbox_frame_tree_node_id,
476 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
477 for (auto& item : navigations) {
478 bool within_subtree = false;
479 // Check whether this NavigationRequest affects a frame within the
480 // sandboxed frame's subtree by walking up the tree looking for the
481 // sandboxed frame.
482 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03483 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16484 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
485 within_subtree = true;
486 break;
487 }
488 }
489 if (!within_subtree)
490 return false;
491 }
492 return true;
493}
494
Titouan Rigoudy6ec70402021-02-02 15:42:19495bool ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(
Antonio Sartori78a749f2020-11-30 12:03:39496 const NavigationRequest* request) {
497 // For local schemes we need to store the policy container in the
498 // FrameNavigationEntry, so that we can reload it in case of history
499 // navigation.
500 //
501 // TODO(https://crbug.com/1146361 and https://crbug.com/1146362): blob: and
502 // filesystem: should be removed from this list when we have properly
503 // implemented storing their policy container in the respective store.
504 return (request->common_params().url.SchemeIs(url::kAboutScheme) ||
505 request->common_params().url.SchemeIs(url::kDataScheme) ||
506 request->common_params().url.SchemeIsBlob() ||
507 request->common_params().url.SchemeIsFileSystem());
508}
509
[email protected]e9ba4472008-09-14 15:42:43510} // namespace
511
arthursonzogni66f711c2019-10-08 14:40:36512// NavigationControllerImpl::PendingEntryRef------------------------------------
513
514NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
515 base::WeakPtr<NavigationControllerImpl> controller)
516 : controller_(controller) {}
517
518NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
519 if (!controller_) // Can be null with interstitials.
520 return;
521
522 controller_->PendingEntryRefDeleted(this);
523}
524
[email protected]d202a7c2012-01-04 07:53:47525// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29526
[email protected]23a918b2014-07-15 09:51:36527const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23528
[email protected]765b35502008-08-21 00:51:20529// static
[email protected]d202a7c2012-01-04 07:53:47530size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23531 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20532
[email protected]e6fec472013-05-14 05:29:02533// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20534// when testing.
535static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29536
[email protected]71fde352011-12-29 03:29:56537// static
dcheng9bfa5162016-04-09 01:00:57538std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
539 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10540 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07541 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10542 ui::PageTransition transition,
543 bool is_renderer_initiated,
544 const std::string& extra_headers,
545 BrowserContext* browser_context,
546 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
547 return NavigationControllerImpl::CreateNavigationEntry(
548 url, referrer, std::move(initiator_origin),
549 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Hayato Ito303654c2021-06-30 09:07:54550 extra_headers, browser_context, std::move(blob_url_loader_factory));
Lukasz Anforowicz641234d52019-11-07 21:07:10551}
552
553// static
554std::unique_ptr<NavigationEntryImpl>
555NavigationControllerImpl::CreateNavigationEntry(
556 const GURL& url,
557 Referrer referrer,
Anton Bikineevf62d1bf2021-05-15 17:56:07558 absl::optional<url::Origin> initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:10559 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57560 ui::PageTransition transition,
561 bool is_renderer_initiated,
562 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09563 BrowserContext* browser_context,
Hayato Ito303654c2021-06-30 09:07:54564 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
Camille Lamy5193caa2018-10-12 11:59:42565 GURL url_to_load;
566 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56567 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42568 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
569 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56570
Lukasz Anforowicz641234d52019-11-07 21:07:10571 // Let the NTP override the navigation params and pretend that this is a
572 // browser-initiated, bookmark-like navigation.
573 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violetcf6ea7e2021-06-09 21:09:21574 source_site_instance, &transition, &is_renderer_initiated, &referrer,
575 &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10576
Patrick Monettef507e982019-06-19 20:18:06577 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28578 nullptr, // The site instance for tabs is sent on navigation
579 // (WebContents::GetSiteInstance).
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:58580 url_to_load, referrer, initiator_origin, std::u16string(), transition,
Camille Lamy5193caa2018-10-12 11:59:42581 is_renderer_initiated, blob_url_loader_factory);
582 entry->SetVirtualURL(virtual_url);
583 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56584 entry->set_update_virtual_url_with_url(reverse_on_redirect);
585 entry->set_extra_headers(extra_headers);
Patrick Monettef507e982019-06-19 20:18:06586 return entry;
[email protected]71fde352011-12-29 03:29:56587}
588
[email protected]cdcb1dee2012-01-04 00:46:20589// static
590void NavigationController::DisablePromptOnRepost() {
591 g_check_for_repost = false;
592}
593
[email protected]c5b88d82012-10-06 17:03:33594base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
595 base::Time t) {
596 // If |t| is between the water marks, we're in a run of duplicates
597 // or just getting out of it, so increase the high-water mark to get
598 // a time that probably hasn't been used before and return it.
599 if (low_water_mark_ <= t && t <= high_water_mark_) {
600 high_water_mark_ += base::TimeDelta::FromMicroseconds(1);
601 return high_water_mark_;
602 }
603
604 // Otherwise, we're clear of the last duplicate run, so reset the
605 // water marks.
606 low_water_mark_ = high_water_mark_ = t;
607 return t;
608}
609
ckitagawa0faa5e42020-06-17 17:30:54610NavigationControllerImpl::ScopedShowRepostDialogForTesting::
611 ScopedShowRepostDialogForTesting()
612 : was_disallowed_(g_check_for_repost) {
613 g_check_for_repost = true;
614}
615
616NavigationControllerImpl::ScopedShowRepostDialogForTesting::
617 ~ScopedShowRepostDialogForTesting() {
618 g_check_for_repost = was_disallowed_;
619}
620
[email protected]d202a7c2012-01-04 07:53:47621NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00622 BrowserContext* browser_context,
623 FrameTree& frame_tree,
624 NavigationControllerDelegate* delegate)
625 : frame_tree_(frame_tree),
626 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57627 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22628 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47629 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37630 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29631}
632
[email protected]d202a7c2012-01-04 07:53:47633NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00634 // The NavigationControllerImpl might be called inside its delegate
635 // destructor. Calling it is not valid anymore.
636 delegate_ = nullptr;
637 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29638}
639
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57640WebContents* NavigationControllerImpl::GetWebContents() {
[email protected]ec6c05f2013-10-23 18:41:57641 return delegate_->GetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32642}
643
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57644BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55645 return browser_context_;
646}
647
[email protected]d202a7c2012-01-04 07:53:47648void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30649 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36650 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57651 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57652 // Verify that this controller is unused and that the input is valid.
Lei Zhang96031532019-10-10 19:05:47653 DCHECK_EQ(0, GetEntryCount());
654 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57655 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14656 selected_navigation < static_cast<int>(entries->size()));
arthursonzogni5c4c202d2017-04-25 23:41:27657 DCHECK_EQ(-1, pending_entry_index_);
[email protected]ce3fa3c2009-04-20 19:55:57658
[email protected]ce3fa3c2009-04-20 19:55:57659 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44660 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03661 entries_.reserve(entries->size());
662 for (auto& entry : *entries)
dcheng36b6aec92015-12-26 06:16:36663 entries_.push_back(
664 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
avif16f85a72015-11-13 18:25:03665
666 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
667 // cleared out safely.
668 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57669
670 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36671 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57672}
673
toyoshim6142d96f2016-12-19 09:07:25674void NavigationControllerImpl::Reload(ReloadType reload_type,
675 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28676 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28677 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32678 int current_index = -1;
679
Carlos IL42b416592019-10-07 23:10:36680 if (entry_replaced_by_post_commit_error_) {
681 // If there is an entry that was replaced by a currently active post-commit
682 // error navigation, this can't be the initial navigation.
683 DCHECK(!IsInitialNavigation());
684 // If the current entry is a post commit error, we reload the entry it
685 // replaced instead. We leave the error entry in place until a commit
686 // replaces it, but the pending entry points to the original entry in the
687 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
688 // case that pending_entry_ != entries_[pending_entry_index_].
689 entry = entry_replaced_by_post_commit_error_.get();
690 current_index = GetCurrentEntryIndex();
691 } else if (IsInitialNavigation() && pending_entry_) {
692 // If we are reloading the initial navigation, just use the current
693 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32694 entry = pending_entry_;
695 // The pending entry might be in entries_ (e.g., after a Clone), so we
696 // should also update the current_index.
697 current_index = pending_entry_index_;
698 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00699 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32700 current_index = GetCurrentEntryIndex();
701 if (current_index != -1) {
creis3da03872015-02-20 21:12:32702 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32703 }
[email protected]979a4bc2013-04-24 01:27:15704 }
[email protected]241db352013-04-22 18:04:05705
[email protected]59167c22013-06-03 18:07:32706 // If we are no where, then we can't reload. TODO(darin): We should add a
707 // CanReload method.
708 if (!entry)
709 return;
710
Takashi Toyoshimac7df3c22019-06-25 14:18:47711 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26712 entry->set_reload_type(reload_type);
713
Aran Gilman37d11632019-10-08 23:07:15714 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30715 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07716 // they really want to do this. If they do, the dialog will call us back
717 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57718 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02719
[email protected]106a0812010-03-18 00:15:12720 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57721 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47722 return;
initial.commit09911bf2008-07-26 23:55:29723 }
Lei Zhang96031532019-10-10 19:05:47724
725 if (!IsInitialNavigation())
726 DiscardNonCommittedEntries();
727
728 pending_entry_ = entry;
729 pending_entry_index_ = current_index;
730 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
731
732 NavigateToExistingPendingEntry(reload_type,
733 FrameTreeNode::kFrameTreeNodeInvalidId);
initial.commit09911bf2008-07-26 23:55:29734}
735
[email protected]d202a7c2012-01-04 07:53:47736void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48737 DCHECK(pending_reload_ != ReloadType::NONE);
738 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12739}
740
[email protected]d202a7c2012-01-04 07:53:47741void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48742 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12743 NOTREACHED();
744 } else {
toyoshim6142d96f2016-12-19 09:07:25745 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48746 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12747 }
748}
749
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57750bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09751 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11752}
753
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57754bool NavigationControllerImpl::IsInitialBlankNavigation() {
creis10a4ab72015-10-13 17:22:40755 // TODO(creis): Once we create a NavigationEntry for the initial blank page,
toyoshim0df1d3a2016-09-09 09:52:48756 // we'll need to check for entry count 1 and restore_type NONE (to exclude
757 // the cloned tab case).
creis10a4ab72015-10-13 17:22:40758 return IsInitialNavigation() && GetEntryCount() == 0;
759}
760
Aran Gilman37d11632019-10-08 23:07:15761NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
762 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58763 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03764 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58765}
766
W. James MacLean1c40862c2020-04-27 21:05:57767void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
768 const url::Origin& origin) {
769 for (int i = 0; i < GetEntryCount(); i++) {
770 auto* entry = GetEntryAtIndex(i);
771 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
772 }
773 if (entry_replaced_by_post_commit_error_) {
774 // It's possible we could come back to this entry if the error
775 // page/interstitial goes away.
776 entry_replaced_by_post_commit_error_
777 ->RegisterExistingOriginToPreventOptInIsolation(origin);
778 }
779 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
780 // visiting pending_entry_, which lacks a committed origin. This will be done
781 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
782}
783
avi25764702015-06-23 15:43:37784void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57785 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00786 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37787 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27788 DCHECK_EQ(-1, pending_entry_index_);
[email protected]8ff00d72012-10-23 19:12:21789 NotificationService::current()->Notify(
Aran Gilman37d11632019-10-08 23:07:15790 NOTIFICATION_NAV_ENTRY_PENDING, Source<NavigationController>(this),
avi25764702015-06-23 15:43:37791 Details<NavigationEntry>(pending_entry_));
[email protected]765b35502008-08-21 00:51:20792}
793
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57794NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47795 if (pending_entry_)
796 return pending_entry_;
797 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20798}
799
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57800NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32801 // The pending entry is safe to return for new (non-history), browser-
802 // initiated navigations. Most renderer-initiated navigations should not
803 // show the pending entry, to prevent URL spoof attacks.
804 //
805 // We make an exception for renderer-initiated navigations in new tabs, as
806 // long as no other page has tried to access the initial empty document in
807 // the new tab. If another page modifies this blank page, a URL spoof is
808 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32809 bool safe_to_show_pending =
810 pending_entry_ &&
811 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09812 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32813 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46814 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32815
816 // Also allow showing the pending entry for history navigations in a new tab,
817 // such as Ctrl+Back. In this case, no existing page is visible and no one
818 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15819 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
820 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32821 safe_to_show_pending = true;
822
823 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19824 return pending_entry_;
825 return GetLastCommittedEntry();
826}
827
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57828int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20829 if (pending_entry_index_ != -1)
830 return pending_entry_index_;
831 return last_committed_entry_index_;
832}
833
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57834NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20835 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28836 return nullptr;
avif16f85a72015-11-13 18:25:03837 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20838}
839
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57840bool NavigationControllerImpl::CanViewSource() {
Carlos Caballeroede6f8c2021-01-28 11:01:50841 const std::string& mime_type = frame_tree_.root()
Alex Moshchuk2e470ea2021-02-03 06:46:34842 ->current_frame_host()
843 ->render_view_host()
Carlos Caballeroede6f8c2021-01-28 11:01:50844 ->contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53845 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
846 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27847 NavigationEntry* visible_entry = GetVisibleEntry();
848 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39849 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16850}
851
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57852int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27853 // The last committed entry index must always be less than the number of
Carlos IL4dea8902020-05-26 15:14:29854 // entries. If there are no entries, it must be -1.
arthursonzogni5c4c202d2017-04-25 23:41:27855 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
856 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55857 return last_committed_entry_index_;
858}
859
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57860int NavigationControllerImpl::GetEntryCount() {
Carlos IL4dea8902020-05-26 15:14:29861 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55862 return static_cast<int>(entries_.size());
863}
864
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57865NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37866 if (index < 0 || index >= GetEntryCount())
867 return nullptr;
868
avif16f85a72015-11-13 18:25:03869 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25870}
871
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57872NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47873 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20874}
875
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57876int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46877 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03878}
879
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57880bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03881 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
882 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
883 return true;
884 }
885 return false;
[email protected]765b35502008-08-21 00:51:20886}
887
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57888bool NavigationControllerImpl::CanGoForward() {
WangHui74286d52021-03-31 16:17:15889 for (int index = GetIndexForOffset(1); index < GetEntryCount(); index++) {
890 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
891 return true;
892 }
893 return false;
[email protected]765b35502008-08-21 00:51:20894}
895
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57896bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03897 int index = GetIndexForOffset(offset);
898 return index >= 0 && index < GetEntryCount();
899}
900
WangHui74286d52021-03-31 16:17:15901#if defined(OS_ANDROID)
902bool NavigationControllerImpl::CanGoToOffsetWithSkipping(int offset) {
WangHui74286d52021-03-31 16:17:15903 if (offset == 0)
904 return true;
905 int increment = offset > 0 ? 1 : -1;
906 int non_skippable_entries = 0;
907 for (int index = GetIndexForOffset(increment);
908 index >= 0 && index < GetEntryCount(); index += increment) {
909 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
910 non_skippable_entries++;
911
912 if (non_skippable_entries == std::abs(offset))
913 return true;
914 }
915 return false;
916}
917#endif
918
[email protected]d202a7c2012-01-04 07:53:47919void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06920 int target_index = GetIndexForOffset(-1);
921
Elly Fong-Jones40ee8112021-06-23 19:10:52922 // Move the target index past the skippable entries.
Shivani Sharma298d12852019-01-22 20:04:03923 bool all_skippable_entries = true;
Elly Fong-Jones40ee8112021-06-23 19:10:52924 while (target_index >= 0) {
925 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui()) {
Shivani Sharma298d12852019-01-22 20:04:03926 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06927 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16928 }
Elly Fong-Jones40ee8112021-06-23 19:10:52929 target_index--;
shivanisha55201872018-12-13 04:29:06930 }
Miyoung Shin1c565c912021-03-17 12:11:21931
Shivani Sharma298d12852019-01-22 20:04:03932 // Do nothing if all entries are skippable. Normally this path would not
933 // happen as consumers would have already checked it in CanGoBack but a lot of
934 // tests do not do that.
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42935 if (all_skippable_entries)
Shivani Sharma298d12852019-01-22 20:04:03936 return;
shivanisha55201872018-12-13 04:29:06937
shivanisha55201872018-12-13 04:29:06938 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20939}
940
[email protected]d202a7c2012-01-04 07:53:47941void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06942 int target_index = GetIndexForOffset(1);
943
Shivani Sharma2d5b4b6b2019-01-08 16:07:16944 // Note that at least one entry (the last one) will be non-skippable since
945 // entries are marked skippable only when they add another entry because of
946 // redirect or pushState.
Elly Fong-Jones40ee8112021-06-23 19:10:52947 while (target_index < static_cast<int>(entries_.size())) {
948 if (!GetEntryAtIndex(target_index)->should_skip_on_back_forward_ui())
shivanisha55201872018-12-13 04:29:06949 break;
Elly Fong-Jones40ee8112021-06-23 19:10:52950 target_index++;
shivanisha55201872018-12-13 04:29:06951 }
shivanisha55201872018-12-13 04:29:06952 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20953}
954
[email protected]d202a7c2012-01-04 07:53:47955void NavigationControllerImpl::GoToIndex(int index) {
Dave Tapuska8bfd84c2019-03-26 20:47:16956 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId);
957}
958
959void NavigationControllerImpl::GoToIndex(int index,
960 int sandbox_frame_tree_node_id) {
sunjian30574a62017-03-21 21:39:44961 TRACE_EVENT0("browser,navigation,benchmark",
962 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:20963 if (index < 0 || index >= static_cast<int>(entries_.size())) {
964 NOTREACHED();
965 return;
966 }
967
[email protected]cbab76d2008-10-13 22:42:47968 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20969
arthursonzogni5c4c202d2017-04-25 23:41:27970 DCHECK_EQ(nullptr, pending_entry_);
971 DCHECK_EQ(-1, pending_entry_index_);
972 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:20973 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:27974 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
975 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Dave Tapuska8bfd84c2019-03-26 20:47:16976 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id);
[email protected]765b35502008-08-21 00:51:20977}
978
[email protected]d202a7c2012-01-04 07:53:47979void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:12980 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:03981 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:20982 return;
983
[email protected]9ba14052012-06-22 23:50:03984 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20985}
986
WangHui74286d52021-03-31 16:17:15987#if defined(OS_ANDROID)
988void NavigationControllerImpl::GoToOffsetWithSkipping(int offset) {
989 // Note: This is actually reached in unit tests.
990 if (!CanGoToOffsetWithSkipping(offset))
991 return;
992
Elly Fong-Jonesccc6d1f2021-06-14 18:32:42993 if (offset == 0) {
WangHui74286d52021-03-31 16:17:15994 GoToIndex(GetIndexForOffset(offset));
995 return;
996 }
997 int increment = offset > 0 ? 1 : -1;
998 // Find the offset without counting skippable entries.
999 int target_index = GetIndexForOffset(increment);
1000 int non_skippable_entries = 0;
1001 for (int index = target_index; index >= 0 && index < GetEntryCount();
1002 index += increment) {
1003 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
1004 non_skippable_entries++;
1005
1006 if (non_skippable_entries == std::abs(offset)) {
1007 target_index = index;
1008 break;
1009 }
1010 }
1011
1012 GoToIndex(target_index);
1013}
1014#endif
1015
[email protected]41374f12013-07-24 02:49:281016bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:151017 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:281018 return false;
[email protected]6a13a6c2011-12-20 21:47:121019
[email protected]43032342011-03-21 14:10:311020 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:281021 return true;
[email protected]cbab76d2008-10-13 22:42:471022}
1023
Michael Thiessen9b14d512019-09-23 21:19:471024void NavigationControllerImpl::PruneForwardEntries() {
1025 DiscardNonCommittedEntries();
1026 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:471027 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:471028 if (num_removed <= 0)
1029 return;
1030 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
1031 NotifyPrunedEntries(this, remove_start_index /* start index */,
1032 num_removed /* count */);
1033}
1034
Aran Gilman37d11632019-10-08 23:07:151035void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
1036 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:321037 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:311038 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:511039 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
1040 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:321041 }
1042}
1043
Harkiran Bolariaba823e42021-05-21 18:30:361044base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURL(
1045 const GURL& url,
1046 const Referrer& referrer,
1047 ui::PageTransition transition,
1048 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471049 LoadURLParams params(url);
1050 params.referrer = referrer;
1051 params.transition_type = transition;
1052 params.extra_headers = extra_headers;
Harkiran Bolariaba823e42021-05-21 18:30:361053 return LoadURLWithParams(params);
[email protected]cf002332012-08-14 19:17:471054}
1055
Harkiran Bolariaba823e42021-05-21 18:30:361056base::WeakPtr<NavigationHandle> NavigationControllerImpl::LoadURLWithParams(
1057 const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061058 if (params.is_renderer_initiated)
1059 DCHECK(params.initiator_origin.has_value());
1060
naskob8744d22014-08-28 17:07:431061 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151062 "NavigationControllerImpl::LoadURLWithParams", "url",
1063 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291064 bool is_explicit_navigation =
1065 GetContentClient()->browser()->IsExplicitNavigation(
1066 params.transition_type);
1067 if (HandleDebugURL(params.url, params.transition_type,
1068 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431069 // If Telemetry is running, allow the URL load to proceed as if it's
1070 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491071 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431072 cc::switches::kEnableGpuBenchmarking))
Harkiran Bolariaba823e42021-05-21 18:30:361073 return nullptr;
[email protected]47752982014-07-29 08:01:431074 }
[email protected]8bf1048012012-02-08 01:22:181075
[email protected]cf002332012-08-14 19:17:471076 // Checks based on params.load_type.
1077 switch (params.load_type) {
1078 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431079 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471080 break;
1081 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261082 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471083 NOTREACHED() << "Data load must use data scheme.";
Harkiran Bolariaba823e42021-05-21 18:30:361084 return nullptr;
[email protected]cf002332012-08-14 19:17:471085 }
1086 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461087 }
[email protected]e47ae9472011-10-13 19:48:341088
[email protected]e47ae9472011-10-13 19:48:341089 // The user initiated a load, we don't need to reload anymore.
1090 needs_reload_ = false;
1091
Harkiran Bolariaba823e42021-05-21 18:30:361092 return NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441093}
1094
Mohamed Abdelhalim833de902019-09-16 17:41:451095bool NavigationControllerImpl::PendingEntryMatchesRequest(
1096 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191097 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451098 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191099}
1100
[email protected]d202a7c2012-01-04 07:53:471101bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321102 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071103 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331104 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441105 bool is_same_document_navigation,
Nate Chapinc7019dd7d2021-06-25 18:29:251106 bool was_on_initial_empty_document,
Shivani Sharmaffb32b82019-04-09 16:58:471107 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131108 NavigationRequest* navigation_request) {
1109 DCHECK(navigation_request);
[email protected]cd2e15742013-03-08 04:08:311110 is_initial_navigation_ = false;
1111
[email protected]0e8db942008-09-24 21:21:481112 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431113 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481114 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361115 if (entry_replaced_by_post_commit_error_) {
1116 if (is_same_document_navigation) {
1117 // Same document navigations should not be possible on error pages and
1118 // would leave the controller in a weird state. Kill the renderer if
1119 // that happens.
1120 bad_message::ReceivedBadMessage(
1121 rfh->GetProcess(), bad_message::NC_SAME_DOCUMENT_POST_COMMIT_ERROR);
1122 }
1123 // Any commit while a post-commit error page is showing should put the
1124 // original entry back, replacing the error page's entry. This includes
1125 // reloads, where the original entry was used as the pending entry and
1126 // should now be at the correct index at commit time.
1127 entries_[last_committed_entry_index_] =
1128 std::move(entry_replaced_by_post_commit_error_);
1129 }
Fergal Daly8e33cf62020-12-12 01:06:071130 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551131 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431132 if (pending_entry_ &&
1133 pending_entry_->GetIsOverridingUserAgent() !=
1134 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1135 overriding_user_agent_changed = true;
[email protected]0e8db942008-09-24 21:21:481136 } else {
Gang Wu325f03f42021-02-25 20:00:461137 // GetLastCommittedEntry() is null, so this is the first entry.
Fergal Daly8e33cf62020-12-12 01:06:071138 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481139 details->previous_entry_index = -1;
Gang Wu325f03f42021-02-25 20:00:461140 if (pending_entry_ && pending_entry_->GetIsOverridingUserAgent()) {
1141 // Default setting is NOT override the user agent, so overriding the user
1142 // agent in first entry should be considered as user agent changed as
1143 // well.
1144 overriding_user_agent_changed = true;
1145 }
[email protected]0e8db942008-09-24 21:21:481146 }
[email protected]ecd9d8702008-08-28 22:10:171147
Alexander Timind2f2e4f22019-04-02 20:04:531148 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1149 // implementing back-forward cache.
1150
1151 // Create a new metrics object or reuse the previous one depending on whether
1152 // it's a main frame navigation or not.
1153 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics =
1154 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1155 GetLastCommittedEntry(), !rfh->GetParent(),
1156 params.document_sequence_number);
1157 // Notify the last active entry that we have navigated away.
1158 if (!rfh->GetParent() && !is_same_document_navigation) {
1159 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1160 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Hajime Hoshic7606502021-04-14 02:42:161161 metrics->MainFrameDidNavigateAwayFromDocument(rfh, navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531162 }
1163 }
1164 }
1165
fdegans9caf66a2015-07-30 21:10:421166 // If there is a pending entry at this point, it should have a SiteInstance,
1167 // except for restored entries.
jam48cea9082017-02-15 06:13:291168 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481169 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081170 pending_entry_->IsRestored());
1171 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151172 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291173 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481174 }
[email protected]e9ba4472008-09-14 15:42:431175
Nasko Oskovaee2f862018-06-15 00:05:521176 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1177 // has changed, this must be treated as a new navigation with replacement.
1178 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491179 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521180 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101181 pending_entry_->GetUniqueID() ==
1182 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521183 pending_entry_->site_instance() &&
1184 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1185 DCHECK_NE(-1, pending_entry_index_);
1186 // TODO(nasko,creis): Instead of setting this value here, set
1187 // should_replace_current_entry on the parameters we send to the
1188 // renderer process as part of CommitNavigation. The renderer should
1189 // in turn send it back here as part of |params| and it can be just
1190 // enforced and renderer process terminated on mismatch.
1191 details->did_replace_entry = true;
1192 } else {
1193 // The renderer tells us whether the navigation replaces the current entry.
1194 details->did_replace_entry = params.should_replace_current_entry;
1195 }
[email protected]bcd904482012-02-01 01:54:221196
[email protected]e9ba4472008-09-14 15:42:431197 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amnif6950d552020-11-24 03:26:101198 details->type = ClassifyNavigation(rfh, params, navigation_request);
[email protected]4bf3522c2010-08-19 21:00:201199
eugenebutee08663a2017-04-27 17:43:121200 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441201 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121202
Lucas Furukawa Gadanie3f7e792021-04-22 17:56:071203 details->is_prerender_activation =
1204 navigation_request->IsPrerenderedPageActivation();
1205
Peter Boströmd7592132019-01-30 04:50:311206 // Make sure we do not discard the pending entry for a different ongoing
1207 // navigation when a same document commit comes in unexpectedly from the
1208 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1209 // other navigation types. See https://crbug.com/900036.
1210 // TODO(crbug.com/926009): Handle history.pushState() as well.
1211 bool keep_pending_entry = is_same_document_navigation &&
Charlie Reisc0f17d2d2021-01-12 18:52:491212 details->type == NAVIGATION_TYPE_EXISTING_ENTRY &&
Peter Boströmd7592132019-01-30 04:50:311213 pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451214 !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311215
[email protected]0e8db942008-09-24 21:21:481216 switch (details->type) {
Charlie Reisc0f17d2d2021-01-12 18:52:491217 case NAVIGATION_TYPE_NEW_ENTRY:
1218 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051219 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451220 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431221 break;
Charlie Reisc0f17d2d2021-01-12 18:52:491222 case NAVIGATION_TYPE_EXISTING_ENTRY:
1223 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1224 was_restored, navigation_request,
1225 keep_pending_entry);
[email protected]e9ba4472008-09-14 15:42:431226 break;
[email protected]8ff00d72012-10-23 19:12:211227 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471228 RendererDidNavigateNewSubframe(
1229 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451230 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431231 break;
[email protected]8ff00d72012-10-23 19:12:211232 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391233 if (!RendererDidNavigateAutoSubframe(
Nate Chapinc7019dd7d2021-06-25 18:29:251234 rfh, params, details->is_same_document,
1235 was_on_initial_empty_document, navigation_request)) {
creisce0ef3572017-01-26 17:53:081236 // We don't send a notification about auto-subframe PageState during
1237 // UpdateStateForFrame, since it looks like nothing has changed. Send
1238 // it here at commit time instead.
1239 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431240 return false;
creis59d5a47cb2016-08-24 23:57:191241 }
[email protected]e9ba4472008-09-14 15:42:431242 break;
[email protected]8ff00d72012-10-23 19:12:211243 case NAVIGATION_TYPE_NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:491244 // If a pending navigation was in progress, this canceled it. We should
1245 // discard it and make sure it is removed from the URL bar. After that,
1246 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:431247 // the caller that nothing has happened.
arthursonzogni69a6a1b2019-09-17 09:23:001248 if (pending_entry_)
[email protected]20d1c992011-04-12 21:17:491249 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431250 return false;
Aran Gilman37d11632019-10-08 23:07:151251 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431252 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151253 break;
[email protected]765b35502008-08-21 00:51:201254 }
1255
[email protected]688aa65c62012-09-28 04:32:221256 // At this point, we know that the navigation has just completed, so
1257 // record the time.
1258 //
1259 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261260 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331261 base::Time timestamp =
1262 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1263 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131264 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221265
Peter Boströmd7592132019-01-30 04:50:311266 // If we aren't keeping the pending entry, there shouldn't be one at this
1267 // point. Clear it again in case any error cases above forgot to do so.
1268 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1269 // been cleared instead of protecting against it.
1270 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001271 DiscardNonCommittedEntries();
[email protected]f233e4232013-02-23 00:55:141272
[email protected]e9ba4472008-09-14 15:42:431273 // All committed entries should have nonempty content state so WebKit doesn't
1274 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471275 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321276 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221277 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441278 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531279 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1280 // implementing back-forward cache.
1281 if (!active_entry->back_forward_cache_metrics()) {
1282 active_entry->set_back_forward_cache_metrics(
1283 std::move(back_forward_cache_metrics));
1284 }
1285 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
Hajime Hoshi446206e2019-10-18 18:36:251286 navigation_request,
1287 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
naskoc7533512016-05-06 17:01:121288
Charles Reisc0507202017-09-21 00:40:021289 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1290 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1291 // A mismatch can occur if the renderer lies or due to a unique name collision
1292 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121293 FrameNavigationEntry* frame_entry =
1294 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021295 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1296 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031297 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081298 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1299 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031300 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201301
1302 // Remember the bindings the renderer process has at this point, so that
1303 // we do not grant this entry additional bindings if we come back to it.
1304 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301305 }
[email protected]132e281a2012-07-31 18:32:441306
[email protected]97d8f0d2013-10-29 16:49:211307 // Once it is committed, we no longer need to track several pieces of state on
1308 // the entry.
naskoc7533512016-05-06 17:01:121309 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131310
[email protected]49bd30e62011-03-22 20:12:591311 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221312 // TODO(creis): This check won't pass for subframes until we create entries
1313 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001314 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421315 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591316
[email protected]e9ba4472008-09-14 15:42:431317 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001318 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001319 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311320 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531321
arthursonzogni7ddc6542021-04-09 09:16:501322 active_entry->SetIsOverridingUserAgent(
1323 navigation_request->is_overriding_user_agent());
Scott Violetc36f7462020-05-06 23:13:031324
[email protected]93f230e02011-06-01 14:40:001325 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291326
John Abd-El-Malek380d3c5922017-09-08 00:20:311327 if (active_entry->GetURL().SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121328 !navigation_request->DidEncounterError()) {
John Abd-El-Malek969cdd94e2017-07-10 22:18:161329 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus",
1330 !!active_entry->GetSSL().certificate);
1331 }
1332
aelias100c9192017-01-13 00:01:431333 if (overriding_user_agent_changed)
1334 delegate_->UpdateOverridingUserAgent();
1335
creis03b48002015-11-04 00:54:561336 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1337 // one knows the latest NavigationEntry it is showing (whether it has
1338 // committed anything in this navigation or not). This allows things like
1339 // state and title updates from RenderFrames to apply to the latest relevant
1340 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381341 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001342 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381343 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
Hayato Ito2c8c08d02021-06-23 03:38:431344
1345 if (navigation_request->IsPrerenderedPageActivation()) {
1346 BroadcastHistoryOffsetAndLength();
1347 // TODO(crbug.com/1222893): Broadcasting happens after the prerendered page
1348 // is activated. As a result, a "prerenderingchange" event listener sees the
1349 // history.length which is not updated yet. We should guarantee that
1350 // history's length and offset should be updated before a
1351 // "prerenderingchange" event listener runs. One possible approach is to use
1352 // the same IPC which "prerenderingchange" uses, and propagate history's
1353 // length and offset together with that.
1354 }
1355
[email protected]e9ba4472008-09-14 15:42:431356 return true;
initial.commit09911bf2008-07-26 23:55:291357}
1358
[email protected]8ff00d72012-10-23 19:12:211359NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321360 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101361 const mojom::DidCommitProvisionalLoadParams& params,
1362 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591363 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511364 "ClassifyNavigation");
1365
avi7c6f35e2015-05-08 17:52:381366 if (params.did_create_new_entry) {
Charlie Reisc0f17d2d2021-01-12 18:52:491367 // A new entry. We may or may not have a corresponding pending entry, and
avi7c6f35e2015-05-08 17:52:381368 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001369 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491370 trace_return.set_return_reason("new entry, no parent, new entry");
1371 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381372 }
1373
1374 // When this is a new subframe navigation, we should have a committed page
1375 // in which it's a subframe. This may not be the case when an iframe is
1376 // navigated on a popup navigated to about:blank (the iframe would be
1377 // written into the popup by script on the main page). For these cases,
1378 // there isn't any navigation stuff we can do, so just ignore it.
Nasko Oskovae49e292020-08-13 02:08:511379 if (!GetLastCommittedEntry()) {
1380 trace_return.set_return_reason("new entry, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381381 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511382 }
avi7c6f35e2015-05-08 17:52:381383
1384 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511385 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381386 return NAVIGATION_TYPE_NEW_SUBFRAME;
1387 }
1388
Charlie Reisc0f17d2d2021-01-12 18:52:491389 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381390 DCHECK(!params.history_list_was_cleared);
1391
avi39c1edd32015-06-04 20:06:001392 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381393 // All manual subframes would be did_create_new_entry and handled above, so
1394 // we know this is auto.
Nasko Oskovae49e292020-08-13 02:08:511395 if (GetLastCommittedEntry()) {
1396 trace_return.set_return_reason("subframe, last commmited, auto subframe");
avi7c6f35e2015-05-08 17:52:381397 return NAVIGATION_TYPE_AUTO_SUBFRAME;
Nasko Oskovae49e292020-08-13 02:08:511398 }
Lei Zhang96031532019-10-10 19:05:471399
1400 // We ignore subframes created in non-committed pages; we'd appreciate if
1401 // people stopped doing that.
Nasko Oskovae49e292020-08-13 02:08:511402 trace_return.set_return_reason("subframe, no last commmited, ignore");
Lei Zhang96031532019-10-10 19:05:471403 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381404 }
1405
Rakina Zata Amnif6950d552020-11-24 03:26:101406 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1407 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381408 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1409 // create a new page.
1410
1411 // Just like above in the did_create_new_entry case, it's possible to
1412 // scribble onto an uncommitted page. Again, there isn't any navigation
1413 // stuff that we can do, so ignore it here as well.
avi3a5b8f32015-05-28 17:50:231414 NavigationEntry* last_committed = GetLastCommittedEntry();
Nasko Oskovae49e292020-08-13 02:08:511415 if (!last_committed) {
1416 trace_return.set_return_reason("nav entry 0, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381417 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511418 }
avi7c6f35e2015-05-08 17:52:381419
Charles Reis1378111f2017-11-08 21:44:061420 // This is history.replaceState() or history.reload().
Nasko Oskov332593c2018-08-16 17:21:341421 // TODO(nasko): With error page isolation, reloading an existing session
1422 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491423 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341424 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511425 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491426 "nav entry 0, last committed, existing entry");
1427 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381428 }
1429
Rakina Zata Amnif6950d552020-11-24 03:26:101430 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521431 // If the SiteInstance of the |pending_entry_| does not match the
1432 // SiteInstance that got committed, treat this as a new navigation with
1433 // replacement. This can happen if back/forward/reload encounters a server
1434 // redirect to a different site or an isolated error page gets successfully
1435 // reloaded into a different SiteInstance.
1436 if (pending_entry_->site_instance() &&
1437 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491438 trace_return.set_return_reason("pending matching nav entry, new entry");
1439 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521440 }
creis77c9aa32015-09-25 19:59:421441
Nasko Oskovaee2f862018-06-15 00:05:521442 if (pending_entry_index_ == -1) {
1443 // In this case, we have a pending entry for a load of a new URL but Blink
1444 // didn't do a new navigation (params.did_create_new_entry). First check
1445 // to make sure Blink didn't treat a new cross-process navigation as
1446 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161447 // we must treat it as NEW rather than the converted reload case below,
1448 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521449 if (!GetLastCommittedEntry() ||
1450 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161451 trace_return.set_return_reason("new pending, new entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491452 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521453 }
1454
1455 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161456 // We will create a pending entry, but NavigateWithoutEntry will convert
1457 // it to a reload since it's the same page and not create a new entry for
1458 // it. (The user doesn't want to have a new back/forward entry when they
1459 // do this.) Therefore we want to just ignore the pending entry and go
1460 // back to where we were (the "existing entry").
1461 trace_return.set_return_reason("new pending, existing (same) entry");
1462 return NAVIGATION_TYPE_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521463 }
avi7c6f35e2015-05-08 17:52:381464 }
1465
creis26d22632017-04-21 20:23:561466 // Everything below here is assumed to be an existing entry, but if there is
1467 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511468 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491469 trace_return.set_return_reason("no last committed, new entry");
1470 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511471 }
creis26d22632017-04-21 20:23:561472
avi7c6f35e2015-05-08 17:52:381473 if (params.intended_as_new_entry) {
1474 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491475 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1476 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161477 trace_return.set_return_reason("intended as new entry, existing entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491478 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381479 }
1480
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121481 if (navigation_request->DidEncounterError() &&
1482 failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101483 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381484 // If the renderer was going to a new pending entry that got cleared because
1485 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491486 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381487 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511488 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491489 "unreachable, matching pending, existing entry");
1490 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381491 }
1492
Charlie Reisc0f17d2d2021-01-12 18:52:491493 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101494 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511495 trace_return.traced_value()->SetInteger("existing_entry_index",
1496 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381497 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441498 // The renderer has committed a navigation to an entry that no longer
1499 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491500 trace_return.set_return_reason("existing entry -1, new entry");
1501 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381502 }
1503
avi7c6f35e2015-05-08 17:52:381504 // Since we weeded out "new" navigations above, we know this is an existing
1505 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491506 trace_return.set_return_reason("default return, existing entry");
1507 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381508}
1509
Charlie Reisc0f17d2d2021-01-12 18:52:491510void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321511 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071512 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361513 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441514 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471515 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451516 NavigationRequest* request) {
dcheng9bfa5162016-04-09 01:00:571517 std::unique_ptr<NavigationEntryImpl> new_entry;
creisf49dfc92016-07-26 17:05:181518 bool update_virtual_url = false;
1519
Anton Bikineevf62d1bf2021-05-15 17:56:071520 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451521 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061522
creisf49dfc92016-07-26 17:05:181523 // First check if this is an in-page navigation. If so, clone the current
1524 // entry instead of looking at the pending entry, because the pending entry
1525 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361526 if (is_same_document && GetLastCommittedEntry()) {
Patrick Monette50e8bd82019-06-13 22:40:451527 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481528 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081529 params.document_sequence_number, params.app_history_key,
1530 rfh->GetSiteInstance(), nullptr, params.url,
1531 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091532 Referrer(*params.referrer), initiator_origin,
1533 request->GetRedirectChain(), params.page_state, params.method,
1534 params.post_id, nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391535 nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481536 request->GetSubresourceWebBundleNavigationInfo(),
Antonio Sartori78a749f2020-11-30 12:03:391537 // We will set the document policies later in this function.
Titouan Rigoudy6ec70402021-02-02 15:42:191538 nullptr /* policy_container_policies */);
Charles Reisf44482022017-10-13 21:15:031539
creisf49dfc92016-07-26 17:05:181540 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Carlos Caballero40b0efd2021-01-26 11:55:001541 frame_entry, true, rfh->frame_tree_node(), frame_tree_.root());
jama78746e2017-02-22 17:21:571542 if (new_entry->GetURL().GetOrigin() != params.url.GetOrigin()) {
1543 // TODO(jam): we had one report of this with a URL that was redirecting to
1544 // only tildes. Until we understand that better, don't copy the cert in
1545 // this case.
1546 new_entry->GetSSL() = SSLStatus();
John Abd-El-Malek9cd697e2017-07-12 21:53:141547
John Abd-El-Malek380d3c5922017-09-08 00:20:311548 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121549 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141550 UMA_HISTOGRAM_BOOLEAN(
1551 "Navigation.SecureSchemeHasSSLStatus.NewPageInPageOriginMismatch",
1552 !!new_entry->GetSSL().certificate);
1553 }
jama78746e2017-02-22 17:21:571554 }
creisf49dfc92016-07-26 17:05:181555
Patrick Monette50e8bd82019-06-13 22:40:451556 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1557 // that |frame_entry| should now have a reference count of exactly 2: one
1558 // due to the local variable |frame_entry|, and another due to |new_entry|
1559 // also retaining one. This should never fail, because it's the main frame.
1560 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181561
1562 update_virtual_url = new_entry->update_virtual_url_with_url();
John Abd-El-Malek9cd697e2017-07-12 21:53:141563
John Abd-El-Malek380d3c5922017-09-08 00:20:311564 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121565 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141566 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus.NewPageInPage",
1567 !!new_entry->GetSSL().certificate);
1568 }
creisf49dfc92016-07-26 17:05:181569 }
1570
Harkiran Bolaria59290d62021-03-17 01:53:011571 // If this is an activation navigation from a prerendered page, transfer the
1572 // new entry from an entry already created and stored in the
1573 // NavigationRequest. |new_entry| will not have been set prior to this as
1574 // |is_same_document| is mutually exclusive with
1575 // |IsPrerenderedPageActivation|.
1576 if (request->IsPrerenderedPageActivation()) {
1577 DCHECK(!is_same_document);
1578 DCHECK(!new_entry);
1579 new_entry = request->TakePrerenderNavigationEntry();
1580 DCHECK(new_entry);
1581 }
1582
Charlie Reisc0f17d2d2021-01-12 18:52:491583 // Only make a copy of the pending entry if it is appropriate for the new
1584 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451585 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041586 // 1. The SiteInstance hasn't been assigned to something else.
1587 // 2. The pending entry was intended as a new entry, rather than being a
1588 // history navigation that was interrupted by an unrelated,
1589 // renderer-initiated navigation.
1590 // TODO(csharrison): Investigate whether we can remove some of the coarser
1591 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451592 if (!new_entry && PendingEntryMatchesRequest(request) &&
1593 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341594 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431595 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351596 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431597
[email protected]f1eb87a2011-05-06 17:49:411598 update_virtual_url = new_entry->update_virtual_url_with_url();
Camille Lamy62b826012019-02-26 09:15:471599 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451600 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141601
John Abd-El-Malek380d3c5922017-09-08 00:20:311602 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121603 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141604 UMA_HISTOGRAM_BOOLEAN(
1605 "Navigation.SecureSchemeHasSSLStatus.NewPagePendingEntryMatches",
1606 !!new_entry->GetSSL().certificate);
1607 }
creisf49dfc92016-07-26 17:05:181608 }
1609
Charlie Reisc0f17d2d2021-01-12 18:52:491610 // For cross-document commits with no matching pending entry, create a new
1611 // entry.
creisf49dfc92016-07-26 17:05:181612 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061613 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071614 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1615 initiator_origin,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:581616 std::u16string(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451617 params.transition, request->IsRendererInitiated(),
Lukasz Anforowicz435bcb582019-07-12 20:50:061618 nullptr); // blob_url_loader_factory
[email protected]f8f93eb2012-09-25 03:06:241619
1620 // Find out whether the new entry needs to update its virtual URL on URL
1621 // change and set up the entry accordingly. This is needed to correctly
1622 // update the virtual URL when replaceState is called after a pushState.
1623 GURL url = params.url;
1624 bool needs_update = false;
[email protected]47752982014-07-29 08:01:431625 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1626 &url, browser_context_, &needs_update);
[email protected]f8f93eb2012-09-25 03:06:241627 new_entry->set_update_virtual_url_with_url(needs_update);
1628
Charlie Reisc0f17d2d2021-01-12 18:52:491629 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411630 // update the virtual URL based on the new URL. For example, this is needed
1631 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1632 // the URL.
[email protected]f8f93eb2012-09-25 03:06:241633 update_virtual_url = needs_update;
Camille Lamy62b826012019-02-26 09:15:471634 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451635 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141636
John Abd-El-Malek380d3c5922017-09-08 00:20:311637 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121638 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141639 UMA_HISTOGRAM_BOOLEAN(
1640 "Navigation.SecureSchemeHasSSLStatus.NewPageNoMatchingEntry",
1641 !!new_entry->GetSSL().certificate);
1642 }
[email protected]e9ba4472008-09-14 15:42:431643 }
1644
Harkiran Bolaria59290d62021-03-17 01:53:011645 // TODO(crbug.com/1179428) - determine which parts of the entry need to be set
1646 // for prerendered contents, if any. This is because prerendering/activation
1647 // technically won't be creating a new document. Unlike BFCache, prerendering
1648 // creates a new NavigationEntry rather than using an existing one.
1649 if (!request->IsPrerenderedPageActivation()) {
1650 // Don't use the page type from the pending entry. Some interstitial page
1651 // may have set the type to interstitial. Once we commit, however, the page
1652 // type must always be normal or error.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121653 new_entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1654 : PAGE_TYPE_NORMAL);
Harkiran Bolaria59290d62021-03-17 01:53:011655 new_entry->SetURL(params.url);
1656 if (update_virtual_url)
1657 UpdateVirtualURLToURL(new_entry.get(), params.url);
1658 new_entry->SetReferrer(Referrer(*params.referrer));
1659 new_entry->SetTransitionType(params.transition);
1660 new_entry->set_site_instance(
1661 static_cast<SiteInstanceImpl*>(rfh->GetSiteInstance()));
1662 new_entry->SetOriginalRequestURL(request->GetOriginalRequestURL());
Rakina Zata Amnib597d632020-12-01 00:56:001663
Antonio Sartori4f5373792021-05-31 10:56:471664 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
Harkiran Bolaria59290d62021-03-17 01:53:011665 new_entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
[email protected]e9ba4472008-09-14 15:42:431666
Harkiran Bolaria59290d62021-03-17 01:53:011667 // Update the FrameNavigationEntry for new main frame commits.
1668 FrameNavigationEntry* frame_entry =
1669 new_entry->GetFrameEntry(rfh->frame_tree_node());
1670 frame_entry->set_frame_unique_name(rfh->frame_tree_node()->unique_name());
1671 frame_entry->set_item_sequence_number(params.item_sequence_number);
1672 frame_entry->set_document_sequence_number(params.document_sequence_number);
Nate Chapinfbfe5af2021-06-10 17:22:081673 frame_entry->set_app_history_key(params.app_history_key);
Harkiran Bolaria59290d62021-03-17 01:53:011674 frame_entry->set_redirect_chain(request->GetRedirectChain());
1675 frame_entry->SetPageState(params.page_state);
1676 frame_entry->set_method(params.method);
1677 frame_entry->set_post_id(params.post_id);
1678 frame_entry->set_policy_container_policies(
1679 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1680 request));
Antonio Sartori78a749f2020-11-30 12:03:391681
Anton Bikineevf62d1bf2021-05-15 17:56:071682 if (absl::optional<url::Origin> committed_origin =
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121683 GetCommittedOriginForFrameEntry(params, request)) {
1684 if (committed_origin.has_value())
1685 frame_entry->set_committed_origin(committed_origin.value());
1686 }
Harkiran Bolaria59290d62021-03-17 01:53:011687 if (request->web_bundle_navigation_info()) {
1688 frame_entry->set_web_bundle_navigation_info(
1689 request->web_bundle_navigation_info()->Clone());
1690 }
creis8b5cd4c2015-06-19 00:11:081691
Harkiran Bolaria59290d62021-03-17 01:53:011692 // history.pushState() is classified as a navigation to a new page, but sets
1693 // is_same_document to true. In this case, we already have the title and
1694 // favicon available, so set them immediately.
1695 if (is_same_document && GetLastCommittedEntry()) {
1696 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
1697 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1698 }
[email protected]3a868f212014-08-09 10:41:191699 }
[email protected]ff64b3e2014-05-31 04:07:331700
[email protected]60d6cca2013-04-30 08:47:131701 DCHECK(!params.history_list_was_cleared || !replace_entry);
1702 // The browser requested to clear the session history when it initiated the
1703 // navigation. Now we know that the renderer has updated its state accordingly
1704 // and it is safe to also clear the browser side history.
1705 if (params.history_list_was_cleared) {
arthursonzogni69a6a1b2019-09-17 09:23:001706 DiscardNonCommittedEntries();
[email protected]60d6cca2013-04-30 08:47:131707 entries_.clear();
1708 last_committed_entry_index_ = -1;
1709 }
1710
Nasko Oskovaee2f862018-06-15 00:05:521711 // If this is a new navigation with replacement and there is a
1712 // pending_entry_ which matches the navigation reported by the renderer
1713 // process, then it should be the one replaced, so update the
1714 // last_committed_entry_index_ to use it.
1715 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101716 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521717 last_committed_entry_index_ = pending_entry_index_;
1718 }
1719
Alexander Timine3ec4192020-04-20 16:39:401720 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411721 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401722 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051723
Carlos IL42b416592019-10-07 23:10:361724 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
1725 !request->post_commit_error_page_html().empty());
[email protected]e9ba4472008-09-14 15:42:431726}
1727
Charlie Reisc0f17d2d2021-01-12 18:52:491728void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321729 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071730 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361731 bool is_same_document,
jam48cea9082017-02-15 06:13:291732 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451733 NavigationRequest* request,
Peter Boströmd7592132019-01-30 04:50:311734 bool keep_pending_entry) {
creis26d22632017-04-21 20:23:561735 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1736 << "that a last committed entry exists.";
1737
[email protected]e9ba4472008-09-14 15:42:431738 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001739 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431740
Charlie Reis7e2cb6d2021-01-26 01:27:161741 NavigationEntryImpl* entry = nullptr;
avicbdc4c12015-07-01 16:07:111742 if (params.intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:161743 // We're guaranteed to have a last committed entry if intended_as_new_entry
1744 // is true.
avicbdc4c12015-07-01 16:07:111745 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:161746 DCHECK(entry);
1747
1748 // If the NavigationRequest matches a new pending entry and is classified as
1749 // EXISTING_ENTRY, then it is a navigation to the same URL that was
1750 // converted to a reload, such as a user pressing enter in the omnibox.
1751 if (pending_entry_ && pending_entry_index_ == -1 &&
1752 pending_entry_->GetUniqueID() ==
1753 request->commit_params().nav_entry_id) {
1754 // Note: The pending entry will usually have a real ReloadType here, but
1755 // it can still be ReloadType::NONE in cases that
1756 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
1757
1758 // If we classified this correctly, the SiteInstance should not have
1759 // changed.
1760 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
1761
1762 // For converted reloads, we assign the entry's unique ID to be that of
1763 // the new one. Since this is always the result of a user action, we want
1764 // to dismiss infobars, etc. like a regular user-initiated navigation.
1765 entry->set_unique_id(pending_entry_->GetUniqueID());
1766
1767 // The extra headers may have changed due to reloading with different
1768 // headers.
1769 entry->set_extra_headers(pending_entry_->extra_headers());
1770 }
1771 // Otherwise, this was intended as a new entry but the pending entry was
1772 // lost in the meantime and no new entry was created. We are stuck at the
1773 // last committed entry.
1774
1775 // Even if this is a converted reload from pressing enter in the omnibox,
1776 // the server could redirect, requiring an update to the SSL status. If this
1777 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451778 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:161779 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:471780 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451781 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:161782 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141783
Charlie Reis7e2cb6d2021-01-26 01:27:161784 if (params.url.SchemeIs(url::kHttpsScheme) &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121785 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141786 bool has_cert = !!entry->GetSSL().certificate;
1787 if (is_same_document) {
1788 UMA_HISTOGRAM_BOOLEAN(
1789 "Navigation.SecureSchemeHasSSLStatus."
1790 "ExistingPageSameDocumentIntendedAsNew",
1791 has_cert);
1792 } else {
1793 UMA_HISTOGRAM_BOOLEAN(
1794 "Navigation.SecureSchemeHasSSLStatus."
1795 "ExistingPageDifferentDocumentIntendedAsNew",
1796 has_cert);
1797 }
1798 }
Rakina Zata Amnif6950d552020-11-24 03:26:101799 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:111800 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:101801 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b902016-09-01 16:58:161802
eugenebut604866f2017-05-10 21:35:361803 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451804 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361805 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1806 // this was a restored same document navigation entry, then it won't have
1807 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1808 // navigated it.
jam48cea9082017-02-15 06:13:291809 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
1810 if (entry->GetURL().GetOrigin() == last_entry->GetURL().GetOrigin() &&
1811 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1812 was_restored) {
1813 entry->GetSSL() = last_entry->GetSSL();
1814 }
1815 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451816 // In rapid back/forward navigations |request| sometimes won't have a cert
1817 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191818 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451819 if (request->GetSSLInfo().has_value() &&
1820 request->GetSSLInfo()->is_valid()) {
1821 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
1822 } else if (entry->GetURL().GetOrigin() != request->GetURL().GetOrigin()) {
John Abd-El-Malek3f247082017-12-07 19:02:191823 entry->GetSSL() = SSLStatus();
1824 }
jam48cea9082017-02-15 06:13:291825 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141826
John Abd-El-Malek380d3c5922017-09-08 00:20:311827 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121828 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141829 bool has_cert = !!entry->GetSSL().certificate;
1830 if (is_same_document && was_restored) {
1831 UMA_HISTOGRAM_BOOLEAN(
1832 "Navigation.SecureSchemeHasSSLStatus."
1833 "ExistingPageSameDocumentRestoredBrowserInitiated",
1834 has_cert);
1835 } else if (is_same_document && !was_restored) {
1836 UMA_HISTOGRAM_BOOLEAN(
1837 "Navigation.SecureSchemeHasSSLStatus."
1838 "ExistingPageSameDocumentBrowserInitiated",
1839 has_cert);
1840 } else if (!is_same_document && was_restored) {
1841 UMA_HISTOGRAM_BOOLEAN(
1842 "Navigation.SecureSchemeHasSSLStatus."
1843 "ExistingPageRestoredBrowserInitiated",
1844 has_cert);
1845 } else {
1846 UMA_HISTOGRAM_BOOLEAN(
1847 "Navigation.SecureSchemeHasSSLStatus.ExistingPageBrowserInitiated",
1848 has_cert);
1849 }
1850 }
avicbdc4c12015-07-01 16:07:111851 } else {
1852 // This is renderer-initiated. The only kinds of renderer-initated
Charlie Reisc0f17d2d2021-01-12 18:52:491853 // navigations that are EXISTING_ENTRY are reloads and history.replaceState,
avicbdc4c12015-07-01 16:07:111854 // which land us at the last committed entry.
1855 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101856
Mikel Astizba9cf2fd2017-12-17 10:38:101857 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1858 // to avoid misleading interpretations (e.g. URLs paired with
1859 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1860 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1861 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1862
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571863 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:101864
eugenebut604866f2017-05-10 21:35:361865 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451866 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361867 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471868 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451869 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141870
John Abd-El-Malek380d3c5922017-09-08 00:20:311871 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121872 !request->DidEncounterError()) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141873 bool has_cert = !!entry->GetSSL().certificate;
1874 if (is_same_document) {
1875 UMA_HISTOGRAM_BOOLEAN(
1876 "Navigation.SecureSchemeHasSSLStatus."
1877 "ExistingPageSameDocumentRendererInitiated",
1878 has_cert);
1879 } else {
1880 UMA_HISTOGRAM_BOOLEAN(
1881 "Navigation.SecureSchemeHasSSLStatus."
1882 "ExistingPageDifferentDocumentRendererInitiated",
1883 has_cert);
1884 }
1885 }
avicbdc4c12015-07-01 16:07:111886 }
1887 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:431888
[email protected]5ccd4dc2012-10-24 02:28:141889 // The URL may have changed due to redirects.
Rakina Zata Amni3a1c0ec2021-04-15 03:35:121890 entry->set_page_type(request->DidEncounterError() ? PAGE_TYPE_ERROR
1891 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041892 entry->SetURL(params.url);
arthursonzogni73fe3212020-11-17 13:24:071893 entry->SetReferrer(Referrer(*params.referrer));
[email protected]38178a42009-12-17 18:58:321894 if (entry->update_virtual_url_with_url())
1895 UpdateVirtualURLToURL(entry, params.url);
[email protected]5ccd4dc2012-10-24 02:28:141896
jam015ba062017-01-06 21:17:001897 // The site instance will normally be the same except
1898 // 1) session restore, when no site instance will be assigned or
1899 // 2) redirect, when the site instance is reset.
Lei Zhang96031532019-10-10 19:05:471900 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
naskoaf182192016-08-11 02:12:011901 entry->site_instance() == rfh->GetSiteInstance());
clamy432acb22016-04-15 19:41:431902
naskoaf182192016-08-11 02:12:011903 // Update the existing FrameNavigationEntry to ensure all of its members
1904 // reflect the parameters coming from the renderer process.
Anton Bikineevf62d1bf2021-05-15 17:56:071905 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451906 request->common_params().initiator_origin;
naskoaf182192016-08-11 02:12:011907 entry->AddOrUpdateFrameEntry(
Nate Chapin9f169072021-06-09 19:32:371908 rfh->frame_tree_node(), NavigationEntryImpl::UpdatePolicy::kUpdate,
1909 params.item_sequence_number, params.document_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081910 params.app_history_key, rfh->GetSiteInstance(), nullptr, params.url,
Nate Chapin9f169072021-06-09 19:32:371911 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091912 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071913 params.page_state, params.method, params.post_id,
1914 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031915 request->web_bundle_navigation_info()
1916 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391917 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481918 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191919 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1920 request));
creis22a7b4c2016-04-28 07:20:301921
[email protected]5ccd4dc2012-10-24 02:28:141922 // The redirected to page should not inherit the favicon from the previous
1923 // page.
eugenebut604866f2017-05-10 21:35:361924 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:481925 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:141926
Peter Boströmd7592132019-01-30 04:50:311927 // We should also usually discard the pending entry if it corresponds to a
1928 // different navigation, since that one is now likely canceled. In rare
1929 // cases, we leave the pending entry for another navigation in place when we
1930 // know it is still ongoing, to avoid a flicker in the omnibox (see
1931 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:431932 //
1933 // Note that we need to use the "internal" version since we don't want to
1934 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:311935 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001936 DiscardNonCommittedEntries();
[email protected]40bcc302009-03-02 20:50:391937
Carlos IL4dea8902020-05-26 15:14:291938 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:111939 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:431940}
1941
[email protected]d202a7c2012-01-04 07:53:471942void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:321943 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071944 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361945 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:471946 bool replace_entry,
1947 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451948 NavigationRequest* request) {
avi25f5f9e2015-07-17 20:08:261949 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1950 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
[email protected]09b8f82f2009-06-16 20:22:111951
[email protected]e9ba4472008-09-14 15:42:431952 // Manual subframe navigations just get the current entry cloned so the user
1953 // can go back or forward to it. The actual subframe information will be
1954 // stored in the page state for each of those entries. This happens out of
1955 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:091956 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1957 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:381958
Mikel Astizba9cf2fd2017-12-17 10:38:101959 // The DCHECK below documents the fact that we don't know of any situation
1960 // where |replace_entry| is true for subframe navigations. This simplifies
1961 // reasoning about the replacement struct for subframes (see
1962 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
1963 DCHECK(!replace_entry);
1964
Patrick Monette50e8bd82019-06-13 22:40:451965 // This FrameNavigationEntry might not end up being used in the
1966 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Anton Bikineevf62d1bf2021-05-15 17:56:071967 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451968 request->common_params().initiator_origin;
Patrick Monette50e8bd82019-06-13 22:40:451969 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481970 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:081971 params.document_sequence_number, params.app_history_key,
1972 rfh->GetSiteInstance(), nullptr, params.url,
1973 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:091974 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:071975 params.page_state, params.method, params.post_id,
1976 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031977 request->web_bundle_navigation_info()
1978 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391979 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:481980 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:191981 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1982 request));
Charles Reisf44482022017-10-13 21:15:031983
creisce0ef3572017-01-26 17:53:081984 std::unique_ptr<NavigationEntryImpl> new_entry =
1985 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:451986 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:001987 frame_tree_.root());
creise062d542015-08-25 02:01:551988
Alexander Timine3ec4192020-04-20 16:39:401989 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411990 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401991 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:471992
creisce0ef3572017-01-26 17:53:081993 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:451994 // to replace, which can happen due to a unique name change. See
1995 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
1996 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:381997
Carlos IL42b416592019-10-07 23:10:361998 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false);
[email protected]e9ba4472008-09-14 15:42:431999}
2000
[email protected]d202a7c2012-01-04 07:53:472001bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:322002 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:072003 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:392004 bool is_same_document,
Nate Chapinc7019dd7d2021-06-25 18:29:252005 bool was_on_initial_empty_document,
Mohamed Abdelhalim833de902019-09-16 17:41:452006 NavigationRequest* request) {
avi9f07a0c2015-02-18 22:51:292007 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
2008 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
2009
[email protected]e9ba4472008-09-14 15:42:432010 // We're guaranteed to have a previously committed entry, and we now need to
2011 // handle navigation inside of a subframe in it without creating a new entry.
2012 DCHECK(GetLastCommittedEntry());
2013
creis913c63ce2016-07-16 19:52:522014 // For newly created subframes, we don't need to send a commit notification.
2015 // This is only necessary for history navigations in subframes.
2016 bool send_commit_notification = false;
2017
Rakina Zata Amnif6950d552020-11-24 03:26:102018 // If |nav_entry_id| is non-zero and matches an existing entry, this
2019 // is a history navigation. Update the last committed index accordingly. If
2020 // we don't recognize the |nav_entry_id|, it might be a recently
2021 // pruned entry. We'll handle it below.
2022 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
2023 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:472024 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:062025 // Make sure that a subframe commit isn't changing the main frame's
2026 // origin. Otherwise the renderer process may be confused, leading to a
2027 // URL spoof. We can't check the path since that may change
2028 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:572029 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
2030 // about:blank, file, and unique origins are more subtle to get right.
Charlie Reis95fbca442021-05-21 21:38:242031 // We should use checks similar to RenderFrameHostImpl's
2032 // CanCommitUrlAndOrigin on the main frame during subframe commits.
2033 // See https://crbug.com/1209092.
creis37988b92016-06-10 18:03:572034 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
2035 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
2036 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
2037 dest_top_url.SchemeIsHTTPOrHTTPS() &&
2038 current_top_url.GetOrigin() != dest_top_url.GetOrigin()) {
creisfb6eeb62016-05-10 19:01:512039 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2040 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:062041 }
creis3cdc3b02015-05-29 23:00:472042
creis913c63ce2016-07-16 19:52:522043 // We only need to discard the pending entry in this history navigation
2044 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:062045 last_committed_entry_index_ = entry_index;
arthursonzogni69a6a1b2019-09-17 09:23:002046 DiscardNonCommittedEntries();
creis913c63ce2016-07-16 19:52:522047
2048 // History navigations should send a commit notification.
2049 send_commit_notification = true;
avi98405c22015-05-21 20:47:062050 }
[email protected]e9ba4472008-09-14 15:42:432051 }
[email protected]f233e4232013-02-23 00:55:142052
creisce0ef3572017-01-26 17:53:082053 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
2054 // it may be a "history auto" case where we update an existing one.
2055 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Nate Chapin9f169072021-06-09 19:32:372056
2057 // We may want to update |last_committed|'s FrameNavigationEntry (if one
2058 // exists), or we may want to clobber it and create a new one. We update in
2059 // cases where the corresponding FrameNavigationEntry is conceptually similar
2060 // to the document described by the commit params: same-document
2061 // navigations, history traversal to an existing entry, and reloads (including
2062 // a "soft reload" where we navigate to the same url without flagging it as a
2063 // reload). But in the case of a different document that is not logically
2064 // related to the committed FrameNavigationEntry's document (cross-document,
2065 // not same url, not a reload, not a history traversal), we replace rather
2066 // than update.
Nate Chapinc7019dd7d2021-06-25 18:29:252067 //
Nate Chapin9f169072021-06-09 19:32:372068 // In the case where we update, the FrameNavigationEntry will potentially be
2069 // shared across multiple NavigationEntries, and any updates will be reflected
2070 // in all of those NavigationEntries. In the replace case, any existing
2071 // sharing with other NavigationEntries will stop.
Nate Chapinc7019dd7d2021-06-25 18:29:252072 //
2073 // When navigating away from the initial empty document, we also update rather
2074 // than replace. Either update or replace will overwrite the initial empty
2075 // document state for |last_committed|, but if the FrameNavigationEntry for
2076 // the initial empty document is shared across multiple NavigationEntries (due
2077 // to a navigation in another frame), we want to make sure we overwrite the
2078 // initial empty document state everywhere this FrameNavigationEntry is used,
2079 // which is accompished by updating the existing FrameNavigationEntry.
Nate Chapin9f169072021-06-09 19:32:372080 FrameNavigationEntry* last_committed_frame_entry =
2081 last_committed->GetFrameEntry(rfh->frame_tree_node());
2082 NavigationEntryImpl::UpdatePolicy update_policy =
2083 NavigationEntryImpl::UpdatePolicy::kUpdate;
2084 if (request->common_params().navigation_type ==
Minggang Wangb9f3fa92021-07-01 15:30:312085 blink::mojom::NavigationType::DIFFERENT_DOCUMENT &&
Nate Chapin9f169072021-06-09 19:32:372086 last_committed_frame_entry &&
Nate Chapinc7019dd7d2021-06-25 18:29:252087 last_committed_frame_entry->url() != params.url &&
2088 !was_on_initial_empty_document) {
Nate Chapin9f169072021-06-09 19:32:372089 update_policy = NavigationEntryImpl::UpdatePolicy::kReplace;
2090 }
2091
Anton Bikineevf62d1bf2021-05-15 17:56:072092 const absl::optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:452093 request->common_params().initiator_origin;
creisce0ef3572017-01-26 17:53:082094 last_committed->AddOrUpdateFrameEntry(
Nate Chapin9f169072021-06-09 19:32:372095 rfh->frame_tree_node(), update_policy, params.item_sequence_number,
Nate Chapinfbfe5af2021-06-10 17:22:082096 params.document_sequence_number, params.app_history_key,
2097 rfh->GetSiteInstance(), nullptr, params.url,
2098 GetCommittedOriginForFrameEntry(params, request),
Rakina Zata Amni82fafba2021-03-11 07:07:092099 Referrer(*params.referrer), initiator_origin, request->GetRedirectChain(),
arthursonzogni73fe3212020-11-17 13:24:072100 params.page_state, params.method, params.post_id,
2101 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:032102 request->web_bundle_navigation_info()
2103 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:392104 : nullptr,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482105 request->GetSubresourceWebBundleNavigationInfo(),
Titouan Rigoudy6ec70402021-02-02 15:42:192106 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
2107 request));
creis625a0c7d2015-03-24 23:17:122108
creis913c63ce2016-07-16 19:52:522109 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432110}
2111
[email protected]d202a7c2012-01-04 07:53:472112int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232113 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032114 for (size_t i = 0; i < entries_.size(); ++i) {
2115 if (entries_[i].get() == entry)
2116 return i;
2117 }
2118 return -1;
[email protected]765b35502008-08-21 00:51:202119}
2120
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572121void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242122 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572123 NavigationControllerImpl* source =
2124 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572125 // Verify that we look new.
Lei Zhang96031532019-10-10 19:05:472126 DCHECK_EQ(0, GetEntryCount());
2127 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:572128
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572129 if (source->GetEntryCount() == 0)
[email protected]ce3fa3c2009-04-20 19:55:572130 return; // Nothing new to do.
2131
Francois Dorayeaace782017-06-21 16:37:242132 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442133 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572134 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572135
Fergal Dalya1d569972021-03-16 03:24:532136 for (auto& it : source->session_storage_namespace_map_) {
[email protected]fdac6ade2013-07-20 01:06:302137 SessionStorageNamespaceImpl* source_namespace =
Fergal Dalya1d569972021-03-16 03:24:532138 static_cast<SessionStorageNamespaceImpl*>(it.second.get());
2139 session_storage_namespace_map_[it.first] = source_namespace->Clone();
Aaron Colwellb731a0ae2021-03-19 19:14:472140 OnStoragePartitionIdAdded(it.first);
[email protected]fdac6ade2013-07-20 01:06:302141 }
[email protected]4e6419c2010-01-15 04:50:342142
Lukasz Anforowicz0de0f452020-12-02 19:57:152143 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572144}
2145
Aran Gilman37d11632019-10-08 23:07:152146void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2147 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162148 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012149 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162150
[email protected]d202a7c2012-01-04 07:53:472151 NavigationControllerImpl* source =
2152 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252153
avi2b177592014-12-10 02:08:022154 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012155 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252156
[email protected]474f8512013-05-31 22:31:162157 // We now have one entry, possibly with a new pending entry. Ensure that
2158 // adding the entries from source won't put us over the limit.
2159 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572160 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572161 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252162
Carlos IL4dea8902020-05-26 15:14:292163 // Insert the entries from source. Ignore any pending entry, since it has not
2164 // committed in source.
[email protected]474f8512013-05-31 22:31:162165 int max_source_index = source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:252166 if (max_source_index == -1)
[email protected]a26023822011-12-29 00:23:552167 max_source_index = source->GetEntryCount();
[email protected]e1cd5452010-08-26 18:03:252168 else
2169 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572170
2171 // Ignore the source's current entry if merging with replacement.
2172 // TODO(davidben): This should preserve entries forward of the current
2173 // too. http://crbug.com/317872
2174 if (replace_entry && max_source_index > 0)
2175 max_source_index--;
2176
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572177 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252178
2179 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552180 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142181
Hayato Ito2c8c08d02021-06-23 03:38:432182 BroadcastHistoryOffsetAndLength();
[email protected]e1cd5452010-08-26 18:03:252183}
2184
[email protected]79368982013-11-13 01:11:012185bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162186 // If there is no last committed entry, we cannot prune. Even if there is a
2187 // pending entry, it may not commit, leaving this WebContents blank, despite
2188 // possibly giving it new entries via CopyStateFromAndPrune.
2189 if (last_committed_entry_index_ == -1)
2190 return false;
[email protected]9350602e2013-02-26 23:27:442191
[email protected]474f8512013-05-31 22:31:162192 // We cannot prune if there is a pending entry at an existing entry index.
2193 // It may not commit, so we have to keep the last committed entry, and thus
2194 // there is no sensible place to keep the pending entry. It is ok to have
2195 // a new pending entry, which can optionally commit as a new navigation.
2196 if (pending_entry_index_ != -1)
2197 return false;
2198
[email protected]474f8512013-05-31 22:31:162199 return true;
2200}
2201
[email protected]79368982013-11-13 01:11:012202void NavigationControllerImpl::PruneAllButLastCommitted() {
2203 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162204
avi2b177592014-12-10 02:08:022205 DCHECK_EQ(0, last_committed_entry_index_);
2206 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442207
Hayato Ito2c8c08d02021-06-23 03:38:432208 BroadcastHistoryOffsetAndLength();
[email protected]9350602e2013-02-26 23:27:442209}
2210
[email protected]79368982013-11-13 01:11:012211void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162212 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012213 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262214
[email protected]474f8512013-05-31 22:31:162215 // Erase all entries but the last committed entry. There may still be a
2216 // new pending entry after this.
2217 entries_.erase(entries_.begin(),
2218 entries_.begin() + last_committed_entry_index_);
2219 entries_.erase(entries_.begin() + 1, entries_.end());
2220 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262221}
2222
Christian Dullweber1af31e62018-02-22 11:49:482223void NavigationControllerImpl::DeleteNavigationEntries(
2224 const DeletionPredicate& deletionPredicate) {
2225 // It is up to callers to check the invariants before calling this.
2226 CHECK(CanPruneAllButLastCommitted());
2227 std::vector<int> delete_indices;
2228 for (size_t i = 0; i < entries_.size(); i++) {
2229 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572230 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482231 delete_indices.push_back(i);
2232 }
2233 }
2234 if (delete_indices.empty())
2235 return;
2236
2237 if (delete_indices.size() == GetEntryCount() - 1U) {
2238 PruneAllButLastCommitted();
2239 } else {
2240 // Do the deletion in reverse to preserve indices.
2241 for (auto it = delete_indices.rbegin(); it != delete_indices.rend(); ++it) {
2242 RemoveEntryAtIndex(*it);
2243 }
Hayato Ito2c8c08d02021-06-23 03:38:432244 BroadcastHistoryOffsetAndLength();
Christian Dullweber1af31e62018-02-22 11:49:482245 }
2246 delegate()->NotifyNavigationEntriesDeleted();
2247}
2248
Shivani Sharma883f5f32019-02-12 18:20:012249bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2250 auto* entry = GetEntryAtIndex(index);
2251 return entry && entry->should_skip_on_back_forward_ui();
2252}
2253
Carlos Caballero35ce710c2019-09-19 10:59:452254BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2255 return back_forward_cache_;
2256}
2257
clamy987a3752018-05-03 17:36:262258void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2259 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2260 // progress, since this will cause a use-after-free. (We only allow this
2261 // when the tab is being destroyed for shutdown, since it won't return to
2262 // NavigateToEntry in that case.) http://crbug.com/347742.
2263 CHECK(!in_navigate_to_pending_entry_ || delegate_->IsBeingDestroyed());
2264
2265 if (was_failure && pending_entry_) {
2266 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2267 } else {
2268 failed_pending_entry_id_ = 0;
2269 }
2270
2271 if (pending_entry_) {
2272 if (pending_entry_index_ == -1)
2273 delete pending_entry_;
2274 pending_entry_index_ = -1;
2275 pending_entry_ = nullptr;
2276 }
arthursonzogni66f711c2019-10-08 14:40:362277
2278 // Ensure any refs to the current pending entry are ignored if they get
2279 // deleted, by clearing the set of known refs. All future pending entries will
2280 // only be affected by new refs.
2281 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262282}
2283
2284void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2285 if (pending_entry_)
2286 pending_entry_->set_ssl_error(error);
2287}
2288
Camille Lamy5193caa2018-10-12 11:59:422289#if defined(OS_ANDROID)
2290// static
2291bool NavigationControllerImpl::ValidateDataURLAsString(
2292 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2293 if (!data_url_as_string)
2294 return false;
2295
2296 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2297 return false;
2298
2299 // The number of characters that is enough for validating a data: URI.
2300 // From the GURL's POV, the only important part here is scheme, it doesn't
2301 // check the actual content. Thus we can take only the prefix of the url, to
2302 // avoid unneeded copying of a potentially long string.
2303 const size_t kDataUriPrefixMaxLen = 64;
2304 GURL data_url(
2305 std::string(data_url_as_string->front_as<char>(),
2306 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2307 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2308 return false;
2309
2310 return true;
2311}
2312#endif
2313
Shivani Sharma194877032019-03-07 17:52:472314void NavigationControllerImpl::NotifyUserActivation() {
2315 // When a user activation occurs, ensure that all adjacent entries for the
2316 // same document clear their skippable bit, so that the history manipulation
2317 // intervention does not apply to them.
Shivani Sharma194877032019-03-07 17:52:472318 auto* last_committed_entry = GetLastCommittedEntry();
2319 if (!last_committed_entry)
2320 return;
Shivani Sharma194877032019-03-07 17:52:472321
shivanigithub99368382021-06-16 18:33:372322 if (base::FeatureList::IsEnabled(
2323 features::kDebugHistoryInterventionNoUserActivation)) {
2324 return;
2325 }
2326
Shivani Sharmac4cc8922019-04-18 03:11:172327 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472328}
2329
clamy987a3752018-05-03 17:36:262330bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2331 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172332 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262333 NavigationEntryImpl* entry =
2334 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2335 if (!entry)
2336 return false;
2337
2338 FrameNavigationEntry* frame_entry =
2339 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2340 if (!frame_entry)
2341 return false;
2342
Camille Lamy5193caa2018-10-12 11:59:422343 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572344 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232345 ReloadType::NONE, false /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422346 true /* is_history_navigation_in_new_child */);
clamyea99ea12018-05-28 13:54:232347
2348 if (!request)
2349 return false;
2350
arthursonzognif046d4a2019-12-12 19:08:102351 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412352
Lukasz Anforowicz9ee83c272020-12-01 20:14:052353 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2354 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232355
2356 return true;
clamy987a3752018-05-03 17:36:262357}
2358
Tsuyoshi Horo52fd08e2020-07-07 07:03:452359bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2360 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2361 if (!entry)
2362 return false;
2363 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2364 if (!frame_entry)
2365 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142366 ReloadType reload_type = ReloadType::NORMAL;
2367 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452368 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142369 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452370 false /* is_same_document_history_load */,
2371 false /* is_history_navigation_in_new_child */);
2372 if (!request)
2373 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052374 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452375 return true;
2376}
2377
Dave Tapuska8bfd84c2019-03-26 20:47:162378void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2379 int offset,
2380 int sandbox_frame_tree_node_id) {
2381 if (!CanGoToOffset(offset))
2382 return;
2383 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id);
2384}
2385
clamy987a3752018-05-03 17:36:262386void NavigationControllerImpl::NavigateFromFrameProxy(
2387 RenderFrameHostImpl* render_frame_host,
2388 const GURL& url,
Chris Hamilton83272dc2021-02-23 00:24:022389 const blink::LocalFrameToken* initiator_frame_token,
Antonio Sartori9a82f6f32020-12-14 09:22:452390 int initiator_process_id,
Anton Bikineevf62d1bf2021-05-15 17:56:072391 const absl::optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262392 bool is_renderer_initiated,
2393 SiteInstance* source_site_instance,
2394 const Referrer& referrer,
2395 ui::PageTransition page_transition,
2396 bool should_replace_current_entry,
Yeunjoo Choi3df791a2021-02-17 07:07:252397 blink::NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262398 const std::string& method,
2399 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092400 const std::string& extra_headers,
Antonio Sartori2f763d9d2021-04-21 10:04:142401 network::mojom::SourceLocationPtr source_location,
John Delaney50425f82020-04-07 16:26:212402 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Anton Bikineevf62d1bf2021-05-15 17:56:072403 const absl::optional<blink::Impression>& impression) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582404 if (is_renderer_initiated)
2405 DCHECK(initiator_origin.has_value());
2406
clamy987a3752018-05-03 17:36:262407 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582408
Rakina Zata Amni66992a82020-06-24 03:57:522409 // Don't allow an entry replacement if there is no entry to replace.
2410 // http://crbug.com/457149
2411 if (GetEntryCount() == 0)
2412 should_replace_current_entry = false;
2413
clamy987a3752018-05-03 17:36:262414 // Create a NavigationEntry for the transfer, without making it the pending
2415 // entry. Subframe transfers should have a clone of the last committed entry
2416 // with a FrameNavigationEntry for the target frame. Main frame transfers
2417 // should have a new NavigationEntry.
2418 // TODO(creis): Make this unnecessary by creating (and validating) the params
2419 // directly, passing them to the destination RenderFrameHost. See
2420 // https://crbug.com/536906.
2421 std::unique_ptr<NavigationEntryImpl> entry;
2422 if (!node->IsMainFrame()) {
2423 // Subframe case: create FrameNavigationEntry.
2424 if (GetLastCommittedEntry()) {
2425 entry = GetLastCommittedEntry()->Clone();
2426 entry->set_extra_headers(extra_headers);
2427 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2428 // Renderer-initiated navigation that target a remote frame are currently
2429 // classified as browser-initiated when this one has already navigated.
2430 // See https://crbug.com/722251.
2431 } else {
2432 // If there's no last committed entry, create an entry for about:blank
2433 // with a subframe entry for our destination.
2434 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2435 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:062436 GURL(url::kAboutBlankURL), referrer, initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:102437 source_site_instance, page_transition, is_renderer_initiated,
2438 extra_headers, browser_context_,
Hayato Ito303654c2021-06-30 09:07:542439 nullptr /* blob_url_loader_factory */));
clamy987a3752018-05-03 17:36:262440 }
Nate Chapin9f169072021-06-09 19:32:372441 // The UpdatePolicy doesn't matter here. |entry| is only used as a parameter
2442 // to CreateNavigationRequestFromLoadParams(), so while kReplace might
2443 // remove child FrameNavigationEntries (e.g., if this is a cross-process
2444 // same-document navigation), they will still be present in the
2445 // committed NavigationEntry that will be referenced to construct the new
2446 // FrameNavigationEntry tree when this navigation commits.
clamy987a3752018-05-03 17:36:262447 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:082448 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582449 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072450 absl::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202451 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392452 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482453 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori79d549d2021-02-18 12:59:542454 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262455 } else {
2456 // Main frame case.
2457 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102458 url, referrer, initiator_origin, source_site_instance, page_transition,
2459 is_renderer_initiated, extra_headers, browser_context_,
Hayato Ito303654c2021-06-30 09:07:542460 blob_url_loader_factory));
clamy987a3752018-05-03 17:36:262461 entry->root_node()->frame_entry->set_source_site_instance(
2462 static_cast<SiteInstanceImpl*>(source_site_instance));
2463 entry->root_node()->frame_entry->set_method(method);
2464 }
clamy987a3752018-05-03 17:36:262465
Camille Lamy5193caa2018-10-12 11:59:422466 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262467 if (GetLastCommittedEntry() &&
2468 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2469 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422470 override_user_agent = true;
clamy987a3752018-05-03 17:36:262471 }
2472 // TODO(creis): Set user gesture and intent received timestamp on Android.
2473
2474 // We may not have successfully added the FrameNavigationEntry to |entry|
2475 // above (per https://crbug.com/608402), in which case we create it from
2476 // scratch. This works because we do not depend on |frame_entry| being inside
2477 // |entry| during NavigateToEntry. This will go away when we shortcut this
2478 // further in https://crbug.com/536906.
2479 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2480 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452481 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Nate Chapinfbfe5af2021-06-10 17:22:082482 node->unique_name(), -1, -1, "", nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582483 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Anton Bikineevf62d1bf2021-05-15 17:56:072484 absl::nullopt /* origin */, referrer, initiator_origin,
Rakina Zata Amni3a1c0ec2021-04-15 03:35:122485 std::vector<GURL>(), blink::PageState(), method, -1,
2486 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:482487 nullptr /* subresource_web_bundle_navigation_info */,
Titouan Rigoudy6ec70402021-02-02 15:42:192488 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262489 }
2490
Camille Lamy5193caa2018-10-12 11:59:422491 LoadURLParams params(url);
Chris Hamilton83272dc2021-02-23 00:24:022492 params.initiator_frame_token = base::OptionalFromPtr(initiator_frame_token);
Antonio Sartori9a82f6f32020-12-14 09:22:452493 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292494 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422495 params.source_site_instance = source_site_instance;
2496 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2497 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032498 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422499 params.referrer = referrer;
2500 /* params.redirect_chain: skip */
2501 params.extra_headers = extra_headers;
2502 params.is_renderer_initiated = is_renderer_initiated;
2503 params.override_user_agent = UA_OVERRIDE_INHERIT;
2504 /* params.base_url_for_data_url: skip */
2505 /* params.virtual_url_for_data_url: skip */
2506 /* params.data_url_as_string: skip */
2507 params.post_data = post_body;
2508 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582509 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422510 /* params.frame_name: skip */
2511 // TODO(clamy): See if user gesture should be propagated to this function.
2512 params.has_user_gesture = false;
2513 params.should_clear_history_list = false;
2514 params.started_from_context_menu = false;
2515 /* params.navigation_ui_data: skip */
2516 /* params.input_start: skip */
Minggang Wangf59db47b2021-06-16 01:56:222517 params.was_activated = blink::mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542518 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212519 params.impression = impression;
Camille Lamy5193caa2018-10-12 11:59:422520
2521 std::unique_ptr<NavigationRequest> request =
2522 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032523 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:142524 false /* has_user_gesture */, std::move(source_location),
Scott Violetcf6ea7e2021-06-09 21:09:212525 download_policy, ReloadType::NONE, entry.get(), frame_entry.get());
clamyea99ea12018-05-28 13:54:232526
2527 if (!request)
2528 return;
2529
Arthur Hemery948742762019-09-18 10:06:242530 // At this stage we are proceeding with this navigation. If this was renderer
2531 // initiated with user gesture, we need to make sure we clear up potential
2532 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2533 DiscardNonCommittedEntries();
2534
Lukasz Anforowicz9ee83c272020-12-01 20:14:052535 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262536}
2537
[email protected]d1198fd2012-08-13 22:50:192538void NavigationControllerImpl::SetSessionStorageNamespace(
Aaron Colwellf3b316e2021-03-11 20:17:052539 const StoragePartitionId& partition_id,
[email protected]8ff00d72012-10-23 19:12:212540 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192541 if (!session_storage_namespace)
2542 return;
2543
2544 // We can't overwrite an existing SessionStorage without violating spec.
2545 // Attempts to do so may give a tab access to another tab's session storage
2546 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152547 bool successful_insert =
2548 session_storage_namespace_map_
Aaron Colwellf3b316e2021-03-11 20:17:052549 .insert(std::make_pair(partition_id,
2550 static_cast<SessionStorageNamespaceImpl*>(
2551 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302552 .second;
2553 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
Aaron Colwellb731a0ae2021-03-19 19:14:472554 OnStoragePartitionIdAdded(partition_id);
[email protected]d1198fd2012-08-13 22:50:192555}
2556
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572557bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Aran Gilman37d11632019-10-08 23:07:152558 return IsInitialNavigation() && !GetLastCommittedEntry() &&
Carlos Caballeroede6f8c2021-01-28 11:01:502559 !frame_tree_.has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462560}
2561
Aran Gilman37d11632019-10-08 23:07:152562SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
Aaron Colwell78b4bde2021-03-16 16:16:092563 const SiteInfo& site_info) {
2564 // TODO(acolwell): Remove partition_id logic once we have successfully
2565 // migrated the implementation to be a StoragePartitionConfig. At that point
2566 // |site_info| can be replaced with a StoragePartitionConfig.
2567 const StoragePartitionId partition_id =
2568 site_info.GetStoragePartitionId(browser_context_);
2569 const StoragePartitionConfig partition_config =
2570 site_info.GetStoragePartitionConfig(browser_context_);
[email protected]d1198fd2012-08-13 22:50:192571
[email protected]fdac6ade2013-07-20 01:06:302572 StoragePartition* partition =
Lukasz Anforowiczb9a969a2021-04-29 15:26:252573 browser_context_->GetStoragePartition(partition_config);
michaelnbacbcbd2016-02-09 00:32:032574 DOMStorageContextWrapper* context_wrapper =
2575 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2576
2577 SessionStorageNamespaceMap::const_iterator it =
2578 session_storage_namespace_map_.find(partition_id);
2579 if (it != session_storage_namespace_map_.end()) {
2580 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152581 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2582 ->IsFromContext(context_wrapper));
Aaron Colwellb731a0ae2021-03-19 19:14:472583
2584 // Verify that the config we generated now matches the one that
2585 // was generated when the namespace was added to the map.
Aaron Colwellb731a0ae2021-03-19 19:14:472586 if (partition_config != it->first.config()) {
2587 LogStoragePartitionIdCrashKeys(it->first, partition_id);
2588 }
Sharon Yangd4f48792021-06-30 17:02:532589 CHECK_EQ(partition_config, it->first.config());
Aaron Colwellb731a0ae2021-03-19 19:14:472590
michaelnbacbcbd2016-02-09 00:32:032591 return it->second.get();
2592 }
2593
2594 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102595 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2596 SessionStorageNamespaceImpl::Create(context_wrapper);
2597 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2598 session_storage_namespace.get();
2599 session_storage_namespace_map_[partition_id] =
2600 std::move(session_storage_namespace);
Aaron Colwellb731a0ae2021-03-19 19:14:472601 OnStoragePartitionIdAdded(partition_id);
[email protected]fdac6ade2013-07-20 01:06:302602
Daniel Murphy31bbb8b12018-02-07 21:44:102603 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302604}
2605
2606SessionStorageNamespace*
2607NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
Aaron Colwell78b4bde2021-03-16 16:16:092608 return GetSessionStorageNamespace(SiteInfo());
[email protected]fdac6ade2013-07-20 01:06:302609}
2610
2611const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572612NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302613 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552614}
[email protected]d202a7c2012-01-04 07:53:472615
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572616bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562617 return needs_reload_;
2618}
[email protected]a26023822011-12-29 00:23:552619
[email protected]46bb5e9c2013-10-03 22:16:472620void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412621 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2622}
2623
2624void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472625 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412626 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542627
2628 if (last_committed_entry_index_ != -1) {
2629 entries_[last_committed_entry_index_]->SetTransitionType(
2630 ui::PAGE_TRANSITION_RELOAD);
2631 }
[email protected]46bb5e9c2013-10-03 22:16:472632}
2633
[email protected]d202a7c2012-01-04 07:53:472634void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572635 DCHECK_LT(index, GetEntryCount());
2636 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312637 DiscardNonCommittedEntries();
2638
2639 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122640 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312641 last_committed_entry_index_--;
2642}
2643
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572644NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272645 // If there is no pending_entry_, there should be no pending_entry_index_.
2646 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2647
2648 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362649 // at that index. An exception is while a reload of a post commit error page
2650 // is ongoing; in that case pending entry will point to the entry replaced
2651 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272652 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362653 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2654 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272655
[email protected]022af742011-12-28 18:37:252656 return pending_entry_;
2657}
2658
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572659int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272660 // The pending entry index must always be less than the number of entries.
2661 // If there are no entries, it must be exactly -1.
2662 DCHECK_LT(pending_entry_index_, GetEntryCount());
2663 DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:552664 return pending_entry_index_;
2665}
2666
avi25764702015-06-23 15:43:372667void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572668 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362669 bool replace,
2670 bool was_post_commit_error) {
mastiz7eddb5f72016-06-23 09:52:452671 DCHECK(!ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2672 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202673
avi5cad4912015-06-19 05:25:442674 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2675 // need to keep continuity with the pending entry, so copy the pending entry's
2676 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2677 // then the renderer navigated on its own, independent of the pending entry,
2678 // so don't copy anything.
2679 if (pending_entry_ && pending_entry_index_ == -1)
2680 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202681
arthursonzogni69a6a1b2019-09-17 09:23:002682 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:202683
creisee17e932015-07-17 17:56:222684 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362685 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102686 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572687 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362688 // If the new entry is a post-commit error page, we store the current last
2689 // committed entry to the side so that we can put it back when navigating
2690 // away from the error.
2691 if (was_post_commit_error) {
2692 DCHECK(!entry_replaced_by_post_commit_error_);
2693 entry_replaced_by_post_commit_error_ =
2694 std::move(entries_[last_committed_entry_index_]);
2695 }
dcheng36b6aec92015-12-26 06:16:362696 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222697 return;
2698 }
[email protected]765b35502008-08-21 00:51:202699
creis37979a62015-08-04 19:48:182700 // We shouldn't see replace == true when there's no committed entries.
2701 DCHECK(!replace);
2702
Michael Thiessen9b14d512019-09-23 21:19:472703 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202704
Shivani Sharmad8c8d652019-02-13 17:27:572705 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202706
dcheng36b6aec92015-12-26 06:16:362707 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202708 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292709}
2710
Shivani Sharmad8c8d652019-02-13 17:27:572711void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162712 if (entries_.size() < max_entry_count())
2713 return;
2714
2715 DCHECK_EQ(max_entry_count(), entries_.size());
2716 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572717 CHECK_EQ(pending_entry_index_, -1);
2718
2719 int index = 0;
Elly Fong-Jonesccc6d1f2021-06-14 18:32:422720 // Retrieve the oldest skippable entry.
2721 for (; index < GetEntryCount(); index++) {
2722 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2723 break;
Shivani Sharmad8c8d652019-02-13 17:27:572724 }
2725
2726 // If there is no skippable entry or if it is the last committed entry then
2727 // fall back to pruning the oldest entry. It is not safe to prune the last
2728 // committed entry.
2729 if (index == GetEntryCount() || index == last_committed_entry_index_)
2730 index = 0;
2731
2732 bool should_succeed = RemoveEntryAtIndex(index);
2733 DCHECK_EQ(true, should_succeed);
2734
2735 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252736}
2737
clamy3cb9bea92018-07-10 12:42:022738void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162739 ReloadType reload_type,
2740 int sandboxed_source_frame_tree_node_id) {
Alexander Timin3a92df72019-09-20 11:59:502741 TRACE_EVENT0("navigation",
2742 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272743 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022744 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362745 if (pending_entry_index_ != -1) {
2746 // The pending entry may not be in entries_ if a post-commit error page is
2747 // showing.
2748 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2749 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2750 }
Gyuyoung Kim107c2a02021-04-13 01:49:302751 DCHECK(!blink::IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572752 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:012753 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:002754 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:572755 int nav_entry_id = pending_entry_->GetUniqueID();
2756
[email protected]83c2e232011-10-07 21:36:462757 // If we were navigating to a slow-to-commit page, and the user performs
2758 // a session history navigation to the last committed page, RenderViewHost
2759 // will force the throbber to start, but WebKit will essentially ignore the
2760 // navigation, and won't send a message to stop the throbber. To prevent this
2761 // from happening, we drop the navigation here and stop the slow-to-commit
2762 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022763 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:082764 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:272765 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
Takashi Toyoshimabf549472021-07-01 07:47:202766 frame_tree_.StopLoading();
[email protected]6a13a6c2011-12-20 21:47:122767
[email protected]83c2e232011-10-07 21:36:462768 DiscardNonCommittedEntries();
2769 return;
2770 }
2771
creisce0ef3572017-01-26 17:53:082772 // Compare FrameNavigationEntries to see which frames in the tree need to be
2773 // navigated.
clamy3cb9bea92018-07-10 12:42:022774 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2775 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Alex Moshchuk3a4e77a2020-05-29 21:32:572776 FindFramesToNavigate(root, reload_type, &same_document_loads,
2777 &different_document_loads);
creis4e2ecb72015-06-20 00:46:302778
2779 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572780 // We were unable to match any frames to navigate. This can happen if a
2781 // history navigation targets a subframe that no longer exists
2782 // (https://crbug.com/705550). In this case, we need to update the current
2783 // history entry to the pending one but keep the main document loaded. We
2784 // also need to ensure that observers are informed about the updated
2785 // current history entry (e.g., for greying out back/forward buttons), and
2786 // that renderer processes update their history offsets. The easiest way
2787 // to do all that is to schedule a "redundant" same-document navigation in
2788 // the main frame.
2789 //
2790 // Note that we don't want to remove this history entry, as it might still
2791 // be valid later, since a frame that it's targeting may be recreated.
2792 //
2793 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
2794 // need to repeat history navigations until finding the one that works.
2795 // Consider changing this behavior to keep looking for the first valid
2796 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:022797 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422798 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572799 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:572800 ReloadType::NONE /* reload_type */,
2801 true /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422802 false /* is_history_navigation_in_new_child */);
clamy3cb9bea92018-07-10 12:42:022803 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572804 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:022805 DiscardPendingEntry(false);
2806 return;
2807 }
Alex Moshchuk3a4e77a2020-05-29 21:32:572808 same_document_loads.push_back(std::move(navigation_request));
2809
2810 // Sanity check that we never take this branch for any kinds of reloads,
2811 // as those should've queued a different-document load in the main frame.
2812 DCHECK(!is_forced_reload);
2813 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:302814 }
2815
Dave Tapuska8bfd84c2019-03-26 20:47:162816 // If |sandboxed_source_frame_node_id| is valid, then track whether this
2817 // navigation affects any frame outside the frame's subtree.
2818 if (sandboxed_source_frame_tree_node_id !=
2819 FrameTreeNode::kFrameTreeNodeInvalidId) {
2820 bool navigates_inside_tree =
2821 DoesSandboxNavigationStayWithinSubtree(
2822 sandboxed_source_frame_tree_node_id, same_document_loads) &&
2823 DoesSandboxNavigationStayWithinSubtree(
2824 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:502825 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:162826 // the number of pages that trigger this.
2827 FrameTreeNode* sandbox_source_frame_tree_node =
2828 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
2829 if (sandbox_source_frame_tree_node) {
2830 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
2831 sandbox_source_frame_tree_node->current_frame_host(),
2832 navigates_inside_tree
2833 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
2834 : blink::mojom::WebFeature::
2835 kSandboxBackForwardAffectsFramesOutsideSubtree);
2836 }
Dave Tapuska855c1e12019-08-23 20:45:522837
2838 // If the navigation occurred outside the tree discard it because
2839 // the sandboxed frame didn't have permission to navigate outside
2840 // its tree. If it is possible that the navigation is both inside and
2841 // outside the frame tree and we discard it entirely because we don't
2842 // want to end up in a history state that didn't exist before.
2843 if (base::FeatureList::IsEnabled(
2844 features::kHistoryPreventSandboxedNavigation) &&
2845 !navigates_inside_tree) {
2846 DiscardPendingEntry(false);
2847 return;
2848 }
Dave Tapuska8bfd84c2019-03-26 20:47:162849 }
2850
Carlos Caballero539a421c2020-07-06 10:25:572851 // BackForwardCache:
2852 // Navigate immediately if the document is in the BackForwardCache.
2853 if (back_forward_cache_.GetEntry(nav_entry_id)) {
2854 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
2855 DCHECK_EQ(reload_type, ReloadType::NONE);
2856 auto navigation_request = CreateNavigationRequestFromEntry(
2857 root, pending_entry_, pending_entry_->GetFrameEntry(root),
2858 ReloadType::NONE, false /* is_same_document_history_load */,
2859 false /* is_history_navigation_in_new_child */);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052860 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:572861
2862 return;
2863 }
2864
2865 // History navigation might try to reuse a specific BrowsingInstance, already
2866 // used by a page in the cache. To avoid having two different main frames that
2867 // live in the same BrowsingInstance, evict the all pages with this
2868 // BrowsingInstance from the cache.
2869 //
2870 // For example, take the following scenario:
2871 //
2872 // A1 = Some page on a.com
2873 // A2 = Some other page on a.com
2874 // B3 = An uncacheable page on b.com
2875 //
2876 // Then the following navigations occur:
2877 // A1->A2->B3->A1
2878 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
2879 // take its place) and A1 will be created in the same BrowsingInstance (and
2880 // SiteInstance), as A2.
2881 //
2882 // If we didn't do anything, both A1 and A2 would remain alive in the same
2883 // BrowsingInstance/SiteInstance, which is unsupported by
2884 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
2885 // A2 from the cache.
2886 if (pending_entry_->site_instance()) {
2887 back_forward_cache_.EvictFramesInRelatedSiteInstances(
2888 pending_entry_->site_instance());
2889 }
2890
clamy3cb9bea92018-07-10 12:42:022891 // This call does not support re-entrancy. See http://crbug.com/347742.
2892 CHECK(!in_navigate_to_pending_entry_);
2893 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:302894
arthursonzogni66f711c2019-10-08 14:40:362895 // It is not possible to delete the pending NavigationEntry while navigating
2896 // to it. Grab a reference to delay potential deletion until the end of this
2897 // function.
2898 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
2899
creis4e2ecb72015-06-20 00:46:302900 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:022901 for (auto& item : same_document_loads) {
2902 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052903 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302904 }
clamy3cb9bea92018-07-10 12:42:022905 for (auto& item : different_document_loads) {
2906 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052907 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302908 }
clamy3cb9bea92018-07-10 12:42:022909
2910 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:302911}
2912
Alex Moshchuk3a4e77a2020-05-29 21:32:572913NavigationControllerImpl::HistoryNavigationAction
2914NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:302915 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:572916 ReloadType reload_type) {
Sreeja Kamishetty8eacabb2021-03-09 11:45:422917 RenderFrameHostImpl* render_frame_host = frame->current_frame_host();
Sreeja Kamishettydb8e2892021-03-10 09:30:582918 // Only active and prerendered documents are allowed to navigate in their
2919 // frame.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422920 if (render_frame_host->lifecycle_state() !=
Sreeja Kamishetty299329ad2021-03-25 14:06:012921 RenderFrameHostImpl::LifecycleStateImpl::kPrerendering) {
Sreeja Kamishettydb8e2892021-03-10 09:30:582922 // - If the document is in pending deletion, the browser already committed
2923 // to destroying this RenderFrameHost. See https://crbug.com/930278.
2924 // - If the document is in back-forward cache, it's not allowed to navigate
2925 // as it should remain frozen. Ignore the request and evict the document
2926 // from back-forward cache.
Sreeja Kamishetty8eacabb2021-03-09 11:45:422927 //
Sreeja Kamishettydb8e2892021-03-10 09:30:582928 // If the document is inactive, there's no need to recurse into subframes,
Sreeja Kamishetty8eacabb2021-03-09 11:45:422929 // which should all be inactive as well.
Sreeja Kamishettydb8e2892021-03-10 09:30:582930 if (render_frame_host->IsInactiveAndDisallowActivation())
Sreeja Kamishetty8eacabb2021-03-09 11:45:422931 return HistoryNavigationAction::kStopLooking;
2932 }
arthursonzogni03f76152019-02-12 10:35:202933
Alex Moshchuk3a4e77a2020-05-29 21:32:572934 // If there's no last committed entry, there is no previous history entry to
2935 // compare against, so fall back to a different-document load. Note that we
2936 // should only reach this case for the root frame and not descend further
2937 // into subframes.
2938 if (!GetLastCommittedEntry()) {
2939 DCHECK(frame->IsMainFrame());
2940 return HistoryNavigationAction::kDifferentDocument;
2941 }
2942
2943 // Reloads should result in a different-document load. Note that reloads may
2944 // also happen via the |needs_reload_| mechanism where the reload_type is
2945 // NONE, so detect this by comparing whether we're going to the same
2946 // entry that we're currently on. Similarly to above, only main frames
2947 // should reach this. Note that subframes support reloads, but that's done
2948 // via a different path that doesn't involve FindFramesToNavigate (see
2949 // RenderFrameHost::Reload()).
2950 if (reload_type != ReloadType::NONE ||
2951 pending_entry_index_ == last_committed_entry_index_) {
2952 DCHECK(frame->IsMainFrame());
2953 return HistoryNavigationAction::kDifferentDocument;
2954 }
2955
Alex Moshchuk47d1a4bd2020-06-01 22:15:342956 // If there is no new FrameNavigationEntry for the frame, ignore the
2957 // load. For example, this may happen when going back to an entry before a
2958 // frame was created. Suppose we commit a same-document navigation that also
2959 // results in adding a new subframe somewhere in the tree. If we go back,
2960 // the new subframe will be missing a FrameNavigationEntry in the previous
2961 // NavigationEntry, but we shouldn't delete or change what's loaded in
2962 // it.
2963 //
Alex Moshchuke65c39272020-06-03 17:55:372964 // Note that in this case, there is no need to keep looking for navigations
2965 // in subframes, which would be missing FrameNavigationEntries as well.
2966 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:342967 // It's important to check this before checking |old_item| below, since both
2968 // might be null, and in that case we still shouldn't change what's loaded in
2969 // this frame. Note that scheduling any loads assumes that |new_item| is
2970 // non-null. See https://crbug.com/1088354.
2971 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
2972 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:372973 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:342974
Alex Moshchuk3a4e77a2020-05-29 21:32:572975 // If there is no old FrameNavigationEntry, schedule a different-document
2976 // load.
2977 //
creis225a7432016-06-03 22:56:272978 // TODO(creis): Store the last committed FrameNavigationEntry to use here,
2979 // rather than assuming the NavigationEntry has up to date info on subframes.
creis4e2ecb72015-06-20 00:46:302980 FrameNavigationEntry* old_item =
2981 GetLastCommittedEntry()->GetFrameEntry(frame);
Alex Moshchuk3a4e77a2020-05-29 21:32:572982 if (!old_item)
2983 return HistoryNavigationAction::kDifferentDocument;
2984
Alex Moshchuk3a4e77a2020-05-29 21:32:572985 // If the new item is not in the same SiteInstance, schedule a
2986 // different-document load. Newly restored items may not have a SiteInstance
2987 // yet, in which case it will be assigned on first commit.
2988 if (new_item->site_instance() &&
2989 new_item->site_instance() != old_item->site_instance())
2990 return HistoryNavigationAction::kDifferentDocument;
2991
2992 // Schedule a different-document load if the current RenderFrameHost is not
danakj25c436d2021-04-01 16:35:312993 // live. This case can happen for Ctrl+Back or after a renderer crash. Note
2994 // that we do this even if the history navigation would not be modifying this
2995 // frame were it live.
2996 if (!frame->current_frame_host()->IsRenderFrameLive())
Alex Moshchuk3a4e77a2020-05-29 21:32:572997 return HistoryNavigationAction::kDifferentDocument;
2998
2999 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
danakj25c436d2021-04-01 16:35:313000 // Starting a navigation after a crash early-promotes the speculative
3001 // RenderFrameHost. Then we have a RenderFrameHost with no document in it
3002 // committed yet, so we can not possibly perform a same-document history
3003 // navigation. The frame would need to be reloaded with a cross-document
3004 // navigation.
3005 if (!frame->current_frame_host()->has_committed_any_navigation())
3006 return HistoryNavigationAction::kDifferentDocument;
3007
creis54131692016-08-12 18:32:253008 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:493009 // sequence number but different item sequence number.
3010 if (new_item->document_sequence_number() ==
3011 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:573012 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:493013 }
avib48cb312016-05-05 21:35:003014
Alex Moshchuk3a4e77a2020-05-29 21:32:573015 // Otherwise, if both item and document sequence numbers differ, this
3016 // should be a different document load.
3017 return HistoryNavigationAction::kDifferentDocument;
3018 }
3019
3020 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:373021 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:573022 DCHECK_EQ(new_item->document_sequence_number(),
3023 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:373024 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:573025}
3026
3027void NavigationControllerImpl::FindFramesToNavigate(
3028 FrameTreeNode* frame,
3029 ReloadType reload_type,
3030 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
3031 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
3032 DCHECK(pending_entry_);
3033 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
3034
3035 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
3036
3037 if (action == HistoryNavigationAction::kSameDocument) {
3038 std::unique_ptr<NavigationRequest> navigation_request =
3039 CreateNavigationRequestFromEntry(
3040 frame, pending_entry_, new_item, reload_type,
3041 true /* is_same_document_history_load */,
3042 false /* is_history_navigation_in_new_child */);
3043 if (navigation_request) {
3044 // Only add the request if was properly created. It's possible for the
3045 // creation to fail in certain cases, e.g. when the URL is invalid.
3046 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303047 }
Alex Moshchuk3a4e77a2020-05-29 21:32:573048 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473049 std::unique_ptr<NavigationRequest> navigation_request =
3050 CreateNavigationRequestFromEntry(
3051 frame, pending_entry_, new_item, reload_type,
3052 false /* is_same_document_history_load */,
3053 false /* is_history_navigation_in_new_child */);
3054 if (navigation_request) {
3055 // Only add the request if was properly created. It's possible for the
3056 // creation to fail in certain cases, e.g. when the URL is invalid.
3057 different_document_loads->push_back(std::move(navigation_request));
3058 }
3059 // For a different document, the subframes will be destroyed, so there's
3060 // no need to consider them.
3061 return;
Alex Moshchuke65c39272020-06-03 17:55:373062 } else if (action == HistoryNavigationAction::kStopLooking) {
3063 return;
creis4e2ecb72015-06-20 00:46:303064 }
3065
3066 for (size_t i = 0; i < frame->child_count(); i++) {
clamy3cb9bea92018-07-10 12:42:023067 FindFramesToNavigate(frame->child_at(i), reload_type, same_document_loads,
creis4e2ecb72015-06-20 00:46:303068 different_document_loads);
3069 }
3070}
3071
Harkiran Bolariaba823e42021-05-21 18:30:363072base::WeakPtr<NavigationHandle> NavigationControllerImpl::NavigateWithoutEntry(
clamy21718cc22018-06-13 13:34:243073 const LoadURLParams& params) {
3074 // Find the appropriate FrameTreeNode.
3075 FrameTreeNode* node = nullptr;
3076 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3077 !params.frame_name.empty()) {
3078 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003079 ? frame_tree_.FindByID(params.frame_tree_node_id)
3080 : frame_tree_.FindByName(params.frame_name);
clamy21718cc22018-06-13 13:34:243081 }
3082
3083 // If no FrameTreeNode was specified, navigate the main frame.
3084 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003085 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243086
Camille Lamy5193caa2018-10-12 11:59:423087 // Compute overrides to the LoadURLParams for |override_user_agent|,
3088 // |should_replace_current_entry| and |has_user_gesture| that will be used
3089 // both in the creation of the NavigationEntry and the NavigationRequest.
3090 // Ideally, the LoadURLParams themselves would be updated, but since they are
3091 // passed as a const reference, this is not possible.
3092 // TODO(clamy): When we only create a NavigationRequest, move this to
3093 // CreateNavigationRequestFromLoadURLParams.
3094 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3095 GetLastCommittedEntry());
3096
3097 // Don't allow an entry replacement if there is no entry to replace.
3098 // http://crbug.com/457149
3099 bool should_replace_current_entry =
3100 params.should_replace_current_entry && entries_.size();
3101
clamy21718cc22018-06-13 13:34:243102 // Javascript URLs should not create NavigationEntries. All other navigations
3103 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243104 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373105 std::unique_ptr<NavigationEntryImpl> entry =
3106 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3107 should_replace_current_entry,
3108 params.has_user_gesture);
clamy21718cc22018-06-13 13:34:243109 DiscardPendingEntry(false);
3110 SetPendingEntry(std::move(entry));
3111 }
3112
3113 // Renderer-debug URLs are sent to the renderer process immediately for
3114 // processing and don't need to create a NavigationRequest.
3115 // Note: this includes navigations to JavaScript URLs, which are considered
3116 // renderer-debug URLs.
3117 // Note: we intentionally leave the pending entry in place for renderer debug
3118 // URLs, unlike the cases below where we clear it if the navigation doesn't
3119 // proceed.
Gyuyoung Kim107c2a02021-04-13 01:49:303120 if (blink::IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483121 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3122 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153123 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483124 params.url, browser_context_)) {
3125 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363126 return nullptr;
Oleg Davydov2cc0167b2019-02-05 14:32:483127 }
3128
clamy21718cc22018-06-13 13:34:243129 HandleRendererDebugURL(node, params.url);
Harkiran Bolariaba823e42021-05-21 18:30:363130 return nullptr;
clamy21718cc22018-06-13 13:34:243131 }
3132
Antonio Sartori78a749f2020-11-30 12:03:393133 DCHECK(pending_entry_);
3134
clamy21718cc22018-06-13 13:34:243135 // Convert navigations to the current URL to a reload.
3136 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3137 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3138 // DevTools sometimes issues navigations to main frames that they do not
3139 // expect to see treated as reload, and it only works because they pass a
3140 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3141 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543142 ReloadType reload_type = params.reload_type;
3143 if (reload_type == ReloadType::NONE &&
3144 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043145 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243146 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243147 params.load_type ==
3148 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
Rakina Zata Amni312822d72021-06-04 16:13:373149 params.should_replace_current_entry, GetLastCommittedEntry())) {
clamy21718cc22018-06-13 13:34:243150 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473151 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393152
3153 // If this is a reload of an existing FrameNavigationEntry and we had a
3154 // policy container for it, then we should copy it into the pending entry,
3155 // so that it is copied to the navigation request in
3156 // CreateNavigationRequestFromLoadParams later.
3157 if (GetLastCommittedEntry()) {
3158 FrameNavigationEntry* previous_frame_entry =
3159 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193160 if (previous_frame_entry &&
3161 previous_frame_entry->policy_container_policies()) {
3162 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163163 previous_frame_entry->policy_container_policies()->Clone());
Antonio Sartori78a749f2020-11-30 12:03:393164 }
3165 }
3166 }
3167
3168 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
3169 // document (so no last commit), then we should copy the document polices from
3170 // RenderFrameHost's PolicyContainerHost. The NavigationRequest will create a
3171 // new PolicyContainerHost with the document policies from the
3172 // |pending_entry_|, and that PolicyContainerHost will be put in the final
3173 // RenderFrameHost for the navigation. This way, we ensure that we keep
3174 // enforcing the right policies on the initial empty document after the
3175 // reload.
3176 if (!GetLastCommittedEntry() && params.url.IsAboutBlank()) {
3177 if (node->current_frame_host() &&
3178 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193179 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
Antonio Sartori5d09b30f2021-03-02 09:27:163180 node->current_frame_host()
3181 ->policy_container_host()
3182 ->policies()
3183 .Clone());
Antonio Sartori78a749f2020-11-30 12:03:393184 }
clamy21718cc22018-06-13 13:34:243185 }
3186
3187 // navigation_ui_data should only be present for main frame navigations.
3188 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
3189
Camille Lamy5193caa2018-10-12 11:59:423190 std::unique_ptr<NavigationRequest> request =
3191 CreateNavigationRequestFromLoadParams(
3192 node, params, override_user_agent, should_replace_current_entry,
Antonio Sartori2f763d9d2021-04-21 10:04:143193 params.has_user_gesture, network::mojom::SourceLocation::New(),
3194 blink::NavigationDownloadPolicy(), reload_type, pending_entry_,
Scott Violetcf6ea7e2021-06-09 21:09:213195 pending_entry_->GetFrameEntry(node));
clamy21718cc22018-06-13 13:34:243196
3197 // If the navigation couldn't start, return immediately and discard the
3198 // pending NavigationEntry.
3199 if (!request) {
3200 DiscardPendingEntry(false);
Harkiran Bolariaba823e42021-05-21 18:30:363201 return nullptr;
clamy21718cc22018-06-13 13:34:243202 }
3203
Camille Lamy5193caa2018-10-12 11:59:423204#if DCHECK_IS_ON()
3205 // Safety check that NavigationRequest and NavigationEntry match.
3206 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3207#endif
3208
clamy21718cc22018-06-13 13:34:243209 // This call does not support re-entrancy. See http://crbug.com/347742.
3210 CHECK(!in_navigate_to_pending_entry_);
3211 in_navigate_to_pending_entry_ = true;
3212
arthursonzogni66f711c2019-10-08 14:40:363213 // It is not possible to delete the pending NavigationEntry while navigating
3214 // to it. Grab a reference to delay potential deletion until the end of this
3215 // function.
3216 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3217
Harkiran Bolariaba823e42021-05-21 18:30:363218 base::WeakPtr<NavigationHandle> created_navigation_handle(
3219 request->GetWeakPtr());
Lukasz Anforowicz9ee83c272020-12-01 20:14:053220 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243221
3222 in_navigate_to_pending_entry_ = false;
Harkiran Bolariaba823e42021-05-21 18:30:363223 return created_navigation_handle;
clamy21718cc22018-06-13 13:34:243224}
3225
clamyea99ea12018-05-28 13:54:233226void NavigationControllerImpl::HandleRendererDebugURL(
3227 FrameTreeNode* frame_tree_node,
3228 const GURL& url) {
3229 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243230 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3231 // the renderer process is not live, unless it is the initial navigation
3232 // of the tab.
clamyea99ea12018-05-28 13:54:233233 if (!IsInitialNavigation()) {
3234 DiscardNonCommittedEntries();
3235 return;
3236 }
Fergal Dalyecd3b0202020-06-25 01:57:373237 // The current frame is always a main frame. If IsInitialNavigation() is
3238 // true then there have been no navigations and any frames of this tab must
3239 // be in the same renderer process. If that has crashed then the only frame
3240 // that can be revived is the main frame.
3241 frame_tree_node->render_manager()
3242 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233243 }
3244 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3245}
3246
clamy21718cc22018-06-13 13:34:243247std::unique_ptr<NavigationEntryImpl>
3248NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3249 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423250 const LoadURLParams& params,
3251 bool override_user_agent,
3252 bool should_replace_current_entry,
3253 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393254 // Browser initiated navigations might not have a blob_url_loader_factory set
3255 // in params even if the navigation is to a blob URL. If that happens, lookup
3256 // the correct url loader factory to use here.
3257 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483258 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473259 // Resolve the blob URL in the storage partition associated with the target
3260 // frame. This is the storage partition the URL will be loaded in, and only
3261 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393262 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473263 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393264 }
3265
clamy21718cc22018-06-13 13:34:243266 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443267 // extra_headers in params are \n separated; navigation entries want \r\n.
3268 std::string extra_headers_crlf;
3269 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243270
3271 // For subframes, create a pending entry with a corresponding frame entry.
3272 if (!node->IsMainFrame()) {
Tommy C. Li03eee77a2019-02-05 02:07:443273 if (GetLastCommittedEntry()) {
Tommy C. Li03eee77a2019-02-05 02:07:443274 entry = GetLastCommittedEntry()->Clone();
3275 } else {
3276 // If there's no last committed entry, create an entry for about:blank
3277 // with a subframe entry for our destination.
3278 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
3279 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063280 GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103281 params.source_site_instance.get(), params.transition_type,
3282 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Hayato Ito303654c2021-06-30 09:07:543283 blob_url_loader_factory));
Tommy C. Li03eee77a2019-02-05 02:07:443284 }
Nasko Oskov18006bc2018-12-06 02:53:583285
clamy21718cc22018-06-13 13:34:243286 entry->AddOrUpdateFrameEntry(
Nate Chapinfbfe5af2021-06-10 17:22:083287 node, NavigationEntryImpl::UpdatePolicy::kReplace, -1, -1, "", nullptr,
clamy21718cc22018-06-13 13:34:243288 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Anton Bikineevf62d1bf2021-05-15 17:56:073289 params.url, absl::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203290 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393291 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Kunihiko Sakamoto346a74e2021-03-10 08:57:483292 nullptr /* subresource_web_bundle_navigation_info */,
Antonio Sartori78a749f2020-11-30 12:03:393293 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163294 // a conversion of a new navigation into a reload, we will set the right
3295 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193296 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243297 } else {
3298 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243299 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063300 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103301 params.source_site_instance.get(), params.transition_type,
3302 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Hayato Ito303654c2021-06-30 09:07:543303 blob_url_loader_factory));
clamy21718cc22018-06-13 13:34:243304 entry->set_source_site_instance(
3305 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3306 entry->SetRedirectChain(params.redirect_chain);
3307 }
3308
3309 // Set the FTN ID (only used in non-site-per-process, for tests).
3310 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243311 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423312 entry->SetIsOverridingUserAgent(override_user_agent);
3313 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543314 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243315
clamy21718cc22018-06-13 13:34:243316 switch (params.load_type) {
3317 case LOAD_TYPE_DEFAULT:
3318 break;
3319 case LOAD_TYPE_HTTP_POST:
3320 entry->SetHasPostData(true);
3321 entry->SetPostData(params.post_data);
3322 break;
3323 case LOAD_TYPE_DATA:
3324 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3325 entry->SetVirtualURL(params.virtual_url_for_data_url);
3326#if defined(OS_ANDROID)
3327 entry->SetDataURLAsString(params.data_url_as_string);
3328#endif
3329 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3330 break;
clamy21718cc22018-06-13 13:34:243331 }
3332
3333 // TODO(clamy): NavigationEntry is meant for information that will be kept
3334 // after the navigation ended and therefore is not appropriate for
3335 // started_from_context_menu. Move started_from_context_menu to
3336 // NavigationUIData.
3337 entry->set_started_from_context_menu(params.started_from_context_menu);
3338
3339 return entry;
3340}
3341
clamyea99ea12018-05-28 13:54:233342std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423343NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3344 FrameTreeNode* node,
3345 const LoadURLParams& params,
3346 bool override_user_agent,
3347 bool should_replace_current_entry,
3348 bool has_user_gesture,
Antonio Sartori2f763d9d2021-04-21 10:04:143349 network::mojom::SourceLocationPtr source_location,
Yeunjoo Choi3df791a2021-02-17 07:07:253350 blink::NavigationDownloadPolicy download_policy,
Camille Lamy5193caa2018-10-12 11:59:423351 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573352 NavigationEntryImpl* entry,
Scott Violetcf6ea7e2021-06-09 21:09:213353 FrameNavigationEntry* frame_entry) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573354 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283355 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533356 // All renderer-initiated navigations must have an initiator_origin.
3357 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513358
Camille Lamy5193caa2018-10-12 11:59:423359 GURL url_to_load;
3360 GURL virtual_url;
Anton Bikineevf62d1bf2021-05-15 17:56:073361 absl::optional<url::Origin> origin_to_commit =
3362 frame_entry ? frame_entry->committed_origin() : absl::nullopt;
Nasko Oskov03912102019-01-11 00:21:323363
Camille Lamy2baa8022018-10-19 16:43:173364 // For main frames, rewrite the URL if necessary and compute the virtual URL
3365 // that should be shown in the address bar.
3366 if (node->IsMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423367 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173368 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423369 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423370
Camille Lamy2baa8022018-10-19 16:43:173371 // For DATA loads, override the virtual URL.
3372 if (params.load_type == LOAD_TYPE_DATA)
3373 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423374
Camille Lamy2baa8022018-10-19 16:43:173375 if (virtual_url.is_empty())
3376 virtual_url = url_to_load;
3377
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573378 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283379
Aran Gilman249eb122019-12-02 23:32:463380 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3381 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3382 // return different results, leading to a different URL in the
3383 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3384 // pending NavigationEntry, as we'll only make one call to
3385 // RewriteUrlForNavigation.
3386 VLOG_IF(1, (url_to_load != frame_entry->url()))
3387 << "NavigationRequest and FrameEntry have different URLs: "
3388 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283389
Camille Lamy2baa8022018-10-19 16:43:173390 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423391 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173392 // NavigationRequest.
3393 } else {
3394 url_to_load = params.url;
3395 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243396 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173397 }
Camille Lamy5193caa2018-10-12 11:59:423398
Ehsan Karamad44fc72112019-02-26 18:15:473399 if (node->render_manager()->is_attaching_inner_delegate()) {
3400 // Avoid starting any new navigations since this node is now preparing for
3401 // attaching an inner delegate.
3402 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203403 }
Camille Lamy5193caa2018-10-12 11:59:423404
Camille Lamy5193caa2018-10-12 11:59:423405 if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load))
3406 return nullptr;
3407
Kunihiko Sakamoto346a74e2021-03-10 08:57:483408 if (!DoesURLMatchOriginForNavigation(
3409 url_to_load, origin_to_commit,
3410 frame_entry->subresource_web_bundle_navigation_info())) {
Nasko Oskov03912102019-01-11 00:21:323411 DCHECK(false) << " url:" << url_to_load
3412 << " origin:" << origin_to_commit.value();
3413 return nullptr;
3414 }
3415
Camille Lamy5193caa2018-10-12 11:59:423416 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403417 blink::PreviewsState previews_state =
3418 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
Camille Lamy5193caa2018-10-12 11:59:423419 if (!node->IsMainFrame()) {
3420 // For subframes, use the state of the top-level frame.
3421 previews_state = node->frame_tree()
3422 ->root()
3423 ->current_frame_host()
3424 ->last_navigation_previews_state();
3425 }
3426
Camille Lamy5193caa2018-10-12 11:59:423427 // This will be used to set the Navigation Timing API navigationStart
3428 // parameter for browser navigations in new tabs (intents, tabs opened through
3429 // "Open link in new tab"). If the navigation must wait on the current
3430 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3431 // will be updated when the BeforeUnload ack is received.
3432 base::TimeTicks navigation_start = base::TimeTicks::Now();
3433
danakjd83d706d2020-11-25 22:11:123434 // Look for a pending commit that is to another document in this
3435 // FrameTreeNode. If one exists, then the last committed URL will not be the
3436 // current URL by the time this navigation commits.
3437 bool has_pending_cross_document_commit =
3438 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493439 bool is_currently_error_page = node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123440
Minggang Wangb9f3fa92021-07-01 15:30:313441 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjb952ef12021-01-14 19:58:493442 /*old_url=*/node->current_url(),
3443 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3444 has_pending_cross_document_commit, is_currently_error_page,
3445 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423446
3447 // Create the NavigationParams based on |params|.
3448
Hiroki Nakagawa4ed61282021-06-18 05:37:233449 bool is_view_source_mode = entry->IsViewSourceMode();
3450 DCHECK_EQ(is_view_source_mode, virtual_url.SchemeIs(kViewSourceScheme));
Charlie Harrison8c113a32019-01-07 16:08:293451
Hiroki Nakagawa4ed61282021-06-18 05:37:233452 // Update |download_policy| if the virtual URL is view-source.
Charlie Harrison8c113a32019-01-07 16:08:293453 if (is_view_source_mode)
Yeunjoo Choi3df791a2021-02-17 07:07:253454 download_policy.SetDisallowed(blink::NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293455
Camille Lamy5193caa2018-10-12 11:59:423456 const GURL& history_url_for_data_url =
3457 params.base_url_for_data_url.is_empty() ? GURL() : virtual_url;
Minggang Wangb9f3fa92021-07-01 15:30:313458 blink::mojom::CommonNavigationParamsPtr common_params =
3459 blink::mojom::CommonNavigationParams::New(
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513460 url_to_load, params.initiator_origin,
3461 blink::mojom::Referrer::New(params.referrer.url,
3462 params.referrer.policy),
Scott Violetcf6ea7e2021-06-09 21:09:213463 params.transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513464 should_replace_current_entry, params.base_url_for_data_url,
3465 history_url_for_data_url, previews_state, navigation_start,
3466 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
Antonio Sartori2f763d9d2021-04-21 10:04:143467 params.post_data, std::move(source_location),
arthursonzogniaf7c62c52020-02-12 10:49:413468 params.started_from_context_menu, has_user_gesture,
Antonio Sartori636adba2021-03-09 12:15:273469 false /* has_text_fragment_token */,
3470 network::mojom::CSPDisposition::CHECK, std::vector<int>(),
3471 params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513472 false /* is_history_navigation_in_new_child_frame */,
Charlie Hu5ffc0152019-12-06 15:59:533473 params.input_start);
Camille Lamy5193caa2018-10-12 11:59:423474
Minggang Wangb9f3fa92021-07-01 15:30:313475 blink::mojom::CommitNavigationParamsPtr commit_params =
3476 blink::mojom::CommitNavigationParams::New(
arthursonzognid5a8d0b2021-03-11 17:36:433477 frame_entry->committed_origin(), network::mojom::WebSandboxFlags(),
3478 override_user_agent, params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323479 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143480 std::vector<net::RedirectInfo>(),
3481 std::string() /* post_content_type */, common_params->url,
3482 common_params->method, params.can_load_local_resources,
Minggang Wangb9f3fa92021-07-01 15:30:313483 frame_entry->page_state().ToEncodedData(), entry->GetUniqueID(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143484 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3485 -1 /* pending_history_list_offset */,
3486 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3487 params.should_clear_history_list ? 0 : GetEntryCount(),
3488 false /* was_discarded */, is_view_source_mode,
Minggang Wangb9f3fa92021-07-01 15:30:313489 params.should_clear_history_list,
3490 blink::mojom::NavigationTiming::New(),
Anton Bikineevf62d1bf2021-05-15 17:56:073491 absl::nullopt /* appcache_host_id */,
Minggang Wangf59db47b2021-06-16 01:56:223492 blink::mojom::WasActivatedOption::kUnknown,
Lucas Furukawa Gadania9c45682019-07-31 22:05:143493 base::UnguessableToken::Create() /* navigation_token */,
Minggang Wang7ee0c742021-06-16 16:16:513494 std::vector<blink::mojom::PrefetchedSignedExchangeInfoPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143495#if defined(OS_ANDROID)
3496 std::string(), /* data_url_as_string */
3497#endif
arthursonzogni14379782020-05-15 09:09:273498 !params.is_renderer_initiated, /* is_browser_initiated */
Sreeja Kamishetty46f762c2021-02-05 07:52:463499 node->frame_tree()->is_prerendering() /* is_prerendering */,
Tsuyoshi Horoe86d7702019-11-29 01:52:473500 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533501 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173502 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073503 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163504 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333505 false /* origin_agent_cluster */,
Maks Orlovichc66745a2020-06-30 17:40:023506 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213507 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Nate Chapind1fe3612021-04-16 20:45:573508 false /* is_cross_browsing_instance */, nullptr /* old_page_info */,
3509 -1 /* http_response_code */,
Minggang Wangb9f3fa92021-07-01 15:30:313510 std::vector<blink::mojom::
3511 AppHistoryEntryPtr>() /* app_history_back_entries */,
Nate Chapind1fe3612021-04-16 20:45:573512 std::vector<
Minggang Wangb9f3fa92021-07-01 15:30:313513 blink::mojom::
3514 AppHistoryEntryPtr>() /* app_history_forward_entries */,
Kenichi Ishibashi17ecfb62021-06-21 03:31:153515 std::vector<GURL>() /* early_hints_preloaded_resources */);
Camille Lamy5193caa2018-10-12 11:59:423516#if defined(OS_ANDROID)
3517 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143518 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423519 }
3520#endif
3521
Lucas Furukawa Gadania9c45682019-07-31 22:05:143522 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423523
3524 // A form submission may happen here if the navigation is a renderer-initiated
3525 // form submission that took the OpenURL path.
3526 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3527
3528 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3529 std::string extra_headers_crlf;
3530 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093531
3532 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143533 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083534 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453535 params.initiator_frame_token.has_value()
3536 ? &(params.initiator_frame_token.value())
3537 : nullptr,
3538 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3539 request_body,
John Delaney50425f82020-04-07 16:26:213540 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
3541 params.impression);
Yao Xiaodc5ed102019-06-04 19:19:093542 navigation_request->set_from_download_cross_origin_redirect(
3543 params.from_download_cross_origin_redirect);
3544 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423545}
3546
3547std::unique_ptr<NavigationRequest>
3548NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233549 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573550 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233551 FrameNavigationEntry* frame_entry,
3552 ReloadType reload_type,
3553 bool is_same_document_history_load,
Arthur Hemerybee4a752019-05-29 10:50:553554 bool is_history_navigation_in_new_child_frame) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343555 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233556 GURL dest_url = frame_entry->url();
Anton Bikineevf62d1bf2021-05-15 17:56:073557 absl::optional<url::Origin> origin_to_commit =
Nasko Oskov03912102019-01-11 00:21:323558 frame_entry->committed_origin();
3559
clamyea99ea12018-05-28 13:54:233560 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013561 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573562 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233563 // We may have been redirected when navigating to the current URL.
3564 // Use the URL the user originally intended to visit as signaled by the
3565 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013566 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573567 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233568 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323569 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233570 }
3571
Ehsan Karamad44fc72112019-02-26 18:15:473572 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3573 // Avoid starting any new navigations since this node is now preparing for
3574 // attaching an inner delegate.
3575 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203576 }
3577
Camille Lamy5193caa2018-10-12 11:59:423578 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573579 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233580 return nullptr;
3581 }
3582
Kunihiko Sakamoto346a74e2021-03-10 08:57:483583 if (!DoesURLMatchOriginForNavigation(
3584 dest_url, origin_to_commit,
3585 frame_entry->subresource_web_bundle_navigation_info())) {
3586 DCHECK(false) << " url:" << dest_url
3587 << " origin:" << origin_to_commit.value();
Nasko Oskov03912102019-01-11 00:21:323588 return nullptr;
3589 }
3590
clamyea99ea12018-05-28 13:54:233591 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403592 blink::PreviewsState previews_state =
3593 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
clamyea99ea12018-05-28 13:54:233594 if (!frame_tree_node->IsMainFrame()) {
3595 // For subframes, use the state of the top-level frame.
3596 previews_state = frame_tree_node->frame_tree()
3597 ->root()
3598 ->current_frame_host()
3599 ->last_navigation_previews_state();
clamyea99ea12018-05-28 13:54:233600 }
3601
clamyea99ea12018-05-28 13:54:233602 // This will be used to set the Navigation Timing API navigationStart
3603 // parameter for browser navigations in new tabs (intents, tabs opened through
3604 // "Open link in new tab"). If the navigation must wait on the current
3605 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3606 // will be updated when the BeforeUnload ack is received.
3607 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233608
danakjd83d706d2020-11-25 22:11:123609 // Look for a pending commit that is to another document in this
3610 // FrameTreeNode. If one exists, then the last committed URL will not be the
3611 // current URL by the time this navigation commits.
3612 bool has_pending_cross_document_commit =
3613 frame_tree_node->render_manager()
3614 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493615 bool is_currently_error_page =
3616 frame_tree_node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123617
Minggang Wangb9f3fa92021-07-01 15:30:313618 blink::mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123619 /*old_url=*/frame_tree_node->current_url(),
3620 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493621 has_pending_cross_document_commit, is_currently_error_page,
3622 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423623
3624 // A form submission may happen here if the navigation is a
3625 // back/forward/reload navigation that does a form resubmission.
3626 scoped_refptr<network::ResourceRequestBody> request_body;
3627 std::string post_content_type;
3628 if (frame_entry->method() == "POST") {
3629 request_body = frame_entry->GetPostData(&post_content_type);
3630 // Might have a LF at end.
Peter Kastingb53b81912021-04-28 19:23:303631 post_content_type = std::string(
3632 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL));
Camille Lamy5193caa2018-10-12 11:59:423633 }
3634
3635 // Create the NavigationParams based on |entry| and |frame_entry|.
Minggang Wangb9f3fa92021-07-01 15:30:313636 blink::mojom::CommonNavigationParamsPtr common_params =
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513637 entry->ConstructCommonNavigationParams(
3638 *frame_entry, request_body, dest_url,
3639 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
3640 navigation_type, previews_state, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533641 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513642 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553643 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423644
3645 // TODO(clamy): |intended_as_new_entry| below should always be false once
3646 // Reload no longer leads to this being called for a pending NavigationEntry
3647 // of index -1.
Minggang Wangb9f3fa92021-07-01 15:30:313648 blink::mojom::CommitNavigationParamsPtr commit_params =
Lucas Furukawa Gadania9c45682019-07-31 22:05:143649 entry->ConstructCommitNavigationParams(
3650 *frame_entry, common_params->url, origin_to_commit,
3651 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3652 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533653 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3654 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143655 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423656
clamyea99ea12018-05-28 13:54:233657 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143658 frame_tree_node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083659 !entry->is_renderer_initiated(), false /* was_opener_suppressed */,
3660 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453661 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3662 entry->extra_headers(), frame_entry, entry, request_body,
Anton Bikineevf62d1bf2021-05-15 17:56:073663 nullptr /* navigation_ui_data */, absl::nullopt /* impression */);
clamyea99ea12018-05-28 13:54:233664}
3665
[email protected]d202a7c2012-01-04 07:53:473666void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213667 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273668 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403669
[email protected]2db9bd72012-04-13 20:20:563670 // We need to notify the ssl_manager_ before the web_contents_ so the
[email protected]df1af242009-05-01 00:11:403671 // location bar will have up-to-date information about the security style
3672 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133673 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403674
[email protected]7f924832014-08-09 05:57:223675 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:573676 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463677
[email protected]b0f724c2013-09-05 04:21:133678 // TODO(avi): Remove. http://crbug.com/170921
3679 NotificationDetails notification_details =
3680 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153681 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3682 Source<NavigationController>(this),
3683 notification_details);
initial.commit09911bf2008-07-26 23:55:293684}
3685
initial.commit09911bf2008-07-26 23:55:293686// static
[email protected]d202a7c2012-01-04 07:53:473687size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233688 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153689 return max_entry_count_for_testing_;
Miyoung Shin1c565c912021-03-17 12:11:213690 return blink::kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233691}
3692
[email protected]d202a7c2012-01-04 07:53:473693void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223694 if (is_active && needs_reload_)
3695 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293696}
3697
[email protected]d202a7c2012-01-04 07:53:473698void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293699 if (!needs_reload_)
3700 return;
3701
Bo Liucdfa4b12018-11-06 00:21:443702 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3703 needs_reload_type_);
3704
initial.commit09911bf2008-07-26 23:55:293705 // Calling Reload() results in ignoring state, and not loading.
3706 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3707 // cached state.
avicc872d7242015-08-19 21:26:343708 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163709 NavigateToExistingPendingEntry(ReloadType::NONE,
3710 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343711 } else if (last_committed_entry_index_ != -1) {
arthursonzogni5c4c202d2017-04-25 23:41:273712 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343713 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163714 NavigateToExistingPendingEntry(ReloadType::NONE,
3715 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343716 } else {
3717 // If there is something to reload, the successful reload will clear the
3718 // |needs_reload_| flag. Otherwise, just do it here.
3719 needs_reload_ = false;
3720 }
initial.commit09911bf2008-07-26 23:55:293721}
3722
Carlos IL42b416592019-10-07 23:10:363723void NavigationControllerImpl::LoadPostCommitErrorPage(
3724 RenderFrameHost* render_frame_host,
3725 const GURL& url,
3726 const std::string& error_page_html,
3727 net::Error error) {
Rakina Zata Amni919b7922020-12-11 09:03:133728 RenderFrameHostImpl* rfhi =
3729 static_cast<RenderFrameHostImpl*>(render_frame_host);
Sreeja Kamishettydb8e2892021-03-10 09:30:583730
3731 // Only active documents can load post-commit error pages:
3732 // - If the document is in pending deletion, the browser already committed to
3733 // destroying this RenderFrameHost so ignore loading the error page.
3734 // - If the document is in back-forward cache, it's not allowed to navigate as
3735 // it should remain frozen. Ignore the request and evict the document from
3736 // back-forward cache.
3737 // - If the document is prerendering, it can navigate but when loading error
3738 // pages, cancel prerendering.
3739 if (rfhi->IsInactiveAndDisallowActivation())
3740 return;
3741
Rakina Zata Amni919b7922020-12-11 09:03:133742 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:413743
Minggang Wangb9f3fa92021-07-01 15:30:313744 blink::mojom::CommonNavigationParamsPtr common_params =
Anatoliy Potapchuk64e86fd2021-06-30 09:44:323745 CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:593746 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
3747 // the frame actually committed (e.g. iframe with "src" set to a
3748 // slow-responding URL). We should rewrite the URL to about:blank in this
3749 // case, as the renderer will only think a page is an error page if it has a
3750 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:133751 common_params->url = url.is_empty() ? GURL("about:blank") : url;
Minggang Wangb9f3fa92021-07-01 15:30:313752 blink::mojom::CommitNavigationParamsPtr commit_params =
Anatoliy Potapchuk64e86fd2021-06-30 09:44:323753 CreateCommitNavigationParams();
Antonio Sartori58591c892021-04-21 06:54:333754 commit_params->original_url = common_params->url;
John Delaney131ad362019-08-08 21:57:413755
arthursonzogni70ac7302020-05-28 08:49:053756 // Error pages have a fully permissive FramePolicy.
3757 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
3758 // error pages.
3759 commit_params->frame_policy = blink::FramePolicy();
3760
John Delaney131ad362019-08-08 21:57:413761 std::unique_ptr<NavigationRequest> navigation_request =
3762 NavigationRequest::CreateBrowserInitiated(
3763 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083764 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:193765 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453766 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:063767 "" /* extra_headers */, nullptr /* frame_entry */,
3768 nullptr /* entry */, nullptr /* post_body */,
Anton Bikineevf62d1bf2021-05-15 17:56:073769 nullptr /* navigation_ui_data */, absl::nullopt /* impression */);
Carlos IL42b416592019-10-07 23:10:363770 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:413771 navigation_request->set_net_error(error);
3772 node->CreatedNavigationRequest(std::move(navigation_request));
3773 DCHECK(node->navigation_request());
3774 node->navigation_request()->BeginNavigation();
3775}
3776
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573777void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213778 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093779 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:153780 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143781 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293782}
3783
[email protected]d202a7c2012-01-04 07:53:473784void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363785 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553786 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363787 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293788
initial.commit09911bf2008-07-26 23:55:293789 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293790}
[email protected]765b35502008-08-21 00:51:203791
arthursonzogni69a6a1b2019-09-17 09:23:003792void NavigationControllerImpl::DiscardNonCommittedEntries() {
Michael Thiessen9b14d512019-09-23 21:19:473793 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:103794 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
3795 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:293796 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:473797 return;
Michael Thiessenc5676d22019-09-25 22:32:103798 }
avi45a72532015-04-07 21:01:453799 DiscardPendingEntry(false);
arthursonzogni69a6a1b2019-09-17 09:23:003800 if (delegate_)
3801 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:483802}
3803
avi7c6f35e2015-05-08 17:52:383804int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3805 int nav_entry_id) const {
3806 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3807 if (entries_[i]->GetUniqueID() == nav_entry_id)
3808 return i;
3809 }
3810 return -1;
3811}
3812
[email protected]d202a7c2012-01-04 07:53:473813void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573814 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253815 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573816 DCHECK_LE(max_index, source->GetEntryCount());
Nate Chapin214a86a2021-06-21 20:35:573817 std::unique_ptr<NavigationEntryRestoreContextImpl> context =
3818 std::make_unique<NavigationEntryRestoreContextImpl>();
[email protected]e1cd5452010-08-26 18:03:253819 for (int i = 0; i < max_index; i++) {
Nate Chapin9f169072021-06-09 19:32:373820 // Normally, cloning a NavigationEntryImpl results in sharing
3821 // FrameNavigationEntries between the original and the clone. However, when
3822 // cloning from a different NavigationControllerImpl, we want to fork the
3823 // FrameNavigationEntries.
Nate Chapin9f169072021-06-09 19:32:373824 entries_.insert(entries_.begin() + i,
Nate Chapin214a86a2021-06-21 20:35:573825 source->entries_[i]->CloneWithoutSharing(context.get()));
[email protected]e1cd5452010-08-26 18:03:253826 }
arthursonzogni5c4c202d2017-04-25 23:41:273827 DCHECK(pending_entry_index_ == -1 ||
3828 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:253829}
[email protected]c5b88d82012-10-06 17:03:333830
3831void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:183832 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:333833 get_timestamp_callback_ = get_timestamp_callback;
3834}
[email protected]8ff00d72012-10-23 19:12:213835
Shivani Sharmaffb32b82019-04-09 16:58:473836// History manipulation intervention:
3837void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:473838 bool replace_entry,
3839 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:403840 bool is_renderer_initiated,
3841 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:453842 // Note that for a subframe, previous_document_was_activated is true if the
3843 // gesture happened in any subframe (propagated to main frame) or in the main
3844 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:473845 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:273846 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:473847 return;
3848 }
3849 if (last_committed_entry_index_ == -1)
3850 return;
3851
Shivani Sharmac4cc8922019-04-18 03:11:173852 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473853
Alexander Timine3ec4192020-04-20 16:39:403854 // Log UKM with the URL we are navigating away from.
3855 ukm::builders::HistoryManipulationIntervention(
3856 previous_page_load_ukm_source_id)
3857 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:473858}
3859
Shivani Sharmac4cc8922019-04-18 03:11:173860void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
3861 int reference_index,
3862 bool skippable) {
3863 auto* reference_entry = GetEntryAtIndex(reference_index);
3864 reference_entry->set_should_skip_on_back_forward_ui(skippable);
3865
3866 int64_t document_sequence_number =
3867 reference_entry->root_node()->frame_entry->document_sequence_number();
3868 for (int index = 0; index < GetEntryCount(); index++) {
3869 auto* entry = GetEntryAtIndex(index);
3870 if (entry->root_node()->frame_entry->document_sequence_number() ==
3871 document_sequence_number) {
3872 entry->set_should_skip_on_back_forward_ui(skippable);
3873 }
3874 }
3875}
3876
arthursonzogni66f711c2019-10-08 14:40:363877std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
3878NavigationControllerImpl::ReferencePendingEntry() {
3879 DCHECK(pending_entry_);
3880 auto pending_entry_ref =
3881 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
3882 pending_entry_refs_.insert(pending_entry_ref.get());
3883 return pending_entry_ref;
3884}
3885
3886void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
3887 // Ignore refs that don't correspond to the current pending entry.
3888 auto it = pending_entry_refs_.find(ref);
3889 if (it == pending_entry_refs_.end())
3890 return;
3891 pending_entry_refs_.erase(it);
3892
3893 if (!pending_entry_refs_.empty())
3894 return;
3895
3896 // The pending entry may be deleted before the last PendingEntryRef.
3897 if (!pending_entry_)
3898 return;
3899
3900 // We usually clear the pending entry when the matching NavigationRequest
3901 // fails, so that an arbitrary URL isn't left visible above a committed page.
3902 //
3903 // However, we do preserve the pending entry in some cases, such as on the
3904 // initial navigation of an unmodified blank tab. We also allow the delegate
3905 // to say when it's safe to leave aborted URLs in the omnibox, to let the
3906 // user edit the URL and try again. This may be useful in cases that the
3907 // committed page cannot be attacker-controlled. In these cases, we still
3908 // allow the view to clear the pending entry and typed URL if the user
3909 // requests (e.g., hitting Escape with focus in the address bar).
3910 //
3911 // Do not leave the pending entry visible if it has an invalid URL, since this
3912 // might be formatted in an unexpected or unsafe way.
3913 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:363914 bool should_preserve_entry =
3915 (pending_entry_ == GetVisibleEntry()) &&
3916 pending_entry_->GetURL().is_valid() &&
3917 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
3918 if (should_preserve_entry)
3919 return;
3920
3921 DiscardPendingEntry(true);
3922 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3923}
3924
Titouan Rigoudy6ec70402021-02-02 15:42:193925std::unique_ptr<PolicyContainerPolicies>
3926NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:393927 RenderFrameHostImpl* rfh,
3928 bool is_same_document,
3929 NavigationRequest* request) {
Titouan Rigoudy6ec70402021-02-02 15:42:193930 if (!ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(request))
Antonio Sartori78a749f2020-11-30 12:03:393931 return nullptr;
3932
3933 if (is_same_document) {
Charlie Reis73e356242021-04-02 17:10:313934 // TODO(https://crbug.com/524208): Remove this nullptr check when we can
3935 // ensure we always have a FrameNavigationEntry here.
3936 if (!GetLastCommittedEntry())
3937 return nullptr;
3938
Antonio Sartori78a749f2020-11-30 12:03:393939 FrameNavigationEntry* previous_frame_entry =
3940 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
3941
3942 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
3943 // ensure we always have a FrameNavigationEntry here.
3944 if (!previous_frame_entry)
3945 return nullptr;
3946
Titouan Rigoudy6ec70402021-02-02 15:42:193947 const PolicyContainerPolicies* previous_policies =
3948 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:393949
Titouan Rigoudy6ec70402021-02-02 15:42:193950 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:393951 return nullptr;
3952
3953 // Make a copy of the policy container for the new FrameNavigationEntry.
Antonio Sartori5d09b30f2021-03-02 09:27:163954 return previous_policies->Clone();
Antonio Sartori78a749f2020-11-30 12:03:393955 }
3956
Antonio Sartori4f5373792021-05-31 10:56:473957 return rfh->policy_container_host()->policies().Clone();
Antonio Sartori78a749f2020-11-30 12:03:393958}
3959
Hayato Ito2c8c08d02021-06-23 03:38:433960void NavigationControllerImpl::BroadcastHistoryOffsetAndLength() {
Carlos Caballeroede6f8c2021-01-28 11:01:503961 OPTIONAL_TRACE_EVENT2(
Hayato Ito2c8c08d02021-06-23 03:38:433962 "content", "NavigationControllerImpl::BroadcastHistoryOffsetAndLength",
3963 "history_offset", GetLastCommittedEntryIndex(), "history_length",
3964 GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:503965
3966 auto callback = base::BindRepeating(
3967 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
3968 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
3969 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
3970 }
3971 },
Hayato Ito2c8c08d02021-06-23 03:38:433972 GetLastCommittedEntryIndex(), GetEntryCount());
Carlos Caballeroede6f8c2021-01-28 11:01:503973 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
3974}
3975
3976void NavigationControllerImpl::DidAccessInitialMainDocument() {
3977 // We may have left a failed browser-initiated navigation in the address bar
3978 // to let the user edit it and try again. Clear it now that content might
3979 // show up underneath it.
3980 if (!frame_tree_.IsLoading() && GetPendingEntry())
3981 DiscardPendingEntry(false);
3982
3983 // Update the URL display.
3984 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3985}
3986
3987void NavigationControllerImpl::UpdateStateForFrame(
3988 RenderFrameHostImpl* rfhi,
3989 const blink::PageState& page_state) {
Alexander Timinf785f342021-03-18 00:00:563990 OPTIONAL_TRACE_EVENT1("content",
3991 "NavigationControllerImpl::UpdateStateForFrame",
3992 "render_frame_host", rfhi);
Carlos Caballeroede6f8c2021-01-28 11:01:503993 // The state update affects the last NavigationEntry associated with the given
3994 // |render_frame_host|. This may not be the last committed NavigationEntry (as
3995 // in the case of an UpdateState from a frame being swapped out). We track
3996 // which entry this is in the RenderFrameHost's nav_entry_id.
3997 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
3998 if (!entry)
3999 return;
4000
4001 FrameNavigationEntry* frame_entry =
4002 entry->GetFrameEntry(rfhi->frame_tree_node());
4003 if (!frame_entry)
4004 return;
4005
4006 // The SiteInstance might not match if we do a cross-process navigation with
4007 // replacement (e.g., auto-subframe), in which case the swap out of the old
4008 // RenderFrameHost runs in the background after the old FrameNavigationEntry
4009 // has already been replaced and destroyed.
4010 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
4011 return;
4012
4013 if (page_state == frame_entry->page_state())
4014 return; // Nothing to update.
4015
4016 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
4017
4018 // The document_sequence_number and item_sequence_number recorded in the
4019 // FrameNavigationEntry should not differ from the one coming with the update,
4020 // since it must come from the same document. Do not update it if a difference
4021 // is detected, as this indicates that |frame_entry| is not the correct one.
4022 blink::ExplodedPageState exploded_state;
4023 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4024 return;
4025
4026 if (exploded_state.top.document_sequence_number !=
4027 frame_entry->document_sequence_number() ||
4028 exploded_state.top.item_sequence_number !=
4029 frame_entry->item_sequence_number()) {
4030 return;
4031 }
4032
4033 frame_entry->SetPageState(page_state);
4034 NotifyEntryChanged(entry);
4035}
4036
Aaron Colwellb731a0ae2021-03-19 19:14:474037void NavigationControllerImpl::OnStoragePartitionIdAdded(
4038 const StoragePartitionId& partition_id) {
4039 auto it = partition_config_to_id_map_.insert(
4040 std::make_pair(partition_id.config(), partition_id));
4041 bool successful_insert = it.second;
Aaron Colwellb731a0ae2021-03-19 19:14:474042 if (!successful_insert) {
4043 LogStoragePartitionIdCrashKeys(it.first->second, partition_id);
4044 }
Sharon Yangd4f48792021-06-30 17:02:534045 CHECK(successful_insert);
Aaron Colwellb731a0ae2021-03-19 19:14:474046}
4047
4048void NavigationControllerImpl::LogStoragePartitionIdCrashKeys(
4049 const StoragePartitionId& original_partition_id,
4050 const StoragePartitionId& new_partition_id) {
4051 base::debug::SetCrashKeyString(
4052 base::debug::AllocateCrashKeyString("original_partition_id",
4053 base::debug::CrashKeySize::Size256),
4054 original_partition_id.ToString());
4055
4056 base::debug::SetCrashKeyString(
4057 base::debug::AllocateCrashKeyString("new_partition_id",
4058 base::debug::CrashKeySize::Size256),
4059 new_partition_id.ToString());
Aaron Colwellb731a0ae2021-03-19 19:14:474060}
4061
Minggang Wangb9f3fa92021-07-01 15:30:314062std::vector<blink::mojom::AppHistoryEntryPtr>
Nate Chapind1fe3612021-04-16 20:45:574063NavigationControllerImpl::PopulateSingleAppHistoryEntryVector(
4064 Direction direction,
4065 int entry_index,
4066 const url::Origin& pending_origin,
4067 FrameTreeNode* node,
4068 SiteInstance* site_instance,
4069 int64_t previous_item_sequence_number) {
Minggang Wangb9f3fa92021-07-01 15:30:314070 std::vector<blink::mojom::AppHistoryEntryPtr> entries;
Nate Chapind1fe3612021-04-16 20:45:574071 int offset = direction == Direction::kForward ? 1 : -1;
4072 for (int i = entry_index + offset; i >= 0 && i < GetEntryCount();
4073 i += offset) {
4074 FrameNavigationEntry* frame_entry = GetEntryAtIndex(i)->GetFrameEntry(node);
4075 if (!frame_entry || !frame_entry->committed_origin())
4076 break;
4077 if (site_instance != frame_entry->site_instance())
4078 break;
4079 if (!pending_origin.IsSameOriginWith(*frame_entry->committed_origin()))
4080 break;
4081 if (previous_item_sequence_number == frame_entry->item_sequence_number())
4082 continue;
4083 blink::ExplodedPageState exploded_page_state;
4084 if (blink::DecodePageState(frame_entry->page_state().ToEncodedData(),
4085 &exploded_page_state)) {
4086 blink::ExplodedFrameState frame_state = exploded_page_state.top;
Minggang Wangb9f3fa92021-07-01 15:30:314087 blink::mojom::AppHistoryEntryPtr entry =
4088 blink::mojom::AppHistoryEntry::New(
4089 frame_state.app_history_key.value_or(std::u16string()),
4090 frame_state.app_history_id.value_or(std::u16string()),
4091 frame_state.url_string.value_or(std::u16string()));
Nate Chapind1fe3612021-04-16 20:45:574092 DCHECK(pending_origin.CanBeDerivedFrom(GURL(entry->url)));
4093 entries.push_back(std::move(entry));
4094 previous_item_sequence_number = frame_entry->item_sequence_number();
4095 }
4096 }
4097 // If |entries| was constructed by iterating backwards from
4098 // |entry_index|, it's latest-at-the-front, but the renderer will want it
4099 // earliest-at-the-front. Reverse it.
4100 if (direction == Direction::kBack)
4101 std::reverse(entries.begin(), entries.end());
4102 return entries;
4103}
4104
4105void NavigationControllerImpl::PopulateAppHistoryEntryVectors(
4106 NavigationRequest* request) {
4107 url::Origin pending_origin =
4108 request->commit_params().origin_to_commit
4109 ? *request->commit_params().origin_to_commit
4110 : url::Origin::Create(request->common_params().url);
4111
4112 FrameTreeNode* node = request->frame_tree_node();
4113 scoped_refptr<SiteInstance> site_instance =
4114 request->GetRenderFrameHost()->GetSiteInstance();
4115
4116 // NOTE: |entry_index| is an estimate of the index where this entry will
4117 // commit, but it may be wrong in corner cases (e.g., if we are at the max
4118 // entry limit, the earliest entry will be dropped). This is ok because this
4119 // algorithm only uses |entry_index| to walk the entry list as it stands right
4120 // now, and it isn't saved for anything post-commit.
4121 int entry_index = GetPendingEntryIndex();
4122 bool will_create_new_entry = false;
4123 if (NavigationTypeUtils::IsReload(request->common_params().navigation_type) ||
4124 request->common_params().should_replace_current_entry) {
4125 entry_index = GetLastCommittedEntryIndex();
4126 } else if (entry_index == -1) {
4127 will_create_new_entry = true;
4128 entry_index = GetLastCommittedEntryIndex() + 1;
4129 }
4130
4131 int64_t pending_item_sequence_number = 0;
4132 if (auto* pending_entry = GetPendingEntry()) {
4133 if (auto* frame_entry = pending_entry->GetFrameEntry(node))
4134 pending_item_sequence_number = frame_entry->item_sequence_number();
4135 }
4136
4137 request->set_app_history_back_entries(PopulateSingleAppHistoryEntryVector(
4138 Direction::kBack, entry_index, pending_origin, node, site_instance.get(),
4139 pending_item_sequence_number));
4140
4141 // Don't populate forward entries if they will be truncated by a new entry.
4142 if (!will_create_new_entry) {
4143 request->set_app_history_forward_entries(
4144 PopulateSingleAppHistoryEntryVector(
4145 Direction::kForward, entry_index, pending_origin, node,
4146 site_instance.get(), pending_item_sequence_number));
4147 }
4148}
4149
Nate Chapinfbfe5af2021-06-10 17:22:084150NavigationControllerImpl::HistoryNavigationAction
4151NavigationControllerImpl::ShouldNavigateToEntryForAppHistoryKey(
4152 FrameNavigationEntry* current_entry,
4153 FrameNavigationEntry* target_entry,
4154 const std::string& app_history_key) {
4155 if (!target_entry || !target_entry->committed_origin())
4156 return HistoryNavigationAction::kStopLooking;
4157 if (current_entry->site_instance() != target_entry->site_instance())
4158 return HistoryNavigationAction::kStopLooking;
4159 if (!current_entry->committed_origin()->IsSameOriginWith(
4160 *target_entry->committed_origin())) {
4161 return HistoryNavigationAction::kStopLooking;
4162 }
4163
4164 // NOTE: We don't actually care between kSameDocument and
4165 // kDifferentDocument, so always use kDifferentDocument by convention.
4166 if (target_entry->app_history_key() == app_history_key)
4167 return HistoryNavigationAction::kDifferentDocument;
4168 return HistoryNavigationAction::kKeepLooking;
4169}
4170
4171void NavigationControllerImpl::NavigateToAppHistoryKey(FrameTreeNode* node,
4172 const std::string& key) {
4173 FrameNavigationEntry* current_entry =
4174 GetLastCommittedEntry()->GetFrameEntry(node);
4175 if (!current_entry)
4176 return;
4177
4178 // We want to find the nearest matching entry that is contiguously
4179 // same-instance and same-origin. Check back first, then forward.
4180 // TODO(japhet): Link spec here once it exists.
4181 for (int i = GetCurrentEntryIndex() - 1; i >= 0; i--) {
4182 auto result = ShouldNavigateToEntryForAppHistoryKey(
4183 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4184 if (result == HistoryNavigationAction::kStopLooking)
4185 break;
4186 if (result != HistoryNavigationAction::kKeepLooking) {
4187 GoToIndex(i);
4188 return;
4189 }
4190 }
4191 for (int i = GetCurrentEntryIndex() + 1; i < GetEntryCount(); i++) {
4192 auto result = ShouldNavigateToEntryForAppHistoryKey(
4193 current_entry, GetEntryAtIndex(i)->GetFrameEntry(node), key);
4194 if (result == HistoryNavigationAction::kStopLooking)
4195 break;
4196 if (result != HistoryNavigationAction::kKeepLooking) {
4197 GoToIndex(i);
4198 return;
4199 }
4200 }
4201}
4202
[email protected]8ff00d72012-10-23 19:12:214203} // namespace content