[email protected] | ee61392 | 2009-09-02 20:38:22 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | f3ec774 | 2009-01-15 00:59:16 | [diff] [blame] | 5 | #include "chrome/browser/tab_contents/navigation_controller.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | 9929da9 | 2009-05-05 02:05:11 | [diff] [blame] | 7 | #include "app/resource_bundle.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | #include "base/file_util.h" |
| 9 | #include "base/logging.h" |
| 10 | #include "base/string_util.h" |
[email protected] | b689fce7 | 2009-03-17 22:45:34 | [diff] [blame] | 11 | #include "base/time.h" |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 12 | #include "chrome/browser/browser_about_handler.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | #include "chrome/browser/browser_process.h" |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 14 | #include "chrome/browser/browser_url_handler.h" |
[email protected] | ce560f8 | 2009-06-03 09:39:44 | [diff] [blame] | 15 | #include "chrome/browser/profile.h" |
[email protected] | 14e60c8d | 2009-06-29 03:56:51 | [diff] [blame] | 16 | #include "chrome/browser/renderer_host/site_instance.h" |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 17 | #include "chrome/browser/sessions/session_types.h" |
[email protected] | f3ec774 | 2009-01-15 00:59:16 | [diff] [blame] | 18 | #include "chrome/browser/tab_contents/navigation_entry.h" |
[email protected] | 5c23875 | 2009-06-13 10:29:07 | [diff] [blame] | 19 | #include "chrome/browser/tab_contents/tab_contents.h" |
[email protected] | 14f3408a | 2009-08-31 20:53:53 | [diff] [blame] | 20 | #include "chrome/browser/tab_contents/tab_contents_delegate.h" |
[email protected] | 299dabd | 2008-11-19 02:27:16 | [diff] [blame] | 21 | #include "chrome/common/navigation_types.h" |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 22 | #include "chrome/common/notification_service.h" |
[email protected] | a23de857 | 2009-06-03 02:16:32 | [diff] [blame] | 23 | #include "chrome/common/pref_names.h" |
| 24 | #include "chrome/common/pref_service.h" |
[email protected] | e09ba55 | 2009-02-05 03:26:29 | [diff] [blame] | 25 | #include "chrome/common/render_messages.h" |
[email protected] | 6de7445 | 2009-02-25 18:04:59 | [diff] [blame] | 26 | #include "chrome/common/url_constants.h" |
[email protected] | 074f1056 | 2009-05-21 22:40:05 | [diff] [blame] | 27 | #include "grit/app_resources.h" |
[email protected] | a23de857 | 2009-06-03 02:16:32 | [diff] [blame] | 28 | #include "net/base/escape.h" |
| 29 | #include "net/base/net_util.h" |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 30 | #include "webkit/glue/webkit_glue.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | // Invoked when entries have been pruned, or removed. For example, if the |
| 35 | // current entries are [google, digg, yahoo], with the current entry google, |
| 36 | // and the user types in cnet, then digg and yahoo are pruned. |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 37 | void NotifyPrunedEntries(NavigationController* nav_controller, |
| 38 | bool from_front, |
| 39 | int count) { |
| 40 | NavigationController::PrunedDetails details; |
| 41 | details.from_front = from_front; |
| 42 | details.count = count; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 43 | NotificationService::current()->Notify( |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 44 | NotificationType::NAV_LIST_PRUNED, |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 45 | Source<NavigationController>(nav_controller), |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 46 | Details<NavigationController::PrunedDetails>(&details)); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // Ensure the given NavigationEntry has a valid state, so that WebKit does not |
| 50 | // get confused if we navigate back to it. |
[email protected] | 40bcc30 | 2009-03-02 20:50:39 | [diff] [blame] | 51 | // |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 52 | // An empty state is treated as a new navigation by WebKit, which would mean |
| 53 | // losing the navigation entries and generating a new navigation entry after |
| 54 | // this one. We don't want that. To avoid this we create a valid state which |
| 55 | // WebKit will not treat as a new navigation. |
| 56 | void SetContentStateIfEmpty(NavigationEntry* entry) { |
[email protected] | 965524b | 2009-04-04 21:32:40 | [diff] [blame] | 57 | if (entry->content_state().empty()) { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 58 | entry->set_content_state( |
| 59 | webkit_glue::CreateHistoryStateForURL(entry->url())); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Configure all the NavigationEntries in entries for restore. This resets |
| 64 | // the transition type to reload and makes sure the content state isn't empty. |
| 65 | void ConfigureEntriesForRestore( |
| 66 | std::vector<linked_ptr<NavigationEntry> >* entries) { |
| 67 | for (size_t i = 0; i < entries->size(); ++i) { |
| 68 | // Use a transition type of reload so that we don't incorrectly increase |
| 69 | // the typed count. |
| 70 | (*entries)[i]->set_transition_type(PageTransition::RELOAD); |
| 71 | (*entries)[i]->set_restored(true); |
| 72 | // NOTE(darin): This code is only needed for backwards compat. |
| 73 | SetContentStateIfEmpty((*entries)[i].get()); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // See NavigationController::IsURLInPageNavigation for how this works and why. |
| 78 | bool AreURLsInPageNavigation(const GURL& existing_url, const GURL& new_url) { |
| 79 | if (existing_url == new_url || !new_url.has_ref()) |
| 80 | return false; |
| 81 | |
| 82 | url_canon::Replacements<char> replacements; |
| 83 | replacements.ClearRef(); |
| 84 | return existing_url.ReplaceComponents(replacements) == |
| 85 | new_url.ReplaceComponents(replacements); |
| 86 | } |
| 87 | |
[email protected] | 09b8f82f | 2009-06-16 20:22:11 | [diff] [blame] | 88 | // Navigation within this limit since the last document load is considered to |
| 89 | // be automatic (i.e., machine-initiated) rather than user-initiated unless |
| 90 | // a user gesture has been observed. |
| 91 | const base::TimeDelta kMaxAutoNavigationTimeDelta = |
| 92 | base::TimeDelta::FromSeconds(5); |
| 93 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 94 | } // namespace |
| 95 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | // NavigationController --------------------------------------------------- |
| 97 | |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 98 | // static |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 99 | size_t NavigationController::max_entry_count_ = 50; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 100 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | // static |
| 102 | bool NavigationController::check_for_repost_ = true; |
| 103 | |
| 104 | // Creates a new NavigationEntry for each TabNavigation in navigations, adding |
| 105 | // the NavigationEntry to entries. This is used during session restore. |
| 106 | static void CreateNavigationEntriesFromTabNavigations( |
| 107 | const std::vector<TabNavigation>& navigations, |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 108 | std::vector<linked_ptr<NavigationEntry> >* entries) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 109 | // Create a NavigationEntry for each of the navigations. |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 110 | int page_id = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | for (std::vector<TabNavigation>::const_iterator i = |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 112 | navigations.begin(); i != navigations.end(); ++i, ++page_id) { |
| 113 | entries->push_back( |
| 114 | linked_ptr<NavigationEntry>(i->ToNavigationEntry(page_id))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 118 | NavigationController::NavigationController(TabContents* contents, |
| 119 | Profile* profile) |
| 120 | : profile_(profile), |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 121 | pending_entry_(NULL), |
| 122 | last_committed_entry_index_(-1), |
| 123 | pending_entry_index_(-1), |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 124 | transient_entry_index_(-1), |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 125 | tab_contents_(contents), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 126 | max_restored_page_id_(-1), |
[email protected] | 5d06384 | 2009-05-15 04:08:24 | [diff] [blame] | 127 | ALLOW_THIS_IN_INITIALIZER_LIST(ssl_manager_(this)), |
[email protected] | 38b8f4e | 2009-09-24 19:44:57 | [diff] [blame^] | 128 | needs_reload_(false), |
| 129 | user_gesture_observed_(false) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 130 | DCHECK(profile_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | } |
| 132 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 133 | NavigationController::~NavigationController() { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 134 | DiscardNonCommittedEntriesInternal(); |
[email protected] | c099387 | 2008-08-21 19:59:44 | [diff] [blame] | 135 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 136 | NotificationService::current()->Notify( |
| 137 | NotificationType::TAB_CLOSED, |
| 138 | Source<NavigationController>(this), |
| 139 | NotificationService::NoDetails()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 142 | void NavigationController::RestoreFromState( |
| 143 | const std::vector<TabNavigation>& navigations, |
| 144 | int selected_navigation) { |
| 145 | // Verify that this controller is unused and that the input is valid. |
| 146 | DCHECK(entry_count() == 0 && !pending_entry()); |
| 147 | DCHECK(selected_navigation >= 0 && |
| 148 | selected_navigation < static_cast<int>(navigations.size())); |
| 149 | |
| 150 | // Populate entries_ from the supplied TabNavigations. |
| 151 | needs_reload_ = true; |
| 152 | CreateNavigationEntriesFromTabNavigations(navigations, &entries_); |
| 153 | |
| 154 | // And finish the restore. |
| 155 | FinishRestore(selected_navigation); |
| 156 | } |
| 157 | |
[email protected] | f1c7411 | 2008-10-30 16:17:04 | [diff] [blame] | 158 | void NavigationController::Reload(bool check_for_repost) { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 159 | // Reloading a transient entry does nothing. |
| 160 | if (transient_entry_index_ != -1) |
| 161 | return; |
| 162 | |
| 163 | DiscardNonCommittedEntriesInternal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 164 | int current_index = GetCurrentEntryIndex(); |
[email protected] | f1c7411 | 2008-10-30 16:17:04 | [diff] [blame] | 165 | if (check_for_repost_ && check_for_repost && current_index != -1 && |
[email protected] | a3a1d14 | 2008-12-19 00:42:30 | [diff] [blame] | 166 | GetEntryAtIndex(current_index)->has_post_data()) { |
| 167 | // The user is asking to reload a page with POST data. Prompt to make sure |
[email protected] | b5bb35f | 2009-02-05 20:17:07 | [diff] [blame] | 168 | // they really want to do this. If they do, the dialog will call us back |
| 169 | // with check_for_repost = false. |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 170 | tab_contents_->Activate(); |
[email protected] | 14f3408a | 2009-08-31 20:53:53 | [diff] [blame] | 171 | tab_contents_->delegate()->ShowRepostFormWarningDialog(tab_contents_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | } else { |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 173 | // Base the navigation on where we are now... |
| 174 | int current_index = GetCurrentEntryIndex(); |
| 175 | |
| 176 | // If we are no where, then we can't reload. TODO(darin): We should add a |
| 177 | // CanReload method. |
| 178 | if (current_index == -1) |
| 179 | return; |
| 180 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 181 | DiscardNonCommittedEntriesInternal(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 182 | |
| 183 | pending_entry_index_ = current_index; |
[email protected] | 1e5645ff | 2008-08-27 18:09:07 | [diff] [blame] | 184 | entries_[pending_entry_index_]->set_transition_type(PageTransition::RELOAD); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 185 | NavigateToPendingEntry(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 189 | NavigationEntry* NavigationController::GetEntryWithPageID( |
[email protected] | 7f0005a | 2009-04-15 03:25:11 | [diff] [blame] | 190 | SiteInstance* instance, int32 page_id) const { |
| 191 | int index = GetEntryIndexWithPageID(instance, page_id); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 192 | return (index != -1) ? entries_[index].get() : NULL; |
| 193 | } |
| 194 | |
| 195 | void NavigationController::LoadEntry(NavigationEntry* entry) { |
[email protected] | cd3d789 | 2009-03-04 23:55:06 | [diff] [blame] | 196 | // Handle non-navigational URLs that popup dialogs and such, these should not |
| 197 | // actually navigate. |
| 198 | if (HandleNonNavigationAboutURL(entry->url())) |
| 199 | return; |
| 200 | |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 201 | // When navigating to a new page, we don't know for sure if we will actually |
| 202 | // end up leaving the current page. The new page load could for example |
| 203 | // result in a download or a 'no content' response (e.g., a mailto: URL). |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 204 | DiscardNonCommittedEntriesInternal(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 205 | pending_entry_ = entry; |
| 206 | NotificationService::current()->Notify( |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 207 | NotificationType::NAV_ENTRY_PENDING, |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 208 | Source<NavigationController>(this), |
| 209 | NotificationService::NoDetails()); |
| 210 | NavigateToPendingEntry(false); |
| 211 | } |
| 212 | |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 213 | NavigationEntry* NavigationController::GetActiveEntry() const { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 214 | if (transient_entry_index_ != -1) |
| 215 | return entries_[transient_entry_index_].get(); |
| 216 | if (pending_entry_) |
| 217 | return pending_entry_; |
| 218 | return GetLastCommittedEntry(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | int NavigationController::GetCurrentEntryIndex() const { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 222 | if (transient_entry_index_ != -1) |
| 223 | return transient_entry_index_; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 224 | if (pending_entry_index_ != -1) |
| 225 | return pending_entry_index_; |
| 226 | return last_committed_entry_index_; |
| 227 | } |
| 228 | |
| 229 | NavigationEntry* NavigationController::GetLastCommittedEntry() const { |
| 230 | if (last_committed_entry_index_ == -1) |
| 231 | return NULL; |
| 232 | return entries_[last_committed_entry_index_].get(); |
| 233 | } |
| 234 | |
| 235 | NavigationEntry* NavigationController::GetEntryAtOffset(int offset) const { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 236 | int index = (transient_entry_index_ != -1) ? |
| 237 | transient_entry_index_ + offset : |
| 238 | last_committed_entry_index_ + offset; |
[email protected] | 7f0005a | 2009-04-15 03:25:11 | [diff] [blame] | 239 | if (index < 0 || index >= entry_count()) |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 240 | return NULL; |
| 241 | |
| 242 | return entries_[index].get(); |
| 243 | } |
| 244 | |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 245 | bool NavigationController::CanGoBack() const { |
| 246 | return entries_.size() > 1 && GetCurrentEntryIndex() > 0; |
| 247 | } |
| 248 | |
| 249 | bool NavigationController::CanGoForward() const { |
| 250 | int index = GetCurrentEntryIndex(); |
| 251 | return index >= 0 && index < (static_cast<int>(entries_.size()) - 1); |
| 252 | } |
| 253 | |
| 254 | void NavigationController::GoBack() { |
| 255 | if (!CanGoBack()) { |
| 256 | NOTREACHED(); |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | // Base the navigation on where we are now... |
| 261 | int current_index = GetCurrentEntryIndex(); |
| 262 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 263 | DiscardNonCommittedEntries(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 264 | |
| 265 | pending_entry_index_ = current_index - 1; |
| 266 | NavigateToPendingEntry(false); |
| 267 | } |
| 268 | |
| 269 | void NavigationController::GoForward() { |
| 270 | if (!CanGoForward()) { |
| 271 | NOTREACHED(); |
| 272 | return; |
| 273 | } |
| 274 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 275 | bool transient = (transient_entry_index_ != -1); |
| 276 | |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 277 | // Base the navigation on where we are now... |
| 278 | int current_index = GetCurrentEntryIndex(); |
| 279 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 280 | DiscardNonCommittedEntries(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 281 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 282 | pending_entry_index_ = current_index; |
| 283 | // If there was a transient entry, we removed it making the current index |
| 284 | // the next page. |
| 285 | if (!transient) |
| 286 | pending_entry_index_++; |
| 287 | |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 288 | NavigateToPendingEntry(false); |
| 289 | } |
| 290 | |
| 291 | void NavigationController::GoToIndex(int index) { |
| 292 | if (index < 0 || index >= static_cast<int>(entries_.size())) { |
| 293 | NOTREACHED(); |
| 294 | return; |
| 295 | } |
| 296 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 297 | if (transient_entry_index_ != -1) { |
| 298 | if (index == transient_entry_index_) { |
| 299 | // Nothing to do when navigating to the transient. |
| 300 | return; |
| 301 | } |
| 302 | if (index > transient_entry_index_) { |
| 303 | // Removing the transient is goint to shift all entries by 1. |
| 304 | index--; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | DiscardNonCommittedEntries(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 309 | |
| 310 | pending_entry_index_ = index; |
| 311 | NavigateToPendingEntry(false); |
| 312 | } |
| 313 | |
| 314 | void NavigationController::GoToOffset(int offset) { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 315 | int index = (transient_entry_index_ != -1) ? |
| 316 | transient_entry_index_ + offset : |
| 317 | last_committed_entry_index_ + offset; |
[email protected] | 7f0005a | 2009-04-15 03:25:11 | [diff] [blame] | 318 | if (index < 0 || index >= entry_count()) |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 319 | return; |
| 320 | |
| 321 | GoToIndex(index); |
| 322 | } |
| 323 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 324 | void NavigationController::RemoveEntryAtIndex(int index, |
| 325 | const GURL& default_url) { |
| 326 | int size = static_cast<int>(entries_.size()); |
| 327 | DCHECK(index < size); |
| 328 | |
| 329 | DiscardNonCommittedEntries(); |
| 330 | |
| 331 | entries_.erase(entries_.begin() + index); |
| 332 | |
| 333 | if (last_committed_entry_index_ == index) { |
| 334 | last_committed_entry_index_--; |
| 335 | // We removed the currently shown entry, so we have to load something else. |
| 336 | if (last_committed_entry_index_ != -1) { |
| 337 | pending_entry_index_ = last_committed_entry_index_; |
| 338 | NavigateToPendingEntry(false); |
| 339 | } else { |
| 340 | // If there is nothing to show, show a default page. |
[email protected] | ed3456f | 2009-02-26 20:24:48 | [diff] [blame] | 341 | LoadURL(default_url.is_empty() ? GURL("about:blank") : default_url, |
[email protected] | c058805 | 2008-10-27 23:01:50 | [diff] [blame] | 342 | GURL(), PageTransition::START_PAGE); |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 343 | } |
| 344 | } else if (last_committed_entry_index_ > index) { |
| 345 | last_committed_entry_index_--; |
| 346 | } |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 347 | } |
| 348 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 349 | NavigationEntry* NavigationController::CreateNavigationEntry( |
[email protected] | c058805 | 2008-10-27 23:01:50 | [diff] [blame] | 350 | const GURL& url, const GURL& referrer, PageTransition::Type transition) { |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 351 | // Allow the browser URL handler to rewrite the URL. This will, for example, |
| 352 | // remove "view-source:" from the beginning of the URL to get the URL that |
| 353 | // will actually be loaded. This real URL won't be shown to the user, just |
| 354 | // used internally. |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 355 | GURL loaded_url(url); |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 356 | BrowserURLHandler::RewriteURLIfNecessary(&loaded_url, profile_); |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 357 | |
[email protected] | b680ad2 | 2009-04-15 23:19:42 | [diff] [blame] | 358 | NavigationEntry* entry = new NavigationEntry(NULL, -1, loaded_url, referrer, |
[email protected] | 4c4d8d2 | 2009-03-04 05:29:27 | [diff] [blame] | 359 | string16(), transition); |
[email protected] | ebe89e06 | 2009-08-13 23:16:54 | [diff] [blame] | 360 | entry->set_virtual_url(url); |
[email protected] | 1e5645ff | 2008-08-27 18:09:07 | [diff] [blame] | 361 | entry->set_user_typed_url(url); |
[email protected] | 8654b68 | 2008-11-01 23:36:17 | [diff] [blame] | 362 | if (url.SchemeIsFile()) { |
[email protected] | a23de857 | 2009-06-03 02:16:32 | [diff] [blame] | 363 | std::wstring languages = profile()->GetPrefs()->GetString( |
| 364 | prefs::kAcceptLanguages); |
[email protected] | 4c4d8d2 | 2009-03-04 05:29:27 | [diff] [blame] | 365 | entry->set_title(WideToUTF16Hack( |
[email protected] | a23de857 | 2009-06-03 02:16:32 | [diff] [blame] | 366 | file_util::GetFilenameFromPath(net::FormatUrl(url, languages)))); |
[email protected] | 8654b68 | 2008-11-01 23:36:17 | [diff] [blame] | 367 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 368 | return entry; |
| 369 | } |
| 370 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 371 | void NavigationController::AddTransientEntry(NavigationEntry* entry) { |
| 372 | // Discard any current transient entry, we can only have one at a time. |
| 373 | int index = 0; |
| 374 | if (last_committed_entry_index_ != -1) |
| 375 | index = last_committed_entry_index_ + 1; |
| 376 | DiscardTransientEntry(); |
| 377 | entries_.insert(entries_.begin() + index, linked_ptr<NavigationEntry>(entry)); |
| 378 | transient_entry_index_ = index; |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 379 | tab_contents_->NotifyNavigationStateChanged( |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 380 | TabContents::INVALIDATE_EVERYTHING); |
| 381 | } |
| 382 | |
[email protected] | c058805 | 2008-10-27 23:01:50 | [diff] [blame] | 383 | void NavigationController::LoadURL(const GURL& url, const GURL& referrer, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 384 | PageTransition::Type transition) { |
| 385 | // The user initiated a load, we don't need to reload anymore. |
| 386 | needs_reload_ = false; |
| 387 | |
[email protected] | c058805 | 2008-10-27 23:01:50 | [diff] [blame] | 388 | NavigationEntry* entry = CreateNavigationEntry(url, referrer, transition); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 389 | |
| 390 | LoadEntry(entry); |
| 391 | } |
| 392 | |
[email protected] | 09b8f82f | 2009-06-16 20:22:11 | [diff] [blame] | 393 | void NavigationController::DocumentLoadedInFrame() { |
| 394 | last_document_loaded_ = base::TimeTicks::Now(); |
| 395 | } |
| 396 | |
| 397 | void NavigationController::OnUserGesture() { |
| 398 | user_gesture_observed_ = true; |
| 399 | } |
| 400 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 401 | bool NavigationController::RendererDidNavigate( |
| 402 | const ViewHostMsg_FrameNavigate_Params& params, |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 403 | LoadCommittedDetails* details) { |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 404 | // Save the previous state before we clobber it. |
| 405 | if (GetLastCommittedEntry()) { |
[email protected] | ecd9d870 | 2008-08-28 22:10:17 | [diff] [blame] | 406 | details->previous_url = GetLastCommittedEntry()->url(); |
[email protected] | 7f0005a | 2009-04-15 03:25:11 | [diff] [blame] | 407 | details->previous_entry_index = last_committed_entry_index(); |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 408 | } else { |
| 409 | details->previous_url = GURL(); |
| 410 | details->previous_entry_index = -1; |
| 411 | } |
[email protected] | ecd9d870 | 2008-08-28 22:10:17 | [diff] [blame] | 412 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 413 | // Assign the current site instance to any pending entry, so we can find it |
| 414 | // later by calling GetEntryIndexWithPageID. We only care about this if the |
| 415 | // pending entry is an existing navigation and not a new one (or else we |
| 416 | // wouldn't care about finding it with GetEntryIndexWithPageID). |
| 417 | // |
| 418 | // TODO(brettw) this seems slightly bogus as we don't really know if the |
| 419 | // pending entry is what this navigation is for. There is a similar TODO |
| 420 | // w.r.t. the pending entry in RendererDidNavigateToNewPage. |
| 421 | if (pending_entry_index_ >= 0) |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 422 | pending_entry_->set_site_instance(tab_contents_->GetSiteInstance()); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 423 | |
| 424 | // Do navigation-type specific actions. These will make and commit an entry. |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 425 | details->type = ClassifyNavigation(params); |
| 426 | switch (details->type) { |
| 427 | case NavigationType::NEW_PAGE: |
[email protected] | befd8d82 | 2009-07-01 04:51:47 | [diff] [blame] | 428 | RendererDidNavigateToNewPage(params, &(details->did_replace_entry)); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 429 | break; |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 430 | case NavigationType::EXISTING_PAGE: |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 431 | RendererDidNavigateToExistingPage(params); |
| 432 | break; |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 433 | case NavigationType::SAME_PAGE: |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 434 | RendererDidNavigateToSamePage(params); |
| 435 | break; |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 436 | case NavigationType::IN_PAGE: |
[email protected] | befd8d82 | 2009-07-01 04:51:47 | [diff] [blame] | 437 | RendererDidNavigateInPage(params, &(details->did_replace_entry)); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 438 | break; |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 439 | case NavigationType::NEW_SUBFRAME: |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 440 | RendererDidNavigateNewSubframe(params); |
| 441 | break; |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 442 | case NavigationType::AUTO_SUBFRAME: |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 443 | if (!RendererDidNavigateAutoSubframe(params)) |
| 444 | return false; |
| 445 | break; |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 446 | case NavigationType::NAV_IGNORE: |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 447 | // There is nothing we can do with this navigation, so we just return to |
| 448 | // the caller that nothing has happened. |
| 449 | return false; |
| 450 | default: |
| 451 | NOTREACHED(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 452 | } |
| 453 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 454 | // All committed entries should have nonempty content state so WebKit doesn't |
| 455 | // get confused when we go back to them (see the function for details). |
| 456 | SetContentStateIfEmpty(GetActiveEntry()); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 457 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 458 | // WebKit doesn't set the "auto" transition on meta refreshes properly (bug |
| 459 | // 1051891) so we manually set it for redirects which we normally treat as |
| 460 | // "non-user-gestures" where we want to update stuff after navigations. |
| 461 | // |
| 462 | // Note that the redirect check also checks for a pending entry to |
| 463 | // differentiate real redirects from browser initiated navigations to a |
| 464 | // redirected entry. This happens when you hit back to go to a page that was |
| 465 | // the destination of a redirect, we don't want to treat it as a redirect |
| 466 | // even though that's what its transition will be. See bug 1117048. |
| 467 | // |
| 468 | // TODO(brettw) write a test for this complicated logic. |
| 469 | details->is_auto = (PageTransition::IsRedirect(params.transition) && |
[email protected] | 7f0005a | 2009-04-15 03:25:11 | [diff] [blame] | 470 | !pending_entry()) || |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 471 | params.gesture == NavigationGestureAuto; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 472 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 473 | // Now prep the rest of the details for the notification and broadcast. |
| 474 | details->entry = GetActiveEntry(); |
| 475 | details->is_in_page = IsURLInPageNavigation(params.url); |
| 476 | details->is_main_frame = PageTransition::IsMainFrame(params.transition); |
[email protected] | f072d2ce | 2008-09-17 17:16:24 | [diff] [blame] | 477 | details->serialized_security_info = params.security_info; |
[email protected] | 8a3422c9 | 2008-09-24 17:42:42 | [diff] [blame] | 478 | details->is_content_filtered = params.is_content_filtered; |
[email protected] | 2e39d2e | 2009-02-19 18:41:31 | [diff] [blame] | 479 | details->http_status_code = params.http_status_code; |
[email protected] | ecd9d870 | 2008-08-28 22:10:17 | [diff] [blame] | 480 | NotifyNavigationEntryCommitted(details); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 481 | |
[email protected] | 09b8f82f | 2009-06-16 20:22:11 | [diff] [blame] | 482 | user_gesture_observed_ = false; |
| 483 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 484 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 485 | } |
| 486 | |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 487 | NavigationType::Type NavigationController::ClassifyNavigation( |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 488 | const ViewHostMsg_FrameNavigate_Params& params) const { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 489 | if (params.page_id == -1) { |
[email protected] | eef9de3 | 2009-09-23 17:19:46 | [diff] [blame] | 490 | // The renderer generates the page IDs, and so if it gives us the invalid |
| 491 | // page ID (-1) we know it didn't actually navigate. This happens in a few |
| 492 | // cases: |
| 493 | // |
| 494 | // - If a page makes a popup navigated to about blank, and then writes |
| 495 | // stuff like a subframe navigated to a real page. We'll get the commit |
| 496 | // for the subframe, but there won't be any commit for the outer page. |
| 497 | // |
| 498 | // - We were also getting these for failed loads (for example, bug 21849). |
| 499 | // The guess is that we get a "load commit" for the alternate error page, |
| 500 | // but that doesn't affect the page ID, so we get the "old" one, which |
| 501 | // could be invalid. This can also happen for a cross-site transition |
| 502 | // that causes us to swap processes. Then the error page load will be in |
| 503 | // a new process with no page IDs ever assigned (and hence a -1 value), |
| 504 | // yet the navigation controller still might have previous pages in its |
| 505 | // list. |
| 506 | // |
| 507 | // In these cases, there's nothing we can do with them, so ignore. |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 508 | return NavigationType::NAV_IGNORE; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 509 | } |
| 510 | |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 511 | if (params.page_id > tab_contents_->GetMaxPageID()) { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 512 | // Greater page IDs than we've ever seen before are new pages. We may or may |
| 513 | // not have a pending entry for the page, and this may or may not be the |
| 514 | // main frame. |
| 515 | if (PageTransition::IsMainFrame(params.transition)) |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 516 | return NavigationType::NEW_PAGE; |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 517 | |
| 518 | // When this is a new subframe navigation, we should have a committed page |
| 519 | // for which it's a suframe in. This may not be the case when an iframe is |
| 520 | // navigated on a popup navigated to about:blank (the iframe would be |
| 521 | // written into the popup by script on the main page). For these cases, |
| 522 | // there isn't any navigation stuff we can do, so just ignore it. |
| 523 | if (!GetLastCommittedEntry()) |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 524 | return NavigationType::NAV_IGNORE; |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 525 | |
| 526 | // Valid subframe navigation. |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 527 | return NavigationType::NEW_SUBFRAME; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | // Now we know that the notification is for an existing page. Find that entry. |
| 531 | int existing_entry_index = GetEntryIndexWithPageID( |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 532 | tab_contents_->GetSiteInstance(), |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 533 | params.page_id); |
| 534 | if (existing_entry_index == -1) { |
| 535 | // The page was not found. It could have been pruned because of the limit on |
| 536 | // back/forward entries (not likely since we'll usually tell it to navigate |
| 537 | // to such entries). It could also mean that the renderer is smoking crack. |
| 538 | NOTREACHED(); |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 539 | return NavigationType::NAV_IGNORE; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 540 | } |
| 541 | NavigationEntry* existing_entry = entries_[existing_entry_index].get(); |
| 542 | |
| 543 | if (pending_entry_ && |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 544 | existing_entry != pending_entry_ && |
[email protected] | a0e6926 | 2009-06-03 19:08:48 | [diff] [blame] | 545 | pending_entry_->page_id() == -1) { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 546 | // In this case, we have a pending entry for a URL but WebCore didn't do a |
| 547 | // new navigation. This happens when you press enter in the URL bar to |
| 548 | // reload. We will create a pending entry, but WebKit will convert it to |
| 549 | // a reload since it's the same page and not create a new entry for it |
| 550 | // (the user doesn't want to have a new back/forward entry when they do |
| 551 | // this). In this case, we want to just ignore the pending entry and go |
| 552 | // back to where we were (the "existing entry"). |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 553 | return NavigationType::SAME_PAGE; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 554 | } |
| 555 | |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 556 | if (!PageTransition::IsMainFrame(params.transition)) { |
| 557 | // All manual subframes would get new IDs and were handled above, so we |
| 558 | // know this is auto. Since the current page was found in the navigation |
| 559 | // entry list, we're guaranteed to have a last committed entry. |
| 560 | DCHECK(GetLastCommittedEntry()); |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 561 | return NavigationType::AUTO_SUBFRAME; |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 562 | } |
| 563 | |
[email protected] | fc60f22 | 2008-12-18 17:36:54 | [diff] [blame] | 564 | // Any toplevel navigations with the same base (minus the reference fragment) |
| 565 | // are in-page navigations. We weeded out subframe navigations above. Most of |
| 566 | // the time this doesn't matter since WebKit doesn't tell us about subframe |
| 567 | // navigations that don't actually navigate, but it can happen when there is |
| 568 | // an encoding override (it always sends a navigation request). |
| 569 | if (AreURLsInPageNavigation(existing_entry->url(), params.url)) |
| 570 | return NavigationType::IN_PAGE; |
| 571 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 572 | // Since we weeded out "new" navigations above, we know this is an existing |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 573 | // (back/forward) navigation. |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 574 | return NavigationType::EXISTING_PAGE; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 575 | } |
| 576 | |
[email protected] | 09b8f82f | 2009-06-16 20:22:11 | [diff] [blame] | 577 | bool NavigationController::IsRedirect( |
| 578 | const ViewHostMsg_FrameNavigate_Params& params) { |
| 579 | // For main frame transition, we judge by params.transition. |
| 580 | // Otherwise, by params.redirects. |
| 581 | if (PageTransition::IsMainFrame(params.transition)) { |
| 582 | return PageTransition::IsRedirect(params.transition); |
| 583 | } |
| 584 | return params.redirects.size() > 1; |
| 585 | } |
| 586 | |
| 587 | bool NavigationController::IsLikelyAutoNavigation(base::TimeTicks now) { |
| 588 | return !user_gesture_observed_ && |
| 589 | (now - last_document_loaded_) < kMaxAutoNavigationTimeDelta; |
| 590 | } |
| 591 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 592 | void NavigationController::RendererDidNavigateToNewPage( |
[email protected] | befd8d82 | 2009-07-01 04:51:47 | [diff] [blame] | 593 | const ViewHostMsg_FrameNavigate_Params& params, bool* did_replace_entry) { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 594 | NavigationEntry* new_entry; |
| 595 | if (pending_entry_) { |
| 596 | // TODO(brettw) this assumes that the pending entry is appropriate for the |
| 597 | // new page that was just loaded. I don't think this is necessarily the |
| 598 | // case! We should have some more tracking to know for sure. This goes along |
| 599 | // with a similar TODO at the top of RendererDidNavigate where we blindly |
| 600 | // set the site instance on the pending entry. |
| 601 | new_entry = new NavigationEntry(*pending_entry_); |
| 602 | |
| 603 | // Don't use the page type from the pending entry. Some interstitial page |
| 604 | // may have set the type to interstitial. Once we commit, however, the page |
| 605 | // type must always be normal. |
| 606 | new_entry->set_page_type(NavigationEntry::NORMAL_PAGE); |
| 607 | } else { |
[email protected] | b680ad2 | 2009-04-15 23:19:42 | [diff] [blame] | 608 | new_entry = new NavigationEntry; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | new_entry->set_url(params.url); |
[email protected] | 740fbda | 2009-02-18 21:38:08 | [diff] [blame] | 612 | new_entry->set_referrer(params.referrer); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 613 | new_entry->set_page_id(params.page_id); |
| 614 | new_entry->set_transition_type(params.transition); |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 615 | new_entry->set_site_instance(tab_contents_->GetSiteInstance()); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 616 | new_entry->set_has_post_data(params.is_post); |
| 617 | |
[email protected] | 09b8f82f | 2009-06-16 20:22:11 | [diff] [blame] | 618 | // If the current entry is a redirection source and the redirection has |
| 619 | // occurred within kMaxAutoNavigationTimeDelta since the last document load, |
| 620 | // this is likely to be machine-initiated redirect and the entry needs to be |
| 621 | // replaced with the new entry to avoid unwanted redirections in navigating |
| 622 | // backward/forward. |
| 623 | // Otherwise, just insert the new entry. |
[email protected] | befd8d82 | 2009-07-01 04:51:47 | [diff] [blame] | 624 | *did_replace_entry = IsRedirect(params) && |
| 625 | IsLikelyAutoNavigation(base::TimeTicks::Now()); |
| 626 | InsertOrReplaceEntry(new_entry, *did_replace_entry); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | void NavigationController::RendererDidNavigateToExistingPage( |
| 630 | const ViewHostMsg_FrameNavigate_Params& params) { |
| 631 | // We should only get here for main frame navigations. |
| 632 | DCHECK(PageTransition::IsMainFrame(params.transition)); |
| 633 | |
| 634 | // This is a back/forward navigation. The existing page for the ID is |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 635 | // guaranteed to exist by ClassifyNavigation, and we just need to update it |
| 636 | // with new information from the renderer. |
[email protected] | 7f0005a | 2009-04-15 03:25:11 | [diff] [blame] | 637 | int entry_index = GetEntryIndexWithPageID(tab_contents_->GetSiteInstance(), |
| 638 | params.page_id); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 639 | DCHECK(entry_index >= 0 && |
| 640 | entry_index < static_cast<int>(entries_.size())); |
| 641 | NavigationEntry* entry = entries_[entry_index].get(); |
| 642 | |
| 643 | // The URL may have changed due to redirects. The site instance will normally |
| 644 | // be the same except during session restore, when no site instance will be |
| 645 | // assigned. |
| 646 | entry->set_url(params.url); |
| 647 | DCHECK(entry->site_instance() == NULL || |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 648 | entry->site_instance() == tab_contents_->GetSiteInstance()); |
| 649 | entry->set_site_instance(tab_contents_->GetSiteInstance()); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 650 | |
| 651 | // The entry we found in the list might be pending if the user hit |
| 652 | // back/forward/reload. This load should commit it (since it's already in the |
| 653 | // list, we can just discard the pending pointer). |
| 654 | // |
| 655 | // Note that we need to use the "internal" version since we don't want to |
| 656 | // actually change any other state, just kill the pointer. |
| 657 | if (entry == pending_entry_) |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 658 | DiscardNonCommittedEntriesInternal(); |
[email protected] | 40bcc30 | 2009-03-02 20:50:39 | [diff] [blame] | 659 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 660 | last_committed_entry_index_ = entry_index; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | void NavigationController::RendererDidNavigateToSamePage( |
| 664 | const ViewHostMsg_FrameNavigate_Params& params) { |
| 665 | // This mode implies we have a pending entry that's the same as an existing |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 666 | // entry for this page ID. This entry is guaranteed to exist by |
| 667 | // ClassifyNavigation. All we need to do is update the existing entry. |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 668 | NavigationEntry* existing_entry = GetEntryWithPageID( |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 669 | tab_contents_->GetSiteInstance(), |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 670 | params.page_id); |
| 671 | |
| 672 | // We assign the entry's unique ID to be that of the new one. Since this is |
| 673 | // always the result of a user action, we want to dismiss infobars, etc. like |
| 674 | // a regular user-initiated navigation. |
| 675 | existing_entry->set_unique_id(pending_entry_->unique_id()); |
| 676 | |
[email protected] | a0e6926 | 2009-06-03 19:08:48 | [diff] [blame] | 677 | // The URL may have changed due to redirects. |
| 678 | existing_entry->set_url(params.url); |
| 679 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 680 | DiscardNonCommittedEntries(); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | void NavigationController::RendererDidNavigateInPage( |
[email protected] | befd8d82 | 2009-07-01 04:51:47 | [diff] [blame] | 684 | const ViewHostMsg_FrameNavigate_Params& params, bool* did_replace_entry) { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 685 | DCHECK(PageTransition::IsMainFrame(params.transition)) << |
| 686 | "WebKit should only tell us about in-page navs for the main frame."; |
| 687 | // We're guaranteed to have an entry for this one. |
| 688 | NavigationEntry* existing_entry = GetEntryWithPageID( |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 689 | tab_contents_->GetSiteInstance(), |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 690 | params.page_id); |
| 691 | |
| 692 | // Reference fragment navigation. We're guaranteed to have the last_committed |
| 693 | // entry and it will be the same page as the new navigation (minus the |
| 694 | // reference fragments, of course). |
| 695 | NavigationEntry* new_entry = new NavigationEntry(*existing_entry); |
| 696 | new_entry->set_page_id(params.page_id); |
| 697 | new_entry->set_url(params.url); |
[email protected] | befd8d82 | 2009-07-01 04:51:47 | [diff] [blame] | 698 | *did_replace_entry = IsRedirect(params) && |
| 699 | IsLikelyAutoNavigation(base::TimeTicks::Now()); |
| 700 | InsertOrReplaceEntry(new_entry, *did_replace_entry); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | void NavigationController::RendererDidNavigateNewSubframe( |
| 704 | const ViewHostMsg_FrameNavigate_Params& params) { |
[email protected] | 09b8f82f | 2009-06-16 20:22:11 | [diff] [blame] | 705 | if (PageTransition::StripQualifier(params.transition) == |
| 706 | PageTransition::AUTO_SUBFRAME) { |
| 707 | // This is not user-initiated. Ignore. |
| 708 | return; |
| 709 | } |
| 710 | if (IsRedirect(params)) { |
| 711 | // This is redirect. Ignore. |
| 712 | return; |
| 713 | } |
| 714 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 715 | // Manual subframe navigations just get the current entry cloned so the user |
| 716 | // can go back or forward to it. The actual subframe information will be |
| 717 | // stored in the page state for each of those entries. This happens out of |
| 718 | // band with the actual navigations. |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 719 | DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee " |
| 720 | << "that a last committed entry exists."; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 721 | NavigationEntry* new_entry = new NavigationEntry(*GetLastCommittedEntry()); |
| 722 | new_entry->set_page_id(params.page_id); |
[email protected] | 672eba29 | 2009-05-13 13:22:45 | [diff] [blame] | 723 | InsertOrReplaceEntry(new_entry, false); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | bool NavigationController::RendererDidNavigateAutoSubframe( |
| 727 | const ViewHostMsg_FrameNavigate_Params& params) { |
| 728 | // We're guaranteed to have a previously committed entry, and we now need to |
| 729 | // handle navigation inside of a subframe in it without creating a new entry. |
| 730 | DCHECK(GetLastCommittedEntry()); |
| 731 | |
| 732 | // Handle the case where we're navigating back/forward to a previous subframe |
| 733 | // navigation entry. This is case "2." in NAV_AUTO_SUBFRAME comment in the |
| 734 | // header file. In case "1." this will be a NOP. |
| 735 | int entry_index = GetEntryIndexWithPageID( |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 736 | tab_contents_->GetSiteInstance(), |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 737 | params.page_id); |
| 738 | if (entry_index < 0 || |
| 739 | entry_index >= static_cast<int>(entries_.size())) { |
| 740 | NOTREACHED(); |
| 741 | return false; |
| 742 | } |
| 743 | |
| 744 | // Update the current navigation entry in case we're going back/forward. |
| 745 | if (entry_index != last_committed_entry_index_) { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 746 | last_committed_entry_index_ = entry_index; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 747 | return true; |
| 748 | } |
| 749 | return false; |
| 750 | } |
| 751 | |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 752 | // TODO(brettw) I think this function is unnecessary. |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 753 | void NavigationController::CommitPendingEntry() { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 754 | DiscardTransientEntry(); |
| 755 | |
[email protected] | 7f0005a | 2009-04-15 03:25:11 | [diff] [blame] | 756 | if (!pending_entry()) |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 757 | return; // Nothing to do. |
| 758 | |
| 759 | // Need to save the previous URL for the notification. |
| 760 | LoadCommittedDetails details; |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 761 | if (GetLastCommittedEntry()) { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 762 | details.previous_url = GetLastCommittedEntry()->url(); |
[email protected] | 7f0005a | 2009-04-15 03:25:11 | [diff] [blame] | 763 | details.previous_entry_index = last_committed_entry_index(); |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 764 | } else { |
| 765 | details.previous_entry_index = -1; |
| 766 | } |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 767 | |
| 768 | if (pending_entry_index_ >= 0) { |
| 769 | // This is a previous navigation (back/forward) that we're just now |
| 770 | // committing. Just mark it as committed. |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 771 | details.type = NavigationType::EXISTING_PAGE; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 772 | int new_entry_index = pending_entry_index_; |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 773 | DiscardNonCommittedEntriesInternal(); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 774 | |
| 775 | // Mark that entry as committed. |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 776 | last_committed_entry_index_ = new_entry_index; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 777 | } else { |
| 778 | // This is a new navigation. It's easiest to just copy the entry and insert |
[email protected] | 672eba29 | 2009-05-13 13:22:45 | [diff] [blame] | 779 | // it new again, since InsertOrReplaceEntry expects to take ownership and |
| 780 | // also discard the pending entry. We also need to synthesize a page ID. We |
| 781 | // can only do this because this function will only be called by our custom |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 782 | // TabContents types. For TabContents, the IDs are generated by the |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 783 | // renderer, so we can't do this. |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 784 | details.type = NavigationType::NEW_PAGE; |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 785 | pending_entry_->set_page_id(tab_contents_->GetMaxPageID() + 1); |
| 786 | tab_contents_->UpdateMaxPageID(pending_entry_->page_id()); |
[email protected] | 672eba29 | 2009-05-13 13:22:45 | [diff] [blame] | 787 | InsertOrReplaceEntry(new NavigationEntry(*pending_entry_), false); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | // Broadcast the notification of the navigation. |
| 791 | details.entry = GetActiveEntry(); |
| 792 | details.is_auto = false; |
| 793 | details.is_in_page = AreURLsInPageNavigation(details.previous_url, |
| 794 | details.entry->url()); |
| 795 | details.is_main_frame = true; |
| 796 | NotifyNavigationEntryCommitted(&details); |
| 797 | } |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 798 | |
| 799 | int NavigationController::GetIndexOfEntry( |
| 800 | const NavigationEntry* entry) const { |
| 801 | const NavigationEntries::const_iterator i(std::find( |
| 802 | entries_.begin(), |
| 803 | entries_.end(), |
| 804 | entry)); |
| 805 | return (i == entries_.end()) ? -1 : static_cast<int>(i - entries_.begin()); |
| 806 | } |
| 807 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 808 | bool NavigationController::IsURLInPageNavigation(const GURL& url) const { |
| 809 | NavigationEntry* last_committed = GetLastCommittedEntry(); |
| 810 | if (!last_committed) |
| 811 | return false; |
| 812 | return AreURLsInPageNavigation(last_committed->url(), url); |
| 813 | } |
| 814 | |
[email protected] | ce3fa3c | 2009-04-20 19:55:57 | [diff] [blame] | 815 | void NavigationController::CopyStateFrom(const NavigationController& source) { |
| 816 | // Verify that we look new. |
| 817 | DCHECK(entry_count() == 0 && !pending_entry()); |
| 818 | |
| 819 | if (source.entry_count() == 0) |
| 820 | return; // Nothing new to do. |
| 821 | |
| 822 | needs_reload_ = true; |
| 823 | for (int i = 0; i < source.entry_count(); i++) { |
| 824 | entries_.push_back(linked_ptr<NavigationEntry>( |
| 825 | new NavigationEntry(*source.entries_[i]))); |
| 826 | } |
| 827 | |
| 828 | FinishRestore(source.last_committed_entry_index_); |
| 829 | } |
| 830 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 831 | void NavigationController::DiscardNonCommittedEntries() { |
| 832 | bool transient = transient_entry_index_ != -1; |
| 833 | DiscardNonCommittedEntriesInternal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 834 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 835 | // If there was a transient entry, invalidate everything so the new active |
| 836 | // entry state is shown. |
| 837 | if (transient) { |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 838 | tab_contents_->NotifyNavigationStateChanged( |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 839 | TabContents::INVALIDATE_EVERYTHING); |
| 840 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 841 | } |
| 842 | |
[email protected] | 672eba29 | 2009-05-13 13:22:45 | [diff] [blame] | 843 | void NavigationController::InsertOrReplaceEntry(NavigationEntry* entry, |
| 844 | bool replace) { |
[email protected] | 1e5645ff | 2008-08-27 18:09:07 | [diff] [blame] | 845 | DCHECK(entry->transition_type() != PageTransition::AUTO_SUBFRAME); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 846 | |
| 847 | // Copy the pending entry's unique ID to the committed entry. |
| 848 | // I don't know if pending_entry_index_ can be other than -1 here. |
| 849 | const NavigationEntry* const pending_entry = (pending_entry_index_ == -1) ? |
| 850 | pending_entry_ : entries_[pending_entry_index_].get(); |
| 851 | if (pending_entry) |
| 852 | entry->set_unique_id(pending_entry->unique_id()); |
| 853 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 854 | DiscardNonCommittedEntriesInternal(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 855 | |
| 856 | int current_size = static_cast<int>(entries_.size()); |
| 857 | |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 858 | if (current_size > 0) { |
[email protected] | 672eba29 | 2009-05-13 13:22:45 | [diff] [blame] | 859 | // Prune any entries which are in front of the current entry. |
| 860 | // Also prune the current entry if we are to replace the current entry. |
| 861 | int prune_up_to = replace ? last_committed_entry_index_ - 1 |
| 862 | : last_committed_entry_index_; |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 863 | int num_pruned = 0; |
[email protected] | 672eba29 | 2009-05-13 13:22:45 | [diff] [blame] | 864 | while (prune_up_to < (current_size - 1)) { |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 865 | num_pruned++; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 866 | entries_.pop_back(); |
| 867 | current_size--; |
| 868 | } |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 869 | if (num_pruned > 0) // Only notify if we did prune something. |
| 870 | NotifyPrunedEntries(this, false, num_pruned); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 871 | } |
| 872 | |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 873 | if (entries_.size() >= max_entry_count_) { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 874 | RemoveEntryAtIndex(0, GURL()); |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 875 | NotifyPrunedEntries(this, true, 1); |
| 876 | } |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 877 | |
| 878 | entries_.push_back(linked_ptr<NavigationEntry>(entry)); |
| 879 | last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 880 | |
| 881 | // This is a new page ID, so we need everybody to know about it. |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 882 | tab_contents_->UpdateMaxPageID(entry->page_id()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | void NavigationController::SetWindowID(const SessionID& id) { |
| 886 | window_id_ = id; |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 887 | NotificationService::current()->Notify(NotificationType::TAB_PARENTED, |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 888 | Source<NavigationController>(this), |
| 889 | NotificationService::NoDetails()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | void NavigationController::NavigateToPendingEntry(bool reload) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 893 | // For session history navigations only the pending_entry_index_ is set. |
| 894 | if (!pending_entry_) { |
| 895 | DCHECK(pending_entry_index_ != -1); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 896 | pending_entry_ = entries_[pending_entry_index_].get(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 897 | } |
| 898 | |
[email protected] | b680ad2 | 2009-04-15 23:19:42 | [diff] [blame] | 899 | if (!tab_contents_->NavigateToPendingEntry(reload)) |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 900 | DiscardNonCommittedEntries(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 901 | } |
| 902 | |
[email protected] | ecd9d870 | 2008-08-28 22:10:17 | [diff] [blame] | 903 | void NavigationController::NotifyNavigationEntryCommitted( |
| 904 | LoadCommittedDetails* details) { |
[email protected] | df1af24 | 2009-05-01 00:11:40 | [diff] [blame] | 905 | details->entry = GetActiveEntry(); |
| 906 | NotificationDetails notification_details = |
| 907 | Details<LoadCommittedDetails>(details); |
| 908 | |
| 909 | // We need to notify the ssl_manager_ before the tab_contents_ so the |
| 910 | // location bar will have up-to-date information about the security style |
| 911 | // when it wants to draw. See http://crbug.com/11157 |
| 912 | ssl_manager_.DidCommitProvisionalLoad(notification_details); |
| 913 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 914 | // TODO(pkasting): http://b/1113079 Probably these explicit notification paths |
| 915 | // should be removed, and interested parties should just listen for the |
| 916 | // notification below instead. |
[email protected] | 9423d941 | 2009-04-14 22:13:55 | [diff] [blame] | 917 | tab_contents_->NotifyNavigationStateChanged( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 918 | TabContents::INVALIDATE_EVERYTHING); |
| 919 | |
[email protected] | ecd9d870 | 2008-08-28 22:10:17 | [diff] [blame] | 920 | NotificationService::current()->Notify( |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 921 | NotificationType::NAV_ENTRY_COMMITTED, |
[email protected] | ecd9d870 | 2008-08-28 22:10:17 | [diff] [blame] | 922 | Source<NavigationController>(this), |
[email protected] | df1af24 | 2009-05-01 00:11:40 | [diff] [blame] | 923 | notification_details); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 924 | } |
| 925 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 926 | // static |
| 927 | void NavigationController::DisablePromptOnRepost() { |
| 928 | check_for_repost_ = false; |
| 929 | } |
| 930 | |
| 931 | void NavigationController::SetActive(bool is_active) { |
[email protected] | ee61392 | 2009-09-02 20:38:22 | [diff] [blame] | 932 | if (is_active && needs_reload_) |
| 933 | LoadIfNecessary(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | void NavigationController::LoadIfNecessary() { |
| 937 | if (!needs_reload_) |
| 938 | return; |
| 939 | |
| 940 | needs_reload_ = false; |
| 941 | // Calling Reload() results in ignoring state, and not loading. |
| 942 | // Explicitly use NavigateToPendingEntry so that the renderer uses the |
| 943 | // cached state. |
| 944 | pending_entry_index_ = last_committed_entry_index_; |
| 945 | NavigateToPendingEntry(false); |
| 946 | } |
| 947 | |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 948 | void NavigationController::NotifyEntryChanged(const NavigationEntry* entry, |
| 949 | int index) { |
| 950 | EntryChangedDetails det; |
| 951 | det.changed_entry = entry; |
| 952 | det.index = index; |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 953 | NotificationService::current()->Notify(NotificationType::NAV_ENTRY_CHANGED, |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 954 | Source<NavigationController>(this), |
| 955 | Details<EntryChangedDetails>(&det)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 956 | } |
| 957 | |
[email protected] | ec322ff | 2008-11-19 22:53:30 | [diff] [blame] | 958 | void NavigationController::FinishRestore(int selected_index) { |
[email protected] | 7f0005a | 2009-04-15 03:25:11 | [diff] [blame] | 959 | DCHECK(selected_index >= 0 && selected_index < entry_count()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 960 | ConfigureEntriesForRestore(&entries_); |
| 961 | |
[email protected] | 7f0005a | 2009-04-15 03:25:11 | [diff] [blame] | 962 | set_max_restored_page_id(entry_count()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 963 | |
| 964 | last_committed_entry_index_ = selected_index; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 965 | } |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 966 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 967 | void NavigationController::DiscardNonCommittedEntriesInternal() { |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 968 | if (pending_entry_index_ == -1) |
| 969 | delete pending_entry_; |
| 970 | pending_entry_ = NULL; |
| 971 | pending_entry_index_ = -1; |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 972 | |
| 973 | DiscardTransientEntry(); |
| 974 | } |
| 975 | |
| 976 | void NavigationController::DiscardTransientEntry() { |
| 977 | if (transient_entry_index_ == -1) |
| 978 | return; |
[email protected] | a0e6926 | 2009-06-03 19:08:48 | [diff] [blame] | 979 | entries_.erase(entries_.begin() + transient_entry_index_); |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 980 | transient_entry_index_ = -1; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | int NavigationController::GetEntryIndexWithPageID( |
[email protected] | 7f0005a | 2009-04-15 03:25:11 | [diff] [blame] | 984 | SiteInstance* instance, int32 page_id) const { |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 985 | for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) { |
[email protected] | 7f0005a | 2009-04-15 03:25:11 | [diff] [blame] | 986 | if ((entries_[i]->site_instance() == instance) && |
[email protected] | 1e5645ff | 2008-08-27 18:09:07 | [diff] [blame] | 987 | (entries_[i]->page_id() == page_id)) |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 988 | return i; |
| 989 | } |
| 990 | return -1; |
| 991 | } |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 992 | |
| 993 | NavigationEntry* NavigationController::GetTransientEntry() const { |
| 994 | if (transient_entry_index_ == -1) |
| 995 | return NULL; |
| 996 | return entries_[transient_entry_index_].get(); |
| 997 | } |