blob: 7afd29306bfefdeb8899adf65fc42e395851b881 [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
[email protected]c5b88d82012-10-06 17:03:3338#include "base/bind.h"
[email protected]eabfe1912014-05-12 10:07:2839#include "base/command_line.h"
initial.commit09911bf2008-07-26 23:55:2940#include "base/logging.h"
[email protected]d6def3d2014-05-19 18:55:3241#include "base/metrics/histogram.h"
[email protected]348fbaac2013-06-11 06:31:5142#include "base/strings/string_number_conversions.h" // Temporary
43#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0044#include "base/strings/utf_string_conversions.h"
[email protected]a43858f2013-06-28 15:18:3745#include "base/time/time.h"
ssid3e765612015-01-28 04:03:4246#include "base/trace_event/trace_event.h"
servolkf3955532015-05-16 00:01:5947#include "build/build_config.h"
[email protected]eabfe1912014-05-12 10:07:2848#include "cc/base/switches.h"
rsleevic327b48f82015-04-30 02:03:2549#include "components/mime_util/mime_util.h"
jamescookda2505812015-03-20 18:01:1850#include "content/browser/bad_message.h"
[email protected]825b1662012-03-12 19:07:3151#include "content/browser/browser_url_handler_impl.h"
[email protected]5f2aa722013-08-07 16:59:4152#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0453#include "content/browser/dom_storage/session_storage_namespace_impl.h"
[email protected]d4a8ca482013-10-30 21:06:4054#include "content/browser/frame_host/debug_urls.h"
55#include "content/browser/frame_host/interstitial_page_impl.h"
56#include "content/browser/frame_host/navigation_entry_impl.h"
[email protected]d1581d72013-11-05 19:11:2657#include "content/browser/frame_host/navigation_entry_screenshot_manager.h"
creis4e2ecb72015-06-20 00:46:3058#include "content/browser/frame_host/navigator.h"
[email protected]b3c41c0b2012-03-06 15:48:3259#include "content/browser/renderer_host/render_view_host_impl.h" // Temporary
[email protected]b6583592012-01-25 19:52:3360#include "content/browser/site_instance_impl.h"
[email protected]37567b432014-02-12 01:12:2261#include "content/common/frame_messages.h"
[email protected]0aed2f52011-03-23 18:06:3662#include "content/common/view_messages.h"
[email protected]ccb797302011-12-15 16:55:1163#include "content/public/browser/browser_context.h"
[email protected]d1198fd2012-08-13 22:50:1964#include "content/public/browser/content_browser_client.h"
[email protected]d9083482012-01-06 00:38:4665#include "content/public/browser/invalidate_type.h"
[email protected]5b96836f2011-12-22 07:39:0066#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3867#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1668#include "content/public/browser/notification_types.h"
[email protected]9677a3c2012-12-22 04:18:5869#include "content/public/browser/render_widget_host.h"
70#include "content/public/browser/render_widget_host_view.h"
[email protected]4c3a23582012-08-18 08:54:3471#include "content/public/browser/storage_partition.h"
[email protected]7f6f44c2011-12-14 13:23:3872#include "content/public/browser/user_metrics.h"
[email protected]d1198fd2012-08-13 22:50:1973#include "content/public/common/content_client.h"
[email protected]7f6f44c2011-12-14 13:23:3874#include "content/public/common/content_constants.h"
avi9f07a0c2015-02-18 22:51:2975#include "content/public/common/content_switches.h"
servolkf3955532015-05-16 00:01:5976#include "media/base/mime_util.h"
[email protected]a23de8572009-06-03 02:16:3277#include "net/base/escape.h"
[email protected]7f070d42011-03-09 20:25:3278#include "net/base/net_util.h"
[email protected]9677a3c2012-12-22 04:18:5879#include "skia/ext/platform_canvas.h"
[email protected]cca6f392014-05-28 21:32:2680#include "url/url_constants.h"
initial.commit09911bf2008-07-26 23:55:2981
[email protected]8ff00d72012-10-23 19:12:2182namespace content {
[email protected]e9ba4472008-09-14 15:42:4383namespace {
84
85// Invoked when entries have been pruned, or removed. For example, if the
86// current entries are [google, digg, yahoo], with the current entry google,
87// and the user types in cnet, then digg and yahoo are pruned.
[email protected]d202a7c2012-01-04 07:53:4788void NotifyPrunedEntries(NavigationControllerImpl* nav_controller,
[email protected]c12bf1a12008-09-17 16:28:4989 bool from_front,
90 int count) {
[email protected]8ff00d72012-10-23 19:12:2191 PrunedDetails details;
[email protected]c12bf1a12008-09-17 16:28:4992 details.from_front = from_front;
93 details.count = count;
[email protected]8ff00d72012-10-23 19:12:2194 NotificationService::current()->Notify(
95 NOTIFICATION_NAV_LIST_PRUNED,
96 Source<NavigationController>(nav_controller),
97 Details<PrunedDetails>(&details));
[email protected]e9ba4472008-09-14 15:42:4398}
99
100// Ensure the given NavigationEntry has a valid state, so that WebKit does not
101// get confused if we navigate back to it.
[email protected]40bcc302009-03-02 20:50:39102//
[email protected]e9ba4472008-09-14 15:42:43103// An empty state is treated as a new navigation by WebKit, which would mean
104// losing the navigation entries and generating a new navigation entry after
105// this one. We don't want that. To avoid this we create a valid state which
106// WebKit will not treat as a new navigation.
[email protected]691aa2f2013-05-28 22:52:04107void SetPageStateIfEmpty(NavigationEntryImpl* entry) {
108 if (!entry->GetPageState().IsValid())
109 entry->SetPageState(PageState::CreateFromURL(entry->GetURL()));
[email protected]e9ba4472008-09-14 15:42:43110}
111
[email protected]2ca1ea662012-10-04 02:26:36112NavigationEntryImpl::RestoreType ControllerRestoreTypeToEntryType(
113 NavigationController::RestoreType type) {
114 switch (type) {
115 case NavigationController::RESTORE_CURRENT_SESSION:
116 return NavigationEntryImpl::RESTORE_CURRENT_SESSION;
117 case NavigationController::RESTORE_LAST_SESSION_EXITED_CLEANLY:
118 return NavigationEntryImpl::RESTORE_LAST_SESSION_EXITED_CLEANLY;
119 case NavigationController::RESTORE_LAST_SESSION_CRASHED:
120 return NavigationEntryImpl::RESTORE_LAST_SESSION_CRASHED;
121 }
122 NOTREACHED();
123 return NavigationEntryImpl::RESTORE_CURRENT_SESSION;
124}
125
[email protected]e9ba4472008-09-14 15:42:43126// Configure all the NavigationEntries in entries for restore. This resets
127// the transition type to reload and makes sure the content state isn't empty.
128void ConfigureEntriesForRestore(
avi25764702015-06-23 15:43:37129 ScopedVector<NavigationEntryImpl>* entries,
[email protected]2ca1ea662012-10-04 02:26:36130 NavigationController::RestoreType type) {
[email protected]e9ba4472008-09-14 15:42:43131 for (size_t i = 0; i < entries->size(); ++i) {
132 // Use a transition type of reload so that we don't incorrectly increase
133 // the typed count.
Sylvain Defresnec6ccc77d2014-09-19 10:19:35134 (*entries)[i]->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
[email protected]2ca1ea662012-10-04 02:26:36135 (*entries)[i]->set_restore_type(ControllerRestoreTypeToEntryType(type));
[email protected]e9ba4472008-09-14 15:42:43136 // NOTE(darin): This code is only needed for backwards compat.
avi25764702015-06-23 15:43:37137 SetPageStateIfEmpty((*entries)[i]);
[email protected]e9ba4472008-09-14 15:42:43138 }
139}
140
[email protected]bf70edce2012-06-20 22:32:22141// Determines whether or not we should be carrying over a user agent override
142// between two NavigationEntries.
[email protected]8ff00d72012-10-23 19:12:21143bool ShouldKeepOverride(const NavigationEntry* last_entry) {
[email protected]bf70edce2012-06-20 22:32:22144 return last_entry && last_entry->GetIsOverridingUserAgent();
145}
146
[email protected]e9ba4472008-09-14 15:42:43147} // namespace
148
[email protected]d202a7c2012-01-04 07:53:47149// NavigationControllerImpl ----------------------------------------------------
initial.commit09911bf2008-07-26 23:55:29150
[email protected]23a918b2014-07-15 09:51:36151const size_t kMaxEntryCountForTestingNotSet = static_cast<size_t>(-1);
[email protected]9b51970d2011-12-09 23:10:23152
[email protected]765b35502008-08-21 00:51:20153// static
[email protected]d202a7c2012-01-04 07:53:47154size_t NavigationControllerImpl::max_entry_count_for_testing_ =
[email protected]9b51970d2011-12-09 23:10:23155 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20156
[email protected]e6fec472013-05-14 05:29:02157// Should Reload check for post data? The default is true, but is set to false
[email protected]cdcb1dee2012-01-04 00:46:20158// when testing.
159static bool g_check_for_repost = true;
initial.commit09911bf2008-07-26 23:55:29160
[email protected]71fde352011-12-29 03:29:56161// static
avi25764702015-06-23 15:43:37162scoped_ptr<NavigationEntry> NavigationController::CreateNavigationEntry(
[email protected]71fde352011-12-29 03:29:56163 const GURL& url,
[email protected]cdcb1dee2012-01-04 00:46:20164 const Referrer& referrer,
Sylvain Defresnec6ccc77d2014-09-19 10:19:35165 ui::PageTransition transition,
[email protected]71fde352011-12-29 03:29:56166 bool is_renderer_initiated,
167 const std::string& extra_headers,
[email protected]cdcb1dee2012-01-04 00:46:20168 BrowserContext* browser_context) {
creis94a977f62015-02-18 23:51:05169 // Fix up the given URL before letting it be rewritten, so that any minor
170 // cleanup (e.g., removing leading dots) will not lead to a virtual URL.
171 GURL dest_url(url);
172 BrowserURLHandlerImpl::GetInstance()->FixupURLBeforeRewrite(&dest_url,
173 browser_context);
174
[email protected]71fde352011-12-29 03:29:56175 // Allow the browser URL handler to rewrite the URL. This will, for example,
176 // remove "view-source:" from the beginning of the URL to get the URL that
177 // will actually be loaded. This real URL won't be shown to the user, just
178 // used internally.
creis94a977f62015-02-18 23:51:05179 GURL loaded_url(dest_url);
[email protected]71fde352011-12-29 03:29:56180 bool reverse_on_redirect = false;
[email protected]825b1662012-03-12 19:07:31181 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
[email protected]71fde352011-12-29 03:29:56182 &loaded_url, browser_context, &reverse_on_redirect);
183
184 NavigationEntryImpl* entry = new NavigationEntryImpl(
185 NULL, // The site instance for tabs is sent on navigation
[email protected]2db9bd72012-04-13 20:20:56186 // (WebContents::GetSiteInstance).
[email protected]71fde352011-12-29 03:29:56187 -1,
188 loaded_url,
189 referrer,
[email protected]fcf75d42013-12-03 20:11:26190 base::string16(),
[email protected]71fde352011-12-29 03:29:56191 transition,
192 is_renderer_initiated);
creis94a977f62015-02-18 23:51:05193 entry->SetVirtualURL(dest_url);
194 entry->set_user_typed_url(dest_url);
[email protected]71fde352011-12-29 03:29:56195 entry->set_update_virtual_url_with_url(reverse_on_redirect);
196 entry->set_extra_headers(extra_headers);
avi25764702015-06-23 15:43:37197 return make_scoped_ptr(entry);
[email protected]71fde352011-12-29 03:29:56198}
199
[email protected]cdcb1dee2012-01-04 00:46:20200// static
201void NavigationController::DisablePromptOnRepost() {
202 g_check_for_repost = false;
203}
204
[email protected]c5b88d82012-10-06 17:03:33205base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
206 base::Time t) {
207 // If |t| is between the water marks, we're in a run of duplicates
208 // or just getting out of it, so increase the high-water mark to get
209 // a time that probably hasn't been used before and return it.
210 if (low_water_mark_ <= t && t <= high_water_mark_) {
211 high_water_mark_ += base::TimeDelta::FromMicroseconds(1);
212 return high_water_mark_;
213 }
214
215 // Otherwise, we're clear of the last duplicate run, so reset the
216 // water marks.
217 low_water_mark_ = high_water_mark_ = t;
218 return t;
219}
220
[email protected]d202a7c2012-01-04 07:53:47221NavigationControllerImpl::NavigationControllerImpl(
[email protected]ec6c05f2013-10-23 18:41:57222 NavigationControllerDelegate* delegate,
[email protected]d1198fd2012-08-13 22:50:19223 BrowserContext* browser_context)
[email protected]3d7474ff2011-07-27 17:47:37224 : browser_context_(browser_context),
[email protected]765b35502008-08-21 00:51:20225 pending_entry_(NULL),
avi45a72532015-04-07 21:01:45226 failed_pending_entry_id_(0),
227 failed_pending_entry_should_replace_(false),
[email protected]765b35502008-08-21 00:51:20228 last_committed_entry_index_(-1),
229 pending_entry_index_(-1),
[email protected]cbab76d2008-10-13 22:42:47230 transient_entry_index_(-1),
[email protected]ec6c05f2013-10-23 18:41:57231 delegate_(delegate),
initial.commit09911bf2008-07-26 23:55:29232 max_restored_page_id_(-1),
[email protected]69e797f2013-04-30 01:10:22233 ssl_manager_(this),
[email protected]38b8f4e2009-09-24 19:44:57234 needs_reload_(false),
[email protected]27ba81c2012-08-21 17:04:09235 is_initial_navigation_(true),
[email protected]40fd3032014-02-28 22:16:28236 in_navigate_to_pending_entry_(false),
[email protected]c5b88d82012-10-06 17:03:33237 pending_reload_(NO_RELOAD),
[email protected]8cea7892013-02-06 06:49:31238 get_timestamp_callback_(base::Bind(&base::Time::Now)),
[email protected]d1581d72013-11-05 19:11:26239 screenshot_manager_(new NavigationEntryScreenshotManager(this)) {
[email protected]3d7474ff2011-07-27 17:47:37240 DCHECK(browser_context_);
initial.commit09911bf2008-07-26 23:55:29241}
242
[email protected]d202a7c2012-01-04 07:53:47243NavigationControllerImpl::~NavigationControllerImpl() {
[email protected]cbab76d2008-10-13 22:42:47244 DiscardNonCommittedEntriesInternal();
initial.commit09911bf2008-07-26 23:55:29245}
246
[email protected]d202a7c2012-01-04 07:53:47247WebContents* NavigationControllerImpl::GetWebContents() const {
[email protected]ec6c05f2013-10-23 18:41:57248 return delegate_->GetWebContents();
[email protected]fbc5e5f92012-01-02 06:08:32249}
250
[email protected]d202a7c2012-01-04 07:53:47251BrowserContext* NavigationControllerImpl::GetBrowserContext() const {
[email protected]a26023822011-12-29 00:23:55252 return browser_context_;
253}
254
[email protected]d202a7c2012-01-04 07:53:47255void NavigationControllerImpl::SetBrowserContext(
256 BrowserContext* browser_context) {
[email protected]cdcb1dee2012-01-04 00:46:20257 browser_context_ = browser_context;
258}
259
[email protected]d202a7c2012-01-04 07:53:47260void NavigationControllerImpl::Restore(
[email protected]5e369672009-11-03 23:48:30261 int selected_navigation,
[email protected]2ca1ea662012-10-04 02:26:36262 RestoreType type,
avi25764702015-06-23 15:43:37263 ScopedVector<NavigationEntry>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57264 // Verify that this controller is unused and that the input is valid.
[email protected]a26023822011-12-29 00:23:55265 DCHECK(GetEntryCount() == 0 && !GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57266 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14267 selected_navigation < static_cast<int>(entries->size()));
[email protected]ce3fa3c2009-04-20 19:55:57268
[email protected]ce3fa3c2009-04-20 19:55:57269 needs_reload_ = true;
[email protected]ad23a092011-12-28 07:02:04270 for (size_t i = 0; i < entries->size(); ++i) {
[email protected]10f417c52011-12-28 21:04:23271 NavigationEntryImpl* entry =
272 NavigationEntryImpl::FromNavigationEntry((*entries)[i]);
avi25764702015-06-23 15:43:37273 entries_.push_back(entry);
[email protected]ad23a092011-12-28 07:02:04274 }
avi25764702015-06-23 15:43:37275 entries->weak_clear();
[email protected]ce3fa3c2009-04-20 19:55:57276
277 // And finish the restore.
[email protected]2ca1ea662012-10-04 02:26:36278 FinishRestore(selected_navigation, type);
[email protected]ce3fa3c2009-04-20 19:55:57279}
280
[email protected]d202a7c2012-01-04 07:53:47281void NavigationControllerImpl::Reload(bool check_for_repost) {
[email protected]1ccb3568d2010-02-19 10:51:16282 ReloadInternal(check_for_repost, RELOAD);
283}
[email protected]d202a7c2012-01-04 07:53:47284void NavigationControllerImpl::ReloadIgnoringCache(bool check_for_repost) {
[email protected]1ccb3568d2010-02-19 10:51:16285 ReloadInternal(check_for_repost, RELOAD_IGNORING_CACHE);
286}
[email protected]7c16976c2012-08-04 02:38:23287void NavigationControllerImpl::ReloadOriginalRequestURL(bool check_for_repost) {
288 ReloadInternal(check_for_repost, RELOAD_ORIGINAL_REQUEST_URL);
289}
[email protected]1ccb3568d2010-02-19 10:51:16290
[email protected]d202a7c2012-01-04 07:53:47291void NavigationControllerImpl::ReloadInternal(bool check_for_repost,
292 ReloadType reload_type) {
[email protected]669e0ba2012-08-30 23:57:36293 if (transient_entry_index_ != -1) {
294 // If an interstitial is showing, treat a reload as a navigation to the
295 // transient entry's URL.
creis3da03872015-02-20 21:12:32296 NavigationEntryImpl* transient_entry = GetTransientEntry();
[email protected]6286a3792013-10-09 04:03:27297 if (!transient_entry)
[email protected]669e0ba2012-08-30 23:57:36298 return;
[email protected]6286a3792013-10-09 04:03:27299 LoadURL(transient_entry->GetURL(),
[email protected]8ff00d72012-10-23 19:12:21300 Referrer(),
Sylvain Defresnec6ccc77d2014-09-19 10:19:35301 ui::PAGE_TRANSITION_RELOAD,
[email protected]6286a3792013-10-09 04:03:27302 transient_entry->extra_headers());
[email protected]cbab76d2008-10-13 22:42:47303 return;
[email protected]669e0ba2012-08-30 23:57:36304 }
[email protected]cbab76d2008-10-13 22:42:47305
[email protected]59167c22013-06-03 18:07:32306 NavigationEntryImpl* entry = NULL;
307 int current_index = -1;
308
309 // If we are reloading the initial navigation, just use the current
310 // pending entry. Otherwise look up the current entry.
311 if (IsInitialNavigation() && pending_entry_) {
312 entry = pending_entry_;
313 // The pending entry might be in entries_ (e.g., after a Clone), so we
314 // should also update the current_index.
315 current_index = pending_entry_index_;
316 } else {
317 DiscardNonCommittedEntriesInternal();
318 current_index = GetCurrentEntryIndex();
319 if (current_index != -1) {
creis3da03872015-02-20 21:12:32320 entry = GetEntryAtIndex(current_index);
[email protected]59167c22013-06-03 18:07:32321 }
[email protected]979a4bc2013-04-24 01:27:15322 }
[email protected]241db352013-04-22 18:04:05323
[email protected]59167c22013-06-03 18:07:32324 // If we are no where, then we can't reload. TODO(darin): We should add a
325 // CanReload method.
326 if (!entry)
327 return;
328
[email protected]eaaba7dd2013-10-14 02:38:07329 if (reload_type == NavigationControllerImpl::RELOAD_ORIGINAL_REQUEST_URL &&
330 entry->GetOriginalRequestURL().is_valid() && !entry->GetHasPostData()) {
331 // We may have been redirected when navigating to the current URL.
332 // Use the URL the user originally intended to visit, if it's valid and if a
333 // POST wasn't involved; the latter case avoids issues with sending data to
334 // the wrong page.
335 entry->SetURL(entry->GetOriginalRequestURL());
[email protected]25cd5f12014-01-10 11:02:20336 entry->SetReferrer(Referrer());
[email protected]eaaba7dd2013-10-14 02:38:07337 }
338
[email protected]cdcb1dee2012-01-04 00:46:20339 if (g_check_for_repost && check_for_repost &&
[email protected]59167c22013-06-03 18:07:32340 entry->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30341 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07342 // they really want to do this. If they do, the dialog will call us back
343 // with check_for_repost = false.
[email protected]ec6c05f2013-10-23 18:41:57344 delegate_->NotifyBeforeFormRepostWarningShow();
[email protected]965bb092010-04-09 11:59:02345
[email protected]106a0812010-03-18 00:15:12346 pending_reload_ = reload_type;
[email protected]ec6c05f2013-10-23 18:41:57347 delegate_->ActivateAndShowRepostFormWarningDialog();
initial.commit09911bf2008-07-26 23:55:29348 } else {
[email protected]59167c22013-06-03 18:07:32349 if (!IsInitialNavigation())
350 DiscardNonCommittedEntriesInternal();
[email protected]c3b3d952012-01-31 12:52:26351
[email protected]bcd904482012-02-01 01:54:22352 // If we are reloading an entry that no longer belongs to the current
353 // site instance (for example, refreshing a page for just installed app),
354 // the reload must happen in a new process.
355 // The new entry must have a new page_id and site instance, so it behaves
356 // as new navigation (which happens to clear forward history).
[email protected]7e58cb272012-03-30 21:22:30357 // Tabs that are discarded due to low memory conditions may not have a site
358 // instance, and should not be treated as a cross-site reload.
[email protected]59167c22013-06-03 18:07:32359 SiteInstanceImpl* site_instance = entry->site_instance();
[email protected]fb4cc8e2013-12-17 17:56:21360 // Permit reloading guests without further checks.
paulmeyer1aeba7aa2015-06-16 18:25:10361 bool is_for_guests_only = site_instance && site_instance->HasProcess() &&
362 site_instance->GetProcess()->IsForGuestsOnly();
363 if (!is_for_guests_only && site_instance &&
[email protected]7e58cb272012-03-30 21:22:30364 site_instance->HasWrongProcessForURL(entry->GetURL())) {
[email protected]bcd904482012-02-01 01:54:22365 // Create a navigation entry that resembles the current one, but do not
avi25764702015-06-23 15:43:37366 // copy page id, site instance, content state, or timestamp. TODO(avi):
367 // This seems wrong. We're setting |pending_entry_| to a different value
368 // than what |pending_entry_index_| points to. Doesn't this leak?
[email protected]bcd904482012-02-01 01:54:22369 NavigationEntryImpl* nav_entry = NavigationEntryImpl::FromNavigationEntry(
370 CreateNavigationEntry(
371 entry->GetURL(), entry->GetReferrer(), entry->GetTransitionType(),
avi25764702015-06-23 15:43:37372 false, entry->extra_headers(), browser_context_).release());
[email protected]c3b3d952012-01-31 12:52:26373
[email protected]11535962012-02-09 21:02:10374 // Mark the reload type as NO_RELOAD, so navigation will not be considered
375 // a reload in the renderer.
376 reload_type = NavigationController::NO_RELOAD;
377
[email protected]e2caa032012-11-15 23:29:18378 nav_entry->set_should_replace_entry(true);
[email protected]bcd904482012-02-01 01:54:22379 pending_entry_ = nav_entry;
380 } else {
[email protected]59167c22013-06-03 18:07:32381 pending_entry_ = entry;
[email protected]bcd904482012-02-01 01:54:22382 pending_entry_index_ = current_index;
383
384 // The title of the page being reloaded might have been removed in the
385 // meanwhile, so we need to revert to the default title upon reload and
386 // invalidate the previously cached title (SetTitle will do both).
387 // See Chromium issue 96041.
[email protected]fcf75d42013-12-03 20:11:26388 pending_entry_->SetTitle(base::string16());
[email protected]bcd904482012-02-01 01:54:22389
Sylvain Defresnec6ccc77d2014-09-19 10:19:35390 pending_entry_->SetTransitionType(ui::PAGE_TRANSITION_RELOAD);
[email protected]bcd904482012-02-01 01:54:22391 }
392
[email protected]1ccb3568d2010-02-19 10:51:16393 NavigateToPendingEntry(reload_type);
initial.commit09911bf2008-07-26 23:55:29394 }
395}
396
[email protected]d202a7c2012-01-04 07:53:47397void NavigationControllerImpl::CancelPendingReload() {
[email protected]106a0812010-03-18 00:15:12398 DCHECK(pending_reload_ != NO_RELOAD);
399 pending_reload_ = NO_RELOAD;
400}
401
[email protected]d202a7c2012-01-04 07:53:47402void NavigationControllerImpl::ContinuePendingReload() {
[email protected]106a0812010-03-18 00:15:12403 if (pending_reload_ == NO_RELOAD) {
404 NOTREACHED();
405 } else {
406 ReloadInternal(false, pending_reload_);
[email protected]965bb092010-04-09 11:59:02407 pending_reload_ = NO_RELOAD;
[email protected]106a0812010-03-18 00:15:12408 }
409}
410
[email protected]cd2e15742013-03-08 04:08:31411bool NavigationControllerImpl::IsInitialNavigation() const {
[email protected]27ba81c2012-08-21 17:04:09412 return is_initial_navigation_;
[email protected]c70f9b82010-04-21 07:31:11413}
414
[email protected]d202a7c2012-01-04 07:53:47415NavigationEntryImpl* NavigationControllerImpl::GetEntryWithPageID(
[email protected]b6583592012-01-25 19:52:33416 SiteInstance* instance, int32 page_id) const {
[email protected]7f0005a2009-04-15 03:25:11417 int index = GetEntryIndexWithPageID(instance, page_id);
avi25764702015-06-23 15:43:37418 return (index != -1) ? entries_[index] : nullptr;
[email protected]765b35502008-08-21 00:51:20419}
420
creis3cdc3b02015-05-29 23:00:47421bool NavigationControllerImpl::HasCommittedRealLoad(
422 FrameTreeNode* frame_tree_node) const {
423 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
creis8b5cd4c2015-06-19 00:11:08424 return last_committed && last_committed->GetFrameEntry(frame_tree_node);
creis3cdc3b02015-05-29 23:00:47425}
426
avi25764702015-06-23 15:43:37427void NavigationControllerImpl::LoadEntry(
428 scoped_ptr<NavigationEntryImpl> entry) {
[email protected]765b35502008-08-21 00:51:20429 // When navigating to a new page, we don't know for sure if we will actually
430 // end up leaving the current page. The new page load could for example
431 // result in a download or a 'no content' response (e.g., a mailto: URL).
avi25764702015-06-23 15:43:37432 SetPendingEntry(entry.Pass());
[email protected]59167c22013-06-03 18:07:32433 NavigateToPendingEntry(NO_RELOAD);
434}
435
avi25764702015-06-23 15:43:37436void NavigationControllerImpl::SetPendingEntry(
437 scoped_ptr<NavigationEntryImpl> entry) {
[email protected]cbab76d2008-10-13 22:42:47438 DiscardNonCommittedEntriesInternal();
avi25764702015-06-23 15:43:37439 pending_entry_ = entry.release();
[email protected]8ff00d72012-10-23 19:12:21440 NotificationService::current()->Notify(
441 NOTIFICATION_NAV_ENTRY_PENDING,
442 Source<NavigationController>(this),
avi25764702015-06-23 15:43:37443 Details<NavigationEntry>(pending_entry_));
[email protected]765b35502008-08-21 00:51:20444}
445
creis3da03872015-02-20 21:12:32446NavigationEntryImpl* NavigationControllerImpl::GetActiveEntry() const {
[email protected]cbab76d2008-10-13 22:42:47447 if (transient_entry_index_ != -1)
avi25764702015-06-23 15:43:37448 return entries_[transient_entry_index_];
[email protected]cbab76d2008-10-13 22:42:47449 if (pending_entry_)
450 return pending_entry_;
451 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20452}
453
creis3da03872015-02-20 21:12:32454NavigationEntryImpl* NavigationControllerImpl::GetVisibleEntry() const {
[email protected]867e1f92011-08-30 19:01:19455 if (transient_entry_index_ != -1)
avi25764702015-06-23 15:43:37456 return entries_[transient_entry_index_];
[email protected]59167c22013-06-03 18:07:32457 // The pending entry is safe to return for new (non-history), browser-
458 // initiated navigations. Most renderer-initiated navigations should not
459 // show the pending entry, to prevent URL spoof attacks.
460 //
461 // We make an exception for renderer-initiated navigations in new tabs, as
462 // long as no other page has tried to access the initial empty document in
463 // the new tab. If another page modifies this blank page, a URL spoof is
464 // possible, so we must stop showing the pending entry.
[email protected]59167c22013-06-03 18:07:32465 bool safe_to_show_pending =
466 pending_entry_ &&
467 // Require a new navigation.
avi0dca04d2015-01-26 20:21:09468 pending_entry_index_ == -1 &&
[email protected]59167c22013-06-03 18:07:32469 // Require either browser-initiated or an unmodified new tab.
[email protected]aa62afd2014-04-22 19:22:46470 (!pending_entry_->is_renderer_initiated() || IsUnmodifiedBlankTab());
[email protected]59167c22013-06-03 18:07:32471
472 // Also allow showing the pending entry for history navigations in a new tab,
473 // such as Ctrl+Back. In this case, no existing page is visible and no one
474 // can script the new tab before it commits.
475 if (!safe_to_show_pending &&
476 pending_entry_ &&
avi0dca04d2015-01-26 20:21:09477 pending_entry_index_ != -1 &&
[email protected]59167c22013-06-03 18:07:32478 IsInitialNavigation() &&
[email protected]e47ae9472011-10-13 19:48:34479 !pending_entry_->is_renderer_initiated())
[email protected]59167c22013-06-03 18:07:32480 safe_to_show_pending = true;
481
482 if (safe_to_show_pending)
[email protected]867e1f92011-08-30 19:01:19483 return pending_entry_;
484 return GetLastCommittedEntry();
485}
486
[email protected]d202a7c2012-01-04 07:53:47487int NavigationControllerImpl::GetCurrentEntryIndex() const {
[email protected]cbab76d2008-10-13 22:42:47488 if (transient_entry_index_ != -1)
489 return transient_entry_index_;
[email protected]765b35502008-08-21 00:51:20490 if (pending_entry_index_ != -1)
491 return pending_entry_index_;
492 return last_committed_entry_index_;
493}
494
creis3da03872015-02-20 21:12:32495NavigationEntryImpl* NavigationControllerImpl::GetLastCommittedEntry() const {
[email protected]765b35502008-08-21 00:51:20496 if (last_committed_entry_index_ == -1)
497 return NULL;
avi25764702015-06-23 15:43:37498 return entries_[last_committed_entry_index_];
[email protected]765b35502008-08-21 00:51:20499}
500
[email protected]d202a7c2012-01-04 07:53:47501bool NavigationControllerImpl::CanViewSource() const {
[email protected]ec6c05f2013-10-23 18:41:57502 const std::string& mime_type = delegate_->GetContentsMimeType();
rsleevic327b48f82015-04-30 02:03:25503 bool is_viewable_mime_type =
504 mime_util::IsSupportedNonImageMimeType(mime_type) &&
servolkf3955532015-05-16 00:01:59505 !media::IsSupportedMediaMimeType(mime_type);
[email protected]6286a3792013-10-09 04:03:27506 NavigationEntry* visible_entry = GetVisibleEntry();
507 return visible_entry && !visible_entry->IsViewSourceMode() &&
[email protected]ec6c05f2013-10-23 18:41:57508 is_viewable_mime_type && !delegate_->GetInterstitialPage();
[email protected]31682282010-01-15 18:05:16509}
510
[email protected]d202a7c2012-01-04 07:53:47511int NavigationControllerImpl::GetLastCommittedEntryIndex() const {
[email protected]a26023822011-12-29 00:23:55512 return last_committed_entry_index_;
513}
514
[email protected]d202a7c2012-01-04 07:53:47515int NavigationControllerImpl::GetEntryCount() const {
[email protected]944822b2012-03-02 20:57:25516 DCHECK(entries_.size() <= max_entry_count());
[email protected]a26023822011-12-29 00:23:55517 return static_cast<int>(entries_.size());
518}
519
creis3da03872015-02-20 21:12:32520NavigationEntryImpl* NavigationControllerImpl::GetEntryAtIndex(
[email protected]022af742011-12-28 18:37:25521 int index) const {
avi25764702015-06-23 15:43:37522 if (index < 0 || index >= GetEntryCount())
523 return nullptr;
524
525 return entries_[index];
[email protected]022af742011-12-28 18:37:25526}
527
creis3da03872015-02-20 21:12:32528NavigationEntryImpl* NavigationControllerImpl::GetEntryAtOffset(
[email protected]022af742011-12-28 18:37:25529 int offset) const {
[email protected]9ba14052012-06-22 23:50:03530 int index = GetIndexForOffset(offset);
[email protected]a26023822011-12-29 00:23:55531 if (index < 0 || index >= GetEntryCount())
avi25764702015-06-23 15:43:37532 return nullptr;
[email protected]765b35502008-08-21 00:51:20533
avi25764702015-06-23 15:43:37534 return entries_[index];
[email protected]765b35502008-08-21 00:51:20535}
536
[email protected]9ba14052012-06-22 23:50:03537int NavigationControllerImpl::GetIndexForOffset(int offset) const {
[email protected]7bc2b032012-12-19 22:45:46538 return GetCurrentEntryIndex() + offset;
[email protected]9ba14052012-06-22 23:50:03539}
540
[email protected]9677a3c2012-12-22 04:18:58541void NavigationControllerImpl::TakeScreenshot() {
[email protected]1a3f5312013-04-26 21:00:10542 screenshot_manager_->TakeScreenshot();
[email protected]9677a3c2012-12-22 04:18:58543}
544
[email protected]1a3f5312013-04-26 21:00:10545void NavigationControllerImpl::SetScreenshotManager(
[email protected]d1581d72013-11-05 19:11:26546 NavigationEntryScreenshotManager* manager) {
[email protected]1a3f5312013-04-26 21:00:10547 screenshot_manager_.reset(manager ? manager :
[email protected]d1581d72013-11-05 19:11:26548 new NavigationEntryScreenshotManager(this));
[email protected]9677a3c2012-12-22 04:18:58549}
550
[email protected]d202a7c2012-01-04 07:53:47551bool NavigationControllerImpl::CanGoBack() const {
[email protected]765b35502008-08-21 00:51:20552 return entries_.size() > 1 && GetCurrentEntryIndex() > 0;
553}
554
[email protected]d202a7c2012-01-04 07:53:47555bool NavigationControllerImpl::CanGoForward() const {
[email protected]765b35502008-08-21 00:51:20556 int index = GetCurrentEntryIndex();
557 return index >= 0 && index < (static_cast<int>(entries_.size()) - 1);
558}
559
[email protected]9ba14052012-06-22 23:50:03560bool NavigationControllerImpl::CanGoToOffset(int offset) const {
561 int index = GetIndexForOffset(offset);
562 return index >= 0 && index < GetEntryCount();
563}
564
[email protected]d202a7c2012-01-04 07:53:47565void NavigationControllerImpl::GoBack() {
[email protected]765b35502008-08-21 00:51:20566 if (!CanGoBack()) {
567 NOTREACHED();
568 return;
569 }
570
571 // Base the navigation on where we are now...
572 int current_index = GetCurrentEntryIndex();
573
[email protected]cbab76d2008-10-13 22:42:47574 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20575
576 pending_entry_index_ = current_index - 1;
[email protected]022af742011-12-28 18:37:25577 entries_[pending_entry_index_]->SetTransitionType(
Sylvain Defresnec6ccc77d2014-09-19 10:19:35578 ui::PageTransitionFromInt(
[email protected]36fc0392011-12-25 03:59:51579 entries_[pending_entry_index_]->GetTransitionType() |
Sylvain Defresnec6ccc77d2014-09-19 10:19:35580 ui::PAGE_TRANSITION_FORWARD_BACK));
[email protected]1ccb3568d2010-02-19 10:51:16581 NavigateToPendingEntry(NO_RELOAD);
[email protected]765b35502008-08-21 00:51:20582}
583
[email protected]d202a7c2012-01-04 07:53:47584void NavigationControllerImpl::GoForward() {
[email protected]765b35502008-08-21 00:51:20585 if (!CanGoForward()) {
586 NOTREACHED();
587 return;
588 }
589
[email protected]cbab76d2008-10-13 22:42:47590 bool transient = (transient_entry_index_ != -1);
591
[email protected]765b35502008-08-21 00:51:20592 // Base the navigation on where we are now...
593 int current_index = GetCurrentEntryIndex();
594
[email protected]cbab76d2008-10-13 22:42:47595 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20596
[email protected]cbab76d2008-10-13 22:42:47597 pending_entry_index_ = current_index;
598 // If there was a transient entry, we removed it making the current index
599 // the next page.
600 if (!transient)
601 pending_entry_index_++;
602
[email protected]022af742011-12-28 18:37:25603 entries_[pending_entry_index_]->SetTransitionType(
Sylvain Defresnec6ccc77d2014-09-19 10:19:35604 ui::PageTransitionFromInt(
[email protected]36fc0392011-12-25 03:59:51605 entries_[pending_entry_index_]->GetTransitionType() |
Sylvain Defresnec6ccc77d2014-09-19 10:19:35606 ui::PAGE_TRANSITION_FORWARD_BACK));
[email protected]1ccb3568d2010-02-19 10:51:16607 NavigateToPendingEntry(NO_RELOAD);
[email protected]765b35502008-08-21 00:51:20608}
609
[email protected]d202a7c2012-01-04 07:53:47610void NavigationControllerImpl::GoToIndex(int index) {
[email protected]765b35502008-08-21 00:51:20611 if (index < 0 || index >= static_cast<int>(entries_.size())) {
612 NOTREACHED();
613 return;
614 }
615
[email protected]cbab76d2008-10-13 22:42:47616 if (transient_entry_index_ != -1) {
617 if (index == transient_entry_index_) {
618 // Nothing to do when navigating to the transient.
619 return;
620 }
621 if (index > transient_entry_index_) {
622 // Removing the transient is goint to shift all entries by 1.
623 index--;
624 }
625 }
626
627 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20628
629 pending_entry_index_ = index;
[email protected]022af742011-12-28 18:37:25630 entries_[pending_entry_index_]->SetTransitionType(
Sylvain Defresnec6ccc77d2014-09-19 10:19:35631 ui::PageTransitionFromInt(
[email protected]36fc0392011-12-25 03:59:51632 entries_[pending_entry_index_]->GetTransitionType() |
Sylvain Defresnec6ccc77d2014-09-19 10:19:35633 ui::PAGE_TRANSITION_FORWARD_BACK));
[email protected]1ccb3568d2010-02-19 10:51:16634 NavigateToPendingEntry(NO_RELOAD);
[email protected]765b35502008-08-21 00:51:20635}
636
[email protected]d202a7c2012-01-04 07:53:47637void NavigationControllerImpl::GoToOffset(int offset) {
[email protected]9ba14052012-06-22 23:50:03638 if (!CanGoToOffset(offset))
[email protected]765b35502008-08-21 00:51:20639 return;
640
[email protected]9ba14052012-06-22 23:50:03641 GoToIndex(GetIndexForOffset(offset));
[email protected]765b35502008-08-21 00:51:20642}
643
[email protected]41374f12013-07-24 02:49:28644bool NavigationControllerImpl::RemoveEntryAtIndex(int index) {
645 if (index == last_committed_entry_index_ ||
646 index == pending_entry_index_)
647 return false;
[email protected]6a13a6c2011-12-20 21:47:12648
[email protected]43032342011-03-21 14:10:31649 RemoveEntryAtIndexInternal(index);
[email protected]41374f12013-07-24 02:49:28650 return true;
[email protected]cbab76d2008-10-13 22:42:47651}
652
[email protected]d202a7c2012-01-04 07:53:47653void NavigationControllerImpl::UpdateVirtualURLToURL(
[email protected]10f417c52011-12-28 21:04:23654 NavigationEntryImpl* entry, const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:32655 GURL new_virtual_url(new_url);
[email protected]825b1662012-03-12 19:07:31656 if (BrowserURLHandlerImpl::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:51657 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
658 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:32659 }
660}
661
[email protected]d202a7c2012-01-04 07:53:47662void NavigationControllerImpl::LoadURL(
[email protected]6c6b02d2011-09-02 03:36:47663 const GURL& url,
[email protected]8ff00d72012-10-23 19:12:21664 const Referrer& referrer,
Sylvain Defresnec6ccc77d2014-09-19 10:19:35665 ui::PageTransition transition,
[email protected]6c6b02d2011-09-02 03:36:47666 const std::string& extra_headers) {
[email protected]cf002332012-08-14 19:17:47667 LoadURLParams params(url);
668 params.referrer = referrer;
669 params.transition_type = transition;
670 params.extra_headers = extra_headers;
671 LoadURLWithParams(params);
672}
673
674void NavigationControllerImpl::LoadURLWithParams(const LoadURLParams& params) {
naskob8744d22014-08-28 17:07:43675 TRACE_EVENT1("browser,navigation",
676 "NavigationControllerImpl::LoadURLWithParams",
677 "url", params.url.possibly_invalid_spec());
[email protected]47752982014-07-29 08:01:43678 if (HandleDebugURL(params.url, params.transition_type)) {
679 // If Telemetry is running, allow the URL load to proceed as if it's
680 // unhandled, otherwise Telemetry can't tell if Navigation completed.
avi83883c82014-12-23 00:08:49681 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]47752982014-07-29 08:01:43682 cc::switches::kEnableGpuBenchmarking))
683 return;
684 }
[email protected]8bf1048012012-02-08 01:22:18685
[email protected]c02f1ba2014-02-03 06:53:53686 // Any renderer-side debug URLs or javascript: URLs should be ignored if the
[email protected]c21e5c22014-02-05 23:56:41687 // renderer process is not live, unless it is the initial navigation of the
688 // tab.
[email protected]c02f1ba2014-02-03 06:53:53689 if (IsRendererDebugURL(params.url)) {
690 // TODO(creis): Find the RVH for the correct frame.
[email protected]c21e5c22014-02-05 23:56:41691 if (!delegate_->GetRenderViewHost()->IsRenderViewLive() &&
692 !IsInitialNavigation())
[email protected]c02f1ba2014-02-03 06:53:53693 return;
694 }
695
[email protected]cf002332012-08-14 19:17:47696 // Checks based on params.load_type.
697 switch (params.load_type) {
698 case LOAD_TYPE_DEFAULT:
699 break;
700 case LOAD_TYPE_BROWSER_INITIATED_HTTP_POST:
[email protected]e8ca69c2014-05-07 15:31:19701 if (!params.url.SchemeIs(url::kHttpScheme) &&
702 !params.url.SchemeIs(url::kHttpsScheme)) {
[email protected]cf002332012-08-14 19:17:47703 NOTREACHED() << "Http post load must use http(s) scheme.";
704 return;
705 }
706 break;
707 case LOAD_TYPE_DATA:
[email protected]cca6f392014-05-28 21:32:26708 if (!params.url.SchemeIs(url::kDataScheme)) {
[email protected]cf002332012-08-14 19:17:47709 NOTREACHED() << "Data load must use data scheme.";
710 return;
711 }
712 break;
713 default:
714 NOTREACHED();
715 break;
716 };
[email protected]e47ae9472011-10-13 19:48:34717
[email protected]e47ae9472011-10-13 19:48:34718 // The user initiated a load, we don't need to reload anymore.
719 needs_reload_ = false;
720
[email protected]cf002332012-08-14 19:17:47721 bool override = false;
722 switch (params.override_user_agent) {
723 case UA_OVERRIDE_INHERIT:
724 override = ShouldKeepOverride(GetLastCommittedEntry());
725 break;
726 case UA_OVERRIDE_TRUE:
727 override = true;
728 break;
729 case UA_OVERRIDE_FALSE:
730 override = false;
731 break;
732 default:
733 NOTREACHED();
734 break;
[email protected]d1ef81d2012-07-24 11:39:36735 }
736
avi25764702015-06-23 15:43:37737 scoped_ptr<NavigationEntryImpl> entry;
creis4e2ecb72015-06-20 00:46:30738
739 // For subframes, create a pending entry with a corresponding frame entry.
740 int frame_tree_node_id = params.frame_tree_node_id;
741 if (frame_tree_node_id != -1 || !params.frame_name.empty()) {
742 FrameTreeNode* node =
743 params.frame_tree_node_id != -1
744 ? delegate_->GetFrameTree()->FindByID(params.frame_tree_node_id)
745 : delegate_->GetFrameTree()->FindByName(params.frame_name);
746 if (node && !node->IsMainFrame()) {
747 DCHECK(GetLastCommittedEntry());
748
749 // Update the FTN ID to use below in case we found a named frame.
750 frame_tree_node_id = node->frame_tree_node_id();
751
752 // In --site-per-process, create an identical NavigationEntry with a
753 // new FrameNavigationEntry for the target subframe.
754 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
755 switches::kSitePerProcess)) {
756 entry = GetLastCommittedEntry()->Clone();
757 entry->SetPageID(-1);
758 entry->AddOrUpdateFrameEntry(node, -1, -1, nullptr, params.url,
759 params.referrer, PageState());
760 }
761 }
creis6a93a812015-04-24 23:13:17762 }
creis4e2ecb72015-06-20 00:46:30763
764 // Otherwise, create a pending entry for the main frame.
765 if (!entry) {
766 entry = NavigationEntryImpl::FromNavigationEntry(CreateNavigationEntry(
767 params.url, params.referrer, params.transition_type,
768 params.is_renderer_initiated, params.extra_headers, browser_context_));
769 }
770 // Set the FTN ID (only used in non-site-per-process, for tests).
771 entry->set_frame_tree_node_id(frame_tree_node_id);
lfg9ef7d2d2014-12-15 22:32:30772 entry->set_source_site_instance(
773 static_cast<SiteInstanceImpl*>(params.source_site_instance.get()));
[email protected]f8872902013-10-30 03:18:57774 if (params.redirect_chain.size() > 0)
[email protected]c1982ff2014-04-23 03:37:21775 entry->SetRedirectChain(params.redirect_chain);
avib6cc1562015-02-12 19:56:39776 // Don't allow an entry replacement if there is no entry to replace.
777 // http://crbug.com/457149
778 if (params.should_replace_current_entry && entries_.size() > 0)
[email protected]e2caa032012-11-15 23:29:18779 entry->set_should_replace_entry(true);
[email protected]60d6cca2013-04-30 08:47:13780 entry->set_should_clear_history_list(params.should_clear_history_list);
[email protected]cf002332012-08-14 19:17:47781 entry->SetIsOverridingUserAgent(override);
782 entry->set_transferred_global_request_id(
783 params.transferred_global_request_id);
[email protected]d1ef81d2012-07-24 11:39:36784
clamy8451aa62015-01-23 16:56:08785#if defined(OS_ANDROID)
786 if (params.intent_received_timestamp > 0) {
787 entry->set_intent_received_timestamp(
788 base::TimeTicks() +
789 base::TimeDelta::FromMilliseconds(params.intent_received_timestamp));
790 }
791#endif
792
[email protected]cf002332012-08-14 19:17:47793 switch (params.load_type) {
794 case LOAD_TYPE_DEFAULT:
795 break;
796 case LOAD_TYPE_BROWSER_INITIATED_HTTP_POST:
797 entry->SetHasPostData(true);
798 entry->SetBrowserInitiatedPostData(
[email protected]fc72bb12013-06-02 21:13:46799 params.browser_initiated_post_data.get());
[email protected]cf002332012-08-14 19:17:47800 break;
801 case LOAD_TYPE_DATA:
802 entry->SetBaseURLForDataURL(params.base_url_for_data_url);
803 entry->SetVirtualURL(params.virtual_url_for_data_url);
[email protected]951a64832012-10-11 16:26:37804 entry->SetCanLoadLocalResources(params.can_load_local_resources);
[email protected]cf002332012-08-14 19:17:47805 break;
806 default:
807 NOTREACHED();
808 break;
809 };
[email protected]132e281a2012-07-31 18:32:44810
avi25764702015-06-23 15:43:37811 LoadEntry(entry.Pass());
[email protected]132e281a2012-07-31 18:32:44812}
813
[email protected]d202a7c2012-01-04 07:53:47814bool NavigationControllerImpl::RendererDidNavigate(
creis3da03872015-02-20 21:12:32815 RenderFrameHostImpl* rfh,
[email protected]37567b432014-02-12 01:12:22816 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
[email protected]8ff00d72012-10-23 19:12:21817 LoadCommittedDetails* details) {
[email protected]cd2e15742013-03-08 04:08:31818 is_initial_navigation_ = false;
819
[email protected]0e8db942008-09-24 21:21:48820 // Save the previous state before we clobber it.
821 if (GetLastCommittedEntry()) {
[email protected]36fc0392011-12-25 03:59:51822 details->previous_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:55823 details->previous_entry_index = GetLastCommittedEntryIndex();
[email protected]0e8db942008-09-24 21:21:48824 } else {
825 details->previous_url = GURL();
826 details->previous_entry_index = -1;
827 }
[email protected]ecd9d8702008-08-28 22:10:17828
[email protected]77362eb2011-08-01 17:18:38829 // If we have a pending entry at this point, it should have a SiteInstance.
830 // Restored entries start out with a null SiteInstance, but we should have
831 // assigned one in NavigateToPendingEntry.
832 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance());
[email protected]e9ba4472008-09-14 15:42:43833
[email protected]bcd904482012-02-01 01:54:22834 // If we are doing a cross-site reload, we need to replace the existing
835 // navigation entry, not add another entry to the history. This has the side
avi45a72532015-04-07 21:01:45836 // effect of removing forward browsing history, if such existed. Or if we are
837 // doing a cross-site redirect navigation, we will do a similar thing.
838 //
839 // If this is an error load, we may have already removed the pending entry
840 // when we got the notice of the load failure. If so, look at the copy of the
841 // pending parameters that were saved.
842 if (params.url_is_unreachable && failed_pending_entry_id_ != 0) {
843 details->did_replace_entry = failed_pending_entry_should_replace_;
844 } else {
845 details->did_replace_entry = pending_entry_ &&
846 pending_entry_->should_replace_entry();
847 }
[email protected]bcd904482012-02-01 01:54:22848
[email protected]e9ba4472008-09-14 15:42:43849 // Do navigation-type specific actions. These will make and commit an entry.
[email protected]27dd82fd2014-03-03 22:11:43850 details->type = ClassifyNavigation(rfh, params);
avi98405c22015-05-21 20:47:06851 NavigationType new_type = ClassifyNavigationWithoutPageID(rfh, params);
avi98405c22015-05-21 20:47:06852 bool ignore_mismatch = false;
853 // There are disagreements on some Android bots over SAME_PAGE between the two
854 // classifiers so ignore disagreements if that's the case.
855 ignore_mismatch |= details->type == NAVIGATION_TYPE_EXISTING_PAGE &&
856 new_type == NAVIGATION_TYPE_SAME_PAGE;
857 ignore_mismatch |= details->type == NAVIGATION_TYPE_SAME_PAGE &&
858 new_type == NAVIGATION_TYPE_EXISTING_PAGE;
avi22afe872015-05-28 20:55:19859 // There are mismatches in the field where the new classifier thinks it's
860 // AUTO_SUBFRAME and the old classifier somehow thinks it's NEW or IGNORE. For
861 // IGNORE we know of at least one repro (https://crbug.com/492875), and for
862 // NEW it's not entirely clear what's going on, but we're pretty sure the new
863 // classifier is correct for both cases, so we're letting these mismatches go.
864 ignore_mismatch |= details->type == NAVIGATION_TYPE_NEW_SUBFRAME &&
865 new_type == NAVIGATION_TYPE_AUTO_SUBFRAME;
866 ignore_mismatch |= details->type == NAVIGATION_TYPE_NAV_IGNORE &&
867 new_type == NAVIGATION_TYPE_AUTO_SUBFRAME;
creis4e2ecb72015-06-20 00:46:30868 bool is_site_per_process = base::CommandLine::ForCurrentProcess()->HasSwitch(
869 switches::kSitePerProcess);
870 if (is_site_per_process) {
creis3cdc3b02015-05-29 23:00:47871 // We know that the old classifier is wrong for OOPIFs, so use the new one
872 // in --site-per-process mode.
873 details->type = new_type;
874 ignore_mismatch = true;
avi98405c22015-05-21 20:47:06875 }
876 if (!ignore_mismatch) {
avib1986b12015-06-11 20:21:23877 DCHECK_EQ(details->type, new_type);
avi7c6f35e2015-05-08 17:52:38878 }
[email protected]4bf3522c2010-08-19 21:00:20879
[email protected]b9d4dfdc2013-08-08 00:25:12880 // is_in_page must be computed before the entry gets committed.
avidb7d1d22015-06-08 21:21:50881 details->is_in_page = IsURLInPageNavigation(
[email protected]5cfbddc2014-06-23 23:52:23882 params.url, params.was_within_same_page, rfh);
[email protected]b9d4dfdc2013-08-08 00:25:12883
[email protected]0e8db942008-09-24 21:21:48884 switch (details->type) {
[email protected]8ff00d72012-10-23 19:12:21885 case NAVIGATION_TYPE_NEW_PAGE:
[email protected]27dd82fd2014-03-03 22:11:43886 RendererDidNavigateToNewPage(rfh, params, details->did_replace_entry);
[email protected]e9ba4472008-09-14 15:42:43887 break;
[email protected]8ff00d72012-10-23 19:12:21888 case NAVIGATION_TYPE_EXISTING_PAGE:
[email protected]27dd82fd2014-03-03 22:11:43889 RendererDidNavigateToExistingPage(rfh, params);
[email protected]e9ba4472008-09-14 15:42:43890 break;
[email protected]8ff00d72012-10-23 19:12:21891 case NAVIGATION_TYPE_SAME_PAGE:
[email protected]27dd82fd2014-03-03 22:11:43892 RendererDidNavigateToSamePage(rfh, params);
[email protected]e9ba4472008-09-14 15:42:43893 break;
[email protected]8ff00d72012-10-23 19:12:21894 case NAVIGATION_TYPE_IN_PAGE:
[email protected]27dd82fd2014-03-03 22:11:43895 RendererDidNavigateInPage(rfh, params, &details->did_replace_entry);
[email protected]e9ba4472008-09-14 15:42:43896 break;
[email protected]8ff00d72012-10-23 19:12:21897 case NAVIGATION_TYPE_NEW_SUBFRAME:
[email protected]27dd82fd2014-03-03 22:11:43898 RendererDidNavigateNewSubframe(rfh, params);
[email protected]e9ba4472008-09-14 15:42:43899 break;
[email protected]8ff00d72012-10-23 19:12:21900 case NAVIGATION_TYPE_AUTO_SUBFRAME:
[email protected]27dd82fd2014-03-03 22:11:43901 if (!RendererDidNavigateAutoSubframe(rfh, params))
[email protected]e9ba4472008-09-14 15:42:43902 return false;
903 break;
[email protected]8ff00d72012-10-23 19:12:21904 case NAVIGATION_TYPE_NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:49905 // If a pending navigation was in progress, this canceled it. We should
906 // discard it and make sure it is removed from the URL bar. After that,
907 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:43908 // the caller that nothing has happened.
[email protected]20d1c992011-04-12 21:17:49909 if (pending_entry_) {
910 DiscardNonCommittedEntries();
[email protected]ec6c05f2013-10-23 18:41:57911 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
[email protected]20d1c992011-04-12 21:17:49912 }
[email protected]e9ba4472008-09-14 15:42:43913 return false;
914 default:
915 NOTREACHED();
[email protected]765b35502008-08-21 00:51:20916 }
917
[email protected]688aa65c62012-09-28 04:32:22918 // At this point, we know that the navigation has just completed, so
919 // record the time.
920 //
921 // TODO(akalin): Use "sane time" as described in
922 // http://www.chromium.org/developers/design-documents/sane-time .
[email protected]c5b88d82012-10-06 17:03:33923 base::Time timestamp =
924 time_smoother_.GetSmoothedTime(get_timestamp_callback_.Run());
925 DVLOG(1) << "Navigation finished at (smoothed) timestamp "
[email protected]688aa65c62012-09-28 04:32:22926 << timestamp.ToInternalValue();
927
[email protected]f233e4232013-02-23 00:55:14928 // We should not have a pending entry anymore. Clear it again in case any
929 // error cases above forgot to do so.
930 DiscardNonCommittedEntriesInternal();
931
[email protected]e9ba4472008-09-14 15:42:43932 // All committed entries should have nonempty content state so WebKit doesn't
933 // get confused when we go back to them (see the function for details).
[email protected]691aa2f2013-05-28 22:52:04934 DCHECK(params.page_state.IsValid());
creis3da03872015-02-20 21:12:32935 NavigationEntryImpl* active_entry = GetLastCommittedEntry();
[email protected]688aa65c62012-09-28 04:32:22936 active_entry->SetTimestamp(timestamp);
[email protected]f49737b32013-08-28 07:51:44937 active_entry->SetHttpStatusCode(params.http_status_code);
creis4e2ecb72015-06-20 00:46:30938 if (is_site_per_process) {
939 // Update the frame-specific PageState.
940 FrameNavigationEntry* frame_entry =
941 active_entry->GetFrameEntry(rfh->frame_tree_node());
942 frame_entry->set_page_state(params.page_state);
943 } else {
944 active_entry->SetPageState(params.page_state);
945 }
[email protected]c1982ff2014-04-23 03:37:21946 active_entry->SetRedirectChain(params.redirects);
[email protected]132e281a2012-07-31 18:32:44947
[email protected]d6def3d2014-05-19 18:55:32948 // Use histogram to track memory impact of redirect chain because it's now
949 // not cleared for committed entries.
950 size_t redirect_chain_size = 0;
951 for (size_t i = 0; i < params.redirects.size(); ++i) {
952 redirect_chain_size += params.redirects[i].spec().length();
953 }
954 UMA_HISTOGRAM_COUNTS("Navigation.RedirectChainSize", redirect_chain_size);
955
[email protected]97d8f0d2013-10-29 16:49:21956 // Once it is committed, we no longer need to track several pieces of state on
957 // the entry.
958 active_entry->ResetForCommit();
[email protected]60d6cca2013-04-30 08:47:13959
[email protected]49bd30e62011-03-22 20:12:59960 // The active entry's SiteInstance should match our SiteInstance.
[email protected]a1b99262013-12-27 21:56:22961 // TODO(creis): This check won't pass for subframes until we create entries
962 // for subframe navigations.
avi39c1edd32015-06-04 20:06:00963 if (!rfh->GetParent())
[email protected]27dd82fd2014-03-03 22:11:43964 CHECK(active_entry->site_instance() == rfh->GetSiteInstance());
[email protected]49bd30e62011-03-22 20:12:59965
[email protected]b26de072013-02-23 02:33:44966 // Remember the bindings the renderer process has at this point, so that
967 // we do not grant this entry additional bindings if we come back to it.
creis3da03872015-02-20 21:12:32968 active_entry->SetBindings(rfh->GetEnabledBindings());
[email protected]b26de072013-02-23 02:33:44969
[email protected]e9ba4472008-09-14 15:42:43970 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:00971 details->entry = active_entry;
avi39c1edd32015-06-04 20:06:00972 details->is_main_frame = !rfh->GetParent();
[email protected]f072d2ce2008-09-17 17:16:24973 details->serialized_security_info = params.security_info;
[email protected]2e39d2e2009-02-19 18:41:31974 details->http_status_code = params.http_status_code;
[email protected]93f230e02011-06-01 14:40:00975 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:29976
[email protected]e9ba4472008-09-14 15:42:43977 return true;
initial.commit09911bf2008-07-26 23:55:29978}
979
[email protected]8ff00d72012-10-23 19:12:21980NavigationType NavigationControllerImpl::ClassifyNavigation(
creis3da03872015-02-20 21:12:32981 RenderFrameHostImpl* rfh,
[email protected]37567b432014-02-12 01:12:22982 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) const {
avi98405c22015-05-21 20:47:06983 if (params.page_id == -1) {
[email protected]eef9de32009-09-23 17:19:46984 // The renderer generates the page IDs, and so if it gives us the invalid
985 // page ID (-1) we know it didn't actually navigate. This happens in a few
986 // cases:
987 //
988 // - If a page makes a popup navigated to about blank, and then writes
989 // stuff like a subframe navigated to a real page. We'll get the commit
990 // for the subframe, but there won't be any commit for the outer page.
991 //
992 // - We were also getting these for failed loads (for example, bug 21849).
993 // The guess is that we get a "load commit" for the alternate error page,
994 // but that doesn't affect the page ID, so we get the "old" one, which
995 // could be invalid. This can also happen for a cross-site transition
996 // that causes us to swap processes. Then the error page load will be in
997 // a new process with no page IDs ever assigned (and hence a -1 value),
998 // yet the navigation controller still might have previous pages in its
999 // list.
1000 //
1001 // In these cases, there's nothing we can do with them, so ignore.
[email protected]8ff00d72012-10-23 19:12:211002 return NAVIGATION_TYPE_NAV_IGNORE;
[email protected]e9ba4472008-09-14 15:42:431003 }
1004
[email protected]a1b99262013-12-27 21:56:221005 if (params.page_id > delegate_->GetMaxPageIDForSiteInstance(
[email protected]27dd82fd2014-03-03 22:11:431006 rfh->GetSiteInstance())) {
[email protected]e9ba4472008-09-14 15:42:431007 // Greater page IDs than we've ever seen before are new pages. We may or may
1008 // not have a pending entry for the page, and this may or may not be the
1009 // main frame.
avi39c1edd32015-06-04 20:06:001010 if (!rfh->GetParent())
[email protected]8ff00d72012-10-23 19:12:211011 return NAVIGATION_TYPE_NEW_PAGE;
[email protected]4c27ba82008-09-24 16:49:091012
1013 // When this is a new subframe navigation, we should have a committed page
1014 // for which it's a suframe in. This may not be the case when an iframe is
1015 // navigated on a popup navigated to about:blank (the iframe would be
1016 // written into the popup by script on the main page). For these cases,
1017 // there isn't any navigation stuff we can do, so just ignore it.
avib1986b12015-06-11 20:21:231018 if (!GetLastCommittedEntry())
[email protected]8ff00d72012-10-23 19:12:211019 return NAVIGATION_TYPE_NAV_IGNORE;
[email protected]4c27ba82008-09-24 16:49:091020
1021 // Valid subframe navigation.
[email protected]8ff00d72012-10-23 19:12:211022 return NAVIGATION_TYPE_NEW_SUBFRAME;
[email protected]e9ba4472008-09-14 15:42:431023 }
1024
[email protected]60d6cca2013-04-30 08:47:131025 // We only clear the session history when navigating to a new page.
1026 DCHECK(!params.history_list_was_cleared);
1027
[email protected]e9ba4472008-09-14 15:42:431028 // Now we know that the notification is for an existing page. Find that entry.
1029 int existing_entry_index = GetEntryIndexWithPageID(
[email protected]27dd82fd2014-03-03 22:11:431030 rfh->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:431031 params.page_id);
1032 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441033 // The renderer has committed a navigation to an entry that no longer
1034 // exists. Because the renderer is showing that page, resurrect that entry.
1035 return NAVIGATION_TYPE_NEW_PAGE;
[email protected]e9ba4472008-09-14 15:42:431036 }
avi25764702015-06-23 15:43:371037 NavigationEntryImpl* existing_entry = entries_[existing_entry_index];
[email protected]e9ba4472008-09-14 15:42:431038
avi39c1edd32015-06-04 20:06:001039 if (rfh->GetParent()) {
[email protected]e6035c22010-05-25 16:15:521040 // All manual subframes would get new IDs and were handled above, so we
1041 // know this is auto. Since the current page was found in the navigation
1042 // entry list, we're guaranteed to have a last committed entry.
1043 DCHECK(GetLastCommittedEntry());
[email protected]8ff00d72012-10-23 19:12:211044 return NAVIGATION_TYPE_AUTO_SUBFRAME;
[email protected]e6035c22010-05-25 16:15:521045 }
1046
1047 // Anything below here we know is a main frame navigation.
[email protected]e9ba4472008-09-14 15:42:431048 if (pending_entry_ &&
[email protected]59167c22013-06-03 18:07:321049 !pending_entry_->is_renderer_initiated() &&
[email protected]e9ba4472008-09-14 15:42:431050 existing_entry != pending_entry_ &&
[email protected]36fc0392011-12-25 03:59:511051 pending_entry_->GetPageID() == -1 &&
avida96ccb12015-05-01 22:46:571052 existing_entry == GetLastCommittedEntry() &&
avi7c6f35e2015-05-08 17:52:381053 !params.was_within_same_page) {
1054 // In order to prevent unrelated pending entries from interfering with
1055 // this classification, make sure that the URL committed matches the URLs
1056 // of both the existing entry and the pending entry. There might have been
1057 // a redirection, though, so allow both the existing and pending entries
1058 // to match either the final URL that committed, or the original one
1059 // before redirection.
1060 GURL original_url;
1061 if (params.redirects.size())
1062 original_url = params.redirects[0];
1063
1064 if ((params.url == existing_entry->GetURL() ||
1065 original_url == existing_entry->GetURL()) &&
1066 (params.url == pending_entry_->GetURL() ||
1067 original_url == pending_entry_->GetURL())) {
1068 // In this case, we have a pending entry for a URL but Blink didn't do a
1069 // new navigation. This happens when you press enter in the URL bar to
1070 // reload. We will create a pending entry, but Blink will convert it to a
1071 // reload since it's the same page and not create a new entry for it (the
1072 // user doesn't want to have a new back/forward entry when they do this).
1073 // If this matches the last committed entry, we want to just ignore the
1074 // pending entry and go back to where we were (the "existing entry").
1075 return NAVIGATION_TYPE_SAME_PAGE;
1076 }
[email protected]e9ba4472008-09-14 15:42:431077 }
1078
[email protected]fc60f222008-12-18 17:36:541079 // Any toplevel navigations with the same base (minus the reference fragment)
1080 // are in-page navigations. We weeded out subframe navigations above. Most of
1081 // the time this doesn't matter since WebKit doesn't tell us about subframe
1082 // navigations that don't actually navigate, but it can happen when there is
1083 // an encoding override (it always sends a navigation request).
avidb7d1d22015-06-08 21:21:501084 if (IsURLInPageNavigation(params.url, params.was_within_same_page, rfh))
[email protected]8ff00d72012-10-23 19:12:211085 return NAVIGATION_TYPE_IN_PAGE;
[email protected]fc60f222008-12-18 17:36:541086
[email protected]e9ba4472008-09-14 15:42:431087 // Since we weeded out "new" navigations above, we know this is an existing
[email protected]4c27ba82008-09-24 16:49:091088 // (back/forward) navigation.
[email protected]8ff00d72012-10-23 19:12:211089 return NAVIGATION_TYPE_EXISTING_PAGE;
[email protected]e9ba4472008-09-14 15:42:431090}
1091
avi7c6f35e2015-05-08 17:52:381092NavigationType NavigationControllerImpl::ClassifyNavigationWithoutPageID(
1093 RenderFrameHostImpl* rfh,
1094 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) const {
1095 if (params.did_create_new_entry) {
1096 // A new entry. We may or may not have a pending entry for the page, and
1097 // this may or may not be the main frame.
avi39c1edd32015-06-04 20:06:001098 if (!rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381099 return NAVIGATION_TYPE_NEW_PAGE;
1100 }
1101
1102 // When this is a new subframe navigation, we should have a committed page
1103 // in which it's a subframe. This may not be the case when an iframe is
1104 // navigated on a popup navigated to about:blank (the iframe would be
1105 // written into the popup by script on the main page). For these cases,
1106 // there isn't any navigation stuff we can do, so just ignore it.
avib1986b12015-06-11 20:21:231107 if (!GetLastCommittedEntry())
avi7c6f35e2015-05-08 17:52:381108 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381109
1110 // Valid subframe navigation.
1111 return NAVIGATION_TYPE_NEW_SUBFRAME;
1112 }
1113
1114 // We only clear the session history when navigating to a new page.
1115 DCHECK(!params.history_list_was_cleared);
1116
avi39c1edd32015-06-04 20:06:001117 if (rfh->GetParent()) {
avi7c6f35e2015-05-08 17:52:381118 // All manual subframes would be did_create_new_entry and handled above, so
1119 // we know this is auto.
1120 if (GetLastCommittedEntry()) {
1121 return NAVIGATION_TYPE_AUTO_SUBFRAME;
1122 } else {
1123 // We ignore subframes created in non-committed pages; we'd appreciate if
1124 // people stopped doing that.
avi7c6f35e2015-05-08 17:52:381125 return NAVIGATION_TYPE_NAV_IGNORE;
1126 }
1127 }
1128
1129 if (params.nav_entry_id == 0) {
1130 // This is a renderer-initiated navigation (nav_entry_id == 0), but didn't
1131 // create a new page.
1132
1133 // Just like above in the did_create_new_entry case, it's possible to
1134 // scribble onto an uncommitted page. Again, there isn't any navigation
1135 // stuff that we can do, so ignore it here as well.
avi3a5b8f32015-05-28 17:50:231136 NavigationEntry* last_committed = GetLastCommittedEntry();
avib1986b12015-06-11 20:21:231137 if (!last_committed)
avi7c6f35e2015-05-08 17:52:381138 return NAVIGATION_TYPE_NAV_IGNORE;
avi7c6f35e2015-05-08 17:52:381139
avidb7d1d22015-06-08 21:21:501140 if (IsURLInPageNavigation(params.url, params.was_within_same_page, rfh)) {
avi7c6f35e2015-05-08 17:52:381141 // This is history.replaceState(), which is renderer-initiated yet within
1142 // the same page.
1143 return NAVIGATION_TYPE_IN_PAGE;
1144 } else {
1145 // This is history.reload() or a client-side redirect.
1146 return NAVIGATION_TYPE_EXISTING_PAGE;
1147 }
1148 }
1149
1150 if (pending_entry_ && pending_entry_index_ == -1 &&
1151 pending_entry_->GetUniqueID() == params.nav_entry_id) {
1152 // In this case, we have a pending entry for a load of a new URL but Blink
1153 // didn't do a new navigation (params.did_create_new_entry). This happens
1154 // when you press enter in the URL bar to reload. We will create a pending
1155 // entry, but Blink will convert it to a reload since it's the same page and
1156 // not create a new entry for it (the user doesn't want to have a new
1157 // back/forward entry when they do this). Therefore we want to just ignore
1158 // the pending entry and go back to where we were (the "existing entry").
1159 return NAVIGATION_TYPE_SAME_PAGE;
1160 }
1161
1162 if (params.intended_as_new_entry) {
1163 // This was intended to be a navigation to a new entry but the pending entry
1164 // got cleared in the meanwhile. Classify as EXISTING_PAGE because we may or
1165 // may not have a pending entry.
1166 return NAVIGATION_TYPE_EXISTING_PAGE;
1167 }
1168
1169 if (params.url_is_unreachable && failed_pending_entry_id_ != 0 &&
1170 params.nav_entry_id == failed_pending_entry_id_) {
1171 // If the renderer was going to a new pending entry that got cleared because
1172 // of an error, this is the case of the user trying to retry a failed load
1173 // by pressing return. Classify as EXISTING_PAGE because we probably don't
1174 // have a pending entry.
1175 return NAVIGATION_TYPE_EXISTING_PAGE;
1176 }
1177
1178 // Now we know that the notification is for an existing page. Find that entry.
1179 int existing_entry_index = GetEntryIndexWithUniqueID(params.nav_entry_id);
1180 if (existing_entry_index == -1) {
avi5cad4912015-06-19 05:25:441181 // The renderer has committed a navigation to an entry that no longer
1182 // exists. Because the renderer is showing that page, resurrect that entry.
1183 return NAVIGATION_TYPE_NEW_PAGE;
avi7c6f35e2015-05-08 17:52:381184 }
1185
1186 // Any top-level navigations with the same base (minus the reference fragment)
1187 // are in-page navigations. (We weeded out subframe navigations above.) Most
1188 // of the time this doesn't matter since Blink doesn't tell us about subframe
1189 // navigations that don't actually navigate, but it can happen when there is
1190 // an encoding override (it always sends a navigation request).
avidb7d1d22015-06-08 21:21:501191 if (IsURLInPageNavigation(params.url, params.was_within_same_page, rfh))
avi7c6f35e2015-05-08 17:52:381192 return NAVIGATION_TYPE_IN_PAGE;
avi7c6f35e2015-05-08 17:52:381193
1194 // Since we weeded out "new" navigations above, we know this is an existing
1195 // (back/forward) navigation.
1196 return NAVIGATION_TYPE_EXISTING_PAGE;
1197}
1198
[email protected]d202a7c2012-01-04 07:53:471199void NavigationControllerImpl::RendererDidNavigateToNewPage(
creis3da03872015-02-20 21:12:321200 RenderFrameHostImpl* rfh,
[email protected]37567b432014-02-12 01:12:221201 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
[email protected]a1b99262013-12-27 21:56:221202 bool replace_entry) {
avi25764702015-06-23 15:43:371203 scoped_ptr<NavigationEntryImpl> new_entry;
[email protected]f1eb87a2011-05-06 17:49:411204 bool update_virtual_url;
[email protected]6dd86ab2013-02-27 00:30:341205 // Only make a copy of the pending entry if it is appropriate for the new page
1206 // that was just loaded. We verify this at a coarse grain by checking that
avi5cad4912015-06-19 05:25:441207 // the SiteInstance hasn't been assigned to something else, and by making sure
1208 // that the pending entry was intended as a new entry (rather than being a
1209 // history navigation that was interrupted by an unrelated, renderer-initiated
1210 // navigation).
1211 if (pending_entry_ && pending_entry_index_ == -1 &&
[email protected]6dd86ab2013-02-27 00:30:341212 (!pending_entry_->site_instance() ||
[email protected]27dd82fd2014-03-03 22:11:431213 pending_entry_->site_instance() == rfh->GetSiteInstance())) {
creisef4a0cb2015-03-12 19:14:351214 new_entry = pending_entry_->Clone();
[email protected]e9ba4472008-09-14 15:42:431215
[email protected]f1eb87a2011-05-06 17:49:411216 update_virtual_url = new_entry->update_virtual_url_with_url();
[email protected]e9ba4472008-09-14 15:42:431217 } else {
avi25764702015-06-23 15:43:371218 new_entry = make_scoped_ptr(new NavigationEntryImpl);
[email protected]f8f93eb2012-09-25 03:06:241219
1220 // Find out whether the new entry needs to update its virtual URL on URL
1221 // change and set up the entry accordingly. This is needed to correctly
1222 // update the virtual URL when replaceState is called after a pushState.
1223 GURL url = params.url;
1224 bool needs_update = false;
[email protected]47752982014-07-29 08:01:431225 BrowserURLHandlerImpl::GetInstance()->RewriteURLIfNecessary(
1226 &url, browser_context_, &needs_update);
[email protected]f8f93eb2012-09-25 03:06:241227 new_entry->set_update_virtual_url_with_url(needs_update);
1228
[email protected]f1eb87a2011-05-06 17:49:411229 // When navigating to a new page, give the browser URL handler a chance to
1230 // update the virtual URL based on the new URL. For example, this is needed
1231 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
1232 // the URL.
[email protected]f8f93eb2012-09-25 03:06:241233 update_virtual_url = needs_update;
[email protected]e9ba4472008-09-14 15:42:431234 }
1235
wjmaclean7431bb22015-02-19 14:53:431236 // Don't use the page type from the pending entry. Some interstitial page
1237 // may have set the type to interstitial. Once we commit, however, the page
1238 // type must always be normal or error.
1239 new_entry->set_page_type(params.url_is_unreachable ? PAGE_TYPE_ERROR
1240 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041241 new_entry->SetURL(params.url);
[email protected]f1eb87a2011-05-06 17:49:411242 if (update_virtual_url)
avi25764702015-06-23 15:43:371243 UpdateVirtualURLToURL(new_entry.get(), params.url);
[email protected]022af742011-12-28 18:37:251244 new_entry->SetReferrer(params.referrer);
[email protected]36fc0392011-12-25 03:59:511245 new_entry->SetPageID(params.page_id);
[email protected]022af742011-12-28 18:37:251246 new_entry->SetTransitionType(params.transition);
[email protected]b6583592012-01-25 19:52:331247 new_entry->set_site_instance(
[email protected]27dd82fd2014-03-03 22:11:431248 static_cast<SiteInstanceImpl*>(rfh->GetSiteInstance()));
[email protected]36fc0392011-12-25 03:59:511249 new_entry->SetHasPostData(params.is_post);
[email protected]86cd9472012-02-03 19:51:051250 new_entry->SetPostID(params.post_id);
[email protected]7c16976c2012-08-04 02:38:231251 new_entry->SetOriginalRequestURL(params.original_request_url);
[email protected]bf70edce2012-06-20 22:32:221252 new_entry->SetIsOverridingUserAgent(params.is_overriding_user_agent);
[email protected]e9ba4472008-09-14 15:42:431253
creis8b5cd4c2015-06-19 00:11:081254 // Update the FrameNavigationEntry for new main frame commits.
1255 FrameNavigationEntry* frame_entry =
1256 new_entry->GetFrameEntry(rfh->frame_tree_node());
1257 frame_entry->set_item_sequence_number(params.item_sequence_number);
1258 frame_entry->set_document_sequence_number(params.document_sequence_number);
1259
[email protected]ff64b3e2014-05-31 04:07:331260 // history.pushState() is classified as a navigation to a new page, but
1261 // sets was_within_same_page to true. In this case, we already have the
[email protected]3a868f212014-08-09 10:41:191262 // title and favicon available, so set them immediately.
1263 if (params.was_within_same_page && GetLastCommittedEntry()) {
[email protected]ff64b3e2014-05-31 04:07:331264 new_entry->SetTitle(GetLastCommittedEntry()->GetTitle());
[email protected]3a868f212014-08-09 10:41:191265 new_entry->GetFavicon() = GetLastCommittedEntry()->GetFavicon();
1266 }
[email protected]ff64b3e2014-05-31 04:07:331267
[email protected]60d6cca2013-04-30 08:47:131268 DCHECK(!params.history_list_was_cleared || !replace_entry);
1269 // The browser requested to clear the session history when it initiated the
1270 // navigation. Now we know that the renderer has updated its state accordingly
1271 // and it is safe to also clear the browser side history.
1272 if (params.history_list_was_cleared) {
1273 DiscardNonCommittedEntriesInternal();
1274 entries_.clear();
1275 last_committed_entry_index_ = -1;
1276 }
1277
avi25764702015-06-23 15:43:371278 InsertOrReplaceEntry(new_entry.Pass(), replace_entry);
[email protected]e9ba4472008-09-14 15:42:431279}
1280
[email protected]d202a7c2012-01-04 07:53:471281void NavigationControllerImpl::RendererDidNavigateToExistingPage(
creis3da03872015-02-20 21:12:321282 RenderFrameHostImpl* rfh,
[email protected]37567b432014-02-12 01:12:221283 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
[email protected]e9ba4472008-09-14 15:42:431284 // We should only get here for main frame navigations.
avi39c1edd32015-06-04 20:06:001285 DCHECK(!rfh->GetParent());
[email protected]e9ba4472008-09-14 15:42:431286
1287 // This is a back/forward navigation. The existing page for the ID is
[email protected]4c27ba82008-09-24 16:49:091288 // guaranteed to exist by ClassifyNavigation, and we just need to update it
1289 // with new information from the renderer.
[email protected]27dd82fd2014-03-03 22:11:431290 int entry_index = GetEntryIndexWithPageID(rfh->GetSiteInstance(),
[email protected]7f0005a2009-04-15 03:25:111291 params.page_id);
[email protected]e9ba4472008-09-14 15:42:431292 DCHECK(entry_index >= 0 &&
1293 entry_index < static_cast<int>(entries_.size()));
avi25764702015-06-23 15:43:371294 NavigationEntryImpl* entry = entries_[entry_index];
[email protected]e9ba4472008-09-14 15:42:431295
[email protected]5ccd4dc2012-10-24 02:28:141296 // The URL may have changed due to redirects.
wjmaclean7431bb22015-02-19 14:53:431297 entry->set_page_type(params.url_is_unreachable ? PAGE_TYPE_ERROR
1298 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041299 entry->SetURL(params.url);
[email protected]25cd5f12014-01-10 11:02:201300 entry->SetReferrer(params.referrer);
[email protected]38178a42009-12-17 18:58:321301 if (entry->update_virtual_url_with_url())
1302 UpdateVirtualURLToURL(entry, params.url);
[email protected]5ccd4dc2012-10-24 02:28:141303
1304 // The redirected to page should not inherit the favicon from the previous
1305 // page.
Sylvain Defresnec6ccc77d2014-09-19 10:19:351306 if (ui::PageTransitionIsRedirect(params.transition))
[email protected]91a4ff82012-10-29 20:29:481307 entry->GetFavicon() = FaviconStatus();
[email protected]5ccd4dc2012-10-24 02:28:141308
1309 // The site instance will normally be the same except during session restore,
1310 // when no site instance will be assigned.
[email protected]e9ba4472008-09-14 15:42:431311 DCHECK(entry->site_instance() == NULL ||
[email protected]27dd82fd2014-03-03 22:11:431312 entry->site_instance() == rfh->GetSiteInstance());
[email protected]b6583592012-01-25 19:52:331313 entry->set_site_instance(
[email protected]27dd82fd2014-03-03 22:11:431314 static_cast<SiteInstanceImpl*>(rfh->GetSiteInstance()));
[email protected]e9ba4472008-09-14 15:42:431315
[email protected]36fc0392011-12-25 03:59:511316 entry->SetHasPostData(params.is_post);
[email protected]86cd9472012-02-03 19:51:051317 entry->SetPostID(params.post_id);
[email protected]d5a49e52010-01-08 03:01:411318
[email protected]e9ba4472008-09-14 15:42:431319 // The entry we found in the list might be pending if the user hit
1320 // back/forward/reload. This load should commit it (since it's already in the
[email protected]49bd30e62011-03-22 20:12:591321 // list, we can just discard the pending pointer). We should also discard the
1322 // pending entry if it corresponds to a different navigation, since that one
1323 // is now likely canceled. If it is not canceled, we will treat it as a new
1324 // navigation when it arrives, which is also ok.
[email protected]e9ba4472008-09-14 15:42:431325 //
1326 // Note that we need to use the "internal" version since we don't want to
1327 // actually change any other state, just kill the pointer.
[email protected]6eebaab2012-11-15 04:39:491328 DiscardNonCommittedEntriesInternal();
[email protected]40bcc302009-03-02 20:50:391329
[email protected]80858db52009-10-15 00:35:181330 // If a transient entry was removed, the indices might have changed, so we
1331 // have to query the entry index again.
1332 last_committed_entry_index_ =
[email protected]27dd82fd2014-03-03 22:11:431333 GetEntryIndexWithPageID(rfh->GetSiteInstance(), params.page_id);
[email protected]e9ba4472008-09-14 15:42:431334}
1335
[email protected]d202a7c2012-01-04 07:53:471336void NavigationControllerImpl::RendererDidNavigateToSamePage(
creis3da03872015-02-20 21:12:321337 RenderFrameHostImpl* rfh,
[email protected]37567b432014-02-12 01:12:221338 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
[email protected]e9ba4472008-09-14 15:42:431339 // This mode implies we have a pending entry that's the same as an existing
[email protected]4c27ba82008-09-24 16:49:091340 // entry for this page ID. This entry is guaranteed to exist by
1341 // ClassifyNavigation. All we need to do is update the existing entry.
[email protected]10f417c52011-12-28 21:04:231342 NavigationEntryImpl* existing_entry = GetEntryWithPageID(
[email protected]27dd82fd2014-03-03 22:11:431343 rfh->GetSiteInstance(), params.page_id);
[email protected]e9ba4472008-09-14 15:42:431344
1345 // We assign the entry's unique ID to be that of the new one. Since this is
1346 // always the result of a user action, we want to dismiss infobars, etc. like
1347 // a regular user-initiated navigation.
[email protected]36fc0392011-12-25 03:59:511348 existing_entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]e9ba4472008-09-14 15:42:431349
[email protected]a0e69262009-06-03 19:08:481350 // The URL may have changed due to redirects.
wjmaclean7431bb22015-02-19 14:53:431351 existing_entry->set_page_type(params.url_is_unreachable ? PAGE_TYPE_ERROR
1352 : PAGE_TYPE_NORMAL);
[email protected]38178a42009-12-17 18:58:321353 if (existing_entry->update_virtual_url_with_url())
1354 UpdateVirtualURLToURL(existing_entry, params.url);
[email protected]ad23a092011-12-28 07:02:041355 existing_entry->SetURL(params.url);
[email protected]25cd5f12014-01-10 11:02:201356 existing_entry->SetReferrer(params.referrer);
[email protected]a0e69262009-06-03 19:08:481357
[email protected]b77686522013-12-11 20:34:191358 // The page may have been requested with a different HTTP method.
1359 existing_entry->SetHasPostData(params.is_post);
1360 existing_entry->SetPostID(params.post_id);
1361
[email protected]cbab76d2008-10-13 22:42:471362 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:431363}
1364
[email protected]d202a7c2012-01-04 07:53:471365void NavigationControllerImpl::RendererDidNavigateInPage(
creis3da03872015-02-20 21:12:321366 RenderFrameHostImpl* rfh,
[email protected]37567b432014-02-12 01:12:221367 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
[email protected]a1b99262013-12-27 21:56:221368 bool* did_replace_entry) {
avi39c1edd32015-06-04 20:06:001369 DCHECK(!rfh->GetParent()) <<
1370 "Blink should only tell us about in-page navs for the main frame.";
[email protected]e9ba4472008-09-14 15:42:431371 // We're guaranteed to have an entry for this one.
[email protected]10f417c52011-12-28 21:04:231372 NavigationEntryImpl* existing_entry = GetEntryWithPageID(
[email protected]27dd82fd2014-03-03 22:11:431373 rfh->GetSiteInstance(), params.page_id);
[email protected]e9ba4472008-09-14 15:42:431374
1375 // Reference fragment navigation. We're guaranteed to have the last_committed
1376 // entry and it will be the same page as the new navigation (minus the
[email protected]d466b8a2011-07-15 21:48:031377 // reference fragments, of course). We'll update the URL of the existing
1378 // entry without pruning the forward history.
wjmaclean7431bb22015-02-19 14:53:431379 existing_entry->set_page_type(params.url_is_unreachable ? PAGE_TYPE_ERROR
1380 : PAGE_TYPE_NORMAL);
[email protected]ad23a092011-12-28 07:02:041381 existing_entry->SetURL(params.url);
[email protected]d466b8a2011-07-15 21:48:031382 if (existing_entry->update_virtual_url_with_url())
1383 UpdateVirtualURLToURL(existing_entry, params.url);
[email protected]ccbe04e2010-03-17 17:58:431384
[email protected]12cef652014-05-31 22:25:151385 existing_entry->SetHasPostData(params.is_post);
1386 existing_entry->SetPostID(params.post_id);
1387
[email protected]ccbe04e2010-03-17 17:58:431388 // This replaces the existing entry since the page ID didn't change.
1389 *did_replace_entry = true;
[email protected]d466b8a2011-07-15 21:48:031390
[email protected]6eebaab2012-11-15 04:39:491391 DiscardNonCommittedEntriesInternal();
[email protected]d466b8a2011-07-15 21:48:031392
1393 // If a transient entry was removed, the indices might have changed, so we
1394 // have to query the entry index again.
1395 last_committed_entry_index_ =
[email protected]27dd82fd2014-03-03 22:11:431396 GetEntryIndexWithPageID(rfh->GetSiteInstance(), params.page_id);
[email protected]e9ba4472008-09-14 15:42:431397}
1398
[email protected]d202a7c2012-01-04 07:53:471399void NavigationControllerImpl::RendererDidNavigateNewSubframe(
creis3da03872015-02-20 21:12:321400 RenderFrameHostImpl* rfh,
[email protected]37567b432014-02-12 01:12:221401 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
avi9f07a0c2015-02-18 22:51:291402 if (!ui::PageTransitionCoreTypeIs(params.transition,
1403 ui::PAGE_TRANSITION_MANUAL_SUBFRAME)) {
1404 // There was a comment here that said, "This is not user-initiated. Ignore."
1405 // But this makes no sense; non-user-initiated navigations should be
1406 // determined to be of type NAVIGATION_TYPE_AUTO_SUBFRAME and sent to
1407 // RendererDidNavigateAutoSubframe below.
1408 //
1409 // This if clause dates back to https://codereview.chromium.org/115919 and
1410 // the handling of immediate redirects. TODO(avi): Is this still valid? I'm
1411 // pretty sure that's there's nothing left of that code and that we should
1412 // take this out.
1413 //
1414 // Except for cross-process iframes; this doesn't work yet for them.
1415 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
1416 switches::kSitePerProcess)) {
1417 NOTREACHED();
1418 }
1419
[email protected]f233e4232013-02-23 00:55:141420 DiscardNonCommittedEntriesInternal();
[email protected]09b8f82f2009-06-16 20:22:111421 return;
1422 }
[email protected]09b8f82f2009-06-16 20:22:111423
[email protected]e9ba4472008-09-14 15:42:431424 // Manual subframe navigations just get the current entry cloned so the user
1425 // can go back or forward to it. The actual subframe information will be
1426 // stored in the page state for each of those entries. This happens out of
1427 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:091428 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
1429 << "that a last committed entry exists.";
creis96fc55082015-06-13 06:42:381430
avi25764702015-06-23 15:43:371431 scoped_ptr<NavigationEntryImpl> new_entry;
creis96fc55082015-06-13 06:42:381432 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1433 switches::kSitePerProcess)) {
1434 // Make sure new_entry takes ownership of frame_entry in a scoped_refptr.
1435 FrameNavigationEntry* frame_entry = new FrameNavigationEntry(
creis8b5cd4c2015-06-19 00:11:081436 rfh->frame_tree_node()->frame_tree_node_id(),
1437 params.item_sequence_number, params.document_sequence_number,
1438 rfh->GetSiteInstance(), params.url, params.referrer);
creis96fc55082015-06-13 06:42:381439 new_entry = GetLastCommittedEntry()->CloneAndReplace(rfh->frame_tree_node(),
1440 frame_entry);
1441 CHECK(frame_entry->HasOneRef());
1442 } else {
1443 new_entry = GetLastCommittedEntry()->Clone();
1444 }
1445
[email protected]36fc0392011-12-25 03:59:511446 new_entry->SetPageID(params.page_id);
avi25764702015-06-23 15:43:371447 InsertOrReplaceEntry(new_entry.Pass(), false);
[email protected]e9ba4472008-09-14 15:42:431448}
1449
[email protected]d202a7c2012-01-04 07:53:471450bool NavigationControllerImpl::RendererDidNavigateAutoSubframe(
creis3da03872015-02-20 21:12:321451 RenderFrameHostImpl* rfh,
[email protected]37567b432014-02-12 01:12:221452 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
avi9f07a0c2015-02-18 22:51:291453 DCHECK(ui::PageTransitionCoreTypeIs(params.transition,
1454 ui::PAGE_TRANSITION_AUTO_SUBFRAME));
1455
[email protected]e9ba4472008-09-14 15:42:431456 // We're guaranteed to have a previously committed entry, and we now need to
1457 // handle navigation inside of a subframe in it without creating a new entry.
1458 DCHECK(GetLastCommittedEntry());
1459
avi98405c22015-05-21 20:47:061460 if (params.nav_entry_id) {
caitkp420384c62015-05-28 14:34:511461 int entry_index = GetEntryIndexWithUniqueID(params.nav_entry_id);
caitkp420384c62015-05-28 14:34:511462
creis3cdc3b02015-05-29 23:00:471463 // If the |nav_entry_id| is non-zero and matches an existing entry, this is
1464 // a history auto" navigation. Update the last committed index accordingly.
1465 // If we don't recognize the |nav_entry_id|, it might be either a pending
1466 // entry for a transfer or a recently pruned entry. We'll handle it below.
1467 if (entry_index != -1 && entry_index != last_committed_entry_index_) {
avi98405c22015-05-21 20:47:061468 // Make sure that a subframe commit isn't changing the main frame's
1469 // origin. Otherwise the renderer process may be confused, leading to a
1470 // URL spoof. We can't check the path since that may change
1471 // (https://crbug.com/373041).
1472 if (GetLastCommittedEntry()->GetURL().GetOrigin() !=
1473 GetEntryAtIndex(entry_index)->GetURL().GetOrigin()) {
1474 // TODO(creis): This is unexpectedly being encountered in practice. If
1475 // you encounter this in practice, please post details to
1476 // https://crbug.com/486916. Once that's resolved, we'll change this to
1477 // kill the renderer process with bad_message::NC_AUTO_SUBFRAME.
1478 NOTREACHED() << "Unexpected main frame origin change on AUTO_SUBFRAME.";
1479 }
creis3cdc3b02015-05-29 23:00:471480
1481 // TODO(creis): Update the FrameNavigationEntry in --site-per-process.
avi98405c22015-05-21 20:47:061482 last_committed_entry_index_ = entry_index;
1483 DiscardNonCommittedEntriesInternal();
1484 return true;
1485 }
[email protected]e9ba4472008-09-14 15:42:431486 }
[email protected]f233e4232013-02-23 00:55:141487
creis625a0c7d2015-03-24 23:17:121488 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1489 switches::kSitePerProcess)) {
1490 // This may be a "new auto" case where we add a new FrameNavigationEntry, or
1491 // it may be a "history auto" case where we update an existing one.
creis625a0c7d2015-03-24 23:17:121492 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
creis8b5cd4c2015-06-19 00:11:081493 last_committed->AddOrUpdateFrameEntry(
1494 rfh->frame_tree_node(), params.item_sequence_number,
1495 params.document_sequence_number, rfh->GetSiteInstance(), params.url,
1496 params.referrer, params.page_state);
creis3cdc3b02015-05-29 23:00:471497
1498 // Cross-process subframe navigations may leave a pending entry around.
creisefbbbb52015-06-01 19:28:541499 // Clear it if it's actually for the subframe.
creis3cdc3b02015-05-29 23:00:471500 // TODO(creis): Don't use pending entries for subframe navigations.
creisefbbbb52015-06-01 19:28:541501 // See https://crbug.com/495161.
1502 if (pending_entry_ &&
1503 pending_entry_->frame_tree_node_id() ==
1504 rfh->frame_tree_node()->frame_tree_node_id()) {
1505 DiscardPendingEntry(false);
1506 }
creis625a0c7d2015-03-24 23:17:121507 }
1508
[email protected]f233e4232013-02-23 00:55:141509 // We do not need to discard the pending entry in this case, since we will
1510 // not generate commit notifications for this auto-subframe navigation.
[email protected]e9ba4472008-09-14 15:42:431511 return false;
1512}
1513
[email protected]d202a7c2012-01-04 07:53:471514int NavigationControllerImpl::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:231515 const NavigationEntryImpl* entry) const {
[email protected]765b35502008-08-21 00:51:201516 const NavigationEntries::const_iterator i(std::find(
1517 entries_.begin(),
1518 entries_.end(),
1519 entry));
1520 return (i == entries_.end()) ? -1 : static_cast<int>(i - entries_.begin());
1521}
1522
avidb7d1d22015-06-08 21:21:501523// There are two general cases where a navigation is "in page":
1524// 1. A fragment navigation, in which the url is kept the same except for the
1525// reference fragment.
1526// 2. A history API navigation (pushState and replaceState). This case is
1527// always in-page, but the urls are not guaranteed to match excluding the
1528// fragment. The relevant spec allows pushState/replaceState to any URL on
1529// the same origin.
1530// However, due to reloads, even identical urls are *not* guaranteed to be
1531// in-page navigations, we have to trust the renderer almost entirely.
1532// The one thing we do know is that cross-origin navigations will *never* be
1533// in-page. Therefore, trust the renderer if the URLs are on the same origin,
1534// and assume the renderer is malicious if a cross-origin navigation claims to
1535// be in-page.
[email protected]ce760d742012-08-30 22:31:101536bool NavigationControllerImpl::IsURLInPageNavigation(
[email protected]b9d4dfdc2013-08-08 00:25:121537 const GURL& url,
1538 bool renderer_says_in_page,
[email protected]5cfbddc2014-06-23 23:52:231539 RenderFrameHost* rfh) const {
avidb7d1d22015-06-08 21:21:501540 GURL last_committed_url;
1541 if (rfh->GetParent()) {
1542 last_committed_url = rfh->GetLastCommittedURL();
1543 } else {
1544 NavigationEntry* last_committed = GetLastCommittedEntry();
1545 // There must be a last-committed entry to compare URLs to. TODO(avi): When
1546 // might Blink say that a navigation is in-page yet there be no last-
1547 // committed entry?
1548 if (!last_committed)
1549 return false;
1550 last_committed_url = last_committed->GetURL();
1551 }
1552
1553 WebPreferences prefs = rfh->GetRenderViewHost()->GetWebkitPreferences();
1554 bool is_same_origin = last_committed_url.is_empty() ||
1555 // TODO(japhet): We should only permit navigations
1556 // originating from about:blank to be in-page if the
1557 // about:blank is the first document that frame loaded.
1558 // We don't have sufficient information to identify
1559 // that case at the moment, so always allow about:blank
1560 // for now.
1561 last_committed_url == GURL(url::kAboutBlankURL) ||
1562 last_committed_url.GetOrigin() == url.GetOrigin() ||
1563 !prefs.web_security_enabled ||
1564 (prefs.allow_universal_access_from_file_urls &&
1565 last_committed_url.SchemeIs(url::kFileScheme));
1566 if (!is_same_origin && renderer_says_in_page) {
1567 bad_message::ReceivedBadMessage(rfh->GetProcess(),
1568 bad_message::NC_IN_PAGE_NAVIGATION);
1569 }
1570 return is_same_origin && renderer_says_in_page;
[email protected]e9ba4472008-09-14 15:42:431571}
1572
[email protected]d202a7c2012-01-04 07:53:471573void NavigationControllerImpl::CopyStateFrom(
[email protected]c5eed492012-01-04 17:07:501574 const NavigationController& temp) {
[email protected]d202a7c2012-01-04 07:53:471575 const NavigationControllerImpl& source =
1576 static_cast<const NavigationControllerImpl&>(temp);
[email protected]ce3fa3c2009-04-20 19:55:571577 // Verify that we look new.
[email protected]a26023822011-12-29 00:23:551578 DCHECK(GetEntryCount() == 0 && !GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:571579
[email protected]a26023822011-12-29 00:23:551580 if (source.GetEntryCount() == 0)
[email protected]ce3fa3c2009-04-20 19:55:571581 return; // Nothing new to do.
1582
1583 needs_reload_ = true;
[email protected]a26023822011-12-29 00:23:551584 InsertEntriesFrom(source, source.GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:571585
[email protected]fdac6ade2013-07-20 01:06:301586 for (SessionStorageNamespaceMap::const_iterator it =
1587 source.session_storage_namespace_map_.begin();
1588 it != source.session_storage_namespace_map_.end();
1589 ++it) {
1590 SessionStorageNamespaceImpl* source_namespace =
1591 static_cast<SessionStorageNamespaceImpl*>(it->second.get());
1592 session_storage_namespace_map_[it->first] = source_namespace->Clone();
1593 }
[email protected]4e6419c2010-01-15 04:50:341594
[email protected]2ca1ea662012-10-04 02:26:361595 FinishRestore(source.last_committed_entry_index_, RESTORE_CURRENT_SESSION);
[email protected]91854cd2012-01-10 19:43:571596
1597 // Copy the max page id map from the old tab to the new tab. This ensures
1598 // that new and existing navigations in the tab's current SiteInstances
1599 // are identified properly.
[email protected]ec6c05f2013-10-23 18:41:571600 delegate_->CopyMaxPageIDsFrom(source.delegate()->GetWebContents());
[email protected]ce3fa3c2009-04-20 19:55:571601}
1602
[email protected]d202a7c2012-01-04 07:53:471603void NavigationControllerImpl::CopyStateFromAndPrune(
[email protected]e78a6852013-12-13 08:08:571604 NavigationController* temp,
1605 bool replace_entry) {
[email protected]474f8512013-05-31 22:31:161606 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:011607 CHECK(CanPruneAllButLastCommitted());
[email protected]474f8512013-05-31 22:31:161608
[email protected]d202a7c2012-01-04 07:53:471609 NavigationControllerImpl* source =
1610 static_cast<NavigationControllerImpl*>(temp);
[email protected]e1cd5452010-08-26 18:03:251611
avi2b177592014-12-10 02:08:021612 // Remove all the entries leaving the last committed entry.
[email protected]79368982013-11-13 01:11:011613 PruneAllButLastCommittedInternal();
[email protected]e1cd5452010-08-26 18:03:251614
[email protected]474f8512013-05-31 22:31:161615 // We now have one entry, possibly with a new pending entry. Ensure that
1616 // adding the entries from source won't put us over the limit.
1617 DCHECK_EQ(1, GetEntryCount());
[email protected]e78a6852013-12-13 08:08:571618 if (!replace_entry)
1619 source->PruneOldestEntryIfFull();
[email protected]944822b2012-03-02 20:57:251620
[email protected]47e020a2010-10-15 14:43:371621 // Insert the entries from source. Don't use source->GetCurrentEntryIndex as
avi2b177592014-12-10 02:08:021622 // we don't want to copy over the transient entry. Ignore any pending entry,
[email protected]474f8512013-05-31 22:31:161623 // since it has not committed in source.
1624 int max_source_index = source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:251625 if (max_source_index == -1)
[email protected]a26023822011-12-29 00:23:551626 max_source_index = source->GetEntryCount();
[email protected]e1cd5452010-08-26 18:03:251627 else
1628 max_source_index++;
[email protected]e78a6852013-12-13 08:08:571629
1630 // Ignore the source's current entry if merging with replacement.
1631 // TODO(davidben): This should preserve entries forward of the current
1632 // too. http://crbug.com/317872
1633 if (replace_entry && max_source_index > 0)
1634 max_source_index--;
1635
[email protected]47e020a2010-10-15 14:43:371636 InsertEntriesFrom(*source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:251637
1638 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:551639 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]796931a92011-08-10 01:32:141640
avi2b177592014-12-10 02:08:021641 delegate_->SetHistoryOffsetAndLength(last_committed_entry_index_,
1642 GetEntryCount());
[email protected]91854cd2012-01-10 19:43:571643
avi2b177592014-12-10 02:08:021644 // Copy the max page id map from the old tab to the new tab. This ensures that
1645 // new and existing navigations in the tab's current SiteInstances are
1646 // identified properly.
creis3da03872015-02-20 21:12:321647 NavigationEntryImpl* last_committed = GetLastCommittedEntry();
avi2b177592014-12-10 02:08:021648 int32 site_max_page_id =
1649 delegate_->GetMaxPageIDForSiteInstance(last_committed->site_instance());
[email protected]ec6c05f2013-10-23 18:41:571650 delegate_->CopyMaxPageIDsFrom(source->delegate()->GetWebContents());
avi2b177592014-12-10 02:08:021651 delegate_->UpdateMaxPageIDForSiteInstance(last_committed->site_instance(),
1652 site_max_page_id);
[email protected]f69ed362014-04-28 14:01:421653 max_restored_page_id_ = source->max_restored_page_id_;
[email protected]e1cd5452010-08-26 18:03:251654}
1655
[email protected]79368982013-11-13 01:11:011656bool NavigationControllerImpl::CanPruneAllButLastCommitted() {
[email protected]474f8512013-05-31 22:31:161657 // If there is no last committed entry, we cannot prune. Even if there is a
1658 // pending entry, it may not commit, leaving this WebContents blank, despite
1659 // possibly giving it new entries via CopyStateFromAndPrune.
1660 if (last_committed_entry_index_ == -1)
1661 return false;
[email protected]9350602e2013-02-26 23:27:441662
[email protected]474f8512013-05-31 22:31:161663 // We cannot prune if there is a pending entry at an existing entry index.
1664 // It may not commit, so we have to keep the last committed entry, and thus
1665 // there is no sensible place to keep the pending entry. It is ok to have
1666 // a new pending entry, which can optionally commit as a new navigation.
1667 if (pending_entry_index_ != -1)
1668 return false;
1669
1670 // We should not prune if we are currently showing a transient entry.
1671 if (transient_entry_index_ != -1)
1672 return false;
1673
1674 return true;
1675}
1676
[email protected]79368982013-11-13 01:11:011677void NavigationControllerImpl::PruneAllButLastCommitted() {
1678 PruneAllButLastCommittedInternal();
[email protected]474f8512013-05-31 22:31:161679
avi2b177592014-12-10 02:08:021680 DCHECK_EQ(0, last_committed_entry_index_);
1681 DCHECK_EQ(1, GetEntryCount());
[email protected]9350602e2013-02-26 23:27:441682
avi2b177592014-12-10 02:08:021683 delegate_->SetHistoryOffsetAndLength(last_committed_entry_index_,
1684 GetEntryCount());
[email protected]9350602e2013-02-26 23:27:441685}
1686
[email protected]79368982013-11-13 01:11:011687void NavigationControllerImpl::PruneAllButLastCommittedInternal() {
[email protected]474f8512013-05-31 22:31:161688 // It is up to callers to check the invariants before calling this.
[email protected]79368982013-11-13 01:11:011689 CHECK(CanPruneAllButLastCommitted());
[email protected]97b6c4f2010-09-27 19:31:261690
[email protected]474f8512013-05-31 22:31:161691 // Erase all entries but the last committed entry. There may still be a
1692 // new pending entry after this.
1693 entries_.erase(entries_.begin(),
1694 entries_.begin() + last_committed_entry_index_);
1695 entries_.erase(entries_.begin() + 1, entries_.end());
1696 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:261697}
1698
[email protected]15f46dc2013-01-24 05:40:241699void NavigationControllerImpl::ClearAllScreenshots() {
[email protected]1a3f5312013-04-26 21:00:101700 screenshot_manager_->ClearAllScreenshots();
[email protected]15f46dc2013-01-24 05:40:241701}
1702
[email protected]d1198fd2012-08-13 22:50:191703void NavigationControllerImpl::SetSessionStorageNamespace(
[email protected]fdac6ade2013-07-20 01:06:301704 const std::string& partition_id,
[email protected]8ff00d72012-10-23 19:12:211705 SessionStorageNamespace* session_storage_namespace) {
[email protected]d1198fd2012-08-13 22:50:191706 if (!session_storage_namespace)
1707 return;
1708
1709 // We can't overwrite an existing SessionStorage without violating spec.
1710 // Attempts to do so may give a tab access to another tab's session storage
1711 // so die hard on an error.
[email protected]fdac6ade2013-07-20 01:06:301712 bool successful_insert = session_storage_namespace_map_.insert(
1713 make_pair(partition_id,
1714 static_cast<SessionStorageNamespaceImpl*>(
1715 session_storage_namespace)))
1716 .second;
1717 CHECK(successful_insert) << "Cannot replace existing SessionStorageNamespace";
[email protected]d1198fd2012-08-13 22:50:191718}
1719
[email protected]d202a7c2012-01-04 07:53:471720void NavigationControllerImpl::SetMaxRestoredPageID(int32 max_id) {
[email protected]71fde352011-12-29 03:29:561721 max_restored_page_id_ = max_id;
1722}
1723
[email protected]d202a7c2012-01-04 07:53:471724int32 NavigationControllerImpl::GetMaxRestoredPageID() const {
[email protected]71fde352011-12-29 03:29:561725 return max_restored_page_id_;
[email protected]a26023822011-12-29 00:23:551726}
1727
[email protected]aa62afd2014-04-22 19:22:461728bool NavigationControllerImpl::IsUnmodifiedBlankTab() const {
[email protected]aa62afd2014-04-22 19:22:461729 return IsInitialNavigation() &&
[email protected]b4c84012014-04-28 19:51:101730 !GetLastCommittedEntry() &&
1731 !delegate_->HasAccessedInitialDocument();
[email protected]aa62afd2014-04-22 19:22:461732}
1733
[email protected]a26023822011-12-29 00:23:551734SessionStorageNamespace*
[email protected]fdac6ade2013-07-20 01:06:301735NavigationControllerImpl::GetSessionStorageNamespace(SiteInstance* instance) {
1736 std::string partition_id;
1737 if (instance) {
1738 // TODO(ajwong): When GetDefaultSessionStorageNamespace() goes away, remove
1739 // this if statement so |instance| must not be NULL.
1740 partition_id =
1741 GetContentClient()->browser()->GetStoragePartitionIdForSite(
1742 browser_context_, instance->GetSiteURL());
1743 }
[email protected]d1198fd2012-08-13 22:50:191744
[email protected]fdac6ade2013-07-20 01:06:301745 SessionStorageNamespaceMap::const_iterator it =
1746 session_storage_namespace_map_.find(partition_id);
1747 if (it != session_storage_namespace_map_.end())
1748 return it->second.get();
1749
1750 // Create one if no one has accessed session storage for this partition yet.
1751 //
1752 // TODO(ajwong): Should this use the |partition_id| directly rather than
1753 // re-lookup via |instance|? http://crbug.com/142685
1754 StoragePartition* partition =
1755 BrowserContext::GetStoragePartition(browser_context_, instance);
1756 SessionStorageNamespaceImpl* session_storage_namespace =
1757 new SessionStorageNamespaceImpl(
[email protected]5f2aa722013-08-07 16:59:411758 static_cast<DOMStorageContextWrapper*>(
[email protected]fdac6ade2013-07-20 01:06:301759 partition->GetDOMStorageContext()));
1760 session_storage_namespace_map_[partition_id] = session_storage_namespace;
1761
1762 return session_storage_namespace;
1763}
1764
1765SessionStorageNamespace*
1766NavigationControllerImpl::GetDefaultSessionStorageNamespace() {
1767 // TODO(ajwong): Remove if statement in GetSessionStorageNamespace().
1768 return GetSessionStorageNamespace(NULL);
1769}
1770
1771const SessionStorageNamespaceMap&
1772NavigationControllerImpl::GetSessionStorageNamespaceMap() const {
1773 return session_storage_namespace_map_;
[email protected]a26023822011-12-29 00:23:551774}
[email protected]d202a7c2012-01-04 07:53:471775
1776bool NavigationControllerImpl::NeedsReload() const {
[email protected]71fde352011-12-29 03:29:561777 return needs_reload_;
1778}
[email protected]a26023822011-12-29 00:23:551779
[email protected]46bb5e9c2013-10-03 22:16:471780void NavigationControllerImpl::SetNeedsReload() {
1781 needs_reload_ = true;
jaekyunc8cefa82015-01-09 20:14:541782
1783 if (last_committed_entry_index_ != -1) {
1784 entries_[last_committed_entry_index_]->SetTransitionType(
1785 ui::PAGE_TRANSITION_RELOAD);
1786 }
[email protected]46bb5e9c2013-10-03 22:16:471787}
1788
[email protected]d202a7c2012-01-04 07:53:471789void NavigationControllerImpl::RemoveEntryAtIndexInternal(int index) {
[email protected]a26023822011-12-29 00:23:551790 DCHECK(index < GetEntryCount());
[email protected]6a13a6c2011-12-20 21:47:121791 DCHECK(index != last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:311792
1793 DiscardNonCommittedEntries();
1794
1795 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:121796 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:311797 last_committed_entry_index_--;
1798}
1799
[email protected]d202a7c2012-01-04 07:53:471800void NavigationControllerImpl::DiscardNonCommittedEntries() {
[email protected]cbab76d2008-10-13 22:42:471801 bool transient = transient_entry_index_ != -1;
1802 DiscardNonCommittedEntriesInternal();
initial.commit09911bf2008-07-26 23:55:291803
[email protected]cbab76d2008-10-13 22:42:471804 // If there was a transient entry, invalidate everything so the new active
1805 // entry state is shown.
1806 if (transient) {
[email protected]7f924832014-08-09 05:57:221807 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]cbab76d2008-10-13 22:42:471808 }
initial.commit09911bf2008-07-26 23:55:291809}
1810
creis3da03872015-02-20 21:12:321811NavigationEntryImpl* NavigationControllerImpl::GetPendingEntry() const {
[email protected]022af742011-12-28 18:37:251812 return pending_entry_;
1813}
1814
[email protected]d202a7c2012-01-04 07:53:471815int NavigationControllerImpl::GetPendingEntryIndex() const {
[email protected]a26023822011-12-29 00:23:551816 return pending_entry_index_;
1817}
1818
avi25764702015-06-23 15:43:371819void NavigationControllerImpl::InsertOrReplaceEntry(
1820 scoped_ptr<NavigationEntryImpl> entry, bool replace) {
Sylvain Defresnec6ccc77d2014-09-19 10:19:351821 DCHECK(entry->GetTransitionType() != ui::PAGE_TRANSITION_AUTO_SUBFRAME);
[email protected]765b35502008-08-21 00:51:201822
avi5cad4912015-06-19 05:25:441823 // If the pending_entry_index_ is -1, the navigation was to a new page, and we
1824 // need to keep continuity with the pending entry, so copy the pending entry's
1825 // unique ID to the committed entry. If the pending_entry_index_ isn't -1,
1826 // then the renderer navigated on its own, independent of the pending entry,
1827 // so don't copy anything.
1828 if (pending_entry_ && pending_entry_index_ == -1)
1829 entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]765b35502008-08-21 00:51:201830
[email protected]cbab76d2008-10-13 22:42:471831 DiscardNonCommittedEntriesInternal();
[email protected]765b35502008-08-21 00:51:201832
1833 int current_size = static_cast<int>(entries_.size());
avi6567f302015-02-26 22:55:151834 DCHECK_IMPLIES(replace, current_size > 0);
[email protected]765b35502008-08-21 00:51:201835
[email protected]765b35502008-08-21 00:51:201836 if (current_size > 0) {
[email protected]672eba292009-05-13 13:22:451837 // Prune any entries which are in front of the current entry.
1838 // Also prune the current entry if we are to replace the current entry.
[email protected]47dcbdc2011-05-27 15:08:311839 // last_committed_entry_index_ must be updated here since calls to
1840 // NotifyPrunedEntries() below may re-enter and we must make sure
1841 // last_committed_entry_index_ is not left in an invalid state.
1842 if (replace)
1843 --last_committed_entry_index_;
1844
[email protected]c12bf1a12008-09-17 16:28:491845 int num_pruned = 0;
[email protected]47dcbdc2011-05-27 15:08:311846 while (last_committed_entry_index_ < (current_size - 1)) {
[email protected]c12bf1a12008-09-17 16:28:491847 num_pruned++;
[email protected]765b35502008-08-21 00:51:201848 entries_.pop_back();
1849 current_size--;
1850 }
[email protected]c12bf1a12008-09-17 16:28:491851 if (num_pruned > 0) // Only notify if we did prune something.
1852 NotifyPrunedEntries(this, false, num_pruned);
[email protected]765b35502008-08-21 00:51:201853 }
1854
[email protected]944822b2012-03-02 20:57:251855 PruneOldestEntryIfFull();
[email protected]765b35502008-08-21 00:51:201856
avi25764702015-06-23 15:43:371857 int32 page_id = entry->GetPageID();
1858 entries_.push_back(entry.Pass());
[email protected]765b35502008-08-21 00:51:201859 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
[email protected]e9ba4472008-09-14 15:42:431860
1861 // This is a new page ID, so we need everybody to know about it.
avi25764702015-06-23 15:43:371862 delegate_->UpdateMaxPageID(page_id);
initial.commit09911bf2008-07-26 23:55:291863}
1864
[email protected]944822b2012-03-02 20:57:251865void NavigationControllerImpl::PruneOldestEntryIfFull() {
1866 if (entries_.size() >= max_entry_count()) {
1867 DCHECK_EQ(max_entry_count(), entries_.size());
[email protected]e6fec472013-05-14 05:29:021868 DCHECK_GT(last_committed_entry_index_, 0);
[email protected]944822b2012-03-02 20:57:251869 RemoveEntryAtIndex(0);
1870 NotifyPrunedEntries(this, true, 1);
1871 }
1872}
1873
[email protected]d202a7c2012-01-04 07:53:471874void NavigationControllerImpl::NavigateToPendingEntry(ReloadType reload_type) {
[email protected]72097fd02010-01-21 23:36:011875 needs_reload_ = false;
1876
[email protected]83c2e232011-10-07 21:36:461877 // If we were navigating to a slow-to-commit page, and the user performs
1878 // a session history navigation to the last committed page, RenderViewHost
1879 // will force the throbber to start, but WebKit will essentially ignore the
1880 // navigation, and won't send a message to stop the throbber. To prevent this
1881 // from happening, we drop the navigation here and stop the slow-to-commit
1882 // page from loading (which would normally happen during the navigation).
1883 if (pending_entry_index_ != -1 &&
1884 pending_entry_index_ == last_committed_entry_index_ &&
1885 (entries_[pending_entry_index_]->restore_type() ==
[email protected]10f417c52011-12-28 21:04:231886 NavigationEntryImpl::RESTORE_NONE) &&
[email protected]36fc0392011-12-25 03:59:511887 (entries_[pending_entry_index_]->GetTransitionType() &
Sylvain Defresnec6ccc77d2014-09-19 10:19:351888 ui::PAGE_TRANSITION_FORWARD_BACK)) {
[email protected]ec6c05f2013-10-23 18:41:571889 delegate_->Stop();
[email protected]6a13a6c2011-12-20 21:47:121890
1891 // If an interstitial page is showing, we want to close it to get back
1892 // to what was showing before.
[email protected]ec6c05f2013-10-23 18:41:571893 if (delegate_->GetInterstitialPage())
1894 delegate_->GetInterstitialPage()->DontProceed();
[email protected]6a13a6c2011-12-20 21:47:121895
[email protected]83c2e232011-10-07 21:36:461896 DiscardNonCommittedEntries();
1897 return;
1898 }
1899
[email protected]6a13a6c2011-12-20 21:47:121900 // If an interstitial page is showing, the previous renderer is blocked and
1901 // cannot make new requests. Unblock (and disable) it to allow this
1902 // navigation to succeed. The interstitial will stay visible until the
1903 // resulting DidNavigate.
[email protected]ec6c05f2013-10-23 18:41:571904 if (delegate_->GetInterstitialPage()) {
1905 static_cast<InterstitialPageImpl*>(delegate_->GetInterstitialPage())->
[email protected]cadaec52012-02-08 21:53:131906 CancelForNavigation();
1907 }
[email protected]6a13a6c2011-12-20 21:47:121908
initial.commit09911bf2008-07-26 23:55:291909 // For session history navigations only the pending_entry_index_ is set.
1910 if (!pending_entry_) {
[email protected]4bf3522c2010-08-19 21:00:201911 DCHECK_NE(pending_entry_index_, -1);
avi25764702015-06-23 15:43:371912 pending_entry_ = entries_[pending_entry_index_];
initial.commit09911bf2008-07-26 23:55:291913 }
1914
[email protected]40fd3032014-02-28 22:16:281915 // This call does not support re-entrancy. See http://crbug.com/347742.
1916 CHECK(!in_navigate_to_pending_entry_);
1917 in_navigate_to_pending_entry_ = true;
creis4e2ecb72015-06-20 00:46:301918 bool success = NavigateToPendingEntryInternal(reload_type);
[email protected]40fd3032014-02-28 22:16:281919 in_navigate_to_pending_entry_ = false;
1920
1921 if (!success)
[email protected]cbab76d2008-10-13 22:42:471922 DiscardNonCommittedEntries();
[email protected]77362eb2011-08-01 17:18:381923
1924 // If the entry is being restored and doesn't have a SiteInstance yet, fill
1925 // it in now that we know. This allows us to find the entry when it commits.
[email protected]77362eb2011-08-01 17:18:381926 if (pending_entry_ && !pending_entry_->site_instance() &&
[email protected]10f417c52011-12-28 21:04:231927 pending_entry_->restore_type() != NavigationEntryImpl::RESTORE_NONE) {
[email protected]b6583592012-01-25 19:52:331928 pending_entry_->set_site_instance(static_cast<SiteInstanceImpl*>(
[email protected]ec6c05f2013-10-23 18:41:571929 delegate_->GetPendingSiteInstance()));
[email protected]10f417c52011-12-28 21:04:231930 pending_entry_->set_restore_type(NavigationEntryImpl::RESTORE_NONE);
[email protected]77362eb2011-08-01 17:18:381931 }
initial.commit09911bf2008-07-26 23:55:291932}
1933
creis4e2ecb72015-06-20 00:46:301934bool NavigationControllerImpl::NavigateToPendingEntryInternal(
1935 ReloadType reload_type) {
1936 DCHECK(pending_entry_);
1937 FrameTreeNode* root = delegate_->GetFrameTree()->root();
1938
1939 // In default Chrome, there are no subframe FrameNavigationEntries. Either
1940 // navigate the main frame or use the main frame's FrameNavigationEntry to
1941 // tell the indicated frame where to go.
1942 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
1943 switches::kSitePerProcess)) {
1944 FrameNavigationEntry* frame_entry = GetPendingEntry()->GetFrameEntry(root);
1945 FrameTreeNode* frame = root;
1946 int ftn_id = GetPendingEntry()->frame_tree_node_id();
1947 if (ftn_id != -1) {
1948 frame = delegate_->GetFrameTree()->FindByID(ftn_id);
1949 DCHECK(frame);
1950 }
1951 return frame->navigator()->NavigateToPendingEntry(frame, *frame_entry,
1952 reload_type, false);
1953 }
1954
1955 // In --site-per-process, we compare FrameNavigationEntries to see which
1956 // frames in the tree need to be navigated.
1957 FrameLoadVector same_document_loads;
1958 FrameLoadVector different_document_loads;
1959 if (GetLastCommittedEntry()) {
1960 FindFramesToNavigate(root, &same_document_loads, &different_document_loads);
1961 }
1962
1963 if (same_document_loads.empty() && different_document_loads.empty()) {
1964 // If we don't have any frames to navigate at this point, either
1965 // (1) there is no previous history entry to compare against, or
1966 // (2) we were unable to match any frames by name. In the first case,
1967 // doing a different document navigation to the root item is the only valid
1968 // thing to do. In the second case, we should have been able to find a
1969 // frame to navigate based on names if this were a same document
1970 // navigation, so we can safely assume this is the different document case.
1971 different_document_loads.push_back(
1972 std::make_pair(root, pending_entry_->GetFrameEntry(root)));
1973 }
1974
1975 // If all the frame loads fail, we will discard the pending entry.
1976 bool success = false;
1977
1978 // Send all the same document frame loads before the different document loads.
1979 for (const auto& item : same_document_loads) {
1980 FrameTreeNode* frame = item.first;
1981 success |= frame->navigator()->NavigateToPendingEntry(frame, *item.second,
1982 reload_type, true);
1983 }
1984 for (const auto& item : different_document_loads) {
1985 FrameTreeNode* frame = item.first;
1986 success |= frame->navigator()->NavigateToPendingEntry(frame, *item.second,
1987 reload_type, false);
1988 }
1989 return success;
1990}
1991
1992void NavigationControllerImpl::FindFramesToNavigate(
1993 FrameTreeNode* frame,
1994 FrameLoadVector* same_document_loads,
1995 FrameLoadVector* different_document_loads) {
1996 DCHECK(pending_entry_);
1997 DCHECK_GE(last_committed_entry_index_, 0);
1998 FrameNavigationEntry* new_item = pending_entry_->GetFrameEntry(frame);
1999 FrameNavigationEntry* old_item =
2000 GetLastCommittedEntry()->GetFrameEntry(frame);
2001 if (!new_item)
2002 return;
2003
2004 // Schedule a load in this frame if the new item isn't for the same item
2005 // sequence number in the same SiteInstance.
2006 if (!old_item ||
2007 new_item->item_sequence_number() != old_item->item_sequence_number() ||
2008 new_item->site_instance() != old_item->site_instance()) {
2009 if (old_item &&
2010 new_item->document_sequence_number() ==
2011 old_item->document_sequence_number()) {
2012 same_document_loads->push_back(std::make_pair(frame, new_item));
2013 } else {
2014 different_document_loads->push_back(std::make_pair(frame, new_item));
2015 }
2016 return;
2017 }
2018
2019 for (size_t i = 0; i < frame->child_count(); i++) {
2020 FindFramesToNavigate(frame->child_at(i), same_document_loads,
2021 different_document_loads);
2022 }
2023}
2024
[email protected]d202a7c2012-01-04 07:53:472025void NavigationControllerImpl::NotifyNavigationEntryCommitted(
[email protected]8ff00d72012-10-23 19:12:212026 LoadCommittedDetails* details) {
[email protected]6286a3792013-10-09 04:03:272027 details->entry = GetLastCommittedEntry();
[email protected]df1af242009-05-01 00:11:402028
[email protected]2db9bd72012-04-13 20:20:562029 // We need to notify the ssl_manager_ before the web_contents_ so the
[email protected]df1af242009-05-01 00:11:402030 // location bar will have up-to-date information about the security style
2031 // when it wants to draw. See http://crbug.com/11157
[email protected]b0f724c2013-09-05 04:21:132032 ssl_manager_.DidCommitProvisionalLoad(*details);
[email protected]df1af242009-05-01 00:11:402033
[email protected]7f924832014-08-09 05:57:222034 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]ec6c05f2013-10-23 18:41:572035 delegate_->NotifyNavigationEntryCommitted(*details);
[email protected]cbb1ef592013-06-05 19:49:462036
[email protected]b0f724c2013-09-05 04:21:132037 // TODO(avi): Remove. http://crbug.com/170921
2038 NotificationDetails notification_details =
2039 Details<LoadCommittedDetails>(details);
[email protected]8ff00d72012-10-23 19:12:212040 NotificationService::current()->Notify(
2041 NOTIFICATION_NAV_ENTRY_COMMITTED,
2042 Source<NavigationController>(this),
[email protected]df1af242009-05-01 00:11:402043 notification_details);
initial.commit09911bf2008-07-26 23:55:292044}
2045
initial.commit09911bf2008-07-26 23:55:292046// static
[email protected]d202a7c2012-01-04 07:53:472047size_t NavigationControllerImpl::max_entry_count() {
[email protected]9b51970d2011-12-09 23:10:232048 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
2049 return max_entry_count_for_testing_;
[email protected]8ff00d72012-10-23 19:12:212050 return kMaxSessionHistoryEntries;
[email protected]9b51970d2011-12-09 23:10:232051}
2052
[email protected]d202a7c2012-01-04 07:53:472053void NavigationControllerImpl::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:222054 if (is_active && needs_reload_)
2055 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:292056}
2057
[email protected]d202a7c2012-01-04 07:53:472058void NavigationControllerImpl::LoadIfNecessary() {
initial.commit09911bf2008-07-26 23:55:292059 if (!needs_reload_)
2060 return;
2061
initial.commit09911bf2008-07-26 23:55:292062 // Calling Reload() results in ignoring state, and not loading.
2063 // Explicitly use NavigateToPendingEntry so that the renderer uses the
2064 // cached state.
2065 pending_entry_index_ = last_committed_entry_index_;
[email protected]1ccb3568d2010-02-19 10:51:162066 NavigateToPendingEntry(NO_RELOAD);
initial.commit09911bf2008-07-26 23:55:292067}
2068
avi47179332015-05-20 21:01:112069void NavigationControllerImpl::NotifyEntryChanged(
2070 const NavigationEntry* entry) {
[email protected]8ff00d72012-10-23 19:12:212071 EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:092072 det.changed_entry = entry;
avi47179332015-05-20 21:01:112073 det.index = GetIndexOfEntry(
2074 NavigationEntryImpl::FromNavigationEntry(entry));
[email protected]8ff00d72012-10-23 19:12:212075 NotificationService::current()->Notify(
2076 NOTIFICATION_NAV_ENTRY_CHANGED,
2077 Source<NavigationController>(this),
2078 Details<EntryChangedDetails>(&det));
initial.commit09911bf2008-07-26 23:55:292079}
2080
[email protected]d202a7c2012-01-04 07:53:472081void NavigationControllerImpl::FinishRestore(int selected_index,
[email protected]2ca1ea662012-10-04 02:26:362082 RestoreType type) {
[email protected]a26023822011-12-29 00:23:552083 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]2ca1ea662012-10-04 02:26:362084 ConfigureEntriesForRestore(&entries_, type);
initial.commit09911bf2008-07-26 23:55:292085
[email protected]71fde352011-12-29 03:29:562086 SetMaxRestoredPageID(static_cast<int32>(GetEntryCount()));
initial.commit09911bf2008-07-26 23:55:292087
2088 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:292089}
[email protected]765b35502008-08-21 00:51:202090
[email protected]d202a7c2012-01-04 07:53:472091void NavigationControllerImpl::DiscardNonCommittedEntriesInternal() {
avi45a72532015-04-07 21:01:452092 DiscardPendingEntry(false);
[email protected]b12eb222013-09-10 00:11:482093 DiscardTransientEntry();
2094}
2095
avi45a72532015-04-07 21:01:452096void NavigationControllerImpl::DiscardPendingEntry(bool was_failure) {
[email protected]40fd3032014-02-28 22:16:282097 // It is not safe to call DiscardPendingEntry while NavigateToEntry is in
[email protected]e7fee58a2014-04-11 19:51:402098 // progress, since this will cause a use-after-free. (We only allow this
2099 // when the tab is being destroyed for shutdown, since it won't return to
2100 // NavigateToEntry in that case.) http://crbug.com/347742.
2101 CHECK(!in_navigate_to_pending_entry_ || delegate_->IsBeingDestroyed());
[email protected]40fd3032014-02-28 22:16:282102
avi45a72532015-04-07 21:01:452103 if (was_failure && pending_entry_) {
2104 failed_pending_entry_id_ = pending_entry_->GetUniqueID();
2105 failed_pending_entry_should_replace_ =
2106 pending_entry_->should_replace_entry();
2107 } else {
2108 failed_pending_entry_id_ = 0;
2109 }
2110
[email protected]765b35502008-08-21 00:51:202111 if (pending_entry_index_ == -1)
2112 delete pending_entry_;
2113 pending_entry_ = NULL;
2114 pending_entry_index_ = -1;
[email protected]cbab76d2008-10-13 22:42:472115}
2116
[email protected]d202a7c2012-01-04 07:53:472117void NavigationControllerImpl::DiscardTransientEntry() {
[email protected]cbab76d2008-10-13 22:42:472118 if (transient_entry_index_ == -1)
2119 return;
[email protected]a0e69262009-06-03 19:08:482120 entries_.erase(entries_.begin() + transient_entry_index_);
[email protected]80858db52009-10-15 00:35:182121 if (last_committed_entry_index_ > transient_entry_index_)
2122 last_committed_entry_index_--;
[email protected]cbab76d2008-10-13 22:42:472123 transient_entry_index_ = -1;
[email protected]765b35502008-08-21 00:51:202124}
2125
[email protected]d202a7c2012-01-04 07:53:472126int NavigationControllerImpl::GetEntryIndexWithPageID(
[email protected]7f0005a2009-04-15 03:25:112127 SiteInstance* instance, int32 page_id) const {
[email protected]765b35502008-08-21 00:51:202128 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
[email protected]7f0005a2009-04-15 03:25:112129 if ((entries_[i]->site_instance() == instance) &&
[email protected]36fc0392011-12-25 03:59:512130 (entries_[i]->GetPageID() == page_id))
[email protected]765b35502008-08-21 00:51:202131 return i;
2132 }
2133 return -1;
2134}
[email protected]cbab76d2008-10-13 22:42:472135
avi7c6f35e2015-05-08 17:52:382136int NavigationControllerImpl::GetEntryIndexWithUniqueID(
2137 int nav_entry_id) const {
2138 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
2139 if (entries_[i]->GetUniqueID() == nav_entry_id)
2140 return i;
2141 }
2142 return -1;
2143}
2144
creis3da03872015-02-20 21:12:322145NavigationEntryImpl* NavigationControllerImpl::GetTransientEntry() const {
[email protected]cbab76d2008-10-13 22:42:472146 if (transient_entry_index_ == -1)
2147 return NULL;
avi25764702015-06-23 15:43:372148 return entries_[transient_entry_index_];
[email protected]cbab76d2008-10-13 22:42:472149}
[email protected]e1cd5452010-08-26 18:03:252150
avi25764702015-06-23 15:43:372151void NavigationControllerImpl::SetTransientEntry(
2152 scoped_ptr<NavigationEntry> entry) {
[email protected]0b684262013-02-20 02:18:212153 // Discard any current transient entry, we can only have one at a time.
2154 int index = 0;
2155 if (last_committed_entry_index_ != -1)
2156 index = last_committed_entry_index_ + 1;
2157 DiscardTransientEntry();
avi25764702015-06-23 15:43:372158 entries_.insert(entries_.begin() + index,
2159 NavigationEntryImpl::FromNavigationEntry(entry.release()));
[email protected]0b684262013-02-20 02:18:212160 transient_entry_index_ = index;
[email protected]7f924832014-08-09 05:57:222161 delegate_->NotifyNavigationStateChanged(INVALIDATE_TYPE_ALL);
[email protected]0b684262013-02-20 02:18:212162}
2163
[email protected]d202a7c2012-01-04 07:53:472164void NavigationControllerImpl::InsertEntriesFrom(
2165 const NavigationControllerImpl& source,
[email protected]e1cd5452010-08-26 18:03:252166 int max_index) {
[email protected]a26023822011-12-29 00:23:552167 DCHECK_LE(max_index, source.GetEntryCount());
[email protected]e1cd5452010-08-26 18:03:252168 size_t insert_index = 0;
2169 for (int i = 0; i < max_index; i++) {
creisef4a0cb2015-03-12 19:14:352170 // When cloning a tab, copy all entries except interstitial pages.
avi25764702015-06-23 15:43:372171 if (source.entries_[i]->GetPageType() != PAGE_TYPE_INTERSTITIAL) {
creisef4a0cb2015-03-12 19:14:352172 // TODO(creis): Once we start sharing FrameNavigationEntries between
2173 // NavigationEntries, it will not be safe to share them with another tab.
2174 // Must have a version of Clone that recreates them.
[email protected]e1cd5452010-08-26 18:03:252175 entries_.insert(entries_.begin() + insert_index++,
avi25764702015-06-23 15:43:372176 source.entries_[i]->Clone().Pass());
[email protected]e1cd5452010-08-26 18:03:252177 }
2178 }
2179}
[email protected]c5b88d82012-10-06 17:03:332180
2181void NavigationControllerImpl::SetGetTimestampCallbackForTest(
2182 const base::Callback<base::Time()>& get_timestamp_callback) {
2183 get_timestamp_callback_ = get_timestamp_callback;
2184}
[email protected]8ff00d72012-10-23 19:12:212185
2186} // namespace content