blob: ae5a9b85ae3c2f987e20cc7c4414b9ca955a2bf2 [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;
[email protected]4c27ba82008-09-24 16:49:09589
590 // When this is a new subframe navigation, we should have a committed page
591 // for which it's a suframe in. This may not be the case when an iframe is
592 // navigated on a popup navigated to about:blank (the iframe would be
593 // written into the popup by script on the main page). For these cases,
594 // there isn't any navigation stuff we can do, so just ignore it.
595 if (!GetLastCommittedEntry())
596 return NAV_IGNORE;
597
598 // Valid subframe navigation.
[email protected]e9ba4472008-09-14 15:42:43599 return NAV_NEW_SUBFRAME;
600 }
601
602 // Now we know that the notification is for an existing page. Find that entry.
603 int existing_entry_index = GetEntryIndexWithPageID(
604 active_contents_->type(),
605 active_contents_->GetSiteInstance(),
606 params.page_id);
607 if (existing_entry_index == -1) {
608 // The page was not found. It could have been pruned because of the limit on
609 // back/forward entries (not likely since we'll usually tell it to navigate
610 // to such entries). It could also mean that the renderer is smoking crack.
611 NOTREACHED();
612 return NAV_IGNORE;
613 }
614 NavigationEntry* existing_entry = entries_[existing_entry_index].get();
615
616 if (pending_entry_ &&
617 pending_entry_->url() == params.url &&
618 existing_entry != pending_entry_ &&
619 pending_entry_->page_id() == -1 &&
620 pending_entry_->url() == existing_entry->url()) {
621 // In this case, we have a pending entry for a URL but WebCore didn't do a
622 // new navigation. This happens when you press enter in the URL bar to
623 // reload. We will create a pending entry, but WebKit will convert it to
624 // a reload since it's the same page and not create a new entry for it
625 // (the user doesn't want to have a new back/forward entry when they do
626 // this). In this case, we want to just ignore the pending entry and go
627 // back to where we were (the "existing entry").
628 return NAV_SAME_PAGE;
629 }
630
631 if (AreURLsInPageNavigation(existing_entry->url(), params.url))
632 return NAV_IN_PAGE;
633
[email protected]4c27ba82008-09-24 16:49:09634 if (!PageTransition::IsMainFrame(params.transition)) {
635 // All manual subframes would get new IDs and were handled above, so we
636 // know this is auto. Since the current page was found in the navigation
637 // entry list, we're guaranteed to have a last committed entry.
638 DCHECK(GetLastCommittedEntry());
639 return NAV_AUTO_SUBFRAME;
640 }
641
[email protected]e9ba4472008-09-14 15:42:43642 // Since we weeded out "new" navigations above, we know this is an existing
[email protected]4c27ba82008-09-24 16:49:09643 // (back/forward) navigation.
[email protected]e9ba4472008-09-14 15:42:43644 return NAV_EXISTING_PAGE;
645}
646
647void NavigationController::RendererDidNavigateToNewPage(
648 const ViewHostMsg_FrameNavigate_Params& params) {
649 NavigationEntry* new_entry;
650 if (pending_entry_) {
651 // TODO(brettw) this assumes that the pending entry is appropriate for the
652 // new page that was just loaded. I don't think this is necessarily the
653 // case! We should have some more tracking to know for sure. This goes along
654 // with a similar TODO at the top of RendererDidNavigate where we blindly
655 // set the site instance on the pending entry.
656 new_entry = new NavigationEntry(*pending_entry_);
657
658 // Don't use the page type from the pending entry. Some interstitial page
659 // may have set the type to interstitial. Once we commit, however, the page
660 // type must always be normal.
661 new_entry->set_page_type(NavigationEntry::NORMAL_PAGE);
662 } else {
663 new_entry = new NavigationEntry(active_contents_->type());
664 }
665
666 new_entry->set_url(params.url);
667 new_entry->set_page_id(params.page_id);
668 new_entry->set_transition_type(params.transition);
669 new_entry->set_site_instance(active_contents_->GetSiteInstance());
670 new_entry->set_has_post_data(params.is_post);
671
672 InsertEntry(new_entry);
673}
674
675void NavigationController::RendererDidNavigateToExistingPage(
676 const ViewHostMsg_FrameNavigate_Params& params) {
677 // We should only get here for main frame navigations.
678 DCHECK(PageTransition::IsMainFrame(params.transition));
679
680 // This is a back/forward navigation. The existing page for the ID is
[email protected]4c27ba82008-09-24 16:49:09681 // guaranteed to exist by ClassifyNavigation, and we just need to update it
682 // with new information from the renderer.
[email protected]e9ba4472008-09-14 15:42:43683 int entry_index = GetEntryIndexWithPageID(
684 active_contents_->type(),
685 active_contents_->GetSiteInstance(),
686 params.page_id);
687 DCHECK(entry_index >= 0 &&
688 entry_index < static_cast<int>(entries_.size()));
689 NavigationEntry* entry = entries_[entry_index].get();
690
691 // The URL may have changed due to redirects. The site instance will normally
692 // be the same except during session restore, when no site instance will be
693 // assigned.
694 entry->set_url(params.url);
695 DCHECK(entry->site_instance() == NULL ||
696 entry->site_instance() == active_contents_->GetSiteInstance());
697 entry->set_site_instance(active_contents_->GetSiteInstance());
698
699 // The entry we found in the list might be pending if the user hit
700 // back/forward/reload. This load should commit it (since it's already in the
701 // list, we can just discard the pending pointer).
702 //
703 // Note that we need to use the "internal" version since we don't want to
704 // actually change any other state, just kill the pointer.
705 if (entry == pending_entry_)
706 DiscardPendingEntryInternal();
707
708 int old_committed_entry_index = last_committed_entry_index_;
709 last_committed_entry_index_ = entry_index;
710 IndexOfActiveEntryChanged(old_committed_entry_index);
711}
712
713void NavigationController::RendererDidNavigateToSamePage(
714 const ViewHostMsg_FrameNavigate_Params& params) {
715 // This mode implies we have a pending entry that's the same as an existing
[email protected]4c27ba82008-09-24 16:49:09716 // entry for this page ID. This entry is guaranteed to exist by
717 // ClassifyNavigation. All we need to do is update the existing entry.
[email protected]e9ba4472008-09-14 15:42:43718 NavigationEntry* existing_entry = GetEntryWithPageID(
719 active_contents_->type(),
720 active_contents_->GetSiteInstance(),
721 params.page_id);
722
723 // We assign the entry's unique ID to be that of the new one. Since this is
724 // always the result of a user action, we want to dismiss infobars, etc. like
725 // a regular user-initiated navigation.
726 existing_entry->set_unique_id(pending_entry_->unique_id());
727
728 DiscardPendingEntry();
729}
730
731void NavigationController::RendererDidNavigateInPage(
732 const ViewHostMsg_FrameNavigate_Params& params) {
733 DCHECK(PageTransition::IsMainFrame(params.transition)) <<
734 "WebKit should only tell us about in-page navs for the main frame.";
735 // We're guaranteed to have an entry for this one.
736 NavigationEntry* existing_entry = GetEntryWithPageID(
737 active_contents_->type(),
738 active_contents_->GetSiteInstance(),
739 params.page_id);
740
741 // Reference fragment navigation. We're guaranteed to have the last_committed
742 // entry and it will be the same page as the new navigation (minus the
743 // reference fragments, of course).
744 NavigationEntry* new_entry = new NavigationEntry(*existing_entry);
745 new_entry->set_page_id(params.page_id);
746 new_entry->set_url(params.url);
747 InsertEntry(new_entry);
748}
749
750void NavigationController::RendererDidNavigateNewSubframe(
751 const ViewHostMsg_FrameNavigate_Params& params) {
752 // Manual subframe navigations just get the current entry cloned so the user
753 // can go back or forward to it. The actual subframe information will be
754 // stored in the page state for each of those entries. This happens out of
755 // band with the actual navigations.
[email protected]4c27ba82008-09-24 16:49:09756 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
757 << "that a last committed entry exists.";
[email protected]e9ba4472008-09-14 15:42:43758 NavigationEntry* new_entry = new NavigationEntry(*GetLastCommittedEntry());
759 new_entry->set_page_id(params.page_id);
760 InsertEntry(new_entry);
761}
762
763bool NavigationController::RendererDidNavigateAutoSubframe(
764 const ViewHostMsg_FrameNavigate_Params& params) {
765 // We're guaranteed to have a previously committed entry, and we now need to
766 // handle navigation inside of a subframe in it without creating a new entry.
767 DCHECK(GetLastCommittedEntry());
768
769 // Handle the case where we're navigating back/forward to a previous subframe
770 // navigation entry. This is case "2." in NAV_AUTO_SUBFRAME comment in the
771 // header file. In case "1." this will be a NOP.
772 int entry_index = GetEntryIndexWithPageID(
773 active_contents_->type(),
774 active_contents_->GetSiteInstance(),
775 params.page_id);
776 if (entry_index < 0 ||
777 entry_index >= static_cast<int>(entries_.size())) {
778 NOTREACHED();
779 return false;
780 }
781
782 // Update the current navigation entry in case we're going back/forward.
783 if (entry_index != last_committed_entry_index_) {
784 int old_committed_entry_index = last_committed_entry_index_;
785 last_committed_entry_index_ = entry_index;
786 IndexOfActiveEntryChanged(old_committed_entry_index);
787 return true;
788 }
789 return false;
790}
791
792void NavigationController::CommitPendingEntry() {
793 if (!GetPendingEntry())
794 return; // Nothing to do.
795
796 // Need to save the previous URL for the notification.
797 LoadCommittedDetails details;
798 if (GetLastCommittedEntry())
799 details.previous_url = GetLastCommittedEntry()->url();
800
801 if (pending_entry_index_ >= 0) {
802 // This is a previous navigation (back/forward) that we're just now
803 // committing. Just mark it as committed.
804 int new_entry_index = pending_entry_index_;
805 DiscardPendingEntryInternal();
806
807 // Mark that entry as committed.
808 int old_committed_entry_index = last_committed_entry_index_;
809 last_committed_entry_index_ = new_entry_index;
810 IndexOfActiveEntryChanged(old_committed_entry_index);
811 } else {
812 // This is a new navigation. It's easiest to just copy the entry and insert
813 // it new again, since InsertEntry expects to take ownership and also
814 // discard the pending entry. We also need to synthesize a page ID. We can
815 // only do this because this function will only be called by our custom
816 // TabContents types. For WebContents, the IDs are generated by the
817 // renderer, so we can't do this.
818 pending_entry_->set_page_id(active_contents_->GetMaxPageID() + 1);
819 active_contents_->UpdateMaxPageID(pending_entry_->page_id());
820 InsertEntry(new NavigationEntry(*pending_entry_));
821 }
822
823 // Broadcast the notification of the navigation.
824 details.entry = GetActiveEntry();
825 details.is_auto = false;
826 details.is_in_page = AreURLsInPageNavigation(details.previous_url,
827 details.entry->url());
828 details.is_main_frame = true;
829 NotifyNavigationEntryCommitted(&details);
830}
[email protected]765b35502008-08-21 00:51:20831
832int NavigationController::GetIndexOfEntry(
833 const NavigationEntry* entry) const {
834 const NavigationEntries::const_iterator i(std::find(
835 entries_.begin(),
836 entries_.end(),
837 entry));
838 return (i == entries_.end()) ? -1 : static_cast<int>(i - entries_.begin());
839}
840
[email protected]e9ba4472008-09-14 15:42:43841void NavigationController::RemoveLastEntryForInterstitial() {
[email protected]765b35502008-08-21 00:51:20842 int current_size = static_cast<int>(entries_.size());
843
844 if (current_size > 0) {
845 if (pending_entry_ == entries_[current_size - 1] ||
846 pending_entry_index_ == current_size - 1)
847 DiscardPendingEntryInternal();
848
849 entries_.pop_back();
850
[email protected]e9ba4472008-09-14 15:42:43851 if (last_committed_entry_index_ >= current_size - 1) {
[email protected]765b35502008-08-21 00:51:20852 last_committed_entry_index_ = current_size - 2;
853
[email protected]e9ba4472008-09-14 15:42:43854 // Broadcast the notification of the navigation. This is kind of a hack,
855 // since the navigation wasn't actually committed. But this function is
856 // used for interstital pages, and the UI needs to get updated when the
857 // interstitial page
858 LoadCommittedDetails details;
859 details.entry = GetActiveEntry();
860 details.is_auto = false;
861 details.is_in_page = false;
862 details.is_main_frame = true;
863 NotifyNavigationEntryCommitted(&details);
864 }
865
[email protected]c12bf1a12008-09-17 16:28:49866 NotifyPrunedEntries(this, false, 1);
[email protected]765b35502008-08-21 00:51:20867 }
868}
869
[email protected]e9ba4472008-09-14 15:42:43870void NavigationController::AddDummyEntryForInterstitial(
871 const NavigationEntry& clone_me) {
872 // We need to send a commit notification for this transition.
873 LoadCommittedDetails details;
874 if (GetLastCommittedEntry())
875 details.previous_url = GetLastCommittedEntry()->url();
876
877 NavigationEntry* new_entry = new NavigationEntry(clone_me);
878 InsertEntry(new_entry);
879 // Watch out, don't use clone_me after this. The caller may have passed in a
880 // reference to our pending entry, which means it would have been destroyed.
881
882 details.is_auto = false;
883 details.entry = GetActiveEntry();
884 details.is_in_page = false;
885 details.is_main_frame = true;
886 NotifyNavigationEntryCommitted(&details);
887}
888
889bool NavigationController::IsURLInPageNavigation(const GURL& url) const {
890 NavigationEntry* last_committed = GetLastCommittedEntry();
891 if (!last_committed)
892 return false;
893 return AreURLsInPageNavigation(last_committed->url(), url);
894}
895
initial.commit09911bf2008-07-26 23:55:29896void NavigationController::DiscardPendingEntry() {
[email protected]765b35502008-08-21 00:51:20897 DiscardPendingEntryInternal();
initial.commit09911bf2008-07-26 23:55:29898
899 // Synchronize the active_contents_ to the last committed entry.
900 NavigationEntry* last_entry = GetLastCommittedEntry();
[email protected]1e5645ff2008-08-27 18:09:07901 if (last_entry && last_entry->tab_type() != active_contents_->type()) {
initial.commit09911bf2008-07-26 23:55:29902 TabContents* from_contents = active_contents_;
903 from_contents->SetActive(false);
904
905 // Switch back to the previous tab contents.
[email protected]1e5645ff2008-08-27 18:09:07906 active_contents_ = GetTabContents(last_entry->tab_type());
initial.commit09911bf2008-07-26 23:55:29907 DCHECK(active_contents_);
908
909 active_contents_->SetActive(true);
910
911 // If we are transitioning from two types of WebContents, we need to migrate
912 // the download shelf if it is visible. The download shelf may have been
913 // created before the error that caused us to discard the entry.
914 WebContents::MigrateShelfView(from_contents, active_contents_);
915
916 if (from_contents->delegate()) {
917 from_contents->delegate()->ReplaceContents(from_contents,
918 active_contents_);
919 }
920
921 // The entry we just discarded needed a different TabContents type. We no
922 // longer need it but we can't destroy it just yet because the TabContents
923 // is very likely involved in the current stack.
924 DCHECK(from_contents != active_contents_);
925 ScheduleTabContentsCollection(from_contents->type());
926 }
initial.commit09911bf2008-07-26 23:55:29927}
928
929void NavigationController::InsertEntry(NavigationEntry* entry) {
[email protected]1e5645ff2008-08-27 18:09:07930 DCHECK(entry->transition_type() != PageTransition::AUTO_SUBFRAME);
[email protected]765b35502008-08-21 00:51:20931
932 // Copy the pending entry's unique ID to the committed entry.
933 // I don't know if pending_entry_index_ can be other than -1 here.
934 const NavigationEntry* const pending_entry = (pending_entry_index_ == -1) ?
935 pending_entry_ : entries_[pending_entry_index_].get();
936 if (pending_entry)
937 entry->set_unique_id(pending_entry->unique_id());
938
939 DiscardPendingEntryInternal();
940
941 int current_size = static_cast<int>(entries_.size());
942
943 // Prune any entries which are in front of the current entry.
944 if (current_size > 0) {
[email protected]c12bf1a12008-09-17 16:28:49945 int num_pruned = 0;
[email protected]765b35502008-08-21 00:51:20946 while (last_committed_entry_index_ < (current_size - 1)) {
[email protected]c12bf1a12008-09-17 16:28:49947 num_pruned++;
[email protected]765b35502008-08-21 00:51:20948 entries_.pop_back();
949 current_size--;
950 }
[email protected]c12bf1a12008-09-17 16:28:49951 if (num_pruned > 0) // Only notify if we did prune something.
952 NotifyPrunedEntries(this, false, num_pruned);
[email protected]765b35502008-08-21 00:51:20953 }
954
[email protected]c12bf1a12008-09-17 16:28:49955 if (entries_.size() >= max_entry_count_) {
[email protected]765b35502008-08-21 00:51:20956 RemoveEntryAtIndex(0);
[email protected]c12bf1a12008-09-17 16:28:49957 NotifyPrunedEntries(this, true, 1);
958 }
[email protected]765b35502008-08-21 00:51:20959
960 entries_.push_back(linked_ptr<NavigationEntry>(entry));
961 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
[email protected]e9ba4472008-09-14 15:42:43962
963 // This is a new page ID, so we need everybody to know about it.
964 active_contents_->UpdateMaxPageID(entry->page_id());
[email protected]765b35502008-08-21 00:51:20965
[email protected]e9ba4472008-09-14 15:42:43966 // TODO(brettw) this seems bogus. The tab contents can listen for the
967 // notification or use the details that we pass back to it.
initial.commit09911bf2008-07-26 23:55:29968 active_contents_->NotifyDidNavigate(NAVIGATION_NEW, 0);
969}
970
971void NavigationController::SetWindowID(const SessionID& id) {
972 window_id_ = id;
[email protected]534e54b2008-08-13 15:40:09973 NotificationService::current()->Notify(NOTIFY_TAB_PARENTED,
974 Source<NavigationController>(this),
975 NotificationService::NoDetails());
initial.commit09911bf2008-07-26 23:55:29976}
977
978void NavigationController::NavigateToPendingEntry(bool reload) {
979 TabContents* from_contents = active_contents_;
980
981 // For session history navigations only the pending_entry_index_ is set.
982 if (!pending_entry_) {
983 DCHECK(pending_entry_index_ != -1);
[email protected]765b35502008-08-21 00:51:20984 pending_entry_ = entries_[pending_entry_index_].get();
initial.commit09911bf2008-07-26 23:55:29985 }
986
987 // Reset the security states as any SSL error may have been resolved since we
988 // last visited that page.
[email protected]eb34392b2008-08-19 15:42:20989 pending_entry_->ssl() = NavigationEntry::SSLStatus();
initial.commit09911bf2008-07-26 23:55:29990
[email protected]1e5645ff2008-08-27 18:09:07991 if (from_contents && from_contents->type() != pending_entry_->tab_type())
initial.commit09911bf2008-07-26 23:55:29992 from_contents->SetActive(false);
993
994 HWND parent =
995 from_contents ? GetParent(from_contents->GetContainerHWND()) : 0;
996 TabContents* contents =
997 GetTabContentsCreateIfNecessary(parent, *pending_entry_);
998
999 contents->SetActive(true);
1000 active_contents_ = contents;
1001
1002 if (from_contents && from_contents != contents) {
1003 if (from_contents->delegate())
1004 from_contents->delegate()->ReplaceContents(from_contents, contents);
1005 }
1006
[email protected]e9ba4472008-09-14 15:42:431007 NavigationEntry temp_entry(*pending_entry_);
1008 if (!contents->NavigateToPendingEntry(reload))
initial.commit09911bf2008-07-26 23:55:291009 DiscardPendingEntry();
initial.commit09911bf2008-07-26 23:55:291010}
1011
[email protected]ecd9d8702008-08-28 22:10:171012void NavigationController::NotifyNavigationEntryCommitted(
1013 LoadCommittedDetails* details) {
initial.commit09911bf2008-07-26 23:55:291014 // TODO(pkasting): http://b/1113079 Probably these explicit notification paths
1015 // should be removed, and interested parties should just listen for the
1016 // notification below instead.
1017 ssl_manager_.NavigationStateChanged();
1018 active_contents_->NotifyNavigationStateChanged(
1019 TabContents::INVALIDATE_EVERYTHING);
1020
[email protected]ecd9d8702008-08-28 22:10:171021 details->entry = GetActiveEntry();
1022 NotificationService::current()->Notify(
1023 NOTIFY_NAV_ENTRY_COMMITTED,
1024 Source<NavigationController>(this),
1025 Details<LoadCommittedDetails>(details));
initial.commit09911bf2008-07-26 23:55:291026}
1027
[email protected]e9ba4472008-09-14 15:42:431028void NavigationController::IndexOfActiveEntryChanged(int prev_committed_index) {
initial.commit09911bf2008-07-26 23:55:291029 NavigationType nav_type = NAVIGATION_BACK_FORWARD;
1030 int relative_navigation_offset =
1031 GetLastCommittedEntryIndex() - prev_committed_index;
[email protected]e9ba4472008-09-14 15:42:431032 if (relative_navigation_offset == 0)
initial.commit09911bf2008-07-26 23:55:291033 nav_type = NAVIGATION_REPLACE;
[email protected]e9ba4472008-09-14 15:42:431034
1035 // TODO(brettw) I don't think this call should be necessary. There is already
1036 // a notification of this event that could be used, or maybe all the tab
1037 // contents' know when we navigate (WebContents does).
initial.commit09911bf2008-07-26 23:55:291038 active_contents_->NotifyDidNavigate(nav_type, relative_navigation_offset);
initial.commit09911bf2008-07-26 23:55:291039}
1040
1041TabContents* NavigationController::GetTabContentsCreateIfNecessary(
1042 HWND parent,
1043 const NavigationEntry& entry) {
[email protected]1e5645ff2008-08-27 18:09:071044 TabContents* contents = GetTabContents(entry.tab_type());
initial.commit09911bf2008-07-26 23:55:291045 if (!contents) {
[email protected]1e5645ff2008-08-27 18:09:071046 contents = TabContents::CreateWithType(entry.tab_type(), parent, profile_,
initial.commit09911bf2008-07-26 23:55:291047 entry.site_instance());
1048 if (!contents->AsWebContents()) {
1049 // Update the max page id, otherwise the newly created TabContents may
1050 // have reset its max page id resulting in all new navigations. We only
1051 // do this for non-WebContents as WebContents takes care of this via its
1052 // SiteInstance. If this creation is the result of a restore, WebContents
1053 // handles invoking ReservePageIDRange to make sure the renderer's
1054 // max_page_id is updated to reflect the restored range of page ids.
1055 int32 max_page_id = contents->GetMaxPageID();
1056 for (size_t i = 0; i < entries_.size(); ++i) {
[email protected]1e5645ff2008-08-27 18:09:071057 if (entries_[i]->tab_type() == entry.tab_type())
1058 max_page_id = std::max(max_page_id, entries_[i]->page_id());
initial.commit09911bf2008-07-26 23:55:291059 }
1060 contents->UpdateMaxPageID(max_page_id);
1061 }
1062 RegisterTabContents(contents);
1063 }
1064
1065 // We should not be trying to collect this tab contents.
1066 DCHECK(tab_contents_collector_map_.find(contents->type()) ==
1067 tab_contents_collector_map_.end());
1068
1069 return contents;
1070}
1071
1072void NavigationController::RegisterTabContents(TabContents* some_contents) {
1073 DCHECK(some_contents);
1074 TabContentsType t = some_contents->type();
1075 TabContents* tc;
1076 if ((tc = tab_contents_map_[t]) != some_contents) {
1077 if (tc) {
1078 NOTREACHED() << "Should not happen. Multiple contents for one type";
1079 } else {
1080 tab_contents_map_[t] = some_contents;
1081 some_contents->set_controller(this);
1082 }
1083 }
1084 if (some_contents->AsDOMUIHost())
1085 some_contents->AsDOMUIHost()->AttachMessageHandlers();
1086}
1087
initial.commit09911bf2008-07-26 23:55:291088// static
1089void NavigationController::DisablePromptOnRepost() {
1090 check_for_repost_ = false;
1091}
1092
1093void NavigationController::SetActive(bool is_active) {
1094 if (is_active) {
1095 if (needs_reload_) {
1096 LoadIfNecessary();
1097 } else if (load_pending_entry_when_active_) {
1098 NavigateToPendingEntry(false);
1099 load_pending_entry_when_active_ = false;
1100 }
1101 }
1102}
1103
1104void NavigationController::LoadIfNecessary() {
1105 if (!needs_reload_)
1106 return;
1107
1108 needs_reload_ = false;
1109 // Calling Reload() results in ignoring state, and not loading.
1110 // Explicitly use NavigateToPendingEntry so that the renderer uses the
1111 // cached state.
1112 pending_entry_index_ = last_committed_entry_index_;
1113 NavigateToPendingEntry(false);
1114}
1115
[email protected]534e54b2008-08-13 15:40:091116void NavigationController::NotifyEntryChanged(const NavigationEntry* entry,
1117 int index) {
1118 EntryChangedDetails det;
1119 det.changed_entry = entry;
1120 det.index = index;
1121 NotificationService::current()->Notify(NOTIFY_NAV_ENTRY_CHANGED,
1122 Source<NavigationController>(this),
1123 Details<EntryChangedDetails>(&det));
initial.commit09911bf2008-07-26 23:55:291124}
1125
[email protected]765b35502008-08-21 00:51:201126void NavigationController::RemoveEntryAtIndex(int index) {
1127 // TODO(brettw) this is only called to remove the first one when we've got
1128 // too many entries. It should probably be more specific for this case.
1129 if (index >= static_cast<int>(entries_.size()) ||
1130 index == pending_entry_index_ || index == last_committed_entry_index_) {
1131 NOTREACHED();
1132 return;
1133 }
1134
1135 entries_.erase(entries_.begin() + index);
1136
1137 if (last_committed_entry_index_ >= index) {
1138 if (!entries_.empty())
1139 last_committed_entry_index_--;
1140 else
1141 last_committed_entry_index_ = -1;
1142 }
1143
1144 // TODO(brettw) bug 1324021: we probably need some notification here so the
1145 // session service can stay in sync.
1146}
1147
initial.commit09911bf2008-07-26 23:55:291148NavigationController* NavigationController::Clone(HWND parent_hwnd) {
1149 NavigationController* nc = new NavigationController(NULL, profile_);
1150
1151 if (GetEntryCount() == 0)
1152 return nc;
1153
1154 nc->needs_reload_ = true;
1155
1156 nc->entries_.reserve(entries_.size());
[email protected]765b35502008-08-21 00:51:201157 for (int i = 0, c = GetEntryCount(); i < c; ++i) {
1158 nc->entries_.push_back(linked_ptr<NavigationEntry>(
1159 new NavigationEntry(*GetEntryAtIndex(i))));
1160 }
initial.commit09911bf2008-07-26 23:55:291161
1162 nc->FinishRestore(parent_hwnd, last_committed_entry_index_);
1163
1164 return nc;
1165}
1166
[email protected]50664fd2008-08-28 16:10:301167void NavigationController::ScheduleTabContentsCollectionForInactiveTabs() {
1168 int index = GetCurrentEntryIndex();
1169 if (index < 0 || GetPendingEntryIndex() != -1)
1170 return;
1171
1172 TabContentsType active_type = GetEntryAtIndex(index)->tab_type();
1173 for (TabContentsMap::iterator i = tab_contents_map_.begin();
1174 i != tab_contents_map_.end(); ++i) {
1175 if (i->first != active_type)
1176 ScheduleTabContentsCollection(i->first);
1177 }
1178}
1179
initial.commit09911bf2008-07-26 23:55:291180void NavigationController::ScheduleTabContentsCollection(TabContentsType t) {
1181 TabContentsCollectorMap::const_iterator i =
1182 tab_contents_collector_map_.find(t);
1183
1184 // The tab contents is already scheduled for collection.
1185 if (i != tab_contents_collector_map_.end())
1186 return;
1187
1188 // If we currently don't have a TabContents for t, skip.
1189 if (tab_contents_map_.find(t) == tab_contents_map_.end())
1190 return;
1191
1192 // Create a collector and schedule it.
1193 TabContentsCollector* tcc = new TabContentsCollector(this, t);
1194 tab_contents_collector_map_[t] = tcc;
1195 MessageLoop::current()->PostTask(FROM_HERE, tcc);
1196}
1197
1198void NavigationController::CancelTabContentsCollection(TabContentsType t) {
1199 TabContentsCollectorMap::iterator i = tab_contents_collector_map_.find(t);
1200
1201 if (i != tab_contents_collector_map_.end()) {
1202 DCHECK(i->second);
1203 i->second->Cancel();
1204 tab_contents_collector_map_.erase(i);
1205 }
1206}
1207
1208void NavigationController::FinishRestore(HWND parent_hwnd, int selected_index) {
1209 DCHECK(selected_index >= 0 && selected_index < GetEntryCount());
1210 ConfigureEntriesForRestore(&entries_);
1211
1212 set_max_restored_page_id(GetEntryCount());
1213
1214 last_committed_entry_index_ = selected_index;
1215
1216 // Callers assume we have an active_contents after restoring, so set it now.
1217 active_contents_ =
1218 GetTabContentsCreateIfNecessary(parent_hwnd, *entries_[selected_index]);
1219}
[email protected]765b35502008-08-21 00:51:201220
1221void NavigationController::DiscardPendingEntryInternal() {
1222 if (pending_entry_index_ == -1)
1223 delete pending_entry_;
1224 pending_entry_ = NULL;
1225 pending_entry_index_ = -1;
1226}
1227
1228int NavigationController::GetEntryIndexWithPageID(
1229 TabContentsType type, SiteInstance* instance, int32 page_id) const {
[email protected]765b35502008-08-21 00:51:201230 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
[email protected]1e5645ff2008-08-27 18:09:071231 if ((entries_[i]->tab_type() == type) &&
[email protected]765b35502008-08-21 00:51:201232 (entries_[i]->site_instance() == instance) &&
[email protected]1e5645ff2008-08-27 18:09:071233 (entries_[i]->page_id() == page_id))
[email protected]765b35502008-08-21 00:51:201234 return i;
1235 }
1236 return -1;
1237}