blob: 82210edbdc34d79472f14c4bed7c616b320d4475 [file] [log] [blame]
[email protected]ee613922009-09-02 20:38:221// Copyright (c) 2009 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]f3ec7742009-01-15 00:59:165#include "chrome/browser/tab_contents/navigation_controller.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]9929da92009-05-05 02:05:117#include "app/resource_bundle.h"
initial.commit09911bf2008-07-26 23:55:298#include "base/file_util.h"
9#include "base/logging.h"
10#include "base/string_util.h"
[email protected]b689fce72009-03-17 22:45:3411#include "base/time.h"
[email protected]cd3d7892009-03-04 23:55:0612#include "chrome/browser/browser_about_handler.h"
initial.commit09911bf2008-07-26 23:55:2913#include "chrome/browser/browser_process.h"
[email protected]9423d9412009-04-14 22:13:5514#include "chrome/browser/browser_url_handler.h"
[email protected]ce560f82009-06-03 09:39:4415#include "chrome/browser/profile.h"
[email protected]14e60c8d2009-06-29 03:56:5116#include "chrome/browser/renderer_host/site_instance.h"
[email protected]169627b2008-12-06 19:30:1917#include "chrome/browser/sessions/session_types.h"
[email protected]f3ec7742009-01-15 00:59:1618#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]5c238752009-06-13 10:29:0719#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]14f3408a2009-08-31 20:53:5320#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]299dabd2008-11-19 02:27:1621#include "chrome/common/navigation_types.h"
[email protected]bfd04a62009-02-01 18:16:5622#include "chrome/common/notification_service.h"
[email protected]a23de8572009-06-03 02:16:3223#include "chrome/common/pref_names.h"
24#include "chrome/common/pref_service.h"
[email protected]e09ba552009-02-05 03:26:2925#include "chrome/common/render_messages.h"
[email protected]6de74452009-02-25 18:04:5926#include "chrome/common/url_constants.h"
[email protected]074f10562009-05-21 22:40:0527#include "grit/app_resources.h"
[email protected]a23de8572009-06-03 02:16:3228#include "net/base/escape.h"
29#include "net/base/net_util.h"
[email protected]765b35502008-08-21 00:51:2030#include "webkit/glue/webkit_glue.h"
initial.commit09911bf2008-07-26 23:55:2931
[email protected]e9ba4472008-09-14 15:42:4332namespace {
33
34// Invoked when entries have been pruned, or removed. For example, if the
35// current entries are [google, digg, yahoo], with the current entry google,
36// and the user types in cnet, then digg and yahoo are pruned.
[email protected]c12bf1a12008-09-17 16:28:4937void NotifyPrunedEntries(NavigationController* nav_controller,
38 bool from_front,
39 int count) {
40 NavigationController::PrunedDetails details;
41 details.from_front = from_front;
42 details.count = count;
[email protected]e9ba4472008-09-14 15:42:4343 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:5644 NotificationType::NAV_LIST_PRUNED,
[email protected]e9ba4472008-09-14 15:42:4345 Source<NavigationController>(nav_controller),
[email protected]c12bf1a12008-09-17 16:28:4946 Details<NavigationController::PrunedDetails>(&details));
[email protected]e9ba4472008-09-14 15:42:4347}
48
49// Ensure the given NavigationEntry has a valid state, so that WebKit does not
50// get confused if we navigate back to it.
[email protected]40bcc302009-03-02 20:50:3951//
[email protected]e9ba4472008-09-14 15:42:4352// An empty state is treated as a new navigation by WebKit, which would mean
53// losing the navigation entries and generating a new navigation entry after
54// this one. We don't want that. To avoid this we create a valid state which
55// WebKit will not treat as a new navigation.
56void SetContentStateIfEmpty(NavigationEntry* entry) {
[email protected]965524b2009-04-04 21:32:4057 if (entry->content_state().empty()) {
[email protected]e9ba4472008-09-14 15:42:4358 entry->set_content_state(
59 webkit_glue::CreateHistoryStateForURL(entry->url()));
60 }
61}
62
63// Configure all the NavigationEntries in entries for restore. This resets
64// the transition type to reload and makes sure the content state isn't empty.
65void ConfigureEntriesForRestore(
66 std::vector<linked_ptr<NavigationEntry> >* entries) {
67 for (size_t i = 0; i < entries->size(); ++i) {
68 // Use a transition type of reload so that we don't incorrectly increase
69 // the typed count.
70 (*entries)[i]->set_transition_type(PageTransition::RELOAD);
71 (*entries)[i]->set_restored(true);
72 // NOTE(darin): This code is only needed for backwards compat.
73 SetContentStateIfEmpty((*entries)[i].get());
74 }
75}
76
77// See NavigationController::IsURLInPageNavigation for how this works and why.
78bool AreURLsInPageNavigation(const GURL& existing_url, const GURL& new_url) {
79 if (existing_url == new_url || !new_url.has_ref())
80 return false;
81
82 url_canon::Replacements<char> replacements;
83 replacements.ClearRef();
84 return existing_url.ReplaceComponents(replacements) ==
85 new_url.ReplaceComponents(replacements);
86}
87
[email protected]09b8f82f2009-06-16 20:22:1188// Navigation within this limit since the last document load is considered to
89// be automatic (i.e., machine-initiated) rather than user-initiated unless
90// a user gesture has been observed.
91const base::TimeDelta kMaxAutoNavigationTimeDelta =
92 base::TimeDelta::FromSeconds(5);
93
[email protected]e9ba4472008-09-14 15:42:4394} // namespace
95
initial.commit09911bf2008-07-26 23:55:2996// NavigationController ---------------------------------------------------
97
[email protected]765b35502008-08-21 00:51:2098// static
[email protected]c12bf1a12008-09-17 16:28:4999size_t NavigationController::max_entry_count_ = 50;
[email protected]765b35502008-08-21 00:51:20100
initial.commit09911bf2008-07-26 23:55:29101// static
102bool NavigationController::check_for_repost_ = true;
103
104// Creates a new NavigationEntry for each TabNavigation in navigations, adding
105// the NavigationEntry to entries. This is used during session restore.
106static void CreateNavigationEntriesFromTabNavigations(
107 const std::vector<TabNavigation>& navigations,
[email protected]765b35502008-08-21 00:51:20108 std::vector<linked_ptr<NavigationEntry> >* entries) {
initial.commit09911bf2008-07-26 23:55:29109 // Create a NavigationEntry for each of the navigations.
[email protected]169627b2008-12-06 19:30:19110 int page_id = 0;
initial.commit09911bf2008-07-26 23:55:29111 for (std::vector<TabNavigation>::const_iterator i =
[email protected]169627b2008-12-06 19:30:19112 navigations.begin(); i != navigations.end(); ++i, ++page_id) {
113 entries->push_back(
114 linked_ptr<NavigationEntry>(i->ToNavigationEntry(page_id)));
initial.commit09911bf2008-07-26 23:55:29115 }
116}
117
initial.commit09911bf2008-07-26 23:55:29118NavigationController::NavigationController(TabContents* contents,
119 Profile* profile)
120 : profile_(profile),
[email protected]765b35502008-08-21 00:51:20121 pending_entry_(NULL),
122 last_committed_entry_index_(-1),
123 pending_entry_index_(-1),
[email protected]cbab76d2008-10-13 22:42:47124 transient_entry_index_(-1),
[email protected]9423d9412009-04-14 22:13:55125 tab_contents_(contents),
initial.commit09911bf2008-07-26 23:55:29126 max_restored_page_id_(-1),
[email protected]5d063842009-05-15 04:08:24127 ALLOW_THIS_IN_INITIALIZER_LIST(ssl_manager_(this)),
[email protected]38b8f4e2009-09-24 19:44:57128 needs_reload_(false),
129 user_gesture_observed_(false) {
initial.commit09911bf2008-07-26 23:55:29130 DCHECK(profile_);
initial.commit09911bf2008-07-26 23:55:29131}
132
initial.commit09911bf2008-07-26 23:55:29133NavigationController::~NavigationController() {
[email protected]cbab76d2008-10-13 22:42:47134 DiscardNonCommittedEntriesInternal();
[email protected]c0993872008-08-21 19:59:44135
[email protected]bfd04a62009-02-01 18:16:56136 NotificationService::current()->Notify(
137 NotificationType::TAB_CLOSED,
138 Source<NavigationController>(this),
139 NotificationService::NoDetails());
initial.commit09911bf2008-07-26 23:55:29140}
141
[email protected]ce3fa3c2009-04-20 19:55:57142void NavigationController::RestoreFromState(
143 const std::vector<TabNavigation>& navigations,
144 int selected_navigation) {
145 // Verify that this controller is unused and that the input is valid.
146 DCHECK(entry_count() == 0 && !pending_entry());
147 DCHECK(selected_navigation >= 0 &&
148 selected_navigation < static_cast<int>(navigations.size()));
149
150 // Populate entries_ from the supplied TabNavigations.
151 needs_reload_ = true;
152 CreateNavigationEntriesFromTabNavigations(navigations, &entries_);
153
154 // And finish the restore.
155 FinishRestore(selected_navigation);
156}
157
[email protected]f1c74112008-10-30 16:17:04158void NavigationController::Reload(bool check_for_repost) {
[email protected]cbab76d2008-10-13 22:42:47159 // Reloading a transient entry does nothing.
160 if (transient_entry_index_ != -1)
161 return;
162
163 DiscardNonCommittedEntriesInternal();
initial.commit09911bf2008-07-26 23:55:29164 int current_index = GetCurrentEntryIndex();
[email protected]f1c74112008-10-30 16:17:04165 if (check_for_repost_ && check_for_repost && current_index != -1 &&
[email protected]a3a1d142008-12-19 00:42:30166 GetEntryAtIndex(current_index)->has_post_data()) {
167 // The user is asking to reload a page with POST data. Prompt to make sure
[email protected]b5bb35f2009-02-05 20:17:07168 // they really want to do this. If they do, the dialog will call us back
169 // with check_for_repost = false.
[email protected]9423d9412009-04-14 22:13:55170 tab_contents_->Activate();
[email protected]14f3408a2009-08-31 20:53:53171 tab_contents_->delegate()->ShowRepostFormWarningDialog(tab_contents_);
initial.commit09911bf2008-07-26 23:55:29172 } else {
[email protected]765b35502008-08-21 00:51:20173 // Base the navigation on where we are now...
174 int current_index = GetCurrentEntryIndex();
175
176 // 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
[email protected]cbab76d2008-10-13 22:42:47181 DiscardNonCommittedEntriesInternal();
[email protected]765b35502008-08-21 00:51:20182
183 pending_entry_index_ = current_index;
[email protected]1e5645ff2008-08-27 18:09:07184 entries_[pending_entry_index_]->set_transition_type(PageTransition::RELOAD);
[email protected]765b35502008-08-21 00:51:20185 NavigateToPendingEntry(true);
initial.commit09911bf2008-07-26 23:55:29186 }
187}
188
[email protected]765b35502008-08-21 00:51:20189NavigationEntry* NavigationController::GetEntryWithPageID(
[email protected]7f0005a2009-04-15 03:25:11190 SiteInstance* instance, int32 page_id) const {
191 int index = GetEntryIndexWithPageID(instance, page_id);
[email protected]765b35502008-08-21 00:51:20192 return (index != -1) ? entries_[index].get() : NULL;
193}
194
195void NavigationController::LoadEntry(NavigationEntry* entry) {
[email protected]cd3d7892009-03-04 23:55:06196 // Handle non-navigational URLs that popup dialogs and such, these should not
197 // actually navigate.
198 if (HandleNonNavigationAboutURL(entry->url()))
199 return;
200
[email protected]765b35502008-08-21 00:51:20201 // When navigating to a new page, we don't know for sure if we will actually
202 // end up leaving the current page. The new page load could for example
203 // result in a download or a 'no content' response (e.g., a mailto: URL).
[email protected]cbab76d2008-10-13 22:42:47204 DiscardNonCommittedEntriesInternal();
[email protected]765b35502008-08-21 00:51:20205 pending_entry_ = entry;
206 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56207 NotificationType::NAV_ENTRY_PENDING,
[email protected]765b35502008-08-21 00:51:20208 Source<NavigationController>(this),
209 NotificationService::NoDetails());
210 NavigateToPendingEntry(false);
211}
212
[email protected]765b35502008-08-21 00:51:20213NavigationEntry* NavigationController::GetActiveEntry() const {
[email protected]cbab76d2008-10-13 22:42:47214 if (transient_entry_index_ != -1)
215 return entries_[transient_entry_index_].get();
216 if (pending_entry_)
217 return pending_entry_;
218 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20219}
220
221int NavigationController::GetCurrentEntryIndex() const {
[email protected]cbab76d2008-10-13 22:42:47222 if (transient_entry_index_ != -1)
223 return transient_entry_index_;
[email protected]765b35502008-08-21 00:51:20224 if (pending_entry_index_ != -1)
225 return pending_entry_index_;
226 return last_committed_entry_index_;
227}
228
229NavigationEntry* NavigationController::GetLastCommittedEntry() const {
230 if (last_committed_entry_index_ == -1)
231 return NULL;
232 return entries_[last_committed_entry_index_].get();
233}
234
235NavigationEntry* NavigationController::GetEntryAtOffset(int offset) const {
[email protected]cbab76d2008-10-13 22:42:47236 int index = (transient_entry_index_ != -1) ?
237 transient_entry_index_ + offset :
238 last_committed_entry_index_ + offset;
[email protected]7f0005a2009-04-15 03:25:11239 if (index < 0 || index >= entry_count())
[email protected]765b35502008-08-21 00:51:20240 return NULL;
241
242 return entries_[index].get();
243}
244
[email protected]765b35502008-08-21 00:51:20245bool NavigationController::CanGoBack() const {
246 return entries_.size() > 1 && GetCurrentEntryIndex() > 0;
247}
248
249bool NavigationController::CanGoForward() const {
250 int index = GetCurrentEntryIndex();
251 return index >= 0 && index < (static_cast<int>(entries_.size()) - 1);
252}
253
254void NavigationController::GoBack() {
255 if (!CanGoBack()) {
256 NOTREACHED();
257 return;
258 }
259
260 // Base the navigation on where we are now...
261 int current_index = GetCurrentEntryIndex();
262
[email protected]cbab76d2008-10-13 22:42:47263 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20264
265 pending_entry_index_ = current_index - 1;
266 NavigateToPendingEntry(false);
267}
268
269void NavigationController::GoForward() {
270 if (!CanGoForward()) {
271 NOTREACHED();
272 return;
273 }
274
[email protected]cbab76d2008-10-13 22:42:47275 bool transient = (transient_entry_index_ != -1);
276
[email protected]765b35502008-08-21 00:51:20277 // Base the navigation on where we are now...
278 int current_index = GetCurrentEntryIndex();
279
[email protected]cbab76d2008-10-13 22:42:47280 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20281
[email protected]cbab76d2008-10-13 22:42:47282 pending_entry_index_ = current_index;
283 // If there was a transient entry, we removed it making the current index
284 // the next page.
285 if (!transient)
286 pending_entry_index_++;
287
[email protected]765b35502008-08-21 00:51:20288 NavigateToPendingEntry(false);
289}
290
291void NavigationController::GoToIndex(int index) {
292 if (index < 0 || index >= static_cast<int>(entries_.size())) {
293 NOTREACHED();
294 return;
295 }
296
[email protected]cbab76d2008-10-13 22:42:47297 if (transient_entry_index_ != -1) {
298 if (index == transient_entry_index_) {
299 // Nothing to do when navigating to the transient.
300 return;
301 }
302 if (index > transient_entry_index_) {
303 // Removing the transient is goint to shift all entries by 1.
304 index--;
305 }
306 }
307
308 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20309
310 pending_entry_index_ = index;
311 NavigateToPendingEntry(false);
312}
313
314void NavigationController::GoToOffset(int offset) {
[email protected]cbab76d2008-10-13 22:42:47315 int index = (transient_entry_index_ != -1) ?
316 transient_entry_index_ + offset :
317 last_committed_entry_index_ + offset;
[email protected]7f0005a2009-04-15 03:25:11318 if (index < 0 || index >= entry_count())
[email protected]765b35502008-08-21 00:51:20319 return;
320
321 GoToIndex(index);
322}
323
[email protected]cbab76d2008-10-13 22:42:47324void NavigationController::RemoveEntryAtIndex(int index,
325 const GURL& default_url) {
326 int size = static_cast<int>(entries_.size());
327 DCHECK(index < size);
328
329 DiscardNonCommittedEntries();
330
331 entries_.erase(entries_.begin() + index);
332
333 if (last_committed_entry_index_ == index) {
334 last_committed_entry_index_--;
335 // We removed the currently shown entry, so we have to load something else.
336 if (last_committed_entry_index_ != -1) {
337 pending_entry_index_ = last_committed_entry_index_;
338 NavigateToPendingEntry(false);
339 } else {
340 // If there is nothing to show, show a default page.
[email protected]ed3456f2009-02-26 20:24:48341 LoadURL(default_url.is_empty() ? GURL("about:blank") : default_url,
[email protected]c0588052008-10-27 23:01:50342 GURL(), PageTransition::START_PAGE);
[email protected]cbab76d2008-10-13 22:42:47343 }
344 } else if (last_committed_entry_index_ > index) {
345 last_committed_entry_index_--;
346 }
[email protected]cbab76d2008-10-13 22:42:47347}
348
initial.commit09911bf2008-07-26 23:55:29349NavigationEntry* NavigationController::CreateNavigationEntry(
[email protected]c0588052008-10-27 23:01:50350 const GURL& url, const GURL& referrer, PageTransition::Type transition) {
[email protected]9423d9412009-04-14 22:13:55351 // Allow the browser URL handler to rewrite the URL. This will, for example,
352 // remove "view-source:" from the beginning of the URL to get the URL that
353 // will actually be loaded. This real URL won't be shown to the user, just
354 // used internally.
[email protected]9423d9412009-04-14 22:13:55355 GURL loaded_url(url);
[email protected]86c008e82009-08-28 20:26:05356 BrowserURLHandler::RewriteURLIfNecessary(&loaded_url, profile_);
[email protected]d3216442009-03-05 21:07:27357
[email protected]b680ad22009-04-15 23:19:42358 NavigationEntry* entry = new NavigationEntry(NULL, -1, loaded_url, referrer,
[email protected]4c4d8d22009-03-04 05:29:27359 string16(), transition);
[email protected]ebe89e062009-08-13 23:16:54360 entry->set_virtual_url(url);
[email protected]1e5645ff2008-08-27 18:09:07361 entry->set_user_typed_url(url);
[email protected]8654b682008-11-01 23:36:17362 if (url.SchemeIsFile()) {
[email protected]a23de8572009-06-03 02:16:32363 std::wstring languages = profile()->GetPrefs()->GetString(
364 prefs::kAcceptLanguages);
[email protected]4c4d8d22009-03-04 05:29:27365 entry->set_title(WideToUTF16Hack(
[email protected]a23de8572009-06-03 02:16:32366 file_util::GetFilenameFromPath(net::FormatUrl(url, languages))));
[email protected]8654b682008-11-01 23:36:17367 }
initial.commit09911bf2008-07-26 23:55:29368 return entry;
369}
370
[email protected]cbab76d2008-10-13 22:42:47371void NavigationController::AddTransientEntry(NavigationEntry* entry) {
372 // Discard any current transient entry, we can only have one at a time.
373 int index = 0;
374 if (last_committed_entry_index_ != -1)
375 index = last_committed_entry_index_ + 1;
376 DiscardTransientEntry();
377 entries_.insert(entries_.begin() + index, linked_ptr<NavigationEntry>(entry));
378 transient_entry_index_ = index;
[email protected]9423d9412009-04-14 22:13:55379 tab_contents_->NotifyNavigationStateChanged(
[email protected]cbab76d2008-10-13 22:42:47380 TabContents::INVALIDATE_EVERYTHING);
381}
382
[email protected]c0588052008-10-27 23:01:50383void NavigationController::LoadURL(const GURL& url, const GURL& referrer,
initial.commit09911bf2008-07-26 23:55:29384 PageTransition::Type transition) {
385 // The user initiated a load, we don't need to reload anymore.
386 needs_reload_ = false;
387
[email protected]c0588052008-10-27 23:01:50388 NavigationEntry* entry = CreateNavigationEntry(url, referrer, transition);
initial.commit09911bf2008-07-26 23:55:29389
390 LoadEntry(entry);
391}
392
[email protected]09b8f82f2009-06-16 20:22:11393void NavigationController::DocumentLoadedInFrame() {
394 last_document_loaded_ = base::TimeTicks::Now();
395}
396
397void NavigationController::OnUserGesture() {
398 user_gesture_observed_ = true;
399}
400
[email protected]e9ba4472008-09-14 15:42:43401bool NavigationController::RendererDidNavigate(
402 const ViewHostMsg_FrameNavigate_Params& params,
[email protected]e9ba4472008-09-14 15:42:43403 LoadCommittedDetails* details) {
[email protected]0e8db942008-09-24 21:21:48404 // Save the previous state before we clobber it.
405 if (GetLastCommittedEntry()) {
[email protected]ecd9d8702008-08-28 22:10:17406 details->previous_url = GetLastCommittedEntry()->url();
[email protected]7f0005a2009-04-15 03:25:11407 details->previous_entry_index = last_committed_entry_index();
[email protected]0e8db942008-09-24 21:21:48408 } else {
409 details->previous_url = GURL();
410 details->previous_entry_index = -1;
411 }
[email protected]ecd9d8702008-08-28 22:10:17412
[email protected]e9ba4472008-09-14 15:42:43413 // Assign the current site instance to any pending entry, so we can find it
414 // later by calling GetEntryIndexWithPageID. We only care about this if the
415 // pending entry is an existing navigation and not a new one (or else we
416 // wouldn't care about finding it with GetEntryIndexWithPageID).
417 //
418 // TODO(brettw) this seems slightly bogus as we don't really know if the
419 // pending entry is what this navigation is for. There is a similar TODO
420 // w.r.t. the pending entry in RendererDidNavigateToNewPage.
421 if (pending_entry_index_ >= 0)
[email protected]9423d9412009-04-14 22:13:55422 pending_entry_->set_site_instance(tab_contents_->GetSiteInstance());
[email protected]e9ba4472008-09-14 15:42:43423
424 // Do navigation-type specific actions. These will make and commit an entry.
[email protected]0e8db942008-09-24 21:21:48425 details->type = ClassifyNavigation(params);
426 switch (details->type) {
427 case NavigationType::NEW_PAGE:
[email protected]befd8d822009-07-01 04:51:47428 RendererDidNavigateToNewPage(params, &(details->did_replace_entry));
[email protected]e9ba4472008-09-14 15:42:43429 break;
[email protected]0e8db942008-09-24 21:21:48430 case NavigationType::EXISTING_PAGE:
[email protected]e9ba4472008-09-14 15:42:43431 RendererDidNavigateToExistingPage(params);
432 break;
[email protected]0e8db942008-09-24 21:21:48433 case NavigationType::SAME_PAGE:
[email protected]e9ba4472008-09-14 15:42:43434 RendererDidNavigateToSamePage(params);
435 break;
[email protected]0e8db942008-09-24 21:21:48436 case NavigationType::IN_PAGE:
[email protected]befd8d822009-07-01 04:51:47437 RendererDidNavigateInPage(params, &(details->did_replace_entry));
[email protected]e9ba4472008-09-14 15:42:43438 break;
[email protected]0e8db942008-09-24 21:21:48439 case NavigationType::NEW_SUBFRAME:
[email protected]e9ba4472008-09-14 15:42:43440 RendererDidNavigateNewSubframe(params);
441 break;
[email protected]0e8db942008-09-24 21:21:48442 case NavigationType::AUTO_SUBFRAME:
[email protected]e9ba4472008-09-14 15:42:43443 if (!RendererDidNavigateAutoSubframe(params))
444 return false;
445 break;
[email protected]0e8db942008-09-24 21:21:48446 case NavigationType::NAV_IGNORE:
[email protected]e9ba4472008-09-14 15:42:43447 // There is nothing we can do with this navigation, so we just return to
448 // the caller that nothing has happened.
449 return false;
450 default:
451 NOTREACHED();
[email protected]765b35502008-08-21 00:51:20452 }
453
[email protected]e9ba4472008-09-14 15:42:43454 // All committed entries should have nonempty content state so WebKit doesn't
455 // get confused when we go back to them (see the function for details).
456 SetContentStateIfEmpty(GetActiveEntry());
[email protected]765b35502008-08-21 00:51:20457
[email protected]e9ba4472008-09-14 15:42:43458 // WebKit doesn't set the "auto" transition on meta refreshes properly (bug
459 // 1051891) so we manually set it for redirects which we normally treat as
460 // "non-user-gestures" where we want to update stuff after navigations.
461 //
462 // Note that the redirect check also checks for a pending entry to
463 // differentiate real redirects from browser initiated navigations to a
464 // redirected entry. This happens when you hit back to go to a page that was
465 // the destination of a redirect, we don't want to treat it as a redirect
466 // even though that's what its transition will be. See bug 1117048.
467 //
468 // TODO(brettw) write a test for this complicated logic.
469 details->is_auto = (PageTransition::IsRedirect(params.transition) &&
[email protected]7f0005a2009-04-15 03:25:11470 !pending_entry()) ||
[email protected]e9ba4472008-09-14 15:42:43471 params.gesture == NavigationGestureAuto;
[email protected]765b35502008-08-21 00:51:20472
[email protected]e9ba4472008-09-14 15:42:43473 // Now prep the rest of the details for the notification and broadcast.
474 details->entry = GetActiveEntry();
475 details->is_in_page = IsURLInPageNavigation(params.url);
476 details->is_main_frame = PageTransition::IsMainFrame(params.transition);
[email protected]f072d2ce2008-09-17 17:16:24477 details->serialized_security_info = params.security_info;
[email protected]8a3422c92008-09-24 17:42:42478 details->is_content_filtered = params.is_content_filtered;
[email protected]2e39d2e2009-02-19 18:41:31479 details->http_status_code = params.http_status_code;
[email protected]ecd9d8702008-08-28 22:10:17480 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:29481
[email protected]09b8f82f2009-06-16 20:22:11482 user_gesture_observed_ = false;
483
[email protected]e9ba4472008-09-14 15:42:43484 return true;
initial.commit09911bf2008-07-26 23:55:29485}
486
[email protected]0e8db942008-09-24 21:21:48487NavigationType::Type NavigationController::ClassifyNavigation(
[email protected]e9ba4472008-09-14 15:42:43488 const ViewHostMsg_FrameNavigate_Params& params) const {
[email protected]e9ba4472008-09-14 15:42:43489 if (params.page_id == -1) {
[email protected]eef9de32009-09-23 17:19:46490 // The renderer generates the page IDs, and so if it gives us the invalid
491 // page ID (-1) we know it didn't actually navigate. This happens in a few
492 // cases:
493 //
494 // - If a page makes a popup navigated to about blank, and then writes
495 // stuff like a subframe navigated to a real page. We'll get the commit
496 // for the subframe, but there won't be any commit for the outer page.
497 //
498 // - We were also getting these for failed loads (for example, bug 21849).
499 // The guess is that we get a "load commit" for the alternate error page,
500 // but that doesn't affect the page ID, so we get the "old" one, which
501 // could be invalid. This can also happen for a cross-site transition
502 // that causes us to swap processes. Then the error page load will be in
503 // a new process with no page IDs ever assigned (and hence a -1 value),
504 // yet the navigation controller still might have previous pages in its
505 // list.
506 //
507 // In these cases, there's nothing we can do with them, so ignore.
[email protected]0e8db942008-09-24 21:21:48508 return NavigationType::NAV_IGNORE;
[email protected]e9ba4472008-09-14 15:42:43509 }
510
[email protected]9423d9412009-04-14 22:13:55511 if (params.page_id > tab_contents_->GetMaxPageID()) {
[email protected]e9ba4472008-09-14 15:42:43512 // Greater page IDs than we've ever seen before are new pages. We may or may
513 // not have a pending entry for the page, and this may or may not be the
514 // main frame.
515 if (PageTransition::IsMainFrame(params.transition))
[email protected]0e8db942008-09-24 21:21:48516 return NavigationType::NEW_PAGE;
[email protected]4c27ba82008-09-24 16:49:09517
518 // When this is a new subframe navigation, we should have a committed page
519 // for which it's a suframe in. This may not be the case when an iframe is
520 // navigated on a popup navigated to about:blank (the iframe would be
521 // written into the popup by script on the main page). For these cases,
522 // there isn't any navigation stuff we can do, so just ignore it.
523 if (!GetLastCommittedEntry())
[email protected]0e8db942008-09-24 21:21:48524 return NavigationType::NAV_IGNORE;
[email protected]4c27ba82008-09-24 16:49:09525
526 // Valid subframe navigation.
[email protected]0e8db942008-09-24 21:21:48527 return NavigationType::NEW_SUBFRAME;
[email protected]e9ba4472008-09-14 15:42:43528 }
529
530 // Now we know that the notification is for an existing page. Find that entry.
531 int existing_entry_index = GetEntryIndexWithPageID(
[email protected]9423d9412009-04-14 22:13:55532 tab_contents_->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:43533 params.page_id);
534 if (existing_entry_index == -1) {
535 // The page was not found. It could have been pruned because of the limit on
536 // back/forward entries (not likely since we'll usually tell it to navigate
537 // to such entries). It could also mean that the renderer is smoking crack.
538 NOTREACHED();
[email protected]0e8db942008-09-24 21:21:48539 return NavigationType::NAV_IGNORE;
[email protected]e9ba4472008-09-14 15:42:43540 }
541 NavigationEntry* existing_entry = entries_[existing_entry_index].get();
542
543 if (pending_entry_ &&
[email protected]e9ba4472008-09-14 15:42:43544 existing_entry != pending_entry_ &&
[email protected]a0e69262009-06-03 19:08:48545 pending_entry_->page_id() == -1) {
[email protected]e9ba4472008-09-14 15:42:43546 // In this case, we have a pending entry for a URL but WebCore didn't do a
547 // new navigation. This happens when you press enter in the URL bar to
548 // reload. We will create a pending entry, but WebKit will convert it to
549 // a reload since it's the same page and not create a new entry for it
550 // (the user doesn't want to have a new back/forward entry when they do
551 // this). In this case, we want to just ignore the pending entry and go
552 // back to where we were (the "existing entry").
[email protected]0e8db942008-09-24 21:21:48553 return NavigationType::SAME_PAGE;
[email protected]e9ba4472008-09-14 15:42:43554 }
555
[email protected]4c27ba82008-09-24 16:49:09556 if (!PageTransition::IsMainFrame(params.transition)) {
557 // All manual subframes would get new IDs and were handled above, so we
558 // know this is auto. Since the current page was found in the navigation
559 // entry list, we're guaranteed to have a last committed entry.
560 DCHECK(GetLastCommittedEntry());
[email protected]0e8db942008-09-24 21:21:48561 return NavigationType::AUTO_SUBFRAME;
[email protected]4c27ba82008-09-24 16:49:09562 }
563
[email protected]fc60f222008-12-18 17:36:54564 // Any toplevel navigations with the same base (minus the reference fragment)
565 // are in-page navigations. We weeded out subframe navigations above. Most of
566 // the time this doesn't matter since WebKit doesn't tell us about subframe
567 // navigations that don't actually navigate, but it can happen when there is
568 // an encoding override (it always sends a navigation request).
569 if (AreURLsInPageNavigation(existing_entry->url(), params.url))
570 return NavigationType::IN_PAGE;
571
[email protected]e9ba4472008-09-14 15:42:43572 // Since we weeded out "new" navigations above, we know this is an existing
[email protected]4c27ba82008-09-24 16:49:09573 // (back/forward) navigation.
[email protected]0e8db942008-09-24 21:21:48574 return NavigationType::EXISTING_PAGE;
[email protected]e9ba4472008-09-14 15:42:43575}
576
[email protected]09b8f82f2009-06-16 20:22:11577bool NavigationController::IsRedirect(
578 const ViewHostMsg_FrameNavigate_Params& params) {
579 // For main frame transition, we judge by params.transition.
580 // Otherwise, by params.redirects.
581 if (PageTransition::IsMainFrame(params.transition)) {
582 return PageTransition::IsRedirect(params.transition);
583 }
584 return params.redirects.size() > 1;
585}
586
587bool NavigationController::IsLikelyAutoNavigation(base::TimeTicks now) {
588 return !user_gesture_observed_ &&
589 (now - last_document_loaded_) < kMaxAutoNavigationTimeDelta;
590}
591
[email protected]e9ba4472008-09-14 15:42:43592void NavigationController::RendererDidNavigateToNewPage(
[email protected]befd8d822009-07-01 04:51:47593 const ViewHostMsg_FrameNavigate_Params& params, bool* did_replace_entry) {
[email protected]e9ba4472008-09-14 15:42:43594 NavigationEntry* new_entry;
595 if (pending_entry_) {
596 // TODO(brettw) this assumes that the pending entry is appropriate for the
597 // new page that was just loaded. I don't think this is necessarily the
598 // case! We should have some more tracking to know for sure. This goes along
599 // with a similar TODO at the top of RendererDidNavigate where we blindly
600 // set the site instance on the pending entry.
601 new_entry = new NavigationEntry(*pending_entry_);
602
603 // Don't use the page type from the pending entry. Some interstitial page
604 // may have set the type to interstitial. Once we commit, however, the page
605 // type must always be normal.
606 new_entry->set_page_type(NavigationEntry::NORMAL_PAGE);
607 } else {
[email protected]b680ad22009-04-15 23:19:42608 new_entry = new NavigationEntry;
[email protected]e9ba4472008-09-14 15:42:43609 }
610
611 new_entry->set_url(params.url);
[email protected]740fbda2009-02-18 21:38:08612 new_entry->set_referrer(params.referrer);
[email protected]e9ba4472008-09-14 15:42:43613 new_entry->set_page_id(params.page_id);
614 new_entry->set_transition_type(params.transition);
[email protected]9423d9412009-04-14 22:13:55615 new_entry->set_site_instance(tab_contents_->GetSiteInstance());
[email protected]e9ba4472008-09-14 15:42:43616 new_entry->set_has_post_data(params.is_post);
617
[email protected]09b8f82f2009-06-16 20:22:11618 // If the current entry is a redirection source and the redirection has
619 // occurred within kMaxAutoNavigationTimeDelta since the last document load,
620 // this is likely to be machine-initiated redirect and the entry needs to be
621 // replaced with the new entry to avoid unwanted redirections in navigating
622 // backward/forward.
623 // Otherwise, just insert the new entry.
[email protected]befd8d822009-07-01 04:51:47624 *did_replace_entry = IsRedirect(params) &&
625 IsLikelyAutoNavigation(base::TimeTicks::Now());
626 InsertOrReplaceEntry(new_entry, *did_replace_entry);
[email protected]e9ba4472008-09-14 15:42:43627}
628
629void NavigationController::RendererDidNavigateToExistingPage(
630 const ViewHostMsg_FrameNavigate_Params& params) {
631 // We should only get here for main frame navigations.
632 DCHECK(PageTransition::IsMainFrame(params.transition));
633
634 // This is a back/forward navigation. The existing page for the ID is
[email protected]4c27ba82008-09-24 16:49:09635 // guaranteed to exist by ClassifyNavigation, and we just need to update it
636 // with new information from the renderer.
[email protected]7f0005a2009-04-15 03:25:11637 int entry_index = GetEntryIndexWithPageID(tab_contents_->GetSiteInstance(),
638 params.page_id);
[email protected]e9ba4472008-09-14 15:42:43639 DCHECK(entry_index >= 0 &&
640 entry_index < static_cast<int>(entries_.size()));
641 NavigationEntry* entry = entries_[entry_index].get();
642
643 // The URL may have changed due to redirects. The site instance will normally
644 // be the same except during session restore, when no site instance will be
645 // assigned.
646 entry->set_url(params.url);
647 DCHECK(entry->site_instance() == NULL ||
[email protected]9423d9412009-04-14 22:13:55648 entry->site_instance() == tab_contents_->GetSiteInstance());
649 entry->set_site_instance(tab_contents_->GetSiteInstance());
[email protected]e9ba4472008-09-14 15:42:43650
651 // The entry we found in the list might be pending if the user hit
652 // back/forward/reload. This load should commit it (since it's already in the
653 // list, we can just discard the pending pointer).
654 //
655 // Note that we need to use the "internal" version since we don't want to
656 // actually change any other state, just kill the pointer.
657 if (entry == pending_entry_)
[email protected]cbab76d2008-10-13 22:42:47658 DiscardNonCommittedEntriesInternal();
[email protected]40bcc302009-03-02 20:50:39659
[email protected]e9ba4472008-09-14 15:42:43660 last_committed_entry_index_ = entry_index;
[email protected]e9ba4472008-09-14 15:42:43661}
662
663void NavigationController::RendererDidNavigateToSamePage(
664 const ViewHostMsg_FrameNavigate_Params& params) {
665 // This mode implies we have a pending entry that's the same as an existing
[email protected]4c27ba82008-09-24 16:49:09666 // entry for this page ID. This entry is guaranteed to exist by
667 // ClassifyNavigation. All we need to do is update the existing entry.
[email protected]e9ba4472008-09-14 15:42:43668 NavigationEntry* existing_entry = GetEntryWithPageID(
[email protected]9423d9412009-04-14 22:13:55669 tab_contents_->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:43670 params.page_id);
671
672 // We assign the entry's unique ID to be that of the new one. Since this is
673 // always the result of a user action, we want to dismiss infobars, etc. like
674 // a regular user-initiated navigation.
675 existing_entry->set_unique_id(pending_entry_->unique_id());
676
[email protected]a0e69262009-06-03 19:08:48677 // The URL may have changed due to redirects.
678 existing_entry->set_url(params.url);
679
[email protected]cbab76d2008-10-13 22:42:47680 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:43681}
682
683void NavigationController::RendererDidNavigateInPage(
[email protected]befd8d822009-07-01 04:51:47684 const ViewHostMsg_FrameNavigate_Params& params, bool* did_replace_entry) {
[email protected]e9ba4472008-09-14 15:42:43685 DCHECK(PageTransition::IsMainFrame(params.transition)) <<
686 "WebKit should only tell us about in-page navs for the main frame.";
687 // We're guaranteed to have an entry for this one.
688 NavigationEntry* existing_entry = GetEntryWithPageID(
[email protected]9423d9412009-04-14 22:13:55689 tab_contents_->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:43690 params.page_id);
691
692 // Reference fragment navigation. We're guaranteed to have the last_committed
693 // entry and it will be the same page as the new navigation (minus the
694 // reference fragments, of course).
695 NavigationEntry* new_entry = new NavigationEntry(*existing_entry);
696 new_entry->set_page_id(params.page_id);
697 new_entry->set_url(params.url);
[email protected]befd8d822009-07-01 04:51:47698 *did_replace_entry = IsRedirect(params) &&
699 IsLikelyAutoNavigation(base::TimeTicks::Now());
700 InsertOrReplaceEntry(new_entry, *did_replace_entry);
[email protected]e9ba4472008-09-14 15:42:43701}
702
703void NavigationController::RendererDidNavigateNewSubframe(
704 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]09b8f82f2009-06-16 20:22:11705 if (PageTransition::StripQualifier(params.transition) ==
706 PageTransition::AUTO_SUBFRAME) {
707 // This is not user-initiated. Ignore.
708 return;
709 }
710 if (IsRedirect(params)) {
711 // This is redirect. Ignore.
712 return;
713 }
714
[email protected]e9ba4472008-09-14 15:42:43715 // Manual subframe navigations just get the current entry cloned so the user
716 // can go back or forward to it. The actual subframe information will be
717 // stored in the page state for each of those entries. This happens out of
718 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:09719 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
720 << "that a last committed entry exists.";
[email protected]e9ba4472008-09-14 15:42:43721 NavigationEntry* new_entry = new NavigationEntry(*GetLastCommittedEntry());
722 new_entry->set_page_id(params.page_id);
[email protected]672eba292009-05-13 13:22:45723 InsertOrReplaceEntry(new_entry, false);
[email protected]e9ba4472008-09-14 15:42:43724}
725
726bool NavigationController::RendererDidNavigateAutoSubframe(
727 const ViewHostMsg_FrameNavigate_Params& params) {
728 // We're guaranteed to have a previously committed entry, and we now need to
729 // handle navigation inside of a subframe in it without creating a new entry.
730 DCHECK(GetLastCommittedEntry());
731
732 // Handle the case where we're navigating back/forward to a previous subframe
733 // navigation entry. This is case "2." in NAV_AUTO_SUBFRAME comment in the
734 // header file. In case "1." this will be a NOP.
735 int entry_index = GetEntryIndexWithPageID(
[email protected]9423d9412009-04-14 22:13:55736 tab_contents_->GetSiteInstance(),
[email protected]e9ba4472008-09-14 15:42:43737 params.page_id);
738 if (entry_index < 0 ||
739 entry_index >= static_cast<int>(entries_.size())) {
740 NOTREACHED();
741 return false;
742 }
743
744 // Update the current navigation entry in case we're going back/forward.
745 if (entry_index != last_committed_entry_index_) {
[email protected]e9ba4472008-09-14 15:42:43746 last_committed_entry_index_ = entry_index;
[email protected]e9ba4472008-09-14 15:42:43747 return true;
748 }
749 return false;
750}
751
[email protected]ce3fa3c2009-04-20 19:55:57752// TODO(brettw) I think this function is unnecessary.
[email protected]e9ba4472008-09-14 15:42:43753void NavigationController::CommitPendingEntry() {
[email protected]cbab76d2008-10-13 22:42:47754 DiscardTransientEntry();
755
[email protected]7f0005a2009-04-15 03:25:11756 if (!pending_entry())
[email protected]e9ba4472008-09-14 15:42:43757 return; // Nothing to do.
758
759 // Need to save the previous URL for the notification.
760 LoadCommittedDetails details;
[email protected]0e8db942008-09-24 21:21:48761 if (GetLastCommittedEntry()) {
[email protected]e9ba4472008-09-14 15:42:43762 details.previous_url = GetLastCommittedEntry()->url();
[email protected]7f0005a2009-04-15 03:25:11763 details.previous_entry_index = last_committed_entry_index();
[email protected]0e8db942008-09-24 21:21:48764 } else {
765 details.previous_entry_index = -1;
766 }
[email protected]e9ba4472008-09-14 15:42:43767
768 if (pending_entry_index_ >= 0) {
769 // This is a previous navigation (back/forward) that we're just now
770 // committing. Just mark it as committed.
[email protected]0e8db942008-09-24 21:21:48771 details.type = NavigationType::EXISTING_PAGE;
[email protected]e9ba4472008-09-14 15:42:43772 int new_entry_index = pending_entry_index_;
[email protected]cbab76d2008-10-13 22:42:47773 DiscardNonCommittedEntriesInternal();
[email protected]e9ba4472008-09-14 15:42:43774
775 // Mark that entry as committed.
[email protected]e9ba4472008-09-14 15:42:43776 last_committed_entry_index_ = new_entry_index;
[email protected]e9ba4472008-09-14 15:42:43777 } else {
778 // This is a new navigation. It's easiest to just copy the entry and insert
[email protected]672eba292009-05-13 13:22:45779 // it new again, since InsertOrReplaceEntry expects to take ownership and
780 // also discard the pending entry. We also need to synthesize a page ID. We
781 // can only do this because this function will only be called by our custom
[email protected]57c6a652009-05-04 07:58:34782 // TabContents types. For TabContents, the IDs are generated by the
[email protected]e9ba4472008-09-14 15:42:43783 // renderer, so we can't do this.
[email protected]0e8db942008-09-24 21:21:48784 details.type = NavigationType::NEW_PAGE;
[email protected]9423d9412009-04-14 22:13:55785 pending_entry_->set_page_id(tab_contents_->GetMaxPageID() + 1);
786 tab_contents_->UpdateMaxPageID(pending_entry_->page_id());
[email protected]672eba292009-05-13 13:22:45787 InsertOrReplaceEntry(new NavigationEntry(*pending_entry_), false);
[email protected]e9ba4472008-09-14 15:42:43788 }
789
790 // Broadcast the notification of the navigation.
791 details.entry = GetActiveEntry();
792 details.is_auto = false;
793 details.is_in_page = AreURLsInPageNavigation(details.previous_url,
794 details.entry->url());
795 details.is_main_frame = true;
796 NotifyNavigationEntryCommitted(&details);
797}
[email protected]765b35502008-08-21 00:51:20798
799int NavigationController::GetIndexOfEntry(
800 const NavigationEntry* entry) const {
801 const NavigationEntries::const_iterator i(std::find(
802 entries_.begin(),
803 entries_.end(),
804 entry));
805 return (i == entries_.end()) ? -1 : static_cast<int>(i - entries_.begin());
806}
807
[email protected]e9ba4472008-09-14 15:42:43808bool NavigationController::IsURLInPageNavigation(const GURL& url) const {
809 NavigationEntry* last_committed = GetLastCommittedEntry();
810 if (!last_committed)
811 return false;
812 return AreURLsInPageNavigation(last_committed->url(), url);
813}
814
[email protected]ce3fa3c2009-04-20 19:55:57815void NavigationController::CopyStateFrom(const NavigationController& source) {
816 // Verify that we look new.
817 DCHECK(entry_count() == 0 && !pending_entry());
818
819 if (source.entry_count() == 0)
820 return; // Nothing new to do.
821
822 needs_reload_ = true;
823 for (int i = 0; i < source.entry_count(); i++) {
824 entries_.push_back(linked_ptr<NavigationEntry>(
825 new NavigationEntry(*source.entries_[i])));
826 }
827
828 FinishRestore(source.last_committed_entry_index_);
829}
830
[email protected]cbab76d2008-10-13 22:42:47831void NavigationController::DiscardNonCommittedEntries() {
832 bool transient = transient_entry_index_ != -1;
833 DiscardNonCommittedEntriesInternal();
initial.commit09911bf2008-07-26 23:55:29834
[email protected]cbab76d2008-10-13 22:42:47835 // If there was a transient entry, invalidate everything so the new active
836 // entry state is shown.
837 if (transient) {
[email protected]9423d9412009-04-14 22:13:55838 tab_contents_->NotifyNavigationStateChanged(
[email protected]cbab76d2008-10-13 22:42:47839 TabContents::INVALIDATE_EVERYTHING);
840 }
initial.commit09911bf2008-07-26 23:55:29841}
842
[email protected]672eba292009-05-13 13:22:45843void NavigationController::InsertOrReplaceEntry(NavigationEntry* entry,
844 bool replace) {
[email protected]1e5645ff2008-08-27 18:09:07845 DCHECK(entry->transition_type() != PageTransition::AUTO_SUBFRAME);
[email protected]765b35502008-08-21 00:51:20846
847 // Copy the pending entry's unique ID to the committed entry.
848 // I don't know if pending_entry_index_ can be other than -1 here.
849 const NavigationEntry* const pending_entry = (pending_entry_index_ == -1) ?
850 pending_entry_ : entries_[pending_entry_index_].get();
851 if (pending_entry)
852 entry->set_unique_id(pending_entry->unique_id());
853
[email protected]cbab76d2008-10-13 22:42:47854 DiscardNonCommittedEntriesInternal();
[email protected]765b35502008-08-21 00:51:20855
856 int current_size = static_cast<int>(entries_.size());
857
[email protected]765b35502008-08-21 00:51:20858 if (current_size > 0) {
[email protected]672eba292009-05-13 13:22:45859 // Prune any entries which are in front of the current entry.
860 // Also prune the current entry if we are to replace the current entry.
861 int prune_up_to = replace ? last_committed_entry_index_ - 1
862 : last_committed_entry_index_;
[email protected]c12bf1a12008-09-17 16:28:49863 int num_pruned = 0;
[email protected]672eba292009-05-13 13:22:45864 while (prune_up_to < (current_size - 1)) {
[email protected]c12bf1a12008-09-17 16:28:49865 num_pruned++;
[email protected]765b35502008-08-21 00:51:20866 entries_.pop_back();
867 current_size--;
868 }
[email protected]c12bf1a12008-09-17 16:28:49869 if (num_pruned > 0) // Only notify if we did prune something.
870 NotifyPrunedEntries(this, false, num_pruned);
[email protected]765b35502008-08-21 00:51:20871 }
872
[email protected]c12bf1a12008-09-17 16:28:49873 if (entries_.size() >= max_entry_count_) {
[email protected]cbab76d2008-10-13 22:42:47874 RemoveEntryAtIndex(0, GURL());
[email protected]c12bf1a12008-09-17 16:28:49875 NotifyPrunedEntries(this, true, 1);
876 }
[email protected]765b35502008-08-21 00:51:20877
878 entries_.push_back(linked_ptr<NavigationEntry>(entry));
879 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
[email protected]e9ba4472008-09-14 15:42:43880
881 // This is a new page ID, so we need everybody to know about it.
[email protected]9423d9412009-04-14 22:13:55882 tab_contents_->UpdateMaxPageID(entry->page_id());
initial.commit09911bf2008-07-26 23:55:29883}
884
885void NavigationController::SetWindowID(const SessionID& id) {
886 window_id_ = id;
[email protected]bfd04a62009-02-01 18:16:56887 NotificationService::current()->Notify(NotificationType::TAB_PARENTED,
[email protected]534e54b2008-08-13 15:40:09888 Source<NavigationController>(this),
889 NotificationService::NoDetails());
initial.commit09911bf2008-07-26 23:55:29890}
891
892void NavigationController::NavigateToPendingEntry(bool reload) {
initial.commit09911bf2008-07-26 23:55:29893 // For session history navigations only the pending_entry_index_ is set.
894 if (!pending_entry_) {
895 DCHECK(pending_entry_index_ != -1);
[email protected]765b35502008-08-21 00:51:20896 pending_entry_ = entries_[pending_entry_index_].get();
initial.commit09911bf2008-07-26 23:55:29897 }
898
[email protected]b680ad22009-04-15 23:19:42899 if (!tab_contents_->NavigateToPendingEntry(reload))
[email protected]cbab76d2008-10-13 22:42:47900 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:29901}
902
[email protected]ecd9d8702008-08-28 22:10:17903void NavigationController::NotifyNavigationEntryCommitted(
904 LoadCommittedDetails* details) {
[email protected]df1af242009-05-01 00:11:40905 details->entry = GetActiveEntry();
906 NotificationDetails notification_details =
907 Details<LoadCommittedDetails>(details);
908
909 // We need to notify the ssl_manager_ before the tab_contents_ so the
910 // location bar will have up-to-date information about the security style
911 // when it wants to draw. See http://crbug.com/11157
912 ssl_manager_.DidCommitProvisionalLoad(notification_details);
913
initial.commit09911bf2008-07-26 23:55:29914 // TODO(pkasting): http://b/1113079 Probably these explicit notification paths
915 // should be removed, and interested parties should just listen for the
916 // notification below instead.
[email protected]9423d9412009-04-14 22:13:55917 tab_contents_->NotifyNavigationStateChanged(
initial.commit09911bf2008-07-26 23:55:29918 TabContents::INVALIDATE_EVERYTHING);
919
[email protected]ecd9d8702008-08-28 22:10:17920 NotificationService::current()->Notify(
[email protected]bfd04a62009-02-01 18:16:56921 NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ecd9d8702008-08-28 22:10:17922 Source<NavigationController>(this),
[email protected]df1af242009-05-01 00:11:40923 notification_details);
initial.commit09911bf2008-07-26 23:55:29924}
925
initial.commit09911bf2008-07-26 23:55:29926// static
927void NavigationController::DisablePromptOnRepost() {
928 check_for_repost_ = false;
929}
930
931void NavigationController::SetActive(bool is_active) {
[email protected]ee613922009-09-02 20:38:22932 if (is_active && needs_reload_)
933 LoadIfNecessary();
initial.commit09911bf2008-07-26 23:55:29934}
935
936void NavigationController::LoadIfNecessary() {
937 if (!needs_reload_)
938 return;
939
940 needs_reload_ = false;
941 // Calling Reload() results in ignoring state, and not loading.
942 // Explicitly use NavigateToPendingEntry so that the renderer uses the
943 // cached state.
944 pending_entry_index_ = last_committed_entry_index_;
945 NavigateToPendingEntry(false);
946}
947
[email protected]534e54b2008-08-13 15:40:09948void NavigationController::NotifyEntryChanged(const NavigationEntry* entry,
949 int index) {
950 EntryChangedDetails det;
951 det.changed_entry = entry;
952 det.index = index;
[email protected]bfd04a62009-02-01 18:16:56953 NotificationService::current()->Notify(NotificationType::NAV_ENTRY_CHANGED,
[email protected]534e54b2008-08-13 15:40:09954 Source<NavigationController>(this),
955 Details<EntryChangedDetails>(&det));
initial.commit09911bf2008-07-26 23:55:29956}
957
[email protected]ec322ff2008-11-19 22:53:30958void NavigationController::FinishRestore(int selected_index) {
[email protected]7f0005a2009-04-15 03:25:11959 DCHECK(selected_index >= 0 && selected_index < entry_count());
initial.commit09911bf2008-07-26 23:55:29960 ConfigureEntriesForRestore(&entries_);
961
[email protected]7f0005a2009-04-15 03:25:11962 set_max_restored_page_id(entry_count());
initial.commit09911bf2008-07-26 23:55:29963
964 last_committed_entry_index_ = selected_index;
initial.commit09911bf2008-07-26 23:55:29965}
[email protected]765b35502008-08-21 00:51:20966
[email protected]cbab76d2008-10-13 22:42:47967void NavigationController::DiscardNonCommittedEntriesInternal() {
[email protected]765b35502008-08-21 00:51:20968 if (pending_entry_index_ == -1)
969 delete pending_entry_;
970 pending_entry_ = NULL;
971 pending_entry_index_ = -1;
[email protected]cbab76d2008-10-13 22:42:47972
973 DiscardTransientEntry();
974}
975
976void NavigationController::DiscardTransientEntry() {
977 if (transient_entry_index_ == -1)
978 return;
[email protected]a0e69262009-06-03 19:08:48979 entries_.erase(entries_.begin() + transient_entry_index_);
[email protected]cbab76d2008-10-13 22:42:47980 transient_entry_index_ = -1;
[email protected]765b35502008-08-21 00:51:20981}
982
983int NavigationController::GetEntryIndexWithPageID(
[email protected]7f0005a2009-04-15 03:25:11984 SiteInstance* instance, int32 page_id) const {
[email protected]765b35502008-08-21 00:51:20985 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
[email protected]7f0005a2009-04-15 03:25:11986 if ((entries_[i]->site_instance() == instance) &&
[email protected]1e5645ff2008-08-27 18:09:07987 (entries_[i]->page_id() == page_id))
[email protected]765b35502008-08-21 00:51:20988 return i;
989 }
990 return -1;
991}
[email protected]cbab76d2008-10-13 22:42:47992
993NavigationEntry* NavigationController::GetTransientEntry() const {
994 if (transient_entry_index_ == -1)
995 return NULL;
996 return entries_[transient_entry_index_].get();
997}