blob: 2e0e8b6cc8cedfef8205f5d66909000386d29247 [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]8ecad5e2010-12-02 21:18:3312#include "chrome/browser/profiles/profile.h"
[email protected]b8148ac2011-07-13 22:03:2513#include "content/browser/browser_url_handler.h"
[email protected]419a0572011-04-18 22:21:4614#include "content/browser/child_process_security_policy.h"
[email protected]567812d2011-02-24 17:40:5015#include "content/browser/in_process_webkit/session_storage_namespace.h"
16#include "content/browser/site_instance.h"
[email protected]0dd3a0ab2011-02-18 08:17:4417#include "content/browser/tab_contents/interstitial_page.h"
[email protected]8286f51a2011-05-31 17:39:1318#include "content/browser/tab_contents/navigation_details.h"
[email protected]0dd3a0ab2011-02-18 08:17:4419#include "content/browser/tab_contents/navigation_entry.h"
20#include "content/browser/tab_contents/tab_contents.h"
21#include "content/browser/tab_contents/tab_contents_delegate.h"
[email protected]9966325b2011-04-18 05:00:1022#include "content/common/content_constants.h"
[email protected]4dd57932011-03-17 06:06:1223#include "content/common/navigation_types.h"
[email protected]7f070d42011-03-09 20:25:3224#include "content/common/notification_service.h"
[email protected]0aed2f52011-03-23 18:06:3625#include "content/common/view_messages.h"
[email protected]a23de8572009-06-03 02:16:3226#include "net/base/escape.h"
[email protected]31682282010-01-15 18:05:1627#include "net/base/mime_util.h"
[email protected]7f070d42011-03-09 20:25:3228#include "net/base/net_util.h"
[email protected]765b35502008-08-21 00:51:2029#include "webkit/glue/webkit_glue.h"
initial.commit09911bf2008-07-26 23:55:2930
[email protected]e9ba4472008-09-14 15:42:4331namespace {
32
[email protected]93f230e02011-06-01 14:40:0033const int kInvalidateAll = 0xFFFFFFFF;
[email protected]8030f012009-09-25 18:09:3734
[email protected]e9ba4472008-09-14 15:42:4335// Invoked when entries have been pruned, or removed. For example, if the
36// current entries are [google, digg, yahoo], with the current entry google,
37// and the user types in cnet, then digg and yahoo are pruned.
[email protected]c12bf1a12008-09-17 16:28:4938void NotifyPrunedEntries(NavigationController* nav_controller,
39 bool from_front,
40 int count) {
[email protected]8286f51a2011-05-31 17:39:1341 content::PrunedDetails details;
[email protected]c12bf1a12008-09-17 16:28:4942 details.from_front = from_front;
43 details.count = count;
[email protected]e9ba4472008-09-14 15:42:4344 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:2745 content::NOTIFICATION_NAV_LIST_PRUNED,
[email protected]e9ba4472008-09-14 15:42:4346 Source<NavigationController>(nav_controller),
[email protected]8286f51a2011-05-31 17:39:1347 Details<content::PrunedDetails>(&details));
[email protected]e9ba4472008-09-14 15:42:4348}
49
50// Ensure the given NavigationEntry has a valid state, so that WebKit does not
51// get confused if we navigate back to it.
[email protected]40bcc302009-03-02 20:50:3952//
[email protected]e9ba4472008-09-14 15:42:4353// An empty state is treated as a new navigation by WebKit, which would mean
54// losing the navigation entries and generating a new navigation entry after
55// this one. We don't want that. To avoid this we create a valid state which
56// WebKit will not treat as a new navigation.
57void SetContentStateIfEmpty(NavigationEntry* entry) {
[email protected]965524b2009-04-04 21:32:4058 if (entry->content_state().empty()) {
[email protected]e9ba4472008-09-14 15:42:4359 entry->set_content_state(
60 webkit_glue::CreateHistoryStateForURL(entry->url()));
61 }
62}
63
64// Configure all the NavigationEntries in entries for restore. This resets
65// the transition type to reload and makes sure the content state isn't empty.
66void ConfigureEntriesForRestore(
[email protected]5e369672009-11-03 23:48:3067 std::vector<linked_ptr<NavigationEntry> >* entries,
68 bool from_last_session) {
[email protected]e9ba4472008-09-14 15:42:4369 for (size_t i = 0; i < entries->size(); ++i) {
70 // Use a transition type of reload so that we don't incorrectly increase
71 // the typed count.
72 (*entries)[i]->set_transition_type(PageTransition::RELOAD);
[email protected]5e369672009-11-03 23:48:3073 (*entries)[i]->set_restore_type(from_last_session ?
74 NavigationEntry::RESTORE_LAST_SESSION :
75 NavigationEntry::RESTORE_CURRENT_SESSION);
[email protected]e9ba4472008-09-14 15:42:4376 // NOTE(darin): This code is only needed for backwards compat.
77 SetContentStateIfEmpty((*entries)[i].get());
78 }
79}
80
81// See NavigationController::IsURLInPageNavigation for how this works and why.
82bool AreURLsInPageNavigation(const GURL& existing_url, const GURL& new_url) {
[email protected]192d8c5e2010-02-23 07:26:3283 if (existing_url == new_url || !new_url.has_ref()) {
84 // TODO(jcampan): what about when navigating back from a ref URL to the top
85 // non ref URL? Nothing is loaded in that case but we return false here.
86 // The user could also navigate from the ref URL to the non ref URL by
87 // entering the non ref URL in the location bar or through a bookmark, in
88 // which case there would be a load. I am not sure if the non-load/load
89 // scenarios can be differentiated with the TransitionType.
[email protected]e9ba4472008-09-14 15:42:4390 return false;
[email protected]192d8c5e2010-02-23 07:26:3291 }
[email protected]e9ba4472008-09-14 15:42:4392
93 url_canon::Replacements<char> replacements;
94 replacements.ClearRef();
95 return existing_url.ReplaceComponents(replacements) ==
96 new_url.ReplaceComponents(replacements);
97}
98
99} // namespace
100
initial.commit09911bf2008-07-26 23:55:29101// NavigationController ---------------------------------------------------
102
[email protected]765b35502008-08-21 00:51:20103// static
[email protected]3cc72b12010-03-18 23:03:00104size_t NavigationController::max_entry_count_ =
[email protected]9966325b2011-04-18 05:00:10105 content::kMaxSessionHistoryEntries;
[email protected]765b35502008-08-21 00:51:20106
initial.commit09911bf2008-07-26 23:55:29107// static
108bool NavigationController::check_for_repost_ = true;
109
[email protected]6ee12c42010-09-14 09:36:07110NavigationController::NavigationController(
111 TabContents* contents,
112 Profile* profile,
113 SessionStorageNamespace* session_storage_namespace)
initial.commit09911bf2008-07-26 23:55:29114 : profile_(profile),
[email protected]765b35502008-08-21 00:51:20115 pending_entry_(NULL),
116 last_committed_entry_index_(-1),
117 pending_entry_index_(-1),
[email protected]cbab76d2008-10-13 22:42:47118 transient_entry_index_(-1),
[email protected]9423d9412009-04-14 22:13:55119 tab_contents_(contents),
initial.commit09911bf2008-07-26 23:55:29120 max_restored_page_id_(-1),
[email protected]5d063842009-05-15 04:08:24121 ALLOW_THIS_IN_INITIALIZER_LIST(ssl_manager_(this)),
[email protected]38b8f4e2009-09-24 19:44:57122 needs_reload_(false),
[email protected]6ee12c42010-09-14 09:36:07123 session_storage_namespace_(session_storage_namespace),
[email protected]106a0812010-03-18 00:15:12124 pending_reload_(NO_RELOAD) {
initial.commit09911bf2008-07-26 23:55:29125 DCHECK(profile_);
[email protected]228d06592011-04-01 20:38:59126 if (!session_storage_namespace_) {
127 session_storage_namespace_ = new SessionStorageNamespace(
128 profile_->GetWebKitContext());
129 }
initial.commit09911bf2008-07-26 23:55:29130}
131
initial.commit09911bf2008-07-26 23:55:29132NavigationController::~NavigationController() {
[email protected]cbab76d2008-10-13 22:42:47133 DiscardNonCommittedEntriesInternal();
[email protected]c0993872008-08-21 19:59:44134
[email protected]bfd04a62009-02-01 18:16:56135 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27136 content::NOTIFICATION_TAB_CLOSED,
[email protected]bfd04a62009-02-01 18:16:56137 Source<NavigationController>(this),
138 NotificationService::NoDetails());
initial.commit09911bf2008-07-26 23:55:29139}
140
[email protected]03838e22011-06-06 15:27:14141void NavigationController::Restore(
[email protected]5e369672009-11-03 23:48:30142 int selected_navigation,
[email protected]03838e22011-06-06 15:27:14143 bool from_last_session,
144 std::vector<NavigationEntry*>* entries) {
[email protected]ce3fa3c2009-04-20 19:55:57145 // Verify that this controller is unused and that the input is valid.
146 DCHECK(entry_count() == 0 && !pending_entry());
147 DCHECK(selected_navigation >= 0 &&
[email protected]03838e22011-06-06 15:27:14148 selected_navigation < static_cast<int>(entries->size()));
[email protected]ce3fa3c2009-04-20 19:55:57149
[email protected]ce3fa3c2009-04-20 19:55:57150 needs_reload_ = true;
[email protected]03838e22011-06-06 15:27:14151 for (size_t i = 0; i < entries->size(); ++i)
152 entries_.push_back(linked_ptr<NavigationEntry>((*entries)[i]));
153 entries->clear();
[email protected]ce3fa3c2009-04-20 19:55:57154
155 // And finish the restore.
[email protected]5e369672009-11-03 23:48:30156 FinishRestore(selected_navigation, from_last_session);
[email protected]ce3fa3c2009-04-20 19:55:57157}
158
[email protected]f1c74112008-10-30 16:17:04159void NavigationController::Reload(bool check_for_repost) {
[email protected]1ccb3568d2010-02-19 10:51:16160 ReloadInternal(check_for_repost, RELOAD);
161}
162void NavigationController::ReloadIgnoringCache(bool check_for_repost) {
163 ReloadInternal(check_for_repost, RELOAD_IGNORING_CACHE);
164}
165
166void NavigationController::ReloadInternal(bool check_for_repost,
167 ReloadType reload_type) {
[email protected]cbab76d2008-10-13 22:42:47168 // Reloading a transient entry does nothing.
169 if (transient_entry_index_ != -1)
170 return;
171
172 DiscardNonCommittedEntriesInternal();
initial.commit09911bf2008-07-26 23:55:29173 int current_index = GetCurrentEntryIndex();
[email protected]106a0812010-03-18 00:15:12174 // If we are no where, then we can't reload. TODO(darin): We should add a
175 // CanReload method.
176 if (current_index == -1) {
177 return;
178 }
179
[email protected]106a0812010-03-18 00:15:12180 if (check_for_repost_ && check_for_repost &&
[email protected]a3a1d142008-12-19 00:42:30181 GetEntryAtIndex(current_index)->has_post_data()) {
182 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07183 // they really want to do this. If they do, the dialog will call us back
184 // with check_for_repost = false.
[email protected]965bb092010-04-09 11:59:02185 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27186 content::NOTIFICATION_REPOST_WARNING_SHOWN,
[email protected]965bb092010-04-09 11:59:02187 Source<NavigationController>(this),
188 NotificationService::NoDetails());
189
[email protected]106a0812010-03-18 00:15:12190 pending_reload_ = reload_type;
[email protected]9423d9412009-04-14 22:13:55191 tab_contents_->Activate();
[email protected]14f3408a2009-08-31 20:53:53192 tab_contents_->delegate()->ShowRepostFormWarningDialog(tab_contents_);
initial.commit09911bf2008-07-26 23:55:29193 } else {
[email protected]cbab76d2008-10-13 22:42:47194 DiscardNonCommittedEntriesInternal();
[email protected]765b35502008-08-21 00:51:20195
196 pending_entry_index_ = current_index;
[email protected]1e5645ff2008-08-27 18:09:07197 entries_[pending_entry_index_]->set_transition_type(PageTransition::RELOAD);
[email protected]1ccb3568d2010-02-19 10:51:16198 NavigateToPendingEntry(reload_type);
initial.commit09911bf2008-07-26 23:55:29199 }
200}
201
[email protected]106a0812010-03-18 00:15:12202void NavigationController::CancelPendingReload() {
203 DCHECK(pending_reload_ != NO_RELOAD);
204 pending_reload_ = NO_RELOAD;
205}
206
207void NavigationController::ContinuePendingReload() {
208 if (pending_reload_ == NO_RELOAD) {
209 NOTREACHED();
210 } else {
211 ReloadInternal(false, pending_reload_);
[email protected]965bb092010-04-09 11:59:02212 pending_reload_ = NO_RELOAD;
[email protected]106a0812010-03-18 00:15:12213 }
214}
215
[email protected]c70f9b82010-04-21 07:31:11216bool NavigationController::IsInitialNavigation() {
217 return last_document_loaded_.is_null();
218}
219
[email protected]b6ea7412010-05-04 23:26:47220// static
221NavigationEntry* NavigationController::CreateNavigationEntry(
222 const GURL& url, const GURL& referrer, PageTransition::Type transition,
223 Profile* profile) {
224 // Allow the browser URL handler to rewrite the URL. This will, for example,
225 // remove "view-source:" from the beginning of the URL to get the URL that
226 // will actually be loaded. This real URL won't be shown to the user, just
227 // used internally.
228 GURL loaded_url(url);
229 bool reverse_on_redirect = false;
[email protected]f1eb87a2011-05-06 17:49:41230 BrowserURLHandler::GetInstance()->RewriteURLIfNecessary(
[email protected]b6ea7412010-05-04 23:26:47231 &loaded_url, profile, &reverse_on_redirect);
232
233 NavigationEntry* entry = new NavigationEntry(
234 NULL, // The site instance for tabs is sent on navigation
235 // (TabContents::GetSiteInstance).
236 -1,
237 loaded_url,
238 referrer,
[email protected]6b2f7a82011-04-25 19:30:51239 string16(),
[email protected]b6ea7412010-05-04 23:26:47240 transition);
241 entry->set_virtual_url(url);
242 entry->set_user_typed_url(url);
243 entry->set_update_virtual_url_with_url(reverse_on_redirect);
[email protected]b6ea7412010-05-04 23:26:47244 return entry;
245}
246
[email protected]765b35502008-08-21 00:51:20247NavigationEntry* NavigationController::GetEntryWithPageID(
[email protected]7f0005a2009-04-15 03:25:11248 SiteInstance* instance, int32 page_id) const {
249 int index = GetEntryIndexWithPageID(instance, page_id);
[email protected]765b35502008-08-21 00:51:20250 return (index != -1) ? entries_[index].get() : NULL;
251}
252
253void NavigationController::LoadEntry(NavigationEntry* entry) {
[email protected]419a0572011-04-18 22:21:46254 // Don't navigate to URLs disabled by policy. This prevents showing the URL
255 // on the Omnibar when it is also going to be blocked by
256 // ChildProcessSecurityPolicy::CanRequestURL.
257 ChildProcessSecurityPolicy *policy =
258 ChildProcessSecurityPolicy::GetInstance();
259 if (policy->IsDisabledScheme(entry->url().scheme()) ||
260 policy->IsDisabledScheme(entry->virtual_url().scheme())) {
261 VLOG(1) << "URL not loaded because the scheme is blocked by policy: "
262 << entry->url();
263 delete entry;
264 return;
265 }
266
[email protected]765b35502008-08-21 00:51:20267 // When navigating to a new page, we don't know for sure if we will actually
268 // end up leaving the current page. The new page load could for example
269 // result in a download or a 'no content' response (e.g., a mailto: URL).
[email protected]cbab76d2008-10-13 22:42:47270 DiscardNonCommittedEntriesInternal();
[email protected]765b35502008-08-21 00:51:20271 pending_entry_ = entry;
272 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27273 content::NOTIFICATION_NAV_ENTRY_PENDING,
[email protected]765b35502008-08-21 00:51:20274 Source<NavigationController>(this),
275 NotificationService::NoDetails());
[email protected]1ccb3568d2010-02-19 10:51:16276 NavigateToPendingEntry(NO_RELOAD);
[email protected]765b35502008-08-21 00:51:20277}
278
[email protected]765b35502008-08-21 00:51:20279NavigationEntry* NavigationController::GetActiveEntry() const {
[email protected]cbab76d2008-10-13 22:42:47280 if (transient_entry_index_ != -1)
281 return entries_[transient_entry_index_].get();
282 if (pending_entry_)
283 return pending_entry_;
284 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20285}
286
287int NavigationController::GetCurrentEntryIndex() const {
[email protected]cbab76d2008-10-13 22:42:47288 if (transient_entry_index_ != -1)
289 return transient_entry_index_;
[email protected]765b35502008-08-21 00:51:20290 if (pending_entry_index_ != -1)
291 return pending_entry_index_;
292 return last_committed_entry_index_;
293}
294
295NavigationEntry* NavigationController::GetLastCommittedEntry() const {
296 if (last_committed_entry_index_ == -1)
297 return NULL;
298 return entries_[last_committed_entry_index_].get();
299}
300
[email protected]31682282010-01-15 18:05:16301bool NavigationController::CanViewSource() const {
302 bool is_supported_mime_type = net::IsSupportedNonImageMimeType(
303 tab_contents_->contents_mime_type().c_str());
304 NavigationEntry* active_entry = GetActiveEntry();
305 return active_entry && !active_entry->IsViewSourceMode() &&
306 is_supported_mime_type;
307}
308
[email protected]765b35502008-08-21 00:51:20309NavigationEntry* NavigationController::GetEntryAtOffset(int offset) const {
[email protected]cbab76d2008-10-13 22:42:47310 int index = (transient_entry_index_ != -1) ?
311 transient_entry_index_ + offset :
312 last_committed_entry_index_ + offset;
[email protected]7f0005a2009-04-15 03:25:11313 if (index < 0 || index >= entry_count())
[email protected]765b35502008-08-21 00:51:20314 return NULL;
315
316 return entries_[index].get();
317}
318
[email protected]765b35502008-08-21 00:51:20319bool NavigationController::CanGoBack() const {
320 return entries_.size() > 1 && GetCurrentEntryIndex() > 0;
321}
322
323bool NavigationController::CanGoForward() const {
324 int index = GetCurrentEntryIndex();
325 return index >= 0 && index < (static_cast<int>(entries_.size()) - 1);
326}
327
328void NavigationController::GoBack() {
329 if (!CanGoBack()) {
330 NOTREACHED();
331 return;
332 }
333
[email protected]25396da2010-03-11 19:19:10334 // If an interstitial page is showing, going back is equivalent to hiding the
335 // interstitial.
336 if (tab_contents_->interstitial_page()) {
337 tab_contents_->interstitial_page()->DontProceed();
338 return;
339 }
340
[email protected]765b35502008-08-21 00:51:20341 // Base the navigation on where we are now...
342 int current_index = GetCurrentEntryIndex();
343
[email protected]cbab76d2008-10-13 22:42:47344 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20345
346 pending_entry_index_ = current_index - 1;
[email protected]784688a62010-09-13 07:06:52347 entries_[pending_entry_index_]->set_transition_type(
348 entries_[pending_entry_index_]->transition_type() |
349 PageTransition::FORWARD_BACK);
[email protected]1ccb3568d2010-02-19 10:51:16350 NavigateToPendingEntry(NO_RELOAD);
[email protected]765b35502008-08-21 00:51:20351}
352
353void NavigationController::GoForward() {
354 if (!CanGoForward()) {
355 NOTREACHED();
356 return;
357 }
358
[email protected]25396da2010-03-11 19:19:10359 // If an interstitial page is showing, the previous renderer is blocked and
360 // cannot make new requests. Unblock (and disable) it to allow this
361 // navigation to succeed. The interstitial will stay visible until the
362 // resulting DidNavigate.
363 if (tab_contents_->interstitial_page()) {
364 tab_contents_->interstitial_page()->CancelForNavigation();
365 }
366
[email protected]cbab76d2008-10-13 22:42:47367 bool transient = (transient_entry_index_ != -1);
368
[email protected]765b35502008-08-21 00:51:20369 // Base the navigation on where we are now...
370 int current_index = GetCurrentEntryIndex();
371
[email protected]cbab76d2008-10-13 22:42:47372 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20373
[email protected]cbab76d2008-10-13 22:42:47374 pending_entry_index_ = current_index;
375 // If there was a transient entry, we removed it making the current index
376 // the next page.
377 if (!transient)
378 pending_entry_index_++;
379
[email protected]784688a62010-09-13 07:06:52380 entries_[pending_entry_index_]->set_transition_type(
381 entries_[pending_entry_index_]->transition_type() |
382 PageTransition::FORWARD_BACK);
[email protected]1ccb3568d2010-02-19 10:51:16383 NavigateToPendingEntry(NO_RELOAD);
[email protected]765b35502008-08-21 00:51:20384}
385
386void NavigationController::GoToIndex(int index) {
387 if (index < 0 || index >= static_cast<int>(entries_.size())) {
388 NOTREACHED();
389 return;
390 }
391
[email protected]cbab76d2008-10-13 22:42:47392 if (transient_entry_index_ != -1) {
393 if (index == transient_entry_index_) {
394 // Nothing to do when navigating to the transient.
395 return;
396 }
397 if (index > transient_entry_index_) {
398 // Removing the transient is goint to shift all entries by 1.
399 index--;
400 }
401 }
402
[email protected]25396da2010-03-11 19:19:10403 // If an interstitial page is showing, the previous renderer is blocked and
404 // cannot make new requests.
405 if (tab_contents_->interstitial_page()) {
406 if (index == GetCurrentEntryIndex() - 1) {
407 // Going back one entry is equivalent to hiding the interstitial.
408 tab_contents_->interstitial_page()->DontProceed();
409 return;
410 } else {
411 // Unblock the renderer (and disable the interstitial) to allow this
412 // navigation to succeed. The interstitial will stay visible until the
413 // resulting DidNavigate.
414 tab_contents_->interstitial_page()->CancelForNavigation();
415 }
416 }
417
[email protected]cbab76d2008-10-13 22:42:47418 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20419
420 pending_entry_index_ = index;
[email protected]784688a62010-09-13 07:06:52421 entries_[pending_entry_index_]->set_transition_type(
422 entries_[pending_entry_index_]->transition_type() |
423 PageTransition::FORWARD_BACK);
[email protected]1ccb3568d2010-02-19 10:51:16424 NavigateToPendingEntry(NO_RELOAD);
[email protected]765b35502008-08-21 00:51:20425}
426
427void NavigationController::GoToOffset(int offset) {
[email protected]cbab76d2008-10-13 22:42:47428 int index = (transient_entry_index_ != -1) ?
429 transient_entry_index_ + offset :
430 last_committed_entry_index_ + offset;
[email protected]7f0005a2009-04-15 03:25:11431 if (index < 0 || index >= entry_count())
[email protected]765b35502008-08-21 00:51:20432 return;
433
434 GoToIndex(index);
435}
436
[email protected]cbab76d2008-10-13 22:42:47437void NavigationController::RemoveEntryAtIndex(int index,
438 const GURL& default_url) {
[email protected]43032342011-03-21 14:10:31439 bool is_current = index == last_committed_entry_index_;
440 RemoveEntryAtIndexInternal(index);
441 if (is_current) {
[email protected]cbab76d2008-10-13 22:42:47442 // 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 }
[email protected]cbab76d2008-10-13 22:42:47451 }
[email protected]cbab76d2008-10-13 22:42:47452}
453
[email protected]38178a42009-12-17 18:58:32454void NavigationController::UpdateVirtualURLToURL(
455 NavigationEntry* entry, const GURL& new_url) {
456 GURL new_virtual_url(new_url);
[email protected]f1eb87a2011-05-06 17:49:41457 if (BrowserURLHandler::GetInstance()->ReverseURLRewrite(
[email protected]38178a42009-12-17 18:58:32458 &new_virtual_url, entry->virtual_url(), profile_)) {
459 entry->set_virtual_url(new_virtual_url);
460 }
461}
462
[email protected]cbab76d2008-10-13 22:42:47463void NavigationController::AddTransientEntry(NavigationEntry* entry) {
464 // Discard any current transient entry, we can only have one at a time.
465 int index = 0;
466 if (last_committed_entry_index_ != -1)
467 index = last_committed_entry_index_ + 1;
468 DiscardTransientEntry();
469 entries_.insert(entries_.begin() + index, linked_ptr<NavigationEntry>(entry));
[email protected]e1cd5452010-08-26 18:03:25470 transient_entry_index_ = index;
[email protected]93f230e02011-06-01 14:40:00471 tab_contents_->NotifyNavigationStateChanged(kInvalidateAll);
[email protected]cbab76d2008-10-13 22:42:47472}
473
[email protected]c0588052008-10-27 23:01:50474void NavigationController::LoadURL(const GURL& url, const GURL& referrer,
initial.commit09911bf2008-07-26 23:55:29475 PageTransition::Type transition) {
476 // The user initiated a load, we don't need to reload anymore.
477 needs_reload_ = false;
478
[email protected]b6ea7412010-05-04 23:26:47479 NavigationEntry* entry = CreateNavigationEntry(url, referrer, transition,
480 profile_);
initial.commit09911bf2008-07-26 23:55:29481
482 LoadEntry(entry);
483}
484
[email protected]09b8f82f2009-06-16 20:22:11485void NavigationController::DocumentLoadedInFrame() {
486 last_document_loaded_ = base::TimeTicks::Now();
487}
488
[email protected]e9ba4472008-09-14 15:42:43489bool NavigationController::RendererDidNavigate(
490 const ViewHostMsg_FrameNavigate_Params& params,
[email protected]8286f51a2011-05-31 17:39:13491 content::LoadCommittedDetails* details) {
[email protected]4bf3522c2010-08-19 21:00:20492
[email protected]0e8db942008-09-24 21:21:48493 // Save the previous state before we clobber it.
494 if (GetLastCommittedEntry()) {
[email protected]ecd9d8702008-08-28 22:10:17495 details->previous_url = GetLastCommittedEntry()->url();
[email protected]7f0005a2009-04-15 03:25:11496 details->previous_entry_index = last_committed_entry_index();
[email protected]0e8db942008-09-24 21:21:48497 } else {
498 details->previous_url = GURL();
499 details->previous_entry_index = -1;
500 }
[email protected]ecd9d8702008-08-28 22:10:17501
[email protected]49bd30e62011-03-22 20:12:59502 // The pending_entry has no SiteInstance when we are restoring an entry. We
503 // must fill it in here so we can find the entry later by calling
504 // GetEntryIndexWithPageID. In all other cases, the SiteInstance should be
505 // assigned already and we shouldn't change it.
506 if (pending_entry_index_ >= 0 && !pending_entry_->site_instance()) {
507 DCHECK(pending_entry_->restore_type() != NavigationEntry::RESTORE_NONE);
[email protected]9423d9412009-04-14 22:13:55508 pending_entry_->set_site_instance(tab_contents_->GetSiteInstance());
[email protected]5e369672009-11-03 23:48:30509 pending_entry_->set_restore_type(NavigationEntry::RESTORE_NONE);
510 }
[email protected]e9ba4472008-09-14 15:42:43511
[email protected]192d8c5e2010-02-23 07:26:32512 // is_in_page must be computed before the entry gets committed.
513 details->is_in_page = IsURLInPageNavigation(params.url);
514
[email protected]e9ba4472008-09-14 15:42:43515 // Do navigation-type specific actions. These will make and commit an entry.
[email protected]0e8db942008-09-24 21:21:48516 details->type = ClassifyNavigation(params);
[email protected]4bf3522c2010-08-19 21:00:20517
[email protected]0e8db942008-09-24 21:21:48518 switch (details->type) {
519 case NavigationType::NEW_PAGE:
[email protected]befd8d822009-07-01 04:51:47520 RendererDidNavigateToNewPage(params, &(details->did_replace_entry));
[email protected]e9ba4472008-09-14 15:42:43521 break;
[email protected]0e8db942008-09-24 21:21:48522 case NavigationType::EXISTING_PAGE:
[email protected]e9ba4472008-09-14 15:42:43523 RendererDidNavigateToExistingPage(params);
524 break;
[email protected]0e8db942008-09-24 21:21:48525 case NavigationType::SAME_PAGE:
[email protected]e9ba4472008-09-14 15:42:43526 RendererDidNavigateToSamePage(params);
527 break;
[email protected]0e8db942008-09-24 21:21:48528 case NavigationType::IN_PAGE:
[email protected]befd8d822009-07-01 04:51:47529 RendererDidNavigateInPage(params, &(details->did_replace_entry));
[email protected]e9ba4472008-09-14 15:42:43530 break;
[email protected]0e8db942008-09-24 21:21:48531 case NavigationType::NEW_SUBFRAME:
[email protected]e9ba4472008-09-14 15:42:43532 RendererDidNavigateNewSubframe(params);
533 break;
[email protected]0e8db942008-09-24 21:21:48534 case NavigationType::AUTO_SUBFRAME:
[email protected]e9ba4472008-09-14 15:42:43535 if (!RendererDidNavigateAutoSubframe(params))
536 return false;
537 break;
[email protected]0e8db942008-09-24 21:21:48538 case NavigationType::NAV_IGNORE:
[email protected]20d1c992011-04-12 21:17:49539 // If a pending navigation was in progress, this canceled it. We should
540 // discard it and make sure it is removed from the URL bar. After that,
541 // there is nothing we can do with this navigation, so we just return to
[email protected]e9ba4472008-09-14 15:42:43542 // the caller that nothing has happened.
[email protected]20d1c992011-04-12 21:17:49543 if (pending_entry_) {
544 DiscardNonCommittedEntries();
[email protected]93f230e02011-06-01 14:40:00545 tab_contents_->NotifyNavigationStateChanged(
546 TabContents::INVALIDATE_URL);
[email protected]20d1c992011-04-12 21:17:49547 }
[email protected]e9ba4472008-09-14 15:42:43548 return false;
549 default:
550 NOTREACHED();
[email protected]765b35502008-08-21 00:51:20551 }
552
[email protected]e9ba4472008-09-14 15:42:43553 // All committed entries should have nonempty content state so WebKit doesn't
554 // get confused when we go back to them (see the function for details).
[email protected]0f38dc4552011-02-25 11:24:00555 DCHECK(!params.content_state.empty());
556 NavigationEntry* active_entry = GetActiveEntry();
557 active_entry->set_content_state(params.content_state);
[email protected]765b35502008-08-21 00:51:20558
[email protected]49bd30e62011-03-22 20:12:59559 // The active entry's SiteInstance should match our SiteInstance.
560 DCHECK(active_entry->site_instance() == tab_contents_->GetSiteInstance());
561
[email protected]e9ba4472008-09-14 15:42:43562 // Now prep the rest of the details for the notification and broadcast.
[email protected]0f38dc4552011-02-25 11:24:00563 details->entry = active_entry;
[email protected]e9ba4472008-09-14 15:42:43564 details->is_main_frame = PageTransition::IsMainFrame(params.transition);
[email protected]f072d2ce2008-09-17 17:16:24565 details->serialized_security_info = params.security_info;
[email protected]2e39d2e2009-02-19 18:41:31566 details->http_status_code = params.http_status_code;
[email protected]93f230e02011-06-01 14:40:00567 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:29568
[email protected]e9ba4472008-09-14 15:42:43569 return true;
initial.commit09911bf2008-07-26 23:55:29570}
571
[email protected]0e8db942008-09-24 21:21:48572NavigationType::Type NavigationController::ClassifyNavigation(
[email protected]e9ba4472008-09-14 15:42:43573 const ViewHostMsg_FrameNavigate_Params& params) const {
[email protected]e9ba4472008-09-14 15:42:43574 if (params.page_id == -1) {
[email protected]eef9de32009-09-23 17:19:46575 // The renderer generates the page IDs, and so if it gives us the invalid
576 // page ID (-1) we know it didn't actually navigate. This happens in a few
577 // cases:
578 //
579 // - If a page makes a popup navigated to about blank, and then writes
580 // stuff like a subframe navigated to a real page. We'll get the commit
581 // for the subframe, but there won't be any commit for the outer page.
582 //
583 // - We were also getting these for failed loads (for example, bug 21849).
584 // The guess is that we get a "load commit" for the alternate error page,
585 // but that doesn't affect the page ID, so we get the "old" one, which
586 // could be invalid. This can also happen for a cross-site transition
587 // that causes us to swap processes. Then the error page load will be in
588 // a new process with no page IDs ever assigned (and hence a -1 value),
589 // yet the navigation controller still might have previous pages in its
590 // list.
591 //
592 // In these cases, there's nothing we can do with them, so ignore.
[email protected]0e8db942008-09-24 21:21:48593 return NavigationType::NAV_IGNORE;
[email protected]e9ba4472008-09-14 15:42:43594 }
595
[email protected]9423d9412009-04-14 22:13:55596 if (params.page_id > tab_contents_->GetMaxPageID()) {
[email protected]e9ba4472008-09-14 15:42:43597 // Greater page IDs than we've ever seen before are new pages. We may or may
598 // not have a pending entry for the page, and this may or may not be the
599 // main frame.
600 if (PageTransition::IsMainFrame(params.transition))
[email protected]0e8db942008-09-24 21:21:48601 return NavigationType::NEW_PAGE;
[email protected]4c27ba82008-09-24 16:49:09602
603 // When this is a new subframe navigation, we should have a committed page
604 // for which it's a suframe in. This may not be the case when an iframe is
605 // navigated on a popup navigated to about:blank (the iframe would be
606 // written into the popup by script on the main page). For these cases,
607 // there isn't any navigation stuff we can do, so just ignore it.
608 if (!GetLastCommittedEntry())
[email protected]0e8db942008-09-24 21:21:48609 return NavigationType::NAV_IGNORE;
[email protected]4c27ba82008-09-24 16:49:09610
611 // Valid subframe navigation.
[email protected]0e8db942008-09-24 21:21:48612 return NavigationType::NEW_SUBFRAME;
[email protected]e9ba4472008-09-14 15:42:43613 }
614
615 // Now we know that the notification is for an existing page. Find that entry.
616 int existing_entry_index = GetEntryIndexWithPageID(
[email protected]9423d9412009-04-14 22:13:55617 tab_contents_->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:43618 params.page_id);
619 if (existing_entry_index == -1) {
620 // The page was not found. It could have been pruned because of the limit on
621 // back/forward entries (not likely since we'll usually tell it to navigate
622 // to such entries). It could also mean that the renderer is smoking crack.
623 NOTREACHED();
[email protected]0e8db942008-09-24 21:21:48624 return NavigationType::NAV_IGNORE;
[email protected]e9ba4472008-09-14 15:42:43625 }
626 NavigationEntry* existing_entry = entries_[existing_entry_index].get();
627
[email protected]e6035c22010-05-25 16:15:52628 if (!PageTransition::IsMainFrame(params.transition)) {
629 // All manual subframes would get new IDs and were handled above, so we
630 // know this is auto. Since the current page was found in the navigation
631 // entry list, we're guaranteed to have a last committed entry.
632 DCHECK(GetLastCommittedEntry());
633 return NavigationType::AUTO_SUBFRAME;
634 }
635
636 // Anything below here we know is a main frame navigation.
[email protected]e9ba4472008-09-14 15:42:43637 if (pending_entry_ &&
[email protected]e9ba4472008-09-14 15:42:43638 existing_entry != pending_entry_ &&
[email protected]230c0012011-04-08 16:27:24639 pending_entry_->page_id() == -1 &&
640 existing_entry == GetLastCommittedEntry()) {
[email protected]e9ba4472008-09-14 15:42:43641 // In this case, we have a pending entry for a URL but WebCore didn't do a
642 // new navigation. This happens when you press enter in the URL bar to
643 // reload. We will create a pending entry, but WebKit will convert it to
644 // a reload since it's the same page and not create a new entry for it
645 // (the user doesn't want to have a new back/forward entry when they do
[email protected]230c0012011-04-08 16:27:24646 // this). If this matches the last committed entry, we want to just ignore
647 // the pending entry and go back to where we were (the "existing entry").
[email protected]0e8db942008-09-24 21:21:48648 return NavigationType::SAME_PAGE;
[email protected]e9ba4472008-09-14 15:42:43649 }
650
[email protected]fc60f222008-12-18 17:36:54651 // Any toplevel navigations with the same base (minus the reference fragment)
652 // are in-page navigations. We weeded out subframe navigations above. Most of
653 // the time this doesn't matter since WebKit doesn't tell us about subframe
654 // navigations that don't actually navigate, but it can happen when there is
655 // an encoding override (it always sends a navigation request).
656 if (AreURLsInPageNavigation(existing_entry->url(), params.url))
657 return NavigationType::IN_PAGE;
658
[email protected]e9ba4472008-09-14 15:42:43659 // Since we weeded out "new" navigations above, we know this is an existing
[email protected]4c27ba82008-09-24 16:49:09660 // (back/forward) navigation.
[email protected]0e8db942008-09-24 21:21:48661 return NavigationType::EXISTING_PAGE;
[email protected]e9ba4472008-09-14 15:42:43662}
663
[email protected]09b8f82f2009-06-16 20:22:11664bool NavigationController::IsRedirect(
665 const ViewHostMsg_FrameNavigate_Params& params) {
666 // For main frame transition, we judge by params.transition.
667 // Otherwise, by params.redirects.
668 if (PageTransition::IsMainFrame(params.transition)) {
669 return PageTransition::IsRedirect(params.transition);
670 }
671 return params.redirects.size() > 1;
672}
673
[email protected]e9ba4472008-09-14 15:42:43674void NavigationController::RendererDidNavigateToNewPage(
[email protected]befd8d822009-07-01 04:51:47675 const ViewHostMsg_FrameNavigate_Params& params, bool* did_replace_entry) {
[email protected]e9ba4472008-09-14 15:42:43676 NavigationEntry* new_entry;
[email protected]f1eb87a2011-05-06 17:49:41677 bool update_virtual_url;
[email protected]e9ba4472008-09-14 15:42:43678 if (pending_entry_) {
679 // TODO(brettw) this assumes that the pending entry is appropriate for the
680 // new page that was just loaded. I don't think this is necessarily the
[email protected]49bd30e62011-03-22 20:12:59681 // case! We should have some more tracking to know for sure.
[email protected]e9ba4472008-09-14 15:42:43682 new_entry = new NavigationEntry(*pending_entry_);
683
684 // Don't use the page type from the pending entry. Some interstitial page
685 // may have set the type to interstitial. Once we commit, however, the page
686 // type must always be normal.
[email protected]cccd3762010-11-12 18:40:01687 new_entry->set_page_type(NORMAL_PAGE);
[email protected]f1eb87a2011-05-06 17:49:41688 update_virtual_url = new_entry->update_virtual_url_with_url();
[email protected]e9ba4472008-09-14 15:42:43689 } else {
[email protected]b680ad22009-04-15 23:19:42690 new_entry = new NavigationEntry;
[email protected]f1eb87a2011-05-06 17:49:41691 // When navigating to a new page, give the browser URL handler a chance to
692 // update the virtual URL based on the new URL. For example, this is needed
693 // to show chrome://bookmarks/#1 when the bookmarks webui extension changes
694 // the URL.
695 update_virtual_url = true;
[email protected]e9ba4472008-09-14 15:42:43696 }
697
698 new_entry->set_url(params.url);
[email protected]f1eb87a2011-05-06 17:49:41699 if (update_virtual_url)
[email protected]38178a42009-12-17 18:58:32700 UpdateVirtualURLToURL(new_entry, params.url);
[email protected]740fbda2009-02-18 21:38:08701 new_entry->set_referrer(params.referrer);
[email protected]e9ba4472008-09-14 15:42:43702 new_entry->set_page_id(params.page_id);
703 new_entry->set_transition_type(params.transition);
[email protected]9423d9412009-04-14 22:13:55704 new_entry->set_site_instance(tab_contents_->GetSiteInstance());
[email protected]e9ba4472008-09-14 15:42:43705 new_entry->set_has_post_data(params.is_post);
706
[email protected]befd8d822009-07-01 04:51:47707 InsertOrReplaceEntry(new_entry, *did_replace_entry);
[email protected]e9ba4472008-09-14 15:42:43708}
709
710void NavigationController::RendererDidNavigateToExistingPage(
711 const ViewHostMsg_FrameNavigate_Params& params) {
712 // We should only get here for main frame navigations.
713 DCHECK(PageTransition::IsMainFrame(params.transition));
714
715 // This is a back/forward navigation. The existing page for the ID is
[email protected]4c27ba82008-09-24 16:49:09716 // guaranteed to exist by ClassifyNavigation, and we just need to update it
717 // with new information from the renderer.
[email protected]7f0005a2009-04-15 03:25:11718 int entry_index = GetEntryIndexWithPageID(tab_contents_->GetSiteInstance(),
719 params.page_id);
[email protected]e9ba4472008-09-14 15:42:43720 DCHECK(entry_index >= 0 &&
721 entry_index < static_cast<int>(entries_.size()));
722 NavigationEntry* entry = entries_[entry_index].get();
723
724 // The URL may have changed due to redirects. The site instance will normally
725 // be the same except during session restore, when no site instance will be
726 // assigned.
727 entry->set_url(params.url);
[email protected]38178a42009-12-17 18:58:32728 if (entry->update_virtual_url_with_url())
729 UpdateVirtualURLToURL(entry, params.url);
[email protected]e9ba4472008-09-14 15:42:43730 DCHECK(entry->site_instance() == NULL ||
[email protected]9423d9412009-04-14 22:13:55731 entry->site_instance() == tab_contents_->GetSiteInstance());
732 entry->set_site_instance(tab_contents_->GetSiteInstance());
[email protected]e9ba4472008-09-14 15:42:43733
[email protected]d5a49e52010-01-08 03:01:41734 entry->set_has_post_data(params.is_post);
735
[email protected]e9ba4472008-09-14 15:42:43736 // The entry we found in the list might be pending if the user hit
737 // back/forward/reload. This load should commit it (since it's already in the
[email protected]49bd30e62011-03-22 20:12:59738 // list, we can just discard the pending pointer). We should also discard the
739 // pending entry if it corresponds to a different navigation, since that one
740 // is now likely canceled. If it is not canceled, we will treat it as a new
741 // navigation when it arrives, which is also ok.
[email protected]e9ba4472008-09-14 15:42:43742 //
743 // Note that we need to use the "internal" version since we don't want to
744 // actually change any other state, just kill the pointer.
[email protected]49bd30e62011-03-22 20:12:59745 if (pending_entry_)
[email protected]cbab76d2008-10-13 22:42:47746 DiscardNonCommittedEntriesInternal();
[email protected]40bcc302009-03-02 20:50:39747
[email protected]80858db52009-10-15 00:35:18748 // If a transient entry was removed, the indices might have changed, so we
749 // have to query the entry index again.
750 last_committed_entry_index_ =
751 GetEntryIndexWithPageID(tab_contents_->GetSiteInstance(), params.page_id);
[email protected]e9ba4472008-09-14 15:42:43752}
753
754void NavigationController::RendererDidNavigateToSamePage(
755 const ViewHostMsg_FrameNavigate_Params& params) {
756 // This mode implies we have a pending entry that's the same as an existing
[email protected]4c27ba82008-09-24 16:49:09757 // entry for this page ID. This entry is guaranteed to exist by
758 // ClassifyNavigation. All we need to do is update the existing entry.
[email protected]e9ba4472008-09-14 15:42:43759 NavigationEntry* existing_entry = GetEntryWithPageID(
[email protected]9423d9412009-04-14 22:13:55760 tab_contents_->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:43761 params.page_id);
762
763 // We assign the entry's unique ID to be that of the new one. Since this is
764 // always the result of a user action, we want to dismiss infobars, etc. like
765 // a regular user-initiated navigation.
766 existing_entry->set_unique_id(pending_entry_->unique_id());
767
[email protected]a0e69262009-06-03 19:08:48768 // The URL may have changed due to redirects.
[email protected]38178a42009-12-17 18:58:32769 if (existing_entry->update_virtual_url_with_url())
770 UpdateVirtualURLToURL(existing_entry, params.url);
[email protected]a0e69262009-06-03 19:08:48771 existing_entry->set_url(params.url);
772
[email protected]cbab76d2008-10-13 22:42:47773 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:43774}
775
776void NavigationController::RendererDidNavigateInPage(
[email protected]befd8d822009-07-01 04:51:47777 const ViewHostMsg_FrameNavigate_Params& params, bool* did_replace_entry) {
[email protected]e9ba4472008-09-14 15:42:43778 DCHECK(PageTransition::IsMainFrame(params.transition)) <<
779 "WebKit should only tell us about in-page navs for the main frame.";
780 // We're guaranteed to have an entry for this one.
781 NavigationEntry* existing_entry = GetEntryWithPageID(
[email protected]9423d9412009-04-14 22:13:55782 tab_contents_->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:43783 params.page_id);
784
785 // Reference fragment navigation. We're guaranteed to have the last_committed
786 // entry and it will be the same page as the new navigation (minus the
787 // reference fragments, of course).
788 NavigationEntry* new_entry = new NavigationEntry(*existing_entry);
789 new_entry->set_page_id(params.page_id);
[email protected]38178a42009-12-17 18:58:32790 if (new_entry->update_virtual_url_with_url())
791 UpdateVirtualURLToURL(new_entry, params.url);
[email protected]e9ba4472008-09-14 15:42:43792 new_entry->set_url(params.url);
[email protected]ccbe04e2010-03-17 17:58:43793
794 // This replaces the existing entry since the page ID didn't change.
795 *did_replace_entry = true;
796 InsertOrReplaceEntry(new_entry, true);
[email protected]e9ba4472008-09-14 15:42:43797}
798
799void NavigationController::RendererDidNavigateNewSubframe(
800 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]09b8f82f2009-06-16 20:22:11801 if (PageTransition::StripQualifier(params.transition) ==
802 PageTransition::AUTO_SUBFRAME) {
803 // This is not user-initiated. Ignore.
804 return;
805 }
[email protected]09b8f82f2009-06-16 20:22:11806
[email protected]e9ba4472008-09-14 15:42:43807 // Manual subframe navigations just get the current entry cloned so the user
808 // can go back or forward to it. The actual subframe information will be
809 // stored in the page state for each of those entries. This happens out of
810 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:09811 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
812 << "that a last committed entry exists.";
[email protected]e9ba4472008-09-14 15:42:43813 NavigationEntry* new_entry = new NavigationEntry(*GetLastCommittedEntry());
814 new_entry->set_page_id(params.page_id);
[email protected]672eba292009-05-13 13:22:45815 InsertOrReplaceEntry(new_entry, false);
[email protected]e9ba4472008-09-14 15:42:43816}
817
818bool NavigationController::RendererDidNavigateAutoSubframe(
819 const ViewHostMsg_FrameNavigate_Params& params) {
820 // We're guaranteed to have a previously committed entry, and we now need to
821 // handle navigation inside of a subframe in it without creating a new entry.
822 DCHECK(GetLastCommittedEntry());
823
824 // Handle the case where we're navigating back/forward to a previous subframe
825 // navigation entry. This is case "2." in NAV_AUTO_SUBFRAME comment in the
826 // header file. In case "1." this will be a NOP.
827 int entry_index = GetEntryIndexWithPageID(
[email protected]9423d9412009-04-14 22:13:55828 tab_contents_->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:43829 params.page_id);
830 if (entry_index < 0 ||
831 entry_index >= static_cast<int>(entries_.size())) {
832 NOTREACHED();
833 return false;
834 }
835
836 // Update the current navigation entry in case we're going back/forward.
837 if (entry_index != last_committed_entry_index_) {
[email protected]e9ba4472008-09-14 15:42:43838 last_committed_entry_index_ = entry_index;
[email protected]e9ba4472008-09-14 15:42:43839 return true;
840 }
841 return false;
842}
843
[email protected]765b35502008-08-21 00:51:20844int NavigationController::GetIndexOfEntry(
845 const NavigationEntry* entry) const {
846 const NavigationEntries::const_iterator i(std::find(
847 entries_.begin(),
848 entries_.end(),
849 entry));
850 return (i == entries_.end()) ? -1 : static_cast<int>(i - entries_.begin());
851}
852
[email protected]e9ba4472008-09-14 15:42:43853bool NavigationController::IsURLInPageNavigation(const GURL& url) const {
854 NavigationEntry* last_committed = GetLastCommittedEntry();
855 if (!last_committed)
856 return false;
857 return AreURLsInPageNavigation(last_committed->url(), url);
858}
859
[email protected]ce3fa3c2009-04-20 19:55:57860void NavigationController::CopyStateFrom(const NavigationController& source) {
861 // Verify that we look new.
862 DCHECK(entry_count() == 0 && !pending_entry());
863
864 if (source.entry_count() == 0)
865 return; // Nothing new to do.
866
867 needs_reload_ = true;
[email protected]e1cd5452010-08-26 18:03:25868 InsertEntriesFrom(source, source.entry_count());
[email protected]ce3fa3c2009-04-20 19:55:57869
[email protected]6ee12c42010-09-14 09:36:07870 session_storage_namespace_ = source.session_storage_namespace_->Clone();
[email protected]4e6419c2010-01-15 04:50:34871
[email protected]5e369672009-11-03 23:48:30872 FinishRestore(source.last_committed_entry_index_, false);
[email protected]ce3fa3c2009-04-20 19:55:57873}
874
[email protected]43032342011-03-21 14:10:31875void NavigationController::CopyStateFromAndPrune(NavigationController* source,
876 bool remove_first_entry) {
[email protected]e1cd5452010-08-26 18:03:25877 // This code is intended for use when the last entry is the active entry.
878 DCHECK((transient_entry_index_ != -1 &&
879 transient_entry_index_ == entry_count() - 1) ||
880 (pending_entry_ && (pending_entry_index_ == -1 ||
881 pending_entry_index_ == entry_count() - 1)) ||
882 (!pending_entry_ && last_committed_entry_index_ == entry_count() - 1));
883
[email protected]43032342011-03-21 14:10:31884 if (remove_first_entry && entry_count()) {
885 // Save then restore the pending entry (RemoveEntryAtIndexInternal chucks
886 // the pending entry).
887 NavigationEntry* pending_entry = pending_entry_;
888 pending_entry_ = NULL;
889 int pending_entry_index = pending_entry_index_;
890 RemoveEntryAtIndexInternal(0);
891 // Restore the pending entry.
892 if (pending_entry_index != -1) {
893 pending_entry_index_ = pending_entry_index - 1;
894 if (pending_entry_index_ != -1)
895 pending_entry_ = entries_[pending_entry_index_].get();
896 } else if (pending_entry) {
897 pending_entry_ = pending_entry;
898 }
899 }
900
[email protected]e1cd5452010-08-26 18:03:25901 // Remove all the entries leaving the active entry.
902 PruneAllButActive();
903
[email protected]47e020a2010-10-15 14:43:37904 // Insert the entries from source. Don't use source->GetCurrentEntryIndex as
[email protected]e1cd5452010-08-26 18:03:25905 // we don't want to copy over the transient entry.
[email protected]47e020a2010-10-15 14:43:37906 int max_source_index = source->pending_entry_index_ != -1 ?
907 source->pending_entry_index_ : source->last_committed_entry_index_;
[email protected]e1cd5452010-08-26 18:03:25908 if (max_source_index == -1)
[email protected]47e020a2010-10-15 14:43:37909 max_source_index = source->entry_count();
[email protected]e1cd5452010-08-26 18:03:25910 else
911 max_source_index++;
[email protected]47e020a2010-10-15 14:43:37912 InsertEntriesFrom(*source, max_source_index);
[email protected]e1cd5452010-08-26 18:03:25913
914 // Adjust indices such that the last entry and pending are at the end now.
915 last_committed_entry_index_ = entry_count() - 1;
916 if (pending_entry_index_ != -1)
917 pending_entry_index_ = entry_count() - 1;
918 if (transient_entry_index_ != -1) {
919 // There's a transient entry. In this case we want the last committed to
920 // point to the previous entry.
921 transient_entry_index_ = entry_count() - 1;
922 if (last_committed_entry_index_ != -1)
923 last_committed_entry_index_--;
924 }
[email protected]e1cd5452010-08-26 18:03:25925}
926
[email protected]97b6c4f2010-09-27 19:31:26927void NavigationController::PruneAllButActive() {
[email protected]97b6c4f2010-09-27 19:31:26928 if (transient_entry_index_ != -1) {
929 // There is a transient entry. Prune up to it.
930 DCHECK_EQ(entry_count() - 1, transient_entry_index_);
[email protected]77d8d622010-12-15 10:30:12931 entries_.erase(entries_.begin(), entries_.begin() + transient_entry_index_);
[email protected]97b6c4f2010-09-27 19:31:26932 transient_entry_index_ = 0;
933 last_committed_entry_index_ = -1;
934 pending_entry_index_ = -1;
935 } else if (!pending_entry_) {
936 // There's no pending entry. Leave the last entry (if there is one).
[email protected]77d8d622010-12-15 10:30:12937 if (!entry_count())
[email protected]97b6c4f2010-09-27 19:31:26938 return;
939
[email protected]77d8d622010-12-15 10:30:12940 DCHECK(last_committed_entry_index_ >= 0);
941 entries_.erase(entries_.begin(),
942 entries_.begin() + last_committed_entry_index_);
943 entries_.erase(entries_.begin() + 1, entries_.end());
[email protected]97b6c4f2010-09-27 19:31:26944 last_committed_entry_index_ = 0;
945 } else if (pending_entry_index_ != -1) {
[email protected]77d8d622010-12-15 10:30:12946 entries_.erase(entries_.begin(), entries_.begin() + pending_entry_index_);
947 entries_.erase(entries_.begin() + 1, entries_.end());
[email protected]97b6c4f2010-09-27 19:31:26948 pending_entry_index_ = 0;
949 last_committed_entry_index_ = 0;
[email protected]97b6c4f2010-09-27 19:31:26950 } else {
951 // There is a pending_entry, but it's not in entries_.
952 pending_entry_index_ = -1;
953 last_committed_entry_index_ = -1;
[email protected]77d8d622010-12-15 10:30:12954 entries_.clear();
[email protected]97b6c4f2010-09-27 19:31:26955 }
956
957 if (tab_contents_->interstitial_page()) {
958 // Normally the interstitial page hides itself if the user doesn't proceeed.
959 // This would result in showing a NavigationEntry we just removed. Set this
960 // so the interstitial triggers a reload if the user doesn't proceed.
961 tab_contents_->interstitial_page()->set_reload_on_dont_proceed(true);
962 }
[email protected]97b6c4f2010-09-27 19:31:26963}
964
[email protected]43032342011-03-21 14:10:31965void NavigationController::RemoveEntryAtIndexInternal(int index) {
966 DCHECK(index < entry_count());
967
968 DiscardNonCommittedEntries();
969
970 entries_.erase(entries_.begin() + index);
971 if (last_committed_entry_index_ == index)
972 last_committed_entry_index_--;
973 else if (last_committed_entry_index_ > index)
974 last_committed_entry_index_--;
975}
976
[email protected]cbab76d2008-10-13 22:42:47977void NavigationController::DiscardNonCommittedEntries() {
978 bool transient = transient_entry_index_ != -1;
979 DiscardNonCommittedEntriesInternal();
initial.commit09911bf2008-07-26 23:55:29980
[email protected]cbab76d2008-10-13 22:42:47981 // If there was a transient entry, invalidate everything so the new active
982 // entry state is shown.
983 if (transient) {
[email protected]93f230e02011-06-01 14:40:00984 tab_contents_->NotifyNavigationStateChanged(kInvalidateAll);
[email protected]cbab76d2008-10-13 22:42:47985 }
initial.commit09911bf2008-07-26 23:55:29986}
987
[email protected]672eba292009-05-13 13:22:45988void NavigationController::InsertOrReplaceEntry(NavigationEntry* entry,
989 bool replace) {
[email protected]1e5645ff2008-08-27 18:09:07990 DCHECK(entry->transition_type() != PageTransition::AUTO_SUBFRAME);
[email protected]765b35502008-08-21 00:51:20991
992 // Copy the pending entry's unique ID to the committed entry.
993 // I don't know if pending_entry_index_ can be other than -1 here.
994 const NavigationEntry* const pending_entry = (pending_entry_index_ == -1) ?
995 pending_entry_ : entries_[pending_entry_index_].get();
996 if (pending_entry)
997 entry->set_unique_id(pending_entry->unique_id());
998
[email protected]cbab76d2008-10-13 22:42:47999 DiscardNonCommittedEntriesInternal();
[email protected]765b35502008-08-21 00:51:201000
1001 int current_size = static_cast<int>(entries_.size());
1002
[email protected]765b35502008-08-21 00:51:201003 if (current_size > 0) {
[email protected]672eba292009-05-13 13:22:451004 // Prune any entries which are in front of the current entry.
1005 // Also prune the current entry if we are to replace the current entry.
[email protected]47dcbdc2011-05-27 15:08:311006 // last_committed_entry_index_ must be updated here since calls to
1007 // NotifyPrunedEntries() below may re-enter and we must make sure
1008 // last_committed_entry_index_ is not left in an invalid state.
1009 if (replace)
1010 --last_committed_entry_index_;
1011
[email protected]c12bf1a12008-09-17 16:28:491012 int num_pruned = 0;
[email protected]47dcbdc2011-05-27 15:08:311013 while (last_committed_entry_index_ < (current_size - 1)) {
[email protected]c12bf1a12008-09-17 16:28:491014 num_pruned++;
[email protected]765b35502008-08-21 00:51:201015 entries_.pop_back();
1016 current_size--;
1017 }
[email protected]c12bf1a12008-09-17 16:28:491018 if (num_pruned > 0) // Only notify if we did prune something.
1019 NotifyPrunedEntries(this, false, num_pruned);
[email protected]765b35502008-08-21 00:51:201020 }
1021
[email protected]c12bf1a12008-09-17 16:28:491022 if (entries_.size() >= max_entry_count_) {
[email protected]cbab76d2008-10-13 22:42:471023 RemoveEntryAtIndex(0, GURL());
[email protected]c12bf1a12008-09-17 16:28:491024 NotifyPrunedEntries(this, true, 1);
1025 }
[email protected]765b35502008-08-21 00:51:201026
1027 entries_.push_back(linked_ptr<NavigationEntry>(entry));
1028 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
[email protected]e9ba4472008-09-14 15:42:431029
1030 // This is a new page ID, so we need everybody to know about it.
[email protected]9423d9412009-04-14 22:13:551031 tab_contents_->UpdateMaxPageID(entry->page_id());
initial.commit09911bf2008-07-26 23:55:291032}
1033
[email protected]1ccb3568d2010-02-19 10:51:161034void NavigationController::NavigateToPendingEntry(ReloadType reload_type) {
[email protected]72097fd02010-01-21 23:36:011035 needs_reload_ = false;
1036
initial.commit09911bf2008-07-26 23:55:291037 // For session history navigations only the pending_entry_index_ is set.
1038 if (!pending_entry_) {
[email protected]4bf3522c2010-08-19 21:00:201039 DCHECK_NE(pending_entry_index_, -1);
[email protected]765b35502008-08-21 00:51:201040 pending_entry_ = entries_[pending_entry_index_].get();
initial.commit09911bf2008-07-26 23:55:291041 }
1042
[email protected]1ccb3568d2010-02-19 10:51:161043 if (!tab_contents_->NavigateToPendingEntry(reload_type))
[email protected]cbab76d2008-10-13 22:42:471044 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:291045}
1046
[email protected]ecd9d8702008-08-28 22:10:171047void NavigationController::NotifyNavigationEntryCommitted(
[email protected]93f230e02011-06-01 14:40:001048 content::LoadCommittedDetails* details) {
[email protected]df1af242009-05-01 00:11:401049 details->entry = GetActiveEntry();
1050 NotificationDetails notification_details =
[email protected]8286f51a2011-05-31 17:39:131051 Details<content::LoadCommittedDetails>(details);
[email protected]df1af242009-05-01 00:11:401052
1053 // We need to notify the ssl_manager_ before the tab_contents_ so the
1054 // location bar will have up-to-date information about the security style
1055 // when it wants to draw. See http://crbug.com/11157
1056 ssl_manager_.DidCommitProvisionalLoad(notification_details);
1057
initial.commit09911bf2008-07-26 23:55:291058 // TODO(pkasting): http://b/1113079 Probably these explicit notification paths
1059 // should be removed, and interested parties should just listen for the
1060 // notification below instead.
[email protected]93f230e02011-06-01 14:40:001061 tab_contents_->NotifyNavigationStateChanged(kInvalidateAll);
initial.commit09911bf2008-07-26 23:55:291062
[email protected]ecd9d8702008-08-28 22:10:171063 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271064 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
[email protected]ecd9d8702008-08-28 22:10:171065 Source<NavigationController>(this),
[email protected]df1af242009-05-01 00:11:401066 notification_details);
initial.commit09911bf2008-07-26 23:55:291067}
1068
initial.commit09911bf2008-07-26 23:55:291069// static
1070void NavigationController::DisablePromptOnRepost() {
1071 check_for_repost_ = false;
1072}
1073
1074void NavigationController::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:221075 if (is_active && needs_reload_)
1076 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:291077}
1078
1079void NavigationController::LoadIfNecessary() {
1080 if (!needs_reload_)
1081 return;
1082
initial.commit09911bf2008-07-26 23:55:291083 // Calling Reload() results in ignoring state, and not loading.
1084 // Explicitly use NavigateToPendingEntry so that the renderer uses the
1085 // cached state.
1086 pending_entry_index_ = last_committed_entry_index_;
[email protected]1ccb3568d2010-02-19 10:51:161087 NavigateToPendingEntry(NO_RELOAD);
initial.commit09911bf2008-07-26 23:55:291088}
1089
[email protected]534e54b2008-08-13 15:40:091090void NavigationController::NotifyEntryChanged(const NavigationEntry* entry,
1091 int index) {
[email protected]8286f51a2011-05-31 17:39:131092 content::EntryChangedDetails det;
[email protected]534e54b2008-08-13 15:40:091093 det.changed_entry = entry;
1094 det.index = index;
[email protected]432115822011-07-10 15:52:271095 NotificationService::current()->Notify(
1096 content::NOTIFICATION_NAV_ENTRY_CHANGED,
[email protected]8286f51a2011-05-31 17:39:131097 Source<NavigationController>(this),
1098 Details<content::EntryChangedDetails>(&det));
initial.commit09911bf2008-07-26 23:55:291099}
1100
[email protected]5e369672009-11-03 23:48:301101void NavigationController::FinishRestore(int selected_index,
1102 bool from_last_session) {
[email protected]7f0005a2009-04-15 03:25:111103 DCHECK(selected_index >= 0 && selected_index < entry_count());
[email protected]5e369672009-11-03 23:48:301104 ConfigureEntriesForRestore(&entries_, from_last_session);
initial.commit09911bf2008-07-26 23:55:291105
[email protected]61398152010-08-26 21:45:341106 set_max_restored_page_id(static_cast<int32>(entry_count()));
initial.commit09911bf2008-07-26 23:55:291107
1108 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:291109}
[email protected]765b35502008-08-21 00:51:201110
[email protected]cbab76d2008-10-13 22:42:471111void NavigationController::DiscardNonCommittedEntriesInternal() {
[email protected]765b35502008-08-21 00:51:201112 if (pending_entry_index_ == -1)
1113 delete pending_entry_;
1114 pending_entry_ = NULL;
1115 pending_entry_index_ = -1;
[email protected]cbab76d2008-10-13 22:42:471116
1117 DiscardTransientEntry();
1118}
1119
1120void NavigationController::DiscardTransientEntry() {
1121 if (transient_entry_index_ == -1)
1122 return;
[email protected]a0e69262009-06-03 19:08:481123 entries_.erase(entries_.begin() + transient_entry_index_);
[email protected]80858db52009-10-15 00:35:181124 if (last_committed_entry_index_ > transient_entry_index_)
1125 last_committed_entry_index_--;
[email protected]cbab76d2008-10-13 22:42:471126 transient_entry_index_ = -1;
[email protected]765b35502008-08-21 00:51:201127}
1128
1129int NavigationController::GetEntryIndexWithPageID(
[email protected]7f0005a2009-04-15 03:25:111130 SiteInstance* instance, int32 page_id) const {
[email protected]765b35502008-08-21 00:51:201131 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
[email protected]7f0005a2009-04-15 03:25:111132 if ((entries_[i]->site_instance() == instance) &&
[email protected]1e5645ff2008-08-27 18:09:071133 (entries_[i]->page_id() == page_id))
[email protected]765b35502008-08-21 00:51:201134 return i;
1135 }
1136 return -1;
1137}
[email protected]cbab76d2008-10-13 22:42:471138
1139NavigationEntry* NavigationController::GetTransientEntry() const {
1140 if (transient_entry_index_ == -1)
1141 return NULL;
1142 return entries_[transient_entry_index_].get();
1143}
[email protected]e1cd5452010-08-26 18:03:251144
[email protected]e1cd5452010-08-26 18:03:251145void NavigationController::InsertEntriesFrom(
1146 const NavigationController& source,
1147 int max_index) {
1148 DCHECK_LE(max_index, source.entry_count());
1149 size_t insert_index = 0;
1150 for (int i = 0; i < max_index; i++) {
1151 // When cloning a tab, copy all entries except interstitial pages
[email protected]cccd3762010-11-12 18:40:011152 if (source.entries_[i].get()->page_type() != INTERSTITIAL_PAGE) {
[email protected]e1cd5452010-08-26 18:03:251153 entries_.insert(entries_.begin() + insert_index++,
1154 linked_ptr<NavigationEntry>(
1155 new NavigationEntry(*source.entries_[i])));
1156 }
1157 }
1158}