blob: 12f5a1a1282b8790f21185c926f438fa4fc7499e [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"
[email protected]348fbaac2013-06-11 06:31:5145#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0046#include "base/strings/utf_string_conversions.h"
[email protected]a43858f2013-06-28 15:18:3747#include "base/time/time.h"
Carlos Caballero40b0efd2021-01-26 11:55:0048#include "base/trace_event/optional_trace_event.h"
ssid3e765612015-01-28 04:03:4249#include "base/trace_event/trace_event.h"
servolkf3955532015-05-16 00:01:5950#include "build/build_config.h"
[email protected]eabfe1912014-05-12 10:07:2851#include "cc/base/switches.h"
jamescookda2505812015-03-20 18:01:1852#include "content/browser/bad_message.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3953#include "content/browser/blob_storage/chrome_blob_storage_context.h"
[email protected]825b1662012-03-12 19:07:3154#include "content/browser/browser_url_handler_impl.h"
[email protected]5f2aa722013-08-07 16:59:4155#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0456#include "content/browser/dom_storage/session_storage_namespace_impl.h"
danakjc492bf82020-09-09 20:02:4457#include "content/browser/renderer_host/debug_urls.h"
58#include "content/browser/renderer_host/frame_tree.h"
Carlos Caballero40b0efd2021-01-26 11:55:0059#include "content/browser/renderer_host/frame_tree_node.h"
danakjc492bf82020-09-09 20:02:4460#include "content/browser/renderer_host/navigation_entry_impl.h"
61#include "content/browser/renderer_host/navigation_request.h"
62#include "content/browser/renderer_host/navigator.h"
63#include "content/browser/renderer_host/render_frame_host_delegate.h"
Carlos Caballero40b0efd2021-01-26 11:55:0064#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]b6583592012-01-25 19:52:3365#include "content/browser/site_instance_impl.h"
Tsuyoshi Horo37493af2019-11-21 23:43:1466#include "content/browser/web_package/web_bundle_navigation_info.h"
Camille Lamy5193caa2018-10-12 11:59:4267#include "content/common/content_constants_internal.h"
[email protected]37567b432014-02-12 01:12:2268#include "content/common/frame_messages.h"
Nasko Oskovae49e292020-08-13 02:08:5169#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1170#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1971#include "content/public/browser/content_browser_client.h"
[email protected]d9083482012-01-06 00:38:4672#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0073#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3874#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1675#include "content/public/browser/notification_types.h"
Lei Zhang96031532019-10-10 19:05:4776#include "content/public/browser/render_view_host.h"
[email protected]9677a3c2012-12-22 04:18:5877#include "content/public/browser/render_widget_host.h"
78#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1079#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3480#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:1981#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:3882#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:1083#include "content/public/common/content_features.h"
Lei Zhang96031532019-10-10 19:05:4784#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4385#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:5986#include "media/base/mime_util.h"
[email protected]a23de8572009-06-03 02:16:3287#include "net/base/escape.h"
Arthur Sonzogni620cec62018-12-13 13:08:5788#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:1889#include "services/metrics/public/cpp/ukm_builders.h"
90#include "services/metrics/public/cpp/ukm_recorder.h"
Yue Ru Sun128804932020-09-30 22:19:1791#include "services/metrics/public/cpp/ukm_source_id.h"
[email protected]9677a3c2012-12-22 04:18:5892#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3993#include "third_party/blink/public/common/blob/blob_utils.h"
Blink Reformata30d4232018-04-07 15:31:0694#include "third_party/blink/public/common/mime_util/mime_util.h"
[email protected]cca6f392014-05-28 21:32:2695#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:2996
[email protected]8ff00d72012-10-23 19:12:2197namespace content {
[email protected]e9ba4472008-09-14 15:42:4398namespace {
99
100// Invoked when entries have been pruned, or removed. For example, if the
101// current entries are [google, digg, yahoo], with the current entry google,
102// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47103void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50104 int index,
[email protected]c12bf1a12008-09-17 16:28:49105 int count) {
[email protected]8ff00d72012-10-23 19:12:21106 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50107 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49108 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14109 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43110}
111
[email protected]e9ba4472008-09-14 15:42:43112// Configure all the NavigationEntries in entries for restore. This resets
113// the transition type to reload and makes sure the content state isn't empty.
114void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57115 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48116 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47117 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43118 // Use a transition type of reload so that we don't incorrectly increase
119 // the typed count.
Lei Zhang96031532019-10-10 19:05:47120 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
121 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43122 }
123}
124
[email protected]bf70edce2012-06-20 22:32:22125// Determines whether or not we should be carrying over a user agent override
126// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57127bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22128 return last_entry && last_entry->GetIsOverridingUserAgent();
129}
130
Camille Lamy5193caa2018-10-12 11:59:42131// Determines whether to override user agent for a navigation.
132bool ShouldOverrideUserAgent(
133 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57134 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42135 switch (override_user_agent) {
136 case NavigationController::UA_OVERRIDE_INHERIT:
137 return ShouldKeepOverride(last_committed_entry);
138 case NavigationController::UA_OVERRIDE_TRUE:
139 return true;
140 case NavigationController::UA_OVERRIDE_FALSE:
141 return false;
Camille Lamy5193caa2018-10-12 11:59:42142 }
143 NOTREACHED();
144 return false;
145}
146
clamy0a656e42018-02-06 18:18:28147// Returns true this navigation should be treated as a reload. For e.g.
148// navigating to the last committed url via the address bar or clicking on a
149// link which results in a navigation to the last committed or pending
150// navigation, etc.
Fergal Daly766177d2020-07-07 07:54:04151// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
152// |base_url_for_data_url|, |transition_type| correspond to the new navigation
153// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
154// navigation that committed.
155bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
156 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57157 const GURL& virtual_url,
158 const GURL& base_url_for_data_url,
159 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57160 bool is_post,
161 bool is_reload,
162 bool is_navigation_to_existing_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57163 NavigationEntryImpl* last_committed_entry) {
Fergal Daly766177d2020-07-07 07:54:04164 if (is_reload || is_navigation_to_existing_entry)
clamy0a656e42018-02-06 18:18:28165 return false;
clamy0a656e42018-02-06 18:18:28166 // Only convert to reload if at least one navigation committed.
167 if (!last_committed_entry)
ananta3bdd8ae2016-12-22 17:11:55168 return false;
169
arthursonzogni7a8243682017-12-14 16:41:42170 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28171 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42172 return false;
173
ananta3bdd8ae2016-12-22 17:11:55174 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
175 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
176 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28177 bool transition_type_can_be_converted = false;
178 if (ui::PageTransitionCoreTypeIs(transition_type,
179 ui::PAGE_TRANSITION_RELOAD) &&
180 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
181 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55182 }
clamy0a656e42018-02-06 18:18:28183 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55184 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28185 transition_type_can_be_converted = true;
186 }
187 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
188 transition_type_can_be_converted = true;
189 if (!transition_type_can_be_converted)
190 return false;
191
192 // This check is required for cases like view-source:, etc. Here the URL of
193 // the navigation entry would contain the url of the page, while the virtual
194 // URL contains the full URL including the view-source prefix.
195 if (virtual_url != last_committed_entry->GetVirtualURL())
196 return false;
197
Fergal Daly766177d2020-07-07 07:54:04198 // Check that the URLs match.
199 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
200 // If there's no frame entry then by definition the URLs don't match.
201 if (!frame_entry)
202 return false;
203
204 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28205 return false;
206
207 // This check is required for Android WebView loadDataWithBaseURL. Apps
208 // can pass in anything in the base URL and we need to ensure that these
209 // match before classifying it as a reload.
210 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
211 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
212 return false;
ananta3bdd8ae2016-12-22 17:11:55213 }
214
clamy0a656e42018-02-06 18:18:28215 // Skip entries with SSL errors.
216 if (last_committed_entry->ssl_error())
217 return false;
218
219 // Don't convert to a reload when the last navigation was a POST or the new
220 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04221 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28222 return false;
223
224 return true;
ananta3bdd8ae2016-12-22 17:11:55225}
226
Nasko Oskov03912102019-01-11 00:21:32227bool DoesURLMatchOriginForNavigation(
228 const GURL& url,
229 const base::Optional<url::Origin>& origin) {
230 // If there is no origin supplied there is nothing to match. This can happen
231 // for navigations to a pending entry and therefore it should be allowed.
232 if (!origin)
233 return true;
234
235 return origin->CanBeDerivedFrom(url);
236}
237
238base::Optional<url::Origin> GetCommittedOriginForFrameEntry(
arthursonzogni73fe3212020-11-17 13:24:07239 const mojom::DidCommitProvisionalLoadParams& params) {
Nasko Oskov03912102019-01-11 00:21:32240 // Error pages commit in an opaque origin, yet have the real URL that resulted
241 // in an error as the |params.url|. Since successful reload of an error page
242 // should commit in the correct origin, setting the opaque origin on the
243 // FrameNavigationEntry will be incorrect.
244 if (params.url_is_unreachable)
245 return base::nullopt;
246
247 return base::make_optional(params.origin);
248}
249
Camille Lamy5193caa2018-10-12 11:59:42250bool IsValidURLForNavigation(bool is_main_frame,
251 const GURL& virtual_url,
252 const GURL& dest_url) {
253 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
254 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
255 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
256 << virtual_url.possibly_invalid_spec();
257 return false;
258 }
259
260 // Don't attempt to navigate to non-empty invalid URLs.
261 if (!dest_url.is_valid() && !dest_url.is_empty()) {
262 LOG(WARNING) << "Refusing to load invalid URL: "
263 << dest_url.possibly_invalid_spec();
264 return false;
265 }
266
267 // The renderer will reject IPC messages with URLs longer than
268 // this limit, so don't attempt to navigate with a longer URL.
269 if (dest_url.spec().size() > url::kMaxURLChars) {
270 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
271 << " characters.";
272 return false;
273 }
274
Aaron Colwell33109c592020-04-21 21:31:19275 // Reject renderer debug URLs because they should have been handled before
276 // we get to this point. This check handles renderer debug URLs
277 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
278 // provides defense-in-depth if a renderer debug URL manages to get here via
279 // some other path. We want to reject the navigation here so it doesn't
280 // violate assumptions in downstream code.
281 if (IsRendererDebugURL(dest_url)) {
282 LOG(WARNING) << "Refusing to load renderer debug URL: "
283 << dest_url.possibly_invalid_spec();
284 return false;
285 }
286
Camille Lamy5193caa2018-10-12 11:59:42287 return true;
288}
289
Mikel Astizba9cf2fd2017-12-17 10:38:10290// See replaced_navigation_entry_data.h for details: this information is meant
291// to ensure |*output_entry| keeps track of its original URL (landing page in
292// case of server redirects) as it gets replaced (e.g. history.replaceState()),
293// without overwriting it later, for main frames.
294void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57295 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10296 NavigationEntryImpl* output_entry) {
297 if (output_entry->GetReplacedEntryData().has_value())
298 return;
299
300 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57301 data.first_committed_url = replaced_entry->GetURL();
302 data.first_timestamp = replaced_entry->GetTimestamp();
303 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29304 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10305}
306
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51307mojom::NavigationType GetNavigationType(const GURL& old_url,
308 const GURL& new_url,
309 ReloadType reload_type,
310 NavigationEntryImpl* entry,
311 const FrameNavigationEntry& frame_entry,
danakjd83d706d2020-11-25 22:11:12312 bool has_pending_cross_document_commit,
danakjb952ef12021-01-14 19:58:49313 bool is_currently_error_page,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51314 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23315 // Reload navigations
316 switch (reload_type) {
317 case ReloadType::NORMAL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51318 return mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23319 case ReloadType::BYPASSING_CACHE:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51320 return mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23321 case ReloadType::ORIGINAL_REQUEST_URL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51322 return mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23323 case ReloadType::NONE:
324 break; // Fall through to rest of function.
325 }
326
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08327 if (entry->IsRestored()) {
Lei Zhang96031532019-10-10 19:05:47328 return entry->GetHasPostData() ? mojom::NavigationType::RESTORE_WITH_POST
329 : mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23330 }
331
danakjb952ef12021-01-14 19:58:49332 const bool can_be_same_document =
333 // A pending cross-document commit means this navigation will not occur in
334 // the current document, as that document would end up being replaced in
335 // the meantime.
336 !has_pending_cross_document_commit &&
337 // If the current document is an error page, we should always treat it as
338 // a different-document navigation so that we'll attempt to load the
339 // document we're navigating to (and not stay in the current error page).
340 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12341
clamyea99ea12018-05-28 13:54:23342 // History navigations.
343 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12344 return can_be_same_document && is_same_document_history_load
Lei Zhang96031532019-10-10 19:05:47345 ? mojom::NavigationType::HISTORY_SAME_DOCUMENT
346 : mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23347 }
348 DCHECK(!is_same_document_history_load);
349
350 // A same-document fragment-navigation happens when the only part of the url
351 // that is modified is after the '#' character.
352 //
353 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12354 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23355 //
356 // Note: this check is only valid for navigations that are not history
357 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
358 // history navigation from 'A#foo' to 'A#bar' is not a same-document
359 // navigation, but a different-document one. This is why history navigation
360 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47361 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
362 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12363
364 // The one case where we do the wrong thing here and incorrectly choose
365 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
366 // the renderer is a frameset. All frameset navigations should be
367 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
368 // navigation would do the right thing, as it would send it to the browser and
369 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
370 // into this method). But since we can't tell that case here for browser-
371 // initiated navigations, we have to get the renderer involved. In that case
372 // the navigation would be restarted due to the renderer spending a reply of
373 // mojom::CommitResult::RestartCrossDocument.
374
375 return can_be_same_document && is_same_doc
376 ? mojom::NavigationType::SAME_DOCUMENT
377 : mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23378}
379
Camille Lamy5193caa2018-10-12 11:59:42380// Adjusts the original input URL if needed, to get the URL to actually load and
381// the virtual URL, which may differ.
382void RewriteUrlForNavigation(const GURL& original_url,
383 BrowserContext* browser_context,
384 GURL* url_to_load,
385 GURL* virtual_url,
386 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42387 // Allow the browser URL handler to rewrite the URL. This will, for example,
388 // remove "view-source:" from the beginning of the URL to get the URL that
389 // will actually be loaded. This real URL won't be shown to the user, just
390 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31391 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42392 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
393 url_to_load, browser_context, reverse_on_redirect);
394}
395
396#if DCHECK_IS_ON()
397// Helper sanity check function used in debug mode.
398void ValidateRequestMatchesEntry(NavigationRequest* request,
399 NavigationEntryImpl* entry) {
400 if (request->frame_tree_node()->IsMainFrame()) {
401 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
402 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
403 request->common_params().transition, entry->GetTransitionType()));
404 }
405 DCHECK_EQ(request->common_params().should_replace_current_entry,
406 entry->should_replace_entry());
Nasko Oskovc36327d2019-01-03 23:23:04407 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42408 entry->should_clear_history_list());
409 DCHECK_EQ(request->common_params().has_user_gesture,
410 entry->has_user_gesture());
411 DCHECK_EQ(request->common_params().base_url_for_data_url,
412 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04413 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42414 entry->GetCanLoadLocalResources());
415 DCHECK_EQ(request->common_params().started_from_context_menu,
416 entry->has_started_from_context_menu());
417
418 FrameNavigationEntry* frame_entry =
419 entry->GetFrameEntry(request->frame_tree_node());
420 if (!frame_entry) {
421 NOTREACHED();
422 return;
423 }
424
Camille Lamy5193caa2018-10-12 11:59:42425 DCHECK_EQ(request->common_params().method, frame_entry->method());
426
Nasko Oskovc36327d2019-01-03 23:23:04427 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42428 if (redirect_size == frame_entry->redirect_chain().size()) {
429 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04430 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42431 frame_entry->redirect_chain()[i]);
432 }
433 } else {
434 NOTREACHED();
435 }
436}
437#endif // DCHECK_IS_ON()
438
Dave Tapuska8bfd84c2019-03-26 20:47:16439// Returns whether the session history NavigationRequests in |navigations|
440// would stay within the subtree of the sandboxed iframe in
441// |sandbox_frame_tree_node_id|.
442bool DoesSandboxNavigationStayWithinSubtree(
443 int sandbox_frame_tree_node_id,
444 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
445 for (auto& item : navigations) {
446 bool within_subtree = false;
447 // Check whether this NavigationRequest affects a frame within the
448 // sandboxed frame's subtree by walking up the tree looking for the
449 // sandboxed frame.
450 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03451 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16452 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
453 within_subtree = true;
454 break;
455 }
456 }
457 if (!within_subtree)
458 return false;
459 }
460 return true;
461}
462
Antonio Sartori78a749f2020-11-30 12:03:39463bool ShouldStoreDocumentPoliciesInFrameNavigationEntry(
464 const NavigationRequest* request) {
465 // For local schemes we need to store the policy container in the
466 // FrameNavigationEntry, so that we can reload it in case of history
467 // navigation.
468 //
469 // TODO(https://crbug.com/1146361 and https://crbug.com/1146362): blob: and
470 // filesystem: should be removed from this list when we have properly
471 // implemented storing their policy container in the respective store.
472 return (request->common_params().url.SchemeIs(url::kAboutScheme) ||
473 request->common_params().url.SchemeIs(url::kDataScheme) ||
474 request->common_params().url.SchemeIsBlob() ||
475 request->common_params().url.SchemeIsFileSystem());
476}
477
[email protected]e9ba4472008-09-14 15:42:43478} // namespace
479
arthursonzogni66f711c2019-10-08 14:40:36480// NavigationControllerImpl::PendingEntryRef------------------------------------
481
482NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
483 base::WeakPtr<NavigationControllerImpl> controller)
484 : controller_(controller) {}
485
486NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
487 if (!controller_) // Can be null with interstitials.
488 return;
489
490 controller_->PendingEntryRefDeleted(this);
491}
492
[email protected]d202a7c2012-01-04 07:53:47493// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29494
[email protected]23a918b2014-07-15 09:51:36495const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23496
[email protected]765b35502008-08-21 00:51:20497// static
[email protected]d202a7c2012-01-04 07:53:47498size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23499 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20500
[email protected]e6fec472013-05-14 05:29:02501// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20502// when testing.
503static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29504
[email protected]71fde352011-12-29 03:29:56505// static
dcheng9bfa5162016-04-09 01:00:57506std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
507 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10508 Referrer referrer,
509 base::Optional<url::Origin> initiator_origin,
510 ui::PageTransition transition,
511 bool is_renderer_initiated,
512 const std::string& extra_headers,
513 BrowserContext* browser_context,
514 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
515 return NavigationControllerImpl::CreateNavigationEntry(
516 url, referrer, std::move(initiator_origin),
517 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Rakina Zata Amni66992a82020-06-24 03:57:52518 extra_headers, browser_context, std::move(blob_url_loader_factory),
Scott Violet5ae6c42e2020-10-28 02:47:37519 false /* should_replace_entry */, nullptr /* web_contents */);
Lukasz Anforowicz641234d52019-11-07 21:07:10520}
521
522// static
523std::unique_ptr<NavigationEntryImpl>
524NavigationControllerImpl::CreateNavigationEntry(
525 const GURL& url,
526 Referrer referrer,
527 base::Optional<url::Origin> initiator_origin,
528 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57529 ui::PageTransition transition,
530 bool is_renderer_initiated,
531 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09532 BrowserContext* browser_context,
Rakina Zata Amni66992a82020-06-24 03:57:52533 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Scott Violet5ae6c42e2020-10-28 02:47:37534 bool should_replace_entry,
535 WebContents* web_contents) {
Camille Lamy5193caa2018-10-12 11:59:42536 GURL url_to_load;
537 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56538 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42539 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
540 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56541
Lukasz Anforowicz641234d52019-11-07 21:07:10542 // Let the NTP override the navigation params and pretend that this is a
543 // browser-initiated, bookmark-like navigation.
544 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violet5ae6c42e2020-10-28 02:47:37545 web_contents, source_site_instance, &transition, &is_renderer_initiated,
546 &referrer, &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10547
Patrick Monettef507e982019-06-19 20:18:06548 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28549 nullptr, // The site instance for tabs is sent on navigation
550 // (WebContents::GetSiteInstance).
Lukasz Anforowicz435bcb582019-07-12 20:50:06551 url_to_load, referrer, initiator_origin, base::string16(), transition,
Camille Lamy5193caa2018-10-12 11:59:42552 is_renderer_initiated, blob_url_loader_factory);
553 entry->SetVirtualURL(virtual_url);
554 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56555 entry->set_update_virtual_url_with_url(reverse_on_redirect);
556 entry->set_extra_headers(extra_headers);
Rakina Zata Amni66992a82020-06-24 03:57:52557 entry->set_should_replace_entry(should_replace_entry);
Patrick Monettef507e982019-06-19 20:18:06558 return entry;
[email protected]71fde352011-12-29 03:29:56559}
560
[email protected]cdcb1dee2012-01-04 00:46:20561// static
562void NavigationController::DisablePromptOnRepost() {
563 g_check_for_repost = false;
564}
565
[email protected]c5b88d82012-10-06 17:03:33566base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
567 base::Time t) {
568 // If |t| is between the water marks, we're in a run of duplicates
569 // or just getting out of it, so increase the high-water mark to get
570 // a time that probably hasn't been used before and return it.
571 if (low_water_mark_ <= t && t <= high_water_mark_) {
572 high_water_mark_ += base::TimeDelta::FromMicroseconds(1);
573 return high_water_mark_;
574 }
575
576 // Otherwise, we're clear of the last duplicate run, so reset the
577 // water marks.
578 low_water_mark_ = high_water_mark_ = t;
579 return t;
580}
581
ckitagawa0faa5e42020-06-17 17:30:54582NavigationControllerImpl::ScopedShowRepostDialogForTesting::
583 ScopedShowRepostDialogForTesting()
584 : was_disallowed_(g_check_for_repost) {
585 g_check_for_repost = true;
586}
587
588NavigationControllerImpl::ScopedShowRepostDialogForTesting::
589 ~ScopedShowRepostDialogForTesting() {
590 g_check_for_repost = was_disallowed_;
591}
592
[email protected]d202a7c2012-01-04 07:53:47593NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00594 BrowserContext* browser_context,
595 FrameTree& frame_tree,
596 NavigationControllerDelegate* delegate)
597 : frame_tree_(frame_tree),
598 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57599 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22600 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47601 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37602 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29603}
604
[email protected]d202a7c2012-01-04 07:53:47605NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00606 // The NavigationControllerImpl might be called inside its delegate
607 // destructor. Calling it is not valid anymore.
608 delegate_ = nullptr;
609 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29610}
611
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57612WebContents* NavigationControllerImpl::GetWebContents() {
[email protected]ec6c05f2013-10-23 18:41:57613 return delegate_->GetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32614}
615
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57616BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55617 return browser_context_;
618}
619
[email protected]d202a7c2012-01-04 07:53:47620void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30621 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36622 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57623 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57624 // Verify that this controller is unused and that the input is valid.
Lei Zhang96031532019-10-10 19:05:47625 DCHECK_EQ(0, GetEntryCount());
626 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57627 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14628 selected_navigation < static_cast<int>(entries->size()));
arthursonzogni5c4c202d2017-04-25 23:41:27629 DCHECK_EQ(-1, pending_entry_index_);
[email protected]ce3fa3c2009-04-20 19:55:57630
[email protected]ce3fa3c2009-04-20 19:55:57631 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44632 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03633 entries_.reserve(entries->size());
634 for (auto& entry : *entries)
dcheng36b6aec92015-12-26 06:16:36635 entries_.push_back(
636 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
avif16f85a72015-11-13 18:25:03637
638 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
639 // cleared out safely.
640 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57641
642 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36643 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57644}
645
toyoshim6142d96f2016-12-19 09:07:25646void NavigationControllerImpl::Reload(ReloadType reload_type,
647 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28648 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28649 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32650 int current_index = -1;
651
Carlos IL42b416592019-10-07 23:10:36652 if (entry_replaced_by_post_commit_error_) {
653 // If there is an entry that was replaced by a currently active post-commit
654 // error navigation, this can't be the initial navigation.
655 DCHECK(!IsInitialNavigation());
656 // If the current entry is a post commit error, we reload the entry it
657 // replaced instead. We leave the error entry in place until a commit
658 // replaces it, but the pending entry points to the original entry in the
659 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
660 // case that pending_entry_ != entries_[pending_entry_index_].
661 entry = entry_replaced_by_post_commit_error_.get();
662 current_index = GetCurrentEntryIndex();
663 } else if (IsInitialNavigation() && pending_entry_) {
664 // If we are reloading the initial navigation, just use the current
665 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32666 entry = pending_entry_;
667 // The pending entry might be in entries_ (e.g., after a Clone), so we
668 // should also update the current_index.
669 current_index = pending_entry_index_;
670 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00671 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32672 current_index = GetCurrentEntryIndex();
673 if (current_index != -1) {
creis3da03872015-02-20 21:12:32674 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32675 }
[email protected]979a4bc2013-04-24 01:27:15676 }
[email protected]241db352013-04-22 18:04:05677
[email protected]59167c22013-06-03 18:07:32678 // If we are no where, then we can't reload. TODO(darin): We should add a
679 // CanReload method.
680 if (!entry)
681 return;
682
Takashi Toyoshimac7df3c22019-06-25 14:18:47683 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26684 entry->set_reload_type(reload_type);
685
Aran Gilman37d11632019-10-08 23:07:15686 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30687 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07688 // they really want to do this. If they do, the dialog will call us back
689 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57690 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02691
[email protected]106a0812010-03-18 00:15:12692 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57693 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47694 return;
initial.commit09911bf2008-07-26 23:55:29695 }
Lei Zhang96031532019-10-10 19:05:47696
697 if (!IsInitialNavigation())
698 DiscardNonCommittedEntries();
699
700 pending_entry_ = entry;
701 pending_entry_index_ = current_index;
702 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
703
704 NavigateToExistingPendingEntry(reload_type,
705 FrameTreeNode::kFrameTreeNodeInvalidId);
initial.commit09911bf2008-07-26 23:55:29706}
707
[email protected]d202a7c2012-01-04 07:53:47708void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48709 DCHECK(pending_reload_ != ReloadType::NONE);
710 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12711}
712
[email protected]d202a7c2012-01-04 07:53:47713void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48714 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12715 NOTREACHED();
716 } else {
toyoshim6142d96f2016-12-19 09:07:25717 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48718 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12719 }
720}
721
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57722bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09723 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11724}
725
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57726bool NavigationControllerImpl::IsInitialBlankNavigation() {
creis10a4ab72015-10-13 17:22:40727 // TODO(creis): Once we create a NavigationEntry for the initial blank page,
toyoshim0df1d3a2016-09-09 09:52:48728 // we'll need to check for entry count 1 and restore_type NONE (to exclude
729 // the cloned tab case).
creis10a4ab72015-10-13 17:22:40730 return IsInitialNavigation() && GetEntryCount() == 0;
731}
732
Aran Gilman37d11632019-10-08 23:07:15733NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
734 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58735 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03736 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58737}
738
W. James MacLean1c40862c2020-04-27 21:05:57739void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
740 const url::Origin& origin) {
741 for (int i = 0; i < GetEntryCount(); i++) {
742 auto* entry = GetEntryAtIndex(i);
743 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
744 }
745 if (entry_replaced_by_post_commit_error_) {
746 // It's possible we could come back to this entry if the error
747 // page/interstitial goes away.
748 entry_replaced_by_post_commit_error_
749 ->RegisterExistingOriginToPreventOptInIsolation(origin);
750 }
751 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
752 // visiting pending_entry_, which lacks a committed origin. This will be done
753 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
754}
755
avi25764702015-06-23 15:43:37756void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57757 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00758 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37759 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27760 DCHECK_EQ(-1, pending_entry_index_);
[email protected]8ff00d72012-10-23 19:12:21761 NotificationService::current()->Notify(
Aran Gilman37d11632019-10-08 23:07:15762 NOTIFICATION_NAV_ENTRY_PENDING, Source<NavigationController>(this),
avi25764702015-06-23 15:43:37763 Details<NavigationEntry>(pending_entry_));
[email protected]765b35502008-08-21 00:51:20764}
765
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57766NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47767 if (pending_entry_)
768 return pending_entry_;
769 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20770}
771
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57772NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32773 // The pending entry is safe to return for new (non-history), browser-
774 // initiated navigations. Most renderer-initiated navigations should not
775 // show the pending entry, to prevent URL spoof attacks.
776 //
777 // We make an exception for renderer-initiated navigations in new tabs, as
778 // long as no other page has tried to access the initial empty document in
779 // the new tab. If another page modifies this blank page, a URL spoof is
780 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32781 bool safe_to_show_pending =
782 pending_entry_ &&
783 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09784 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32785 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46786 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32787
788 // Also allow showing the pending entry for history navigations in a new tab,
789 // such as Ctrl+Back. In this case, no existing page is visible and no one
790 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15791 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
792 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32793 safe_to_show_pending = true;
794
795 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19796 return pending_entry_;
797 return GetLastCommittedEntry();
798}
799
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57800int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20801 if (pending_entry_index_ != -1)
802 return pending_entry_index_;
803 return last_committed_entry_index_;
804}
805
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57806NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20807 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28808 return nullptr;
avif16f85a72015-11-13 18:25:03809 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20810}
811
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57812bool NavigationControllerImpl::CanViewSource() {
[email protected]ec6c05f2013-10-23 18:41:57813 const std::string& mime_type = delegate_->GetContentsMimeType();
Kinuko Yasuda74702f92017-07-31 03:27:53814 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
815 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27816 NavigationEntry* visible_entry = GetVisibleEntry();
817 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39818 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16819}
820
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57821int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27822 // The last committed entry index must always be less than the number of
Carlos IL4dea8902020-05-26 15:14:29823 // entries. If there are no entries, it must be -1.
arthursonzogni5c4c202d2017-04-25 23:41:27824 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
825 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55826 return last_committed_entry_index_;
827}
828
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57829int NavigationControllerImpl::GetEntryCount() {
Carlos IL4dea8902020-05-26 15:14:29830 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55831 return static_cast<int>(entries_.size());
832}
833
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57834NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37835 if (index < 0 || index >= GetEntryCount())
836 return nullptr;
837
avif16f85a72015-11-13 18:25:03838 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25839}
840
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57841NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47842 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20843}
844
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57845int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46846 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03847}
848
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57849bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03850 if (!base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention))
851 return CanGoToOffset(-1);
852
853 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
854 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
855 return true;
856 }
857 return false;
[email protected]765b35502008-08-21 00:51:20858}
859
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57860bool NavigationControllerImpl::CanGoForward() {
avi56e40c92015-08-27 00:11:22861 return CanGoToOffset(1);
[email protected]765b35502008-08-21 00:51:20862}
863
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57864bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03865 int index = GetIndexForOffset(offset);
866 return index >= 0 && index < GetEntryCount();
867}
868
[email protected]d202a7c2012-01-04 07:53:47869void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06870 int target_index = GetIndexForOffset(-1);
871
872 // Log metrics for the number of entries that are eligible for skipping on
Shivani Sharma2d5b4b6b2019-01-08 16:07:16873 // back button and move the target index past the skippable entries, if
874 // history intervention is enabled.
shivanisha55201872018-12-13 04:29:06875 int count_entries_skipped = 0;
Shivani Sharma298d12852019-01-22 20:04:03876 bool all_skippable_entries = true;
Aran Gilman37d11632019-10-08 23:07:15877 bool history_intervention_enabled =
878 base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention);
shivanisha55201872018-12-13 04:29:06879 for (int index = target_index; index >= 0; index--) {
Shivani Sharma2d5b4b6b2019-01-08 16:07:16880 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
shivanisha55201872018-12-13 04:29:06881 count_entries_skipped++;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16882 } else {
883 if (history_intervention_enabled)
884 target_index = index;
Shivani Sharma298d12852019-01-22 20:04:03885 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06886 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16887 }
shivanisha55201872018-12-13 04:29:06888 }
889 UMA_HISTOGRAM_ENUMERATION("Navigation.BackForward.BackTargetSkipped",
890 count_entries_skipped, kMaxSessionHistoryEntries);
Shivani Sharma298d12852019-01-22 20:04:03891 UMA_HISTOGRAM_BOOLEAN("Navigation.BackForward.AllBackTargetsSkippable",
892 all_skippable_entries);
893
894 // Do nothing if all entries are skippable. Normally this path would not
895 // happen as consumers would have already checked it in CanGoBack but a lot of
896 // tests do not do that.
897 if (history_intervention_enabled && all_skippable_entries)
898 return;
shivanisha55201872018-12-13 04:29:06899
shivanisha55201872018-12-13 04:29:06900 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20901}
902
[email protected]d202a7c2012-01-04 07:53:47903void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06904 int target_index = GetIndexForOffset(1);
905
906 // Log metrics for the number of entries that are eligible for skipping on
Shivani Sharma2d5b4b6b2019-01-08 16:07:16907 // forward button and move the target index past the skippable entries, if
908 // history intervention is enabled.
909 // Note that at least one entry (the last one) will be non-skippable since
910 // entries are marked skippable only when they add another entry because of
911 // redirect or pushState.
shivanisha55201872018-12-13 04:29:06912 int count_entries_skipped = 0;
Aran Gilman37d11632019-10-08 23:07:15913 bool history_intervention_enabled =
914 base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention);
shivanisha55201872018-12-13 04:29:06915 for (size_t index = target_index; index < entries_.size(); index++) {
Shivani Sharma2d5b4b6b2019-01-08 16:07:16916 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
shivanisha55201872018-12-13 04:29:06917 count_entries_skipped++;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16918 } else {
919 if (history_intervention_enabled)
920 target_index = index;
shivanisha55201872018-12-13 04:29:06921 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16922 }
shivanisha55201872018-12-13 04:29:06923 }
924 UMA_HISTOGRAM_ENUMERATION("Navigation.BackForward.ForwardTargetSkipped",
925 count_entries_skipped, kMaxSessionHistoryEntries);
926
shivanisha55201872018-12-13 04:29:06927 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20928}
929
[email protected]d202a7c2012-01-04 07:53:47930void NavigationControllerImpl::GoToIndex(int index) {
Dave Tapuska8bfd84c2019-03-26 20:47:16931 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId);
932}
933
934void NavigationControllerImpl::GoToIndex(int index,
935 int sandbox_frame_tree_node_id) {
sunjian30574a62017-03-21 21:39:44936 TRACE_EVENT0("browser,navigation,benchmark",
937 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:20938 if (index < 0 || index >= static_cast<int>(entries_.size())) {
939 NOTREACHED();
940 return;
941 }
942
[email protected]cbab76d2008-10-13 22:42:47943 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20944
arthursonzogni5c4c202d2017-04-25 23:41:27945 DCHECK_EQ(nullptr, pending_entry_);
946 DCHECK_EQ(-1, pending_entry_index_);
947 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:20948 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:27949 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
950 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Dave Tapuska8bfd84c2019-03-26 20:47:16951 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id);
[email protected]765b35502008-08-21 00:51:20952}
953
[email protected]d202a7c2012-01-04 07:53:47954void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:12955 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:03956 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:20957 return;
958
[email protected]9ba14052012-06-22 23:50:03959 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20960}
961
[email protected]41374f12013-07-24 02:49:28962bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:15963 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:28964 return false;
[email protected]6a13a6c2011-12-20 21:47:12965
[email protected]43032342011-03-21 14:10:31966 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:28967 return true;
[email protected]cbab76d2008-10-13 22:42:47968}
969
Michael Thiessen9b14d512019-09-23 21:19:47970void NavigationControllerImpl::PruneForwardEntries() {
971 DiscardNonCommittedEntries();
972 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:47973 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:47974 if (num_removed <= 0)
975 return;
976 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
977 NotifyPrunedEntries(this, remove_start_index /* start index */,
978 num_removed /* count */);
979}
980
Aran Gilman37d11632019-10-08 23:07:15981void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
982 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:32983 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:31984 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:51985 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
986 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:32987 }
988}
989
Aran Gilman37d11632019-10-08 23:07:15990void NavigationControllerImpl::LoadURL(const GURL& url,
991 const Referrer& referrer,
992 ui::PageTransition transition,
993 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:47994 LoadURLParams params(url);
995 params.referrer = referrer;
996 params.transition_type = transition;
997 params.extra_headers = extra_headers;
998 LoadURLWithParams(params);
999}
1000
1001void NavigationControllerImpl::LoadURLWithParams(const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061002 if (params.is_renderer_initiated)
1003 DCHECK(params.initiator_origin.has_value());
1004
naskob8744d22014-08-28 17:07:431005 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151006 "NavigationControllerImpl::LoadURLWithParams", "url",
1007 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291008 bool is_explicit_navigation =
1009 GetContentClient()->browser()->IsExplicitNavigation(
1010 params.transition_type);
1011 if (HandleDebugURL(params.url, params.transition_type,
1012 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431013 // If Telemetry is running, allow the URL load to proceed as if it's
1014 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491015 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431016 cc::switches::kEnableGpuBenchmarking))
1017 return;
1018 }
[email protected]8bf1048012012-02-08 01:22:181019
[email protected]cf002332012-08-14 19:17:471020 // Checks based on params.load_type.
1021 switch (params.load_type) {
1022 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431023 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471024 break;
1025 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261026 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471027 NOTREACHED() << "Data load must use data scheme.";
1028 return;
1029 }
1030 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461031 }
[email protected]e47ae9472011-10-13 19:48:341032
[email protected]e47ae9472011-10-13 19:48:341033 // The user initiated a load, we don't need to reload anymore.
1034 needs_reload_ = false;
1035
clamy21718cc22018-06-13 13:34:241036 NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441037}
1038
Mohamed Abdelhalim833de902019-09-16 17:41:451039bool NavigationControllerImpl::PendingEntryMatchesRequest(
1040 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191041 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451042 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191043}
1044
[email protected]d202a7c2012-01-04 07:53:471045bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321046 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071047 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331048 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441049 bool is_same_document_navigation,
Shivani Sharmaffb32b82019-04-09 16:58:471050 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131051 NavigationRequest* navigation_request) {
1052 DCHECK(navigation_request);
[email protected]cd2e15742013-03-08 04:08:311053 is_initial_navigation_ = false;
1054
[email protected]0e8db942008-09-24 21:21:481055 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431056 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481057 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361058 if (entry_replaced_by_post_commit_error_) {
1059 if (is_same_document_navigation) {
1060 // Same document navigations should not be possible on error pages and
1061 // would leave the controller in a weird state. Kill the renderer if
1062 // that happens.
1063 bad_message::ReceivedBadMessage(
1064 rfh->GetProcess(), bad_message::NC_SAME_DOCUMENT_POST_COMMIT_ERROR);
1065 }
1066 // Any commit while a post-commit error page is showing should put the
1067 // original entry back, replacing the error page's entry. This includes
1068 // reloads, where the original entry was used as the pending entry and
1069 // should now be at the correct index at commit time.
1070 entries_[last_committed_entry_index_] =
1071 std::move(entry_replaced_by_post_commit_error_);
1072 }
Fergal Daly8e33cf62020-12-12 01:06:071073 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551074 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431075 if (pending_entry_ &&
1076 pending_entry_->GetIsOverridingUserAgent() !=
1077 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1078 overriding_user_agent_changed = true;
[email protected]0e8db942008-09-24 21:21:481079 } else {
Fergal Daly8e33cf62020-12-12 01:06:071080 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481081 details->previous_entry_index = -1;
1082 }
[email protected]ecd9d8702008-08-28 22:10:171083
Alexander Timind2f2e4f22019-04-02 20:04:531084 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1085 // implementing back-forward cache.
1086
1087 // Create a new metrics object or reuse the previous one depending on whether
1088 // it's a main frame navigation or not.
1089 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics =
1090 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1091 GetLastCommittedEntry(), !rfh->GetParent(),
1092 params.document_sequence_number);
1093 // Notify the last active entry that we have navigated away.
1094 if (!rfh->GetParent() && !is_same_document_navigation) {
1095 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1096 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Alexander Timinde527cd2019-12-02 09:41:121097 metrics->MainFrameDidNavigateAwayFromDocument(rfh, details,
1098 navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531099 }
1100 }
1101 }
1102
fdegans9caf66a2015-07-30 21:10:421103 // If there is a pending entry at this point, it should have a SiteInstance,
1104 // except for restored entries.
jam48cea9082017-02-15 06:13:291105 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481106 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081107 pending_entry_->IsRestored());
1108 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151109 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291110 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481111 }
[email protected]e9ba4472008-09-14 15:42:431112
Nasko Oskovaee2f862018-06-15 00:05:521113 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1114 // has changed, this must be treated as a new navigation with replacement.
1115 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491116 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521117 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101118 pending_entry_->GetUniqueID() ==
1119 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521120 pending_entry_->site_instance() &&
1121 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1122 DCHECK_NE(-1, pending_entry_index_);
1123 // TODO(nasko,creis): Instead of setting this value here, set
1124 // should_replace_current_entry on the parameters we send to the
1125 // renderer process as part of CommitNavigation. The renderer should
1126 // in turn send it back here as part of |params| and it can be just
1127 // enforced and renderer process terminated on mismatch.
1128 details->did_replace_entry = true;
1129 } else {
1130 // The renderer tells us whether the navigation replaces the current entry.
1131 details->did_replace_entry = params.should_replace_current_entry;
1132 }
[email protected]bcd904482012-02-01 01:54:221133
[email protected]e9ba4472008-09-14 15:42:431134 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amnif6950d552020-11-24 03:26:101135 details->type = ClassifyNavigation(rfh, params, navigation_request);
[email protected]4bf3522c2010-08-19 21:00:201136
eugenebutee08663a2017-04-27 17:43:121137 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441138 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121139
Peter Boströmd7592132019-01-30 04:50:311140 // Make sure we do not discard the pending entry for a different ongoing
1141 // navigation when a same document commit comes in unexpectedly from the
1142 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1143 // other navigation types. See https://crbug.com/900036.
1144 // TODO(crbug.com/926009): Handle history.pushState() as well.
1145 bool keep_pending_entry = is_same_document_navigation &&
Charlie Reisc0f17d2d2021-01-12 18:52:491146 details->type == NAVIGATION_TYPE_EXISTING_ENTRY &&
Peter Boströmd7592132019-01-30 04:50:311147 pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451148 !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311149
[email protected]0e8db942008-09-24 21:21:481150 switch (details->type) {
Charlie Reisc0f17d2d2021-01-12 18:52:491151 case NAVIGATION_TYPE_NEW_ENTRY:
1152 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051153 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451154 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431155 break;
Charlie Reisc0f17d2d2021-01-12 18:52:491156 case NAVIGATION_TYPE_EXISTING_ENTRY:
1157 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1158 was_restored, navigation_request,
1159 keep_pending_entry);
[email protected]e9ba4472008-09-14 15:42:431160 break;
[email protected]8ff00d72012-10-23 19:12:211161 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471162 RendererDidNavigateNewSubframe(
1163 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451164 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431165 break;
[email protected]8ff00d72012-10-23 19:12:211166 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391167 if (!RendererDidNavigateAutoSubframe(
1168 rfh, params, details->is_same_document, navigation_request)) {
creisce0ef3572017-01-26 17:53:081169 // We don't send a notification about auto-subframe PageState during
1170 // UpdateStateForFrame, since it looks like nothing has changed. Send
1171 // it here at commit time instead.
1172 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431173 return false;
creis59d5a47cb2016-08-24 23:57:191174 }
[email protected]e9ba4472008-09-14 15:42:431175 break;
[email protected]8ff00d72012-10-23 19:12:211176 case NAVIGATION_TYPE_NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:491177 // If a pending navigation was in progress, this canceled it. We should
1178 // discard it and make sure it is removed from the URL bar. After that,
1179 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:431180 // the caller that nothing has happened.
arthursonzogni69a6a1b2019-09-17 09:23:001181 if (pending_entry_)
[email protected]20d1c992011-04-12 21:17:491182 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431183 return false;
Aran Gilman37d11632019-10-08 23:07:151184 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431185 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151186 break;
[email protected]765b35502008-08-21 00:51:201187 }
1188
[email protected]688aa65c62012-09-28 04:32:221189 // At this point, we know that the navigation has just completed, so
1190 // record the time.
1191 //
1192 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261193 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331194 base::Time timestamp =
1195 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1196 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131197 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221198
Peter Boströmd7592132019-01-30 04:50:311199 // If we aren't keeping the pending entry, there shouldn't be one at this
1200 // point. Clear it again in case any error cases above forgot to do so.
1201 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1202 // been cleared instead of protecting against it.
1203 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001204 DiscardNonCommittedEntries();
[email protected]f233e4232013-02-23 00:55:141205
[email protected]e9ba4472008-09-14 15:42:431206 // All committed entries should have nonempty content state so WebKit doesn't
1207 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471208 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321209 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221210 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441211 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531212 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1213 // implementing back-forward cache.
1214 if (!active_entry->back_forward_cache_metrics()) {
1215 active_entry->set_back_forward_cache_metrics(
1216 std::move(back_forward_cache_metrics));
1217 }
1218 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
Hajime Hoshi446206e2019-10-18 18:36:251219 navigation_request,
1220 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
naskoc7533512016-05-06 17:01:121221
Charles Reisc0507202017-09-21 00:40:021222 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1223 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1224 // A mismatch can occur if the renderer lies or due to a unique name collision
1225 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121226 FrameNavigationEntry* frame_entry =
1227 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021228 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1229 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031230 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081231 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1232 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031233 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201234
1235 // Remember the bindings the renderer process has at this point, so that
1236 // we do not grant this entry additional bindings if we come back to it.
1237 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301238 }
[email protected]132e281a2012-07-31 18:32:441239
[email protected]97d8f0d2013-10-29 16:49:211240 // Once it is committed, we no longer need to track several pieces of state on
1241 // the entry.
naskoc7533512016-05-06 17:01:121242 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131243
[email protected]49bd30e62011-03-22 20:12:591244 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221245 // TODO(creis): This check won't pass for subframes until we create entries
1246 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001247 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421248 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591249
[email protected]e9ba4472008-09-14 15:42:431250 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001251 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001252 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311253 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531254
Scott Violetc36f7462020-05-06 23:13:031255 // If the NavigationRequest was created without a NavigationEntry and
1256 // SetIsOverridingUserAgent() was called, it needs to be applied to the
1257 // NavigationEntry now.
1258 if (!navigation_request->nav_entry_id() &&
1259 navigation_request->was_set_overriding_user_agent_called()) {
1260 active_entry->SetIsOverridingUserAgent(
1261 navigation_request->entry_overrides_ua());
1262 }
1263
[email protected]93f230e02011-06-01 14:40:001264 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291265
John Abd-El-Malek380d3c5922017-09-08 00:20:311266 if (active_entry->GetURL().SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451267 navigation_request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek969cdd94e2017-07-10 22:18:161268 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus",
1269 !!active_entry->GetSSL().certificate);
1270 }
1271
aelias100c9192017-01-13 00:01:431272 if (overriding_user_agent_changed)
1273 delegate_->UpdateOverridingUserAgent();
1274
creis03b48002015-11-04 00:54:561275 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1276 // one knows the latest NavigationEntry it is showing (whether it has
1277 // committed anything in this navigation or not). This allows things like
1278 // state and title updates from RenderFrames to apply to the latest relevant
1279 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381280 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001281 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381282 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
[email protected]e9ba4472008-09-14 15:42:431283 return true;
initial.commit09911bf2008-07-26 23:55:291284}
1285
[email protected]8ff00d72012-10-23 19:12:211286NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321287 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101288 const mojom::DidCommitProvisionalLoadParams& params,
1289 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591290 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511291 "ClassifyNavigation");
1292
avi7c6f35e2015-05-08 17:52:381293 if (params.did_create_new_entry) {
Charlie Reisc0f17d2d2021-01-12 18:52:491294 // A new entry. We may or may not have a corresponding pending entry, and
avi7c6f35e2015-05-08 17:52:381295 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001296 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491297 trace_return.set_return_reason("new entry, no parent, new entry");
1298 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381299 }
1300
1301 // When this is a new subframe navigation, we should have a committed page
1302 // in which it's a subframe. This may not be the case when an iframe is
1303 // navigated on a popup navigated to about:blank (the iframe would be
1304 // written into the popup by script on the main page). For these cases,
1305 // there isn't any navigation stuff we can do, so just ignore it.
Nasko Oskovae49e292020-08-13 02:08:511306 if (!GetLastCommittedEntry()) {
1307 trace_return.set_return_reason("new entry, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381308 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511309 }
avi7c6f35e2015-05-08 17:52:381310
1311 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511312 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381313 return NAVIGATION_TYPE_NEW_SUBFRAME;
1314 }
1315
Charlie Reisc0f17d2d2021-01-12 18:52:491316 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381317 DCHECK(!params.history_list_was_cleared);
1318
avi39c1edd32015-06-04 20:06:001319 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381320 // All manual subframes would be did_create_new_entry and handled above, so
1321 // we know this is auto.
Nasko Oskovae49e292020-08-13 02:08:511322 if (GetLastCommittedEntry()) {
1323 trace_return.set_return_reason("subframe, last commmited, auto subframe");
avi7c6f35e2015-05-08 17:52:381324 return NAVIGATION_TYPE_AUTO_SUBFRAME;
Nasko Oskovae49e292020-08-13 02:08:511325 }
Lei Zhang96031532019-10-10 19:05:471326
1327 // We ignore subframes created in non-committed pages; we'd appreciate if
1328 // people stopped doing that.
Nasko Oskovae49e292020-08-13 02:08:511329 trace_return.set_return_reason("subframe, no last commmited, ignore");
Lei Zhang96031532019-10-10 19:05:471330 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381331 }
1332
Rakina Zata Amnif6950d552020-11-24 03:26:101333 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1334 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381335 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1336 // create a new page.
1337
1338 // Just like above in the did_create_new_entry case, it's possible to
1339 // scribble onto an uncommitted page. Again, there isn't any navigation
1340 // stuff that we can do, so ignore it here as well.
avi3a5b8f32015-05-28 17:50:231341 NavigationEntry* last_committed = GetLastCommittedEntry();
Nasko Oskovae49e292020-08-13 02:08:511342 if (!last_committed) {
1343 trace_return.set_return_reason("nav entry 0, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381344 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511345 }
avi7c6f35e2015-05-08 17:52:381346
Charles Reis1378111f2017-11-08 21:44:061347 // This is history.replaceState() or history.reload().
Nasko Oskov332593c2018-08-16 17:21:341348 // TODO(nasko): With error page isolation, reloading an existing session
1349 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491350 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341351 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511352 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491353 "nav entry 0, last committed, existing entry");
1354 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381355 }
1356
Rakina Zata Amnif6950d552020-11-24 03:26:101357 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521358 // If the SiteInstance of the |pending_entry_| does not match the
1359 // SiteInstance that got committed, treat this as a new navigation with
1360 // replacement. This can happen if back/forward/reload encounters a server
1361 // redirect to a different site or an isolated error page gets successfully
1362 // reloaded into a different SiteInstance.
1363 if (pending_entry_->site_instance() &&
1364 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491365 trace_return.set_return_reason("pending matching nav entry, new entry");
1366 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521367 }
creis77c9aa32015-09-25 19:59:421368
Nasko Oskovaee2f862018-06-15 00:05:521369 if (pending_entry_index_ == -1) {
1370 // In this case, we have a pending entry for a load of a new URL but Blink
1371 // didn't do a new navigation (params.did_create_new_entry). First check
1372 // to make sure Blink didn't treat a new cross-process navigation as
1373 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161374 // we must treat it as NEW rather than the converted reload case below,
1375 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521376 if (!GetLastCommittedEntry() ||
1377 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161378 trace_return.set_return_reason("new pending, new entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491379 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521380 }
1381
1382 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161383 // We will create a pending entry, but NavigateWithoutEntry will convert
1384 // it to a reload since it's the same page and not create a new entry for
1385 // it. (The user doesn't want to have a new back/forward entry when they
1386 // do this.) Therefore we want to just ignore the pending entry and go
1387 // back to where we were (the "existing entry").
1388 trace_return.set_return_reason("new pending, existing (same) entry");
1389 return NAVIGATION_TYPE_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521390 }
avi7c6f35e2015-05-08 17:52:381391 }
1392
creis26d22632017-04-21 20:23:561393 // Everything below here is assumed to be an existing entry, but if there is
1394 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511395 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491396 trace_return.set_return_reason("no last committed, new entry");
1397 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511398 }
creis26d22632017-04-21 20:23:561399
avi7c6f35e2015-05-08 17:52:381400 if (params.intended_as_new_entry) {
1401 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491402 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1403 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161404 trace_return.set_return_reason("intended as new entry, existing entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491405 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381406 }
1407
1408 if (params.url_is_unreachable && failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101409 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381410 // If the renderer was going to a new pending entry that got cleared because
1411 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491412 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381413 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511414 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491415 "unreachable, matching pending, existing entry");
1416 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381417 }
1418
Charlie Reisc0f17d2d2021-01-12 18:52:491419 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101420 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511421 trace_return.traced_value()->SetInteger("existing_entry_index",
1422 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381423 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441424 // The renderer has committed a navigation to an entry that no longer
1425 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491426 trace_return.set_return_reason("existing entry -1, new entry");
1427 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381428 }
1429
avi7c6f35e2015-05-08 17:52:381430 // Since we weeded out "new" navigations above, we know this is an existing
1431 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491432 trace_return.set_return_reason("default return, existing entry");
1433 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381434}
1435
Charlie Reisc0f17d2d2021-01-12 18:52:491436void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321437 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071438 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361439 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441440 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471441 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451442 NavigationRequest* request) {
dcheng9bfa5162016-04-09 01:00:571443 std::unique_ptr<NavigationEntryImpl> new_entry;
creisf49dfc92016-07-26 17:05:181444 bool update_virtual_url = false;
1445
Lukasz Anforowicz435bcb582019-07-12 20:50:061446 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451447 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061448
creisf49dfc92016-07-26 17:05:181449 // First check if this is an in-page navigation. If so, clone the current
1450 // entry instead of looking at the pending entry, because the pending entry
1451 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361452 if (is_same_document && GetLastCommittedEntry()) {
Patrick Monette50e8bd82019-06-13 22:40:451453 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481454 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
creisf49dfc92016-07-26 17:05:181455 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Nasko Oskov03912102019-01-11 00:21:321456 params.url, (params.url_is_unreachable) ? nullptr : &params.origin,
arthursonzogni73fe3212020-11-17 13:24:071457 Referrer(*params.referrer), initiator_origin, params.redirects,
1458 params.page_state, params.method, params.post_id,
1459 nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391460 nullptr /* web_bundle_navigation_info */,
1461 // We will set the document policies later in this function.
1462 nullptr /* document_policies */);
Charles Reisf44482022017-10-13 21:15:031463
creisf49dfc92016-07-26 17:05:181464 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Carlos Caballero40b0efd2021-01-26 11:55:001465 frame_entry, true, rfh->frame_tree_node(), frame_tree_.root());
jama78746e2017-02-22 17:21:571466 if (new_entry->GetURL().GetOrigin() != params.url.GetOrigin()) {
1467 // TODO(jam): we had one report of this with a URL that was redirecting to
1468 // only tildes. Until we understand that better, don't copy the cert in
1469 // this case.
1470 new_entry->GetSSL() = SSLStatus();
John Abd-El-Malek9cd697e2017-07-12 21:53:141471
John Abd-El-Malek380d3c5922017-09-08 00:20:311472 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451473 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141474 UMA_HISTOGRAM_BOOLEAN(
1475 "Navigation.SecureSchemeHasSSLStatus.NewPageInPageOriginMismatch",
1476 !!new_entry->GetSSL().certificate);
1477 }
jama78746e2017-02-22 17:21:571478 }
creisf49dfc92016-07-26 17:05:181479
Patrick Monette50e8bd82019-06-13 22:40:451480 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1481 // that |frame_entry| should now have a reference count of exactly 2: one
1482 // due to the local variable |frame_entry|, and another due to |new_entry|
1483 // also retaining one. This should never fail, because it's the main frame.
1484 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181485
1486 update_virtual_url = new_entry->update_virtual_url_with_url();
John Abd-El-Malek9cd697e2017-07-12 21:53:141487
John Abd-El-Malek380d3c5922017-09-08 00:20:311488 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451489 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141490 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus.NewPageInPage",
1491 !!new_entry->GetSSL().certificate);
1492 }
creisf49dfc92016-07-26 17:05:181493 }
1494
Charlie Reisc0f17d2d2021-01-12 18:52:491495 // Only make a copy of the pending entry if it is appropriate for the new
1496 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451497 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041498 // 1. The SiteInstance hasn't been assigned to something else.
1499 // 2. The pending entry was intended as a new entry, rather than being a
1500 // history navigation that was interrupted by an unrelated,
1501 // renderer-initiated navigation.
1502 // TODO(csharrison): Investigate whether we can remove some of the coarser
1503 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451504 if (!new_entry && PendingEntryMatchesRequest(request) &&
1505 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341506 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431507 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351508 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431509
[email protected]f1eb87a2011-05-06 17:49:411510 update_virtual_url = new_entry->update_virtual_url_with_url();
Camille Lamy62b826012019-02-26 09:15:471511 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451512 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141513
John Abd-El-Malek380d3c5922017-09-08 00:20:311514 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451515 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141516 UMA_HISTOGRAM_BOOLEAN(
1517 "Navigation.SecureSchemeHasSSLStatus.NewPagePendingEntryMatches",
1518 !!new_entry->GetSSL().certificate);
1519 }
creisf49dfc92016-07-26 17:05:181520 }
1521
Charlie Reisc0f17d2d2021-01-12 18:52:491522 // For cross-document commits with no matching pending entry, create a new
1523 // entry.
creisf49dfc92016-07-26 17:05:181524 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061525 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071526 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1527 initiator_origin,
Lukasz Anforowicz435bcb582019-07-12 20:50:061528 base::string16(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451529 params.transition, request->IsRendererInitiated(),
Lukasz Anforowicz435bcb582019-07-12 20:50:061530 nullptr); // blob_url_loader_factory
[email protected]f8f93eb2012-09-25 03:06:241531
1532 // Find out whether the new entry needs to update its virtual URL on URL
1533 // change and set up the entry accordingly. This is needed to correctly
1534 // update the virtual URL when replaceState is called after a pushState.
1535 GURL url = params.url;
1536 bool needs_update = false;
[email protected]47752982014-07-29 08:01:431537 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1538 &url, browser_context_, &needs_update);
[email protected]f8f93eb2012-09-25 03:06:241539 new_entry->set_update_virtual_url_with_url(needs_update);
1540
Charlie Reisc0f17d2d2021-01-12 18:52:491541 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411542 // update the virtual URL based on the new URL. For example, this is needed
1543 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1544 // the URL.
[email protected]f8f93eb2012-09-25 03:06:241545 update_virtual_url = needs_update;
Camille Lamy62b826012019-02-26 09:15:471546 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451547 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141548
John Abd-El-Malek380d3c5922017-09-08 00:20:311549 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451550 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141551 UMA_HISTOGRAM_BOOLEAN(
1552 "Navigation.SecureSchemeHasSSLStatus.NewPageNoMatchingEntry",
1553 !!new_entry->GetSSL().certificate);
1554 }
[email protected]e9ba4472008-09-14 15:42:431555 }
1556
wjmaclean7431bb22015-02-19 14:53:431557 // Don't use the page type from the pending entry. Some interstitial page
1558 // may have set the type to interstitial. Once we commit, however, the page
1559 // type must always be normal or error.
1560 new_entry->set_page_type(params.url_is_unreachable ? PAGE_TYPE_ERROR
1561 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041562 new_entry->SetURL(params.url);
[email protected]f1eb87a2011-05-06 17:49:411563 if (update_virtual_url)
avi25764702015-06-23 15:43:371564 UpdateVirtualURLToURL(new_entry.get(), params.url);
arthursonzogni73fe3212020-11-17 13:24:071565 new_entry->SetReferrer(Referrer(*params.referrer));
[email protected]022af742011-12-28 18:37:251566 new_entry->SetTransitionType(params.transition);
[email protected]b6583592012-01-25 19:52:331567 new_entry->set_site_instance(
[email protected]27dd82fd2014-03-03 22:11:431568 static_cast<SiteInstanceImpl*>(rfh->GetSiteInstance()));
[email protected]7c16976c2012-08-04 02:38:231569 new_entry->SetOriginalRequestURL(params.original_request_url);
Rakina Zata Amnib597d632020-12-01 00:56:001570
1571 if (!is_same_document) {
1572 DCHECK_EQ(request->IsOverridingUserAgent() && !rfh->GetParent(),
1573 params.is_overriding_user_agent);
1574 } else {
1575 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
1576 }
[email protected]bf70edce2012-06-20 22:32:221577 new_entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
[email protected]e9ba4472008-09-14 15:42:431578
creis8b5cd4c2015-06-19 00:11:081579 // Update the FrameNavigationEntry for new main frame commits.
1580 FrameNavigationEntry* frame_entry =
1581 new_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisd2a509f2017-09-27 23:47:481582 frame_entry->set_frame_unique_name(rfh->frame_tree_node()->unique_name());
creis8b5cd4c2015-06-19 00:11:081583 frame_entry->set_item_sequence_number(params.item_sequence_number);
1584 frame_entry->set_document_sequence_number(params.document_sequence_number);
Charles Reisf44482022017-10-13 21:15:031585 frame_entry->set_redirect_chain(params.redirects);
1586 frame_entry->SetPageState(params.page_state);
clamy432acb22016-04-15 19:41:431587 frame_entry->set_method(params.method);
1588 frame_entry->set_post_id(params.post_id);
Antonio Sartori78a749f2020-11-30 12:03:391589 frame_entry->set_document_policies(
1590 ComputeDocumentPoliciesForFrameEntry(rfh, is_same_document, request));
1591
Nasko Oskov03912102019-01-11 00:21:321592 if (!params.url_is_unreachable)
1593 frame_entry->set_committed_origin(params.origin);
Tsuyoshi Horob7b033e2020-03-09 15:23:311594 if (request->web_bundle_navigation_info()) {
1595 frame_entry->set_web_bundle_navigation_info(
1596 request->web_bundle_navigation_info()->Clone());
1597 }
creis8b5cd4c2015-06-19 00:11:081598
eugenebut604866f2017-05-10 21:35:361599 // history.pushState() is classified as a navigation to a new page, but sets
1600 // is_same_document to true. In this case, we already have the title and
creisf49dfc92016-07-26 17:05:181601 // favicon available, so set them immediately.
eugenebut604866f2017-05-10 21:35:361602 if (is_same_document && GetLastCommittedEntry()) {
[email protected]ff64b3e2014-05-31 04:07:331603 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
[email protected]3a868f212014-08-09 10:41:191604 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1605 }
[email protected]ff64b3e2014-05-31 04:07:331606
[email protected]60d6cca2013-04-30 08:47:131607 DCHECK(!params.history_list_was_cleared || !replace_entry);
1608 // The browser requested to clear the session history when it initiated the
1609 // navigation. Now we know that the renderer has updated its state accordingly
1610 // and it is safe to also clear the browser side history.
1611 if (params.history_list_was_cleared) {
arthursonzogni69a6a1b2019-09-17 09:23:001612 DiscardNonCommittedEntries();
[email protected]60d6cca2013-04-30 08:47:131613 entries_.clear();
1614 last_committed_entry_index_ = -1;
1615 }
1616
Nasko Oskovaee2f862018-06-15 00:05:521617 // If this is a new navigation with replacement and there is a
1618 // pending_entry_ which matches the navigation reported by the renderer
1619 // process, then it should be the one replaced, so update the
1620 // last_committed_entry_index_ to use it.
1621 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101622 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521623 last_committed_entry_index_ = pending_entry_index_;
1624 }
1625
Alexander Timine3ec4192020-04-20 16:39:401626 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411627 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401628 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051629
Carlos IL42b416592019-10-07 23:10:361630 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
1631 !request->post_commit_error_page_html().empty());
[email protected]e9ba4472008-09-14 15:42:431632}
1633
Charlie Reisc0f17d2d2021-01-12 18:52:491634void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321635 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071636 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361637 bool is_same_document,
jam48cea9082017-02-15 06:13:291638 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451639 NavigationRequest* request,
Peter Boströmd7592132019-01-30 04:50:311640 bool keep_pending_entry) {
creis26d22632017-04-21 20:23:561641 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1642 << "that a last committed entry exists.";
1643
[email protected]e9ba4472008-09-14 15:42:431644 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001645 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431646
Charlie Reis7e2cb6d2021-01-26 01:27:161647 NavigationEntryImpl* entry = nullptr;
avicbdc4c12015-07-01 16:07:111648 if (params.intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:161649 // We're guaranteed to have a last committed entry if intended_as_new_entry
1650 // is true.
avicbdc4c12015-07-01 16:07:111651 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:161652 DCHECK(entry);
1653
1654 // If the NavigationRequest matches a new pending entry and is classified as
1655 // EXISTING_ENTRY, then it is a navigation to the same URL that was
1656 // converted to a reload, such as a user pressing enter in the omnibox.
1657 if (pending_entry_ && pending_entry_index_ == -1 &&
1658 pending_entry_->GetUniqueID() ==
1659 request->commit_params().nav_entry_id) {
1660 // Note: The pending entry will usually have a real ReloadType here, but
1661 // it can still be ReloadType::NONE in cases that
1662 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
1663
1664 // If we classified this correctly, the SiteInstance should not have
1665 // changed.
1666 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
1667
1668 // For converted reloads, we assign the entry's unique ID to be that of
1669 // the new one. Since this is always the result of a user action, we want
1670 // to dismiss infobars, etc. like a regular user-initiated navigation.
1671 entry->set_unique_id(pending_entry_->GetUniqueID());
1672
1673 // The extra headers may have changed due to reloading with different
1674 // headers.
1675 entry->set_extra_headers(pending_entry_->extra_headers());
1676 }
1677 // Otherwise, this was intended as a new entry but the pending entry was
1678 // lost in the meantime and no new entry was created. We are stuck at the
1679 // last committed entry.
1680
1681 // Even if this is a converted reload from pressing enter in the omnibox,
1682 // the server could redirect, requiring an update to the SSL status. If this
1683 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451684 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:161685 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:471686 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451687 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:161688 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141689
Charlie Reis7e2cb6d2021-01-26 01:27:161690 if (params.url.SchemeIs(url::kHttpsScheme) &&
Mohamed Abdelhalim833de902019-09-16 17:41:451691 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141692 bool has_cert = !!entry->GetSSL().certificate;
1693 if (is_same_document) {
1694 UMA_HISTOGRAM_BOOLEAN(
1695 "Navigation.SecureSchemeHasSSLStatus."
1696 "ExistingPageSameDocumentIntendedAsNew",
1697 has_cert);
1698 } else {
1699 UMA_HISTOGRAM_BOOLEAN(
1700 "Navigation.SecureSchemeHasSSLStatus."
1701 "ExistingPageDifferentDocumentIntendedAsNew",
1702 has_cert);
1703 }
1704 }
Rakina Zata Amnif6950d552020-11-24 03:26:101705 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:111706 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:101707 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b90ce2016-09-01 16:58:161708
eugenebut604866f2017-05-10 21:35:361709 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451710 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361711 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1712 // this was a restored same document navigation entry, then it won't have
1713 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1714 // navigated it.
jam48cea9082017-02-15 06:13:291715 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
1716 if (entry->GetURL().GetOrigin() == last_entry->GetURL().GetOrigin() &&
1717 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1718 was_restored) {
1719 entry->GetSSL() = last_entry->GetSSL();
1720 }
1721 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451722 // In rapid back/forward navigations |request| sometimes won't have a cert
1723 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191724 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451725 if (request->GetSSLInfo().has_value() &&
1726 request->GetSSLInfo()->is_valid()) {
1727 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
1728 } else if (entry->GetURL().GetOrigin() != request->GetURL().GetOrigin()) {
John Abd-El-Malek3f247082017-12-07 19:02:191729 entry->GetSSL() = SSLStatus();
1730 }
jam48cea9082017-02-15 06:13:291731 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141732
John Abd-El-Malek380d3c5922017-09-08 00:20:311733 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451734 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141735 bool has_cert = !!entry->GetSSL().certificate;
1736 if (is_same_document && was_restored) {
1737 UMA_HISTOGRAM_BOOLEAN(
1738 "Navigation.SecureSchemeHasSSLStatus."
1739 "ExistingPageSameDocumentRestoredBrowserInitiated",
1740 has_cert);
1741 } else if (is_same_document && !was_restored) {
1742 UMA_HISTOGRAM_BOOLEAN(
1743 "Navigation.SecureSchemeHasSSLStatus."
1744 "ExistingPageSameDocumentBrowserInitiated",
1745 has_cert);
1746 } else if (!is_same_document && was_restored) {
1747 UMA_HISTOGRAM_BOOLEAN(
1748 "Navigation.SecureSchemeHasSSLStatus."
1749 "ExistingPageRestoredBrowserInitiated",
1750 has_cert);
1751 } else {
1752 UMA_HISTOGRAM_BOOLEAN(
1753 "Navigation.SecureSchemeHasSSLStatus.ExistingPageBrowserInitiated",
1754 has_cert);
1755 }
1756 }
avicbdc4c12015-07-01 16:07:111757 } else {
1758 // This is renderer-initiated. The only kinds of renderer-initated
Charlie Reisc0f17d2d2021-01-12 18:52:491759 // navigations that are EXISTING_ENTRY are reloads and history.replaceState,
avicbdc4c12015-07-01 16:07:111760 // which land us at the last committed entry.
1761 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101762
Mikel Astizba9cf2fd2017-12-17 10:38:101763 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1764 // to avoid misleading interpretations (e.g. URLs paired with
1765 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1766 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1767 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1768
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571769 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:101770
eugenebut604866f2017-05-10 21:35:361771 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451772 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361773 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471774 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451775 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141776
John Abd-El-Malek380d3c5922017-09-08 00:20:311777 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451778 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141779 bool has_cert = !!entry->GetSSL().certificate;
1780 if (is_same_document) {
1781 UMA_HISTOGRAM_BOOLEAN(
1782 "Navigation.SecureSchemeHasSSLStatus."
1783 "ExistingPageSameDocumentRendererInitiated",
1784 has_cert);
1785 } else {
1786 UMA_HISTOGRAM_BOOLEAN(
1787 "Navigation.SecureSchemeHasSSLStatus."
1788 "ExistingPageDifferentDocumentRendererInitiated",
1789 has_cert);
1790 }
1791 }
avicbdc4c12015-07-01 16:07:111792 }
1793 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:431794
[email protected]5ccd4dc2012-10-24 02:28:141795 // The URL may have changed due to redirects.
wjmaclean7431bb22015-02-19 14:53:431796 entry->set_page_type(params.url_is_unreachable ? PAGE_TYPE_ERROR
1797 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041798 entry->SetURL(params.url);
arthursonzogni73fe3212020-11-17 13:24:071799 entry->SetReferrer(Referrer(*params.referrer));
[email protected]38178a42009-12-17 18:58:321800 if (entry->update_virtual_url_with_url())
1801 UpdateVirtualURLToURL(entry, params.url);
[email protected]5ccd4dc2012-10-24 02:28:141802
jam015ba062017-01-06 21:17:001803 // The site instance will normally be the same except
1804 // 1) session restore, when no site instance will be assigned or
1805 // 2) redirect, when the site instance is reset.
Lei Zhang96031532019-10-10 19:05:471806 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
naskoaf182192016-08-11 02:12:011807 entry->site_instance() == rfh->GetSiteInstance());
clamy432acb22016-04-15 19:41:431808
naskoaf182192016-08-11 02:12:011809 // Update the existing FrameNavigationEntry to ensure all of its members
1810 // reflect the parameters coming from the renderer process.
Lukasz Anforowicz435bcb582019-07-12 20:50:061811 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451812 request->common_params().initiator_origin;
naskoaf182192016-08-11 02:12:011813 entry->AddOrUpdateFrameEntry(
1814 rfh->frame_tree_node(), params.item_sequence_number,
1815 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
arthursonzogni73fe3212020-11-17 13:24:071816 params.url, GetCommittedOriginForFrameEntry(params),
1817 Referrer(*params.referrer), initiator_origin, params.redirects,
1818 params.page_state, params.method, params.post_id,
1819 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031820 request->web_bundle_navigation_info()
1821 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391822 : nullptr,
1823 ComputeDocumentPoliciesForFrameEntry(rfh, is_same_document, request));
creis22a7b4c2016-04-28 07:20:301824
[email protected]5ccd4dc2012-10-24 02:28:141825 // The redirected to page should not inherit the favicon from the previous
1826 // page.
eugenebut604866f2017-05-10 21:35:361827 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:481828 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:141829
Peter Boströmd7592132019-01-30 04:50:311830 // We should also usually discard the pending entry if it corresponds to a
1831 // different navigation, since that one is now likely canceled. In rare
1832 // cases, we leave the pending entry for another navigation in place when we
1833 // know it is still ongoing, to avoid a flicker in the omnibox (see
1834 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:431835 //
1836 // Note that we need to use the "internal" version since we don't want to
1837 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:311838 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001839 DiscardNonCommittedEntries();
[email protected]40bcc302009-03-02 20:50:391840
Carlos IL4dea8902020-05-26 15:14:291841 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:111842 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:431843}
1844
[email protected]d202a7c2012-01-04 07:53:471845void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:321846 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071847 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361848 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:471849 bool replace_entry,
1850 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451851 NavigationRequest* request) {
avi25f5f9e2015-07-17 20:08:261852 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1853 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
[email protected]09b8f82f2009-06-16 20:22:111854
[email protected]e9ba4472008-09-14 15:42:431855 // Manual subframe navigations just get the current entry cloned so the user
1856 // can go back or forward to it. The actual subframe information will be
1857 // stored in the page state for each of those entries. This happens out of
1858 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:091859 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1860 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:381861
Mikel Astizba9cf2fd2017-12-17 10:38:101862 // The DCHECK below documents the fact that we don't know of any situation
1863 // where |replace_entry| is true for subframe navigations. This simplifies
1864 // reasoning about the replacement struct for subframes (see
1865 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
1866 DCHECK(!replace_entry);
1867
Patrick Monette50e8bd82019-06-13 22:40:451868 // This FrameNavigationEntry might not end up being used in the
1869 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Lukasz Anforowicz435bcb582019-07-12 20:50:061870 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451871 request->common_params().initiator_origin;
Patrick Monette50e8bd82019-06-13 22:40:451872 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481873 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
creisce0ef3572017-01-26 17:53:081874 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Nasko Oskov03912102019-01-11 00:21:321875 params.url, (params.url_is_unreachable) ? nullptr : &params.origin,
arthursonzogni73fe3212020-11-17 13:24:071876 Referrer(*params.referrer), initiator_origin, params.redirects,
1877 params.page_state, params.method, params.post_id,
1878 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031879 request->web_bundle_navigation_info()
1880 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391881 : nullptr,
1882 ComputeDocumentPoliciesForFrameEntry(rfh, is_same_document, request));
Charles Reisf44482022017-10-13 21:15:031883
creisce0ef3572017-01-26 17:53:081884 std::unique_ptr<NavigationEntryImpl> new_entry =
1885 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:451886 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:001887 frame_tree_.root());
creise062d542015-08-25 02:01:551888
Alexander Timine3ec4192020-04-20 16:39:401889 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411890 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401891 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:471892
creisce0ef3572017-01-26 17:53:081893 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:451894 // to replace, which can happen due to a unique name change. See
1895 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
1896 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:381897
Carlos IL42b416592019-10-07 23:10:361898 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false);
[email protected]e9ba4472008-09-14 15:42:431899}
1900
[email protected]d202a7c2012-01-04 07:53:471901bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:321902 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071903 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:391904 bool is_same_document,
Mohamed Abdelhalim833de902019-09-16 17:41:451905 NavigationRequest* request) {
avi9f07a0c2015-02-18 22:51:291906 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1907 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
1908
[email protected]e9ba4472008-09-14 15:42:431909 // We're guaranteed to have a previously committed entry, and we now need to
1910 // handle navigation inside of a subframe in it without creating a new entry.
1911 DCHECK(GetLastCommittedEntry());
1912
creis913c63ce2016-07-16 19:52:521913 // For newly created subframes, we don't need to send a commit notification.
1914 // This is only necessary for history navigations in subframes.
1915 bool send_commit_notification = false;
1916
Rakina Zata Amnif6950d552020-11-24 03:26:101917 // If |nav_entry_id| is non-zero and matches an existing entry, this
1918 // is a history navigation. Update the last committed index accordingly. If
1919 // we don't recognize the |nav_entry_id|, it might be a recently
1920 // pruned entry. We'll handle it below.
1921 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
1922 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:471923 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:061924 // Make sure that a subframe commit isn't changing the main frame's
1925 // origin. Otherwise the renderer process may be confused, leading to a
1926 // URL spoof. We can't check the path since that may change
1927 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:571928 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
1929 // about:blank, file, and unique origins are more subtle to get right.
Eugene But9fb98d12018-05-22 18:28:331930 // We'll abstract out the relevant checks from IsURLSameDocumentNavigation
1931 // and share them here. See https://crbug.com/618104.
creis37988b92016-06-10 18:03:571932 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
1933 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
1934 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
1935 dest_top_url.SchemeIsHTTPOrHTTPS() &&
1936 current_top_url.GetOrigin() != dest_top_url.GetOrigin()) {
creisfb6eeb62016-05-10 19:01:511937 bad_message::ReceivedBadMessage(rfh->GetProcess(),
1938 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:061939 }
creis3cdc3b02015-05-29 23:00:471940
creis913c63ce2016-07-16 19:52:521941 // We only need to discard the pending entry in this history navigation
1942 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:061943 last_committed_entry_index_ = entry_index;
arthursonzogni69a6a1b2019-09-17 09:23:001944 DiscardNonCommittedEntries();
creis913c63ce2016-07-16 19:52:521945
1946 // History navigations should send a commit notification.
1947 send_commit_notification = true;
avi98405c22015-05-21 20:47:061948 }
[email protected]e9ba4472008-09-14 15:42:431949 }
[email protected]f233e4232013-02-23 00:55:141950
creisce0ef3572017-01-26 17:53:081951 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
1952 // it may be a "history auto" case where we update an existing one.
1953 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Lukasz Anforowicz435bcb582019-07-12 20:50:061954 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451955 request->common_params().initiator_origin;
creisce0ef3572017-01-26 17:53:081956 last_committed->AddOrUpdateFrameEntry(
1957 rfh->frame_tree_node(), params.item_sequence_number,
1958 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
arthursonzogni73fe3212020-11-17 13:24:071959 params.url, GetCommittedOriginForFrameEntry(params),
1960 Referrer(*params.referrer), initiator_origin, params.redirects,
1961 params.page_state, params.method, params.post_id,
1962 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031963 request->web_bundle_navigation_info()
1964 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391965 : nullptr,
1966 ComputeDocumentPoliciesForFrameEntry(rfh, is_same_document, request));
creis625a0c7d2015-03-24 23:17:121967
creis913c63ce2016-07-16 19:52:521968 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:431969}
1970
[email protected]d202a7c2012-01-04 07:53:471971int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:231972 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:031973 for (size_t i = 0; i < entries_.size(); ++i) {
1974 if (entries_[i].get() == entry)
1975 return i;
1976 }
1977 return -1;
[email protected]765b35502008-08-21 00:51:201978}
1979
Eugene But7cc259d2017-10-09 23:52:491980// There are two general cases where a navigation is "same-document":
avidb7d1d22015-06-08 21:21:501981// 1. A fragment navigation, in which the url is kept the same except for the
1982// reference fragment.
1983// 2. A history API navigation (pushState and replaceState). This case is
Eugene But7cc259d2017-10-09 23:52:491984// always same-document, but the urls are not guaranteed to match excluding
1985// the fragment. The relevant spec allows pushState/replaceState to any URL
1986// on the same origin.
avidb7d1d22015-06-08 21:21:501987// However, due to reloads, even identical urls are *not* guaranteed to be
Eugene But7cc259d2017-10-09 23:52:491988// same-document navigations, we have to trust the renderer almost entirely.
avidb7d1d22015-06-08 21:21:501989// The one thing we do know is that cross-origin navigations will *never* be
Eugene But7cc259d2017-10-09 23:52:491990// same-document. Therefore, trust the renderer if the URLs are on the same
1991// origin, and assume the renderer is malicious if a cross-origin navigation
1992// claims to be same-document.
creisf164daa2016-06-07 00:17:051993//
1994// TODO(creis): Clean up and simplify the about:blank and origin checks below,
1995// which are likely redundant with each other. Be careful about data URLs vs
1996// about:blank, both of which are unique origins and thus not considered equal.
Eugene But7cc259d2017-10-09 23:52:491997bool NavigationControllerImpl::IsURLSameDocumentNavigation(
[email protected]b9d4dfdc2013-08-08 00:25:121998 const GURL& url,
creisf164daa2016-06-07 00:17:051999 const url::Origin& origin,
Eugene But7cc259d2017-10-09 23:52:492000 bool renderer_says_same_document,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572001 RenderFrameHost* rfh) {
creis225a7432016-06-03 22:56:272002 RenderFrameHostImpl* rfhi = static_cast<RenderFrameHostImpl*>(rfh);
avidb7d1d22015-06-08 21:21:502003 GURL last_committed_url;
2004 if (rfh->GetParent()) {
creis225a7432016-06-03 22:56:272005 // Use the FrameTreeNode's current_url and not rfh->GetLastCommittedURL(),
2006 // which might be empty in a new RenderFrameHost after a process swap.
2007 // Here, we care about the last committed URL in the FrameTreeNode,
2008 // regardless of which process it is in.
2009 last_committed_url = rfhi->frame_tree_node()->current_url();
avidb7d1d22015-06-08 21:21:502010 } else {
2011 NavigationEntry* last_committed = GetLastCommittedEntry();
2012 // There must be a last-committed entry to compare URLs to. TODO(avi): When
2013 // might Blink say that a navigation is in-page yet there be no last-
2014 // committed entry?
2015 if (!last_committed)
2016 return false;
2017 last_committed_url = last_committed->GetURL();
2018 }
2019
Gyuyoung Kim1ac4ca782020-09-11 03:32:512020 auto prefs = rfhi->GetOrCreateWebPreferences();
creis225a7432016-06-03 22:56:272021 const url::Origin& committed_origin =
2022 rfhi->frame_tree_node()->current_origin();
avidb7d1d22015-06-08 21:21:502023 bool is_same_origin = last_committed_url.is_empty() ||
2024 // TODO(japhet): We should only permit navigations
2025 // originating from about:blank to be in-page if the
2026 // about:blank is the first document that frame loaded.
2027 // We don't have sufficient information to identify
2028 // that case at the moment, so always allow about:blank
2029 // for now.
csharrisona3bd0b32016-10-19 18:40:482030 last_committed_url == url::kAboutBlankURL ||
avidb7d1d22015-06-08 21:21:502031 last_committed_url.GetOrigin() == url.GetOrigin() ||
creisf164daa2016-06-07 00:17:052032 committed_origin == origin ||
avidb7d1d22015-06-08 21:21:502033 !prefs.web_security_enabled ||
2034 (prefs.allow_universal_access_from_file_urls &&
creisdc004e02015-11-20 21:28:472035 committed_origin.scheme() == url::kFileScheme);
Eugene But7cc259d2017-10-09 23:52:492036 if (!is_same_origin && renderer_says_same_document) {
avidb7d1d22015-06-08 21:21:502037 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2038 bad_message::NC_IN_PAGE_NAVIGATION);
2039 }
Eugene But7cc259d2017-10-09 23:52:492040 return is_same_origin && renderer_says_same_document;
[email protected]e9ba4472008-09-14 15:42:432041}
2042
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572043void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242044 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572045 NavigationControllerImpl* source =
2046 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572047 // Verify that we look new.
Lei Zhang96031532019-10-10 19:05:472048 DCHECK_EQ(0, GetEntryCount());
2049 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:572050
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572051 if (source->GetEntryCount() == 0)
[email protected]ce3fa3c2009-04-20 19:55:572052 return; // Nothing new to do.
2053
Francois Dorayeaace782017-06-21 16:37:242054 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442055 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572056 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572057
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572058 for (auto it = source->session_storage_namespace_map_.begin();
2059 it != source->session_storage_namespace_map_.end(); ++it) {
[email protected]fdac6ade2013-07-20 01:06:302060 SessionStorageNamespaceImpl* source_namespace =
2061 static_cast<SessionStorageNamespaceImpl*>(it->second.get());
2062 session_storage_namespace_map_[it->first] = source_namespace->Clone();
2063 }
[email protected]4e6419c2010-01-15 04:50:342064
Lukasz Anforowicz0de0f452020-12-02 19:57:152065 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572066}
2067
Aran Gilman37d11632019-10-08 23:07:152068void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2069 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162070 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012071 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162072
[email protected]d202a7c2012-01-04 07:53:472073 NavigationControllerImpl* source =
2074 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252075
avi2b177592014-12-10 02:08:022076 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012077 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252078
[email protected]474f8512013-05-31 22:31:162079 // We now have one entry, possibly with a new pending entry. Ensure that
2080 // adding the entries from source won't put us over the limit.
2081 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572082 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572083 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252084
Carlos IL4dea8902020-05-26 15:14:292085 // Insert the entries from source. Ignore any pending entry, since it has not
2086 // committed in source.
[email protected]474f8512013-05-31 22:31:162087 int max_source_index = source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:252088 if (max_source_index == -1)
[email protected]a26023822011-12-29 00:23:552089 max_source_index = source->GetEntryCount();
[email protected]e1cd5452010-08-26 18:03:252090 else
2091 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572092
2093 // Ignore the source's current entry if merging with replacement.
2094 // TODO(davidben): This should preserve entries forward of the current
2095 // too. http://crbug.com/317872
2096 if (replace_entry && max_source_index > 0)
2097 max_source_index--;
2098
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572099 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252100
2101 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552102 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142103
avi2b177592014-12-10 02:08:022104 delegate_->SetHistoryOffsetAndLength(last_committed_entry_index_,
2105 GetEntryCount());
[email protected]e1cd5452010-08-26 18:03:252106}
2107
[email protected]79368982013-11-13 01:11:012108bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162109 // If there is no last committed entry, we cannot prune. Even if there is a
2110 // pending entry, it may not commit, leaving this WebContents blank, despite
2111 // possibly giving it new entries via CopyStateFromAndPrune.
2112 if (last_committed_entry_index_ == -1)
2113 return false;
[email protected]9350602e2013-02-26 23:27:442114
[email protected]474f8512013-05-31 22:31:162115 // We cannot prune if there is a pending entry at an existing entry index.
2116 // It may not commit, so we have to keep the last committed entry, and thus
2117 // there is no sensible place to keep the pending entry. It is ok to have
2118 // a new pending entry, which can optionally commit as a new navigation.
2119 if (pending_entry_index_ != -1)
2120 return false;
2121
[email protected]474f8512013-05-31 22:31:162122 return true;
2123}
2124
[email protected]79368982013-11-13 01:11:012125void NavigationControllerImpl::PruneAllButLastCommitted() {
2126 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162127
avi2b177592014-12-10 02:08:022128 DCHECK_EQ(0, last_committed_entry_index_);
2129 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442130
avi2b177592014-12-10 02:08:022131 delegate_->SetHistoryOffsetAndLength(last_committed_entry_index_,
2132 GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442133}
2134
[email protected]79368982013-11-13 01:11:012135void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162136 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012137 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262138
[email protected]474f8512013-05-31 22:31:162139 // Erase all entries but the last committed entry. There may still be a
2140 // new pending entry after this.
2141 entries_.erase(entries_.begin(),
2142 entries_.begin() + last_committed_entry_index_);
2143 entries_.erase(entries_.begin() + 1, entries_.end());
2144 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262145}
2146
Christian Dullweber1af31e62018-02-22 11:49:482147void NavigationControllerImpl::DeleteNavigationEntries(
2148 const DeletionPredicate& deletionPredicate) {
2149 // It is up to callers to check the invariants before calling this.
2150 CHECK(CanPruneAllButLastCommitted());
2151 std::vector<int> delete_indices;
2152 for (size_t i = 0; i < entries_.size(); i++) {
2153 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572154 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482155 delete_indices.push_back(i);
2156 }
2157 }
2158 if (delete_indices.empty())
2159 return;
2160
2161 if (delete_indices.size() == GetEntryCount() - 1U) {
2162 PruneAllButLastCommitted();
2163 } else {
2164 // Do the deletion in reverse to preserve indices.
2165 for (auto it = delete_indices.rbegin(); it != delete_indices.rend(); ++it) {
2166 RemoveEntryAtIndex(*it);
2167 }
2168 delegate_->SetHistoryOffsetAndLength(last_committed_entry_index_,
2169 GetEntryCount());
2170 }
2171 delegate()->NotifyNavigationEntriesDeleted();
2172}
2173
Shivani Sharma883f5f32019-02-12 18:20:012174bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2175 auto* entry = GetEntryAtIndex(index);
2176 return entry && entry->should_skip_on_back_forward_ui();
2177}
2178
Carlos Caballero35ce710c2019-09-19 10:59:452179BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2180 return back_forward_cache_;
2181}
2182
clamy987a3752018-05-03 17:36:262183void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2184 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2185 // progress, since this will cause a use-after-free. (We only allow this
2186 // when the tab is being destroyed for shutdown, since it won't return to
2187 // NavigateToEntry in that case.) http://crbug.com/347742.
2188 CHECK(!in_navigate_to_pending_entry_ || delegate_->IsBeingDestroyed());
2189
2190 if (was_failure && pending_entry_) {
2191 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2192 } else {
2193 failed_pending_entry_id_ = 0;
2194 }
2195
2196 if (pending_entry_) {
2197 if (pending_entry_index_ == -1)
2198 delete pending_entry_;
2199 pending_entry_index_ = -1;
2200 pending_entry_ = nullptr;
2201 }
arthursonzogni66f711c2019-10-08 14:40:362202
2203 // Ensure any refs to the current pending entry are ignored if they get
2204 // deleted, by clearing the set of known refs. All future pending entries will
2205 // only be affected by new refs.
2206 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262207}
2208
2209void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2210 if (pending_entry_)
2211 pending_entry_->set_ssl_error(error);
2212}
2213
Camille Lamy5193caa2018-10-12 11:59:422214#if defined(OS_ANDROID)
2215// static
2216bool NavigationControllerImpl::ValidateDataURLAsString(
2217 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2218 if (!data_url_as_string)
2219 return false;
2220
2221 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2222 return false;
2223
2224 // The number of characters that is enough for validating a data: URI.
2225 // From the GURL's POV, the only important part here is scheme, it doesn't
2226 // check the actual content. Thus we can take only the prefix of the url, to
2227 // avoid unneeded copying of a potentially long string.
2228 const size_t kDataUriPrefixMaxLen = 64;
2229 GURL data_url(
2230 std::string(data_url_as_string->front_as<char>(),
2231 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2232 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2233 return false;
2234
2235 return true;
2236}
2237#endif
2238
Shivani Sharma194877032019-03-07 17:52:472239void NavigationControllerImpl::NotifyUserActivation() {
2240 // When a user activation occurs, ensure that all adjacent entries for the
2241 // same document clear their skippable bit, so that the history manipulation
2242 // intervention does not apply to them.
Shivani Sharmaffb32b82019-04-09 16:58:472243 // TODO(crbug.com/949279) in case it becomes necessary for resetting based on
2244 // which frame created an entry and which frame has the user gesture.
Shivani Sharma194877032019-03-07 17:52:472245 auto* last_committed_entry = GetLastCommittedEntry();
2246 if (!last_committed_entry)
2247 return;
Shivani Sharma194877032019-03-07 17:52:472248
Shivani Sharmac4cc8922019-04-18 03:11:172249 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472250}
2251
clamy987a3752018-05-03 17:36:262252bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2253 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172254 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262255 NavigationEntryImpl* entry =
2256 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2257 if (!entry)
2258 return false;
2259
2260 FrameNavigationEntry* frame_entry =
2261 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2262 if (!frame_entry)
2263 return false;
2264
Camille Lamy5193caa2018-10-12 11:59:422265 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572266 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232267 ReloadType::NONE, false /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422268 true /* is_history_navigation_in_new_child */);
clamyea99ea12018-05-28 13:54:232269
2270 if (!request)
2271 return false;
2272
arthursonzognif046d4a2019-12-12 19:08:102273 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412274
Lukasz Anforowicz9ee83c272020-12-01 20:14:052275 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2276 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232277
2278 return true;
clamy987a3752018-05-03 17:36:262279}
2280
Tsuyoshi Horo52fd08e2020-07-07 07:03:452281bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2282 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2283 if (!entry)
2284 return false;
2285 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2286 if (!frame_entry)
2287 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142288 ReloadType reload_type = ReloadType::NORMAL;
2289 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452290 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142291 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452292 false /* is_same_document_history_load */,
2293 false /* is_history_navigation_in_new_child */);
2294 if (!request)
2295 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052296 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452297 return true;
2298}
2299
Dave Tapuska8bfd84c2019-03-26 20:47:162300void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2301 int offset,
2302 int sandbox_frame_tree_node_id) {
2303 if (!CanGoToOffset(offset))
2304 return;
2305 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id);
2306}
2307
clamy987a3752018-05-03 17:36:262308void NavigationControllerImpl::NavigateFromFrameProxy(
2309 RenderFrameHostImpl* render_frame_host,
2310 const GURL& url,
Antonio Sartori9a82f6f32020-12-14 09:22:452311 const base::UnguessableToken* initiator_frame_token,
2312 int initiator_process_id,
Lukasz Anforowicz63f3b9432019-05-30 05:42:582313 const base::Optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262314 bool is_renderer_initiated,
2315 SiteInstance* source_site_instance,
2316 const Referrer& referrer,
2317 ui::PageTransition page_transition,
2318 bool should_replace_current_entry,
Charlie Harrison8c113a32019-01-07 16:08:292319 NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262320 const std::string& method,
2321 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092322 const std::string& extra_headers,
John Delaney50425f82020-04-07 16:26:212323 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Gyuyoung Kim38e63632021-01-14 13:11:392324 const base::Optional<blink::Impression>& impression) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582325 if (is_renderer_initiated)
2326 DCHECK(initiator_origin.has_value());
2327
clamy987a3752018-05-03 17:36:262328 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582329
Rakina Zata Amni66992a82020-06-24 03:57:522330 // Don't allow an entry replacement if there is no entry to replace.
2331 // http://crbug.com/457149
2332 if (GetEntryCount() == 0)
2333 should_replace_current_entry = false;
2334
clamy987a3752018-05-03 17:36:262335 // Create a NavigationEntry for the transfer, without making it the pending
2336 // entry. Subframe transfers should have a clone of the last committed entry
2337 // with a FrameNavigationEntry for the target frame. Main frame transfers
2338 // should have a new NavigationEntry.
2339 // TODO(creis): Make this unnecessary by creating (and validating) the params
2340 // directly, passing them to the destination RenderFrameHost. See
2341 // https://crbug.com/536906.
2342 std::unique_ptr<NavigationEntryImpl> entry;
2343 if (!node->IsMainFrame()) {
2344 // Subframe case: create FrameNavigationEntry.
2345 if (GetLastCommittedEntry()) {
2346 entry = GetLastCommittedEntry()->Clone();
2347 entry->set_extra_headers(extra_headers);
2348 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2349 // Renderer-initiated navigation that target a remote frame are currently
2350 // classified as browser-initiated when this one has already navigated.
2351 // See https://crbug.com/722251.
2352 } else {
2353 // If there's no last committed entry, create an entry for about:blank
2354 // with a subframe entry for our destination.
2355 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2356 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:062357 GURL(url::kAboutBlankURL), referrer, initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:102358 source_site_instance, page_transition, is_renderer_initiated,
2359 extra_headers, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:372360 nullptr /* blob_url_loader_factory */, should_replace_current_entry,
2361 GetWebContents()));
2362 // CreateNavigationEntry() may have changed the transition type.
2363 page_transition = entry->GetTransitionType();
clamy987a3752018-05-03 17:36:262364 }
Antonio Sartori78a749f2020-11-30 12:03:392365 std::unique_ptr<PolicyContainerHost::DocumentPolicies> document_policies;
2366 if (GetLastCommittedEntry()) {
2367 FrameNavigationEntry* previous_frame_entry =
2368 GetLastCommittedEntry()->GetFrameEntry(node);
2369 if (previous_frame_entry && previous_frame_entry->document_policies()) {
2370 document_policies =
2371 std::make_unique<PolicyContainerHost::DocumentPolicies>(
2372 *previous_frame_entry->document_policies());
2373 }
2374 }
clamy987a3752018-05-03 17:36:262375 entry->AddOrUpdateFrameEntry(
2376 node, -1, -1, nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582377 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Lukasz Anforowicz435bcb582019-07-12 20:50:062378 base::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202379 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392380 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
2381 std::move(document_policies));
clamy987a3752018-05-03 17:36:262382 } else {
2383 // Main frame case.
2384 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102385 url, referrer, initiator_origin, source_site_instance, page_transition,
2386 is_renderer_initiated, extra_headers, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:372387 blob_url_loader_factory, should_replace_current_entry,
2388 GetWebContents()));
2389 // CreateNavigationEntry() may have changed the transition type.
2390 page_transition = entry->GetTransitionType();
clamy987a3752018-05-03 17:36:262391 entry->root_node()->frame_entry->set_source_site_instance(
2392 static_cast<SiteInstanceImpl*>(source_site_instance));
2393 entry->root_node()->frame_entry->set_method(method);
2394 }
clamy987a3752018-05-03 17:36:262395
Camille Lamy5193caa2018-10-12 11:59:422396 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262397 if (GetLastCommittedEntry() &&
2398 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2399 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422400 override_user_agent = true;
clamy987a3752018-05-03 17:36:262401 }
2402 // TODO(creis): Set user gesture and intent received timestamp on Android.
2403
2404 // We may not have successfully added the FrameNavigationEntry to |entry|
2405 // above (per https://crbug.com/608402), in which case we create it from
2406 // scratch. This works because we do not depend on |frame_entry| being inside
2407 // |entry| during NavigateToEntry. This will go away when we shortcut this
2408 // further in https://crbug.com/536906.
2409 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2410 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452411 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
clamy987a3752018-05-03 17:36:262412 node->unique_name(), -1, -1, nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582413 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Lukasz Anforowicz435bcb582019-07-12 20:50:062414 nullptr /* origin */, referrer, initiator_origin, std::vector<GURL>(),
Miyoung Shin5d77f72072020-10-09 15:14:202415 blink::PageState(), method, -1, blob_url_loader_factory,
Antonio Sartori78a749f2020-11-30 12:03:392416 nullptr /* web_bundle_navigation_info */,
2417 nullptr /* document_policies */);
clamy987a3752018-05-03 17:36:262418 }
2419
Camille Lamy5193caa2018-10-12 11:59:422420 LoadURLParams params(url);
Antonio Sartori9a82f6f32020-12-14 09:22:452421 params.initiator_frame_token =
2422 initiator_frame_token ? base::make_optional(*initiator_frame_token)
2423 : base::nullopt;
2424 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292425 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422426 params.source_site_instance = source_site_instance;
2427 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2428 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032429 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422430 params.referrer = referrer;
2431 /* params.redirect_chain: skip */
2432 params.extra_headers = extra_headers;
2433 params.is_renderer_initiated = is_renderer_initiated;
2434 params.override_user_agent = UA_OVERRIDE_INHERIT;
2435 /* params.base_url_for_data_url: skip */
2436 /* params.virtual_url_for_data_url: skip */
2437 /* params.data_url_as_string: skip */
2438 params.post_data = post_body;
2439 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582440 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422441 /* params.frame_name: skip */
2442 // TODO(clamy): See if user gesture should be propagated to this function.
2443 params.has_user_gesture = false;
2444 params.should_clear_history_list = false;
2445 params.started_from_context_menu = false;
2446 /* params.navigation_ui_data: skip */
2447 /* params.input_start: skip */
Lucas Furukawa Gadania9c45682019-07-31 22:05:142448 params.was_activated = mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542449 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212450 params.impression = impression;
Camille Lamy5193caa2018-10-12 11:59:422451
2452 std::unique_ptr<NavigationRequest> request =
2453 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032454 node, params, override_user_agent, should_replace_current_entry,
2455 false /* has_user_gesture */, download_policy, ReloadType::NONE,
Scott Violet5ae6c42e2020-10-28 02:47:372456 entry.get(), frame_entry.get(), params.transition_type);
clamyea99ea12018-05-28 13:54:232457
2458 if (!request)
2459 return;
2460
Arthur Hemery948742762019-09-18 10:06:242461 // At this stage we are proceeding with this navigation. If this was renderer
2462 // initiated with user gesture, we need to make sure we clear up potential
2463 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2464 DiscardNonCommittedEntries();
2465
Lukasz Anforowicz9ee83c272020-12-01 20:14:052466 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262467}
2468
[email protected]d1198fd2012-08-13 22:50:192469void NavigationControllerImpl::SetSessionStorageNamespace(
[email protected]fdac6ade2013-07-20 01:06:302470 const std::string& partition_id,
[email protected]8ff00d72012-10-23 19:12:212471 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192472 if (!session_storage_namespace)
2473 return;
2474
2475 // We can't overwrite an existing SessionStorage without violating spec.
2476 // Attempts to do so may give a tab access to another tab's session storage
2477 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152478 bool successful_insert =
2479 session_storage_namespace_map_
2480 .insert(
2481 make_pair(partition_id, static_cast<SessionStorageNamespaceImpl*>(
2482 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302483 .second;
2484 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
[email protected]d1198fd2012-08-13 22:50:192485}
2486
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572487bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Aran Gilman37d11632019-10-08 23:07:152488 return IsInitialNavigation() && !GetLastCommittedEntry() &&
[email protected]b4c84012014-04-28 19:51:102489 !delegate_->HasAccessedInitialDocument();
[email protected]aa62afd2014-04-22 19:22:462490}
2491
Aran Gilman37d11632019-10-08 23:07:152492SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
2493 SiteInstance* instance) {
[email protected]fdac6ade2013-07-20 01:06:302494 std::string partition_id;
2495 if (instance) {
2496 // TODO(ajwong): When GetDefaultSessionStorageNamespace() goes away, remove
danakjf26536bf2020-09-10 00:46:132497 // this if statement so |instance| must not be null.
Aran Gilman37d11632019-10-08 23:07:152498 partition_id = GetContentClient()->browser()->GetStoragePartitionIdForSite(
2499 browser_context_, instance->GetSiteURL());
[email protected]fdac6ade2013-07-20 01:06:302500 }
[email protected]d1198fd2012-08-13 22:50:192501
[email protected]fdac6ade2013-07-20 01:06:302502 // TODO(ajwong): Should this use the |partition_id| directly rather than
2503 // re-lookup via |instance|? http://crbug.com/142685
2504 StoragePartition* partition =
michaelnbacbcbd2016-02-09 00:32:032505 BrowserContext::GetStoragePartition(browser_context_, instance);
2506 DOMStorageContextWrapper* context_wrapper =
2507 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2508
2509 SessionStorageNamespaceMap::const_iterator it =
2510 session_storage_namespace_map_.find(partition_id);
2511 if (it != session_storage_namespace_map_.end()) {
2512 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152513 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2514 ->IsFromContext(context_wrapper));
michaelnbacbcbd2016-02-09 00:32:032515 return it->second.get();
2516 }
2517
2518 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102519 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2520 SessionStorageNamespaceImpl::Create(context_wrapper);
2521 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2522 session_storage_namespace.get();
2523 session_storage_namespace_map_[partition_id] =
2524 std::move(session_storage_namespace);
[email protected]fdac6ade2013-07-20 01:06:302525
Daniel Murphy31bbb8b12018-02-07 21:44:102526 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302527}
2528
2529SessionStorageNamespace*
2530NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
2531 // TODO(ajwong): Remove if statement in GetSessionStorageNamespace().
Ivan Kotenkov2c0d2bb32017-11-01 15:41:282532 return GetSessionStorageNamespace(nullptr);
[email protected]fdac6ade2013-07-20 01:06:302533}
2534
2535const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572536NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302537 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552538}
[email protected]d202a7c2012-01-04 07:53:472539
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572540bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562541 return needs_reload_;
2542}
[email protected]a26023822011-12-29 00:23:552543
[email protected]46bb5e9c2013-10-03 22:16:472544void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412545 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2546}
2547
2548void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472549 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412550 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542551
2552 if (last_committed_entry_index_ != -1) {
2553 entries_[last_committed_entry_index_]->SetTransitionType(
2554 ui::PAGE_TRANSITION_RELOAD);
2555 }
[email protected]46bb5e9c2013-10-03 22:16:472556}
2557
[email protected]d202a7c2012-01-04 07:53:472558void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572559 DCHECK_LT(index, GetEntryCount());
2560 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312561 DiscardNonCommittedEntries();
2562
2563 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122564 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312565 last_committed_entry_index_--;
2566}
2567
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572568NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272569 // If there is no pending_entry_, there should be no pending_entry_index_.
2570 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2571
2572 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362573 // at that index. An exception is while a reload of a post commit error page
2574 // is ongoing; in that case pending entry will point to the entry replaced
2575 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272576 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362577 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2578 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272579
[email protected]022af742011-12-28 18:37:252580 return pending_entry_;
2581}
2582
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572583int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272584 // The pending entry index must always be less than the number of entries.
2585 // If there are no entries, it must be exactly -1.
2586 DCHECK_LT(pending_entry_index_, GetEntryCount());
2587 DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:552588 return pending_entry_index_;
2589}
2590
avi25764702015-06-23 15:43:372591void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572592 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362593 bool replace,
2594 bool was_post_commit_error) {
mastiz7eddb5f72016-06-23 09:52:452595 DCHECK(!ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2596 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202597
avi5cad4912015-06-19 05:25:442598 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2599 // need to keep continuity with the pending entry, so copy the pending entry's
2600 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2601 // then the renderer navigated on its own, independent of the pending entry,
2602 // so don't copy anything.
2603 if (pending_entry_ && pending_entry_index_ == -1)
2604 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202605
arthursonzogni69a6a1b2019-09-17 09:23:002606 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:202607
creisee17e932015-07-17 17:56:222608 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362609 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102610 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572611 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362612 // If the new entry is a post-commit error page, we store the current last
2613 // committed entry to the side so that we can put it back when navigating
2614 // away from the error.
2615 if (was_post_commit_error) {
2616 DCHECK(!entry_replaced_by_post_commit_error_);
2617 entry_replaced_by_post_commit_error_ =
2618 std::move(entries_[last_committed_entry_index_]);
2619 }
dcheng36b6aec92015-12-26 06:16:362620 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222621 return;
2622 }
[email protected]765b35502008-08-21 00:51:202623
creis37979a62015-08-04 19:48:182624 // We shouldn't see replace == true when there's no committed entries.
2625 DCHECK(!replace);
2626
Michael Thiessen9b14d512019-09-23 21:19:472627 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202628
Shivani Sharmad8c8d652019-02-13 17:27:572629 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202630
dcheng36b6aec92015-12-26 06:16:362631 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202632 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292633}
2634
Shivani Sharmad8c8d652019-02-13 17:27:572635void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162636 if (entries_.size() < max_entry_count())
2637 return;
2638
2639 DCHECK_EQ(max_entry_count(), entries_.size());
2640 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572641 CHECK_EQ(pending_entry_index_, -1);
2642
2643 int index = 0;
2644 if (base::FeatureList::IsEnabled(
2645 features::kHistoryManipulationIntervention)) {
2646 // Retrieve the oldest skippable entry.
2647 for (; index < GetEntryCount(); index++) {
2648 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2649 break;
2650 }
2651 }
2652
2653 // If there is no skippable entry or if it is the last committed entry then
2654 // fall back to pruning the oldest entry. It is not safe to prune the last
2655 // committed entry.
2656 if (index == GetEntryCount() || index == last_committed_entry_index_)
2657 index = 0;
2658
2659 bool should_succeed = RemoveEntryAtIndex(index);
2660 DCHECK_EQ(true, should_succeed);
2661
2662 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252663}
2664
clamy3cb9bea92018-07-10 12:42:022665void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162666 ReloadType reload_type,
2667 int sandboxed_source_frame_tree_node_id) {
Alexander Timin3a92df72019-09-20 11:59:502668 TRACE_EVENT0("navigation",
2669 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272670 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022671 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362672 if (pending_entry_index_ != -1) {
2673 // The pending entry may not be in entries_ if a post-commit error page is
2674 // showing.
2675 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2676 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2677 }
clamy3cb9bea92018-07-10 12:42:022678 DCHECK(!IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572679 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:012680 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:002681 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:572682 int nav_entry_id = pending_entry_->GetUniqueID();
2683
[email protected]83c2e232011-10-07 21:36:462684 // If we were navigating to a slow-to-commit page, and the user performs
2685 // a session history navigation to the last committed page, RenderViewHost
2686 // will force the throbber to start, but WebKit will essentially ignore the
2687 // navigation, and won't send a message to stop the throbber. To prevent this
2688 // from happening, we drop the navigation here and stop the slow-to-commit
2689 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022690 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:082691 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:272692 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
[email protected]ec6c05f2013-10-23 18:41:572693 delegate_->Stop();
[email protected]6a13a6c2011-12-20 21:47:122694
[email protected]83c2e232011-10-07 21:36:462695 DiscardNonCommittedEntries();
2696 return;
2697 }
2698
creisce0ef3572017-01-26 17:53:082699 // Compare FrameNavigationEntries to see which frames in the tree need to be
2700 // navigated.
clamy3cb9bea92018-07-10 12:42:022701 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2702 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Alex Moshchuk3a4e77a2020-05-29 21:32:572703 FindFramesToNavigate(root, reload_type, &same_document_loads,
2704 &different_document_loads);
creis4e2ecb72015-06-20 00:46:302705
2706 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572707 // We were unable to match any frames to navigate. This can happen if a
2708 // history navigation targets a subframe that no longer exists
2709 // (https://crbug.com/705550). In this case, we need to update the current
2710 // history entry to the pending one but keep the main document loaded. We
2711 // also need to ensure that observers are informed about the updated
2712 // current history entry (e.g., for greying out back/forward buttons), and
2713 // that renderer processes update their history offsets. The easiest way
2714 // to do all that is to schedule a "redundant" same-document navigation in
2715 // the main frame.
2716 //
2717 // Note that we don't want to remove this history entry, as it might still
2718 // be valid later, since a frame that it's targeting may be recreated.
2719 //
2720 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
2721 // need to repeat history navigations until finding the one that works.
2722 // Consider changing this behavior to keep looking for the first valid
2723 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:022724 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422725 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572726 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:572727 ReloadType::NONE /* reload_type */,
2728 true /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422729 false /* is_history_navigation_in_new_child */);
clamy3cb9bea92018-07-10 12:42:022730 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572731 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:022732 DiscardPendingEntry(false);
2733 return;
2734 }
Alex Moshchuk3a4e77a2020-05-29 21:32:572735 same_document_loads.push_back(std::move(navigation_request));
2736
2737 // Sanity check that we never take this branch for any kinds of reloads,
2738 // as those should've queued a different-document load in the main frame.
2739 DCHECK(!is_forced_reload);
2740 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:302741 }
2742
Dave Tapuska8bfd84c2019-03-26 20:47:162743 // If |sandboxed_source_frame_node_id| is valid, then track whether this
2744 // navigation affects any frame outside the frame's subtree.
2745 if (sandboxed_source_frame_tree_node_id !=
2746 FrameTreeNode::kFrameTreeNodeInvalidId) {
2747 bool navigates_inside_tree =
2748 DoesSandboxNavigationStayWithinSubtree(
2749 sandboxed_source_frame_tree_node_id, same_document_loads) &&
2750 DoesSandboxNavigationStayWithinSubtree(
2751 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:502752 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:162753 // the number of pages that trigger this.
2754 FrameTreeNode* sandbox_source_frame_tree_node =
2755 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
2756 if (sandbox_source_frame_tree_node) {
2757 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
2758 sandbox_source_frame_tree_node->current_frame_host(),
2759 navigates_inside_tree
2760 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
2761 : blink::mojom::WebFeature::
2762 kSandboxBackForwardAffectsFramesOutsideSubtree);
2763 }
Dave Tapuska855c1e12019-08-23 20:45:522764
2765 // If the navigation occurred outside the tree discard it because
2766 // the sandboxed frame didn't have permission to navigate outside
2767 // its tree. If it is possible that the navigation is both inside and
2768 // outside the frame tree and we discard it entirely because we don't
2769 // want to end up in a history state that didn't exist before.
2770 if (base::FeatureList::IsEnabled(
2771 features::kHistoryPreventSandboxedNavigation) &&
2772 !navigates_inside_tree) {
2773 DiscardPendingEntry(false);
2774 return;
2775 }
Dave Tapuska8bfd84c2019-03-26 20:47:162776 }
2777
Carlos Caballero539a421c2020-07-06 10:25:572778 // BackForwardCache:
2779 // Navigate immediately if the document is in the BackForwardCache.
2780 if (back_forward_cache_.GetEntry(nav_entry_id)) {
2781 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
2782 DCHECK_EQ(reload_type, ReloadType::NONE);
2783 auto navigation_request = CreateNavigationRequestFromEntry(
2784 root, pending_entry_, pending_entry_->GetFrameEntry(root),
2785 ReloadType::NONE, false /* is_same_document_history_load */,
2786 false /* is_history_navigation_in_new_child */);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052787 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:572788
2789 return;
2790 }
2791
2792 // History navigation might try to reuse a specific BrowsingInstance, already
2793 // used by a page in the cache. To avoid having two different main frames that
2794 // live in the same BrowsingInstance, evict the all pages with this
2795 // BrowsingInstance from the cache.
2796 //
2797 // For example, take the following scenario:
2798 //
2799 // A1 = Some page on a.com
2800 // A2 = Some other page on a.com
2801 // B3 = An uncacheable page on b.com
2802 //
2803 // Then the following navigations occur:
2804 // A1->A2->B3->A1
2805 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
2806 // take its place) and A1 will be created in the same BrowsingInstance (and
2807 // SiteInstance), as A2.
2808 //
2809 // If we didn't do anything, both A1 and A2 would remain alive in the same
2810 // BrowsingInstance/SiteInstance, which is unsupported by
2811 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
2812 // A2 from the cache.
2813 if (pending_entry_->site_instance()) {
2814 back_forward_cache_.EvictFramesInRelatedSiteInstances(
2815 pending_entry_->site_instance());
2816 }
2817
clamy3cb9bea92018-07-10 12:42:022818 // This call does not support re-entrancy. See http://crbug.com/347742.
2819 CHECK(!in_navigate_to_pending_entry_);
2820 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:302821
arthursonzogni66f711c2019-10-08 14:40:362822 // It is not possible to delete the pending NavigationEntry while navigating
2823 // to it. Grab a reference to delay potential deletion until the end of this
2824 // function.
2825 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
2826
creis4e2ecb72015-06-20 00:46:302827 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:022828 for (auto& item : same_document_loads) {
2829 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052830 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302831 }
clamy3cb9bea92018-07-10 12:42:022832 for (auto& item : different_document_loads) {
2833 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052834 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302835 }
clamy3cb9bea92018-07-10 12:42:022836
2837 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:302838}
2839
Alex Moshchuk3a4e77a2020-05-29 21:32:572840NavigationControllerImpl::HistoryNavigationAction
2841NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:302842 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:572843 ReloadType reload_type) {
Sreeja Kamishetty0be2ac52020-05-28 01:47:462844 // Only active frames can navigate:
2845 // - If the frame is in pending deletion, the browser already committed to
2846 // destroying this RenderFrameHost. See https://crbug.com/930278.
2847 // - If the frame is in back-forward cache, it's not allowed to navigate as it
2848 // should remain frozen. Ignore the request and evict the document from
2849 // back-forward cache.
Alex Moshchuke65c39272020-06-03 17:55:372850 //
2851 // If the frame is inactive, there's no need to recurse into subframes, which
2852 // should all be inactive as well.
Sreeja Kamishetty0be2ac52020-05-28 01:47:462853 if (frame->current_frame_host()->IsInactiveAndDisallowReactivation())
Alex Moshchuke65c39272020-06-03 17:55:372854 return HistoryNavigationAction::kStopLooking;
arthursonzogni03f76152019-02-12 10:35:202855
Alex Moshchuk3a4e77a2020-05-29 21:32:572856 // If there's no last committed entry, there is no previous history entry to
2857 // compare against, so fall back to a different-document load. Note that we
2858 // should only reach this case for the root frame and not descend further
2859 // into subframes.
2860 if (!GetLastCommittedEntry()) {
2861 DCHECK(frame->IsMainFrame());
2862 return HistoryNavigationAction::kDifferentDocument;
2863 }
2864
2865 // Reloads should result in a different-document load. Note that reloads may
2866 // also happen via the |needs_reload_| mechanism where the reload_type is
2867 // NONE, so detect this by comparing whether we're going to the same
2868 // entry that we're currently on. Similarly to above, only main frames
2869 // should reach this. Note that subframes support reloads, but that's done
2870 // via a different path that doesn't involve FindFramesToNavigate (see
2871 // RenderFrameHost::Reload()).
2872 if (reload_type != ReloadType::NONE ||
2873 pending_entry_index_ == last_committed_entry_index_) {
2874 DCHECK(frame->IsMainFrame());
2875 return HistoryNavigationAction::kDifferentDocument;
2876 }
2877
Alex Moshchuk47d1a4bd2020-06-01 22:15:342878 // If there is no new FrameNavigationEntry for the frame, ignore the
2879 // load. For example, this may happen when going back to an entry before a
2880 // frame was created. Suppose we commit a same-document navigation that also
2881 // results in adding a new subframe somewhere in the tree. If we go back,
2882 // the new subframe will be missing a FrameNavigationEntry in the previous
2883 // NavigationEntry, but we shouldn't delete or change what's loaded in
2884 // it.
2885 //
Alex Moshchuke65c39272020-06-03 17:55:372886 // Note that in this case, there is no need to keep looking for navigations
2887 // in subframes, which would be missing FrameNavigationEntries as well.
2888 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:342889 // It's important to check this before checking |old_item| below, since both
2890 // might be null, and in that case we still shouldn't change what's loaded in
2891 // this frame. Note that scheduling any loads assumes that |new_item| is
2892 // non-null. See https://crbug.com/1088354.
2893 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
2894 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:372895 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:342896
Alex Moshchuk3a4e77a2020-05-29 21:32:572897 // If there is no old FrameNavigationEntry, schedule a different-document
2898 // load.
2899 //
creis225a7432016-06-03 22:56:272900 // TODO(creis): Store the last committed FrameNavigationEntry to use here,
2901 // rather than assuming the NavigationEntry has up to date info on subframes.
Alex Moshchuk3a4e77a2020-05-29 21:32:572902 // Note that this may require sharing FrameNavigationEntries between
2903 // NavigationEntries (https://crbug.com/373041) as a prerequisite, since
2904 // otherwise the stored FrameNavigationEntry might get stale (e.g., after
2905 // subframe navigations, or in the case where we don't find any frames to
2906 // navigate and ignore a back/forward navigation while moving to a different
2907 // NavigationEntry, as in https://crbug.com/705550).
creis4e2ecb72015-06-20 00:46:302908 FrameNavigationEntry* old_item =
2909 GetLastCommittedEntry()->GetFrameEntry(frame);
Alex Moshchuk3a4e77a2020-05-29 21:32:572910 if (!old_item)
2911 return HistoryNavigationAction::kDifferentDocument;
2912
Alex Moshchuk3a4e77a2020-05-29 21:32:572913 // If the new item is not in the same SiteInstance, schedule a
2914 // different-document load. Newly restored items may not have a SiteInstance
2915 // yet, in which case it will be assigned on first commit.
2916 if (new_item->site_instance() &&
2917 new_item->site_instance() != old_item->site_instance())
2918 return HistoryNavigationAction::kDifferentDocument;
2919
2920 // Schedule a different-document load if the current RenderFrameHost is not
2921 // live. This case can happen for Ctrl+Back or after
2922 // a renderer crash.
2923 if (!frame->current_frame_host()->IsRenderFrameLive())
2924 return HistoryNavigationAction::kDifferentDocument;
2925
2926 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
creis54131692016-08-12 18:32:252927 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:492928 // sequence number but different item sequence number.
2929 if (new_item->document_sequence_number() ==
2930 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572931 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:492932 }
avib48cb312016-05-05 21:35:002933
Alex Moshchuk3a4e77a2020-05-29 21:32:572934 // Otherwise, if both item and document sequence numbers differ, this
2935 // should be a different document load.
2936 return HistoryNavigationAction::kDifferentDocument;
2937 }
2938
2939 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:372940 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:572941 DCHECK_EQ(new_item->document_sequence_number(),
2942 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:372943 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:572944}
2945
2946void NavigationControllerImpl::FindFramesToNavigate(
2947 FrameTreeNode* frame,
2948 ReloadType reload_type,
2949 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
2950 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
2951 DCHECK(pending_entry_);
2952 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
2953
2954 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
2955
2956 if (action == HistoryNavigationAction::kSameDocument) {
2957 std::unique_ptr<NavigationRequest> navigation_request =
2958 CreateNavigationRequestFromEntry(
2959 frame, pending_entry_, new_item, reload_type,
2960 true /* is_same_document_history_load */,
2961 false /* is_history_navigation_in_new_child */);
2962 if (navigation_request) {
2963 // Only add the request if was properly created. It's possible for the
2964 // creation to fail in certain cases, e.g. when the URL is invalid.
2965 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:302966 }
Lei Zhang96031532019-10-10 19:05:472967
Alex Moshchuk3a4e77a2020-05-29 21:32:572968 // TODO(avi, creis): This is a bug; we should not return here. Rather, we
2969 // should continue on and navigate all child frames which have also
2970 // changed. This bug is the cause of <https://crbug.com/542299>, which is
2971 // a NC_IN_PAGE_NAVIGATION renderer kill.
2972 //
2973 // However, this bug is a bandaid over a deeper and worse problem. Doing a
2974 // pushState immediately after loading a subframe is a race, one that no
2975 // web page author expects. If we fix this bug, many large websites break.
2976 // For example, see <https://crbug.com/598043> and the spec discussion at
2977 // <https://github.com/whatwg/html/issues/1191>.
2978 //
2979 // For now, we accept this bug, and hope to resolve the race in a
2980 // different way that will one day allow us to fix this.
2981 return;
2982 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:472983 std::unique_ptr<NavigationRequest> navigation_request =
2984 CreateNavigationRequestFromEntry(
2985 frame, pending_entry_, new_item, reload_type,
2986 false /* is_same_document_history_load */,
2987 false /* is_history_navigation_in_new_child */);
2988 if (navigation_request) {
2989 // Only add the request if was properly created. It's possible for the
2990 // creation to fail in certain cases, e.g. when the URL is invalid.
2991 different_document_loads->push_back(std::move(navigation_request));
2992 }
2993 // For a different document, the subframes will be destroyed, so there's
2994 // no need to consider them.
2995 return;
Alex Moshchuke65c39272020-06-03 17:55:372996 } else if (action == HistoryNavigationAction::kStopLooking) {
2997 return;
creis4e2ecb72015-06-20 00:46:302998 }
2999
3000 for (size_t i = 0; i < frame->child_count(); i++) {
clamy3cb9bea92018-07-10 12:42:023001 FindFramesToNavigate(frame->child_at(i), reload_type, same_document_loads,
creis4e2ecb72015-06-20 00:46:303002 different_document_loads);
3003 }
3004}
3005
clamy21718cc22018-06-13 13:34:243006void NavigationControllerImpl::NavigateWithoutEntry(
3007 const LoadURLParams& params) {
3008 // Find the appropriate FrameTreeNode.
3009 FrameTreeNode* node = nullptr;
3010 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3011 !params.frame_name.empty()) {
3012 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003013 ? frame_tree_.FindByID(params.frame_tree_node_id)
3014 : frame_tree_.FindByName(params.frame_name);
clamy21718cc22018-06-13 13:34:243015 }
3016
3017 // If no FrameTreeNode was specified, navigate the main frame.
3018 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003019 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243020
Camille Lamy5193caa2018-10-12 11:59:423021 // Compute overrides to the LoadURLParams for |override_user_agent|,
3022 // |should_replace_current_entry| and |has_user_gesture| that will be used
3023 // both in the creation of the NavigationEntry and the NavigationRequest.
3024 // Ideally, the LoadURLParams themselves would be updated, but since they are
3025 // passed as a const reference, this is not possible.
3026 // TODO(clamy): When we only create a NavigationRequest, move this to
3027 // CreateNavigationRequestFromLoadURLParams.
3028 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3029 GetLastCommittedEntry());
3030
3031 // Don't allow an entry replacement if there is no entry to replace.
3032 // http://crbug.com/457149
3033 bool should_replace_current_entry =
3034 params.should_replace_current_entry && entries_.size();
3035
Scott Violet5ae6c42e2020-10-28 02:47:373036 ui::PageTransition transition_type = params.transition_type;
3037
clamy21718cc22018-06-13 13:34:243038 // Javascript URLs should not create NavigationEntries. All other navigations
3039 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243040 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373041 std::unique_ptr<NavigationEntryImpl> entry =
3042 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3043 should_replace_current_entry,
3044 params.has_user_gesture);
3045 // CreateNavigationEntryFromLoadParams() may modify the transition type.
3046 transition_type = entry->GetTransitionType();
clamy21718cc22018-06-13 13:34:243047 DiscardPendingEntry(false);
3048 SetPendingEntry(std::move(entry));
3049 }
3050
3051 // Renderer-debug URLs are sent to the renderer process immediately for
3052 // processing and don't need to create a NavigationRequest.
3053 // Note: this includes navigations to JavaScript URLs, which are considered
3054 // renderer-debug URLs.
3055 // Note: we intentionally leave the pending entry in place for renderer debug
3056 // URLs, unlike the cases below where we clear it if the navigation doesn't
3057 // proceed.
3058 if (IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483059 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3060 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153061 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483062 params.url, browser_context_)) {
3063 DiscardPendingEntry(false);
3064 return;
3065 }
3066
clamy21718cc22018-06-13 13:34:243067 HandleRendererDebugURL(node, params.url);
3068 return;
3069 }
3070
Antonio Sartori78a749f2020-11-30 12:03:393071 DCHECK(pending_entry_);
3072
clamy21718cc22018-06-13 13:34:243073 // Convert navigations to the current URL to a reload.
3074 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3075 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3076 // DevTools sometimes issues navigations to main frames that they do not
3077 // expect to see treated as reload, and it only works because they pass a
3078 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3079 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543080 ReloadType reload_type = params.reload_type;
3081 if (reload_type == ReloadType::NONE &&
3082 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043083 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243084 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243085 params.load_type ==
3086 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
3087 false /* is_reload */, false /* is_navigation_to_existing_entry */,
clamy21718cc22018-06-13 13:34:243088 GetLastCommittedEntry())) {
3089 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473090 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393091
3092 // If this is a reload of an existing FrameNavigationEntry and we had a
3093 // policy container for it, then we should copy it into the pending entry,
3094 // so that it is copied to the navigation request in
3095 // CreateNavigationRequestFromLoadParams later.
3096 if (GetLastCommittedEntry()) {
3097 FrameNavigationEntry* previous_frame_entry =
3098 GetLastCommittedEntry()->GetFrameEntry(node);
3099 if (previous_frame_entry && previous_frame_entry->document_policies()) {
3100 pending_entry_->GetFrameEntry(node)->set_document_policies(
3101 std::make_unique<PolicyContainerHost::DocumentPolicies>(
3102 *previous_frame_entry->document_policies()));
3103 }
3104 }
3105 }
3106
3107 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
3108 // document (so no last commit), then we should copy the document polices from
3109 // RenderFrameHost's PolicyContainerHost. The NavigationRequest will create a
3110 // new PolicyContainerHost with the document policies from the
3111 // |pending_entry_|, and that PolicyContainerHost will be put in the final
3112 // RenderFrameHost for the navigation. This way, we ensure that we keep
3113 // enforcing the right policies on the initial empty document after the
3114 // reload.
3115 if (!GetLastCommittedEntry() && params.url.IsAboutBlank()) {
3116 if (node->current_frame_host() &&
3117 node->current_frame_host()->policy_container_host()) {
3118 pending_entry_->GetFrameEntry(node)->set_document_policies(
3119 std::make_unique<PolicyContainerHost::DocumentPolicies>(
3120 node->current_frame_host()
3121 ->policy_container_host()
3122 ->document_policies()));
3123 }
clamy21718cc22018-06-13 13:34:243124 }
3125
3126 // navigation_ui_data should only be present for main frame navigations.
3127 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
3128
Camille Lamy5193caa2018-10-12 11:59:423129 std::unique_ptr<NavigationRequest> request =
3130 CreateNavigationRequestFromLoadParams(
3131 node, params, override_user_agent, should_replace_current_entry,
John Abd-El-Malekd95f34382020-10-06 04:56:463132 params.has_user_gesture, NavigationDownloadPolicy(), reload_type,
Scott Violet5ae6c42e2020-10-28 02:47:373133 pending_entry_, pending_entry_->GetFrameEntry(node), transition_type);
clamy21718cc22018-06-13 13:34:243134
3135 // If the navigation couldn't start, return immediately and discard the
3136 // pending NavigationEntry.
3137 if (!request) {
3138 DiscardPendingEntry(false);
3139 return;
3140 }
3141
Camille Lamy5193caa2018-10-12 11:59:423142#if DCHECK_IS_ON()
3143 // Safety check that NavigationRequest and NavigationEntry match.
3144 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3145#endif
3146
clamy21718cc22018-06-13 13:34:243147 // This call does not support re-entrancy. See http://crbug.com/347742.
3148 CHECK(!in_navigate_to_pending_entry_);
3149 in_navigate_to_pending_entry_ = true;
3150
arthursonzogni66f711c2019-10-08 14:40:363151 // It is not possible to delete the pending NavigationEntry while navigating
3152 // to it. Grab a reference to delay potential deletion until the end of this
3153 // function.
3154 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3155
Lukasz Anforowicz9ee83c272020-12-01 20:14:053156 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243157
3158 in_navigate_to_pending_entry_ = false;
3159}
3160
clamyea99ea12018-05-28 13:54:233161void NavigationControllerImpl::HandleRendererDebugURL(
3162 FrameTreeNode* frame_tree_node,
3163 const GURL& url) {
3164 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243165 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3166 // the renderer process is not live, unless it is the initial navigation
3167 // of the tab.
clamyea99ea12018-05-28 13:54:233168 if (!IsInitialNavigation()) {
3169 DiscardNonCommittedEntries();
3170 return;
3171 }
Fergal Dalyecd3b0202020-06-25 01:57:373172 // The current frame is always a main frame. If IsInitialNavigation() is
3173 // true then there have been no navigations and any frames of this tab must
3174 // be in the same renderer process. If that has crashed then the only frame
3175 // that can be revived is the main frame.
3176 frame_tree_node->render_manager()
3177 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233178 }
3179 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3180}
3181
clamy21718cc22018-06-13 13:34:243182std::unique_ptr<NavigationEntryImpl>
3183NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3184 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423185 const LoadURLParams& params,
3186 bool override_user_agent,
3187 bool should_replace_current_entry,
3188 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393189 // Browser initiated navigations might not have a blob_url_loader_factory set
3190 // in params even if the navigation is to a blob URL. If that happens, lookup
3191 // the correct url loader factory to use here.
3192 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483193 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473194 // Resolve the blob URL in the storage partition associated with the target
3195 // frame. This is the storage partition the URL will be loaded in, and only
3196 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393197 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473198 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393199 }
3200
clamy21718cc22018-06-13 13:34:243201 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443202 // extra_headers in params are \n separated; navigation entries want \r\n.
3203 std::string extra_headers_crlf;
3204 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243205
3206 // For subframes, create a pending entry with a corresponding frame entry.
3207 if (!node->IsMainFrame()) {
Tommy C. Li03eee77a2019-02-05 02:07:443208 if (GetLastCommittedEntry()) {
3209 // Create an identical NavigationEntry with a new FrameNavigationEntry for
3210 // the target subframe.
3211 entry = GetLastCommittedEntry()->Clone();
3212 } else {
3213 // If there's no last committed entry, create an entry for about:blank
3214 // with a subframe entry for our destination.
3215 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
3216 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063217 GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103218 params.source_site_instance.get(), params.transition_type,
3219 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:373220 blob_url_loader_factory, should_replace_current_entry,
3221 GetWebContents()));
Tommy C. Li03eee77a2019-02-05 02:07:443222 }
Nasko Oskov18006bc2018-12-06 02:53:583223
clamy21718cc22018-06-13 13:34:243224 entry->AddOrUpdateFrameEntry(
3225 node, -1, -1, nullptr,
3226 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Lukasz Anforowicz435bcb582019-07-12 20:50:063227 params.url, base::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203228 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393229 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
3230 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163231 // a conversion of a new navigation into a reload, we will set the right
3232 // document policies there.
Antonio Sartori78a749f2020-11-30 12:03:393233 nullptr /* document_policies */);
clamy21718cc22018-06-13 13:34:243234 } else {
3235 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243236 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063237 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103238 params.source_site_instance.get(), params.transition_type,
3239 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:373240 blob_url_loader_factory, should_replace_current_entry,
3241 GetWebContents()));
clamy21718cc22018-06-13 13:34:243242 entry->set_source_site_instance(
3243 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3244 entry->SetRedirectChain(params.redirect_chain);
3245 }
3246
3247 // Set the FTN ID (only used in non-site-per-process, for tests).
3248 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243249 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423250 entry->SetIsOverridingUserAgent(override_user_agent);
3251 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543252 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243253
clamy21718cc22018-06-13 13:34:243254 switch (params.load_type) {
3255 case LOAD_TYPE_DEFAULT:
3256 break;
3257 case LOAD_TYPE_HTTP_POST:
3258 entry->SetHasPostData(true);
3259 entry->SetPostData(params.post_data);
3260 break;
3261 case LOAD_TYPE_DATA:
3262 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3263 entry->SetVirtualURL(params.virtual_url_for_data_url);
3264#if defined(OS_ANDROID)
3265 entry->SetDataURLAsString(params.data_url_as_string);
3266#endif
3267 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3268 break;
clamy21718cc22018-06-13 13:34:243269 }
3270
3271 // TODO(clamy): NavigationEntry is meant for information that will be kept
3272 // after the navigation ended and therefore is not appropriate for
3273 // started_from_context_menu. Move started_from_context_menu to
3274 // NavigationUIData.
3275 entry->set_started_from_context_menu(params.started_from_context_menu);
3276
3277 return entry;
3278}
3279
clamyea99ea12018-05-28 13:54:233280std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423281NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3282 FrameTreeNode* node,
3283 const LoadURLParams& params,
3284 bool override_user_agent,
3285 bool should_replace_current_entry,
3286 bool has_user_gesture,
Charlie Harrison8c113a32019-01-07 16:08:293287 NavigationDownloadPolicy download_policy,
Camille Lamy5193caa2018-10-12 11:59:423288 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573289 NavigationEntryImpl* entry,
Scott Violet5ae6c42e2020-10-28 02:47:373290 FrameNavigationEntry* frame_entry,
3291 ui::PageTransition transition_type) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573292 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283293 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533294 // All renderer-initiated navigations must have an initiator_origin.
3295 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513296
Camille Lamy5193caa2018-10-12 11:59:423297 GURL url_to_load;
3298 GURL virtual_url;
Nasko Oskov03912102019-01-11 00:21:323299 base::Optional<url::Origin> origin_to_commit =
3300 frame_entry ? frame_entry->committed_origin() : base::nullopt;
3301
Camille Lamy2baa8022018-10-19 16:43:173302 // For main frames, rewrite the URL if necessary and compute the virtual URL
3303 // that should be shown in the address bar.
3304 if (node->IsMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423305 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173306 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423307 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423308
Camille Lamy2baa8022018-10-19 16:43:173309 // For DATA loads, override the virtual URL.
3310 if (params.load_type == LOAD_TYPE_DATA)
3311 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423312
Camille Lamy2baa8022018-10-19 16:43:173313 if (virtual_url.is_empty())
3314 virtual_url = url_to_load;
3315
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573316 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283317
Aran Gilman249eb122019-12-02 23:32:463318 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3319 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3320 // return different results, leading to a different URL in the
3321 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3322 // pending NavigationEntry, as we'll only make one call to
3323 // RewriteUrlForNavigation.
3324 VLOG_IF(1, (url_to_load != frame_entry->url()))
3325 << "NavigationRequest and FrameEntry have different URLs: "
3326 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283327
Camille Lamy2baa8022018-10-19 16:43:173328 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423329 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173330 // NavigationRequest.
3331 } else {
3332 url_to_load = params.url;
3333 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243334 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173335 }
Camille Lamy5193caa2018-10-12 11:59:423336
Ehsan Karamad44fc72112019-02-26 18:15:473337 if (node->render_manager()->is_attaching_inner_delegate()) {
3338 // Avoid starting any new navigations since this node is now preparing for
3339 // attaching an inner delegate.
3340 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203341 }
Camille Lamy5193caa2018-10-12 11:59:423342
Camille Lamy5193caa2018-10-12 11:59:423343 if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load))
3344 return nullptr;
3345
Nasko Oskov03912102019-01-11 00:21:323346 if (!DoesURLMatchOriginForNavigation(url_to_load, origin_to_commit)) {
3347 DCHECK(false) << " url:" << url_to_load
3348 << " origin:" << origin_to_commit.value();
3349 return nullptr;
3350 }
3351
Camille Lamy5193caa2018-10-12 11:59:423352 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403353 blink::PreviewsState previews_state =
3354 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
Camille Lamy5193caa2018-10-12 11:59:423355 if (!node->IsMainFrame()) {
3356 // For subframes, use the state of the top-level frame.
3357 previews_state = node->frame_tree()
3358 ->root()
3359 ->current_frame_host()
3360 ->last_navigation_previews_state();
3361 }
3362
Camille Lamy5193caa2018-10-12 11:59:423363 // This will be used to set the Navigation Timing API navigationStart
3364 // parameter for browser navigations in new tabs (intents, tabs opened through
3365 // "Open link in new tab"). If the navigation must wait on the current
3366 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3367 // will be updated when the BeforeUnload ack is received.
3368 base::TimeTicks navigation_start = base::TimeTicks::Now();
3369
danakjd83d706d2020-11-25 22:11:123370 // Look for a pending commit that is to another document in this
3371 // FrameTreeNode. If one exists, then the last committed URL will not be the
3372 // current URL by the time this navigation commits.
3373 bool has_pending_cross_document_commit =
3374 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493375 bool is_currently_error_page = node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123376
danakjb952ef12021-01-14 19:58:493377 mojom::NavigationType navigation_type = GetNavigationType(
3378 /*old_url=*/node->current_url(),
3379 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3380 has_pending_cross_document_commit, is_currently_error_page,
3381 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423382
3383 // Create the NavigationParams based on |params|.
3384
3385 bool is_view_source_mode = virtual_url.SchemeIs(kViewSourceScheme);
Charlie Harrison8c113a32019-01-07 16:08:293386
3387 // Update |download_policy| if the virtual URL is view-source. Why do this
3388 // now? Possibly the URL could be rewritten to a view-source via some URL
3389 // handler.
3390 if (is_view_source_mode)
Yao Xiaoaf79ca9b2019-03-12 19:54:183391 download_policy.SetDisallowed(NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293392
Camille Lamy5193caa2018-10-12 11:59:423393 const GURL& history_url_for_data_url =
3394 params.base_url_for_data_url.is_empty() ? GURL() : virtual_url;
Scott Violet5ae6c42e2020-10-28 02:47:373395 // Don't use |params.transition_type| as calling code may supply a different
3396 // value.
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513397 mojom::CommonNavigationParamsPtr common_params =
3398 mojom::CommonNavigationParams::New(
3399 url_to_load, params.initiator_origin,
3400 blink::mojom::Referrer::New(params.referrer.url,
3401 params.referrer.policy),
Scott Violet5ae6c42e2020-10-28 02:47:373402 transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513403 should_replace_current_entry, params.base_url_for_data_url,
3404 history_url_for_data_url, previews_state, navigation_start,
3405 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
arthursonzogniaf7c62c52020-02-12 10:49:413406 params.post_data, network::mojom::SourceLocation::New(),
3407 params.started_from_context_menu, has_user_gesture,
David Bokan71e81512020-08-18 17:19:463408 false /* has_text_fragment_token */, CreateInitiatorCSPInfo(),
3409 std::vector<int>(), params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513410 false /* is_history_navigation_in_new_child_frame */,
Charlie Hu5ffc0152019-12-06 15:59:533411 params.input_start);
Camille Lamy5193caa2018-10-12 11:59:423412
Lucas Furukawa Gadania9c45682019-07-31 22:05:143413 mojom::CommitNavigationParamsPtr commit_params =
3414 mojom::CommitNavigationParams::New(
Arthur Sonzogni367718c2021-01-25 20:55:393415 frame_entry->committed_origin(), override_user_agent,
3416 params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323417 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143418 std::vector<net::RedirectInfo>(),
3419 std::string() /* post_content_type */, common_params->url,
3420 common_params->method, params.can_load_local_resources,
3421 frame_entry->page_state(), entry->GetUniqueID(),
3422 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3423 -1 /* pending_history_list_offset */,
3424 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3425 params.should_clear_history_list ? 0 : GetEntryCount(),
3426 false /* was_discarded */, is_view_source_mode,
3427 params.should_clear_history_list, mojom::NavigationTiming::New(),
3428 base::nullopt /* appcache_host_id */,
3429 mojom::WasActivatedOption::kUnknown,
3430 base::UnguessableToken::Create() /* navigation_token */,
Lucas Furukawa Gadani97ea6382019-08-07 19:14:513431 std::vector<mojom::PrefetchedSignedExchangeInfoPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143432#if defined(OS_ANDROID)
3433 std::string(), /* data_url_as_string */
3434#endif
arthursonzogni14379782020-05-15 09:09:273435 !params.is_renderer_initiated, /* is_browser_initiated */
Takashi Toyoshimae87b7be2021-01-22 11:51:083436 node->parent() ? node->parent()->IsPrerendering()
3437 : params.is_prerendering,
Tsuyoshi Horoe86d7702019-11-29 01:52:473438 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533439 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173440 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073441 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163442 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333443 false /* origin_agent_cluster */,
Maks Orlovichc66745a2020-06-30 17:40:023444 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213445 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Antonio Sartori1e09fd7b2020-08-10 14:16:173446 false /* is_cross_browsing_instance */,
Rakina Zata Amni60858132020-08-19 10:33:483447 std::vector<std::string>() /* forced_content_security_policies */,
Rakina Zata Amniba3eecb2020-11-02 10:12:283448 nullptr /* old_page_info */, -1 /* http_response_code */);
Camille Lamy5193caa2018-10-12 11:59:423449#if defined(OS_ANDROID)
3450 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143451 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423452 }
3453#endif
3454
Lucas Furukawa Gadania9c45682019-07-31 22:05:143455 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423456
3457 // A form submission may happen here if the navigation is a renderer-initiated
3458 // form submission that took the OpenURL path.
3459 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3460
3461 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3462 std::string extra_headers_crlf;
3463 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093464
3465 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143466 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083467 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453468 params.initiator_frame_token.has_value()
3469 ? &(params.initiator_frame_token.value())
3470 : nullptr,
3471 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3472 request_body,
John Delaney50425f82020-04-07 16:26:213473 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
3474 params.impression);
Yao Xiaodc5ed102019-06-04 19:19:093475 navigation_request->set_from_download_cross_origin_redirect(
3476 params.from_download_cross_origin_redirect);
3477 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423478}
3479
3480std::unique_ptr<NavigationRequest>
3481NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233482 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573483 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233484 FrameNavigationEntry* frame_entry,
3485 ReloadType reload_type,
3486 bool is_same_document_history_load,
Arthur Hemerybee4a752019-05-29 10:50:553487 bool is_history_navigation_in_new_child_frame) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343488 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233489 GURL dest_url = frame_entry->url();
Nasko Oskov03912102019-01-11 00:21:323490 base::Optional<url::Origin> origin_to_commit =
3491 frame_entry->committed_origin();
3492
clamyea99ea12018-05-28 13:54:233493 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013494 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573495 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233496 // We may have been redirected when navigating to the current URL.
3497 // Use the URL the user originally intended to visit as signaled by the
3498 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013499 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573500 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233501 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323502 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233503 }
3504
Ehsan Karamad44fc72112019-02-26 18:15:473505 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3506 // Avoid starting any new navigations since this node is now preparing for
3507 // attaching an inner delegate.
3508 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203509 }
3510
Camille Lamy5193caa2018-10-12 11:59:423511 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573512 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233513 return nullptr;
3514 }
3515
Nasko Oskov03912102019-01-11 00:21:323516 if (!DoesURLMatchOriginForNavigation(dest_url, origin_to_commit)) {
3517 DCHECK(false) << " url:" << dest_url
3518 << " origin:" << origin_to_commit.value();
3519 return nullptr;
3520 }
3521
clamyea99ea12018-05-28 13:54:233522 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403523 blink::PreviewsState previews_state =
3524 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
clamyea99ea12018-05-28 13:54:233525 if (!frame_tree_node->IsMainFrame()) {
3526 // For subframes, use the state of the top-level frame.
3527 previews_state = frame_tree_node->frame_tree()
3528 ->root()
3529 ->current_frame_host()
3530 ->last_navigation_previews_state();
clamyea99ea12018-05-28 13:54:233531 }
3532
clamyea99ea12018-05-28 13:54:233533 // This will be used to set the Navigation Timing API navigationStart
3534 // parameter for browser navigations in new tabs (intents, tabs opened through
3535 // "Open link in new tab"). If the navigation must wait on the current
3536 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3537 // will be updated when the BeforeUnload ack is received.
3538 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233539
danakjd83d706d2020-11-25 22:11:123540 // Look for a pending commit that is to another document in this
3541 // FrameTreeNode. If one exists, then the last committed URL will not be the
3542 // current URL by the time this navigation commits.
3543 bool has_pending_cross_document_commit =
3544 frame_tree_node->render_manager()
3545 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493546 bool is_currently_error_page =
3547 frame_tree_node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123548
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513549 mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123550 /*old_url=*/frame_tree_node->current_url(),
3551 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493552 has_pending_cross_document_commit, is_currently_error_page,
3553 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423554
3555 // A form submission may happen here if the navigation is a
3556 // back/forward/reload navigation that does a form resubmission.
3557 scoped_refptr<network::ResourceRequestBody> request_body;
3558 std::string post_content_type;
3559 if (frame_entry->method() == "POST") {
3560 request_body = frame_entry->GetPostData(&post_content_type);
3561 // Might have a LF at end.
3562 post_content_type =
3563 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL)
3564 .as_string();
3565 }
3566
3567 // Create the NavigationParams based on |entry| and |frame_entry|.
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513568 mojom::CommonNavigationParamsPtr common_params =
3569 entry->ConstructCommonNavigationParams(
3570 *frame_entry, request_body, dest_url,
3571 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
3572 navigation_type, previews_state, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533573 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513574 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553575 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423576
3577 // TODO(clamy): |intended_as_new_entry| below should always be false once
3578 // Reload no longer leads to this being called for a pending NavigationEntry
3579 // of index -1.
Lucas Furukawa Gadania9c45682019-07-31 22:05:143580 mojom::CommitNavigationParamsPtr commit_params =
3581 entry->ConstructCommitNavigationParams(
3582 *frame_entry, common_params->url, origin_to_commit,
3583 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3584 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533585 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3586 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143587 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423588
clamyea99ea12018-05-28 13:54:233589 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143590 frame_tree_node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083591 !entry->is_renderer_initiated(), false /* was_opener_suppressed */,
3592 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453593 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3594 entry->extra_headers(), frame_entry, entry, request_body,
3595 nullptr /* navigation_ui_data */, base::nullopt /* impression */);
clamyea99ea12018-05-28 13:54:233596}
3597
[email protected]d202a7c2012-01-04 07:53:473598void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213599 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273600 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403601
[email protected]2db9bd72012-04-13 20:20:563602 // We need to notify the ssl_manager_ before the web_contents_ so the
[email protected]df1af242009-05-01 00:11:403603 // location bar will have up-to-date information about the security style
3604 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133605 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403606
[email protected]7f924832014-08-09 05:57:223607 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:573608 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463609
[email protected]b0f724c2013-09-05 04:21:133610 // TODO(avi): Remove. http://crbug.com/170921
3611 NotificationDetails notification_details =
3612 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153613 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3614 Source<NavigationController>(this),
3615 notification_details);
initial.commit09911bf2008-07-26 23:55:293616}
3617
initial.commit09911bf2008-07-26 23:55:293618// static
[email protected]d202a7c2012-01-04 07:53:473619size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233620 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153621 return max_entry_count_for_testing_;
[email protected]8ff00d72012-10-23 19:12:213622 return kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233623}
3624
[email protected]d202a7c2012-01-04 07:53:473625void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223626 if (is_active && needs_reload_)
3627 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293628}
3629
[email protected]d202a7c2012-01-04 07:53:473630void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293631 if (!needs_reload_)
3632 return;
3633
Bo Liucdfa4b12018-11-06 00:21:443634 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3635 needs_reload_type_);
3636
initial.commit09911bf2008-07-26 23:55:293637 // Calling Reload() results in ignoring state, and not loading.
3638 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3639 // cached state.
avicc872d7242015-08-19 21:26:343640 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163641 NavigateToExistingPendingEntry(ReloadType::NONE,
3642 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343643 } else if (last_committed_entry_index_ != -1) {
arthursonzogni5c4c202d2017-04-25 23:41:273644 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343645 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163646 NavigateToExistingPendingEntry(ReloadType::NONE,
3647 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343648 } else {
3649 // If there is something to reload, the successful reload will clear the
3650 // |needs_reload_| flag. Otherwise, just do it here.
3651 needs_reload_ = false;
3652 }
initial.commit09911bf2008-07-26 23:55:293653}
3654
Carlos IL42b416592019-10-07 23:10:363655void NavigationControllerImpl::LoadPostCommitErrorPage(
3656 RenderFrameHost* render_frame_host,
3657 const GURL& url,
3658 const std::string& error_page_html,
3659 net::Error error) {
Sreeja Kamishetty0be2ac52020-05-28 01:47:463660 // Only active frames can load post-commit error pages:
3661 // - If the frame is in pending deletion, the browser already committed to
3662 // destroying this RenderFrameHost so ignore loading the error page.
3663 // - If the frame is in back-forward cache, it's not allowed to navigate as it
3664 // should remain frozen. Ignore the request and evict the document from
3665 // back-forward cache.
3666 if (static_cast<RenderFrameHostImpl*>(render_frame_host)
3667 ->IsInactiveAndDisallowReactivation()) {
John Delaney56f73332019-11-04 19:39:253668 return;
Sreeja Kamishetty0be2ac52020-05-28 01:47:463669 }
Rakina Zata Amni919b7922020-12-11 09:03:133670 RenderFrameHostImpl* rfhi =
3671 static_cast<RenderFrameHostImpl*>(render_frame_host);
3672 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:413673
3674 mojom::CommonNavigationParamsPtr common_params =
3675 CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:593676 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
3677 // the frame actually committed (e.g. iframe with "src" set to a
3678 // slow-responding URL). We should rewrite the URL to about:blank in this
3679 // case, as the renderer will only think a page is an error page if it has a
3680 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:133681 common_params->url = url.is_empty() ? GURL("about:blank") : url;
John Delaney131ad362019-08-08 21:57:413682 mojom::CommitNavigationParamsPtr commit_params =
3683 CreateCommitNavigationParams();
3684
arthursonzogni70ac7302020-05-28 08:49:053685 // Error pages have a fully permissive FramePolicy.
3686 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
3687 // error pages.
3688 commit_params->frame_policy = blink::FramePolicy();
3689
John Delaney131ad362019-08-08 21:57:413690 std::unique_ptr<NavigationRequest> navigation_request =
3691 NavigationRequest::CreateBrowserInitiated(
3692 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083693 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:193694 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453695 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:063696 "" /* extra_headers */, nullptr /* frame_entry */,
3697 nullptr /* entry */, nullptr /* post_body */,
3698 nullptr /* navigation_ui_data */, base::nullopt /* impression */);
Carlos IL42b416592019-10-07 23:10:363699 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:413700 navigation_request->set_net_error(error);
3701 node->CreatedNavigationRequest(std::move(navigation_request));
3702 DCHECK(node->navigation_request());
3703 node->navigation_request()->BeginNavigation();
3704}
3705
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573706void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213707 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093708 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:153709 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143710 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293711}
3712
[email protected]d202a7c2012-01-04 07:53:473713void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363714 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553715 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363716 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293717
initial.commit09911bf2008-07-26 23:55:293718 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293719}
[email protected]765b35502008-08-21 00:51:203720
arthursonzogni69a6a1b2019-09-17 09:23:003721void NavigationControllerImpl::DiscardNonCommittedEntries() {
Michael Thiessen9b14d512019-09-23 21:19:473722 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:103723 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
3724 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:293725 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:473726 return;
Michael Thiessenc5676d22019-09-25 22:32:103727 }
avi45a72532015-04-07 21:01:453728 DiscardPendingEntry(false);
arthursonzogni69a6a1b2019-09-17 09:23:003729 if (delegate_)
3730 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:483731}
3732
avi7c6f35e2015-05-08 17:52:383733int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3734 int nav_entry_id) const {
3735 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3736 if (entries_[i]->GetUniqueID() == nav_entry_id)
3737 return i;
3738 }
3739 return -1;
3740}
3741
[email protected]d202a7c2012-01-04 07:53:473742void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573743 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253744 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573745 DCHECK_LE(max_index, source->GetEntryCount());
[email protected]e1cd5452010-08-26 18:03:253746 for (int i = 0; i < max_index; i++) {
Nasko Oskov923ca482020-12-04 21:20:463747 // TODO(creis): Once we start sharing FrameNavigationEntries between
3748 // NavigationEntries, it will not be safe to share them with another tab.
3749 // Must have a version of Clone that recreates them.
3750 entries_.insert(entries_.begin() + i, source->entries_[i]->Clone());
[email protected]e1cd5452010-08-26 18:03:253751 }
arthursonzogni5c4c202d2017-04-25 23:41:273752 DCHECK(pending_entry_index_ == -1 ||
3753 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:253754}
[email protected]c5b88d82012-10-06 17:03:333755
3756void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:183757 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:333758 get_timestamp_callback_ = get_timestamp_callback;
3759}
[email protected]8ff00d72012-10-23 19:12:213760
Shivani Sharmaffb32b82019-04-09 16:58:473761// History manipulation intervention:
3762void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:473763 bool replace_entry,
3764 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:403765 bool is_renderer_initiated,
3766 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:453767 // Note that for a subframe, previous_document_was_activated is true if the
3768 // gesture happened in any subframe (propagated to main frame) or in the main
3769 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:473770 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:273771 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:473772 if (last_committed_entry_index_ != -1) {
shivanigithube92c33da2020-09-14 13:01:413773 // This histogram always counts when navigating away from an entry,
3774 // irrespective of whether the skippable flag was changed or not, and
3775 // whether this entry is being reused or not.
Shivani Sharmaffb32b82019-04-09 16:58:473776 UMA_HISTOGRAM_BOOLEAN(
shivanigithube92c33da2020-09-14 13:01:413777 "Navigation.BackForward.SetShouldSkipOnBackForwardUI",
3778 GetEntryAtIndex(last_committed_entry_index_)
3779 ->should_skip_on_back_forward_ui());
Shivani Sharmaffb32b82019-04-09 16:58:473780 }
3781 return;
3782 }
3783 if (last_committed_entry_index_ == -1)
3784 return;
3785
Shivani Sharmac4cc8922019-04-18 03:11:173786 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473787 UMA_HISTOGRAM_BOOLEAN("Navigation.BackForward.SetShouldSkipOnBackForwardUI",
3788 true);
3789
Alexander Timine3ec4192020-04-20 16:39:403790 // Log UKM with the URL we are navigating away from.
3791 ukm::builders::HistoryManipulationIntervention(
3792 previous_page_load_ukm_source_id)
3793 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:473794}
3795
Shivani Sharmac4cc8922019-04-18 03:11:173796void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
3797 int reference_index,
3798 bool skippable) {
3799 auto* reference_entry = GetEntryAtIndex(reference_index);
3800 reference_entry->set_should_skip_on_back_forward_ui(skippable);
3801
3802 int64_t document_sequence_number =
3803 reference_entry->root_node()->frame_entry->document_sequence_number();
3804 for (int index = 0; index < GetEntryCount(); index++) {
3805 auto* entry = GetEntryAtIndex(index);
3806 if (entry->root_node()->frame_entry->document_sequence_number() ==
3807 document_sequence_number) {
3808 entry->set_should_skip_on_back_forward_ui(skippable);
3809 }
3810 }
3811}
3812
arthursonzogni66f711c2019-10-08 14:40:363813std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
3814NavigationControllerImpl::ReferencePendingEntry() {
3815 DCHECK(pending_entry_);
3816 auto pending_entry_ref =
3817 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
3818 pending_entry_refs_.insert(pending_entry_ref.get());
3819 return pending_entry_ref;
3820}
3821
3822void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
3823 // Ignore refs that don't correspond to the current pending entry.
3824 auto it = pending_entry_refs_.find(ref);
3825 if (it == pending_entry_refs_.end())
3826 return;
3827 pending_entry_refs_.erase(it);
3828
3829 if (!pending_entry_refs_.empty())
3830 return;
3831
3832 // The pending entry may be deleted before the last PendingEntryRef.
3833 if (!pending_entry_)
3834 return;
3835
3836 // We usually clear the pending entry when the matching NavigationRequest
3837 // fails, so that an arbitrary URL isn't left visible above a committed page.
3838 //
3839 // However, we do preserve the pending entry in some cases, such as on the
3840 // initial navigation of an unmodified blank tab. We also allow the delegate
3841 // to say when it's safe to leave aborted URLs in the omnibox, to let the
3842 // user edit the URL and try again. This may be useful in cases that the
3843 // committed page cannot be attacker-controlled. In these cases, we still
3844 // allow the view to clear the pending entry and typed URL if the user
3845 // requests (e.g., hitting Escape with focus in the address bar).
3846 //
3847 // Do not leave the pending entry visible if it has an invalid URL, since this
3848 // might be formatted in an unexpected or unsafe way.
3849 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:363850 bool should_preserve_entry =
3851 (pending_entry_ == GetVisibleEntry()) &&
3852 pending_entry_->GetURL().is_valid() &&
3853 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
3854 if (should_preserve_entry)
3855 return;
3856
3857 DiscardPendingEntry(true);
3858 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3859}
3860
Antonio Sartori78a749f2020-11-30 12:03:393861std::unique_ptr<PolicyContainerHost::DocumentPolicies>
3862NavigationControllerImpl::ComputeDocumentPoliciesForFrameEntry(
3863 RenderFrameHostImpl* rfh,
3864 bool is_same_document,
3865 NavigationRequest* request) {
3866 if (!ShouldStoreDocumentPoliciesInFrameNavigationEntry(request))
3867 return nullptr;
3868
3869 if (is_same_document) {
3870 FrameNavigationEntry* previous_frame_entry =
3871 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
3872
3873 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
3874 // ensure we always have a FrameNavigationEntry here.
3875 if (!previous_frame_entry)
3876 return nullptr;
3877
3878 const PolicyContainerHost::DocumentPolicies* previous_document_policies =
3879 previous_frame_entry->document_policies();
3880
3881 if (!previous_document_policies)
3882 return nullptr;
3883
3884 // Make a copy of the policy container for the new FrameNavigationEntry.
3885 return std::make_unique<PolicyContainerHost::DocumentPolicies>(
3886 *previous_document_policies);
3887 }
3888
3889 if (!request->IsWaitingToCommit()) {
3890 // This is the initial, "fake" navigation to about:blank. The
3891 // NavigationRequest contains a dummy policy container, while the
3892 // RenderFrameHost already inherited the policy container from the
3893 // creator, so let's take the policies from there.
3894 return std::make_unique<PolicyContainerHost::DocumentPolicies>(
3895 rfh->policy_container_host()->document_policies());
3896 }
3897
3898 // Take the policy container from the request since we did not move it
3899 // into the RFH yet.
3900 return std::make_unique<PolicyContainerHost::DocumentPolicies>(
3901 request->policy_container_host()->document_policies());
3902}
3903
[email protected]8ff00d72012-10-23 19:12:213904} // namespace content