blob: 897a33e14e16c6e4e38fe2ecbe4ee570d8acf211 [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"
danakjd412a3e2021-02-05 22:36:3543#include "base/debug/dump_without_crashing.h"
initial.commit09911bf2008-07-26 23:55:2944#include "base/logging.h"
asvitkine30330812016-08-30 04:01:0845#include "base/metrics/histogram_macros.h"
[email protected]348fbaac2013-06-11 06:31:5146#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0047#include "base/strings/utf_string_conversions.h"
[email protected]a43858f2013-06-28 15:18:3748#include "base/time/time.h"
Carlos Caballero40b0efd2021-01-26 11:55:0049#include "base/trace_event/optional_trace_event.h"
Carlos Caballeroede6f8c2021-01-28 11:01:5050#include "base/trace_event/trace_conversion_helper.h"
ssid3e765612015-01-28 04:03:4251#include "base/trace_event/trace_event.h"
servolkf3955532015-05-16 00:01:5952#include "build/build_config.h"
[email protected]eabfe1912014-05-12 10:07:2853#include "cc/base/switches.h"
jamescookda2505812015-03-20 18:01:1854#include "content/browser/bad_message.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3955#include "content/browser/blob_storage/chrome_blob_storage_context.h"
[email protected]825b1662012-03-12 19:07:3156#include "content/browser/browser_url_handler_impl.h"
[email protected]5f2aa722013-08-07 16:59:4157#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0458#include "content/browser/dom_storage/session_storage_namespace_impl.h"
danakjc492bf82020-09-09 20:02:4459#include "content/browser/renderer_host/debug_urls.h"
60#include "content/browser/renderer_host/frame_tree.h"
Carlos Caballero40b0efd2021-01-26 11:55:0061#include "content/browser/renderer_host/frame_tree_node.h"
danakjc492bf82020-09-09 20:02:4462#include "content/browser/renderer_host/navigation_entry_impl.h"
63#include "content/browser/renderer_host/navigation_request.h"
64#include "content/browser/renderer_host/navigator.h"
65#include "content/browser/renderer_host/render_frame_host_delegate.h"
Carlos Caballero40b0efd2021-01-26 11:55:0066#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]b6583592012-01-25 19:52:3367#include "content/browser/site_instance_impl.h"
Tsuyoshi Horo37493af2019-11-21 23:43:1468#include "content/browser/web_package/web_bundle_navigation_info.h"
Camille Lamy5193caa2018-10-12 11:59:4269#include "content/common/content_constants_internal.h"
[email protected]37567b432014-02-12 01:12:2270#include "content/common/frame_messages.h"
Nasko Oskovae49e292020-08-13 02:08:5171#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1172#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1973#include "content/public/browser/content_browser_client.h"
[email protected]d9083482012-01-06 00:38:4674#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0075#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3876#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1677#include "content/public/browser/notification_types.h"
Lei Zhang96031532019-10-10 19:05:4778#include "content/public/browser/render_view_host.h"
[email protected]9677a3c2012-12-22 04:18:5879#include "content/public/browser/render_widget_host.h"
80#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1081#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3482#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:1983#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:3884#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:1085#include "content/public/common/content_features.h"
Lei Zhang96031532019-10-10 19:05:4786#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4387#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:5988#include "media/base/mime_util.h"
[email protected]a23de8572009-06-03 02:16:3289#include "net/base/escape.h"
Arthur Sonzogni620cec62018-12-13 13:08:5790#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:1891#include "services/metrics/public/cpp/ukm_builders.h"
92#include "services/metrics/public/cpp/ukm_recorder.h"
Yue Ru Sun128804932020-09-30 22:19:1793#include "services/metrics/public/cpp/ukm_source_id.h"
[email protected]9677a3c2012-12-22 04:18:5894#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3995#include "third_party/blink/public/common/blob/blob_utils.h"
Blink Reformata30d4232018-04-07 15:31:0696#include "third_party/blink/public/common/mime_util/mime_util.h"
Carlos Caballeroede6f8c2021-01-28 11:01:5097#include "third_party/blink/public/common/page_state/page_state_serialization.h"
[email protected]cca6f392014-05-28 21:32:2698#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:2999
[email protected]8ff00d72012-10-23 19:12:21100namespace content {
[email protected]e9ba4472008-09-14 15:42:43101namespace {
102
103// Invoked when entries have been pruned, or removed. For example, if the
104// current entries are [google, digg, yahoo], with the current entry google,
105// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47106void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50107 int index,
[email protected]c12bf1a12008-09-17 16:28:49108 int count) {
[email protected]8ff00d72012-10-23 19:12:21109 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50110 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49111 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14112 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43113}
114
[email protected]e9ba4472008-09-14 15:42:43115// Configure all the NavigationEntries in entries for restore. This resets
116// the transition type to reload and makes sure the content state isn't empty.
117void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57118 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48119 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47120 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43121 // Use a transition type of reload so that we don't incorrectly increase
122 // the typed count.
Lei Zhang96031532019-10-10 19:05:47123 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
124 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43125 }
126}
127
[email protected]bf70edce2012-06-20 22:32:22128// Determines whether or not we should be carrying over a user agent override
129// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57130bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22131 return last_entry && last_entry->GetIsOverridingUserAgent();
132}
133
Camille Lamy5193caa2018-10-12 11:59:42134// Determines whether to override user agent for a navigation.
135bool ShouldOverrideUserAgent(
136 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57137 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42138 switch (override_user_agent) {
139 case NavigationController::UA_OVERRIDE_INHERIT:
140 return ShouldKeepOverride(last_committed_entry);
141 case NavigationController::UA_OVERRIDE_TRUE:
142 return true;
143 case NavigationController::UA_OVERRIDE_FALSE:
144 return false;
Camille Lamy5193caa2018-10-12 11:59:42145 }
146 NOTREACHED();
147 return false;
148}
149
clamy0a656e42018-02-06 18:18:28150// Returns true this navigation should be treated as a reload. For e.g.
151// navigating to the last committed url via the address bar or clicking on a
152// link which results in a navigation to the last committed or pending
153// navigation, etc.
Fergal Daly766177d2020-07-07 07:54:04154// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
155// |base_url_for_data_url|, |transition_type| correspond to the new navigation
156// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
157// navigation that committed.
158bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
159 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57160 const GURL& virtual_url,
161 const GURL& base_url_for_data_url,
162 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57163 bool is_post,
164 bool is_reload,
165 bool is_navigation_to_existing_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57166 NavigationEntryImpl* last_committed_entry) {
Fergal Daly766177d2020-07-07 07:54:04167 if (is_reload || is_navigation_to_existing_entry)
clamy0a656e42018-02-06 18:18:28168 return false;
clamy0a656e42018-02-06 18:18:28169 // Only convert to reload if at least one navigation committed.
170 if (!last_committed_entry)
ananta3bdd8ae2016-12-22 17:11:55171 return false;
172
arthursonzogni7a8243682017-12-14 16:41:42173 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28174 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42175 return false;
176
ananta3bdd8ae2016-12-22 17:11:55177 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
178 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
179 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28180 bool transition_type_can_be_converted = false;
181 if (ui::PageTransitionCoreTypeIs(transition_type,
182 ui::PAGE_TRANSITION_RELOAD) &&
183 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
184 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55185 }
clamy0a656e42018-02-06 18:18:28186 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55187 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28188 transition_type_can_be_converted = true;
189 }
190 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
191 transition_type_can_be_converted = true;
192 if (!transition_type_can_be_converted)
193 return false;
194
195 // This check is required for cases like view-source:, etc. Here the URL of
196 // the navigation entry would contain the url of the page, while the virtual
197 // URL contains the full URL including the view-source prefix.
198 if (virtual_url != last_committed_entry->GetVirtualURL())
199 return false;
200
Fergal Daly766177d2020-07-07 07:54:04201 // Check that the URLs match.
202 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
203 // If there's no frame entry then by definition the URLs don't match.
204 if (!frame_entry)
205 return false;
206
207 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28208 return false;
209
210 // This check is required for Android WebView loadDataWithBaseURL. Apps
211 // can pass in anything in the base URL and we need to ensure that these
212 // match before classifying it as a reload.
213 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
214 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
215 return false;
ananta3bdd8ae2016-12-22 17:11:55216 }
217
clamy0a656e42018-02-06 18:18:28218 // Skip entries with SSL errors.
219 if (last_committed_entry->ssl_error())
220 return false;
221
222 // Don't convert to a reload when the last navigation was a POST or the new
223 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04224 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28225 return false;
226
227 return true;
ananta3bdd8ae2016-12-22 17:11:55228}
229
Nasko Oskov03912102019-01-11 00:21:32230bool DoesURLMatchOriginForNavigation(
231 const GURL& url,
232 const base::Optional<url::Origin>& origin) {
233 // If there is no origin supplied there is nothing to match. This can happen
234 // for navigations to a pending entry and therefore it should be allowed.
235 if (!origin)
236 return true;
237
238 return origin->CanBeDerivedFrom(url);
239}
240
241base::Optional<url::Origin> GetCommittedOriginForFrameEntry(
arthursonzogni73fe3212020-11-17 13:24:07242 const mojom::DidCommitProvisionalLoadParams& params) {
Nasko Oskov03912102019-01-11 00:21:32243 // Error pages commit in an opaque origin, yet have the real URL that resulted
244 // in an error as the |params.url|. Since successful reload of an error page
245 // should commit in the correct origin, setting the opaque origin on the
246 // FrameNavigationEntry will be incorrect.
247 if (params.url_is_unreachable)
248 return base::nullopt;
249
250 return base::make_optional(params.origin);
251}
252
Camille Lamy5193caa2018-10-12 11:59:42253bool IsValidURLForNavigation(bool is_main_frame,
254 const GURL& virtual_url,
255 const GURL& dest_url) {
256 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
257 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
258 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
259 << virtual_url.possibly_invalid_spec();
260 return false;
261 }
262
263 // Don't attempt to navigate to non-empty invalid URLs.
264 if (!dest_url.is_valid() && !dest_url.is_empty()) {
265 LOG(WARNING) << "Refusing to load invalid URL: "
266 << dest_url.possibly_invalid_spec();
267 return false;
268 }
269
270 // The renderer will reject IPC messages with URLs longer than
271 // this limit, so don't attempt to navigate with a longer URL.
272 if (dest_url.spec().size() > url::kMaxURLChars) {
273 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
274 << " characters.";
275 return false;
276 }
277
Aaron Colwell33109c592020-04-21 21:31:19278 // Reject renderer debug URLs because they should have been handled before
279 // we get to this point. This check handles renderer debug URLs
280 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
281 // provides defense-in-depth if a renderer debug URL manages to get here via
282 // some other path. We want to reject the navigation here so it doesn't
283 // violate assumptions in downstream code.
284 if (IsRendererDebugURL(dest_url)) {
285 LOG(WARNING) << "Refusing to load renderer debug URL: "
286 << dest_url.possibly_invalid_spec();
287 return false;
288 }
289
Camille Lamy5193caa2018-10-12 11:59:42290 return true;
291}
292
Mikel Astizba9cf2fd2017-12-17 10:38:10293// See replaced_navigation_entry_data.h for details: this information is meant
294// to ensure |*output_entry| keeps track of its original URL (landing page in
295// case of server redirects) as it gets replaced (e.g. history.replaceState()),
296// without overwriting it later, for main frames.
297void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57298 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10299 NavigationEntryImpl* output_entry) {
300 if (output_entry->GetReplacedEntryData().has_value())
301 return;
302
303 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57304 data.first_committed_url = replaced_entry->GetURL();
305 data.first_timestamp = replaced_entry->GetTimestamp();
306 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29307 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10308}
309
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51310mojom::NavigationType GetNavigationType(const GURL& old_url,
311 const GURL& new_url,
312 ReloadType reload_type,
313 NavigationEntryImpl* entry,
314 const FrameNavigationEntry& frame_entry,
danakjd83d706d2020-11-25 22:11:12315 bool has_pending_cross_document_commit,
danakjb952ef12021-01-14 19:58:49316 bool is_currently_error_page,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51317 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23318 // Reload navigations
319 switch (reload_type) {
320 case ReloadType::NORMAL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51321 return mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23322 case ReloadType::BYPASSING_CACHE:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51323 return mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23324 case ReloadType::ORIGINAL_REQUEST_URL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51325 return mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23326 case ReloadType::NONE:
327 break; // Fall through to rest of function.
328 }
329
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08330 if (entry->IsRestored()) {
Lei Zhang96031532019-10-10 19:05:47331 return entry->GetHasPostData() ? mojom::NavigationType::RESTORE_WITH_POST
332 : mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23333 }
334
danakjb952ef12021-01-14 19:58:49335 const bool can_be_same_document =
336 // A pending cross-document commit means this navigation will not occur in
337 // the current document, as that document would end up being replaced in
338 // the meantime.
339 !has_pending_cross_document_commit &&
340 // If the current document is an error page, we should always treat it as
341 // a different-document navigation so that we'll attempt to load the
342 // document we're navigating to (and not stay in the current error page).
343 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12344
clamyea99ea12018-05-28 13:54:23345 // History navigations.
346 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12347 return can_be_same_document && is_same_document_history_load
Lei Zhang96031532019-10-10 19:05:47348 ? mojom::NavigationType::HISTORY_SAME_DOCUMENT
349 : mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23350 }
351 DCHECK(!is_same_document_history_load);
352
353 // A same-document fragment-navigation happens when the only part of the url
354 // that is modified is after the '#' character.
355 //
356 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12357 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23358 //
359 // Note: this check is only valid for navigations that are not history
360 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
361 // history navigation from 'A#foo' to 'A#bar' is not a same-document
362 // navigation, but a different-document one. This is why history navigation
363 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47364 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
365 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12366
367 // The one case where we do the wrong thing here and incorrectly choose
368 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
369 // the renderer is a frameset. All frameset navigations should be
370 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
371 // navigation would do the right thing, as it would send it to the browser and
372 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
373 // into this method). But since we can't tell that case here for browser-
374 // initiated navigations, we have to get the renderer involved. In that case
375 // the navigation would be restarted due to the renderer spending a reply of
376 // mojom::CommitResult::RestartCrossDocument.
377
378 return can_be_same_document && is_same_doc
379 ? mojom::NavigationType::SAME_DOCUMENT
380 : mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23381}
382
Camille Lamy5193caa2018-10-12 11:59:42383// Adjusts the original input URL if needed, to get the URL to actually load and
384// the virtual URL, which may differ.
385void RewriteUrlForNavigation(const GURL& original_url,
386 BrowserContext* browser_context,
387 GURL* url_to_load,
388 GURL* virtual_url,
389 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42390 // Allow the browser URL handler to rewrite the URL. This will, for example,
391 // remove "view-source:" from the beginning of the URL to get the URL that
392 // will actually be loaded. This real URL won't be shown to the user, just
393 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31394 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42395 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
396 url_to_load, browser_context, reverse_on_redirect);
397}
398
399#if DCHECK_IS_ON()
400// Helper sanity check function used in debug mode.
401void ValidateRequestMatchesEntry(NavigationRequest* request,
402 NavigationEntryImpl* entry) {
403 if (request->frame_tree_node()->IsMainFrame()) {
404 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
405 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
406 request->common_params().transition, entry->GetTransitionType()));
407 }
408 DCHECK_EQ(request->common_params().should_replace_current_entry,
409 entry->should_replace_entry());
Nasko Oskovc36327d2019-01-03 23:23:04410 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42411 entry->should_clear_history_list());
412 DCHECK_EQ(request->common_params().has_user_gesture,
413 entry->has_user_gesture());
414 DCHECK_EQ(request->common_params().base_url_for_data_url,
415 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04416 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42417 entry->GetCanLoadLocalResources());
418 DCHECK_EQ(request->common_params().started_from_context_menu,
419 entry->has_started_from_context_menu());
420
421 FrameNavigationEntry* frame_entry =
422 entry->GetFrameEntry(request->frame_tree_node());
423 if (!frame_entry) {
424 NOTREACHED();
425 return;
426 }
427
Camille Lamy5193caa2018-10-12 11:59:42428 DCHECK_EQ(request->common_params().method, frame_entry->method());
429
Nasko Oskovc36327d2019-01-03 23:23:04430 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42431 if (redirect_size == frame_entry->redirect_chain().size()) {
432 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04433 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42434 frame_entry->redirect_chain()[i]);
435 }
436 } else {
437 NOTREACHED();
438 }
439}
440#endif // DCHECK_IS_ON()
441
Dave Tapuska8bfd84c2019-03-26 20:47:16442// Returns whether the session history NavigationRequests in |navigations|
443// would stay within the subtree of the sandboxed iframe in
444// |sandbox_frame_tree_node_id|.
445bool DoesSandboxNavigationStayWithinSubtree(
446 int sandbox_frame_tree_node_id,
447 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
448 for (auto& item : navigations) {
449 bool within_subtree = false;
450 // Check whether this NavigationRequest affects a frame within the
451 // sandboxed frame's subtree by walking up the tree looking for the
452 // sandboxed frame.
453 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03454 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16455 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
456 within_subtree = true;
457 break;
458 }
459 }
460 if (!within_subtree)
461 return false;
462 }
463 return true;
464}
465
Titouan Rigoudy6ec70402021-02-02 15:42:19466bool ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(
Antonio Sartori78a749f2020-11-30 12:03:39467 const NavigationRequest* request) {
468 // For local schemes we need to store the policy container in the
469 // FrameNavigationEntry, so that we can reload it in case of history
470 // navigation.
471 //
472 // TODO(https://crbug.com/1146361 and https://crbug.com/1146362): blob: and
473 // filesystem: should be removed from this list when we have properly
474 // implemented storing their policy container in the respective store.
475 return (request->common_params().url.SchemeIs(url::kAboutScheme) ||
476 request->common_params().url.SchemeIs(url::kDataScheme) ||
477 request->common_params().url.SchemeIsBlob() ||
478 request->common_params().url.SchemeIsFileSystem());
479}
480
[email protected]e9ba4472008-09-14 15:42:43481} // namespace
482
arthursonzogni66f711c2019-10-08 14:40:36483// NavigationControllerImpl::PendingEntryRef------------------------------------
484
485NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
486 base::WeakPtr<NavigationControllerImpl> controller)
487 : controller_(controller) {}
488
489NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
490 if (!controller_) // Can be null with interstitials.
491 return;
492
493 controller_->PendingEntryRefDeleted(this);
494}
495
[email protected]d202a7c2012-01-04 07:53:47496// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29497
[email protected]23a918b2014-07-15 09:51:36498const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23499
[email protected]765b35502008-08-21 00:51:20500// static
[email protected]d202a7c2012-01-04 07:53:47501size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23502 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20503
[email protected]e6fec472013-05-14 05:29:02504// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20505// when testing.
506static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29507
[email protected]71fde352011-12-29 03:29:56508// static
dcheng9bfa5162016-04-09 01:00:57509std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
510 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10511 Referrer referrer,
512 base::Optional<url::Origin> initiator_origin,
513 ui::PageTransition transition,
514 bool is_renderer_initiated,
515 const std::string& extra_headers,
516 BrowserContext* browser_context,
517 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
518 return NavigationControllerImpl::CreateNavigationEntry(
519 url, referrer, std::move(initiator_origin),
520 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Rakina Zata Amni66992a82020-06-24 03:57:52521 extra_headers, browser_context, std::move(blob_url_loader_factory),
Scott Violet5ae6c42e2020-10-28 02:47:37522 false /* should_replace_entry */, nullptr /* web_contents */);
Lukasz Anforowicz641234d52019-11-07 21:07:10523}
524
525// static
526std::unique_ptr<NavigationEntryImpl>
527NavigationControllerImpl::CreateNavigationEntry(
528 const GURL& url,
529 Referrer referrer,
530 base::Optional<url::Origin> initiator_origin,
531 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57532 ui::PageTransition transition,
533 bool is_renderer_initiated,
534 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09535 BrowserContext* browser_context,
Rakina Zata Amni66992a82020-06-24 03:57:52536 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Scott Violet5ae6c42e2020-10-28 02:47:37537 bool should_replace_entry,
538 WebContents* web_contents) {
Camille Lamy5193caa2018-10-12 11:59:42539 GURL url_to_load;
540 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56541 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42542 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
543 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56544
Lukasz Anforowicz641234d52019-11-07 21:07:10545 // Let the NTP override the navigation params and pretend that this is a
546 // browser-initiated, bookmark-like navigation.
547 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violet5ae6c42e2020-10-28 02:47:37548 web_contents, source_site_instance, &transition, &is_renderer_initiated,
549 &referrer, &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10550
Patrick Monettef507e982019-06-19 20:18:06551 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28552 nullptr, // The site instance for tabs is sent on navigation
553 // (WebContents::GetSiteInstance).
Lukasz Anforowicz435bcb582019-07-12 20:50:06554 url_to_load, referrer, initiator_origin, base::string16(), transition,
Camille Lamy5193caa2018-10-12 11:59:42555 is_renderer_initiated, blob_url_loader_factory);
556 entry->SetVirtualURL(virtual_url);
557 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56558 entry->set_update_virtual_url_with_url(reverse_on_redirect);
559 entry->set_extra_headers(extra_headers);
Rakina Zata Amni66992a82020-06-24 03:57:52560 entry->set_should_replace_entry(should_replace_entry);
Patrick Monettef507e982019-06-19 20:18:06561 return entry;
[email protected]71fde352011-12-29 03:29:56562}
563
[email protected]cdcb1dee2012-01-04 00:46:20564// static
565void NavigationController::DisablePromptOnRepost() {
566 g_check_for_repost = false;
567}
568
[email protected]c5b88d82012-10-06 17:03:33569base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
570 base::Time t) {
571 // If |t| is between the water marks, we're in a run of duplicates
572 // or just getting out of it, so increase the high-water mark to get
573 // a time that probably hasn't been used before and return it.
574 if (low_water_mark_ <= t && t <= high_water_mark_) {
575 high_water_mark_ += base::TimeDelta::FromMicroseconds(1);
576 return high_water_mark_;
577 }
578
579 // Otherwise, we're clear of the last duplicate run, so reset the
580 // water marks.
581 low_water_mark_ = high_water_mark_ = t;
582 return t;
583}
584
ckitagawa0faa5e42020-06-17 17:30:54585NavigationControllerImpl::ScopedShowRepostDialogForTesting::
586 ScopedShowRepostDialogForTesting()
587 : was_disallowed_(g_check_for_repost) {
588 g_check_for_repost = true;
589}
590
591NavigationControllerImpl::ScopedShowRepostDialogForTesting::
592 ~ScopedShowRepostDialogForTesting() {
593 g_check_for_repost = was_disallowed_;
594}
595
[email protected]d202a7c2012-01-04 07:53:47596NavigationControllerImpl::NavigationControllerImpl(
Carlos Caballero40b0efd2021-01-26 11:55:00597 BrowserContext* browser_context,
598 FrameTree& frame_tree,
599 NavigationControllerDelegate* delegate)
600 : frame_tree_(frame_tree),
601 browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57602 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22603 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47604 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37605 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29606}
607
[email protected]d202a7c2012-01-04 07:53:47608NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00609 // The NavigationControllerImpl might be called inside its delegate
610 // destructor. Calling it is not valid anymore.
611 delegate_ = nullptr;
612 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29613}
614
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57615WebContents* NavigationControllerImpl::GetWebContents() {
[email protected]ec6c05f2013-10-23 18:41:57616 return delegate_->GetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32617}
618
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57619BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55620 return browser_context_;
621}
622
[email protected]d202a7c2012-01-04 07:53:47623void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30624 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36625 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57626 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57627 // Verify that this controller is unused and that the input is valid.
Lei Zhang96031532019-10-10 19:05:47628 DCHECK_EQ(0, GetEntryCount());
629 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57630 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14631 selected_navigation < static_cast<int>(entries->size()));
arthursonzogni5c4c202d2017-04-25 23:41:27632 DCHECK_EQ(-1, pending_entry_index_);
[email protected]ce3fa3c2009-04-20 19:55:57633
[email protected]ce3fa3c2009-04-20 19:55:57634 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44635 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03636 entries_.reserve(entries->size());
637 for (auto& entry : *entries)
dcheng36b6aec92015-12-26 06:16:36638 entries_.push_back(
639 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
avif16f85a72015-11-13 18:25:03640
641 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
642 // cleared out safely.
643 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57644
645 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36646 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57647}
648
toyoshim6142d96f2016-12-19 09:07:25649void NavigationControllerImpl::Reload(ReloadType reload_type,
650 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28651 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28652 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32653 int current_index = -1;
654
Carlos IL42b416592019-10-07 23:10:36655 if (entry_replaced_by_post_commit_error_) {
656 // If there is an entry that was replaced by a currently active post-commit
657 // error navigation, this can't be the initial navigation.
658 DCHECK(!IsInitialNavigation());
659 // If the current entry is a post commit error, we reload the entry it
660 // replaced instead. We leave the error entry in place until a commit
661 // replaces it, but the pending entry points to the original entry in the
662 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
663 // case that pending_entry_ != entries_[pending_entry_index_].
664 entry = entry_replaced_by_post_commit_error_.get();
665 current_index = GetCurrentEntryIndex();
666 } else if (IsInitialNavigation() && pending_entry_) {
667 // If we are reloading the initial navigation, just use the current
668 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32669 entry = pending_entry_;
670 // The pending entry might be in entries_ (e.g., after a Clone), so we
671 // should also update the current_index.
672 current_index = pending_entry_index_;
673 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00674 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32675 current_index = GetCurrentEntryIndex();
676 if (current_index != -1) {
creis3da03872015-02-20 21:12:32677 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32678 }
[email protected]979a4bc2013-04-24 01:27:15679 }
[email protected]241db352013-04-22 18:04:05680
[email protected]59167c22013-06-03 18:07:32681 // If we are no where, then we can't reload. TODO(darin): We should add a
682 // CanReload method.
683 if (!entry)
684 return;
685
Takashi Toyoshimac7df3c22019-06-25 14:18:47686 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26687 entry->set_reload_type(reload_type);
688
Aran Gilman37d11632019-10-08 23:07:15689 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30690 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07691 // they really want to do this. If they do, the dialog will call us back
692 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57693 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02694
[email protected]106a0812010-03-18 00:15:12695 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57696 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47697 return;
initial.commit09911bf2008-07-26 23:55:29698 }
Lei Zhang96031532019-10-10 19:05:47699
700 if (!IsInitialNavigation())
701 DiscardNonCommittedEntries();
702
703 pending_entry_ = entry;
704 pending_entry_index_ = current_index;
705 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
706
707 NavigateToExistingPendingEntry(reload_type,
708 FrameTreeNode::kFrameTreeNodeInvalidId);
initial.commit09911bf2008-07-26 23:55:29709}
710
[email protected]d202a7c2012-01-04 07:53:47711void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48712 DCHECK(pending_reload_ != ReloadType::NONE);
713 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12714}
715
[email protected]d202a7c2012-01-04 07:53:47716void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48717 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12718 NOTREACHED();
719 } else {
toyoshim6142d96f2016-12-19 09:07:25720 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48721 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12722 }
723}
724
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57725bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09726 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11727}
728
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57729bool NavigationControllerImpl::IsInitialBlankNavigation() {
creis10a4ab72015-10-13 17:22:40730 // TODO(creis): Once we create a NavigationEntry for the initial blank page,
toyoshim0df1d3a2016-09-09 09:52:48731 // we'll need to check for entry count 1 and restore_type NONE (to exclude
732 // the cloned tab case).
creis10a4ab72015-10-13 17:22:40733 return IsInitialNavigation() && GetEntryCount() == 0;
734}
735
Aran Gilman37d11632019-10-08 23:07:15736NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
737 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58738 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03739 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58740}
741
W. James MacLean1c40862c2020-04-27 21:05:57742void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
743 const url::Origin& origin) {
744 for (int i = 0; i < GetEntryCount(); i++) {
745 auto* entry = GetEntryAtIndex(i);
746 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
747 }
748 if (entry_replaced_by_post_commit_error_) {
749 // It's possible we could come back to this entry if the error
750 // page/interstitial goes away.
751 entry_replaced_by_post_commit_error_
752 ->RegisterExistingOriginToPreventOptInIsolation(origin);
753 }
754 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
755 // visiting pending_entry_, which lacks a committed origin. This will be done
756 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
757}
758
avi25764702015-06-23 15:43:37759void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57760 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00761 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37762 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27763 DCHECK_EQ(-1, pending_entry_index_);
[email protected]8ff00d72012-10-23 19:12:21764 NotificationService::current()->Notify(
Aran Gilman37d11632019-10-08 23:07:15765 NOTIFICATION_NAV_ENTRY_PENDING, Source<NavigationController>(this),
avi25764702015-06-23 15:43:37766 Details<NavigationEntry>(pending_entry_));
[email protected]765b35502008-08-21 00:51:20767}
768
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57769NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47770 if (pending_entry_)
771 return pending_entry_;
772 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20773}
774
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57775NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32776 // The pending entry is safe to return for new (non-history), browser-
777 // initiated navigations. Most renderer-initiated navigations should not
778 // show the pending entry, to prevent URL spoof attacks.
779 //
780 // We make an exception for renderer-initiated navigations in new tabs, as
781 // long as no other page has tried to access the initial empty document in
782 // the new tab. If another page modifies this blank page, a URL spoof is
783 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32784 bool safe_to_show_pending =
785 pending_entry_ &&
786 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09787 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32788 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46789 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32790
791 // Also allow showing the pending entry for history navigations in a new tab,
792 // such as Ctrl+Back. In this case, no existing page is visible and no one
793 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15794 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
795 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32796 safe_to_show_pending = true;
797
798 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19799 return pending_entry_;
800 return GetLastCommittedEntry();
801}
802
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57803int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20804 if (pending_entry_index_ != -1)
805 return pending_entry_index_;
806 return last_committed_entry_index_;
807}
808
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57809NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20810 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28811 return nullptr;
avif16f85a72015-11-13 18:25:03812 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20813}
814
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57815bool NavigationControllerImpl::CanViewSource() {
Carlos Caballeroede6f8c2021-01-28 11:01:50816 const std::string& mime_type = frame_tree_.root()
Alex Moshchuk2e470ea2021-02-03 06:46:34817 ->current_frame_host()
818 ->render_view_host()
Carlos Caballeroede6f8c2021-01-28 11:01:50819 ->contents_mime_type();
Kinuko Yasuda74702f92017-07-31 03:27:53820 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
821 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27822 NavigationEntry* visible_entry = GetVisibleEntry();
823 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39824 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16825}
826
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57827int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27828 // The last committed entry index must always be less than the number of
Carlos IL4dea8902020-05-26 15:14:29829 // entries. If there are no entries, it must be -1.
arthursonzogni5c4c202d2017-04-25 23:41:27830 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
831 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55832 return last_committed_entry_index_;
833}
834
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57835int NavigationControllerImpl::GetEntryCount() {
Carlos IL4dea8902020-05-26 15:14:29836 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55837 return static_cast<int>(entries_.size());
838}
839
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57840NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37841 if (index < 0 || index >= GetEntryCount())
842 return nullptr;
843
avif16f85a72015-11-13 18:25:03844 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25845}
846
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57847NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47848 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20849}
850
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57851int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46852 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03853}
854
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57855bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03856 if (!base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention))
857 return CanGoToOffset(-1);
858
859 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
860 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
861 return true;
862 }
863 return false;
[email protected]765b35502008-08-21 00:51:20864}
865
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57866bool NavigationControllerImpl::CanGoForward() {
avi56e40c92015-08-27 00:11:22867 return CanGoToOffset(1);
[email protected]765b35502008-08-21 00:51:20868}
869
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57870bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03871 int index = GetIndexForOffset(offset);
872 return index >= 0 && index < GetEntryCount();
873}
874
[email protected]d202a7c2012-01-04 07:53:47875void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06876 int target_index = GetIndexForOffset(-1);
877
878 // Log metrics for the number of entries that are eligible for skipping on
Shivani Sharma2d5b4b6b2019-01-08 16:07:16879 // back button and move the target index past the skippable entries, if
880 // history intervention is enabled.
shivanisha55201872018-12-13 04:29:06881 int count_entries_skipped = 0;
Shivani Sharma298d12852019-01-22 20:04:03882 bool all_skippable_entries = true;
Aran Gilman37d11632019-10-08 23:07:15883 bool history_intervention_enabled =
884 base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention);
shivanisha55201872018-12-13 04:29:06885 for (int index = target_index; index >= 0; index--) {
Shivani Sharma2d5b4b6b2019-01-08 16:07:16886 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
shivanisha55201872018-12-13 04:29:06887 count_entries_skipped++;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16888 } else {
889 if (history_intervention_enabled)
890 target_index = index;
Shivani Sharma298d12852019-01-22 20:04:03891 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06892 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16893 }
shivanisha55201872018-12-13 04:29:06894 }
895 UMA_HISTOGRAM_ENUMERATION("Navigation.BackForward.BackTargetSkipped",
896 count_entries_skipped, kMaxSessionHistoryEntries);
Shivani Sharma298d12852019-01-22 20:04:03897 UMA_HISTOGRAM_BOOLEAN("Navigation.BackForward.AllBackTargetsSkippable",
898 all_skippable_entries);
899
900 // Do nothing if all entries are skippable. Normally this path would not
901 // happen as consumers would have already checked it in CanGoBack but a lot of
902 // tests do not do that.
903 if (history_intervention_enabled && all_skippable_entries)
904 return;
shivanisha55201872018-12-13 04:29:06905
shivanisha55201872018-12-13 04:29:06906 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20907}
908
[email protected]d202a7c2012-01-04 07:53:47909void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06910 int target_index = GetIndexForOffset(1);
911
912 // Log metrics for the number of entries that are eligible for skipping on
Shivani Sharma2d5b4b6b2019-01-08 16:07:16913 // forward button and move the target index past the skippable entries, if
914 // history intervention is enabled.
915 // Note that at least one entry (the last one) will be non-skippable since
916 // entries are marked skippable only when they add another entry because of
917 // redirect or pushState.
shivanisha55201872018-12-13 04:29:06918 int count_entries_skipped = 0;
Aran Gilman37d11632019-10-08 23:07:15919 bool history_intervention_enabled =
920 base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention);
shivanisha55201872018-12-13 04:29:06921 for (size_t index = target_index; index < entries_.size(); index++) {
Shivani Sharma2d5b4b6b2019-01-08 16:07:16922 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
shivanisha55201872018-12-13 04:29:06923 count_entries_skipped++;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16924 } else {
925 if (history_intervention_enabled)
926 target_index = index;
shivanisha55201872018-12-13 04:29:06927 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16928 }
shivanisha55201872018-12-13 04:29:06929 }
930 UMA_HISTOGRAM_ENUMERATION("Navigation.BackForward.ForwardTargetSkipped",
931 count_entries_skipped, kMaxSessionHistoryEntries);
932
shivanisha55201872018-12-13 04:29:06933 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20934}
935
[email protected]d202a7c2012-01-04 07:53:47936void NavigationControllerImpl::GoToIndex(int index) {
Dave Tapuska8bfd84c2019-03-26 20:47:16937 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId);
938}
939
940void NavigationControllerImpl::GoToIndex(int index,
941 int sandbox_frame_tree_node_id) {
sunjian30574a62017-03-21 21:39:44942 TRACE_EVENT0("browser,navigation,benchmark",
943 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:20944 if (index < 0 || index >= static_cast<int>(entries_.size())) {
945 NOTREACHED();
946 return;
947 }
948
[email protected]cbab76d2008-10-13 22:42:47949 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20950
arthursonzogni5c4c202d2017-04-25 23:41:27951 DCHECK_EQ(nullptr, pending_entry_);
952 DCHECK_EQ(-1, pending_entry_index_);
953 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:20954 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:27955 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
956 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Dave Tapuska8bfd84c2019-03-26 20:47:16957 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id);
[email protected]765b35502008-08-21 00:51:20958}
959
[email protected]d202a7c2012-01-04 07:53:47960void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:12961 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:03962 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:20963 return;
964
[email protected]9ba14052012-06-22 23:50:03965 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20966}
967
[email protected]41374f12013-07-24 02:49:28968bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:15969 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:28970 return false;
[email protected]6a13a6c2011-12-20 21:47:12971
[email protected]43032342011-03-21 14:10:31972 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:28973 return true;
[email protected]cbab76d2008-10-13 22:42:47974}
975
Michael Thiessen9b14d512019-09-23 21:19:47976void NavigationControllerImpl::PruneForwardEntries() {
977 DiscardNonCommittedEntries();
978 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:47979 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:47980 if (num_removed <= 0)
981 return;
982 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
983 NotifyPrunedEntries(this, remove_start_index /* start index */,
984 num_removed /* count */);
985}
986
Aran Gilman37d11632019-10-08 23:07:15987void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
988 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:32989 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:31990 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:51991 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
992 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:32993 }
994}
995
Aran Gilman37d11632019-10-08 23:07:15996void NavigationControllerImpl::LoadURL(const GURL& url,
997 const Referrer& referrer,
998 ui::PageTransition transition,
999 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:471000 LoadURLParams params(url);
1001 params.referrer = referrer;
1002 params.transition_type = transition;
1003 params.extra_headers = extra_headers;
1004 LoadURLWithParams(params);
1005}
1006
1007void NavigationControllerImpl::LoadURLWithParams(const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061008 if (params.is_renderer_initiated)
1009 DCHECK(params.initiator_origin.has_value());
1010
naskob8744d22014-08-28 17:07:431011 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151012 "NavigationControllerImpl::LoadURLWithParams", "url",
1013 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291014 bool is_explicit_navigation =
1015 GetContentClient()->browser()->IsExplicitNavigation(
1016 params.transition_type);
1017 if (HandleDebugURL(params.url, params.transition_type,
1018 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431019 // If Telemetry is running, allow the URL load to proceed as if it's
1020 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491021 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431022 cc::switches::kEnableGpuBenchmarking))
1023 return;
1024 }
[email protected]8bf1048012012-02-08 01:22:181025
[email protected]cf002332012-08-14 19:17:471026 // Checks based on params.load_type.
1027 switch (params.load_type) {
1028 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431029 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471030 break;
1031 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261032 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471033 NOTREACHED() << "Data load must use data scheme.";
1034 return;
1035 }
1036 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461037 }
[email protected]e47ae9472011-10-13 19:48:341038
[email protected]e47ae9472011-10-13 19:48:341039 // The user initiated a load, we don't need to reload anymore.
1040 needs_reload_ = false;
1041
clamy21718cc22018-06-13 13:34:241042 NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441043}
1044
Mohamed Abdelhalim833de902019-09-16 17:41:451045bool NavigationControllerImpl::PendingEntryMatchesRequest(
1046 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191047 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451048 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191049}
1050
[email protected]d202a7c2012-01-04 07:53:471051bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321052 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071053 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331054 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441055 bool is_same_document_navigation,
Shivani Sharmaffb32b82019-04-09 16:58:471056 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131057 NavigationRequest* navigation_request) {
1058 DCHECK(navigation_request);
[email protected]cd2e15742013-03-08 04:08:311059 is_initial_navigation_ = false;
1060
[email protected]0e8db942008-09-24 21:21:481061 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431062 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481063 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361064 if (entry_replaced_by_post_commit_error_) {
1065 if (is_same_document_navigation) {
1066 // Same document navigations should not be possible on error pages and
1067 // would leave the controller in a weird state. Kill the renderer if
1068 // that happens.
1069 bad_message::ReceivedBadMessage(
1070 rfh->GetProcess(), bad_message::NC_SAME_DOCUMENT_POST_COMMIT_ERROR);
1071 }
1072 // Any commit while a post-commit error page is showing should put the
1073 // original entry back, replacing the error page's entry. This includes
1074 // reloads, where the original entry was used as the pending entry and
1075 // should now be at the correct index at commit time.
1076 entries_[last_committed_entry_index_] =
1077 std::move(entry_replaced_by_post_commit_error_);
1078 }
Fergal Daly8e33cf62020-12-12 01:06:071079 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551080 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431081 if (pending_entry_ &&
1082 pending_entry_->GetIsOverridingUserAgent() !=
1083 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1084 overriding_user_agent_changed = true;
[email protected]0e8db942008-09-24 21:21:481085 } else {
Fergal Daly8e33cf62020-12-12 01:06:071086 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481087 details->previous_entry_index = -1;
1088 }
[email protected]ecd9d8702008-08-28 22:10:171089
Alexander Timind2f2e4f22019-04-02 20:04:531090 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1091 // implementing back-forward cache.
1092
1093 // Create a new metrics object or reuse the previous one depending on whether
1094 // it's a main frame navigation or not.
1095 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics =
1096 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1097 GetLastCommittedEntry(), !rfh->GetParent(),
1098 params.document_sequence_number);
1099 // Notify the last active entry that we have navigated away.
1100 if (!rfh->GetParent() && !is_same_document_navigation) {
1101 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1102 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Alexander Timinde527cd2019-12-02 09:41:121103 metrics->MainFrameDidNavigateAwayFromDocument(rfh, details,
1104 navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531105 }
1106 }
1107 }
1108
fdegans9caf66a2015-07-30 21:10:421109 // If there is a pending entry at this point, it should have a SiteInstance,
1110 // except for restored entries.
jam48cea9082017-02-15 06:13:291111 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481112 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081113 pending_entry_->IsRestored());
1114 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151115 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291116 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481117 }
[email protected]e9ba4472008-09-14 15:42:431118
Nasko Oskovaee2f862018-06-15 00:05:521119 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1120 // has changed, this must be treated as a new navigation with replacement.
1121 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491122 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521123 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101124 pending_entry_->GetUniqueID() ==
1125 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521126 pending_entry_->site_instance() &&
1127 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1128 DCHECK_NE(-1, pending_entry_index_);
1129 // TODO(nasko,creis): Instead of setting this value here, set
1130 // should_replace_current_entry on the parameters we send to the
1131 // renderer process as part of CommitNavigation. The renderer should
1132 // in turn send it back here as part of |params| and it can be just
1133 // enforced and renderer process terminated on mismatch.
1134 details->did_replace_entry = true;
1135 } else {
1136 // The renderer tells us whether the navigation replaces the current entry.
1137 details->did_replace_entry = params.should_replace_current_entry;
1138 }
[email protected]bcd904482012-02-01 01:54:221139
[email protected]e9ba4472008-09-14 15:42:431140 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amnif6950d552020-11-24 03:26:101141 details->type = ClassifyNavigation(rfh, params, navigation_request);
[email protected]4bf3522c2010-08-19 21:00:201142
eugenebutee08663a2017-04-27 17:43:121143 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441144 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121145
Peter Boströmd7592132019-01-30 04:50:311146 // Make sure we do not discard the pending entry for a different ongoing
1147 // navigation when a same document commit comes in unexpectedly from the
1148 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1149 // other navigation types. See https://crbug.com/900036.
1150 // TODO(crbug.com/926009): Handle history.pushState() as well.
1151 bool keep_pending_entry = is_same_document_navigation &&
Charlie Reisc0f17d2d2021-01-12 18:52:491152 details->type == NAVIGATION_TYPE_EXISTING_ENTRY &&
Peter Boströmd7592132019-01-30 04:50:311153 pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451154 !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311155
[email protected]0e8db942008-09-24 21:21:481156 switch (details->type) {
Charlie Reisc0f17d2d2021-01-12 18:52:491157 case NAVIGATION_TYPE_NEW_ENTRY:
1158 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051159 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451160 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431161 break;
Charlie Reisc0f17d2d2021-01-12 18:52:491162 case NAVIGATION_TYPE_EXISTING_ENTRY:
1163 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1164 was_restored, navigation_request,
1165 keep_pending_entry);
[email protected]e9ba4472008-09-14 15:42:431166 break;
[email protected]8ff00d72012-10-23 19:12:211167 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471168 RendererDidNavigateNewSubframe(
1169 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451170 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431171 break;
[email protected]8ff00d72012-10-23 19:12:211172 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391173 if (!RendererDidNavigateAutoSubframe(
1174 rfh, params, details->is_same_document, navigation_request)) {
creisce0ef3572017-01-26 17:53:081175 // We don't send a notification about auto-subframe PageState during
1176 // UpdateStateForFrame, since it looks like nothing has changed. Send
1177 // it here at commit time instead.
1178 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431179 return false;
creis59d5a47cb2016-08-24 23:57:191180 }
[email protected]e9ba4472008-09-14 15:42:431181 break;
[email protected]8ff00d72012-10-23 19:12:211182 case NAVIGATION_TYPE_NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:491183 // If a pending navigation was in progress, this canceled it. We should
1184 // discard it and make sure it is removed from the URL bar. After that,
1185 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:431186 // the caller that nothing has happened.
arthursonzogni69a6a1b2019-09-17 09:23:001187 if (pending_entry_)
[email protected]20d1c992011-04-12 21:17:491188 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431189 return false;
Aran Gilman37d11632019-10-08 23:07:151190 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431191 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151192 break;
[email protected]765b35502008-08-21 00:51:201193 }
1194
[email protected]688aa65c62012-09-28 04:32:221195 // At this point, we know that the navigation has just completed, so
1196 // record the time.
1197 //
1198 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261199 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331200 base::Time timestamp =
1201 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1202 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131203 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221204
Peter Boströmd7592132019-01-30 04:50:311205 // If we aren't keeping the pending entry, there shouldn't be one at this
1206 // point. Clear it again in case any error cases above forgot to do so.
1207 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1208 // been cleared instead of protecting against it.
1209 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001210 DiscardNonCommittedEntries();
[email protected]f233e4232013-02-23 00:55:141211
[email protected]e9ba4472008-09-14 15:42:431212 // All committed entries should have nonempty content state so WebKit doesn't
1213 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471214 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321215 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221216 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441217 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531218 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1219 // implementing back-forward cache.
1220 if (!active_entry->back_forward_cache_metrics()) {
1221 active_entry->set_back_forward_cache_metrics(
1222 std::move(back_forward_cache_metrics));
1223 }
1224 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
Hajime Hoshi446206e2019-10-18 18:36:251225 navigation_request,
1226 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
naskoc7533512016-05-06 17:01:121227
Charles Reisc0507202017-09-21 00:40:021228 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1229 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1230 // A mismatch can occur if the renderer lies or due to a unique name collision
1231 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121232 FrameNavigationEntry* frame_entry =
1233 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021234 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1235 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031236 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081237 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1238 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031239 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201240
1241 // Remember the bindings the renderer process has at this point, so that
1242 // we do not grant this entry additional bindings if we come back to it.
1243 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301244 }
[email protected]132e281a2012-07-31 18:32:441245
[email protected]97d8f0d2013-10-29 16:49:211246 // Once it is committed, we no longer need to track several pieces of state on
1247 // the entry.
naskoc7533512016-05-06 17:01:121248 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131249
[email protected]49bd30e62011-03-22 20:12:591250 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221251 // TODO(creis): This check won't pass for subframes until we create entries
1252 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001253 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421254 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591255
[email protected]e9ba4472008-09-14 15:42:431256 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001257 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001258 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311259 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531260
Scott Violetc36f7462020-05-06 23:13:031261 // If the NavigationRequest was created without a NavigationEntry and
1262 // SetIsOverridingUserAgent() was called, it needs to be applied to the
1263 // NavigationEntry now.
1264 if (!navigation_request->nav_entry_id() &&
1265 navigation_request->was_set_overriding_user_agent_called()) {
1266 active_entry->SetIsOverridingUserAgent(
1267 navigation_request->entry_overrides_ua());
1268 }
1269
[email protected]93f230e02011-06-01 14:40:001270 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291271
John Abd-El-Malek380d3c5922017-09-08 00:20:311272 if (active_entry->GetURL().SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451273 navigation_request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek969cdd94e2017-07-10 22:18:161274 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus",
1275 !!active_entry->GetSSL().certificate);
1276 }
1277
aelias100c9192017-01-13 00:01:431278 if (overriding_user_agent_changed)
1279 delegate_->UpdateOverridingUserAgent();
1280
creis03b48002015-11-04 00:54:561281 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1282 // one knows the latest NavigationEntry it is showing (whether it has
1283 // committed anything in this navigation or not). This allows things like
1284 // state and title updates from RenderFrames to apply to the latest relevant
1285 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381286 int nav_entry_id = active_entry->GetUniqueID();
Carlos Caballero40b0efd2021-01-26 11:55:001287 for (FrameTreeNode* node : frame_tree_.Nodes())
dcheng57e39e22016-01-21 00:25:381288 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
[email protected]e9ba4472008-09-14 15:42:431289 return true;
initial.commit09911bf2008-07-26 23:55:291290}
1291
[email protected]8ff00d72012-10-23 19:12:211292NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321293 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101294 const mojom::DidCommitProvisionalLoadParams& params,
1295 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591296 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511297 "ClassifyNavigation");
1298
avi7c6f35e2015-05-08 17:52:381299 if (params.did_create_new_entry) {
Charlie Reisc0f17d2d2021-01-12 18:52:491300 // A new entry. We may or may not have a corresponding pending entry, and
avi7c6f35e2015-05-08 17:52:381301 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001302 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491303 trace_return.set_return_reason("new entry, no parent, new entry");
1304 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381305 }
1306
1307 // When this is a new subframe navigation, we should have a committed page
1308 // in which it's a subframe. This may not be the case when an iframe is
1309 // navigated on a popup navigated to about:blank (the iframe would be
1310 // written into the popup by script on the main page). For these cases,
1311 // there isn't any navigation stuff we can do, so just ignore it.
Nasko Oskovae49e292020-08-13 02:08:511312 if (!GetLastCommittedEntry()) {
1313 trace_return.set_return_reason("new entry, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381314 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511315 }
avi7c6f35e2015-05-08 17:52:381316
1317 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511318 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381319 return NAVIGATION_TYPE_NEW_SUBFRAME;
1320 }
1321
Charlie Reisc0f17d2d2021-01-12 18:52:491322 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381323 DCHECK(!params.history_list_was_cleared);
1324
avi39c1edd32015-06-04 20:06:001325 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381326 // All manual subframes would be did_create_new_entry and handled above, so
1327 // we know this is auto.
Nasko Oskovae49e292020-08-13 02:08:511328 if (GetLastCommittedEntry()) {
1329 trace_return.set_return_reason("subframe, last commmited, auto subframe");
avi7c6f35e2015-05-08 17:52:381330 return NAVIGATION_TYPE_AUTO_SUBFRAME;
Nasko Oskovae49e292020-08-13 02:08:511331 }
Lei Zhang96031532019-10-10 19:05:471332
1333 // We ignore subframes created in non-committed pages; we'd appreciate if
1334 // people stopped doing that.
Nasko Oskovae49e292020-08-13 02:08:511335 trace_return.set_return_reason("subframe, no last commmited, ignore");
Lei Zhang96031532019-10-10 19:05:471336 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381337 }
1338
Rakina Zata Amnif6950d552020-11-24 03:26:101339 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1340 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381341 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1342 // create a new page.
1343
1344 // Just like above in the did_create_new_entry case, it's possible to
1345 // scribble onto an uncommitted page. Again, there isn't any navigation
1346 // stuff that we can do, so ignore it here as well.
avi3a5b8f32015-05-28 17:50:231347 NavigationEntry* last_committed = GetLastCommittedEntry();
Nasko Oskovae49e292020-08-13 02:08:511348 if (!last_committed) {
1349 trace_return.set_return_reason("nav entry 0, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381350 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511351 }
avi7c6f35e2015-05-08 17:52:381352
Charles Reis1378111f2017-11-08 21:44:061353 // This is history.replaceState() or history.reload().
Nasko Oskov332593c2018-08-16 17:21:341354 // TODO(nasko): With error page isolation, reloading an existing session
1355 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491356 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341357 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511358 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491359 "nav entry 0, last committed, existing entry");
1360 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381361 }
1362
Rakina Zata Amnif6950d552020-11-24 03:26:101363 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521364 // If the SiteInstance of the |pending_entry_| does not match the
1365 // SiteInstance that got committed, treat this as a new navigation with
1366 // replacement. This can happen if back/forward/reload encounters a server
1367 // redirect to a different site or an isolated error page gets successfully
1368 // reloaded into a different SiteInstance.
1369 if (pending_entry_->site_instance() &&
1370 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491371 trace_return.set_return_reason("pending matching nav entry, new entry");
1372 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521373 }
creis77c9aa32015-09-25 19:59:421374
Nasko Oskovaee2f862018-06-15 00:05:521375 if (pending_entry_index_ == -1) {
1376 // In this case, we have a pending entry for a load of a new URL but Blink
1377 // didn't do a new navigation (params.did_create_new_entry). First check
1378 // to make sure Blink didn't treat a new cross-process navigation as
1379 // inert, and thus set params.did_create_new_entry to false. In that case,
Charlie Reis7e2cb6d2021-01-26 01:27:161380 // we must treat it as NEW rather than the converted reload case below,
1381 // since the new SiteInstance doesn't match the last committed entry.
Nasko Oskovaee2f862018-06-15 00:05:521382 if (!GetLastCommittedEntry() ||
1383 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reis7e2cb6d2021-01-26 01:27:161384 trace_return.set_return_reason("new pending, new entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491385 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521386 }
1387
1388 // Otherwise, this happens when you press enter in the URL bar to reload.
Charlie Reis7e2cb6d2021-01-26 01:27:161389 // We will create a pending entry, but NavigateWithoutEntry will convert
1390 // it to a reload since it's the same page and not create a new entry for
1391 // it. (The user doesn't want to have a new back/forward entry when they
1392 // do this.) Therefore we want to just ignore the pending entry and go
1393 // back to where we were (the "existing entry").
1394 trace_return.set_return_reason("new pending, existing (same) entry");
1395 return NAVIGATION_TYPE_EXISTING_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521396 }
avi7c6f35e2015-05-08 17:52:381397 }
1398
creis26d22632017-04-21 20:23:561399 // Everything below here is assumed to be an existing entry, but if there is
1400 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511401 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491402 trace_return.set_return_reason("no last committed, new entry");
1403 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511404 }
creis26d22632017-04-21 20:23:561405
avi7c6f35e2015-05-08 17:52:381406 if (params.intended_as_new_entry) {
1407 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491408 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1409 // or may not have a pending entry.
Charlie Reis7e2cb6d2021-01-26 01:27:161410 trace_return.set_return_reason("intended as new entry, existing entry");
Charlie Reisc0f17d2d2021-01-12 18:52:491411 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381412 }
1413
1414 if (params.url_is_unreachable && failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101415 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381416 // If the renderer was going to a new pending entry that got cleared because
1417 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491418 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381419 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511420 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491421 "unreachable, matching pending, existing entry");
1422 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381423 }
1424
Charlie Reisc0f17d2d2021-01-12 18:52:491425 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101426 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511427 trace_return.traced_value()->SetInteger("existing_entry_index",
1428 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381429 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441430 // The renderer has committed a navigation to an entry that no longer
1431 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491432 trace_return.set_return_reason("existing entry -1, new entry");
1433 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381434 }
1435
avi7c6f35e2015-05-08 17:52:381436 // Since we weeded out "new" navigations above, we know this is an existing
1437 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491438 trace_return.set_return_reason("default return, existing entry");
1439 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381440}
1441
Charlie Reisc0f17d2d2021-01-12 18:52:491442void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321443 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071444 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361445 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441446 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471447 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451448 NavigationRequest* request) {
dcheng9bfa5162016-04-09 01:00:571449 std::unique_ptr<NavigationEntryImpl> new_entry;
creisf49dfc92016-07-26 17:05:181450 bool update_virtual_url = false;
1451
Lukasz Anforowicz435bcb582019-07-12 20:50:061452 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451453 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061454
creisf49dfc92016-07-26 17:05:181455 // First check if this is an in-page navigation. If so, clone the current
1456 // entry instead of looking at the pending entry, because the pending entry
1457 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361458 if (is_same_document && GetLastCommittedEntry()) {
Patrick Monette50e8bd82019-06-13 22:40:451459 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481460 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
creisf49dfc92016-07-26 17:05:181461 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Nasko Oskov03912102019-01-11 00:21:321462 params.url, (params.url_is_unreachable) ? nullptr : &params.origin,
arthursonzogni73fe3212020-11-17 13:24:071463 Referrer(*params.referrer), initiator_origin, params.redirects,
1464 params.page_state, params.method, params.post_id,
1465 nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391466 nullptr /* web_bundle_navigation_info */,
1467 // We will set the document policies later in this function.
Titouan Rigoudy6ec70402021-02-02 15:42:191468 nullptr /* policy_container_policies */);
Charles Reisf44482022017-10-13 21:15:031469
creisf49dfc92016-07-26 17:05:181470 new_entry = GetLastCommittedEntry()->CloneAndReplace(
Carlos Caballero40b0efd2021-01-26 11:55:001471 frame_entry, true, rfh->frame_tree_node(), frame_tree_.root());
jama78746e2017-02-22 17:21:571472 if (new_entry->GetURL().GetOrigin() != params.url.GetOrigin()) {
1473 // TODO(jam): we had one report of this with a URL that was redirecting to
1474 // only tildes. Until we understand that better, don't copy the cert in
1475 // this case.
1476 new_entry->GetSSL() = SSLStatus();
John Abd-El-Malek9cd697e2017-07-12 21:53:141477
John Abd-El-Malek380d3c5922017-09-08 00:20:311478 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451479 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141480 UMA_HISTOGRAM_BOOLEAN(
1481 "Navigation.SecureSchemeHasSSLStatus.NewPageInPageOriginMismatch",
1482 !!new_entry->GetSSL().certificate);
1483 }
jama78746e2017-02-22 17:21:571484 }
creisf49dfc92016-07-26 17:05:181485
Patrick Monette50e8bd82019-06-13 22:40:451486 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1487 // that |frame_entry| should now have a reference count of exactly 2: one
1488 // due to the local variable |frame_entry|, and another due to |new_entry|
1489 // also retaining one. This should never fail, because it's the main frame.
1490 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181491
1492 update_virtual_url = new_entry->update_virtual_url_with_url();
John Abd-El-Malek9cd697e2017-07-12 21:53:141493
John Abd-El-Malek380d3c5922017-09-08 00:20:311494 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451495 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141496 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus.NewPageInPage",
1497 !!new_entry->GetSSL().certificate);
1498 }
creisf49dfc92016-07-26 17:05:181499 }
1500
Charlie Reisc0f17d2d2021-01-12 18:52:491501 // Only make a copy of the pending entry if it is appropriate for the new
1502 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451503 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041504 // 1. The SiteInstance hasn't been assigned to something else.
1505 // 2. The pending entry was intended as a new entry, rather than being a
1506 // history navigation that was interrupted by an unrelated,
1507 // renderer-initiated navigation.
1508 // TODO(csharrison): Investigate whether we can remove some of the coarser
1509 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451510 if (!new_entry && PendingEntryMatchesRequest(request) &&
1511 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341512 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431513 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351514 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431515
[email protected]f1eb87a2011-05-06 17:49:411516 update_virtual_url = new_entry->update_virtual_url_with_url();
Camille Lamy62b826012019-02-26 09:15:471517 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451518 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141519
John Abd-El-Malek380d3c5922017-09-08 00:20:311520 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451521 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141522 UMA_HISTOGRAM_BOOLEAN(
1523 "Navigation.SecureSchemeHasSSLStatus.NewPagePendingEntryMatches",
1524 !!new_entry->GetSSL().certificate);
1525 }
creisf49dfc92016-07-26 17:05:181526 }
1527
Charlie Reisc0f17d2d2021-01-12 18:52:491528 // For cross-document commits with no matching pending entry, create a new
1529 // entry.
creisf49dfc92016-07-26 17:05:181530 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061531 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071532 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1533 initiator_origin,
Lukasz Anforowicz435bcb582019-07-12 20:50:061534 base::string16(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451535 params.transition, request->IsRendererInitiated(),
Lukasz Anforowicz435bcb582019-07-12 20:50:061536 nullptr); // blob_url_loader_factory
[email protected]f8f93eb2012-09-25 03:06:241537
1538 // Find out whether the new entry needs to update its virtual URL on URL
1539 // change and set up the entry accordingly. This is needed to correctly
1540 // update the virtual URL when replaceState is called after a pushState.
1541 GURL url = params.url;
1542 bool needs_update = false;
[email protected]47752982014-07-29 08:01:431543 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1544 &url, browser_context_, &needs_update);
[email protected]f8f93eb2012-09-25 03:06:241545 new_entry->set_update_virtual_url_with_url(needs_update);
1546
Charlie Reisc0f17d2d2021-01-12 18:52:491547 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411548 // update the virtual URL based on the new URL. For example, this is needed
1549 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1550 // the URL.
[email protected]f8f93eb2012-09-25 03:06:241551 update_virtual_url = needs_update;
Camille Lamy62b826012019-02-26 09:15:471552 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451553 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141554
John Abd-El-Malek380d3c5922017-09-08 00:20:311555 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451556 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141557 UMA_HISTOGRAM_BOOLEAN(
1558 "Navigation.SecureSchemeHasSSLStatus.NewPageNoMatchingEntry",
1559 !!new_entry->GetSSL().certificate);
1560 }
[email protected]e9ba4472008-09-14 15:42:431561 }
1562
wjmaclean7431bb22015-02-19 14:53:431563 // Don't use the page type from the pending entry. Some interstitial page
1564 // may have set the type to interstitial. Once we commit, however, the page
1565 // type must always be normal or error.
1566 new_entry->set_page_type(params.url_is_unreachable ? PAGE_TYPE_ERROR
1567 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041568 new_entry->SetURL(params.url);
[email protected]f1eb87a2011-05-06 17:49:411569 if (update_virtual_url)
avi25764702015-06-23 15:43:371570 UpdateVirtualURLToURL(new_entry.get(), params.url);
arthursonzogni73fe3212020-11-17 13:24:071571 new_entry->SetReferrer(Referrer(*params.referrer));
[email protected]022af742011-12-28 18:37:251572 new_entry->SetTransitionType(params.transition);
[email protected]b6583592012-01-25 19:52:331573 new_entry->set_site_instance(
[email protected]27dd82fd2014-03-03 22:11:431574 static_cast<SiteInstanceImpl*>(rfh->GetSiteInstance()));
Rakina Zata Amni6345d2f2021-02-12 04:07:571575 new_entry->SetOriginalRequestURL(request->GetOriginalRequestURL());
Rakina Zata Amnib597d632020-12-01 00:56:001576
1577 if (!is_same_document) {
1578 DCHECK_EQ(request->IsOverridingUserAgent() && !rfh->GetParent(),
1579 params.is_overriding_user_agent);
1580 } else {
1581 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
1582 }
[email protected]bf70edce2012-06-20 22:32:221583 new_entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
[email protected]e9ba4472008-09-14 15:42:431584
creis8b5cd4c2015-06-19 00:11:081585 // Update the FrameNavigationEntry for new main frame commits.
1586 FrameNavigationEntry* frame_entry =
1587 new_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisd2a509f2017-09-27 23:47:481588 frame_entry->set_frame_unique_name(rfh->frame_tree_node()->unique_name());
creis8b5cd4c2015-06-19 00:11:081589 frame_entry->set_item_sequence_number(params.item_sequence_number);
1590 frame_entry->set_document_sequence_number(params.document_sequence_number);
Charles Reisf44482022017-10-13 21:15:031591 frame_entry->set_redirect_chain(params.redirects);
1592 frame_entry->SetPageState(params.page_state);
clamy432acb22016-04-15 19:41:431593 frame_entry->set_method(params.method);
1594 frame_entry->set_post_id(params.post_id);
Titouan Rigoudy6ec70402021-02-02 15:42:191595 frame_entry->set_policy_container_policies(
1596 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1597 request));
Antonio Sartori78a749f2020-11-30 12:03:391598
Nasko Oskov03912102019-01-11 00:21:321599 if (!params.url_is_unreachable)
1600 frame_entry->set_committed_origin(params.origin);
Tsuyoshi Horob7b033e2020-03-09 15:23:311601 if (request->web_bundle_navigation_info()) {
1602 frame_entry->set_web_bundle_navigation_info(
1603 request->web_bundle_navigation_info()->Clone());
1604 }
creis8b5cd4c2015-06-19 00:11:081605
eugenebut604866f2017-05-10 21:35:361606 // history.pushState() is classified as a navigation to a new page, but sets
1607 // is_same_document to true. In this case, we already have the title and
creisf49dfc92016-07-26 17:05:181608 // favicon available, so set them immediately.
eugenebut604866f2017-05-10 21:35:361609 if (is_same_document && GetLastCommittedEntry()) {
[email protected]ff64b3e2014-05-31 04:07:331610 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
[email protected]3a868f212014-08-09 10:41:191611 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1612 }
[email protected]ff64b3e2014-05-31 04:07:331613
[email protected]60d6cca2013-04-30 08:47:131614 DCHECK(!params.history_list_was_cleared || !replace_entry);
1615 // The browser requested to clear the session history when it initiated the
1616 // navigation. Now we know that the renderer has updated its state accordingly
1617 // and it is safe to also clear the browser side history.
1618 if (params.history_list_was_cleared) {
arthursonzogni69a6a1b2019-09-17 09:23:001619 DiscardNonCommittedEntries();
[email protected]60d6cca2013-04-30 08:47:131620 entries_.clear();
1621 last_committed_entry_index_ = -1;
1622 }
1623
Nasko Oskovaee2f862018-06-15 00:05:521624 // If this is a new navigation with replacement and there is a
1625 // pending_entry_ which matches the navigation reported by the renderer
1626 // process, then it should be the one replaced, so update the
1627 // last_committed_entry_index_ to use it.
1628 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101629 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521630 last_committed_entry_index_ = pending_entry_index_;
1631 }
1632
Alexander Timine3ec4192020-04-20 16:39:401633 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411634 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401635 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051636
Carlos IL42b416592019-10-07 23:10:361637 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
1638 !request->post_commit_error_page_html().empty());
[email protected]e9ba4472008-09-14 15:42:431639}
1640
Charlie Reisc0f17d2d2021-01-12 18:52:491641void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321642 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071643 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361644 bool is_same_document,
jam48cea9082017-02-15 06:13:291645 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451646 NavigationRequest* request,
Peter Boströmd7592132019-01-30 04:50:311647 bool keep_pending_entry) {
creis26d22632017-04-21 20:23:561648 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1649 << "that a last committed entry exists.";
1650
[email protected]e9ba4472008-09-14 15:42:431651 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001652 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431653
Charlie Reis7e2cb6d2021-01-26 01:27:161654 NavigationEntryImpl* entry = nullptr;
avicbdc4c12015-07-01 16:07:111655 if (params.intended_as_new_entry) {
Charlie Reis7e2cb6d2021-01-26 01:27:161656 // We're guaranteed to have a last committed entry if intended_as_new_entry
1657 // is true.
avicbdc4c12015-07-01 16:07:111658 entry = GetLastCommittedEntry();
Charlie Reis7e2cb6d2021-01-26 01:27:161659 DCHECK(entry);
1660
1661 // If the NavigationRequest matches a new pending entry and is classified as
1662 // EXISTING_ENTRY, then it is a navigation to the same URL that was
1663 // converted to a reload, such as a user pressing enter in the omnibox.
1664 if (pending_entry_ && pending_entry_index_ == -1 &&
1665 pending_entry_->GetUniqueID() ==
1666 request->commit_params().nav_entry_id) {
1667 // Note: The pending entry will usually have a real ReloadType here, but
1668 // it can still be ReloadType::NONE in cases that
1669 // ShouldTreatNavigationAsReload returns false (e.g., POST, view-source).
1670
1671 // If we classified this correctly, the SiteInstance should not have
1672 // changed.
1673 CHECK_EQ(entry->site_instance(), rfh->GetSiteInstance());
1674
1675 // For converted reloads, we assign the entry's unique ID to be that of
1676 // the new one. Since this is always the result of a user action, we want
1677 // to dismiss infobars, etc. like a regular user-initiated navigation.
1678 entry->set_unique_id(pending_entry_->GetUniqueID());
1679
1680 // The extra headers may have changed due to reloading with different
1681 // headers.
1682 entry->set_extra_headers(pending_entry_->extra_headers());
1683 }
1684 // Otherwise, this was intended as a new entry but the pending entry was
1685 // lost in the meantime and no new entry was created. We are stuck at the
1686 // last committed entry.
1687
1688 // Even if this is a converted reload from pressing enter in the omnibox,
1689 // the server could redirect, requiring an update to the SSL status. If this
1690 // is a same document navigation, though, there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451691 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
Charlie Reis7e2cb6d2021-01-26 01:27:161692 if (!is_same_document) {
Camille Lamy62b826012019-02-26 09:15:471693 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451694 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
Charlie Reis7e2cb6d2021-01-26 01:27:161695 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141696
Charlie Reis7e2cb6d2021-01-26 01:27:161697 if (params.url.SchemeIs(url::kHttpsScheme) &&
Mohamed Abdelhalim833de902019-09-16 17:41:451698 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141699 bool has_cert = !!entry->GetSSL().certificate;
1700 if (is_same_document) {
1701 UMA_HISTOGRAM_BOOLEAN(
1702 "Navigation.SecureSchemeHasSSLStatus."
1703 "ExistingPageSameDocumentIntendedAsNew",
1704 has_cert);
1705 } else {
1706 UMA_HISTOGRAM_BOOLEAN(
1707 "Navigation.SecureSchemeHasSSLStatus."
1708 "ExistingPageDifferentDocumentIntendedAsNew",
1709 has_cert);
1710 }
1711 }
Rakina Zata Amnif6950d552020-11-24 03:26:101712 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:111713 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:101714 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b902016-09-01 16:58:161715
eugenebut604866f2017-05-10 21:35:361716 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451717 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361718 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1719 // this was a restored same document navigation entry, then it won't have
1720 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1721 // navigated it.
jam48cea9082017-02-15 06:13:291722 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
1723 if (entry->GetURL().GetOrigin() == last_entry->GetURL().GetOrigin() &&
1724 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1725 was_restored) {
1726 entry->GetSSL() = last_entry->GetSSL();
1727 }
1728 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451729 // In rapid back/forward navigations |request| sometimes won't have a cert
1730 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191731 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451732 if (request->GetSSLInfo().has_value() &&
1733 request->GetSSLInfo()->is_valid()) {
1734 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
1735 } else if (entry->GetURL().GetOrigin() != request->GetURL().GetOrigin()) {
John Abd-El-Malek3f247082017-12-07 19:02:191736 entry->GetSSL() = SSLStatus();
1737 }
jam48cea9082017-02-15 06:13:291738 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141739
John Abd-El-Malek380d3c5922017-09-08 00:20:311740 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451741 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141742 bool has_cert = !!entry->GetSSL().certificate;
1743 if (is_same_document && was_restored) {
1744 UMA_HISTOGRAM_BOOLEAN(
1745 "Navigation.SecureSchemeHasSSLStatus."
1746 "ExistingPageSameDocumentRestoredBrowserInitiated",
1747 has_cert);
1748 } else if (is_same_document && !was_restored) {
1749 UMA_HISTOGRAM_BOOLEAN(
1750 "Navigation.SecureSchemeHasSSLStatus."
1751 "ExistingPageSameDocumentBrowserInitiated",
1752 has_cert);
1753 } else if (!is_same_document && was_restored) {
1754 UMA_HISTOGRAM_BOOLEAN(
1755 "Navigation.SecureSchemeHasSSLStatus."
1756 "ExistingPageRestoredBrowserInitiated",
1757 has_cert);
1758 } else {
1759 UMA_HISTOGRAM_BOOLEAN(
1760 "Navigation.SecureSchemeHasSSLStatus.ExistingPageBrowserInitiated",
1761 has_cert);
1762 }
1763 }
avicbdc4c12015-07-01 16:07:111764 } else {
1765 // This is renderer-initiated. The only kinds of renderer-initated
Charlie Reisc0f17d2d2021-01-12 18:52:491766 // navigations that are EXISTING_ENTRY are reloads and history.replaceState,
avicbdc4c12015-07-01 16:07:111767 // which land us at the last committed entry.
1768 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101769
Mikel Astizba9cf2fd2017-12-17 10:38:101770 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1771 // to avoid misleading interpretations (e.g. URLs paired with
1772 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1773 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1774 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1775
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571776 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:101777
eugenebut604866f2017-05-10 21:35:361778 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451779 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361780 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471781 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451782 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141783
John Abd-El-Malek380d3c5922017-09-08 00:20:311784 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451785 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141786 bool has_cert = !!entry->GetSSL().certificate;
1787 if (is_same_document) {
1788 UMA_HISTOGRAM_BOOLEAN(
1789 "Navigation.SecureSchemeHasSSLStatus."
1790 "ExistingPageSameDocumentRendererInitiated",
1791 has_cert);
1792 } else {
1793 UMA_HISTOGRAM_BOOLEAN(
1794 "Navigation.SecureSchemeHasSSLStatus."
1795 "ExistingPageDifferentDocumentRendererInitiated",
1796 has_cert);
1797 }
1798 }
avicbdc4c12015-07-01 16:07:111799 }
1800 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:431801
[email protected]5ccd4dc2012-10-24 02:28:141802 // The URL may have changed due to redirects.
wjmaclean7431bb22015-02-19 14:53:431803 entry->set_page_type(params.url_is_unreachable ? PAGE_TYPE_ERROR
1804 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041805 entry->SetURL(params.url);
arthursonzogni73fe3212020-11-17 13:24:071806 entry->SetReferrer(Referrer(*params.referrer));
[email protected]38178a42009-12-17 18:58:321807 if (entry->update_virtual_url_with_url())
1808 UpdateVirtualURLToURL(entry, params.url);
[email protected]5ccd4dc2012-10-24 02:28:141809
jam015ba062017-01-06 21:17:001810 // The site instance will normally be the same except
1811 // 1) session restore, when no site instance will be assigned or
1812 // 2) redirect, when the site instance is reset.
Lei Zhang96031532019-10-10 19:05:471813 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
naskoaf182192016-08-11 02:12:011814 entry->site_instance() == rfh->GetSiteInstance());
clamy432acb22016-04-15 19:41:431815
naskoaf182192016-08-11 02:12:011816 // Update the existing FrameNavigationEntry to ensure all of its members
1817 // reflect the parameters coming from the renderer process.
Lukasz Anforowicz435bcb582019-07-12 20:50:061818 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451819 request->common_params().initiator_origin;
naskoaf182192016-08-11 02:12:011820 entry->AddOrUpdateFrameEntry(
1821 rfh->frame_tree_node(), params.item_sequence_number,
1822 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
arthursonzogni73fe3212020-11-17 13:24:071823 params.url, GetCommittedOriginForFrameEntry(params),
1824 Referrer(*params.referrer), initiator_origin, params.redirects,
1825 params.page_state, params.method, params.post_id,
1826 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031827 request->web_bundle_navigation_info()
1828 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391829 : nullptr,
Titouan Rigoudy6ec70402021-02-02 15:42:191830 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1831 request));
creis22a7b4c2016-04-28 07:20:301832
[email protected]5ccd4dc2012-10-24 02:28:141833 // The redirected to page should not inherit the favicon from the previous
1834 // page.
eugenebut604866f2017-05-10 21:35:361835 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:481836 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:141837
Peter Boströmd7592132019-01-30 04:50:311838 // We should also usually discard the pending entry if it corresponds to a
1839 // different navigation, since that one is now likely canceled. In rare
1840 // cases, we leave the pending entry for another navigation in place when we
1841 // know it is still ongoing, to avoid a flicker in the omnibox (see
1842 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:431843 //
1844 // Note that we need to use the "internal" version since we don't want to
1845 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:311846 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001847 DiscardNonCommittedEntries();
[email protected]40bcc302009-03-02 20:50:391848
Carlos IL4dea8902020-05-26 15:14:291849 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:111850 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:431851}
1852
[email protected]d202a7c2012-01-04 07:53:471853void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:321854 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071855 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361856 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:471857 bool replace_entry,
1858 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451859 NavigationRequest* request) {
avi25f5f9e2015-07-17 20:08:261860 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1861 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
[email protected]09b8f82f2009-06-16 20:22:111862
[email protected]e9ba4472008-09-14 15:42:431863 // Manual subframe navigations just get the current entry cloned so the user
1864 // can go back or forward to it. The actual subframe information will be
1865 // stored in the page state for each of those entries. This happens out of
1866 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:091867 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1868 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:381869
Mikel Astizba9cf2fd2017-12-17 10:38:101870 // The DCHECK below documents the fact that we don't know of any situation
1871 // where |replace_entry| is true for subframe navigations. This simplifies
1872 // reasoning about the replacement struct for subframes (see
1873 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
1874 DCHECK(!replace_entry);
1875
Patrick Monette50e8bd82019-06-13 22:40:451876 // This FrameNavigationEntry might not end up being used in the
1877 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Lukasz Anforowicz435bcb582019-07-12 20:50:061878 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451879 request->common_params().initiator_origin;
Patrick Monette50e8bd82019-06-13 22:40:451880 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481881 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
creisce0ef3572017-01-26 17:53:081882 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Nasko Oskov03912102019-01-11 00:21:321883 params.url, (params.url_is_unreachable) ? nullptr : &params.origin,
arthursonzogni73fe3212020-11-17 13:24:071884 Referrer(*params.referrer), initiator_origin, params.redirects,
1885 params.page_state, params.method, params.post_id,
1886 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031887 request->web_bundle_navigation_info()
1888 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391889 : nullptr,
Titouan Rigoudy6ec70402021-02-02 15:42:191890 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1891 request));
Charles Reisf44482022017-10-13 21:15:031892
creisce0ef3572017-01-26 17:53:081893 std::unique_ptr<NavigationEntryImpl> new_entry =
1894 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:451895 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
Carlos Caballero40b0efd2021-01-26 11:55:001896 frame_tree_.root());
creise062d542015-08-25 02:01:551897
Alexander Timine3ec4192020-04-20 16:39:401898 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411899 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401900 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:471901
creisce0ef3572017-01-26 17:53:081902 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:451903 // to replace, which can happen due to a unique name change. See
1904 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
1905 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:381906
Carlos IL42b416592019-10-07 23:10:361907 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false);
[email protected]e9ba4472008-09-14 15:42:431908}
1909
[email protected]d202a7c2012-01-04 07:53:471910bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:321911 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071912 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:391913 bool is_same_document,
Mohamed Abdelhalim833de902019-09-16 17:41:451914 NavigationRequest* request) {
avi9f07a0c2015-02-18 22:51:291915 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1916 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
1917
[email protected]e9ba4472008-09-14 15:42:431918 // We're guaranteed to have a previously committed entry, and we now need to
1919 // handle navigation inside of a subframe in it without creating a new entry.
1920 DCHECK(GetLastCommittedEntry());
1921
creis913c63ce2016-07-16 19:52:521922 // For newly created subframes, we don't need to send a commit notification.
1923 // This is only necessary for history navigations in subframes.
1924 bool send_commit_notification = false;
1925
Rakina Zata Amnif6950d552020-11-24 03:26:101926 // If |nav_entry_id| is non-zero and matches an existing entry, this
1927 // is a history navigation. Update the last committed index accordingly. If
1928 // we don't recognize the |nav_entry_id|, it might be a recently
1929 // pruned entry. We'll handle it below.
1930 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
1931 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:471932 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:061933 // Make sure that a subframe commit isn't changing the main frame's
1934 // origin. Otherwise the renderer process may be confused, leading to a
1935 // URL spoof. We can't check the path since that may change
1936 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:571937 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
1938 // about:blank, file, and unique origins are more subtle to get right.
Eugene But9fb98d12018-05-22 18:28:331939 // We'll abstract out the relevant checks from IsURLSameDocumentNavigation
1940 // and share them here. See https://crbug.com/618104.
creis37988b92016-06-10 18:03:571941 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
1942 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
1943 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
1944 dest_top_url.SchemeIsHTTPOrHTTPS() &&
1945 current_top_url.GetOrigin() != dest_top_url.GetOrigin()) {
creisfb6eeb62016-05-10 19:01:511946 bad_message::ReceivedBadMessage(rfh->GetProcess(),
1947 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:061948 }
creis3cdc3b02015-05-29 23:00:471949
creis913c63ce2016-07-16 19:52:521950 // We only need to discard the pending entry in this history navigation
1951 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:061952 last_committed_entry_index_ = entry_index;
arthursonzogni69a6a1b2019-09-17 09:23:001953 DiscardNonCommittedEntries();
creis913c63ce2016-07-16 19:52:521954
1955 // History navigations should send a commit notification.
1956 send_commit_notification = true;
avi98405c22015-05-21 20:47:061957 }
[email protected]e9ba4472008-09-14 15:42:431958 }
[email protected]f233e4232013-02-23 00:55:141959
creisce0ef3572017-01-26 17:53:081960 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
1961 // it may be a "history auto" case where we update an existing one.
1962 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Lukasz Anforowicz435bcb582019-07-12 20:50:061963 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451964 request->common_params().initiator_origin;
creisce0ef3572017-01-26 17:53:081965 last_committed->AddOrUpdateFrameEntry(
1966 rfh->frame_tree_node(), params.item_sequence_number,
1967 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
arthursonzogni73fe3212020-11-17 13:24:071968 params.url, GetCommittedOriginForFrameEntry(params),
1969 Referrer(*params.referrer), initiator_origin, params.redirects,
1970 params.page_state, params.method, params.post_id,
1971 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031972 request->web_bundle_navigation_info()
1973 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391974 : nullptr,
Titouan Rigoudy6ec70402021-02-02 15:42:191975 ComputePolicyContainerPoliciesForFrameEntry(rfh, is_same_document,
1976 request));
creis625a0c7d2015-03-24 23:17:121977
creis913c63ce2016-07-16 19:52:521978 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:431979}
1980
[email protected]d202a7c2012-01-04 07:53:471981int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:231982 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:031983 for (size_t i = 0; i < entries_.size(); ++i) {
1984 if (entries_[i].get() == entry)
1985 return i;
1986 }
1987 return -1;
[email protected]765b35502008-08-21 00:51:201988}
1989
Eugene But7cc259d2017-10-09 23:52:491990// There are two general cases where a navigation is "same-document":
avidb7d1d22015-06-08 21:21:501991// 1. A fragment navigation, in which the url is kept the same except for the
1992// reference fragment.
1993// 2. A history API navigation (pushState and replaceState). This case is
Eugene But7cc259d2017-10-09 23:52:491994// always same-document, but the urls are not guaranteed to match excluding
1995// the fragment. The relevant spec allows pushState/replaceState to any URL
1996// on the same origin.
avidb7d1d22015-06-08 21:21:501997// However, due to reloads, even identical urls are *not* guaranteed to be
Eugene But7cc259d2017-10-09 23:52:491998// same-document navigations, we have to trust the renderer almost entirely.
avidb7d1d22015-06-08 21:21:501999// The one thing we do know is that cross-origin navigations will *never* be
Eugene But7cc259d2017-10-09 23:52:492000// same-document. Therefore, trust the renderer if the URLs are on the same
2001// origin, and assume the renderer is malicious if a cross-origin navigation
2002// claims to be same-document.
creisf164daa2016-06-07 00:17:052003//
2004// TODO(creis): Clean up and simplify the about:blank and origin checks below,
2005// which are likely redundant with each other. Be careful about data URLs vs
2006// about:blank, both of which are unique origins and thus not considered equal.
Eugene But7cc259d2017-10-09 23:52:492007bool NavigationControllerImpl::IsURLSameDocumentNavigation(
[email protected]b9d4dfdc2013-08-08 00:25:122008 const GURL& url,
creisf164daa2016-06-07 00:17:052009 const url::Origin& origin,
Eugene But7cc259d2017-10-09 23:52:492010 bool renderer_says_same_document,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572011 RenderFrameHost* rfh) {
creis225a7432016-06-03 22:56:272012 RenderFrameHostImpl* rfhi = static_cast<RenderFrameHostImpl*>(rfh);
avidb7d1d22015-06-08 21:21:502013 GURL last_committed_url;
2014 if (rfh->GetParent()) {
creis225a7432016-06-03 22:56:272015 // Use the FrameTreeNode's current_url and not rfh->GetLastCommittedURL(),
2016 // which might be empty in a new RenderFrameHost after a process swap.
2017 // Here, we care about the last committed URL in the FrameTreeNode,
2018 // regardless of which process it is in.
2019 last_committed_url = rfhi->frame_tree_node()->current_url();
avidb7d1d22015-06-08 21:21:502020 } else {
2021 NavigationEntry* last_committed = GetLastCommittedEntry();
2022 // There must be a last-committed entry to compare URLs to. TODO(avi): When
2023 // might Blink say that a navigation is in-page yet there be no last-
2024 // committed entry?
2025 if (!last_committed)
2026 return false;
2027 last_committed_url = last_committed->GetURL();
2028 }
2029
Gyuyoung Kim1ac4ca782020-09-11 03:32:512030 auto prefs = rfhi->GetOrCreateWebPreferences();
creis225a7432016-06-03 22:56:272031 const url::Origin& committed_origin =
2032 rfhi->frame_tree_node()->current_origin();
avidb7d1d22015-06-08 21:21:502033 bool is_same_origin = last_committed_url.is_empty() ||
2034 // TODO(japhet): We should only permit navigations
2035 // originating from about:blank to be in-page if the
2036 // about:blank is the first document that frame loaded.
2037 // We don't have sufficient information to identify
2038 // that case at the moment, so always allow about:blank
2039 // for now.
csharrisona3bd0b32016-10-19 18:40:482040 last_committed_url == url::kAboutBlankURL ||
avidb7d1d22015-06-08 21:21:502041 last_committed_url.GetOrigin() == url.GetOrigin() ||
creisf164daa2016-06-07 00:17:052042 committed_origin == origin ||
avidb7d1d22015-06-08 21:21:502043 !prefs.web_security_enabled ||
2044 (prefs.allow_universal_access_from_file_urls &&
creisdc004e02015-11-20 21:28:472045 committed_origin.scheme() == url::kFileScheme);
Eugene But7cc259d2017-10-09 23:52:492046 if (!is_same_origin && renderer_says_same_document) {
avidb7d1d22015-06-08 21:21:502047 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2048 bad_message::NC_IN_PAGE_NAVIGATION);
2049 }
Eugene But7cc259d2017-10-09 23:52:492050 return is_same_origin && renderer_says_same_document;
[email protected]e9ba4472008-09-14 15:42:432051}
2052
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572053void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242054 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572055 NavigationControllerImpl* source =
2056 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572057 // Verify that we look new.
Lei Zhang96031532019-10-10 19:05:472058 DCHECK_EQ(0, GetEntryCount());
2059 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:572060
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572061 if (source->GetEntryCount() == 0)
[email protected]ce3fa3c2009-04-20 19:55:572062 return; // Nothing new to do.
2063
Francois Dorayeaace782017-06-21 16:37:242064 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442065 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572066 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572067
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572068 for (auto it = source->session_storage_namespace_map_.begin();
2069 it != source->session_storage_namespace_map_.end(); ++it) {
[email protected]fdac6ade2013-07-20 01:06:302070 SessionStorageNamespaceImpl* source_namespace =
2071 static_cast<SessionStorageNamespaceImpl*>(it->second.get());
2072 session_storage_namespace_map_[it->first] = source_namespace->Clone();
2073 }
[email protected]4e6419c2010-01-15 04:50:342074
Lukasz Anforowicz0de0f452020-12-02 19:57:152075 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572076}
2077
Aran Gilman37d11632019-10-08 23:07:152078void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2079 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162080 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012081 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162082
[email protected]d202a7c2012-01-04 07:53:472083 NavigationControllerImpl* source =
2084 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252085
avi2b177592014-12-10 02:08:022086 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012087 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252088
[email protected]474f8512013-05-31 22:31:162089 // We now have one entry, possibly with a new pending entry. Ensure that
2090 // adding the entries from source won't put us over the limit.
2091 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572092 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572093 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252094
Carlos IL4dea8902020-05-26 15:14:292095 // Insert the entries from source. Ignore any pending entry, since it has not
2096 // committed in source.
[email protected]474f8512013-05-31 22:31:162097 int max_source_index = source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:252098 if (max_source_index == -1)
[email protected]a26023822011-12-29 00:23:552099 max_source_index = source->GetEntryCount();
[email protected]e1cd5452010-08-26 18:03:252100 else
2101 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572102
2103 // Ignore the source's current entry if merging with replacement.
2104 // TODO(davidben): This should preserve entries forward of the current
2105 // too. http://crbug.com/317872
2106 if (replace_entry && max_source_index > 0)
2107 max_source_index--;
2108
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572109 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252110
2111 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552112 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142113
Carlos Caballeroede6f8c2021-01-28 11:01:502114 SetHistoryOffsetAndLength(last_committed_entry_index_, GetEntryCount());
[email protected]e1cd5452010-08-26 18:03:252115}
2116
[email protected]79368982013-11-13 01:11:012117bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162118 // If there is no last committed entry, we cannot prune. Even if there is a
2119 // pending entry, it may not commit, leaving this WebContents blank, despite
2120 // possibly giving it new entries via CopyStateFromAndPrune.
2121 if (last_committed_entry_index_ == -1)
2122 return false;
[email protected]9350602e2013-02-26 23:27:442123
[email protected]474f8512013-05-31 22:31:162124 // We cannot prune if there is a pending entry at an existing entry index.
2125 // It may not commit, so we have to keep the last committed entry, and thus
2126 // there is no sensible place to keep the pending entry. It is ok to have
2127 // a new pending entry, which can optionally commit as a new navigation.
2128 if (pending_entry_index_ != -1)
2129 return false;
2130
[email protected]474f8512013-05-31 22:31:162131 return true;
2132}
2133
[email protected]79368982013-11-13 01:11:012134void NavigationControllerImpl::PruneAllButLastCommitted() {
2135 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162136
avi2b177592014-12-10 02:08:022137 DCHECK_EQ(0, last_committed_entry_index_);
2138 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442139
Carlos Caballeroede6f8c2021-01-28 11:01:502140 SetHistoryOffsetAndLength(last_committed_entry_index_, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442141}
2142
[email protected]79368982013-11-13 01:11:012143void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162144 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012145 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262146
[email protected]474f8512013-05-31 22:31:162147 // Erase all entries but the last committed entry. There may still be a
2148 // new pending entry after this.
2149 entries_.erase(entries_.begin(),
2150 entries_.begin() + last_committed_entry_index_);
2151 entries_.erase(entries_.begin() + 1, entries_.end());
2152 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262153}
2154
Christian Dullweber1af31e62018-02-22 11:49:482155void NavigationControllerImpl::DeleteNavigationEntries(
2156 const DeletionPredicate& deletionPredicate) {
2157 // It is up to callers to check the invariants before calling this.
2158 CHECK(CanPruneAllButLastCommitted());
2159 std::vector<int> delete_indices;
2160 for (size_t i = 0; i < entries_.size(); i++) {
2161 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572162 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482163 delete_indices.push_back(i);
2164 }
2165 }
2166 if (delete_indices.empty())
2167 return;
2168
2169 if (delete_indices.size() == GetEntryCount() - 1U) {
2170 PruneAllButLastCommitted();
2171 } else {
2172 // Do the deletion in reverse to preserve indices.
2173 for (auto it = delete_indices.rbegin(); it != delete_indices.rend(); ++it) {
2174 RemoveEntryAtIndex(*it);
2175 }
Carlos Caballeroede6f8c2021-01-28 11:01:502176 SetHistoryOffsetAndLength(last_committed_entry_index_, GetEntryCount());
Christian Dullweber1af31e62018-02-22 11:49:482177 }
2178 delegate()->NotifyNavigationEntriesDeleted();
2179}
2180
Shivani Sharma883f5f32019-02-12 18:20:012181bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2182 auto* entry = GetEntryAtIndex(index);
2183 return entry && entry->should_skip_on_back_forward_ui();
2184}
2185
Carlos Caballero35ce710c2019-09-19 10:59:452186BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2187 return back_forward_cache_;
2188}
2189
clamy987a3752018-05-03 17:36:262190void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2191 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2192 // progress, since this will cause a use-after-free. (We only allow this
2193 // when the tab is being destroyed for shutdown, since it won't return to
2194 // NavigateToEntry in that case.) http://crbug.com/347742.
2195 CHECK(!in_navigate_to_pending_entry_ || delegate_->IsBeingDestroyed());
2196
2197 if (was_failure && pending_entry_) {
2198 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2199 } else {
2200 failed_pending_entry_id_ = 0;
2201 }
2202
2203 if (pending_entry_) {
2204 if (pending_entry_index_ == -1)
2205 delete pending_entry_;
2206 pending_entry_index_ = -1;
2207 pending_entry_ = nullptr;
2208 }
arthursonzogni66f711c2019-10-08 14:40:362209
2210 // Ensure any refs to the current pending entry are ignored if they get
2211 // deleted, by clearing the set of known refs. All future pending entries will
2212 // only be affected by new refs.
2213 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262214}
2215
2216void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2217 if (pending_entry_)
2218 pending_entry_->set_ssl_error(error);
2219}
2220
Camille Lamy5193caa2018-10-12 11:59:422221#if defined(OS_ANDROID)
2222// static
2223bool NavigationControllerImpl::ValidateDataURLAsString(
2224 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2225 if (!data_url_as_string)
2226 return false;
2227
2228 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2229 return false;
2230
2231 // The number of characters that is enough for validating a data: URI.
2232 // From the GURL's POV, the only important part here is scheme, it doesn't
2233 // check the actual content. Thus we can take only the prefix of the url, to
2234 // avoid unneeded copying of a potentially long string.
2235 const size_t kDataUriPrefixMaxLen = 64;
2236 GURL data_url(
2237 std::string(data_url_as_string->front_as<char>(),
2238 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2239 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2240 return false;
2241
2242 return true;
2243}
2244#endif
2245
Shivani Sharma194877032019-03-07 17:52:472246void NavigationControllerImpl::NotifyUserActivation() {
2247 // When a user activation occurs, ensure that all adjacent entries for the
2248 // same document clear their skippable bit, so that the history manipulation
2249 // intervention does not apply to them.
Shivani Sharmaffb32b82019-04-09 16:58:472250 // TODO(crbug.com/949279) in case it becomes necessary for resetting based on
2251 // which frame created an entry and which frame has the user gesture.
Shivani Sharma194877032019-03-07 17:52:472252 auto* last_committed_entry = GetLastCommittedEntry();
2253 if (!last_committed_entry)
2254 return;
Shivani Sharma194877032019-03-07 17:52:472255
Shivani Sharmac4cc8922019-04-18 03:11:172256 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472257}
2258
clamy987a3752018-05-03 17:36:262259bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2260 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172261 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262262 NavigationEntryImpl* entry =
2263 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2264 if (!entry)
2265 return false;
2266
2267 FrameNavigationEntry* frame_entry =
2268 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2269 if (!frame_entry)
2270 return false;
2271
Camille Lamy5193caa2018-10-12 11:59:422272 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572273 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232274 ReloadType::NONE, false /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422275 true /* is_history_navigation_in_new_child */);
clamyea99ea12018-05-28 13:54:232276
2277 if (!request)
2278 return false;
2279
arthursonzognif046d4a2019-12-12 19:08:102280 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412281
Lukasz Anforowicz9ee83c272020-12-01 20:14:052282 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2283 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232284
2285 return true;
clamy987a3752018-05-03 17:36:262286}
2287
Tsuyoshi Horo52fd08e2020-07-07 07:03:452288bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2289 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2290 if (!entry)
2291 return false;
2292 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2293 if (!frame_entry)
2294 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142295 ReloadType reload_type = ReloadType::NORMAL;
2296 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452297 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142298 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452299 false /* is_same_document_history_load */,
2300 false /* is_history_navigation_in_new_child */);
2301 if (!request)
2302 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052303 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452304 return true;
2305}
2306
Dave Tapuska8bfd84c2019-03-26 20:47:162307void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2308 int offset,
2309 int sandbox_frame_tree_node_id) {
2310 if (!CanGoToOffset(offset))
2311 return;
2312 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id);
2313}
2314
clamy987a3752018-05-03 17:36:262315void NavigationControllerImpl::NavigateFromFrameProxy(
2316 RenderFrameHostImpl* render_frame_host,
2317 const GURL& url,
Antonio Sartori9a82f6f32020-12-14 09:22:452318 const base::UnguessableToken* initiator_frame_token,
2319 int initiator_process_id,
Lukasz Anforowicz63f3b9432019-05-30 05:42:582320 const base::Optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262321 bool is_renderer_initiated,
2322 SiteInstance* source_site_instance,
2323 const Referrer& referrer,
2324 ui::PageTransition page_transition,
2325 bool should_replace_current_entry,
Charlie Harrison8c113a32019-01-07 16:08:292326 NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262327 const std::string& method,
2328 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092329 const std::string& extra_headers,
John Delaney50425f82020-04-07 16:26:212330 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Gyuyoung Kim38e63632021-01-14 13:11:392331 const base::Optional<blink::Impression>& impression) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582332 if (is_renderer_initiated)
2333 DCHECK(initiator_origin.has_value());
2334
clamy987a3752018-05-03 17:36:262335 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582336
Rakina Zata Amni66992a82020-06-24 03:57:522337 // Don't allow an entry replacement if there is no entry to replace.
2338 // http://crbug.com/457149
2339 if (GetEntryCount() == 0)
2340 should_replace_current_entry = false;
2341
clamy987a3752018-05-03 17:36:262342 // Create a NavigationEntry for the transfer, without making it the pending
2343 // entry. Subframe transfers should have a clone of the last committed entry
2344 // with a FrameNavigationEntry for the target frame. Main frame transfers
2345 // should have a new NavigationEntry.
2346 // TODO(creis): Make this unnecessary by creating (and validating) the params
2347 // directly, passing them to the destination RenderFrameHost. See
2348 // https://crbug.com/536906.
2349 std::unique_ptr<NavigationEntryImpl> entry;
2350 if (!node->IsMainFrame()) {
2351 // Subframe case: create FrameNavigationEntry.
2352 if (GetLastCommittedEntry()) {
2353 entry = GetLastCommittedEntry()->Clone();
2354 entry->set_extra_headers(extra_headers);
2355 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2356 // Renderer-initiated navigation that target a remote frame are currently
2357 // classified as browser-initiated when this one has already navigated.
2358 // See https://crbug.com/722251.
2359 } else {
2360 // If there's no last committed entry, create an entry for about:blank
2361 // with a subframe entry for our destination.
2362 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2363 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:062364 GURL(url::kAboutBlankURL), referrer, initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:102365 source_site_instance, page_transition, is_renderer_initiated,
2366 extra_headers, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:372367 nullptr /* blob_url_loader_factory */, should_replace_current_entry,
2368 GetWebContents()));
2369 // CreateNavigationEntry() may have changed the transition type.
2370 page_transition = entry->GetTransitionType();
clamy987a3752018-05-03 17:36:262371 }
Titouan Rigoudy6ec70402021-02-02 15:42:192372 std::unique_ptr<PolicyContainerPolicies> policies;
Antonio Sartori78a749f2020-11-30 12:03:392373 if (GetLastCommittedEntry()) {
2374 FrameNavigationEntry* previous_frame_entry =
2375 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:192376 if (previous_frame_entry &&
2377 previous_frame_entry->policy_container_policies()) {
2378 policies = std::make_unique<PolicyContainerPolicies>(
2379 *previous_frame_entry->policy_container_policies());
Antonio Sartori78a749f2020-11-30 12:03:392380 }
2381 }
clamy987a3752018-05-03 17:36:262382 entry->AddOrUpdateFrameEntry(
2383 node, -1, -1, nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582384 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Lukasz Anforowicz435bcb582019-07-12 20:50:062385 base::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202386 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392387 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
Titouan Rigoudy6ec70402021-02-02 15:42:192388 std::move(policies));
clamy987a3752018-05-03 17:36:262389 } else {
2390 // Main frame case.
2391 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102392 url, referrer, initiator_origin, source_site_instance, page_transition,
2393 is_renderer_initiated, extra_headers, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:372394 blob_url_loader_factory, should_replace_current_entry,
2395 GetWebContents()));
2396 // CreateNavigationEntry() may have changed the transition type.
2397 page_transition = entry->GetTransitionType();
clamy987a3752018-05-03 17:36:262398 entry->root_node()->frame_entry->set_source_site_instance(
2399 static_cast<SiteInstanceImpl*>(source_site_instance));
2400 entry->root_node()->frame_entry->set_method(method);
2401 }
clamy987a3752018-05-03 17:36:262402
Camille Lamy5193caa2018-10-12 11:59:422403 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262404 if (GetLastCommittedEntry() &&
2405 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2406 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422407 override_user_agent = true;
clamy987a3752018-05-03 17:36:262408 }
2409 // TODO(creis): Set user gesture and intent received timestamp on Android.
2410
2411 // We may not have successfully added the FrameNavigationEntry to |entry|
2412 // above (per https://crbug.com/608402), in which case we create it from
2413 // scratch. This works because we do not depend on |frame_entry| being inside
2414 // |entry| during NavigateToEntry. This will go away when we shortcut this
2415 // further in https://crbug.com/536906.
2416 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2417 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452418 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
clamy987a3752018-05-03 17:36:262419 node->unique_name(), -1, -1, nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582420 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Lukasz Anforowicz435bcb582019-07-12 20:50:062421 nullptr /* origin */, referrer, initiator_origin, std::vector<GURL>(),
Miyoung Shin5d77f72072020-10-09 15:14:202422 blink::PageState(), method, -1, blob_url_loader_factory,
Antonio Sartori78a749f2020-11-30 12:03:392423 nullptr /* web_bundle_navigation_info */,
Titouan Rigoudy6ec70402021-02-02 15:42:192424 nullptr /* policy_container_policies */);
clamy987a3752018-05-03 17:36:262425 }
2426
Camille Lamy5193caa2018-10-12 11:59:422427 LoadURLParams params(url);
Antonio Sartori9a82f6f32020-12-14 09:22:452428 params.initiator_frame_token =
2429 initiator_frame_token ? base::make_optional(*initiator_frame_token)
2430 : base::nullopt;
2431 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292432 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422433 params.source_site_instance = source_site_instance;
2434 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2435 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032436 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422437 params.referrer = referrer;
2438 /* params.redirect_chain: skip */
2439 params.extra_headers = extra_headers;
2440 params.is_renderer_initiated = is_renderer_initiated;
2441 params.override_user_agent = UA_OVERRIDE_INHERIT;
2442 /* params.base_url_for_data_url: skip */
2443 /* params.virtual_url_for_data_url: skip */
2444 /* params.data_url_as_string: skip */
2445 params.post_data = post_body;
2446 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582447 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422448 /* params.frame_name: skip */
2449 // TODO(clamy): See if user gesture should be propagated to this function.
2450 params.has_user_gesture = false;
2451 params.should_clear_history_list = false;
2452 params.started_from_context_menu = false;
2453 /* params.navigation_ui_data: skip */
2454 /* params.input_start: skip */
Lucas Furukawa Gadania9c45682019-07-31 22:05:142455 params.was_activated = mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542456 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212457 params.impression = impression;
Camille Lamy5193caa2018-10-12 11:59:422458
2459 std::unique_ptr<NavigationRequest> request =
2460 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032461 node, params, override_user_agent, should_replace_current_entry,
2462 false /* has_user_gesture */, download_policy, ReloadType::NONE,
Scott Violet5ae6c42e2020-10-28 02:47:372463 entry.get(), frame_entry.get(), params.transition_type);
clamyea99ea12018-05-28 13:54:232464
2465 if (!request)
2466 return;
2467
Arthur Hemery948742762019-09-18 10:06:242468 // At this stage we are proceeding with this navigation. If this was renderer
2469 // initiated with user gesture, we need to make sure we clear up potential
2470 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2471 DiscardNonCommittedEntries();
2472
Lukasz Anforowicz9ee83c272020-12-01 20:14:052473 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262474}
2475
[email protected]d1198fd2012-08-13 22:50:192476void NavigationControllerImpl::SetSessionStorageNamespace(
[email protected]fdac6ade2013-07-20 01:06:302477 const std::string& partition_id,
[email protected]8ff00d72012-10-23 19:12:212478 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192479 if (!session_storage_namespace)
2480 return;
2481
2482 // We can't overwrite an existing SessionStorage without violating spec.
2483 // Attempts to do so may give a tab access to another tab's session storage
2484 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152485 bool successful_insert =
2486 session_storage_namespace_map_
2487 .insert(
2488 make_pair(partition_id, static_cast<SessionStorageNamespaceImpl*>(
2489 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302490 .second;
2491 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
[email protected]d1198fd2012-08-13 22:50:192492}
2493
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572494bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Aran Gilman37d11632019-10-08 23:07:152495 return IsInitialNavigation() && !GetLastCommittedEntry() &&
Carlos Caballeroede6f8c2021-01-28 11:01:502496 !frame_tree_.has_accessed_initial_main_document();
[email protected]aa62afd2014-04-22 19:22:462497}
2498
Aran Gilman37d11632019-10-08 23:07:152499SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
2500 SiteInstance* instance) {
[email protected]fdac6ade2013-07-20 01:06:302501 std::string partition_id;
2502 if (instance) {
2503 // TODO(ajwong): When GetDefaultSessionStorageNamespace() goes away, remove
danakjf26536bf2020-09-10 00:46:132504 // this if statement so |instance| must not be null.
Aran Gilman37d11632019-10-08 23:07:152505 partition_id = GetContentClient()->browser()->GetStoragePartitionIdForSite(
2506 browser_context_, instance->GetSiteURL());
[email protected]fdac6ade2013-07-20 01:06:302507 }
[email protected]d1198fd2012-08-13 22:50:192508
[email protected]fdac6ade2013-07-20 01:06:302509 // TODO(ajwong): Should this use the |partition_id| directly rather than
2510 // re-lookup via |instance|? http://crbug.com/142685
2511 StoragePartition* partition =
michaelnbacbcbd2016-02-09 00:32:032512 BrowserContext::GetStoragePartition(browser_context_, instance);
2513 DOMStorageContextWrapper* context_wrapper =
2514 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2515
2516 SessionStorageNamespaceMap::const_iterator it =
2517 session_storage_namespace_map_.find(partition_id);
2518 if (it != session_storage_namespace_map_.end()) {
2519 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152520 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2521 ->IsFromContext(context_wrapper));
michaelnbacbcbd2016-02-09 00:32:032522 return it->second.get();
2523 }
2524
2525 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102526 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2527 SessionStorageNamespaceImpl::Create(context_wrapper);
2528 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2529 session_storage_namespace.get();
2530 session_storage_namespace_map_[partition_id] =
2531 std::move(session_storage_namespace);
[email protected]fdac6ade2013-07-20 01:06:302532
Daniel Murphy31bbb8b12018-02-07 21:44:102533 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302534}
2535
2536SessionStorageNamespace*
2537NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
2538 // TODO(ajwong): Remove if statement in GetSessionStorageNamespace().
Ivan Kotenkov2c0d2bb32017-11-01 15:41:282539 return GetSessionStorageNamespace(nullptr);
[email protected]fdac6ade2013-07-20 01:06:302540}
2541
2542const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572543NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302544 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552545}
[email protected]d202a7c2012-01-04 07:53:472546
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572547bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562548 return needs_reload_;
2549}
[email protected]a26023822011-12-29 00:23:552550
[email protected]46bb5e9c2013-10-03 22:16:472551void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412552 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2553}
2554
2555void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472556 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412557 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542558
2559 if (last_committed_entry_index_ != -1) {
2560 entries_[last_committed_entry_index_]->SetTransitionType(
2561 ui::PAGE_TRANSITION_RELOAD);
2562 }
[email protected]46bb5e9c2013-10-03 22:16:472563}
2564
[email protected]d202a7c2012-01-04 07:53:472565void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572566 DCHECK_LT(index, GetEntryCount());
2567 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312568 DiscardNonCommittedEntries();
2569
2570 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122571 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312572 last_committed_entry_index_--;
2573}
2574
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572575NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272576 // If there is no pending_entry_, there should be no pending_entry_index_.
2577 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2578
2579 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362580 // at that index. An exception is while a reload of a post commit error page
2581 // is ongoing; in that case pending entry will point to the entry replaced
2582 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272583 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362584 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2585 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272586
[email protected]022af742011-12-28 18:37:252587 return pending_entry_;
2588}
2589
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572590int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272591 // The pending entry index must always be less than the number of entries.
2592 // If there are no entries, it must be exactly -1.
2593 DCHECK_LT(pending_entry_index_, GetEntryCount());
2594 DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:552595 return pending_entry_index_;
2596}
2597
avi25764702015-06-23 15:43:372598void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572599 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362600 bool replace,
2601 bool was_post_commit_error) {
mastiz7eddb5f72016-06-23 09:52:452602 DCHECK(!ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2603 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202604
avi5cad4912015-06-19 05:25:442605 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2606 // need to keep continuity with the pending entry, so copy the pending entry's
2607 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2608 // then the renderer navigated on its own, independent of the pending entry,
2609 // so don't copy anything.
2610 if (pending_entry_ && pending_entry_index_ == -1)
2611 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202612
arthursonzogni69a6a1b2019-09-17 09:23:002613 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:202614
creisee17e932015-07-17 17:56:222615 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362616 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102617 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572618 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362619 // If the new entry is a post-commit error page, we store the current last
2620 // committed entry to the side so that we can put it back when navigating
2621 // away from the error.
2622 if (was_post_commit_error) {
2623 DCHECK(!entry_replaced_by_post_commit_error_);
2624 entry_replaced_by_post_commit_error_ =
2625 std::move(entries_[last_committed_entry_index_]);
2626 }
dcheng36b6aec92015-12-26 06:16:362627 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222628 return;
2629 }
[email protected]765b35502008-08-21 00:51:202630
creis37979a62015-08-04 19:48:182631 // We shouldn't see replace == true when there's no committed entries.
2632 DCHECK(!replace);
2633
Michael Thiessen9b14d512019-09-23 21:19:472634 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202635
Shivani Sharmad8c8d652019-02-13 17:27:572636 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202637
dcheng36b6aec92015-12-26 06:16:362638 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202639 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292640}
2641
Shivani Sharmad8c8d652019-02-13 17:27:572642void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162643 if (entries_.size() < max_entry_count())
2644 return;
2645
2646 DCHECK_EQ(max_entry_count(), entries_.size());
2647 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572648 CHECK_EQ(pending_entry_index_, -1);
2649
2650 int index = 0;
2651 if (base::FeatureList::IsEnabled(
2652 features::kHistoryManipulationIntervention)) {
2653 // Retrieve the oldest skippable entry.
2654 for (; index < GetEntryCount(); index++) {
2655 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2656 break;
2657 }
2658 }
2659
2660 // If there is no skippable entry or if it is the last committed entry then
2661 // fall back to pruning the oldest entry. It is not safe to prune the last
2662 // committed entry.
2663 if (index == GetEntryCount() || index == last_committed_entry_index_)
2664 index = 0;
2665
2666 bool should_succeed = RemoveEntryAtIndex(index);
2667 DCHECK_EQ(true, should_succeed);
2668
2669 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252670}
2671
clamy3cb9bea92018-07-10 12:42:022672void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162673 ReloadType reload_type,
2674 int sandboxed_source_frame_tree_node_id) {
Alexander Timin3a92df72019-09-20 11:59:502675 TRACE_EVENT0("navigation",
2676 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272677 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022678 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362679 if (pending_entry_index_ != -1) {
2680 // The pending entry may not be in entries_ if a post-commit error page is
2681 // showing.
2682 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2683 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2684 }
clamy3cb9bea92018-07-10 12:42:022685 DCHECK(!IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572686 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:012687 needs_reload_ = false;
Carlos Caballero40b0efd2021-01-26 11:55:002688 FrameTreeNode* root = frame_tree_.root();
Arthur Sonzogni620cec62018-12-13 13:08:572689 int nav_entry_id = pending_entry_->GetUniqueID();
2690
[email protected]83c2e232011-10-07 21:36:462691 // If we were navigating to a slow-to-commit page, and the user performs
2692 // a session history navigation to the last committed page, RenderViewHost
2693 // will force the throbber to start, but WebKit will essentially ignore the
2694 // navigation, and won't send a message to stop the throbber. To prevent this
2695 // from happening, we drop the navigation here and stop the slow-to-commit
2696 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022697 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:082698 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:272699 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
[email protected]ec6c05f2013-10-23 18:41:572700 delegate_->Stop();
[email protected]6a13a6c2011-12-20 21:47:122701
[email protected]83c2e232011-10-07 21:36:462702 DiscardNonCommittedEntries();
2703 return;
2704 }
2705
creisce0ef3572017-01-26 17:53:082706 // Compare FrameNavigationEntries to see which frames in the tree need to be
2707 // navigated.
clamy3cb9bea92018-07-10 12:42:022708 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2709 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Alex Moshchuk3a4e77a2020-05-29 21:32:572710 FindFramesToNavigate(root, reload_type, &same_document_loads,
2711 &different_document_loads);
creis4e2ecb72015-06-20 00:46:302712
2713 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572714 // We were unable to match any frames to navigate. This can happen if a
2715 // history navigation targets a subframe that no longer exists
2716 // (https://crbug.com/705550). In this case, we need to update the current
2717 // history entry to the pending one but keep the main document loaded. We
2718 // also need to ensure that observers are informed about the updated
2719 // current history entry (e.g., for greying out back/forward buttons), and
2720 // that renderer processes update their history offsets. The easiest way
2721 // to do all that is to schedule a "redundant" same-document navigation in
2722 // the main frame.
2723 //
2724 // Note that we don't want to remove this history entry, as it might still
2725 // be valid later, since a frame that it's targeting may be recreated.
2726 //
2727 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
2728 // need to repeat history navigations until finding the one that works.
2729 // Consider changing this behavior to keep looking for the first valid
2730 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:022731 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422732 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572733 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:572734 ReloadType::NONE /* reload_type */,
2735 true /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422736 false /* is_history_navigation_in_new_child */);
clamy3cb9bea92018-07-10 12:42:022737 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572738 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:022739 DiscardPendingEntry(false);
2740 return;
2741 }
Alex Moshchuk3a4e77a2020-05-29 21:32:572742 same_document_loads.push_back(std::move(navigation_request));
2743
2744 // Sanity check that we never take this branch for any kinds of reloads,
2745 // as those should've queued a different-document load in the main frame.
2746 DCHECK(!is_forced_reload);
2747 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:302748 }
2749
Dave Tapuska8bfd84c2019-03-26 20:47:162750 // If |sandboxed_source_frame_node_id| is valid, then track whether this
2751 // navigation affects any frame outside the frame's subtree.
2752 if (sandboxed_source_frame_tree_node_id !=
2753 FrameTreeNode::kFrameTreeNodeInvalidId) {
2754 bool navigates_inside_tree =
2755 DoesSandboxNavigationStayWithinSubtree(
2756 sandboxed_source_frame_tree_node_id, same_document_loads) &&
2757 DoesSandboxNavigationStayWithinSubtree(
2758 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:502759 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:162760 // the number of pages that trigger this.
2761 FrameTreeNode* sandbox_source_frame_tree_node =
2762 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
2763 if (sandbox_source_frame_tree_node) {
2764 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
2765 sandbox_source_frame_tree_node->current_frame_host(),
2766 navigates_inside_tree
2767 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
2768 : blink::mojom::WebFeature::
2769 kSandboxBackForwardAffectsFramesOutsideSubtree);
2770 }
Dave Tapuska855c1e12019-08-23 20:45:522771
2772 // If the navigation occurred outside the tree discard it because
2773 // the sandboxed frame didn't have permission to navigate outside
2774 // its tree. If it is possible that the navigation is both inside and
2775 // outside the frame tree and we discard it entirely because we don't
2776 // want to end up in a history state that didn't exist before.
2777 if (base::FeatureList::IsEnabled(
2778 features::kHistoryPreventSandboxedNavigation) &&
2779 !navigates_inside_tree) {
2780 DiscardPendingEntry(false);
2781 return;
2782 }
Dave Tapuska8bfd84c2019-03-26 20:47:162783 }
2784
Carlos Caballero539a421c2020-07-06 10:25:572785 // BackForwardCache:
2786 // Navigate immediately if the document is in the BackForwardCache.
2787 if (back_forward_cache_.GetEntry(nav_entry_id)) {
2788 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
2789 DCHECK_EQ(reload_type, ReloadType::NONE);
2790 auto navigation_request = CreateNavigationRequestFromEntry(
2791 root, pending_entry_, pending_entry_->GetFrameEntry(root),
2792 ReloadType::NONE, false /* is_same_document_history_load */,
2793 false /* is_history_navigation_in_new_child */);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052794 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:572795
2796 return;
2797 }
2798
2799 // History navigation might try to reuse a specific BrowsingInstance, already
2800 // used by a page in the cache. To avoid having two different main frames that
2801 // live in the same BrowsingInstance, evict the all pages with this
2802 // BrowsingInstance from the cache.
2803 //
2804 // For example, take the following scenario:
2805 //
2806 // A1 = Some page on a.com
2807 // A2 = Some other page on a.com
2808 // B3 = An uncacheable page on b.com
2809 //
2810 // Then the following navigations occur:
2811 // A1->A2->B3->A1
2812 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
2813 // take its place) and A1 will be created in the same BrowsingInstance (and
2814 // SiteInstance), as A2.
2815 //
2816 // If we didn't do anything, both A1 and A2 would remain alive in the same
2817 // BrowsingInstance/SiteInstance, which is unsupported by
2818 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
2819 // A2 from the cache.
2820 if (pending_entry_->site_instance()) {
2821 back_forward_cache_.EvictFramesInRelatedSiteInstances(
2822 pending_entry_->site_instance());
2823 }
2824
clamy3cb9bea92018-07-10 12:42:022825 // This call does not support re-entrancy. See http://crbug.com/347742.
2826 CHECK(!in_navigate_to_pending_entry_);
2827 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:302828
arthursonzogni66f711c2019-10-08 14:40:362829 // It is not possible to delete the pending NavigationEntry while navigating
2830 // to it. Grab a reference to delay potential deletion until the end of this
2831 // function.
2832 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
2833
creis4e2ecb72015-06-20 00:46:302834 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:022835 for (auto& item : same_document_loads) {
2836 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052837 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302838 }
clamy3cb9bea92018-07-10 12:42:022839 for (auto& item : different_document_loads) {
2840 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052841 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302842 }
clamy3cb9bea92018-07-10 12:42:022843
2844 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:302845}
2846
Alex Moshchuk3a4e77a2020-05-29 21:32:572847NavigationControllerImpl::HistoryNavigationAction
2848NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:302849 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:572850 ReloadType reload_type) {
Sreeja Kamishetty0be2ac52020-05-28 01:47:462851 // Only active frames can navigate:
2852 // - If the frame is in pending deletion, the browser already committed to
2853 // destroying this RenderFrameHost. See https://crbug.com/930278.
2854 // - If the frame is in back-forward cache, it's not allowed to navigate as it
2855 // should remain frozen. Ignore the request and evict the document from
2856 // back-forward cache.
Alex Moshchuke65c39272020-06-03 17:55:372857 //
2858 // If the frame is inactive, there's no need to recurse into subframes, which
2859 // should all be inactive as well.
Sreeja Kamishetty0be2ac52020-05-28 01:47:462860 if (frame->current_frame_host()->IsInactiveAndDisallowReactivation())
Alex Moshchuke65c39272020-06-03 17:55:372861 return HistoryNavigationAction::kStopLooking;
arthursonzogni03f76152019-02-12 10:35:202862
Alex Moshchuk3a4e77a2020-05-29 21:32:572863 // If there's no last committed entry, there is no previous history entry to
2864 // compare against, so fall back to a different-document load. Note that we
2865 // should only reach this case for the root frame and not descend further
2866 // into subframes.
2867 if (!GetLastCommittedEntry()) {
2868 DCHECK(frame->IsMainFrame());
2869 return HistoryNavigationAction::kDifferentDocument;
2870 }
2871
2872 // Reloads should result in a different-document load. Note that reloads may
2873 // also happen via the |needs_reload_| mechanism where the reload_type is
2874 // NONE, so detect this by comparing whether we're going to the same
2875 // entry that we're currently on. Similarly to above, only main frames
2876 // should reach this. Note that subframes support reloads, but that's done
2877 // via a different path that doesn't involve FindFramesToNavigate (see
2878 // RenderFrameHost::Reload()).
2879 if (reload_type != ReloadType::NONE ||
2880 pending_entry_index_ == last_committed_entry_index_) {
2881 DCHECK(frame->IsMainFrame());
2882 return HistoryNavigationAction::kDifferentDocument;
2883 }
2884
Alex Moshchuk47d1a4bd2020-06-01 22:15:342885 // If there is no new FrameNavigationEntry for the frame, ignore the
2886 // load. For example, this may happen when going back to an entry before a
2887 // frame was created. Suppose we commit a same-document navigation that also
2888 // results in adding a new subframe somewhere in the tree. If we go back,
2889 // the new subframe will be missing a FrameNavigationEntry in the previous
2890 // NavigationEntry, but we shouldn't delete or change what's loaded in
2891 // it.
2892 //
Alex Moshchuke65c39272020-06-03 17:55:372893 // Note that in this case, there is no need to keep looking for navigations
2894 // in subframes, which would be missing FrameNavigationEntries as well.
2895 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:342896 // It's important to check this before checking |old_item| below, since both
2897 // might be null, and in that case we still shouldn't change what's loaded in
2898 // this frame. Note that scheduling any loads assumes that |new_item| is
2899 // non-null. See https://crbug.com/1088354.
2900 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
2901 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:372902 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:342903
Alex Moshchuk3a4e77a2020-05-29 21:32:572904 // If there is no old FrameNavigationEntry, schedule a different-document
2905 // load.
2906 //
creis225a7432016-06-03 22:56:272907 // TODO(creis): Store the last committed FrameNavigationEntry to use here,
2908 // rather than assuming the NavigationEntry has up to date info on subframes.
Alex Moshchuk3a4e77a2020-05-29 21:32:572909 // Note that this may require sharing FrameNavigationEntries between
2910 // NavigationEntries (https://crbug.com/373041) as a prerequisite, since
2911 // otherwise the stored FrameNavigationEntry might get stale (e.g., after
2912 // subframe navigations, or in the case where we don't find any frames to
2913 // navigate and ignore a back/forward navigation while moving to a different
2914 // NavigationEntry, as in https://crbug.com/705550).
creis4e2ecb72015-06-20 00:46:302915 FrameNavigationEntry* old_item =
2916 GetLastCommittedEntry()->GetFrameEntry(frame);
Alex Moshchuk3a4e77a2020-05-29 21:32:572917 if (!old_item)
2918 return HistoryNavigationAction::kDifferentDocument;
2919
Alex Moshchuk3a4e77a2020-05-29 21:32:572920 // If the new item is not in the same SiteInstance, schedule a
2921 // different-document load. Newly restored items may not have a SiteInstance
2922 // yet, in which case it will be assigned on first commit.
2923 if (new_item->site_instance() &&
2924 new_item->site_instance() != old_item->site_instance())
2925 return HistoryNavigationAction::kDifferentDocument;
2926
2927 // Schedule a different-document load if the current RenderFrameHost is not
2928 // live. This case can happen for Ctrl+Back or after
2929 // a renderer crash.
2930 if (!frame->current_frame_host()->IsRenderFrameLive())
2931 return HistoryNavigationAction::kDifferentDocument;
2932
2933 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
creis54131692016-08-12 18:32:252934 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:492935 // sequence number but different item sequence number.
2936 if (new_item->document_sequence_number() ==
2937 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572938 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:492939 }
avib48cb312016-05-05 21:35:002940
Alex Moshchuk3a4e77a2020-05-29 21:32:572941 // Otherwise, if both item and document sequence numbers differ, this
2942 // should be a different document load.
2943 return HistoryNavigationAction::kDifferentDocument;
2944 }
2945
2946 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:372947 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:572948 DCHECK_EQ(new_item->document_sequence_number(),
2949 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:372950 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:572951}
2952
2953void NavigationControllerImpl::FindFramesToNavigate(
2954 FrameTreeNode* frame,
2955 ReloadType reload_type,
2956 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
2957 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
2958 DCHECK(pending_entry_);
2959 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
2960
2961 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
2962
2963 if (action == HistoryNavigationAction::kSameDocument) {
2964 std::unique_ptr<NavigationRequest> navigation_request =
2965 CreateNavigationRequestFromEntry(
2966 frame, pending_entry_, new_item, reload_type,
2967 true /* is_same_document_history_load */,
2968 false /* is_history_navigation_in_new_child */);
2969 if (navigation_request) {
2970 // Only add the request if was properly created. It's possible for the
2971 // creation to fail in certain cases, e.g. when the URL is invalid.
2972 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:302973 }
Lei Zhang96031532019-10-10 19:05:472974
Alex Moshchuk3a4e77a2020-05-29 21:32:572975 // TODO(avi, creis): This is a bug; we should not return here. Rather, we
2976 // should continue on and navigate all child frames which have also
2977 // changed. This bug is the cause of <https://crbug.com/542299>, which is
2978 // a NC_IN_PAGE_NAVIGATION renderer kill.
2979 //
2980 // However, this bug is a bandaid over a deeper and worse problem. Doing a
2981 // pushState immediately after loading a subframe is a race, one that no
2982 // web page author expects. If we fix this bug, many large websites break.
2983 // For example, see <https://crbug.com/598043> and the spec discussion at
2984 // <https://github.com/whatwg/html/issues/1191>.
2985 //
2986 // For now, we accept this bug, and hope to resolve the race in a
2987 // different way that will one day allow us to fix this.
2988 return;
2989 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:472990 std::unique_ptr<NavigationRequest> navigation_request =
2991 CreateNavigationRequestFromEntry(
2992 frame, pending_entry_, new_item, reload_type,
2993 false /* is_same_document_history_load */,
2994 false /* is_history_navigation_in_new_child */);
2995 if (navigation_request) {
2996 // Only add the request if was properly created. It's possible for the
2997 // creation to fail in certain cases, e.g. when the URL is invalid.
2998 different_document_loads->push_back(std::move(navigation_request));
2999 }
3000 // For a different document, the subframes will be destroyed, so there's
3001 // no need to consider them.
3002 return;
Alex Moshchuke65c39272020-06-03 17:55:373003 } else if (action == HistoryNavigationAction::kStopLooking) {
3004 return;
creis4e2ecb72015-06-20 00:46:303005 }
3006
3007 for (size_t i = 0; i < frame->child_count(); i++) {
clamy3cb9bea92018-07-10 12:42:023008 FindFramesToNavigate(frame->child_at(i), reload_type, same_document_loads,
creis4e2ecb72015-06-20 00:46:303009 different_document_loads);
3010 }
3011}
3012
clamy21718cc22018-06-13 13:34:243013void NavigationControllerImpl::NavigateWithoutEntry(
3014 const LoadURLParams& params) {
3015 // Find the appropriate FrameTreeNode.
3016 FrameTreeNode* node = nullptr;
3017 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3018 !params.frame_name.empty()) {
3019 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
Carlos Caballero40b0efd2021-01-26 11:55:003020 ? frame_tree_.FindByID(params.frame_tree_node_id)
3021 : frame_tree_.FindByName(params.frame_name);
clamy21718cc22018-06-13 13:34:243022 }
3023
3024 // If no FrameTreeNode was specified, navigate the main frame.
3025 if (!node)
Carlos Caballero40b0efd2021-01-26 11:55:003026 node = frame_tree_.root();
clamy21718cc22018-06-13 13:34:243027
Camille Lamy5193caa2018-10-12 11:59:423028 // Compute overrides to the LoadURLParams for |override_user_agent|,
3029 // |should_replace_current_entry| and |has_user_gesture| that will be used
3030 // both in the creation of the NavigationEntry and the NavigationRequest.
3031 // Ideally, the LoadURLParams themselves would be updated, but since they are
3032 // passed as a const reference, this is not possible.
3033 // TODO(clamy): When we only create a NavigationRequest, move this to
3034 // CreateNavigationRequestFromLoadURLParams.
3035 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3036 GetLastCommittedEntry());
3037
3038 // Don't allow an entry replacement if there is no entry to replace.
3039 // http://crbug.com/457149
3040 bool should_replace_current_entry =
3041 params.should_replace_current_entry && entries_.size();
3042
Scott Violet5ae6c42e2020-10-28 02:47:373043 ui::PageTransition transition_type = params.transition_type;
3044
clamy21718cc22018-06-13 13:34:243045 // Javascript URLs should not create NavigationEntries. All other navigations
3046 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243047 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373048 std::unique_ptr<NavigationEntryImpl> entry =
3049 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3050 should_replace_current_entry,
3051 params.has_user_gesture);
3052 // CreateNavigationEntryFromLoadParams() may modify the transition type.
3053 transition_type = entry->GetTransitionType();
clamy21718cc22018-06-13 13:34:243054 DiscardPendingEntry(false);
3055 SetPendingEntry(std::move(entry));
3056 }
3057
3058 // Renderer-debug URLs are sent to the renderer process immediately for
3059 // processing and don't need to create a NavigationRequest.
3060 // Note: this includes navigations to JavaScript URLs, which are considered
3061 // renderer-debug URLs.
3062 // Note: we intentionally leave the pending entry in place for renderer debug
3063 // URLs, unlike the cases below where we clear it if the navigation doesn't
3064 // proceed.
3065 if (IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483066 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3067 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153068 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483069 params.url, browser_context_)) {
3070 DiscardPendingEntry(false);
3071 return;
3072 }
3073
clamy21718cc22018-06-13 13:34:243074 HandleRendererDebugURL(node, params.url);
3075 return;
3076 }
3077
Antonio Sartori78a749f2020-11-30 12:03:393078 DCHECK(pending_entry_);
3079
clamy21718cc22018-06-13 13:34:243080 // Convert navigations to the current URL to a reload.
3081 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3082 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3083 // DevTools sometimes issues navigations to main frames that they do not
3084 // expect to see treated as reload, and it only works because they pass a
3085 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3086 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543087 ReloadType reload_type = params.reload_type;
3088 if (reload_type == ReloadType::NONE &&
3089 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043090 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243091 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243092 params.load_type ==
3093 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
3094 false /* is_reload */, false /* is_navigation_to_existing_entry */,
clamy21718cc22018-06-13 13:34:243095 GetLastCommittedEntry())) {
3096 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473097 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393098
3099 // If this is a reload of an existing FrameNavigationEntry and we had a
3100 // policy container for it, then we should copy it into the pending entry,
3101 // so that it is copied to the navigation request in
3102 // CreateNavigationRequestFromLoadParams later.
3103 if (GetLastCommittedEntry()) {
3104 FrameNavigationEntry* previous_frame_entry =
3105 GetLastCommittedEntry()->GetFrameEntry(node);
Titouan Rigoudy6ec70402021-02-02 15:42:193106 if (previous_frame_entry &&
3107 previous_frame_entry->policy_container_policies()) {
3108 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
3109 std::make_unique<PolicyContainerPolicies>(
3110 *previous_frame_entry->policy_container_policies()));
Antonio Sartori78a749f2020-11-30 12:03:393111 }
3112 }
3113 }
3114
3115 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
3116 // document (so no last commit), then we should copy the document polices from
3117 // RenderFrameHost's PolicyContainerHost. The NavigationRequest will create a
3118 // new PolicyContainerHost with the document policies from the
3119 // |pending_entry_|, and that PolicyContainerHost will be put in the final
3120 // RenderFrameHost for the navigation. This way, we ensure that we keep
3121 // enforcing the right policies on the initial empty document after the
3122 // reload.
3123 if (!GetLastCommittedEntry() && params.url.IsAboutBlank()) {
3124 if (node->current_frame_host() &&
3125 node->current_frame_host()->policy_container_host()) {
Titouan Rigoudy6ec70402021-02-02 15:42:193126 pending_entry_->GetFrameEntry(node)->set_policy_container_policies(
3127 std::make_unique<PolicyContainerPolicies>(
3128 node->current_frame_host()->policy_container_host()->policies()));
Antonio Sartori78a749f2020-11-30 12:03:393129 }
clamy21718cc22018-06-13 13:34:243130 }
3131
3132 // navigation_ui_data should only be present for main frame navigations.
3133 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
3134
Camille Lamy5193caa2018-10-12 11:59:423135 std::unique_ptr<NavigationRequest> request =
3136 CreateNavigationRequestFromLoadParams(
3137 node, params, override_user_agent, should_replace_current_entry,
John Abd-El-Malekd95f34382020-10-06 04:56:463138 params.has_user_gesture, NavigationDownloadPolicy(), reload_type,
Scott Violet5ae6c42e2020-10-28 02:47:373139 pending_entry_, pending_entry_->GetFrameEntry(node), transition_type);
clamy21718cc22018-06-13 13:34:243140
3141 // If the navigation couldn't start, return immediately and discard the
3142 // pending NavigationEntry.
3143 if (!request) {
3144 DiscardPendingEntry(false);
3145 return;
3146 }
3147
Camille Lamy5193caa2018-10-12 11:59:423148#if DCHECK_IS_ON()
3149 // Safety check that NavigationRequest and NavigationEntry match.
3150 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3151#endif
3152
clamy21718cc22018-06-13 13:34:243153 // This call does not support re-entrancy. See http://crbug.com/347742.
3154 CHECK(!in_navigate_to_pending_entry_);
3155 in_navigate_to_pending_entry_ = true;
3156
arthursonzogni66f711c2019-10-08 14:40:363157 // It is not possible to delete the pending NavigationEntry while navigating
3158 // to it. Grab a reference to delay potential deletion until the end of this
3159 // function.
3160 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3161
Lukasz Anforowicz9ee83c272020-12-01 20:14:053162 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243163
3164 in_navigate_to_pending_entry_ = false;
3165}
3166
clamyea99ea12018-05-28 13:54:233167void NavigationControllerImpl::HandleRendererDebugURL(
3168 FrameTreeNode* frame_tree_node,
3169 const GURL& url) {
3170 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243171 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3172 // the renderer process is not live, unless it is the initial navigation
3173 // of the tab.
clamyea99ea12018-05-28 13:54:233174 if (!IsInitialNavigation()) {
3175 DiscardNonCommittedEntries();
3176 return;
3177 }
Fergal Dalyecd3b0202020-06-25 01:57:373178 // The current frame is always a main frame. If IsInitialNavigation() is
3179 // true then there have been no navigations and any frames of this tab must
3180 // be in the same renderer process. If that has crashed then the only frame
3181 // that can be revived is the main frame.
3182 frame_tree_node->render_manager()
3183 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233184 }
3185 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3186}
3187
clamy21718cc22018-06-13 13:34:243188std::unique_ptr<NavigationEntryImpl>
3189NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3190 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423191 const LoadURLParams& params,
3192 bool override_user_agent,
3193 bool should_replace_current_entry,
3194 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393195 // Browser initiated navigations might not have a blob_url_loader_factory set
3196 // in params even if the navigation is to a blob URL. If that happens, lookup
3197 // the correct url loader factory to use here.
3198 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483199 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473200 // Resolve the blob URL in the storage partition associated with the target
3201 // frame. This is the storage partition the URL will be loaded in, and only
3202 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393203 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473204 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393205 }
3206
clamy21718cc22018-06-13 13:34:243207 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443208 // extra_headers in params are \n separated; navigation entries want \r\n.
3209 std::string extra_headers_crlf;
3210 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243211
3212 // For subframes, create a pending entry with a corresponding frame entry.
3213 if (!node->IsMainFrame()) {
Tommy C. Li03eee77a2019-02-05 02:07:443214 if (GetLastCommittedEntry()) {
3215 // Create an identical NavigationEntry with a new FrameNavigationEntry for
3216 // the target subframe.
3217 entry = GetLastCommittedEntry()->Clone();
3218 } else {
3219 // If there's no last committed entry, create an entry for about:blank
3220 // with a subframe entry for our destination.
3221 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
3222 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063223 GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103224 params.source_site_instance.get(), params.transition_type,
3225 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:373226 blob_url_loader_factory, should_replace_current_entry,
3227 GetWebContents()));
Tommy C. Li03eee77a2019-02-05 02:07:443228 }
Nasko Oskov18006bc2018-12-06 02:53:583229
clamy21718cc22018-06-13 13:34:243230 entry->AddOrUpdateFrameEntry(
3231 node, -1, -1, nullptr,
3232 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Lukasz Anforowicz435bcb582019-07-12 20:50:063233 params.url, base::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203234 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393235 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
3236 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reis7e2cb6d2021-01-26 01:27:163237 // a conversion of a new navigation into a reload, we will set the right
3238 // document policies there.
Titouan Rigoudy6ec70402021-02-02 15:42:193239 nullptr /* policy_container_policies */);
clamy21718cc22018-06-13 13:34:243240 } else {
3241 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243242 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063243 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103244 params.source_site_instance.get(), params.transition_type,
3245 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:373246 blob_url_loader_factory, should_replace_current_entry,
3247 GetWebContents()));
clamy21718cc22018-06-13 13:34:243248 entry->set_source_site_instance(
3249 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3250 entry->SetRedirectChain(params.redirect_chain);
3251 }
3252
3253 // Set the FTN ID (only used in non-site-per-process, for tests).
3254 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243255 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423256 entry->SetIsOverridingUserAgent(override_user_agent);
3257 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543258 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243259
clamy21718cc22018-06-13 13:34:243260 switch (params.load_type) {
3261 case LOAD_TYPE_DEFAULT:
3262 break;
3263 case LOAD_TYPE_HTTP_POST:
3264 entry->SetHasPostData(true);
3265 entry->SetPostData(params.post_data);
3266 break;
3267 case LOAD_TYPE_DATA:
3268 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3269 entry->SetVirtualURL(params.virtual_url_for_data_url);
3270#if defined(OS_ANDROID)
3271 entry->SetDataURLAsString(params.data_url_as_string);
3272#endif
3273 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3274 break;
clamy21718cc22018-06-13 13:34:243275 }
3276
3277 // TODO(clamy): NavigationEntry is meant for information that will be kept
3278 // after the navigation ended and therefore is not appropriate for
3279 // started_from_context_menu. Move started_from_context_menu to
3280 // NavigationUIData.
3281 entry->set_started_from_context_menu(params.started_from_context_menu);
3282
3283 return entry;
3284}
3285
clamyea99ea12018-05-28 13:54:233286std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423287NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3288 FrameTreeNode* node,
3289 const LoadURLParams& params,
3290 bool override_user_agent,
3291 bool should_replace_current_entry,
3292 bool has_user_gesture,
Charlie Harrison8c113a32019-01-07 16:08:293293 NavigationDownloadPolicy download_policy,
Camille Lamy5193caa2018-10-12 11:59:423294 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573295 NavigationEntryImpl* entry,
Scott Violet5ae6c42e2020-10-28 02:47:373296 FrameNavigationEntry* frame_entry,
3297 ui::PageTransition transition_type) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573298 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283299 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533300 // All renderer-initiated navigations must have an initiator_origin.
3301 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513302
Camille Lamy5193caa2018-10-12 11:59:423303 GURL url_to_load;
3304 GURL virtual_url;
Nasko Oskov03912102019-01-11 00:21:323305 base::Optional<url::Origin> origin_to_commit =
3306 frame_entry ? frame_entry->committed_origin() : base::nullopt;
3307
Camille Lamy2baa8022018-10-19 16:43:173308 // For main frames, rewrite the URL if necessary and compute the virtual URL
3309 // that should be shown in the address bar.
3310 if (node->IsMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423311 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173312 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423313 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423314
Camille Lamy2baa8022018-10-19 16:43:173315 // For DATA loads, override the virtual URL.
3316 if (params.load_type == LOAD_TYPE_DATA)
3317 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423318
Camille Lamy2baa8022018-10-19 16:43:173319 if (virtual_url.is_empty())
3320 virtual_url = url_to_load;
3321
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573322 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283323
Aran Gilman249eb122019-12-02 23:32:463324 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3325 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3326 // return different results, leading to a different URL in the
3327 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3328 // pending NavigationEntry, as we'll only make one call to
3329 // RewriteUrlForNavigation.
3330 VLOG_IF(1, (url_to_load != frame_entry->url()))
3331 << "NavigationRequest and FrameEntry have different URLs: "
3332 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283333
Camille Lamy2baa8022018-10-19 16:43:173334 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423335 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173336 // NavigationRequest.
3337 } else {
3338 url_to_load = params.url;
3339 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243340 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173341 }
Camille Lamy5193caa2018-10-12 11:59:423342
Ehsan Karamad44fc72112019-02-26 18:15:473343 if (node->render_manager()->is_attaching_inner_delegate()) {
3344 // Avoid starting any new navigations since this node is now preparing for
3345 // attaching an inner delegate.
3346 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203347 }
Camille Lamy5193caa2018-10-12 11:59:423348
Camille Lamy5193caa2018-10-12 11:59:423349 if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load))
3350 return nullptr;
3351
Nasko Oskov03912102019-01-11 00:21:323352 if (!DoesURLMatchOriginForNavigation(url_to_load, origin_to_commit)) {
3353 DCHECK(false) << " url:" << url_to_load
3354 << " origin:" << origin_to_commit.value();
3355 return nullptr;
3356 }
3357
Camille Lamy5193caa2018-10-12 11:59:423358 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403359 blink::PreviewsState previews_state =
3360 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
Camille Lamy5193caa2018-10-12 11:59:423361 if (!node->IsMainFrame()) {
3362 // For subframes, use the state of the top-level frame.
3363 previews_state = node->frame_tree()
3364 ->root()
3365 ->current_frame_host()
3366 ->last_navigation_previews_state();
3367 }
3368
Camille Lamy5193caa2018-10-12 11:59:423369 // This will be used to set the Navigation Timing API navigationStart
3370 // parameter for browser navigations in new tabs (intents, tabs opened through
3371 // "Open link in new tab"). If the navigation must wait on the current
3372 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3373 // will be updated when the BeforeUnload ack is received.
3374 base::TimeTicks navigation_start = base::TimeTicks::Now();
3375
danakjd83d706d2020-11-25 22:11:123376 // Look for a pending commit that is to another document in this
3377 // FrameTreeNode. If one exists, then the last committed URL will not be the
3378 // current URL by the time this navigation commits.
3379 bool has_pending_cross_document_commit =
3380 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493381 bool is_currently_error_page = node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123382
danakjb952ef12021-01-14 19:58:493383 mojom::NavigationType navigation_type = GetNavigationType(
3384 /*old_url=*/node->current_url(),
3385 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3386 has_pending_cross_document_commit, is_currently_error_page,
3387 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423388
3389 // Create the NavigationParams based on |params|.
3390
3391 bool is_view_source_mode = virtual_url.SchemeIs(kViewSourceScheme);
Charlie Harrison8c113a32019-01-07 16:08:293392
3393 // Update |download_policy| if the virtual URL is view-source. Why do this
3394 // now? Possibly the URL could be rewritten to a view-source via some URL
3395 // handler.
3396 if (is_view_source_mode)
Yao Xiaoaf79ca9b2019-03-12 19:54:183397 download_policy.SetDisallowed(NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293398
Camille Lamy5193caa2018-10-12 11:59:423399 const GURL& history_url_for_data_url =
3400 params.base_url_for_data_url.is_empty() ? GURL() : virtual_url;
Scott Violet5ae6c42e2020-10-28 02:47:373401 // Don't use |params.transition_type| as calling code may supply a different
3402 // value.
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513403 mojom::CommonNavigationParamsPtr common_params =
3404 mojom::CommonNavigationParams::New(
3405 url_to_load, params.initiator_origin,
3406 blink::mojom::Referrer::New(params.referrer.url,
3407 params.referrer.policy),
Scott Violet5ae6c42e2020-10-28 02:47:373408 transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513409 should_replace_current_entry, params.base_url_for_data_url,
3410 history_url_for_data_url, previews_state, navigation_start,
3411 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
arthursonzogniaf7c62c52020-02-12 10:49:413412 params.post_data, network::mojom::SourceLocation::New(),
3413 params.started_from_context_menu, has_user_gesture,
David Bokan71e81512020-08-18 17:19:463414 false /* has_text_fragment_token */, CreateInitiatorCSPInfo(),
3415 std::vector<int>(), params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513416 false /* is_history_navigation_in_new_child_frame */,
Charlie Hu5ffc0152019-12-06 15:59:533417 params.input_start);
Camille Lamy5193caa2018-10-12 11:59:423418
Lucas Furukawa Gadania9c45682019-07-31 22:05:143419 mojom::CommitNavigationParamsPtr commit_params =
3420 mojom::CommitNavigationParams::New(
Arthur Sonzogni367718c2021-01-25 20:55:393421 frame_entry->committed_origin(), override_user_agent,
3422 params.redirect_chain,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323423 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143424 std::vector<net::RedirectInfo>(),
3425 std::string() /* post_content_type */, common_params->url,
3426 common_params->method, params.can_load_local_resources,
3427 frame_entry->page_state(), entry->GetUniqueID(),
3428 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3429 -1 /* pending_history_list_offset */,
3430 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3431 params.should_clear_history_list ? 0 : GetEntryCount(),
3432 false /* was_discarded */, is_view_source_mode,
3433 params.should_clear_history_list, mojom::NavigationTiming::New(),
3434 base::nullopt /* appcache_host_id */,
3435 mojom::WasActivatedOption::kUnknown,
3436 base::UnguessableToken::Create() /* navigation_token */,
Lucas Furukawa Gadani97ea6382019-08-07 19:14:513437 std::vector<mojom::PrefetchedSignedExchangeInfoPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143438#if defined(OS_ANDROID)
3439 std::string(), /* data_url_as_string */
3440#endif
arthursonzogni14379782020-05-15 09:09:273441 !params.is_renderer_initiated, /* is_browser_initiated */
Sreeja Kamishetty46f762c2021-02-05 07:52:463442 node->frame_tree()->is_prerendering() /* is_prerendering */,
Tsuyoshi Horoe86d7702019-11-29 01:52:473443 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533444 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173445 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073446 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163447 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333448 false /* origin_agent_cluster */,
Maks Orlovichc66745a2020-06-30 17:40:023449 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213450 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Antonio Sartori1e09fd7b2020-08-10 14:16:173451 false /* is_cross_browsing_instance */,
Rakina Zata Amni60858132020-08-19 10:33:483452 std::vector<std::string>() /* forced_content_security_policies */,
Rakina Zata Amniba3eecb2020-11-02 10:12:283453 nullptr /* old_page_info */, -1 /* http_response_code */);
Camille Lamy5193caa2018-10-12 11:59:423454#if defined(OS_ANDROID)
3455 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143456 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423457 }
3458#endif
3459
Lucas Furukawa Gadania9c45682019-07-31 22:05:143460 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423461
3462 // A form submission may happen here if the navigation is a renderer-initiated
3463 // form submission that took the OpenURL path.
3464 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3465
3466 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3467 std::string extra_headers_crlf;
3468 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093469
3470 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143471 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083472 !params.is_renderer_initiated, params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453473 params.initiator_frame_token.has_value()
3474 ? &(params.initiator_frame_token.value())
3475 : nullptr,
3476 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3477 request_body,
John Delaney50425f82020-04-07 16:26:213478 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
3479 params.impression);
Yao Xiaodc5ed102019-06-04 19:19:093480 navigation_request->set_from_download_cross_origin_redirect(
3481 params.from_download_cross_origin_redirect);
3482 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423483}
3484
3485std::unique_ptr<NavigationRequest>
3486NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233487 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573488 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233489 FrameNavigationEntry* frame_entry,
3490 ReloadType reload_type,
3491 bool is_same_document_history_load,
Arthur Hemerybee4a752019-05-29 10:50:553492 bool is_history_navigation_in_new_child_frame) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343493 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233494 GURL dest_url = frame_entry->url();
Nasko Oskov03912102019-01-11 00:21:323495 base::Optional<url::Origin> origin_to_commit =
3496 frame_entry->committed_origin();
3497
clamyea99ea12018-05-28 13:54:233498 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013499 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573500 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233501 // We may have been redirected when navigating to the current URL.
3502 // Use the URL the user originally intended to visit as signaled by the
3503 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013504 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573505 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233506 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323507 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233508 }
3509
Ehsan Karamad44fc72112019-02-26 18:15:473510 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3511 // Avoid starting any new navigations since this node is now preparing for
3512 // attaching an inner delegate.
3513 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203514 }
3515
Camille Lamy5193caa2018-10-12 11:59:423516 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573517 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233518 return nullptr;
3519 }
3520
Nasko Oskov03912102019-01-11 00:21:323521 if (!DoesURLMatchOriginForNavigation(dest_url, origin_to_commit)) {
3522 DCHECK(false) << " url:" << dest_url
3523 << " origin:" << origin_to_commit.value();
3524 return nullptr;
3525 }
3526
clamyea99ea12018-05-28 13:54:233527 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403528 blink::PreviewsState previews_state =
3529 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
clamyea99ea12018-05-28 13:54:233530 if (!frame_tree_node->IsMainFrame()) {
3531 // For subframes, use the state of the top-level frame.
3532 previews_state = frame_tree_node->frame_tree()
3533 ->root()
3534 ->current_frame_host()
3535 ->last_navigation_previews_state();
clamyea99ea12018-05-28 13:54:233536 }
3537
clamyea99ea12018-05-28 13:54:233538 // This will be used to set the Navigation Timing API navigationStart
3539 // parameter for browser navigations in new tabs (intents, tabs opened through
3540 // "Open link in new tab"). If the navigation must wait on the current
3541 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3542 // will be updated when the BeforeUnload ack is received.
3543 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233544
danakjd83d706d2020-11-25 22:11:123545 // Look for a pending commit that is to another document in this
3546 // FrameTreeNode. If one exists, then the last committed URL will not be the
3547 // current URL by the time this navigation commits.
3548 bool has_pending_cross_document_commit =
3549 frame_tree_node->render_manager()
3550 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493551 bool is_currently_error_page =
3552 frame_tree_node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123553
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513554 mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123555 /*old_url=*/frame_tree_node->current_url(),
3556 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493557 has_pending_cross_document_commit, is_currently_error_page,
3558 is_same_document_history_load);
danakjd412a3e2021-02-05 22:36:353559 if (navigation_type == mojom::NavigationType::HISTORY_SAME_DOCUMENT &&
3560 frame_tree_node->current_frame_host()->GetLastCommittedURL() == GURL()) {
3561 base::debug::DumpWithoutCrashing();
3562 }
Camille Lamy5193caa2018-10-12 11:59:423563
3564 // A form submission may happen here if the navigation is a
3565 // back/forward/reload navigation that does a form resubmission.
3566 scoped_refptr<network::ResourceRequestBody> request_body;
3567 std::string post_content_type;
3568 if (frame_entry->method() == "POST") {
3569 request_body = frame_entry->GetPostData(&post_content_type);
3570 // Might have a LF at end.
3571 post_content_type =
3572 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL)
3573 .as_string();
3574 }
3575
3576 // Create the NavigationParams based on |entry| and |frame_entry|.
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513577 mojom::CommonNavigationParamsPtr common_params =
3578 entry->ConstructCommonNavigationParams(
3579 *frame_entry, request_body, dest_url,
3580 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
3581 navigation_type, previews_state, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533582 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513583 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553584 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423585
3586 // TODO(clamy): |intended_as_new_entry| below should always be false once
3587 // Reload no longer leads to this being called for a pending NavigationEntry
3588 // of index -1.
Lucas Furukawa Gadania9c45682019-07-31 22:05:143589 mojom::CommitNavigationParamsPtr commit_params =
3590 entry->ConstructCommitNavigationParams(
3591 *frame_entry, common_params->url, origin_to_commit,
3592 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3593 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533594 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3595 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143596 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423597
clamyea99ea12018-05-28 13:54:233598 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143599 frame_tree_node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083600 !entry->is_renderer_initiated(), false /* was_opener_suppressed */,
3601 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453602 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3603 entry->extra_headers(), frame_entry, entry, request_body,
3604 nullptr /* navigation_ui_data */, base::nullopt /* impression */);
clamyea99ea12018-05-28 13:54:233605}
3606
[email protected]d202a7c2012-01-04 07:53:473607void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213608 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273609 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403610
[email protected]2db9bd72012-04-13 20:20:563611 // We need to notify the ssl_manager_ before the web_contents_ so the
[email protected]df1af242009-05-01 00:11:403612 // location bar will have up-to-date information about the security style
3613 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133614 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403615
[email protected]7f924832014-08-09 05:57:223616 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:573617 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463618
[email protected]b0f724c2013-09-05 04:21:133619 // TODO(avi): Remove. http://crbug.com/170921
3620 NotificationDetails notification_details =
3621 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153622 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3623 Source<NavigationController>(this),
3624 notification_details);
initial.commit09911bf2008-07-26 23:55:293625}
3626
initial.commit09911bf2008-07-26 23:55:293627// static
[email protected]d202a7c2012-01-04 07:53:473628size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233629 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153630 return max_entry_count_for_testing_;
[email protected]8ff00d72012-10-23 19:12:213631 return kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233632}
3633
[email protected]d202a7c2012-01-04 07:53:473634void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223635 if (is_active && needs_reload_)
3636 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293637}
3638
[email protected]d202a7c2012-01-04 07:53:473639void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293640 if (!needs_reload_)
3641 return;
3642
Bo Liucdfa4b12018-11-06 00:21:443643 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3644 needs_reload_type_);
3645
initial.commit09911bf2008-07-26 23:55:293646 // Calling Reload() results in ignoring state, and not loading.
3647 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3648 // cached state.
avicc872d7242015-08-19 21:26:343649 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163650 NavigateToExistingPendingEntry(ReloadType::NONE,
3651 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343652 } else if (last_committed_entry_index_ != -1) {
arthursonzogni5c4c202d2017-04-25 23:41:273653 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343654 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163655 NavigateToExistingPendingEntry(ReloadType::NONE,
3656 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343657 } else {
3658 // If there is something to reload, the successful reload will clear the
3659 // |needs_reload_| flag. Otherwise, just do it here.
3660 needs_reload_ = false;
3661 }
initial.commit09911bf2008-07-26 23:55:293662}
3663
Carlos IL42b416592019-10-07 23:10:363664void NavigationControllerImpl::LoadPostCommitErrorPage(
3665 RenderFrameHost* render_frame_host,
3666 const GURL& url,
3667 const std::string& error_page_html,
3668 net::Error error) {
Sreeja Kamishetty0be2ac52020-05-28 01:47:463669 // Only active frames can load post-commit error pages:
3670 // - If the frame is in pending deletion, the browser already committed to
3671 // destroying this RenderFrameHost so ignore loading the error page.
3672 // - If the frame is in back-forward cache, it's not allowed to navigate as it
3673 // should remain frozen. Ignore the request and evict the document from
3674 // back-forward cache.
3675 if (static_cast<RenderFrameHostImpl*>(render_frame_host)
3676 ->IsInactiveAndDisallowReactivation()) {
John Delaney56f73332019-11-04 19:39:253677 return;
Sreeja Kamishetty0be2ac52020-05-28 01:47:463678 }
Rakina Zata Amni919b7922020-12-11 09:03:133679 RenderFrameHostImpl* rfhi =
3680 static_cast<RenderFrameHostImpl*>(render_frame_host);
3681 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:413682
3683 mojom::CommonNavigationParamsPtr common_params =
3684 CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:593685 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
3686 // the frame actually committed (e.g. iframe with "src" set to a
3687 // slow-responding URL). We should rewrite the URL to about:blank in this
3688 // case, as the renderer will only think a page is an error page if it has a
3689 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:133690 common_params->url = url.is_empty() ? GURL("about:blank") : url;
John Delaney131ad362019-08-08 21:57:413691 mojom::CommitNavigationParamsPtr commit_params =
3692 CreateCommitNavigationParams();
3693
arthursonzogni70ac7302020-05-28 08:49:053694 // Error pages have a fully permissive FramePolicy.
3695 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
3696 // error pages.
3697 commit_params->frame_policy = blink::FramePolicy();
3698
John Delaney131ad362019-08-08 21:57:413699 std::unique_ptr<NavigationRequest> navigation_request =
3700 NavigationRequest::CreateBrowserInitiated(
3701 node, std::move(common_params), std::move(commit_params),
Takashi Toyoshimae87b7be2021-01-22 11:51:083702 true /* browser_initiated */, false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:193703 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453704 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:063705 "" /* extra_headers */, nullptr /* frame_entry */,
3706 nullptr /* entry */, nullptr /* post_body */,
3707 nullptr /* navigation_ui_data */, base::nullopt /* impression */);
Carlos IL42b416592019-10-07 23:10:363708 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:413709 navigation_request->set_net_error(error);
3710 node->CreatedNavigationRequest(std::move(navigation_request));
3711 DCHECK(node->navigation_request());
3712 node->navigation_request()->BeginNavigation();
3713}
3714
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573715void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213716 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093717 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:153718 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143719 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293720}
3721
[email protected]d202a7c2012-01-04 07:53:473722void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363723 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553724 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363725 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293726
initial.commit09911bf2008-07-26 23:55:293727 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293728}
[email protected]765b35502008-08-21 00:51:203729
arthursonzogni69a6a1b2019-09-17 09:23:003730void NavigationControllerImpl::DiscardNonCommittedEntries() {
Michael Thiessen9b14d512019-09-23 21:19:473731 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:103732 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
3733 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:293734 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:473735 return;
Michael Thiessenc5676d22019-09-25 22:32:103736 }
avi45a72532015-04-07 21:01:453737 DiscardPendingEntry(false);
arthursonzogni69a6a1b2019-09-17 09:23:003738 if (delegate_)
3739 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:483740}
3741
avi7c6f35e2015-05-08 17:52:383742int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3743 int nav_entry_id) const {
3744 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3745 if (entries_[i]->GetUniqueID() == nav_entry_id)
3746 return i;
3747 }
3748 return -1;
3749}
3750
[email protected]d202a7c2012-01-04 07:53:473751void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573752 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253753 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573754 DCHECK_LE(max_index, source->GetEntryCount());
[email protected]e1cd5452010-08-26 18:03:253755 for (int i = 0; i < max_index; i++) {
Nasko Oskov923ca482020-12-04 21:20:463756 // TODO(creis): Once we start sharing FrameNavigationEntries between
3757 // NavigationEntries, it will not be safe to share them with another tab.
3758 // Must have a version of Clone that recreates them.
3759 entries_.insert(entries_.begin() + i, source->entries_[i]->Clone());
[email protected]e1cd5452010-08-26 18:03:253760 }
arthursonzogni5c4c202d2017-04-25 23:41:273761 DCHECK(pending_entry_index_ == -1 ||
3762 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:253763}
[email protected]c5b88d82012-10-06 17:03:333764
3765void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:183766 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:333767 get_timestamp_callback_ = get_timestamp_callback;
3768}
[email protected]8ff00d72012-10-23 19:12:213769
Shivani Sharmaffb32b82019-04-09 16:58:473770// History manipulation intervention:
3771void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:473772 bool replace_entry,
3773 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:403774 bool is_renderer_initiated,
3775 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:453776 // Note that for a subframe, previous_document_was_activated is true if the
3777 // gesture happened in any subframe (propagated to main frame) or in the main
3778 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:473779 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:273780 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:473781 if (last_committed_entry_index_ != -1) {
shivanigithube92c33da2020-09-14 13:01:413782 // This histogram always counts when navigating away from an entry,
3783 // irrespective of whether the skippable flag was changed or not, and
3784 // whether this entry is being reused or not.
Shivani Sharmaffb32b82019-04-09 16:58:473785 UMA_HISTOGRAM_BOOLEAN(
shivanigithube92c33da2020-09-14 13:01:413786 "Navigation.BackForward.SetShouldSkipOnBackForwardUI",
3787 GetEntryAtIndex(last_committed_entry_index_)
3788 ->should_skip_on_back_forward_ui());
Shivani Sharmaffb32b82019-04-09 16:58:473789 }
3790 return;
3791 }
3792 if (last_committed_entry_index_ == -1)
3793 return;
3794
Shivani Sharmac4cc8922019-04-18 03:11:173795 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473796 UMA_HISTOGRAM_BOOLEAN("Navigation.BackForward.SetShouldSkipOnBackForwardUI",
3797 true);
3798
Alexander Timine3ec4192020-04-20 16:39:403799 // Log UKM with the URL we are navigating away from.
3800 ukm::builders::HistoryManipulationIntervention(
3801 previous_page_load_ukm_source_id)
3802 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:473803}
3804
Shivani Sharmac4cc8922019-04-18 03:11:173805void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
3806 int reference_index,
3807 bool skippable) {
3808 auto* reference_entry = GetEntryAtIndex(reference_index);
3809 reference_entry->set_should_skip_on_back_forward_ui(skippable);
3810
3811 int64_t document_sequence_number =
3812 reference_entry->root_node()->frame_entry->document_sequence_number();
3813 for (int index = 0; index < GetEntryCount(); index++) {
3814 auto* entry = GetEntryAtIndex(index);
3815 if (entry->root_node()->frame_entry->document_sequence_number() ==
3816 document_sequence_number) {
3817 entry->set_should_skip_on_back_forward_ui(skippable);
3818 }
3819 }
3820}
3821
arthursonzogni66f711c2019-10-08 14:40:363822std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
3823NavigationControllerImpl::ReferencePendingEntry() {
3824 DCHECK(pending_entry_);
3825 auto pending_entry_ref =
3826 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
3827 pending_entry_refs_.insert(pending_entry_ref.get());
3828 return pending_entry_ref;
3829}
3830
3831void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
3832 // Ignore refs that don't correspond to the current pending entry.
3833 auto it = pending_entry_refs_.find(ref);
3834 if (it == pending_entry_refs_.end())
3835 return;
3836 pending_entry_refs_.erase(it);
3837
3838 if (!pending_entry_refs_.empty())
3839 return;
3840
3841 // The pending entry may be deleted before the last PendingEntryRef.
3842 if (!pending_entry_)
3843 return;
3844
3845 // We usually clear the pending entry when the matching NavigationRequest
3846 // fails, so that an arbitrary URL isn't left visible above a committed page.
3847 //
3848 // However, we do preserve the pending entry in some cases, such as on the
3849 // initial navigation of an unmodified blank tab. We also allow the delegate
3850 // to say when it's safe to leave aborted URLs in the omnibox, to let the
3851 // user edit the URL and try again. This may be useful in cases that the
3852 // committed page cannot be attacker-controlled. In these cases, we still
3853 // allow the view to clear the pending entry and typed URL if the user
3854 // requests (e.g., hitting Escape with focus in the address bar).
3855 //
3856 // Do not leave the pending entry visible if it has an invalid URL, since this
3857 // might be formatted in an unexpected or unsafe way.
3858 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:363859 bool should_preserve_entry =
3860 (pending_entry_ == GetVisibleEntry()) &&
3861 pending_entry_->GetURL().is_valid() &&
3862 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
3863 if (should_preserve_entry)
3864 return;
3865
3866 DiscardPendingEntry(true);
3867 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3868}
3869
Titouan Rigoudy6ec70402021-02-02 15:42:193870std::unique_ptr<PolicyContainerPolicies>
3871NavigationControllerImpl::ComputePolicyContainerPoliciesForFrameEntry(
Antonio Sartori78a749f2020-11-30 12:03:393872 RenderFrameHostImpl* rfh,
3873 bool is_same_document,
3874 NavigationRequest* request) {
Titouan Rigoudy6ec70402021-02-02 15:42:193875 if (!ShouldStorePolicyContainerPoliciesInFrameNavigationEntry(request))
Antonio Sartori78a749f2020-11-30 12:03:393876 return nullptr;
3877
3878 if (is_same_document) {
3879 FrameNavigationEntry* previous_frame_entry =
3880 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
3881
3882 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
3883 // ensure we always have a FrameNavigationEntry here.
3884 if (!previous_frame_entry)
3885 return nullptr;
3886
Titouan Rigoudy6ec70402021-02-02 15:42:193887 const PolicyContainerPolicies* previous_policies =
3888 previous_frame_entry->policy_container_policies();
Antonio Sartori78a749f2020-11-30 12:03:393889
Titouan Rigoudy6ec70402021-02-02 15:42:193890 if (!previous_policies)
Antonio Sartori78a749f2020-11-30 12:03:393891 return nullptr;
3892
3893 // Make a copy of the policy container for the new FrameNavigationEntry.
Titouan Rigoudy6ec70402021-02-02 15:42:193894 return std::make_unique<PolicyContainerPolicies>(*previous_policies);
Antonio Sartori78a749f2020-11-30 12:03:393895 }
3896
3897 if (!request->IsWaitingToCommit()) {
3898 // This is the initial, "fake" navigation to about:blank. The
3899 // NavigationRequest contains a dummy policy container, while the
3900 // RenderFrameHost already inherited the policy container from the
3901 // creator, so let's take the policies from there.
Titouan Rigoudy6ec70402021-02-02 15:42:193902 return std::make_unique<PolicyContainerPolicies>(
3903 rfh->policy_container_host()->policies());
Antonio Sartori78a749f2020-11-30 12:03:393904 }
3905
3906 // Take the policy container from the request since we did not move it
3907 // into the RFH yet.
Titouan Rigoudy6ec70402021-02-02 15:42:193908 return std::make_unique<PolicyContainerPolicies>(
3909 request->policy_container_host()->policies());
Antonio Sartori78a749f2020-11-30 12:03:393910}
3911
Carlos Caballeroede6f8c2021-01-28 11:01:503912void NavigationControllerImpl::SetHistoryOffsetAndLength(int history_offset,
3913 int history_length) {
3914 OPTIONAL_TRACE_EVENT2(
3915 "content", "NavigationControllerImpl::SetHistoryOffsetAndLength",
3916 "history_offset", history_offset, "history_length", history_length);
3917
3918 auto callback = base::BindRepeating(
3919 [](int history_offset, int history_length, RenderViewHostImpl* rvh) {
3920 if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) {
3921 broadcast->SetHistoryOffsetAndLength(history_offset, history_length);
3922 }
3923 },
3924 history_offset, history_length);
3925 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
3926}
3927
3928void NavigationControllerImpl::DidAccessInitialMainDocument() {
3929 // We may have left a failed browser-initiated navigation in the address bar
3930 // to let the user edit it and try again. Clear it now that content might
3931 // show up underneath it.
3932 if (!frame_tree_.IsLoading() && GetPendingEntry())
3933 DiscardPendingEntry(false);
3934
3935 // Update the URL display.
3936 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3937}
3938
Charlie Reiseaa61ce2021-02-10 08:10:583939void NavigationControllerImpl::HideEntryForEarlySwapAfterCrash(
3940 RenderFrameHostImpl* rfh) {
3941 // Nothing to do if there is no last committed entry to hide, or if we aren't
3942 // swapping RenderFrameHosts before commit after a crash.
3943 if (!GetLastCommittedEntry() || ShouldSkipEarlyCommitPendingForCrashedFrame())
3944 return;
3945
3946 // Create a temporary about:blank NavigationEntry to show if we swap to a
3947 // scriptable RenderFrameHost before the navigation commits (after a crash).
3948 // The previous last committed entry will be set aside and restored if we go
3949 // anywhere else or attempt a reload, just like a post-commit error page.
3950 //
3951 // Other uses of post-commit error pages will put the original entry back in
3952 // RendererDidNavigate before creating a subsequent post-commit error page in
3953 // InsertOrReplaceEntry.
3954 //
3955 // TODO(https://crbug.com/1072817): Remove this hack after we stop swapping
3956 // RenderFrameHosts before the navigation commits.
3957 InsertOrReplaceEntry(
3958 std::make_unique<NavigationEntryImpl>(
3959 rfh->GetSiteInstance(), GURL(url::kAboutBlankURL), Referrer(),
3960 url::Origin() /* initiator origin */,
3961 base::string16() /* extra_headers */, ui::PAGE_TRANSITION_LINK,
3962 true /*is_renderer_initiated*/,
3963 nullptr /* blob_url_loader_factory */),
3964 true /* replace */, true /* was_post_commit_error */);
3965}
3966
Carlos Caballeroede6f8c2021-01-28 11:01:503967void NavigationControllerImpl::UpdateStateForFrame(
3968 RenderFrameHostImpl* rfhi,
3969 const blink::PageState& page_state) {
3970 OPTIONAL_TRACE_EVENT1(
3971 "content", "NavigationControllerImpl::UpdateStateForFrame",
3972 "render_frame_host", base::trace_event::ToTracedValue(rfhi));
3973 // The state update affects the last NavigationEntry associated with the given
3974 // |render_frame_host|. This may not be the last committed NavigationEntry (as
3975 // in the case of an UpdateState from a frame being swapped out). We track
3976 // which entry this is in the RenderFrameHost's nav_entry_id.
3977 NavigationEntryImpl* entry = GetEntryWithUniqueID(rfhi->nav_entry_id());
3978 if (!entry)
3979 return;
3980
3981 FrameNavigationEntry* frame_entry =
3982 entry->GetFrameEntry(rfhi->frame_tree_node());
3983 if (!frame_entry)
3984 return;
3985
3986 // The SiteInstance might not match if we do a cross-process navigation with
3987 // replacement (e.g., auto-subframe), in which case the swap out of the old
3988 // RenderFrameHost runs in the background after the old FrameNavigationEntry
3989 // has already been replaced and destroyed.
3990 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
3991 return;
3992
3993 if (page_state == frame_entry->page_state())
3994 return; // Nothing to update.
3995
3996 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
3997
3998 // The document_sequence_number and item_sequence_number recorded in the
3999 // FrameNavigationEntry should not differ from the one coming with the update,
4000 // since it must come from the same document. Do not update it if a difference
4001 // is detected, as this indicates that |frame_entry| is not the correct one.
4002 blink::ExplodedPageState exploded_state;
4003 if (!blink::DecodePageState(page_state.ToEncodedData(), &exploded_state))
4004 return;
4005
4006 if (exploded_state.top.document_sequence_number !=
4007 frame_entry->document_sequence_number() ||
4008 exploded_state.top.item_sequence_number !=
4009 frame_entry->item_sequence_number()) {
4010 return;
4011 }
4012
4013 frame_entry->SetPageState(page_state);
4014 NotifyEntryChanged(entry);
4015}
4016
[email protected]8ff00d72012-10-23 19:12:214017} // namespace content