blob: 606d4535822dc397af8180db99b1efaee91854c6 [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
[email protected]d4a8ca482013-10-30 21:06:4036#include "content/browser/frame_host/navigation_controller_impl.h"
initial.commit09911bf2008-07-26 23:55:2937
dcheng36b6aec92015-12-26 06:16:3638#include <utility>
39
[email protected]c5b88d82012-10-06 17:03:3340#include "base/bind.h"
[email protected]eabfe1912014-05-12 10:07:2841#include "base/command_line.h"
initial.commit09911bf2008-07-26 23:55:2942#include "base/logging.h"
asvitkine30330812016-08-30 04:01:0843#include "base/metrics/histogram_macros.h"
[email protected]348fbaac2013-06-11 06:31:5144#include "base/strings/string_number_conversions.h" // Temporary
45#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"
[email protected]d4a8ca482013-10-30 21:06:4056#include "content/browser/frame_host/debug_urls.h"
57#include "content/browser/frame_host/interstitial_page_impl.h"
58#include "content/browser/frame_host/navigation_entry_impl.h"
Mohamed Abdelhalim833de902019-09-16 17:41:4559#include "content/browser/frame_host/navigation_request.h"
creis4e2ecb72015-06-20 00:46:3060#include "content/browser/frame_host/navigator.h"
[email protected]b3c41c0b2012-03-06 15:48:3261#include "content/browser/renderer_host/render_view_host_impl.h" // Temporary
[email protected]b6583592012-01-25 19:52:3362#include "content/browser/site_instance_impl.h"
Camille Lamy5193caa2018-10-12 11:59:4263#include "content/common/content_constants_internal.h"
[email protected]37567b432014-02-12 01:12:2264#include "content/common/frame_messages.h"
[email protected]0aed2f52011-03-23 18:06:3665#include "content/common/view_messages.h"
[email protected]ccb797302011-12-15 16:55:1166#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1967#include "content/public/browser/content_browser_client.h"
[email protected]d9083482012-01-06 00:38:4668#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0069#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3870#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1671#include "content/public/browser/notification_types.h"
[email protected]9677a3c2012-12-22 04:18:5872#include "content/public/browser/render_widget_host.h"
73#include "content/public/browser/render_widget_host_view.h"
Mikel Astizba9cf2fd2017-12-17 10:38:1074#include "content/public/browser/replaced_navigation_entry_data.h"
[email protected]4c3a23582012-08-18 08:54:3475#include "content/public/browser/storage_partition.h"
[email protected]d1198fd2012-08-13 22:50:1976#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:3877#include "content/public/common/content_constants.h"
toyoshim86e34ec2016-02-25 08:56:1078#include "content/public/common/content_features.h"
clamy7fced7b2017-11-16 19:52:4379#include "content/public/common/url_utils.h"
servolkf3955532015-05-16 00:01:5980#include "media/base/mime_util.h"
[email protected]a23de8572009-06-03 02:16:3281#include "net/base/escape.h"
Arthur Sonzogni620cec62018-12-13 13:08:5782#include "net/http/http_status_code.h"
Shivani Sharma93329102019-01-24 19:44:1883#include "services/metrics/public/cpp/ukm_builders.h"
84#include "services/metrics/public/cpp/ukm_recorder.h"
[email protected]9677a3c2012-12-22 04:18:5885#include "skia/ext/platform_canvas.h"
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:3986#include "third_party/blink/public/common/blob/blob_utils.h"
Blink Reformata30d4232018-04-07 15:31:0687#include "third_party/blink/public/common/mime_util/mime_util.h"
[email protected]cca6f392014-05-28 21:32:2688#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:2989
[email protected]8ff00d72012-10-23 19:12:2190namespace content {
[email protected]e9ba4472008-09-14 15:42:4391namespace {
92
93// Invoked when entries have been pruned, or removed. For example, if the
94// current entries are [google, digg, yahoo], with the current entry google,
95// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:4796void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
Shivani Sharmab9c46de82019-02-08 16:54:5097 int index,
[email protected]c12bf1a12008-09-17 16:28:4998 int count) {
[email protected]8ff00d72012-10-23 19:12:2199 PrunedDetails details;
Shivani Sharmab9c46de82019-02-08 16:54:50100 details.index = index;
[email protected]c12bf1a12008-09-17 16:28:49101 details.count = count;
Sam McNally5c087a32017-08-25 01:46:14102 nav_controller->delegate()->NotifyNavigationListPruned(details);
[email protected]e9ba4472008-09-14 15:42:43103}
104
105// Ensure the given NavigationEntry has a valid state, so that WebKit does not
106// get confused if we navigate back to it.
[email protected]40bcc302009-03-02 20:50:39107//
[email protected]e9ba4472008-09-14 15:42:43108// An empty state is treated as a new navigation by WebKit, which would mean
109// losing the navigation entries and generating a new navigation entry after
110// this one. We don't want that. To avoid this we create a valid state which
111// WebKit will not treat as a new navigation.
[email protected]691aa2f2013-05-28 22:52:04112void SetPageStateIfEmpty(NavigationEntryImpl* entry) {
113 if (!entry->GetPageState().IsValid())
114 entry->SetPageState(PageState::CreateFromURL(entry->GetURL()));
[email protected]e9ba4472008-09-14 15:42:43115}
116
117// Configure all the NavigationEntries in entries for restore. This resets
118// the transition type to reload and makes sure the content state isn't empty.
119void ConfigureEntriesForRestore(
dcheng9bfa5162016-04-09 01:00:57120 std::vector<std::unique_ptr<NavigationEntryImpl>>* entries,
toyoshim0df1d3a2016-09-09 09:52:48121 RestoreType type) {
[email protected]e9ba4472008-09-14 15:42:43122 for (size_t i = 0; i < entries->size(); ++i) {
123 // Use a transition type of reload so that we don't incorrectly increase
124 // the typed count.
Sylvain Defresnec6ccc77d2014-09-19 10:19:35125 (*entries)[i]->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
toyoshim0df1d3a2016-09-09 09:52:48126 (*entries)[i]->set_restore_type(type);
[email protected]e9ba4472008-09-14 15:42:43127 // NOTE(darin): This code is only needed for backwards compat.
avif16f85a72015-11-13 18:25:03128 SetPageStateIfEmpty((*entries)[i].get());
[email protected]e9ba4472008-09-14 15:42:43129 }
130}
131
[email protected]bf70edce2012-06-20 22:32:22132// Determines whether or not we should be carrying over a user agent override
133// between two NavigationEntries.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57134bool ShouldKeepOverride(NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22135 return last_entry && last_entry->GetIsOverridingUserAgent();
136}
137
Camille Lamy5193caa2018-10-12 11:59:42138// Determines whether to override user agent for a navigation.
139bool ShouldOverrideUserAgent(
140 NavigationController::UserAgentOverrideOption override_user_agent,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57141 NavigationEntry* last_committed_entry) {
Camille Lamy5193caa2018-10-12 11:59:42142 switch (override_user_agent) {
143 case NavigationController::UA_OVERRIDE_INHERIT:
144 return ShouldKeepOverride(last_committed_entry);
145 case NavigationController::UA_OVERRIDE_TRUE:
146 return true;
147 case NavigationController::UA_OVERRIDE_FALSE:
148 return false;
149 default:
150 break;
151 }
152 NOTREACHED();
153 return false;
154}
155
clamy0a656e42018-02-06 18:18:28156// Returns true this navigation should be treated as a reload. For e.g.
157// navigating to the last committed url via the address bar or clicking on a
158// link which results in a navigation to the last committed or pending
159// navigation, etc.
160// |url|, |virtual_url|, |base_url_for_data_url|, |transition_type| correspond
161// to the new navigation (i.e. the pending NavigationEntry).
162// |last_committed_entry| is the last navigation that committed.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57163bool ShouldTreatNavigationAsReload(const GURL& url,
164 const GURL& virtual_url,
165 const GURL& base_url_for_data_url,
166 ui::PageTransition transition_type,
167 bool is_main_frame,
168 bool is_post,
169 bool is_reload,
170 bool is_navigation_to_existing_entry,
171 bool has_interstitial,
172 NavigationEntryImpl* last_committed_entry) {
clamy0a656e42018-02-06 18:18:28173 // Don't convert when an interstitial is showing.
174 if (has_interstitial)
175 return false;
176
177 // Only convert main frame navigations to a new entry.
178 if (!is_main_frame || is_reload || is_navigation_to_existing_entry)
179 return false;
180
181 // Only convert to reload if at least one navigation committed.
182 if (!last_committed_entry)
ananta3bdd8ae2016-12-22 17:11:55183 return false;
184
arthursonzogni7a8243682017-12-14 16:41:42185 // Skip navigations initiated by external applications.
clamy0a656e42018-02-06 18:18:28186 if (transition_type & ui::PAGE_TRANSITION_FROM_API)
arthursonzogni7a8243682017-12-14 16:41:42187 return false;
188
ananta3bdd8ae2016-12-22 17:11:55189 // We treat (PAGE_TRANSITION_RELOAD | PAGE_TRANSITION_FROM_ADDRESS_BAR),
190 // PAGE_TRANSITION_TYPED or PAGE_TRANSITION_LINK transitions as navigations
191 // which should be treated as reloads.
clamy0a656e42018-02-06 18:18:28192 bool transition_type_can_be_converted = false;
193 if (ui::PageTransitionCoreTypeIs(transition_type,
194 ui::PAGE_TRANSITION_RELOAD) &&
195 (transition_type & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
196 transition_type_can_be_converted = true;
ananta3bdd8ae2016-12-22 17:11:55197 }
clamy0a656e42018-02-06 18:18:28198 if (ui::PageTransitionCoreTypeIs(transition_type,
ananta3bdd8ae2016-12-22 17:11:55199 ui::PAGE_TRANSITION_TYPED)) {
clamy0a656e42018-02-06 18:18:28200 transition_type_can_be_converted = true;
201 }
202 if (ui::PageTransitionCoreTypeIs(transition_type, ui::PAGE_TRANSITION_LINK))
203 transition_type_can_be_converted = true;
204 if (!transition_type_can_be_converted)
205 return false;
206
207 // This check is required for cases like view-source:, etc. Here the URL of
208 // the navigation entry would contain the url of the page, while the virtual
209 // URL contains the full URL including the view-source prefix.
210 if (virtual_url != last_committed_entry->GetVirtualURL())
211 return false;
212
213 // Check that the URL match.
214 if (url != last_committed_entry->GetURL())
215 return false;
216
217 // This check is required for Android WebView loadDataWithBaseURL. Apps
218 // can pass in anything in the base URL and we need to ensure that these
219 // match before classifying it as a reload.
220 if (url.SchemeIs(url::kDataScheme) && base_url_for_data_url.is_valid()) {
221 if (base_url_for_data_url != last_committed_entry->GetBaseURLForDataURL())
222 return false;
ananta3bdd8ae2016-12-22 17:11:55223 }
224
clamy0a656e42018-02-06 18:18:28225 // Skip entries with SSL errors.
226 if (last_committed_entry->ssl_error())
227 return false;
228
229 // Don't convert to a reload when the last navigation was a POST or the new
230 // navigation is a POST.
231 if (last_committed_entry->GetHasPostData() || is_post)
232 return false;
233
234 return true;
ananta3bdd8ae2016-12-22 17:11:55235}
236
Nasko Oskov03912102019-01-11 00:21:32237bool DoesURLMatchOriginForNavigation(
238 const GURL& url,
239 const base::Optional<url::Origin>& origin) {
240 // If there is no origin supplied there is nothing to match. This can happen
241 // for navigations to a pending entry and therefore it should be allowed.
242 if (!origin)
243 return true;
244
245 return origin->CanBeDerivedFrom(url);
246}
247
248base::Optional<url::Origin> GetCommittedOriginForFrameEntry(
249 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
250 // Error pages commit in an opaque origin, yet have the real URL that resulted
251 // in an error as the |params.url|. Since successful reload of an error page
252 // should commit in the correct origin, setting the opaque origin on the
253 // FrameNavigationEntry will be incorrect.
254 if (params.url_is_unreachable)
255 return base::nullopt;
256
257 return base::make_optional(params.origin);
258}
259
Camille Lamy5193caa2018-10-12 11:59:42260bool IsValidURLForNavigation(bool is_main_frame,
261 const GURL& virtual_url,
262 const GURL& dest_url) {
263 // Don't attempt to navigate if the virtual URL is non-empty and invalid.
264 if (is_main_frame && !virtual_url.is_valid() && !virtual_url.is_empty()) {
265 LOG(WARNING) << "Refusing to load for invalid virtual URL: "
266 << virtual_url.possibly_invalid_spec();
267 return false;
268 }
269
270 // Don't attempt to navigate to non-empty invalid URLs.
271 if (!dest_url.is_valid() && !dest_url.is_empty()) {
272 LOG(WARNING) << "Refusing to load invalid URL: "
273 << dest_url.possibly_invalid_spec();
274 return false;
275 }
276
277 // The renderer will reject IPC messages with URLs longer than
278 // this limit, so don't attempt to navigate with a longer URL.
279 if (dest_url.spec().size() > url::kMaxURLChars) {
280 LOG(WARNING) << "Refusing to load URL as it exceeds " << url::kMaxURLChars
281 << " characters.";
282 return false;
283 }
284
285 return true;
286}
287
Mikel Astizba9cf2fd2017-12-17 10:38:10288// See replaced_navigation_entry_data.h for details: this information is meant
289// to ensure |*output_entry| keeps track of its original URL (landing page in
290// case of server redirects) as it gets replaced (e.g. history.replaceState()),
291// without overwriting it later, for main frames.
292void CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57293 NavigationEntryImpl* replaced_entry,
Mikel Astizba9cf2fd2017-12-17 10:38:10294 NavigationEntryImpl* output_entry) {
295 if (output_entry->GetReplacedEntryData().has_value())
296 return;
297
298 ReplacedNavigationEntryData data;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57299 data.first_committed_url = replaced_entry->GetURL();
300 data.first_timestamp = replaced_entry->GetTimestamp();
301 data.first_transition_type = replaced_entry->GetTransitionType();
Charlie Reisb55438f2019-01-08 01:54:29302 output_entry->set_replaced_entry_data(data);
Mikel Astizba9cf2fd2017-12-17 10:38:10303}
304
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51305mojom::NavigationType GetNavigationType(const GURL& old_url,
306 const GURL& new_url,
307 ReloadType reload_type,
308 NavigationEntryImpl* entry,
309 const FrameNavigationEntry& frame_entry,
310 bool is_same_document_history_load) {
clamyea99ea12018-05-28 13:54:23311 // Reload navigations
312 switch (reload_type) {
313 case ReloadType::NORMAL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51314 return mojom::NavigationType::RELOAD;
clamyea99ea12018-05-28 13:54:23315 case ReloadType::BYPASSING_CACHE:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51316 return mojom::NavigationType::RELOAD_BYPASSING_CACHE;
clamyea99ea12018-05-28 13:54:23317 case ReloadType::ORIGINAL_REQUEST_URL:
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51318 return mojom::NavigationType::RELOAD_ORIGINAL_REQUEST_URL;
clamyea99ea12018-05-28 13:54:23319 case ReloadType::NONE:
320 break; // Fall through to rest of function.
321 }
322
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57323 if (entry->restore_type() == RestoreType::LAST_SESSION_EXITED_CLEANLY) {
324 if (entry->GetHasPostData())
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51325 return mojom::NavigationType::RESTORE_WITH_POST;
clamyea99ea12018-05-28 13:54:23326 else
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51327 return mojom::NavigationType::RESTORE;
clamyea99ea12018-05-28 13:54:23328 }
329
330 // History navigations.
331 if (frame_entry.page_state().IsValid()) {
332 if (is_same_document_history_load)
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51333 return mojom::NavigationType::HISTORY_SAME_DOCUMENT;
clamyea99ea12018-05-28 13:54:23334 else
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51335 return mojom::NavigationType::HISTORY_DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23336 }
337 DCHECK(!is_same_document_history_load);
338
339 // A same-document fragment-navigation happens when the only part of the url
340 // that is modified is after the '#' character.
341 //
342 // When modifying this condition, please take a look at:
343 // FrameLoader::shouldPerformFragmentNavigation.
344 //
345 // Note: this check is only valid for navigations that are not history
346 // navigations. For instance, if the history is: 'A#bar' -> 'B' -> 'A#foo', a
347 // history navigation from 'A#foo' to 'A#bar' is not a same-document
348 // navigation, but a different-document one. This is why history navigation
349 // are classified before this check.
350 if (new_url.has_ref() && old_url.EqualsIgnoringRef(new_url) &&
351 frame_entry.method() == "GET") {
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51352 return mojom::NavigationType::SAME_DOCUMENT;
clamyea99ea12018-05-28 13:54:23353 } else {
Lucas Furukawa Gadanief8290a2019-07-29 20:27:51354 return mojom::NavigationType::DIFFERENT_DOCUMENT;
clamyea99ea12018-05-28 13:54:23355 }
356}
357
Camille Lamy5193caa2018-10-12 11:59:42358// Adjusts the original input URL if needed, to get the URL to actually load and
359// the virtual URL, which may differ.
360void RewriteUrlForNavigation(const GURL& original_url,
361 BrowserContext* browser_context,
362 GURL* url_to_load,
363 GURL* virtual_url,
364 bool* reverse_on_redirect) {
365 // Fix up the given URL before letting it be rewritten, so that any minor
366 // cleanup (e.g., removing leading dots) will not lead to a virtual URL.
367 *virtual_url = original_url;
368 BrowserURLHandlerImpl::GetInstance()->FixupURLBeforeRewrite(virtual_url,
369 browser_context);
370
371 // Allow the browser URL handler to rewrite the URL. This will, for example,
372 // remove "view-source:" from the beginning of the URL to get the URL that
373 // will actually be loaded. This real URL won't be shown to the user, just
374 // used internally.
375 *url_to_load = *virtual_url;
376 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
377 url_to_load, browser_context, reverse_on_redirect);
378}
379
380#if DCHECK_IS_ON()
381// Helper sanity check function used in debug mode.
382void ValidateRequestMatchesEntry(NavigationRequest* request,
383 NavigationEntryImpl* entry) {
384 if (request->frame_tree_node()->IsMainFrame()) {
385 DCHECK_EQ(request->browser_initiated(), !entry->is_renderer_initiated());
386 DCHECK(ui::PageTransitionTypeIncludingQualifiersIs(
387 request->common_params().transition, entry->GetTransitionType()));
388 }
389 DCHECK_EQ(request->common_params().should_replace_current_entry,
390 entry->should_replace_entry());
Nasko Oskovc36327d2019-01-03 23:23:04391 DCHECK_EQ(request->commit_params().should_clear_history_list,
Camille Lamy5193caa2018-10-12 11:59:42392 entry->should_clear_history_list());
393 DCHECK_EQ(request->common_params().has_user_gesture,
394 entry->has_user_gesture());
395 DCHECK_EQ(request->common_params().base_url_for_data_url,
396 entry->GetBaseURLForDataURL());
Nasko Oskovc36327d2019-01-03 23:23:04397 DCHECK_EQ(request->commit_params().can_load_local_resources,
Camille Lamy5193caa2018-10-12 11:59:42398 entry->GetCanLoadLocalResources());
399 DCHECK_EQ(request->common_params().started_from_context_menu,
400 entry->has_started_from_context_menu());
401
402 FrameNavigationEntry* frame_entry =
403 entry->GetFrameEntry(request->frame_tree_node());
404 if (!frame_entry) {
405 NOTREACHED();
406 return;
407 }
408
409 DCHECK_EQ(request->common_params().url, frame_entry->url());
410 DCHECK_EQ(request->common_params().method, frame_entry->method());
411
Nasko Oskovc36327d2019-01-03 23:23:04412 size_t redirect_size = request->commit_params().redirects.size();
Camille Lamy5193caa2018-10-12 11:59:42413 if (redirect_size == frame_entry->redirect_chain().size()) {
414 for (size_t i = 0; i < redirect_size; ++i) {
Nasko Oskovc36327d2019-01-03 23:23:04415 DCHECK_EQ(request->commit_params().redirects[i],
Camille Lamy5193caa2018-10-12 11:59:42416 frame_entry->redirect_chain()[i]);
417 }
418 } else {
419 NOTREACHED();
420 }
421}
422#endif // DCHECK_IS_ON()
423
Dave Tapuska8bfd84c2019-03-26 20:47:16424// Returns whether the session history NavigationRequests in |navigations|
425// would stay within the subtree of the sandboxed iframe in
426// |sandbox_frame_tree_node_id|.
427bool DoesSandboxNavigationStayWithinSubtree(
428 int sandbox_frame_tree_node_id,
429 const std::vector<std::unique_ptr<NavigationRequest>>& navigations) {
430 for (auto& item : navigations) {
431 bool within_subtree = false;
432 // Check whether this NavigationRequest affects a frame within the
433 // sandboxed frame's subtree by walking up the tree looking for the
434 // sandboxed frame.
435 for (auto* frame = item->frame_tree_node(); frame;
436 frame = frame->parent()) {
437 if (frame->frame_tree_node_id() == sandbox_frame_tree_node_id) {
438 within_subtree = true;
439 break;
440 }
441 }
442 if (!within_subtree)
443 return false;
444 }
445 return true;
446}
447
[email protected]e9ba4472008-09-14 15:42:43448} // namespace
449
[email protected]d202a7c2012-01-04 07:53:47450// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29451
[email protected]23a918b2014-07-15 09:51:36452const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23453
[email protected]765b35502008-08-21 00:51:20454// static
[email protected]d202a7c2012-01-04 07:53:47455size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23456 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20457
[email protected]e6fec472013-05-14 05:29:02458// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20459// when testing.
460static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29461
[email protected]71fde352011-12-29 03:29:56462// static
dcheng9bfa5162016-04-09 01:00:57463std::unique_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
464 const GURL& url,
465 const Referrer& referrer,
Lukasz Anforowicz435bcb582019-07-12 20:50:06466 const base::Optional<url::Origin>& initiator_origin,
dcheng9bfa5162016-04-09 01:00:57467 ui::PageTransition transition,
468 bool is_renderer_initiated,
469 const std::string& extra_headers,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09470 BrowserContext* browser_context,
471 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
Camille Lamy5193caa2018-10-12 11:59:42472 GURL url_to_load;
473 GURL virtual_url;
[email protected]71fde352011-12-29 03:29:56474 bool reverse_on_redirect = false;
Camille Lamy5193caa2018-10-12 11:59:42475 RewriteUrlForNavigation(url, browser_context, &url_to_load, &virtual_url,
476 &reverse_on_redirect);
[email protected]71fde352011-12-29 03:29:56477
Patrick Monettef507e982019-06-19 20:18:06478 auto entry = std::make_unique<NavigationEntryImpl>(
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28479 nullptr, // The site instance for tabs is sent on navigation
480 // (WebContents::GetSiteInstance).
Lukasz Anforowicz435bcb582019-07-12 20:50:06481 url_to_load, referrer, initiator_origin, base::string16(), transition,
Camille Lamy5193caa2018-10-12 11:59:42482 is_renderer_initiated, blob_url_loader_factory);
483 entry->SetVirtualURL(virtual_url);
484 entry->set_user_typed_url(virtual_url);
[email protected]71fde352011-12-29 03:29:56485 entry->set_update_virtual_url_with_url(reverse_on_redirect);
486 entry->set_extra_headers(extra_headers);
Patrick Monettef507e982019-06-19 20:18:06487 return entry;
[email protected]71fde352011-12-29 03:29:56488}
489
[email protected]cdcb1dee2012-01-04 00:46:20490// static
491void NavigationController::DisablePromptOnRepost() {
492 g_check_for_repost = false;
493}
494
[email protected]c5b88d82012-10-06 17:03:33495base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
496 base::Time t) {
497 // If |t| is between the water marks, we're in a run of duplicates
498 // or just getting out of it, so increase the high-water mark to get
499 // a time that probably hasn't been used before and return it.
500 if (low_water_mark_ <= t && t <= high_water_mark_) {
501 high_water_mark_ += base::TimeDelta::FromMicroseconds(1);
502 return high_water_mark_;
503 }
504
505 // Otherwise, we're clear of the last duplicate run, so reset the
506 // water marks.
507 low_water_mark_ = high_water_mark_ = t;
508 return t;
509}
510
[email protected]d202a7c2012-01-04 07:53:47511NavigationControllerImpl::NavigationControllerImpl(
[email protected]ec6c05f2013-10-23 18:41:57512 NavigationControllerDelegate* delegate,
[email protected]d1198fd2012-08-13 22:50:19513 BrowserContext* browser_context)
[email protected]3d7474ff2011-07-27 17:47:37514 : browser_context_(browser_context),
ananta3bdd8ae2016-12-22 17:11:55515 pending_entry_(nullptr),
avi45a72532015-04-07 21:01:45516 failed_pending_entry_id_(0),
[email protected]765b35502008-08-21 00:51:20517 last_committed_entry_index_(-1),
518 pending_entry_index_(-1),
[email protected]cbab76d2008-10-13 22:42:47519 transient_entry_index_(-1),
[email protected]ec6c05f2013-10-23 18:41:57520 delegate_(delegate),
[email protected]69e797f2013-04-30 01:10:22521 ssl_manager_(this),
[email protected]38b8f4e2009-09-24 19:44:57522 needs_reload_(false),
[email protected]27ba81c2012-08-21 17:04:09523 is_initial_navigation_(true),
[email protected]40fd3032014-02-28 22:16:28524 in_navigate_to_pending_entry_(false),
toyoshim0df1d3a2016-09-09 09:52:48525 pending_reload_(ReloadType::NONE),
Takashi Toyoshimac7df3c22019-06-25 14:18:47526 get_timestamp_callback_(base::Bind(&base::Time::Now)) {
[email protected]3d7474ff2011-07-27 17:47:37527 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29528}
529
[email protected]d202a7c2012-01-04 07:53:47530NavigationControllerImpl::~NavigationControllerImpl() {
arthursonzogni69a6a1b2019-09-17 09:23:00531 // The NavigationControllerImpl might be called inside its delegate
532 // destructor. Calling it is not valid anymore.
533 delegate_ = nullptr;
534 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29535}
536
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57537WebContents* NavigationControllerImpl::GetWebContents() {
[email protected]ec6c05f2013-10-23 18:41:57538 return delegate_->GetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32539}
540
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57541BrowserContext* NavigationControllerImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55542 return browser_context_;
543}
544
[email protected]d202a7c2012-01-04 07:53:47545void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30546 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36547 RestoreType type,
dcheng9bfa5162016-04-09 01:00:57548 std::vector<std::unique_ptr<NavigationEntry>>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57549 // Verify that this controller is unused and that the input is valid.
[email protected]a26023822011-12-29 00:23:55550 DCHECK(GetEntryCount() == 0 && !GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57551 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14552 selected_navigation < static_cast<int>(entries->size()));
arthursonzogni5c4c202d2017-04-25 23:41:27553 DCHECK_EQ(-1, pending_entry_index_);
[email protected]ce3fa3c2009-04-20 19:55:57554
[email protected]ce3fa3c2009-04-20 19:55:57555 needs_reload_ = true;
Bo Liucdfa4b12018-11-06 00:21:44556 needs_reload_type_ = NeedsReloadType::kRestoreSession;
avif16f85a72015-11-13 18:25:03557 entries_.reserve(entries->size());
558 for (auto& entry : *entries)
dcheng36b6aec92015-12-26 06:16:36559 entries_.push_back(
560 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
avif16f85a72015-11-13 18:25:03561
562 // At this point, the |entries| is full of empty scoped_ptrs, so it can be
563 // cleared out safely.
564 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57565
566 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36567 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57568}
569
toyoshim6142d96f2016-12-19 09:07:25570void NavigationControllerImpl::Reload(ReloadType reload_type,
571 bool check_for_repost) {
liaoyuke9168fba2017-03-10 19:20:28572 DCHECK_NE(ReloadType::NONE, reload_type);
573
[email protected]669e0ba2012-08-30 23:57:36574 if (transient_entry_index_ != -1) {
575 // If an interstitial is showing, treat a reload as a navigation to the
576 // transient entry's URL.
creis3da03872015-02-20 21:12:32577 NavigationEntryImpl* transient_entry = GetTransientEntry();
[email protected]6286a3792013-10-09 04:03:27578 if (!transient_entry)
[email protected]669e0ba2012-08-30 23:57:36579 return;
[email protected]6286a3792013-10-09 04:03:27580 LoadURL(transient_entry->GetURL(),
[email protected]8ff00d72012-10-23 19:12:21581 Referrer(),
Sylvain Defresnec6ccc77d2014-09-19 10:19:35582 ui::PAGE_TRANSITION_RELOAD,
[email protected]6286a3792013-10-09 04:03:27583 transient_entry->extra_headers());
[email protected]cbab76d2008-10-13 22:42:47584 return;
[email protected]669e0ba2012-08-30 23:57:36585 }
[email protected]cbab76d2008-10-13 22:42:47586
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28587 NavigationEntryImpl* entry = nullptr;
[email protected]59167c22013-06-03 18:07:32588 int current_index = -1;
589
590 // If we are reloading the initial navigation, just use the current
591 // pending entry. Otherwise look up the current entry.
592 if (IsInitialNavigation() && pending_entry_) {
593 entry = pending_entry_;
594 // The pending entry might be in entries_ (e.g., after a Clone), so we
595 // should also update the current_index.
596 current_index = pending_entry_index_;
597 } else {
arthursonzogni69a6a1b2019-09-17 09:23:00598 DiscardNonCommittedEntries();
[email protected]59167c22013-06-03 18:07:32599 current_index = GetCurrentEntryIndex();
600 if (current_index != -1) {
creis3da03872015-02-20 21:12:32601 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32602 }
[email protected]979a4bc2013-04-24 01:27:15603 }
[email protected]241db352013-04-22 18:04:05604
[email protected]59167c22013-06-03 18:07:32605 // If we are no where, then we can't reload. TODO(darin): We should add a
606 // CanReload method.
607 if (!entry)
608 return;
609
Takashi Toyoshimac7df3c22019-06-25 14:18:47610 // Set ReloadType for |entry|.
toyoshima63c2a62016-09-29 09:03:26611 entry->set_reload_type(reload_type);
612
[email protected]cdcb1dee2012-01-04 00:46:20613 if (g_check_for_repost && check_for_repost &&
[email protected]59167c22013-06-03 18:07:32614 entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30615 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07616 // they really want to do this. If they do, the dialog will call us back
617 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57618 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02619
[email protected]106a0812010-03-18 00:15:12620 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57621 delegate_->ActivateAndShowRepostFormWarningDialog();
initial.commit09911bf2008-07-26 23:55:29622 } else {
[email protected]59167c22013-06-03 18:07:32623 if (!IsInitialNavigation())
arthursonzogni69a6a1b2019-09-17 09:23:00624 DiscardNonCommittedEntries();
[email protected]c3b3d952012-01-31 12:52:26625
Nasko Oskovaee2f862018-06-15 00:05:52626 pending_entry_ = entry;
627 pending_entry_index_ = current_index;
628 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
[email protected]bcd904482012-02-01 01:54:22629
Dave Tapuska8bfd84c2019-03-26 20:47:16630 NavigateToExistingPendingEntry(reload_type,
631 FrameTreeNode::kFrameTreeNodeInvalidId);
initial.commit09911bf2008-07-26 23:55:29632 }
633}
634
[email protected]d202a7c2012-01-04 07:53:47635void NavigationControllerImpl::CancelPendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48636 DCHECK(pending_reload_ != ReloadType::NONE);
637 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12638}
639
[email protected]d202a7c2012-01-04 07:53:47640void NavigationControllerImpl::ContinuePendingReload() {
toyoshim0df1d3a2016-09-09 09:52:48641 if (pending_reload_ == ReloadType::NONE) {
[email protected]106a0812010-03-18 00:15:12642 NOTREACHED();
643 } else {
toyoshim6142d96f2016-12-19 09:07:25644 Reload(pending_reload_, false);
toyoshim0df1d3a2016-09-09 09:52:48645 pending_reload_ = ReloadType::NONE;
[email protected]106a0812010-03-18 00:15:12646 }
647}
648
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57649bool NavigationControllerImpl::IsInitialNavigation() {
[email protected]27ba81c2012-08-21 17:04:09650 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11651}
652
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57653bool NavigationControllerImpl::IsInitialBlankNavigation() {
creis10a4ab72015-10-13 17:22:40654 // TODO(creis): Once we create a NavigationEntry for the initial blank page,
toyoshim0df1d3a2016-09-09 09:52:48655 // we'll need to check for entry count 1 and restore_type NONE (to exclude
656 // the cloned tab case).
creis10a4ab72015-10-13 17:22:40657 return IsInitialNavigation() && GetEntryCount() == 0;
658}
659
avi254eff02015-07-01 08:27:58660NavigationEntryImpl*
661NavigationControllerImpl::GetEntryWithUniqueID(int nav_entry_id) const {
662 int index = GetEntryIndexWithUniqueID(nav_entry_id);
avif16f85a72015-11-13 18:25:03663 return (index != -1) ? entries_[index].get() : nullptr;
avi254eff02015-07-01 08:27:58664}
665
avi25764702015-06-23 15:43:37666void NavigationControllerImpl::SetPendingEntry(
dcheng9bfa5162016-04-09 01:00:57667 std::unique_ptr<NavigationEntryImpl> entry) {
arthursonzogni69a6a1b2019-09-17 09:23:00668 DiscardNonCommittedEntries();
avi25764702015-06-23 15:43:37669 pending_entry_ = entry.release();
arthursonzogni5c4c202d2017-04-25 23:41:27670 DCHECK_EQ(-1, pending_entry_index_);
[email protected]8ff00d72012-10-23 19:12:21671 NotificationService::current()->Notify(
672 NOTIFICATION_NAV_ENTRY_PENDING,
673 Source<NavigationController>(this),
avi25764702015-06-23 15:43:37674 Details<NavigationEntry>(pending_entry_));
[email protected]765b35502008-08-21 00:51:20675}
676
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57677NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() {
[email protected]cbab76d2008-10-13 22:42:47678 if (transient_entry_index_ != -1)
avif16f85a72015-11-13 18:25:03679 return entries_[transient_entry_index_].get();
[email protected]cbab76d2008-10-13 22:42:47680 if (pending_entry_)
681 return pending_entry_;
682 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20683}
684
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57685NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() {
[email protected]867e1f92011-08-30 19:01:19686 if (transient_entry_index_ != -1)
avif16f85a72015-11-13 18:25:03687 return entries_[transient_entry_index_].get();
[email protected]59167c22013-06-03 18:07:32688 // The pending entry is safe to return for new (non-history), browser-
689 // initiated navigations. Most renderer-initiated navigations should not
690 // show the pending entry, to prevent URL spoof attacks.
691 //
692 // We make an exception for renderer-initiated navigations in new tabs, as
693 // long as no other page has tried to access the initial empty document in
694 // the new tab. If another page modifies this blank page, a URL spoof is
695 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32696 bool safe_to_show_pending =
697 pending_entry_ &&
698 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09699 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32700 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46701 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32702
703 // Also allow showing the pending entry for history navigations in a new tab,
704 // such as Ctrl+Back. In this case, no existing page is visible and no one
705 // can script the new tab before it commits.
706 if (!safe_to_show_pending &&
707 pending_entry_ &&
avi0dca04d2015-01-26 20:21:09708 pending_entry_index_ != -1 &&
[email protected]59167c22013-06-03 18:07:32709 IsInitialNavigation() &&
[email protected]e47ae9472011-10-13 19:48:34710 !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32711 safe_to_show_pending = true;
712
713 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19714 return pending_entry_;
715 return GetLastCommittedEntry();
716}
717
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57718int NavigationControllerImpl::GetCurrentEntryIndex() {
[email protected]cbab76d2008-10-13 22:42:47719 if (transient_entry_index_ != -1)
720 return transient_entry_index_;
[email protected]765b35502008-08-21 00:51:20721 if (pending_entry_index_ != -1)
722 return pending_entry_index_;
723 return last_committed_entry_index_;
724}
725
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57726NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() {
[email protected]765b35502008-08-21 00:51:20727 if (last_committed_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28728 return nullptr;
avif16f85a72015-11-13 18:25:03729 return entries_[last_committed_entry_index_].get();
[email protected]765b35502008-08-21 00:51:20730}
731
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57732bool NavigationControllerImpl::CanViewSource() {
[email protected]ec6c05f2013-10-23 18:41:57733 const std::string& mime_type = delegate_->GetContentsMimeType();
Kinuko Yasuda74702f92017-07-31 03:27:53734 bool is_viewable_mime_type = blink::IsSupportedNonImageMimeType(mime_type) &&
735 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27736 NavigationEntry* visible_entry = GetVisibleEntry();
737 return visible_entry && !visible_entry->IsViewSourceMode() &&
[email protected]ec6c05f2013-10-23 18:41:57738 is_viewable_mime_type && !delegate_->GetInterstitialPage();
[email protected]31682282010-01-15 18:05:16739}
740
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57741int NavigationControllerImpl::GetLastCommittedEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:27742 // The last committed entry index must always be less than the number of
743 // entries. If there are no entries, it must be -1. However, there may be a
744 // transient entry while the last committed entry index is still -1.
745 DCHECK_LT(last_committed_entry_index_, GetEntryCount());
746 DCHECK(GetEntryCount() || last_committed_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:55747 return last_committed_entry_index_;
748}
749
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57750int NavigationControllerImpl::GetEntryCount() {
Piotr Kalinowski2cccb522019-07-09 12:10:51751 DCHECK_LE(entries_.size(),
752 max_entry_count() + (transient_entry_index_ == -1 ? 0 : 1));
[email protected]a26023822011-12-29 00:23:55753 return static_cast<int>(entries_.size());
754}
755
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57756NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(int index) {
avi25764702015-06-23 15:43:37757 if (index < 0 || index >= GetEntryCount())
758 return nullptr;
759
avif16f85a72015-11-13 18:25:03760 return entries_[index].get();
[email protected]022af742011-12-28 18:37:25761}
762
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57763NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(int offset) {
avi057ce1492015-06-29 15:59:47764 return GetEntryAtIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20765}
766
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57767int NavigationControllerImpl::GetIndexForOffset(int offset) {
[email protected]7bc2b032012-12-19 22:45:46768 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03769}
770
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57771bool NavigationControllerImpl::CanGoBack() {
Shivani Sharma298d12852019-01-22 20:04:03772 if (!base::FeatureList::IsEnabled(features::kHistoryManipulationIntervention))
773 return CanGoToOffset(-1);
774
775 for (int index = GetIndexForOffset(-1); index >= 0; index--) {
776 if (!GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
777 return true;
778 }
779 return false;
[email protected]765b35502008-08-21 00:51:20780}
781
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57782bool NavigationControllerImpl::CanGoForward() {
avi56e40c92015-08-27 00:11:22783 return CanGoToOffset(1);
[email protected]765b35502008-08-21 00:51:20784}
785
Lucas Furukawa Gadani5553a1582019-01-08 18:55:57786bool NavigationControllerImpl::CanGoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03787 int index = GetIndexForOffset(offset);
788 return index >= 0 && index < GetEntryCount();
789}
790
[email protected]d202a7c2012-01-04 07:53:47791void NavigationControllerImpl::GoBack() {
shivanisha55201872018-12-13 04:29:06792 int target_index = GetIndexForOffset(-1);
793
794 // Log metrics for the number of entries that are eligible for skipping on
Shivani Sharma2d5b4b6b2019-01-08 16:07:16795 // back button and move the target index past the skippable entries, if
796 // history intervention is enabled.
shivanisha55201872018-12-13 04:29:06797 int count_entries_skipped = 0;
Shivani Sharma298d12852019-01-22 20:04:03798 bool all_skippable_entries = true;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16799 bool history_intervention_enabled = base::FeatureList::IsEnabled(
800 features::kHistoryManipulationIntervention);
shivanisha55201872018-12-13 04:29:06801 for (int index = target_index; index >= 0; index--) {
Shivani Sharma2d5b4b6b2019-01-08 16:07:16802 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
shivanisha55201872018-12-13 04:29:06803 count_entries_skipped++;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16804 } else {
805 if (history_intervention_enabled)
806 target_index = index;
Shivani Sharma298d12852019-01-22 20:04:03807 all_skippable_entries = false;
shivanisha55201872018-12-13 04:29:06808 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16809 }
shivanisha55201872018-12-13 04:29:06810 }
811 UMA_HISTOGRAM_ENUMERATION("Navigation.BackForward.BackTargetSkipped",
812 count_entries_skipped, kMaxSessionHistoryEntries);
Shivani Sharma298d12852019-01-22 20:04:03813 UMA_HISTOGRAM_BOOLEAN("Navigation.BackForward.AllBackTargetsSkippable",
814 all_skippable_entries);
815
816 // Do nothing if all entries are skippable. Normally this path would not
817 // happen as consumers would have already checked it in CanGoBack but a lot of
818 // tests do not do that.
819 if (history_intervention_enabled && all_skippable_entries)
820 return;
shivanisha55201872018-12-13 04:29:06821
shivanisha55201872018-12-13 04:29:06822 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20823}
824
[email protected]d202a7c2012-01-04 07:53:47825void NavigationControllerImpl::GoForward() {
shivanisha55201872018-12-13 04:29:06826 int target_index = GetIndexForOffset(1);
827
828 // Log metrics for the number of entries that are eligible for skipping on
Shivani Sharma2d5b4b6b2019-01-08 16:07:16829 // forward button and move the target index past the skippable entries, if
830 // history intervention is enabled.
831 // Note that at least one entry (the last one) will be non-skippable since
832 // entries are marked skippable only when they add another entry because of
833 // redirect or pushState.
shivanisha55201872018-12-13 04:29:06834 int count_entries_skipped = 0;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16835 bool history_intervention_enabled = base::FeatureList::IsEnabled(
836 features::kHistoryManipulationIntervention);
shivanisha55201872018-12-13 04:29:06837 for (size_t index = target_index; index < entries_.size(); index++) {
Shivani Sharma2d5b4b6b2019-01-08 16:07:16838 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui()) {
shivanisha55201872018-12-13 04:29:06839 count_entries_skipped++;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16840 } else {
841 if (history_intervention_enabled)
842 target_index = index;
shivanisha55201872018-12-13 04:29:06843 break;
Shivani Sharma2d5b4b6b2019-01-08 16:07:16844 }
shivanisha55201872018-12-13 04:29:06845 }
846 UMA_HISTOGRAM_ENUMERATION("Navigation.BackForward.ForwardTargetSkipped",
847 count_entries_skipped, kMaxSessionHistoryEntries);
848
shivanisha55201872018-12-13 04:29:06849 GoToIndex(target_index);
[email protected]765b35502008-08-21 00:51:20850}
851
[email protected]d202a7c2012-01-04 07:53:47852void NavigationControllerImpl::GoToIndex(int index) {
Dave Tapuska8bfd84c2019-03-26 20:47:16853 GoToIndex(index, FrameTreeNode::kFrameTreeNodeInvalidId);
854}
855
856void NavigationControllerImpl::GoToIndex(int index,
857 int sandbox_frame_tree_node_id) {
sunjian30574a62017-03-21 21:39:44858 TRACE_EVENT0("browser,navigation,benchmark",
859 "NavigationControllerImpl::GoToIndex");
[email protected]765b35502008-08-21 00:51:20860 if (index < 0 || index >= static_cast<int>(entries_.size())) {
861 NOTREACHED();
862 return;
863 }
864
[email protected]cbab76d2008-10-13 22:42:47865 if (transient_entry_index_ != -1) {
866 if (index == transient_entry_index_) {
867 // Nothing to do when navigating to the transient.
868 return;
869 }
870 if (index > transient_entry_index_) {
871 // Removing the transient is goint to shift all entries by 1.
872 index--;
873 }
874 }
875
876 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20877
arthursonzogni5c4c202d2017-04-25 23:41:27878 DCHECK_EQ(nullptr, pending_entry_);
879 DCHECK_EQ(-1, pending_entry_index_);
880 pending_entry_ = entries_[index].get();
[email protected]765b35502008-08-21 00:51:20881 pending_entry_index_ = index;
arthursonzogni5c4c202d2017-04-25 23:41:27882 pending_entry_->SetTransitionType(ui::PageTransitionFromInt(
883 pending_entry_->GetTransitionType() | ui::PAGE_TRANSITION_FORWARD_BACK));
Dave Tapuska8bfd84c2019-03-26 20:47:16884 NavigateToExistingPendingEntry(ReloadType::NONE, sandbox_frame_tree_node_id);
[email protected]765b35502008-08-21 00:51:20885}
886
[email protected]d202a7c2012-01-04 07:53:47887void NavigationControllerImpl::GoToOffset(int offset) {
toyoshim3af4d502016-03-30 12:38:12888 // Note: This is actually reached in unit tests.
[email protected]9ba14052012-06-22 23:50:03889 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:20890 return;
891
[email protected]9ba14052012-06-22 23:50:03892 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20893}
894
[email protected]41374f12013-07-24 02:49:28895bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
896 if (index == last_committed_entry_index_ ||
897 index == pending_entry_index_)
898 return false;
[email protected]6a13a6c2011-12-20 21:47:12899
[email protected]43032342011-03-21 14:10:31900 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:28901 return true;
[email protected]cbab76d2008-10-13 22:42:47902}
903
[email protected]d202a7c2012-01-04 07:53:47904void NavigationControllerImpl::UpdateVirtualURLToURL(
[email protected]10f417c52011-12-28 21:04:23905 NavigationEntryImpl* entry, const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:32906 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:31907 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:51908 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
909 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:32910 }
911}
912
[email protected]d202a7c2012-01-04 07:53:47913void NavigationControllerImpl::LoadURL(
[email protected]6c6b02d2011-09-02 03:36:47914 const GURL& url,
[email protected]8ff00d72012-10-23 19:12:21915 const Referrer& referrer,
Sylvain Defresnec6ccc77d2014-09-19 10:19:35916 ui::PageTransition transition,
[email protected]6c6b02d2011-09-02 03:36:47917 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:47918 LoadURLParams params(url);
919 params.referrer = referrer;
920 params.transition_type = transition;
921 params.extra_headers = extra_headers;
922 LoadURLWithParams(params);
923}
924
925void NavigationControllerImpl::LoadURLWithParams(const LoadURLParams& params) {
Lukasz Anforowicz435bcb582019-07-12 20:50:06926 if (params.is_renderer_initiated)
927 DCHECK(params.initiator_origin.has_value());
928
naskob8744d22014-08-28 17:07:43929 TRACE_EVENT1("browser,navigation",
930 "NavigationControllerImpl::LoadURLWithParams",
931 "url", params.url.possibly_invalid_spec());
Ian Vollick9dda0522019-09-11 02:24:29932 bool is_explicit_navigation =
933 GetContentClient()->browser()->IsExplicitNavigation(
934 params.transition_type);
935 if (HandleDebugURL(params.url, params.transition_type,
936 is_explicit_navigation)) {
[email protected]47752982014-07-29 08:01:43937 // If Telemetry is running, allow the URL load to proceed as if it's
938 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:49939 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:43940 cc::switches::kEnableGpuBenchmarking))
941 return;
942 }
[email protected]8bf1048012012-02-08 01:22:18943
[email protected]cf002332012-08-14 19:17:47944 // Checks based on params.load_type.
945 switch (params.load_type) {
946 case LOAD_TYPE_DEFAULT:
lukasza477a5a22016-06-16 18:28:43947 case LOAD_TYPE_HTTP_POST:
[email protected]cf002332012-08-14 19:17:47948 break;
949 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:26950 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:47951 NOTREACHED() << "Data load must use data scheme.";
952 return;
953 }
954 break;
955 default:
956 NOTREACHED();
957 break;
Lukasz Anforowiczbb0cfd5e2017-12-14 22:39:46958 }
[email protected]e47ae9472011-10-13 19:48:34959
[email protected]e47ae9472011-10-13 19:48:34960 // The user initiated a load, we don't need to reload anymore.
961 needs_reload_ = false;
962
clamy21718cc22018-06-13 13:34:24963 NavigateWithoutEntry(params);
[email protected]132e281a2012-07-31 18:32:44964}
965
Mohamed Abdelhalim833de902019-09-16 17:41:45966bool NavigationControllerImpl::PendingEntryMatchesRequest(
967 NavigationRequest* request) const {
creisb4dc9332016-03-14 21:39:19968 return pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:45969 pending_entry_->GetUniqueID() == request->nav_entry_id();
creisb4dc9332016-03-14 21:39:19970}
971
[email protected]d202a7c2012-01-04 07:53:47972bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:32973 RenderFrameHostImpl* rfh,
[email protected]37567b432014-02-12 01:12:22974 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
peary21b0f797b2016-09-28 17:28:33975 LoadCommittedDetails* details,
Eugene But712f03d2018-05-22 16:03:44976 bool is_same_document_navigation,
Shivani Sharmaffb32b82019-04-09 16:58:47977 bool previous_document_was_activated,
Camille Lamy10aafcd32018-12-05 15:48:13978 NavigationRequest* navigation_request) {
979 DCHECK(navigation_request);
[email protected]cd2e15742013-03-08 04:08:31980 is_initial_navigation_ = false;
981
[email protected]0e8db942008-09-24 21:21:48982 // Save the previous state before we clobber it.
aelias100c9192017-01-13 00:01:43983 bool overriding_user_agent_changed = false;
[email protected]0e8db942008-09-24 21:21:48984 if (GetLastCommittedEntry()) {
[email protected]36fc0392011-12-25 03:59:51985 details->previous_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:55986 details->previous_entry_index = GetLastCommittedEntryIndex();
aelias100c9192017-01-13 00:01:43987 if (pending_entry_ &&
988 pending_entry_->GetIsOverridingUserAgent() !=
989 GetLastCommittedEntry()->GetIsOverridingUserAgent())
990 overriding_user_agent_changed = true;
[email protected]0e8db942008-09-24 21:21:48991 } else {
992 details->previous_url = GURL();
993 details->previous_entry_index = -1;
994 }
[email protected]ecd9d8702008-08-28 22:10:17995
Alexander Timind2f2e4f22019-04-02 20:04:53996 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
997 // implementing back-forward cache.
998
999 // Create a new metrics object or reuse the previous one depending on whether
1000 // it's a main frame navigation or not.
1001 scoped_refptr<BackForwardCacheMetrics> back_forward_cache_metrics =
1002 BackForwardCacheMetrics::CreateOrReuseBackForwardCacheMetrics(
1003 GetLastCommittedEntry(), !rfh->GetParent(),
1004 params.document_sequence_number);
1005 // Notify the last active entry that we have navigated away.
1006 if (!rfh->GetParent() && !is_same_document_navigation) {
1007 if (NavigationEntryImpl* navigation_entry = GetLastCommittedEntry()) {
1008 if (auto* metrics = navigation_entry->back_forward_cache_metrics()) {
1009 metrics->MainFrameDidNavigateAwayFromDocument();
1010 }
1011 }
1012 }
1013
fdegans9caf66a2015-07-30 21:10:421014 // If there is a pending entry at this point, it should have a SiteInstance,
1015 // except for restored entries.
jam48cea9082017-02-15 06:13:291016 bool was_restored = false;
toyoshim0df1d3a2016-09-09 09:52:481017 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance() ||
1018 pending_entry_->restore_type() != RestoreType::NONE);
1019 if (pending_entry_ && pending_entry_->restore_type() != RestoreType::NONE) {
1020 pending_entry_->set_restore_type(RestoreType::NONE);
jam48cea9082017-02-15 06:13:291021 was_restored = true;
toyoshim0df1d3a2016-09-09 09:52:481022 }
[email protected]e9ba4472008-09-14 15:42:431023
Nasko Oskovaee2f862018-06-15 00:05:521024 // If this is a navigation to a matching pending_entry_ and the SiteInstance
1025 // has changed, this must be treated as a new navigation with replacement.
1026 // Set the replacement bit here and ClassifyNavigation will identify this
1027 // case and return NEW_PAGE.
1028 if (!rfh->GetParent() && pending_entry_ &&
1029 pending_entry_->GetUniqueID() == params.nav_entry_id &&
1030 pending_entry_->site_instance() &&
1031 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
1032 DCHECK_NE(-1, pending_entry_index_);
1033 // TODO(nasko,creis): Instead of setting this value here, set
1034 // should_replace_current_entry on the parameters we send to the
1035 // renderer process as part of CommitNavigation. The renderer should
1036 // in turn send it back here as part of |params| and it can be just
1037 // enforced and renderer process terminated on mismatch.
1038 details->did_replace_entry = true;
1039 } else {
1040 // The renderer tells us whether the navigation replaces the current entry.
1041 details->did_replace_entry = params.should_replace_current_entry;
1042 }
[email protected]bcd904482012-02-01 01:54:221043
[email protected]e9ba4472008-09-14 15:42:431044 // Do navigation-type specific actions. These will make and commit an entry.
[email protected]27dd82fd2014-03-03 22:11:431045 details->type = ClassifyNavigation(rfh, params);
[email protected]4bf3522c2010-08-19 21:00:201046
eugenebutee08663a2017-04-27 17:43:121047 // is_same_document must be computed before the entry gets committed.
Eugene But712f03d2018-05-22 16:03:441048 details->is_same_document = is_same_document_navigation;
[email protected]b9d4dfdc2013-08-08 00:25:121049
Peter Boströmd7592132019-01-30 04:50:311050 // Make sure we do not discard the pending entry for a different ongoing
1051 // navigation when a same document commit comes in unexpectedly from the
1052 // renderer. Limit this to a very narrow set of conditions to avoid risks to
1053 // other navigation types. See https://crbug.com/900036.
1054 // TODO(crbug.com/926009): Handle history.pushState() as well.
Peter Boströmd7592132019-01-30 04:50:311055 bool keep_pending_entry = is_same_document_navigation &&
1056 details->type == NAVIGATION_TYPE_EXISTING_PAGE &&
1057 pending_entry_ &&
Mohamed Abdelhalim833de902019-09-16 17:41:451058 !PendingEntryMatchesRequest(navigation_request);
Peter Boströmd7592132019-01-30 04:50:311059
[email protected]0e8db942008-09-24 21:21:481060 switch (details->type) {
[email protected]8ff00d72012-10-23 19:12:211061 case NAVIGATION_TYPE_NEW_PAGE:
shivanisha41f04c52018-12-12 15:52:051062 RendererDidNavigateToNewPage(
1063 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451064 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431065 break;
[email protected]8ff00d72012-10-23 19:12:211066 case NAVIGATION_TYPE_EXISTING_PAGE:
eugenebutee08663a2017-04-27 17:43:121067 RendererDidNavigateToExistingPage(rfh, params, details->is_same_document,
Mohamed Abdelhalim833de902019-09-16 17:41:451068 was_restored, navigation_request,
Peter Boströmd7592132019-01-30 04:50:311069 keep_pending_entry);
[email protected]e9ba4472008-09-14 15:42:431070 break;
[email protected]8ff00d72012-10-23 19:12:211071 case NAVIGATION_TYPE_SAME_PAGE:
Joe DeBlasio531e3d92018-11-06 06:26:121072 RendererDidNavigateToSamePage(rfh, params, details->is_same_document,
Mohamed Abdelhalim833de902019-09-16 17:41:451073 navigation_request);
[email protected]e9ba4472008-09-14 15:42:431074 break;
[email protected]8ff00d72012-10-23 19:12:211075 case NAVIGATION_TYPE_NEW_SUBFRAME:
Shivani Sharmaffb32b82019-04-09 16:58:471076 RendererDidNavigateNewSubframe(
1077 rfh, params, details->is_same_document, details->did_replace_entry,
Mohamed Abdelhalim833de902019-09-16 17:41:451078 previous_document_was_activated, navigation_request);
[email protected]e9ba4472008-09-14 15:42:431079 break;
[email protected]8ff00d72012-10-23 19:12:211080 case NAVIGATION_TYPE_AUTO_SUBFRAME:
Mohamed Abdelhalim833de902019-09-16 17:41:451081 if (!RendererDidNavigateAutoSubframe(rfh, params, navigation_request)) {
creisce0ef3572017-01-26 17:53:081082 // We don't send a notification about auto-subframe PageState during
1083 // UpdateStateForFrame, since it looks like nothing has changed. Send
1084 // it here at commit time instead.
1085 NotifyEntryChanged(GetLastCommittedEntry());
[email protected]e9ba4472008-09-14 15:42:431086 return false;
creis59d5a47cb2016-08-24 23:57:191087 }
[email protected]e9ba4472008-09-14 15:42:431088 break;
[email protected]8ff00d72012-10-23 19:12:211089 case NAVIGATION_TYPE_NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:491090 // If a pending navigation was in progress, this canceled it. We should
1091 // discard it and make sure it is removed from the URL bar. After that,
1092 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:431093 // the caller that nothing has happened.
arthursonzogni69a6a1b2019-09-17 09:23:001094 if (pending_entry_)
[email protected]20d1c992011-04-12 21:17:491095 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431096 return false;
1097 default:
1098 NOTREACHED();
[email protected]765b35502008-08-21 00:51:201099 }
1100
[email protected]688aa65c62012-09-28 04:32:221101 // At this point, we know that the navigation has just completed, so
1102 // record the time.
1103 //
1104 // TODO(akalin): Use "sane time" as described in
Adam Langley4463fb832018-01-28 22:42:261105 // https://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:331106 base::Time timestamp =
1107 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
1108 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
[email protected]688aa65c62012-09-28 04:32:221109 << timestamp.ToInternalValue();
1110
Peter Boströmd7592132019-01-30 04:50:311111 // If we aren't keeping the pending entry, there shouldn't be one at this
1112 // point. Clear it again in case any error cases above forgot to do so.
1113 // TODO(pbos): Consider a CHECK here that verifies that the pending entry has
1114 // been cleared instead of protecting against it.
1115 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001116 DiscardNonCommittedEntries();
[email protected]f233e4232013-02-23 00:55:141117
[email protected]e9ba4472008-09-14 15:42:431118 // All committed entries should have nonempty content state so WebKit doesn't
1119 // get confused when we go back to them (see the function for details).
creis0cade2e2017-02-28 06:37:471120 DCHECK(params.page_state.IsValid()) << "Shouldn't see an empty PageState.";
creis3da03872015-02-20 21:12:321121 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:221122 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:441123 active_entry->SetHttpStatusCode(params.http_status_code);
Alexander Timind2f2e4f22019-04-02 20:04:531124 // TODO(altimin, crbug.com/933147): Remove this logic after we are done with
1125 // implementing back-forward cache.
1126 if (!active_entry->back_forward_cache_metrics()) {
1127 active_entry->set_back_forward_cache_metrics(
1128 std::move(back_forward_cache_metrics));
1129 }
1130 active_entry->back_forward_cache_metrics()->DidCommitNavigation(
Mohamed Abdelhalim833de902019-09-16 17:41:451131 navigation_request->GetNavigationId(), active_entry->GetUniqueID(),
1132 rfh->frame_tree_node()->IsMainFrame());
naskoc7533512016-05-06 17:01:121133
Charles Reisc0507202017-09-21 00:40:021134 // Grab the corresponding FrameNavigationEntry for a few updates, but only if
1135 // the SiteInstance matches (to avoid updating the wrong entry by mistake).
1136 // A mismatch can occur if the renderer lies or due to a unique name collision
1137 // after a race with an OOPIF (see https://crbug.com/616820).
naskoc7533512016-05-06 17:01:121138 FrameNavigationEntry* frame_entry =
1139 active_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisc0507202017-09-21 00:40:021140 if (frame_entry && frame_entry->site_instance() != rfh->GetSiteInstance())
1141 frame_entry = nullptr;
Charles Reisf44482022017-10-13 21:15:031142 // Make sure we've updated the PageState in one of the helper methods.
creisce0ef3572017-01-26 17:53:081143 // TODO(creis): Remove the "if" once https://crbug.com/522193 is fixed.
1144 if (frame_entry) {
Charles Reisf44482022017-10-13 21:15:031145 DCHECK(params.page_state == frame_entry->page_state());
creis4e2ecb72015-06-20 00:46:301146 }
[email protected]132e281a2012-07-31 18:32:441147
[email protected]97d8f0d2013-10-29 16:49:211148 // Once it is committed, we no longer need to track several pieces of state on
1149 // the entry.
naskoc7533512016-05-06 17:01:121150 active_entry->ResetForCommit(frame_entry);
[email protected]60d6cca2013-04-30 08:47:131151
shivanisha41f04c52018-12-12 15:52:051152 // It is possible that we are re-using this entry and it was marked to be
1153 // skipped on back/forward UI in its previous navigation. Reset it here so
1154 // that it is set afresh, if applicable, for this navigation.
1155 active_entry->set_should_skip_on_back_forward_ui(false);
1156
[email protected]49bd30e62011-03-22 20:12:591157 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:221158 // TODO(creis): This check won't pass for subframes until we create entries
1159 // for subframe navigations.
avi39c1edd32015-06-04 20:06:001160 if (!rfh->GetParent())
creis77c9aa32015-09-25 19:59:421161 CHECK_EQ(active_entry->site_instance(), rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:591162
[email protected]b26de072013-02-23 02:33:441163 // Remember the bindings the renderer process has at this point, so that
1164 // we do not grant this entry additional bindings if we come back to it.
creis3da03872015-02-20 21:12:321165 active_entry->SetBindings(rfh->GetEnabledBindings());
[email protected]b26de072013-02-23 02:33:441166
[email protected]e9ba4472008-09-14 15:42:431167 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:001168 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:001169 details->is_main_frame = !rfh->GetParent();
[email protected]2e39d2e2009-02-19 18:41:311170 details->http_status_code = params.http_status_code;
estarka5635c42015-07-14 00:06:531171
[email protected]93f230e02011-06-01 14:40:001172 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:291173
John Abd-El-Malek380d3c5922017-09-08 00:20:311174 if (active_entry->GetURL().SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451175 navigation_request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek969cdd94e2017-07-10 22:18:161176 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus",
1177 !!active_entry->GetSSL().certificate);
1178 }
1179
aelias100c9192017-01-13 00:01:431180 if (overriding_user_agent_changed)
1181 delegate_->UpdateOverridingUserAgent();
1182
creis03b48002015-11-04 00:54:561183 // Update the nav_entry_id for each RenderFrameHost in the tree, so that each
1184 // one knows the latest NavigationEntry it is showing (whether it has
1185 // committed anything in this navigation or not). This allows things like
1186 // state and title updates from RenderFrames to apply to the latest relevant
1187 // NavigationEntry.
dcheng57e39e22016-01-21 00:25:381188 int nav_entry_id = active_entry->GetUniqueID();
1189 for (FrameTreeNode* node : delegate_->GetFrameTree()->Nodes())
1190 node->current_frame_host()->set_nav_entry_id(nav_entry_id);
[email protected]e9ba4472008-09-14 15:42:431191 return true;
initial.commit09911bf2008-07-26 23:55:291192}
1193
[email protected]8ff00d72012-10-23 19:12:211194NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:321195 RenderFrameHostImpl* rfh,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571196 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
avi7c6f35e2015-05-08 17:52:381197 if (params.did_create_new_entry) {
1198 // A new entry. We may or may not have a pending entry for the page, and
1199 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001200 if (!rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381201 return NAVIGATION_TYPE_NEW_PAGE;
1202 }
1203
1204 // When this is a new subframe navigation, we should have a committed page
1205 // in which it's a subframe. This may not be the case when an iframe is
1206 // navigated on a popup navigated to about:blank (the iframe would be
1207 // written into the popup by script on the main page). For these cases,
1208 // there isn't any navigation stuff we can do, so just ignore it.
avib1986b12015-06-11 20:21:231209 if (!GetLastCommittedEntry())
avi7c6f35e2015-05-08 17:52:381210 return NAVIGATION_TYPE_NAV_IGNORE;
1211
1212 // Valid subframe navigation.
1213 return NAVIGATION_TYPE_NEW_SUBFRAME;
1214 }
1215
1216 // We only clear the session history when navigating to a new page.
1217 DCHECK(!params.history_list_was_cleared);
1218
avi39c1edd32015-06-04 20:06:001219 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381220 // All manual subframes would be did_create_new_entry and handled above, so
1221 // we know this is auto.
1222 if (GetLastCommittedEntry()) {
1223 return NAVIGATION_TYPE_AUTO_SUBFRAME;
1224 } else {
1225 // We ignore subframes created in non-committed pages; we'd appreciate if
1226 // people stopped doing that.
1227 return NAVIGATION_TYPE_NAV_IGNORE;
1228 }
1229 }
1230
1231 if (params.nav_entry_id == 0) {
1232 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1233 // create a new page.
1234
1235 // Just like above in the did_create_new_entry case, it's possible to
1236 // scribble onto an uncommitted page. Again, there isn't any navigation
1237 // stuff that we can do, so ignore it here as well.
avi3a5b8f32015-05-28 17:50:231238 NavigationEntry* last_committed = GetLastCommittedEntry();
avib1986b12015-06-11 20:21:231239 if (!last_committed)
avi7c6f35e2015-05-08 17:52:381240 return NAVIGATION_TYPE_NAV_IGNORE;
1241
Charles Reis1378111f2017-11-08 21:44:061242 // This is history.replaceState() or history.reload().
Nasko Oskov332593c2018-08-16 17:21:341243 // TODO(nasko): With error page isolation, reloading an existing session
1244 // history entry can result in change of SiteInstance. Check for such a case
1245 // here and classify it as NEW_PAGE, as such navigations should be treated
1246 // as new with replacement.
avi259dc792015-07-07 04:42:361247 return NAVIGATION_TYPE_EXISTING_PAGE;
avi7c6f35e2015-05-08 17:52:381248 }
1249
Nasko Oskovaee2f862018-06-15 00:05:521250 if (pending_entry_ && pending_entry_->GetUniqueID() == params.nav_entry_id) {
1251 // If the SiteInstance of the |pending_entry_| does not match the
1252 // SiteInstance that got committed, treat this as a new navigation with
1253 // replacement. This can happen if back/forward/reload encounters a server
1254 // redirect to a different site or an isolated error page gets successfully
1255 // reloaded into a different SiteInstance.
1256 if (pending_entry_->site_instance() &&
1257 pending_entry_->site_instance() != rfh->GetSiteInstance()) {
creis77c9aa32015-09-25 19:59:421258 return NAVIGATION_TYPE_NEW_PAGE;
Nasko Oskovaee2f862018-06-15 00:05:521259 }
creis77c9aa32015-09-25 19:59:421260
Nasko Oskovaee2f862018-06-15 00:05:521261 if (pending_entry_index_ == -1) {
1262 // In this case, we have a pending entry for a load of a new URL but Blink
1263 // didn't do a new navigation (params.did_create_new_entry). First check
1264 // to make sure Blink didn't treat a new cross-process navigation as
1265 // inert, and thus set params.did_create_new_entry to false. In that case,
1266 // we must treat it as NEW since the SiteInstance doesn't match the entry.
1267 if (!GetLastCommittedEntry() ||
1268 GetLastCommittedEntry()->site_instance() != rfh->GetSiteInstance()) {
1269 return NAVIGATION_TYPE_NEW_PAGE;
1270 }
1271
1272 // Otherwise, this happens when you press enter in the URL bar to reload.
1273 // We will create a pending entry, but Blink will convert it to a reload
1274 // since it's the same page and not create a new entry for it (the user
1275 // doesn't want to have a new back/forward entry when they do this).
1276 // Therefore we want to just ignore the pending entry and go back to where
1277 // we were (the "existing entry").
1278 // TODO(creis,avi): Eliminate SAME_PAGE in https://crbug.com/536102.
1279 return NAVIGATION_TYPE_SAME_PAGE;
1280 }
avi7c6f35e2015-05-08 17:52:381281 }
1282
creis26d22632017-04-21 20:23:561283 // Everything below here is assumed to be an existing entry, but if there is
1284 // no last committed entry, we must consider it a new navigation instead.
1285 if (!GetLastCommittedEntry())
1286 return NAVIGATION_TYPE_NEW_PAGE;
1287
avi7c6f35e2015-05-08 17:52:381288 if (params.intended_as_new_entry) {
1289 // This was intended to be a navigation to a new entry but the pending entry
1290 // got cleared in the meanwhile. Classify as EXISTING_PAGE because we may or
1291 // may not have a pending entry.
1292 return NAVIGATION_TYPE_EXISTING_PAGE;
1293 }
1294
1295 if (params.url_is_unreachable && failed_pending_entry_id_ != 0 &&
1296 params.nav_entry_id == failed_pending_entry_id_) {
1297 // If the renderer was going to a new pending entry that got cleared because
1298 // of an error, this is the case of the user trying to retry a failed load
1299 // by pressing return. Classify as EXISTING_PAGE because we probably don't
1300 // have a pending entry.
1301 return NAVIGATION_TYPE_EXISTING_PAGE;
1302 }
1303
1304 // Now we know that the notification is for an existing page. Find that entry.
1305 int existing_entry_index = GetEntryIndexWithUniqueID(params.nav_entry_id);
1306 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441307 // The renderer has committed a navigation to an entry that no longer
1308 // exists. Because the renderer is showing that page, resurrect that entry.
1309 return NAVIGATION_TYPE_NEW_PAGE;
avi7c6f35e2015-05-08 17:52:381310 }
1311
avi7c6f35e2015-05-08 17:52:381312 // Since we weeded out "new" navigations above, we know this is an existing
1313 // (back/forward) navigation.
1314 return NAVIGATION_TYPE_EXISTING_PAGE;
1315}
1316
[email protected]d202a7c2012-01-04 07:53:471317void NavigationControllerImpl::RendererDidNavigateToNewPage(
creis3da03872015-02-20 21:12:321318 RenderFrameHostImpl* rfh,
[email protected]37567b432014-02-12 01:12:221319 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
eugenebut604866f2017-05-10 21:35:361320 bool is_same_document,
clamy3bf35e3c2016-11-10 15:59:441321 bool replace_entry,
Shivani Sharmaffb32b82019-04-09 16:58:471322 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451323 NavigationRequest* request) {
dcheng9bfa5162016-04-09 01:00:571324 std::unique_ptr<NavigationEntryImpl> new_entry;
creisf49dfc92016-07-26 17:05:181325 bool update_virtual_url = false;
1326
Lukasz Anforowicz435bcb582019-07-12 20:50:061327 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451328 request->common_params().initiator_origin;
Lukasz Anforowicz435bcb582019-07-12 20:50:061329
creisf49dfc92016-07-26 17:05:181330 // First check if this is an in-page navigation. If so, clone the current
1331 // entry instead of looking at the pending entry, because the pending entry
1332 // does not have any subframe history items.
eugenebut604866f2017-05-10 21:35:361333 if (is_same_document && GetLastCommittedEntry()) {
Patrick Monette50e8bd82019-06-13 22:40:451334 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481335 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
creisf49dfc92016-07-26 17:05:181336 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Nasko Oskov03912102019-01-11 00:21:321337 params.url, (params.url_is_unreachable) ? nullptr : &params.origin,
Lukasz Anforowicz435bcb582019-07-12 20:50:061338 params.referrer, initiator_origin, params.redirects, params.page_state,
1339 params.method, params.post_id, nullptr /* blob_url_loader_factory */);
Charles Reisf44482022017-10-13 21:15:031340
creisf49dfc92016-07-26 17:05:181341 new_entry = GetLastCommittedEntry()->CloneAndReplace(
1342 frame_entry, true, rfh->frame_tree_node(),
1343 delegate_->GetFrameTree()->root());
jama78746e2017-02-22 17:21:571344 if (new_entry->GetURL().GetOrigin() != params.url.GetOrigin()) {
1345 // TODO(jam): we had one report of this with a URL that was redirecting to
1346 // only tildes. Until we understand that better, don't copy the cert in
1347 // this case.
1348 new_entry->GetSSL() = SSLStatus();
John Abd-El-Malek9cd697e2017-07-12 21:53:141349
John Abd-El-Malek380d3c5922017-09-08 00:20:311350 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451351 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141352 UMA_HISTOGRAM_BOOLEAN(
1353 "Navigation.SecureSchemeHasSSLStatus.NewPageInPageOriginMismatch",
1354 !!new_entry->GetSSL().certificate);
1355 }
jama78746e2017-02-22 17:21:571356 }
creisf49dfc92016-07-26 17:05:181357
Patrick Monette50e8bd82019-06-13 22:40:451358 // It is expected that |frame_entry| is now owned by |new_entry|. This means
1359 // that |frame_entry| should now have a reference count of exactly 2: one
1360 // due to the local variable |frame_entry|, and another due to |new_entry|
1361 // also retaining one. This should never fail, because it's the main frame.
1362 CHECK(!frame_entry->HasOneRef() && frame_entry->HasAtLeastOneRef());
creisf49dfc92016-07-26 17:05:181363
1364 update_virtual_url = new_entry->update_virtual_url_with_url();
John Abd-El-Malek9cd697e2017-07-12 21:53:141365
John Abd-El-Malek380d3c5922017-09-08 00:20:311366 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451367 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141368 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus.NewPageInPage",
1369 !!new_entry->GetSSL().certificate);
1370 }
creisf49dfc92016-07-26 17:05:181371 }
1372
[email protected]6dd86ab2013-02-27 00:30:341373 // Only make a copy of the pending entry if it is appropriate for the new page
csharrison9a9142bc42016-03-01 17:24:041374 // that was just loaded. Verify this by checking if the entry corresponds
Mohamed Abdelhalim833de902019-09-16 17:41:451375 // to the given NavigationRequest. Additionally, coarsely check that:
csharrison9a9142bc42016-03-01 17:24:041376 // 1. The SiteInstance hasn't been assigned to something else.
1377 // 2. The pending entry was intended as a new entry, rather than being a
1378 // history navigation that was interrupted by an unrelated,
1379 // renderer-initiated navigation.
1380 // TODO(csharrison): Investigate whether we can remove some of the coarser
1381 // checks.
Mohamed Abdelhalim833de902019-09-16 17:41:451382 if (!new_entry && PendingEntryMatchesRequest(request) &&
1383 pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341384 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431385 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351386 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431387
[email protected]f1eb87a2011-05-06 17:49:411388 update_virtual_url = new_entry->update_virtual_url_with_url();
Camille Lamy62b826012019-02-26 09:15:471389 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451390 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141391
John Abd-El-Malek380d3c5922017-09-08 00:20:311392 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451393 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141394 UMA_HISTOGRAM_BOOLEAN(
1395 "Navigation.SecureSchemeHasSSLStatus.NewPagePendingEntryMatches",
1396 !!new_entry->GetSSL().certificate);
1397 }
creisf49dfc92016-07-26 17:05:181398 }
1399
1400 // For non-in-page commits with no matching pending entry, create a new entry.
1401 if (!new_entry) {
Lukasz Anforowicz435bcb582019-07-12 20:50:061402 new_entry = std::make_unique<NavigationEntryImpl>(
1403 rfh->GetSiteInstance(), params.url, params.referrer, initiator_origin,
1404 base::string16(), // title
Mohamed Abdelhalim833de902019-09-16 17:41:451405 params.transition, request->IsRendererInitiated(),
Lukasz Anforowicz435bcb582019-07-12 20:50:061406 nullptr); // blob_url_loader_factory
[email protected]f8f93eb2012-09-25 03:06:241407
1408 // Find out whether the new entry needs to update its virtual URL on URL
1409 // change and set up the entry accordingly. This is needed to correctly
1410 // update the virtual URL when replaceState is called after a pushState.
1411 GURL url = params.url;
1412 bool needs_update = false;
[email protected]47752982014-07-29 08:01:431413 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1414 &url, browser_context_, &needs_update);
[email protected]f8f93eb2012-09-25 03:06:241415 new_entry->set_update_virtual_url_with_url(needs_update);
1416
[email protected]f1eb87a2011-05-06 17:49:411417 // When navigating to a new page, give the browser URL handler a chance to
1418 // update the virtual URL based on the new URL. For example, this is needed
1419 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1420 // the URL.
[email protected]f8f93eb2012-09-25 03:06:241421 update_virtual_url = needs_update;
Camille Lamy62b826012019-02-26 09:15:471422 new_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451423 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141424
John Abd-El-Malek380d3c5922017-09-08 00:20:311425 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451426 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141427 UMA_HISTOGRAM_BOOLEAN(
1428 "Navigation.SecureSchemeHasSSLStatus.NewPageNoMatchingEntry",
1429 !!new_entry->GetSSL().certificate);
1430 }
[email protected]e9ba4472008-09-14 15:42:431431 }
1432
wjmaclean7431bb22015-02-19 14:53:431433 // Don't use the page type from the pending entry. Some interstitial page
1434 // may have set the type to interstitial. Once we commit, however, the page
1435 // type must always be normal or error.
1436 new_entry->set_page_type(params.url_is_unreachable ? PAGE_TYPE_ERROR
1437 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041438 new_entry->SetURL(params.url);
[email protected]f1eb87a2011-05-06 17:49:411439 if (update_virtual_url)
avi25764702015-06-23 15:43:371440 UpdateVirtualURLToURL(new_entry.get(), params.url);
[email protected]022af742011-12-28 18:37:251441 new_entry->SetReferrer(params.referrer);
[email protected]022af742011-12-28 18:37:251442 new_entry->SetTransitionType(params.transition);
[email protected]b6583592012-01-25 19:52:331443 new_entry->set_site_instance(
[email protected]27dd82fd2014-03-03 22:11:431444 static_cast<SiteInstanceImpl*>(rfh->GetSiteInstance()));
[email protected]7c16976c2012-08-04 02:38:231445 new_entry->SetOriginalRequestURL(params.original_request_url);
[email protected]bf70edce2012-06-20 22:32:221446 new_entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
[email protected]e9ba4472008-09-14 15:42:431447
creis8b5cd4c2015-06-19 00:11:081448 // Update the FrameNavigationEntry for new main frame commits.
1449 FrameNavigationEntry* frame_entry =
1450 new_entry->GetFrameEntry(rfh->frame_tree_node());
Charles Reisd2a509f2017-09-27 23:47:481451 frame_entry->set_frame_unique_name(rfh->frame_tree_node()->unique_name());
creis8b5cd4c2015-06-19 00:11:081452 frame_entry->set_item_sequence_number(params.item_sequence_number);
1453 frame_entry->set_document_sequence_number(params.document_sequence_number);
Charles Reisf44482022017-10-13 21:15:031454 frame_entry->set_redirect_chain(params.redirects);
1455 frame_entry->SetPageState(params.page_state);
clamy432acb22016-04-15 19:41:431456 frame_entry->set_method(params.method);
1457 frame_entry->set_post_id(params.post_id);
Nasko Oskov03912102019-01-11 00:21:321458 if (!params.url_is_unreachable)
1459 frame_entry->set_committed_origin(params.origin);
creis8b5cd4c2015-06-19 00:11:081460
eugenebut604866f2017-05-10 21:35:361461 // history.pushState() is classified as a navigation to a new page, but sets
1462 // is_same_document to true. In this case, we already have the title and
creisf49dfc92016-07-26 17:05:181463 // favicon available, so set them immediately.
eugenebut604866f2017-05-10 21:35:361464 if (is_same_document && GetLastCommittedEntry()) {
[email protected]ff64b3e2014-05-31 04:07:331465 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
[email protected]3a868f212014-08-09 10:41:191466 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1467 }
[email protected]ff64b3e2014-05-31 04:07:331468
[email protected]60d6cca2013-04-30 08:47:131469 DCHECK(!params.history_list_was_cleared || !replace_entry);
1470 // The browser requested to clear the session history when it initiated the
1471 // navigation. Now we know that the renderer has updated its state accordingly
1472 // and it is safe to also clear the browser side history.
1473 if (params.history_list_was_cleared) {
arthursonzogni69a6a1b2019-09-17 09:23:001474 DiscardNonCommittedEntries();
[email protected]60d6cca2013-04-30 08:47:131475 entries_.clear();
1476 last_committed_entry_index_ = -1;
1477 }
1478
Nasko Oskovaee2f862018-06-15 00:05:521479 // If this is a new navigation with replacement and there is a
1480 // pending_entry_ which matches the navigation reported by the renderer
1481 // process, then it should be the one replaced, so update the
1482 // last_committed_entry_index_ to use it.
1483 if (replace_entry && pending_entry_index_ != -1 &&
1484 pending_entry_->GetUniqueID() == params.nav_entry_id) {
1485 last_committed_entry_index_ = pending_entry_index_;
1486 }
1487
Shivani Sharmaffb32b82019-04-09 16:58:471488 SetShouldSkipOnBackForwardUIIfNeeded(rfh, replace_entry,
1489 previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451490 request->IsRendererInitiated());
shivanisha41f04c52018-12-12 15:52:051491
dcheng36b6aec92015-12-26 06:16:361492 InsertOrReplaceEntry(std::move(new_entry), replace_entry);
[email protected]e9ba4472008-09-14 15:42:431493}
1494
[email protected]d202a7c2012-01-04 07:53:471495void NavigationControllerImpl::RendererDidNavigateToExistingPage(
creis3da03872015-02-20 21:12:321496 RenderFrameHostImpl* rfh,
jam0576b132016-09-07 05:13:101497 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
eugenebut604866f2017-05-10 21:35:361498 bool is_same_document,
jam48cea9082017-02-15 06:13:291499 bool was_restored,
Mohamed Abdelhalim833de902019-09-16 17:41:451500 NavigationRequest* request,
Peter Boströmd7592132019-01-30 04:50:311501 bool keep_pending_entry) {
creis26d22632017-04-21 20:23:561502 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1503 << "that a last committed entry exists.";
1504
[email protected]e9ba4472008-09-14 15:42:431505 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001506 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431507
avicbdc4c12015-07-01 16:07:111508 NavigationEntryImpl* entry;
1509 if (params.intended_as_new_entry) {
1510 // This was intended as a new entry but the pending entry was lost in the
1511 // meanwhile and no new page was created. We are stuck at the last committed
1512 // entry.
1513 entry = GetLastCommittedEntry();
eugenebut604866f2017-05-10 21:35:361514 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451515 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361516 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471517 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451518 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141519
John Abd-El-Malek380d3c5922017-09-08 00:20:311520 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451521 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141522 bool has_cert = !!entry->GetSSL().certificate;
1523 if (is_same_document) {
1524 UMA_HISTOGRAM_BOOLEAN(
1525 "Navigation.SecureSchemeHasSSLStatus."
1526 "ExistingPageSameDocumentIntendedAsNew",
1527 has_cert);
1528 } else {
1529 UMA_HISTOGRAM_BOOLEAN(
1530 "Navigation.SecureSchemeHasSSLStatus."
1531 "ExistingPageDifferentDocumentIntendedAsNew",
1532 has_cert);
1533 }
1534 }
avicbdc4c12015-07-01 16:07:111535 } else if (params.nav_entry_id) {
1536 // This is a browser-initiated navigation (back/forward/reload).
1537 entry = GetEntryWithUniqueID(params.nav_entry_id);
jamd208b90ce2016-09-01 16:58:161538
eugenebut604866f2017-05-10 21:35:361539 if (is_same_document) {
Mohamed Abdelhalim833de902019-09-16 17:41:451540 // There's no SSLStatus in the NavigationRequest for same document
eugenebut604866f2017-05-10 21:35:361541 // navigations, so normally we leave |entry|'s SSLStatus as is. However if
1542 // this was a restored same document navigation entry, then it won't have
1543 // an SSLStatus. So we need to copy over the SSLStatus from the entry that
1544 // navigated it.
jam48cea9082017-02-15 06:13:291545 NavigationEntryImpl* last_entry = GetLastCommittedEntry();
1546 if (entry->GetURL().GetOrigin() == last_entry->GetURL().GetOrigin() &&
1547 last_entry->GetSSL().initialized && !entry->GetSSL().initialized &&
1548 was_restored) {
1549 entry->GetSSL() = last_entry->GetSSL();
1550 }
1551 } else {
Mohamed Abdelhalim833de902019-09-16 17:41:451552 // In rapid back/forward navigations |request| sometimes won't have a cert
1553 // (http://crbug.com/727892). So we use the request's cert if it exists,
John Abd-El-Malek3f247082017-12-07 19:02:191554 // otherwise we only reuse the existing cert if the origins match.
Mohamed Abdelhalim833de902019-09-16 17:41:451555 if (request->GetSSLInfo().has_value() &&
1556 request->GetSSLInfo()->is_valid()) {
1557 entry->GetSSL() = SSLStatus(*(request->GetSSLInfo()));
1558 } else if (entry->GetURL().GetOrigin() != request->GetURL().GetOrigin()) {
John Abd-El-Malek3f247082017-12-07 19:02:191559 entry->GetSSL() = SSLStatus();
1560 }
jam48cea9082017-02-15 06:13:291561 }
John Abd-El-Malek9cd697e2017-07-12 21:53:141562
John Abd-El-Malek380d3c5922017-09-08 00:20:311563 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451564 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141565 bool has_cert = !!entry->GetSSL().certificate;
1566 if (is_same_document && was_restored) {
1567 UMA_HISTOGRAM_BOOLEAN(
1568 "Navigation.SecureSchemeHasSSLStatus."
1569 "ExistingPageSameDocumentRestoredBrowserInitiated",
1570 has_cert);
1571 } else if (is_same_document && !was_restored) {
1572 UMA_HISTOGRAM_BOOLEAN(
1573 "Navigation.SecureSchemeHasSSLStatus."
1574 "ExistingPageSameDocumentBrowserInitiated",
1575 has_cert);
1576 } else if (!is_same_document && was_restored) {
1577 UMA_HISTOGRAM_BOOLEAN(
1578 "Navigation.SecureSchemeHasSSLStatus."
1579 "ExistingPageRestoredBrowserInitiated",
1580 has_cert);
1581 } else {
1582 UMA_HISTOGRAM_BOOLEAN(
1583 "Navigation.SecureSchemeHasSSLStatus.ExistingPageBrowserInitiated",
1584 has_cert);
1585 }
1586 }
avicbdc4c12015-07-01 16:07:111587 } else {
1588 // This is renderer-initiated. The only kinds of renderer-initated
Charles Reis1378111f2017-11-08 21:44:061589 // navigations that are EXISTING_PAGE are reloads and history.replaceState,
avicbdc4c12015-07-01 16:07:111590 // which land us at the last committed entry.
1591 entry = GetLastCommittedEntry();
jam0576b132016-09-07 05:13:101592
Mikel Astizba9cf2fd2017-12-17 10:38:101593 // TODO(crbug.com/751023): Set page transition type to PAGE_TRANSITION_LINK
1594 // to avoid misleading interpretations (e.g. URLs paired with
1595 // PAGE_TRANSITION_TYPED that haven't actually been typed) as well as to fix
1596 // the inconsistency with what we report to observers (PAGE_TRANSITION_LINK
1597 // | PAGE_TRANSITION_CLIENT_REDIRECT).
1598
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571599 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(entry, entry);
Mikel Astizba9cf2fd2017-12-17 10:38:101600
eugenebut604866f2017-05-10 21:35:361601 // If this is a same document navigation, then there's no SSLStatus in the
Mohamed Abdelhalim833de902019-09-16 17:41:451602 // NavigationRequest so don't overwrite the existing entry's SSLStatus.
eugenebut604866f2017-05-10 21:35:361603 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471604 entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451605 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
John Abd-El-Malek9cd697e2017-07-12 21:53:141606
John Abd-El-Malek380d3c5922017-09-08 00:20:311607 if (params.url.SchemeIs(url::kHttpsScheme) && !rfh->GetParent() &&
Mohamed Abdelhalim833de902019-09-16 17:41:451608 request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141609 bool has_cert = !!entry->GetSSL().certificate;
1610 if (is_same_document) {
1611 UMA_HISTOGRAM_BOOLEAN(
1612 "Navigation.SecureSchemeHasSSLStatus."
1613 "ExistingPageSameDocumentRendererInitiated",
1614 has_cert);
1615 } else {
1616 UMA_HISTOGRAM_BOOLEAN(
1617 "Navigation.SecureSchemeHasSSLStatus."
1618 "ExistingPageDifferentDocumentRendererInitiated",
1619 has_cert);
1620 }
1621 }
avicbdc4c12015-07-01 16:07:111622 }
1623 DCHECK(entry);
[email protected]e9ba4472008-09-14 15:42:431624
[email protected]5ccd4dc2012-10-24 02:28:141625 // The URL may have changed due to redirects.
wjmaclean7431bb22015-02-19 14:53:431626 entry->set_page_type(params.url_is_unreachable ? PAGE_TYPE_ERROR
1627 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041628 entry->SetURL(params.url);
[email protected]25cd5f12014-01-10 11:02:201629 entry->SetReferrer(params.referrer);
[email protected]38178a42009-12-17 18:58:321630 if (entry->update_virtual_url_with_url())
1631 UpdateVirtualURLToURL(entry, params.url);
[email protected]5ccd4dc2012-10-24 02:28:141632
jam015ba062017-01-06 21:17:001633 // The site instance will normally be the same except
1634 // 1) session restore, when no site instance will be assigned or
1635 // 2) redirect, when the site instance is reset.
naskoaf182192016-08-11 02:12:011636 DCHECK(entry->site_instance() == nullptr ||
jam015ba062017-01-06 21:17:001637 !entry->GetRedirectChain().empty() ||
naskoaf182192016-08-11 02:12:011638 entry->site_instance() == rfh->GetSiteInstance());
clamy432acb22016-04-15 19:41:431639
naskoaf182192016-08-11 02:12:011640 // Update the existing FrameNavigationEntry to ensure all of its members
1641 // reflect the parameters coming from the renderer process.
Lukasz Anforowicz435bcb582019-07-12 20:50:061642 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451643 request->common_params().initiator_origin;
naskoaf182192016-08-11 02:12:011644 entry->AddOrUpdateFrameEntry(
1645 rfh->frame_tree_node(), params.item_sequence_number,
1646 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Nasko Oskov03912102019-01-11 00:21:321647 params.url, GetCommittedOriginForFrameEntry(params), params.referrer,
Lukasz Anforowicz435bcb582019-07-12 20:50:061648 initiator_origin, params.redirects, params.page_state, params.method,
1649 params.post_id, nullptr /* blob_url_loader_factory */);
creis22a7b4c2016-04-28 07:20:301650
[email protected]5ccd4dc2012-10-24 02:28:141651 // The redirected to page should not inherit the favicon from the previous
1652 // page.
eugenebut604866f2017-05-10 21:35:361653 if (ui::PageTransitionIsRedirect(params.transition) && !is_same_document)
[email protected]91a4ff82012-10-29 20:29:481654 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:141655
Peter Boströmd7592132019-01-30 04:50:311656 // We should also usually discard the pending entry if it corresponds to a
1657 // different navigation, since that one is now likely canceled. In rare
1658 // cases, we leave the pending entry for another navigation in place when we
1659 // know it is still ongoing, to avoid a flicker in the omnibox (see
1660 // https://crbug.com/900036).
[email protected]e9ba4472008-09-14 15:42:431661 //
1662 // Note that we need to use the "internal" version since we don't want to
1663 // actually change any other state, just kill the pointer.
Peter Boströmd7592132019-01-30 04:50:311664 if (!keep_pending_entry)
arthursonzogni69a6a1b2019-09-17 09:23:001665 DiscardNonCommittedEntries();
[email protected]40bcc302009-03-02 20:50:391666
[email protected]80858db52009-10-15 00:35:181667 // If a transient entry was removed, the indices might have changed, so we
1668 // have to query the entry index again.
avicbdc4c12015-07-01 16:07:111669 last_committed_entry_index_ = GetIndexOfEntry(entry);
[email protected]e9ba4472008-09-14 15:42:431670}
1671
[email protected]d202a7c2012-01-04 07:53:471672void NavigationControllerImpl::RendererDidNavigateToSamePage(
creis3da03872015-02-20 21:12:321673 RenderFrameHostImpl* rfh,
clamy3bf35e3c2016-11-10 15:59:441674 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
Joe DeBlasio531e3d92018-11-06 06:26:121675 bool is_same_document,
Mohamed Abdelhalim833de902019-09-16 17:41:451676 NavigationRequest* request) {
aviaf5660962015-06-30 15:12:291677 // This classification says that we have a pending entry that's the same as
1678 // the last committed entry. This entry is guaranteed to exist by
[email protected]4c27ba82008-09-24 16:49:091679 // ClassifyNavigation. All we need to do is update the existing entry.
aviaf5660962015-06-30 15:12:291680 NavigationEntryImpl* existing_entry = GetLastCommittedEntry();
[email protected]e9ba4472008-09-14 15:42:431681
creis77c9aa32015-09-25 19:59:421682 // If we classified this correctly, the SiteInstance should not have changed.
1683 CHECK_EQ(existing_entry->site_instance(), rfh->GetSiteInstance());
1684
[email protected]e9ba4472008-09-14 15:42:431685 // We assign the entry's unique ID to be that of the new one. Since this is
1686 // always the result of a user action, we want to dismiss infobars, etc. like
1687 // a regular user-initiated navigation.
creis77c9aa32015-09-25 19:59:421688 DCHECK_EQ(pending_entry_->GetUniqueID(), params.nav_entry_id);
[email protected]36fc0392011-12-25 03:59:511689 existing_entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]e9ba4472008-09-14 15:42:431690
[email protected]a0e69262009-06-03 19:08:481691 // The URL may have changed due to redirects.
wjmaclean7431bb22015-02-19 14:53:431692 existing_entry->set_page_type(params.url_is_unreachable ? PAGE_TYPE_ERROR
1693 : PAGE_TYPE_NORMAL);
[email protected]38178a42009-12-17 18:58:321694 if (existing_entry->update_virtual_url_with_url())
1695 UpdateVirtualURLToURL(existing_entry, params.url);
[email protected]ad23a092011-12-28 07:02:041696 existing_entry->SetURL(params.url);
[email protected]a0e69262009-06-03 19:08:481697
jamd208b90ce2016-09-01 16:58:161698 // If a user presses enter in the omnibox and the server redirects, the URL
Joe DeBlasio531e3d92018-11-06 06:26:121699 // might change (but it's still considered a SAME_PAGE navigation), so we must
1700 // update the SSL status if we perform a network request (e.g. a
1701 // non-same-document navigation). Requests that don't result in a network
1702 // request do not have a valid SSL status, but since the document didn't
1703 // change, the previous SSLStatus is still valid.
1704 if (!is_same_document)
Camille Lamy62b826012019-02-26 09:15:471705 existing_entry->GetSSL() =
Mohamed Abdelhalim833de902019-09-16 17:41:451706 SSLStatus(request->GetSSLInfo().value_or(net::SSLInfo()));
jamd208b90ce2016-09-01 16:58:161707
John Abd-El-Malek509dfd62017-09-05 21:34:491708 if (existing_entry->GetURL().SchemeIs(url::kHttpsScheme) &&
Mohamed Abdelhalim833de902019-09-16 17:41:451709 !rfh->GetParent() && request->GetNetErrorCode() == net::OK) {
John Abd-El-Malek9cd697e2017-07-12 21:53:141710 UMA_HISTOGRAM_BOOLEAN("Navigation.SecureSchemeHasSSLStatus.SamePage",
1711 !!existing_entry->GetSSL().certificate);
1712 }
1713
jianlid26f6c92016-10-12 21:03:171714 // The extra headers may have changed due to reloading with different headers.
1715 existing_entry->set_extra_headers(pending_entry_->extra_headers());
1716
naskoaf182192016-08-11 02:12:011717 // Update the existing FrameNavigationEntry to ensure all of its members
1718 // reflect the parameters coming from the renderer process.
Lukasz Anforowicz435bcb582019-07-12 20:50:061719 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451720 request->common_params().initiator_origin;
naskoaf182192016-08-11 02:12:011721 existing_entry->AddOrUpdateFrameEntry(
1722 rfh->frame_tree_node(), params.item_sequence_number,
1723 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Nasko Oskov03912102019-01-11 00:21:321724 params.url, GetCommittedOriginForFrameEntry(params), params.referrer,
Lukasz Anforowicz435bcb582019-07-12 20:50:061725 initiator_origin, params.redirects, params.page_state, params.method,
1726 params.post_id, nullptr /* blob_url_loader_factory */);
[email protected]b77686522013-12-11 20:34:191727
[email protected]cbab76d2008-10-13 22:42:471728 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431729}
1730
[email protected]d202a7c2012-01-04 07:53:471731void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:321732 RenderFrameHostImpl* rfh,
creis1857908a2016-02-25 20:31:521733 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
eugenebut604866f2017-05-10 21:35:361734 bool is_same_document,
Shivani Sharmaffb32b82019-04-09 16:58:471735 bool replace_entry,
1736 bool previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451737 NavigationRequest* request) {
avi25f5f9e2015-07-17 20:08:261738 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1739 ui::PAGE_TRANSITION_MANUAL_SUBFRAME));
[email protected]09b8f82f2009-06-16 20:22:111740
[email protected]e9ba4472008-09-14 15:42:431741 // Manual subframe navigations just get the current entry cloned so the user
1742 // can go back or forward to it. The actual subframe information will be
1743 // stored in the page state for each of those entries. This happens out of
1744 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:091745 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1746 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:381747
Mikel Astizba9cf2fd2017-12-17 10:38:101748 // The DCHECK below documents the fact that we don't know of any situation
1749 // where |replace_entry| is true for subframe navigations. This simplifies
1750 // reasoning about the replacement struct for subframes (see
1751 // CopyReplacedNavigationEntryDataIfPreviouslyEmpty()).
1752 DCHECK(!replace_entry);
1753
Patrick Monette50e8bd82019-06-13 22:40:451754 // This FrameNavigationEntry might not end up being used in the
1755 // CloneAndReplace() call below, if a spot can't be found for it in the tree.
Lukasz Anforowicz435bcb582019-07-12 20:50:061756 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451757 request->common_params().initiator_origin;
Patrick Monette50e8bd82019-06-13 22:40:451758 auto frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
Charles Reisd2a509f2017-09-27 23:47:481759 rfh->frame_tree_node()->unique_name(), params.item_sequence_number,
creisce0ef3572017-01-26 17:53:081760 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Nasko Oskov03912102019-01-11 00:21:321761 params.url, (params.url_is_unreachable) ? nullptr : &params.origin,
Lukasz Anforowicz435bcb582019-07-12 20:50:061762 params.referrer, initiator_origin, params.redirects, params.page_state,
1763 params.method, params.post_id, nullptr /* blob_url_loader_factory */);
Charles Reisf44482022017-10-13 21:15:031764
creisce0ef3572017-01-26 17:53:081765 std::unique_ptr<NavigationEntryImpl> new_entry =
1766 GetLastCommittedEntry()->CloneAndReplace(
Patrick Monette50e8bd82019-06-13 22:40:451767 std::move(frame_entry), is_same_document, rfh->frame_tree_node(),
creisce0ef3572017-01-26 17:53:081768 delegate_->GetFrameTree()->root());
creise062d542015-08-25 02:01:551769
Shivani Sharmaffb32b82019-04-09 16:58:471770 SetShouldSkipOnBackForwardUIIfNeeded(rfh, replace_entry,
1771 previous_document_was_activated,
Mohamed Abdelhalim833de902019-09-16 17:41:451772 request->IsRendererInitiated());
Shivani Sharmaffb32b82019-04-09 16:58:471773
creisce0ef3572017-01-26 17:53:081774 // TODO(creis): Update this to add the frame_entry if we can't find the one
Patrick Monette50e8bd82019-06-13 22:40:451775 // to replace, which can happen due to a unique name change. See
1776 // https://crbug.com/607205. For now, the call to CloneAndReplace() will
1777 // delete the |frame_entry| when the function exits if it doesn't get used.
creis96fc55082015-06-13 06:42:381778
creis1857908a2016-02-25 20:31:521779 InsertOrReplaceEntry(std::move(new_entry), replace_entry);
[email protected]e9ba4472008-09-14 15:42:431780}
1781
[email protected]d202a7c2012-01-04 07:53:471782bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:321783 RenderFrameHostImpl* rfh,
Lukasz Anforowicz435bcb582019-07-12 20:50:061784 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
Mohamed Abdelhalim833de902019-09-16 17:41:451785 NavigationRequest* request) {
avi9f07a0c2015-02-18 22:51:291786 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1787 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
1788
[email protected]e9ba4472008-09-14 15:42:431789 // We're guaranteed to have a previously committed entry, and we now need to
1790 // handle navigation inside of a subframe in it without creating a new entry.
1791 DCHECK(GetLastCommittedEntry());
1792
creis913c63ce2016-07-16 19:52:521793 // For newly created subframes, we don't need to send a commit notification.
1794 // This is only necessary for history navigations in subframes.
1795 bool send_commit_notification = false;
1796
1797 // If the |nav_entry_id| is non-zero and matches an existing entry, this is
1798 // a history navigation. Update the last committed index accordingly.
1799 // If we don't recognize the |nav_entry_id|, it might be a recently pruned
1800 // entry. We'll handle it below.
avi98405c22015-05-21 20:47:061801 if (params.nav_entry_id) {
caitkp420384c62015-05-28 14:34:511802 int entry_index = GetEntryIndexWithUniqueID(params.nav_entry_id);
creis3cdc3b02015-05-29 23:00:471803 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:061804 // Make sure that a subframe commit isn't changing the main frame's
1805 // origin. Otherwise the renderer process may be confused, leading to a
1806 // URL spoof. We can't check the path since that may change
1807 // (https://crbug.com/373041).
creis37988b92016-06-10 18:03:571808 // TODO(creis): For now, restrict this check to HTTP(S) origins, because
1809 // about:blank, file, and unique origins are more subtle to get right.
Eugene But9fb98d12018-05-22 18:28:331810 // We'll abstract out the relevant checks from IsURLSameDocumentNavigation
1811 // and share them here. See https://crbug.com/618104.
creis37988b92016-06-10 18:03:571812 const GURL& dest_top_url = GetEntryAtIndex(entry_index)->GetURL();
1813 const GURL& current_top_url = GetLastCommittedEntry()->GetURL();
1814 if (current_top_url.SchemeIsHTTPOrHTTPS() &&
1815 dest_top_url.SchemeIsHTTPOrHTTPS() &&
1816 current_top_url.GetOrigin() != dest_top_url.GetOrigin()) {
creisfb6eeb62016-05-10 19:01:511817 bad_message::ReceivedBadMessage(rfh->GetProcess(),
1818 bad_message::NC_AUTO_SUBFRAME);
avi98405c22015-05-21 20:47:061819 }
creis3cdc3b02015-05-29 23:00:471820
creis913c63ce2016-07-16 19:52:521821 // We only need to discard the pending entry in this history navigation
1822 // case. For newly created subframes, there was no pending entry.
avi98405c22015-05-21 20:47:061823 last_committed_entry_index_ = entry_index;
arthursonzogni69a6a1b2019-09-17 09:23:001824 DiscardNonCommittedEntries();
creis913c63ce2016-07-16 19:52:521825
1826 // History navigations should send a commit notification.
1827 send_commit_notification = true;
avi98405c22015-05-21 20:47:061828 }
[email protected]e9ba4472008-09-14 15:42:431829 }
[email protected]f233e4232013-02-23 00:55:141830
creisce0ef3572017-01-26 17:53:081831 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
1832 // it may be a "history auto" case where we update an existing one.
1833 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
Lukasz Anforowicz435bcb582019-07-12 20:50:061834 const base::Optional<url::Origin>& initiator_origin =
Mohamed Abdelhalim833de902019-09-16 17:41:451835 request->common_params().initiator_origin;
creisce0ef3572017-01-26 17:53:081836 last_committed->AddOrUpdateFrameEntry(
1837 rfh->frame_tree_node(), params.item_sequence_number,
1838 params.document_sequence_number, rfh->GetSiteInstance(), nullptr,
Nasko Oskov03912102019-01-11 00:21:321839 params.url, GetCommittedOriginForFrameEntry(params), params.referrer,
Lukasz Anforowicz435bcb582019-07-12 20:50:061840 initiator_origin, params.redirects, params.page_state, params.method,
1841 params.post_id, nullptr /* blob_url_loader_factory */);
creis625a0c7d2015-03-24 23:17:121842
creis913c63ce2016-07-16 19:52:521843 return send_commit_notification;
[email protected]e9ba4472008-09-14 15:42:431844}
1845
[email protected]d202a7c2012-01-04 07:53:471846int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:231847 const NavigationEntryImpl* entry) const {
avif16f85a72015-11-13 18:25:031848 for (size_t i = 0; i < entries_.size(); ++i) {
1849 if (entries_[i].get() == entry)
1850 return i;
1851 }
1852 return -1;
[email protected]765b35502008-08-21 00:51:201853}
1854
Eugene But7cc259d2017-10-09 23:52:491855// There are two general cases where a navigation is "same-document":
avidb7d1d22015-06-08 21:21:501856// 1. A fragment navigation, in which the url is kept the same except for the
1857// reference fragment.
1858// 2. A history API navigation (pushState and replaceState). This case is
Eugene But7cc259d2017-10-09 23:52:491859// always same-document, but the urls are not guaranteed to match excluding
1860// the fragment. The relevant spec allows pushState/replaceState to any URL
1861// on the same origin.
avidb7d1d22015-06-08 21:21:501862// However, due to reloads, even identical urls are *not* guaranteed to be
Eugene But7cc259d2017-10-09 23:52:491863// same-document navigations, we have to trust the renderer almost entirely.
avidb7d1d22015-06-08 21:21:501864// The one thing we do know is that cross-origin navigations will *never* be
Eugene But7cc259d2017-10-09 23:52:491865// same-document. Therefore, trust the renderer if the URLs are on the same
1866// origin, and assume the renderer is malicious if a cross-origin navigation
1867// claims to be same-document.
creisf164daa2016-06-07 00:17:051868//
1869// TODO(creis): Clean up and simplify the about:blank and origin checks below,
1870// which are likely redundant with each other. Be careful about data URLs vs
1871// about:blank, both of which are unique origins and thus not considered equal.
Eugene But7cc259d2017-10-09 23:52:491872bool NavigationControllerImpl::IsURLSameDocumentNavigation(
[email protected]b9d4dfdc2013-08-08 00:25:121873 const GURL& url,
creisf164daa2016-06-07 00:17:051874 const url::Origin& origin,
Eugene But7cc259d2017-10-09 23:52:491875 bool renderer_says_same_document,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571876 RenderFrameHost* rfh) {
creis225a7432016-06-03 22:56:271877 RenderFrameHostImpl* rfhi = static_cast<RenderFrameHostImpl*>(rfh);
avidb7d1d22015-06-08 21:21:501878 GURL last_committed_url;
1879 if (rfh->GetParent()) {
creis225a7432016-06-03 22:56:271880 // Use the FrameTreeNode's current_url and not rfh->GetLastCommittedURL(),
1881 // which might be empty in a new RenderFrameHost after a process swap.
1882 // Here, we care about the last committed URL in the FrameTreeNode,
1883 // regardless of which process it is in.
1884 last_committed_url = rfhi->frame_tree_node()->current_url();
avidb7d1d22015-06-08 21:21:501885 } else {
1886 NavigationEntry* last_committed = GetLastCommittedEntry();
1887 // There must be a last-committed entry to compare URLs to. TODO(avi): When
1888 // might Blink say that a navigation is in-page yet there be no last-
1889 // committed entry?
1890 if (!last_committed)
1891 return false;
1892 last_committed_url = last_committed->GetURL();
1893 }
1894
1895 WebPreferences prefs = rfh->GetRenderViewHost()->GetWebkitPreferences();
creis225a7432016-06-03 22:56:271896 const url::Origin& committed_origin =
1897 rfhi->frame_tree_node()->current_origin();
avidb7d1d22015-06-08 21:21:501898 bool is_same_origin = last_committed_url.is_empty() ||
1899 // TODO(japhet): We should only permit navigations
1900 // originating from about:blank to be in-page if the
1901 // about:blank is the first document that frame loaded.
1902 // We don't have sufficient information to identify
1903 // that case at the moment, so always allow about:blank
1904 // for now.
csharrisona3bd0b32016-10-19 18:40:481905 last_committed_url == url::kAboutBlankURL ||
avidb7d1d22015-06-08 21:21:501906 last_committed_url.GetOrigin() == url.GetOrigin() ||
creisf164daa2016-06-07 00:17:051907 committed_origin == origin ||
avidb7d1d22015-06-08 21:21:501908 !prefs.web_security_enabled ||
1909 (prefs.allow_universal_access_from_file_urls &&
creisdc004e02015-11-20 21:28:471910 committed_origin.scheme() == url::kFileScheme);
Eugene But7cc259d2017-10-09 23:52:491911 if (!is_same_origin && renderer_says_same_document) {
avidb7d1d22015-06-08 21:21:501912 bad_message::ReceivedBadMessage(rfh->GetProcess(),
1913 bad_message::NC_IN_PAGE_NAVIGATION);
1914 }
Eugene But7cc259d2017-10-09 23:52:491915 return is_same_origin && renderer_says_same_document;
[email protected]e9ba4472008-09-14 15:42:431916}
1917
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571918void NavigationControllerImpl::CopyStateFrom(NavigationController* temp,
Francois Dorayeaace782017-06-21 16:37:241919 bool needs_reload) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571920 NavigationControllerImpl* source =
1921 static_cast<NavigationControllerImpl*>(temp);
[email protected]ce3fa3c2009-04-20 19:55:571922 // Verify that we look new.
[email protected]a26023822011-12-29 00:23:551923 DCHECK(GetEntryCount() == 0 && !GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:571924
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571925 if (source->GetEntryCount() == 0)
[email protected]ce3fa3c2009-04-20 19:55:571926 return; // Nothing new to do.
1927
Francois Dorayeaace782017-06-21 16:37:241928 needs_reload_ = needs_reload;
Bo Liucdfa4b12018-11-06 00:21:441929 needs_reload_type_ = NeedsReloadType::kCopyStateFrom;
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571930 InsertEntriesFrom(source, source->GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:571931
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571932 for (auto it = source->session_storage_namespace_map_.begin();
1933 it != source->session_storage_namespace_map_.end(); ++it) {
[email protected]fdac6ade2013-07-20 01:06:301934 SessionStorageNamespaceImpl* source_namespace =
1935 static_cast<SessionStorageNamespaceImpl*>(it->second.get());
1936 session_storage_namespace_map_[it->first] = source_namespace->Clone();
1937 }
[email protected]4e6419c2010-01-15 04:50:341938
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571939 FinishRestore(source->last_committed_entry_index_,
toyoshim0df1d3a2016-09-09 09:52:481940 RestoreType::CURRENT_SESSION);
[email protected]ce3fa3c2009-04-20 19:55:571941}
1942
[email protected]d202a7c2012-01-04 07:53:471943void NavigationControllerImpl::CopyStateFromAndPrune(
[email protected]e78a6852013-12-13 08:08:571944 NavigationController* temp,
1945 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:161946 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:011947 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:161948
[email protected]d202a7c2012-01-04 07:53:471949 NavigationControllerImpl* source =
1950 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:251951
avi2b177592014-12-10 02:08:021952 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:011953 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:251954
[email protected]474f8512013-05-31 22:31:161955 // We now have one entry, possibly with a new pending entry. Ensure that
1956 // adding the entries from source won't put us over the limit.
1957 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:571958 if (!replace_entry)
Shivani Sharmad8c8d652019-02-13 17:27:571959 source->PruneOldestSkippableEntryIfFull();
[email protected]944822b2012-03-02 20:57:251960
[email protected]47e020a2010-10-15 14:43:371961 // Insert the entries from source. Don't use source->GetCurrentEntryIndex as
avi2b177592014-12-10 02:08:021962 // we don't want to copy over the transient entry. Ignore any pending entry,
[email protected]474f8512013-05-31 22:31:161963 // since it has not committed in source.
1964 int max_source_index = source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:251965 if (max_source_index == -1)
[email protected]a26023822011-12-29 00:23:551966 max_source_index = source->GetEntryCount();
[email protected]e1cd5452010-08-26 18:03:251967 else
1968 max_source_index++;
[email protected]e78a6852013-12-13 08:08:571969
1970 // Ignore the source's current entry if merging with replacement.
1971 // TODO(davidben): This should preserve entries forward of the current
1972 // too. http://crbug.com/317872
1973 if (replace_entry && max_source_index > 0)
1974 max_source_index--;
1975
Lucas Furukawa Gadani5553a1582019-01-08 18:55:571976 InsertEntriesFrom(source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:251977
1978 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:551979 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:141980
avi2b177592014-12-10 02:08:021981 delegate_->SetHistoryOffsetAndLength(last_committed_entry_index_,
1982 GetEntryCount());
[email protected]e1cd5452010-08-26 18:03:251983}
1984
[email protected]79368982013-11-13 01:11:011985bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:161986 // If there is no last committed entry, we cannot prune. Even if there is a
1987 // pending entry, it may not commit, leaving this WebContents blank, despite
1988 // possibly giving it new entries via CopyStateFromAndPrune.
1989 if (last_committed_entry_index_ == -1)
1990 return false;
[email protected]9350602e2013-02-26 23:27:441991
[email protected]474f8512013-05-31 22:31:161992 // We cannot prune if there is a pending entry at an existing entry index.
1993 // It may not commit, so we have to keep the last committed entry, and thus
1994 // there is no sensible place to keep the pending entry. It is ok to have
1995 // a new pending entry, which can optionally commit as a new navigation.
1996 if (pending_entry_index_ != -1)
1997 return false;
1998
1999 // We should not prune if we are currently showing a transient entry.
2000 if (transient_entry_index_ != -1)
2001 return false;
2002
2003 return true;
2004}
2005
[email protected]79368982013-11-13 01:11:012006void NavigationControllerImpl::PruneAllButLastCommitted() {
2007 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:162008
avi2b177592014-12-10 02:08:022009 DCHECK_EQ(0, last_committed_entry_index_);
2010 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442011
avi2b177592014-12-10 02:08:022012 delegate_->SetHistoryOffsetAndLength(last_committed_entry_index_,
2013 GetEntryCount());
[email protected]9350602e2013-02-26 23:27:442014}
2015
[email protected]79368982013-11-13 01:11:012016void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:162017 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:012018 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:262019
[email protected]474f8512013-05-31 22:31:162020 // Erase all entries but the last committed entry. There may still be a
2021 // new pending entry after this.
2022 entries_.erase(entries_.begin(),
2023 entries_.begin() + last_committed_entry_index_);
2024 entries_.erase(entries_.begin() + 1, entries_.end());
2025 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:262026}
2027
Christian Dullweber1af31e62018-02-22 11:49:482028void NavigationControllerImpl::DeleteNavigationEntries(
2029 const DeletionPredicate& deletionPredicate) {
2030 // It is up to callers to check the invariants before calling this.
2031 CHECK(CanPruneAllButLastCommitted());
2032 std::vector<int> delete_indices;
2033 for (size_t i = 0; i < entries_.size(); i++) {
2034 if (i != static_cast<size_t>(last_committed_entry_index_) &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572035 deletionPredicate.Run(entries_[i].get())) {
Christian Dullweber1af31e62018-02-22 11:49:482036 delete_indices.push_back(i);
2037 }
2038 }
2039 if (delete_indices.empty())
2040 return;
2041
2042 if (delete_indices.size() == GetEntryCount() - 1U) {
2043 PruneAllButLastCommitted();
2044 } else {
2045 // Do the deletion in reverse to preserve indices.
2046 for (auto it = delete_indices.rbegin(); it != delete_indices.rend(); ++it) {
2047 RemoveEntryAtIndex(*it);
2048 }
2049 delegate_->SetHistoryOffsetAndLength(last_committed_entry_index_,
2050 GetEntryCount());
2051 }
2052 delegate()->NotifyNavigationEntriesDeleted();
2053}
2054
Shivani Sharma883f5f32019-02-12 18:20:012055bool NavigationControllerImpl::IsEntryMarkedToBeSkipped(int index) {
2056 auto* entry = GetEntryAtIndex(index);
2057 return entry && entry->should_skip_on_back_forward_ui();
2058}
2059
clamy987a3752018-05-03 17:36:262060void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
2061 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
2062 // progress, since this will cause a use-after-free. (We only allow this
2063 // when the tab is being destroyed for shutdown, since it won't return to
2064 // NavigateToEntry in that case.) http://crbug.com/347742.
2065 CHECK(!in_navigate_to_pending_entry_ || delegate_->IsBeingDestroyed());
2066
2067 if (was_failure && pending_entry_) {
2068 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2069 } else {
2070 failed_pending_entry_id_ = 0;
2071 }
2072
2073 if (pending_entry_) {
2074 if (pending_entry_index_ == -1)
2075 delete pending_entry_;
2076 pending_entry_index_ = -1;
2077 pending_entry_ = nullptr;
2078 }
2079}
2080
2081void NavigationControllerImpl::SetPendingNavigationSSLError(bool error) {
2082 if (pending_entry_)
2083 pending_entry_->set_ssl_error(error);
2084}
2085
Camille Lamy5193caa2018-10-12 11:59:422086#if defined(OS_ANDROID)
2087// static
2088bool NavigationControllerImpl::ValidateDataURLAsString(
2089 const scoped_refptr<const base::RefCountedString>& data_url_as_string) {
2090 if (!data_url_as_string)
2091 return false;
2092
2093 if (data_url_as_string->size() > kMaxLengthOfDataURLString)
2094 return false;
2095
2096 // The number of characters that is enough for validating a data: URI.
2097 // From the GURL's POV, the only important part here is scheme, it doesn't
2098 // check the actual content. Thus we can take only the prefix of the url, to
2099 // avoid unneeded copying of a potentially long string.
2100 const size_t kDataUriPrefixMaxLen = 64;
2101 GURL data_url(
2102 std::string(data_url_as_string->front_as<char>(),
2103 std::min(data_url_as_string->size(), kDataUriPrefixMaxLen)));
2104 if (!data_url.is_valid() || !data_url.SchemeIs(url::kDataScheme))
2105 return false;
2106
2107 return true;
2108}
2109#endif
2110
Shivani Sharma194877032019-03-07 17:52:472111void NavigationControllerImpl::NotifyUserActivation() {
2112 // When a user activation occurs, ensure that all adjacent entries for the
2113 // same document clear their skippable bit, so that the history manipulation
2114 // intervention does not apply to them.
Shivani Sharmaffb32b82019-04-09 16:58:472115 // TODO(crbug.com/949279) in case it becomes necessary for resetting based on
2116 // which frame created an entry and which frame has the user gesture.
Shivani Sharma194877032019-03-07 17:52:472117 auto* last_committed_entry = GetLastCommittedEntry();
2118 if (!last_committed_entry)
2119 return;
Shivani Sharma194877032019-03-07 17:52:472120
2121 // |last_committed_entry| should not be skippable because it is the current
2122 // entry and in case the skippable bit was earlier set then on re-navigation
2123 // it would have been reset.
2124 DCHECK(!last_committed_entry->should_skip_on_back_forward_ui());
Shivani Sharmac4cc8922019-04-18 03:11:172125 SetSkippableForSameDocumentEntries(GetLastCommittedEntryIndex(), false);
Shivani Sharma194877032019-03-07 17:52:472126}
2127
clamy987a3752018-05-03 17:36:262128bool NavigationControllerImpl::StartHistoryNavigationInNewSubframe(
2129 RenderFrameHostImpl* render_frame_host,
Julie Jeongeun Kimed2e5ba72019-09-12 10:14:172130 mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) {
clamy987a3752018-05-03 17:36:262131 NavigationEntryImpl* entry =
2132 GetEntryWithUniqueID(render_frame_host->nav_entry_id());
2133 if (!entry)
2134 return false;
2135
2136 FrameNavigationEntry* frame_entry =
2137 entry->GetFrameEntry(render_frame_host->frame_tree_node());
2138 if (!frame_entry)
2139 return false;
2140
Camille Lamy5193caa2018-10-12 11:59:422141 std::unique_ptr<NavigationRequest> request = CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572142 render_frame_host->frame_tree_node(), entry, frame_entry,
clamyea99ea12018-05-28 13:54:232143 ReloadType::NONE, false /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422144 true /* is_history_navigation_in_new_child */);
clamyea99ea12018-05-28 13:54:232145
2146 if (!request)
2147 return false;
2148
Arthur Hemery06173ce2019-05-29 12:11:412149 request->SetNavigationClient(std::move(*navigation_client),
2150 render_frame_host->GetSiteInstance()->GetId());
2151
clamyea99ea12018-05-28 13:54:232152 render_frame_host->frame_tree_node()->navigator()->Navigate(
2153 std::move(request), ReloadType::NONE, RestoreType::NONE);
2154
2155 return true;
clamy987a3752018-05-03 17:36:262156}
2157
Dave Tapuska8bfd84c2019-03-26 20:47:162158void NavigationControllerImpl::GoToOffsetInSandboxedFrame(
2159 int offset,
2160 int sandbox_frame_tree_node_id) {
2161 if (!CanGoToOffset(offset))
2162 return;
2163 GoToIndex(GetIndexForOffset(offset), sandbox_frame_tree_node_id);
2164}
2165
clamy987a3752018-05-03 17:36:262166void NavigationControllerImpl::NavigateFromFrameProxy(
2167 RenderFrameHostImpl* render_frame_host,
2168 const GURL& url,
Lukasz Anforowicz63f3b9432019-05-30 05:42:582169 const base::Optional<url::Origin>& initiator_origin,
clamy987a3752018-05-03 17:36:262170 bool is_renderer_initiated,
2171 SiteInstance* source_site_instance,
2172 const Referrer& referrer,
2173 ui::PageTransition page_transition,
2174 bool should_replace_current_entry,
Charlie Harrison8c113a32019-01-07 16:08:292175 NavigationDownloadPolicy download_policy,
clamy987a3752018-05-03 17:36:262176 const std::string& method,
2177 scoped_refptr<network::ResourceRequestBody> post_body,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:092178 const std::string& extra_headers,
2179 scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) {
Lukasz Anforowicz63f3b9432019-05-30 05:42:582180 if (is_renderer_initiated)
2181 DCHECK(initiator_origin.has_value());
2182
clamy987a3752018-05-03 17:36:262183 FrameTreeNode* node = render_frame_host->frame_tree_node();
Nasko Oskov18006bc2018-12-06 02:53:582184
clamy987a3752018-05-03 17:36:262185 // Create a NavigationEntry for the transfer, without making it the pending
2186 // entry. Subframe transfers should have a clone of the last committed entry
2187 // with a FrameNavigationEntry for the target frame. Main frame transfers
2188 // should have a new NavigationEntry.
2189 // TODO(creis): Make this unnecessary by creating (and validating) the params
2190 // directly, passing them to the destination RenderFrameHost. See
2191 // https://crbug.com/536906.
2192 std::unique_ptr<NavigationEntryImpl> entry;
2193 if (!node->IsMainFrame()) {
2194 // Subframe case: create FrameNavigationEntry.
2195 if (GetLastCommittedEntry()) {
2196 entry = GetLastCommittedEntry()->Clone();
2197 entry->set_extra_headers(extra_headers);
2198 // TODO(arthursonzogni): What about |is_renderer_initiated|?
2199 // Renderer-initiated navigation that target a remote frame are currently
2200 // classified as browser-initiated when this one has already navigated.
2201 // See https://crbug.com/722251.
2202 } else {
2203 // If there's no last committed entry, create an entry for about:blank
2204 // with a subframe entry for our destination.
2205 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2206 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:062207 GURL(url::kAboutBlankURL), referrer, initiator_origin,
2208 page_transition, is_renderer_initiated, extra_headers,
2209 browser_context_, nullptr /* blob_url_loader_factory */));
clamy987a3752018-05-03 17:36:262210 }
2211 entry->AddOrUpdateFrameEntry(
2212 node, -1, -1, nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582213 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Lukasz Anforowicz435bcb582019-07-12 20:50:062214 base::nullopt /* commit_origin */, referrer, initiator_origin,
2215 std::vector<GURL>(), PageState(), method, -1, blob_url_loader_factory);
clamy987a3752018-05-03 17:36:262216 } else {
2217 // Main frame case.
2218 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:062219 url, referrer, initiator_origin, page_transition, is_renderer_initiated,
2220 extra_headers, browser_context_, blob_url_loader_factory));
clamy987a3752018-05-03 17:36:262221 entry->root_node()->frame_entry->set_source_site_instance(
2222 static_cast<SiteInstanceImpl*>(source_site_instance));
2223 entry->root_node()->frame_entry->set_method(method);
2224 }
clamy987a3752018-05-03 17:36:262225
2226 // Don't allow an entry replacement if there is no entry to replace.
2227 // http://crbug.com/457149
2228 if (should_replace_current_entry && GetEntryCount() > 0)
2229 entry->set_should_replace_entry(true);
Camille Lamy5193caa2018-10-12 11:59:422230
2231 bool override_user_agent = false;
clamy987a3752018-05-03 17:36:262232 if (GetLastCommittedEntry() &&
2233 GetLastCommittedEntry()->GetIsOverridingUserAgent()) {
2234 entry->SetIsOverridingUserAgent(true);
Camille Lamy5193caa2018-10-12 11:59:422235 override_user_agent = true;
clamy987a3752018-05-03 17:36:262236 }
2237 // TODO(creis): Set user gesture and intent received timestamp on Android.
2238
2239 // We may not have successfully added the FrameNavigationEntry to |entry|
2240 // above (per https://crbug.com/608402), in which case we create it from
2241 // scratch. This works because we do not depend on |frame_entry| being inside
2242 // |entry| during NavigateToEntry. This will go away when we shortcut this
2243 // further in https://crbug.com/536906.
2244 scoped_refptr<FrameNavigationEntry> frame_entry(entry->GetFrameEntry(node));
2245 if (!frame_entry) {
Patrick Monette50e8bd82019-06-13 22:40:452246 frame_entry = base::MakeRefCounted<FrameNavigationEntry>(
clamy987a3752018-05-03 17:36:262247 node->unique_name(), -1, -1, nullptr,
Nasko Oskov18006bc2018-12-06 02:53:582248 static_cast<SiteInstanceImpl*>(source_site_instance), url,
Lukasz Anforowicz435bcb582019-07-12 20:50:062249 nullptr /* origin */, referrer, initiator_origin, std::vector<GURL>(),
2250 PageState(), method, -1, blob_url_loader_factory);
clamy987a3752018-05-03 17:36:262251 }
2252
Camille Lamy5193caa2018-10-12 11:59:422253 LoadURLParams params(url);
Nasko Oskov93e7c55c2018-12-19 01:59:292254 params.initiator_origin = initiator_origin;
Camille Lamy5193caa2018-10-12 11:59:422255 params.source_site_instance = source_site_instance;
2256 params.load_type = method == "POST" ? LOAD_TYPE_HTTP_POST : LOAD_TYPE_DEFAULT;
2257 params.transition_type = page_transition;
Dominic Farolino226226af2019-06-25 00:58:032258 params.frame_tree_node_id = node->frame_tree_node_id();
Camille Lamy5193caa2018-10-12 11:59:422259 params.referrer = referrer;
2260 /* params.redirect_chain: skip */
2261 params.extra_headers = extra_headers;
2262 params.is_renderer_initiated = is_renderer_initiated;
2263 params.override_user_agent = UA_OVERRIDE_INHERIT;
2264 /* params.base_url_for_data_url: skip */
2265 /* params.virtual_url_for_data_url: skip */
2266 /* params.data_url_as_string: skip */
2267 params.post_data = post_body;
2268 params.can_load_local_resources = false;
2269 params.should_replace_current_entry = false;
2270 /* params.frame_name: skip */
2271 // TODO(clamy): See if user gesture should be propagated to this function.
2272 params.has_user_gesture = false;
2273 params.should_clear_history_list = false;
2274 params.started_from_context_menu = false;
2275 /* params.navigation_ui_data: skip */
2276 /* params.input_start: skip */
Lucas Furukawa Gadania9c45682019-07-31 22:05:142277 params.was_activated = mojom::WasActivatedOption::kUnknown;
Robert Ogden011a8082019-01-23 19:04:542278 /* params.reload_type: skip */
Camille Lamy5193caa2018-10-12 11:59:422279
2280 std::unique_ptr<NavigationRequest> request =
2281 CreateNavigationRequestFromLoadParams(
Dominic Farolino226226af2019-06-25 00:58:032282 node, params, override_user_agent, should_replace_current_entry,
2283 false /* has_user_gesture */, download_policy, ReloadType::NONE,
2284 entry.get(), frame_entry.get());
clamyea99ea12018-05-28 13:54:232285
2286 if (!request)
2287 return;
2288
Dominic Farolino226226af2019-06-25 00:58:032289 node->navigator()->Navigate(std::move(request), ReloadType::NONE,
2290 RestoreType::NONE);
clamy987a3752018-05-03 17:36:262291}
2292
[email protected]d1198fd2012-08-13 22:50:192293void NavigationControllerImpl::SetSessionStorageNamespace(
[email protected]fdac6ade2013-07-20 01:06:302294 const std::string& partition_id,
[email protected]8ff00d72012-10-23 19:12:212295 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:192296 if (!session_storage_namespace)
2297 return;
2298
2299 // We can't overwrite an existing SessionStorage without violating spec.
2300 // Attempts to do so may give a tab access to another tab's session storage
2301 // so die hard on an error.
[email protected]fdac6ade2013-07-20 01:06:302302 bool successful_insert = session_storage_namespace_map_.insert(
2303 make_pair(partition_id,
2304 static_cast<SessionStorageNamespaceImpl*>(
2305 session_storage_namespace)))
2306 .second;
2307 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
[email protected]d1198fd2012-08-13 22:50:192308}
2309
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572310bool NavigationControllerImpl::IsUnmodifiedBlankTab() {
[email protected]aa62afd2014-04-22 19:22:462311 return IsInitialNavigation() &&
[email protected]b4c84012014-04-28 19:51:102312 !GetLastCommittedEntry() &&
2313 !delegate_->HasAccessedInitialDocument();
[email protected]aa62afd2014-04-22 19:22:462314}
2315
[email protected]a26023822011-12-29 00:23:552316SessionStorageNamespace*
[email protected]fdac6ade2013-07-20 01:06:302317NavigationControllerImpl::GetSessionStorageNamespace(SiteInstance* instance) {
2318 std::string partition_id;
2319 if (instance) {
2320 // TODO(ajwong): When GetDefaultSessionStorageNamespace() goes away, remove
2321 // this if statement so |instance| must not be NULL.
2322 partition_id =
2323 GetContentClient()->browser()->GetStoragePartitionIdForSite(
2324 browser_context_, instance->GetSiteURL());
2325 }
[email protected]d1198fd2012-08-13 22:50:192326
[email protected]fdac6ade2013-07-20 01:06:302327 // TODO(ajwong): Should this use the |partition_id| directly rather than
2328 // re-lookup via |instance|? http://crbug.com/142685
2329 StoragePartition* partition =
michaelnbacbcbd2016-02-09 00:32:032330 BrowserContext::GetStoragePartition(browser_context_, instance);
2331 DOMStorageContextWrapper* context_wrapper =
2332 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
2333
2334 SessionStorageNamespaceMap::const_iterator it =
2335 session_storage_namespace_map_.find(partition_id);
2336 if (it != session_storage_namespace_map_.end()) {
2337 // Ensure that this namespace actually belongs to this partition.
2338 DCHECK(static_cast<SessionStorageNamespaceImpl*>(it->second.get())->
2339 IsFromContext(context_wrapper));
2340 return it->second.get();
2341 }
2342
2343 // Create one if no one has accessed session storage for this partition yet.
Daniel Murphy31bbb8b12018-02-07 21:44:102344 scoped_refptr<SessionStorageNamespaceImpl> session_storage_namespace =
2345 SessionStorageNamespaceImpl::Create(context_wrapper);
2346 SessionStorageNamespaceImpl* session_storage_namespace_ptr =
2347 session_storage_namespace.get();
2348 session_storage_namespace_map_[partition_id] =
2349 std::move(session_storage_namespace);
[email protected]fdac6ade2013-07-20 01:06:302350
Daniel Murphy31bbb8b12018-02-07 21:44:102351 return session_storage_namespace_ptr;
[email protected]fdac6ade2013-07-20 01:06:302352}
2353
2354SessionStorageNamespace*
2355NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
2356 // TODO(ajwong): Remove if statement in GetSessionStorageNamespace().
Ivan Kotenkov2c0d2bb32017-11-01 15:41:282357 return GetSessionStorageNamespace(nullptr);
[email protected]fdac6ade2013-07-20 01:06:302358}
2359
2360const SessionStorageNamespaceMap&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572361NavigationControllerImpl::GetSessionStorageNamespaceMap() {
[email protected]fdac6ade2013-07-20 01:06:302362 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:552363}
[email protected]d202a7c2012-01-04 07:53:472364
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572365bool NavigationControllerImpl::NeedsReload() {
[email protected]71fde352011-12-29 03:29:562366 return needs_reload_;
2367}
[email protected]a26023822011-12-29 00:23:552368
[email protected]46bb5e9c2013-10-03 22:16:472369void NavigationControllerImpl::SetNeedsReload() {
Alex Moshchuk7b4f0652019-05-30 18:54:412370 SetNeedsReload(NeedsReloadType::kRequestedByClient);
2371}
2372
2373void NavigationControllerImpl::SetNeedsReload(NeedsReloadType type) {
[email protected]46bb5e9c2013-10-03 22:16:472374 needs_reload_ = true;
Alex Moshchuk7b4f0652019-05-30 18:54:412375 needs_reload_type_ = type;
jaekyunc8cefa82015-01-09 20:14:542376
2377 if (last_committed_entry_index_ != -1) {
2378 entries_[last_committed_entry_index_]->SetTransitionType(
2379 ui::PAGE_TRANSITION_RELOAD);
2380 }
[email protected]46bb5e9c2013-10-03 22:16:472381}
2382
[email protected]d202a7c2012-01-04 07:53:472383void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
Kevin McNee05164772019-09-03 17:24:572384 DCHECK_LT(index, GetEntryCount());
2385 DCHECK_NE(index, last_committed_entry_index_);
2386
2387 const bool was_transient = index == transient_entry_index_;
[email protected]43032342011-03-21 14:10:312388
2389 DiscardNonCommittedEntries();
2390
Kevin McNee05164772019-09-03 17:24:572391 if (was_transient) {
2392 // There's nothing left to do if the index referred to a transient entry
2393 // that we just discarded.
2394 DCHECK(!GetTransientEntry());
2395 return;
2396 }
2397
[email protected]43032342011-03-21 14:10:312398 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:122399 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:312400 last_committed_entry_index_--;
2401}
2402
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572403NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() {
arthursonzogni5c4c202d2017-04-25 23:41:272404 // If there is no pending_entry_, there should be no pending_entry_index_.
2405 DCHECK(pending_entry_ || pending_entry_index_ == -1);
2406
2407 // If there is a pending_entry_index_, then pending_entry_ must be the entry
2408 // at that index.
2409 DCHECK(pending_entry_index_ == -1 ||
2410 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
2411
[email protected]022af742011-12-28 18:37:252412 return pending_entry_;
2413}
2414
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572415int NavigationControllerImpl::GetPendingEntryIndex() {
arthursonzogni5c4c202d2017-04-25 23:41:272416 // The pending entry index must always be less than the number of entries.
2417 // If there are no entries, it must be exactly -1.
2418 DCHECK_LT(pending_entry_index_, GetEntryCount());
2419 DCHECK(GetEntryCount() != 0 || pending_entry_index_ == -1);
[email protected]a26023822011-12-29 00:23:552420 return pending_entry_index_;
2421}
2422
avi25764702015-06-23 15:43:372423void NavigationControllerImpl::InsertOrReplaceEntry(
dcheng9bfa5162016-04-09 01:00:572424 std::unique_ptr<NavigationEntryImpl> entry,
2425 bool replace) {
mastiz7eddb5f72016-06-23 09:52:452426 DCHECK(!ui::PageTransitionCoreTypeIs(entry->GetTransitionType(),
2427 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
[email protected]765b35502008-08-21 00:51:202428
avi5cad4912015-06-19 05:25:442429 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
2430 // need to keep continuity with the pending entry, so copy the pending entry's
2431 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
2432 // then the renderer navigated on its own, independent of the pending entry,
2433 // so don't copy anything.
2434 if (pending_entry_ && pending_entry_index_ == -1)
2435 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:202436
arthursonzogni69a6a1b2019-09-17 09:23:002437 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:202438
2439 int current_size = static_cast<int>(entries_.size());
creisee17e932015-07-17 17:56:222440
2441 // When replacing, don't prune the forward history.
creis37979a62015-08-04 19:48:182442 if (replace && current_size > 0) {
Mikel Astizba9cf2fd2017-12-17 10:38:102443 CopyReplacedNavigationEntryDataIfPreviouslyEmpty(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572444 entries_[last_committed_entry_index_].get(), entry.get());
dcheng36b6aec92015-12-26 06:16:362445 entries_[last_committed_entry_index_] = std::move(entry);
creisee17e932015-07-17 17:56:222446 return;
2447 }
[email protected]765b35502008-08-21 00:51:202448
creis37979a62015-08-04 19:48:182449 // We shouldn't see replace == true when there's no committed entries.
2450 DCHECK(!replace);
2451
[email protected]765b35502008-08-21 00:51:202452 if (current_size > 0) {
[email protected]672eba292009-05-13 13:22:452453 // Prune any entries which are in front of the current entry.
[email protected]c12bf1a12008-09-17 16:28:492454 int num_pruned = 0;
[email protected]47dcbdc2011-05-27 15:08:312455 while (last_committed_entry_index_ < (current_size - 1)) {
[email protected]c12bf1a12008-09-17 16:28:492456 num_pruned++;
[email protected]765b35502008-08-21 00:51:202457 entries_.pop_back();
2458 current_size--;
2459 }
Shivani Sharmab9c46de82019-02-08 16:54:502460 if (num_pruned > 0) { // Only notify if we did prune something.
2461 NotifyPrunedEntries(this,
2462 last_committed_entry_index_ + 1 /* start index */,
2463 num_pruned /* count */);
2464 }
[email protected]765b35502008-08-21 00:51:202465 }
2466
Shivani Sharmad8c8d652019-02-13 17:27:572467 PruneOldestSkippableEntryIfFull();
[email protected]765b35502008-08-21 00:51:202468
dcheng36b6aec92015-12-26 06:16:362469 entries_.push_back(std::move(entry));
[email protected]765b35502008-08-21 00:51:202470 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
initial.commit09911bf2008-07-26 23:55:292471}
2472
Shivani Sharmad8c8d652019-02-13 17:27:572473void NavigationControllerImpl::PruneOldestSkippableEntryIfFull() {
Shivani Sharma2d5b4b6b2019-01-08 16:07:162474 if (entries_.size() < max_entry_count())
2475 return;
2476
2477 DCHECK_EQ(max_entry_count(), entries_.size());
2478 DCHECK_GT(last_committed_entry_index_, 0);
Shivani Sharmad8c8d652019-02-13 17:27:572479 CHECK_EQ(pending_entry_index_, -1);
2480
2481 int index = 0;
2482 if (base::FeatureList::IsEnabled(
2483 features::kHistoryManipulationIntervention)) {
2484 // Retrieve the oldest skippable entry.
2485 for (; index < GetEntryCount(); index++) {
2486 if (GetEntryAtIndex(index)->should_skip_on_back_forward_ui())
2487 break;
2488 }
2489 }
2490
2491 // If there is no skippable entry or if it is the last committed entry then
2492 // fall back to pruning the oldest entry. It is not safe to prune the last
2493 // committed entry.
2494 if (index == GetEntryCount() || index == last_committed_entry_index_)
2495 index = 0;
2496
2497 bool should_succeed = RemoveEntryAtIndex(index);
2498 DCHECK_EQ(true, should_succeed);
2499
2500 NotifyPrunedEntries(this, index, 1);
[email protected]944822b2012-03-02 20:57:252501}
2502
clamy3cb9bea92018-07-10 12:42:022503void NavigationControllerImpl::NavigateToExistingPendingEntry(
Dave Tapuska8bfd84c2019-03-26 20:47:162504 ReloadType reload_type,
2505 int sandboxed_source_frame_tree_node_id) {
arthursonzogni5c4c202d2017-04-25 23:41:272506 DCHECK(pending_entry_);
clamy3cb9bea92018-07-10 12:42:022507 DCHECK(IsInitialNavigation() || pending_entry_index_ != -1);
2508 DCHECK(!IsRendererDebugURL(pending_entry_->GetURL()));
[email protected]72097fd02010-01-21 23:36:012509 needs_reload_ = false;
Arthur Sonzogni620cec62018-12-13 13:08:572510 FrameTreeNode* root = delegate_->GetFrameTree()->root();
2511 int nav_entry_id = pending_entry_->GetUniqueID();
2512
2513 // BackForwardCache:
Lowell Manners0c04d632019-08-05 10:52:022514 // Navigate immediately if the document is in the BackForwardCache.
Lowell Manners889189f2019-08-22 16:47:452515 if (back_forward_cache_.GetDocument(nav_entry_id)) {
Lowell Manners0c04d632019-08-05 10:52:022516 DCHECK_EQ(reload_type, ReloadType::NONE);
2517 auto navigation_request = CreateNavigationRequestFromEntry(
2518 root, pending_entry_, pending_entry_->GetFrameEntry(root),
2519 ReloadType::NONE, false /* is_same_document_history_load */,
2520 false /* is_history_navigation_in_new_child */);
2521 root->navigator()->Navigate(std::move(navigation_request), ReloadType::NONE,
2522 RestoreType::NONE);
2523
Arthur Sonzogni620cec62018-12-13 13:08:572524 return;
2525 }
[email protected]72097fd02010-01-21 23:36:012526
arthursonzogni6c27c3152019-09-12 08:00:572527 // By design, a page in the BackForwardCache is alone in its BrowsingInstance.
2528 // History navigation might try to reuse a specific SiteInstance, already used
2529 // by a page in the cache. This must not happen. It would fail creating the
2530 // RenderFrame, because only one main document can live there. For this
2531 // reason, the BackForwardCache is flushed.
2532 // TODO(arthursonzogni): Flushing the entire cache is a bit overkill, this can
2533 // be refined to only delete the page (if any) using the same
2534 // BrowsingInstance.
2535 if (pending_entry_->site_instance()) {
2536 SiteInstance* current = root->current_frame_host()->GetSiteInstance();
2537 if (!current->IsRelatedSiteInstance(pending_entry_->site_instance()))
2538 back_forward_cache_.Flush();
2539 }
2540
[email protected]83c2e232011-10-07 21:36:462541 // If we were navigating to a slow-to-commit page, and the user performs
2542 // a session history navigation to the last committed page, RenderViewHost
2543 // will force the throbber to start, but WebKit will essentially ignore the
2544 // navigation, and won't send a message to stop the throbber. To prevent this
2545 // from happening, we drop the navigation here and stop the slow-to-commit
2546 // page from loading (which would normally happen during the navigation).
clamy3cb9bea92018-07-10 12:42:022547 if (pending_entry_index_ == last_committed_entry_index_ &&
arthursonzogni5c4c202d2017-04-25 23:41:272548 pending_entry_->restore_type() == RestoreType::NONE &&
2549 pending_entry_->GetTransitionType() & ui::PAGE_TRANSITION_FORWARD_BACK) {
[email protected]ec6c05f2013-10-23 18:41:572550 delegate_->Stop();
[email protected]6a13a6c2011-12-20 21:47:122551
avi668f5232017-06-22 22:52:172552 // If an interstitial page is showing, we want to close it to get back to
2553 // what was showing before.
2554 //
2555 // There are two ways to get the interstitial page given a WebContents.
2556 // Because WebContents::GetInterstitialPage() returns null between the
2557 // interstitial's Show() method being called and the interstitial becoming
2558 // visible, while InterstitialPage::GetInterstitialPage() returns the
2559 // interstitial during that time, use the latter.
2560 InterstitialPage* interstitial =
2561 InterstitialPage::GetInterstitialPage(GetWebContents());
2562 if (interstitial)
2563 interstitial->DontProceed();
[email protected]6a13a6c2011-12-20 21:47:122564
[email protected]83c2e232011-10-07 21:36:462565 DiscardNonCommittedEntries();
2566 return;
2567 }
2568
creisce0ef3572017-01-26 17:53:082569 // Compare FrameNavigationEntries to see which frames in the tree need to be
2570 // navigated.
clamy3cb9bea92018-07-10 12:42:022571 std::vector<std::unique_ptr<NavigationRequest>> same_document_loads;
2572 std::vector<std::unique_ptr<NavigationRequest>> different_document_loads;
creis4e2ecb72015-06-20 00:46:302573 if (GetLastCommittedEntry()) {
clamy3cb9bea92018-07-10 12:42:022574 FindFramesToNavigate(root, reload_type, &same_document_loads,
2575 &different_document_loads);
creis4e2ecb72015-06-20 00:46:302576 }
2577
2578 if (same_document_loads.empty() && different_document_loads.empty()) {
2579 // If we don't have any frames to navigate at this point, either
2580 // (1) there is no previous history entry to compare against, or
2581 // (2) we were unable to match any frames by name. In the first case,
2582 // doing a different document navigation to the root item is the only valid
2583 // thing to do. In the second case, we should have been able to find a
2584 // frame to navigate based on names if this were a same document
2585 // navigation, so we can safely assume this is the different document case.
clamy3cb9bea92018-07-10 12:42:022586 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422587 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572588 root, pending_entry_, pending_entry_->GetFrameEntry(root),
clamy3cb9bea92018-07-10 12:42:022589 reload_type, false /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422590 false /* is_history_navigation_in_new_child */);
clamy3cb9bea92018-07-10 12:42:022591 if (!navigation_request) {
2592 // This navigation cannot start (e.g. the URL is invalid), delete the
2593 // pending NavigationEntry.
2594 DiscardPendingEntry(false);
2595 return;
2596 }
2597 different_document_loads.push_back(std::move(navigation_request));
creis4e2ecb72015-06-20 00:46:302598 }
2599
Dave Tapuska8bfd84c2019-03-26 20:47:162600 // If |sandboxed_source_frame_node_id| is valid, then track whether this
2601 // navigation affects any frame outside the frame's subtree.
2602 if (sandboxed_source_frame_tree_node_id !=
2603 FrameTreeNode::kFrameTreeNodeInvalidId) {
2604 bool navigates_inside_tree =
2605 DoesSandboxNavigationStayWithinSubtree(
2606 sandboxed_source_frame_tree_node_id, same_document_loads) &&
2607 DoesSandboxNavigationStayWithinSubtree(
2608 sandboxed_source_frame_tree_node_id, different_document_loads);
2609 UMA_HISTOGRAM_BOOLEAN(
2610 "Navigation.SandboxFrameBackForwardStaysWithinSubtree",
2611 navigates_inside_tree);
Dave Tapuska8bfd84c2019-03-26 20:47:162612 // Also count the navigations as web use counters so we can determine
2613 // the number of pages that trigger this.
2614 FrameTreeNode* sandbox_source_frame_tree_node =
2615 FrameTreeNode::GloballyFindByID(sandboxed_source_frame_tree_node_id);
2616 if (sandbox_source_frame_tree_node) {
2617 GetContentClient()->browser()->LogWebFeatureForCurrentPage(
2618 sandbox_source_frame_tree_node->current_frame_host(),
2619 navigates_inside_tree
2620 ? blink::mojom::WebFeature::kSandboxBackForwardStaysWithinSubtree
2621 : blink::mojom::WebFeature::
2622 kSandboxBackForwardAffectsFramesOutsideSubtree);
2623 }
Dave Tapuska855c1e12019-08-23 20:45:522624
2625 // If the navigation occurred outside the tree discard it because
2626 // the sandboxed frame didn't have permission to navigate outside
2627 // its tree. If it is possible that the navigation is both inside and
2628 // outside the frame tree and we discard it entirely because we don't
2629 // want to end up in a history state that didn't exist before.
2630 if (base::FeatureList::IsEnabled(
2631 features::kHistoryPreventSandboxedNavigation) &&
2632 !navigates_inside_tree) {
2633 DiscardPendingEntry(false);
2634 return;
2635 }
Dave Tapuska8bfd84c2019-03-26 20:47:162636 }
2637
clamy3cb9bea92018-07-10 12:42:022638 // If an interstitial page is showing, the previous renderer is blocked and
2639 // cannot make new requests. Unblock (and disable) it to allow this
2640 // navigation to succeed. The interstitial will stay visible until the
2641 // resulting DidNavigate.
Arthur Hemery0dd65812019-08-01 14:18:452642 // TODO(clamy): See if this can be removed. See https://crbug.com/849250.
clamy3cb9bea92018-07-10 12:42:022643 if (delegate_->GetInterstitialPage()) {
2644 static_cast<InterstitialPageImpl*>(delegate_->GetInterstitialPage())
2645 ->CancelForNavigation();
2646 }
2647
2648 // This call does not support re-entrancy. See http://crbug.com/347742.
2649 CHECK(!in_navigate_to_pending_entry_);
2650 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:302651
2652 // Send all the same document frame loads before the different document loads.
clamy3cb9bea92018-07-10 12:42:022653 for (auto& item : same_document_loads) {
2654 FrameTreeNode* frame = item->frame_tree_node();
2655 frame->navigator()->Navigate(std::move(item), reload_type,
2656 pending_entry_->restore_type());
creis4e2ecb72015-06-20 00:46:302657 }
clamy3cb9bea92018-07-10 12:42:022658 for (auto& item : different_document_loads) {
2659 FrameTreeNode* frame = item->frame_tree_node();
2660 frame->navigator()->Navigate(std::move(item), reload_type,
2661 pending_entry_->restore_type());
creis4e2ecb72015-06-20 00:46:302662 }
clamy3cb9bea92018-07-10 12:42:022663
2664 in_navigate_to_pending_entry_ = false;
creis4e2ecb72015-06-20 00:46:302665}
2666
2667void NavigationControllerImpl::FindFramesToNavigate(
2668 FrameTreeNode* frame,
clamy3cb9bea92018-07-10 12:42:022669 ReloadType reload_type,
2670 std::vector<std::unique_ptr<NavigationRequest>>* same_document_loads,
2671 std::vector<std::unique_ptr<NavigationRequest>>* different_document_loads) {
arthursonzogni03f76152019-02-12 10:35:202672 // A frame pending deletion is not allowed to navigate anymore. It has been
2673 // deleted and the browser already committed to destroying this
2674 // RenderFrameHost. See https://crbug.com/930278.
2675 if (!frame->current_frame_host()->is_active())
2676 return;
2677
creis4e2ecb72015-06-20 00:46:302678 DCHECK(pending_entry_);
2679 DCHECK_GE(last_committed_entry_index_, 0);
2680 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
creis225a7432016-06-03 22:56:272681 // TODO(creis): Store the last committed FrameNavigationEntry to use here,
2682 // rather than assuming the NavigationEntry has up to date info on subframes.
creis4e2ecb72015-06-20 00:46:302683 FrameNavigationEntry* old_item =
2684 GetLastCommittedEntry()->GetFrameEntry(frame);
2685 if (!new_item)
2686 return;
2687
2688 // Schedule a load in this frame if the new item isn't for the same item
creis7115a702015-12-03 18:55:562689 // sequence number in the same SiteInstance. Newly restored items may not have
2690 // a SiteInstance yet, in which case it will be assigned on first commit.
creis4e2ecb72015-06-20 00:46:302691 if (!old_item ||
2692 new_item->item_sequence_number() != old_item->item_sequence_number() ||
creis7115a702015-12-03 18:55:562693 (new_item->site_instance() != nullptr &&
2694 new_item->site_instance() != old_item->site_instance())) {
creis54131692016-08-12 18:32:252695 // Same document loads happen if the previous item has the same document
2696 // sequence number. Note that we should treat them as different document if
2697 // the destination RenderFrameHost (which is necessarily the current
2698 // RenderFrameHost for same document navigations) doesn't have a last
2699 // committed page. This case can happen for Ctrl+Back or after a renderer
2700 // crash.
creis4e2ecb72015-06-20 00:46:302701 if (old_item &&
2702 new_item->document_sequence_number() ==
creis54131692016-08-12 18:32:252703 old_item->document_sequence_number() &&
Avi Drissman2eb7a482017-11-30 04:36:312704 !frame->current_frame_host()->GetLastCommittedURL().is_empty()) {
clamy3cb9bea92018-07-10 12:42:022705 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422706 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572707 frame, pending_entry_, new_item, reload_type,
clamy3cb9bea92018-07-10 12:42:022708 true /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422709 false /* is_history_navigation_in_new_child */);
clamy3cb9bea92018-07-10 12:42:022710 if (navigation_request) {
2711 // Only add the request if was properly created. It's possible for the
2712 // creation to fail in certain cases, e.g. when the URL is invalid.
2713 same_document_loads->push_back(std::move(navigation_request));
2714 }
avib48cb312016-05-05 21:35:002715
2716 // TODO(avi, creis): This is a bug; we should not return here. Rather, we
2717 // should continue on and navigate all child frames which have also
2718 // changed. This bug is the cause of <https://crbug.com/542299>, which is
2719 // a NC_IN_PAGE_NAVIGATION renderer kill.
2720 //
2721 // However, this bug is a bandaid over a deeper and worse problem. Doing a
2722 // pushState immediately after loading a subframe is a race, one that no
2723 // web page author expects. If we fix this bug, many large websites break.
2724 // For example, see <https://crbug.com/598043> and the spec discussion at
2725 // <https://github.com/whatwg/html/issues/1191>.
2726 //
2727 // For now, we accept this bug, and hope to resolve the race in a
2728 // different way that will one day allow us to fix this.
2729 return;
creis4e2ecb72015-06-20 00:46:302730 } else {
clamy3cb9bea92018-07-10 12:42:022731 std::unique_ptr<NavigationRequest> navigation_request =
Camille Lamy5193caa2018-10-12 11:59:422732 CreateNavigationRequestFromEntry(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572733 frame, pending_entry_, new_item, reload_type,
clamy3cb9bea92018-07-10 12:42:022734 false /* is_same_document_history_load */,
Camille Lamy5193caa2018-10-12 11:59:422735 false /* is_history_navigation_in_new_child */);
clamy3cb9bea92018-07-10 12:42:022736 if (navigation_request) {
2737 // Only add the request if was properly created. It's possible for the
2738 // creation to fail in certain cases, e.g. when the URL is invalid.
2739 different_document_loads->push_back(std::move(navigation_request));
2740 }
avib3b47ab2016-01-22 20:53:582741 // For a different document, the subframes will be destroyed, so there's
2742 // no need to consider them.
2743 return;
creis4e2ecb72015-06-20 00:46:302744 }
creis4e2ecb72015-06-20 00:46:302745 }
2746
2747 for (size_t i = 0; i < frame->child_count(); i++) {
clamy3cb9bea92018-07-10 12:42:022748 FindFramesToNavigate(frame->child_at(i), reload_type, same_document_loads,
creis4e2ecb72015-06-20 00:46:302749 different_document_loads);
2750 }
2751}
2752
clamy21718cc22018-06-13 13:34:242753void NavigationControllerImpl::NavigateWithoutEntry(
2754 const LoadURLParams& params) {
2755 // Find the appropriate FrameTreeNode.
2756 FrameTreeNode* node = nullptr;
2757 if (params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId ||
2758 !params.frame_name.empty()) {
2759 node = params.frame_tree_node_id != RenderFrameHost::kNoFrameTreeNodeId
2760 ? delegate_->GetFrameTree()->FindByID(params.frame_tree_node_id)
2761 : delegate_->GetFrameTree()->FindByName(params.frame_name);
2762 }
2763
2764 // If no FrameTreeNode was specified, navigate the main frame.
2765 if (!node)
2766 node = delegate_->GetFrameTree()->root();
2767
Camille Lamy5193caa2018-10-12 11:59:422768 // Compute overrides to the LoadURLParams for |override_user_agent|,
2769 // |should_replace_current_entry| and |has_user_gesture| that will be used
2770 // both in the creation of the NavigationEntry and the NavigationRequest.
2771 // Ideally, the LoadURLParams themselves would be updated, but since they are
2772 // passed as a const reference, this is not possible.
2773 // TODO(clamy): When we only create a NavigationRequest, move this to
2774 // CreateNavigationRequestFromLoadURLParams.
2775 bool override_user_agent = ShouldOverrideUserAgent(params.override_user_agent,
2776 GetLastCommittedEntry());
2777
2778 // Don't allow an entry replacement if there is no entry to replace.
2779 // http://crbug.com/457149
2780 bool should_replace_current_entry =
2781 params.should_replace_current_entry && entries_.size();
2782
2783 // Always propagate `has_user_gesture` on Android but only when the request
2784 // was originated by the renderer on other platforms. This is merely for
2785 // backward compatibility as browser process user gestures create confusion in
2786 // many tests.
2787 bool has_user_gesture = false;
2788#if defined(OS_ANDROID)
2789 has_user_gesture = params.has_user_gesture;
2790#else
2791 if (params.is_renderer_initiated)
2792 has_user_gesture = params.has_user_gesture;
2793#endif
2794
clamy21718cc22018-06-13 13:34:242795 // Javascript URLs should not create NavigationEntries. All other navigations
2796 // do, including navigations to chrome renderer debug URLs.
2797 std::unique_ptr<NavigationEntryImpl> entry;
2798 if (!params.url.SchemeIs(url::kJavaScriptScheme)) {
Camille Lamy5193caa2018-10-12 11:59:422799 entry = CreateNavigationEntryFromLoadParams(
2800 node, params, override_user_agent, should_replace_current_entry,
2801 has_user_gesture);
clamy21718cc22018-06-13 13:34:242802 DiscardPendingEntry(false);
2803 SetPendingEntry(std::move(entry));
2804 }
2805
2806 // Renderer-debug URLs are sent to the renderer process immediately for
2807 // processing and don't need to create a NavigationRequest.
2808 // Note: this includes navigations to JavaScript URLs, which are considered
2809 // renderer-debug URLs.
2810 // Note: we intentionally leave the pending entry in place for renderer debug
2811 // URLs, unlike the cases below where we clear it if the navigation doesn't
2812 // proceed.
2813 if (IsRendererDebugURL(params.url)) {
Oleg Davydov2cc0167b2019-02-05 14:32:482814 // Renderer-debug URLs won't go through NavigationThrottlers so we have to
2815 // check them explicitly. See bug 913334.
2816 if (GetContentClient()->browser()->IsRendererDebugURLBlacklisted(
2817 params.url, browser_context_)) {
2818 DiscardPendingEntry(false);
2819 return;
2820 }
2821
clamy21718cc22018-06-13 13:34:242822 HandleRendererDebugURL(node, params.url);
2823 return;
2824 }
2825
2826 // Convert navigations to the current URL to a reload.
2827 // TODO(clamy): We should be using FrameTreeNode::IsMainFrame here instead of
2828 // relying on the frame tree node id from LoadURLParams. Unfortunately,
2829 // DevTools sometimes issues navigations to main frames that they do not
2830 // expect to see treated as reload, and it only works because they pass a
2831 // FrameTreeNode id in their LoadURLParams. Change this once they no longer do
2832 // that. See https://crbug.com/850926.
Robert Ogden011a8082019-01-23 19:04:542833 ReloadType reload_type = params.reload_type;
2834 if (reload_type == ReloadType::NONE &&
2835 ShouldTreatNavigationAsReload(
clamy21718cc22018-06-13 13:34:242836 params.url, pending_entry_->GetVirtualURL(),
2837 params.base_url_for_data_url, params.transition_type,
2838 params.frame_tree_node_id == RenderFrameHost::kNoFrameTreeNodeId,
2839 params.load_type ==
2840 NavigationController::LOAD_TYPE_HTTP_POST /* is_post */,
2841 false /* is_reload */, false /* is_navigation_to_existing_entry */,
2842 transient_entry_index_ != -1 /* has_interstitial */,
2843 GetLastCommittedEntry())) {
2844 reload_type = ReloadType::NORMAL;
2845 }
2846
2847 // navigation_ui_data should only be present for main frame navigations.
2848 DCHECK(node->IsMainFrame() || !params.navigation_ui_data);
2849
clamy21718cc22018-06-13 13:34:242850 DCHECK(pending_entry_);
Camille Lamy5193caa2018-10-12 11:59:422851 std::unique_ptr<NavigationRequest> request =
2852 CreateNavigationRequestFromLoadParams(
2853 node, params, override_user_agent, should_replace_current_entry,
Yao Xiaoaf79ca9b2019-03-12 19:54:182854 has_user_gesture, NavigationDownloadPolicy(), reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:572855 pending_entry_, pending_entry_->GetFrameEntry(node));
clamy21718cc22018-06-13 13:34:242856
2857 // If the navigation couldn't start, return immediately and discard the
2858 // pending NavigationEntry.
2859 if (!request) {
2860 DiscardPendingEntry(false);
2861 return;
2862 }
2863
Camille Lamy5193caa2018-10-12 11:59:422864#if DCHECK_IS_ON()
2865 // Safety check that NavigationRequest and NavigationEntry match.
2866 ValidateRequestMatchesEntry(request.get(), pending_entry_);
2867#endif
2868
clamy21718cc22018-06-13 13:34:242869 // If an interstitial page is showing, the previous renderer is blocked and
2870 // cannot make new requests. Unblock (and disable) it to allow this
2871 // navigation to succeed. The interstitial will stay visible until the
2872 // resulting DidNavigate.
2873 if (delegate_->GetInterstitialPage()) {
2874 static_cast<InterstitialPageImpl*>(delegate_->GetInterstitialPage())
2875 ->CancelForNavigation();
2876 }
2877
2878 // This call does not support re-entrancy. See http://crbug.com/347742.
2879 CHECK(!in_navigate_to_pending_entry_);
2880 in_navigate_to_pending_entry_ = true;
2881
2882 node->navigator()->Navigate(std::move(request), reload_type,
2883 RestoreType::NONE);
2884
2885 in_navigate_to_pending_entry_ = false;
2886}
2887
clamyea99ea12018-05-28 13:54:232888void NavigationControllerImpl::HandleRendererDebugURL(
2889 FrameTreeNode* frame_tree_node,
2890 const GURL& url) {
2891 if (!frame_tree_node->current_frame_host()->IsRenderFrameLive()) {
clamy21718cc22018-06-13 13:34:242892 // Any renderer-side debug URLs or javascript: URLs should be ignored if
2893 // the renderer process is not live, unless it is the initial navigation
2894 // of the tab.
clamyea99ea12018-05-28 13:54:232895 if (!IsInitialNavigation()) {
2896 DiscardNonCommittedEntries();
2897 return;
2898 }
2899 frame_tree_node->render_manager()->InitializeRenderFrameIfNecessary(
2900 frame_tree_node->current_frame_host());
2901 }
2902 frame_tree_node->current_frame_host()->HandleRendererDebugURL(url);
2903}
2904
clamy21718cc22018-06-13 13:34:242905std::unique_ptr<NavigationEntryImpl>
2906NavigationControllerImpl::CreateNavigationEntryFromLoadParams(
2907 FrameTreeNode* node,
Camille Lamy5193caa2018-10-12 11:59:422908 const LoadURLParams& params,
2909 bool override_user_agent,
2910 bool should_replace_current_entry,
2911 bool has_user_gesture) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:392912 // Browser initiated navigations might not have a blob_url_loader_factory set
2913 // in params even if the navigation is to a blob URL. If that happens, lookup
2914 // the correct url loader factory to use here.
2915 auto blob_url_loader_factory = params.blob_url_loader_factory;
Kinuko Yasuda7d925ea22019-08-01 10:08:482916 if (!blob_url_loader_factory && params.url.SchemeIsBlob()) {
Marijn Kruisselbrink0c87e6e2018-06-22 22:57:392917 blob_url_loader_factory = ChromeBlobStorageContext::URLLoaderFactoryForUrl(
2918 GetBrowserContext(), params.url);
2919 }
2920
clamy21718cc22018-06-13 13:34:242921 std::unique_ptr<NavigationEntryImpl> entry;
Tommy C. Li03eee77a2019-02-05 02:07:442922 // extra_headers in params are \n separated; navigation entries want \r\n.
2923 std::string extra_headers_crlf;
2924 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
clamy21718cc22018-06-13 13:34:242925
2926 // For subframes, create a pending entry with a corresponding frame entry.
2927 if (!node->IsMainFrame()) {
Tommy C. Li03eee77a2019-02-05 02:07:442928 if (GetLastCommittedEntry()) {
2929 // Create an identical NavigationEntry with a new FrameNavigationEntry for
2930 // the target subframe.
2931 entry = GetLastCommittedEntry()->Clone();
2932 } else {
2933 // If there's no last committed entry, create an entry for about:blank
2934 // with a subframe entry for our destination.
2935 // TODO(creis): Ensure this case can't exist in https://crbug.com/524208.
2936 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:062937 GURL(url::kAboutBlankURL), params.referrer, params.initiator_origin,
2938 params.transition_type, params.is_renderer_initiated,
2939 extra_headers_crlf, browser_context_, blob_url_loader_factory));
Tommy C. Li03eee77a2019-02-05 02:07:442940 }
Nasko Oskov18006bc2018-12-06 02:53:582941
clamy21718cc22018-06-13 13:34:242942 entry->AddOrUpdateFrameEntry(
2943 node, -1, -1, nullptr,
2944 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()),
Lukasz Anforowicz435bcb582019-07-12 20:50:062945 params.url, base::nullopt, params.referrer, params.initiator_origin,
2946 params.redirect_chain, PageState(), "GET", -1, blob_url_loader_factory);
clamy21718cc22018-06-13 13:34:242947 } else {
2948 // Otherwise, create a pending entry for the main frame.
clamy21718cc22018-06-13 13:34:242949 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
Lukasz Anforowicz435bcb582019-07-12 20:50:062950 params.url, params.referrer, params.initiator_origin,
2951 params.transition_type, params.is_renderer_initiated,
2952 extra_headers_crlf, browser_context_, blob_url_loader_factory));
clamy21718cc22018-06-13 13:34:242953 entry->set_source_site_instance(
2954 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
2955 entry->SetRedirectChain(params.redirect_chain);
2956 }
2957
2958 // Set the FTN ID (only used in non-site-per-process, for tests).
2959 entry->set_frame_tree_node_id(node->frame_tree_node_id());
Camille Lamy5193caa2018-10-12 11:59:422960 entry->set_should_replace_entry(should_replace_current_entry);
clamy21718cc22018-06-13 13:34:242961 entry->set_should_clear_history_list(params.should_clear_history_list);
Camille Lamy5193caa2018-10-12 11:59:422962 entry->SetIsOverridingUserAgent(override_user_agent);
2963 entry->set_has_user_gesture(has_user_gesture);
Robert Ogden011a8082019-01-23 19:04:542964 entry->set_reload_type(params.reload_type);
clamy21718cc22018-06-13 13:34:242965
clamy21718cc22018-06-13 13:34:242966 switch (params.load_type) {
2967 case LOAD_TYPE_DEFAULT:
2968 break;
2969 case LOAD_TYPE_HTTP_POST:
2970 entry->SetHasPostData(true);
2971 entry->SetPostData(params.post_data);
2972 break;
2973 case LOAD_TYPE_DATA:
2974 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
2975 entry->SetVirtualURL(params.virtual_url_for_data_url);
2976#if defined(OS_ANDROID)
2977 entry->SetDataURLAsString(params.data_url_as_string);
2978#endif
2979 entry->SetCanLoadLocalResources(params.can_load_local_resources);
2980 break;
2981 default:
2982 NOTREACHED();
2983 break;
2984 }
2985
2986 // TODO(clamy): NavigationEntry is meant for information that will be kept
2987 // after the navigation ended and therefore is not appropriate for
2988 // started_from_context_menu. Move started_from_context_menu to
2989 // NavigationUIData.
2990 entry->set_started_from_context_menu(params.started_from_context_menu);
2991
2992 return entry;
2993}
2994
clamyea99ea12018-05-28 13:54:232995std::unique_ptr<NavigationRequest>
Camille Lamy5193caa2018-10-12 11:59:422996NavigationControllerImpl::CreateNavigationRequestFromLoadParams(
2997 FrameTreeNode* node,
2998 const LoadURLParams& params,
2999 bool override_user_agent,
3000 bool should_replace_current_entry,
3001 bool has_user_gesture,
Charlie Harrison8c113a32019-01-07 16:08:293002 NavigationDownloadPolicy download_policy,
Camille Lamy5193caa2018-10-12 11:59:423003 ReloadType reload_type,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573004 NavigationEntryImpl* entry,
Camille Lamy5193caa2018-10-12 11:59:423005 FrameNavigationEntry* frame_entry) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573006 DCHECK_EQ(-1, GetIndexOfEntry(entry));
Camille Lamyb9ed3c52018-11-19 15:34:283007 DCHECK(frame_entry);
Nasko Oskov3c2f9e252019-01-10 17:45:533008 // All renderer-initiated navigations must have an initiator_origin.
3009 DCHECK(!params.is_renderer_initiated || params.initiator_origin.has_value());
Camille Lamyff7c4822018-11-07 15:42:513010
Camille Lamy5193caa2018-10-12 11:59:423011 GURL url_to_load;
3012 GURL virtual_url;
Nasko Oskov03912102019-01-11 00:21:323013 base::Optional<url::Origin> origin_to_commit =
3014 frame_entry ? frame_entry->committed_origin() : base::nullopt;
3015
Camille Lamy2baa8022018-10-19 16:43:173016 // For main frames, rewrite the URL if necessary and compute the virtual URL
3017 // that should be shown in the address bar.
3018 if (node->IsMainFrame()) {
3019 bool reverse_on_redirect = false;
3020 RewriteUrlForNavigation(params.url, browser_context_, &url_to_load,
3021 &virtual_url, &reverse_on_redirect);
Camille Lamy5193caa2018-10-12 11:59:423022
Camille Lamy2baa8022018-10-19 16:43:173023 // For DATA loads, override the virtual URL.
3024 if (params.load_type == LOAD_TYPE_DATA)
3025 virtual_url = params.virtual_url_for_data_url;
Camille Lamy5193caa2018-10-12 11:59:423026
Camille Lamy2baa8022018-10-19 16:43:173027 if (virtual_url.is_empty())
3028 virtual_url = url_to_load;
3029
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573030 CHECK(virtual_url == entry->GetVirtualURL());
Camille Lamyb9ed3c52018-11-19 15:34:283031
3032 // This is a DCHECK and not a CHECK as URL rewrite has non-deterministic
3033 // behavior in the field: it is possible for two calls to
3034 // RewriteUrlForNavigation to return different results, leading to a
3035 // different URL in the NavigationRequest and FrameEntry. This will be fixed
3036 // once we remove the pending NavigationEntry, as we'll only make one call
3037 // to RewriteUrlForNavigation.
3038 DCHECK_EQ(url_to_load, frame_entry->url());
3039
Camille Lamy2baa8022018-10-19 16:43:173040 // TODO(clamy): In order to remove the pending NavigationEntry,
3041 // |virtual_url| and |reverse_on_redirect| should be stored in the
3042 // NavigationRequest.
3043 } else {
3044 url_to_load = params.url;
3045 virtual_url = params.url;
Camille Lamyf664f7622019-01-07 19:28:243046 CHECK(!frame_entry || url_to_load == frame_entry->url());
Camille Lamy2baa8022018-10-19 16:43:173047 }
Camille Lamy5193caa2018-10-12 11:59:423048
Ehsan Karamad44fc72112019-02-26 18:15:473049 if (node->render_manager()->is_attaching_inner_delegate()) {
3050 // Avoid starting any new navigations since this node is now preparing for
3051 // attaching an inner delegate.
3052 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203053 }
Camille Lamy5193caa2018-10-12 11:59:423054
Camille Lamy5193caa2018-10-12 11:59:423055 if (!IsValidURLForNavigation(node->IsMainFrame(), virtual_url, url_to_load))
3056 return nullptr;
3057
Nasko Oskov03912102019-01-11 00:21:323058 if (!DoesURLMatchOriginForNavigation(url_to_load, origin_to_commit)) {
3059 DCHECK(false) << " url:" << url_to_load
3060 << " origin:" << origin_to_commit.value();
3061 return nullptr;
3062 }
3063
Camille Lamy5193caa2018-10-12 11:59:423064 // Determine if Previews should be used for the navigation.
3065 PreviewsState previews_state = PREVIEWS_UNSPECIFIED;
3066 if (!node->IsMainFrame()) {
3067 // For subframes, use the state of the top-level frame.
3068 previews_state = node->frame_tree()
3069 ->root()
3070 ->current_frame_host()
3071 ->last_navigation_previews_state();
3072 }
3073
Camille Lamy5193caa2018-10-12 11:59:423074 // This will be used to set the Navigation Timing API navigationStart
3075 // parameter for browser navigations in new tabs (intents, tabs opened through
3076 // "Open link in new tab"). If the navigation must wait on the current
3077 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3078 // will be updated when the BeforeUnload ack is received.
3079 base::TimeTicks navigation_start = base::TimeTicks::Now();
3080
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513081 mojom::NavigationType navigation_type =
Camille Lamy5193caa2018-10-12 11:59:423082 GetNavigationType(node->current_url(), // old_url
3083 url_to_load, // new_url
3084 reload_type, // reload_type
3085 entry, // entry
3086 *frame_entry, // frame_entry
3087 false); // is_same_document_history_load
3088
3089 // Create the NavigationParams based on |params|.
3090
3091 bool is_view_source_mode = virtual_url.SchemeIs(kViewSourceScheme);
Charlie Harrison8c113a32019-01-07 16:08:293092
3093 // Update |download_policy| if the virtual URL is view-source. Why do this
3094 // now? Possibly the URL could be rewritten to a view-source via some URL
3095 // handler.
3096 if (is_view_source_mode)
Yao Xiaoaf79ca9b2019-03-12 19:54:183097 download_policy.SetDisallowed(NavigationDownloadType::kViewSource);
Charlie Harrison8c113a32019-01-07 16:08:293098
Camille Lamy5193caa2018-10-12 11:59:423099 const GURL& history_url_for_data_url =
3100 params.base_url_for_data_url.is_empty() ? GURL() : virtual_url;
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513101 mojom::CommonNavigationParamsPtr common_params =
3102 mojom::CommonNavigationParams::New(
3103 url_to_load, params.initiator_origin,
3104 blink::mojom::Referrer::New(params.referrer.url,
3105 params.referrer.policy),
3106 params.transition_type, navigation_type, download_policy,
3107 should_replace_current_entry, params.base_url_for_data_url,
3108 history_url_for_data_url, previews_state, navigation_start,
3109 params.load_type == LOAD_TYPE_HTTP_POST ? "POST" : "GET",
3110 params.post_data, base::Optional<SourceLocation>(),
3111 params.started_from_context_menu, has_user_gesture,
3112 InitiatorCSPInfo(), std::vector<int>(), params.href_translate,
3113 false /* is_history_navigation_in_new_child_frame */,
3114 params.input_start);
Camille Lamy5193caa2018-10-12 11:59:423115
Lucas Furukawa Gadania9c45682019-07-31 22:05:143116 mojom::CommitNavigationParamsPtr commit_params =
3117 mojom::CommitNavigationParams::New(
3118 frame_entry->committed_origin(), override_user_agent,
Lucas Furukawa Gadani81e294b2019-08-29 16:26:323119 params.redirect_chain,
3120 std::vector<network::mojom::URLResponseHeadPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143121 std::vector<net::RedirectInfo>(),
3122 std::string() /* post_content_type */, common_params->url,
3123 common_params->method, params.can_load_local_resources,
3124 frame_entry->page_state(), entry->GetUniqueID(),
3125 entry->GetSubframeUniqueNames(node), true /* intended_as_new_entry */,
3126 -1 /* pending_history_list_offset */,
3127 params.should_clear_history_list ? -1 : GetLastCommittedEntryIndex(),
3128 params.should_clear_history_list ? 0 : GetEntryCount(),
3129 false /* was_discarded */, is_view_source_mode,
3130 params.should_clear_history_list, mojom::NavigationTiming::New(),
3131 base::nullopt /* appcache_host_id */,
3132 mojom::WasActivatedOption::kUnknown,
3133 base::UnguessableToken::Create() /* navigation_token */,
Lucas Furukawa Gadani97ea6382019-08-07 19:14:513134 std::vector<mojom::PrefetchedSignedExchangeInfoPtr>(),
Lucas Furukawa Gadania9c45682019-07-31 22:05:143135#if defined(OS_ANDROID)
3136 std::string(), /* data_url_as_string */
3137#endif
Mike West136c0172019-08-23 08:52:123138 false, /* is_browser_initiated */
3139 network::mojom::IPAddressSpace::kUnknown);
Camille Lamy5193caa2018-10-12 11:59:423140#if defined(OS_ANDROID)
3141 if (ValidateDataURLAsString(params.data_url_as_string)) {
Lucas Furukawa Gadania9c45682019-07-31 22:05:143142 commit_params->data_url_as_string = params.data_url_as_string->data();
Camille Lamy5193caa2018-10-12 11:59:423143 }
3144#endif
3145
Lucas Furukawa Gadania9c45682019-07-31 22:05:143146 commit_params->was_activated = params.was_activated;
Camille Lamy5193caa2018-10-12 11:59:423147
3148 // A form submission may happen here if the navigation is a renderer-initiated
3149 // form submission that took the OpenURL path.
3150 scoped_refptr<network::ResourceRequestBody> request_body = params.post_data;
3151
3152 // extra_headers in params are \n separated; NavigationRequests want \r\n.
3153 std::string extra_headers_crlf;
3154 base::ReplaceChars(params.extra_headers, "\n", "\r\n", &extra_headers_crlf);
Yao Xiaodc5ed102019-06-04 19:19:093155
3156 auto navigation_request = NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143157 node, std::move(common_params), std::move(commit_params),
John Delaney131ad362019-08-08 21:57:413158 !params.is_renderer_initiated, extra_headers_crlf, frame_entry, entry,
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513159 request_body,
Camille Lamy5193caa2018-10-12 11:59:423160 params.navigation_ui_data ? params.navigation_ui_data->Clone() : nullptr);
Yao Xiaodc5ed102019-06-04 19:19:093161 navigation_request->set_from_download_cross_origin_redirect(
3162 params.from_download_cross_origin_redirect);
3163 return navigation_request;
Camille Lamy5193caa2018-10-12 11:59:423164}
3165
3166std::unique_ptr<NavigationRequest>
3167NavigationControllerImpl::CreateNavigationRequestFromEntry(
clamyea99ea12018-05-28 13:54:233168 FrameTreeNode* frame_tree_node,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573169 NavigationEntryImpl* entry,
clamyea99ea12018-05-28 13:54:233170 FrameNavigationEntry* frame_entry,
3171 ReloadType reload_type,
3172 bool is_same_document_history_load,
Arthur Hemerybee4a752019-05-29 10:50:553173 bool is_history_navigation_in_new_child_frame) {
clamyea99ea12018-05-28 13:54:233174 GURL dest_url = frame_entry->url();
Nasko Oskov03912102019-01-11 00:21:323175 base::Optional<url::Origin> origin_to_commit =
3176 frame_entry->committed_origin();
3177
clamyea99ea12018-05-28 13:54:233178 Referrer dest_referrer = frame_entry->referrer();
Ryan Sturmc4da1992018-07-17 16:59:013179 if (reload_type == ReloadType::ORIGINAL_REQUEST_URL &&
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573180 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
clamyea99ea12018-05-28 13:54:233181 // We may have been redirected when navigating to the current URL.
3182 // Use the URL the user originally intended to visit as signaled by the
3183 // ReloadType, if it's valid and if a POST wasn't involved; the latter
Ryan Sturmc4da1992018-07-17 16:59:013184 // case avoids issues with sending data to the wrong page.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573185 dest_url = entry->GetOriginalRequestURL();
clamyea99ea12018-05-28 13:54:233186 dest_referrer = Referrer();
Nasko Oskov03912102019-01-11 00:21:323187 origin_to_commit.reset();
clamyea99ea12018-05-28 13:54:233188 }
3189
Ehsan Karamad44fc72112019-02-26 18:15:473190 if (frame_tree_node->render_manager()->is_attaching_inner_delegate()) {
3191 // Avoid starting any new navigations since this node is now preparing for
3192 // attaching an inner delegate.
3193 return nullptr;
Ehsan Karamaddd9a4142018-12-04 20:38:203194 }
3195
Camille Lamy5193caa2018-10-12 11:59:423196 if (!IsValidURLForNavigation(frame_tree_node->IsMainFrame(),
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573197 entry->GetVirtualURL(), dest_url)) {
clamyea99ea12018-05-28 13:54:233198 return nullptr;
3199 }
3200
Nasko Oskov03912102019-01-11 00:21:323201 if (!DoesURLMatchOriginForNavigation(dest_url, origin_to_commit)) {
3202 DCHECK(false) << " url:" << dest_url
3203 << " origin:" << origin_to_commit.value();
3204 return nullptr;
3205 }
3206
clamyea99ea12018-05-28 13:54:233207 // Determine if Previews should be used for the navigation.
3208 PreviewsState previews_state = PREVIEWS_UNSPECIFIED;
3209 if (!frame_tree_node->IsMainFrame()) {
3210 // For subframes, use the state of the top-level frame.
3211 previews_state = frame_tree_node->frame_tree()
3212 ->root()
3213 ->current_frame_host()
3214 ->last_navigation_previews_state();
clamyea99ea12018-05-28 13:54:233215 }
3216
clamyea99ea12018-05-28 13:54:233217 // This will be used to set the Navigation Timing API navigationStart
3218 // parameter for browser navigations in new tabs (intents, tabs opened through
3219 // "Open link in new tab"). If the navigation must wait on the current
3220 // RenderFrameHost to execute its BeforeUnload event, the navigation start
3221 // will be updated when the BeforeUnload ack is received.
3222 base::TimeTicks navigation_start = base::TimeTicks::Now();
clamyea99ea12018-05-28 13:54:233223
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513224 mojom::NavigationType navigation_type = GetNavigationType(
clamyea99ea12018-05-28 13:54:233225 frame_tree_node->current_url(), // old_url
3226 dest_url, // new_url
3227 reload_type, // reload_type
3228 entry, // entry
3229 *frame_entry, // frame_entry
3230 is_same_document_history_load); // is_same_document_history_load
Camille Lamy5193caa2018-10-12 11:59:423231
3232 // A form submission may happen here if the navigation is a
3233 // back/forward/reload navigation that does a form resubmission.
3234 scoped_refptr<network::ResourceRequestBody> request_body;
3235 std::string post_content_type;
3236 if (frame_entry->method() == "POST") {
3237 request_body = frame_entry->GetPostData(&post_content_type);
3238 // Might have a LF at end.
3239 post_content_type =
3240 base::TrimWhitespaceASCII(post_content_type, base::TRIM_ALL)
3241 .as_string();
3242 }
3243
3244 // Create the NavigationParams based on |entry| and |frame_entry|.
Lucas Furukawa Gadanief8290a2019-07-29 20:27:513245 mojom::CommonNavigationParamsPtr common_params =
3246 entry->ConstructCommonNavigationParams(
3247 *frame_entry, request_body, dest_url,
3248 blink::mojom::Referrer::New(dest_referrer.url, dest_referrer.policy),
3249 navigation_type, previews_state, navigation_start,
3250 base::TimeTicks() /* input_start */);
3251 common_params->is_history_navigation_in_new_child_frame =
Arthur Hemerybee4a752019-05-29 10:50:553252 is_history_navigation_in_new_child_frame;
Camille Lamy5193caa2018-10-12 11:59:423253
3254 // TODO(clamy): |intended_as_new_entry| below should always be false once
3255 // Reload no longer leads to this being called for a pending NavigationEntry
3256 // of index -1.
Lucas Furukawa Gadania9c45682019-07-31 22:05:143257 mojom::CommitNavigationParamsPtr commit_params =
3258 entry->ConstructCommitNavigationParams(
3259 *frame_entry, common_params->url, origin_to_commit,
3260 common_params->method, entry->GetSubframeUniqueNames(frame_tree_node),
3261 GetPendingEntryIndex() == -1 /* intended_as_new_entry */,
3262 GetIndexOfEntry(entry), GetLastCommittedEntryIndex(),
3263 GetEntryCount());
3264 commit_params->post_content_type = post_content_type;
Camille Lamy5193caa2018-10-12 11:59:423265
clamyea99ea12018-05-28 13:54:233266 return NavigationRequest::CreateBrowserInitiated(
Lucas Furukawa Gadania9c45682019-07-31 22:05:143267 frame_tree_node, std::move(common_params), std::move(commit_params),
John Delaney131ad362019-08-08 21:57:413268 !entry->is_renderer_initiated(), entry->extra_headers(), frame_entry,
Camille Lamy5193caa2018-10-12 11:59:423269 entry, request_body, nullptr /* navigation_ui_data */);
clamyea99ea12018-05-28 13:54:233270}
3271
[email protected]d202a7c2012-01-04 07:53:473272void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:213273 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:273274 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:403275
[email protected]2db9bd72012-04-13 20:20:563276 // We need to notify the ssl_manager_ before the web_contents_ so the
[email protected]df1af242009-05-01 00:11:403277 // location bar will have up-to-date information about the security style
3278 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:133279 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:403280
[email protected]7f924832014-08-09 05:57:223281 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:573282 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:463283
[email protected]b0f724c2013-09-05 04:21:133284 // TODO(avi): Remove. http://crbug.com/170921
3285 NotificationDetails notification_details =
3286 Details<LoadCommittedDetails>(details);
[email protected]8ff00d72012-10-23 19:12:213287 NotificationService::current()->Notify(
3288 NOTIFICATION_NAV_ENTRY_COMMITTED,
3289 Source<NavigationController>(this),
[email protected]df1af242009-05-01 00:11:403290 notification_details);
initial.commit09911bf2008-07-26 23:55:293291}
3292
initial.commit09911bf2008-07-26 23:55:293293// static
[email protected]d202a7c2012-01-04 07:53:473294size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:233295 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
3296 return max_entry_count_for_testing_;
[email protected]8ff00d72012-10-23 19:12:213297 return kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:233298}
3299
[email protected]d202a7c2012-01-04 07:53:473300void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:223301 if (is_active && needs_reload_)
3302 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:293303}
3304
[email protected]d202a7c2012-01-04 07:53:473305void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:293306 if (!needs_reload_)
3307 return;
3308
Bo Liucdfa4b12018-11-06 00:21:443309 UMA_HISTOGRAM_ENUMERATION("Navigation.LoadIfNecessaryType",
3310 needs_reload_type_);
3311
initial.commit09911bf2008-07-26 23:55:293312 // Calling Reload() results in ignoring state, and not loading.
3313 // Explicitly use NavigateToPendingEntry so that the renderer uses the
3314 // cached state.
avicc872d7242015-08-19 21:26:343315 if (pending_entry_) {
Dave Tapuska8bfd84c2019-03-26 20:47:163316 NavigateToExistingPendingEntry(ReloadType::NONE,
3317 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343318 } else if (last_committed_entry_index_ != -1) {
arthursonzogni5c4c202d2017-04-25 23:41:273319 pending_entry_ = entries_[last_committed_entry_index_].get();
avicc872d7242015-08-19 21:26:343320 pending_entry_index_ = last_committed_entry_index_;
Dave Tapuska8bfd84c2019-03-26 20:47:163321 NavigateToExistingPendingEntry(ReloadType::NONE,
3322 FrameTreeNode::kFrameTreeNodeInvalidId);
avicc872d7242015-08-19 21:26:343323 } else {
3324 // If there is something to reload, the successful reload will clear the
3325 // |needs_reload_| flag. Otherwise, just do it here.
3326 needs_reload_ = false;
3327 }
initial.commit09911bf2008-07-26 23:55:293328}
3329
John Delaney131ad362019-08-08 21:57:413330void NavigationControllerImpl::LoadErrorPage(RenderFrameHost* render_frame_host,
3331 const GURL& url,
3332 const std::string& error_page_html,
3333 net::Error error) {
3334 FrameTreeNode* node =
3335 static_cast<RenderFrameHostImpl*>(render_frame_host)->frame_tree_node();
3336
3337 mojom::CommonNavigationParamsPtr common_params =
3338 CreateCommonNavigationParams();
3339 common_params->url = url;
3340 mojom::CommitNavigationParamsPtr commit_params =
3341 CreateCommitNavigationParams();
3342
3343 std::unique_ptr<NavigationRequest> navigation_request =
3344 NavigationRequest::CreateBrowserInitiated(
3345 node, std::move(common_params), std::move(commit_params),
3346 true /* browser_initiated */, "" /* extra_headers */,
3347 nullptr /* frame_entry */, nullptr /* entry */,
3348 nullptr /* post_body */, nullptr /* navigation_ui_data */);
3349 navigation_request->set_error_page_html(error_page_html);
3350 navigation_request->set_net_error(error);
3351 node->CreatedNavigationRequest(std::move(navigation_request));
3352 DCHECK(node->navigation_request());
3353 node->navigation_request()->BeginNavigation();
3354}
3355
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573356void NavigationControllerImpl::NotifyEntryChanged(NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:213357 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:093358 det.changed_entry = entry;
avi47179332015-05-20 21:01:113359 det.index = GetIndexOfEntry(
3360 NavigationEntryImpl::FromNavigationEntry(entry));
Sam McNally5c087a32017-08-25 01:46:143361 delegate_->NotifyNavigationEntryChanged(det);
initial.commit09911bf2008-07-26 23:55:293362}
3363
[email protected]d202a7c2012-01-04 07:53:473364void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:363365 RestoreType type) {
[email protected]a26023822011-12-29 00:23:553366 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:363367 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:293368
initial.commit09911bf2008-07-26 23:55:293369 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:293370}
[email protected]765b35502008-08-21 00:51:203371
arthursonzogni69a6a1b2019-09-17 09:23:003372void NavigationControllerImpl::DiscardNonCommittedEntries() {
avi45a72532015-04-07 21:01:453373 DiscardPendingEntry(false);
[email protected]b12eb222013-09-10 00:11:483374 DiscardTransientEntry();
arthursonzogni69a6a1b2019-09-17 09:23:003375 if (delegate_)
3376 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]b12eb222013-09-10 00:11:483377}
3378
[email protected]d202a7c2012-01-04 07:53:473379void NavigationControllerImpl::DiscardTransientEntry() {
[email protected]cbab76d2008-10-13 22:42:473380 if (transient_entry_index_ == -1)
3381 return;
[email protected]a0e69262009-06-03 19:08:483382 entries_.erase(entries_.begin() + transient_entry_index_);
[email protected]80858db52009-10-15 00:35:183383 if (last_committed_entry_index_ > transient_entry_index_)
3384 last_committed_entry_index_--;
arthursonzogni5c4c202d2017-04-25 23:41:273385 if (pending_entry_index_ > transient_entry_index_)
3386 pending_entry_index_--;
[email protected]cbab76d2008-10-13 22:42:473387 transient_entry_index_ = -1;
[email protected]765b35502008-08-21 00:51:203388}
3389
avi7c6f35e2015-05-08 17:52:383390int NavigationControllerImpl::GetEntryIndexWithUniqueID(
3391 int nav_entry_id) const {
3392 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
3393 if (entries_[i]->GetUniqueID() == nav_entry_id)
3394 return i;
3395 }
3396 return -1;
3397}
3398
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573399NavigationEntryImpl* NavigationControllerImpl::GetTransientEntry() {
[email protected]cbab76d2008-10-13 22:42:473400 if (transient_entry_index_ == -1)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:283401 return nullptr;
avif16f85a72015-11-13 18:25:033402 return entries_[transient_entry_index_].get();
[email protected]cbab76d2008-10-13 22:42:473403}
[email protected]e1cd5452010-08-26 18:03:253404
avi25764702015-06-23 15:43:373405void NavigationControllerImpl::SetTransientEntry(
dcheng9bfa5162016-04-09 01:00:573406 std::unique_ptr<NavigationEntry> entry) {
[email protected]0b684262013-02-20 02:18:213407 // Discard any current transient entry, we can only have one at a time.
Piotr Kalinowski2cccb522019-07-09 12:10:513408 DiscardTransientEntry();
[email protected]0b684262013-02-20 02:18:213409 int index = 0;
3410 if (last_committed_entry_index_ != -1)
3411 index = last_committed_entry_index_ + 1;
avi25764702015-06-23 15:43:373412 entries_.insert(entries_.begin() + index,
dcheng36b6aec92015-12-26 06:16:363413 NavigationEntryImpl::FromNavigationEntry(std::move(entry)));
arthursonzogni5c4c202d2017-04-25 23:41:273414 if (pending_entry_index_ >= index)
3415 pending_entry_index_++;
[email protected]0b684262013-02-20 02:18:213416 transient_entry_index_ = index;
[email protected]7f924832014-08-09 05:57:223417 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]0b684262013-02-20 02:18:213418}
3419
[email protected]d202a7c2012-01-04 07:53:473420void NavigationControllerImpl::InsertEntriesFrom(
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573421 NavigationControllerImpl* source,
[email protected]e1cd5452010-08-26 18:03:253422 int max_index) {
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573423 DCHECK_LE(max_index, source->GetEntryCount());
[email protected]e1cd5452010-08-26 18:03:253424 size_t insert_index = 0;
3425 for (int i = 0; i < max_index; i++) {
creisef4a0cb2015-03-12 19:14:353426 // When cloning a tab, copy all entries except interstitial pages.
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573427 if (source->entries_[i]->GetPageType() != PAGE_TYPE_INTERSTITIAL) {
creisef4a0cb2015-03-12 19:14:353428 // TODO(creis): Once we start sharing FrameNavigationEntries between
3429 // NavigationEntries, it will not be safe to share them with another tab.
3430 // Must have a version of Clone that recreates them.
[email protected]e1cd5452010-08-26 18:03:253431 entries_.insert(entries_.begin() + insert_index++,
Lucas Furukawa Gadani5553a1582019-01-08 18:55:573432 source->entries_[i]->Clone());
[email protected]e1cd5452010-08-26 18:03:253433 }
3434 }
arthursonzogni5c4c202d2017-04-25 23:41:273435 DCHECK(pending_entry_index_ == -1 ||
3436 pending_entry_ == GetEntryAtIndex(pending_entry_index_));
[email protected]e1cd5452010-08-26 18:03:253437}
[email protected]c5b88d82012-10-06 17:03:333438
3439void NavigationControllerImpl::SetGetTimestampCallbackForTest(
3440 const base::Callback<base::Time()>& get_timestamp_callback) {
3441 get_timestamp_callback_ = get_timestamp_callback;
3442}
[email protected]8ff00d72012-10-23 19:12:213443
Shivani Sharmaffb32b82019-04-09 16:58:473444// History manipulation intervention:
3445void NavigationControllerImpl::SetShouldSkipOnBackForwardUIIfNeeded(
3446 RenderFrameHostImpl* rfh,
3447 bool replace_entry,
3448 bool previous_document_was_activated,
3449 bool is_renderer_initiated) {
Shivani Sharma712d5d72019-04-16 21:56:453450 // Note that for a subframe, previous_document_was_activated is true if the
3451 // gesture happened in any subframe (propagated to main frame) or in the main
3452 // frame itself.
Shivani Sharma985474f2019-05-23 21:36:473453 // TODO(crbug.com/934637): Remove the check for HadInnerWebContents() when
3454 // pdf and any inner web contents user gesture is properly propagated. This is
3455 // a temporary fix for history intervention to be disabled for pdfs
3456 // (crbug.com/965434).
Shivani Sharmaffb32b82019-04-09 16:58:473457 if (replace_entry || previous_document_was_activated ||
Shivani Sharma985474f2019-05-23 21:36:473458 !is_renderer_initiated || delegate_->HadInnerWebContents()) {
Shivani Sharmaffb32b82019-04-09 16:58:473459 if (last_committed_entry_index_ != -1) {
3460 UMA_HISTOGRAM_BOOLEAN(
3461 "Navigation.BackForward.SetShouldSkipOnBackForwardUI", false);
3462 }
3463 return;
3464 }
3465 if (last_committed_entry_index_ == -1)
3466 return;
3467
Shivani Sharmac4cc8922019-04-18 03:11:173468 SetSkippableForSameDocumentEntries(last_committed_entry_index_, true);
Shivani Sharmaffb32b82019-04-09 16:58:473469 UMA_HISTOGRAM_BOOLEAN("Navigation.BackForward.SetShouldSkipOnBackForwardUI",
3470 true);
3471
3472 // Log UKM with the URL we are navigating away from. Note that
3473 // GetUkmSourceIdForLastCommittedSource looks into the WebContents to get
3474 // the last committed source. Since WebContents has not yet been updated
3475 // with the current URL being committed, this should give the correct source
3476 // even though |rfh| here belongs to the current navigation.
3477 ukm::SourceId source_id =
3478 rfh->delegate()->GetUkmSourceIdForLastCommittedSource();
3479 ukm::builders::HistoryManipulationIntervention(source_id).Record(
3480 ukm::UkmRecorder::Get());
3481}
3482
Shivani Sharmac4cc8922019-04-18 03:11:173483void NavigationControllerImpl::SetSkippableForSameDocumentEntries(
3484 int reference_index,
3485 bool skippable) {
3486 auto* reference_entry = GetEntryAtIndex(reference_index);
3487 reference_entry->set_should_skip_on_back_forward_ui(skippable);
3488
3489 int64_t document_sequence_number =
3490 reference_entry->root_node()->frame_entry->document_sequence_number();
3491 for (int index = 0; index < GetEntryCount(); index++) {
3492 auto* entry = GetEntryAtIndex(index);
3493 if (entry->root_node()->frame_entry->document_sequence_number() ==
3494 document_sequence_number) {
3495 entry->set_should_skip_on_back_forward_ui(skippable);
3496 }
3497 }
3498}
3499
[email protected]8ff00d72012-10-23 19:12:213500} // namespace content