blob: 35aec383e7bea873c2da3c17301e361f211ddbf8 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// 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
5#include "chrome/browser/navigation_controller.h"
6
7#include "base/command_line.h"
8#include "base/file_util.h"
9#include "base/logging.h"
10#include "base/string_util.h"
initial.commit09911bf2008-07-26 23:55:2911#include "chrome/browser/browser_process.h"
12#include "chrome/browser/dom_ui/dom_ui_host.h"
13#include "chrome/browser/navigation_entry.h"
14#include "chrome/browser/profile.h"
15#include "chrome/browser/repost_form_warning_dialog.h"
[email protected]169627b2008-12-06 19:30:1916#include "chrome/browser/sessions/session_types.h"
initial.commit09911bf2008-07-26 23:55:2917#include "chrome/browser/site_instance.h"
18#include "chrome/browser/tab_contents.h"
19#include "chrome/browser/tab_contents_delegate.h"
20#include "chrome/common/chrome_switches.h"
[email protected]299dabd2008-11-19 02:27:1621#include "chrome/common/navigation_types.h"
22#include "chrome/common/resource_bundle.h"
23#include "chrome/common/scoped_vector.h"
initial.commit09911bf2008-07-26 23:55:2924#include "net/base/net_util.h"
[email protected]765b35502008-08-21 00:51:2025#include "webkit/glue/webkit_glue.h"
initial.commit09911bf2008-07-26 23:55:2926
[email protected]e9ba4472008-09-14 15:42:4327namespace {
28
29// Invoked when entries have been pruned, or removed. For example, if the
30// current entries are [google, digg, yahoo], with the current entry google,
31// and the user types in cnet, then digg and yahoo are pruned.
[email protected]c12bf1a12008-09-17 16:28:4932void NotifyPrunedEntries(NavigationController* nav_controller,
33 bool from_front,
34 int count) {
35 NavigationController::PrunedDetails details;
36 details.from_front = from_front;
37 details.count = count;
[email protected]e9ba4472008-09-14 15:42:4338 NotificationService::current()->Notify(
39 NOTIFY_NAV_LIST_PRUNED,
40 Source<NavigationController>(nav_controller),
[email protected]c12bf1a12008-09-17 16:28:4941 Details<NavigationController::PrunedDetails>(&details));
[email protected]e9ba4472008-09-14 15:42:4342}
43
44// Ensure the given NavigationEntry has a valid state, so that WebKit does not
45// get confused if we navigate back to it.
46//
47// An empty state is treated as a new navigation by WebKit, which would mean
48// losing the navigation entries and generating a new navigation entry after
49// this one. We don't want that. To avoid this we create a valid state which
50// WebKit will not treat as a new navigation.
51void SetContentStateIfEmpty(NavigationEntry* entry) {
52 if (entry->content_state().empty() &&
53 (entry->tab_type() == TAB_CONTENTS_WEB ||
54 entry->tab_type() == TAB_CONTENTS_NEW_TAB_UI ||
55 entry->tab_type() == TAB_CONTENTS_ABOUT_UI ||
56 entry->tab_type() == TAB_CONTENTS_HTML_DIALOG)) {
57 entry->set_content_state(
58 webkit_glue::CreateHistoryStateForURL(entry->url()));
59 }
60}
61
62// Configure all the NavigationEntries in entries for restore. This resets
63// the transition type to reload and makes sure the content state isn't empty.
64void ConfigureEntriesForRestore(
65 std::vector<linked_ptr<NavigationEntry> >* entries) {
66 for (size_t i = 0; i < entries->size(); ++i) {
67 // Use a transition type of reload so that we don't incorrectly increase
68 // the typed count.
69 (*entries)[i]->set_transition_type(PageTransition::RELOAD);
70 (*entries)[i]->set_restored(true);
71 // NOTE(darin): This code is only needed for backwards compat.
72 SetContentStateIfEmpty((*entries)[i].get());
73 }
74}
75
76// See NavigationController::IsURLInPageNavigation for how this works and why.
77bool AreURLsInPageNavigation(const GURL& existing_url, const GURL& new_url) {
78 if (existing_url == new_url || !new_url.has_ref())
79 return false;
80
81 url_canon::Replacements<char> replacements;
82 replacements.ClearRef();
83 return existing_url.ReplaceComponents(replacements) ==
84 new_url.ReplaceComponents(replacements);
85}
86
87} // namespace
88
initial.commit09911bf2008-07-26 23:55:2989// TabContentsCollector ---------------------------------------------------
90
91// We never destroy a TabContents synchronously because there are some
92// complex code path that cause the current TabContents to be in the call
93// stack. So instead, we use a TabContentsCollector which either destroys
94// the TabContents or does nothing if it has been cancelled.
95class TabContentsCollector : public Task {
96 public:
97 TabContentsCollector(NavigationController* target,
98 TabContentsType target_type)
99 : target_(target),
100 target_type_(target_type) {
101 }
102
103 void Cancel() {
104 target_ = NULL;
105 }
106
107 virtual void Run() {
108 if (target_) {
109 // Note: this will cancel this task as a side effect so target_ is
110 // now null.
111 TabContents* tc = target_->GetTabContents(target_type_);
112 tc->Destroy();
113 }
114 }
115
116 private:
117 // The NavigationController we are acting on.
118 NavigationController* target_;
119
120 // The TabContentsType that needs to be collected.
121 TabContentsType target_type_;
122
123 DISALLOW_EVIL_CONSTRUCTORS(TabContentsCollector);
124};
125
126// NavigationController ---------------------------------------------------
127
[email protected]765b35502008-08-21 00:51:20128// static
[email protected]c12bf1a12008-09-17 16:28:49129size_t NavigationController::max_entry_count_ = 50;
[email protected]765b35502008-08-21 00:51:20130
initial.commit09911bf2008-07-26 23:55:29131// static
132bool NavigationController::check_for_repost_ = true;
133
134// Creates a new NavigationEntry for each TabNavigation in navigations, adding
135// the NavigationEntry to entries. This is used during session restore.
136static void CreateNavigationEntriesFromTabNavigations(
137 const std::vector<TabNavigation>& navigations,
[email protected]765b35502008-08-21 00:51:20138 std::vector<linked_ptr<NavigationEntry> >* entries) {
initial.commit09911bf2008-07-26 23:55:29139 // Create a NavigationEntry for each of the navigations.
[email protected]169627b2008-12-06 19:30:19140 int page_id = 0;
initial.commit09911bf2008-07-26 23:55:29141 for (std::vector<TabNavigation>::const_iterator i =
[email protected]169627b2008-12-06 19:30:19142 navigations.begin(); i != navigations.end(); ++i, ++page_id) {
143 entries->push_back(
144 linked_ptr<NavigationEntry>(i->ToNavigationEntry(page_id)));
initial.commit09911bf2008-07-26 23:55:29145 }
146}
147
initial.commit09911bf2008-07-26 23:55:29148NavigationController::NavigationController(TabContents* contents,
149 Profile* profile)
150 : profile_(profile),
[email protected]765b35502008-08-21 00:51:20151 pending_entry_(NULL),
152 last_committed_entry_index_(-1),
153 pending_entry_index_(-1),
[email protected]cbab76d2008-10-13 22:42:47154 transient_entry_index_(-1),
initial.commit09911bf2008-07-26 23:55:29155 active_contents_(contents),
initial.commit09911bf2008-07-26 23:55:29156 max_restored_page_id_(-1),
157 ssl_manager_(this, NULL),
158 needs_reload_(false),
159 load_pending_entry_when_active_(false) {
160 if (contents)
161 RegisterTabContents(contents);
162 DCHECK(profile_);
initial.commit09911bf2008-07-26 23:55:29163}
164
165NavigationController::NavigationController(
166 Profile* profile,
167 const std::vector<TabNavigation>& navigations,
[email protected]ec322ff2008-11-19 22:53:30168 int selected_navigation)
initial.commit09911bf2008-07-26 23:55:29169 : profile_(profile),
[email protected]765b35502008-08-21 00:51:20170 pending_entry_(NULL),
171 last_committed_entry_index_(-1),
172 pending_entry_index_(-1),
[email protected]cbab76d2008-10-13 22:42:47173 transient_entry_index_(-1),
initial.commit09911bf2008-07-26 23:55:29174 active_contents_(NULL),
initial.commit09911bf2008-07-26 23:55:29175 max_restored_page_id_(-1),
176 ssl_manager_(this, NULL),
177 needs_reload_(true),
178 load_pending_entry_when_active_(false) {
179 DCHECK(profile_);
180 DCHECK(selected_navigation >= 0 &&
181 selected_navigation < static_cast<int>(navigations.size()));
182
initial.commit09911bf2008-07-26 23:55:29183 // Populate entries_ from the supplied TabNavigations.
184 CreateNavigationEntriesFromTabNavigations(navigations, &entries_);
185
186 // And finish the restore.
[email protected]ec322ff2008-11-19 22:53:30187 FinishRestore(selected_navigation);
initial.commit09911bf2008-07-26 23:55:29188}
189
190NavigationController::~NavigationController() {
191 DCHECK(tab_contents_map_.empty());
192 DCHECK(tab_contents_collector_map_.empty());
193
[email protected]cbab76d2008-10-13 22:42:47194 DiscardNonCommittedEntriesInternal();
[email protected]c0993872008-08-21 19:59:44195
[email protected]534e54b2008-08-13 15:40:09196 NotificationService::current()->Notify(NOTIFY_TAB_CLOSED,
197 Source<NavigationController>(this),
198 NotificationService::NoDetails());
initial.commit09911bf2008-07-26 23:55:29199}
200
201TabContents* NavigationController::GetTabContents(TabContentsType t) {
202 // Make sure the TabContents is no longer scheduled for collection.
203 CancelTabContentsCollection(t);
204 return tab_contents_map_[t];
205}
206
[email protected]f1c74112008-10-30 16:17:04207void NavigationController::Reload(bool check_for_repost) {
[email protected]cbab76d2008-10-13 22:42:47208 // Reloading a transient entry does nothing.
209 if (transient_entry_index_ != -1)
210 return;
211
212 DiscardNonCommittedEntriesInternal();
initial.commit09911bf2008-07-26 23:55:29213 int current_index = GetCurrentEntryIndex();
[email protected]f1c74112008-10-30 16:17:04214 if (check_for_repost_ && check_for_repost && current_index != -1 &&
[email protected]a07296712008-12-17 19:36:52215 GetEntryAtIndex(current_index)->has_post_data()) {
216 // The user is asking to reload a page with POST data. Prompt to make sure
217 // they really want to do this. If they do, RepostFormWarningDialog calls us
218 // back with ReloadDontCheckForRepost.
initial.commit09911bf2008-07-26 23:55:29219 active_contents_->Activate();
220 RepostFormWarningDialog::RunRepostFormWarningDialog(this);
221 } else {
[email protected]765b35502008-08-21 00:51:20222 // Base the navigation on where we are now...
223 int current_index = GetCurrentEntryIndex();
224
225 // If we are no where, then we can't reload. TODO(darin): We should add a
226 // CanReload method.
227 if (current_index == -1)
228 return;
229
[email protected]cbab76d2008-10-13 22:42:47230 DiscardNonCommittedEntriesInternal();
[email protected]765b35502008-08-21 00:51:20231
232 pending_entry_index_ = current_index;
[email protected]1e5645ff2008-08-27 18:09:07233 entries_[pending_entry_index_]->set_transition_type(PageTransition::RELOAD);
[email protected]765b35502008-08-21 00:51:20234 NavigateToPendingEntry(true);
initial.commit09911bf2008-07-26 23:55:29235 }
236}
237
[email protected]765b35502008-08-21 00:51:20238NavigationEntry* NavigationController::GetEntryWithPageID(
239 TabContentsType type, SiteInstance* instance, int32 page_id) const {
240 int index = GetEntryIndexWithPageID(type, instance, page_id);
241 return (index != -1) ? entries_[index].get() : NULL;
242}
243
244void NavigationController::LoadEntry(NavigationEntry* entry) {
245 // When navigating to a new page, we don't know for sure if we will actually
246 // end up leaving the current page. The new page load could for example
247 // result in a download or a 'no content' response (e.g., a mailto: URL).
[email protected]cbab76d2008-10-13 22:42:47248 DiscardNonCommittedEntriesInternal();
[email protected]765b35502008-08-21 00:51:20249 pending_entry_ = entry;
250 NotificationService::current()->Notify(
251 NOTIFY_NAV_ENTRY_PENDING,
252 Source<NavigationController>(this),
253 NotificationService::NoDetails());
254 NavigateToPendingEntry(false);
255}
256
[email protected]765b35502008-08-21 00:51:20257NavigationEntry* NavigationController::GetActiveEntry() const {
[email protected]cbab76d2008-10-13 22:42:47258 if (transient_entry_index_ != -1)
259 return entries_[transient_entry_index_].get();
260 if (pending_entry_)
261 return pending_entry_;
262 return GetLastCommittedEntry();
[email protected]765b35502008-08-21 00:51:20263}
264
265int NavigationController::GetCurrentEntryIndex() const {
[email protected]cbab76d2008-10-13 22:42:47266 if (transient_entry_index_ != -1)
267 return transient_entry_index_;
[email protected]765b35502008-08-21 00:51:20268 if (pending_entry_index_ != -1)
269 return pending_entry_index_;
270 return last_committed_entry_index_;
271}
272
273NavigationEntry* NavigationController::GetLastCommittedEntry() const {
274 if (last_committed_entry_index_ == -1)
275 return NULL;
276 return entries_[last_committed_entry_index_].get();
277}
278
279NavigationEntry* NavigationController::GetEntryAtOffset(int offset) const {
[email protected]cbab76d2008-10-13 22:42:47280 int index = (transient_entry_index_ != -1) ?
281 transient_entry_index_ + offset :
282 last_committed_entry_index_ + offset;
[email protected]765b35502008-08-21 00:51:20283 if (index < 0 || index >= GetEntryCount())
284 return NULL;
285
286 return entries_[index].get();
287}
288
[email protected]765b35502008-08-21 00:51:20289bool NavigationController::CanGoBack() const {
290 return entries_.size() > 1 && GetCurrentEntryIndex() > 0;
291}
292
293bool NavigationController::CanGoForward() const {
294 int index = GetCurrentEntryIndex();
295 return index >= 0 && index < (static_cast<int>(entries_.size()) - 1);
296}
297
298void NavigationController::GoBack() {
299 if (!CanGoBack()) {
300 NOTREACHED();
301 return;
302 }
303
304 // Base the navigation on where we are now...
305 int current_index = GetCurrentEntryIndex();
306
[email protected]cbab76d2008-10-13 22:42:47307 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20308
309 pending_entry_index_ = current_index - 1;
310 NavigateToPendingEntry(false);
311}
312
313void NavigationController::GoForward() {
314 if (!CanGoForward()) {
315 NOTREACHED();
316 return;
317 }
318
[email protected]cbab76d2008-10-13 22:42:47319 bool transient = (transient_entry_index_ != -1);
320
[email protected]765b35502008-08-21 00:51:20321 // Base the navigation on where we are now...
322 int current_index = GetCurrentEntryIndex();
323
[email protected]cbab76d2008-10-13 22:42:47324 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20325
[email protected]cbab76d2008-10-13 22:42:47326 pending_entry_index_ = current_index;
327 // If there was a transient entry, we removed it making the current index
328 // the next page.
329 if (!transient)
330 pending_entry_index_++;
331
[email protected]765b35502008-08-21 00:51:20332 NavigateToPendingEntry(false);
333}
334
335void NavigationController::GoToIndex(int index) {
336 if (index < 0 || index >= static_cast<int>(entries_.size())) {
337 NOTREACHED();
338 return;
339 }
340
[email protected]cbab76d2008-10-13 22:42:47341 if (transient_entry_index_ != -1) {
342 if (index == transient_entry_index_) {
343 // Nothing to do when navigating to the transient.
344 return;
345 }
346 if (index > transient_entry_index_) {
347 // Removing the transient is goint to shift all entries by 1.
348 index--;
349 }
350 }
351
352 DiscardNonCommittedEntries();
[email protected]765b35502008-08-21 00:51:20353
354 pending_entry_index_ = index;
355 NavigateToPendingEntry(false);
356}
357
358void NavigationController::GoToOffset(int offset) {
[email protected]cbab76d2008-10-13 22:42:47359 int index = (transient_entry_index_ != -1) ?
360 transient_entry_index_ + offset :
361 last_committed_entry_index_ + offset;
[email protected]765b35502008-08-21 00:51:20362 if (index < 0 || index >= GetEntryCount())
363 return;
364
365 GoToIndex(index);
366}
367
[email protected]cbab76d2008-10-13 22:42:47368void NavigationController::RemoveEntryAtIndex(int index,
369 const GURL& default_url) {
370 int size = static_cast<int>(entries_.size());
371 DCHECK(index < size);
372
373 DiscardNonCommittedEntries();
374
375 entries_.erase(entries_.begin() + index);
376
377 if (last_committed_entry_index_ == index) {
378 last_committed_entry_index_--;
379 // We removed the currently shown entry, so we have to load something else.
380 if (last_committed_entry_index_ != -1) {
381 pending_entry_index_ = last_committed_entry_index_;
382 NavigateToPendingEntry(false);
383 } else {
384 // If there is nothing to show, show a default page.
385 LoadURL(default_url.is_empty() ? GURL("about:blank") : default_url,
[email protected]c0588052008-10-27 23:01:50386 GURL(), PageTransition::START_PAGE);
[email protected]cbab76d2008-10-13 22:42:47387 }
388 } else if (last_committed_entry_index_ > index) {
389 last_committed_entry_index_--;
390 }
[email protected]cbab76d2008-10-13 22:42:47391}
392
initial.commit09911bf2008-07-26 23:55:29393void NavigationController::Destroy() {
[email protected]b33452302008-08-04 19:36:36394 // Close all tab contents owned by this controller. We make a list on the
395 // stack because they are removed from the map as they are Destroyed
initial.commit09911bf2008-07-26 23:55:29396 // (invalidating the iterators), which may or may not occur synchronously.
[email protected]b33452302008-08-04 19:36:36397 // We also keep track of any NULL entries in the map so that we can clean
398 // them out.
initial.commit09911bf2008-07-26 23:55:29399 std::list<TabContents*> tabs_to_destroy;
[email protected]b33452302008-08-04 19:36:36400 std::list<TabContentsType> tab_types_to_erase;
initial.commit09911bf2008-07-26 23:55:29401 for (TabContentsMap::iterator i = tab_contents_map_.begin();
402 i != tab_contents_map_.end(); ++i) {
[email protected]b33452302008-08-04 19:36:36403 if (i->second)
404 tabs_to_destroy.push_back(i->second);
405 else
406 tab_types_to_erase.push_back(i->first);
407 }
408
409 // Clean out all NULL entries in the map so that we know empty map means all
410 // tabs destroyed. This is needed since TabContentsWasDestroyed() won't get
411 // called for types that are in our map with a NULL contents. (We don't do
412 // this by iterating over TAB_CONTENTS_NUM_TYPES because some tests create
413 // additional types.)
414 for (std::list<TabContentsType>::iterator i = tab_types_to_erase.begin();
415 i != tab_types_to_erase.end(); ++i) {
416 TabContentsMap::iterator map_iterator = tab_contents_map_.find(*i);
417 if (map_iterator != tab_contents_map_.end()) {
418 DCHECK(!map_iterator->second);
419 tab_contents_map_.erase(map_iterator);
420 }
initial.commit09911bf2008-07-26 23:55:29421 }
422
423 // Cancel all the TabContentsCollectors.
424 for (TabContentsCollectorMap::iterator i =
425 tab_contents_collector_map_.begin();
426 i != tab_contents_collector_map_.end(); ++i) {
427 DCHECK(i->second);
428 i->second->Cancel();
429 }
[email protected]ccfc1a7b2008-08-14 16:26:20430 tab_contents_collector_map_.clear();
431
initial.commit09911bf2008-07-26 23:55:29432
433 // Finally destroy all the tab contents.
434 for (std::list<TabContents*>::iterator i = tabs_to_destroy.begin();
435 i != tabs_to_destroy.end(); ++i) {
436 (*i)->Destroy();
437 }
438 // We are deleted at this point.
439}
440
441void NavigationController::TabContentsWasDestroyed(TabContentsType type) {
442 TabContentsMap::iterator i = tab_contents_map_.find(type);
443 DCHECK(i != tab_contents_map_.end());
444 tab_contents_map_.erase(i);
445
446 // Make sure we cancel any collector for that TabContents.
[email protected]ccfc1a7b2008-08-14 16:26:20447 CancelTabContentsCollection(type);
initial.commit09911bf2008-07-26 23:55:29448
449 // If that was the last tab to be destroyed, delete ourselves.
450 if (tab_contents_map_.empty())
451 delete this;
452}
453
454NavigationEntry* NavigationController::CreateNavigationEntry(
[email protected]c0588052008-10-27 23:01:50455 const GURL& url, const GURL& referrer, PageTransition::Type transition) {
initial.commit09911bf2008-07-26 23:55:29456 GURL real_url = url;
457 TabContentsType type;
458
459 // If the active contents supports |url|, use it.
460 // Note: in both cases, we give TabContents a chance to rewrite the URL.
461 TabContents* active = active_contents();
462 if (active && active->SupportsURL(&real_url))
463 type = active->type();
464 else
465 type = TabContents::TypeForURL(&real_url);
466
467 NavigationEntry* entry = new NavigationEntry(type, NULL, -1, real_url,
[email protected]c0588052008-10-27 23:01:50468 referrer,
initial.commit09911bf2008-07-26 23:55:29469 std::wstring(), transition);
[email protected]1e5645ff2008-08-27 18:09:07470 entry->set_display_url(url);
471 entry->set_user_typed_url(url);
[email protected]8654b682008-11-01 23:36:17472 if (url.SchemeIsFile()) {
473 entry->set_title(file_util::GetFilenameFromPath(UTF8ToWide(url.host() +
474 url.path())));
475 }
initial.commit09911bf2008-07-26 23:55:29476 return entry;
477}
478
[email protected]cbab76d2008-10-13 22:42:47479void NavigationController::AddTransientEntry(NavigationEntry* entry) {
480 // Discard any current transient entry, we can only have one at a time.
481 int index = 0;
482 if (last_committed_entry_index_ != -1)
483 index = last_committed_entry_index_ + 1;
484 DiscardTransientEntry();
485 entries_.insert(entries_.begin() + index, linked_ptr<NavigationEntry>(entry));
486 transient_entry_index_ = index;
487 active_contents_->NotifyNavigationStateChanged(
488 TabContents::INVALIDATE_EVERYTHING);
489}
490
[email protected]c0588052008-10-27 23:01:50491void NavigationController::LoadURL(const GURL& url, const GURL& referrer,
initial.commit09911bf2008-07-26 23:55:29492 PageTransition::Type transition) {
493 // The user initiated a load, we don't need to reload anymore.
494 needs_reload_ = false;
495
[email protected]c0588052008-10-27 23:01:50496 NavigationEntry* entry = CreateNavigationEntry(url, referrer, transition);
initial.commit09911bf2008-07-26 23:55:29497
498 LoadEntry(entry);
499}
500
501void NavigationController::LoadURLLazily(const GURL& url,
[email protected]c0588052008-10-27 23:01:50502 const GURL& referrer,
initial.commit09911bf2008-07-26 23:55:29503 PageTransition::Type type,
504 const std::wstring& title,
505 SkBitmap* icon) {
[email protected]c0588052008-10-27 23:01:50506 NavigationEntry* entry = CreateNavigationEntry(url, referrer, type);
[email protected]1e5645ff2008-08-27 18:09:07507 entry->set_title(title);
initial.commit09911bf2008-07-26 23:55:29508 if (icon)
[email protected]1e5645ff2008-08-27 18:09:07509 entry->favicon().set_bitmap(*icon);
initial.commit09911bf2008-07-26 23:55:29510
[email protected]cbab76d2008-10-13 22:42:47511 DiscardNonCommittedEntriesInternal();
initial.commit09911bf2008-07-26 23:55:29512 pending_entry_ = entry;
513 load_pending_entry_when_active_ = true;
514}
515
516bool NavigationController::LoadingURLLazily() {
517 return load_pending_entry_when_active_;
518}
519
520const std::wstring& NavigationController::GetLazyTitle() const {
521 if (pending_entry_)
[email protected]3d627bbc2008-10-23 20:49:07522 return pending_entry_->GetTitleForDisplay();
initial.commit09911bf2008-07-26 23:55:29523 else
524 return EmptyWString();
525}
526
527const SkBitmap& NavigationController::GetLazyFavIcon() const {
528 if (pending_entry_) {
[email protected]1e5645ff2008-08-27 18:09:07529 return pending_entry_->favicon().bitmap();
initial.commit09911bf2008-07-26 23:55:29530 } else {
531 ResourceBundle &rb = ResourceBundle::GetSharedInstance();
532 return *rb.GetBitmapNamed(IDR_DEFAULT_FAVICON);
533 }
534}
535
[email protected]e9ba4472008-09-14 15:42:43536bool NavigationController::RendererDidNavigate(
537 const ViewHostMsg_FrameNavigate_Params& params,
[email protected]e9ba4472008-09-14 15:42:43538 LoadCommittedDetails* details) {
[email protected]0e8db942008-09-24 21:21:48539 // Save the previous state before we clobber it.
540 if (GetLastCommittedEntry()) {
[email protected]ecd9d8702008-08-28 22:10:17541 details->previous_url = GetLastCommittedEntry()->url();
[email protected]0e8db942008-09-24 21:21:48542 details->previous_entry_index = GetLastCommittedEntryIndex();
543 } else {
544 details->previous_url = GURL();
545 details->previous_entry_index = -1;
546 }
[email protected]ecd9d8702008-08-28 22:10:17547
[email protected]e9ba4472008-09-14 15:42:43548 // Assign the current site instance to any pending entry, so we can find it
549 // later by calling GetEntryIndexWithPageID. We only care about this if the
550 // pending entry is an existing navigation and not a new one (or else we
551 // wouldn't care about finding it with GetEntryIndexWithPageID).
552 //
553 // TODO(brettw) this seems slightly bogus as we don't really know if the
554 // pending entry is what this navigation is for. There is a similar TODO
555 // w.r.t. the pending entry in RendererDidNavigateToNewPage.
556 if (pending_entry_index_ >= 0)
557 pending_entry_->set_site_instance(active_contents_->GetSiteInstance());
558
559 // Do navigation-type specific actions. These will make and commit an entry.
[email protected]0e8db942008-09-24 21:21:48560 details->type = ClassifyNavigation(params);
561 switch (details->type) {
562 case NavigationType::NEW_PAGE:
[email protected]e9ba4472008-09-14 15:42:43563 RendererDidNavigateToNewPage(params);
564 break;
[email protected]0e8db942008-09-24 21:21:48565 case NavigationType::EXISTING_PAGE:
[email protected]e9ba4472008-09-14 15:42:43566 RendererDidNavigateToExistingPage(params);
567 break;
[email protected]0e8db942008-09-24 21:21:48568 case NavigationType::SAME_PAGE:
[email protected]e9ba4472008-09-14 15:42:43569 RendererDidNavigateToSamePage(params);
570 break;
[email protected]0e8db942008-09-24 21:21:48571 case NavigationType::IN_PAGE:
[email protected]e9ba4472008-09-14 15:42:43572 RendererDidNavigateInPage(params);
573 break;
[email protected]0e8db942008-09-24 21:21:48574 case NavigationType::NEW_SUBFRAME:
[email protected]e9ba4472008-09-14 15:42:43575 RendererDidNavigateNewSubframe(params);
576 break;
[email protected]0e8db942008-09-24 21:21:48577 case NavigationType::AUTO_SUBFRAME:
[email protected]e9ba4472008-09-14 15:42:43578 if (!RendererDidNavigateAutoSubframe(params))
579 return false;
580 break;
[email protected]0e8db942008-09-24 21:21:48581 case NavigationType::NAV_IGNORE:
[email protected]e9ba4472008-09-14 15:42:43582 // There is nothing we can do with this navigation, so we just return to
583 // the caller that nothing has happened.
584 return false;
585 default:
586 NOTREACHED();
[email protected]765b35502008-08-21 00:51:20587 }
588
[email protected]e9ba4472008-09-14 15:42:43589 // All committed entries should have nonempty content state so WebKit doesn't
590 // get confused when we go back to them (see the function for details).
591 SetContentStateIfEmpty(GetActiveEntry());
[email protected]765b35502008-08-21 00:51:20592
[email protected]e9ba4472008-09-14 15:42:43593 // WebKit doesn't set the "auto" transition on meta refreshes properly (bug
594 // 1051891) so we manually set it for redirects which we normally treat as
595 // "non-user-gestures" where we want to update stuff after navigations.
596 //
597 // Note that the redirect check also checks for a pending entry to
598 // differentiate real redirects from browser initiated navigations to a
599 // redirected entry. This happens when you hit back to go to a page that was
600 // the destination of a redirect, we don't want to treat it as a redirect
601 // even though that's what its transition will be. See bug 1117048.
602 //
603 // TODO(brettw) write a test for this complicated logic.
604 details->is_auto = (PageTransition::IsRedirect(params.transition) &&
605 !GetPendingEntry()) ||
606 params.gesture == NavigationGestureAuto;
[email protected]765b35502008-08-21 00:51:20607
[email protected]e9ba4472008-09-14 15:42:43608 // Now prep the rest of the details for the notification and broadcast.
609 details->entry = GetActiveEntry();
610 details->is_in_page = IsURLInPageNavigation(params.url);
611 details->is_main_frame = PageTransition::IsMainFrame(params.transition);
[email protected]f072d2ce2008-09-17 17:16:24612 details->serialized_security_info = params.security_info;
[email protected]8a3422c92008-09-24 17:42:42613 details->is_content_filtered = params.is_content_filtered;
[email protected]ecd9d8702008-08-28 22:10:17614 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:29615
[email protected]b33452302008-08-04 19:36:36616 // It is now a safe time to schedule collection for any tab contents of a
617 // different type, because a navigation is necessary to get back to them.
[email protected]50664fd2008-08-28 16:10:30618 ScheduleTabContentsCollectionForInactiveTabs();
[email protected]e9ba4472008-09-14 15:42:43619 return true;
initial.commit09911bf2008-07-26 23:55:29620}
621
[email protected]0e8db942008-09-24 21:21:48622NavigationType::Type NavigationController::ClassifyNavigation(
[email protected]e9ba4472008-09-14 15:42:43623 const ViewHostMsg_FrameNavigate_Params& params) const {
624 // If a page makes a popup navigated to about blank, and then writes stuff
625 // like a subframe navigated to a real site, we'll get a notification with an
626 // invalid page ID. There's nothing we can do with these, so just ignore them.
627 if (params.page_id == -1) {
628 DCHECK(!GetActiveEntry()) << "Got an invalid page ID but we seem to be "
629 " navigated to a valid page. This should be impossible.";
[email protected]0e8db942008-09-24 21:21:48630 return NavigationType::NAV_IGNORE;
[email protected]e9ba4472008-09-14 15:42:43631 }
632
633 if (params.page_id > active_contents_->GetMaxPageID()) {
634 // Greater page IDs than we've ever seen before are new pages. We may or may
635 // not have a pending entry for the page, and this may or may not be the
636 // main frame.
637 if (PageTransition::IsMainFrame(params.transition))
[email protected]0e8db942008-09-24 21:21:48638 return NavigationType::NEW_PAGE;
[email protected]4c27ba82008-09-24 16:49:09639
640 // When this is a new subframe navigation, we should have a committed page
641 // for which it's a suframe in. This may not be the case when an iframe is
642 // navigated on a popup navigated to about:blank (the iframe would be
643 // written into the popup by script on the main page). For these cases,
644 // there isn't any navigation stuff we can do, so just ignore it.
645 if (!GetLastCommittedEntry())
[email protected]0e8db942008-09-24 21:21:48646 return NavigationType::NAV_IGNORE;
[email protected]4c27ba82008-09-24 16:49:09647
648 // Valid subframe navigation.
[email protected]0e8db942008-09-24 21:21:48649 return NavigationType::NEW_SUBFRAME;
[email protected]e9ba4472008-09-14 15:42:43650 }
651
652 // Now we know that the notification is for an existing page. Find that entry.
653 int existing_entry_index = GetEntryIndexWithPageID(
654 active_contents_->type(),
655 active_contents_->GetSiteInstance(),
656 params.page_id);
657 if (existing_entry_index == -1) {
658 // The page was not found. It could have been pruned because of the limit on
659 // back/forward entries (not likely since we'll usually tell it to navigate
660 // to such entries). It could also mean that the renderer is smoking crack.
661 NOTREACHED();
[email protected]0e8db942008-09-24 21:21:48662 return NavigationType::NAV_IGNORE;
[email protected]e9ba4472008-09-14 15:42:43663 }
664 NavigationEntry* existing_entry = entries_[existing_entry_index].get();
665
666 if (pending_entry_ &&
667 pending_entry_->url() == params.url &&
668 existing_entry != pending_entry_ &&
669 pending_entry_->page_id() == -1 &&
670 pending_entry_->url() == existing_entry->url()) {
671 // In this case, we have a pending entry for a URL but WebCore didn't do a
672 // new navigation. This happens when you press enter in the URL bar to
673 // reload. We will create a pending entry, but WebKit will convert it to
674 // a reload since it's the same page and not create a new entry for it
675 // (the user doesn't want to have a new back/forward entry when they do
676 // this). In this case, we want to just ignore the pending entry and go
677 // back to where we were (the "existing entry").
[email protected]0e8db942008-09-24 21:21:48678 return NavigationType::SAME_PAGE;
[email protected]e9ba4472008-09-14 15:42:43679 }
680
681 if (AreURLsInPageNavigation(existing_entry->url(), params.url))
[email protected]0e8db942008-09-24 21:21:48682 return NavigationType::IN_PAGE;
[email protected]e9ba4472008-09-14 15:42:43683
[email protected]4c27ba82008-09-24 16:49:09684 if (!PageTransition::IsMainFrame(params.transition)) {
685 // All manual subframes would get new IDs and were handled above, so we
686 // know this is auto. Since the current page was found in the navigation
687 // entry list, we're guaranteed to have a last committed entry.
688 DCHECK(GetLastCommittedEntry());
[email protected]0e8db942008-09-24 21:21:48689 return NavigationType::AUTO_SUBFRAME;
[email protected]4c27ba82008-09-24 16:49:09690 }
691
[email protected]e9ba4472008-09-14 15:42:43692 // Since we weeded out "new" navigations above, we know this is an existing
[email protected]4c27ba82008-09-24 16:49:09693 // (back/forward) navigation.
[email protected]0e8db942008-09-24 21:21:48694 return NavigationType::EXISTING_PAGE;
[email protected]e9ba4472008-09-14 15:42:43695}
696
697void NavigationController::RendererDidNavigateToNewPage(
698 const ViewHostMsg_FrameNavigate_Params& params) {
699 NavigationEntry* new_entry;
700 if (pending_entry_) {
701 // TODO(brettw) this assumes that the pending entry is appropriate for the
702 // new page that was just loaded. I don't think this is necessarily the
703 // case! We should have some more tracking to know for sure. This goes along
704 // with a similar TODO at the top of RendererDidNavigate where we blindly
705 // set the site instance on the pending entry.
706 new_entry = new NavigationEntry(*pending_entry_);
707
708 // Don't use the page type from the pending entry. Some interstitial page
709 // may have set the type to interstitial. Once we commit, however, the page
710 // type must always be normal.
711 new_entry->set_page_type(NavigationEntry::NORMAL_PAGE);
712 } else {
713 new_entry = new NavigationEntry(active_contents_->type());
714 }
715
716 new_entry->set_url(params.url);
717 new_entry->set_page_id(params.page_id);
718 new_entry->set_transition_type(params.transition);
719 new_entry->set_site_instance(active_contents_->GetSiteInstance());
720 new_entry->set_has_post_data(params.is_post);
721
722 InsertEntry(new_entry);
723}
724
725void NavigationController::RendererDidNavigateToExistingPage(
726 const ViewHostMsg_FrameNavigate_Params& params) {
727 // We should only get here for main frame navigations.
728 DCHECK(PageTransition::IsMainFrame(params.transition));
729
730 // This is a back/forward navigation. The existing page for the ID is
[email protected]4c27ba82008-09-24 16:49:09731 // guaranteed to exist by ClassifyNavigation, and we just need to update it
732 // with new information from the renderer.
[email protected]e9ba4472008-09-14 15:42:43733 int entry_index = GetEntryIndexWithPageID(
734 active_contents_->type(),
735 active_contents_->GetSiteInstance(),
736 params.page_id);
737 DCHECK(entry_index >= 0 &&
738 entry_index < static_cast<int>(entries_.size()));
739 NavigationEntry* entry = entries_[entry_index].get();
740
741 // The URL may have changed due to redirects. The site instance will normally
742 // be the same except during session restore, when no site instance will be
743 // assigned.
744 entry->set_url(params.url);
745 DCHECK(entry->site_instance() == NULL ||
746 entry->site_instance() == active_contents_->GetSiteInstance());
747 entry->set_site_instance(active_contents_->GetSiteInstance());
748
749 // The entry we found in the list might be pending if the user hit
750 // back/forward/reload. This load should commit it (since it's already in the
751 // list, we can just discard the pending pointer).
752 //
753 // Note that we need to use the "internal" version since we don't want to
754 // actually change any other state, just kill the pointer.
755 if (entry == pending_entry_)
[email protected]cbab76d2008-10-13 22:42:47756 DiscardNonCommittedEntriesInternal();
[email protected]e9ba4472008-09-14 15:42:43757
[email protected]e9ba4472008-09-14 15:42:43758 last_committed_entry_index_ = entry_index;
[email protected]e9ba4472008-09-14 15:42:43759}
760
761void NavigationController::RendererDidNavigateToSamePage(
762 const ViewHostMsg_FrameNavigate_Params& params) {
763 // This mode implies we have a pending entry that's the same as an existing
[email protected]4c27ba82008-09-24 16:49:09764 // entry for this page ID. This entry is guaranteed to exist by
765 // ClassifyNavigation. All we need to do is update the existing entry.
[email protected]e9ba4472008-09-14 15:42:43766 NavigationEntry* existing_entry = GetEntryWithPageID(
767 active_contents_->type(),
768 active_contents_->GetSiteInstance(),
769 params.page_id);
770
771 // We assign the entry's unique ID to be that of the new one. Since this is
772 // always the result of a user action, we want to dismiss infobars, etc. like
773 // a regular user-initiated navigation.
774 existing_entry->set_unique_id(pending_entry_->unique_id());
775
[email protected]cbab76d2008-10-13 22:42:47776 DiscardNonCommittedEntries();
[email protected]e9ba4472008-09-14 15:42:43777}
778
779void NavigationController::RendererDidNavigateInPage(
780 const ViewHostMsg_FrameNavigate_Params& params) {
781 DCHECK(PageTransition::IsMainFrame(params.transition)) <<
782 "WebKit should only tell us about in-page navs for the main frame.";
783 // We're guaranteed to have an entry for this one.
784 NavigationEntry* existing_entry = GetEntryWithPageID(
785 active_contents_->type(),
786 active_contents_->GetSiteInstance(),
787 params.page_id);
788
789 // Reference fragment navigation. We're guaranteed to have the last_committed
790 // entry and it will be the same page as the new navigation (minus the
791 // reference fragments, of course).
792 NavigationEntry* new_entry = new NavigationEntry(*existing_entry);
793 new_entry->set_page_id(params.page_id);
794 new_entry->set_url(params.url);
795 InsertEntry(new_entry);
796}
797
798void NavigationController::RendererDidNavigateNewSubframe(
799 const ViewHostMsg_FrameNavigate_Params& params) {
800 // Manual subframe navigations just get the current entry cloned so the user
801 // can go back or forward to it. The actual subframe information will be
802 // stored in the page state for each of those entries. This happens out of
803 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:09804 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
805 << "that a last committed entry exists.";
[email protected]e9ba4472008-09-14 15:42:43806 NavigationEntry* new_entry = new NavigationEntry(*GetLastCommittedEntry());
807 new_entry->set_page_id(params.page_id);
808 InsertEntry(new_entry);
809}
810
811bool NavigationController::RendererDidNavigateAutoSubframe(
812 const ViewHostMsg_FrameNavigate_Params& params) {
813 // We're guaranteed to have a previously committed entry, and we now need to
814 // handle navigation inside of a subframe in it without creating a new entry.
815 DCHECK(GetLastCommittedEntry());
816
817 // Handle the case where we're navigating back/forward to a previous subframe
818 // navigation entry. This is case "2." in NAV_AUTO_SUBFRAME comment in the
819 // header file. In case "1." this will be a NOP.
820 int entry_index = GetEntryIndexWithPageID(
821 active_contents_->type(),
822 active_contents_->GetSiteInstance(),
823 params.page_id);
824 if (entry_index < 0 ||
825 entry_index >= static_cast<int>(entries_.size())) {
826 NOTREACHED();
827 return false;
828 }
829
830 // Update the current navigation entry in case we're going back/forward.
831 if (entry_index != last_committed_entry_index_) {
[email protected]e9ba4472008-09-14 15:42:43832 last_committed_entry_index_ = entry_index;
[email protected]e9ba4472008-09-14 15:42:43833 return true;
834 }
835 return false;
836}
837
838void NavigationController::CommitPendingEntry() {
[email protected]cbab76d2008-10-13 22:42:47839 DiscardTransientEntry();
840
[email protected]e9ba4472008-09-14 15:42:43841 if (!GetPendingEntry())
842 return; // Nothing to do.
843
844 // Need to save the previous URL for the notification.
845 LoadCommittedDetails details;
[email protected]0e8db942008-09-24 21:21:48846 if (GetLastCommittedEntry()) {
[email protected]e9ba4472008-09-14 15:42:43847 details.previous_url = GetLastCommittedEntry()->url();
[email protected]0e8db942008-09-24 21:21:48848 details.previous_entry_index = GetLastCommittedEntryIndex();
849 } else {
850 details.previous_entry_index = -1;
851 }
[email protected]e9ba4472008-09-14 15:42:43852
853 if (pending_entry_index_ >= 0) {
854 // This is a previous navigation (back/forward) that we're just now
855 // committing. Just mark it as committed.
[email protected]0e8db942008-09-24 21:21:48856 details.type = NavigationType::EXISTING_PAGE;
[email protected]e9ba4472008-09-14 15:42:43857 int new_entry_index = pending_entry_index_;
[email protected]cbab76d2008-10-13 22:42:47858 DiscardNonCommittedEntriesInternal();
[email protected]e9ba4472008-09-14 15:42:43859
860 // Mark that entry as committed.
[email protected]e9ba4472008-09-14 15:42:43861 last_committed_entry_index_ = new_entry_index;
[email protected]e9ba4472008-09-14 15:42:43862 } else {
863 // This is a new navigation. It's easiest to just copy the entry and insert
864 // it new again, since InsertEntry expects to take ownership and also
865 // discard the pending entry. We also need to synthesize a page ID. We can
866 // only do this because this function will only be called by our custom
867 // TabContents types. For WebContents, the IDs are generated by the
868 // renderer, so we can't do this.
[email protected]0e8db942008-09-24 21:21:48869 details.type = NavigationType::NEW_PAGE;
[email protected]e9ba4472008-09-14 15:42:43870 pending_entry_->set_page_id(active_contents_->GetMaxPageID() + 1);
871 active_contents_->UpdateMaxPageID(pending_entry_->page_id());
872 InsertEntry(new NavigationEntry(*pending_entry_));
873 }
874
875 // Broadcast the notification of the navigation.
876 details.entry = GetActiveEntry();
877 details.is_auto = false;
878 details.is_in_page = AreURLsInPageNavigation(details.previous_url,
879 details.entry->url());
880 details.is_main_frame = true;
881 NotifyNavigationEntryCommitted(&details);
882}
[email protected]765b35502008-08-21 00:51:20883
884int NavigationController::GetIndexOfEntry(
885 const NavigationEntry* entry) const {
886 const NavigationEntries::const_iterator i(std::find(
887 entries_.begin(),
888 entries_.end(),
889 entry));
890 return (i == entries_.end()) ? -1 : static_cast<int>(i - entries_.begin());
891}
892
[email protected]e9ba4472008-09-14 15:42:43893bool NavigationController::IsURLInPageNavigation(const GURL& url) const {
894 NavigationEntry* last_committed = GetLastCommittedEntry();
895 if (!last_committed)
896 return false;
897 return AreURLsInPageNavigation(last_committed->url(), url);
898}
899
[email protected]cbab76d2008-10-13 22:42:47900void NavigationController::DiscardNonCommittedEntries() {
901 bool transient = transient_entry_index_ != -1;
902 DiscardNonCommittedEntriesInternal();
initial.commit09911bf2008-07-26 23:55:29903
904 // Synchronize the active_contents_ to the last committed entry.
905 NavigationEntry* last_entry = GetLastCommittedEntry();
[email protected]1e5645ff2008-08-27 18:09:07906 if (last_entry && last_entry->tab_type() != active_contents_->type()) {
initial.commit09911bf2008-07-26 23:55:29907 TabContents* from_contents = active_contents_;
[email protected]d5f942ba2008-09-26 19:30:34908 from_contents->set_is_active(false);
initial.commit09911bf2008-07-26 23:55:29909
910 // Switch back to the previous tab contents.
[email protected]1e5645ff2008-08-27 18:09:07911 active_contents_ = GetTabContents(last_entry->tab_type());
initial.commit09911bf2008-07-26 23:55:29912 DCHECK(active_contents_);
913
[email protected]d5f942ba2008-09-26 19:30:34914 active_contents_->set_is_active(true);
initial.commit09911bf2008-07-26 23:55:29915
916 // If we are transitioning from two types of WebContents, we need to migrate
917 // the download shelf if it is visible. The download shelf may have been
918 // created before the error that caused us to discard the entry.
919 WebContents::MigrateShelfView(from_contents, active_contents_);
920
921 if (from_contents->delegate()) {
922 from_contents->delegate()->ReplaceContents(from_contents,
923 active_contents_);
924 }
925
926 // The entry we just discarded needed a different TabContents type. We no
927 // longer need it but we can't destroy it just yet because the TabContents
928 // is very likely involved in the current stack.
929 DCHECK(from_contents != active_contents_);
930 ScheduleTabContentsCollection(from_contents->type());
931 }
[email protected]cbab76d2008-10-13 22:42:47932
933 // If there was a transient entry, invalidate everything so the new active
934 // entry state is shown.
935 if (transient) {
936 active_contents_->NotifyNavigationStateChanged(
937 TabContents::INVALIDATE_EVERYTHING);
938 }
initial.commit09911bf2008-07-26 23:55:29939}
940
941void NavigationController::InsertEntry(NavigationEntry* entry) {
[email protected]1e5645ff2008-08-27 18:09:07942 DCHECK(entry->transition_type() != PageTransition::AUTO_SUBFRAME);
[email protected]765b35502008-08-21 00:51:20943
944 // Copy the pending entry's unique ID to the committed entry.
945 // I don't know if pending_entry_index_ can be other than -1 here.
946 const NavigationEntry* const pending_entry = (pending_entry_index_ == -1) ?
947 pending_entry_ : entries_[pending_entry_index_].get();
948 if (pending_entry)
949 entry->set_unique_id(pending_entry->unique_id());
950
[email protected]cbab76d2008-10-13 22:42:47951 DiscardNonCommittedEntriesInternal();
[email protected]765b35502008-08-21 00:51:20952
953 int current_size = static_cast<int>(entries_.size());
954
955 // Prune any entries which are in front of the current entry.
956 if (current_size > 0) {
[email protected]c12bf1a12008-09-17 16:28:49957 int num_pruned = 0;
[email protected]765b35502008-08-21 00:51:20958 while (last_committed_entry_index_ < (current_size - 1)) {
[email protected]c12bf1a12008-09-17 16:28:49959 num_pruned++;
[email protected]765b35502008-08-21 00:51:20960 entries_.pop_back();
961 current_size--;
962 }
[email protected]c12bf1a12008-09-17 16:28:49963 if (num_pruned > 0) // Only notify if we did prune something.
964 NotifyPrunedEntries(this, false, num_pruned);
[email protected]765b35502008-08-21 00:51:20965 }
966
[email protected]c12bf1a12008-09-17 16:28:49967 if (entries_.size() >= max_entry_count_) {
[email protected]cbab76d2008-10-13 22:42:47968 RemoveEntryAtIndex(0, GURL());
[email protected]c12bf1a12008-09-17 16:28:49969 NotifyPrunedEntries(this, true, 1);
970 }
[email protected]765b35502008-08-21 00:51:20971
972 entries_.push_back(linked_ptr<NavigationEntry>(entry));
973 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
[email protected]e9ba4472008-09-14 15:42:43974
975 // This is a new page ID, so we need everybody to know about it.
976 active_contents_->UpdateMaxPageID(entry->page_id());
initial.commit09911bf2008-07-26 23:55:29977}
978
979void NavigationController::SetWindowID(const SessionID& id) {
980 window_id_ = id;
[email protected]534e54b2008-08-13 15:40:09981 NotificationService::current()->Notify(NOTIFY_TAB_PARENTED,
982 Source<NavigationController>(this),
983 NotificationService::NoDetails());
initial.commit09911bf2008-07-26 23:55:29984}
985
986void NavigationController::NavigateToPendingEntry(bool reload) {
987 TabContents* from_contents = active_contents_;
988
989 // For session history navigations only the pending_entry_index_ is set.
990 if (!pending_entry_) {
991 DCHECK(pending_entry_index_ != -1);
[email protected]765b35502008-08-21 00:51:20992 pending_entry_ = entries_[pending_entry_index_].get();
initial.commit09911bf2008-07-26 23:55:29993 }
994
995 // Reset the security states as any SSL error may have been resolved since we
996 // last visited that page.
[email protected]eb34392b2008-08-19 15:42:20997 pending_entry_->ssl() = NavigationEntry::SSLStatus();
initial.commit09911bf2008-07-26 23:55:29998
[email protected]1e5645ff2008-08-27 18:09:07999 if (from_contents && from_contents->type() != pending_entry_->tab_type())
[email protected]d5f942ba2008-09-26 19:30:341000 from_contents->set_is_active(false);
initial.commit09911bf2008-07-26 23:55:291001
[email protected]ec322ff2008-11-19 22:53:301002 TabContents* contents = GetTabContentsCreateIfNecessary(*pending_entry_);
initial.commit09911bf2008-07-26 23:55:291003
[email protected]d5f942ba2008-09-26 19:30:341004 contents->set_is_active(true);
initial.commit09911bf2008-07-26 23:55:291005 active_contents_ = contents;
1006
1007 if (from_contents && from_contents != contents) {
1008 if (from_contents->delegate())
1009 from_contents->delegate()->ReplaceContents(from_contents, contents);
1010 }
1011
[email protected]e9ba4472008-09-14 15:42:431012 NavigationEntry temp_entry(*pending_entry_);
1013 if (!contents->NavigateToPendingEntry(reload))
[email protected]cbab76d2008-10-13 22:42:471014 DiscardNonCommittedEntries();
initial.commit09911bf2008-07-26 23:55:291015}
1016
[email protected]ecd9d8702008-08-28 22:10:171017void NavigationController::NotifyNavigationEntryCommitted(
1018 LoadCommittedDetails* details) {
initial.commit09911bf2008-07-26 23:55:291019 // TODO(pkasting): http://b/1113079 Probably these explicit notification paths
1020 // should be removed, and interested parties should just listen for the
1021 // notification below instead.
1022 ssl_manager_.NavigationStateChanged();
1023 active_contents_->NotifyNavigationStateChanged(
1024 TabContents::INVALIDATE_EVERYTHING);
1025
[email protected]ecd9d8702008-08-28 22:10:171026 details->entry = GetActiveEntry();
1027 NotificationService::current()->Notify(
1028 NOTIFY_NAV_ENTRY_COMMITTED,
1029 Source<NavigationController>(this),
1030 Details<LoadCommittedDetails>(details));
initial.commit09911bf2008-07-26 23:55:291031}
1032
initial.commit09911bf2008-07-26 23:55:291033TabContents* NavigationController::GetTabContentsCreateIfNecessary(
initial.commit09911bf2008-07-26 23:55:291034 const NavigationEntry& entry) {
[email protected]1e5645ff2008-08-27 18:09:071035 TabContents* contents = GetTabContents(entry.tab_type());
initial.commit09911bf2008-07-26 23:55:291036 if (!contents) {
[email protected]ec322ff2008-11-19 22:53:301037 contents = TabContents::CreateWithType(entry.tab_type(), profile_,
initial.commit09911bf2008-07-26 23:55:291038 entry.site_instance());
1039 if (!contents->AsWebContents()) {
1040 // Update the max page id, otherwise the newly created TabContents may
1041 // have reset its max page id resulting in all new navigations. We only
1042 // do this for non-WebContents as WebContents takes care of this via its
1043 // SiteInstance. If this creation is the result of a restore, WebContents
1044 // handles invoking ReservePageIDRange to make sure the renderer's
1045 // max_page_id is updated to reflect the restored range of page ids.
1046 int32 max_page_id = contents->GetMaxPageID();
1047 for (size_t i = 0; i < entries_.size(); ++i) {
[email protected]1e5645ff2008-08-27 18:09:071048 if (entries_[i]->tab_type() == entry.tab_type())
1049 max_page_id = std::max(max_page_id, entries_[i]->page_id());
initial.commit09911bf2008-07-26 23:55:291050 }
1051 contents->UpdateMaxPageID(max_page_id);
1052 }
1053 RegisterTabContents(contents);
1054 }
1055
1056 // We should not be trying to collect this tab contents.
1057 DCHECK(tab_contents_collector_map_.find(contents->type()) ==
1058 tab_contents_collector_map_.end());
1059
1060 return contents;
1061}
1062
1063void NavigationController::RegisterTabContents(TabContents* some_contents) {
1064 DCHECK(some_contents);
1065 TabContentsType t = some_contents->type();
1066 TabContents* tc;
1067 if ((tc = tab_contents_map_[t]) != some_contents) {
1068 if (tc) {
1069 NOTREACHED() << "Should not happen. Multiple contents for one type";
1070 } else {
1071 tab_contents_map_[t] = some_contents;
1072 some_contents->set_controller(this);
1073 }
1074 }
1075 if (some_contents->AsDOMUIHost())
1076 some_contents->AsDOMUIHost()->AttachMessageHandlers();
1077}
1078
initial.commit09911bf2008-07-26 23:55:291079// static
1080void NavigationController::DisablePromptOnRepost() {
1081 check_for_repost_ = false;
1082}
1083
1084void NavigationController::SetActive(bool is_active) {
1085 if (is_active) {
1086 if (needs_reload_) {
1087 LoadIfNecessary();
1088 } else if (load_pending_entry_when_active_) {
1089 NavigateToPendingEntry(false);
1090 load_pending_entry_when_active_ = false;
1091 }
1092 }
1093}
1094
1095void NavigationController::LoadIfNecessary() {
1096 if (!needs_reload_)
1097 return;
1098
1099 needs_reload_ = false;
1100 // Calling Reload() results in ignoring state, and not loading.
1101 // Explicitly use NavigateToPendingEntry so that the renderer uses the
1102 // cached state.
1103 pending_entry_index_ = last_committed_entry_index_;
1104 NavigateToPendingEntry(false);
1105}
1106
[email protected]534e54b2008-08-13 15:40:091107void NavigationController::NotifyEntryChanged(const NavigationEntry* entry,
1108 int index) {
1109 EntryChangedDetails det;
1110 det.changed_entry = entry;
1111 det.index = index;
1112 NotificationService::current()->Notify(NOTIFY_NAV_ENTRY_CHANGED,
1113 Source<NavigationController>(this),
1114 Details<EntryChangedDetails>(&det));
initial.commit09911bf2008-07-26 23:55:291115}
1116
[email protected]ec322ff2008-11-19 22:53:301117NavigationController* NavigationController::Clone() {
initial.commit09911bf2008-07-26 23:55:291118 NavigationController* nc = new NavigationController(NULL, profile_);
1119
1120 if (GetEntryCount() == 0)
1121 return nc;
1122
1123 nc->needs_reload_ = true;
1124
1125 nc->entries_.reserve(entries_.size());
[email protected]765b35502008-08-21 00:51:201126 for (int i = 0, c = GetEntryCount(); i < c; ++i) {
1127 nc->entries_.push_back(linked_ptr<NavigationEntry>(
1128 new NavigationEntry(*GetEntryAtIndex(i))));
1129 }
initial.commit09911bf2008-07-26 23:55:291130
[email protected]ec322ff2008-11-19 22:53:301131 nc->FinishRestore(last_committed_entry_index_);
initial.commit09911bf2008-07-26 23:55:291132
1133 return nc;
1134}
1135
[email protected]50664fd2008-08-28 16:10:301136void NavigationController::ScheduleTabContentsCollectionForInactiveTabs() {
1137 int index = GetCurrentEntryIndex();
1138 if (index < 0 || GetPendingEntryIndex() != -1)
1139 return;
1140
1141 TabContentsType active_type = GetEntryAtIndex(index)->tab_type();
1142 for (TabContentsMap::iterator i = tab_contents_map_.begin();
1143 i != tab_contents_map_.end(); ++i) {
1144 if (i->first != active_type)
1145 ScheduleTabContentsCollection(i->first);
1146 }
1147}
1148
initial.commit09911bf2008-07-26 23:55:291149void NavigationController::ScheduleTabContentsCollection(TabContentsType t) {
1150 TabContentsCollectorMap::const_iterator i =
1151 tab_contents_collector_map_.find(t);
1152
1153 // The tab contents is already scheduled for collection.
1154 if (i != tab_contents_collector_map_.end())
1155 return;
1156
1157 // If we currently don't have a TabContents for t, skip.
1158 if (tab_contents_map_.find(t) == tab_contents_map_.end())
1159 return;
1160
1161 // Create a collector and schedule it.
1162 TabContentsCollector* tcc = new TabContentsCollector(this, t);
1163 tab_contents_collector_map_[t] = tcc;
1164 MessageLoop::current()->PostTask(FROM_HERE, tcc);
1165}
1166
1167void NavigationController::CancelTabContentsCollection(TabContentsType t) {
1168 TabContentsCollectorMap::iterator i = tab_contents_collector_map_.find(t);
1169
1170 if (i != tab_contents_collector_map_.end()) {
1171 DCHECK(i->second);
1172 i->second->Cancel();
1173 tab_contents_collector_map_.erase(i);
1174 }
1175}
1176
[email protected]ec322ff2008-11-19 22:53:301177void NavigationController::FinishRestore(int selected_index) {
initial.commit09911bf2008-07-26 23:55:291178 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
1179 ConfigureEntriesForRestore(&entries_);
1180
1181 set_max_restored_page_id(GetEntryCount());
1182
1183 last_committed_entry_index_ = selected_index;
1184
1185 // Callers assume we have an active_contents after restoring, so set it now.
[email protected]ec322ff2008-11-19 22:53:301186 active_contents_ = GetTabContentsCreateIfNecessary(*entries_[selected_index]);
initial.commit09911bf2008-07-26 23:55:291187}
[email protected]765b35502008-08-21 00:51:201188
[email protected]cbab76d2008-10-13 22:42:471189void NavigationController::DiscardNonCommittedEntriesInternal() {
[email protected]765b35502008-08-21 00:51:201190 if (pending_entry_index_ == -1)
1191 delete pending_entry_;
1192 pending_entry_ = NULL;
1193 pending_entry_index_ = -1;
[email protected]cbab76d2008-10-13 22:42:471194
1195 DiscardTransientEntry();
1196}
1197
1198void NavigationController::DiscardTransientEntry() {
1199 if (transient_entry_index_ == -1)
1200 return;
1201 entries_.erase(entries_.begin() + transient_entry_index_ );
1202 transient_entry_index_ = -1;
[email protected]765b35502008-08-21 00:51:201203}
1204
1205int NavigationController::GetEntryIndexWithPageID(
1206 TabContentsType type, SiteInstance* instance, int32 page_id) const {
[email protected]765b35502008-08-21 00:51:201207 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
[email protected]1e5645ff2008-08-27 18:09:071208 if ((entries_[i]->tab_type() == type) &&
[email protected]765b35502008-08-21 00:51:201209 (entries_[i]->site_instance() == instance) &&
[email protected]1e5645ff2008-08-27 18:09:071210 (entries_[i]->page_id() == page_id))
[email protected]765b35502008-08-21 00:51:201211 return i;
1212 }
1213 return -1;
1214}
[email protected]cbab76d2008-10-13 22:42:471215
1216NavigationEntry* NavigationController::GetTransientEntry() const {
1217 if (transient_entry_index_ == -1)
1218 return NULL;
1219 return entries_[transient_entry_index_].get();
1220}