Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: chrome/browser/tab_contents/navigation_controller.cc

Issue 6537015: Start moving core pieces of Chrome multi-process code to src\content. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2010 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.
4
5 #include "chrome/browser/tab_contents/navigation_controller.h"
6
7 #include "base/file_util.h"
8 #include "base/logging.h"
9 #include "base/string_util.h"
10 #include "base/time.h"
11 #include "base/utf_string_conversions.h"
12 #include "chrome/browser/browser_about_handler.h"
13 #include "chrome/browser/browser_url_handler.h"
14 #include "chrome/browser/in_process_webkit/session_storage_namespace.h"
15 #include "chrome/browser/prefs/pref_service.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/renderer_host/site_instance.h"
18 #include "chrome/browser/sessions/session_types.h"
19 #include "chrome/browser/tab_contents/interstitial_page.h"
20 #include "chrome/browser/tab_contents/navigation_entry.h"
21 #include "chrome/browser/tab_contents/tab_contents.h"
22 #include "chrome/browser/tab_contents/tab_contents_delegate.h"
23 #include "chrome/common/chrome_constants.h"
24 #include "chrome/common/navigation_types.h"
25 #include "chrome/common/notification_service.h"
26 #include "chrome/common/pref_names.h"
27 #include "chrome/common/render_messages_params.h"
28 #include "chrome/common/url_constants.h"
29 #include "grit/app_resources.h"
30 #include "net/base/escape.h"
31 #include "net/base/net_util.h"
32 #include "net/base/mime_util.h"
33 #include "webkit/glue/webkit_glue.h"
34
35 namespace {
36
37 const int kInvalidateAllButShelves =
38 0xFFFFFFFF & ~TabContents::INVALIDATE_BOOKMARK_BAR;
39
40 // Invoked when entries have been pruned, or removed. For example, if the
41 // current entries are [google, digg, yahoo], with the current entry google,
42 // and the user types in cnet, then digg and yahoo are pruned.
43 void NotifyPrunedEntries(NavigationController* nav_controller,
44 bool from_front,
45 int count) {
46 NavigationController::PrunedDetails details;
47 details.from_front = from_front;
48 details.count = count;
49 NotificationService::current()->Notify(
50 NotificationType::NAV_LIST_PRUNED,
51 Source<NavigationController>(nav_controller),
52 Details<NavigationController::PrunedDetails>(&details));
53 }
54
55 // Ensure the given NavigationEntry has a valid state, so that WebKit does not
56 // get confused if we navigate back to it.
57 //
58 // An empty state is treated as a new navigation by WebKit, which would mean
59 // losing the navigation entries and generating a new navigation entry after
60 // this one. We don't want that. To avoid this we create a valid state which
61 // WebKit will not treat as a new navigation.
62 void SetContentStateIfEmpty(NavigationEntry* entry) {
63 if (entry->content_state().empty()) {
64 entry->set_content_state(
65 webkit_glue::CreateHistoryStateForURL(entry->url()));
66 }
67 }
68
69 // Configure all the NavigationEntries in entries for restore. This resets
70 // the transition type to reload and makes sure the content state isn't empty.
71 void ConfigureEntriesForRestore(
72 std::vector<linked_ptr<NavigationEntry> >* entries,
73 bool from_last_session) {
74 for (size_t i = 0; i < entries->size(); ++i) {
75 // Use a transition type of reload so that we don't incorrectly increase
76 // the typed count.
77 (*entries)[i]->set_transition_type(PageTransition::RELOAD);
78 (*entries)[i]->set_restore_type(from_last_session ?
79 NavigationEntry::RESTORE_LAST_SESSION :
80 NavigationEntry::RESTORE_CURRENT_SESSION);
81 // NOTE(darin): This code is only needed for backwards compat.
82 SetContentStateIfEmpty((*entries)[i].get());
83 }
84 }
85
86 // See NavigationController::IsURLInPageNavigation for how this works and why.
87 bool AreURLsInPageNavigation(const GURL& existing_url, const GURL& new_url) {
88 if (existing_url == new_url || !new_url.has_ref()) {
89 // TODO(jcampan): what about when navigating back from a ref URL to the top
90 // non ref URL? Nothing is loaded in that case but we return false here.
91 // The user could also navigate from the ref URL to the non ref URL by
92 // entering the non ref URL in the location bar or through a bookmark, in
93 // which case there would be a load. I am not sure if the non-load/load
94 // scenarios can be differentiated with the TransitionType.
95 return false;
96 }
97
98 url_canon::Replacements<char> replacements;
99 replacements.ClearRef();
100 return existing_url.ReplaceComponents(replacements) ==
101 new_url.ReplaceComponents(replacements);
102 }
103
104 } // namespace
105
106 // NavigationController ---------------------------------------------------
107
108 // static
109 size_t NavigationController::max_entry_count_ =
110 chrome::kMaxSessionHistoryEntries;
111
112 // static
113 bool NavigationController::check_for_repost_ = true;
114
115 NavigationController::NavigationController(
116 TabContents* contents,
117 Profile* profile,
118 SessionStorageNamespace* session_storage_namespace)
119 : profile_(profile),
120 pending_entry_(NULL),
121 last_committed_entry_index_(-1),
122 pending_entry_index_(-1),
123 transient_entry_index_(-1),
124 tab_contents_(contents),
125 max_restored_page_id_(-1),
126 ALLOW_THIS_IN_INITIALIZER_LIST(ssl_manager_(this)),
127 needs_reload_(false),
128 session_storage_namespace_(session_storage_namespace),
129 pending_reload_(NO_RELOAD) {
130 DCHECK(profile_);
131 if (!session_storage_namespace_)
132 session_storage_namespace_ = new SessionStorageNamespace(profile_);
133 }
134
135 NavigationController::~NavigationController() {
136 DiscardNonCommittedEntriesInternal();
137
138 NotificationService::current()->Notify(
139 NotificationType::TAB_CLOSED,
140 Source<NavigationController>(this),
141 NotificationService::NoDetails());
142 }
143
144 void NavigationController::RestoreFromState(
145 const std::vector<TabNavigation>& navigations,
146 int selected_navigation,
147 bool from_last_session) {
148 // Verify that this controller is unused and that the input is valid.
149 DCHECK(entry_count() == 0 && !pending_entry());
150 DCHECK(selected_navigation >= 0 &&
151 selected_navigation < static_cast<int>(navigations.size()));
152
153 // Populate entries_ from the supplied TabNavigations.
154 needs_reload_ = true;
155 CreateNavigationEntriesFromTabNavigations(navigations, &entries_);
156
157 // And finish the restore.
158 FinishRestore(selected_navigation, from_last_session);
159 }
160
161 void NavigationController::Reload(bool check_for_repost) {
162 ReloadInternal(check_for_repost, RELOAD);
163 }
164 void NavigationController::ReloadIgnoringCache(bool check_for_repost) {
165 ReloadInternal(check_for_repost, RELOAD_IGNORING_CACHE);
166 }
167
168 void NavigationController::ReloadInternal(bool check_for_repost,
169 ReloadType reload_type) {
170 // Reloading a transient entry does nothing.
171 if (transient_entry_index_ != -1)
172 return;
173
174 DiscardNonCommittedEntriesInternal();
175 int current_index = GetCurrentEntryIndex();
176 // If we are no where, then we can't reload. TODO(darin): We should add a
177 // CanReload method.
178 if (current_index == -1) {
179 return;
180 }
181
182 if (check_for_repost_ && check_for_repost &&
183 GetEntryAtIndex(current_index)->has_post_data()) {
184 // The user is asking to reload a page with POST data. Prompt to make sure
185 // they really want to do this. If they do, the dialog will call us back
186 // with check_for_repost = false.
187 NotificationService::current()->Notify(
188 NotificationType::REPOST_WARNING_SHOWN,
189 Source<NavigationController>(this),
190 NotificationService::NoDetails());
191
192 pending_reload_ = reload_type;
193 tab_contents_->Activate();
194 tab_contents_->delegate()->ShowRepostFormWarningDialog(tab_contents_);
195 } else {
196 DiscardNonCommittedEntriesInternal();
197
198 pending_entry_index_ = current_index;
199 entries_[pending_entry_index_]->set_transition_type(PageTransition::RELOAD);
200 NavigateToPendingEntry(reload_type);
201 }
202 }
203
204 void NavigationController::CancelPendingReload() {
205 DCHECK(pending_reload_ != NO_RELOAD);
206 pending_reload_ = NO_RELOAD;
207 }
208
209 void NavigationController::ContinuePendingReload() {
210 if (pending_reload_ == NO_RELOAD) {
211 NOTREACHED();
212 } else {
213 ReloadInternal(false, pending_reload_);
214 pending_reload_ = NO_RELOAD;
215 }
216 }
217
218 bool NavigationController::IsInitialNavigation() {
219 return last_document_loaded_.is_null();
220 }
221
222 // static
223 NavigationEntry* NavigationController::CreateNavigationEntry(
224 const GURL& url, const GURL& referrer, PageTransition::Type transition,
225 Profile* profile) {
226 // Allow the browser URL handler to rewrite the URL. This will, for example,
227 // remove "view-source:" from the beginning of the URL to get the URL that
228 // will actually be loaded. This real URL won't be shown to the user, just
229 // used internally.
230 GURL loaded_url(url);
231 bool reverse_on_redirect = false;
232 BrowserURLHandler::RewriteURLIfNecessary(
233 &loaded_url, profile, &reverse_on_redirect);
234
235 NavigationEntry* entry = new NavigationEntry(
236 NULL, // The site instance for tabs is sent on navigation
237 // (TabContents::GetSiteInstance).
238 -1,
239 loaded_url,
240 referrer,
241 string16(),
242 transition);
243 entry->set_virtual_url(url);
244 entry->set_user_typed_url(url);
245 entry->set_update_virtual_url_with_url(reverse_on_redirect);
246 if (url.SchemeIsFile()) {
247 // Use the filename as the title, not the full path.
248 // We need to call FormatUrl() to perform URL de-escaping;
249 // it's a bit ugly to grab the filename out of the resulting string.
250 std::string languages =
251 profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
252 std::wstring formatted = UTF16ToWideHack(net::FormatUrl(url, languages));
253 std::wstring filename =
254 FilePath::FromWStringHack(formatted).BaseName().ToWStringHack();
255 entry->set_title(WideToUTF16Hack(filename));
256 }
257 return entry;
258 }
259
260 NavigationEntry* NavigationController::GetEntryWithPageID(
261 SiteInstance* instance, int32 page_id) const {
262 int index = GetEntryIndexWithPageID(instance, page_id);
263 return (index != -1) ? entries_[index].get() : NULL;
264 }
265
266 void NavigationController::LoadEntry(NavigationEntry* entry) {
267 // Handle non-navigational URLs that popup dialogs and such, these should not
268 // actually navigate.
269 if (HandleNonNavigationAboutURL(entry->url()))
270 return;
271
272 // When navigating to a new page, we don't know for sure if we will actually
273 // end up leaving the current page. The new page load could for example
274 // result in a download or a 'no content' response (e.g., a mailto: URL).
275 DiscardNonCommittedEntriesInternal();
276 pending_entry_ = entry;
277 NotificationService::current()->Notify(
278 NotificationType::NAV_ENTRY_PENDING,
279 Source<NavigationController>(this),
280 NotificationService::NoDetails());
281 NavigateToPendingEntry(NO_RELOAD);
282 }
283
284 NavigationEntry* NavigationController::GetActiveEntry() const {
285 if (transient_entry_index_ != -1)
286 return entries_[transient_entry_index_].get();
287 if (pending_entry_)
288 return pending_entry_;
289 return GetLastCommittedEntry();
290 }
291
292 int NavigationController::GetCurrentEntryIndex() const {
293 if (transient_entry_index_ != -1)
294 return transient_entry_index_;
295 if (pending_entry_index_ != -1)
296 return pending_entry_index_;
297 return last_committed_entry_index_;
298 }
299
300 NavigationEntry* NavigationController::GetLastCommittedEntry() const {
301 if (last_committed_entry_index_ == -1)
302 return NULL;
303 return entries_[last_committed_entry_index_].get();
304 }
305
306 bool NavigationController::CanViewSource() const {
307 bool is_supported_mime_type = net::IsSupportedNonImageMimeType(
308 tab_contents_->contents_mime_type().c_str());
309 NavigationEntry* active_entry = GetActiveEntry();
310 return active_entry && !active_entry->IsViewSourceMode() &&
311 is_supported_mime_type;
312 }
313
314 NavigationEntry* NavigationController::GetEntryAtOffset(int offset) const {
315 int index = (transient_entry_index_ != -1) ?
316 transient_entry_index_ + offset :
317 last_committed_entry_index_ + offset;
318 if (index < 0 || index >= entry_count())
319 return NULL;
320
321 return entries_[index].get();
322 }
323
324 bool NavigationController::CanGoBack() const {
325 return entries_.size() > 1 && GetCurrentEntryIndex() > 0;
326 }
327
328 bool NavigationController::CanGoForward() const {
329 int index = GetCurrentEntryIndex();
330 return index >= 0 && index < (static_cast<int>(entries_.size()) - 1);
331 }
332
333 void NavigationController::GoBack() {
334 if (!CanGoBack()) {
335 NOTREACHED();
336 return;
337 }
338
339 // If an interstitial page is showing, going back is equivalent to hiding the
340 // interstitial.
341 if (tab_contents_->interstitial_page()) {
342 tab_contents_->interstitial_page()->DontProceed();
343 return;
344 }
345
346 // Base the navigation on where we are now...
347 int current_index = GetCurrentEntryIndex();
348
349 DiscardNonCommittedEntries();
350
351 pending_entry_index_ = current_index - 1;
352 entries_[pending_entry_index_]->set_transition_type(
353 entries_[pending_entry_index_]->transition_type() |
354 PageTransition::FORWARD_BACK);
355 NavigateToPendingEntry(NO_RELOAD);
356 }
357
358 void NavigationController::GoForward() {
359 if (!CanGoForward()) {
360 NOTREACHED();
361 return;
362 }
363
364 // If an interstitial page is showing, the previous renderer is blocked and
365 // cannot make new requests. Unblock (and disable) it to allow this
366 // navigation to succeed. The interstitial will stay visible until the
367 // resulting DidNavigate.
368 if (tab_contents_->interstitial_page()) {
369 tab_contents_->interstitial_page()->CancelForNavigation();
370 }
371
372 bool transient = (transient_entry_index_ != -1);
373
374 // Base the navigation on where we are now...
375 int current_index = GetCurrentEntryIndex();
376
377 DiscardNonCommittedEntries();
378
379 pending_entry_index_ = current_index;
380 // If there was a transient entry, we removed it making the current index
381 // the next page.
382 if (!transient)
383 pending_entry_index_++;
384
385 entries_[pending_entry_index_]->set_transition_type(
386 entries_[pending_entry_index_]->transition_type() |
387 PageTransition::FORWARD_BACK);
388 NavigateToPendingEntry(NO_RELOAD);
389 }
390
391 void NavigationController::GoToIndex(int index) {
392 if (index < 0 || index >= static_cast<int>(entries_.size())) {
393 NOTREACHED();
394 return;
395 }
396
397 if (transient_entry_index_ != -1) {
398 if (index == transient_entry_index_) {
399 // Nothing to do when navigating to the transient.
400 return;
401 }
402 if (index > transient_entry_index_) {
403 // Removing the transient is goint to shift all entries by 1.
404 index--;
405 }
406 }
407
408 // If an interstitial page is showing, the previous renderer is blocked and
409 // cannot make new requests.
410 if (tab_contents_->interstitial_page()) {
411 if (index == GetCurrentEntryIndex() - 1) {
412 // Going back one entry is equivalent to hiding the interstitial.
413 tab_contents_->interstitial_page()->DontProceed();
414 return;
415 } else {
416 // Unblock the renderer (and disable the interstitial) to allow this
417 // navigation to succeed. The interstitial will stay visible until the
418 // resulting DidNavigate.
419 tab_contents_->interstitial_page()->CancelForNavigation();
420 }
421 }
422
423 DiscardNonCommittedEntries();
424
425 pending_entry_index_ = index;
426 entries_[pending_entry_index_]->set_transition_type(
427 entries_[pending_entry_index_]->transition_type() |
428 PageTransition::FORWARD_BACK);
429 NavigateToPendingEntry(NO_RELOAD);
430 }
431
432 void NavigationController::GoToOffset(int offset) {
433 int index = (transient_entry_index_ != -1) ?
434 transient_entry_index_ + offset :
435 last_committed_entry_index_ + offset;
436 if (index < 0 || index >= entry_count())
437 return;
438
439 GoToIndex(index);
440 }
441
442 void NavigationController::RemoveEntryAtIndex(int index,
443 const GURL& default_url) {
444 int size = static_cast<int>(entries_.size());
445 DCHECK(index < size);
446
447 DiscardNonCommittedEntries();
448
449 entries_.erase(entries_.begin() + index);
450
451 if (last_committed_entry_index_ == index) {
452 last_committed_entry_index_--;
453 // We removed the currently shown entry, so we have to load something else.
454 if (last_committed_entry_index_ != -1) {
455 pending_entry_index_ = last_committed_entry_index_;
456 NavigateToPendingEntry(NO_RELOAD);
457 } else {
458 // If there is nothing to show, show a default page.
459 LoadURL(default_url.is_empty() ? GURL("about:blank") : default_url,
460 GURL(), PageTransition::START_PAGE);
461 }
462 } else if (last_committed_entry_index_ > index) {
463 last_committed_entry_index_--;
464 }
465 }
466
467 void NavigationController::UpdateVirtualURLToURL(
468 NavigationEntry* entry, const GURL& new_url) {
469 GURL new_virtual_url(new_url);
470 if (BrowserURLHandler::ReverseURLRewrite(
471 &new_virtual_url, entry->virtual_url(), profile_)) {
472 entry->set_virtual_url(new_virtual_url);
473 }
474 }
475
476 void NavigationController::AddTransientEntry(NavigationEntry* entry) {
477 // Discard any current transient entry, we can only have one at a time.
478 int index = 0;
479 if (last_committed_entry_index_ != -1)
480 index = last_committed_entry_index_ + 1;
481 DiscardTransientEntry();
482 entries_.insert(entries_.begin() + index, linked_ptr<NavigationEntry>(entry));
483 transient_entry_index_ = index;
484 tab_contents_->NotifyNavigationStateChanged(kInvalidateAllButShelves);
485 }
486
487 void NavigationController::LoadURL(const GURL& url, const GURL& referrer,
488 PageTransition::Type transition) {
489 // The user initiated a load, we don't need to reload anymore.
490 needs_reload_ = false;
491
492 NavigationEntry* entry = CreateNavigationEntry(url, referrer, transition,
493 profile_);
494
495 LoadEntry(entry);
496 }
497
498 void NavigationController::DocumentLoadedInFrame() {
499 last_document_loaded_ = base::TimeTicks::Now();
500 }
501
502 bool NavigationController::RendererDidNavigate(
503 const ViewHostMsg_FrameNavigate_Params& params,
504 int extra_invalidate_flags,
505 LoadCommittedDetails* details) {
506
507 // Save the previous state before we clobber it.
508 if (GetLastCommittedEntry()) {
509 details->previous_url = GetLastCommittedEntry()->url();
510 details->previous_entry_index = last_committed_entry_index();
511 } else {
512 details->previous_url = GURL();
513 details->previous_entry_index = -1;
514 }
515
516 // Assign the current site instance to any pending entry, so we can find it
517 // later by calling GetEntryIndexWithPageID. We only care about this if the
518 // pending entry is an existing navigation and not a new one (or else we
519 // wouldn't care about finding it with GetEntryIndexWithPageID).
520 //
521 // TODO(brettw) this seems slightly bogus as we don't really know if the
522 // pending entry is what this navigation is for. There is a similar TODO
523 // w.r.t. the pending entry in RendererDidNavigateToNewPage.
524 if (pending_entry_index_ >= 0) {
525 pending_entry_->set_site_instance(tab_contents_->GetSiteInstance());
526 pending_entry_->set_restore_type(NavigationEntry::RESTORE_NONE);
527 }
528
529 // is_in_page must be computed before the entry gets committed.
530 details->is_in_page = IsURLInPageNavigation(params.url);
531
532 // Do navigation-type specific actions. These will make and commit an entry.
533 details->type = ClassifyNavigation(params);
534
535 switch (details->type) {
536 case NavigationType::NEW_PAGE:
537 RendererDidNavigateToNewPage(params, &(details->did_replace_entry));
538 break;
539 case NavigationType::EXISTING_PAGE:
540 RendererDidNavigateToExistingPage(params);
541 break;
542 case NavigationType::SAME_PAGE:
543 RendererDidNavigateToSamePage(params);
544 break;
545 case NavigationType::IN_PAGE:
546 RendererDidNavigateInPage(params, &(details->did_replace_entry));
547 break;
548 case NavigationType::NEW_SUBFRAME:
549 RendererDidNavigateNewSubframe(params);
550 break;
551 case NavigationType::AUTO_SUBFRAME:
552 if (!RendererDidNavigateAutoSubframe(params))
553 return false;
554 break;
555 case NavigationType::NAV_IGNORE:
556 // There is nothing we can do with this navigation, so we just return to
557 // the caller that nothing has happened.
558 return false;
559 default:
560 NOTREACHED();
561 }
562
563 // All committed entries should have nonempty content state so WebKit doesn't
564 // get confused when we go back to them (see the function for details).
565 SetContentStateIfEmpty(GetActiveEntry());
566
567 // WebKit doesn't set the "auto" transition on meta refreshes properly (bug
568 // 1051891) so we manually set it for redirects which we normally treat as
569 // "non-user-gestures" where we want to update stuff after navigations.
570 //
571 // Note that the redirect check also checks for a pending entry to
572 // differentiate real redirects from browser initiated navigations to a
573 // redirected entry. This happens when you hit back to go to a page that was
574 // the destination of a redirect, we don't want to treat it as a redirect
575 // even though that's what its transition will be. See bug 1117048.
576 //
577 // TODO(brettw) write a test for this complicated logic.
578 details->is_auto = (PageTransition::IsRedirect(params.transition) &&
579 !pending_entry()) ||
580 params.gesture == NavigationGestureAuto;
581
582 // Now prep the rest of the details for the notification and broadcast.
583 details->entry = GetActiveEntry();
584 details->is_main_frame = PageTransition::IsMainFrame(params.transition);
585 details->serialized_security_info = params.security_info;
586 details->is_content_filtered = params.is_content_filtered;
587 details->http_status_code = params.http_status_code;
588 NotifyNavigationEntryCommitted(details, extra_invalidate_flags);
589
590 return true;
591 }
592
593 NavigationType::Type NavigationController::ClassifyNavigation(
594 const ViewHostMsg_FrameNavigate_Params& params) const {
595 if (params.page_id == -1) {
596 // The renderer generates the page IDs, and so if it gives us the invalid
597 // page ID (-1) we know it didn't actually navigate. This happens in a few
598 // cases:
599 //
600 // - If a page makes a popup navigated to about blank, and then writes
601 // stuff like a subframe navigated to a real page. We'll get the commit
602 // for the subframe, but there won't be any commit for the outer page.
603 //
604 // - We were also getting these for failed loads (for example, bug 21849).
605 // The guess is that we get a "load commit" for the alternate error page,
606 // but that doesn't affect the page ID, so we get the "old" one, which
607 // could be invalid. This can also happen for a cross-site transition
608 // that causes us to swap processes. Then the error page load will be in
609 // a new process with no page IDs ever assigned (and hence a -1 value),
610 // yet the navigation controller still might have previous pages in its
611 // list.
612 //
613 // In these cases, there's nothing we can do with them, so ignore.
614 return NavigationType::NAV_IGNORE;
615 }
616
617 if (params.page_id > tab_contents_->GetMaxPageID()) {
618 // Greater page IDs than we've ever seen before are new pages. We may or may
619 // not have a pending entry for the page, and this may or may not be the
620 // main frame.
621 if (PageTransition::IsMainFrame(params.transition))
622 return NavigationType::NEW_PAGE;
623
624 // When this is a new subframe navigation, we should have a committed page
625 // for which it's a suframe in. This may not be the case when an iframe is
626 // navigated on a popup navigated to about:blank (the iframe would be
627 // written into the popup by script on the main page). For these cases,
628 // there isn't any navigation stuff we can do, so just ignore it.
629 if (!GetLastCommittedEntry())
630 return NavigationType::NAV_IGNORE;
631
632 // Valid subframe navigation.
633 return NavigationType::NEW_SUBFRAME;
634 }
635
636 // Now we know that the notification is for an existing page. Find that entry.
637 int existing_entry_index = GetEntryIndexWithPageID(
638 tab_contents_->GetSiteInstance(),
639 params.page_id);
640 if (existing_entry_index == -1) {
641 // The page was not found. It could have been pruned because of the limit on
642 // back/forward entries (not likely since we'll usually tell it to navigate
643 // to such entries). It could also mean that the renderer is smoking crack.
644 NOTREACHED();
645 return NavigationType::NAV_IGNORE;
646 }
647 NavigationEntry* existing_entry = entries_[existing_entry_index].get();
648
649 if (!PageTransition::IsMainFrame(params.transition)) {
650 // All manual subframes would get new IDs and were handled above, so we
651 // know this is auto. Since the current page was found in the navigation
652 // entry list, we're guaranteed to have a last committed entry.
653 DCHECK(GetLastCommittedEntry());
654 return NavigationType::AUTO_SUBFRAME;
655 }
656
657 // Anything below here we know is a main frame navigation.
658 if (pending_entry_ &&
659 existing_entry != pending_entry_ &&
660 pending_entry_->page_id() == -1) {
661 // In this case, we have a pending entry for a URL but WebCore didn't do a
662 // new navigation. This happens when you press enter in the URL bar to
663 // reload. We will create a pending entry, but WebKit will convert it to
664 // a reload since it's the same page and not create a new entry for it
665 // (the user doesn't want to have a new back/forward entry when they do
666 // this). In this case, we want to just ignore the pending entry and go
667 // back to where we were (the "existing entry").
668 return NavigationType::SAME_PAGE;
669 }
670
671 // Any toplevel navigations with the same base (minus the reference fragment)
672 // are in-page navigations. We weeded out subframe navigations above. Most of
673 // the time this doesn't matter since WebKit doesn't tell us about subframe
674 // navigations that don't actually navigate, but it can happen when there is
675 // an encoding override (it always sends a navigation request).
676 if (AreURLsInPageNavigation(existing_entry->url(), params.url))
677 return NavigationType::IN_PAGE;
678
679 // Since we weeded out "new" navigations above, we know this is an existing
680 // (back/forward) navigation.
681 return NavigationType::EXISTING_PAGE;
682 }
683
684 bool NavigationController::IsRedirect(
685 const ViewHostMsg_FrameNavigate_Params& params) {
686 // For main frame transition, we judge by params.transition.
687 // Otherwise, by params.redirects.
688 if (PageTransition::IsMainFrame(params.transition)) {
689 return PageTransition::IsRedirect(params.transition);
690 }
691 return params.redirects.size() > 1;
692 }
693
694 void NavigationController::CreateNavigationEntriesFromTabNavigations(
695 const std::vector<TabNavigation>& navigations,
696 std::vector<linked_ptr<NavigationEntry> >* entries) {
697 // Create a NavigationEntry for each of the navigations.
698 int page_id = 0;
699 for (std::vector<TabNavigation>::const_iterator i =
700 navigations.begin(); i != navigations.end(); ++i, ++page_id) {
701 linked_ptr<NavigationEntry> entry(i->ToNavigationEntry(page_id, profile_));
702 entries->push_back(entry);
703 }
704 }
705
706 void NavigationController::RendererDidNavigateToNewPage(
707 const ViewHostMsg_FrameNavigate_Params& params, bool* did_replace_entry) {
708 NavigationEntry* new_entry;
709 if (pending_entry_) {
710 // TODO(brettw) this assumes that the pending entry is appropriate for the
711 // new page that was just loaded. I don't think this is necessarily the
712 // case! We should have some more tracking to know for sure. This goes along
713 // with a similar TODO at the top of RendererDidNavigate where we blindly
714 // set the site instance on the pending entry.
715 new_entry = new NavigationEntry(*pending_entry_);
716
717 // Don't use the page type from the pending entry. Some interstitial page
718 // may have set the type to interstitial. Once we commit, however, the page
719 // type must always be normal.
720 new_entry->set_page_type(NORMAL_PAGE);
721 } else {
722 new_entry = new NavigationEntry;
723 }
724
725 new_entry->set_url(params.url);
726 if (new_entry->update_virtual_url_with_url())
727 UpdateVirtualURLToURL(new_entry, params.url);
728 new_entry->set_referrer(params.referrer);
729 new_entry->set_page_id(params.page_id);
730 new_entry->set_transition_type(params.transition);
731 new_entry->set_site_instance(tab_contents_->GetSiteInstance());
732 new_entry->set_has_post_data(params.is_post);
733
734 InsertOrReplaceEntry(new_entry, *did_replace_entry);
735 }
736
737 void NavigationController::RendererDidNavigateToExistingPage(
738 const ViewHostMsg_FrameNavigate_Params& params) {
739 // We should only get here for main frame navigations.
740 DCHECK(PageTransition::IsMainFrame(params.transition));
741
742 // This is a back/forward navigation. The existing page for the ID is
743 // guaranteed to exist by ClassifyNavigation, and we just need to update it
744 // with new information from the renderer.
745 int entry_index = GetEntryIndexWithPageID(tab_contents_->GetSiteInstance(),
746 params.page_id);
747 DCHECK(entry_index >= 0 &&
748 entry_index < static_cast<int>(entries_.size()));
749 NavigationEntry* entry = entries_[entry_index].get();
750
751 // The URL may have changed due to redirects. The site instance will normally
752 // be the same except during session restore, when no site instance will be
753 // assigned.
754 entry->set_url(params.url);
755 if (entry->update_virtual_url_with_url())
756 UpdateVirtualURLToURL(entry, params.url);
757 DCHECK(entry->site_instance() == NULL ||
758 entry->site_instance() == tab_contents_->GetSiteInstance());
759 entry->set_site_instance(tab_contents_->GetSiteInstance());
760
761 entry->set_has_post_data(params.is_post);
762
763 // The entry we found in the list might be pending if the user hit
764 // back/forward/reload. This load should commit it (since it's already in the
765 // list, we can just discard the pending pointer).
766 //
767 // Note that we need to use the "internal" version since we don't want to
768 // actually change any other state, just kill the pointer.
769 if (entry == pending_entry_)
770 DiscardNonCommittedEntriesInternal();
771
772 // If a transient entry was removed, the indices might have changed, so we
773 // have to query the entry index again.
774 last_committed_entry_index_ =
775 GetEntryIndexWithPageID(tab_contents_->GetSiteInstance(), params.page_id);
776 }
777
778 void NavigationController::RendererDidNavigateToSamePage(
779 const ViewHostMsg_FrameNavigate_Params& params) {
780 // This mode implies we have a pending entry that's the same as an existing
781 // entry for this page ID. This entry is guaranteed to exist by
782 // ClassifyNavigation. All we need to do is update the existing entry.
783 NavigationEntry* existing_entry = GetEntryWithPageID(
784 tab_contents_->GetSiteInstance(),
785 params.page_id);
786
787 // We assign the entry's unique ID to be that of the new one. Since this is
788 // always the result of a user action, we want to dismiss infobars, etc. like
789 // a regular user-initiated navigation.
790 existing_entry->set_unique_id(pending_entry_->unique_id());
791
792 // The URL may have changed due to redirects.
793 if (existing_entry->update_virtual_url_with_url())
794 UpdateVirtualURLToURL(existing_entry, params.url);
795 existing_entry->set_url(params.url);
796
797 DiscardNonCommittedEntries();
798 }
799
800 void NavigationController::RendererDidNavigateInPage(
801 const ViewHostMsg_FrameNavigate_Params& params, bool* did_replace_entry) {
802 DCHECK(PageTransition::IsMainFrame(params.transition)) <<
803 "WebKit should only tell us about in-page navs for the main frame.";
804 // We're guaranteed to have an entry for this one.
805 NavigationEntry* existing_entry = GetEntryWithPageID(
806 tab_contents_->GetSiteInstance(),
807 params.page_id);
808
809 // Reference fragment navigation. We're guaranteed to have the last_committed
810 // entry and it will be the same page as the new navigation (minus the
811 // reference fragments, of course).
812 NavigationEntry* new_entry = new NavigationEntry(*existing_entry);
813 new_entry->set_page_id(params.page_id);
814 if (new_entry->update_virtual_url_with_url())
815 UpdateVirtualURLToURL(new_entry, params.url);
816 new_entry->set_url(params.url);
817
818 // This replaces the existing entry since the page ID didn't change.
819 *did_replace_entry = true;
820 InsertOrReplaceEntry(new_entry, true);
821 }
822
823 void NavigationController::RendererDidNavigateNewSubframe(
824 const ViewHostMsg_FrameNavigate_Params& params) {
825 if (PageTransition::StripQualifier(params.transition) ==
826 PageTransition::AUTO_SUBFRAME) {
827 // This is not user-initiated. Ignore.
828 return;
829 }
830
831 // Manual subframe navigations just get the current entry cloned so the user
832 // can go back or forward to it. The actual subframe information will be
833 // stored in the page state for each of those entries. This happens out of
834 // band with the actual navigations.
835 DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee "
836 << "that a last committed entry exists.";
837 NavigationEntry* new_entry = new NavigationEntry(*GetLastCommittedEntry());
838 new_entry->set_page_id(params.page_id);
839 InsertOrReplaceEntry(new_entry, false);
840 }
841
842 bool NavigationController::RendererDidNavigateAutoSubframe(
843 const ViewHostMsg_FrameNavigate_Params& params) {
844 // We're guaranteed to have a previously committed entry, and we now need to
845 // handle navigation inside of a subframe in it without creating a new entry.
846 DCHECK(GetLastCommittedEntry());
847
848 // Handle the case where we're navigating back/forward to a previous subframe
849 // navigation entry. This is case "2." in NAV_AUTO_SUBFRAME comment in the
850 // header file. In case "1." this will be a NOP.
851 int entry_index = GetEntryIndexWithPageID(
852 tab_contents_->GetSiteInstance(),
853 params.page_id);
854 if (entry_index < 0 ||
855 entry_index >= static_cast<int>(entries_.size())) {
856 NOTREACHED();
857 return false;
858 }
859
860 // Update the current navigation entry in case we're going back/forward.
861 if (entry_index != last_committed_entry_index_) {
862 last_committed_entry_index_ = entry_index;
863 return true;
864 }
865 return false;
866 }
867
868 // TODO(brettw) I think this function is unnecessary.
869 void NavigationController::CommitPendingEntry() {
870 DiscardTransientEntry();
871
872 if (!pending_entry())
873 return; // Nothing to do.
874
875 // Need to save the previous URL for the notification.
876 LoadCommittedDetails details;
877 if (GetLastCommittedEntry()) {
878 details.previous_url = GetLastCommittedEntry()->url();
879 details.previous_entry_index = last_committed_entry_index();
880 } else {
881 details.previous_entry_index = -1;
882 }
883
884 if (pending_entry_index_ >= 0) {
885 // This is a previous navigation (back/forward) that we're just now
886 // committing. Just mark it as committed.
887 details.type = NavigationType::EXISTING_PAGE;
888 int new_entry_index = pending_entry_index_;
889 DiscardNonCommittedEntriesInternal();
890
891 // Mark that entry as committed.
892 last_committed_entry_index_ = new_entry_index;
893 } else {
894 // This is a new navigation. It's easiest to just copy the entry and insert
895 // it new again, since InsertOrReplaceEntry expects to take ownership and
896 // also discard the pending entry. We also need to synthesize a page ID. We
897 // can only do this because this function will only be called by our custom
898 // TabContents types. For TabContents, the IDs are generated by the
899 // renderer, so we can't do this.
900 details.type = NavigationType::NEW_PAGE;
901 pending_entry_->set_page_id(tab_contents_->GetMaxPageID() + 1);
902 tab_contents_->UpdateMaxPageID(pending_entry_->page_id());
903 InsertOrReplaceEntry(new NavigationEntry(*pending_entry_), false);
904 }
905
906 // Broadcast the notification of the navigation.
907 details.entry = GetActiveEntry();
908 details.is_auto = false;
909 details.is_in_page = AreURLsInPageNavigation(details.previous_url,
910 details.entry->url());
911 details.is_main_frame = true;
912 NotifyNavigationEntryCommitted(&details, 0);
913 }
914
915 int NavigationController::GetIndexOfEntry(
916 const NavigationEntry* entry) const {
917 const NavigationEntries::const_iterator i(std::find(
918 entries_.begin(),
919 entries_.end(),
920 entry));
921 return (i == entries_.end()) ? -1 : static_cast<int>(i - entries_.begin());
922 }
923
924 bool NavigationController::IsURLInPageNavigation(const GURL& url) const {
925 NavigationEntry* last_committed = GetLastCommittedEntry();
926 if (!last_committed)
927 return false;
928 return AreURLsInPageNavigation(last_committed->url(), url);
929 }
930
931 void NavigationController::CopyStateFrom(const NavigationController& source) {
932 // Verify that we look new.
933 DCHECK(entry_count() == 0 && !pending_entry());
934
935 if (source.entry_count() == 0)
936 return; // Nothing new to do.
937
938 needs_reload_ = true;
939 InsertEntriesFrom(source, source.entry_count());
940
941 session_storage_namespace_ = source.session_storage_namespace_->Clone();
942
943 FinishRestore(source.last_committed_entry_index_, false);
944 }
945
946 void NavigationController::CopyStateFromAndPrune(NavigationController* source) {
947 // This code is intended for use when the last entry is the active entry.
948 DCHECK((transient_entry_index_ != -1 &&
949 transient_entry_index_ == entry_count() - 1) ||
950 (pending_entry_ && (pending_entry_index_ == -1 ||
951 pending_entry_index_ == entry_count() - 1)) ||
952 (!pending_entry_ && last_committed_entry_index_ == entry_count() - 1));
953
954 // Remove all the entries leaving the active entry.
955 PruneAllButActive();
956
957 // Insert the entries from source. Don't use source->GetCurrentEntryIndex as
958 // we don't want to copy over the transient entry.
959 int max_source_index = source->pending_entry_index_ != -1 ?
960 source->pending_entry_index_ : source->last_committed_entry_index_;
961 if (max_source_index == -1)
962 max_source_index = source->entry_count();
963 else
964 max_source_index++;
965 InsertEntriesFrom(*source, max_source_index);
966
967 // Adjust indices such that the last entry and pending are at the end now.
968 last_committed_entry_index_ = entry_count() - 1;
969 if (pending_entry_index_ != -1)
970 pending_entry_index_ = entry_count() - 1;
971 if (transient_entry_index_ != -1) {
972 // There's a transient entry. In this case we want the last committed to
973 // point to the previous entry.
974 transient_entry_index_ = entry_count() - 1;
975 if (last_committed_entry_index_ != -1)
976 last_committed_entry_index_--;
977 }
978 }
979
980 void NavigationController::PruneAllButActive() {
981 if (transient_entry_index_ != -1) {
982 // There is a transient entry. Prune up to it.
983 DCHECK_EQ(entry_count() - 1, transient_entry_index_);
984 entries_.erase(entries_.begin(), entries_.begin() + transient_entry_index_);
985 transient_entry_index_ = 0;
986 last_committed_entry_index_ = -1;
987 pending_entry_index_ = -1;
988 } else if (!pending_entry_) {
989 // There's no pending entry. Leave the last entry (if there is one).
990 if (!entry_count())
991 return;
992
993 DCHECK(last_committed_entry_index_ >= 0);
994 entries_.erase(entries_.begin(),
995 entries_.begin() + last_committed_entry_index_);
996 entries_.erase(entries_.begin() + 1, entries_.end());
997 last_committed_entry_index_ = 0;
998 } else if (pending_entry_index_ != -1) {
999 entries_.erase(entries_.begin(), entries_.begin() + pending_entry_index_);
1000 entries_.erase(entries_.begin() + 1, entries_.end());
1001 pending_entry_index_ = 0;
1002 last_committed_entry_index_ = 0;
1003 } else {
1004 // There is a pending_entry, but it's not in entries_.
1005 pending_entry_index_ = -1;
1006 last_committed_entry_index_ = -1;
1007 entries_.clear();
1008 }
1009
1010 if (tab_contents_->interstitial_page()) {
1011 // Normally the interstitial page hides itself if the user doesn't proceeed.
1012 // This would result in showing a NavigationEntry we just removed. Set this
1013 // so the interstitial triggers a reload if the user doesn't proceed.
1014 tab_contents_->interstitial_page()->set_reload_on_dont_proceed(true);
1015 }
1016 }
1017
1018 void NavigationController::DiscardNonCommittedEntries() {
1019 bool transient = transient_entry_index_ != -1;
1020 DiscardNonCommittedEntriesInternal();
1021
1022 // If there was a transient entry, invalidate everything so the new active
1023 // entry state is shown.
1024 if (transient) {
1025 tab_contents_->NotifyNavigationStateChanged(kInvalidateAllButShelves);
1026 }
1027 }
1028
1029 void NavigationController::InsertOrReplaceEntry(NavigationEntry* entry,
1030 bool replace) {
1031 DCHECK(entry->transition_type() != PageTransition::AUTO_SUBFRAME);
1032
1033 // Copy the pending entry's unique ID to the committed entry.
1034 // I don't know if pending_entry_index_ can be other than -1 here.
1035 const NavigationEntry* const pending_entry = (pending_entry_index_ == -1) ?
1036 pending_entry_ : entries_[pending_entry_index_].get();
1037 if (pending_entry)
1038 entry->set_unique_id(pending_entry->unique_id());
1039
1040 DiscardNonCommittedEntriesInternal();
1041
1042 int current_size = static_cast<int>(entries_.size());
1043
1044 if (current_size > 0) {
1045 // Prune any entries which are in front of the current entry.
1046 // Also prune the current entry if we are to replace the current entry.
1047 int prune_up_to = replace ? last_committed_entry_index_ - 1
1048 : last_committed_entry_index_;
1049 int num_pruned = 0;
1050 while (prune_up_to < (current_size - 1)) {
1051 num_pruned++;
1052 entries_.pop_back();
1053 current_size--;
1054 }
1055 if (num_pruned > 0) // Only notify if we did prune something.
1056 NotifyPrunedEntries(this, false, num_pruned);
1057 }
1058
1059 if (entries_.size() >= max_entry_count_) {
1060 RemoveEntryAtIndex(0, GURL());
1061 NotifyPrunedEntries(this, true, 1);
1062 }
1063
1064 entries_.push_back(linked_ptr<NavigationEntry>(entry));
1065 last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1;
1066
1067 // This is a new page ID, so we need everybody to know about it.
1068 tab_contents_->UpdateMaxPageID(entry->page_id());
1069 }
1070
1071 void NavigationController::SetWindowID(const SessionID& id) {
1072 window_id_ = id;
1073 NotificationService::current()->Notify(NotificationType::TAB_PARENTED,
1074 Source<NavigationController>(this),
1075 NotificationService::NoDetails());
1076 }
1077
1078 void NavigationController::NavigateToPendingEntry(ReloadType reload_type) {
1079 needs_reload_ = false;
1080
1081 // For session history navigations only the pending_entry_index_ is set.
1082 if (!pending_entry_) {
1083 DCHECK_NE(pending_entry_index_, -1);
1084 pending_entry_ = entries_[pending_entry_index_].get();
1085 }
1086
1087 if (!tab_contents_->NavigateToPendingEntry(reload_type))
1088 DiscardNonCommittedEntries();
1089 }
1090
1091 void NavigationController::NotifyNavigationEntryCommitted(
1092 LoadCommittedDetails* details,
1093 int extra_invalidate_flags) {
1094 details->entry = GetActiveEntry();
1095 NotificationDetails notification_details =
1096 Details<LoadCommittedDetails>(details);
1097
1098 // We need to notify the ssl_manager_ before the tab_contents_ so the
1099 // location bar will have up-to-date information about the security style
1100 // when it wants to draw. See http://crbug.com/11157
1101 ssl_manager_.DidCommitProvisionalLoad(notification_details);
1102
1103 // TODO(pkasting): http://b/1113079 Probably these explicit notification paths
1104 // should be removed, and interested parties should just listen for the
1105 // notification below instead.
1106 tab_contents_->NotifyNavigationStateChanged(
1107 kInvalidateAllButShelves | extra_invalidate_flags);
1108
1109 NotificationService::current()->Notify(
1110 NotificationType::NAV_ENTRY_COMMITTED,
1111 Source<NavigationController>(this),
1112 notification_details);
1113 }
1114
1115 // static
1116 void NavigationController::DisablePromptOnRepost() {
1117 check_for_repost_ = false;
1118 }
1119
1120 void NavigationController::SetActive(bool is_active) {
1121 if (is_active && needs_reload_)
1122 LoadIfNecessary();
1123 }
1124
1125 void NavigationController::LoadIfNecessary() {
1126 if (!needs_reload_)
1127 return;
1128
1129 // Calling Reload() results in ignoring state, and not loading.
1130 // Explicitly use NavigateToPendingEntry so that the renderer uses the
1131 // cached state.
1132 pending_entry_index_ = last_committed_entry_index_;
1133 NavigateToPendingEntry(NO_RELOAD);
1134 }
1135
1136 void NavigationController::NotifyEntryChanged(const NavigationEntry* entry,
1137 int index) {
1138 EntryChangedDetails det;
1139 det.changed_entry = entry;
1140 det.index = index;
1141 NotificationService::current()->Notify(NotificationType::NAV_ENTRY_CHANGED,
1142 Source<NavigationController>(this),
1143 Details<EntryChangedDetails>(&det));
1144 }
1145
1146 void NavigationController::FinishRestore(int selected_index,
1147 bool from_last_session) {
1148 DCHECK(selected_index >= 0 && selected_index < entry_count());
1149 ConfigureEntriesForRestore(&entries_, from_last_session);
1150
1151 set_max_restored_page_id(static_cast<int32>(entry_count()));
1152
1153 last_committed_entry_index_ = selected_index;
1154 }
1155
1156 void NavigationController::DiscardNonCommittedEntriesInternal() {
1157 if (pending_entry_index_ == -1)
1158 delete pending_entry_;
1159 pending_entry_ = NULL;
1160 pending_entry_index_ = -1;
1161
1162 DiscardTransientEntry();
1163 }
1164
1165 void NavigationController::DiscardTransientEntry() {
1166 if (transient_entry_index_ == -1)
1167 return;
1168 entries_.erase(entries_.begin() + transient_entry_index_);
1169 if (last_committed_entry_index_ > transient_entry_index_)
1170 last_committed_entry_index_--;
1171 transient_entry_index_ = -1;
1172 }
1173
1174 int NavigationController::GetEntryIndexWithPageID(
1175 SiteInstance* instance, int32 page_id) const {
1176 for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) {
1177 if ((entries_[i]->site_instance() == instance) &&
1178 (entries_[i]->page_id() == page_id))
1179 return i;
1180 }
1181 return -1;
1182 }
1183
1184 NavigationEntry* NavigationController::GetTransientEntry() const {
1185 if (transient_entry_index_ == -1)
1186 return NULL;
1187 return entries_[transient_entry_index_].get();
1188 }
1189
1190 void NavigationController::InsertEntriesFrom(
1191 const NavigationController& source,
1192 int max_index) {
1193 DCHECK_LE(max_index, source.entry_count());
1194 size_t insert_index = 0;
1195 for (int i = 0; i < max_index; i++) {
1196 // When cloning a tab, copy all entries except interstitial pages
1197 if (source.entries_[i].get()->page_type() != INTERSTITIAL_PAGE) {
1198 entries_.insert(entries_.begin() + insert_index++,
1199 linked_ptr<NavigationEntry>(
1200 new NavigationEntry(*source.entries_[i])));
1201 }
1202 }
1203 }
OLDNEW
« no previous file with comments | « chrome/browser/tab_contents/navigation_controller.h ('k') | chrome/browser/tab_contents/navigation_controller_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698