blob: 31ff778f29b70274026e32e89f3df7071dd0c1bd [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"
11#include "chrome/common/navigation_types.h"
12#include "chrome/common/resource_bundle.h"
13#include "chrome/common/scoped_vector.h"
14#include "chrome/browser/browser_process.h"
15#include "chrome/browser/dom_ui/dom_ui_host.h"
16#include "chrome/browser/navigation_entry.h"
17#include "chrome/browser/profile.h"
18#include "chrome/browser/repost_form_warning_dialog.h"
19#include "chrome/browser/site_instance.h"
20#include "chrome/browser/tab_contents.h"
21#include "chrome/browser/tab_contents_delegate.h"
22#include "chrome/common/chrome_switches.h"
23#include "net/base/net_util.h"
[email protected]765b35502008-08-21 00:51:2024#include "webkit/glue/webkit_glue.h"
initial.commit09911bf2008-07-26 23:55:2925
[email protected]e9ba4472008-09-14 15:42:4326namespace {
27
28// Invoked when entries have been pruned, or removed. For example, if the
29// current entries are [google, digg, yahoo], with the current entry google,
30// and the user types in cnet, then digg and yahoo are pruned.
[email protected]c12bf1a12008-09-17 16:28:4931void NotifyPrunedEntries(NavigationController* nav_controller,
32 bool from_front,
33 int count) {
34 NavigationController::PrunedDetails details;
35 details.from_front = from_front;
36 details.count = count;
[email protected]e9ba4472008-09-14 15:42:4337 NotificationService::current()->Notify(
38 NOTIFY_NAV_LIST_PRUNED,
39 Source<NavigationController>(nav_controller),
[email protected]c12bf1a12008-09-17 16:28:4940 Details<NavigationController::PrunedDetails>(&details));
[email protected]e9ba4472008-09-14 15:42:4341}
42
43// Ensure the given NavigationEntry has a valid state, so that WebKit does not
44// get confused if we navigate back to it.
45//
46// An empty state is treated as a new navigation by WebKit, which would mean
47// losing the navigation entries and generating a new navigation entry after
48// this one. We don't want that. To avoid this we create a valid state which
49// WebKit will not treat as a new navigation.
50void SetContentStateIfEmpty(NavigationEntry* entry) {
51 if (entry->content_state().empty() &&
52 (entry->tab_type() == TAB_CONTENTS_WEB ||
53 entry->tab_type() == TAB_CONTENTS_NEW_TAB_UI ||
54 entry->tab_type() == TAB_CONTENTS_ABOUT_UI ||
55 entry->tab_type() == TAB_CONTENTS_HTML_DIALOG)) {
56 entry->set_content_state(
57 webkit_glue::CreateHistoryStateForURL(entry->url()));
58 }
59}
60
61// Configure all the NavigationEntries in entries for restore. This resets
62// the transition type to reload and makes sure the content state isn't empty.
63void ConfigureEntriesForRestore(
64 std::vector<linked_ptr<NavigationEntry> >* entries) {
65 for (size_t i = 0; i < entries->size(); ++i) {
66 // Use a transition type of reload so that we don't incorrectly increase
67 // the typed count.
68 (*entries)[i]->set_transition_type(PageTransition::RELOAD);
69 (*entries)[i]->set_restored(true);
70 // NOTE(darin): This code is only needed for backwards compat.
71 SetContentStateIfEmpty((*entries)[i].get());
72 }
73}
74
75// See NavigationController::IsURLInPageNavigation for how this works and why.
76bool AreURLsInPageNavigation(const GURL& existing_url, const GURL& new_url) {
77 if (existing_url == new_url || !new_url.has_ref())
78 return false;
79
80 url_canon::Replacements<char> replacements;
81 replacements.ClearRef();
82 return existing_url.ReplaceComponents(replacements) ==
83 new_url.ReplaceComponents(replacements);
84}
85
86} // namespace
87
initial.commit09911bf2008-07-26 23:55:2988// TabContentsCollector ---------------------------------------------------
89
90// We never destroy a TabContents synchronously because there are some
91// complex code path that cause the current TabContents to be in the call
92// stack. So instead, we use a TabContentsCollector which either destroys
93// the TabContents or does nothing if it has been cancelled.
94class TabContentsCollector : public Task {
95 public:
96 TabContentsCollector(NavigationController* target,
97 TabContentsType target_type)
98 : target_(target),
99 target_type_(target_type) {
100 }
101
102 void Cancel() {
103 target_ = NULL;
104 }
105
106 virtual void Run() {
107 if (target_) {
108 // Note: this will cancel this task as a side effect so target_ is
109 // now null.
110 TabContents* tc = target_->GetTabContents(target_type_);
111 tc->Destroy();
112 }
113 }
114
115 private:
116 // The NavigationController we are acting on.
117 NavigationController* target_;
118
119 // The TabContentsType that needs to be collected.
120 TabContentsType target_type_;
121
122 DISALLOW_EVIL_CONSTRUCTORS(TabContentsCollector);
123};
124
125// NavigationController ---------------------------------------------------
126
[email protected]765b35502008-08-21 00:51:20127// static
[email protected]c12bf1a12008-09-17 16:28:49128size_t NavigationController::max_entry_count_ = 50;
[email protected]765b35502008-08-21 00:51:20129
initial.commit09911bf2008-07-26 23:55:29130// static
131bool NavigationController::check_for_repost_ = true;
132
133// Creates a new NavigationEntry for each TabNavigation in navigations, adding
134// the NavigationEntry to entries. This is used during session restore.
135static void CreateNavigationEntriesFromTabNavigations(
136 const std::vector<TabNavigation>& navigations,
[email protected]765b35502008-08-21 00:51:20137 std::vector<linked_ptr<NavigationEntry> >* entries) {
initial.commit09911bf2008-07-26 23:55:29138 // Create a NavigationEntry for each of the navigations.
139 for (std::vector<TabNavigation>::const_iterator i =
140 navigations.begin(); i != navigations.end(); ++i) {
141 const TabNavigation& navigation = *i;
142
143 GURL real_url = navigation.url;
144 TabContentsType type = TabContents::TypeForURL(&real_url);
145 DCHECK(type != TAB_CONTENTS_UNKNOWN_TYPE);
146
147 NavigationEntry* entry = new NavigationEntry(
148 type,
149 NULL, // The site instance for restored tabs is sent on naviagion
150 // (WebContents::GetSiteInstanceForEntry).
151 static_cast<int>(i - navigations.begin()),
152 real_url,
153 navigation.title,
154 // Use a transition type of reload so that we don't incorrectly
155 // increase the typed count.
156 PageTransition::RELOAD);
[email protected]1e5645ff2008-08-27 18:09:07157 entry->set_display_url(navigation.url);
158 entry->set_content_state(navigation.state);
159 entry->set_has_post_data(
160 navigation.type_mask & TabNavigation::HAS_POST_DATA);
[email protected]765b35502008-08-21 00:51:20161 entries->push_back(linked_ptr<NavigationEntry>(entry));
initial.commit09911bf2008-07-26 23:55:29162 }
163}
164
initial.commit09911bf2008-07-26 23:55:29165NavigationController::NavigationController(TabContents* contents,
166 Profile* profile)
167 : profile_(profile),
[email protected]765b35502008-08-21 00:51:20168 pending_entry_(NULL),
169 last_committed_entry_index_(-1),
170 pending_entry_index_(-1),
initial.commit09911bf2008-07-26 23:55:29171 active_contents_(contents),
initial.commit09911bf2008-07-26 23:55:29172 max_restored_page_id_(-1),
173 ssl_manager_(this, NULL),
174 needs_reload_(false),
175 load_pending_entry_when_active_(false) {
176 if (contents)
177 RegisterTabContents(contents);
178 DCHECK(profile_);
initial.commit09911bf2008-07-26 23:55:29179}
180
181NavigationController::NavigationController(
182 Profile* profile,
183 const std::vector<TabNavigation>& navigations,
184 int selected_navigation,
185 HWND parent)
186 : profile_(profile),
[email protected]765b35502008-08-21 00:51:20187 pending_entry_(NULL),
188 last_committed_entry_index_(-1),
189 pending_entry_index_(-1),
initial.commit09911bf2008-07-26 23:55:29190 active_contents_(NULL),
initial.commit09911bf2008-07-26 23:55:29191 max_restored_page_id_(-1),
192 ssl_manager_(this, NULL),
193 needs_reload_(true),
194 load_pending_entry_when_active_(false) {
195 DCHECK(profile_);
196 DCHECK(selected_navigation >= 0 &&
197 selected_navigation < static_cast<int>(navigations.size()));
198
initial.commit09911bf2008-07-26 23:55:29199 // Populate entries_ from the supplied TabNavigations.
200 CreateNavigationEntriesFromTabNavigations(navigations, &entries_);
201
202 // And finish the restore.
203 FinishRestore(parent, selected_navigation);
204}
205
206NavigationController::~NavigationController() {
207 DCHECK(tab_contents_map_.empty());
208 DCHECK(tab_contents_collector_map_.empty());
209
[email protected]c0993872008-08-21 19:59:44210 DiscardPendingEntryInternal();
211
[email protected]534e54b2008-08-13 15:40:09212 NotificationService::current()->Notify(NOTIFY_TAB_CLOSED,
213 Source<NavigationController>(this),
214 NotificationService::NoDetails());
initial.commit09911bf2008-07-26 23:55:29215}
216
217TabContents* NavigationController::GetTabContents(TabContentsType t) {
218 // Make sure the TabContents is no longer scheduled for collection.
219 CancelTabContentsCollection(t);
220 return tab_contents_map_[t];
221}
222
initial.commit09911bf2008-07-26 23:55:29223void NavigationController::Reload() {
initial.commit09911bf2008-07-26 23:55:29224 DiscardPendingEntryInternal();
225 int current_index = GetCurrentEntryIndex();
226 if (check_for_repost_ && current_index != -1 &&
[email protected]1e5645ff2008-08-27 18:09:07227 GetEntryAtIndex(current_index)->has_post_data() &&
initial.commit09911bf2008-07-26 23:55:29228 active_contents_->AsWebContents() &&
229 !active_contents_->AsWebContents()->showing_repost_interstitial()) {
230 // The user is asking to reload a page with POST data and we're not showing
231 // the POST interstitial. Prompt to make sure they really want to do this.
232 // If they do, RepostFormWarningDialog calls us back with
233 // ReloadDontCheckForRepost.
234 active_contents_->Activate();
235 RepostFormWarningDialog::RunRepostFormWarningDialog(this);
236 } else {
[email protected]765b35502008-08-21 00:51:20237 // Base the navigation on where we are now...
238 int current_index = GetCurrentEntryIndex();
239
240 // If we are no where, then we can't reload. TODO(darin): We should add a
241 // CanReload method.
242 if (current_index == -1)
243 return;
244
[email protected]765b35502008-08-21 00:51:20245 DiscardPendingEntryInternal();
246
247 pending_entry_index_ = current_index;
[email protected]1e5645ff2008-08-27 18:09:07248 entries_[pending_entry_index_]->set_transition_type(PageTransition::RELOAD);
[email protected]765b35502008-08-21 00:51:20249 NavigateToPendingEntry(true);
initial.commit09911bf2008-07-26 23:55:29250 }
251}
252
[email protected]765b35502008-08-21 00:51:20253NavigationEntry* NavigationController::GetEntryWithPageID(
254 TabContentsType type, SiteInstance* instance, int32 page_id) const {
255 int index = GetEntryIndexWithPageID(type, instance, page_id);
256 return (index != -1) ? entries_[index].get() : NULL;
257}
258
259void NavigationController::LoadEntry(NavigationEntry* entry) {
260 // When navigating to a new page, we don't know for sure if we will actually
261 // end up leaving the current page. The new page load could for example
262 // result in a download or a 'no content' response (e.g., a mailto: URL).
[email protected]765b35502008-08-21 00:51:20263 DiscardPendingEntryInternal();
264 pending_entry_ = entry;
265 NotificationService::current()->Notify(
266 NOTIFY_NAV_ENTRY_PENDING,
267 Source<NavigationController>(this),
268 NotificationService::NoDetails());
269 NavigateToPendingEntry(false);
270}
271
[email protected]765b35502008-08-21 00:51:20272NavigationEntry* NavigationController::GetActiveEntry() const {
273 NavigationEntry* entry = pending_entry_;
274 if (!entry)
275 entry = GetLastCommittedEntry();
276 return entry;
277}
278
279int NavigationController::GetCurrentEntryIndex() const {
280 if (pending_entry_index_ != -1)
281 return pending_entry_index_;
282 return last_committed_entry_index_;
283}
284
285NavigationEntry* NavigationController::GetLastCommittedEntry() const {
286 if (last_committed_entry_index_ == -1)
287 return NULL;
288 return entries_[last_committed_entry_index_].get();
289}
290
291NavigationEntry* NavigationController::GetEntryAtOffset(int offset) const {
292 int index = last_committed_entry_index_ + offset;
293 if (index < 0 || index >= GetEntryCount())
294 return NULL;
295
296 return entries_[index].get();
297}
298
[email protected]765b35502008-08-21 00:51:20299bool NavigationController::CanGoBack() const {
300 return entries_.size() > 1 && GetCurrentEntryIndex() > 0;
301}
302
303bool NavigationController::CanGoForward() const {
304 int index = GetCurrentEntryIndex();
305 return index >= 0 && index < (static_cast<int>(entries_.size()) - 1);
306}
307
308void NavigationController::GoBack() {
309 if (!CanGoBack()) {
310 NOTREACHED();
311 return;
312 }
313
314 // Base the navigation on where we are now...
315 int current_index = GetCurrentEntryIndex();
316
317 DiscardPendingEntry();
318
319 pending_entry_index_ = current_index - 1;
320 NavigateToPendingEntry(false);
321}
322
323void NavigationController::GoForward() {
324 if (!CanGoForward()) {
325 NOTREACHED();
326 return;
327 }
328
329 // Base the navigation on where we are now...
330 int current_index = GetCurrentEntryIndex();
331
332 DiscardPendingEntry();
333
334 pending_entry_index_ = current_index + 1;
335 NavigateToPendingEntry(false);
336}
337
338void NavigationController::GoToIndex(int index) {
339 if (index < 0 || index >= static_cast<int>(entries_.size())) {
340 NOTREACHED();
341 return;
342 }
343
344 DiscardPendingEntry();
345
346 pending_entry_index_ = index;
347 NavigateToPendingEntry(false);
348}
349
350void NavigationController::GoToOffset(int offset) {
351 int index = last_committed_entry_index_ + offset;
352 if (index < 0 || index >= GetEntryCount())
353 return;
354
355 GoToIndex(index);
356}
357
initial.commit09911bf2008-07-26 23:55:29358void NavigationController::ReloadDontCheckForRepost() {
[email protected]765b35502008-08-21 00:51:20359 Reload();
initial.commit09911bf2008-07-26 23:55:29360}
361
362void NavigationController::Destroy() {
[email protected]b33452302008-08-04 19:36:36363 // Close all tab contents owned by this controller. We make a list on the
364 // stack because they are removed from the map as they are Destroyed
initial.commit09911bf2008-07-26 23:55:29365 // (invalidating the iterators), which may or may not occur synchronously.
[email protected]b33452302008-08-04 19:36:36366 // We also keep track of any NULL entries in the map so that we can clean
367 // them out.
initial.commit09911bf2008-07-26 23:55:29368 std::list<TabContents*> tabs_to_destroy;
[email protected]b33452302008-08-04 19:36:36369 std::list<TabContentsType> tab_types_to_erase;
initial.commit09911bf2008-07-26 23:55:29370 for (TabContentsMap::iterator i = tab_contents_map_.begin();
371 i != tab_contents_map_.end(); ++i) {
[email protected]b33452302008-08-04 19:36:36372 if (i->second)
373 tabs_to_destroy.push_back(i->second);
374 else
375 tab_types_to_erase.push_back(i->first);
376 }
377
378 // Clean out all NULL entries in the map so that we know empty map means all
379 // tabs destroyed. This is needed since TabContentsWasDestroyed() won't get
380 // called for types that are in our map with a NULL contents. (We don't do
381 // this by iterating over TAB_CONTENTS_NUM_TYPES because some tests create
382 // additional types.)
383 for (std::list<TabContentsType>::iterator i = tab_types_to_erase.begin();
384 i != tab_types_to_erase.end(); ++i) {
385 TabContentsMap::iterator map_iterator = tab_contents_map_.find(*i);
386 if (map_iterator != tab_contents_map_.end()) {
387 DCHECK(!map_iterator->second);
388 tab_contents_map_.erase(map_iterator);
389 }
initial.commit09911bf2008-07-26 23:55:29390 }
391
392 // Cancel all the TabContentsCollectors.
393 for (TabContentsCollectorMap::iterator i =
394 tab_contents_collector_map_.begin();
395 i != tab_contents_collector_map_.end(); ++i) {
396 DCHECK(i->second);
397 i->second->Cancel();
398 }
[email protected]ccfc1a7b2008-08-14 16:26:20399 tab_contents_collector_map_.clear();
400
initial.commit09911bf2008-07-26 23:55:29401
402 // Finally destroy all the tab contents.
403 for (std::list<TabContents*>::iterator i = tabs_to_destroy.begin();
404 i != tabs_to_destroy.end(); ++i) {
405 (*i)->Destroy();
406 }
407 // We are deleted at this point.
408}
409
410void NavigationController::TabContentsWasDestroyed(TabContentsType type) {
411 TabContentsMap::iterator i = tab_contents_map_.find(type);
412 DCHECK(i != tab_contents_map_.end());
413 tab_contents_map_.erase(i);
414
415 // Make sure we cancel any collector for that TabContents.
[email protected]ccfc1a7b2008-08-14 16:26:20416 CancelTabContentsCollection(type);
initial.commit09911bf2008-07-26 23:55:29417
418 // If that was the last tab to be destroyed, delete ourselves.
419 if (tab_contents_map_.empty())
420 delete this;
421}
422
423NavigationEntry* NavigationController::CreateNavigationEntry(
424 const GURL& url, PageTransition::Type transition) {
425 GURL real_url = url;
426 TabContentsType type;
427
428 // If the active contents supports |url|, use it.
429 // Note: in both cases, we give TabContents a chance to rewrite the URL.
430 TabContents* active = active_contents();
431 if (active && active->SupportsURL(&real_url))
432 type = active->type();
433 else
434 type = TabContents::TypeForURL(&real_url);
435
436 NavigationEntry* entry = new NavigationEntry(type, NULL, -1, real_url,
437 std::wstring(), transition);
[email protected]1e5645ff2008-08-27 18:09:07438 entry->set_display_url(url);
439 entry->set_user_typed_url(url);
initial.commit09911bf2008-07-26 23:55:29440 if (url.SchemeIsFile()) {
[email protected]1e5645ff2008-08-27 18:09:07441 entry->set_title(file_util::GetFilenameFromPath(UTF8ToWide(url.host() +
442 url.path())));
initial.commit09911bf2008-07-26 23:55:29443 }
444 return entry;
445}
446
447void NavigationController::LoadURL(const GURL& url,
448 PageTransition::Type transition) {
449 // The user initiated a load, we don't need to reload anymore.
450 needs_reload_ = false;
451
452 NavigationEntry* entry = CreateNavigationEntry(url, transition);
453
454 LoadEntry(entry);
455}
456
457void NavigationController::LoadURLLazily(const GURL& url,
458 PageTransition::Type type,
459 const std::wstring& title,
460 SkBitmap* icon) {
461 NavigationEntry* entry = CreateNavigationEntry(url, type);
[email protected]1e5645ff2008-08-27 18:09:07462 entry->set_title(title);
initial.commit09911bf2008-07-26 23:55:29463 if (icon)
[email protected]1e5645ff2008-08-27 18:09:07464 entry->favicon().set_bitmap(*icon);
initial.commit09911bf2008-07-26 23:55:29465
initial.commit09911bf2008-07-26 23:55:29466 DiscardPendingEntryInternal();
467 pending_entry_ = entry;
468 load_pending_entry_when_active_ = true;
469}
470
471bool NavigationController::LoadingURLLazily() {
472 return load_pending_entry_when_active_;
473}
474
475const std::wstring& NavigationController::GetLazyTitle() const {
476 if (pending_entry_)
[email protected]1e5645ff2008-08-27 18:09:07477 return pending_entry_->title();
initial.commit09911bf2008-07-26 23:55:29478 else
479 return EmptyWString();
480}
481
482const SkBitmap& NavigationController::GetLazyFavIcon() const {
483 if (pending_entry_) {
[email protected]1e5645ff2008-08-27 18:09:07484 return pending_entry_->favicon().bitmap();
initial.commit09911bf2008-07-26 23:55:29485 } else {
486 ResourceBundle &rb = ResourceBundle::GetSharedInstance();
487 return *rb.GetBitmapNamed(IDR_DEFAULT_FAVICON);
488 }
489}
490
[email protected]e9ba4472008-09-14 15:42:43491bool NavigationController::RendererDidNavigate(
492 const ViewHostMsg_FrameNavigate_Params& params,
493 bool is_interstitial,
494 LoadCommittedDetails* details) {
495 // Save the previous URL before we clobber it.
[email protected]ecd9d8702008-08-28 22:10:17496 if (GetLastCommittedEntry())
497 details->previous_url = GetLastCommittedEntry()->url();
498
[email protected]e9ba4472008-09-14 15:42:43499 // Assign the current site instance to any pending entry, so we can find it
500 // later by calling GetEntryIndexWithPageID. We only care about this if the
501 // pending entry is an existing navigation and not a new one (or else we
502 // wouldn't care about finding it with GetEntryIndexWithPageID).
503 //
504 // TODO(brettw) this seems slightly bogus as we don't really know if the
505 // pending entry is what this navigation is for. There is a similar TODO
506 // w.r.t. the pending entry in RendererDidNavigateToNewPage.
507 if (pending_entry_index_ >= 0)
508 pending_entry_->set_site_instance(active_contents_->GetSiteInstance());
509
510 // Do navigation-type specific actions. These will make and commit an entry.
511 switch (ClassifyNavigation(params)) {
512 case NAV_NEW_PAGE:
513 RendererDidNavigateToNewPage(params);
514 break;
515 case NAV_EXISTING_PAGE:
516 RendererDidNavigateToExistingPage(params);
517 break;
518 case NAV_SAME_PAGE:
519 RendererDidNavigateToSamePage(params);
520 break;
521 case NAV_IN_PAGE:
522 RendererDidNavigateInPage(params);
523 break;
524 case NAV_NEW_SUBFRAME:
525 RendererDidNavigateNewSubframe(params);
526 break;
527 case NAV_AUTO_SUBFRAME:
528 if (!RendererDidNavigateAutoSubframe(params))
529 return false;
530 break;
531 case NAV_IGNORE:
532 // There is nothing we can do with this navigation, so we just return to
533 // the caller that nothing has happened.
534 return false;
535 default:
536 NOTREACHED();
[email protected]765b35502008-08-21 00:51:20537 }
538
[email protected]e9ba4472008-09-14 15:42:43539 // All committed entries should have nonempty content state so WebKit doesn't
540 // get confused when we go back to them (see the function for details).
541 SetContentStateIfEmpty(GetActiveEntry());
[email protected]765b35502008-08-21 00:51:20542
[email protected]e9ba4472008-09-14 15:42:43543 // WebKit doesn't set the "auto" transition on meta refreshes properly (bug
544 // 1051891) so we manually set it for redirects which we normally treat as
545 // "non-user-gestures" where we want to update stuff after navigations.
546 //
547 // Note that the redirect check also checks for a pending entry to
548 // differentiate real redirects from browser initiated navigations to a
549 // redirected entry. This happens when you hit back to go to a page that was
550 // the destination of a redirect, we don't want to treat it as a redirect
551 // even though that's what its transition will be. See bug 1117048.
552 //
553 // TODO(brettw) write a test for this complicated logic.
554 details->is_auto = (PageTransition::IsRedirect(params.transition) &&
555 !GetPendingEntry()) ||
556 params.gesture == NavigationGestureAuto;
[email protected]765b35502008-08-21 00:51:20557
[email protected]e9ba4472008-09-14 15:42:43558 // Now prep the rest of the details for the notification and broadcast.
559 details->entry = GetActiveEntry();
560 details->is_in_page = IsURLInPageNavigation(params.url);
561 details->is_main_frame = PageTransition::IsMainFrame(params.transition);
[email protected]f072d2ce2008-09-17 17:16:24562 details->is_interstitial = is_interstitial;
563 details->serialized_security_info = params.security_info;
[email protected]ecd9d8702008-08-28 22:10:17564 NotifyNavigationEntryCommitted(details);
initial.commit09911bf2008-07-26 23:55:29565
[email protected]b33452302008-08-04 19:36:36566 // It is now a safe time to schedule collection for any tab contents of a
567 // different type, because a navigation is necessary to get back to them.
[email protected]50664fd2008-08-28 16:10:30568 ScheduleTabContentsCollectionForInactiveTabs();
[email protected]e9ba4472008-09-14 15:42:43569 return true;
initial.commit09911bf2008-07-26 23:55:29570}
571
[email protected]e9ba4472008-09-14 15:42:43572NavigationController::NavClass NavigationController::ClassifyNavigation(
573 const ViewHostMsg_FrameNavigate_Params& params) const {
574 // If a page makes a popup navigated to about blank, and then writes stuff
575 // like a subframe navigated to a real site, we'll get a notification with an
576 // invalid page ID. There's nothing we can do with these, so just ignore them.
577 if (params.page_id == -1) {
578 DCHECK(!GetActiveEntry()) << "Got an invalid page ID but we seem to be "
579 " navigated to a valid page. This should be impossible.";
580 return NAV_IGNORE;
581 }
582
583 if (params.page_id > active_contents_->GetMaxPageID()) {
584 // Greater page IDs than we've ever seen before are new pages. We may or may
585 // not have a pending entry for the page, and this may or may not be the
586 // main frame.
587 if (PageTransition::IsMainFrame(params.transition))
588 return NAV_NEW_PAGE;
589 return NAV_NEW_SUBFRAME;
590 }
591
592 // Now we know that the notification is for an existing page. Find that entry.
593 int existing_entry_index = GetEntryIndexWithPageID(
594 active_contents_->type(),
595 active_contents_->GetSiteInstance(),
596 params.page_id);
597 if (existing_entry_index == -1) {
598 // The page was not found. It could have been pruned because of the limit on
599 // back/forward entries (not likely since we'll usually tell it to navigate
600 // to such entries). It could also mean that the renderer is smoking crack.
601 NOTREACHED();
602 return NAV_IGNORE;
603 }
604 NavigationEntry* existing_entry = entries_[existing_entry_index].get();
605
606 if (pending_entry_ &&
607 pending_entry_->url() == params.url &&
608 existing_entry != pending_entry_ &&
609 pending_entry_->page_id() == -1 &&
610 pending_entry_->url() == existing_entry->url()) {
611 // In this case, we have a pending entry for a URL but WebCore didn't do a
612 // new navigation. This happens when you press enter in the URL bar to
613 // reload. We will create a pending entry, but WebKit will convert it to
614 // a reload since it's the same page and not create a new entry for it
615 // (the user doesn't want to have a new back/forward entry when they do
616 // this). In this case, we want to just ignore the pending entry and go
617 // back to where we were (the "existing entry").
618 return NAV_SAME_PAGE;
619 }
620
621 if (AreURLsInPageNavigation(existing_entry->url(), params.url))
622 return NAV_IN_PAGE;
623
624 if (!PageTransition::IsMainFrame(params.transition))
625 return NAV_AUTO_SUBFRAME; // All manual subframes would get new IDs and
626 // were handled above.
627 // Since we weeded out "new" navigations above, we know this is an existing
628 // navigation.
629 return NAV_EXISTING_PAGE;
630}
631
632void NavigationController::RendererDidNavigateToNewPage(
633 const ViewHostMsg_FrameNavigate_Params& params) {
634 NavigationEntry* new_entry;
635 if (pending_entry_) {
636 // TODO(brettw) this assumes that the pending entry is appropriate for the
637 // new page that was just loaded. I don't think this is necessarily the
638 // case! We should have some more tracking to know for sure. This goes along
639 // with a similar TODO at the top of RendererDidNavigate where we blindly
640 // set the site instance on the pending entry.
641 new_entry = new NavigationEntry(*pending_entry_);
642
643 // Don't use the page type from the pending entry. Some interstitial page
644 // may have set the type to interstitial. Once we commit, however, the page
645 // type must always be normal.
646 new_entry->set_page_type(NavigationEntry::NORMAL_PAGE);
647 } else {
648 new_entry = new NavigationEntry(active_contents_->type());
649 }
650
651 new_entry->set_url(params.url);
652 new_entry->set_page_id(params.page_id);
653 new_entry->set_transition_type(params.transition);
654 new_entry->set_site_instance(active_contents_->GetSiteInstance());
655 new_entry->set_has_post_data(params.is_post);
656
657 InsertEntry(new_entry);
658}
659
660void NavigationController::RendererDidNavigateToExistingPage(
661 const ViewHostMsg_FrameNavigate_Params& params) {
662 // We should only get here for main frame navigations.
663 DCHECK(PageTransition::IsMainFrame(params.transition));
664
665 // This is a back/forward navigation. The existing page for the ID is
666 // guaranteed to exist, and we just need to update it with new information
667 // from the renderer.
668 int entry_index = GetEntryIndexWithPageID(
669 active_contents_->type(),
670 active_contents_->GetSiteInstance(),
671 params.page_id);
672 DCHECK(entry_index >= 0 &&
673 entry_index < static_cast<int>(entries_.size()));
674 NavigationEntry* entry = entries_[entry_index].get();
675
676 // The URL may have changed due to redirects. The site instance will normally
677 // be the same except during session restore, when no site instance will be
678 // assigned.
679 entry->set_url(params.url);
680 DCHECK(entry->site_instance() == NULL ||
681 entry->site_instance() == active_contents_->GetSiteInstance());
682 entry->set_site_instance(active_contents_->GetSiteInstance());
683
684 // The entry we found in the list might be pending if the user hit
685 // back/forward/reload. This load should commit it (since it's already in the
686 // list, we can just discard the pending pointer).
687 //
688 // Note that we need to use the "internal" version since we don't want to
689 // actually change any other state, just kill the pointer.
690 if (entry == pending_entry_)
691 DiscardPendingEntryInternal();
692
693 int old_committed_entry_index = last_committed_entry_index_;
694 last_committed_entry_index_ = entry_index;
695 IndexOfActiveEntryChanged(old_committed_entry_index);
696}
697
698void NavigationController::RendererDidNavigateToSamePage(
699 const ViewHostMsg_FrameNavigate_Params& params) {
700 // This mode implies we have a pending entry that's the same as an existing
701 // entry for this page ID. All we need to do is update the existing entry.
702 NavigationEntry* existing_entry = GetEntryWithPageID(
703 active_contents_->type(),
704 active_contents_->GetSiteInstance(),
705 params.page_id);
706
707 // We assign the entry's unique ID to be that of the new one. Since this is
708 // always the result of a user action, we want to dismiss infobars, etc. like
709 // a regular user-initiated navigation.
710 existing_entry->set_unique_id(pending_entry_->unique_id());
711
712 DiscardPendingEntry();
713}
714
715void NavigationController::RendererDidNavigateInPage(
716 const ViewHostMsg_FrameNavigate_Params& params) {
717 DCHECK(PageTransition::IsMainFrame(params.transition)) <<
718 "WebKit should only tell us about in-page navs for the main frame.";
719 // We're guaranteed to have an entry for this one.
720 NavigationEntry* existing_entry = GetEntryWithPageID(
721 active_contents_->type(),
722 active_contents_->GetSiteInstance(),
723 params.page_id);
724
725 // Reference fragment navigation. We're guaranteed to have the last_committed
726 // entry and it will be the same page as the new navigation (minus the
727 // reference fragments, of course).
728 NavigationEntry* new_entry = new NavigationEntry(*existing_entry);
729 new_entry->set_page_id(params.page_id);
730 new_entry->set_url(params.url);
731 InsertEntry(new_entry);
732}
733
734void NavigationController::RendererDidNavigateNewSubframe(
735 const ViewHostMsg_FrameNavigate_Params& params) {
736 // Manual subframe navigations just get the current entry cloned so the user
737 // can go back or forward to it. The actual subframe information will be
738 // stored in the page state for each of those entries. This happens out of
739 // band with the actual navigations.
740 DCHECK(GetLastCommittedEntry());
741 NavigationEntry* new_entry = new NavigationEntry(*GetLastCommittedEntry());
742 new_entry->set_page_id(params.page_id);
743 InsertEntry(new_entry);
744}
745
746bool NavigationController::RendererDidNavigateAutoSubframe(
747 const ViewHostMsg_FrameNavigate_Params& params) {
748 // We're guaranteed to have a previously committed entry, and we now need to
749 // handle navigation inside of a subframe in it without creating a new entry.
750 DCHECK(GetLastCommittedEntry());
751
752 // Handle the case where we're navigating back/forward to a previous subframe
753 // navigation entry. This is case "2." in NAV_AUTO_SUBFRAME comment in the
754 // header file. In case "1." this will be a NOP.
755 int entry_index = GetEntryIndexWithPageID(
756 active_contents_->type(),
757 active_contents_->GetSiteInstance(),
758 params.page_id);
759 if (entry_index < 0 ||
760 entry_index >= static_cast<int>(entries_.size())) {
761 NOTREACHED();
762 return false;
763 }
764
765 // Update the current navigation entry in case we're going back/forward.
766 if (entry_index != last_committed_entry_index_) {
767 int old_committed_entry_index = last_committed_entry_index_;
768 last_committed_entry_index_ = entry_index;
769 IndexOfActiveEntryChanged(old_committed_entry_index);
770 return true;
771 }
772 return false;
773}
774
775void NavigationController::CommitPendingEntry() {
776 if (!GetPendingEntry())
777 return; // Nothing to do.
778
779 // Need to save the previous URL for the notification.
780 LoadCommittedDetails details;
781 if (GetLastCommittedEntry())
782 details.previous_url = GetLastCommittedEntry()->url();
783
784 if (pending_entry_index_ >= 0) {
785 // This is a previous navigation (back/forward) that we're just now
786 // committing. Just mark it as committed.
787 int new_entry_index = pending_entry_index_;
788 DiscardPendingEntryInternal();
789
790 // Mark that entry as committed.
791 int old_committed_entry_index = last_committed_entry_index_;
792 last_committed_entry_index_ = new_entry_index;
793 IndexOfActiveEntryChanged(old_committed_entry_index);
794 } else {
795 // This is a new navigation. It's easiest to just copy the entry and insert
796 // it new again, since InsertEntry expects to take ownership and also
797 // discard the pending entry. We also need to synthesize a page ID. We can
798 // only do this because this function will only be called by our custom
799 // TabContents types. For WebContents, the IDs are generated by the
800 // renderer, so we can't do this.
801 pending_entry_->set_page_id(active_contents_->GetMaxPageID() + 1);
802 active_contents_->UpdateMaxPageID(pending_entry_->page_id());
803 InsertEntry(new NavigationEntry(*pending_entry_));
804 }
805
806 // Broadcast the notification of the navigation.
807 details.entry = GetActiveEntry();
808 details.is_auto = false;
809 details.is_in_page = AreURLsInPageNavigation(details.previous_url,
810 details.entry->url());
811 details.is_main_frame = true;
812 NotifyNavigationEntryCommitted(&details);
813}
[email protected]765b35502008-08-21 00:51:20814
815int NavigationController::GetIndexOfEntry(
816 const NavigationEntry* entry) const {
817 const NavigationEntries::const_iterator i(std::find(
818 entries_.begin(),
819 entries_.end(),
820 entry));
821 return (i == entries_.end()) ? -1 : static_cast<int>(i - entries_.begin());
822}
823
[email protected]e9ba4472008-09-14 15:42:43824void NavigationController::RemoveLastEntryForInterstitial() {
[email protected]765b35502008-08-21 00:51:20825 int current_size = static_cast<int>(entries_.size());
826
827 if (current_size > 0) {
828 if (pending_entry_ == entries_[current_size - 1] ||
829 pending_entry_index_ == current_size - 1)
830 DiscardPendingEntryInternal();
831
832 entries_.pop_back();
833
[email protected]e9ba4472008-09-14 15:42:43834 if (last_committed_entry_index_ >= current_size - 1) {
[email protected]765b35502008-08-21 00:51:20835 last_committed_entry_index_ = current_size - 2;
836
[email protected]e9ba4472008-09-14 15:42:43837 // Broadcast the notification of the navigation. This is kind of a hack,
838 // since the navigation wasn't actually committed. But this function is
839 // used for interstital pages, and the UI needs to get updated when the
840 // interstitial page
841 LoadCommittedDetails details;
842 details.entry = GetActiveEntry();
843 details.is_auto = false;
844 details.is_in_page = false;
845 details.is_main_frame = true;
846 NotifyNavigationEntryCommitted(&details);
847 }
848
[email protected]c12bf1a12008-09-17 16:28:49849 NotifyPrunedEntries(this, false, 1);
[email protected]765b35502008-08-21 00:51:20850 }
851}
852
[email protected]e9ba4472008-09-14 15:42:43853void NavigationController::AddDummyEntryForInterstitial(
854 const NavigationEntry& clone_me) {
855 // We need to send a commit notification for this transition.
856 LoadCommittedDetails details;
857 if (GetLastCommittedEntry())
858 details.previous_url = GetLastCommittedEntry()->url();
859
860 NavigationEntry* new_entry = new NavigationEntry(clone_me);
861 InsertEntry(new_entry);
862 // Watch out, don't use clone_me after this. The caller may have passed in a
863 // reference to our pending entry, which means it would have been destroyed.
864
865 details.is_auto = false;
866 details.entry = GetActiveEntry();
867 details.is_in_page = false;
868 details.is_main_frame = true;
869 NotifyNavigationEntryCommitted(&details);
870}
871
872bool NavigationController::IsURLInPageNavigation(const GURL& url) const {
873 NavigationEntry* last_committed = GetLastCommittedEntry();
874 if (!last_committed)
875 return false;
876 return AreURLsInPageNavigation(last_committed->url(), url);
877}
878
initial.commit09911bf2008-07-26 23:55:29879void NavigationController::DiscardPendingEntry() {
[email protected]765b35502008-08-21 00:51:20880 DiscardPendingEntryInternal();
initial.commit09911bf2008-07-26 23:55:29881
882 // Synchronize the active_contents_ to the last committed entry.
883 NavigationEntry* last_entry = GetLastCommittedEntry();
[email protected]1e5645ff2008-08-27 18:09:07884 if (last_entry && last_entry->tab_type() != active_contents_->type()) {
initial.commit09911bf2008-07-26 23:55:29885 TabContents* from_contents = active_contents_;
886 from_contents->SetActive(false);
887
888 // Switch back to the previous tab contents.
[email protected]1e5645ff2008-08-27 18:09:07889 active_contents_ = GetTabContents(last_entry->tab_type());
initial.commit09911bf2008-07-26 23:55:29890 DCHECK(active_contents_);
891
892 active_contents_->SetActive(true);
893
894 // If we are transitioning from two types of WebContents, we need to migrate
895 // the download shelf if it is visible. The download shelf may have been
896 // created before the error that caused us to discard the entry.
897 WebContents::MigrateShelfView(from_contents, active_contents_);
898
899 if (from_contents->delegate()) {
900 from_contents->delegate()->ReplaceContents(from_contents,
901 active_contents_);
902 }
903
904 // The entry we just discarded needed a different TabContents type. We no
905 // longer need it but we can't destroy it just yet because the TabContents
906 // is very likely involved in the current stack.
907 DCHECK(from_contents != active_contents_);
908 ScheduleTabContentsCollection(from_contents->type());
909 }
initial.commit09911bf2008-07-26 23:55:29910}
911
912void NavigationController::InsertEntry(NavigationEntry* entry) {
[email protected]1e5645ff2008-08-27 18:09:07913 DCHECK(entry->transition_type() != PageTransition::AUTO_SUBFRAME);
[email protected]765b35502008-08-21 00:51:20914
915 // Copy the pending entry's unique ID to the committed entry.
916 // I don't know if pending_entry_index_ can be other than -1 here.
917 const NavigationEntry* const pending_entry = (pending_entry_index_ == -1) ?
918 pending_entry_ : entries_[pending_entry_index_].get();
919 if (pending_entry)
920 entry->set_unique_id(pending_entry->unique_id());
921
922 DiscardPendingEntryInternal();
923
924 int current_size = static_cast<int>(entries_.size());
925
926 // Prune any entries which are in front of the current entry.
927 if (current_size > 0) {
[email protected]c12bf1a12008-09-17 16:28:49928 int num_pruned = 0;
[email protected]765b35502008-08-21 00:51:20929 while (last_committed_entry_index_ < (current_size - 1)) {
[email protected]c12bf1a12008-09-17 16:28:49930 num_pruned++;
[email protected]765b35502008-08-21 00:51:20931 entries_.pop_back();
932 current_size--;
933 }
[email protected]c12bf1a12008-09-17 16:28:49934 if (num_pruned > 0) // Only notify if we did prune something.
935 NotifyPrunedEntries(this, false, num_pruned);
[email protected]765b35502008-08-21 00:51:20936 }
937
[email protected]c12bf1a12008-09-17 16:28:49938 if (entries_.size() >= max_entry_count_) {
[email protected]765b35502008-08-21 00:51:20939 RemoveEntryAtIndex(0);
[email protected]c12bf1a12008-09-17 16:28:49940 NotifyPrunedEntries(this, true, 1);
941 }
[email protected]765b35502008-08-21 00:51:20942
943 entries_.push_back(linked_ptr<NavigationEntry>(entry));
944 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
[email protected]e9ba4472008-09-14 15:42:43945
946 // This is a new page ID, so we need everybody to know about it.
947 active_contents_->UpdateMaxPageID(entry->page_id());
[email protected]765b35502008-08-21 00:51:20948
[email protected]e9ba4472008-09-14 15:42:43949 // TODO(brettw) this seems bogus. The tab contents can listen for the
950 // notification or use the details that we pass back to it.
initial.commit09911bf2008-07-26 23:55:29951 active_contents_->NotifyDidNavigate(NAVIGATION_NEW, 0);
952}
953
954void NavigationController::SetWindowID(const SessionID& id) {
955 window_id_ = id;
[email protected]534e54b2008-08-13 15:40:09956 NotificationService::current()->Notify(NOTIFY_TAB_PARENTED,
957 Source<NavigationController>(this),
958 NotificationService::NoDetails());
initial.commit09911bf2008-07-26 23:55:29959}
960
961void NavigationController::NavigateToPendingEntry(bool reload) {
962 TabContents* from_contents = active_contents_;
963
964 // For session history navigations only the pending_entry_index_ is set.
965 if (!pending_entry_) {
966 DCHECK(pending_entry_index_ != -1);
[email protected]765b35502008-08-21 00:51:20967 pending_entry_ = entries_[pending_entry_index_].get();
initial.commit09911bf2008-07-26 23:55:29968 }
969
970 // Reset the security states as any SSL error may have been resolved since we
971 // last visited that page.
[email protected]eb34392b2008-08-19 15:42:20972 pending_entry_->ssl() = NavigationEntry::SSLStatus();
initial.commit09911bf2008-07-26 23:55:29973
[email protected]1e5645ff2008-08-27 18:09:07974 if (from_contents && from_contents->type() != pending_entry_->tab_type())
initial.commit09911bf2008-07-26 23:55:29975 from_contents->SetActive(false);
976
977 HWND parent =
978 from_contents ? GetParent(from_contents->GetContainerHWND()) : 0;
979 TabContents* contents =
980 GetTabContentsCreateIfNecessary(parent, *pending_entry_);
981
982 contents->SetActive(true);
983 active_contents_ = contents;
984
985 if (from_contents && from_contents != contents) {
986 if (from_contents->delegate())
987 from_contents->delegate()->ReplaceContents(from_contents, contents);
988 }
989
[email protected]e9ba4472008-09-14 15:42:43990 NavigationEntry temp_entry(*pending_entry_);
991 if (!contents->NavigateToPendingEntry(reload))
initial.commit09911bf2008-07-26 23:55:29992 DiscardPendingEntry();
initial.commit09911bf2008-07-26 23:55:29993}
994
[email protected]ecd9d8702008-08-28 22:10:17995void NavigationController::NotifyNavigationEntryCommitted(
996 LoadCommittedDetails* details) {
initial.commit09911bf2008-07-26 23:55:29997 // TODO(pkasting): http://b/1113079 Probably these explicit notification paths
998 // should be removed, and interested parties should just listen for the
999 // notification below instead.
1000 ssl_manager_.NavigationStateChanged();
1001 active_contents_->NotifyNavigationStateChanged(
1002 TabContents::INVALIDATE_EVERYTHING);
1003
[email protected]ecd9d8702008-08-28 22:10:171004 details->entry = GetActiveEntry();
1005 NotificationService::current()->Notify(
1006 NOTIFY_NAV_ENTRY_COMMITTED,
1007 Source<NavigationController>(this),
1008 Details<LoadCommittedDetails>(details));
initial.commit09911bf2008-07-26 23:55:291009}
1010
[email protected]e9ba4472008-09-14 15:42:431011void NavigationController::IndexOfActiveEntryChanged(int prev_committed_index) {
initial.commit09911bf2008-07-26 23:55:291012 NavigationType nav_type = NAVIGATION_BACK_FORWARD;
1013 int relative_navigation_offset =
1014 GetLastCommittedEntryIndex() - prev_committed_index;
[email protected]e9ba4472008-09-14 15:42:431015 if (relative_navigation_offset == 0)
initial.commit09911bf2008-07-26 23:55:291016 nav_type = NAVIGATION_REPLACE;
[email protected]e9ba4472008-09-14 15:42:431017
1018 // TODO(brettw) I don't think this call should be necessary. There is already
1019 // a notification of this event that could be used, or maybe all the tab
1020 // contents' know when we navigate (WebContents does).
initial.commit09911bf2008-07-26 23:55:291021 active_contents_->NotifyDidNavigate(nav_type, relative_navigation_offset);
initial.commit09911bf2008-07-26 23:55:291022}
1023
1024TabContents* NavigationController::GetTabContentsCreateIfNecessary(
1025 HWND parent,
1026 const NavigationEntry& entry) {
[email protected]1e5645ff2008-08-27 18:09:071027 TabContents* contents = GetTabContents(entry.tab_type());
initial.commit09911bf2008-07-26 23:55:291028 if (!contents) {
[email protected]1e5645ff2008-08-27 18:09:071029 contents = TabContents::CreateWithType(entry.tab_type(), parent, profile_,
initial.commit09911bf2008-07-26 23:55:291030 entry.site_instance());
1031 if (!contents->AsWebContents()) {
1032 // Update the max page id, otherwise the newly created TabContents may
1033 // have reset its max page id resulting in all new navigations. We only
1034 // do this for non-WebContents as WebContents takes care of this via its
1035 // SiteInstance. If this creation is the result of a restore, WebContents
1036 // handles invoking ReservePageIDRange to make sure the renderer's
1037 // max_page_id is updated to reflect the restored range of page ids.
1038 int32 max_page_id = contents->GetMaxPageID();
1039 for (size_t i = 0; i < entries_.size(); ++i) {
[email protected]1e5645ff2008-08-27 18:09:071040 if (entries_[i]->tab_type() == entry.tab_type())
1041 max_page_id = std::max(max_page_id, entries_[i]->page_id());
initial.commit09911bf2008-07-26 23:55:291042 }
1043 contents->UpdateMaxPageID(max_page_id);
1044 }
1045 RegisterTabContents(contents);
1046 }
1047
1048 // We should not be trying to collect this tab contents.
1049 DCHECK(tab_contents_collector_map_.find(contents->type()) ==
1050 tab_contents_collector_map_.end());
1051
1052 return contents;
1053}
1054
1055void NavigationController::RegisterTabContents(TabContents* some_contents) {
1056 DCHECK(some_contents);
1057 TabContentsType t = some_contents->type();
1058 TabContents* tc;
1059 if ((tc = tab_contents_map_[t]) != some_contents) {
1060 if (tc) {
1061 NOTREACHED() << "Should not happen. Multiple contents for one type";
1062 } else {
1063 tab_contents_map_[t] = some_contents;
1064 some_contents->set_controller(this);
1065 }
1066 }
1067 if (some_contents->AsDOMUIHost())
1068 some_contents->AsDOMUIHost()->AttachMessageHandlers();
1069}
1070
initial.commit09911bf2008-07-26 23:55:291071// static
1072void NavigationController::DisablePromptOnRepost() {
1073 check_for_repost_ = false;
1074}
1075
1076void NavigationController::SetActive(bool is_active) {
1077 if (is_active) {
1078 if (needs_reload_) {
1079 LoadIfNecessary();
1080 } else if (load_pending_entry_when_active_) {
1081 NavigateToPendingEntry(false);
1082 load_pending_entry_when_active_ = false;
1083 }
1084 }
1085}
1086
1087void NavigationController::LoadIfNecessary() {
1088 if (!needs_reload_)
1089 return;
1090
1091 needs_reload_ = false;
1092 // Calling Reload() results in ignoring state, and not loading.
1093 // Explicitly use NavigateToPendingEntry so that the renderer uses the
1094 // cached state.
1095 pending_entry_index_ = last_committed_entry_index_;
1096 NavigateToPendingEntry(false);
1097}
1098
[email protected]534e54b2008-08-13 15:40:091099void NavigationController::NotifyEntryChanged(const NavigationEntry* entry,
1100 int index) {
1101 EntryChangedDetails det;
1102 det.changed_entry = entry;
1103 det.index = index;
1104 NotificationService::current()->Notify(NOTIFY_NAV_ENTRY_CHANGED,
1105 Source<NavigationController>(this),
1106 Details<EntryChangedDetails>(&det));
initial.commit09911bf2008-07-26 23:55:291107}
1108
[email protected]765b35502008-08-21 00:51:201109void NavigationController::RemoveEntryAtIndex(int index) {
1110 // TODO(brettw) this is only called to remove the first one when we've got
1111 // too many entries. It should probably be more specific for this case.
1112 if (index >= static_cast<int>(entries_.size()) ||
1113 index == pending_entry_index_ || index == last_committed_entry_index_) {
1114 NOTREACHED();
1115 return;
1116 }
1117
1118 entries_.erase(entries_.begin() + index);
1119
1120 if (last_committed_entry_index_ >= index) {
1121 if (!entries_.empty())
1122 last_committed_entry_index_--;
1123 else
1124 last_committed_entry_index_ = -1;
1125 }
1126
1127 // TODO(brettw) bug 1324021: we probably need some notification here so the
1128 // session service can stay in sync.
1129}
1130
initial.commit09911bf2008-07-26 23:55:291131NavigationController* NavigationController::Clone(HWND parent_hwnd) {
1132 NavigationController* nc = new NavigationController(NULL, profile_);
1133
1134 if (GetEntryCount() == 0)
1135 return nc;
1136
1137 nc->needs_reload_ = true;
1138
1139 nc->entries_.reserve(entries_.size());
[email protected]765b35502008-08-21 00:51:201140 for (int i = 0, c = GetEntryCount(); i < c; ++i) {
1141 nc->entries_.push_back(linked_ptr<NavigationEntry>(
1142 new NavigationEntry(*GetEntryAtIndex(i))));
1143 }
initial.commit09911bf2008-07-26 23:55:291144
1145 nc->FinishRestore(parent_hwnd, last_committed_entry_index_);
1146
1147 return nc;
1148}
1149
[email protected]50664fd2008-08-28 16:10:301150void NavigationController::ScheduleTabContentsCollectionForInactiveTabs() {
1151 int index = GetCurrentEntryIndex();
1152 if (index < 0 || GetPendingEntryIndex() != -1)
1153 return;
1154
1155 TabContentsType active_type = GetEntryAtIndex(index)->tab_type();
1156 for (TabContentsMap::iterator i = tab_contents_map_.begin();
1157 i != tab_contents_map_.end(); ++i) {
1158 if (i->first != active_type)
1159 ScheduleTabContentsCollection(i->first);
1160 }
1161}
1162
initial.commit09911bf2008-07-26 23:55:291163void NavigationController::ScheduleTabContentsCollection(TabContentsType t) {
1164 TabContentsCollectorMap::const_iterator i =
1165 tab_contents_collector_map_.find(t);
1166
1167 // The tab contents is already scheduled for collection.
1168 if (i != tab_contents_collector_map_.end())
1169 return;
1170
1171 // If we currently don't have a TabContents for t, skip.
1172 if (tab_contents_map_.find(t) == tab_contents_map_.end())
1173 return;
1174
1175 // Create a collector and schedule it.
1176 TabContentsCollector* tcc = new TabContentsCollector(this, t);
1177 tab_contents_collector_map_[t] = tcc;
1178 MessageLoop::current()->PostTask(FROM_HERE, tcc);
1179}
1180
1181void NavigationController::CancelTabContentsCollection(TabContentsType t) {
1182 TabContentsCollectorMap::iterator i = tab_contents_collector_map_.find(t);
1183
1184 if (i != tab_contents_collector_map_.end()) {
1185 DCHECK(i->second);
1186 i->second->Cancel();
1187 tab_contents_collector_map_.erase(i);
1188 }
1189}
1190
1191void NavigationController::FinishRestore(HWND parent_hwnd, int selected_index) {
1192 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
1193 ConfigureEntriesForRestore(&entries_);
1194
1195 set_max_restored_page_id(GetEntryCount());
1196
1197 last_committed_entry_index_ = selected_index;
1198
1199 // Callers assume we have an active_contents after restoring, so set it now.
1200 active_contents_ =
1201 GetTabContentsCreateIfNecessary(parent_hwnd, *entries_[selected_index]);
1202}
[email protected]765b35502008-08-21 00:51:201203
1204void NavigationController::DiscardPendingEntryInternal() {
1205 if (pending_entry_index_ == -1)
1206 delete pending_entry_;
1207 pending_entry_ = NULL;
1208 pending_entry_index_ = -1;
1209}
1210
1211int NavigationController::GetEntryIndexWithPageID(
1212 TabContentsType type, SiteInstance* instance, int32 page_id) const {
[email protected]765b35502008-08-21 00:51:201213 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
[email protected]1e5645ff2008-08-27 18:09:071214 if ((entries_[i]->tab_type() == type) &&
[email protected]765b35502008-08-21 00:51:201215 (entries_[i]->site_instance() == instance) &&
[email protected]1e5645ff2008-08-27 18:09:071216 (entries_[i]->page_id() == page_id))
[email protected]765b35502008-08-21 00:51:201217 return i;
1218 }
1219 return -1;
1220}