blob: 5d76b053e8699fbd0564e5d971a67ed7883d4fae [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"
9#include "base/string_util.h"
[email protected]b689fce72009-03-17 22:45:3410#include "base/time.h"
[email protected]252cad62010-08-18 18:33:5711#include "base/utf_string_conversions.h"
[email protected]cd3d7892009-03-04 23:55:0612#include "chrome/browser/browser_about_handler.h"
[email protected]9423d9412009-04-14 22:13:5513#include "chrome/browser/browser_url_handler.h"
[email protected]37858e52010-08-26 00:22:0214#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3315#include "chrome/browser/profiles/profile.h"
[email protected]169627b2008-12-06 19:30:1916#include "chrome/browser/sessions/session_types.h"
[email protected]3cc72b12010-03-18 23:03:0017#include "chrome/common/chrome_constants.h"
[email protected]a23de8572009-06-03 02:16:3218#include "chrome/common/pref_names.h"
[email protected]6de74452009-02-25 18:04:5919#include "chrome/common/url_constants.h"
[email protected]567812d2011-02-24 17:40:5020#include "content/browser/in_process_webkit/session_storage_namespace.h"
21#include "content/browser/site_instance.h"
[email protected]0dd3a0ab2011-02-18 08:17:4422#include "content/browser/tab_contents/interstitial_page.h"
23#include "content/browser/tab_contents/navigation_entry.h"
24#include "content/browser/tab_contents/tab_contents.h"
25#include "content/browser/tab_contents/tab_contents_delegate.h"
[email protected]4dd57932011-03-17 06:06:1226#include "content/common/navigation_types.h"
[email protected]7f070d42011-03-09 20:25:3227#include "content/common/notification_service.h"
[email protected]0aed2f52011-03-23 18:06:3628#include "content/common/view_messages.h"
[email protected]074f10562009-05-21 22:40:0529#include "grit/app_resources.h"
[email protected]a23de8572009-06-03 02:16:3230#include "net/base/escape.h"
[email protected]31682282010-01-15 18:05:1631#include "net/base/mime_util.h"
[email protected]7f070d42011-03-09 20:25:3232#include "net/base/net_util.h"
[email protected]765b35502008-08-21 00:51:2033#include "webkit/glue/webkit_glue.h"
initial.commit09911bf2008-07-26 23:55:2934
[email protected]e9ba4472008-09-14 15:42:4335namespace {
36
[email protected]8030f012009-09-25 18:09:3737const int kInvalidateAllButShelves =
[email protected]6d7a6042010-08-12 20:12:4238 0xFFFFFFFF & ~TabContents::INVALIDATE_BOOKMARK_BAR;
[email protected]8030f012009-09-25 18:09:3739
[email protected]e9ba4472008-09-14 15:42:4340// Invoked when entries have been pruned, or removed. For example, if the
41// current entries are [google, digg, yahoo], with the current entry google,
42// and the user types in cnet, then digg and yahoo are pruned.
[email protected]c12bf1a12008-09-17 16:28:4943void NotifyPrunedEntries(NavigationController* nav_controller,
44 bool from_front,
45 int count) {
46 NavigationController::PrunedDetails details;
47 details.from_front = from_front;
48 details.count = count;
[email protected]e9ba4472008-09-14 15:42:4349 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:5650 NotificationType::NAV_LIST_PRUNED,
[email protected]e9ba4472008-09-14 15:42:4351 Source<NavigationController>(nav_controller),
[email protected]c12bf1a12008-09-17 16:28:4952 Details<NavigationController::PrunedDetails>(&details));
[email protected]e9ba4472008-09-14 15:42:4353}
54
55// Ensure the given NavigationEntry has a valid state, so that WebKit does not
56// get confused if we navigate back to it.
[email protected]40bcc302009-03-02 20:50:3957//
[email protected]e9ba4472008-09-14 15:42:4358// An empty state is treated as a new navigation by WebKit, which would mean
59// losing the navigation entries and generating a new navigation entry after
60// this one. We don't want that. To avoid this we create a valid state which
61// WebKit will not treat as a new navigation.
62void SetContentStateIfEmpty(NavigationEntry* entry) {
[email protected]965524b2009-04-04 21:32:4063 if (entry->content_state().empty()) {
[email protected]e9ba4472008-09-14 15:42:4364 entry->set_content_state(
65 webkit_glue::CreateHistoryStateForURL(entry->url()));
66 }
67}
68
69// Configure all the NavigationEntries in entries for restore. This resets
70// the transition type to reload and makes sure the content state isn't empty.
71void ConfigureEntriesForRestore(
[email protected]5e369672009-11-03 23:48:3072 std::vector<linked_ptr<NavigationEntry> >* entries,
73 bool from_last_session) {
[email protected]e9ba4472008-09-14 15:42:4374 for (size_t i = 0; i < entries->size(); ++i) {
75 // Use a transition type of reload so that we don't incorrectly increase
76 // the typed count.
77 (*entries)[i]->set_transition_type(PageTransition::RELOAD);
[email protected]5e369672009-11-03 23:48:3078 (*entries)[i]->set_restore_type(from_last_session ?
79 NavigationEntry::RESTORE_LAST_SESSION :
80 NavigationEntry::RESTORE_CURRENT_SESSION);
[email protected]e9ba4472008-09-14 15:42:4381 // NOTE(darin): This code is only needed for backwards compat.
82 SetContentStateIfEmpty((*entries)[i].get());
83 }
84}
85
86// See NavigationController::IsURLInPageNavigation for how this works and why.
87bool AreURLsInPageNavigation(const GURL& existing_url, const GURL& new_url) {
[email protected]192d8c5e2010-02-23 07:26:3288 if (existing_url == new_url || !new_url.has_ref()) {
89 // TODO(jcampan): what about when navigating back from a ref URL to the top
90 // non ref URL? Nothing is loaded in that case but we return false here.
91 // The user could also navigate from the ref URL to the non ref URL by
92 // entering the non ref URL in the location bar or through a bookmark, in
93 // which case there would be a load. I am not sure if the non-load/load
94 // scenarios can be differentiated with the TransitionType.
[email protected]e9ba4472008-09-14 15:42:4395 return false;
[email protected]192d8c5e2010-02-23 07:26:3296 }
[email protected]e9ba4472008-09-14 15:42:4397
98 url_canon::Replacements<char> replacements;
99 replacements.ClearRef();
100 return existing_url.ReplaceComponents(replacements) ==
101 new_url.ReplaceComponents(replacements);
102}
103
104} // namespace
105
initial.commit09911bf2008-07-26 23:55:29106// NavigationController ---------------------------------------------------
107
[email protected]765b35502008-08-21 00:51:20108// static
[email protected]3cc72b12010-03-18 23:03:00109size_t NavigationController::max_entry_count_ =
110 chrome::kMaxSessionHistoryEntries;
[email protected]765b35502008-08-21 00:51:20111
initial.commit09911bf2008-07-26 23:55:29112// static
113bool NavigationController::check_for_repost_ = true;
114
[email protected]6ee12c42010-09-14 09:36:07115NavigationController::NavigationController(
116 TabContents* contents,
117 Profile* profile,
118 SessionStorageNamespace* session_storage_namespace)
initial.commit09911bf2008-07-26 23:55:29119 : profile_(profile),
[email protected]765b35502008-08-21 00:51:20120 pending_entry_(NULL),
121 last_committed_entry_index_(-1),
122 pending_entry_index_(-1),
[email protected]cbab76d2008-10-13 22:42:47123 transient_entry_index_(-1),
[email protected]9423d9412009-04-14 22:13:55124 tab_contents_(contents),
initial.commit09911bf2008-07-26 23:55:29125 max_restored_page_id_(-1),
[email protected]5d063842009-05-15 04:08:24126 ALLOW_THIS_IN_INITIALIZER_LIST(ssl_manager_(this)),
[email protected]38b8f4e2009-09-24 19:44:57127 needs_reload_(false),
[email protected]6ee12c42010-09-14 09:36:07128 session_storage_namespace_(session_storage_namespace),
[email protected]106a0812010-03-18 00:15:12129 pending_reload_(NO_RELOAD) {
initial.commit09911bf2008-07-26 23:55:29130 DCHECK(profile_);
[email protected]6ee12c42010-09-14 09:36:07131 if (!session_storage_namespace_)
132 session_storage_namespace_ = new SessionStorageNamespace(profile_);
initial.commit09911bf2008-07-26 23:55:29133}
134
initial.commit09911bf2008-07-26 23:55:29135NavigationController::~NavigationController() {
[email protected]cbab76d2008-10-13 22:42:47136 DiscardNonCommittedEntriesInternal();
[email protected]c0993872008-08-21 19:59:44137
[email protected]bfd04a62009-02-01 18:16:56138 NotificationService::current()->Notify(
139 NotificationType::TAB_CLOSED,
140 Source<NavigationController>(this),
141 NotificationService::NoDetails());
initial.commit09911bf2008-07-26 23:55:29142}
143
[email protected]ce3fa3c2009-04-20 19:55:57144void NavigationController::RestoreFromState(
145 const std::vector<TabNavigation>& navigations,
[email protected]5e369672009-11-03 23:48:30146 int selected_navigation,
147 bool from_last_session) {
[email protected]ce3fa3c2009-04-20 19:55:57148 // Verify that this controller is unused and that the input is valid.
149 DCHECK(entry_count() == 0 && !pending_entry());
150 DCHECK(selected_navigation >= 0 &&
151 selected_navigation < static_cast<int>(navigations.size()));
152
153 // Populate entries_ from the supplied TabNavigations.
154 needs_reload_ = true;
155 CreateNavigationEntriesFromTabNavigations(navigations, &entries_);
156
157 // And finish the restore.
[email protected]5e369672009-11-03 23:48:30158 FinishRestore(selected_navigation, from_last_session);
[email protected]ce3fa3c2009-04-20 19:55:57159}
160
[email protected]f1c74112008-10-30 16:17:04161void NavigationController::Reload(bool check_for_repost) {
[email protected]1ccb3568d2010-02-19 10:51:16162 ReloadInternal(check_for_repost, RELOAD);
163}
164void NavigationController::ReloadIgnoringCache(bool check_for_repost) {
165 ReloadInternal(check_for_repost, RELOAD_IGNORING_CACHE);
166}
167
168void NavigationController::ReloadInternal(bool check_for_repost,
169 ReloadType reload_type) {
[email protected]cbab76d2008-10-13 22:42:47170 // Reloading a transient entry does nothing.
171 if (transient_entry_index_ != -1)
172 return;
173
174 DiscardNonCommittedEntriesInternal();
initial.commit09911bf2008-07-26 23:55:29175 int current_index = GetCurrentEntryIndex();
[email protected]106a0812010-03-18 00:15:12176 // If we are no where, then we can't reload. TODO(darin): We should add a
177 // CanReload method.
178 if (current_index == -1) {
179 return;
180 }
181
[email protected]106a0812010-03-18 00:15:12182 if (check_for_repost_ && check_for_repost &&
[email protected]a3a1d142008-12-19 00:42:30183 GetEntryAtIndex(current_index)->has_post_data()) {
184 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07185 // they really want to do this. If they do, the dialog will call us back
186 // with check_for_repost = false.
[email protected]965bb092010-04-09 11:59:02187 NotificationService::current()->Notify(
188 NotificationType::REPOST_WARNING_SHOWN,
189 Source<NavigationController>(this),
190 NotificationService::NoDetails());
191
[email protected]106a0812010-03-18 00:15:12192 pending_reload_ = reload_type;
[email protected]9423d9412009-04-14 22:13:55193 tab_contents_->Activate();
[email protected]14f3408a2009-08-31 20:53:53194 tab_contents_->delegate()->ShowRepostFormWarningDialog(tab_contents_);
initial.commit09911bf2008-07-26 23:55:29195 } else {
[email protected]cbab76d2008-10-13 22:42:47196 DiscardNonCommittedEntriesInternal();
[email protected]765b35502008-08-21 00:51:20197
198 pending_entry_index_ = current_index;
[email protected]1e5645ff2008-08-27 18:09:07199 entries_[pending_entry_index_]->set_transition_type(PageTransition::RELOAD);
[email protected]1ccb3568d2010-02-19 10:51:16200 NavigateToPendingEntry(reload_type);
initial.commit09911bf2008-07-26 23:55:29201 }
202}
203
[email protected]106a0812010-03-18 00:15:12204void NavigationController::CancelPendingReload() {
205 DCHECK(pending_reload_ != NO_RELOAD);
206 pending_reload_ = NO_RELOAD;
207}
208
209void NavigationController::ContinuePendingReload() {
210 if (pending_reload_ == NO_RELOAD) {
211 NOTREACHED();
212 } else {
213 ReloadInternal(false, pending_reload_);
[email protected]965bb092010-04-09 11:59:02214 pending_reload_ = NO_RELOAD;
[email protected]106a0812010-03-18 00:15:12215 }
216}
217
[email protected]c70f9b82010-04-21 07:31:11218bool NavigationController::IsInitialNavigation() {
219 return last_document_loaded_.is_null();
220}
221
[email protected]b6ea7412010-05-04 23:26:47222// static
223NavigationEntry* NavigationController::CreateNavigationEntry(
224 const GURL& url, const GURL& referrer, PageTransition::Type transition,
225 Profile* profile) {
226 // Allow the browser URL handler to rewrite the URL. This will, for example,
227 // remove "view-source:" from the beginning of the URL to get the URL that
228 // will actually be loaded. This real URL won't be shown to the user, just
229 // used internally.
230 GURL loaded_url(url);
231 bool reverse_on_redirect = false;
232 BrowserURLHandler::RewriteURLIfNecessary(
233 &loaded_url, profile, &reverse_on_redirect);
234
235 NavigationEntry* entry = new NavigationEntry(
236 NULL, // The site instance for tabs is sent on navigation
237 // (TabContents::GetSiteInstance).
238 -1,
239 loaded_url,
240 referrer,
241 string16(),
242 transition);
243 entry->set_virtual_url(url);
244 entry->set_user_typed_url(url);
245 entry->set_update_virtual_url_with_url(reverse_on_redirect);
[email protected]b6ea7412010-05-04 23:26:47246 return entry;
247}
248
[email protected]765b35502008-08-21 00:51:20249NavigationEntry* NavigationController::GetEntryWithPageID(
[email protected]7f0005a2009-04-15 03:25:11250 SiteInstance* instance, int32 page_id) const {
251 int index = GetEntryIndexWithPageID(instance, page_id);
[email protected]765b35502008-08-21 00:51:20252 return (index != -1) ? entries_[index].get() : NULL;
253}
254
255void NavigationController::LoadEntry(NavigationEntry* entry) {
[email protected]cd3d7892009-03-04 23:55:06256 // Handle non-navigational URLs that popup dialogs and such, these should not
257 // actually navigate.
258 if (HandleNonNavigationAboutURL(entry->url()))
259 return;
260
[email protected]765b35502008-08-21 00:51:20261 // When navigating to a new page, we don't know for sure if we will actually
262 // end up leaving the current page. The new page load could for example
263 // result in a download or a 'no content' response (e.g., a mailto: URL).
[email protected]cbab76d2008-10-13 22:42:47264 DiscardNonCommittedEntriesInternal();
[email protected]765b35502008-08-21 00:51:20265 pending_entry_ = entry;
266 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56267 NotificationType::NAV_ENTRY_PENDING,
[email protected]765b35502008-08-21 00:51:20268 Source<NavigationController>(this),
269 NotificationService::NoDetails());
[email protected]1ccb3568d2010-02-19 10:51:16270 NavigateToPendingEntry(NO_RELOAD);
[email protected]765b35502008-08-21 00:51:20271}
272
[email protected]765b35502008-08-21 00:51:20273NavigationEntry* NavigationController::GetActiveEntry() const {
[email protected]cbab76d2008-10-13 22:42:47274 if (transient_entry_index_ != -1)
275 return entries_[transient_entry_index_].get();
276 if (pending_entry_)
277 return pending_entry_;
278 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20279}
280
281int NavigationController::GetCurrentEntryIndex() const {
[email protected]cbab76d2008-10-13 22:42:47282 if (transient_entry_index_ != -1)
283 return transient_entry_index_;
[email protected]765b35502008-08-21 00:51:20284 if (pending_entry_index_ != -1)
285 return pending_entry_index_;
286 return last_committed_entry_index_;
287}
288
289NavigationEntry* NavigationController::GetLastCommittedEntry() const {
290 if (last_committed_entry_index_ == -1)
291 return NULL;
292 return entries_[last_committed_entry_index_].get();
293}
294
[email protected]31682282010-01-15 18:05:16295bool NavigationController::CanViewSource() const {
296 bool is_supported_mime_type = net::IsSupportedNonImageMimeType(
297 tab_contents_->contents_mime_type().c_str());
298 NavigationEntry* active_entry = GetActiveEntry();
299 return active_entry && !active_entry->IsViewSourceMode() &&
300 is_supported_mime_type;
301}
302
[email protected]765b35502008-08-21 00:51:20303NavigationEntry* NavigationController::GetEntryAtOffset(int offset) const {
[email protected]cbab76d2008-10-13 22:42:47304 int index = (transient_entry_index_ != -1) ?
305 transient_entry_index_ + offset :
306 last_committed_entry_index_ + offset;
[email protected]7f0005a2009-04-15 03:25:11307 if (index < 0 || index >= entry_count())
[email protected]765b35502008-08-21 00:51:20308 return NULL;
309
310 return entries_[index].get();
311}
312
[email protected]765b35502008-08-21 00:51:20313bool NavigationController::CanGoBack() const {
314 return entries_.size() > 1 && GetCurrentEntryIndex() > 0;
315}
316
317bool NavigationController::CanGoForward() const {
318 int index = GetCurrentEntryIndex();
319 return index >= 0 && index < (static_cast<int>(entries_.size()) - 1);
320}
321
322void NavigationController::GoBack() {
323 if (!CanGoBack()) {
324 NOTREACHED();
325 return;
326 }
327
[email protected]25396da2010-03-11 19:19:10328 // If an interstitial page is showing, going back is equivalent to hiding the
329 // interstitial.
330 if (tab_contents_->interstitial_page()) {
331 tab_contents_->interstitial_page()->DontProceed();
332 return;
333 }
334
[email protected]765b35502008-08-21 00:51:20335 // Base the navigation on where we are now...
336 int current_index = GetCurrentEntryIndex();
337
[email protected]cbab76d2008-10-13 22:42:47338 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20339
340 pending_entry_index_ = current_index - 1;
[email protected]784688a62010-09-13 07:06:52341 entries_[pending_entry_index_]->set_transition_type(
342 entries_[pending_entry_index_]->transition_type() |
343 PageTransition::FORWARD_BACK);
[email protected]1ccb3568d2010-02-19 10:51:16344 NavigateToPendingEntry(NO_RELOAD);
[email protected]765b35502008-08-21 00:51:20345}
346
347void NavigationController::GoForward() {
348 if (!CanGoForward()) {
349 NOTREACHED();
350 return;
351 }
352
[email protected]25396da2010-03-11 19:19:10353 // If an interstitial page is showing, the previous renderer is blocked and
354 // cannot make new requests. Unblock (and disable) it to allow this
355 // navigation to succeed. The interstitial will stay visible until the
356 // resulting DidNavigate.
357 if (tab_contents_->interstitial_page()) {
358 tab_contents_->interstitial_page()->CancelForNavigation();
359 }
360
[email protected]cbab76d2008-10-13 22:42:47361 bool transient = (transient_entry_index_ != -1);
362
[email protected]765b35502008-08-21 00:51:20363 // Base the navigation on where we are now...
364 int current_index = GetCurrentEntryIndex();
365
[email protected]cbab76d2008-10-13 22:42:47366 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20367
[email protected]cbab76d2008-10-13 22:42:47368 pending_entry_index_ = current_index;
369 // If there was a transient entry, we removed it making the current index
370 // the next page.
371 if (!transient)
372 pending_entry_index_++;
373
[email protected]784688a62010-09-13 07:06:52374 entries_[pending_entry_index_]->set_transition_type(
375 entries_[pending_entry_index_]->transition_type() |
376 PageTransition::FORWARD_BACK);
[email protected]1ccb3568d2010-02-19 10:51:16377 NavigateToPendingEntry(NO_RELOAD);
[email protected]765b35502008-08-21 00:51:20378}
379
380void NavigationController::GoToIndex(int index) {
381 if (index < 0 || index >= static_cast<int>(entries_.size())) {
382 NOTREACHED();
383 return;
384 }
385
[email protected]cbab76d2008-10-13 22:42:47386 if (transient_entry_index_ != -1) {
387 if (index == transient_entry_index_) {
388 // Nothing to do when navigating to the transient.
389 return;
390 }
391 if (index > transient_entry_index_) {
392 // Removing the transient is goint to shift all entries by 1.
393 index--;
394 }
395 }
396
[email protected]25396da2010-03-11 19:19:10397 // If an interstitial page is showing, the previous renderer is blocked and
398 // cannot make new requests.
399 if (tab_contents_->interstitial_page()) {
400 if (index == GetCurrentEntryIndex() - 1) {
401 // Going back one entry is equivalent to hiding the interstitial.
402 tab_contents_->interstitial_page()->DontProceed();
403 return;
404 } else {
405 // Unblock the renderer (and disable the interstitial) to allow this
406 // navigation to succeed. The interstitial will stay visible until the
407 // resulting DidNavigate.
408 tab_contents_->interstitial_page()->CancelForNavigation();
409 }
410 }
411
[email protected]cbab76d2008-10-13 22:42:47412 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20413
414 pending_entry_index_ = index;
[email protected]784688a62010-09-13 07:06:52415 entries_[pending_entry_index_]->set_transition_type(
416 entries_[pending_entry_index_]->transition_type() |
417 PageTransition::FORWARD_BACK);
[email protected]1ccb3568d2010-02-19 10:51:16418 NavigateToPendingEntry(NO_RELOAD);
[email protected]765b35502008-08-21 00:51:20419}
420
421void NavigationController::GoToOffset(int offset) {
[email protected]cbab76d2008-10-13 22:42:47422 int index = (transient_entry_index_ != -1) ?
423 transient_entry_index_ + offset :
424 last_committed_entry_index_ + offset;
[email protected]7f0005a2009-04-15 03:25:11425 if (index < 0 || index >= entry_count())
[email protected]765b35502008-08-21 00:51:20426 return;
427
428 GoToIndex(index);
429}
430
[email protected]cbab76d2008-10-13 22:42:47431void NavigationController::RemoveEntryAtIndex(int index,
432 const GURL& default_url) {
[email protected]43032342011-03-21 14:10:31433 bool is_current = index == last_committed_entry_index_;
434 RemoveEntryAtIndexInternal(index);
435 if (is_current) {
[email protected]cbab76d2008-10-13 22:42:47436 // We removed the currently shown entry, so we have to load something else.
437 if (last_committed_entry_index_ != -1) {
438 pending_entry_index_ = last_committed_entry_index_;
[email protected]1ccb3568d2010-02-19 10:51:16439 NavigateToPendingEntry(NO_RELOAD);
[email protected]cbab76d2008-10-13 22:42:47440 } else {
441 // If there is nothing to show, show a default page.
[email protected]ed3456f2009-02-26 20:24:48442 LoadURL(default_url.is_empty() ? GURL("about:blank") : default_url,
[email protected]c0588052008-10-27 23:01:50443 GURL(), PageTransition::START_PAGE);
[email protected]cbab76d2008-10-13 22:42:47444 }
[email protected]cbab76d2008-10-13 22:42:47445 }
[email protected]cbab76d2008-10-13 22:42:47446}
447
[email protected]38178a42009-12-17 18:58:32448void NavigationController::UpdateVirtualURLToURL(
449 NavigationEntry* entry, const GURL& new_url) {
450 GURL new_virtual_url(new_url);
451 if (BrowserURLHandler::ReverseURLRewrite(
452 &new_virtual_url, entry->virtual_url(), profile_)) {
453 entry->set_virtual_url(new_virtual_url);
454 }
455}
456
[email protected]cbab76d2008-10-13 22:42:47457void NavigationController::AddTransientEntry(NavigationEntry* entry) {
458 // Discard any current transient entry, we can only have one at a time.
459 int index = 0;
460 if (last_committed_entry_index_ != -1)
461 index = last_committed_entry_index_ + 1;
462 DiscardTransientEntry();
463 entries_.insert(entries_.begin() + index, linked_ptr<NavigationEntry>(entry));
[email protected]e1cd5452010-08-26 18:03:25464 transient_entry_index_ = index;
[email protected]8030f012009-09-25 18:09:37465 tab_contents_->NotifyNavigationStateChanged(kInvalidateAllButShelves);
[email protected]cbab76d2008-10-13 22:42:47466}
467
[email protected]c0588052008-10-27 23:01:50468void NavigationController::LoadURL(const GURL& url, const GURL& referrer,
initial.commit09911bf2008-07-26 23:55:29469 PageTransition::Type transition) {
470 // The user initiated a load, we don't need to reload anymore.
471 needs_reload_ = false;
472
[email protected]b6ea7412010-05-04 23:26:47473 NavigationEntry* entry = CreateNavigationEntry(url, referrer, transition,
474 profile_);
initial.commit09911bf2008-07-26 23:55:29475
476 LoadEntry(entry);
477}
478
[email protected]09b8f82f2009-06-16 20:22:11479void NavigationController::DocumentLoadedInFrame() {
480 last_document_loaded_ = base::TimeTicks::Now();
481}
482
[email protected]e9ba4472008-09-14 15:42:43483bool NavigationController::RendererDidNavigate(
484 const ViewHostMsg_FrameNavigate_Params& params,
[email protected]8030f012009-09-25 18:09:37485 int extra_invalidate_flags,
[email protected]e9ba4472008-09-14 15:42:43486 LoadCommittedDetails* details) {
[email protected]4bf3522c2010-08-19 21:00:20487
[email protected]0e8db942008-09-24 21:21:48488 // Save the previous state before we clobber it.
489 if (GetLastCommittedEntry()) {
[email protected]ecd9d8702008-08-28 22:10:17490 details->previous_url = GetLastCommittedEntry()->url();
[email protected]7f0005a2009-04-15 03:25:11491 details->previous_entry_index = last_committed_entry_index();
[email protected]0e8db942008-09-24 21:21:48492 } else {
493 details->previous_url = GURL();
494 details->previous_entry_index = -1;
495 }
[email protected]ecd9d8702008-08-28 22:10:17496
[email protected]49bd30e62011-03-22 20:12:59497 // The pending_entry has no SiteInstance when we are restoring an entry. We
498 // must fill it in here so we can find the entry later by calling
499 // GetEntryIndexWithPageID. In all other cases, the SiteInstance should be
500 // assigned already and we shouldn't change it.
501 if (pending_entry_index_ >= 0 && !pending_entry_->site_instance()) {
502 DCHECK(pending_entry_->restore_type() != NavigationEntry::RESTORE_NONE);
[email protected]9423d9412009-04-14 22:13:55503 pending_entry_->set_site_instance(tab_contents_->GetSiteInstance());
[email protected]5e369672009-11-03 23:48:30504 pending_entry_->set_restore_type(NavigationEntry::RESTORE_NONE);
505 }
[email protected]e9ba4472008-09-14 15:42:43506
[email protected]192d8c5e2010-02-23 07:26:32507 // is_in_page must be computed before the entry gets committed.
508 details->is_in_page = IsURLInPageNavigation(params.url);
509
[email protected]e9ba4472008-09-14 15:42:43510 // Do navigation-type specific actions. These will make and commit an entry.
[email protected]0e8db942008-09-24 21:21:48511 details->type = ClassifyNavigation(params);
[email protected]4bf3522c2010-08-19 21:00:20512
[email protected]0e8db942008-09-24 21:21:48513 switch (details->type) {
514 case NavigationType::NEW_PAGE:
[email protected]befd8d822009-07-01 04:51:47515 RendererDidNavigateToNewPage(params, &(details->did_replace_entry));
[email protected]e9ba4472008-09-14 15:42:43516 break;
[email protected]0e8db942008-09-24 21:21:48517 case NavigationType::EXISTING_PAGE:
[email protected]e9ba4472008-09-14 15:42:43518 RendererDidNavigateToExistingPage(params);
519 break;
[email protected]0e8db942008-09-24 21:21:48520 case NavigationType::SAME_PAGE:
[email protected]e9ba4472008-09-14 15:42:43521 RendererDidNavigateToSamePage(params);
522 break;
[email protected]0e8db942008-09-24 21:21:48523 case NavigationType::IN_PAGE:
[email protected]befd8d822009-07-01 04:51:47524 RendererDidNavigateInPage(params, &(details->did_replace_entry));
[email protected]e9ba4472008-09-14 15:42:43525 break;
[email protected]0e8db942008-09-24 21:21:48526 case NavigationType::NEW_SUBFRAME:
[email protected]e9ba4472008-09-14 15:42:43527 RendererDidNavigateNewSubframe(params);
528 break;
[email protected]0e8db942008-09-24 21:21:48529 case NavigationType::AUTO_SUBFRAME:
[email protected]e9ba4472008-09-14 15:42:43530 if (!RendererDidNavigateAutoSubframe(params))
531 return false;
532 break;
[email protected]0e8db942008-09-24 21:21:48533 case NavigationType::NAV_IGNORE:
[email protected]e9ba4472008-09-14 15:42:43534 // There is nothing we can do with this navigation, so we just return to
535 // the caller that nothing has happened.
536 return false;
537 default:
538 NOTREACHED();
[email protected]765b35502008-08-21 00:51:20539 }
540
[email protected]e9ba4472008-09-14 15:42:43541 // All committed entries should have nonempty content state so WebKit doesn't
542 // get confused when we go back to them (see the function for details).
[email protected]0f38dc4552011-02-25 11:24:00543 DCHECK(!params.content_state.empty());
544 NavigationEntry* active_entry = GetActiveEntry();
545 active_entry->set_content_state(params.content_state);
[email protected]765b35502008-08-21 00:51:20546
[email protected]49bd30e62011-03-22 20:12:59547 // The active entry's SiteInstance should match our SiteInstance.
548 DCHECK(active_entry->site_instance() == tab_contents_->GetSiteInstance());
549
[email protected]e9ba4472008-09-14 15:42:43550 // WebKit doesn't set the "auto" transition on meta refreshes properly (bug
551 // 1051891) so we manually set it for redirects which we normally treat as
552 // "non-user-gestures" where we want to update stuff after navigations.
553 //
554 // Note that the redirect check also checks for a pending entry to
555 // differentiate real redirects from browser initiated navigations to a
556 // redirected entry. This happens when you hit back to go to a page that was
557 // the destination of a redirect, we don't want to treat it as a redirect
558 // even though that's what its transition will be. See bug 1117048.
559 //
560 // TODO(brettw) write a test for this complicated logic.
561 details->is_auto = (PageTransition::IsRedirect(params.transition) &&
[email protected]7f0005a2009-04-15 03:25:11562 !pending_entry()) ||
[email protected]e9ba4472008-09-14 15:42:43563 params.gesture == NavigationGestureAuto;
[email protected]765b35502008-08-21 00:51:20564
[email protected]e9ba4472008-09-14 15:42:43565 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:00566 details->entry = active_entry;
[email protected]e9ba4472008-09-14 15:42:43567 details->is_main_frame = PageTransition::IsMainFrame(params.transition);
[email protected]f072d2ce2008-09-17 17:16:24568 details->serialized_security_info = params.security_info;
[email protected]2e39d2e2009-02-19 18:41:31569 details->http_status_code = params.http_status_code;
[email protected]8030f012009-09-25 18:09:37570 NotifyNavigationEntryCommitted(details, extra_invalidate_flags);
initial.commit09911bf2008-07-26 23:55:29571
[email protected]e9ba4472008-09-14 15:42:43572 return true;
initial.commit09911bf2008-07-26 23:55:29573}
574
[email protected]0e8db942008-09-24 21:21:48575NavigationType::Type NavigationController::ClassifyNavigation(
[email protected]e9ba4472008-09-14 15:42:43576 const ViewHostMsg_FrameNavigate_Params& params) const {
[email protected]e9ba4472008-09-14 15:42:43577 if (params.page_id == -1) {
[email protected]eef9de32009-09-23 17:19:46578 // The renderer generates the page IDs, and so if it gives us the invalid
579 // page ID (-1) we know it didn't actually navigate. This happens in a few
580 // cases:
581 //
582 // - If a page makes a popup navigated to about blank, and then writes
583 // stuff like a subframe navigated to a real page. We'll get the commit
584 // for the subframe, but there won't be any commit for the outer page.
585 //
586 // - We were also getting these for failed loads (for example, bug 21849).
587 // The guess is that we get a "load commit" for the alternate error page,
588 // but that doesn't affect the page ID, so we get the "old" one, which
589 // could be invalid. This can also happen for a cross-site transition
590 // that causes us to swap processes. Then the error page load will be in
591 // a new process with no page IDs ever assigned (and hence a -1 value),
592 // yet the navigation controller still might have previous pages in its
593 // list.
594 //
595 // In these cases, there's nothing we can do with them, so ignore.
[email protected]0e8db942008-09-24 21:21:48596 return NavigationType::NAV_IGNORE;
[email protected]e9ba4472008-09-14 15:42:43597 }
598
[email protected]9423d9412009-04-14 22:13:55599 if (params.page_id > tab_contents_->GetMaxPageID()) {
[email protected]e9ba4472008-09-14 15:42:43600 // Greater page IDs than we've ever seen before are new pages. We may or may
601 // not have a pending entry for the page, and this may or may not be the
602 // main frame.
603 if (PageTransition::IsMainFrame(params.transition))
[email protected]0e8db942008-09-24 21:21:48604 return NavigationType::NEW_PAGE;
[email protected]4c27ba82008-09-24 16:49:09605
606 // When this is a new subframe navigation, we should have a committed page
607 // for which it's a suframe in. This may not be the case when an iframe is
608 // navigated on a popup navigated to about:blank (the iframe would be
609 // written into the popup by script on the main page). For these cases,
610 // there isn't any navigation stuff we can do, so just ignore it.
611 if (!GetLastCommittedEntry())
[email protected]0e8db942008-09-24 21:21:48612 return NavigationType::NAV_IGNORE;
[email protected]4c27ba82008-09-24 16:49:09613
614 // Valid subframe navigation.
[email protected]0e8db942008-09-24 21:21:48615 return NavigationType::NEW_SUBFRAME;
[email protected]e9ba4472008-09-14 15:42:43616 }
617
618 // Now we know that the notification is for an existing page. Find that entry.
619 int existing_entry_index = GetEntryIndexWithPageID(
[email protected]9423d9412009-04-14 22:13:55620 tab_contents_->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:43621 params.page_id);
622 if (existing_entry_index == -1) {
623 // The page was not found. It could have been pruned because of the limit on
624 // back/forward entries (not likely since we'll usually tell it to navigate
625 // to such entries). It could also mean that the renderer is smoking crack.
626 NOTREACHED();
[email protected]0e8db942008-09-24 21:21:48627 return NavigationType::NAV_IGNORE;
[email protected]e9ba4472008-09-14 15:42:43628 }
629 NavigationEntry* existing_entry = entries_[existing_entry_index].get();
630
[email protected]e6035c22010-05-25 16:15:52631 if (!PageTransition::IsMainFrame(params.transition)) {
632 // All manual subframes would get new IDs and were handled above, so we
633 // know this is auto. Since the current page was found in the navigation
634 // entry list, we're guaranteed to have a last committed entry.
635 DCHECK(GetLastCommittedEntry());
636 return NavigationType::AUTO_SUBFRAME;
637 }
638
639 // Anything below here we know is a main frame navigation.
[email protected]e9ba4472008-09-14 15:42:43640 if (pending_entry_ &&
[email protected]e9ba4472008-09-14 15:42:43641 existing_entry != pending_entry_ &&
[email protected]a0e69262009-06-03 19:08:48642 pending_entry_->page_id() == -1) {
[email protected]e9ba4472008-09-14 15:42:43643 // In this case, we have a pending entry for a URL but WebCore didn't do a
644 // new navigation. This happens when you press enter in the URL bar to
645 // reload. We will create a pending entry, but WebKit will convert it to
646 // a reload since it's the same page and not create a new entry for it
647 // (the user doesn't want to have a new back/forward entry when they do
648 // this). In this case, we want to just ignore the pending entry and go
649 // back to where we were (the "existing entry").
[email protected]0e8db942008-09-24 21:21:48650 return NavigationType::SAME_PAGE;
[email protected]e9ba4472008-09-14 15:42:43651 }
652
[email protected]fc60f222008-12-18 17:36:54653 // Any toplevel navigations with the same base (minus the reference fragment)
654 // are in-page navigations. We weeded out subframe navigations above. Most of
655 // the time this doesn't matter since WebKit doesn't tell us about subframe
656 // navigations that don't actually navigate, but it can happen when there is
657 // an encoding override (it always sends a navigation request).
658 if (AreURLsInPageNavigation(existing_entry->url(), params.url))
659 return NavigationType::IN_PAGE;
660
[email protected]e9ba4472008-09-14 15:42:43661 // Since we weeded out "new" navigations above, we know this is an existing
[email protected]4c27ba82008-09-24 16:49:09662 // (back/forward) navigation.
[email protected]0e8db942008-09-24 21:21:48663 return NavigationType::EXISTING_PAGE;
[email protected]e9ba4472008-09-14 15:42:43664}
665
[email protected]09b8f82f2009-06-16 20:22:11666bool NavigationController::IsRedirect(
667 const ViewHostMsg_FrameNavigate_Params& params) {
668 // For main frame transition, we judge by params.transition.
669 // Otherwise, by params.redirects.
670 if (PageTransition::IsMainFrame(params.transition)) {
671 return PageTransition::IsRedirect(params.transition);
672 }
673 return params.redirects.size() > 1;
674}
675
[email protected]b6ea7412010-05-04 23:26:47676void NavigationController::CreateNavigationEntriesFromTabNavigations(
677 const std::vector<TabNavigation>& navigations,
678 std::vector<linked_ptr<NavigationEntry> >* entries) {
679 // Create a NavigationEntry for each of the navigations.
680 int page_id = 0;
681 for (std::vector<TabNavigation>::const_iterator i =
682 navigations.begin(); i != navigations.end(); ++i, ++page_id) {
683 linked_ptr<NavigationEntry> entry(i->ToNavigationEntry(page_id, profile_));
684 entries->push_back(entry);
685 }
686}
687
[email protected]e9ba4472008-09-14 15:42:43688void NavigationController::RendererDidNavigateToNewPage(
[email protected]befd8d822009-07-01 04:51:47689 const ViewHostMsg_FrameNavigate_Params& params, bool* did_replace_entry) {
[email protected]e9ba4472008-09-14 15:42:43690 NavigationEntry* new_entry;
691 if (pending_entry_) {
692 // TODO(brettw) this assumes that the pending entry is appropriate for the
693 // new page that was just loaded. I don't think this is necessarily the
[email protected]49bd30e62011-03-22 20:12:59694 // case! We should have some more tracking to know for sure.
[email protected]e9ba4472008-09-14 15:42:43695 new_entry = new NavigationEntry(*pending_entry_);
696
697 // Don't use the page type from the pending entry. Some interstitial page
698 // may have set the type to interstitial. Once we commit, however, the page
699 // type must always be normal.
[email protected]cccd3762010-11-12 18:40:01700 new_entry->set_page_type(NORMAL_PAGE);
[email protected]e9ba4472008-09-14 15:42:43701 } else {
[email protected]b680ad22009-04-15 23:19:42702 new_entry = new NavigationEntry;
[email protected]e9ba4472008-09-14 15:42:43703 }
704
705 new_entry->set_url(params.url);
[email protected]38178a42009-12-17 18:58:32706 if (new_entry->update_virtual_url_with_url())
707 UpdateVirtualURLToURL(new_entry, params.url);
[email protected]740fbda2009-02-18 21:38:08708 new_entry->set_referrer(params.referrer);
[email protected]e9ba4472008-09-14 15:42:43709 new_entry->set_page_id(params.page_id);
710 new_entry->set_transition_type(params.transition);
[email protected]9423d9412009-04-14 22:13:55711 new_entry->set_site_instance(tab_contents_->GetSiteInstance());
[email protected]e9ba4472008-09-14 15:42:43712 new_entry->set_has_post_data(params.is_post);
713
[email protected]befd8d822009-07-01 04:51:47714 InsertOrReplaceEntry(new_entry, *did_replace_entry);
[email protected]e9ba4472008-09-14 15:42:43715}
716
717void NavigationController::RendererDidNavigateToExistingPage(
718 const ViewHostMsg_FrameNavigate_Params& params) {
719 // We should only get here for main frame navigations.
720 DCHECK(PageTransition::IsMainFrame(params.transition));
721
722 // This is a back/forward navigation. The existing page for the ID is
[email protected]4c27ba82008-09-24 16:49:09723 // guaranteed to exist by ClassifyNavigation, and we just need to update it
724 // with new information from the renderer.
[email protected]7f0005a2009-04-15 03:25:11725 int entry_index = GetEntryIndexWithPageID(tab_contents_->GetSiteInstance(),
726 params.page_id);
[email protected]e9ba4472008-09-14 15:42:43727 DCHECK(entry_index >= 0 &&
728 entry_index < static_cast<int>(entries_.size()));
729 NavigationEntry* entry = entries_[entry_index].get();
730
731 // The URL may have changed due to redirects. The site instance will normally
732 // be the same except during session restore, when no site instance will be
733 // assigned.
734 entry->set_url(params.url);
[email protected]38178a42009-12-17 18:58:32735 if (entry->update_virtual_url_with_url())
736 UpdateVirtualURLToURL(entry, params.url);
[email protected]e9ba4472008-09-14 15:42:43737 DCHECK(entry->site_instance() == NULL ||
[email protected]9423d9412009-04-14 22:13:55738 entry->site_instance() == tab_contents_->GetSiteInstance());
739 entry->set_site_instance(tab_contents_->GetSiteInstance());
[email protected]e9ba4472008-09-14 15:42:43740
[email protected]d5a49e52010-01-08 03:01:41741 entry->set_has_post_data(params.is_post);
742
[email protected]e9ba4472008-09-14 15:42:43743 // The entry we found in the list might be pending if the user hit
744 // back/forward/reload. This load should commit it (since it's already in the
[email protected]49bd30e62011-03-22 20:12:59745 // list, we can just discard the pending pointer). We should also discard the
746 // pending entry if it corresponds to a different navigation, since that one
747 // is now likely canceled. If it is not canceled, we will treat it as a new
748 // navigation when it arrives, which is also ok.
[email protected]e9ba4472008-09-14 15:42:43749 //
750 // Note that we need to use the "internal" version since we don't want to
751 // actually change any other state, just kill the pointer.
[email protected]49bd30e62011-03-22 20:12:59752 if (pending_entry_)
[email protected]cbab76d2008-10-13 22:42:47753 DiscardNonCommittedEntriesInternal();
[email protected]40bcc302009-03-02 20:50:39754
[email protected]80858db52009-10-15 00:35:18755 // If a transient entry was removed, the indices might have changed, so we
756 // have to query the entry index again.
757 last_committed_entry_index_ =
758 GetEntryIndexWithPageID(tab_contents_->GetSiteInstance(), params.page_id);
[email protected]e9ba4472008-09-14 15:42:43759}
760
761void NavigationController::RendererDidNavigateToSamePage(
762 const ViewHostMsg_FrameNavigate_Params& params) {
763 // This mode implies we have a pending entry that's the same as an existing
[email protected]4c27ba82008-09-24 16:49:09764 // entry for this page ID. This entry is guaranteed to exist by
765 // ClassifyNavigation. All we need to do is update the existing entry.
[email protected]e9ba4472008-09-14 15:42:43766 NavigationEntry* existing_entry = GetEntryWithPageID(
[email protected]9423d9412009-04-14 22:13:55767 tab_contents_->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:43768 params.page_id);
769
770 // We assign the entry's unique ID to be that of the new one. Since this is
771 // always the result of a user action, we want to dismiss infobars, etc. like
772 // a regular user-initiated navigation.
773 existing_entry->set_unique_id(pending_entry_->unique_id());
774
[email protected]a0e69262009-06-03 19:08:48775 // The URL may have changed due to redirects.
[email protected]38178a42009-12-17 18:58:32776 if (existing_entry->update_virtual_url_with_url())
777 UpdateVirtualURLToURL(existing_entry, params.url);
[email protected]a0e69262009-06-03 19:08:48778 existing_entry->set_url(params.url);
779
[email protected]cbab76d2008-10-13 22:42:47780 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:43781}
782
783void NavigationController::RendererDidNavigateInPage(
[email protected]befd8d822009-07-01 04:51:47784 const ViewHostMsg_FrameNavigate_Params& params, bool* did_replace_entry) {
[email protected]e9ba4472008-09-14 15:42:43785 DCHECK(PageTransition::IsMainFrame(params.transition)) <<
786 "WebKit should only tell us about in-page navs for the main frame.";
787 // We're guaranteed to have an entry for this one.
788 NavigationEntry* existing_entry = GetEntryWithPageID(
[email protected]9423d9412009-04-14 22:13:55789 tab_contents_->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:43790 params.page_id);
791
792 // Reference fragment navigation. We're guaranteed to have the last_committed
793 // entry and it will be the same page as the new navigation (minus the
794 // reference fragments, of course).
795 NavigationEntry* new_entry = new NavigationEntry(*existing_entry);
796 new_entry->set_page_id(params.page_id);
[email protected]38178a42009-12-17 18:58:32797 if (new_entry->update_virtual_url_with_url())
798 UpdateVirtualURLToURL(new_entry, params.url);
[email protected]e9ba4472008-09-14 15:42:43799 new_entry->set_url(params.url);
[email protected]ccbe04e2010-03-17 17:58:43800
801 // This replaces the existing entry since the page ID didn't change.
802 *did_replace_entry = true;
803 InsertOrReplaceEntry(new_entry, true);
[email protected]e9ba4472008-09-14 15:42:43804}
805
806void NavigationController::RendererDidNavigateNewSubframe(
807 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]09b8f82f2009-06-16 20:22:11808 if (PageTransition::StripQualifier(params.transition) ==
809 PageTransition::AUTO_SUBFRAME) {
810 // This is not user-initiated. Ignore.
811 return;
812 }
[email protected]09b8f82f2009-06-16 20:22:11813
[email protected]e9ba4472008-09-14 15:42:43814 // Manual subframe navigations just get the current entry cloned so the user
815 // can go back or forward to it. The actual subframe information will be
816 // stored in the page state for each of those entries. This happens out of
817 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:09818 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
819 << "that a last committed entry exists.";
[email protected]e9ba4472008-09-14 15:42:43820 NavigationEntry* new_entry = new NavigationEntry(*GetLastCommittedEntry());
821 new_entry->set_page_id(params.page_id);
[email protected]672eba292009-05-13 13:22:45822 InsertOrReplaceEntry(new_entry, false);
[email protected]e9ba4472008-09-14 15:42:43823}
824
825bool NavigationController::RendererDidNavigateAutoSubframe(
826 const ViewHostMsg_FrameNavigate_Params& params) {
827 // We're guaranteed to have a previously committed entry, and we now need to
828 // handle navigation inside of a subframe in it without creating a new entry.
829 DCHECK(GetLastCommittedEntry());
830
831 // Handle the case where we're navigating back/forward to a previous subframe
832 // navigation entry. This is case "2." in NAV_AUTO_SUBFRAME comment in the
833 // header file. In case "1." this will be a NOP.
834 int entry_index = GetEntryIndexWithPageID(
[email protected]9423d9412009-04-14 22:13:55835 tab_contents_->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:43836 params.page_id);
837 if (entry_index < 0 ||
838 entry_index >= static_cast<int>(entries_.size())) {
839 NOTREACHED();
840 return false;
841 }
842
843 // Update the current navigation entry in case we're going back/forward.
844 if (entry_index != last_committed_entry_index_) {
[email protected]e9ba4472008-09-14 15:42:43845 last_committed_entry_index_ = entry_index;
[email protected]e9ba4472008-09-14 15:42:43846 return true;
847 }
848 return false;
849}
850
[email protected]ce3fa3c2009-04-20 19:55:57851// TODO(brettw) I think this function is unnecessary.
[email protected]e9ba4472008-09-14 15:42:43852void NavigationController::CommitPendingEntry() {
[email protected]cbab76d2008-10-13 22:42:47853 DiscardTransientEntry();
854
[email protected]7f0005a2009-04-15 03:25:11855 if (!pending_entry())
[email protected]e9ba4472008-09-14 15:42:43856 return; // Nothing to do.
857
858 // Need to save the previous URL for the notification.
859 LoadCommittedDetails details;
[email protected]0e8db942008-09-24 21:21:48860 if (GetLastCommittedEntry()) {
[email protected]e9ba4472008-09-14 15:42:43861 details.previous_url = GetLastCommittedEntry()->url();
[email protected]7f0005a2009-04-15 03:25:11862 details.previous_entry_index = last_committed_entry_index();
[email protected]0e8db942008-09-24 21:21:48863 } else {
864 details.previous_entry_index = -1;
865 }
[email protected]e9ba4472008-09-14 15:42:43866
867 if (pending_entry_index_ >= 0) {
868 // This is a previous navigation (back/forward) that we're just now
869 // committing. Just mark it as committed.
[email protected]0e8db942008-09-24 21:21:48870 details.type = NavigationType::EXISTING_PAGE;
[email protected]e9ba4472008-09-14 15:42:43871 int new_entry_index = pending_entry_index_;
[email protected]cbab76d2008-10-13 22:42:47872 DiscardNonCommittedEntriesInternal();
[email protected]e9ba4472008-09-14 15:42:43873
874 // Mark that entry as committed.
[email protected]e9ba4472008-09-14 15:42:43875 last_committed_entry_index_ = new_entry_index;
[email protected]e9ba4472008-09-14 15:42:43876 } else {
877 // This is a new navigation. It's easiest to just copy the entry and insert
[email protected]672eba292009-05-13 13:22:45878 // it new again, since InsertOrReplaceEntry expects to take ownership and
879 // also discard the pending entry. We also need to synthesize a page ID. We
880 // can only do this because this function will only be called by our custom
[email protected]57c6a652009-05-04 07:58:34881 // TabContents types. For TabContents, the IDs are generated by the
[email protected]e9ba4472008-09-14 15:42:43882 // renderer, so we can't do this.
[email protected]0e8db942008-09-24 21:21:48883 details.type = NavigationType::NEW_PAGE;
[email protected]9423d9412009-04-14 22:13:55884 pending_entry_->set_page_id(tab_contents_->GetMaxPageID() + 1);
885 tab_contents_->UpdateMaxPageID(pending_entry_->page_id());
[email protected]672eba292009-05-13 13:22:45886 InsertOrReplaceEntry(new NavigationEntry(*pending_entry_), false);
[email protected]e9ba4472008-09-14 15:42:43887 }
888
889 // Broadcast the notification of the navigation.
890 details.entry = GetActiveEntry();
891 details.is_auto = false;
892 details.is_in_page = AreURLsInPageNavigation(details.previous_url,
893 details.entry->url());
894 details.is_main_frame = true;
[email protected]8030f012009-09-25 18:09:37895 NotifyNavigationEntryCommitted(&details, 0);
[email protected]e9ba4472008-09-14 15:42:43896}
[email protected]765b35502008-08-21 00:51:20897
898int NavigationController::GetIndexOfEntry(
899 const NavigationEntry* entry) const {
900 const NavigationEntries::const_iterator i(std::find(
901 entries_.begin(),
902 entries_.end(),
903 entry));
904 return (i == entries_.end()) ? -1 : static_cast<int>(i - entries_.begin());
905}
906
[email protected]e9ba4472008-09-14 15:42:43907bool NavigationController::IsURLInPageNavigation(const GURL& url) const {
908 NavigationEntry* last_committed = GetLastCommittedEntry();
909 if (!last_committed)
910 return false;
911 return AreURLsInPageNavigation(last_committed->url(), url);
912}
913
[email protected]ce3fa3c2009-04-20 19:55:57914void NavigationController::CopyStateFrom(const NavigationController& source) {
915 // Verify that we look new.
916 DCHECK(entry_count() == 0 && !pending_entry());
917
918 if (source.entry_count() == 0)
919 return; // Nothing new to do.
920
921 needs_reload_ = true;
[email protected]e1cd5452010-08-26 18:03:25922 InsertEntriesFrom(source, source.entry_count());
[email protected]ce3fa3c2009-04-20 19:55:57923
[email protected]6ee12c42010-09-14 09:36:07924 session_storage_namespace_ = source.session_storage_namespace_->Clone();
[email protected]4e6419c2010-01-15 04:50:34925
[email protected]5e369672009-11-03 23:48:30926 FinishRestore(source.last_committed_entry_index_, false);
[email protected]ce3fa3c2009-04-20 19:55:57927}
928
[email protected]43032342011-03-21 14:10:31929void NavigationController::CopyStateFromAndPrune(NavigationController* source,
930 bool remove_first_entry) {
[email protected]e1cd5452010-08-26 18:03:25931 // This code is intended for use when the last entry is the active entry.
932 DCHECK((transient_entry_index_ != -1 &&
933 transient_entry_index_ == entry_count() - 1) ||
934 (pending_entry_ && (pending_entry_index_ == -1 ||
935 pending_entry_index_ == entry_count() - 1)) ||
936 (!pending_entry_ && last_committed_entry_index_ == entry_count() - 1));
937
[email protected]43032342011-03-21 14:10:31938 if (remove_first_entry && entry_count()) {
939 // Save then restore the pending entry (RemoveEntryAtIndexInternal chucks
940 // the pending entry).
941 NavigationEntry* pending_entry = pending_entry_;
942 pending_entry_ = NULL;
943 int pending_entry_index = pending_entry_index_;
944 RemoveEntryAtIndexInternal(0);
945 // Restore the pending entry.
946 if (pending_entry_index != -1) {
947 pending_entry_index_ = pending_entry_index - 1;
948 if (pending_entry_index_ != -1)
949 pending_entry_ = entries_[pending_entry_index_].get();
950 } else if (pending_entry) {
951 pending_entry_ = pending_entry;
952 }
953 }
954
[email protected]e1cd5452010-08-26 18:03:25955 // Remove all the entries leaving the active entry.
956 PruneAllButActive();
957
[email protected]47e020a2010-10-15 14:43:37958 // Insert the entries from source. Don't use source->GetCurrentEntryIndex as
[email protected]e1cd5452010-08-26 18:03:25959 // we don't want to copy over the transient entry.
[email protected]47e020a2010-10-15 14:43:37960 int max_source_index = source->pending_entry_index_ != -1 ?
961 source->pending_entry_index_ : source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:25962 if (max_source_index == -1)
[email protected]47e020a2010-10-15 14:43:37963 max_source_index = source->entry_count();
[email protected]e1cd5452010-08-26 18:03:25964 else
965 max_source_index++;
[email protected]47e020a2010-10-15 14:43:37966 InsertEntriesFrom(*source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:25967
968 // Adjust indices such that the last entry and pending are at the end now.
969 last_committed_entry_index_ = entry_count() - 1;
970 if (pending_entry_index_ != -1)
971 pending_entry_index_ = entry_count() - 1;
972 if (transient_entry_index_ != -1) {
973 // There's a transient entry. In this case we want the last committed to
974 // point to the previous entry.
975 transient_entry_index_ = entry_count() - 1;
976 if (last_committed_entry_index_ != -1)
977 last_committed_entry_index_--;
978 }
[email protected]e1cd5452010-08-26 18:03:25979}
980
[email protected]97b6c4f2010-09-27 19:31:26981void NavigationController::PruneAllButActive() {
[email protected]97b6c4f2010-09-27 19:31:26982 if (transient_entry_index_ != -1) {
983 // There is a transient entry. Prune up to it.
984 DCHECK_EQ(entry_count() - 1, transient_entry_index_);
[email protected]77d8d622010-12-15 10:30:12985 entries_.erase(entries_.begin(), entries_.begin() + transient_entry_index_);
[email protected]97b6c4f2010-09-27 19:31:26986 transient_entry_index_ = 0;
987 last_committed_entry_index_ = -1;
988 pending_entry_index_ = -1;
989 } else if (!pending_entry_) {
990 // There's no pending entry. Leave the last entry (if there is one).
[email protected]77d8d622010-12-15 10:30:12991 if (!entry_count())
[email protected]97b6c4f2010-09-27 19:31:26992 return;
993
[email protected]77d8d622010-12-15 10:30:12994 DCHECK(last_committed_entry_index_ >= 0);
995 entries_.erase(entries_.begin(),
996 entries_.begin() + last_committed_entry_index_);
997 entries_.erase(entries_.begin() + 1, entries_.end());
[email protected]97b6c4f2010-09-27 19:31:26998 last_committed_entry_index_ = 0;
999 } else if (pending_entry_index_ != -1) {
[email protected]77d8d622010-12-15 10:30:121000 entries_.erase(entries_.begin(), entries_.begin() + pending_entry_index_);
1001 entries_.erase(entries_.begin() + 1, entries_.end());
[email protected]97b6c4f2010-09-27 19:31:261002 pending_entry_index_ = 0;
1003 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:261004 } else {
1005 // There is a pending_entry, but it's not in entries_.
1006 pending_entry_index_ = -1;
1007 last_committed_entry_index_ = -1;
[email protected]77d8d622010-12-15 10:30:121008 entries_.clear();
[email protected]97b6c4f2010-09-27 19:31:261009 }
1010
1011 if (tab_contents_->interstitial_page()) {
1012 // Normally the interstitial page hides itself if the user doesn't proceeed.
1013 // This would result in showing a NavigationEntry we just removed. Set this
1014 // so the interstitial triggers a reload if the user doesn't proceed.
1015 tab_contents_->interstitial_page()->set_reload_on_dont_proceed(true);
1016 }
[email protected]97b6c4f2010-09-27 19:31:261017}
1018
[email protected]43032342011-03-21 14:10:311019void NavigationController::RemoveEntryAtIndexInternal(int index) {
1020 DCHECK(index < entry_count());
1021
1022 DiscardNonCommittedEntries();
1023
1024 entries_.erase(entries_.begin() + index);
1025 if (last_committed_entry_index_ == index)
1026 last_committed_entry_index_--;
1027 else if (last_committed_entry_index_ > index)
1028 last_committed_entry_index_--;
1029}
1030
[email protected]cbab76d2008-10-13 22:42:471031void NavigationController::DiscardNonCommittedEntries() {
1032 bool transient = transient_entry_index_ != -1;
1033 DiscardNonCommittedEntriesInternal();
initial.commit09911bf2008-07-26 23:55:291034
[email protected]cbab76d2008-10-13 22:42:471035 // If there was a transient entry, invalidate everything so the new active
1036 // entry state is shown.
1037 if (transient) {
[email protected]8030f012009-09-25 18:09:371038 tab_contents_->NotifyNavigationStateChanged(kInvalidateAllButShelves);
[email protected]cbab76d2008-10-13 22:42:471039 }
initial.commit09911bf2008-07-26 23:55:291040}
1041
[email protected]672eba292009-05-13 13:22:451042void NavigationController::InsertOrReplaceEntry(NavigationEntry* entry,
1043 bool replace) {
[email protected]1e5645ff2008-08-27 18:09:071044 DCHECK(entry->transition_type() != PageTransition::AUTO_SUBFRAME);
[email protected]765b35502008-08-21 00:51:201045
1046 // Copy the pending entry's unique ID to the committed entry.
1047 // I don't know if pending_entry_index_ can be other than -1 here.
1048 const NavigationEntry* const pending_entry = (pending_entry_index_ == -1) ?
1049 pending_entry_ : entries_[pending_entry_index_].get();
1050 if (pending_entry)
1051 entry->set_unique_id(pending_entry->unique_id());
1052
[email protected]cbab76d2008-10-13 22:42:471053 DiscardNonCommittedEntriesInternal();
[email protected]765b35502008-08-21 00:51:201054
1055 int current_size = static_cast<int>(entries_.size());
1056
[email protected]765b35502008-08-21 00:51:201057 if (current_size > 0) {
[email protected]672eba292009-05-13 13:22:451058 // Prune any entries which are in front of the current entry.
1059 // Also prune the current entry if we are to replace the current entry.
1060 int prune_up_to = replace ? last_committed_entry_index_ - 1
1061 : last_committed_entry_index_;
[email protected]c12bf1a12008-09-17 16:28:491062 int num_pruned = 0;
[email protected]672eba292009-05-13 13:22:451063 while (prune_up_to < (current_size - 1)) {
[email protected]c12bf1a12008-09-17 16:28:491064 num_pruned++;
[email protected]765b35502008-08-21 00:51:201065 entries_.pop_back();
1066 current_size--;
1067 }
[email protected]c12bf1a12008-09-17 16:28:491068 if (num_pruned > 0) // Only notify if we did prune something.
1069 NotifyPrunedEntries(this, false, num_pruned);
[email protected]765b35502008-08-21 00:51:201070 }
1071
[email protected]c12bf1a12008-09-17 16:28:491072 if (entries_.size() >= max_entry_count_) {
[email protected]cbab76d2008-10-13 22:42:471073 RemoveEntryAtIndex(0, GURL());
[email protected]c12bf1a12008-09-17 16:28:491074 NotifyPrunedEntries(this, true, 1);
1075 }
[email protected]765b35502008-08-21 00:51:201076
1077 entries_.push_back(linked_ptr<NavigationEntry>(entry));
1078 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
[email protected]e9ba4472008-09-14 15:42:431079
1080 // This is a new page ID, so we need everybody to know about it.
[email protected]9423d9412009-04-14 22:13:551081 tab_contents_->UpdateMaxPageID(entry->page_id());
initial.commit09911bf2008-07-26 23:55:291082}
1083
1084void NavigationController::SetWindowID(const SessionID& id) {
1085 window_id_ = id;
[email protected]bfd04a62009-02-01 18:16:561086 NotificationService::current()->Notify(NotificationType::TAB_PARENTED,
[email protected]534e54b2008-08-13 15:40:091087 Source<NavigationController>(this),
1088 NotificationService::NoDetails());
initial.commit09911bf2008-07-26 23:55:291089}
1090
[email protected]1ccb3568d2010-02-19 10:51:161091void NavigationController::NavigateToPendingEntry(ReloadType reload_type) {
[email protected]72097fd02010-01-21 23:36:011092 needs_reload_ = false;
1093
initial.commit09911bf2008-07-26 23:55:291094 // For session history navigations only the pending_entry_index_ is set.
1095 if (!pending_entry_) {
[email protected]4bf3522c2010-08-19 21:00:201096 DCHECK_NE(pending_entry_index_, -1);
[email protected]765b35502008-08-21 00:51:201097 pending_entry_ = entries_[pending_entry_index_].get();
initial.commit09911bf2008-07-26 23:55:291098 }
1099
[email protected]1ccb3568d2010-02-19 10:51:161100 if (!tab_contents_->NavigateToPendingEntry(reload_type))
[email protected]cbab76d2008-10-13 22:42:471101 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:291102}
1103
[email protected]ecd9d8702008-08-28 22:10:171104void NavigationController::NotifyNavigationEntryCommitted(
[email protected]8030f012009-09-25 18:09:371105 LoadCommittedDetails* details,
1106 int extra_invalidate_flags) {
[email protected]df1af242009-05-01 00:11:401107 details->entry = GetActiveEntry();
1108 NotificationDetails notification_details =
1109 Details<LoadCommittedDetails>(details);
1110
1111 // We need to notify the ssl_manager_ before the tab_contents_ so the
1112 // location bar will have up-to-date information about the security style
1113 // when it wants to draw. See http://crbug.com/11157
1114 ssl_manager_.DidCommitProvisionalLoad(notification_details);
1115
initial.commit09911bf2008-07-26 23:55:291116 // TODO(pkasting): http://b/1113079 Probably these explicit notification paths
1117 // should be removed, and interested parties should just listen for the
1118 // notification below instead.
[email protected]9423d9412009-04-14 22:13:551119 tab_contents_->NotifyNavigationStateChanged(
[email protected]6ebdc9b2010-09-27 16:55:571120 kInvalidateAllButShelves | extra_invalidate_flags);
initial.commit09911bf2008-07-26 23:55:291121
[email protected]ecd9d8702008-08-28 22:10:171122 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:561123 NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ecd9d8702008-08-28 22:10:171124 Source<NavigationController>(this),
[email protected]df1af242009-05-01 00:11:401125 notification_details);
initial.commit09911bf2008-07-26 23:55:291126}
1127
initial.commit09911bf2008-07-26 23:55:291128// static
1129void NavigationController::DisablePromptOnRepost() {
1130 check_for_repost_ = false;
1131}
1132
1133void NavigationController::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:221134 if (is_active && needs_reload_)
1135 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:291136}
1137
1138void NavigationController::LoadIfNecessary() {
1139 if (!needs_reload_)
1140 return;
1141
initial.commit09911bf2008-07-26 23:55:291142 // Calling Reload() results in ignoring state, and not loading.
1143 // Explicitly use NavigateToPendingEntry so that the renderer uses the
1144 // cached state.
1145 pending_entry_index_ = last_committed_entry_index_;
[email protected]1ccb3568d2010-02-19 10:51:161146 NavigateToPendingEntry(NO_RELOAD);
initial.commit09911bf2008-07-26 23:55:291147}
1148
[email protected]534e54b2008-08-13 15:40:091149void NavigationController::NotifyEntryChanged(const NavigationEntry* entry,
1150 int index) {
1151 EntryChangedDetails det;
1152 det.changed_entry = entry;
1153 det.index = index;
[email protected]bfd04a62009-02-01 18:16:561154 NotificationService::current()->Notify(NotificationType::NAV_ENTRY_CHANGED,
[email protected]534e54b2008-08-13 15:40:091155 Source<NavigationController>(this),
1156 Details<EntryChangedDetails>(&det));
initial.commit09911bf2008-07-26 23:55:291157}
1158
[email protected]5e369672009-11-03 23:48:301159void NavigationController::FinishRestore(int selected_index,
1160 bool from_last_session) {
[email protected]7f0005a2009-04-15 03:25:111161 DCHECK(selected_index >= 0 && selected_index < entry_count());
[email protected]5e369672009-11-03 23:48:301162 ConfigureEntriesForRestore(&entries_, from_last_session);
initial.commit09911bf2008-07-26 23:55:291163
[email protected]61398152010-08-26 21:45:341164 set_max_restored_page_id(static_cast<int32>(entry_count()));
initial.commit09911bf2008-07-26 23:55:291165
1166 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:291167}
[email protected]765b35502008-08-21 00:51:201168
[email protected]cbab76d2008-10-13 22:42:471169void NavigationController::DiscardNonCommittedEntriesInternal() {
[email protected]765b35502008-08-21 00:51:201170 if (pending_entry_index_ == -1)
1171 delete pending_entry_;
1172 pending_entry_ = NULL;
1173 pending_entry_index_ = -1;
[email protected]cbab76d2008-10-13 22:42:471174
1175 DiscardTransientEntry();
1176}
1177
1178void NavigationController::DiscardTransientEntry() {
1179 if (transient_entry_index_ == -1)
1180 return;
[email protected]a0e69262009-06-03 19:08:481181 entries_.erase(entries_.begin() + transient_entry_index_);
[email protected]80858db52009-10-15 00:35:181182 if (last_committed_entry_index_ > transient_entry_index_)
1183 last_committed_entry_index_--;
[email protected]cbab76d2008-10-13 22:42:471184 transient_entry_index_ = -1;
[email protected]765b35502008-08-21 00:51:201185}
1186
1187int NavigationController::GetEntryIndexWithPageID(
[email protected]7f0005a2009-04-15 03:25:111188 SiteInstance* instance, int32 page_id) const {
[email protected]765b35502008-08-21 00:51:201189 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
[email protected]7f0005a2009-04-15 03:25:111190 if ((entries_[i]->site_instance() == instance) &&
[email protected]1e5645ff2008-08-27 18:09:071191 (entries_[i]->page_id() == page_id))
[email protected]765b35502008-08-21 00:51:201192 return i;
1193 }
1194 return -1;
1195}
[email protected]cbab76d2008-10-13 22:42:471196
1197NavigationEntry* NavigationController::GetTransientEntry() const {
1198 if (transient_entry_index_ == -1)
1199 return NULL;
1200 return entries_[transient_entry_index_].get();
1201}
[email protected]e1cd5452010-08-26 18:03:251202
[email protected]e1cd5452010-08-26 18:03:251203void NavigationController::InsertEntriesFrom(
1204 const NavigationController& source,
1205 int max_index) {
1206 DCHECK_LE(max_index, source.entry_count());
1207 size_t insert_index = 0;
1208 for (int i = 0; i < max_index; i++) {
1209 // When cloning a tab, copy all entries except interstitial pages
[email protected]cccd3762010-11-12 18:40:011210 if (source.entries_[i].get()->page_type() != INTERSTITIAL_PAGE) {
[email protected]e1cd5452010-08-26 18:03:251211 entries_.insert(entries_.begin() + insert_index++,
1212 linked_ptr<NavigationEntry>(
1213 new NavigationEntry(*source.entries_[i])));
1214 }
1215 }
1216}