blob: dbbf9d4e321b7411c1a98b2fbc2c23e980968d10 [file] [log] [blame]
[email protected]7f070d42011-03-09 20:25:321// Copyright (c) 2011 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
[email protected]0dd3a0ab2011-02-18 08:17:445#include "content/browser/tab_contents/navigation_controller.h"
initial.commit09911bf2008-07-26 23:55:296
initial.commit09911bf2008-07-26 23:55:297#include "base/file_util.h"
8#include "base/logging.h"
[email protected]b69934e2011-10-29 02:51:529#include "base/string_number_conversions.h" // Temporary
initial.commit09911bf2008-07-26 23:55:2910#include "base/string_util.h"
[email protected]b689fce72009-03-17 22:45:3411#include "base/time.h"
[email protected]252cad62010-08-18 18:33:5712#include "base/utf_string_conversions.h"
[email protected]b8148ac2011-07-13 22:03:2513#include "content/browser/browser_url_handler.h"
[email protected]419a0572011-04-18 22:21:4614#include "content/browser/child_process_security_policy.h"
[email protected]567812d2011-02-24 17:40:5015#include "content/browser/in_process_webkit/session_storage_namespace.h"
[email protected]b69934e2011-10-29 02:51:5216#include "content/browser/renderer_host/render_view_host.h" // Temporary
[email protected]567812d2011-02-24 17:40:5017#include "content/browser/site_instance.h"
[email protected]0dd3a0ab2011-02-18 08:17:4418#include "content/browser/tab_contents/interstitial_page.h"
[email protected]10f417c52011-12-28 21:04:2319#include "content/browser/tab_contents/navigation_entry_impl.h"
[email protected]0dd3a0ab2011-02-18 08:17:4420#include "content/browser/tab_contents/tab_contents.h"
[email protected]0aed2f52011-03-23 18:06:3621#include "content/common/view_messages.h"
[email protected]ccb797302011-12-15 16:55:1122#include "content/public/browser/browser_context.h"
[email protected]5b96836f2011-12-22 07:39:0023#include "content/public/browser/navigation_details.h"
[email protected]7f6f44c2011-12-14 13:23:3824#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1625#include "content/public/browser/notification_types.h"
[email protected]7f6f44c2011-12-14 13:23:3826#include "content/public/browser/user_metrics.h"
[email protected]674bc592011-12-20 23:00:4227#include "content/public/browser/web_contents_delegate.h"
[email protected]7f6f44c2011-12-14 13:23:3828#include "content/public/common/content_constants.h"
[email protected]a23de8572009-06-03 02:16:3229#include "net/base/escape.h"
[email protected]31682282010-01-15 18:05:1630#include "net/base/mime_util.h"
[email protected]7f070d42011-03-09 20:25:3231#include "net/base/net_util.h"
[email protected]765b35502008-08-21 00:51:2032#include "webkit/glue/webkit_glue.h"
initial.commit09911bf2008-07-26 23:55:2933
[email protected]e5d549d2011-12-28 01:29:2034using content::GlobalRequestID;
[email protected]10f417c52011-12-28 21:04:2335using content::NavigationEntry;
36using content::NavigationEntryImpl;
[email protected]7f6f44c2011-12-14 13:23:3837using content::UserMetricsAction;
[email protected]fbc5e5f92012-01-02 06:08:3238using content::WebContents;
[email protected]7f6f44c2011-12-14 13:23:3839
[email protected]e9ba4472008-09-14 15:42:4340namespace {
41
[email protected]93f230e02011-06-01 14:40:0042const int kInvalidateAll = 0xFFFFFFFF;
[email protected]8030f012009-09-25 18:09:3743
[email protected]e9ba4472008-09-14 15:42:4344// Invoked when entries have been pruned, or removed. For example, if the
45// current entries are [google, digg, yahoo], with the current entry google,
46// and the user types in cnet, then digg and yahoo are pruned.
[email protected]c12bf1a12008-09-17 16:28:4947void NotifyPrunedEntries(NavigationController* nav_controller,
48 bool from_front,
49 int count) {
[email protected]8286f51a2011-05-31 17:39:1350 content::PrunedDetails details;
[email protected]c12bf1a12008-09-17 16:28:4951 details.from_front = from_front;
52 details.count = count;
[email protected]ad50def52011-10-19 23:17:0753 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:2754 content::NOTIFICATION_NAV_LIST_PRUNED,
[email protected]cca0f1e2012-01-03 18:27:4655 content::Source<content::NavigationController>(nav_controller),
[email protected]6c2381d2011-10-19 02:52:5356 content::Details<content::PrunedDetails>(&details));
[email protected]e9ba4472008-09-14 15:42:4357}
58
59// Ensure the given NavigationEntry has a valid state, so that WebKit does not
60// get confused if we navigate back to it.
[email protected]40bcc302009-03-02 20:50:3961//
[email protected]e9ba4472008-09-14 15:42:4362// An empty state is treated as a new navigation by WebKit, which would mean
63// losing the navigation entries and generating a new navigation entry after
64// this one. We don't want that. To avoid this we create a valid state which
65// WebKit will not treat as a new navigation.
[email protected]10f417c52011-12-28 21:04:2366void SetContentStateIfEmpty(NavigationEntryImpl* entry) {
[email protected]36fc0392011-12-25 03:59:5167 if (entry->GetContentState().empty()) {
68 entry->SetContentState(
69 webkit_glue::CreateHistoryStateForURL(entry->GetURL()));
[email protected]e9ba4472008-09-14 15:42:4370 }
71}
72
73// Configure all the NavigationEntries in entries for restore. This resets
74// the transition type to reload and makes sure the content state isn't empty.
75void ConfigureEntriesForRestore(
[email protected]10f417c52011-12-28 21:04:2376 std::vector<linked_ptr<NavigationEntryImpl> >* entries,
[email protected]5e369672009-11-03 23:48:3077 bool from_last_session) {
[email protected]e9ba4472008-09-14 15:42:4378 for (size_t i = 0; i < entries->size(); ++i) {
79 // Use a transition type of reload so that we don't incorrectly increase
80 // the typed count.
[email protected]022af742011-12-28 18:37:2581 (*entries)[i]->SetTransitionType(content::PAGE_TRANSITION_RELOAD);
[email protected]5e369672009-11-03 23:48:3082 (*entries)[i]->set_restore_type(from_last_session ?
[email protected]10f417c52011-12-28 21:04:2383 NavigationEntryImpl::RESTORE_LAST_SESSION :
84 NavigationEntryImpl::RESTORE_CURRENT_SESSION);
[email protected]e9ba4472008-09-14 15:42:4385 // NOTE(darin): This code is only needed for backwards compat.
86 SetContentStateIfEmpty((*entries)[i].get());
87 }
88}
89
90// See NavigationController::IsURLInPageNavigation for how this works and why.
91bool AreURLsInPageNavigation(const GURL& existing_url, const GURL& new_url) {
[email protected]192d8c5e2010-02-23 07:26:3292 if (existing_url == new_url || !new_url.has_ref()) {
93 // TODO(jcampan): what about when navigating back from a ref URL to the top
94 // non ref URL? Nothing is loaded in that case but we return false here.
95 // The user could also navigate from the ref URL to the non ref URL by
96 // entering the non ref URL in the location bar or through a bookmark, in
97 // which case there would be a load. I am not sure if the non-load/load
98 // scenarios can be differentiated with the TransitionType.
[email protected]e9ba4472008-09-14 15:42:4399 return false;
[email protected]192d8c5e2010-02-23 07:26:32100 }
[email protected]e9ba4472008-09-14 15:42:43101
102 url_canon::Replacements<char> replacements;
103 replacements.ClearRef();
104 return existing_url.ReplaceComponents(replacements) ==
105 new_url.ReplaceComponents(replacements);
106}
107
108} // namespace
109
initial.commit09911bf2008-07-26 23:55:29110// NavigationController ---------------------------------------------------
111
[email protected]9b51970d2011-12-09 23:10:23112const size_t kMaxEntryCountForTestingNotSet = -1;
113
[email protected]765b35502008-08-21 00:51:20114// static
[email protected]9b51970d2011-12-09 23:10:23115size_t NavigationController::max_entry_count_for_testing_ =
116 kMaxEntryCountForTestingNotSet;
[email protected]765b35502008-08-21 00:51:20117
initial.commit09911bf2008-07-26 23:55:29118// static
119bool NavigationController::check_for_repost_ = true;
120
[email protected]71fde352011-12-29 03:29:56121// static
122NavigationEntry* content::NavigationController::CreateNavigationEntry(
123 const GURL& url,
124 const content::Referrer& referrer,
125 content::PageTransition transition,
126 bool is_renderer_initiated,
127 const std::string& extra_headers,
128 content::BrowserContext* browser_context) {
129 // Allow the browser URL handler to rewrite the URL. This will, for example,
130 // remove "view-source:" from the beginning of the URL to get the URL that
131 // will actually be loaded. This real URL won't be shown to the user, just
132 // used internally.
133 GURL loaded_url(url);
134 bool reverse_on_redirect = false;
135 BrowserURLHandler::GetInstance()->RewriteURLIfNecessary(
136 &loaded_url, browser_context, &reverse_on_redirect);
137
138 NavigationEntryImpl* entry = new NavigationEntryImpl(
139 NULL, // The site instance for tabs is sent on navigation
140 // (TabContents::GetSiteInstance).
141 -1,
142 loaded_url,
143 referrer,
144 string16(),
145 transition,
146 is_renderer_initiated);
147 entry->SetVirtualURL(url);
148 entry->set_user_typed_url(url);
149 entry->set_update_virtual_url_with_url(reverse_on_redirect);
150 entry->set_extra_headers(extra_headers);
151 return entry;
152}
153
[email protected]6ee12c42010-09-14 09:36:07154NavigationController::NavigationController(
155 TabContents* contents,
[email protected]3d7474ff2011-07-27 17:47:37156 content::BrowserContext* browser_context,
[email protected]6ee12c42010-09-14 09:36:07157 SessionStorageNamespace* session_storage_namespace)
[email protected]3d7474ff2011-07-27 17:47:37158 : browser_context_(browser_context),
[email protected]765b35502008-08-21 00:51:20159 pending_entry_(NULL),
160 last_committed_entry_index_(-1),
161 pending_entry_index_(-1),
[email protected]cbab76d2008-10-13 22:42:47162 transient_entry_index_(-1),
[email protected]9423d9412009-04-14 22:13:55163 tab_contents_(contents),
initial.commit09911bf2008-07-26 23:55:29164 max_restored_page_id_(-1),
[email protected]5d063842009-05-15 04:08:24165 ALLOW_THIS_IN_INITIALIZER_LIST(ssl_manager_(this)),
[email protected]38b8f4e2009-09-24 19:44:57166 needs_reload_(false),
[email protected]6ee12c42010-09-14 09:36:07167 session_storage_namespace_(session_storage_namespace),
[email protected]106a0812010-03-18 00:15:12168 pending_reload_(NO_RELOAD) {
[email protected]3d7474ff2011-07-27 17:47:37169 DCHECK(browser_context_);
[email protected]228d06592011-04-01 20:38:59170 if (!session_storage_namespace_) {
171 session_storage_namespace_ = new SessionStorageNamespace(
[email protected]3d7474ff2011-07-27 17:47:37172 browser_context_->GetWebKitContext());
[email protected]228d06592011-04-01 20:38:59173 }
initial.commit09911bf2008-07-26 23:55:29174}
175
initial.commit09911bf2008-07-26 23:55:29176NavigationController::~NavigationController() {
[email protected]cbab76d2008-10-13 22:42:47177 DiscardNonCommittedEntriesInternal();
[email protected]c0993872008-08-21 19:59:44178
[email protected]ad50def52011-10-19 23:17:07179 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27180 content::NOTIFICATION_TAB_CLOSED,
[email protected]cca0f1e2012-01-03 18:27:46181 content::Source<content::NavigationController>(this),
[email protected]ad50def52011-10-19 23:17:07182 content::NotificationService::NoDetails());
initial.commit09911bf2008-07-26 23:55:29183}
184
[email protected]fbc5e5f92012-01-02 06:08:32185WebContents* NavigationController::GetWebContents() const {
186 return tab_contents_;
187}
188
[email protected]a26023822011-12-29 00:23:55189content::BrowserContext* NavigationController::GetBrowserContext() const {
190 return browser_context_;
191}
192
[email protected]03838e22011-06-06 15:27:14193void NavigationController::Restore(
[email protected]5e369672009-11-03 23:48:30194 int selected_navigation,
[email protected]03838e22011-06-06 15:27:14195 bool from_last_session,
[email protected]10f417c52011-12-28 21:04:23196 std::vector<NavigationEntry*>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57197 // Verify that this controller is unused and that the input is valid.
[email protected]a26023822011-12-29 00:23:55198 DCHECK(GetEntryCount() == 0 && !GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57199 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14200 selected_navigation < static_cast<int>(entries->size()));
[email protected]ce3fa3c2009-04-20 19:55:57201
[email protected]ce3fa3c2009-04-20 19:55:57202 needs_reload_ = true;
[email protected]ad23a092011-12-28 07:02:04203 for (size_t i = 0; i < entries->size(); ++i) {
[email protected]10f417c52011-12-28 21:04:23204 NavigationEntryImpl* entry =
205 NavigationEntryImpl::FromNavigationEntry((*entries)[i]);
206 entries_.push_back(linked_ptr<NavigationEntryImpl>(entry));
[email protected]ad23a092011-12-28 07:02:04207 }
[email protected]03838e22011-06-06 15:27:14208 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57209
210 // And finish the restore.
[email protected]5e369672009-11-03 23:48:30211 FinishRestore(selected_navigation, from_last_session);
[email protected]ce3fa3c2009-04-20 19:55:57212}
213
[email protected]f1c74112008-10-30 16:17:04214void NavigationController::Reload(bool check_for_repost) {
[email protected]1ccb3568d2010-02-19 10:51:16215 ReloadInternal(check_for_repost, RELOAD);
216}
217void NavigationController::ReloadIgnoringCache(bool check_for_repost) {
218 ReloadInternal(check_for_repost, RELOAD_IGNORING_CACHE);
219}
220
221void NavigationController::ReloadInternal(bool check_for_repost,
222 ReloadType reload_type) {
[email protected]cbab76d2008-10-13 22:42:47223 // Reloading a transient entry does nothing.
224 if (transient_entry_index_ != -1)
225 return;
226
227 DiscardNonCommittedEntriesInternal();
initial.commit09911bf2008-07-26 23:55:29228 int current_index = GetCurrentEntryIndex();
[email protected]106a0812010-03-18 00:15:12229 // If we are no where, then we can't reload. TODO(darin): We should add a
230 // CanReload method.
231 if (current_index == -1) {
232 return;
233 }
234
[email protected]106a0812010-03-18 00:15:12235 if (check_for_repost_ && check_for_repost &&
[email protected]36fc0392011-12-25 03:59:51236 GetEntryAtIndex(current_index)->GetHasPostData()) {
[email protected]a3a1d142008-12-19 00:42:30237 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07238 // they really want to do this. If they do, the dialog will call us back
239 // with check_for_repost = false.
[email protected]ad50def52011-10-19 23:17:07240 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27241 content::NOTIFICATION_REPOST_WARNING_SHOWN,
[email protected]cca0f1e2012-01-03 18:27:46242 content::Source<content::NavigationController>(this),
[email protected]ad50def52011-10-19 23:17:07243 content::NotificationService::NoDetails());
[email protected]965bb092010-04-09 11:59:02244
[email protected]106a0812010-03-18 00:15:12245 pending_reload_ = reload_type;
[email protected]9423d9412009-04-14 22:13:55246 tab_contents_->Activate();
[email protected]6934a702011-12-20 00:04:51247 tab_contents_->GetDelegate()->ShowRepostFormWarningDialog(tab_contents_);
initial.commit09911bf2008-07-26 23:55:29248 } else {
[email protected]cbab76d2008-10-13 22:42:47249 DiscardNonCommittedEntriesInternal();
[email protected]765b35502008-08-21 00:51:20250
251 pending_entry_index_ = current_index;
[email protected]022af742011-12-28 18:37:25252 entries_[pending_entry_index_]->SetTransitionType(
[email protected]2905f742011-10-13 03:51:58253 content::PAGE_TRANSITION_RELOAD);
[email protected]1ccb3568d2010-02-19 10:51:16254 NavigateToPendingEntry(reload_type);
initial.commit09911bf2008-07-26 23:55:29255 }
256}
257
[email protected]106a0812010-03-18 00:15:12258void NavigationController::CancelPendingReload() {
259 DCHECK(pending_reload_ != NO_RELOAD);
260 pending_reload_ = NO_RELOAD;
261}
262
263void NavigationController::ContinuePendingReload() {
264 if (pending_reload_ == NO_RELOAD) {
265 NOTREACHED();
266 } else {
267 ReloadInternal(false, pending_reload_);
[email protected]965bb092010-04-09 11:59:02268 pending_reload_ = NO_RELOAD;
[email protected]106a0812010-03-18 00:15:12269 }
270}
271
[email protected]c70f9b82010-04-21 07:31:11272bool NavigationController::IsInitialNavigation() {
273 return last_document_loaded_.is_null();
274}
275
[email protected]10f417c52011-12-28 21:04:23276NavigationEntryImpl* NavigationController::GetEntryWithPageID(
[email protected]7f0005a2009-04-15 03:25:11277 SiteInstance* instance, int32 page_id) const {
278 int index = GetEntryIndexWithPageID(instance, page_id);
[email protected]765b35502008-08-21 00:51:20279 return (index != -1) ? entries_[index].get() : NULL;
280}
281
[email protected]10f417c52011-12-28 21:04:23282void NavigationController::LoadEntry(NavigationEntryImpl* entry) {
[email protected]419a0572011-04-18 22:21:46283 // Don't navigate to URLs disabled by policy. This prevents showing the URL
284 // on the Omnibar when it is also going to be blocked by
285 // ChildProcessSecurityPolicy::CanRequestURL.
286 ChildProcessSecurityPolicy *policy =
287 ChildProcessSecurityPolicy::GetInstance();
[email protected]36fc0392011-12-25 03:59:51288 if (policy->IsDisabledScheme(entry->GetURL().scheme()) ||
289 policy->IsDisabledScheme(entry->GetVirtualURL().scheme())) {
[email protected]419a0572011-04-18 22:21:46290 VLOG(1) << "URL not loaded because the scheme is blocked by policy: "
[email protected]36fc0392011-12-25 03:59:51291 << entry->GetURL();
[email protected]419a0572011-04-18 22:21:46292 delete entry;
293 return;
294 }
295
[email protected]765b35502008-08-21 00:51:20296 // When navigating to a new page, we don't know for sure if we will actually
297 // end up leaving the current page. The new page load could for example
298 // result in a download or a 'no content' response (e.g., a mailto: URL).
[email protected]cbab76d2008-10-13 22:42:47299 DiscardNonCommittedEntriesInternal();
[email protected]765b35502008-08-21 00:51:20300 pending_entry_ = entry;
[email protected]ad50def52011-10-19 23:17:07301 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27302 content::NOTIFICATION_NAV_ENTRY_PENDING,
[email protected]cca0f1e2012-01-03 18:27:46303 content::Source<content::NavigationController>(this),
[email protected]10f417c52011-12-28 21:04:23304 content::Details<NavigationEntry>(entry));
[email protected]1ccb3568d2010-02-19 10:51:16305 NavigateToPendingEntry(NO_RELOAD);
[email protected]765b35502008-08-21 00:51:20306}
307
[email protected]10f417c52011-12-28 21:04:23308NavigationEntry* NavigationController::GetActiveEntry() const {
[email protected]cbab76d2008-10-13 22:42:47309 if (transient_entry_index_ != -1)
310 return entries_[transient_entry_index_].get();
311 if (pending_entry_)
312 return pending_entry_;
313 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20314}
315
[email protected]10f417c52011-12-28 21:04:23316NavigationEntry* NavigationController::GetVisibleEntry() const {
[email protected]867e1f92011-08-30 19:01:19317 if (transient_entry_index_ != -1)
318 return entries_[transient_entry_index_].get();
[email protected]e47ae9472011-10-13 19:48:34319 // Only return the pending_entry for new (non-history), browser-initiated
320 // navigations, in order to prevent URL spoof attacks.
321 // Ideally we would also show the pending entry's URL for new renderer-
322 // initiated navigations with no last committed entry (e.g., a link opening
323 // in a new tab), but an attacker can insert content into the about:blank
324 // page while the pending URL loads in that case.
325 if (pending_entry_ &&
[email protected]36fc0392011-12-25 03:59:51326 pending_entry_->GetPageID() == -1 &&
[email protected]e47ae9472011-10-13 19:48:34327 !pending_entry_->is_renderer_initiated())
[email protected]867e1f92011-08-30 19:01:19328 return pending_entry_;
329 return GetLastCommittedEntry();
330}
331
[email protected]765b35502008-08-21 00:51:20332int NavigationController::GetCurrentEntryIndex() const {
[email protected]cbab76d2008-10-13 22:42:47333 if (transient_entry_index_ != -1)
334 return transient_entry_index_;
[email protected]765b35502008-08-21 00:51:20335 if (pending_entry_index_ != -1)
336 return pending_entry_index_;
337 return last_committed_entry_index_;
338}
339
[email protected]10f417c52011-12-28 21:04:23340NavigationEntry* NavigationController::GetLastCommittedEntry() const {
[email protected]765b35502008-08-21 00:51:20341 if (last_committed_entry_index_ == -1)
342 return NULL;
343 return entries_[last_committed_entry_index_].get();
344}
345
[email protected]31682282010-01-15 18:05:16346bool NavigationController::CanViewSource() const {
347 bool is_supported_mime_type = net::IsSupportedNonImageMimeType(
[email protected]0bfbf882011-12-22 18:19:27348 tab_contents_->GetContentsMimeType().c_str());
[email protected]10f417c52011-12-28 21:04:23349 NavigationEntry* active_entry = GetActiveEntry();
[email protected]31682282010-01-15 18:05:16350 return active_entry && !active_entry->IsViewSourceMode() &&
[email protected]0bfbf882011-12-22 18:19:27351 is_supported_mime_type && !tab_contents_->GetInterstitialPage();
[email protected]31682282010-01-15 18:05:16352}
353
[email protected]a26023822011-12-29 00:23:55354int NavigationController::GetLastCommittedEntryIndex() const {
355 return last_committed_entry_index_;
356}
357
358int NavigationController::GetEntryCount() const {
359 return static_cast<int>(entries_.size());
360}
361
[email protected]10f417c52011-12-28 21:04:23362NavigationEntry* NavigationController::GetEntryAtIndex(
[email protected]022af742011-12-28 18:37:25363 int index) const {
364 return entries_.at(index).get();
365}
366
[email protected]10f417c52011-12-28 21:04:23367NavigationEntry* NavigationController::GetEntryAtOffset(
[email protected]022af742011-12-28 18:37:25368 int offset) const {
[email protected]cbab76d2008-10-13 22:42:47369 int index = (transient_entry_index_ != -1) ?
370 transient_entry_index_ + offset :
371 last_committed_entry_index_ + offset;
[email protected]a26023822011-12-29 00:23:55372 if (index < 0 || index >= GetEntryCount())
[email protected]765b35502008-08-21 00:51:20373 return NULL;
374
375 return entries_[index].get();
376}
377
[email protected]765b35502008-08-21 00:51:20378bool NavigationController::CanGoBack() const {
379 return entries_.size() > 1 && GetCurrentEntryIndex() > 0;
380}
381
382bool NavigationController::CanGoForward() const {
383 int index = GetCurrentEntryIndex();
384 return index >= 0 && index < (static_cast<int>(entries_.size()) - 1);
385}
386
387void NavigationController::GoBack() {
388 if (!CanGoBack()) {
389 NOTREACHED();
390 return;
391 }
392
393 // Base the navigation on where we are now...
394 int current_index = GetCurrentEntryIndex();
395
[email protected]cbab76d2008-10-13 22:42:47396 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20397
398 pending_entry_index_ = current_index - 1;
[email protected]022af742011-12-28 18:37:25399 entries_[pending_entry_index_]->SetTransitionType(
[email protected]2905f742011-10-13 03:51:58400 content::PageTransitionFromInt(
[email protected]36fc0392011-12-25 03:59:51401 entries_[pending_entry_index_]->GetTransitionType() |
[email protected]2905f742011-10-13 03:51:58402 content::PAGE_TRANSITION_FORWARD_BACK));
[email protected]1ccb3568d2010-02-19 10:51:16403 NavigateToPendingEntry(NO_RELOAD);
[email protected]765b35502008-08-21 00:51:20404}
405
406void NavigationController::GoForward() {
407 if (!CanGoForward()) {
408 NOTREACHED();
409 return;
410 }
411
[email protected]cbab76d2008-10-13 22:42:47412 bool transient = (transient_entry_index_ != -1);
413
[email protected]765b35502008-08-21 00:51:20414 // Base the navigation on where we are now...
415 int current_index = GetCurrentEntryIndex();
416
[email protected]cbab76d2008-10-13 22:42:47417 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20418
[email protected]cbab76d2008-10-13 22:42:47419 pending_entry_index_ = current_index;
420 // If there was a transient entry, we removed it making the current index
421 // the next page.
422 if (!transient)
423 pending_entry_index_++;
424
[email protected]022af742011-12-28 18:37:25425 entries_[pending_entry_index_]->SetTransitionType(
[email protected]2905f742011-10-13 03:51:58426 content::PageTransitionFromInt(
[email protected]36fc0392011-12-25 03:59:51427 entries_[pending_entry_index_]->GetTransitionType() |
[email protected]2905f742011-10-13 03:51:58428 content::PAGE_TRANSITION_FORWARD_BACK));
[email protected]1ccb3568d2010-02-19 10:51:16429 NavigateToPendingEntry(NO_RELOAD);
[email protected]765b35502008-08-21 00:51:20430}
431
432void NavigationController::GoToIndex(int index) {
433 if (index < 0 || index >= static_cast<int>(entries_.size())) {
434 NOTREACHED();
435 return;
436 }
437
[email protected]cbab76d2008-10-13 22:42:47438 if (transient_entry_index_ != -1) {
439 if (index == transient_entry_index_) {
440 // Nothing to do when navigating to the transient.
441 return;
442 }
443 if (index > transient_entry_index_) {
444 // Removing the transient is goint to shift all entries by 1.
445 index--;
446 }
447 }
448
449 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20450
451 pending_entry_index_ = index;
[email protected]022af742011-12-28 18:37:25452 entries_[pending_entry_index_]->SetTransitionType(
[email protected]2905f742011-10-13 03:51:58453 content::PageTransitionFromInt(
[email protected]36fc0392011-12-25 03:59:51454 entries_[pending_entry_index_]->GetTransitionType() |
[email protected]2905f742011-10-13 03:51:58455 content::PAGE_TRANSITION_FORWARD_BACK));
[email protected]1ccb3568d2010-02-19 10:51:16456 NavigateToPendingEntry(NO_RELOAD);
[email protected]765b35502008-08-21 00:51:20457}
458
459void NavigationController::GoToOffset(int offset) {
[email protected]cbab76d2008-10-13 22:42:47460 int index = (transient_entry_index_ != -1) ?
461 transient_entry_index_ + offset :
462 last_committed_entry_index_ + offset;
[email protected]a26023822011-12-29 00:23:55463 if (index < 0 || index >= GetEntryCount())
[email protected]765b35502008-08-21 00:51:20464 return;
465
466 GoToIndex(index);
467}
468
[email protected]6a13a6c2011-12-20 21:47:12469void NavigationController::RemoveEntryAtIndex(int index) {
470 if (index == last_committed_entry_index_)
471 return;
472
[email protected]43032342011-03-21 14:10:31473 RemoveEntryAtIndexInternal(index);
[email protected]cbab76d2008-10-13 22:42:47474}
475
[email protected]38178a42009-12-17 18:58:32476void NavigationController::UpdateVirtualURLToURL(
[email protected]10f417c52011-12-28 21:04:23477 NavigationEntryImpl* entry, const GURL& new_url) {
[email protected]38178a42009-12-17 18:58:32478 GURL new_virtual_url(new_url);
[email protected]f1eb87a2011-05-06 17:49:41479 if (BrowserURLHandler::GetInstance()->ReverseURLRewrite(
[email protected]36fc0392011-12-25 03:59:51480 &new_virtual_url, entry->GetVirtualURL(), browser_context_)) {
481 entry->SetVirtualURL(new_virtual_url);
[email protected]38178a42009-12-17 18:58:32482 }
483}
484
[email protected]10f417c52011-12-28 21:04:23485void NavigationController::AddTransientEntry(NavigationEntryImpl* entry) {
[email protected]cbab76d2008-10-13 22:42:47486 // Discard any current transient entry, we can only have one at a time.
487 int index = 0;
488 if (last_committed_entry_index_ != -1)
489 index = last_committed_entry_index_ + 1;
490 DiscardTransientEntry();
[email protected]10f417c52011-12-28 21:04:23491 entries_.insert(
492 entries_.begin() + index, linked_ptr<NavigationEntryImpl>(entry));
[email protected]e1cd5452010-08-26 18:03:25493 transient_entry_index_ = index;
[email protected]93f230e02011-06-01 14:40:00494 tab_contents_->NotifyNavigationStateChanged(kInvalidateAll);
[email protected]cbab76d2008-10-13 22:42:47495}
496
[email protected]4ad5d77d2011-12-03 02:00:48497void NavigationController::TransferURL(
498 const GURL& url,
[email protected]95dacd02011-12-05 10:35:26499 const content::Referrer& referrer,
[email protected]4ad5d77d2011-12-03 02:00:48500 content::PageTransition transition,
501 const std::string& extra_headers,
502 const GlobalRequestID& transferred_global_request_id,
503 bool is_renderer_initiated) {
504 // The user initiated a load, we don't need to reload anymore.
505 needs_reload_ = false;
506
[email protected]71fde352011-12-29 03:29:56507 NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry(
508 CreateNavigationEntry(
509 url, referrer, transition, is_renderer_initiated, extra_headers,
510 browser_context_));
[email protected]4ad5d77d2011-12-03 02:00:48511 entry->set_transferred_global_request_id(transferred_global_request_id);
512
513 LoadEntry(entry);
514}
515
[email protected]79046fd2011-09-10 05:48:33516void NavigationController::LoadURL(
[email protected]6c6b02d2011-09-02 03:36:47517 const GURL& url,
[email protected]95dacd02011-12-05 10:35:26518 const content::Referrer& referrer,
[email protected]2905f742011-10-13 03:51:58519 content::PageTransition transition,
[email protected]6c6b02d2011-09-02 03:36:47520 const std::string& extra_headers) {
initial.commit09911bf2008-07-26 23:55:29521 // The user initiated a load, we don't need to reload anymore.
522 needs_reload_ = false;
523
[email protected]71fde352011-12-29 03:29:56524 NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry(
525 CreateNavigationEntry(
526 url, referrer, transition, false, extra_headers, browser_context_));
[email protected]e47ae9472011-10-13 19:48:34527
528 LoadEntry(entry);
529}
530
531void NavigationController::LoadURLFromRenderer(
532 const GURL& url,
[email protected]95dacd02011-12-05 10:35:26533 const content::Referrer& referrer,
[email protected]e47ae9472011-10-13 19:48:34534 content::PageTransition transition,
535 const std::string& extra_headers) {
536 // The user initiated a load, we don't need to reload anymore.
537 needs_reload_ = false;
538
[email protected]71fde352011-12-29 03:29:56539 NavigationEntryImpl* entry = NavigationEntryImpl::FromNavigationEntry(
540 CreateNavigationEntry(
541 url, referrer, transition, true, extra_headers, browser_context_));
initial.commit09911bf2008-07-26 23:55:29542
543 LoadEntry(entry);
544}
545
[email protected]09b8f82f2009-06-16 20:22:11546void NavigationController::DocumentLoadedInFrame() {
547 last_document_loaded_ = base::TimeTicks::Now();
548}
549
[email protected]e9ba4472008-09-14 15:42:43550bool NavigationController::RendererDidNavigate(
551 const ViewHostMsg_FrameNavigate_Params& params,
[email protected]8286f51a2011-05-31 17:39:13552 content::LoadCommittedDetails* details) {
[email protected]4bf3522c2010-08-19 21:00:20553
[email protected]0e8db942008-09-24 21:21:48554 // Save the previous state before we clobber it.
555 if (GetLastCommittedEntry()) {
[email protected]36fc0392011-12-25 03:59:51556 details->previous_url = GetLastCommittedEntry()->GetURL();
[email protected]a26023822011-12-29 00:23:55557 details->previous_entry_index = GetLastCommittedEntryIndex();
[email protected]0e8db942008-09-24 21:21:48558 } else {
559 details->previous_url = GURL();
560 details->previous_entry_index = -1;
561 }
[email protected]ecd9d8702008-08-28 22:10:17562
[email protected]77362eb2011-08-01 17:18:38563 // If we have a pending entry at this point, it should have a SiteInstance.
564 // Restored entries start out with a null SiteInstance, but we should have
565 // assigned one in NavigateToPendingEntry.
566 DCHECK(pending_entry_index_ == -1 || pending_entry_->site_instance());
[email protected]e9ba4472008-09-14 15:42:43567
[email protected]192d8c5e2010-02-23 07:26:32568 // is_in_page must be computed before the entry gets committed.
569 details->is_in_page = IsURLInPageNavigation(params.url);
570
[email protected]e9ba4472008-09-14 15:42:43571 // Do navigation-type specific actions. These will make and commit an entry.
[email protected]0e8db942008-09-24 21:21:48572 details->type = ClassifyNavigation(params);
[email protected]4bf3522c2010-08-19 21:00:20573
[email protected]0e8db942008-09-24 21:21:48574 switch (details->type) {
[email protected]610cbb82011-10-18 16:35:08575 case content::NAVIGATION_TYPE_NEW_PAGE:
[email protected]befd8d822009-07-01 04:51:47576 RendererDidNavigateToNewPage(params, &(details->did_replace_entry));
[email protected]e9ba4472008-09-14 15:42:43577 break;
[email protected]610cbb82011-10-18 16:35:08578 case content::NAVIGATION_TYPE_EXISTING_PAGE:
[email protected]e9ba4472008-09-14 15:42:43579 RendererDidNavigateToExistingPage(params);
580 break;
[email protected]610cbb82011-10-18 16:35:08581 case content::NAVIGATION_TYPE_SAME_PAGE:
[email protected]e9ba4472008-09-14 15:42:43582 RendererDidNavigateToSamePage(params);
583 break;
[email protected]610cbb82011-10-18 16:35:08584 case content::NAVIGATION_TYPE_IN_PAGE:
[email protected]befd8d822009-07-01 04:51:47585 RendererDidNavigateInPage(params, &(details->did_replace_entry));
[email protected]e9ba4472008-09-14 15:42:43586 break;
[email protected]610cbb82011-10-18 16:35:08587 case content::NAVIGATION_TYPE_NEW_SUBFRAME:
[email protected]e9ba4472008-09-14 15:42:43588 RendererDidNavigateNewSubframe(params);
589 break;
[email protected]610cbb82011-10-18 16:35:08590 case content::NAVIGATION_TYPE_AUTO_SUBFRAME:
[email protected]e9ba4472008-09-14 15:42:43591 if (!RendererDidNavigateAutoSubframe(params))
592 return false;
593 break;
[email protected]610cbb82011-10-18 16:35:08594 case content::NAVIGATION_TYPE_NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:49595 // If a pending navigation was in progress, this canceled it. We should
596 // discard it and make sure it is removed from the URL bar. After that,
597 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:43598 // the caller that nothing has happened.
[email protected]20d1c992011-04-12 21:17:49599 if (pending_entry_) {
600 DiscardNonCommittedEntries();
[email protected]93f230e02011-06-01 14:40:00601 tab_contents_->NotifyNavigationStateChanged(
602 TabContents::INVALIDATE_URL);
[email protected]20d1c992011-04-12 21:17:49603 }
[email protected]e9ba4472008-09-14 15:42:43604 return false;
605 default:
606 NOTREACHED();
[email protected]765b35502008-08-21 00:51:20607 }
608
[email protected]e9ba4472008-09-14 15:42:43609 // All committed entries should have nonempty content state so WebKit doesn't
610 // get confused when we go back to them (see the function for details).
[email protected]0f38dc4552011-02-25 11:24:00611 DCHECK(!params.content_state.empty());
[email protected]10f417c52011-12-28 21:04:23612 NavigationEntryImpl* active_entry =
613 NavigationEntryImpl::FromNavigationEntry(GetActiveEntry());
[email protected]36fc0392011-12-25 03:59:51614 active_entry->SetContentState(params.content_state);
[email protected]765b35502008-08-21 00:51:20615
[email protected]e47ae9472011-10-13 19:48:34616 // Once committed, we do not need to track if the entry was initiated by
617 // the renderer.
618 active_entry->set_is_renderer_initiated(false);
619
[email protected]49bd30e62011-03-22 20:12:59620 // The active entry's SiteInstance should match our SiteInstance.
621 DCHECK(active_entry->site_instance() == tab_contents_->GetSiteInstance());
622
[email protected]e9ba4472008-09-14 15:42:43623 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:00624 details->entry = active_entry;
[email protected]2905f742011-10-13 03:51:58625 details->is_main_frame =
626 content::PageTransitionIsMainFrame(params.transition);
[email protected]f072d2ce2008-09-17 17:16:24627 details->serialized_security_info = params.security_info;
[email protected]2e39d2e2009-02-19 18:41:31628 details->http_status_code = params.http_status_code;
[email protected]93f230e02011-06-01 14:40:00629 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:29630
[email protected]e9ba4472008-09-14 15:42:43631 return true;
initial.commit09911bf2008-07-26 23:55:29632}
633
[email protected]610cbb82011-10-18 16:35:08634content::NavigationType NavigationController::ClassifyNavigation(
[email protected]e9ba4472008-09-14 15:42:43635 const ViewHostMsg_FrameNavigate_Params& params) const {
[email protected]e9ba4472008-09-14 15:42:43636 if (params.page_id == -1) {
[email protected]eef9de32009-09-23 17:19:46637 // The renderer generates the page IDs, and so if it gives us the invalid
638 // page ID (-1) we know it didn't actually navigate. This happens in a few
639 // cases:
640 //
641 // - If a page makes a popup navigated to about blank, and then writes
642 // stuff like a subframe navigated to a real page. We'll get the commit
643 // for the subframe, but there won't be any commit for the outer page.
644 //
645 // - We were also getting these for failed loads (for example, bug 21849).
646 // The guess is that we get a "load commit" for the alternate error page,
647 // but that doesn't affect the page ID, so we get the "old" one, which
648 // could be invalid. This can also happen for a cross-site transition
649 // that causes us to swap processes. Then the error page load will be in
650 // a new process with no page IDs ever assigned (and hence a -1 value),
651 // yet the navigation controller still might have previous pages in its
652 // list.
653 //
654 // In these cases, there's nothing we can do with them, so ignore.
[email protected]610cbb82011-10-18 16:35:08655 return content::NAVIGATION_TYPE_NAV_IGNORE;
[email protected]e9ba4472008-09-14 15:42:43656 }
657
[email protected]9423d9412009-04-14 22:13:55658 if (params.page_id > tab_contents_->GetMaxPageID()) {
[email protected]e9ba4472008-09-14 15:42:43659 // Greater page IDs than we've ever seen before are new pages. We may or may
660 // not have a pending entry for the page, and this may or may not be the
661 // main frame.
[email protected]2905f742011-10-13 03:51:58662 if (content::PageTransitionIsMainFrame(params.transition))
[email protected]610cbb82011-10-18 16:35:08663 return content::NAVIGATION_TYPE_NEW_PAGE;
[email protected]4c27ba82008-09-24 16:49:09664
665 // When this is a new subframe navigation, we should have a committed page
666 // for which it's a suframe in. This may not be the case when an iframe is
667 // navigated on a popup navigated to about:blank (the iframe would be
668 // written into the popup by script on the main page). For these cases,
669 // there isn't any navigation stuff we can do, so just ignore it.
670 if (!GetLastCommittedEntry())
[email protected]610cbb82011-10-18 16:35:08671 return content::NAVIGATION_TYPE_NAV_IGNORE;
[email protected]4c27ba82008-09-24 16:49:09672
673 // Valid subframe navigation.
[email protected]610cbb82011-10-18 16:35:08674 return content::NAVIGATION_TYPE_NEW_SUBFRAME;
[email protected]e9ba4472008-09-14 15:42:43675 }
676
677 // Now we know that the notification is for an existing page. Find that entry.
678 int existing_entry_index = GetEntryIndexWithPageID(
[email protected]9423d9412009-04-14 22:13:55679 tab_contents_->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:43680 params.page_id);
681 if (existing_entry_index == -1) {
682 // The page was not found. It could have been pruned because of the limit on
683 // back/forward entries (not likely since we'll usually tell it to navigate
684 // to such entries). It could also mean that the renderer is smoking crack.
685 NOTREACHED();
[email protected]485e8232011-07-24 18:03:46686
687 // Because the unknown entry has committed, we risk showing the wrong URL in
688 // release builds. Instead, we'll kill the renderer process to be safe.
689 LOG(ERROR) << "terminating renderer for bad navigation: " << params.url;
[email protected]7f6f44c2011-12-14 13:23:38690 content::RecordAction(UserMetricsAction("BadMessageTerminate_NC"));
[email protected]b69934e2011-10-29 02:51:52691
[email protected]7ec6f382011-11-11 01:28:17692 // Temporary code so we can get more information. Format:
[email protected]8c380582011-12-02 03:16:10693 // http://url/foo.html#page1#max3#frame1#ids:2_Nx,1_1x,3_2
694 std::string temp = params.url.spec();
[email protected]7ec6f382011-11-11 01:28:17695 temp.append("#page");
[email protected]b69934e2011-10-29 02:51:52696 temp.append(base::IntToString(params.page_id));
[email protected]7ec6f382011-11-11 01:28:17697 temp.append("#max");
698 temp.append(base::IntToString(tab_contents_->GetMaxPageID()));
699 temp.append("#frame");
700 temp.append(base::IntToString(params.frame_id));
701 temp.append("#ids");
702 for (int i = 0; i < static_cast<int>(entries_.size()); ++i) {
[email protected]8c380582011-12-02 03:16:10703 // Append entry metadata (e.g., 3_7x):
704 // 3: page_id
705 // 7: SiteInstance ID, or N for null
706 // x: appended if not from the current SiteInstance
[email protected]36fc0392011-12-25 03:59:51707 temp.append(base::IntToString(entries_[i]->GetPageID()));
[email protected]8c380582011-12-02 03:16:10708 temp.append("_");
709 if (entries_[i]->site_instance())
710 temp.append(base::IntToString(entries_[i]->site_instance()->id()));
[email protected]7ec6f382011-11-11 01:28:17711 else
[email protected]8c380582011-12-02 03:16:10712 temp.append("N");
713 if (entries_[i]->site_instance() != tab_contents_->GetSiteInstance())
[email protected]7ec6f382011-11-11 01:28:17714 temp.append("x");
715 temp.append(",");
[email protected]b69934e2011-10-29 02:51:52716 }
717 GURL url(temp);
[email protected]151a63d2011-12-20 22:32:52718 tab_contents_->GetRenderViewHost()->Send(new ViewMsg_TempCrashWithData(url));
[email protected]b69934e2011-10-29 02:51:52719 return content::NAVIGATION_TYPE_NAV_IGNORE;
[email protected]e9ba4472008-09-14 15:42:43720 }
[email protected]10f417c52011-12-28 21:04:23721 NavigationEntryImpl* existing_entry = entries_[existing_entry_index].get();
[email protected]e9ba4472008-09-14 15:42:43722
[email protected]2905f742011-10-13 03:51:58723 if (!content::PageTransitionIsMainFrame(params.transition)) {
[email protected]e6035c22010-05-25 16:15:52724 // All manual subframes would get new IDs and were handled above, so we
725 // know this is auto. Since the current page was found in the navigation
726 // entry list, we're guaranteed to have a last committed entry.
727 DCHECK(GetLastCommittedEntry());
[email protected]610cbb82011-10-18 16:35:08728 return content::NAVIGATION_TYPE_AUTO_SUBFRAME;
[email protected]e6035c22010-05-25 16:15:52729 }
730
731 // Anything below here we know is a main frame navigation.
[email protected]e9ba4472008-09-14 15:42:43732 if (pending_entry_ &&
[email protected]e9ba4472008-09-14 15:42:43733 existing_entry != pending_entry_ &&
[email protected]36fc0392011-12-25 03:59:51734 pending_entry_->GetPageID() == -1 &&
[email protected]230c0012011-04-08 16:27:24735 existing_entry == GetLastCommittedEntry()) {
[email protected]e9ba4472008-09-14 15:42:43736 // In this case, we have a pending entry for a URL but WebCore didn't do a
737 // new navigation. This happens when you press enter in the URL bar to
738 // reload. We will create a pending entry, but WebKit will convert it to
739 // a reload since it's the same page and not create a new entry for it
740 // (the user doesn't want to have a new back/forward entry when they do
[email protected]230c0012011-04-08 16:27:24741 // this). If this matches the last committed entry, we want to just ignore
742 // the pending entry and go back to where we were (the "existing entry").
[email protected]610cbb82011-10-18 16:35:08743 return content::NAVIGATION_TYPE_SAME_PAGE;
[email protected]e9ba4472008-09-14 15:42:43744 }
745
[email protected]fc60f222008-12-18 17:36:54746 // Any toplevel navigations with the same base (minus the reference fragment)
747 // are in-page navigations. We weeded out subframe navigations above. Most of
748 // the time this doesn't matter since WebKit doesn't tell us about subframe
749 // navigations that don't actually navigate, but it can happen when there is
750 // an encoding override (it always sends a navigation request).
[email protected]36fc0392011-12-25 03:59:51751 if (AreURLsInPageNavigation(existing_entry->GetURL(), params.url))
[email protected]610cbb82011-10-18 16:35:08752 return content::NAVIGATION_TYPE_IN_PAGE;
[email protected]fc60f222008-12-18 17:36:54753
[email protected]e9ba4472008-09-14 15:42:43754 // Since we weeded out "new" navigations above, we know this is an existing
[email protected]4c27ba82008-09-24 16:49:09755 // (back/forward) navigation.
[email protected]610cbb82011-10-18 16:35:08756 return content::NAVIGATION_TYPE_EXISTING_PAGE;
[email protected]e9ba4472008-09-14 15:42:43757}
758
[email protected]09b8f82f2009-06-16 20:22:11759bool NavigationController::IsRedirect(
760 const ViewHostMsg_FrameNavigate_Params& params) {
761 // For main frame transition, we judge by params.transition.
762 // Otherwise, by params.redirects.
[email protected]2905f742011-10-13 03:51:58763 if (content::PageTransitionIsMainFrame(params.transition)) {
764 return content::PageTransitionIsRedirect(params.transition);
[email protected]09b8f82f2009-06-16 20:22:11765 }
766 return params.redirects.size() > 1;
767}
768
[email protected]e9ba4472008-09-14 15:42:43769void NavigationController::RendererDidNavigateToNewPage(
[email protected]befd8d822009-07-01 04:51:47770 const ViewHostMsg_FrameNavigate_Params& params, bool* did_replace_entry) {
[email protected]10f417c52011-12-28 21:04:23771 NavigationEntryImpl* new_entry;
[email protected]f1eb87a2011-05-06 17:49:41772 bool update_virtual_url;
[email protected]e9ba4472008-09-14 15:42:43773 if (pending_entry_) {
774 // TODO(brettw) this assumes that the pending entry is appropriate for the
775 // new page that was just loaded. I don't think this is necessarily the
[email protected]49bd30e62011-03-22 20:12:59776 // case! We should have some more tracking to know for sure.
[email protected]10f417c52011-12-28 21:04:23777 new_entry = new NavigationEntryImpl(*pending_entry_);
[email protected]e9ba4472008-09-14 15:42:43778
779 // Don't use the page type from the pending entry. Some interstitial page
780 // may have set the type to interstitial. Once we commit, however, the page
781 // type must always be normal.
[email protected]66fdbd52011-10-18 17:08:53782 new_entry->set_page_type(content::PAGE_TYPE_NORMAL);
[email protected]f1eb87a2011-05-06 17:49:41783 update_virtual_url = new_entry->update_virtual_url_with_url();
[email protected]e9ba4472008-09-14 15:42:43784 } else {
[email protected]10f417c52011-12-28 21:04:23785 new_entry = new NavigationEntryImpl;
[email protected]f1eb87a2011-05-06 17:49:41786 // When navigating to a new page, give the browser URL handler a chance to
787 // update the virtual URL based on the new URL. For example, this is needed
788 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
789 // the URL.
790 update_virtual_url = true;
[email protected]e9ba4472008-09-14 15:42:43791 }
792
[email protected]ad23a092011-12-28 07:02:04793 new_entry->SetURL(params.url);
[email protected]f1eb87a2011-05-06 17:49:41794 if (update_virtual_url)
[email protected]38178a42009-12-17 18:58:32795 UpdateVirtualURLToURL(new_entry, params.url);
[email protected]022af742011-12-28 18:37:25796 new_entry->SetReferrer(params.referrer);
[email protected]36fc0392011-12-25 03:59:51797 new_entry->SetPageID(params.page_id);
[email protected]022af742011-12-28 18:37:25798 new_entry->SetTransitionType(params.transition);
[email protected]9423d9412009-04-14 22:13:55799 new_entry->set_site_instance(tab_contents_->GetSiteInstance());
[email protected]36fc0392011-12-25 03:59:51800 new_entry->SetHasPostData(params.is_post);
[email protected]e9ba4472008-09-14 15:42:43801
[email protected]befd8d822009-07-01 04:51:47802 InsertOrReplaceEntry(new_entry, *did_replace_entry);
[email protected]e9ba4472008-09-14 15:42:43803}
804
805void NavigationController::RendererDidNavigateToExistingPage(
806 const ViewHostMsg_FrameNavigate_Params& params) {
807 // We should only get here for main frame navigations.
[email protected]2905f742011-10-13 03:51:58808 DCHECK(content::PageTransitionIsMainFrame(params.transition));
[email protected]e9ba4472008-09-14 15:42:43809
810 // This is a back/forward navigation. The existing page for the ID is
[email protected]4c27ba82008-09-24 16:49:09811 // guaranteed to exist by ClassifyNavigation, and we just need to update it
812 // with new information from the renderer.
[email protected]7f0005a2009-04-15 03:25:11813 int entry_index = GetEntryIndexWithPageID(tab_contents_->GetSiteInstance(),
814 params.page_id);
[email protected]e9ba4472008-09-14 15:42:43815 DCHECK(entry_index >= 0 &&
816 entry_index < static_cast<int>(entries_.size()));
[email protected]10f417c52011-12-28 21:04:23817 NavigationEntryImpl* entry = entries_[entry_index].get();
[email protected]e9ba4472008-09-14 15:42:43818
819 // The URL may have changed due to redirects. The site instance will normally
820 // be the same except during session restore, when no site instance will be
821 // assigned.
[email protected]ad23a092011-12-28 07:02:04822 entry->SetURL(params.url);
[email protected]38178a42009-12-17 18:58:32823 if (entry->update_virtual_url_with_url())
824 UpdateVirtualURLToURL(entry, params.url);
[email protected]e9ba4472008-09-14 15:42:43825 DCHECK(entry->site_instance() == NULL ||
[email protected]9423d9412009-04-14 22:13:55826 entry->site_instance() == tab_contents_->GetSiteInstance());
827 entry->set_site_instance(tab_contents_->GetSiteInstance());
[email protected]e9ba4472008-09-14 15:42:43828
[email protected]36fc0392011-12-25 03:59:51829 entry->SetHasPostData(params.is_post);
[email protected]d5a49e52010-01-08 03:01:41830
[email protected]e9ba4472008-09-14 15:42:43831 // The entry we found in the list might be pending if the user hit
832 // back/forward/reload. This load should commit it (since it's already in the
[email protected]49bd30e62011-03-22 20:12:59833 // list, we can just discard the pending pointer). We should also discard the
834 // pending entry if it corresponds to a different navigation, since that one
835 // is now likely canceled. If it is not canceled, we will treat it as a new
836 // navigation when it arrives, which is also ok.
[email protected]e9ba4472008-09-14 15:42:43837 //
838 // Note that we need to use the "internal" version since we don't want to
839 // actually change any other state, just kill the pointer.
[email protected]49bd30e62011-03-22 20:12:59840 if (pending_entry_)
[email protected]cbab76d2008-10-13 22:42:47841 DiscardNonCommittedEntriesInternal();
[email protected]40bcc302009-03-02 20:50:39842
[email protected]80858db52009-10-15 00:35:18843 // If a transient entry was removed, the indices might have changed, so we
844 // have to query the entry index again.
845 last_committed_entry_index_ =
846 GetEntryIndexWithPageID(tab_contents_->GetSiteInstance(), params.page_id);
[email protected]e9ba4472008-09-14 15:42:43847}
848
849void NavigationController::RendererDidNavigateToSamePage(
850 const ViewHostMsg_FrameNavigate_Params& params) {
851 // This mode implies we have a pending entry that's the same as an existing
[email protected]4c27ba82008-09-24 16:49:09852 // entry for this page ID. This entry is guaranteed to exist by
853 // ClassifyNavigation. All we need to do is update the existing entry.
[email protected]10f417c52011-12-28 21:04:23854 NavigationEntryImpl* existing_entry = GetEntryWithPageID(
855 tab_contents_->GetSiteInstance(), params.page_id);
[email protected]e9ba4472008-09-14 15:42:43856
857 // We assign the entry's unique ID to be that of the new one. Since this is
858 // always the result of a user action, we want to dismiss infobars, etc. like
859 // a regular user-initiated navigation.
[email protected]36fc0392011-12-25 03:59:51860 existing_entry->set_unique_id(pending_entry_->GetUniqueID());
[email protected]e9ba4472008-09-14 15:42:43861
[email protected]a0e69262009-06-03 19:08:48862 // The URL may have changed due to redirects.
[email protected]38178a42009-12-17 18:58:32863 if (existing_entry->update_virtual_url_with_url())
864 UpdateVirtualURLToURL(existing_entry, params.url);
[email protected]ad23a092011-12-28 07:02:04865 existing_entry->SetURL(params.url);
[email protected]a0e69262009-06-03 19:08:48866
[email protected]cbab76d2008-10-13 22:42:47867 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:43868}
869
870void NavigationController::RendererDidNavigateInPage(
[email protected]befd8d822009-07-01 04:51:47871 const ViewHostMsg_FrameNavigate_Params& params, bool* did_replace_entry) {
[email protected]2905f742011-10-13 03:51:58872 DCHECK(content::PageTransitionIsMainFrame(params.transition)) <<
[email protected]e9ba4472008-09-14 15:42:43873 "WebKit should only tell us about in-page navs for the main frame.";
874 // We're guaranteed to have an entry for this one.
[email protected]10f417c52011-12-28 21:04:23875 NavigationEntryImpl* existing_entry = GetEntryWithPageID(
876 tab_contents_->GetSiteInstance(), params.page_id);
[email protected]e9ba4472008-09-14 15:42:43877
878 // Reference fragment navigation. We're guaranteed to have the last_committed
879 // entry and it will be the same page as the new navigation (minus the
[email protected]d466b8a2011-07-15 21:48:03880 // reference fragments, of course). We'll update the URL of the existing
881 // entry without pruning the forward history.
[email protected]ad23a092011-12-28 07:02:04882 existing_entry->SetURL(params.url);
[email protected]d466b8a2011-07-15 21:48:03883 if (existing_entry->update_virtual_url_with_url())
884 UpdateVirtualURLToURL(existing_entry, params.url);
[email protected]ccbe04e2010-03-17 17:58:43885
886 // This replaces the existing entry since the page ID didn't change.
887 *did_replace_entry = true;
[email protected]d466b8a2011-07-15 21:48:03888
889 if (pending_entry_)
890 DiscardNonCommittedEntriesInternal();
891
892 // If a transient entry was removed, the indices might have changed, so we
893 // have to query the entry index again.
894 last_committed_entry_index_ =
895 GetEntryIndexWithPageID(tab_contents_->GetSiteInstance(), params.page_id);
[email protected]e9ba4472008-09-14 15:42:43896}
897
898void NavigationController::RendererDidNavigateNewSubframe(
899 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]2905f742011-10-13 03:51:58900 if (content::PageTransitionStripQualifier(params.transition) ==
901 content::PAGE_TRANSITION_AUTO_SUBFRAME) {
[email protected]09b8f82f2009-06-16 20:22:11902 // This is not user-initiated. Ignore.
903 return;
904 }
[email protected]09b8f82f2009-06-16 20:22:11905
[email protected]e9ba4472008-09-14 15:42:43906 // Manual subframe navigations just get the current entry cloned so the user
907 // can go back or forward to it. The actual subframe information will be
908 // stored in the page state for each of those entries. This happens out of
909 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:09910 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
911 << "that a last committed entry exists.";
[email protected]10f417c52011-12-28 21:04:23912 NavigationEntryImpl* new_entry = new NavigationEntryImpl(
913 *NavigationEntryImpl::FromNavigationEntry(GetLastCommittedEntry()));
[email protected]36fc0392011-12-25 03:59:51914 new_entry->SetPageID(params.page_id);
[email protected]672eba292009-05-13 13:22:45915 InsertOrReplaceEntry(new_entry, false);
[email protected]e9ba4472008-09-14 15:42:43916}
917
918bool NavigationController::RendererDidNavigateAutoSubframe(
919 const ViewHostMsg_FrameNavigate_Params& params) {
920 // We're guaranteed to have a previously committed entry, and we now need to
921 // handle navigation inside of a subframe in it without creating a new entry.
922 DCHECK(GetLastCommittedEntry());
923
924 // Handle the case where we're navigating back/forward to a previous subframe
925 // navigation entry. This is case "2." in NAV_AUTO_SUBFRAME comment in the
926 // header file. In case "1." this will be a NOP.
927 int entry_index = GetEntryIndexWithPageID(
[email protected]9423d9412009-04-14 22:13:55928 tab_contents_->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:43929 params.page_id);
930 if (entry_index < 0 ||
931 entry_index >= static_cast<int>(entries_.size())) {
932 NOTREACHED();
933 return false;
934 }
935
936 // Update the current navigation entry in case we're going back/forward.
937 if (entry_index != last_committed_entry_index_) {
[email protected]e9ba4472008-09-14 15:42:43938 last_committed_entry_index_ = entry_index;
[email protected]e9ba4472008-09-14 15:42:43939 return true;
940 }
941 return false;
942}
943
[email protected]765b35502008-08-21 00:51:20944int NavigationController::GetIndexOfEntry(
[email protected]10f417c52011-12-28 21:04:23945 const NavigationEntryImpl* entry) const {
[email protected]765b35502008-08-21 00:51:20946 const NavigationEntries::const_iterator i(std::find(
947 entries_.begin(),
948 entries_.end(),
949 entry));
950 return (i == entries_.end()) ? -1 : static_cast<int>(i - entries_.begin());
951}
952
[email protected]e9ba4472008-09-14 15:42:43953bool NavigationController::IsURLInPageNavigation(const GURL& url) const {
[email protected]10f417c52011-12-28 21:04:23954 NavigationEntry* last_committed = GetLastCommittedEntry();
[email protected]e9ba4472008-09-14 15:42:43955 if (!last_committed)
956 return false;
[email protected]36fc0392011-12-25 03:59:51957 return AreURLsInPageNavigation(last_committed->GetURL(), url);
[email protected]e9ba4472008-09-14 15:42:43958}
959
[email protected]ce3fa3c2009-04-20 19:55:57960void NavigationController::CopyStateFrom(const NavigationController& source) {
961 // Verify that we look new.
[email protected]a26023822011-12-29 00:23:55962 DCHECK(GetEntryCount() == 0 && !GetPendingEntry());
[email protected]ce3fa3c2009-04-20 19:55:57963
[email protected]a26023822011-12-29 00:23:55964 if (source.GetEntryCount() == 0)
[email protected]ce3fa3c2009-04-20 19:55:57965 return; // Nothing new to do.
966
967 needs_reload_ = true;
[email protected]a26023822011-12-29 00:23:55968 InsertEntriesFrom(source, source.GetEntryCount());
[email protected]ce3fa3c2009-04-20 19:55:57969
[email protected]6ee12c42010-09-14 09:36:07970 session_storage_namespace_ = source.session_storage_namespace_->Clone();
[email protected]4e6419c2010-01-15 04:50:34971
[email protected]5e369672009-11-03 23:48:30972 FinishRestore(source.last_committed_entry_index_, false);
[email protected]ce3fa3c2009-04-20 19:55:57973}
974
[email protected]71fde352011-12-29 03:29:56975void NavigationController::CopyStateFromAndPrune(
976 content::NavigationController* temp) {
977 NavigationController* source = static_cast<NavigationController*>(temp);
[email protected]9e1ad4b2011-08-14 16:49:19978 // The SiteInstance and page_id of the last committed entry needs to be
979 // remembered at this point, in case there is only one committed entry
980 // and it is pruned.
[email protected]10f417c52011-12-28 21:04:23981 NavigationEntryImpl* last_committed =
982 NavigationEntryImpl::FromNavigationEntry(GetLastCommittedEntry());
[email protected]9e1ad4b2011-08-14 16:49:19983 SiteInstance* site_instance =
984 last_committed ? last_committed->site_instance() : NULL;
[email protected]36fc0392011-12-25 03:59:51985 int32 minimum_page_id = last_committed ? last_committed->GetPageID() : -1;
[email protected]9e1ad4b2011-08-14 16:49:19986
[email protected]e1cd5452010-08-26 18:03:25987 // This code is intended for use when the last entry is the active entry.
[email protected]a26023822011-12-29 00:23:55988 DCHECK(
989 (transient_entry_index_ != -1 &&
990 transient_entry_index_ == GetEntryCount() - 1) ||
991 (pending_entry_ && (pending_entry_index_ == -1 ||
992 pending_entry_index_ == GetEntryCount() - 1)) ||
993 (!pending_entry_ && last_committed_entry_index_ == GetEntryCount() - 1));
[email protected]e1cd5452010-08-26 18:03:25994
995 // Remove all the entries leaving the active entry.
996 PruneAllButActive();
997
[email protected]47e020a2010-10-15 14:43:37998 // Insert the entries from source. Don't use source->GetCurrentEntryIndex as
[email protected]e1cd5452010-08-26 18:03:25999 // we don't want to copy over the transient entry.
[email protected]47e020a2010-10-15 14:43:371000 int max_source_index = source->pending_entry_index_ != -1 ?
1001 source->pending_entry_index_ : source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:251002 if (max_source_index == -1)
[email protected]a26023822011-12-29 00:23:551003 max_source_index = source->GetEntryCount();
[email protected]e1cd5452010-08-26 18:03:251004 else
1005 max_source_index++;
[email protected]47e020a2010-10-15 14:43:371006 InsertEntriesFrom(*source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:251007
1008 // Adjust indices such that the last entry and pending are at the end now.
[email protected]a26023822011-12-29 00:23:551009 last_committed_entry_index_ = GetEntryCount() - 1;
[email protected]e1cd5452010-08-26 18:03:251010 if (pending_entry_index_ != -1)
[email protected]a26023822011-12-29 00:23:551011 pending_entry_index_ = GetEntryCount() - 1;
[email protected]e1cd5452010-08-26 18:03:251012 if (transient_entry_index_ != -1) {
1013 // There's a transient entry. In this case we want the last committed to
1014 // point to the previous entry.
[email protected]a26023822011-12-29 00:23:551015 transient_entry_index_ = GetEntryCount() - 1;
[email protected]e1cd5452010-08-26 18:03:251016 if (last_committed_entry_index_ != -1)
1017 last_committed_entry_index_--;
1018 }
[email protected]796931a92011-08-10 01:32:141019
[email protected]9e1ad4b2011-08-14 16:49:191020 tab_contents_->SetHistoryLengthAndPrune(site_instance,
1021 max_source_index,
1022 minimum_page_id);
[email protected]e1cd5452010-08-26 18:03:251023}
1024
[email protected]97b6c4f2010-09-27 19:31:261025void NavigationController::PruneAllButActive() {
[email protected]97b6c4f2010-09-27 19:31:261026 if (transient_entry_index_ != -1) {
1027 // There is a transient entry. Prune up to it.
[email protected]a26023822011-12-29 00:23:551028 DCHECK_EQ(GetEntryCount() - 1, transient_entry_index_);
[email protected]77d8d622010-12-15 10:30:121029 entries_.erase(entries_.begin(), entries_.begin() + transient_entry_index_);
[email protected]97b6c4f2010-09-27 19:31:261030 transient_entry_index_ = 0;
1031 last_committed_entry_index_ = -1;
1032 pending_entry_index_ = -1;
1033 } else if (!pending_entry_) {
1034 // There's no pending entry. Leave the last entry (if there is one).
[email protected]a26023822011-12-29 00:23:551035 if (!GetEntryCount())
[email protected]97b6c4f2010-09-27 19:31:261036 return;
1037
[email protected]77d8d622010-12-15 10:30:121038 DCHECK(last_committed_entry_index_ >= 0);
1039 entries_.erase(entries_.begin(),
1040 entries_.begin() + last_committed_entry_index_);
1041 entries_.erase(entries_.begin() + 1, entries_.end());
[email protected]97b6c4f2010-09-27 19:31:261042 last_committed_entry_index_ = 0;
1043 } else if (pending_entry_index_ != -1) {
[email protected]77d8d622010-12-15 10:30:121044 entries_.erase(entries_.begin(), entries_.begin() + pending_entry_index_);
1045 entries_.erase(entries_.begin() + 1, entries_.end());
[email protected]97b6c4f2010-09-27 19:31:261046 pending_entry_index_ = 0;
1047 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:261048 } else {
1049 // There is a pending_entry, but it's not in entries_.
1050 pending_entry_index_ = -1;
1051 last_committed_entry_index_ = -1;
[email protected]77d8d622010-12-15 10:30:121052 entries_.clear();
[email protected]97b6c4f2010-09-27 19:31:261053 }
1054
[email protected]0bfbf882011-12-22 18:19:271055 if (tab_contents_->GetInterstitialPage()) {
[email protected]97b6c4f2010-09-27 19:31:261056 // Normally the interstitial page hides itself if the user doesn't proceeed.
1057 // This would result in showing a NavigationEntry we just removed. Set this
1058 // so the interstitial triggers a reload if the user doesn't proceed.
[email protected]0bfbf882011-12-22 18:19:271059 tab_contents_->GetInterstitialPage()->set_reload_on_dont_proceed(true);
[email protected]97b6c4f2010-09-27 19:31:261060 }
[email protected]97b6c4f2010-09-27 19:31:261061}
1062
[email protected]71fde352011-12-29 03:29:561063SSLManager* NavigationController::GetSSLManager() {
1064 return &ssl_manager_;
1065}
1066
1067void NavigationController::SetMaxRestoredPageID(int32 max_id) {
1068 max_restored_page_id_ = max_id;
1069}
1070
1071int32 NavigationController::GetMaxRestoredPageID() const {
1072 return max_restored_page_id_;
[email protected]a26023822011-12-29 00:23:551073}
1074
1075SessionStorageNamespace*
1076 NavigationController::GetSessionStorageNamespace() const {
1077 return session_storage_namespace_;
1078}
[email protected]71fde352011-12-29 03:29:561079bool NavigationController::NeedsReload() const {
1080 return needs_reload_;
1081}
[email protected]a26023822011-12-29 00:23:551082
[email protected]43032342011-03-21 14:10:311083void NavigationController::RemoveEntryAtIndexInternal(int index) {
[email protected]a26023822011-12-29 00:23:551084 DCHECK(index < GetEntryCount());
[email protected]6a13a6c2011-12-20 21:47:121085 DCHECK(index != last_committed_entry_index_);
[email protected]43032342011-03-21 14:10:311086
1087 DiscardNonCommittedEntries();
1088
1089 entries_.erase(entries_.begin() + index);
[email protected]6a13a6c2011-12-20 21:47:121090 if (last_committed_entry_index_ > index)
[email protected]43032342011-03-21 14:10:311091 last_committed_entry_index_--;
1092}
1093
[email protected]cbab76d2008-10-13 22:42:471094void NavigationController::DiscardNonCommittedEntries() {
1095 bool transient = transient_entry_index_ != -1;
1096 DiscardNonCommittedEntriesInternal();
initial.commit09911bf2008-07-26 23:55:291097
[email protected]cbab76d2008-10-13 22:42:471098 // If there was a transient entry, invalidate everything so the new active
1099 // entry state is shown.
1100 if (transient) {
[email protected]93f230e02011-06-01 14:40:001101 tab_contents_->NotifyNavigationStateChanged(kInvalidateAll);
[email protected]cbab76d2008-10-13 22:42:471102 }
initial.commit09911bf2008-07-26 23:55:291103}
1104
[email protected]10f417c52011-12-28 21:04:231105NavigationEntry* NavigationController::GetPendingEntry() const {
[email protected]022af742011-12-28 18:37:251106 return pending_entry_;
1107}
1108
[email protected]a26023822011-12-29 00:23:551109int NavigationController::GetPendingEntryIndex() const {
1110 return pending_entry_index_;
1111}
1112
[email protected]10f417c52011-12-28 21:04:231113void NavigationController::InsertOrReplaceEntry(NavigationEntryImpl* entry,
[email protected]672eba292009-05-13 13:22:451114 bool replace) {
[email protected]36fc0392011-12-25 03:59:511115 DCHECK(entry->GetTransitionType() != content::PAGE_TRANSITION_AUTO_SUBFRAME);
[email protected]765b35502008-08-21 00:51:201116
1117 // Copy the pending entry's unique ID to the committed entry.
1118 // I don't know if pending_entry_index_ can be other than -1 here.
[email protected]10f417c52011-12-28 21:04:231119 const NavigationEntryImpl* const pending_entry =
1120 (pending_entry_index_ == -1) ?
1121 pending_entry_ : entries_[pending_entry_index_].get();
[email protected]765b35502008-08-21 00:51:201122 if (pending_entry)
[email protected]36fc0392011-12-25 03:59:511123 entry->set_unique_id(pending_entry->GetUniqueID());
[email protected]765b35502008-08-21 00:51:201124
[email protected]cbab76d2008-10-13 22:42:471125 DiscardNonCommittedEntriesInternal();
[email protected]765b35502008-08-21 00:51:201126
1127 int current_size = static_cast<int>(entries_.size());
1128
[email protected]765b35502008-08-21 00:51:201129 if (current_size > 0) {
[email protected]672eba292009-05-13 13:22:451130 // Prune any entries which are in front of the current entry.
1131 // Also prune the current entry if we are to replace the current entry.
[email protected]47dcbdc2011-05-27 15:08:311132 // last_committed_entry_index_ must be updated here since calls to
1133 // NotifyPrunedEntries() below may re-enter and we must make sure
1134 // last_committed_entry_index_ is not left in an invalid state.
1135 if (replace)
1136 --last_committed_entry_index_;
1137
[email protected]c12bf1a12008-09-17 16:28:491138 int num_pruned = 0;
[email protected]47dcbdc2011-05-27 15:08:311139 while (last_committed_entry_index_ < (current_size - 1)) {
[email protected]c12bf1a12008-09-17 16:28:491140 num_pruned++;
[email protected]765b35502008-08-21 00:51:201141 entries_.pop_back();
1142 current_size--;
1143 }
[email protected]c12bf1a12008-09-17 16:28:491144 if (num_pruned > 0) // Only notify if we did prune something.
1145 NotifyPrunedEntries(this, false, num_pruned);
[email protected]765b35502008-08-21 00:51:201146 }
1147
[email protected]9b51970d2011-12-09 23:10:231148 if (entries_.size() >= max_entry_count()) {
[email protected]6a13a6c2011-12-20 21:47:121149 DCHECK(last_committed_entry_index_ > 0);
1150 RemoveEntryAtIndex(0);
[email protected]c12bf1a12008-09-17 16:28:491151 NotifyPrunedEntries(this, true, 1);
1152 }
[email protected]765b35502008-08-21 00:51:201153
[email protected]10f417c52011-12-28 21:04:231154 entries_.push_back(linked_ptr<NavigationEntryImpl>(entry));
[email protected]765b35502008-08-21 00:51:201155 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
[email protected]e9ba4472008-09-14 15:42:431156
1157 // This is a new page ID, so we need everybody to know about it.
[email protected]36fc0392011-12-25 03:59:511158 tab_contents_->UpdateMaxPageID(entry->GetPageID());
initial.commit09911bf2008-07-26 23:55:291159}
1160
[email protected]1ccb3568d2010-02-19 10:51:161161void NavigationController::NavigateToPendingEntry(ReloadType reload_type) {
[email protected]72097fd02010-01-21 23:36:011162 needs_reload_ = false;
1163
[email protected]83c2e232011-10-07 21:36:461164 // If we were navigating to a slow-to-commit page, and the user performs
1165 // a session history navigation to the last committed page, RenderViewHost
1166 // will force the throbber to start, but WebKit will essentially ignore the
1167 // navigation, and won't send a message to stop the throbber. To prevent this
1168 // from happening, we drop the navigation here and stop the slow-to-commit
1169 // page from loading (which would normally happen during the navigation).
1170 if (pending_entry_index_ != -1 &&
1171 pending_entry_index_ == last_committed_entry_index_ &&
1172 (entries_[pending_entry_index_]->restore_type() ==
[email protected]10f417c52011-12-28 21:04:231173 NavigationEntryImpl::RESTORE_NONE) &&
[email protected]36fc0392011-12-25 03:59:511174 (entries_[pending_entry_index_]->GetTransitionType() &
[email protected]2905f742011-10-13 03:51:581175 content::PAGE_TRANSITION_FORWARD_BACK)) {
[email protected]83c2e232011-10-07 21:36:461176 tab_contents_->Stop();
[email protected]6a13a6c2011-12-20 21:47:121177
1178 // If an interstitial page is showing, we want to close it to get back
1179 // to what was showing before.
[email protected]0bfbf882011-12-22 18:19:271180 if (tab_contents_->GetInterstitialPage())
1181 tab_contents_->GetInterstitialPage()->DontProceed();
[email protected]6a13a6c2011-12-20 21:47:121182
[email protected]83c2e232011-10-07 21:36:461183 DiscardNonCommittedEntries();
1184 return;
1185 }
1186
[email protected]6a13a6c2011-12-20 21:47:121187 // If an interstitial page is showing, the previous renderer is blocked and
1188 // cannot make new requests. Unblock (and disable) it to allow this
1189 // navigation to succeed. The interstitial will stay visible until the
1190 // resulting DidNavigate.
[email protected]0bfbf882011-12-22 18:19:271191 if (tab_contents_->GetInterstitialPage())
1192 tab_contents_->GetInterstitialPage()->CancelForNavigation();
[email protected]6a13a6c2011-12-20 21:47:121193
initial.commit09911bf2008-07-26 23:55:291194 // For session history navigations only the pending_entry_index_ is set.
1195 if (!pending_entry_) {
[email protected]4bf3522c2010-08-19 21:00:201196 DCHECK_NE(pending_entry_index_, -1);
[email protected]765b35502008-08-21 00:51:201197 pending_entry_ = entries_[pending_entry_index_].get();
initial.commit09911bf2008-07-26 23:55:291198 }
1199
[email protected]1ccb3568d2010-02-19 10:51:161200 if (!tab_contents_->NavigateToPendingEntry(reload_type))
[email protected]cbab76d2008-10-13 22:42:471201 DiscardNonCommittedEntries();
[email protected]77362eb2011-08-01 17:18:381202
1203 // If the entry is being restored and doesn't have a SiteInstance yet, fill
1204 // it in now that we know. This allows us to find the entry when it commits.
1205 // This works for browser-initiated navigations. We handle renderer-initiated
1206 // navigations to restored entries in TabContents::OnGoToEntryAtOffset.
1207 if (pending_entry_ && !pending_entry_->site_instance() &&
[email protected]10f417c52011-12-28 21:04:231208 pending_entry_->restore_type() != NavigationEntryImpl::RESTORE_NONE) {
[email protected]77362eb2011-08-01 17:18:381209 pending_entry_->set_site_instance(tab_contents_->GetPendingSiteInstance());
[email protected]10f417c52011-12-28 21:04:231210 pending_entry_->set_restore_type(NavigationEntryImpl::RESTORE_NONE);
[email protected]77362eb2011-08-01 17:18:381211 }
initial.commit09911bf2008-07-26 23:55:291212}
1213
[email protected]ecd9d8702008-08-28 22:10:171214void NavigationController::NotifyNavigationEntryCommitted(
[email protected]93f230e02011-06-01 14:40:001215 content::LoadCommittedDetails* details) {
[email protected]df1af242009-05-01 00:11:401216 details->entry = GetActiveEntry();
[email protected]6c2381d2011-10-19 02:52:531217 content::NotificationDetails notification_details =
1218 content::Details<content::LoadCommittedDetails>(details);
[email protected]df1af242009-05-01 00:11:401219
1220 // We need to notify the ssl_manager_ before the tab_contents_ so the
1221 // location bar will have up-to-date information about the security style
1222 // when it wants to draw. See http://crbug.com/11157
1223 ssl_manager_.DidCommitProvisionalLoad(notification_details);
1224
initial.commit09911bf2008-07-26 23:55:291225 // TODO(pkasting): http://b/1113079 Probably these explicit notification paths
1226 // should be removed, and interested parties should just listen for the
1227 // notification below instead.
[email protected]93f230e02011-06-01 14:40:001228 tab_contents_->NotifyNavigationStateChanged(kInvalidateAll);
initial.commit09911bf2008-07-26 23:55:291229
[email protected]ad50def52011-10-19 23:17:071230 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271231 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
[email protected]cca0f1e2012-01-03 18:27:461232 content::Source<content::NavigationController>(this),
[email protected]df1af242009-05-01 00:11:401233 notification_details);
initial.commit09911bf2008-07-26 23:55:291234}
1235
initial.commit09911bf2008-07-26 23:55:291236// static
1237void NavigationController::DisablePromptOnRepost() {
1238 check_for_repost_ = false;
1239}
1240
[email protected]9b51970d2011-12-09 23:10:231241// static
1242size_t NavigationController::max_entry_count() {
1243 if (max_entry_count_for_testing_ != kMaxEntryCountForTestingNotSet)
1244 return max_entry_count_for_testing_;
1245 return content::kMaxSessionHistoryEntries;
1246}
1247
initial.commit09911bf2008-07-26 23:55:291248void NavigationController::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:221249 if (is_active && needs_reload_)
1250 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:291251}
1252
1253void NavigationController::LoadIfNecessary() {
1254 if (!needs_reload_)
1255 return;
1256
initial.commit09911bf2008-07-26 23:55:291257 // Calling Reload() results in ignoring state, and not loading.
1258 // Explicitly use NavigateToPendingEntry so that the renderer uses the
1259 // cached state.
1260 pending_entry_index_ = last_committed_entry_index_;
[email protected]1ccb3568d2010-02-19 10:51:161261 NavigateToPendingEntry(NO_RELOAD);
initial.commit09911bf2008-07-26 23:55:291262}
1263
[email protected]10f417c52011-12-28 21:04:231264void NavigationController::NotifyEntryChanged(const NavigationEntry* entry,
1265 int index) {
[email protected]8286f51a2011-05-31 17:39:131266 content::EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:091267 det.changed_entry = entry;
1268 det.index = index;
[email protected]ad50def52011-10-19 23:17:071269 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271270 content::NOTIFICATION_NAV_ENTRY_CHANGED,
[email protected]cca0f1e2012-01-03 18:27:461271 content::Source<content::NavigationController>(this),
[email protected]6c2381d2011-10-19 02:52:531272 content::Details<content::EntryChangedDetails>(&det));
initial.commit09911bf2008-07-26 23:55:291273}
1274
[email protected]5e369672009-11-03 23:48:301275void NavigationController::FinishRestore(int selected_index,
1276 bool from_last_session) {
[email protected]a26023822011-12-29 00:23:551277 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
[email protected]5e369672009-11-03 23:48:301278 ConfigureEntriesForRestore(&entries_, from_last_session);
initial.commit09911bf2008-07-26 23:55:291279
[email protected]71fde352011-12-29 03:29:561280 SetMaxRestoredPageID(static_cast<int32>(GetEntryCount()));
initial.commit09911bf2008-07-26 23:55:291281
1282 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:291283}
[email protected]765b35502008-08-21 00:51:201284
[email protected]cbab76d2008-10-13 22:42:471285void NavigationController::DiscardNonCommittedEntriesInternal() {
[email protected]765b35502008-08-21 00:51:201286 if (pending_entry_index_ == -1)
1287 delete pending_entry_;
1288 pending_entry_ = NULL;
1289 pending_entry_index_ = -1;
[email protected]cbab76d2008-10-13 22:42:471290
1291 DiscardTransientEntry();
1292}
1293
1294void NavigationController::DiscardTransientEntry() {
1295 if (transient_entry_index_ == -1)
1296 return;
[email protected]a0e69262009-06-03 19:08:481297 entries_.erase(entries_.begin() + transient_entry_index_);
[email protected]80858db52009-10-15 00:35:181298 if (last_committed_entry_index_ > transient_entry_index_)
1299 last_committed_entry_index_--;
[email protected]cbab76d2008-10-13 22:42:471300 transient_entry_index_ = -1;
[email protected]765b35502008-08-21 00:51:201301}
1302
1303int NavigationController::GetEntryIndexWithPageID(
[email protected]7f0005a2009-04-15 03:25:111304 SiteInstance* instance, int32 page_id) const {
[email protected]765b35502008-08-21 00:51:201305 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
[email protected]7f0005a2009-04-15 03:25:111306 if ((entries_[i]->site_instance() == instance) &&
[email protected]36fc0392011-12-25 03:59:511307 (entries_[i]->GetPageID() == page_id))
[email protected]765b35502008-08-21 00:51:201308 return i;
1309 }
1310 return -1;
1311}
[email protected]cbab76d2008-10-13 22:42:471312
[email protected]10f417c52011-12-28 21:04:231313NavigationEntry* NavigationController::GetTransientEntry() const {
[email protected]cbab76d2008-10-13 22:42:471314 if (transient_entry_index_ == -1)
1315 return NULL;
1316 return entries_[transient_entry_index_].get();
1317}
[email protected]e1cd5452010-08-26 18:03:251318
[email protected]e1cd5452010-08-26 18:03:251319void NavigationController::InsertEntriesFrom(
1320 const NavigationController& source,
1321 int max_index) {
[email protected]a26023822011-12-29 00:23:551322 DCHECK_LE(max_index, source.GetEntryCount());
[email protected]e1cd5452010-08-26 18:03:251323 size_t insert_index = 0;
1324 for (int i = 0; i < max_index; i++) {
1325 // When cloning a tab, copy all entries except interstitial pages
[email protected]ad23a092011-12-28 07:02:041326 if (source.entries_[i].get()->GetPageType() !=
[email protected]66fdbd52011-10-18 17:08:531327 content::PAGE_TYPE_INTERSTITIAL) {
[email protected]e1cd5452010-08-26 18:03:251328 entries_.insert(entries_.begin() + insert_index++,
[email protected]10f417c52011-12-28 21:04:231329 linked_ptr<NavigationEntryImpl>(
1330 new NavigationEntryImpl(*source.entries_[i])));
[email protected]e1cd5452010-08-26 18:03:251331 }
1332 }
1333}