blob: e30ac46201f723c12076c24de0060786f5a74b15 [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]299dabd2008-11-19 02:27:1618#include "chrome/common/navigation_types.h"
[email protected]a23de8572009-06-03 02:16:3219#include "chrome/common/pref_names.h"
[email protected]939856a2010-08-24 20:29:0220#include "chrome/common/render_messages_params.h"
[email protected]6de74452009-02-25 18:04:5921#include "chrome/common/url_constants.h"
[email protected]567812d2011-02-24 17:40:5022#include "content/browser/in_process_webkit/session_storage_namespace.h"
23#include "content/browser/site_instance.h"
[email protected]0dd3a0ab2011-02-18 08:17:4424#include "content/browser/tab_contents/interstitial_page.h"
25#include "content/browser/tab_contents/navigation_entry.h"
26#include "content/browser/tab_contents/tab_contents.h"
27#include "content/browser/tab_contents/tab_contents_delegate.h"
[email protected]7f070d42011-03-09 20:25:3228#include "content/common/notification_service.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) {
433 int size = static_cast<int>(entries_.size());
434 DCHECK(index < size);
435
436 DiscardNonCommittedEntries();
437
438 entries_.erase(entries_.begin() + index);
439
440 if (last_committed_entry_index_ == index) {
441 last_committed_entry_index_--;
442 // We removed the currently shown entry, so we have to load something else.
443 if (last_committed_entry_index_ != -1) {
444 pending_entry_index_ = last_committed_entry_index_;
[email protected]1ccb3568d2010-02-19 10:51:16445 NavigateToPendingEntry(NO_RELOAD);
[email protected]cbab76d2008-10-13 22:42:47446 } else {
447 // If there is nothing to show, show a default page.
[email protected]ed3456f2009-02-26 20:24:48448 LoadURL(default_url.is_empty() ? GURL("about:blank") : default_url,
[email protected]c0588052008-10-27 23:01:50449 GURL(), PageTransition::START_PAGE);
[email protected]cbab76d2008-10-13 22:42:47450 }
451 } else if (last_committed_entry_index_ > index) {
452 last_committed_entry_index_--;
453 }
[email protected]cbab76d2008-10-13 22:42:47454}
455
[email protected]38178a42009-12-17 18:58:32456void NavigationController::UpdateVirtualURLToURL(
457 NavigationEntry* entry, const GURL& new_url) {
458 GURL new_virtual_url(new_url);
459 if (BrowserURLHandler::ReverseURLRewrite(
460 &new_virtual_url, entry->virtual_url(), profile_)) {
461 entry->set_virtual_url(new_virtual_url);
462 }
463}
464
[email protected]cbab76d2008-10-13 22:42:47465void NavigationController::AddTransientEntry(NavigationEntry* entry) {
466 // Discard any current transient entry, we can only have one at a time.
467 int index = 0;
468 if (last_committed_entry_index_ != -1)
469 index = last_committed_entry_index_ + 1;
470 DiscardTransientEntry();
471 entries_.insert(entries_.begin() + index, linked_ptr<NavigationEntry>(entry));
[email protected]e1cd5452010-08-26 18:03:25472 transient_entry_index_ = index;
[email protected]8030f012009-09-25 18:09:37473 tab_contents_->NotifyNavigationStateChanged(kInvalidateAllButShelves);
[email protected]cbab76d2008-10-13 22:42:47474}
475
[email protected]c0588052008-10-27 23:01:50476void NavigationController::LoadURL(const GURL& url, const GURL& referrer,
initial.commit09911bf2008-07-26 23:55:29477 PageTransition::Type transition) {
478 // The user initiated a load, we don't need to reload anymore.
479 needs_reload_ = false;
480
[email protected]b6ea7412010-05-04 23:26:47481 NavigationEntry* entry = CreateNavigationEntry(url, referrer, transition,
482 profile_);
initial.commit09911bf2008-07-26 23:55:29483
484 LoadEntry(entry);
485}
486
[email protected]09b8f82f2009-06-16 20:22:11487void NavigationController::DocumentLoadedInFrame() {
488 last_document_loaded_ = base::TimeTicks::Now();
489}
490
[email protected]e9ba4472008-09-14 15:42:43491bool NavigationController::RendererDidNavigate(
492 const ViewHostMsg_FrameNavigate_Params& params,
[email protected]8030f012009-09-25 18:09:37493 int extra_invalidate_flags,
[email protected]e9ba4472008-09-14 15:42:43494 LoadCommittedDetails* details) {
[email protected]4bf3522c2010-08-19 21:00:20495
[email protected]0e8db942008-09-24 21:21:48496 // Save the previous state before we clobber it.
497 if (GetLastCommittedEntry()) {
[email protected]ecd9d8702008-08-28 22:10:17498 details->previous_url = GetLastCommittedEntry()->url();
[email protected]7f0005a2009-04-15 03:25:11499 details->previous_entry_index = last_committed_entry_index();
[email protected]0e8db942008-09-24 21:21:48500 } else {
501 details->previous_url = GURL();
502 details->previous_entry_index = -1;
503 }
[email protected]ecd9d8702008-08-28 22:10:17504
[email protected]e9ba4472008-09-14 15:42:43505 // Assign the current site instance to any pending entry, so we can find it
506 // later by calling GetEntryIndexWithPageID. We only care about this if the
507 // pending entry is an existing navigation and not a new one (or else we
508 // wouldn't care about finding it with GetEntryIndexWithPageID).
509 //
510 // TODO(brettw) this seems slightly bogus as we don't really know if the
511 // pending entry is what this navigation is for. There is a similar TODO
512 // w.r.t. the pending entry in RendererDidNavigateToNewPage.
[email protected]5e369672009-11-03 23:48:30513 if (pending_entry_index_ >= 0) {
[email protected]9423d9412009-04-14 22:13:55514 pending_entry_->set_site_instance(tab_contents_->GetSiteInstance());
[email protected]5e369672009-11-03 23:48:30515 pending_entry_->set_restore_type(NavigationEntry::RESTORE_NONE);
516 }
[email protected]e9ba4472008-09-14 15:42:43517
[email protected]192d8c5e2010-02-23 07:26:32518 // is_in_page must be computed before the entry gets committed.
519 details->is_in_page = IsURLInPageNavigation(params.url);
520
[email protected]e9ba4472008-09-14 15:42:43521 // Do navigation-type specific actions. These will make and commit an entry.
[email protected]0e8db942008-09-24 21:21:48522 details->type = ClassifyNavigation(params);
[email protected]4bf3522c2010-08-19 21:00:20523
[email protected]0e8db942008-09-24 21:21:48524 switch (details->type) {
525 case NavigationType::NEW_PAGE:
[email protected]befd8d822009-07-01 04:51:47526 RendererDidNavigateToNewPage(params, &(details->did_replace_entry));
[email protected]e9ba4472008-09-14 15:42:43527 break;
[email protected]0e8db942008-09-24 21:21:48528 case NavigationType::EXISTING_PAGE:
[email protected]e9ba4472008-09-14 15:42:43529 RendererDidNavigateToExistingPage(params);
530 break;
[email protected]0e8db942008-09-24 21:21:48531 case NavigationType::SAME_PAGE:
[email protected]e9ba4472008-09-14 15:42:43532 RendererDidNavigateToSamePage(params);
533 break;
[email protected]0e8db942008-09-24 21:21:48534 case NavigationType::IN_PAGE:
[email protected]befd8d822009-07-01 04:51:47535 RendererDidNavigateInPage(params, &(details->did_replace_entry));
[email protected]e9ba4472008-09-14 15:42:43536 break;
[email protected]0e8db942008-09-24 21:21:48537 case NavigationType::NEW_SUBFRAME:
[email protected]e9ba4472008-09-14 15:42:43538 RendererDidNavigateNewSubframe(params);
539 break;
[email protected]0e8db942008-09-24 21:21:48540 case NavigationType::AUTO_SUBFRAME:
[email protected]e9ba4472008-09-14 15:42:43541 if (!RendererDidNavigateAutoSubframe(params))
542 return false;
543 break;
[email protected]0e8db942008-09-24 21:21:48544 case NavigationType::NAV_IGNORE:
[email protected]e9ba4472008-09-14 15:42:43545 // There is nothing we can do with this navigation, so we just return to
546 // the caller that nothing has happened.
547 return false;
548 default:
549 NOTREACHED();
[email protected]765b35502008-08-21 00:51:20550 }
551
[email protected]e9ba4472008-09-14 15:42:43552 // All committed entries should have nonempty content state so WebKit doesn't
553 // get confused when we go back to them (see the function for details).
[email protected]0f38dc4552011-02-25 11:24:00554 DCHECK(!params.content_state.empty());
555 NavigationEntry* active_entry = GetActiveEntry();
556 active_entry->set_content_state(params.content_state);
[email protected]765b35502008-08-21 00:51:20557
[email protected]e9ba4472008-09-14 15:42:43558 // WebKit doesn't set the "auto" transition on meta refreshes properly (bug
559 // 1051891) so we manually set it for redirects which we normally treat as
560 // "non-user-gestures" where we want to update stuff after navigations.
561 //
562 // Note that the redirect check also checks for a pending entry to
563 // differentiate real redirects from browser initiated navigations to a
564 // redirected entry. This happens when you hit back to go to a page that was
565 // the destination of a redirect, we don't want to treat it as a redirect
566 // even though that's what its transition will be. See bug 1117048.
567 //
568 // TODO(brettw) write a test for this complicated logic.
569 details->is_auto = (PageTransition::IsRedirect(params.transition) &&
[email protected]7f0005a2009-04-15 03:25:11570 !pending_entry()) ||
[email protected]e9ba4472008-09-14 15:42:43571 params.gesture == NavigationGestureAuto;
[email protected]765b35502008-08-21 00:51:20572
[email protected]e9ba4472008-09-14 15:42:43573 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:00574 details->entry = active_entry;
[email protected]e9ba4472008-09-14 15:42:43575 details->is_main_frame = PageTransition::IsMainFrame(params.transition);
[email protected]f072d2ce2008-09-17 17:16:24576 details->serialized_security_info = params.security_info;
[email protected]2e39d2e2009-02-19 18:41:31577 details->http_status_code = params.http_status_code;
[email protected]8030f012009-09-25 18:09:37578 NotifyNavigationEntryCommitted(details, extra_invalidate_flags);
initial.commit09911bf2008-07-26 23:55:29579
[email protected]e9ba4472008-09-14 15:42:43580 return true;
initial.commit09911bf2008-07-26 23:55:29581}
582
[email protected]0e8db942008-09-24 21:21:48583NavigationType::Type NavigationController::ClassifyNavigation(
[email protected]e9ba4472008-09-14 15:42:43584 const ViewHostMsg_FrameNavigate_Params& params) const {
[email protected]e9ba4472008-09-14 15:42:43585 if (params.page_id == -1) {
[email protected]eef9de32009-09-23 17:19:46586 // The renderer generates the page IDs, and so if it gives us the invalid
587 // page ID (-1) we know it didn't actually navigate. This happens in a few
588 // cases:
589 //
590 // - If a page makes a popup navigated to about blank, and then writes
591 // stuff like a subframe navigated to a real page. We'll get the commit
592 // for the subframe, but there won't be any commit for the outer page.
593 //
594 // - We were also getting these for failed loads (for example, bug 21849).
595 // The guess is that we get a "load commit" for the alternate error page,
596 // but that doesn't affect the page ID, so we get the "old" one, which
597 // could be invalid. This can also happen for a cross-site transition
598 // that causes us to swap processes. Then the error page load will be in
599 // a new process with no page IDs ever assigned (and hence a -1 value),
600 // yet the navigation controller still might have previous pages in its
601 // list.
602 //
603 // In these cases, there's nothing we can do with them, so ignore.
[email protected]0e8db942008-09-24 21:21:48604 return NavigationType::NAV_IGNORE;
[email protected]e9ba4472008-09-14 15:42:43605 }
606
[email protected]9423d9412009-04-14 22:13:55607 if (params.page_id > tab_contents_->GetMaxPageID()) {
[email protected]e9ba4472008-09-14 15:42:43608 // Greater page IDs than we've ever seen before are new pages. We may or may
609 // not have a pending entry for the page, and this may or may not be the
610 // main frame.
611 if (PageTransition::IsMainFrame(params.transition))
[email protected]0e8db942008-09-24 21:21:48612 return NavigationType::NEW_PAGE;
[email protected]4c27ba82008-09-24 16:49:09613
614 // When this is a new subframe navigation, we should have a committed page
615 // for which it's a suframe in. This may not be the case when an iframe is
616 // navigated on a popup navigated to about:blank (the iframe would be
617 // written into the popup by script on the main page). For these cases,
618 // there isn't any navigation stuff we can do, so just ignore it.
619 if (!GetLastCommittedEntry())
[email protected]0e8db942008-09-24 21:21:48620 return NavigationType::NAV_IGNORE;
[email protected]4c27ba82008-09-24 16:49:09621
622 // Valid subframe navigation.
[email protected]0e8db942008-09-24 21:21:48623 return NavigationType::NEW_SUBFRAME;
[email protected]e9ba4472008-09-14 15:42:43624 }
625
626 // Now we know that the notification is for an existing page. Find that entry.
627 int existing_entry_index = GetEntryIndexWithPageID(
[email protected]9423d9412009-04-14 22:13:55628 tab_contents_->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:43629 params.page_id);
630 if (existing_entry_index == -1) {
[email protected]e9ba4472008-09-14 15:42:43631 // The page was not found. It could have been pruned because of the limit on
632 // back/forward entries (not likely since we'll usually tell it to navigate
633 // to such entries). It could also mean that the renderer is smoking crack.
634 NOTREACHED();
[email protected]0e8db942008-09-24 21:21:48635 return NavigationType::NAV_IGNORE;
[email protected]e9ba4472008-09-14 15:42:43636 }
637 NavigationEntry* existing_entry = entries_[existing_entry_index].get();
638
[email protected]e6035c22010-05-25 16:15:52639 if (!PageTransition::IsMainFrame(params.transition)) {
640 // All manual subframes would get new IDs and were handled above, so we
641 // know this is auto. Since the current page was found in the navigation
642 // entry list, we're guaranteed to have a last committed entry.
643 DCHECK(GetLastCommittedEntry());
644 return NavigationType::AUTO_SUBFRAME;
645 }
646
647 // Anything below here we know is a main frame navigation.
[email protected]e9ba4472008-09-14 15:42:43648 if (pending_entry_ &&
[email protected]e9ba4472008-09-14 15:42:43649 existing_entry != pending_entry_ &&
[email protected]a0e69262009-06-03 19:08:48650 pending_entry_->page_id() == -1) {
[email protected]e9ba4472008-09-14 15:42:43651 // In this case, we have a pending entry for a URL but WebCore didn't do a
652 // new navigation. This happens when you press enter in the URL bar to
653 // reload. We will create a pending entry, but WebKit will convert it to
654 // a reload since it's the same page and not create a new entry for it
655 // (the user doesn't want to have a new back/forward entry when they do
656 // this). In this case, we want to just ignore the pending entry and go
657 // back to where we were (the "existing entry").
[email protected]0e8db942008-09-24 21:21:48658 return NavigationType::SAME_PAGE;
[email protected]e9ba4472008-09-14 15:42:43659 }
660
[email protected]fc60f222008-12-18 17:36:54661 // Any toplevel navigations with the same base (minus the reference fragment)
662 // are in-page navigations. We weeded out subframe navigations above. Most of
663 // the time this doesn't matter since WebKit doesn't tell us about subframe
664 // navigations that don't actually navigate, but it can happen when there is
665 // an encoding override (it always sends a navigation request).
666 if (AreURLsInPageNavigation(existing_entry->url(), params.url))
667 return NavigationType::IN_PAGE;
668
[email protected]e9ba4472008-09-14 15:42:43669 // Since we weeded out "new" navigations above, we know this is an existing
[email protected]4c27ba82008-09-24 16:49:09670 // (back/forward) navigation.
[email protected]0e8db942008-09-24 21:21:48671 return NavigationType::EXISTING_PAGE;
[email protected]e9ba4472008-09-14 15:42:43672}
673
[email protected]09b8f82f2009-06-16 20:22:11674bool NavigationController::IsRedirect(
675 const ViewHostMsg_FrameNavigate_Params& params) {
676 // For main frame transition, we judge by params.transition.
677 // Otherwise, by params.redirects.
678 if (PageTransition::IsMainFrame(params.transition)) {
679 return PageTransition::IsRedirect(params.transition);
680 }
681 return params.redirects.size() > 1;
682}
683
[email protected]b6ea7412010-05-04 23:26:47684void NavigationController::CreateNavigationEntriesFromTabNavigations(
685 const std::vector<TabNavigation>& navigations,
686 std::vector<linked_ptr<NavigationEntry> >* entries) {
687 // Create a NavigationEntry for each of the navigations.
688 int page_id = 0;
689 for (std::vector<TabNavigation>::const_iterator i =
690 navigations.begin(); i != navigations.end(); ++i, ++page_id) {
691 linked_ptr<NavigationEntry> entry(i->ToNavigationEntry(page_id, profile_));
692 entries->push_back(entry);
693 }
694}
695
[email protected]e9ba4472008-09-14 15:42:43696void NavigationController::RendererDidNavigateToNewPage(
[email protected]befd8d822009-07-01 04:51:47697 const ViewHostMsg_FrameNavigate_Params& params, bool* did_replace_entry) {
[email protected]e9ba4472008-09-14 15:42:43698 NavigationEntry* new_entry;
699 if (pending_entry_) {
700 // TODO(brettw) this assumes that the pending entry is appropriate for the
701 // new page that was just loaded. I don't think this is necessarily the
702 // case! We should have some more tracking to know for sure. This goes along
703 // with a similar TODO at the top of RendererDidNavigate where we blindly
704 // set the site instance on the pending entry.
705 new_entry = new NavigationEntry(*pending_entry_);
706
707 // Don't use the page type from the pending entry. Some interstitial page
708 // may have set the type to interstitial. Once we commit, however, the page
709 // type must always be normal.
[email protected]cccd3762010-11-12 18:40:01710 new_entry->set_page_type(NORMAL_PAGE);
[email protected]e9ba4472008-09-14 15:42:43711 } else {
[email protected]b680ad22009-04-15 23:19:42712 new_entry = new NavigationEntry;
[email protected]e9ba4472008-09-14 15:42:43713 }
714
715 new_entry->set_url(params.url);
[email protected]38178a42009-12-17 18:58:32716 if (new_entry->update_virtual_url_with_url())
717 UpdateVirtualURLToURL(new_entry, params.url);
[email protected]740fbda2009-02-18 21:38:08718 new_entry->set_referrer(params.referrer);
[email protected]e9ba4472008-09-14 15:42:43719 new_entry->set_page_id(params.page_id);
720 new_entry->set_transition_type(params.transition);
[email protected]9423d9412009-04-14 22:13:55721 new_entry->set_site_instance(tab_contents_->GetSiteInstance());
[email protected]e9ba4472008-09-14 15:42:43722 new_entry->set_has_post_data(params.is_post);
723
[email protected]befd8d822009-07-01 04:51:47724 InsertOrReplaceEntry(new_entry, *did_replace_entry);
[email protected]e9ba4472008-09-14 15:42:43725}
726
727void NavigationController::RendererDidNavigateToExistingPage(
728 const ViewHostMsg_FrameNavigate_Params& params) {
729 // We should only get here for main frame navigations.
730 DCHECK(PageTransition::IsMainFrame(params.transition));
731
732 // This is a back/forward navigation. The existing page for the ID is
[email protected]4c27ba82008-09-24 16:49:09733 // guaranteed to exist by ClassifyNavigation, and we just need to update it
734 // with new information from the renderer.
[email protected]7f0005a2009-04-15 03:25:11735 int entry_index = GetEntryIndexWithPageID(tab_contents_->GetSiteInstance(),
736 params.page_id);
[email protected]e9ba4472008-09-14 15:42:43737 DCHECK(entry_index >= 0 &&
738 entry_index < static_cast<int>(entries_.size()));
739 NavigationEntry* entry = entries_[entry_index].get();
740
741 // The URL may have changed due to redirects. The site instance will normally
742 // be the same except during session restore, when no site instance will be
743 // assigned.
744 entry->set_url(params.url);
[email protected]38178a42009-12-17 18:58:32745 if (entry->update_virtual_url_with_url())
746 UpdateVirtualURLToURL(entry, params.url);
[email protected]e9ba4472008-09-14 15:42:43747 DCHECK(entry->site_instance() == NULL ||
[email protected]9423d9412009-04-14 22:13:55748 entry->site_instance() == tab_contents_->GetSiteInstance());
749 entry->set_site_instance(tab_contents_->GetSiteInstance());
[email protected]e9ba4472008-09-14 15:42:43750
[email protected]d5a49e52010-01-08 03:01:41751 entry->set_has_post_data(params.is_post);
752
[email protected]e9ba4472008-09-14 15:42:43753 // The entry we found in the list might be pending if the user hit
754 // back/forward/reload. This load should commit it (since it's already in the
755 // list, we can just discard the pending pointer).
756 //
757 // Note that we need to use the "internal" version since we don't want to
758 // actually change any other state, just kill the pointer.
759 if (entry == pending_entry_)
[email protected]cbab76d2008-10-13 22:42:47760 DiscardNonCommittedEntriesInternal();
[email protected]40bcc302009-03-02 20:50:39761
[email protected]80858db52009-10-15 00:35:18762 // If a transient entry was removed, the indices might have changed, so we
763 // have to query the entry index again.
764 last_committed_entry_index_ =
765 GetEntryIndexWithPageID(tab_contents_->GetSiteInstance(), params.page_id);
[email protected]e9ba4472008-09-14 15:42:43766}
767
768void NavigationController::RendererDidNavigateToSamePage(
769 const ViewHostMsg_FrameNavigate_Params& params) {
770 // This mode implies we have a pending entry that's the same as an existing
[email protected]4c27ba82008-09-24 16:49:09771 // entry for this page ID. This entry is guaranteed to exist by
772 // ClassifyNavigation. All we need to do is update the existing entry.
[email protected]e9ba4472008-09-14 15:42:43773 NavigationEntry* existing_entry = GetEntryWithPageID(
[email protected]9423d9412009-04-14 22:13:55774 tab_contents_->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:43775 params.page_id);
776
777 // We assign the entry's unique ID to be that of the new one. Since this is
778 // always the result of a user action, we want to dismiss infobars, etc. like
779 // a regular user-initiated navigation.
780 existing_entry->set_unique_id(pending_entry_->unique_id());
781
[email protected]a0e69262009-06-03 19:08:48782 // The URL may have changed due to redirects.
[email protected]38178a42009-12-17 18:58:32783 if (existing_entry->update_virtual_url_with_url())
784 UpdateVirtualURLToURL(existing_entry, params.url);
[email protected]a0e69262009-06-03 19:08:48785 existing_entry->set_url(params.url);
786
[email protected]cbab76d2008-10-13 22:42:47787 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:43788}
789
790void NavigationController::RendererDidNavigateInPage(
[email protected]befd8d822009-07-01 04:51:47791 const ViewHostMsg_FrameNavigate_Params& params, bool* did_replace_entry) {
[email protected]e9ba4472008-09-14 15:42:43792 DCHECK(PageTransition::IsMainFrame(params.transition)) <<
793 "WebKit should only tell us about in-page navs for the main frame.";
794 // We're guaranteed to have an entry for this one.
795 NavigationEntry* existing_entry = GetEntryWithPageID(
[email protected]9423d9412009-04-14 22:13:55796 tab_contents_->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:43797 params.page_id);
798
799 // Reference fragment navigation. We're guaranteed to have the last_committed
800 // entry and it will be the same page as the new navigation (minus the
801 // reference fragments, of course).
802 NavigationEntry* new_entry = new NavigationEntry(*existing_entry);
803 new_entry->set_page_id(params.page_id);
[email protected]38178a42009-12-17 18:58:32804 if (new_entry->update_virtual_url_with_url())
805 UpdateVirtualURLToURL(new_entry, params.url);
[email protected]e9ba4472008-09-14 15:42:43806 new_entry->set_url(params.url);
[email protected]ccbe04e2010-03-17 17:58:43807
808 // This replaces the existing entry since the page ID didn't change.
809 *did_replace_entry = true;
810 InsertOrReplaceEntry(new_entry, true);
[email protected]e9ba4472008-09-14 15:42:43811}
812
813void NavigationController::RendererDidNavigateNewSubframe(
814 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]09b8f82f2009-06-16 20:22:11815 if (PageTransition::StripQualifier(params.transition) ==
816 PageTransition::AUTO_SUBFRAME) {
817 // This is not user-initiated. Ignore.
818 return;
819 }
[email protected]09b8f82f2009-06-16 20:22:11820
[email protected]e9ba4472008-09-14 15:42:43821 // Manual subframe navigations just get the current entry cloned so the user
822 // can go back or forward to it. The actual subframe information will be
823 // stored in the page state for each of those entries. This happens out of
824 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:09825 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
826 << "that a last committed entry exists.";
[email protected]e9ba4472008-09-14 15:42:43827 NavigationEntry* new_entry = new NavigationEntry(*GetLastCommittedEntry());
828 new_entry->set_page_id(params.page_id);
[email protected]672eba292009-05-13 13:22:45829 InsertOrReplaceEntry(new_entry, false);
[email protected]e9ba4472008-09-14 15:42:43830}
831
832bool NavigationController::RendererDidNavigateAutoSubframe(
833 const ViewHostMsg_FrameNavigate_Params& params) {
834 // We're guaranteed to have a previously committed entry, and we now need to
835 // handle navigation inside of a subframe in it without creating a new entry.
836 DCHECK(GetLastCommittedEntry());
837
838 // Handle the case where we're navigating back/forward to a previous subframe
839 // navigation entry. This is case "2." in NAV_AUTO_SUBFRAME comment in the
840 // header file. In case "1." this will be a NOP.
841 int entry_index = GetEntryIndexWithPageID(
[email protected]9423d9412009-04-14 22:13:55842 tab_contents_->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:43843 params.page_id);
844 if (entry_index < 0 ||
845 entry_index >= static_cast<int>(entries_.size())) {
846 NOTREACHED();
847 return false;
848 }
849
850 // Update the current navigation entry in case we're going back/forward.
851 if (entry_index != last_committed_entry_index_) {
[email protected]e9ba4472008-09-14 15:42:43852 last_committed_entry_index_ = entry_index;
[email protected]e9ba4472008-09-14 15:42:43853 return true;
854 }
855 return false;
856}
857
[email protected]ce3fa3c2009-04-20 19:55:57858// TODO(brettw) I think this function is unnecessary.
[email protected]e9ba4472008-09-14 15:42:43859void NavigationController::CommitPendingEntry() {
[email protected]cbab76d2008-10-13 22:42:47860 DiscardTransientEntry();
861
[email protected]7f0005a2009-04-15 03:25:11862 if (!pending_entry())
[email protected]e9ba4472008-09-14 15:42:43863 return; // Nothing to do.
864
865 // Need to save the previous URL for the notification.
866 LoadCommittedDetails details;
[email protected]0e8db942008-09-24 21:21:48867 if (GetLastCommittedEntry()) {
[email protected]e9ba4472008-09-14 15:42:43868 details.previous_url = GetLastCommittedEntry()->url();
[email protected]7f0005a2009-04-15 03:25:11869 details.previous_entry_index = last_committed_entry_index();
[email protected]0e8db942008-09-24 21:21:48870 } else {
871 details.previous_entry_index = -1;
872 }
[email protected]e9ba4472008-09-14 15:42:43873
874 if (pending_entry_index_ >= 0) {
875 // This is a previous navigation (back/forward) that we're just now
876 // committing. Just mark it as committed.
[email protected]0e8db942008-09-24 21:21:48877 details.type = NavigationType::EXISTING_PAGE;
[email protected]e9ba4472008-09-14 15:42:43878 int new_entry_index = pending_entry_index_;
[email protected]cbab76d2008-10-13 22:42:47879 DiscardNonCommittedEntriesInternal();
[email protected]e9ba4472008-09-14 15:42:43880
881 // Mark that entry as committed.
[email protected]e9ba4472008-09-14 15:42:43882 last_committed_entry_index_ = new_entry_index;
[email protected]e9ba4472008-09-14 15:42:43883 } else {
884 // This is a new navigation. It's easiest to just copy the entry and insert
[email protected]672eba292009-05-13 13:22:45885 // it new again, since InsertOrReplaceEntry expects to take ownership and
886 // also discard the pending entry. We also need to synthesize a page ID. We
887 // can only do this because this function will only be called by our custom
[email protected]57c6a652009-05-04 07:58:34888 // TabContents types. For TabContents, the IDs are generated by the
[email protected]e9ba4472008-09-14 15:42:43889 // renderer, so we can't do this.
[email protected]0e8db942008-09-24 21:21:48890 details.type = NavigationType::NEW_PAGE;
[email protected]9423d9412009-04-14 22:13:55891 pending_entry_->set_page_id(tab_contents_->GetMaxPageID() + 1);
892 tab_contents_->UpdateMaxPageID(pending_entry_->page_id());
[email protected]672eba292009-05-13 13:22:45893 InsertOrReplaceEntry(new NavigationEntry(*pending_entry_), false);
[email protected]e9ba4472008-09-14 15:42:43894 }
895
896 // Broadcast the notification of the navigation.
897 details.entry = GetActiveEntry();
898 details.is_auto = false;
899 details.is_in_page = AreURLsInPageNavigation(details.previous_url,
900 details.entry->url());
901 details.is_main_frame = true;
[email protected]8030f012009-09-25 18:09:37902 NotifyNavigationEntryCommitted(&details, 0);
[email protected]e9ba4472008-09-14 15:42:43903}
[email protected]765b35502008-08-21 00:51:20904
905int NavigationController::GetIndexOfEntry(
906 const NavigationEntry* entry) const {
907 const NavigationEntries::const_iterator i(std::find(
908 entries_.begin(),
909 entries_.end(),
910 entry));
911 return (i == entries_.end()) ? -1 : static_cast<int>(i - entries_.begin());
912}
913
[email protected]e9ba4472008-09-14 15:42:43914bool NavigationController::IsURLInPageNavigation(const GURL& url) const {
915 NavigationEntry* last_committed = GetLastCommittedEntry();
916 if (!last_committed)
917 return false;
918 return AreURLsInPageNavigation(last_committed->url(), url);
919}
920
[email protected]ce3fa3c2009-04-20 19:55:57921void NavigationController::CopyStateFrom(const NavigationController& source) {
922 // Verify that we look new.
923 DCHECK(entry_count() == 0 && !pending_entry());
924
925 if (source.entry_count() == 0)
926 return; // Nothing new to do.
927
928 needs_reload_ = true;
[email protected]e1cd5452010-08-26 18:03:25929 InsertEntriesFrom(source, source.entry_count());
[email protected]ce3fa3c2009-04-20 19:55:57930
[email protected]6ee12c42010-09-14 09:36:07931 session_storage_namespace_ = source.session_storage_namespace_->Clone();
[email protected]4e6419c2010-01-15 04:50:34932
[email protected]5e369672009-11-03 23:48:30933 FinishRestore(source.last_committed_entry_index_, false);
[email protected]ce3fa3c2009-04-20 19:55:57934}
935
[email protected]47e020a2010-10-15 14:43:37936void NavigationController::CopyStateFromAndPrune(NavigationController* source) {
[email protected]e1cd5452010-08-26 18:03:25937 // This code is intended for use when the last entry is the active entry.
938 DCHECK((transient_entry_index_ != -1 &&
939 transient_entry_index_ == entry_count() - 1) ||
940 (pending_entry_ && (pending_entry_index_ == -1 ||
941 pending_entry_index_ == entry_count() - 1)) ||
942 (!pending_entry_ && last_committed_entry_index_ == entry_count() - 1));
943
944 // Remove all the entries leaving the active entry.
945 PruneAllButActive();
946
[email protected]47e020a2010-10-15 14:43:37947 // Insert the entries from source. Don't use source->GetCurrentEntryIndex as
[email protected]e1cd5452010-08-26 18:03:25948 // we don't want to copy over the transient entry.
[email protected]47e020a2010-10-15 14:43:37949 int max_source_index = source->pending_entry_index_ != -1 ?
950 source->pending_entry_index_ : source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:25951 if (max_source_index == -1)
[email protected]47e020a2010-10-15 14:43:37952 max_source_index = source->entry_count();
[email protected]e1cd5452010-08-26 18:03:25953 else
954 max_source_index++;
[email protected]47e020a2010-10-15 14:43:37955 InsertEntriesFrom(*source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:25956
957 // Adjust indices such that the last entry and pending are at the end now.
958 last_committed_entry_index_ = entry_count() - 1;
959 if (pending_entry_index_ != -1)
960 pending_entry_index_ = entry_count() - 1;
961 if (transient_entry_index_ != -1) {
962 // There's a transient entry. In this case we want the last committed to
963 // point to the previous entry.
964 transient_entry_index_ = entry_count() - 1;
965 if (last_committed_entry_index_ != -1)
966 last_committed_entry_index_--;
967 }
[email protected]e1cd5452010-08-26 18:03:25968}
969
[email protected]97b6c4f2010-09-27 19:31:26970void NavigationController::PruneAllButActive() {
[email protected]97b6c4f2010-09-27 19:31:26971 if (transient_entry_index_ != -1) {
972 // There is a transient entry. Prune up to it.
973 DCHECK_EQ(entry_count() - 1, transient_entry_index_);
[email protected]77d8d622010-12-15 10:30:12974 entries_.erase(entries_.begin(), entries_.begin() + transient_entry_index_);
[email protected]97b6c4f2010-09-27 19:31:26975 transient_entry_index_ = 0;
976 last_committed_entry_index_ = -1;
977 pending_entry_index_ = -1;
978 } else if (!pending_entry_) {
979 // There's no pending entry. Leave the last entry (if there is one).
[email protected]77d8d622010-12-15 10:30:12980 if (!entry_count())
[email protected]97b6c4f2010-09-27 19:31:26981 return;
982
[email protected]77d8d622010-12-15 10:30:12983 DCHECK(last_committed_entry_index_ >= 0);
984 entries_.erase(entries_.begin(),
985 entries_.begin() + last_committed_entry_index_);
986 entries_.erase(entries_.begin() + 1, entries_.end());
[email protected]97b6c4f2010-09-27 19:31:26987 last_committed_entry_index_ = 0;
988 } else if (pending_entry_index_ != -1) {
[email protected]77d8d622010-12-15 10:30:12989 entries_.erase(entries_.begin(), entries_.begin() + pending_entry_index_);
990 entries_.erase(entries_.begin() + 1, entries_.end());
[email protected]97b6c4f2010-09-27 19:31:26991 pending_entry_index_ = 0;
992 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:26993 } else {
994 // There is a pending_entry, but it's not in entries_.
995 pending_entry_index_ = -1;
996 last_committed_entry_index_ = -1;
[email protected]77d8d622010-12-15 10:30:12997 entries_.clear();
[email protected]97b6c4f2010-09-27 19:31:26998 }
999
1000 if (tab_contents_->interstitial_page()) {
1001 // Normally the interstitial page hides itself if the user doesn't proceeed.
1002 // This would result in showing a NavigationEntry we just removed. Set this
1003 // so the interstitial triggers a reload if the user doesn't proceed.
1004 tab_contents_->interstitial_page()->set_reload_on_dont_proceed(true);
1005 }
[email protected]97b6c4f2010-09-27 19:31:261006}
1007
[email protected]cbab76d2008-10-13 22:42:471008void NavigationController::DiscardNonCommittedEntries() {
1009 bool transient = transient_entry_index_ != -1;
1010 DiscardNonCommittedEntriesInternal();
initial.commit09911bf2008-07-26 23:55:291011
[email protected]cbab76d2008-10-13 22:42:471012 // If there was a transient entry, invalidate everything so the new active
1013 // entry state is shown.
1014 if (transient) {
[email protected]8030f012009-09-25 18:09:371015 tab_contents_->NotifyNavigationStateChanged(kInvalidateAllButShelves);
[email protected]cbab76d2008-10-13 22:42:471016 }
initial.commit09911bf2008-07-26 23:55:291017}
1018
[email protected]672eba292009-05-13 13:22:451019void NavigationController::InsertOrReplaceEntry(NavigationEntry* entry,
1020 bool replace) {
[email protected]1e5645ff2008-08-27 18:09:071021 DCHECK(entry->transition_type() != PageTransition::AUTO_SUBFRAME);
[email protected]765b35502008-08-21 00:51:201022
1023 // Copy the pending entry's unique ID to the committed entry.
1024 // I don't know if pending_entry_index_ can be other than -1 here.
1025 const NavigationEntry* const pending_entry = (pending_entry_index_ == -1) ?
1026 pending_entry_ : entries_[pending_entry_index_].get();
1027 if (pending_entry)
1028 entry->set_unique_id(pending_entry->unique_id());
1029
[email protected]cbab76d2008-10-13 22:42:471030 DiscardNonCommittedEntriesInternal();
[email protected]765b35502008-08-21 00:51:201031
1032 int current_size = static_cast<int>(entries_.size());
1033
[email protected]765b35502008-08-21 00:51:201034 if (current_size > 0) {
[email protected]672eba292009-05-13 13:22:451035 // Prune any entries which are in front of the current entry.
1036 // Also prune the current entry if we are to replace the current entry.
1037 int prune_up_to = replace ? last_committed_entry_index_ - 1
1038 : last_committed_entry_index_;
[email protected]c12bf1a12008-09-17 16:28:491039 int num_pruned = 0;
[email protected]672eba292009-05-13 13:22:451040 while (prune_up_to < (current_size - 1)) {
[email protected]c12bf1a12008-09-17 16:28:491041 num_pruned++;
[email protected]765b35502008-08-21 00:51:201042 entries_.pop_back();
1043 current_size--;
1044 }
[email protected]c12bf1a12008-09-17 16:28:491045 if (num_pruned > 0) // Only notify if we did prune something.
1046 NotifyPrunedEntries(this, false, num_pruned);
[email protected]765b35502008-08-21 00:51:201047 }
1048
[email protected]c12bf1a12008-09-17 16:28:491049 if (entries_.size() >= max_entry_count_) {
[email protected]cbab76d2008-10-13 22:42:471050 RemoveEntryAtIndex(0, GURL());
[email protected]c12bf1a12008-09-17 16:28:491051 NotifyPrunedEntries(this, true, 1);
1052 }
[email protected]765b35502008-08-21 00:51:201053
1054 entries_.push_back(linked_ptr<NavigationEntry>(entry));
1055 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
[email protected]e9ba4472008-09-14 15:42:431056
1057 // This is a new page ID, so we need everybody to know about it.
[email protected]9423d9412009-04-14 22:13:551058 tab_contents_->UpdateMaxPageID(entry->page_id());
initial.commit09911bf2008-07-26 23:55:291059}
1060
1061void NavigationController::SetWindowID(const SessionID& id) {
1062 window_id_ = id;
[email protected]bfd04a62009-02-01 18:16:561063 NotificationService::current()->Notify(NotificationType::TAB_PARENTED,
[email protected]534e54b2008-08-13 15:40:091064 Source<NavigationController>(this),
1065 NotificationService::NoDetails());
initial.commit09911bf2008-07-26 23:55:291066}
1067
[email protected]1ccb3568d2010-02-19 10:51:161068void NavigationController::NavigateToPendingEntry(ReloadType reload_type) {
[email protected]72097fd02010-01-21 23:36:011069 needs_reload_ = false;
1070
initial.commit09911bf2008-07-26 23:55:291071 // For session history navigations only the pending_entry_index_ is set.
1072 if (!pending_entry_) {
[email protected]4bf3522c2010-08-19 21:00:201073 DCHECK_NE(pending_entry_index_, -1);
[email protected]765b35502008-08-21 00:51:201074 pending_entry_ = entries_[pending_entry_index_].get();
initial.commit09911bf2008-07-26 23:55:291075 }
1076
[email protected]1ccb3568d2010-02-19 10:51:161077 if (!tab_contents_->NavigateToPendingEntry(reload_type))
[email protected]cbab76d2008-10-13 22:42:471078 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:291079}
1080
[email protected]ecd9d8702008-08-28 22:10:171081void NavigationController::NotifyNavigationEntryCommitted(
[email protected]8030f012009-09-25 18:09:371082 LoadCommittedDetails* details,
1083 int extra_invalidate_flags) {
[email protected]df1af242009-05-01 00:11:401084 details->entry = GetActiveEntry();
1085 NotificationDetails notification_details =
1086 Details<LoadCommittedDetails>(details);
1087
1088 // We need to notify the ssl_manager_ before the tab_contents_ so the
1089 // location bar will have up-to-date information about the security style
1090 // when it wants to draw. See http://crbug.com/11157
1091 ssl_manager_.DidCommitProvisionalLoad(notification_details);
1092
initial.commit09911bf2008-07-26 23:55:291093 // TODO(pkasting): http://b/1113079 Probably these explicit notification paths
1094 // should be removed, and interested parties should just listen for the
1095 // notification below instead.
[email protected]9423d9412009-04-14 22:13:551096 tab_contents_->NotifyNavigationStateChanged(
[email protected]6ebdc9b2010-09-27 16:55:571097 kInvalidateAllButShelves | extra_invalidate_flags);
initial.commit09911bf2008-07-26 23:55:291098
[email protected]ecd9d8702008-08-28 22:10:171099 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:561100 NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ecd9d8702008-08-28 22:10:171101 Source<NavigationController>(this),
[email protected]df1af242009-05-01 00:11:401102 notification_details);
initial.commit09911bf2008-07-26 23:55:291103}
1104
initial.commit09911bf2008-07-26 23:55:291105// static
1106void NavigationController::DisablePromptOnRepost() {
1107 check_for_repost_ = false;
1108}
1109
1110void NavigationController::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:221111 if (is_active && needs_reload_)
1112 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:291113}
1114
1115void NavigationController::LoadIfNecessary() {
1116 if (!needs_reload_)
1117 return;
1118
initial.commit09911bf2008-07-26 23:55:291119 // Calling Reload() results in ignoring state, and not loading.
1120 // Explicitly use NavigateToPendingEntry so that the renderer uses the
1121 // cached state.
1122 pending_entry_index_ = last_committed_entry_index_;
[email protected]1ccb3568d2010-02-19 10:51:161123 NavigateToPendingEntry(NO_RELOAD);
initial.commit09911bf2008-07-26 23:55:291124}
1125
[email protected]534e54b2008-08-13 15:40:091126void NavigationController::NotifyEntryChanged(const NavigationEntry* entry,
1127 int index) {
1128 EntryChangedDetails det;
1129 det.changed_entry = entry;
1130 det.index = index;
[email protected]bfd04a62009-02-01 18:16:561131 NotificationService::current()->Notify(NotificationType::NAV_ENTRY_CHANGED,
[email protected]534e54b2008-08-13 15:40:091132 Source<NavigationController>(this),
1133 Details<EntryChangedDetails>(&det));
initial.commit09911bf2008-07-26 23:55:291134}
1135
[email protected]5e369672009-11-03 23:48:301136void NavigationController::FinishRestore(int selected_index,
1137 bool from_last_session) {
[email protected]7f0005a2009-04-15 03:25:111138 DCHECK(selected_index >= 0 && selected_index < entry_count());
[email protected]5e369672009-11-03 23:48:301139 ConfigureEntriesForRestore(&entries_, from_last_session);
initial.commit09911bf2008-07-26 23:55:291140
[email protected]61398152010-08-26 21:45:341141 set_max_restored_page_id(static_cast<int32>(entry_count()));
initial.commit09911bf2008-07-26 23:55:291142
1143 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:291144}
[email protected]765b35502008-08-21 00:51:201145
[email protected]cbab76d2008-10-13 22:42:471146void NavigationController::DiscardNonCommittedEntriesInternal() {
[email protected]765b35502008-08-21 00:51:201147 if (pending_entry_index_ == -1)
1148 delete pending_entry_;
1149 pending_entry_ = NULL;
1150 pending_entry_index_ = -1;
[email protected]cbab76d2008-10-13 22:42:471151
1152 DiscardTransientEntry();
1153}
1154
1155void NavigationController::DiscardTransientEntry() {
1156 if (transient_entry_index_ == -1)
1157 return;
[email protected]a0e69262009-06-03 19:08:481158 entries_.erase(entries_.begin() + transient_entry_index_);
[email protected]80858db52009-10-15 00:35:181159 if (last_committed_entry_index_ > transient_entry_index_)
1160 last_committed_entry_index_--;
[email protected]cbab76d2008-10-13 22:42:471161 transient_entry_index_ = -1;
[email protected]765b35502008-08-21 00:51:201162}
1163
1164int NavigationController::GetEntryIndexWithPageID(
[email protected]7f0005a2009-04-15 03:25:111165 SiteInstance* instance, int32 page_id) const {
[email protected]765b35502008-08-21 00:51:201166 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
[email protected]7f0005a2009-04-15 03:25:111167 if ((entries_[i]->site_instance() == instance) &&
[email protected]1e5645ff2008-08-27 18:09:071168 (entries_[i]->page_id() == page_id))
[email protected]765b35502008-08-21 00:51:201169 return i;
1170 }
1171 return -1;
1172}
[email protected]cbab76d2008-10-13 22:42:471173
1174NavigationEntry* NavigationController::GetTransientEntry() const {
1175 if (transient_entry_index_ == -1)
1176 return NULL;
1177 return entries_[transient_entry_index_].get();
1178}
[email protected]e1cd5452010-08-26 18:03:251179
[email protected]e1cd5452010-08-26 18:03:251180void NavigationController::InsertEntriesFrom(
1181 const NavigationController& source,
1182 int max_index) {
1183 DCHECK_LE(max_index, source.entry_count());
1184 size_t insert_index = 0;
1185 for (int i = 0; i < max_index; i++) {
1186 // When cloning a tab, copy all entries except interstitial pages
[email protected]cccd3762010-11-12 18:40:011187 if (source.entries_[i].get()->page_type() != INTERSTITIAL_PAGE) {
[email protected]e1cd5452010-08-26 18:03:251188 entries_.insert(entries_.begin() + insert_index++,
1189 linked_ptr<NavigationEntry>(
1190 new NavigationEntry(*source.entries_[i])));
1191 }
1192 }
1193}