blob: 8b36fd136359f5e5abdb55561557babff6c555b4 [file] [log] [blame]
[email protected]d4a8ca482013-10-30 21:06:401// Copyright 2013 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
creis4e2ecb72015-06-20 00:46:305/*
6 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
7 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved.
9 * (http://www.torchmobile.com/)
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
21 * its contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
25 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
28 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
danakjc492bf82020-09-09 20:02:4436#include "content/browser/renderer_host/navigation_controller_impl.h"
initial.commit09911bf2008-07-26 23:55:2937
Lei Zhang96031532019-10-10 19:05:4738#include <algorithm>
dcheng36b6aec92015-12-26 06:16:3639#include <utility>
40
[email protected]c5b88d82012-10-06 17:03:3341#include "base/bind.h"
[email protected]eabfe1912014-05-12 10:07:2842#include "base/command_line.h"
initial.commit09911bf2008-07-26 23:55:2943#include "base/logging.h"
asvitkine30330812016-08-30 04:01:0844#include "base/metrics/histogram_macros.h"
[email protected]348fbaac2013-06-11 06:31:5145#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0046#include "base/strings/utf_string_conversions.h"
[email protected]a43858f2013-06-28 15:18:3747#include "base/time/time.h"
ssid3e765612015-01-28 04:03:4248#include "base/trace_event/trace_event.h"
servolkf3955532015-05-16 00:01:5949#include "build/build_config.h"
[email protected]eabfe1912014-05-12 10:07:2850#include "cc/base/switches.h"
jamescookda2505812015-03-20 18:01:1851#include "content/browser/bad_message.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3952#include "content/browser/blob_storage/chrome_blob_storage_context.h"
[email protected]825b1662012-03-12 19:07:3153#include "content/browser/browser_url_handler_impl.h"
[email protected]5f2aa722013-08-07 16:59:4154#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0455#include "content/browser/dom_storage/session_storage_namespace_impl.h"
danakjc492bf82020-09-09 20:02:4456#include "content/browser/renderer_host/debug_urls.h"
57#include "content/browser/renderer_host/frame_tree.h"
58#include "content/browser/renderer_host/navigation_entry_impl.h"
59#include "content/browser/renderer_host/navigation_request.h"
60#include "content/browser/renderer_host/navigator.h"
61#include "content/browser/renderer_host/render_frame_host_delegate.h"
[email protected]b6583592012-01-25 19:52:3362#include "content/browser/site_instance_impl.h"
Tsuyoshi Horo37493af2019-11-21 23:43:1463#include "content/browser/web_package/web_bundle_navigation_info.h"
Camille Lamy5193caa2018-10-12 11:59:4264#include "content/common/content_constants_internal.h"
[email protected]37567b432014-02-12 01:12:2265#include "content/common/frame_messages.h"
Nasko Oskovae49e292020-08-13 02:08:5166#include "content/common/trace_utils.h"
[email protected]ccb797302011-12-15 16:55:1167#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1968#include "content/public/browser/content_browser_client.h"
[email protected]d9083482012-01-06 00:38:4669#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0070#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3871#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1672#include "content/public/browser/notification_types.h"
Lei Zhang96031532019-10-10 19:05:4773#include "content/public/browser/render_view_host.h"
[email protected]9677a3c2012-12-22 04:18:5874#include "content/public/browser/render_widget_host.h"
75#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1076#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3477#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:1978#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:3879#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:1080#include "content/public/common/content_features.h"
Lei Zhang96031532019-10-10 19:05:4781#include "content/public/common/url_constants.h"
clamy7fced7b2017-11-16 19:52:4382#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:5983#include "media/base/mime_util.h"
[email protected]a23de8572009-06-03 02:16:3284#include "net/base/escape.h"
Arthur Sonzogni620cec62018-12-13 13:08:5785#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:1886#include "services/metrics/public/cpp/ukm_builders.h"
87#include "services/metrics/public/cpp/ukm_recorder.h"
Yue Ru Sun128804932020-09-30 22:19:1788#include "services/metrics/public/cpp/ukm_source_id.h"
[email protected]9677a3c2012-12-22 04:18:5889#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3990#include "third_party/blink/public/common/blob/blob_utils.h"
Blink Reformata30d4232018-04-07 15:31:0691#include "third_party/blink/public/common/mime_util/mime_util.h"
[email protected]cca6f392014-05-28 21:32:2692#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:2993
[email protected]8ff00d72012-10-23 19:12:2194namespace content {
[email protected]e9ba4472008-09-14 15:42:4395namespace {
96
97// Invoked when entries have been pruned, or removed. For example, if the
98// current entries are [google, digg, yahoo], with the current entry google,
99// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:47100void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:50101 int index,
[email protected]c12bf1a12008-09-17 16:28:49102 int count) {
[email protected]8ff00d72012-10-23 19:12:21103 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50104 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49105 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14106 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43107}
108
[email protected]e9ba4472008-09-14 15:42:43109// Configure all the NavigationEntries in entries for restore. This resets
110// the transition type to reload and makes sure the content state isn't empty.
111void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57112 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48113 RestoreType type) {
Lei Zhang96031532019-10-10 19:05:47114 for (auto& entry : *entries) {
[email protected]e9ba4472008-09-14 15:42:43115 // Use a transition type of reload so that we don't incorrectly increase
116 // the typed count.
Lei Zhang96031532019-10-10 19:05:47117 entry->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
118 entry->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43119 }
120}
121
[email protected]bf70edce2012-06-20 22:32:22122// Determines whether or not we should be carrying over a user agent override
123// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57124bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22125 return last_entry && last_entry->GetIsOverridingUserAgent();
126}
127
Camille Lamy5193caa2018-10-12 11:59:42128// Determines whether to override user agent for a navigation.
129bool ShouldOverrideUserAgent(
130 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57131 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42132 switch (override_user_agent) {
133 case NavigationController::UA_OVERRIDE_INHERIT:
134 return ShouldKeepOverride(last_committed_entry);
135 case NavigationController::UA_OVERRIDE_TRUE:
136 return true;
137 case NavigationController::UA_OVERRIDE_FALSE:
138 return false;
Camille Lamy5193caa2018-10-12 11:59:42139 }
140 NOTREACHED();
141 return false;
142}
143
clamy0a656e42018-02-06 18:18:28144// Returns true this navigation should be treated as a reload. For e.g.
145// navigating to the last committed url via the address bar or clicking on a
146// link which results in a navigation to the last committed or pending
147// navigation, etc.
Fergal Daly766177d2020-07-07 07:54:04148// |node| is the FrameTreeNode which is navigating. |url|, |virtual_url|,
149// |base_url_for_data_url|, |transition_type| correspond to the new navigation
150// (i.e. the pending NavigationEntry). |last_committed_entry| is the last
151// navigation that committed.
152bool ShouldTreatNavigationAsReload(FrameTreeNode* node,
153 const GURL& url,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57154 const GURL& virtual_url,
155 const GURL& base_url_for_data_url,
156 ui::PageTransition transition_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57157 bool is_post,
158 bool is_reload,
159 bool is_navigation_to_existing_entry,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57160 NavigationEntryImpl* last_committed_entry) {
Fergal Daly766177d2020-07-07 07:54:04161 if (is_reload || is_navigation_to_existing_entry)
clamy0a656e42018-02-06 18:18:28162 return false;
clamy0a656e42018-02-06 18:18:28163 // Only convert to reload if at least one navigation committed.
164 if (!last_committed_entry)
ananta3bdd8ae2016-12-22 17:11:55165 return false;
166
arthursonzogni7a8243682017-12-14 16:41:42167 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28168 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42169 return false;
170
ananta3bdd8ae2016-12-22 17:11:55171 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
172 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
173 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28174 bool transition_type_can_be_converted = false;
175 if (ui::PageTransitionCoreTypeIs(transition_type,
176 ui::PAGE_TRANSITION_RELOAD) &&
177 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
178 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55179 }
clamy0a656e42018-02-06 18:18:28180 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55181 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28182 transition_type_can_be_converted = true;
183 }
184 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
185 transition_type_can_be_converted = true;
186 if (!transition_type_can_be_converted)
187 return false;
188
189 // This check is required for cases like view-source:, etc. Here the URL of
190 // the navigation entry would contain the url of the page, while the virtual
191 // URL contains the full URL including the view-source prefix.
192 if (virtual_url != last_committed_entry->GetVirtualURL())
193 return false;
194
Fergal Daly766177d2020-07-07 07:54:04195 // Check that the URLs match.
196 FrameNavigationEntry* frame_entry = last_committed_entry->GetFrameEntry(node);
197 // If there's no frame entry then by definition the URLs don't match.
198 if (!frame_entry)
199 return false;
200
201 if (url != frame_entry->url())
clamy0a656e42018-02-06 18:18:28202 return false;
203
204 // This check is required for Android WebView loadDataWithBaseURL. Apps
205 // can pass in anything in the base URL and we need to ensure that these
206 // match before classifying it as a reload.
207 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
208 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
209 return false;
ananta3bdd8ae2016-12-22 17:11:55210 }
211
clamy0a656e42018-02-06 18:18:28212 // Skip entries with SSL errors.
213 if (last_committed_entry->ssl_error())
214 return false;
215
216 // Don't convert to a reload when the last navigation was a POST or the new
217 // navigation is a POST.
Fergal Daly766177d2020-07-07 07:54:04218 if (frame_entry->get_has_post_data() || is_post)
clamy0a656e42018-02-06 18:18:28219 return false;
220
221 return true;
ananta3bdd8ae2016-12-22 17:11:55222}
223
Nasko Oskov03912102019-01-11 00:21:32224bool DoesURLMatchOriginForNavigation(
225 const GURL& url,
226 const base::Optional<url::Origin>& origin) {
227 // If there is no origin supplied there is nothing to match. This can happen
228 // for navigations to a pending entry and therefore it should be allowed.
229 if (!origin)
230 return true;
231
232 return origin->CanBeDerivedFrom(url);
233}
234
235base::Optional<url::Origin> GetCommittedOriginForFrameEntry(
arthursonzogni73fe3212020-11-17 13:24:07236 const mojom::DidCommitProvisionalLoadParams& params) {
Nasko Oskov03912102019-01-11 00:21:32237 // Error pages commit in an opaque origin, yet have the real URL that resulted
238 // in an error as the |params.url|. Since successful reload of an error page
239 // should commit in the correct origin, setting the opaque origin on the
240 // FrameNavigationEntry will be incorrect.
241 if (params.url_is_unreachable)
242 return base::nullopt;
243
244 return base::make_optional(params.origin);
245}
246
Camille Lamy5193caa2018-10-12 11:59:42247bool IsValidURLForNavigation(bool is_main_frame,
248 const GURL& virtual_url,
249 const GURL& dest_url) {
250 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
251 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
252 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
253 << virtual_url.possibly_invalid_spec();
254 return false;
255 }
256
257 // Don't attempt to navigate to non-empty invalid URLs.
258 if (!dest_url.is_valid() && !dest_url.is_empty()) {
259 LOG(WARNING) << "Refusing to load invalid URL: "
260 << dest_url.possibly_invalid_spec();
261 return false;
262 }
263
264 // The renderer will reject IPC messages with URLs longer than
265 // this limit, so don't attempt to navigate with a longer URL.
266 if (dest_url.spec().size() > url::kMaxURLChars) {
267 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
268 << " characters.";
269 return false;
270 }
271
Aaron Colwell33109c592020-04-21 21:31:19272 // Reject renderer debug URLs because they should have been handled before
273 // we get to this point. This check handles renderer debug URLs
274 // that are inside a view-source: URL (e.g. view-source:chrome://kill) and
275 // provides defense-in-depth if a renderer debug URL manages to get here via
276 // some other path. We want to reject the navigation here so it doesn't
277 // violate assumptions in downstream code.
278 if (IsRendererDebugURL(dest_url)) {
279 LOG(WARNING) << "Refusing to load renderer debug URL: "
280 << dest_url.possibly_invalid_spec();
281 return false;
282 }
283
Camille Lamy5193caa2018-10-12 11:59:42284 return true;
285}
286
Mikel Astizba9cf2fd2017-12-17 10:38:10287// See replaced_navigation_entry_data.h for details: this information is meant
288// to ensure |*output_entry| keeps track of its original URL (landing page in
289// case of server redirects) as it gets replaced (e.g. history.replaceState()),
290// without overwriting it later, for main frames.
291void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57292 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10293 NavigationEntryImpl* output_entry) {
294 if (output_entry->GetReplacedEntryData().has_value())
295 return;
296
297 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57298 data.first_committed_url = replaced_entry->GetURL();
299 data.first_timestamp = replaced_entry->GetTimestamp();
300 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29301 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10302}
303
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51304mojom::NavigationType GetNavigationType(const GURL& old_url,
305 const GURL& new_url,
306 ReloadType reload_type,
307 NavigationEntryImpl* entry,
308 const FrameNavigationEntry& frame_entry,
danakjd83d706d2020-11-25 22:11:12309 bool has_pending_cross_document_commit,
danakjb952ef12021-01-14 19:58:49310 bool is_currently_error_page,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51311 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23312 // Reload navigations
313 switch (reload_type) {
314 case ReloadType::NORMAL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51315 return mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23316 case ReloadType::BYPASSING_CACHE:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51317 return mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23318 case ReloadType::ORIGINAL_REQUEST_URL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51319 return mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23320 case ReloadType::NONE:
321 break; // Fall through to rest of function.
322 }
323
Lukasz Anforowicz6b75c0d2020-12-01 22:56:08324 if (entry->IsRestored()) {
Lei Zhang96031532019-10-10 19:05:47325 return entry->GetHasPostData() ? mojom::NavigationType::RESTORE_WITH_POST
326 : mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23327 }
328
danakjb952ef12021-01-14 19:58:49329 const bool can_be_same_document =
330 // A pending cross-document commit means this navigation will not occur in
331 // the current document, as that document would end up being replaced in
332 // the meantime.
333 !has_pending_cross_document_commit &&
334 // If the current document is an error page, we should always treat it as
335 // a different-document navigation so that we'll attempt to load the
336 // document we're navigating to (and not stay in the current error page).
337 !is_currently_error_page;
danakjd83d706d2020-11-25 22:11:12338
clamyea99ea12018-05-28 13:54:23339 // History navigations.
340 if (frame_entry.page_state().IsValid()) {
danakjd83d706d2020-11-25 22:11:12341 return can_be_same_document && is_same_document_history_load
Lei Zhang96031532019-10-10 19:05:47342 ? mojom::NavigationType::HISTORY_SAME_DOCUMENT
343 : mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23344 }
345 DCHECK(!is_same_document_history_load);
346
347 // A same-document fragment-navigation happens when the only part of the url
348 // that is modified is after the '#' character.
349 //
350 // When modifying this condition, please take a look at:
danakjd83d706d2020-11-25 22:11:12351 // FrameLoader::ShouldPerformFragmentNavigation().
clamyea99ea12018-05-28 13:54:23352 //
353 // Note: this check is only valid for navigations that are not history
354 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
355 // history navigation from 'A#foo' to 'A#bar' is not a same-document
356 // navigation, but a different-document one. This is why history navigation
357 // are classified before this check.
Lei Zhang96031532019-10-10 19:05:47358 bool is_same_doc = new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
359 frame_entry.method() == "GET";
danakjd83d706d2020-11-25 22:11:12360
361 // The one case where we do the wrong thing here and incorrectly choose
362 // SAME_DOCUMENT is if the navigation is browser-initiated but the document in
363 // the renderer is a frameset. All frameset navigations should be
364 // DIFFERENT_DOCUMENT, even if their URLs match. A renderer-initiated
365 // navigation would do the right thing, as it would send it to the browser and
366 // all renderer-initiated navigations are DIFFERENT_DOCUMENT (they don't get
367 // into this method). But since we can't tell that case here for browser-
368 // initiated navigations, we have to get the renderer involved. In that case
369 // the navigation would be restarted due to the renderer spending a reply of
370 // mojom::CommitResult::RestartCrossDocument.
371
372 return can_be_same_document && is_same_doc
373 ? mojom::NavigationType::SAME_DOCUMENT
374 : mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23375}
376
Camille Lamy5193caa2018-10-12 11:59:42377// Adjusts the original input URL if needed, to get the URL to actually load and
378// the virtual URL, which may differ.
379void RewriteUrlForNavigation(const GURL& original_url,
380 BrowserContext* browser_context,
381 GURL* url_to_load,
382 GURL* virtual_url,
383 bool* reverse_on_redirect) {
Camille Lamy5193caa2018-10-12 11:59:42384 // Allow the browser URL handler to rewrite the URL. This will, for example,
385 // remove "view-source:" from the beginning of the URL to get the URL that
386 // will actually be loaded. This real URL won't be shown to the user, just
387 // used internally.
Lukasz Anforowicz7b078792020-10-20 17:04:31388 *url_to_load = *virtual_url = original_url;
Camille Lamy5193caa2018-10-12 11:59:42389 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
390 url_to_load, browser_context, reverse_on_redirect);
391}
392
393#if DCHECK_IS_ON()
394// Helper sanity check function used in debug mode.
395void ValidateRequestMatchesEntry(NavigationRequest* request,
396 NavigationEntryImpl* entry) {
397 if (request->frame_tree_node()->IsMainFrame()) {
398 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
399 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
400 request->common_params().transition, entry->GetTransitionType()));
401 }
402 DCHECK_EQ(request->common_params().should_replace_current_entry,
403 entry->should_replace_entry());
Nasko Oskovc36327d2019-01-03 23:23:04404 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42405 entry->should_clear_history_list());
406 DCHECK_EQ(request->common_params().has_user_gesture,
407 entry->has_user_gesture());
408 DCHECK_EQ(request->common_params().base_url_for_data_url,
409 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04410 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42411 entry->GetCanLoadLocalResources());
412 DCHECK_EQ(request->common_params().started_from_context_menu,
413 entry->has_started_from_context_menu());
414
415 FrameNavigationEntry* frame_entry =
416 entry->GetFrameEntry(request->frame_tree_node());
417 if (!frame_entry) {
418 NOTREACHED();
419 return;
420 }
421
Camille Lamy5193caa2018-10-12 11:59:42422 DCHECK_EQ(request->common_params().method, frame_entry->method());
423
Nasko Oskovc36327d2019-01-03 23:23:04424 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42425 if (redirect_size == frame_entry->redirect_chain().size()) {
426 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04427 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42428 frame_entry->redirect_chain()[i]);
429 }
430 } else {
431 NOTREACHED();
432 }
433}
434#endif // DCHECK_IS_ON()
435
Dave Tapuska8bfd84c2019-03-26 20:47:16436// Returns whether the session history NavigationRequests in |navigations|
437// would stay within the subtree of the sandboxed iframe in
438// |sandbox_frame_tree_node_id|.
439bool DoesSandboxNavigationStayWithinSubtree(
440 int sandbox_frame_tree_node_id,
441 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
442 for (auto& item : navigations) {
443 bool within_subtree = false;
444 // Check whether this NavigationRequest affects a frame within the
445 // sandboxed frame's subtree by walking up the tree looking for the
446 // sandboxed frame.
447 for (auto* frame = item->frame_tree_node(); frame;
Alexander Timin381e7e182020-04-28 19:04:03448 frame = FrameTreeNode::From(frame->parent())) {
Dave Tapuska8bfd84c2019-03-26 20:47:16449 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
450 within_subtree = true;
451 break;
452 }
453 }
454 if (!within_subtree)
455 return false;
456 }
457 return true;
458}
459
Antonio Sartori78a749f2020-11-30 12:03:39460bool ShouldStoreDocumentPoliciesInFrameNavigationEntry(
461 const NavigationRequest* request) {
462 // For local schemes we need to store the policy container in the
463 // FrameNavigationEntry, so that we can reload it in case of history
464 // navigation.
465 //
466 // TODO(https://crbug.com/1146361 and https://crbug.com/1146362): blob: and
467 // filesystem: should be removed from this list when we have properly
468 // implemented storing their policy container in the respective store.
469 return (request->common_params().url.SchemeIs(url::kAboutScheme) ||
470 request->common_params().url.SchemeIs(url::kDataScheme) ||
471 request->common_params().url.SchemeIsBlob() ||
472 request->common_params().url.SchemeIsFileSystem());
473}
474
[email protected]e9ba4472008-09-14 15:42:43475} // namespace
476
arthursonzogni66f711c2019-10-08 14:40:36477// NavigationControllerImpl::PendingEntryRef------------------------------------
478
479NavigationControllerImpl::PendingEntryRef::PendingEntryRef(
480 base::WeakPtr<NavigationControllerImpl> controller)
481 : controller_(controller) {}
482
483NavigationControllerImpl::PendingEntryRef::~PendingEntryRef() {
484 if (!controller_) // Can be null with interstitials.
485 return;
486
487 controller_->PendingEntryRefDeleted(this);
488}
489
[email protected]d202a7c2012-01-04 07:53:47490// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29491
[email protected]23a918b2014-07-15 09:51:36492const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23493
[email protected]765b35502008-08-21 00:51:20494// static
[email protected]d202a7c2012-01-04 07:53:47495size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23496 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20497
[email protected]e6fec472013-05-14 05:29:02498// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20499// when testing.
500static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29501
[email protected]71fde352011-12-29 03:29:56502// static
dcheng9bfa5162016-04-09 01:00:57503std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
504 const GURL& url,
Lukasz Anforowicz641234d52019-11-07 21:07:10505 Referrer referrer,
506 base::Optional<url::Origin> initiator_origin,
507 ui::PageTransition transition,
508 bool is_renderer_initiated,
509 const std::string& extra_headers,
510 BrowserContext* browser_context,
511 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
512 return NavigationControllerImpl::CreateNavigationEntry(
513 url, referrer, std::move(initiator_origin),
514 nullptr /* source_site_instance */, transition, is_renderer_initiated,
Rakina Zata Amni66992a82020-06-24 03:57:52515 extra_headers, browser_context, std::move(blob_url_loader_factory),
Scott Violet5ae6c42e2020-10-28 02:47:37516 false /* should_replace_entry */, nullptr /* web_contents */);
Lukasz Anforowicz641234d52019-11-07 21:07:10517}
518
519// static
520std::unique_ptr<NavigationEntryImpl>
521NavigationControllerImpl::CreateNavigationEntry(
522 const GURL& url,
523 Referrer referrer,
524 base::Optional<url::Origin> initiator_origin,
525 SiteInstance* source_site_instance,
dcheng9bfa5162016-04-09 01:00:57526 ui::PageTransition transition,
527 bool is_renderer_initiated,
528 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09529 BrowserContext* browser_context,
Rakina Zata Amni66992a82020-06-24 03:57:52530 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Scott Violet5ae6c42e2020-10-28 02:47:37531 bool should_replace_entry,
532 WebContents* web_contents) {
Camille Lamy5193caa2018-10-12 11:59:42533 GURL url_to_load;
534 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56535 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42536 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
537 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56538
Lukasz Anforowicz641234d52019-11-07 21:07:10539 // Let the NTP override the navigation params and pretend that this is a
540 // browser-initiated, bookmark-like navigation.
541 GetContentClient()->browser()->OverrideNavigationParams(
Scott Violet5ae6c42e2020-10-28 02:47:37542 web_contents, source_site_instance, &transition, &is_renderer_initiated,
543 &referrer, &initiator_origin);
Lukasz Anforowicz641234d52019-11-07 21:07:10544
Patrick Monettef507e982019-06-19 20:18:06545 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28546 nullptr, // The site instance for tabs is sent on navigation
547 // (WebContents::GetSiteInstance).
Lukasz Anforowicz435bcb582019-07-12 20:50:06548 url_to_load, referrer, initiator_origin, base::string16(), transition,
Camille Lamy5193caa2018-10-12 11:59:42549 is_renderer_initiated, blob_url_loader_factory);
550 entry->SetVirtualURL(virtual_url);
551 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56552 entry->set_update_virtual_url_with_url(reverse_on_redirect);
553 entry->set_extra_headers(extra_headers);
Rakina Zata Amni66992a82020-06-24 03:57:52554 entry->set_should_replace_entry(should_replace_entry);
Patrick Monettef507e982019-06-19 20:18:06555 return entry;
[email protected]71fde352011-12-29 03:29:56556}
557
[email protected]cdcb1dee2012-01-04 00:46:20558// static
559void NavigationController::DisablePromptOnRepost() {
560 g_check_for_repost = false;
561}
562
[email protected]c5b88d82012-10-06 17:03:33563base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
564 base::Time t) {
565 // If |t| is between the water marks, we're in a run of duplicates
566 // or just getting out of it, so increase the high-water mark to get
567 // a time that probably hasn't been used before and return it.
568 if (low_water_mark_ <= t && t <= high_water_mark_) {
569 high_water_mark_ += base::TimeDelta::FromMicroseconds(1);
570 return high_water_mark_;
571 }
572
573 // Otherwise, we're clear of the last duplicate run, so reset the
574 // water marks.
575 low_water_mark_ = high_water_mark_ = t;
576 return t;
577}
578
ckitagawa0faa5e42020-06-17 17:30:54579NavigationControllerImpl::ScopedShowRepostDialogForTesting::
580 ScopedShowRepostDialogForTesting()
581 : was_disallowed_(g_check_for_repost) {
582 g_check_for_repost = true;
583}
584
585NavigationControllerImpl::ScopedShowRepostDialogForTesting::
586 ~ScopedShowRepostDialogForTesting() {
587 g_check_for_repost = was_disallowed_;
588}
589
[email protected]d202a7c2012-01-04 07:53:47590NavigationControllerImpl::NavigationControllerImpl(
[email protected]ec6c05f2013-10-23 18:41:57591 NavigationControllerDelegate* delegate,
[email protected]d1198fd2012-08-13 22:50:19592 BrowserContext* browser_context)
[email protected]3d7474ff2011-07-27 17:47:37593 : browser_context_(browser_context),
[email protected]ec6c05f2013-10-23 18:41:57594 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22595 ssl_manager_(this),
Lei Zhang96031532019-10-10 19:05:47596 get_timestamp_callback_(base::BindRepeating(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37597 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29598}
599
[email protected]d202a7c2012-01-04 07:53:47600NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00601 // The NavigationControllerImpl might be called inside its delegate
602 // destructor. Calling it is not valid anymore.
603 delegate_ = nullptr;
604 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29605}
606
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57607WebContents* NavigationControllerImpl::GetWebContents() {
[email protected]ec6c05f2013-10-23 18:41:57608 return delegate_->GetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32609}
610
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57611BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55612 return browser_context_;
613}
614
[email protected]d202a7c2012-01-04 07:53:47615void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30616 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36617 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57618 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57619 // Verify that this controller is unused and that the input is valid.
Lei Zhang96031532019-10-10 19:05:47620 DCHECK_EQ(0, GetEntryCount());
621 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57622 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14623 selected_navigation < static_cast<int>(entries->size()));
arthursonzogni5c4c202d2017-04-25 23:41:27624 DCHECK_EQ(-1, pending_entry_index_);
[email protected]ce3fa3c2009-04-20 19:55:57625
[email protected]ce3fa3c2009-04-20 19:55:57626 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44627 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03628 entries_.reserve(entries->size());
629 for (auto& entry : *entries)
dcheng36b6aec92015-12-26 06:16:36630 entries_.push_back(
631 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
avif16f85a72015-11-13 18:25:03632
633 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
634 // cleared out safely.
635 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57636
637 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36638 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57639}
640
toyoshim6142d96f2016-12-19 09:07:25641void NavigationControllerImpl::Reload(ReloadType reload_type,
642 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28643 DCHECK_NE(ReloadType::NONE, reload_type);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28644 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32645 int current_index = -1;
646
Carlos IL42b416592019-10-07 23:10:36647 if (entry_replaced_by_post_commit_error_) {
648 // If there is an entry that was replaced by a currently active post-commit
649 // error navigation, this can't be the initial navigation.
650 DCHECK(!IsInitialNavigation());
651 // If the current entry is a post commit error, we reload the entry it
652 // replaced instead. We leave the error entry in place until a commit
653 // replaces it, but the pending entry points to the original entry in the
654 // meantime. Note that NavigateToExistingPendingEntry is able to handle the
655 // case that pending_entry_ != entries_[pending_entry_index_].
656 entry = entry_replaced_by_post_commit_error_.get();
657 current_index = GetCurrentEntryIndex();
658 } else if (IsInitialNavigation() && pending_entry_) {
659 // If we are reloading the initial navigation, just use the current
660 // pending entry. Otherwise look up the current entry.
[email protected]59167c22013-06-03 18:07:32661 entry = pending_entry_;
662 // The pending entry might be in entries_ (e.g., after a Clone), so we
663 // should also update the current_index.
664 current_index = pending_entry_index_;
665 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00666 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32667 current_index = GetCurrentEntryIndex();
668 if (current_index != -1) {
creis3da03872015-02-20 21:12:32669 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32670 }
[email protected]979a4bc2013-04-24 01:27:15671 }
[email protected]241db352013-04-22 18:04:05672
[email protected]59167c22013-06-03 18:07:32673 // If we are no where, then we can't reload. TODO(darin): We should add a
674 // CanReload method.
675 if (!entry)
676 return;
677
Takashi Toyoshimac7df3c22019-06-25 14:18:47678 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26679 entry->set_reload_type(reload_type);
680
Aran Gilman37d11632019-10-08 23:07:15681 if (g_check_for_repost && check_for_repost && entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30682 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07683 // they really want to do this. If they do, the dialog will call us back
684 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57685 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02686
[email protected]106a0812010-03-18 00:15:12687 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57688 delegate_->ActivateAndShowRepostFormWarningDialog();
Lei Zhang96031532019-10-10 19:05:47689 return;
initial.commit09911bf2008-07-26 23:55:29690 }
Lei Zhang96031532019-10-10 19:05:47691
692 if (!IsInitialNavigation())
693 DiscardNonCommittedEntries();
694
695 pending_entry_ = entry;
696 pending_entry_index_ = current_index;
697 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
698
699 NavigateToExistingPendingEntry(reload_type,
700 FrameTreeNode::kFrameTreeNodeInvalidId);
initial.commit09911bf2008-07-26 23:55:29701}
702
[email protected]d202a7c2012-01-04 07:53:47703void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48704 DCHECK(pending_reload_ != ReloadType::NONE);
705 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12706}
707
[email protected]d202a7c2012-01-04 07:53:47708void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48709 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12710 NOTREACHED();
711 } else {
toyoshim6142d96f2016-12-19 09:07:25712 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48713 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12714 }
715}
716
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57717bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09718 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11719}
720
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57721bool NavigationControllerImpl::IsInitialBlankNavigation() {
creis10a4ab72015-10-13 17:22:40722 // TODO(creis): Once we create a NavigationEntry for the initial blank page,
toyoshim0df1d3a2016-09-09 09:52:48723 // we'll need to check for entry count 1 and restore_type NONE (to exclude
724 // the cloned tab case).
creis10a4ab72015-10-13 17:22:40725 return IsInitialNavigation() && GetEntryCount() == 0;
726}
727
Aran Gilman37d11632019-10-08 23:07:15728NavigationEntryImpl* NavigationControllerImpl::GetEntryWithUniqueID(
729 int nav_entry_id) const {
avi254eff02015-07-01 08:27:58730 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03731 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58732}
733
W. James MacLean1c40862c2020-04-27 21:05:57734void NavigationControllerImpl::RegisterExistingOriginToPreventOptInIsolation(
735 const url::Origin& origin) {
736 for (int i = 0; i < GetEntryCount(); i++) {
737 auto* entry = GetEntryAtIndex(i);
738 entry->RegisterExistingOriginToPreventOptInIsolation(origin);
739 }
740 if (entry_replaced_by_post_commit_error_) {
741 // It's possible we could come back to this entry if the error
742 // page/interstitial goes away.
743 entry_replaced_by_post_commit_error_
744 ->RegisterExistingOriginToPreventOptInIsolation(origin);
745 }
746 // TODO(wjmaclean): Register pending commit NavigationRequests rather than
747 // visiting pending_entry_, which lacks a committed origin. This will be done
748 // in https://chromium-review.googlesource.com/c/chromium/src/+/2136703.
749}
750
avi25764702015-06-23 15:43:37751void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57752 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00753 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37754 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27755 DCHECK_EQ(-1, pending_entry_index_);
[email protected]8ff00d72012-10-23 19:12:21756 NotificationService::current()->Notify(
Aran Gilman37d11632019-10-08 23:07:15757 NOTIFICATION_NAV_ENTRY_PENDING, Source<NavigationController>(this),
avi25764702015-06-23 15:43:37758 Details<NavigationEntry>(pending_entry_));
[email protected]765b35502008-08-21 00:51:20759}
760
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57761NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47762 if (pending_entry_)
763 return pending_entry_;
764 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20765}
766
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57767NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]59167c22013-06-03 18:07:32768 // The pending entry is safe to return for new (non-history), browser-
769 // initiated navigations. Most renderer-initiated navigations should not
770 // show the pending entry, to prevent URL spoof attacks.
771 //
772 // We make an exception for renderer-initiated navigations in new tabs, as
773 // long as no other page has tried to access the initial empty document in
774 // the new tab. If another page modifies this blank page, a URL spoof is
775 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32776 bool safe_to_show_pending =
777 pending_entry_ &&
778 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09779 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32780 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46781 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32782
783 // Also allow showing the pending entry for history navigations in a new tab,
784 // such as Ctrl+Back. In this case, no existing page is visible and no one
785 // can script the new tab before it commits.
Aran Gilman37d11632019-10-08 23:07:15786 if (!safe_to_show_pending && pending_entry_ && pending_entry_index_ != -1 &&
787 IsInitialNavigation() && !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32788 safe_to_show_pending = true;
789
790 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19791 return pending_entry_;
792 return GetLastCommittedEntry();
793}
794
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57795int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]765b35502008-08-21 00:51:20796 if (pending_entry_index_ != -1)
797 return pending_entry_index_;
798 return last_committed_entry_index_;
799}
800
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57801NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20802 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28803 return nullptr;
avif16f85a72015-11-13 18:25:03804 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20805}
806
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57807bool NavigationControllerImpl::CanViewSource() {
[email protected]ec6c05f2013-10-23 18:41:57808 const std::string& mime_type = delegate_->GetContentsMimeType();
Kinuko Yasuda74702f92017-07-31 03:27:53809 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
810 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27811 NavigationEntry* visible_entry = GetVisibleEntry();
812 return visible_entry && !visible_entry->IsViewSourceMode() &&
Carlos ILd51e7702020-05-07 18:51:39813 is_viewable_mime_type;
[email protected]31682282010-01-15 18:05:16814}
815
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57816int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27817 // The last committed entry index must always be less than the number of
Carlos IL4dea8902020-05-26 15:14:29818 // entries. If there are no entries, it must be -1.
arthursonzogni5c4c202d2017-04-25 23:41:27819 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
820 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55821 return last_committed_entry_index_;
822}
823
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57824int NavigationControllerImpl::GetEntryCount() {
Carlos IL4dea8902020-05-26 15:14:29825 DCHECK_LE(entries_.size(), max_entry_count());
[email protected]a26023822011-12-29 00:23:55826 return static_cast<int>(entries_.size());
827}
828
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57829NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37830 if (index < 0 || index >= GetEntryCount())
831 return nullptr;
832
avif16f85a72015-11-13 18:25:03833 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25834}
835
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57836NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47837 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20838}
839
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57840int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46841 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03842}
843
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57844bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03845 if (!base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention))
846 return CanGoToOffset(-1);
847
848 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
849 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
850 return true;
851 }
852 return false;
[email protected]765b35502008-08-21 00:51:20853}
854
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57855bool NavigationControllerImpl::CanGoForward() {
avi56e40c92015-08-27 00:11:22856 return CanGoToOffset(1);
[email protected]765b35502008-08-21 00:51:20857}
858
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57859bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03860 int index = GetIndexForOffset(offset);
861 return index >= 0 && index < GetEntryCount();
862}
863
[email protected]d202a7c2012-01-04 07:53:47864void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06865 int target_index = GetIndexForOffset(-1);
866
867 // Log metrics for the number of entries that are eligible for skipping on
Shivani Sharma2d5b4b6b2019-01-08 16:07:16868 // back button and move the target index past the skippable entries, if
869 // history intervention is enabled.
shivanisha55201872018-12-13 04:29:06870 int count_entries_skipped = 0;
Shivani Sharma298d12852019-01-22 20:04:03871 bool all_skippable_entries = true;
Aran Gilman37d11632019-10-08 23:07:15872 bool history_intervention_enabled =
873 base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention);
shivanisha55201872018-12-13 04:29:06874 for (int index = target_index; index >= 0; index--) {
Shivani Sharma2d5b4b6b2019-01-08 16:07:16875 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
shivanisha55201872018-12-13 04:29:06876 count_entries_skipped++;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16877 } else {
878 if (history_intervention_enabled)
879 target_index = index;
Shivani Sharma298d12852019-01-22 20:04:03880 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06881 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16882 }
shivanisha55201872018-12-13 04:29:06883 }
884 UMA_HISTOGRAM_ENUMERATION("Navigation.BackForward.BackTargetSkipped",
885 count_entries_skipped, kMaxSessionHistoryEntries);
Shivani Sharma298d12852019-01-22 20:04:03886 UMA_HISTOGRAM_BOOLEAN("Navigation.BackForward.AllBackTargetsSkippable",
887 all_skippable_entries);
888
889 // Do nothing if all entries are skippable. Normally this path would not
890 // happen as consumers would have already checked it in CanGoBack but a lot of
891 // tests do not do that.
892 if (history_intervention_enabled && all_skippable_entries)
893 return;
shivanisha55201872018-12-13 04:29:06894
shivanisha55201872018-12-13 04:29:06895 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20896}
897
[email protected]d202a7c2012-01-04 07:53:47898void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06899 int target_index = GetIndexForOffset(1);
900
901 // Log metrics for the number of entries that are eligible for skipping on
Shivani Sharma2d5b4b6b2019-01-08 16:07:16902 // forward button and move the target index past the skippable entries, if
903 // history intervention is enabled.
904 // Note that at least one entry (the last one) will be non-skippable since
905 // entries are marked skippable only when they add another entry because of
906 // redirect or pushState.
shivanisha55201872018-12-13 04:29:06907 int count_entries_skipped = 0;
Aran Gilman37d11632019-10-08 23:07:15908 bool history_intervention_enabled =
909 base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention);
shivanisha55201872018-12-13 04:29:06910 for (size_t index = target_index; index < entries_.size(); index++) {
Shivani Sharma2d5b4b6b2019-01-08 16:07:16911 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
shivanisha55201872018-12-13 04:29:06912 count_entries_skipped++;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16913 } else {
914 if (history_intervention_enabled)
915 target_index = index;
shivanisha55201872018-12-13 04:29:06916 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16917 }
shivanisha55201872018-12-13 04:29:06918 }
919 UMA_HISTOGRAM_ENUMERATION("Navigation.BackForward.ForwardTargetSkipped",
920 count_entries_skipped, kMaxSessionHistoryEntries);
921
shivanisha55201872018-12-13 04:29:06922 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20923}
924
[email protected]d202a7c2012-01-04 07:53:47925void NavigationControllerImpl::GoToIndex(int index) {
Dave Tapuska8bfd84c2019-03-26 20:47:16926 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId);
927}
928
929void NavigationControllerImpl::GoToIndex(int index,
930 int sandbox_frame_tree_node_id) {
sunjian30574a62017-03-21 21:39:44931 TRACE_EVENT0("browser,navigation,benchmark",
932 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:20933 if (index < 0 || index >= static_cast<int>(entries_.size())) {
934 NOTREACHED();
935 return;
936 }
937
[email protected]cbab76d2008-10-13 22:42:47938 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20939
arthursonzogni5c4c202d2017-04-25 23:41:27940 DCHECK_EQ(nullptr, pending_entry_);
941 DCHECK_EQ(-1, pending_entry_index_);
942 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:20943 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:27944 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
945 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Dave Tapuska8bfd84c2019-03-26 20:47:16946 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id);
[email protected]765b35502008-08-21 00:51:20947}
948
[email protected]d202a7c2012-01-04 07:53:47949void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:12950 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:03951 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:20952 return;
953
[email protected]9ba14052012-06-22 23:50:03954 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20955}
956
[email protected]41374f12013-07-24 02:49:28957bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
Aran Gilman37d11632019-10-08 23:07:15958 if (index == last_committed_entry_index_ || index == pending_entry_index_)
[email protected]41374f12013-07-24 02:49:28959 return false;
[email protected]6a13a6c2011-12-20 21:47:12960
[email protected]43032342011-03-21 14:10:31961 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:28962 return true;
[email protected]cbab76d2008-10-13 22:42:47963}
964
Michael Thiessen9b14d512019-09-23 21:19:47965void NavigationControllerImpl::PruneForwardEntries() {
966 DiscardNonCommittedEntries();
967 int remove_start_index = last_committed_entry_index_ + 1;
Lei Zhang96031532019-10-10 19:05:47968 int num_removed = static_cast<int>(entries_.size()) - remove_start_index;
Michael Thiessen9b14d512019-09-23 21:19:47969 if (num_removed <= 0)
970 return;
971 entries_.erase(entries_.begin() + remove_start_index, entries_.end());
972 NotifyPrunedEntries(this, remove_start_index /* start index */,
973 num_removed /* count */);
974}
975
Aran Gilman37d11632019-10-08 23:07:15976void NavigationControllerImpl::UpdateVirtualURLToURL(NavigationEntryImpl* entry,
977 const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:32978 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:31979 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:51980 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
981 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:32982 }
983}
984
Aran Gilman37d11632019-10-08 23:07:15985void NavigationControllerImpl::LoadURL(const GURL& url,
986 const Referrer& referrer,
987 ui::PageTransition transition,
988 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:47989 LoadURLParams params(url);
990 params.referrer = referrer;
991 params.transition_type = transition;
992 params.extra_headers = extra_headers;
993 LoadURLWithParams(params);
994}
995
996void NavigationControllerImpl::LoadURLWithParams(const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:06997 if (params.is_renderer_initiated)
998 DCHECK(params.initiator_origin.has_value());
999
naskob8744d22014-08-28 17:07:431000 TRACE_EVENT1("browser,navigation",
Aran Gilman37d11632019-10-08 23:07:151001 "NavigationControllerImpl::LoadURLWithParams", "url",
1002 params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:291003 bool is_explicit_navigation =
1004 GetContentClient()->browser()->IsExplicitNavigation(
1005 params.transition_type);
1006 if (HandleDebugURL(params.url, params.transition_type,
1007 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:431008 // If Telemetry is running, allow the URL load to proceed as if it's
1009 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:491010 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:431011 cc::switches::kEnableGpuBenchmarking))
1012 return;
1013 }
[email protected]8bf1048012012-02-08 01:22:181014
[email protected]cf002332012-08-14 19:17:471015 // Checks based on params.load_type.
1016 switch (params.load_type) {
1017 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:431018 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:471019 break;
1020 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:261021 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:471022 NOTREACHED() << "Data load must use data scheme.";
1023 return;
1024 }
1025 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:461026 }
[email protected]e47ae9472011-10-13 19:48:341027
[email protected]e47ae9472011-10-13 19:48:341028 // The user initiated a load, we don't need to reload anymore.
1029 needs_reload_ = false;
1030
clamy21718cc22018-06-13 13:34:241031 NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:441032}
1033
Mohamed Abdelhalim833de902019-09-16 17:41:451034bool NavigationControllerImpl::PendingEntryMatchesRequest(
1035 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:191036 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451037 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:191038}
1039
[email protected]d202a7c2012-01-04 07:53:471040bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:321041 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071042 const mojom::DidCommitProvisionalLoadParams& params,
peary21b0f797b2016-09-28 17:28:331043 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:441044 bool is_same_document_navigation,
Shivani Sharmaffb32b82019-04-09 16:58:471045 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:131046 NavigationRequest* navigation_request) {
1047 DCHECK(navigation_request);
[email protected]cd2e15742013-03-08 04:08:311048 is_initial_navigation_ = false;
1049
[email protected]0e8db942008-09-24 21:21:481050 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:431051 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:481052 if (GetLastCommittedEntry()) {
Carlos IL42b416592019-10-07 23:10:361053 if (entry_replaced_by_post_commit_error_) {
1054 if (is_same_document_navigation) {
1055 // Same document navigations should not be possible on error pages and
1056 // would leave the controller in a weird state. Kill the renderer if
1057 // that happens.
1058 bad_message::ReceivedBadMessage(
1059 rfh->GetProcess(), bad_message::NC_SAME_DOCUMENT_POST_COMMIT_ERROR);
1060 }
1061 // Any commit while a post-commit error page is showing should put the
1062 // original entry back, replacing the error page's entry. This includes
1063 // reloads, where the original entry was used as the pending entry and
1064 // should now be at the correct index at commit time.
1065 entries_[last_committed_entry_index_] =
1066 std::move(entry_replaced_by_post_commit_error_);
1067 }
Fergal Daly8e33cf62020-12-12 01:06:071068 details->previous_main_frame_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:551069 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:431070 if (pending_entry_ &&
1071 pending_entry_->GetIsOverridingUserAgent() !=
1072 GetLastCommittedEntry()->GetIsOverridingUserAgent())
1073 overriding_user_agent_changed = true;
[email protected]0e8db942008-09-24 21:21:481074 } else {
Fergal Daly8e33cf62020-12-12 01:06:071075 details->previous_main_frame_url = GURL();
[email protected]0e8db942008-09-24 21:21:481076 details->previous_entry_index = -1;
1077 }
[email protected]ecd9d8702008-08-28 22:10:171078
Alexander Timind2f2e4f22019-04-02 20:04:531079 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1080 // implementing back-forward cache.
1081
1082 // Create a new metrics object or reuse the previous one depending on whether
1083 // it's a main frame navigation or not.
1084 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics =
1085 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1086 GetLastCommittedEntry(), !rfh->GetParent(),
1087 params.document_sequence_number);
1088 // Notify the last active entry that we have navigated away.
1089 if (!rfh->GetParent() && !is_same_document_navigation) {
1090 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1091 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
Alexander Timinde527cd2019-12-02 09:41:121092 metrics->MainFrameDidNavigateAwayFromDocument(rfh, details,
1093 navigation_request);
Alexander Timind2f2e4f22019-04-02 20:04:531094 }
1095 }
1096 }
1097
fdegans9caf66a2015-07-30 21:10:421098 // If there is a pending entry at this point, it should have a SiteInstance,
1099 // except for restored entries.
jam48cea9082017-02-15 06:13:291100 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481101 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
Lukasz Anforowicz6b75c0d2020-12-01 22:56:081102 pending_entry_->IsRestored());
1103 if (pending_entry_ && pending_entry_->IsRestored()) {
Lukasz Anforowicz0de0f452020-12-02 19:57:151104 pending_entry_->set_restore_type(RestoreType::kNotRestored);
jam48cea9082017-02-15 06:13:291105 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481106 }
[email protected]e9ba4472008-09-14 15:42:431107
Nasko Oskovaee2f862018-06-15 00:05:521108 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1109 // has changed, this must be treated as a new navigation with replacement.
1110 // Set the replacement bit here and ClassifyNavigation will identify this
Charlie Reisc0f17d2d2021-01-12 18:52:491111 // case and return NEW_ENTRY.
Nasko Oskovaee2f862018-06-15 00:05:521112 if (!rfh->GetParent() && pending_entry_ &&
Rakina Zata Amnif6950d552020-11-24 03:26:101113 pending_entry_->GetUniqueID() ==
1114 navigation_request->commit_params().nav_entry_id &&
Nasko Oskovaee2f862018-06-15 00:05:521115 pending_entry_->site_instance() &&
1116 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1117 DCHECK_NE(-1, pending_entry_index_);
1118 // TODO(nasko,creis): Instead of setting this value here, set
1119 // should_replace_current_entry on the parameters we send to the
1120 // renderer process as part of CommitNavigation. The renderer should
1121 // in turn send it back here as part of |params| and it can be just
1122 // enforced and renderer process terminated on mismatch.
1123 details->did_replace_entry = true;
1124 } else {
1125 // The renderer tells us whether the navigation replaces the current entry.
1126 details->did_replace_entry = params.should_replace_current_entry;
1127 }
[email protected]bcd904482012-02-01 01:54:221128
[email protected]e9ba4472008-09-14 15:42:431129 // Do navigation-type specific actions. These will make and commit an entry.
Rakina Zata Amnif6950d552020-11-24 03:26:101130 details->type = ClassifyNavigation(rfh, params, navigation_request);
[email protected]4bf3522c2010-08-19 21:00:201131
eugenebutee08663a2017-04-27 17:43:121132 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441133 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121134
Peter Boströmd7592132019-01-30 04:50:311135 // Make sure we do not discard the pending entry for a different ongoing
1136 // navigation when a same document commit comes in unexpectedly from the
1137 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1138 // other navigation types. See https://crbug.com/900036.
1139 // TODO(crbug.com/926009): Handle history.pushState() as well.
1140 bool keep_pending_entry = is_same_document_navigation &&
Charlie Reisc0f17d2d2021-01-12 18:52:491141 details->type == NAVIGATION_TYPE_EXISTING_ENTRY &&
Peter Boströmd7592132019-01-30 04:50:311142 pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451143 !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311144
[email protected]0e8db942008-09-24 21:21:481145 switch (details->type) {
Charlie Reisc0f17d2d2021-01-12 18:52:491146 case NAVIGATION_TYPE_NEW_ENTRY:
1147 RendererDidNavigateToNewEntry(
shivanisha41f04c52018-12-12 15:52:051148 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451149 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431150 break;
Charlie Reisc0f17d2d2021-01-12 18:52:491151 case NAVIGATION_TYPE_EXISTING_ENTRY:
1152 RendererDidNavigateToExistingEntry(rfh, params, details->is_same_document,
1153 was_restored, navigation_request,
1154 keep_pending_entry);
[email protected]e9ba4472008-09-14 15:42:431155 break;
Charlie Reisc0f17d2d2021-01-12 18:52:491156 case NAVIGATION_TYPE_SAME_ENTRY:
1157 RendererDidNavigateToSameEntry(rfh, params, details->is_same_document,
1158 navigation_request);
[email protected]e9ba4472008-09-14 15:42:431159 break;
[email protected]8ff00d72012-10-23 19:12:211160 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471161 RendererDidNavigateNewSubframe(
1162 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451163 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431164 break;
[email protected]8ff00d72012-10-23 19:12:211165 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Antonio Sartori78a749f2020-11-30 12:03:391166 if (!RendererDidNavigateAutoSubframe(
1167 rfh, params, details->is_same_document, navigation_request)) {
creisce0ef3572017-01-26 17:53:081168 // We don't send a notification about auto-subframe PageState during
1169 // UpdateStateForFrame, since it looks like nothing has changed. Send
1170 // it here at commit time instead.
1171 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431172 return false;
creis59d5a47cb2016-08-24 23:57:191173 }
[email protected]e9ba4472008-09-14 15:42:431174 break;
[email protected]8ff00d72012-10-23 19:12:211175 case NAVIGATION_TYPE_NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:491176 // If a pending navigation was in progress, this canceled it. We should
1177 // discard it and make sure it is removed from the URL bar. After that,
1178 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:431179 // the caller that nothing has happened.
arthursonzogni69a6a1b2019-09-17 09:23:001180 if (pending_entry_)
[email protected]20d1c992011-04-12 21:17:491181 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431182 return false;
Aran Gilman37d11632019-10-08 23:07:151183 case NAVIGATION_TYPE_UNKNOWN:
[email protected]e9ba4472008-09-14 15:42:431184 NOTREACHED();
Aran Gilman37d11632019-10-08 23:07:151185 break;
[email protected]765b35502008-08-21 00:51:201186 }
1187
[email protected]688aa65c62012-09-28 04:32:221188 // At this point, we know that the navigation has just completed, so
1189 // record the time.
1190 //
1191 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261192 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331193 base::Time timestamp =
1194 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1195 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
danakjf26536bf2020-09-10 00:46:131196 << timestamp.ToDeltaSinceWindowsEpoch().InMicroseconds();
[email protected]688aa65c62012-09-28 04:32:221197
Peter Boströmd7592132019-01-30 04:50:311198 // If we aren't keeping the pending entry, there shouldn't be one at this
1199 // point. Clear it again in case any error cases above forgot to do so.
1200 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1201 // been cleared instead of protecting against it.
1202 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001203 DiscardNonCommittedEntries();
[email protected]f233e4232013-02-23 00:55:141204
[email protected]e9ba4472008-09-14 15:42:431205 // All committed entries should have nonempty content state so WebKit doesn't
1206 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471207 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321208 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221209 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441210 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531211 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1212 // implementing back-forward cache.
1213 if (!active_entry->back_forward_cache_metrics()) {
1214 active_entry->set_back_forward_cache_metrics(
1215 std::move(back_forward_cache_metrics));
1216 }
1217 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
Hajime Hoshi446206e2019-10-18 18:36:251218 navigation_request,
1219 back_forward_cache_.IsAllowed(navigation_request->GetURL()));
naskoc7533512016-05-06 17:01:121220
Charles Reisc0507202017-09-21 00:40:021221 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1222 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1223 // A mismatch can occur if the renderer lies or due to a unique name collision
1224 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121225 FrameNavigationEntry* frame_entry =
1226 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021227 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1228 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031229 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081230 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1231 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031232 DCHECK(params.page_state == frame_entry->page_state());
Nasko Oskovbbcfc0002019-11-20 20:03:201233
1234 // Remember the bindings the renderer process has at this point, so that
1235 // we do not grant this entry additional bindings if we come back to it.
1236 frame_entry->SetBindings(rfh->GetEnabledBindings());
creis4e2ecb72015-06-20 00:46:301237 }
[email protected]132e281a2012-07-31 18:32:441238
[email protected]97d8f0d2013-10-29 16:49:211239 // Once it is committed, we no longer need to track several pieces of state on
1240 // the entry.
naskoc7533512016-05-06 17:01:121241 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131242
[email protected]49bd30e62011-03-22 20:12:591243 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221244 // TODO(creis): This check won't pass for subframes until we create entries
1245 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001246 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421247 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591248
[email protected]e9ba4472008-09-14 15:42:431249 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001250 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001251 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311252 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531253
Scott Violetc36f7462020-05-06 23:13:031254 // If the NavigationRequest was created without a NavigationEntry and
1255 // SetIsOverridingUserAgent() was called, it needs to be applied to the
1256 // NavigationEntry now.
1257 if (!navigation_request->nav_entry_id() &&
1258 navigation_request->was_set_overriding_user_agent_called()) {
1259 active_entry->SetIsOverridingUserAgent(
1260 navigation_request->entry_overrides_ua());
1261 }
1262
[email protected]93f230e02011-06-01 14:40:001263 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291264
John Abd-El-Malek380d3c5922017-09-08 00:20:311265 if (active_entry->GetURL().SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451266 navigation_request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek969cdd94e2017-07-10 22:18:161267 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus",
1268 !!active_entry->GetSSL().certificate);
1269 }
1270
aelias100c9192017-01-13 00:01:431271 if (overriding_user_agent_changed)
1272 delegate_->UpdateOverridingUserAgent();
1273
creis03b48002015-11-04 00:54:561274 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1275 // one knows the latest NavigationEntry it is showing (whether it has
1276 // committed anything in this navigation or not). This allows things like
1277 // state and title updates from RenderFrames to apply to the latest relevant
1278 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381279 int nav_entry_id = active_entry->GetUniqueID();
1280 for (FrameTreeNode* node : delegate_->GetFrameTree()->Nodes())
1281 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
[email protected]e9ba4472008-09-14 15:42:431282 return true;
initial.commit09911bf2008-07-26 23:55:291283}
1284
[email protected]8ff00d72012-10-23 19:12:211285NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321286 RenderFrameHostImpl* rfh,
Rakina Zata Amnif6950d552020-11-24 03:26:101287 const mojom::DidCommitProvisionalLoadParams& params,
1288 NavigationRequest* navigation_request) {
Piotr Tworekbad51282020-09-30 19:17:591289 TraceReturnReason<tracing_category::kNavigation> trace_return(
Nasko Oskovae49e292020-08-13 02:08:511290 "ClassifyNavigation");
1291
avi7c6f35e2015-05-08 17:52:381292 if (params.did_create_new_entry) {
Charlie Reisc0f17d2d2021-01-12 18:52:491293 // A new entry. We may or may not have a corresponding pending entry, and
avi7c6f35e2015-05-08 17:52:381294 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001295 if (!rfh->GetParent()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491296 trace_return.set_return_reason("new entry, no parent, new entry");
1297 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381298 }
1299
1300 // When this is a new subframe navigation, we should have a committed page
1301 // in which it's a subframe. This may not be the case when an iframe is
1302 // navigated on a popup navigated to about:blank (the iframe would be
1303 // written into the popup by script on the main page). For these cases,
1304 // there isn't any navigation stuff we can do, so just ignore it.
Nasko Oskovae49e292020-08-13 02:08:511305 if (!GetLastCommittedEntry()) {
1306 trace_return.set_return_reason("new entry, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381307 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511308 }
avi7c6f35e2015-05-08 17:52:381309
1310 // Valid subframe navigation.
Nasko Oskovae49e292020-08-13 02:08:511311 trace_return.set_return_reason("new entry, new subframe");
avi7c6f35e2015-05-08 17:52:381312 return NAVIGATION_TYPE_NEW_SUBFRAME;
1313 }
1314
Charlie Reisc0f17d2d2021-01-12 18:52:491315 // We only clear the session history in tests when navigating to a new entry.
avi7c6f35e2015-05-08 17:52:381316 DCHECK(!params.history_list_was_cleared);
1317
avi39c1edd32015-06-04 20:06:001318 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381319 // All manual subframes would be did_create_new_entry and handled above, so
1320 // we know this is auto.
Nasko Oskovae49e292020-08-13 02:08:511321 if (GetLastCommittedEntry()) {
1322 trace_return.set_return_reason("subframe, last commmited, auto subframe");
avi7c6f35e2015-05-08 17:52:381323 return NAVIGATION_TYPE_AUTO_SUBFRAME;
Nasko Oskovae49e292020-08-13 02:08:511324 }
Lei Zhang96031532019-10-10 19:05:471325
1326 // We ignore subframes created in non-committed pages; we'd appreciate if
1327 // people stopped doing that.
Nasko Oskovae49e292020-08-13 02:08:511328 trace_return.set_return_reason("subframe, no last commmited, ignore");
Lei Zhang96031532019-10-10 19:05:471329 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381330 }
1331
Rakina Zata Amnif6950d552020-11-24 03:26:101332 const int nav_entry_id = navigation_request->commit_params().nav_entry_id;
1333 if (nav_entry_id == 0) {
avi7c6f35e2015-05-08 17:52:381334 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1335 // create a new page.
1336
1337 // Just like above in the did_create_new_entry case, it's possible to
1338 // scribble onto an uncommitted page. Again, there isn't any navigation
1339 // stuff that we can do, so ignore it here as well.
avi3a5b8f32015-05-28 17:50:231340 NavigationEntry* last_committed = GetLastCommittedEntry();
Nasko Oskovae49e292020-08-13 02:08:511341 if (!last_committed) {
1342 trace_return.set_return_reason("nav entry 0, no last committed, ignore");
avi7c6f35e2015-05-08 17:52:381343 return NAVIGATION_TYPE_NAV_IGNORE;
Nasko Oskovae49e292020-08-13 02:08:511344 }
avi7c6f35e2015-05-08 17:52:381345
Charles Reis1378111f2017-11-08 21:44:061346 // This is history.replaceState() or history.reload().
Nasko Oskov332593c2018-08-16 17:21:341347 // TODO(nasko): With error page isolation, reloading an existing session
1348 // history entry can result in change of SiteInstance. Check for such a case
Charlie Reisc0f17d2d2021-01-12 18:52:491349 // here and classify it as NEW_ENTRY, as such navigations should be treated
Nasko Oskov332593c2018-08-16 17:21:341350 // as new with replacement.
Nasko Oskovae49e292020-08-13 02:08:511351 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491352 "nav entry 0, last committed, existing entry");
1353 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381354 }
1355
Rakina Zata Amnif6950d552020-11-24 03:26:101356 if (pending_entry_ && pending_entry_->GetUniqueID() == nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521357 // If the SiteInstance of the |pending_entry_| does not match the
1358 // SiteInstance that got committed, treat this as a new navigation with
1359 // replacement. This can happen if back/forward/reload encounters a server
1360 // redirect to a different site or an isolated error page gets successfully
1361 // reloaded into a different SiteInstance.
1362 if (pending_entry_->site_instance() &&
1363 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491364 trace_return.set_return_reason("pending matching nav entry, new entry");
1365 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521366 }
creis77c9aa32015-09-25 19:59:421367
Nasko Oskovaee2f862018-06-15 00:05:521368 if (pending_entry_index_ == -1) {
1369 // In this case, we have a pending entry for a load of a new URL but Blink
1370 // didn't do a new navigation (params.did_create_new_entry). First check
1371 // to make sure Blink didn't treat a new cross-process navigation as
1372 // inert, and thus set params.did_create_new_entry to false. In that case,
1373 // we must treat it as NEW since the SiteInstance doesn't match the entry.
1374 if (!GetLastCommittedEntry() ||
1375 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491376 trace_return.set_return_reason("no pending, new entry");
1377 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521378 }
1379
1380 // Otherwise, this happens when you press enter in the URL bar to reload.
1381 // We will create a pending entry, but Blink will convert it to a reload
1382 // since it's the same page and not create a new entry for it (the user
1383 // doesn't want to have a new back/forward entry when they do this).
1384 // Therefore we want to just ignore the pending entry and go back to where
1385 // we were (the "existing entry").
Charlie Reisc0f17d2d2021-01-12 18:52:491386 // TODO(creis,avi): Eliminate SAME_ENTRY in https://crbug.com/536102.
1387 trace_return.set_return_reason("no pending, same entry");
1388 return NAVIGATION_TYPE_SAME_ENTRY;
Nasko Oskovaee2f862018-06-15 00:05:521389 }
avi7c6f35e2015-05-08 17:52:381390 }
1391
creis26d22632017-04-21 20:23:561392 // Everything below here is assumed to be an existing entry, but if there is
1393 // no last committed entry, we must consider it a new navigation instead.
Nasko Oskovae49e292020-08-13 02:08:511394 if (!GetLastCommittedEntry()) {
Charlie Reisc0f17d2d2021-01-12 18:52:491395 trace_return.set_return_reason("no last committed, new entry");
1396 return NAVIGATION_TYPE_NEW_ENTRY;
Nasko Oskovae49e292020-08-13 02:08:511397 }
creis26d22632017-04-21 20:23:561398
avi7c6f35e2015-05-08 17:52:381399 if (params.intended_as_new_entry) {
1400 // This was intended to be a navigation to a new entry but the pending entry
Charlie Reisc0f17d2d2021-01-12 18:52:491401 // got cleared in the meanwhile. Classify as EXISTING_ENTRY because we may
1402 // or may not have a pending entry.
1403 trace_return.set_return_reason("indented as new entry, existing entry");
1404 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381405 }
1406
1407 if (params.url_is_unreachable && failed_pending_entry_id_ != 0 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101408 nav_entry_id == failed_pending_entry_id_) {
avi7c6f35e2015-05-08 17:52:381409 // If the renderer was going to a new pending entry that got cleared because
1410 // of an error, this is the case of the user trying to retry a failed load
Charlie Reisc0f17d2d2021-01-12 18:52:491411 // by pressing return. Classify as EXISTING_ENTRY because we probably don't
avi7c6f35e2015-05-08 17:52:381412 // have a pending entry.
Nasko Oskovae49e292020-08-13 02:08:511413 trace_return.set_return_reason(
Charlie Reisc0f17d2d2021-01-12 18:52:491414 "unreachable, matching pending, existing entry");
1415 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381416 }
1417
Charlie Reisc0f17d2d2021-01-12 18:52:491418 // Now we know that the notification is for an existing entry; find it.
Rakina Zata Amnif6950d552020-11-24 03:26:101419 int existing_entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
Nasko Oskovae49e292020-08-13 02:08:511420 trace_return.traced_value()->SetInteger("existing_entry_index",
1421 existing_entry_index);
avi7c6f35e2015-05-08 17:52:381422 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441423 // The renderer has committed a navigation to an entry that no longer
1424 // exists. Because the renderer is showing that page, resurrect that entry.
Charlie Reisc0f17d2d2021-01-12 18:52:491425 trace_return.set_return_reason("existing entry -1, new entry");
1426 return NAVIGATION_TYPE_NEW_ENTRY;
avi7c6f35e2015-05-08 17:52:381427 }
1428
avi7c6f35e2015-05-08 17:52:381429 // Since we weeded out "new" navigations above, we know this is an existing
1430 // (back/forward) navigation.
Charlie Reisc0f17d2d2021-01-12 18:52:491431 trace_return.set_return_reason("default return, existing entry");
1432 return NAVIGATION_TYPE_EXISTING_ENTRY;
avi7c6f35e2015-05-08 17:52:381433}
1434
Charlie Reisc0f17d2d2021-01-12 18:52:491435void NavigationControllerImpl::RendererDidNavigateToNewEntry(
creis3da03872015-02-20 21:12:321436 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071437 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361438 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441439 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471440 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451441 NavigationRequest* request) {
dcheng9bfa5162016-04-09 01:00:571442 std::unique_ptr<NavigationEntryImpl> new_entry;
creisf49dfc92016-07-26 17:05:181443 bool update_virtual_url = false;
1444
Lukasz Anforowicz435bcb582019-07-12 20:50:061445 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451446 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061447
creisf49dfc92016-07-26 17:05:181448 // First check if this is an in-page navigation. If so, clone the current
1449 // entry instead of looking at the pending entry, because the pending entry
1450 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361451 if (is_same_document && GetLastCommittedEntry()) {
Patrick Monette50e8bd82019-06-13 22:40:451452 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481453 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
creisf49dfc92016-07-26 17:05:181454 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Nasko Oskov03912102019-01-11 00:21:321455 params.url, (params.url_is_unreachable) ? nullptr : &params.origin,
arthursonzogni73fe3212020-11-17 13:24:071456 Referrer(*params.referrer), initiator_origin, params.redirects,
1457 params.page_state, params.method, params.post_id,
1458 nullptr /* blob_url_loader_factory */,
Antonio Sartori78a749f2020-11-30 12:03:391459 nullptr /* web_bundle_navigation_info */,
1460 // We will set the document policies later in this function.
1461 nullptr /* document_policies */);
Charles Reisf44482022017-10-13 21:15:031462
creisf49dfc92016-07-26 17:05:181463 new_entry = GetLastCommittedEntry()->CloneAndReplace(
1464 frame_entry, true, rfh->frame_tree_node(),
1465 delegate_->GetFrameTree()->root());
jama78746e2017-02-22 17:21:571466 if (new_entry->GetURL().GetOrigin() != params.url.GetOrigin()) {
1467 // TODO(jam): we had one report of this with a URL that was redirecting to
1468 // only tildes. Until we understand that better, don't copy the cert in
1469 // this case.
1470 new_entry->GetSSL() = SSLStatus();
John Abd-El-Malek9cd697e2017-07-12 21:53:141471
John Abd-El-Malek380d3c5922017-09-08 00:20:311472 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451473 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141474 UMA_HISTOGRAM_BOOLEAN(
1475 "Navigation.SecureSchemeHasSSLStatus.NewPageInPageOriginMismatch",
1476 !!new_entry->GetSSL().certificate);
1477 }
jama78746e2017-02-22 17:21:571478 }
creisf49dfc92016-07-26 17:05:181479
Patrick Monette50e8bd82019-06-13 22:40:451480 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1481 // that |frame_entry| should now have a reference count of exactly 2: one
1482 // due to the local variable |frame_entry|, and another due to |new_entry|
1483 // also retaining one. This should never fail, because it's the main frame.
1484 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181485
1486 update_virtual_url = new_entry->update_virtual_url_with_url();
John Abd-El-Malek9cd697e2017-07-12 21:53:141487
John Abd-El-Malek380d3c5922017-09-08 00:20:311488 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451489 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141490 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus.NewPageInPage",
1491 !!new_entry->GetSSL().certificate);
1492 }
creisf49dfc92016-07-26 17:05:181493 }
1494
Charlie Reisc0f17d2d2021-01-12 18:52:491495 // Only make a copy of the pending entry if it is appropriate for the new
1496 // document that just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451497 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041498 // 1. The SiteInstance hasn't been assigned to something else.
1499 // 2. The pending entry was intended as a new entry, rather than being a
1500 // history navigation that was interrupted by an unrelated,
1501 // renderer-initiated navigation.
1502 // TODO(csharrison): Investigate whether we can remove some of the coarser
1503 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451504 if (!new_entry && PendingEntryMatchesRequest(request) &&
1505 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341506 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431507 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351508 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431509
[email protected]f1eb87a2011-05-06 17:49:411510 update_virtual_url = new_entry->update_virtual_url_with_url();
Camille Lamy62b826012019-02-26 09:15:471511 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451512 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141513
John Abd-El-Malek380d3c5922017-09-08 00:20:311514 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451515 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141516 UMA_HISTOGRAM_BOOLEAN(
1517 "Navigation.SecureSchemeHasSSLStatus.NewPagePendingEntryMatches",
1518 !!new_entry->GetSSL().certificate);
1519 }
creisf49dfc92016-07-26 17:05:181520 }
1521
Charlie Reisc0f17d2d2021-01-12 18:52:491522 // For cross-document commits with no matching pending entry, create a new
1523 // entry.
creisf49dfc92016-07-26 17:05:181524 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061525 new_entry = std::make_unique<NavigationEntryImpl>(
arthursonzogni73fe3212020-11-17 13:24:071526 rfh->GetSiteInstance(), params.url, Referrer(*params.referrer),
1527 initiator_origin,
Lukasz Anforowicz435bcb582019-07-12 20:50:061528 base::string16(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451529 params.transition, request->IsRendererInitiated(),
Lukasz Anforowicz435bcb582019-07-12 20:50:061530 nullptr); // blob_url_loader_factory
[email protected]f8f93eb2012-09-25 03:06:241531
1532 // Find out whether the new entry needs to update its virtual URL on URL
1533 // change and set up the entry accordingly. This is needed to correctly
1534 // update the virtual URL when replaceState is called after a pushState.
1535 GURL url = params.url;
1536 bool needs_update = false;
[email protected]47752982014-07-29 08:01:431537 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1538 &url, browser_context_, &needs_update);
[email protected]f8f93eb2012-09-25 03:06:241539 new_entry->set_update_virtual_url_with_url(needs_update);
1540
Charlie Reisc0f17d2d2021-01-12 18:52:491541 // When navigating to a new entry, give the browser URL handler a chance to
[email protected]f1eb87a2011-05-06 17:49:411542 // update the virtual URL based on the new URL. For example, this is needed
1543 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1544 // the URL.
[email protected]f8f93eb2012-09-25 03:06:241545 update_virtual_url = needs_update;
Camille Lamy62b826012019-02-26 09:15:471546 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451547 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141548
John Abd-El-Malek380d3c5922017-09-08 00:20:311549 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451550 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141551 UMA_HISTOGRAM_BOOLEAN(
1552 "Navigation.SecureSchemeHasSSLStatus.NewPageNoMatchingEntry",
1553 !!new_entry->GetSSL().certificate);
1554 }
[email protected]e9ba4472008-09-14 15:42:431555 }
1556
wjmaclean7431bb22015-02-19 14:53:431557 // Don't use the page type from the pending entry. Some interstitial page
1558 // may have set the type to interstitial. Once we commit, however, the page
1559 // type must always be normal or error.
1560 new_entry->set_page_type(params.url_is_unreachable ? PAGE_TYPE_ERROR
1561 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041562 new_entry->SetURL(params.url);
[email protected]f1eb87a2011-05-06 17:49:411563 if (update_virtual_url)
avi25764702015-06-23 15:43:371564 UpdateVirtualURLToURL(new_entry.get(), params.url);
arthursonzogni73fe3212020-11-17 13:24:071565 new_entry->SetReferrer(Referrer(*params.referrer));
[email protected]022af742011-12-28 18:37:251566 new_entry->SetTransitionType(params.transition);
[email protected]b6583592012-01-25 19:52:331567 new_entry->set_site_instance(
[email protected]27dd82fd2014-03-03 22:11:431568 static_cast<SiteInstanceImpl*>(rfh->GetSiteInstance()));
[email protected]7c16976c2012-08-04 02:38:231569 new_entry->SetOriginalRequestURL(params.original_request_url);
Rakina Zata Amnib597d632020-12-01 00:56:001570
1571 if (!is_same_document) {
1572 DCHECK_EQ(request->IsOverridingUserAgent() && !rfh->GetParent(),
1573 params.is_overriding_user_agent);
1574 } else {
1575 DCHECK_EQ(rfh->is_overriding_user_agent(), params.is_overriding_user_agent);
1576 }
[email protected]bf70edce2012-06-20 22:32:221577 new_entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
[email protected]e9ba4472008-09-14 15:42:431578
creis8b5cd4c2015-06-19 00:11:081579 // Update the FrameNavigationEntry for new main frame commits.
1580 FrameNavigationEntry* frame_entry =
1581 new_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisd2a509f2017-09-27 23:47:481582 frame_entry->set_frame_unique_name(rfh->frame_tree_node()->unique_name());
creis8b5cd4c2015-06-19 00:11:081583 frame_entry->set_item_sequence_number(params.item_sequence_number);
1584 frame_entry->set_document_sequence_number(params.document_sequence_number);
Charles Reisf44482022017-10-13 21:15:031585 frame_entry->set_redirect_chain(params.redirects);
1586 frame_entry->SetPageState(params.page_state);
clamy432acb22016-04-15 19:41:431587 frame_entry->set_method(params.method);
1588 frame_entry->set_post_id(params.post_id);
Antonio Sartori78a749f2020-11-30 12:03:391589 frame_entry->set_document_policies(
1590 ComputeDocumentPoliciesForFrameEntry(rfh, is_same_document, request));
1591
Nasko Oskov03912102019-01-11 00:21:321592 if (!params.url_is_unreachable)
1593 frame_entry->set_committed_origin(params.origin);
Tsuyoshi Horob7b033e2020-03-09 15:23:311594 if (request->web_bundle_navigation_info()) {
1595 frame_entry->set_web_bundle_navigation_info(
1596 request->web_bundle_navigation_info()->Clone());
1597 }
creis8b5cd4c2015-06-19 00:11:081598
eugenebut604866f2017-05-10 21:35:361599 // history.pushState() is classified as a navigation to a new page, but sets
1600 // is_same_document to true. In this case, we already have the title and
creisf49dfc92016-07-26 17:05:181601 // favicon available, so set them immediately.
eugenebut604866f2017-05-10 21:35:361602 if (is_same_document && GetLastCommittedEntry()) {
[email protected]ff64b3e2014-05-31 04:07:331603 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
[email protected]3a868f212014-08-09 10:41:191604 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1605 }
[email protected]ff64b3e2014-05-31 04:07:331606
[email protected]60d6cca2013-04-30 08:47:131607 DCHECK(!params.history_list_was_cleared || !replace_entry);
1608 // The browser requested to clear the session history when it initiated the
1609 // navigation. Now we know that the renderer has updated its state accordingly
1610 // and it is safe to also clear the browser side history.
1611 if (params.history_list_was_cleared) {
arthursonzogni69a6a1b2019-09-17 09:23:001612 DiscardNonCommittedEntries();
[email protected]60d6cca2013-04-30 08:47:131613 entries_.clear();
1614 last_committed_entry_index_ = -1;
1615 }
1616
Nasko Oskovaee2f862018-06-15 00:05:521617 // If this is a new navigation with replacement and there is a
1618 // pending_entry_ which matches the navigation reported by the renderer
1619 // process, then it should be the one replaced, so update the
1620 // last_committed_entry_index_ to use it.
1621 if (replace_entry && pending_entry_index_ != -1 &&
Rakina Zata Amnif6950d552020-11-24 03:26:101622 pending_entry_->GetUniqueID() == request->commit_params().nav_entry_id) {
Nasko Oskovaee2f862018-06-15 00:05:521623 last_committed_entry_index_ = pending_entry_index_;
1624 }
1625
Alexander Timine3ec4192020-04-20 16:39:401626 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411627 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401628 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
shivanisha41f04c52018-12-12 15:52:051629
Carlos IL42b416592019-10-07 23:10:361630 InsertOrReplaceEntry(std::move(new_entry), replace_entry,
1631 !request->post_commit_error_page_html().empty());
[email protected]e9ba4472008-09-14 15:42:431632}
1633
Charlie Reisc0f17d2d2021-01-12 18:52:491634void NavigationControllerImpl::RendererDidNavigateToExistingEntry(
creis3da03872015-02-20 21:12:321635 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071636 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361637 bool is_same_document,
jam48cea9082017-02-15 06:13:291638 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451639 NavigationRequest* request,
Peter Boströmd7592132019-01-30 04:50:311640 bool keep_pending_entry) {
creis26d22632017-04-21 20:23:561641 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1642 << "that a last committed entry exists.";
1643
[email protected]e9ba4472008-09-14 15:42:431644 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001645 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431646
avicbdc4c12015-07-01 16:07:111647 NavigationEntryImpl* entry;
1648 if (params.intended_as_new_entry) {
1649 // This was intended as a new entry but the pending entry was lost in the
Charlie Reisc0f17d2d2021-01-12 18:52:491650 // meanwhile and no new entry was created. We are stuck at the last
1651 // committed entry.
avicbdc4c12015-07-01 16:07:111652 entry = GetLastCommittedEntry();
eugenebut604866f2017-05-10 21:35:361653 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451654 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361655 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471656 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451657 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141658
John Abd-El-Malek380d3c5922017-09-08 00:20:311659 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451660 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141661 bool has_cert = !!entry->GetSSL().certificate;
1662 if (is_same_document) {
1663 UMA_HISTOGRAM_BOOLEAN(
1664 "Navigation.SecureSchemeHasSSLStatus."
1665 "ExistingPageSameDocumentIntendedAsNew",
1666 has_cert);
1667 } else {
1668 UMA_HISTOGRAM_BOOLEAN(
1669 "Navigation.SecureSchemeHasSSLStatus."
1670 "ExistingPageDifferentDocumentIntendedAsNew",
1671 has_cert);
1672 }
1673 }
Rakina Zata Amnif6950d552020-11-24 03:26:101674 } else if (const int nav_entry_id = request->commit_params().nav_entry_id) {
avicbdc4c12015-07-01 16:07:111675 // This is a browser-initiated navigation (back/forward/reload).
Rakina Zata Amnif6950d552020-11-24 03:26:101676 entry = GetEntryWithUniqueID(nav_entry_id);
jamd208b90ce2016-09-01 16:58:161677
eugenebut604866f2017-05-10 21:35:361678 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451679 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361680 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1681 // this was a restored same document navigation entry, then it won't have
1682 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1683 // navigated it.
jam48cea9082017-02-15 06:13:291684 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
1685 if (entry->GetURL().GetOrigin() == last_entry->GetURL().GetOrigin() &&
1686 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1687 was_restored) {
1688 entry->GetSSL() = last_entry->GetSSL();
1689 }
1690 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451691 // In rapid back/forward navigations |request| sometimes won't have a cert
1692 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191693 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451694 if (request->GetSSLInfo().has_value() &&
1695 request->GetSSLInfo()->is_valid()) {
1696 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
1697 } else if (entry->GetURL().GetOrigin() != request->GetURL().GetOrigin()) {
John Abd-El-Malek3f247082017-12-07 19:02:191698 entry->GetSSL() = SSLStatus();
1699 }
jam48cea9082017-02-15 06:13:291700 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141701
John Abd-El-Malek380d3c5922017-09-08 00:20:311702 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451703 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141704 bool has_cert = !!entry->GetSSL().certificate;
1705 if (is_same_document && was_restored) {
1706 UMA_HISTOGRAM_BOOLEAN(
1707 "Navigation.SecureSchemeHasSSLStatus."
1708 "ExistingPageSameDocumentRestoredBrowserInitiated",
1709 has_cert);
1710 } else if (is_same_document && !was_restored) {
1711 UMA_HISTOGRAM_BOOLEAN(
1712 "Navigation.SecureSchemeHasSSLStatus."
1713 "ExistingPageSameDocumentBrowserInitiated",
1714 has_cert);
1715 } else if (!is_same_document && was_restored) {
1716 UMA_HISTOGRAM_BOOLEAN(
1717 "Navigation.SecureSchemeHasSSLStatus."
1718 "ExistingPageRestoredBrowserInitiated",
1719 has_cert);
1720 } else {
1721 UMA_HISTOGRAM_BOOLEAN(
1722 "Navigation.SecureSchemeHasSSLStatus.ExistingPageBrowserInitiated",
1723 has_cert);
1724 }
1725 }
avicbdc4c12015-07-01 16:07:111726 } else {
1727 // This is renderer-initiated. The only kinds of renderer-initated
Charlie Reisc0f17d2d2021-01-12 18:52:491728 // navigations that are EXISTING_ENTRY are reloads and history.replaceState,
avicbdc4c12015-07-01 16:07:111729 // which land us at the last committed entry.
1730 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101731
Mikel Astizba9cf2fd2017-12-17 10:38:101732 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1733 // to avoid misleading interpretations (e.g. URLs paired with
1734 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1735 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1736 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1737
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571738 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:101739
eugenebut604866f2017-05-10 21:35:361740 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451741 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361742 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471743 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451744 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141745
John Abd-El-Malek380d3c5922017-09-08 00:20:311746 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451747 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141748 bool has_cert = !!entry->GetSSL().certificate;
1749 if (is_same_document) {
1750 UMA_HISTOGRAM_BOOLEAN(
1751 "Navigation.SecureSchemeHasSSLStatus."
1752 "ExistingPageSameDocumentRendererInitiated",
1753 has_cert);
1754 } else {
1755 UMA_HISTOGRAM_BOOLEAN(
1756 "Navigation.SecureSchemeHasSSLStatus."
1757 "ExistingPageDifferentDocumentRendererInitiated",
1758 has_cert);
1759 }
1760 }
avicbdc4c12015-07-01 16:07:111761 }
1762 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:431763
[email protected]5ccd4dc2012-10-24 02:28:141764 // The URL may have changed due to redirects.
wjmaclean7431bb22015-02-19 14:53:431765 entry->set_page_type(params.url_is_unreachable ? PAGE_TYPE_ERROR
1766 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041767 entry->SetURL(params.url);
arthursonzogni73fe3212020-11-17 13:24:071768 entry->SetReferrer(Referrer(*params.referrer));
[email protected]38178a42009-12-17 18:58:321769 if (entry->update_virtual_url_with_url())
1770 UpdateVirtualURLToURL(entry, params.url);
[email protected]5ccd4dc2012-10-24 02:28:141771
jam015ba062017-01-06 21:17:001772 // The site instance will normally be the same except
1773 // 1) session restore, when no site instance will be assigned or
1774 // 2) redirect, when the site instance is reset.
Lei Zhang96031532019-10-10 19:05:471775 DCHECK(!entry->site_instance() || !entry->GetRedirectChain().empty() ||
naskoaf182192016-08-11 02:12:011776 entry->site_instance() == rfh->GetSiteInstance());
clamy432acb22016-04-15 19:41:431777
naskoaf182192016-08-11 02:12:011778 // Update the existing FrameNavigationEntry to ensure all of its members
1779 // reflect the parameters coming from the renderer process.
Lukasz Anforowicz435bcb582019-07-12 20:50:061780 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451781 request->common_params().initiator_origin;
naskoaf182192016-08-11 02:12:011782 entry->AddOrUpdateFrameEntry(
1783 rfh->frame_tree_node(), params.item_sequence_number,
1784 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
arthursonzogni73fe3212020-11-17 13:24:071785 params.url, GetCommittedOriginForFrameEntry(params),
1786 Referrer(*params.referrer), initiator_origin, params.redirects,
1787 params.page_state, params.method, params.post_id,
1788 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031789 request->web_bundle_navigation_info()
1790 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391791 : nullptr,
1792 ComputeDocumentPoliciesForFrameEntry(rfh, is_same_document, request));
creis22a7b4c2016-04-28 07:20:301793
[email protected]5ccd4dc2012-10-24 02:28:141794 // The redirected to page should not inherit the favicon from the previous
1795 // page.
eugenebut604866f2017-05-10 21:35:361796 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:481797 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:141798
Peter Boströmd7592132019-01-30 04:50:311799 // We should also usually discard the pending entry if it corresponds to a
1800 // different navigation, since that one is now likely canceled. In rare
1801 // cases, we leave the pending entry for another navigation in place when we
1802 // know it is still ongoing, to avoid a flicker in the omnibox (see
1803 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:431804 //
1805 // Note that we need to use the "internal" version since we don't want to
1806 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:311807 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001808 DiscardNonCommittedEntries();
[email protected]40bcc302009-03-02 20:50:391809
Carlos IL4dea8902020-05-26 15:14:291810 // Update the last committed index to reflect the committed entry.
avicbdc4c12015-07-01 16:07:111811 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:431812}
1813
Charlie Reisc0f17d2d2021-01-12 18:52:491814void NavigationControllerImpl::RendererDidNavigateToSameEntry(
creis3da03872015-02-20 21:12:321815 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071816 const mojom::DidCommitProvisionalLoadParams& params,
Joe DeBlasio531e3d92018-11-06 06:26:121817 bool is_same_document,
Mohamed Abdelhalim833de902019-09-16 17:41:451818 NavigationRequest* request) {
aviaf5660962015-06-30 15:12:291819 // This classification says that we have a pending entry that's the same as
1820 // the last committed entry. This entry is guaranteed to exist by
[email protected]4c27ba82008-09-24 16:49:091821 // ClassifyNavigation. All we need to do is update the existing entry.
aviaf5660962015-06-30 15:12:291822 NavigationEntryImpl* existing_entry = GetLastCommittedEntry();
[email protected]e9ba4472008-09-14 15:42:431823
creis77c9aa32015-09-25 19:59:421824 // If we classified this correctly, the SiteInstance should not have changed.
1825 CHECK_EQ(existing_entry->site_instance(), rfh->GetSiteInstance());
1826
[email protected]e9ba4472008-09-14 15:42:431827 // We assign the entry's unique ID to be that of the new one. Since this is
1828 // always the result of a user action, we want to dismiss infobars, etc. like
1829 // a regular user-initiated navigation.
Rakina Zata Amnif6950d552020-11-24 03:26:101830 DCHECK_EQ(pending_entry_->GetUniqueID(),
1831 request->commit_params().nav_entry_id);
[email protected]36fc0392011-12-25 03:59:511832 existing_entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]e9ba4472008-09-14 15:42:431833
[email protected]a0e69262009-06-03 19:08:481834 // The URL may have changed due to redirects.
wjmaclean7431bb22015-02-19 14:53:431835 existing_entry->set_page_type(params.url_is_unreachable ? PAGE_TYPE_ERROR
1836 : PAGE_TYPE_NORMAL);
[email protected]38178a42009-12-17 18:58:321837 if (existing_entry->update_virtual_url_with_url())
1838 UpdateVirtualURLToURL(existing_entry, params.url);
[email protected]ad23a092011-12-28 07:02:041839 existing_entry->SetURL(params.url);
[email protected]a0e69262009-06-03 19:08:481840
jamd208b90ce2016-09-01 16:58:161841 // If a user presses enter in the omnibox and the server redirects, the URL
Charlie Reisc0f17d2d2021-01-12 18:52:491842 // might change (but it's still considered a SAME_ENTRY navigation), so we
1843 // must update the SSL status if we perform a network request (e.g. a
Joe DeBlasio531e3d92018-11-06 06:26:121844 // non-same-document navigation). Requests that don't result in a network
1845 // request do not have a valid SSL status, but since the document didn't
1846 // change, the previous SSLStatus is still valid.
1847 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471848 existing_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451849 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
jamd208b90ce2016-09-01 16:58:161850
John Abd-El-Malek509dfd62017-09-05 21:34:491851 if (existing_entry->GetURL().SchemeIs(url::kHttpsScheme) &&
Mohamed Abdelhalim833de902019-09-16 17:41:451852 !rfh->GetParent() && request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141853 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus.SamePage",
1854 !!existing_entry->GetSSL().certificate);
1855 }
1856
jianlid26f6c92016-10-12 21:03:171857 // The extra headers may have changed due to reloading with different headers.
1858 existing_entry->set_extra_headers(pending_entry_->extra_headers());
1859
naskoaf182192016-08-11 02:12:011860 // Update the existing FrameNavigationEntry to ensure all of its members
1861 // reflect the parameters coming from the renderer process.
Lukasz Anforowicz435bcb582019-07-12 20:50:061862 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451863 request->common_params().initiator_origin;
naskoaf182192016-08-11 02:12:011864 existing_entry->AddOrUpdateFrameEntry(
1865 rfh->frame_tree_node(), params.item_sequence_number,
1866 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
arthursonzogni73fe3212020-11-17 13:24:071867 params.url, GetCommittedOriginForFrameEntry(params),
1868 Referrer(*params.referrer), initiator_origin, params.redirects,
1869 params.page_state, params.method, params.post_id,
1870 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031871 request->web_bundle_navigation_info()
1872 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391873 : nullptr,
1874 ComputeDocumentPoliciesForFrameEntry(rfh, is_same_document, request));
[email protected]b77686522013-12-11 20:34:191875
[email protected]cbab76d2008-10-13 22:42:471876 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431877}
1878
[email protected]d202a7c2012-01-04 07:53:471879void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:321880 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071881 const mojom::DidCommitProvisionalLoadParams& params,
eugenebut604866f2017-05-10 21:35:361882 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:471883 bool replace_entry,
1884 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451885 NavigationRequest* request) {
avi25f5f9e2015-07-17 20:08:261886 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1887 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
[email protected]09b8f82f2009-06-16 20:22:111888
[email protected]e9ba4472008-09-14 15:42:431889 // Manual subframe navigations just get the current entry cloned so the user
1890 // can go back or forward to it. The actual subframe information will be
1891 // stored in the page state for each of those entries. This happens out of
1892 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:091893 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1894 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:381895
Mikel Astizba9cf2fd2017-12-17 10:38:101896 // The DCHECK below documents the fact that we don't know of any situation
1897 // where |replace_entry| is true for subframe navigations. This simplifies
1898 // reasoning about the replacement struct for subframes (see
1899 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
1900 DCHECK(!replace_entry);
1901
Patrick Monette50e8bd82019-06-13 22:40:451902 // This FrameNavigationEntry might not end up being used in the
1903 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Lukasz Anforowicz435bcb582019-07-12 20:50:061904 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451905 request->common_params().initiator_origin;
Patrick Monette50e8bd82019-06-13 22:40:451906 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481907 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
creisce0ef3572017-01-26 17:53:081908 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Nasko Oskov03912102019-01-11 00:21:321909 params.url, (params.url_is_unreachable) ? nullptr : &params.origin,
arthursonzogni73fe3212020-11-17 13:24:071910 Referrer(*params.referrer), initiator_origin, params.redirects,
1911 params.page_state, params.method, params.post_id,
1912 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031913 request->web_bundle_navigation_info()
1914 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391915 : nullptr,
1916 ComputeDocumentPoliciesForFrameEntry(rfh, is_same_document, request));
Charles Reisf44482022017-10-13 21:15:031917
creisce0ef3572017-01-26 17:53:081918 std::unique_ptr<NavigationEntryImpl> new_entry =
1919 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:451920 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
creisce0ef3572017-01-26 17:53:081921 delegate_->GetFrameTree()->root());
creise062d542015-08-25 02:01:551922
Alexander Timine3ec4192020-04-20 16:39:401923 SetShouldSkipOnBackForwardUIIfNeeded(
shivanigithube92c33da2020-09-14 13:01:411924 replace_entry, previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:401925 request->IsRendererInitiated(), request->GetPreviousPageUkmSourceId());
Shivani Sharmaffb32b82019-04-09 16:58:471926
creisce0ef3572017-01-26 17:53:081927 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:451928 // to replace, which can happen due to a unique name change. See
1929 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
1930 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:381931
Carlos IL42b416592019-10-07 23:10:361932 InsertOrReplaceEntry(std::move(new_entry), replace_entry, false);
[email protected]e9ba4472008-09-14 15:42:431933}
1934
[email protected]d202a7c2012-01-04 07:53:471935bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:321936 RenderFrameHostImpl* rfh,
arthursonzogni73fe3212020-11-17 13:24:071937 const mojom::DidCommitProvisionalLoadParams& params,
Antonio Sartori78a749f2020-11-30 12:03:391938 bool is_same_document,
Mohamed Abdelhalim833de902019-09-16 17:41:451939 NavigationRequest* request) {
avi9f07a0c2015-02-18 22:51:291940 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1941 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
1942
[email protected]e9ba4472008-09-14 15:42:431943 // We're guaranteed to have a previously committed entry, and we now need to
1944 // handle navigation inside of a subframe in it without creating a new entry.
1945 DCHECK(GetLastCommittedEntry());
1946
creis913c63ce2016-07-16 19:52:521947 // For newly created subframes, we don't need to send a commit notification.
1948 // This is only necessary for history navigations in subframes.
1949 bool send_commit_notification = false;
1950
Rakina Zata Amnif6950d552020-11-24 03:26:101951 // If |nav_entry_id| is non-zero and matches an existing entry, this
1952 // is a history navigation. Update the last committed index accordingly. If
1953 // we don't recognize the |nav_entry_id|, it might be a recently
1954 // pruned entry. We'll handle it below.
1955 if (const int nav_entry_id = request->commit_params().nav_entry_id) {
1956 int entry_index = GetEntryIndexWithUniqueID(nav_entry_id);
creis3cdc3b02015-05-29 23:00:471957 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:061958 // Make sure that a subframe commit isn't changing the main frame's
1959 // origin. Otherwise the renderer process may be confused, leading to a
1960 // URL spoof. We can't check the path since that may change
1961 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:571962 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
1963 // about:blank, file, and unique origins are more subtle to get right.
Eugene But9fb98d12018-05-22 18:28:331964 // We'll abstract out the relevant checks from IsURLSameDocumentNavigation
1965 // and share them here. See https://crbug.com/618104.
creis37988b92016-06-10 18:03:571966 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
1967 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
1968 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
1969 dest_top_url.SchemeIsHTTPOrHTTPS() &&
1970 current_top_url.GetOrigin() != dest_top_url.GetOrigin()) {
creisfb6eeb62016-05-10 19:01:511971 bad_message::ReceivedBadMessage(rfh->GetProcess(),
1972 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:061973 }
creis3cdc3b02015-05-29 23:00:471974
creis913c63ce2016-07-16 19:52:521975 // We only need to discard the pending entry in this history navigation
1976 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:061977 last_committed_entry_index_ = entry_index;
arthursonzogni69a6a1b2019-09-17 09:23:001978 DiscardNonCommittedEntries();
creis913c63ce2016-07-16 19:52:521979
1980 // History navigations should send a commit notification.
1981 send_commit_notification = true;
avi98405c22015-05-21 20:47:061982 }
[email protected]e9ba4472008-09-14 15:42:431983 }
[email protected]f233e4232013-02-23 00:55:141984
creisce0ef3572017-01-26 17:53:081985 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
1986 // it may be a "history auto" case where we update an existing one.
1987 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Lukasz Anforowicz435bcb582019-07-12 20:50:061988 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451989 request->common_params().initiator_origin;
creisce0ef3572017-01-26 17:53:081990 last_committed->AddOrUpdateFrameEntry(
1991 rfh->frame_tree_node(), params.item_sequence_number,
1992 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
arthursonzogni73fe3212020-11-17 13:24:071993 params.url, GetCommittedOriginForFrameEntry(params),
1994 Referrer(*params.referrer), initiator_origin, params.redirects,
1995 params.page_state, params.method, params.post_id,
1996 nullptr /* blob_url_loader_factory */,
Tsuyoshi Horo0c605782020-05-27 00:21:031997 request->web_bundle_navigation_info()
1998 ? request->web_bundle_navigation_info()->Clone()
Antonio Sartori78a749f2020-11-30 12:03:391999 : nullptr,
2000 ComputeDocumentPoliciesForFrameEntry(rfh, is_same_document, request));
creis625a0c7d2015-03-24 23:17:122001
creis913c63ce2016-07-16 19:52:522002 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:432003}
2004
[email protected]d202a7c2012-01-04 07:53:472005int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:232006 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:032007 for (size_t i = 0; i < entries_.size(); ++i) {
2008 if (entries_[i].get() == entry)
2009 return i;
2010 }
2011 return -1;
[email protected]765b35502008-08-21 00:51:202012}
2013
Eugene But7cc259d2017-10-09 23:52:492014// There are two general cases where a navigation is "same-document":
avidb7d1d22015-06-08 21:21:502015// 1. A fragment navigation, in which the url is kept the same except for the
2016// reference fragment.
2017// 2. A history API navigation (pushState and replaceState). This case is
Eugene But7cc259d2017-10-09 23:52:492018// always same-document, but the urls are not guaranteed to match excluding
2019// the fragment. The relevant spec allows pushState/replaceState to any URL
2020// on the same origin.
avidb7d1d22015-06-08 21:21:502021// However, due to reloads, even identical urls are *not* guaranteed to be
Eugene But7cc259d2017-10-09 23:52:492022// same-document navigations, we have to trust the renderer almost entirely.
avidb7d1d22015-06-08 21:21:502023// The one thing we do know is that cross-origin navigations will *never* be
Eugene But7cc259d2017-10-09 23:52:492024// same-document. Therefore, trust the renderer if the URLs are on the same
2025// origin, and assume the renderer is malicious if a cross-origin navigation
2026// claims to be same-document.
creisf164daa2016-06-07 00:17:052027//
2028// TODO(creis): Clean up and simplify the about:blank and origin checks below,
2029// which are likely redundant with each other. Be careful about data URLs vs
2030// about:blank, both of which are unique origins and thus not considered equal.
Eugene But7cc259d2017-10-09 23:52:492031bool NavigationControllerImpl::IsURLSameDocumentNavigation(
[email protected]b9d4dfdc2013-08-08 00:25:122032 const GURL& url,
creisf164daa2016-06-07 00:17:052033 const url::Origin& origin,
Eugene But7cc259d2017-10-09 23:52:492034 bool renderer_says_same_document,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572035 RenderFrameHost* rfh) {
creis225a7432016-06-03 22:56:272036 RenderFrameHostImpl* rfhi = static_cast<RenderFrameHostImpl*>(rfh);
avidb7d1d22015-06-08 21:21:502037 GURL last_committed_url;
2038 if (rfh->GetParent()) {
creis225a7432016-06-03 22:56:272039 // Use the FrameTreeNode's current_url and not rfh->GetLastCommittedURL(),
2040 // which might be empty in a new RenderFrameHost after a process swap.
2041 // Here, we care about the last committed URL in the FrameTreeNode,
2042 // regardless of which process it is in.
2043 last_committed_url = rfhi->frame_tree_node()->current_url();
avidb7d1d22015-06-08 21:21:502044 } else {
2045 NavigationEntry* last_committed = GetLastCommittedEntry();
2046 // There must be a last-committed entry to compare URLs to. TODO(avi): When
2047 // might Blink say that a navigation is in-page yet there be no last-
2048 // committed entry?
2049 if (!last_committed)
2050 return false;
2051 last_committed_url = last_committed->GetURL();
2052 }
2053
Gyuyoung Kim1ac4ca782020-09-11 03:32:512054 auto prefs = rfhi->GetOrCreateWebPreferences();
creis225a7432016-06-03 22:56:272055 const url::Origin& committed_origin =
2056 rfhi->frame_tree_node()->current_origin();
avidb7d1d22015-06-08 21:21:502057 bool is_same_origin = last_committed_url.is_empty() ||
2058 // TODO(japhet): We should only permit navigations
2059 // originating from about:blank to be in-page if the
2060 // about:blank is the first document that frame loaded.
2061 // We don't have sufficient information to identify
2062 // that case at the moment, so always allow about:blank
2063 // for now.
csharrisona3bd0b32016-10-19 18:40:482064 last_committed_url == url::kAboutBlankURL ||
avidb7d1d22015-06-08 21:21:502065 last_committed_url.GetOrigin() == url.GetOrigin() ||
creisf164daa2016-06-07 00:17:052066 committed_origin == origin ||
avidb7d1d22015-06-08 21:21:502067 !prefs.web_security_enabled ||
2068 (prefs.allow_universal_access_from_file_urls &&
creisdc004e02015-11-20 21:28:472069 committed_origin.scheme() == url::kFileScheme);
Eugene But7cc259d2017-10-09 23:52:492070 if (!is_same_origin && renderer_says_same_document) {
avidb7d1d22015-06-08 21:21:502071 bad_message::ReceivedBadMessage(rfh->GetProcess(),
2072 bad_message::NC_IN_PAGE_NAVIGATION);
2073 }
Eugene But7cc259d2017-10-09 23:52:492074 return is_same_origin && renderer_says_same_document;
[email protected]e9ba4472008-09-14 15:42:432075}
2076
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572077void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:242078 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572079 NavigationControllerImpl* source =
2080 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:572081 // Verify that we look new.
Lei Zhang96031532019-10-10 19:05:472082 DCHECK_EQ(0, GetEntryCount());
2083 DCHECK(!GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:572084
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572085 if (source->GetEntryCount() == 0)
[email protected]ce3fa3c2009-04-20 19:55:572086 return; // Nothing new to do.
2087
Francois Dorayeaace782017-06-21 16:37:242088 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:442089 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572090 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:572091
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572092 for (auto it = source->session_storage_namespace_map_.begin();
2093 it != source->session_storage_namespace_map_.end(); ++it) {
[email protected]fdac6ade2013-07-20 01:06:302094 SessionStorageNamespaceImpl* source_namespace =
2095 static_cast<SessionStorageNamespaceImpl*>(it->second.get());
2096 session_storage_namespace_map_[it->first] = source_namespace->Clone();
2097 }
[email protected]4e6419c2010-01-15 04:50:342098
Lukasz Anforowicz0de0f452020-12-02 19:57:152099 FinishRestore(source->last_committed_entry_index_, RestoreType::kRestored);
[email protected]ce3fa3c2009-04-20 19:55:572100}
2101
Aran Gilman37d11632019-10-08 23:07:152102void NavigationControllerImpl::CopyStateFromAndPrune(NavigationController* temp,
2103 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:162104 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012105 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:162106
[email protected]d202a7c2012-01-04 07:53:472107 NavigationControllerImpl* source =
2108 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:252109
avi2b177592014-12-10 02:08:022110 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:012111 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:252112
[email protected]474f8512013-05-31 22:31:162113 // We now have one entry, possibly with a new pending entry. Ensure that
2114 // adding the entries from source won't put us over the limit.
2115 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:572116 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:572117 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:252118
Carlos IL4dea8902020-05-26 15:14:292119 // Insert the entries from source. Ignore any pending entry, since it has not
2120 // committed in source.
[email protected]474f8512013-05-31 22:31:162121 int max_source_index = source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:252122 if (max_source_index == -1)
[email protected]a26023822011-12-29 00:23:552123 max_source_index = source->GetEntryCount();
[email protected]e1cd5452010-08-26 18:03:252124 else
2125 max_source_index++;
[email protected]e78a6852013-12-13 08:08:572126
2127 // Ignore the source's current entry if merging with replacement.
2128 // TODO(davidben): This should preserve entries forward of the current
2129 // too. http://crbug.com/317872
2130 if (replace_entry && max_source_index > 0)
2131 max_source_index--;
2132
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572133 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:252134
2135 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:552136 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:142137
avi2b177592014-12-10 02:08:022138 delegate_->SetHistoryOffsetAndLength(last_committed_entry_index_,
2139 GetEntryCount());
[email protected]e1cd5452010-08-26 18:03:252140}
2141
[email protected]79368982013-11-13 01:11:012142bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:162143 // If there is no last committed entry, we cannot prune. Even if there is a
2144 // pending entry, it may not commit, leaving this WebContents blank, despite
2145 // possibly giving it new entries via CopyStateFromAndPrune.
2146 if (last_committed_entry_index_ == -1)
2147 return false;
[email protected]9350602e2013-02-26 23:27:442148
[email protected]474f8512013-05-31 22:31:162149 // We cannot prune if there is a pending entry at an existing entry index.
2150 // It may not commit, so we have to keep the last committed entry, and thus
2151 // there is no sensible place to keep the pending entry. It is ok to have
2152 // a new pending entry, which can optionally commit as a new navigation.
2153 if (pending_entry_index_ != -1)
2154 return false;
2155
[email protected]474f8512013-05-31 22:31:162156 return true;
2157}
2158
[email protected]79368982013-11-13 01:11:012159void NavigationControllerImpl::PruneAllButLastCommitted() {
2160 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162161
avi2b177592014-12-10 02:08:022162 DCHECK_EQ(0, last_committed_entry_index_);
2163 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442164
avi2b177592014-12-10 02:08:022165 delegate_->SetHistoryOffsetAndLength(last_committed_entry_index_,
2166 GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442167}
2168
[email protected]79368982013-11-13 01:11:012169void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162170 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012171 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262172
[email protected]474f8512013-05-31 22:31:162173 // Erase all entries but the last committed entry. There may still be a
2174 // new pending entry after this.
2175 entries_.erase(entries_.begin(),
2176 entries_.begin() + last_committed_entry_index_);
2177 entries_.erase(entries_.begin() + 1, entries_.end());
2178 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262179}
2180
Christian Dullweber1af31e62018-02-22 11:49:482181void NavigationControllerImpl::DeleteNavigationEntries(
2182 const DeletionPredicate& deletionPredicate) {
2183 // It is up to callers to check the invariants before calling this.
2184 CHECK(CanPruneAllButLastCommitted());
2185 std::vector<int> delete_indices;
2186 for (size_t i = 0; i < entries_.size(); i++) {
2187 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572188 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482189 delete_indices.push_back(i);
2190 }
2191 }
2192 if (delete_indices.empty())
2193 return;
2194
2195 if (delete_indices.size() == GetEntryCount() - 1U) {
2196 PruneAllButLastCommitted();
2197 } else {
2198 // Do the deletion in reverse to preserve indices.
2199 for (auto it = delete_indices.rbegin(); it != delete_indices.rend(); ++it) {
2200 RemoveEntryAtIndex(*it);
2201 }
2202 delegate_->SetHistoryOffsetAndLength(last_committed_entry_index_,
2203 GetEntryCount());
2204 }
2205 delegate()->NotifyNavigationEntriesDeleted();
2206}
2207
Shivani Sharma883f5f32019-02-12 18:20:012208bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2209 auto* entry = GetEntryAtIndex(index);
2210 return entry && entry->should_skip_on_back_forward_ui();
2211}
2212
Carlos Caballero35ce710c2019-09-19 10:59:452213BackForwardCacheImpl& NavigationControllerImpl::GetBackForwardCache() {
2214 return back_forward_cache_;
2215}
2216
clamy987a3752018-05-03 17:36:262217void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2218 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2219 // progress, since this will cause a use-after-free. (We only allow this
2220 // when the tab is being destroyed for shutdown, since it won't return to
2221 // NavigateToEntry in that case.) http://crbug.com/347742.
2222 CHECK(!in_navigate_to_pending_entry_ || delegate_->IsBeingDestroyed());
2223
2224 if (was_failure && pending_entry_) {
2225 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2226 } else {
2227 failed_pending_entry_id_ = 0;
2228 }
2229
2230 if (pending_entry_) {
2231 if (pending_entry_index_ == -1)
2232 delete pending_entry_;
2233 pending_entry_index_ = -1;
2234 pending_entry_ = nullptr;
2235 }
arthursonzogni66f711c2019-10-08 14:40:362236
2237 // Ensure any refs to the current pending entry are ignored if they get
2238 // deleted, by clearing the set of known refs. All future pending entries will
2239 // only be affected by new refs.
2240 pending_entry_refs_.clear();
clamy987a3752018-05-03 17:36:262241}
2242
2243void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2244 if (pending_entry_)
2245 pending_entry_->set_ssl_error(error);
2246}
2247
Camille Lamy5193caa2018-10-12 11:59:422248#if defined(OS_ANDROID)
2249// static
2250bool NavigationControllerImpl::ValidateDataURLAsString(
2251 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2252 if (!data_url_as_string)
2253 return false;
2254
2255 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2256 return false;
2257
2258 // The number of characters that is enough for validating a data: URI.
2259 // From the GURL's POV, the only important part here is scheme, it doesn't
2260 // check the actual content. Thus we can take only the prefix of the url, to
2261 // avoid unneeded copying of a potentially long string.
2262 const size_t kDataUriPrefixMaxLen = 64;
2263 GURL data_url(
2264 std::string(data_url_as_string->front_as<char>(),
2265 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2266 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2267 return false;
2268
2269 return true;
2270}
2271#endif
2272
Shivani Sharma194877032019-03-07 17:52:472273void NavigationControllerImpl::NotifyUserActivation() {
2274 // When a user activation occurs, ensure that all adjacent entries for the
2275 // same document clear their skippable bit, so that the history manipulation
2276 // intervention does not apply to them.
Shivani Sharmaffb32b82019-04-09 16:58:472277 // TODO(crbug.com/949279) in case it becomes necessary for resetting based on
2278 // which frame created an entry and which frame has the user gesture.
Shivani Sharma194877032019-03-07 17:52:472279 auto* last_committed_entry = GetLastCommittedEntry();
2280 if (!last_committed_entry)
2281 return;
Shivani Sharma194877032019-03-07 17:52:472282
Shivani Sharmac4cc8922019-04-18 03:11:172283 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472284}
2285
clamy987a3752018-05-03 17:36:262286bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2287 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172288 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262289 NavigationEntryImpl* entry =
2290 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2291 if (!entry)
2292 return false;
2293
2294 FrameNavigationEntry* frame_entry =
2295 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2296 if (!frame_entry)
2297 return false;
2298
Camille Lamy5193caa2018-10-12 11:59:422299 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572300 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232301 ReloadType::NONE, false /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422302 true /* is_history_navigation_in_new_child */);
clamyea99ea12018-05-28 13:54:232303
2304 if (!request)
2305 return false;
2306
arthursonzognif046d4a2019-12-12 19:08:102307 request->SetNavigationClient(std::move(*navigation_client));
Arthur Hemery06173ce2019-05-29 12:11:412308
Lukasz Anforowicz9ee83c272020-12-01 20:14:052309 render_frame_host->frame_tree_node()->navigator().Navigate(std::move(request),
2310 ReloadType::NONE);
clamyea99ea12018-05-28 13:54:232311
2312 return true;
clamy987a3752018-05-03 17:36:262313}
2314
Tsuyoshi Horo52fd08e2020-07-07 07:03:452315bool NavigationControllerImpl::ReloadFrame(FrameTreeNode* frame_tree_node) {
2316 NavigationEntryImpl* entry = GetEntryAtIndex(GetCurrentEntryIndex());
2317 if (!entry)
2318 return false;
2319 FrameNavigationEntry* frame_entry = entry->GetFrameEntry(frame_tree_node);
2320 if (!frame_entry)
2321 return false;
John Abd-El-Malek5b669132020-07-14 01:04:142322 ReloadType reload_type = ReloadType::NORMAL;
2323 entry->set_reload_type(reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452324 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
John Abd-El-Malek5b669132020-07-14 01:04:142325 frame_tree_node, entry, frame_entry, reload_type,
Tsuyoshi Horo52fd08e2020-07-07 07:03:452326 false /* is_same_document_history_load */,
2327 false /* is_history_navigation_in_new_child */);
2328 if (!request)
2329 return false;
Lukasz Anforowicz9ee83c272020-12-01 20:14:052330 frame_tree_node->navigator().Navigate(std::move(request), reload_type);
Tsuyoshi Horo52fd08e2020-07-07 07:03:452331 return true;
2332}
2333
Dave Tapuska8bfd84c2019-03-26 20:47:162334void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2335 int offset,
2336 int sandbox_frame_tree_node_id) {
2337 if (!CanGoToOffset(offset))
2338 return;
2339 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id);
2340}
2341
clamy987a3752018-05-03 17:36:262342void NavigationControllerImpl::NavigateFromFrameProxy(
2343 RenderFrameHostImpl* render_frame_host,
2344 const GURL& url,
Antonio Sartori9a82f6f32020-12-14 09:22:452345 const base::UnguessableToken* initiator_frame_token,
2346 int initiator_process_id,
Lukasz Anforowicz63f3b9432019-05-30 05:42:582347 const base::Optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262348 bool is_renderer_initiated,
2349 SiteInstance* source_site_instance,
2350 const Referrer& referrer,
2351 ui::PageTransition page_transition,
2352 bool should_replace_current_entry,
Charlie Harrison8c113a32019-01-07 16:08:292353 NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262354 const std::string& method,
2355 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092356 const std::string& extra_headers,
John Delaney50425f82020-04-07 16:26:212357 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
Gyuyoung Kim38e63632021-01-14 13:11:392358 const base::Optional<blink::Impression>& impression) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582359 if (is_renderer_initiated)
2360 DCHECK(initiator_origin.has_value());
2361
clamy987a3752018-05-03 17:36:262362 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582363
Rakina Zata Amni66992a82020-06-24 03:57:522364 // Don't allow an entry replacement if there is no entry to replace.
2365 // http://crbug.com/457149
2366 if (GetEntryCount() == 0)
2367 should_replace_current_entry = false;
2368
clamy987a3752018-05-03 17:36:262369 // Create a NavigationEntry for the transfer, without making it the pending
2370 // entry. Subframe transfers should have a clone of the last committed entry
2371 // with a FrameNavigationEntry for the target frame. Main frame transfers
2372 // should have a new NavigationEntry.
2373 // TODO(creis): Make this unnecessary by creating (and validating) the params
2374 // directly, passing them to the destination RenderFrameHost. See
2375 // https://crbug.com/536906.
2376 std::unique_ptr<NavigationEntryImpl> entry;
2377 if (!node->IsMainFrame()) {
2378 // Subframe case: create FrameNavigationEntry.
2379 if (GetLastCommittedEntry()) {
2380 entry = GetLastCommittedEntry()->Clone();
2381 entry->set_extra_headers(extra_headers);
2382 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2383 // Renderer-initiated navigation that target a remote frame are currently
2384 // classified as browser-initiated when this one has already navigated.
2385 // See https://crbug.com/722251.
2386 } else {
2387 // If there's no last committed entry, create an entry for about:blank
2388 // with a subframe entry for our destination.
2389 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2390 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:062391 GURL(url::kAboutBlankURL), referrer, initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:102392 source_site_instance, page_transition, is_renderer_initiated,
2393 extra_headers, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:372394 nullptr /* 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 }
Antonio Sartori78a749f2020-11-30 12:03:392399 std::unique_ptr<PolicyContainerHost::DocumentPolicies> document_policies;
2400 if (GetLastCommittedEntry()) {
2401 FrameNavigationEntry* previous_frame_entry =
2402 GetLastCommittedEntry()->GetFrameEntry(node);
2403 if (previous_frame_entry && previous_frame_entry->document_policies()) {
2404 document_policies =
2405 std::make_unique<PolicyContainerHost::DocumentPolicies>(
2406 *previous_frame_entry->document_policies());
2407 }
2408 }
clamy987a3752018-05-03 17:36:262409 entry->AddOrUpdateFrameEntry(
2410 node, -1, -1, nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582411 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Lukasz Anforowicz435bcb582019-07-12 20:50:062412 base::nullopt /* commit_origin */, referrer, initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:202413 std::vector<GURL>(), blink::PageState(), method, -1,
Antonio Sartori78a749f2020-11-30 12:03:392414 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
2415 std::move(document_policies));
clamy987a3752018-05-03 17:36:262416 } else {
2417 // Main frame case.
2418 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz641234d52019-11-07 21:07:102419 url, referrer, initiator_origin, source_site_instance, page_transition,
2420 is_renderer_initiated, extra_headers, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:372421 blob_url_loader_factory, should_replace_current_entry,
2422 GetWebContents()));
2423 // CreateNavigationEntry() may have changed the transition type.
2424 page_transition = entry->GetTransitionType();
clamy987a3752018-05-03 17:36:262425 entry->root_node()->frame_entry->set_source_site_instance(
2426 static_cast<SiteInstanceImpl*>(source_site_instance));
2427 entry->root_node()->frame_entry->set_method(method);
2428 }
clamy987a3752018-05-03 17:36:262429
Camille Lamy5193caa2018-10-12 11:59:422430 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262431 if (GetLastCommittedEntry() &&
2432 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2433 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422434 override_user_agent = true;
clamy987a3752018-05-03 17:36:262435 }
2436 // TODO(creis): Set user gesture and intent received timestamp on Android.
2437
2438 // We may not have successfully added the FrameNavigationEntry to |entry|
2439 // above (per https://crbug.com/608402), in which case we create it from
2440 // scratch. This works because we do not depend on |frame_entry| being inside
2441 // |entry| during NavigateToEntry. This will go away when we shortcut this
2442 // further in https://crbug.com/536906.
2443 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2444 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452445 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
clamy987a3752018-05-03 17:36:262446 node->unique_name(), -1, -1, nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582447 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Lukasz Anforowicz435bcb582019-07-12 20:50:062448 nullptr /* origin */, referrer, initiator_origin, std::vector<GURL>(),
Miyoung Shin5d77f72072020-10-09 15:14:202449 blink::PageState(), method, -1, blob_url_loader_factory,
Antonio Sartori78a749f2020-11-30 12:03:392450 nullptr /* web_bundle_navigation_info */,
2451 nullptr /* document_policies */);
clamy987a3752018-05-03 17:36:262452 }
2453
Camille Lamy5193caa2018-10-12 11:59:422454 LoadURLParams params(url);
Antonio Sartori9a82f6f32020-12-14 09:22:452455 params.initiator_frame_token =
2456 initiator_frame_token ? base::make_optional(*initiator_frame_token)
2457 : base::nullopt;
2458 params.initiator_process_id = initiator_process_id;
Nasko Oskov93e7c55c2018-12-19 01:59:292459 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422460 params.source_site_instance = source_site_instance;
2461 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2462 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032463 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422464 params.referrer = referrer;
2465 /* params.redirect_chain: skip */
2466 params.extra_headers = extra_headers;
2467 params.is_renderer_initiated = is_renderer_initiated;
2468 params.override_user_agent = UA_OVERRIDE_INHERIT;
2469 /* params.base_url_for_data_url: skip */
2470 /* params.virtual_url_for_data_url: skip */
2471 /* params.data_url_as_string: skip */
2472 params.post_data = post_body;
2473 params.can_load_local_resources = false;
Kevin McNeee60e76b2019-11-27 20:01:582474 /* params.should_replace_current_entry: skip */
Camille Lamy5193caa2018-10-12 11:59:422475 /* params.frame_name: skip */
2476 // TODO(clamy): See if user gesture should be propagated to this function.
2477 params.has_user_gesture = false;
2478 params.should_clear_history_list = false;
2479 params.started_from_context_menu = false;
2480 /* params.navigation_ui_data: skip */
2481 /* params.input_start: skip */
Lucas Furukawa Gadania9c45682019-07-31 22:05:142482 params.was_activated = mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542483 /* params.reload_type: skip */
John Delaney50425f82020-04-07 16:26:212484 params.impression = impression;
Camille Lamy5193caa2018-10-12 11:59:422485
2486 std::unique_ptr<NavigationRequest> request =
2487 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032488 node, params, override_user_agent, should_replace_current_entry,
2489 false /* has_user_gesture */, download_policy, ReloadType::NONE,
Scott Violet5ae6c42e2020-10-28 02:47:372490 entry.get(), frame_entry.get(), params.transition_type);
clamyea99ea12018-05-28 13:54:232491
2492 if (!request)
2493 return;
2494
Arthur Hemery948742762019-09-18 10:06:242495 // At this stage we are proceeding with this navigation. If this was renderer
2496 // initiated with user gesture, we need to make sure we clear up potential
2497 // remains of a cancelled browser initiated navigation to avoid URL spoofs.
2498 DiscardNonCommittedEntries();
2499
Lukasz Anforowicz9ee83c272020-12-01 20:14:052500 node->navigator().Navigate(std::move(request), ReloadType::NONE);
clamy987a3752018-05-03 17:36:262501}
2502
[email protected]d1198fd2012-08-13 22:50:192503void NavigationControllerImpl::SetSessionStorageNamespace(
[email protected]fdac6ade2013-07-20 01:06:302504 const std::string& partition_id,
[email protected]8ff00d72012-10-23 19:12:212505 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192506 if (!session_storage_namespace)
2507 return;
2508
2509 // We can't overwrite an existing SessionStorage without violating spec.
2510 // Attempts to do so may give a tab access to another tab's session storage
2511 // so die hard on an error.
Aran Gilman37d11632019-10-08 23:07:152512 bool successful_insert =
2513 session_storage_namespace_map_
2514 .insert(
2515 make_pair(partition_id, static_cast<SessionStorageNamespaceImpl*>(
2516 session_storage_namespace)))
[email protected]fdac6ade2013-07-20 01:06:302517 .second;
2518 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
[email protected]d1198fd2012-08-13 22:50:192519}
2520
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572521bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
Aran Gilman37d11632019-10-08 23:07:152522 return IsInitialNavigation() && !GetLastCommittedEntry() &&
[email protected]b4c84012014-04-28 19:51:102523 !delegate_->HasAccessedInitialDocument();
[email protected]aa62afd2014-04-22 19:22:462524}
2525
Aran Gilman37d11632019-10-08 23:07:152526SessionStorageNamespace* NavigationControllerImpl::GetSessionStorageNamespace(
2527 SiteInstance* instance) {
[email protected]fdac6ade2013-07-20 01:06:302528 std::string partition_id;
2529 if (instance) {
2530 // TODO(ajwong): When GetDefaultSessionStorageNamespace() goes away, remove
danakjf26536bf2020-09-10 00:46:132531 // this if statement so |instance| must not be null.
Aran Gilman37d11632019-10-08 23:07:152532 partition_id = GetContentClient()->browser()->GetStoragePartitionIdForSite(
2533 browser_context_, instance->GetSiteURL());
[email protected]fdac6ade2013-07-20 01:06:302534 }
[email protected]d1198fd2012-08-13 22:50:192535
[email protected]fdac6ade2013-07-20 01:06:302536 // TODO(ajwong): Should this use the |partition_id| directly rather than
2537 // re-lookup via |instance|? http://crbug.com/142685
2538 StoragePartition* partition =
michaelnbacbcbd2016-02-09 00:32:032539 BrowserContext::GetStoragePartition(browser_context_, instance);
2540 DOMStorageContextWrapper* context_wrapper =
2541 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2542
2543 SessionStorageNamespaceMap::const_iterator it =
2544 session_storage_namespace_map_.find(partition_id);
2545 if (it != session_storage_namespace_map_.end()) {
2546 // Ensure that this namespace actually belongs to this partition.
Aran Gilman37d11632019-10-08 23:07:152547 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())
2548 ->IsFromContext(context_wrapper));
michaelnbacbcbd2016-02-09 00:32:032549 return it->second.get();
2550 }
2551
2552 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102553 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2554 SessionStorageNamespaceImpl::Create(context_wrapper);
2555 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2556 session_storage_namespace.get();
2557 session_storage_namespace_map_[partition_id] =
2558 std::move(session_storage_namespace);
[email protected]fdac6ade2013-07-20 01:06:302559
Daniel Murphy31bbb8b12018-02-07 21:44:102560 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302561}
2562
2563SessionStorageNamespace*
2564NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
2565 // TODO(ajwong): Remove if statement in GetSessionStorageNamespace().
Ivan Kotenkov2c0d2bb32017-11-01 15:41:282566 return GetSessionStorageNamespace(nullptr);
[email protected]fdac6ade2013-07-20 01:06:302567}
2568
2569const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572570NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302571 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552572}
[email protected]d202a7c2012-01-04 07:53:472573
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572574bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562575 return needs_reload_;
2576}
[email protected]a26023822011-12-29 00:23:552577
[email protected]46bb5e9c2013-10-03 22:16:472578void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412579 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2580}
2581
2582void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472583 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412584 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542585
2586 if (last_committed_entry_index_ != -1) {
2587 entries_[last_committed_entry_index_]->SetTransitionType(
2588 ui::PAGE_TRANSITION_RELOAD);
2589 }
[email protected]46bb5e9c2013-10-03 22:16:472590}
2591
[email protected]d202a7c2012-01-04 07:53:472592void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572593 DCHECK_LT(index, GetEntryCount());
2594 DCHECK_NE(index, last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:312595 DiscardNonCommittedEntries();
2596
2597 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122598 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312599 last_committed_entry_index_--;
2600}
2601
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572602NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272603 // If there is no pending_entry_, there should be no pending_entry_index_.
2604 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2605
2606 // If there is a pending_entry_index_, then pending_entry_ must be the entry
Carlos IL42b416592019-10-07 23:10:362607 // at that index. An exception is while a reload of a post commit error page
2608 // is ongoing; in that case pending entry will point to the entry replaced
2609 // by the error.
arthursonzogni5c4c202d2017-04-25 23:41:272610 DCHECK(pending_entry_index_ == -1 ||
Carlos IL42b416592019-10-07 23:10:362611 pending_entry_ == GetEntryAtIndex(pending_entry_index_) ||
2612 pending_entry_ == entry_replaced_by_post_commit_error_.get());
arthursonzogni5c4c202d2017-04-25 23:41:272613
[email protected]022af742011-12-28 18:37:252614 return pending_entry_;
2615}
2616
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572617int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272618 // The pending entry index must always be less than the number of entries.
2619 // If there are no entries, it must be exactly -1.
2620 DCHECK_LT(pending_entry_index_, GetEntryCount());
2621 DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:552622 return pending_entry_index_;
2623}
2624
avi25764702015-06-23 15:43:372625void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572626 std::unique_ptr<NavigationEntryImpl> entry,
Carlos IL42b416592019-10-07 23:10:362627 bool replace,
2628 bool was_post_commit_error) {
mastiz7eddb5f72016-06-23 09:52:452629 DCHECK(!ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2630 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202631
avi5cad4912015-06-19 05:25:442632 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2633 // need to keep continuity with the pending entry, so copy the pending entry's
2634 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2635 // then the renderer navigated on its own, independent of the pending entry,
2636 // so don't copy anything.
2637 if (pending_entry_ && pending_entry_index_ == -1)
2638 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202639
arthursonzogni69a6a1b2019-09-17 09:23:002640 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:202641
creisee17e932015-07-17 17:56:222642 // When replacing, don't prune the forward history.
Carlos IL42b416592019-10-07 23:10:362643 if ((replace || was_post_commit_error) && entries_.size() > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102644 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572645 entries_[last_committed_entry_index_].get(), entry.get());
Carlos IL42b416592019-10-07 23:10:362646 // If the new entry is a post-commit error page, we store the current last
2647 // committed entry to the side so that we can put it back when navigating
2648 // away from the error.
2649 if (was_post_commit_error) {
2650 DCHECK(!entry_replaced_by_post_commit_error_);
2651 entry_replaced_by_post_commit_error_ =
2652 std::move(entries_[last_committed_entry_index_]);
2653 }
dcheng36b6aec92015-12-26 06:16:362654 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222655 return;
2656 }
[email protected]765b35502008-08-21 00:51:202657
creis37979a62015-08-04 19:48:182658 // We shouldn't see replace == true when there's no committed entries.
2659 DCHECK(!replace);
2660
Michael Thiessen9b14d512019-09-23 21:19:472661 PruneForwardEntries();
[email protected]765b35502008-08-21 00:51:202662
Shivani Sharmad8c8d652019-02-13 17:27:572663 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202664
dcheng36b6aec92015-12-26 06:16:362665 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202666 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292667}
2668
Shivani Sharmad8c8d652019-02-13 17:27:572669void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162670 if (entries_.size() < max_entry_count())
2671 return;
2672
2673 DCHECK_EQ(max_entry_count(), entries_.size());
2674 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572675 CHECK_EQ(pending_entry_index_, -1);
2676
2677 int index = 0;
2678 if (base::FeatureList::IsEnabled(
2679 features::kHistoryManipulationIntervention)) {
2680 // Retrieve the oldest skippable entry.
2681 for (; index < GetEntryCount(); index++) {
2682 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2683 break;
2684 }
2685 }
2686
2687 // If there is no skippable entry or if it is the last committed entry then
2688 // fall back to pruning the oldest entry. It is not safe to prune the last
2689 // committed entry.
2690 if (index == GetEntryCount() || index == last_committed_entry_index_)
2691 index = 0;
2692
2693 bool should_succeed = RemoveEntryAtIndex(index);
2694 DCHECK_EQ(true, should_succeed);
2695
2696 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252697}
2698
clamy3cb9bea92018-07-10 12:42:022699void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162700 ReloadType reload_type,
2701 int sandboxed_source_frame_tree_node_id) {
Alexander Timin3a92df72019-09-20 11:59:502702 TRACE_EVENT0("navigation",
2703 "NavigationControllerImpl::NavigateToExistingPendingEntry");
arthursonzogni5c4c202d2017-04-25 23:41:272704 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022705 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
Carlos IL42b416592019-10-07 23:10:362706 if (pending_entry_index_ != -1) {
2707 // The pending entry may not be in entries_ if a post-commit error page is
2708 // showing.
2709 DCHECK(pending_entry_ == entries_[pending_entry_index_].get() ||
2710 pending_entry_ == entry_replaced_by_post_commit_error_.get());
2711 }
clamy3cb9bea92018-07-10 12:42:022712 DCHECK(!IsRendererDebugURL(pending_entry_->GetURL()));
Alex Moshchuk3a4e77a2020-05-29 21:32:572713 bool is_forced_reload = needs_reload_;
[email protected]72097fd02010-01-21 23:36:012714 needs_reload_ = false;
Arthur Sonzogni620cec62018-12-13 13:08:572715 FrameTreeNode* root = delegate_->GetFrameTree()->root();
2716 int nav_entry_id = pending_entry_->GetUniqueID();
2717
[email protected]83c2e232011-10-07 21:36:462718 // If we were navigating to a slow-to-commit page, and the user performs
2719 // a session history navigation to the last committed page, RenderViewHost
2720 // will force the throbber to start, but WebKit will essentially ignore the
2721 // navigation, and won't send a message to stop the throbber. To prevent this
2722 // from happening, we drop the navigation here and stop the slow-to-commit
2723 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022724 if (pending_entry_index_ == last_committed_entry_index_ &&
Lukasz Anforowicz6b75c0d2020-12-01 22:56:082725 !pending_entry_->IsRestored() &&
arthursonzogni5c4c202d2017-04-25 23:41:272726 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
[email protected]ec6c05f2013-10-23 18:41:572727 delegate_->Stop();
[email protected]6a13a6c2011-12-20 21:47:122728
[email protected]83c2e232011-10-07 21:36:462729 DiscardNonCommittedEntries();
2730 return;
2731 }
2732
creisce0ef3572017-01-26 17:53:082733 // Compare FrameNavigationEntries to see which frames in the tree need to be
2734 // navigated.
clamy3cb9bea92018-07-10 12:42:022735 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2736 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
Alex Moshchuk3a4e77a2020-05-29 21:32:572737 FindFramesToNavigate(root, reload_type, &same_document_loads,
2738 &different_document_loads);
creis4e2ecb72015-06-20 00:46:302739
2740 if (same_document_loads.empty() && different_document_loads.empty()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572741 // We were unable to match any frames to navigate. This can happen if a
2742 // history navigation targets a subframe that no longer exists
2743 // (https://crbug.com/705550). In this case, we need to update the current
2744 // history entry to the pending one but keep the main document loaded. We
2745 // also need to ensure that observers are informed about the updated
2746 // current history entry (e.g., for greying out back/forward buttons), and
2747 // that renderer processes update their history offsets. The easiest way
2748 // to do all that is to schedule a "redundant" same-document navigation in
2749 // the main frame.
2750 //
2751 // Note that we don't want to remove this history entry, as it might still
2752 // be valid later, since a frame that it's targeting may be recreated.
2753 //
2754 // TODO(alexmos, creis): This behavior isn't ideal, as the user would
2755 // need to repeat history navigations until finding the one that works.
2756 // Consider changing this behavior to keep looking for the first valid
2757 // history entry that finds frames to navigate.
clamy3cb9bea92018-07-10 12:42:022758 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422759 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572760 root, pending_entry_, pending_entry_->GetFrameEntry(root),
Alex Moshchuk3a4e77a2020-05-29 21:32:572761 ReloadType::NONE /* reload_type */,
2762 true /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422763 false /* is_history_navigation_in_new_child */);
clamy3cb9bea92018-07-10 12:42:022764 if (!navigation_request) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572765 // If this navigation cannot start, delete the pending NavigationEntry.
clamy3cb9bea92018-07-10 12:42:022766 DiscardPendingEntry(false);
2767 return;
2768 }
Alex Moshchuk3a4e77a2020-05-29 21:32:572769 same_document_loads.push_back(std::move(navigation_request));
2770
2771 // Sanity check that we never take this branch for any kinds of reloads,
2772 // as those should've queued a different-document load in the main frame.
2773 DCHECK(!is_forced_reload);
2774 DCHECK_EQ(reload_type, ReloadType::NONE);
creis4e2ecb72015-06-20 00:46:302775 }
2776
Dave Tapuska8bfd84c2019-03-26 20:47:162777 // If |sandboxed_source_frame_node_id| is valid, then track whether this
2778 // navigation affects any frame outside the frame's subtree.
2779 if (sandboxed_source_frame_tree_node_id !=
2780 FrameTreeNode::kFrameTreeNodeInvalidId) {
2781 bool navigates_inside_tree =
2782 DoesSandboxNavigationStayWithinSubtree(
2783 sandboxed_source_frame_tree_node_id, same_document_loads) &&
2784 DoesSandboxNavigationStayWithinSubtree(
2785 sandboxed_source_frame_tree_node_id, different_document_loads);
Dave Tapuska716ed3af2019-09-23 18:45:502786 // Count the navigations as web use counters so we can determine
Dave Tapuska8bfd84c2019-03-26 20:47:162787 // the number of pages that trigger this.
2788 FrameTreeNode* sandbox_source_frame_tree_node =
2789 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
2790 if (sandbox_source_frame_tree_node) {
2791 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
2792 sandbox_source_frame_tree_node->current_frame_host(),
2793 navigates_inside_tree
2794 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
2795 : blink::mojom::WebFeature::
2796 kSandboxBackForwardAffectsFramesOutsideSubtree);
2797 }
Dave Tapuska855c1e12019-08-23 20:45:522798
2799 // If the navigation occurred outside the tree discard it because
2800 // the sandboxed frame didn't have permission to navigate outside
2801 // its tree. If it is possible that the navigation is both inside and
2802 // outside the frame tree and we discard it entirely because we don't
2803 // want to end up in a history state that didn't exist before.
2804 if (base::FeatureList::IsEnabled(
2805 features::kHistoryPreventSandboxedNavigation) &&
2806 !navigates_inside_tree) {
2807 DiscardPendingEntry(false);
2808 return;
2809 }
Dave Tapuska8bfd84c2019-03-26 20:47:162810 }
2811
Carlos Caballero539a421c2020-07-06 10:25:572812 // BackForwardCache:
2813 // Navigate immediately if the document is in the BackForwardCache.
2814 if (back_forward_cache_.GetEntry(nav_entry_id)) {
2815 TRACE_EVENT0("navigation", "BackForwardCache_CreateNavigationRequest");
2816 DCHECK_EQ(reload_type, ReloadType::NONE);
2817 auto navigation_request = CreateNavigationRequestFromEntry(
2818 root, pending_entry_, pending_entry_->GetFrameEntry(root),
2819 ReloadType::NONE, false /* is_same_document_history_load */,
2820 false /* is_history_navigation_in_new_child */);
Lukasz Anforowicz9ee83c272020-12-01 20:14:052821 root->navigator().Navigate(std::move(navigation_request), ReloadType::NONE);
Carlos Caballero539a421c2020-07-06 10:25:572822
2823 return;
2824 }
2825
2826 // History navigation might try to reuse a specific BrowsingInstance, already
2827 // used by a page in the cache. To avoid having two different main frames that
2828 // live in the same BrowsingInstance, evict the all pages with this
2829 // BrowsingInstance from the cache.
2830 //
2831 // For example, take the following scenario:
2832 //
2833 // A1 = Some page on a.com
2834 // A2 = Some other page on a.com
2835 // B3 = An uncacheable page on b.com
2836 //
2837 // Then the following navigations occur:
2838 // A1->A2->B3->A1
2839 // On the navigation from B3 to A1, A2 will remain in the cache (B3 doesn't
2840 // take its place) and A1 will be created in the same BrowsingInstance (and
2841 // SiteInstance), as A2.
2842 //
2843 // If we didn't do anything, both A1 and A2 would remain alive in the same
2844 // BrowsingInstance/SiteInstance, which is unsupported by
2845 // RenderFrameHostManager::CommitPending(). To avoid this conundrum, we evict
2846 // A2 from the cache.
2847 if (pending_entry_->site_instance()) {
2848 back_forward_cache_.EvictFramesInRelatedSiteInstances(
2849 pending_entry_->site_instance());
2850 }
2851
clamy3cb9bea92018-07-10 12:42:022852 // This call does not support re-entrancy. See http://crbug.com/347742.
2853 CHECK(!in_navigate_to_pending_entry_);
2854 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:302855
arthursonzogni66f711c2019-10-08 14:40:362856 // It is not possible to delete the pending NavigationEntry while navigating
2857 // to it. Grab a reference to delay potential deletion until the end of this
2858 // function.
2859 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
2860
creis4e2ecb72015-06-20 00:46:302861 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:022862 for (auto& item : same_document_loads) {
2863 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052864 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302865 }
clamy3cb9bea92018-07-10 12:42:022866 for (auto& item : different_document_loads) {
2867 FrameTreeNode* frame = item->frame_tree_node();
Lukasz Anforowicz9ee83c272020-12-01 20:14:052868 frame->navigator().Navigate(std::move(item), reload_type);
creis4e2ecb72015-06-20 00:46:302869 }
clamy3cb9bea92018-07-10 12:42:022870
2871 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:302872}
2873
Alex Moshchuk3a4e77a2020-05-29 21:32:572874NavigationControllerImpl::HistoryNavigationAction
2875NavigationControllerImpl::DetermineActionForHistoryNavigation(
creis4e2ecb72015-06-20 00:46:302876 FrameTreeNode* frame,
Alex Moshchuk3a4e77a2020-05-29 21:32:572877 ReloadType reload_type) {
Sreeja Kamishetty0be2ac52020-05-28 01:47:462878 // Only active frames can navigate:
2879 // - If the frame is in pending deletion, the browser already committed to
2880 // destroying this RenderFrameHost. See https://crbug.com/930278.
2881 // - If the frame is in back-forward cache, it's not allowed to navigate as it
2882 // should remain frozen. Ignore the request and evict the document from
2883 // back-forward cache.
Alex Moshchuke65c39272020-06-03 17:55:372884 //
2885 // If the frame is inactive, there's no need to recurse into subframes, which
2886 // should all be inactive as well.
Sreeja Kamishetty0be2ac52020-05-28 01:47:462887 if (frame->current_frame_host()->IsInactiveAndDisallowReactivation())
Alex Moshchuke65c39272020-06-03 17:55:372888 return HistoryNavigationAction::kStopLooking;
arthursonzogni03f76152019-02-12 10:35:202889
Alex Moshchuk3a4e77a2020-05-29 21:32:572890 // If there's no last committed entry, there is no previous history entry to
2891 // compare against, so fall back to a different-document load. Note that we
2892 // should only reach this case for the root frame and not descend further
2893 // into subframes.
2894 if (!GetLastCommittedEntry()) {
2895 DCHECK(frame->IsMainFrame());
2896 return HistoryNavigationAction::kDifferentDocument;
2897 }
2898
2899 // Reloads should result in a different-document load. Note that reloads may
2900 // also happen via the |needs_reload_| mechanism where the reload_type is
2901 // NONE, so detect this by comparing whether we're going to the same
2902 // entry that we're currently on. Similarly to above, only main frames
2903 // should reach this. Note that subframes support reloads, but that's done
2904 // via a different path that doesn't involve FindFramesToNavigate (see
2905 // RenderFrameHost::Reload()).
2906 if (reload_type != ReloadType::NONE ||
2907 pending_entry_index_ == last_committed_entry_index_) {
2908 DCHECK(frame->IsMainFrame());
2909 return HistoryNavigationAction::kDifferentDocument;
2910 }
2911
Alex Moshchuk47d1a4bd2020-06-01 22:15:342912 // If there is no new FrameNavigationEntry for the frame, ignore the
2913 // load. For example, this may happen when going back to an entry before a
2914 // frame was created. Suppose we commit a same-document navigation that also
2915 // results in adding a new subframe somewhere in the tree. If we go back,
2916 // the new subframe will be missing a FrameNavigationEntry in the previous
2917 // NavigationEntry, but we shouldn't delete or change what's loaded in
2918 // it.
2919 //
Alex Moshchuke65c39272020-06-03 17:55:372920 // Note that in this case, there is no need to keep looking for navigations
2921 // in subframes, which would be missing FrameNavigationEntries as well.
2922 //
Alex Moshchuk47d1a4bd2020-06-01 22:15:342923 // It's important to check this before checking |old_item| below, since both
2924 // might be null, and in that case we still shouldn't change what's loaded in
2925 // this frame. Note that scheduling any loads assumes that |new_item| is
2926 // non-null. See https://crbug.com/1088354.
2927 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
2928 if (!new_item)
Alex Moshchuke65c39272020-06-03 17:55:372929 return HistoryNavigationAction::kStopLooking;
Alex Moshchuk47d1a4bd2020-06-01 22:15:342930
Alex Moshchuk3a4e77a2020-05-29 21:32:572931 // If there is no old FrameNavigationEntry, schedule a different-document
2932 // load.
2933 //
creis225a7432016-06-03 22:56:272934 // TODO(creis): Store the last committed FrameNavigationEntry to use here,
2935 // rather than assuming the NavigationEntry has up to date info on subframes.
Alex Moshchuk3a4e77a2020-05-29 21:32:572936 // Note that this may require sharing FrameNavigationEntries between
2937 // NavigationEntries (https://crbug.com/373041) as a prerequisite, since
2938 // otherwise the stored FrameNavigationEntry might get stale (e.g., after
2939 // subframe navigations, or in the case where we don't find any frames to
2940 // navigate and ignore a back/forward navigation while moving to a different
2941 // NavigationEntry, as in https://crbug.com/705550).
creis4e2ecb72015-06-20 00:46:302942 FrameNavigationEntry* old_item =
2943 GetLastCommittedEntry()->GetFrameEntry(frame);
Alex Moshchuk3a4e77a2020-05-29 21:32:572944 if (!old_item)
2945 return HistoryNavigationAction::kDifferentDocument;
2946
Alex Moshchuk3a4e77a2020-05-29 21:32:572947 // If the new item is not in the same SiteInstance, schedule a
2948 // different-document load. Newly restored items may not have a SiteInstance
2949 // yet, in which case it will be assigned on first commit.
2950 if (new_item->site_instance() &&
2951 new_item->site_instance() != old_item->site_instance())
2952 return HistoryNavigationAction::kDifferentDocument;
2953
2954 // Schedule a different-document load if the current RenderFrameHost is not
2955 // live. This case can happen for Ctrl+Back or after
2956 // a renderer crash.
2957 if (!frame->current_frame_host()->IsRenderFrameLive())
2958 return HistoryNavigationAction::kDifferentDocument;
2959
2960 if (new_item->item_sequence_number() != old_item->item_sequence_number()) {
creis54131692016-08-12 18:32:252961 // Same document loads happen if the previous item has the same document
danakjb952ef12021-01-14 19:58:492962 // sequence number but different item sequence number.
2963 if (new_item->document_sequence_number() ==
2964 old_item->document_sequence_number()) {
Alex Moshchuk3a4e77a2020-05-29 21:32:572965 return HistoryNavigationAction::kSameDocument;
danakjb952ef12021-01-14 19:58:492966 }
avib48cb312016-05-05 21:35:002967
Alex Moshchuk3a4e77a2020-05-29 21:32:572968 // Otherwise, if both item and document sequence numbers differ, this
2969 // should be a different document load.
2970 return HistoryNavigationAction::kDifferentDocument;
2971 }
2972
2973 // If the item sequence numbers match, there is no need to navigate this
Alex Moshchuke65c39272020-06-03 17:55:372974 // frame. Keep looking for navigations in this frame's children.
Alex Moshchuk3a4e77a2020-05-29 21:32:572975 DCHECK_EQ(new_item->document_sequence_number(),
2976 old_item->document_sequence_number());
Alex Moshchuke65c39272020-06-03 17:55:372977 return HistoryNavigationAction::kKeepLooking;
Alex Moshchuk3a4e77a2020-05-29 21:32:572978}
2979
2980void NavigationControllerImpl::FindFramesToNavigate(
2981 FrameTreeNode* frame,
2982 ReloadType reload_type,
2983 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
2984 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
2985 DCHECK(pending_entry_);
2986 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
2987
2988 auto action = DetermineActionForHistoryNavigation(frame, reload_type);
2989
2990 if (action == HistoryNavigationAction::kSameDocument) {
2991 std::unique_ptr<NavigationRequest> navigation_request =
2992 CreateNavigationRequestFromEntry(
2993 frame, pending_entry_, new_item, reload_type,
2994 true /* is_same_document_history_load */,
2995 false /* is_history_navigation_in_new_child */);
2996 if (navigation_request) {
2997 // Only add the request if was properly created. It's possible for the
2998 // creation to fail in certain cases, e.g. when the URL is invalid.
2999 same_document_loads->push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:303000 }
Lei Zhang96031532019-10-10 19:05:473001
Alex Moshchuk3a4e77a2020-05-29 21:32:573002 // TODO(avi, creis): This is a bug; we should not return here. Rather, we
3003 // should continue on and navigate all child frames which have also
3004 // changed. This bug is the cause of <https://crbug.com/542299>, which is
3005 // a NC_IN_PAGE_NAVIGATION renderer kill.
3006 //
3007 // However, this bug is a bandaid over a deeper and worse problem. Doing a
3008 // pushState immediately after loading a subframe is a race, one that no
3009 // web page author expects. If we fix this bug, many large websites break.
3010 // For example, see <https://crbug.com/598043> and the spec discussion at
3011 // <https://github.com/whatwg/html/issues/1191>.
3012 //
3013 // For now, we accept this bug, and hope to resolve the race in a
3014 // different way that will one day allow us to fix this.
3015 return;
3016 } else if (action == HistoryNavigationAction::kDifferentDocument) {
Lei Zhang96031532019-10-10 19:05:473017 std::unique_ptr<NavigationRequest> navigation_request =
3018 CreateNavigationRequestFromEntry(
3019 frame, pending_entry_, new_item, reload_type,
3020 false /* is_same_document_history_load */,
3021 false /* is_history_navigation_in_new_child */);
3022 if (navigation_request) {
3023 // Only add the request if was properly created. It's possible for the
3024 // creation to fail in certain cases, e.g. when the URL is invalid.
3025 different_document_loads->push_back(std::move(navigation_request));
3026 }
3027 // For a different document, the subframes will be destroyed, so there's
3028 // no need to consider them.
3029 return;
Alex Moshchuke65c39272020-06-03 17:55:373030 } else if (action == HistoryNavigationAction::kStopLooking) {
3031 return;
creis4e2ecb72015-06-20 00:46:303032 }
3033
3034 for (size_t i = 0; i < frame->child_count(); i++) {
clamy3cb9bea92018-07-10 12:42:023035 FindFramesToNavigate(frame->child_at(i), reload_type, same_document_loads,
creis4e2ecb72015-06-20 00:46:303036 different_document_loads);
3037 }
3038}
3039
clamy21718cc22018-06-13 13:34:243040void NavigationControllerImpl::NavigateWithoutEntry(
3041 const LoadURLParams& params) {
3042 // Find the appropriate FrameTreeNode.
3043 FrameTreeNode* node = nullptr;
3044 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
3045 !params.frame_name.empty()) {
3046 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
3047 ? delegate_->GetFrameTree()->FindByID(params.frame_tree_node_id)
3048 : delegate_->GetFrameTree()->FindByName(params.frame_name);
3049 }
3050
3051 // If no FrameTreeNode was specified, navigate the main frame.
3052 if (!node)
3053 node = delegate_->GetFrameTree()->root();
3054
Camille Lamy5193caa2018-10-12 11:59:423055 // Compute overrides to the LoadURLParams for |override_user_agent|,
3056 // |should_replace_current_entry| and |has_user_gesture| that will be used
3057 // both in the creation of the NavigationEntry and the NavigationRequest.
3058 // Ideally, the LoadURLParams themselves would be updated, but since they are
3059 // passed as a const reference, this is not possible.
3060 // TODO(clamy): When we only create a NavigationRequest, move this to
3061 // CreateNavigationRequestFromLoadURLParams.
3062 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
3063 GetLastCommittedEntry());
3064
3065 // Don't allow an entry replacement if there is no entry to replace.
3066 // http://crbug.com/457149
3067 bool should_replace_current_entry =
3068 params.should_replace_current_entry && entries_.size();
3069
Scott Violet5ae6c42e2020-10-28 02:47:373070 ui::PageTransition transition_type = params.transition_type;
3071
clamy21718cc22018-06-13 13:34:243072 // Javascript URLs should not create NavigationEntries. All other navigations
3073 // do, including navigations to chrome renderer debug URLs.
clamy21718cc22018-06-13 13:34:243074 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Scott Violet5ae6c42e2020-10-28 02:47:373075 std::unique_ptr<NavigationEntryImpl> entry =
3076 CreateNavigationEntryFromLoadParams(node, params, override_user_agent,
3077 should_replace_current_entry,
3078 params.has_user_gesture);
3079 // CreateNavigationEntryFromLoadParams() may modify the transition type.
3080 transition_type = entry->GetTransitionType();
clamy21718cc22018-06-13 13:34:243081 DiscardPendingEntry(false);
3082 SetPendingEntry(std::move(entry));
3083 }
3084
3085 // Renderer-debug URLs are sent to the renderer process immediately for
3086 // processing and don't need to create a NavigationRequest.
3087 // Note: this includes navigations to JavaScript URLs, which are considered
3088 // renderer-debug URLs.
3089 // Note: we intentionally leave the pending entry in place for renderer debug
3090 // URLs, unlike the cases below where we clear it if the navigation doesn't
3091 // proceed.
3092 if (IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:483093 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
3094 // check them explicitly. See bug 913334.
Aaron Colwelle1908d982020-06-26 22:08:153095 if (GetContentClient()->browser()->ShouldBlockRendererDebugURL(
Oleg Davydov2cc0167b2019-02-05 14:32:483096 params.url, browser_context_)) {
3097 DiscardPendingEntry(false);
3098 return;
3099 }
3100
clamy21718cc22018-06-13 13:34:243101 HandleRendererDebugURL(node, params.url);
3102 return;
3103 }
3104
Antonio Sartori78a749f2020-11-30 12:03:393105 DCHECK(pending_entry_);
3106
clamy21718cc22018-06-13 13:34:243107 // Convert navigations to the current URL to a reload.
3108 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
3109 // relying on the frame tree node id from LoadURLParams. Unfortunately,
3110 // DevTools sometimes issues navigations to main frames that they do not
3111 // expect to see treated as reload, and it only works because they pass a
3112 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
3113 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:543114 ReloadType reload_type = params.reload_type;
3115 if (reload_type == ReloadType::NONE &&
3116 ShouldTreatNavigationAsReload(
Fergal Daly766177d2020-07-07 07:54:043117 node, params.url, pending_entry_->GetVirtualURL(),
clamy21718cc22018-06-13 13:34:243118 params.base_url_for_data_url, params.transition_type,
clamy21718cc22018-06-13 13:34:243119 params.load_type ==
3120 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
3121 false /* is_reload */, false /* is_navigation_to_existing_entry */,
clamy21718cc22018-06-13 13:34:243122 GetLastCommittedEntry())) {
3123 reload_type = ReloadType::NORMAL;
Alexander Timinb70f67382020-12-10 00:03:473124 pending_entry_->set_reload_type(reload_type);
Antonio Sartori78a749f2020-11-30 12:03:393125
3126 // If this is a reload of an existing FrameNavigationEntry and we had a
3127 // policy container for it, then we should copy it into the pending entry,
3128 // so that it is copied to the navigation request in
3129 // CreateNavigationRequestFromLoadParams later.
3130 if (GetLastCommittedEntry()) {
3131 FrameNavigationEntry* previous_frame_entry =
3132 GetLastCommittedEntry()->GetFrameEntry(node);
3133 if (previous_frame_entry && previous_frame_entry->document_policies()) {
3134 pending_entry_->GetFrameEntry(node)->set_document_policies(
3135 std::make_unique<PolicyContainerHost::DocumentPolicies>(
3136 *previous_frame_entry->document_policies()));
3137 }
3138 }
3139 }
3140
3141 // If this navigation is an "Enter-in-omnibox" with the initial about:blank
3142 // document (so no last commit), then we should copy the document polices from
3143 // RenderFrameHost's PolicyContainerHost. The NavigationRequest will create a
3144 // new PolicyContainerHost with the document policies from the
3145 // |pending_entry_|, and that PolicyContainerHost will be put in the final
3146 // RenderFrameHost for the navigation. This way, we ensure that we keep
3147 // enforcing the right policies on the initial empty document after the
3148 // reload.
3149 if (!GetLastCommittedEntry() && params.url.IsAboutBlank()) {
3150 if (node->current_frame_host() &&
3151 node->current_frame_host()->policy_container_host()) {
3152 pending_entry_->GetFrameEntry(node)->set_document_policies(
3153 std::make_unique<PolicyContainerHost::DocumentPolicies>(
3154 node->current_frame_host()
3155 ->policy_container_host()
3156 ->document_policies()));
3157 }
clamy21718cc22018-06-13 13:34:243158 }
3159
3160 // navigation_ui_data should only be present for main frame navigations.
3161 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
3162
Camille Lamy5193caa2018-10-12 11:59:423163 std::unique_ptr<NavigationRequest> request =
3164 CreateNavigationRequestFromLoadParams(
3165 node, params, override_user_agent, should_replace_current_entry,
John Abd-El-Malekd95f34382020-10-06 04:56:463166 params.has_user_gesture, NavigationDownloadPolicy(), reload_type,
Scott Violet5ae6c42e2020-10-28 02:47:373167 pending_entry_, pending_entry_->GetFrameEntry(node), transition_type);
clamy21718cc22018-06-13 13:34:243168
3169 // If the navigation couldn't start, return immediately and discard the
3170 // pending NavigationEntry.
3171 if (!request) {
3172 DiscardPendingEntry(false);
3173 return;
3174 }
3175
Camille Lamy5193caa2018-10-12 11:59:423176#if DCHECK_IS_ON()
3177 // Safety check that NavigationRequest and NavigationEntry match.
3178 ValidateRequestMatchesEntry(request.get(), pending_entry_);
3179#endif
3180
clamy21718cc22018-06-13 13:34:243181 // This call does not support re-entrancy. See http://crbug.com/347742.
3182 CHECK(!in_navigate_to_pending_entry_);
3183 in_navigate_to_pending_entry_ = true;
3184
arthursonzogni66f711c2019-10-08 14:40:363185 // It is not possible to delete the pending NavigationEntry while navigating
3186 // to it. Grab a reference to delay potential deletion until the end of this
3187 // function.
3188 std::unique_ptr<PendingEntryRef> pending_entry_ref = ReferencePendingEntry();
3189
Lukasz Anforowicz9ee83c272020-12-01 20:14:053190 node->navigator().Navigate(std::move(request), reload_type);
clamy21718cc22018-06-13 13:34:243191
3192 in_navigate_to_pending_entry_ = false;
3193}
3194
clamyea99ea12018-05-28 13:54:233195void NavigationControllerImpl::HandleRendererDebugURL(
3196 FrameTreeNode* frame_tree_node,
3197 const GURL& url) {
3198 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:243199 // Any renderer-side debug URLs or javascript: URLs should be ignored if
3200 // the renderer process is not live, unless it is the initial navigation
3201 // of the tab.
clamyea99ea12018-05-28 13:54:233202 if (!IsInitialNavigation()) {
3203 DiscardNonCommittedEntries();
3204 return;
3205 }
Fergal Dalyecd3b0202020-06-25 01:57:373206 // The current frame is always a main frame. If IsInitialNavigation() is
3207 // true then there have been no navigations and any frames of this tab must
3208 // be in the same renderer process. If that has crashed then the only frame
3209 // that can be revived is the main frame.
3210 frame_tree_node->render_manager()
3211 ->InitializeMainRenderFrameForImmediateUse();
clamyea99ea12018-05-28 13:54:233212 }
3213 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
3214}
3215
clamy21718cc22018-06-13 13:34:243216std::unique_ptr<NavigationEntryImpl>
3217NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
3218 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:423219 const LoadURLParams& params,
3220 bool override_user_agent,
3221 bool should_replace_current_entry,
3222 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393223 // Browser initiated navigations might not have a blob_url_loader_factory set
3224 // in params even if the navigation is to a blob URL. If that happens, lookup
3225 // the correct url loader factory to use here.
3226 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:483227 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink8ffda442020-09-03 18:29:473228 // Resolve the blob URL in the storage partition associated with the target
3229 // frame. This is the storage partition the URL will be loaded in, and only
3230 // URLs that can be resolved by it should be able to access its data.
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393231 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
Marijn Kruisselbrink8ffda442020-09-03 18:29:473232 node->current_frame_host()->GetStoragePartition(), params.url);
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:393233 }
3234
clamy21718cc22018-06-13 13:34:243235 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:443236 // extra_headers in params are \n separated; navigation entries want \r\n.
3237 std::string extra_headers_crlf;
3238 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:243239
3240 // For subframes, create a pending entry with a corresponding frame entry.
3241 if (!node->IsMainFrame()) {
Tommy C. Li03eee77a2019-02-05 02:07:443242 if (GetLastCommittedEntry()) {
3243 // Create an identical NavigationEntry with a new FrameNavigationEntry for
3244 // the target subframe.
3245 entry = GetLastCommittedEntry()->Clone();
3246 } else {
3247 // If there's no last committed entry, create an entry for about:blank
3248 // with a subframe entry for our destination.
3249 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
3250 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063251 GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103252 params.source_site_instance.get(), params.transition_type,
3253 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:373254 blob_url_loader_factory, should_replace_current_entry,
3255 GetWebContents()));
Tommy C. Li03eee77a2019-02-05 02:07:443256 }
Nasko Oskov18006bc2018-12-06 02:53:583257
clamy21718cc22018-06-13 13:34:243258 entry->AddOrUpdateFrameEntry(
3259 node, -1, -1, nullptr,
3260 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Lukasz Anforowicz435bcb582019-07-12 20:50:063261 params.url, base::nullopt, params.referrer, params.initiator_origin,
Miyoung Shin5d77f72072020-10-09 15:14:203262 params.redirect_chain, blink::PageState(), "GET", -1,
Antonio Sartori78a749f2020-11-30 12:03:393263 blob_url_loader_factory, nullptr /* web_bundle_navigation_info */,
3264 // If in NavigateWithoutEntry we later determine that this navigation is
Charlie Reisc0f17d2d2021-01-12 18:52:493265 // a SAME_ENTRY conversion of a new navigation into a reload, we will
3266 // set the right document policies there.
Antonio Sartori78a749f2020-11-30 12:03:393267 nullptr /* document_policies */);
clamy21718cc22018-06-13 13:34:243268 } else {
3269 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:243270 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:063271 params.url, params.referrer, params.initiator_origin,
Lukasz Anforowicz641234d52019-11-07 21:07:103272 params.source_site_instance.get(), params.transition_type,
3273 params.is_renderer_initiated, extra_headers_crlf, browser_context_,
Scott Violet5ae6c42e2020-10-28 02:47:373274 blob_url_loader_factory, should_replace_current_entry,
3275 GetWebContents()));
clamy21718cc22018-06-13 13:34:243276 entry->set_source_site_instance(
3277 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
3278 entry->SetRedirectChain(params.redirect_chain);
3279 }
3280
3281 // Set the FTN ID (only used in non-site-per-process, for tests).
3282 entry->set_frame_tree_node_id(node->frame_tree_node_id());
clamy21718cc22018-06-13 13:34:243283 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:423284 entry->SetIsOverridingUserAgent(override_user_agent);
3285 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:543286 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:243287
clamy21718cc22018-06-13 13:34:243288 switch (params.load_type) {
3289 case LOAD_TYPE_DEFAULT:
3290 break;
3291 case LOAD_TYPE_HTTP_POST:
3292 entry->SetHasPostData(true);
3293 entry->SetPostData(params.post_data);
3294 break;
3295 case LOAD_TYPE_DATA:
3296 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
3297 entry->SetVirtualURL(params.virtual_url_for_data_url);
3298#if defined(OS_ANDROID)
3299 entry->SetDataURLAsString(params.data_url_as_string);
3300#endif
3301 entry->SetCanLoadLocalResources(params.can_load_local_resources);
3302 break;
clamy21718cc22018-06-13 13:34:243303 }
3304
3305 // TODO(clamy): NavigationEntry is meant for information that will be kept
3306 // after the navigation ended and therefore is not appropriate for
3307 // started_from_context_menu. Move started_from_context_menu to
3308 // NavigationUIData.
3309 entry->set_started_from_context_menu(params.started_from_context_menu);
3310
3311 return entry;
3312}
3313
clamyea99ea12018-05-28 13:54:233314std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:423315NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
3316 FrameTreeNode* node,
3317 const LoadURLParams& params,
3318 bool override_user_agent,
3319 bool should_replace_current_entry,
3320 bool has_user_gesture,
Charlie Harrison8c113a32019-01-07 16:08:293321 NavigationDownloadPolicy download_policy,
Camille Lamy5193caa2018-10-12 11:59:423322 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573323 NavigationEntryImpl* entry,
Scott Violet5ae6c42e2020-10-28 02:47:373324 FrameNavigationEntry* frame_entry,
3325 ui::PageTransition transition_type) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573326 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283327 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533328 // All renderer-initiated navigations must have an initiator_origin.
3329 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513330
Camille Lamy5193caa2018-10-12 11:59:423331 GURL url_to_load;
3332 GURL virtual_url;
Nasko Oskov03912102019-01-11 00:21:323333 base::Optional<url::Origin> origin_to_commit =
3334 frame_entry ? frame_entry->committed_origin() : base::nullopt;
3335
Camille Lamy2baa8022018-10-19 16:43:173336 // For main frames, rewrite the URL if necessary and compute the virtual URL
3337 // that should be shown in the address bar.
3338 if (node->IsMainFrame()) {
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423339 bool ignored_reverse_on_redirect = false;
Camille Lamy2baa8022018-10-19 16:43:173340 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423341 &virtual_url, &ignored_reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423342
Camille Lamy2baa8022018-10-19 16:43:173343 // For DATA loads, override the virtual URL.
3344 if (params.load_type == LOAD_TYPE_DATA)
3345 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423346
Camille Lamy2baa8022018-10-19 16:43:173347 if (virtual_url.is_empty())
3348 virtual_url = url_to_load;
3349
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573350 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283351
Aran Gilman249eb122019-12-02 23:32:463352 // This is a LOG and not a CHECK/DCHECK as URL rewrite has non-deterministic
3353 // behavior: it is possible for two calls to RewriteUrlForNavigation to
3354 // return different results, leading to a different URL in the
3355 // NavigationRequest and FrameEntry. This will be fixed once we remove the
3356 // pending NavigationEntry, as we'll only make one call to
3357 // RewriteUrlForNavigation.
3358 VLOG_IF(1, (url_to_load != frame_entry->url()))
3359 << "NavigationRequest and FrameEntry have different URLs: "
3360 << url_to_load << " vs " << frame_entry->url();
Camille Lamyb9ed3c52018-11-19 15:34:283361
Camille Lamy2baa8022018-10-19 16:43:173362 // TODO(clamy): In order to remove the pending NavigationEntry,
Lukasz Anforowiczb2eb19b12020-01-25 00:40:423363 // |virtual_url| and |ignored_reverse_on_redirect| should be stored in the
Camille Lamy2baa8022018-10-19 16:43:173364 // NavigationRequest.
3365 } else {
3366 url_to_load = params.url;
3367 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243368 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173369 }
Camille Lamy5193caa2018-10-12 11:59:423370
Ehsan Karamad44fc72112019-02-26 18:15:473371 if (node->render_manager()->is_attaching_inner_delegate()) {
3372 // Avoid starting any new navigations since this node is now preparing for
3373 // attaching an inner delegate.
3374 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203375 }
Camille Lamy5193caa2018-10-12 11:59:423376
Camille Lamy5193caa2018-10-12 11:59:423377 if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load))
3378 return nullptr;
3379
Nasko Oskov03912102019-01-11 00:21:323380 if (!DoesURLMatchOriginForNavigation(url_to_load, origin_to_commit)) {
3381 DCHECK(false) << " url:" << url_to_load
3382 << " origin:" << origin_to_commit.value();
3383 return nullptr;
3384 }
3385
Camille Lamy5193caa2018-10-12 11:59:423386 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403387 blink::PreviewsState previews_state =
3388 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
Camille Lamy5193caa2018-10-12 11:59:423389 if (!node->IsMainFrame()) {
3390 // For subframes, use the state of the top-level frame.
3391 previews_state = node->frame_tree()
3392 ->root()
3393 ->current_frame_host()
3394 ->last_navigation_previews_state();
3395 }
3396
Camille Lamy5193caa2018-10-12 11:59:423397 // This will be used to set the Navigation Timing API navigationStart
3398 // parameter for browser navigations in new tabs (intents, tabs opened through
3399 // "Open link in new tab"). If the navigation must wait on the current
3400 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3401 // will be updated when the BeforeUnload ack is received.
3402 base::TimeTicks navigation_start = base::TimeTicks::Now();
3403
danakjd83d706d2020-11-25 22:11:123404 // Look for a pending commit that is to another document in this
3405 // FrameTreeNode. If one exists, then the last committed URL will not be the
3406 // current URL by the time this navigation commits.
3407 bool has_pending_cross_document_commit =
3408 node->render_manager()->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493409 bool is_currently_error_page = node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123410
danakjb952ef12021-01-14 19:58:493411 mojom::NavigationType navigation_type = GetNavigationType(
3412 /*old_url=*/node->current_url(),
3413 /*new_url=*/url_to_load, reload_type, entry, *frame_entry,
3414 has_pending_cross_document_commit, is_currently_error_page,
3415 /*is_same_document_history_load=*/false);
Camille Lamy5193caa2018-10-12 11:59:423416
3417 // Create the NavigationParams based on |params|.
3418
3419 bool is_view_source_mode = virtual_url.SchemeIs(kViewSourceScheme);
Charlie Harrison8c113a32019-01-07 16:08:293420
3421 // Update |download_policy| if the virtual URL is view-source. Why do this
3422 // now? Possibly the URL could be rewritten to a view-source via some URL
3423 // handler.
3424 if (is_view_source_mode)
Yao Xiaoaf79ca9b2019-03-12 19:54:183425 download_policy.SetDisallowed(NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293426
Camille Lamy5193caa2018-10-12 11:59:423427 const GURL& history_url_for_data_url =
3428 params.base_url_for_data_url.is_empty() ? GURL() : virtual_url;
Scott Violet5ae6c42e2020-10-28 02:47:373429 // Don't use |params.transition_type| as calling code may supply a different
3430 // value.
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513431 mojom::CommonNavigationParamsPtr common_params =
3432 mojom::CommonNavigationParams::New(
3433 url_to_load, params.initiator_origin,
3434 blink::mojom::Referrer::New(params.referrer.url,
3435 params.referrer.policy),
Scott Violet5ae6c42e2020-10-28 02:47:373436 transition_type, navigation_type, download_policy,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513437 should_replace_current_entry, params.base_url_for_data_url,
3438 history_url_for_data_url, previews_state, navigation_start,
3439 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
arthursonzogniaf7c62c52020-02-12 10:49:413440 params.post_data, network::mojom::SourceLocation::New(),
3441 params.started_from_context_menu, has_user_gesture,
David Bokan71e81512020-08-18 17:19:463442 false /* has_text_fragment_token */, CreateInitiatorCSPInfo(),
3443 std::vector<int>(), params.href_translate,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513444 false /* is_history_navigation_in_new_child_frame */,
Charlie Hu5ffc0152019-12-06 15:59:533445 params.input_start);
Camille Lamy5193caa2018-10-12 11:59:423446
Lucas Furukawa Gadania9c45682019-07-31 22:05:143447 mojom::CommitNavigationParamsPtr commit_params =
3448 mojom::CommitNavigationParams::New(
3449 frame_entry->committed_origin(), override_user_agent,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323450 params.redirect_chain,
3451 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143452 std::vector<net::RedirectInfo>(),
3453 std::string() /* post_content_type */, common_params->url,
3454 common_params->method, params.can_load_local_resources,
3455 frame_entry->page_state(), entry->GetUniqueID(),
3456 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3457 -1 /* pending_history_list_offset */,
3458 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3459 params.should_clear_history_list ? 0 : GetEntryCount(),
3460 false /* was_discarded */, is_view_source_mode,
3461 params.should_clear_history_list, mojom::NavigationTiming::New(),
3462 base::nullopt /* appcache_host_id */,
3463 mojom::WasActivatedOption::kUnknown,
3464 base::UnguessableToken::Create() /* navigation_token */,
Lucas Furukawa Gadani97ea6382019-08-07 19:14:513465 std::vector<mojom::PrefetchedSignedExchangeInfoPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143466#if defined(OS_ANDROID)
3467 std::string(), /* data_url_as_string */
3468#endif
arthursonzogni14379782020-05-15 09:09:273469 !params.is_renderer_initiated, /* is_browser_initiated */
Tsuyoshi Horoa78f0562019-09-26 06:40:413470 network::mojom::IPAddressSpace::kUnknown,
Tsuyoshi Horoe86d7702019-11-29 01:52:473471 GURL() /* web_bundle_physical_url */,
Charlie Hu5ffc0152019-12-06 15:59:533472 GURL() /* base_url_override_for_web_bundle */,
Yue Ru Sun128804932020-09-30 22:19:173473 ukm::kInvalidSourceId /* document_ukm_source_id */,
Jiewei Qian0406fc02020-03-09 06:02:073474 node->pending_frame_policy(),
Domenic Denicola4778c35392020-06-25 21:25:163475 std::vector<std::string>() /* force_enabled_origin_trials */,
Domenic Denicola55701ee2021-01-14 00:18:333476 false /* origin_agent_cluster */,
Maks Orlovichc66745a2020-06-30 17:40:023477 std::vector<
Shuran Huanga055ce72020-07-23 14:13:213478 network::mojom::WebClientHintsType>() /* enabled_client_hints */,
Antonio Sartori1e09fd7b2020-08-10 14:16:173479 false /* is_cross_browsing_instance */,
Rakina Zata Amni60858132020-08-19 10:33:483480 std::vector<std::string>() /* forced_content_security_policies */,
Rakina Zata Amniba3eecb2020-11-02 10:12:283481 nullptr /* old_page_info */, -1 /* http_response_code */);
Camille Lamy5193caa2018-10-12 11:59:423482#if defined(OS_ANDROID)
3483 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143484 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423485 }
3486#endif
3487
Lucas Furukawa Gadania9c45682019-07-31 22:05:143488 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423489
3490 // A form submission may happen here if the navigation is a renderer-initiated
3491 // form submission that took the OpenURL path.
3492 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3493
3494 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3495 std::string extra_headers_crlf;
3496 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093497
3498 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143499 node, std::move(common_params), std::move(commit_params),
Lingqi Chi82efa95e2020-12-29 05:31:193500 !params.is_renderer_initiated, params.is_prerendering,
John Delaney8623c642021-01-06 17:37:073501 params.was_opener_suppressed,
Antonio Sartori9a82f6f32020-12-14 09:22:453502 params.initiator_frame_token.has_value()
3503 ? &(params.initiator_frame_token.value())
3504 : nullptr,
3505 params.initiator_process_id, extra_headers_crlf, frame_entry, entry,
3506 request_body,
John Delaney50425f82020-04-07 16:26:213507 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr,
3508 params.impression);
Yao Xiaodc5ed102019-06-04 19:19:093509 navigation_request->set_from_download_cross_origin_redirect(
3510 params.from_download_cross_origin_redirect);
3511 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423512}
3513
3514std::unique_ptr<NavigationRequest>
3515NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233516 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573517 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233518 FrameNavigationEntry* frame_entry,
3519 ReloadType reload_type,
3520 bool is_same_document_history_load,
Arthur Hemerybee4a752019-05-29 10:50:553521 bool is_history_navigation_in_new_child_frame) {
Alex Moshchuk47d1a4bd2020-06-01 22:15:343522 DCHECK(frame_entry);
clamyea99ea12018-05-28 13:54:233523 GURL dest_url = frame_entry->url();
Nasko Oskov03912102019-01-11 00:21:323524 base::Optional<url::Origin> origin_to_commit =
3525 frame_entry->committed_origin();
3526
clamyea99ea12018-05-28 13:54:233527 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013528 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573529 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233530 // We may have been redirected when navigating to the current URL.
3531 // Use the URL the user originally intended to visit as signaled by the
3532 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013533 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573534 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233535 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323536 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233537 }
3538
Ehsan Karamad44fc72112019-02-26 18:15:473539 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3540 // Avoid starting any new navigations since this node is now preparing for
3541 // attaching an inner delegate.
3542 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203543 }
3544
Camille Lamy5193caa2018-10-12 11:59:423545 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573546 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233547 return nullptr;
3548 }
3549
Nasko Oskov03912102019-01-11 00:21:323550 if (!DoesURLMatchOriginForNavigation(dest_url, origin_to_commit)) {
3551 DCHECK(false) << " url:" << dest_url
3552 << " origin:" << origin_to_commit.value();
3553 return nullptr;
3554 }
3555
clamyea99ea12018-05-28 13:54:233556 // Determine if Previews should be used for the navigation.
Hiroki Nakagawa41366152020-07-27 18:31:403557 blink::PreviewsState previews_state =
3558 blink::PreviewsTypes::PREVIEWS_UNSPECIFIED;
clamyea99ea12018-05-28 13:54:233559 if (!frame_tree_node->IsMainFrame()) {
3560 // For subframes, use the state of the top-level frame.
3561 previews_state = frame_tree_node->frame_tree()
3562 ->root()
3563 ->current_frame_host()
3564 ->last_navigation_previews_state();
clamyea99ea12018-05-28 13:54:233565 }
3566
clamyea99ea12018-05-28 13:54:233567 // This will be used to set the Navigation Timing API navigationStart
3568 // parameter for browser navigations in new tabs (intents, tabs opened through
3569 // "Open link in new tab"). If the navigation must wait on the current
3570 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3571 // will be updated when the BeforeUnload ack is received.
3572 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233573
danakjd83d706d2020-11-25 22:11:123574 // Look for a pending commit that is to another document in this
3575 // FrameTreeNode. If one exists, then the last committed URL will not be the
3576 // current URL by the time this navigation commits.
3577 bool has_pending_cross_document_commit =
3578 frame_tree_node->render_manager()
3579 ->HasPendingCommitForCrossDocumentNavigation();
danakjb952ef12021-01-14 19:58:493580 bool is_currently_error_page =
3581 frame_tree_node->current_frame_host()->is_error_page();
danakjd83d706d2020-11-25 22:11:123582
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513583 mojom::NavigationType navigation_type = GetNavigationType(
danakjd83d706d2020-11-25 22:11:123584 /*old_url=*/frame_tree_node->current_url(),
3585 /*new_url=*/dest_url, reload_type, entry, *frame_entry,
danakjb952ef12021-01-14 19:58:493586 has_pending_cross_document_commit, is_currently_error_page,
3587 is_same_document_history_load);
Camille Lamy5193caa2018-10-12 11:59:423588
3589 // A form submission may happen here if the navigation is a
3590 // back/forward/reload navigation that does a form resubmission.
3591 scoped_refptr<network::ResourceRequestBody> request_body;
3592 std::string post_content_type;
3593 if (frame_entry->method() == "POST") {
3594 request_body = frame_entry->GetPostData(&post_content_type);
3595 // Might have a LF at end.
3596 post_content_type =
3597 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL)
3598 .as_string();
3599 }
3600
3601 // Create the NavigationParams based on |entry| and |frame_entry|.
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513602 mojom::CommonNavigationParamsPtr common_params =
3603 entry->ConstructCommonNavigationParams(
3604 *frame_entry, request_body, dest_url,
3605 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
3606 navigation_type, previews_state, navigation_start,
Charlie Hu5ffc0152019-12-06 15:59:533607 base::TimeTicks() /* input_start */);
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513608 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553609 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423610
3611 // TODO(clamy): |intended_as_new_entry| below should always be false once
3612 // Reload no longer leads to this being called for a pending NavigationEntry
3613 // of index -1.
Lucas Furukawa Gadania9c45682019-07-31 22:05:143614 mojom::CommitNavigationParamsPtr commit_params =
3615 entry->ConstructCommitNavigationParams(
3616 *frame_entry, common_params->url, origin_to_commit,
3617 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3618 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
Charlie Hu5ffc0152019-12-06 15:59:533619 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(), GetEntryCount(),
3620 frame_tree_node->pending_frame_policy());
Lucas Furukawa Gadania9c45682019-07-31 22:05:143621 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423622
clamyea99ea12018-05-28 13:54:233623 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143624 frame_tree_node, std::move(common_params), std::move(commit_params),
Lingqi Chi82efa95e2020-12-29 05:31:193625 !entry->is_renderer_initiated(), false /* is_prerendering */,
John Delaney8623c642021-01-06 17:37:073626 false /* was_opener_suppressed */, nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453627 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
3628 entry->extra_headers(), frame_entry, entry, request_body,
3629 nullptr /* navigation_ui_data */, base::nullopt /* impression */);
clamyea99ea12018-05-28 13:54:233630}
3631
[email protected]d202a7c2012-01-04 07:53:473632void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213633 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273634 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403635
[email protected]2db9bd72012-04-13 20:20:563636 // We need to notify the ssl_manager_ before the web_contents_ so the
[email protected]df1af242009-05-01 00:11:403637 // location bar will have up-to-date information about the security style
3638 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133639 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403640
[email protected]7f924832014-08-09 05:57:223641 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:573642 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463643
[email protected]b0f724c2013-09-05 04:21:133644 // TODO(avi): Remove. http://crbug.com/170921
3645 NotificationDetails notification_details =
3646 Details<LoadCommittedDetails>(details);
Aran Gilman37d11632019-10-08 23:07:153647 NotificationService::current()->Notify(NOTIFICATION_NAV_ENTRY_COMMITTED,
3648 Source<NavigationController>(this),
3649 notification_details);
initial.commit09911bf2008-07-26 23:55:293650}
3651
initial.commit09911bf2008-07-26 23:55:293652// static
[email protected]d202a7c2012-01-04 07:53:473653size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233654 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
Aran Gilman37d11632019-10-08 23:07:153655 return max_entry_count_for_testing_;
[email protected]8ff00d72012-10-23 19:12:213656 return kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233657}
3658
[email protected]d202a7c2012-01-04 07:53:473659void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223660 if (is_active && needs_reload_)
3661 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293662}
3663
[email protected]d202a7c2012-01-04 07:53:473664void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293665 if (!needs_reload_)
3666 return;
3667
Bo Liucdfa4b12018-11-06 00:21:443668 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3669 needs_reload_type_);
3670
initial.commit09911bf2008-07-26 23:55:293671 // Calling Reload() results in ignoring state, and not loading.
3672 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3673 // cached state.
avicc872d7242015-08-19 21:26:343674 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163675 NavigateToExistingPendingEntry(ReloadType::NONE,
3676 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343677 } else if (last_committed_entry_index_ != -1) {
arthursonzogni5c4c202d2017-04-25 23:41:273678 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343679 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163680 NavigateToExistingPendingEntry(ReloadType::NONE,
3681 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343682 } else {
3683 // If there is something to reload, the successful reload will clear the
3684 // |needs_reload_| flag. Otherwise, just do it here.
3685 needs_reload_ = false;
3686 }
initial.commit09911bf2008-07-26 23:55:293687}
3688
Carlos IL42b416592019-10-07 23:10:363689void NavigationControllerImpl::LoadPostCommitErrorPage(
3690 RenderFrameHost* render_frame_host,
3691 const GURL& url,
3692 const std::string& error_page_html,
3693 net::Error error) {
Sreeja Kamishetty0be2ac52020-05-28 01:47:463694 // Only active frames can load post-commit error pages:
3695 // - If the frame is in pending deletion, the browser already committed to
3696 // destroying this RenderFrameHost so ignore loading the error page.
3697 // - If the frame is in back-forward cache, it's not allowed to navigate as it
3698 // should remain frozen. Ignore the request and evict the document from
3699 // back-forward cache.
3700 if (static_cast<RenderFrameHostImpl*>(render_frame_host)
3701 ->IsInactiveAndDisallowReactivation()) {
John Delaney56f73332019-11-04 19:39:253702 return;
Sreeja Kamishetty0be2ac52020-05-28 01:47:463703 }
Rakina Zata Amni919b7922020-12-11 09:03:133704 RenderFrameHostImpl* rfhi =
3705 static_cast<RenderFrameHostImpl*>(render_frame_host);
3706 FrameTreeNode* node = rfhi->frame_tree_node();
John Delaney131ad362019-08-08 21:57:413707
3708 mojom::CommonNavigationParamsPtr common_params =
3709 CreateCommonNavigationParams();
Rakina Zata Amnid2da1542020-12-23 00:52:593710 // |url| might be empty, such as when LoadPostCommitErrorPage happens before
3711 // the frame actually committed (e.g. iframe with "src" set to a
3712 // slow-responding URL). We should rewrite the URL to about:blank in this
3713 // case, as the renderer will only think a page is an error page if it has a
3714 // non-empty unreachable URL.
Rakina Zata Amni919b7922020-12-11 09:03:133715 common_params->url = url.is_empty() ? GURL("about:blank") : url;
John Delaney131ad362019-08-08 21:57:413716 mojom::CommitNavigationParamsPtr commit_params =
3717 CreateCommitNavigationParams();
3718
arthursonzogni70ac7302020-05-28 08:49:053719 // Error pages have a fully permissive FramePolicy.
3720 // TODO(arthursonzogni): Consider providing the minimal capabilities to the
3721 // error pages.
3722 commit_params->frame_policy = blink::FramePolicy();
3723
John Delaney131ad362019-08-08 21:57:413724 std::unique_ptr<NavigationRequest> navigation_request =
3725 NavigationRequest::CreateBrowserInitiated(
3726 node, std::move(common_params), std::move(commit_params),
Lingqi Chi82efa95e2020-12-29 05:31:193727 true /* browser_initiated */, false /* is_prerendering */,
John Delaney8623c642021-01-06 17:37:073728 false /* was_opener_suppressed */,
Lingqi Chi82efa95e2020-12-29 05:31:193729 nullptr /* initiator_frame_token */,
Antonio Sartori9a82f6f32020-12-14 09:22:453730 ChildProcessHost::kInvalidUniqueID /* initiator_process_id */,
John Delaneyf43556d2020-05-04 23:19:063731 "" /* extra_headers */, nullptr /* frame_entry */,
3732 nullptr /* entry */, nullptr /* post_body */,
3733 nullptr /* navigation_ui_data */, base::nullopt /* impression */);
Carlos IL42b416592019-10-07 23:10:363734 navigation_request->set_post_commit_error_page_html(error_page_html);
John Delaney131ad362019-08-08 21:57:413735 navigation_request->set_net_error(error);
3736 node->CreatedNavigationRequest(std::move(navigation_request));
3737 DCHECK(node->navigation_request());
3738 node->navigation_request()->BeginNavigation();
3739}
3740
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573741void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213742 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093743 det.changed_entry = entry;
Aran Gilman37d11632019-10-08 23:07:153744 det.index = GetIndexOfEntry(NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143745 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293746}
3747
[email protected]d202a7c2012-01-04 07:53:473748void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363749 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553750 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363751 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293752
initial.commit09911bf2008-07-26 23:55:293753 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293754}
[email protected]765b35502008-08-21 00:51:203755
arthursonzogni69a6a1b2019-09-17 09:23:003756void NavigationControllerImpl::DiscardNonCommittedEntries() {
Michael Thiessen9b14d512019-09-23 21:19:473757 // Avoid sending a notification if there is nothing to discard.
Michael Thiessenc5676d22019-09-25 22:32:103758 // TODO(mthiesse): Temporarily checking failed_pending_entry_id_ to help
3759 // diagnose https://bugs.chromium.org/p/chromium/issues/detail?id=1007570.
Carlos IL4dea8902020-05-26 15:14:293760 if (!pending_entry_ && failed_pending_entry_id_ == 0) {
Michael Thiessen9b14d512019-09-23 21:19:473761 return;
Michael Thiessenc5676d22019-09-25 22:32:103762 }
avi45a72532015-04-07 21:01:453763 DiscardPendingEntry(false);
arthursonzogni69a6a1b2019-09-17 09:23:003764 if (delegate_)
3765 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:483766}
3767
avi7c6f35e2015-05-08 17:52:383768int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3769 int nav_entry_id) const {
3770 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3771 if (entries_[i]->GetUniqueID() == nav_entry_id)
3772 return i;
3773 }
3774 return -1;
3775}
3776
[email protected]d202a7c2012-01-04 07:53:473777void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573778 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253779 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573780 DCHECK_LE(max_index, source->GetEntryCount());
[email protected]e1cd5452010-08-26 18:03:253781 for (int i = 0; i < max_index; i++) {
Nasko Oskov923ca482020-12-04 21:20:463782 // TODO(creis): Once we start sharing FrameNavigationEntries between
3783 // NavigationEntries, it will not be safe to share them with another tab.
3784 // Must have a version of Clone that recreates them.
3785 entries_.insert(entries_.begin() + i, source->entries_[i]->Clone());
[email protected]e1cd5452010-08-26 18:03:253786 }
arthursonzogni5c4c202d2017-04-25 23:41:273787 DCHECK(pending_entry_index_ == -1 ||
3788 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:253789}
[email protected]c5b88d82012-10-06 17:03:333790
3791void NavigationControllerImpl::SetGetTimestampCallbackForTest(
Makoto Shimazud2aa2202019-10-09 13:57:183792 const base::RepeatingCallback<base::Time()>& get_timestamp_callback) {
[email protected]c5b88d82012-10-06 17:03:333793 get_timestamp_callback_ = get_timestamp_callback;
3794}
[email protected]8ff00d72012-10-23 19:12:213795
Shivani Sharmaffb32b82019-04-09 16:58:473796// History manipulation intervention:
3797void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
Shivani Sharmaffb32b82019-04-09 16:58:473798 bool replace_entry,
3799 bool previous_document_was_activated,
Alexander Timine3ec4192020-04-20 16:39:403800 bool is_renderer_initiated,
3801 ukm::SourceId previous_page_load_ukm_source_id) {
Shivani Sharma712d5d72019-04-16 21:56:453802 // Note that for a subframe, previous_document_was_activated is true if the
3803 // gesture happened in any subframe (propagated to main frame) or in the main
3804 // frame itself.
Shivani Sharmaffb32b82019-04-09 16:58:473805 if (replace_entry || previous_document_was_activated ||
shivanigithubcceeacf2020-03-06 20:00:273806 !is_renderer_initiated) {
Shivani Sharmaffb32b82019-04-09 16:58:473807 if (last_committed_entry_index_ != -1) {
shivanigithube92c33da2020-09-14 13:01:413808 // This histogram always counts when navigating away from an entry,
3809 // irrespective of whether the skippable flag was changed or not, and
3810 // whether this entry is being reused or not.
Shivani Sharmaffb32b82019-04-09 16:58:473811 UMA_HISTOGRAM_BOOLEAN(
shivanigithube92c33da2020-09-14 13:01:413812 "Navigation.BackForward.SetShouldSkipOnBackForwardUI",
3813 GetEntryAtIndex(last_committed_entry_index_)
3814 ->should_skip_on_back_forward_ui());
Shivani Sharmaffb32b82019-04-09 16:58:473815 }
3816 return;
3817 }
3818 if (last_committed_entry_index_ == -1)
3819 return;
3820
Shivani Sharmac4cc8922019-04-18 03:11:173821 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473822 UMA_HISTOGRAM_BOOLEAN("Navigation.BackForward.SetShouldSkipOnBackForwardUI",
3823 true);
3824
Alexander Timine3ec4192020-04-20 16:39:403825 // Log UKM with the URL we are navigating away from.
3826 ukm::builders::HistoryManipulationIntervention(
3827 previous_page_load_ukm_source_id)
3828 .Record(ukm::UkmRecorder::Get());
Shivani Sharmaffb32b82019-04-09 16:58:473829}
3830
Shivani Sharmac4cc8922019-04-18 03:11:173831void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
3832 int reference_index,
3833 bool skippable) {
3834 auto* reference_entry = GetEntryAtIndex(reference_index);
3835 reference_entry->set_should_skip_on_back_forward_ui(skippable);
3836
3837 int64_t document_sequence_number =
3838 reference_entry->root_node()->frame_entry->document_sequence_number();
3839 for (int index = 0; index < GetEntryCount(); index++) {
3840 auto* entry = GetEntryAtIndex(index);
3841 if (entry->root_node()->frame_entry->document_sequence_number() ==
3842 document_sequence_number) {
3843 entry->set_should_skip_on_back_forward_ui(skippable);
3844 }
3845 }
3846}
3847
arthursonzogni66f711c2019-10-08 14:40:363848std::unique_ptr<NavigationControllerImpl::PendingEntryRef>
3849NavigationControllerImpl::ReferencePendingEntry() {
3850 DCHECK(pending_entry_);
3851 auto pending_entry_ref =
3852 std::make_unique<PendingEntryRef>(weak_factory_.GetWeakPtr());
3853 pending_entry_refs_.insert(pending_entry_ref.get());
3854 return pending_entry_ref;
3855}
3856
3857void NavigationControllerImpl::PendingEntryRefDeleted(PendingEntryRef* ref) {
3858 // Ignore refs that don't correspond to the current pending entry.
3859 auto it = pending_entry_refs_.find(ref);
3860 if (it == pending_entry_refs_.end())
3861 return;
3862 pending_entry_refs_.erase(it);
3863
3864 if (!pending_entry_refs_.empty())
3865 return;
3866
3867 // The pending entry may be deleted before the last PendingEntryRef.
3868 if (!pending_entry_)
3869 return;
3870
3871 // We usually clear the pending entry when the matching NavigationRequest
3872 // fails, so that an arbitrary URL isn't left visible above a committed page.
3873 //
3874 // However, we do preserve the pending entry in some cases, such as on the
3875 // initial navigation of an unmodified blank tab. We also allow the delegate
3876 // to say when it's safe to leave aborted URLs in the omnibox, to let the
3877 // user edit the URL and try again. This may be useful in cases that the
3878 // committed page cannot be attacker-controlled. In these cases, we still
3879 // allow the view to clear the pending entry and typed URL if the user
3880 // requests (e.g., hitting Escape with focus in the address bar).
3881 //
3882 // Do not leave the pending entry visible if it has an invalid URL, since this
3883 // might be formatted in an unexpected or unsafe way.
3884 // TODO(creis): Block navigations to invalid URLs in https://crbug.com/850824.
arthursonzogni66f711c2019-10-08 14:40:363885 bool should_preserve_entry =
3886 (pending_entry_ == GetVisibleEntry()) &&
3887 pending_entry_->GetURL().is_valid() &&
3888 (IsUnmodifiedBlankTab() || delegate_->ShouldPreserveAbortedURLs());
3889 if (should_preserve_entry)
3890 return;
3891
3892 DiscardPendingEntry(true);
3893 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3894}
3895
Antonio Sartori78a749f2020-11-30 12:03:393896std::unique_ptr<PolicyContainerHost::DocumentPolicies>
3897NavigationControllerImpl::ComputeDocumentPoliciesForFrameEntry(
3898 RenderFrameHostImpl* rfh,
3899 bool is_same_document,
3900 NavigationRequest* request) {
3901 if (!ShouldStoreDocumentPoliciesInFrameNavigationEntry(request))
3902 return nullptr;
3903
3904 if (is_same_document) {
3905 FrameNavigationEntry* previous_frame_entry =
3906 GetLastCommittedEntry()->GetFrameEntry(rfh->frame_tree_node());
3907
3908 // TODO(https://crbug.com/608402): Remove this nullptr check when we can
3909 // ensure we always have a FrameNavigationEntry here.
3910 if (!previous_frame_entry)
3911 return nullptr;
3912
3913 const PolicyContainerHost::DocumentPolicies* previous_document_policies =
3914 previous_frame_entry->document_policies();
3915
3916 if (!previous_document_policies)
3917 return nullptr;
3918
3919 // Make a copy of the policy container for the new FrameNavigationEntry.
3920 return std::make_unique<PolicyContainerHost::DocumentPolicies>(
3921 *previous_document_policies);
3922 }
3923
3924 if (!request->IsWaitingToCommit()) {
3925 // This is the initial, "fake" navigation to about:blank. The
3926 // NavigationRequest contains a dummy policy container, while the
3927 // RenderFrameHost already inherited the policy container from the
3928 // creator, so let's take the policies from there.
3929 return std::make_unique<PolicyContainerHost::DocumentPolicies>(
3930 rfh->policy_container_host()->document_policies());
3931 }
3932
3933 // Take the policy container from the request since we did not move it
3934 // into the RFH yet.
3935 return std::make_unique<PolicyContainerHost::DocumentPolicies>(
3936 request->policy_container_host()->document_policies());
3937}
3938
[email protected]8ff00d72012-10-23 19:12:213939} // namespace content