license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.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 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | #include "base/file_util.h" |
| 8 | #include "base/logging.h" |
| 9 | #include "base/string_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include "chrome/browser/browser_process.h" |
| 11 | #include "chrome/browser/dom_ui/dom_ui_host.h" |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 12 | #include "chrome/browser/sessions/session_types.h" |
[email protected] | f3ec774 | 2009-01-15 00:59:16 | [diff] [blame] | 13 | #include "chrome/browser/tab_contents/navigation_entry.h" |
[email protected] | b5bb35f | 2009-02-05 20:17:07 | [diff] [blame] | 14 | #include "chrome/browser/tab_contents/repost_form_warning.h" |
[email protected] | f3ec774 | 2009-01-15 00:59:16 | [diff] [blame] | 15 | #include "chrome/browser/tab_contents/site_instance.h" |
[email protected] | 299dabd | 2008-11-19 02:27:16 | [diff] [blame] | 16 | #include "chrome/common/navigation_types.h" |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 17 | #include "chrome/common/notification_service.h" |
[email protected] | e09ba55 | 2009-02-05 03:26:29 | [diff] [blame] | 18 | #include "chrome/common/render_messages.h" |
[email protected] | 299dabd | 2008-11-19 02:27:16 | [diff] [blame] | 19 | #include "chrome/common/resource_bundle.h" |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 20 | #include "webkit/glue/webkit_glue.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | |
[email protected] | faa138b | 2009-02-05 18:33:15 | [diff] [blame] | 22 | #if defined(OS_WIN) |
[email protected] | 32b8d5e9 | 2009-02-05 20:27:18 | [diff] [blame] | 23 | #include "chrome/browser/tab_contents/repost_form_warning.h" |
[email protected] | faa138b | 2009-02-05 18:33:15 | [diff] [blame] | 24 | #include "chrome/browser/tab_contents/tab_contents.h" |
| 25 | #include "chrome/browser/tab_contents/tab_contents_delegate.h" |
[email protected] | faa138b | 2009-02-05 18:33:15 | [diff] [blame] | 26 | #endif |
| 27 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | // Invoked when entries have been pruned, or removed. For example, if the |
| 31 | // current entries are [google, digg, yahoo], with the current entry google, |
| 32 | // and the user types in cnet, then digg and yahoo are pruned. |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 33 | void NotifyPrunedEntries(NavigationController* nav_controller, |
| 34 | bool from_front, |
| 35 | int count) { |
| 36 | NavigationController::PrunedDetails details; |
| 37 | details.from_front = from_front; |
| 38 | details.count = count; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 39 | NotificationService::current()->Notify( |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 40 | NotificationType::NAV_LIST_PRUNED, |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 41 | Source<NavigationController>(nav_controller), |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 42 | Details<NavigationController::PrunedDetails>(&details)); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // Ensure the given NavigationEntry has a valid state, so that WebKit does not |
| 46 | // get confused if we navigate back to it. |
| 47 | // |
| 48 | // An empty state is treated as a new navigation by WebKit, which would mean |
| 49 | // losing the navigation entries and generating a new navigation entry after |
| 50 | // this one. We don't want that. To avoid this we create a valid state which |
| 51 | // WebKit will not treat as a new navigation. |
| 52 | void SetContentStateIfEmpty(NavigationEntry* entry) { |
| 53 | if (entry->content_state().empty() && |
| 54 | (entry->tab_type() == TAB_CONTENTS_WEB || |
| 55 | entry->tab_type() == TAB_CONTENTS_NEW_TAB_UI || |
| 56 | entry->tab_type() == TAB_CONTENTS_ABOUT_UI || |
[email protected] | 581532a | 2008-12-31 02:08:33 | [diff] [blame] | 57 | entry->tab_type() == TAB_CONTENTS_HTML_DIALOG || |
| 58 | entry->tab_type() == TAB_CONTENTS_VIEW_SOURCE)) { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 59 | entry->set_content_state( |
| 60 | webkit_glue::CreateHistoryStateForURL(entry->url())); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Configure all the NavigationEntries in entries for restore. This resets |
| 65 | // the transition type to reload and makes sure the content state isn't empty. |
| 66 | void ConfigureEntriesForRestore( |
| 67 | std::vector<linked_ptr<NavigationEntry> >* entries) { |
| 68 | for (size_t i = 0; i < entries->size(); ++i) { |
| 69 | // Use a transition type of reload so that we don't incorrectly increase |
| 70 | // the typed count. |
| 71 | (*entries)[i]->set_transition_type(PageTransition::RELOAD); |
| 72 | (*entries)[i]->set_restored(true); |
| 73 | // NOTE(darin): This code is only needed for backwards compat. |
| 74 | SetContentStateIfEmpty((*entries)[i].get()); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // See NavigationController::IsURLInPageNavigation for how this works and why. |
| 79 | bool AreURLsInPageNavigation(const GURL& existing_url, const GURL& new_url) { |
| 80 | if (existing_url == new_url || !new_url.has_ref()) |
| 81 | return false; |
| 82 | |
| 83 | url_canon::Replacements<char> replacements; |
| 84 | replacements.ClearRef(); |
| 85 | return existing_url.ReplaceComponents(replacements) == |
| 86 | new_url.ReplaceComponents(replacements); |
| 87 | } |
| 88 | |
| 89 | } // namespace |
| 90 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 91 | // TabContentsCollector --------------------------------------------------- |
| 92 | |
| 93 | // We never destroy a TabContents synchronously because there are some |
| 94 | // complex code path that cause the current TabContents to be in the call |
| 95 | // stack. So instead, we use a TabContentsCollector which either destroys |
| 96 | // the TabContents or does nothing if it has been cancelled. |
| 97 | class TabContentsCollector : public Task { |
| 98 | public: |
| 99 | TabContentsCollector(NavigationController* target, |
| 100 | TabContentsType target_type) |
| 101 | : target_(target), |
| 102 | target_type_(target_type) { |
| 103 | } |
| 104 | |
| 105 | void Cancel() { |
| 106 | target_ = NULL; |
| 107 | } |
| 108 | |
| 109 | virtual void Run() { |
| 110 | if (target_) { |
| 111 | // Note: this will cancel this task as a side effect so target_ is |
| 112 | // now null. |
| 113 | TabContents* tc = target_->GetTabContents(target_type_); |
| 114 | tc->Destroy(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | private: |
| 119 | // The NavigationController we are acting on. |
| 120 | NavigationController* target_; |
| 121 | |
| 122 | // The TabContentsType that needs to be collected. |
| 123 | TabContentsType target_type_; |
| 124 | |
| 125 | DISALLOW_EVIL_CONSTRUCTORS(TabContentsCollector); |
| 126 | }; |
| 127 | |
| 128 | // NavigationController --------------------------------------------------- |
| 129 | |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 130 | // static |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 131 | size_t NavigationController::max_entry_count_ = 50; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 132 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 133 | // static |
| 134 | bool NavigationController::check_for_repost_ = true; |
| 135 | |
| 136 | // Creates a new NavigationEntry for each TabNavigation in navigations, adding |
| 137 | // the NavigationEntry to entries. This is used during session restore. |
| 138 | static void CreateNavigationEntriesFromTabNavigations( |
| 139 | const std::vector<TabNavigation>& navigations, |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 140 | std::vector<linked_ptr<NavigationEntry> >* entries) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 141 | // Create a NavigationEntry for each of the navigations. |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 142 | int page_id = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 143 | for (std::vector<TabNavigation>::const_iterator i = |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 144 | navigations.begin(); i != navigations.end(); ++i, ++page_id) { |
| 145 | entries->push_back( |
| 146 | linked_ptr<NavigationEntry>(i->ToNavigationEntry(page_id))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | NavigationController::NavigationController(TabContents* contents, |
| 151 | Profile* profile) |
| 152 | : profile_(profile), |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 153 | pending_entry_(NULL), |
| 154 | last_committed_entry_index_(-1), |
| 155 | pending_entry_index_(-1), |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 156 | transient_entry_index_(-1), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 157 | active_contents_(contents), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 158 | max_restored_page_id_(-1), |
| 159 | ssl_manager_(this, NULL), |
| 160 | needs_reload_(false), |
| 161 | load_pending_entry_when_active_(false) { |
| 162 | if (contents) |
| 163 | RegisterTabContents(contents); |
| 164 | DCHECK(profile_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | NavigationController::NavigationController( |
| 168 | Profile* profile, |
| 169 | const std::vector<TabNavigation>& navigations, |
[email protected] | ec322ff | 2008-11-19 22:53:30 | [diff] [blame] | 170 | int selected_navigation) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 171 | : profile_(profile), |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 172 | pending_entry_(NULL), |
| 173 | last_committed_entry_index_(-1), |
| 174 | pending_entry_index_(-1), |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 175 | transient_entry_index_(-1), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 176 | active_contents_(NULL), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 177 | max_restored_page_id_(-1), |
| 178 | ssl_manager_(this, NULL), |
| 179 | needs_reload_(true), |
| 180 | load_pending_entry_when_active_(false) { |
| 181 | DCHECK(profile_); |
| 182 | DCHECK(selected_navigation >= 0 && |
| 183 | selected_navigation < static_cast<int>(navigations.size())); |
| 184 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 185 | // Populate entries_ from the supplied TabNavigations. |
| 186 | CreateNavigationEntriesFromTabNavigations(navigations, &entries_); |
| 187 | |
| 188 | // And finish the restore. |
[email protected] | ec322ff | 2008-11-19 22:53:30 | [diff] [blame] | 189 | FinishRestore(selected_navigation); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | NavigationController::~NavigationController() { |
| 193 | DCHECK(tab_contents_map_.empty()); |
| 194 | DCHECK(tab_contents_collector_map_.empty()); |
| 195 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 196 | DiscardNonCommittedEntriesInternal(); |
[email protected] | c099387 | 2008-08-21 19:59:44 | [diff] [blame] | 197 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 198 | NotificationService::current()->Notify( |
| 199 | NotificationType::TAB_CLOSED, |
| 200 | Source<NavigationController>(this), |
| 201 | NotificationService::NoDetails()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | TabContents* NavigationController::GetTabContents(TabContentsType t) { |
| 205 | // Make sure the TabContents is no longer scheduled for collection. |
| 206 | CancelTabContentsCollection(t); |
| 207 | return tab_contents_map_[t]; |
| 208 | } |
| 209 | |
[email protected] | f1c7411 | 2008-10-30 16:17:04 | [diff] [blame] | 210 | void NavigationController::Reload(bool check_for_repost) { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 211 | // Reloading a transient entry does nothing. |
| 212 | if (transient_entry_index_ != -1) |
| 213 | return; |
| 214 | |
| 215 | DiscardNonCommittedEntriesInternal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 216 | int current_index = GetCurrentEntryIndex(); |
[email protected] | f1c7411 | 2008-10-30 16:17:04 | [diff] [blame] | 217 | if (check_for_repost_ && check_for_repost && current_index != -1 && |
[email protected] | a3a1d14 | 2008-12-19 00:42:30 | [diff] [blame] | 218 | GetEntryAtIndex(current_index)->has_post_data()) { |
| 219 | // 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] | 220 | // they really want to do this. If they do, the dialog will call us back |
| 221 | // with check_for_repost = false. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 222 | active_contents_->Activate(); |
[email protected] | b5bb35f | 2009-02-05 20:17:07 | [diff] [blame] | 223 | RunRepostFormWarningDialog(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 224 | } else { |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 225 | // Base the navigation on where we are now... |
| 226 | int current_index = GetCurrentEntryIndex(); |
| 227 | |
| 228 | // If we are no where, then we can't reload. TODO(darin): We should add a |
| 229 | // CanReload method. |
| 230 | if (current_index == -1) |
| 231 | return; |
| 232 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 233 | DiscardNonCommittedEntriesInternal(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 234 | |
| 235 | pending_entry_index_ = current_index; |
[email protected] | 1e5645ff | 2008-08-27 18:09:07 | [diff] [blame] | 236 | entries_[pending_entry_index_]->set_transition_type(PageTransition::RELOAD); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 237 | NavigateToPendingEntry(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 241 | NavigationEntry* NavigationController::GetEntryWithPageID( |
| 242 | TabContentsType type, SiteInstance* instance, int32 page_id) const { |
| 243 | int index = GetEntryIndexWithPageID(type, instance, page_id); |
| 244 | return (index != -1) ? entries_[index].get() : NULL; |
| 245 | } |
| 246 | |
| 247 | void NavigationController::LoadEntry(NavigationEntry* entry) { |
| 248 | // When navigating to a new page, we don't know for sure if we will actually |
| 249 | // end up leaving the current page. The new page load could for example |
| 250 | // 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] | 251 | DiscardNonCommittedEntriesInternal(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 252 | pending_entry_ = entry; |
| 253 | NotificationService::current()->Notify( |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 254 | NotificationType::NAV_ENTRY_PENDING, |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 255 | Source<NavigationController>(this), |
| 256 | NotificationService::NoDetails()); |
| 257 | NavigateToPendingEntry(false); |
| 258 | } |
| 259 | |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 260 | NavigationEntry* NavigationController::GetActiveEntry() const { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 261 | if (transient_entry_index_ != -1) |
| 262 | return entries_[transient_entry_index_].get(); |
| 263 | if (pending_entry_) |
| 264 | return pending_entry_; |
| 265 | return GetLastCommittedEntry(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | int NavigationController::GetCurrentEntryIndex() const { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 269 | if (transient_entry_index_ != -1) |
| 270 | return transient_entry_index_; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 271 | if (pending_entry_index_ != -1) |
| 272 | return pending_entry_index_; |
| 273 | return last_committed_entry_index_; |
| 274 | } |
| 275 | |
| 276 | NavigationEntry* NavigationController::GetLastCommittedEntry() const { |
| 277 | if (last_committed_entry_index_ == -1) |
| 278 | return NULL; |
| 279 | return entries_[last_committed_entry_index_].get(); |
| 280 | } |
| 281 | |
| 282 | NavigationEntry* NavigationController::GetEntryAtOffset(int offset) const { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 283 | int index = (transient_entry_index_ != -1) ? |
| 284 | transient_entry_index_ + offset : |
| 285 | last_committed_entry_index_ + offset; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 286 | if (index < 0 || index >= GetEntryCount()) |
| 287 | return NULL; |
| 288 | |
| 289 | return entries_[index].get(); |
| 290 | } |
| 291 | |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 292 | bool NavigationController::CanGoBack() const { |
| 293 | return entries_.size() > 1 && GetCurrentEntryIndex() > 0; |
| 294 | } |
| 295 | |
| 296 | bool NavigationController::CanGoForward() const { |
| 297 | int index = GetCurrentEntryIndex(); |
| 298 | return index >= 0 && index < (static_cast<int>(entries_.size()) - 1); |
| 299 | } |
| 300 | |
| 301 | void NavigationController::GoBack() { |
| 302 | if (!CanGoBack()) { |
| 303 | NOTREACHED(); |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | // Base the navigation on where we are now... |
| 308 | int current_index = GetCurrentEntryIndex(); |
| 309 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 310 | DiscardNonCommittedEntries(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 311 | |
| 312 | pending_entry_index_ = current_index - 1; |
| 313 | NavigateToPendingEntry(false); |
| 314 | } |
| 315 | |
| 316 | void NavigationController::GoForward() { |
| 317 | if (!CanGoForward()) { |
| 318 | NOTREACHED(); |
| 319 | return; |
| 320 | } |
| 321 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 322 | bool transient = (transient_entry_index_ != -1); |
| 323 | |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 324 | // Base the navigation on where we are now... |
| 325 | int current_index = GetCurrentEntryIndex(); |
| 326 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 327 | DiscardNonCommittedEntries(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 328 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 329 | pending_entry_index_ = current_index; |
| 330 | // If there was a transient entry, we removed it making the current index |
| 331 | // the next page. |
| 332 | if (!transient) |
| 333 | pending_entry_index_++; |
| 334 | |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 335 | NavigateToPendingEntry(false); |
| 336 | } |
| 337 | |
| 338 | void NavigationController::GoToIndex(int index) { |
| 339 | if (index < 0 || index >= static_cast<int>(entries_.size())) { |
| 340 | NOTREACHED(); |
| 341 | return; |
| 342 | } |
| 343 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 344 | if (transient_entry_index_ != -1) { |
| 345 | if (index == transient_entry_index_) { |
| 346 | // Nothing to do when navigating to the transient. |
| 347 | return; |
| 348 | } |
| 349 | if (index > transient_entry_index_) { |
| 350 | // Removing the transient is goint to shift all entries by 1. |
| 351 | index--; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | DiscardNonCommittedEntries(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 356 | |
| 357 | pending_entry_index_ = index; |
| 358 | NavigateToPendingEntry(false); |
| 359 | } |
| 360 | |
| 361 | void NavigationController::GoToOffset(int offset) { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 362 | int index = (transient_entry_index_ != -1) ? |
| 363 | transient_entry_index_ + offset : |
| 364 | last_committed_entry_index_ + offset; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 365 | if (index < 0 || index >= GetEntryCount()) |
| 366 | return; |
| 367 | |
| 368 | GoToIndex(index); |
| 369 | } |
| 370 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 371 | void NavigationController::RemoveEntryAtIndex(int index, |
| 372 | const GURL& default_url) { |
| 373 | int size = static_cast<int>(entries_.size()); |
| 374 | DCHECK(index < size); |
| 375 | |
| 376 | DiscardNonCommittedEntries(); |
| 377 | |
| 378 | entries_.erase(entries_.begin() + index); |
| 379 | |
| 380 | if (last_committed_entry_index_ == index) { |
| 381 | last_committed_entry_index_--; |
| 382 | // We removed the currently shown entry, so we have to load something else. |
| 383 | if (last_committed_entry_index_ != -1) { |
| 384 | pending_entry_index_ = last_committed_entry_index_; |
| 385 | NavigateToPendingEntry(false); |
| 386 | } else { |
| 387 | // If there is nothing to show, show a default page. |
| 388 | LoadURL(default_url.is_empty() ? GURL("about:blank") : default_url, |
[email protected] | c058805 | 2008-10-27 23:01:50 | [diff] [blame] | 389 | GURL(), PageTransition::START_PAGE); |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 390 | } |
| 391 | } else if (last_committed_entry_index_ > index) { |
| 392 | last_committed_entry_index_--; |
| 393 | } |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 394 | } |
| 395 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 396 | void NavigationController::Destroy() { |
[email protected] | b3345230 | 2008-08-04 19:36:36 | [diff] [blame] | 397 | // Close all tab contents owned by this controller. We make a list on the |
| 398 | // stack because they are removed from the map as they are Destroyed |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 399 | // (invalidating the iterators), which may or may not occur synchronously. |
[email protected] | b3345230 | 2008-08-04 19:36:36 | [diff] [blame] | 400 | // We also keep track of any NULL entries in the map so that we can clean |
| 401 | // them out. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 402 | std::list<TabContents*> tabs_to_destroy; |
[email protected] | b3345230 | 2008-08-04 19:36:36 | [diff] [blame] | 403 | std::list<TabContentsType> tab_types_to_erase; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 404 | for (TabContentsMap::iterator i = tab_contents_map_.begin(); |
| 405 | i != tab_contents_map_.end(); ++i) { |
[email protected] | b3345230 | 2008-08-04 19:36:36 | [diff] [blame] | 406 | if (i->second) |
| 407 | tabs_to_destroy.push_back(i->second); |
| 408 | else |
| 409 | tab_types_to_erase.push_back(i->first); |
| 410 | } |
| 411 | |
| 412 | // Clean out all NULL entries in the map so that we know empty map means all |
| 413 | // tabs destroyed. This is needed since TabContentsWasDestroyed() won't get |
| 414 | // called for types that are in our map with a NULL contents. (We don't do |
| 415 | // this by iterating over TAB_CONTENTS_NUM_TYPES because some tests create |
| 416 | // additional types.) |
| 417 | for (std::list<TabContentsType>::iterator i = tab_types_to_erase.begin(); |
| 418 | i != tab_types_to_erase.end(); ++i) { |
| 419 | TabContentsMap::iterator map_iterator = tab_contents_map_.find(*i); |
| 420 | if (map_iterator != tab_contents_map_.end()) { |
| 421 | DCHECK(!map_iterator->second); |
| 422 | tab_contents_map_.erase(map_iterator); |
| 423 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | // Cancel all the TabContentsCollectors. |
| 427 | for (TabContentsCollectorMap::iterator i = |
| 428 | tab_contents_collector_map_.begin(); |
| 429 | i != tab_contents_collector_map_.end(); ++i) { |
| 430 | DCHECK(i->second); |
| 431 | i->second->Cancel(); |
| 432 | } |
[email protected] | ccfc1a7b | 2008-08-14 16:26:20 | [diff] [blame] | 433 | tab_contents_collector_map_.clear(); |
| 434 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 435 | |
| 436 | // Finally destroy all the tab contents. |
| 437 | for (std::list<TabContents*>::iterator i = tabs_to_destroy.begin(); |
| 438 | i != tabs_to_destroy.end(); ++i) { |
| 439 | (*i)->Destroy(); |
| 440 | } |
| 441 | // We are deleted at this point. |
| 442 | } |
| 443 | |
| 444 | void NavigationController::TabContentsWasDestroyed(TabContentsType type) { |
| 445 | TabContentsMap::iterator i = tab_contents_map_.find(type); |
| 446 | DCHECK(i != tab_contents_map_.end()); |
| 447 | tab_contents_map_.erase(i); |
| 448 | |
| 449 | // Make sure we cancel any collector for that TabContents. |
[email protected] | ccfc1a7b | 2008-08-14 16:26:20 | [diff] [blame] | 450 | CancelTabContentsCollection(type); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 451 | |
| 452 | // If that was the last tab to be destroyed, delete ourselves. |
| 453 | if (tab_contents_map_.empty()) |
| 454 | delete this; |
| 455 | } |
| 456 | |
| 457 | NavigationEntry* NavigationController::CreateNavigationEntry( |
[email protected] | c058805 | 2008-10-27 23:01:50 | [diff] [blame] | 458 | const GURL& url, const GURL& referrer, PageTransition::Type transition) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 459 | GURL real_url = url; |
| 460 | TabContentsType type; |
| 461 | |
| 462 | // If the active contents supports |url|, use it. |
| 463 | // Note: in both cases, we give TabContents a chance to rewrite the URL. |
| 464 | TabContents* active = active_contents(); |
| 465 | if (active && active->SupportsURL(&real_url)) |
| 466 | type = active->type(); |
| 467 | else |
| 468 | type = TabContents::TypeForURL(&real_url); |
| 469 | |
| 470 | NavigationEntry* entry = new NavigationEntry(type, NULL, -1, real_url, |
[email protected] | c058805 | 2008-10-27 23:01:50 | [diff] [blame] | 471 | referrer, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 472 | std::wstring(), transition); |
[email protected] | 1e5645ff | 2008-08-27 18:09:07 | [diff] [blame] | 473 | entry->set_display_url(url); |
| 474 | entry->set_user_typed_url(url); |
[email protected] | 8654b68 | 2008-11-01 23:36:17 | [diff] [blame] | 475 | if (url.SchemeIsFile()) { |
| 476 | entry->set_title(file_util::GetFilenameFromPath(UTF8ToWide(url.host() + |
| 477 | url.path()))); |
| 478 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 479 | return entry; |
| 480 | } |
| 481 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 482 | void NavigationController::AddTransientEntry(NavigationEntry* entry) { |
| 483 | // Discard any current transient entry, we can only have one at a time. |
| 484 | int index = 0; |
| 485 | if (last_committed_entry_index_ != -1) |
| 486 | index = last_committed_entry_index_ + 1; |
| 487 | DiscardTransientEntry(); |
| 488 | entries_.insert(entries_.begin() + index, linked_ptr<NavigationEntry>(entry)); |
| 489 | transient_entry_index_ = index; |
| 490 | active_contents_->NotifyNavigationStateChanged( |
| 491 | TabContents::INVALIDATE_EVERYTHING); |
| 492 | } |
| 493 | |
[email protected] | c058805 | 2008-10-27 23:01:50 | [diff] [blame] | 494 | void NavigationController::LoadURL(const GURL& url, const GURL& referrer, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 495 | PageTransition::Type transition) { |
| 496 | // The user initiated a load, we don't need to reload anymore. |
| 497 | needs_reload_ = false; |
| 498 | |
[email protected] | c058805 | 2008-10-27 23:01:50 | [diff] [blame] | 499 | NavigationEntry* entry = CreateNavigationEntry(url, referrer, transition); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 500 | |
| 501 | LoadEntry(entry); |
| 502 | } |
| 503 | |
| 504 | void NavigationController::LoadURLLazily(const GURL& url, |
[email protected] | c058805 | 2008-10-27 23:01:50 | [diff] [blame] | 505 | const GURL& referrer, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 506 | PageTransition::Type type, |
| 507 | const std::wstring& title, |
| 508 | SkBitmap* icon) { |
[email protected] | c058805 | 2008-10-27 23:01:50 | [diff] [blame] | 509 | NavigationEntry* entry = CreateNavigationEntry(url, referrer, type); |
[email protected] | 1e5645ff | 2008-08-27 18:09:07 | [diff] [blame] | 510 | entry->set_title(title); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 511 | if (icon) |
[email protected] | 1e5645ff | 2008-08-27 18:09:07 | [diff] [blame] | 512 | entry->favicon().set_bitmap(*icon); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 513 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 514 | DiscardNonCommittedEntriesInternal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 515 | pending_entry_ = entry; |
| 516 | load_pending_entry_when_active_ = true; |
| 517 | } |
| 518 | |
| 519 | bool NavigationController::LoadingURLLazily() { |
| 520 | return load_pending_entry_when_active_; |
| 521 | } |
| 522 | |
| 523 | const std::wstring& NavigationController::GetLazyTitle() const { |
| 524 | if (pending_entry_) |
[email protected] | 3d627bbc | 2008-10-23 20:49:07 | [diff] [blame] | 525 | return pending_entry_->GetTitleForDisplay(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 526 | else |
| 527 | return EmptyWString(); |
| 528 | } |
| 529 | |
| 530 | const SkBitmap& NavigationController::GetLazyFavIcon() const { |
| 531 | if (pending_entry_) { |
[email protected] | 1e5645ff | 2008-08-27 18:09:07 | [diff] [blame] | 532 | return pending_entry_->favicon().bitmap(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 533 | } else { |
| 534 | ResourceBundle &rb = ResourceBundle::GetSharedInstance(); |
| 535 | return *rb.GetBitmapNamed(IDR_DEFAULT_FAVICON); |
| 536 | } |
| 537 | } |
| 538 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 539 | bool NavigationController::RendererDidNavigate( |
| 540 | const ViewHostMsg_FrameNavigate_Params& params, |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 541 | LoadCommittedDetails* details) { |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 542 | // Save the previous state before we clobber it. |
| 543 | if (GetLastCommittedEntry()) { |
[email protected] | ecd9d870 | 2008-08-28 22:10:17 | [diff] [blame] | 544 | details->previous_url = GetLastCommittedEntry()->url(); |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 545 | details->previous_entry_index = GetLastCommittedEntryIndex(); |
| 546 | } else { |
| 547 | details->previous_url = GURL(); |
| 548 | details->previous_entry_index = -1; |
| 549 | } |
[email protected] | ecd9d870 | 2008-08-28 22:10:17 | [diff] [blame] | 550 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 551 | // Assign the current site instance to any pending entry, so we can find it |
| 552 | // later by calling GetEntryIndexWithPageID. We only care about this if the |
| 553 | // pending entry is an existing navigation and not a new one (or else we |
| 554 | // wouldn't care about finding it with GetEntryIndexWithPageID). |
| 555 | // |
| 556 | // TODO(brettw) this seems slightly bogus as we don't really know if the |
| 557 | // pending entry is what this navigation is for. There is a similar TODO |
| 558 | // w.r.t. the pending entry in RendererDidNavigateToNewPage. |
| 559 | if (pending_entry_index_ >= 0) |
| 560 | pending_entry_->set_site_instance(active_contents_->GetSiteInstance()); |
| 561 | |
| 562 | // Do navigation-type specific actions. These will make and commit an entry. |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 563 | details->type = ClassifyNavigation(params); |
| 564 | switch (details->type) { |
| 565 | case NavigationType::NEW_PAGE: |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 566 | RendererDidNavigateToNewPage(params); |
| 567 | break; |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 568 | case NavigationType::EXISTING_PAGE: |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 569 | RendererDidNavigateToExistingPage(params); |
| 570 | break; |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 571 | case NavigationType::SAME_PAGE: |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 572 | RendererDidNavigateToSamePage(params); |
| 573 | break; |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 574 | case NavigationType::IN_PAGE: |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 575 | RendererDidNavigateInPage(params); |
| 576 | break; |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 577 | case NavigationType::NEW_SUBFRAME: |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 578 | RendererDidNavigateNewSubframe(params); |
| 579 | break; |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 580 | case NavigationType::AUTO_SUBFRAME: |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 581 | if (!RendererDidNavigateAutoSubframe(params)) |
| 582 | return false; |
| 583 | break; |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 584 | case NavigationType::NAV_IGNORE: |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 585 | // There is nothing we can do with this navigation, so we just return to |
| 586 | // the caller that nothing has happened. |
| 587 | return false; |
| 588 | default: |
| 589 | NOTREACHED(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 590 | } |
| 591 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 592 | // All committed entries should have nonempty content state so WebKit doesn't |
| 593 | // get confused when we go back to them (see the function for details). |
| 594 | SetContentStateIfEmpty(GetActiveEntry()); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 595 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 596 | // WebKit doesn't set the "auto" transition on meta refreshes properly (bug |
| 597 | // 1051891) so we manually set it for redirects which we normally treat as |
| 598 | // "non-user-gestures" where we want to update stuff after navigations. |
| 599 | // |
| 600 | // Note that the redirect check also checks for a pending entry to |
| 601 | // differentiate real redirects from browser initiated navigations to a |
| 602 | // redirected entry. This happens when you hit back to go to a page that was |
| 603 | // the destination of a redirect, we don't want to treat it as a redirect |
| 604 | // even though that's what its transition will be. See bug 1117048. |
| 605 | // |
| 606 | // TODO(brettw) write a test for this complicated logic. |
| 607 | details->is_auto = (PageTransition::IsRedirect(params.transition) && |
| 608 | !GetPendingEntry()) || |
| 609 | params.gesture == NavigationGestureAuto; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 610 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 611 | // Now prep the rest of the details for the notification and broadcast. |
| 612 | details->entry = GetActiveEntry(); |
| 613 | details->is_in_page = IsURLInPageNavigation(params.url); |
| 614 | details->is_main_frame = PageTransition::IsMainFrame(params.transition); |
[email protected] | f072d2ce | 2008-09-17 17:16:24 | [diff] [blame] | 615 | details->serialized_security_info = params.security_info; |
[email protected] | 8a3422c9 | 2008-09-24 17:42:42 | [diff] [blame] | 616 | details->is_content_filtered = params.is_content_filtered; |
[email protected] | 2e39d2e | 2009-02-19 18:41:31 | [diff] [blame^] | 617 | details->http_status_code = params.http_status_code; |
[email protected] | ecd9d870 | 2008-08-28 22:10:17 | [diff] [blame] | 618 | NotifyNavigationEntryCommitted(details); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 619 | |
[email protected] | b3345230 | 2008-08-04 19:36:36 | [diff] [blame] | 620 | // It is now a safe time to schedule collection for any tab contents of a |
| 621 | // different type, because a navigation is necessary to get back to them. |
[email protected] | 50664fd | 2008-08-28 16:10:30 | [diff] [blame] | 622 | ScheduleTabContentsCollectionForInactiveTabs(); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 623 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 624 | } |
| 625 | |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 626 | NavigationType::Type NavigationController::ClassifyNavigation( |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 627 | const ViewHostMsg_FrameNavigate_Params& params) const { |
| 628 | // If a page makes a popup navigated to about blank, and then writes stuff |
| 629 | // like a subframe navigated to a real site, we'll get a notification with an |
| 630 | // invalid page ID. There's nothing we can do with these, so just ignore them. |
| 631 | if (params.page_id == -1) { |
| 632 | DCHECK(!GetActiveEntry()) << "Got an invalid page ID but we seem to be " |
| 633 | " navigated to a valid page. This should be impossible."; |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 634 | return NavigationType::NAV_IGNORE; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | if (params.page_id > active_contents_->GetMaxPageID()) { |
| 638 | // Greater page IDs than we've ever seen before are new pages. We may or may |
| 639 | // not have a pending entry for the page, and this may or may not be the |
| 640 | // main frame. |
| 641 | if (PageTransition::IsMainFrame(params.transition)) |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 642 | return NavigationType::NEW_PAGE; |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 643 | |
| 644 | // When this is a new subframe navigation, we should have a committed page |
| 645 | // for which it's a suframe in. This may not be the case when an iframe is |
| 646 | // navigated on a popup navigated to about:blank (the iframe would be |
| 647 | // written into the popup by script on the main page). For these cases, |
| 648 | // there isn't any navigation stuff we can do, so just ignore it. |
| 649 | if (!GetLastCommittedEntry()) |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 650 | return NavigationType::NAV_IGNORE; |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 651 | |
| 652 | // Valid subframe navigation. |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 653 | return NavigationType::NEW_SUBFRAME; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | // Now we know that the notification is for an existing page. Find that entry. |
| 657 | int existing_entry_index = GetEntryIndexWithPageID( |
| 658 | active_contents_->type(), |
| 659 | active_contents_->GetSiteInstance(), |
| 660 | params.page_id); |
| 661 | if (existing_entry_index == -1) { |
| 662 | // The page was not found. It could have been pruned because of the limit on |
| 663 | // back/forward entries (not likely since we'll usually tell it to navigate |
| 664 | // to such entries). It could also mean that the renderer is smoking crack. |
| 665 | NOTREACHED(); |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 666 | return NavigationType::NAV_IGNORE; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 667 | } |
| 668 | NavigationEntry* existing_entry = entries_[existing_entry_index].get(); |
| 669 | |
| 670 | if (pending_entry_ && |
| 671 | pending_entry_->url() == params.url && |
| 672 | existing_entry != pending_entry_ && |
| 673 | pending_entry_->page_id() == -1 && |
| 674 | pending_entry_->url() == existing_entry->url()) { |
| 675 | // In this case, we have a pending entry for a URL but WebCore didn't do a |
| 676 | // new navigation. This happens when you press enter in the URL bar to |
| 677 | // reload. We will create a pending entry, but WebKit will convert it to |
| 678 | // a reload since it's the same page and not create a new entry for it |
| 679 | // (the user doesn't want to have a new back/forward entry when they do |
| 680 | // this). In this case, we want to just ignore the pending entry and go |
| 681 | // back to where we were (the "existing entry"). |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 682 | return NavigationType::SAME_PAGE; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 683 | } |
| 684 | |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 685 | if (!PageTransition::IsMainFrame(params.transition)) { |
| 686 | // All manual subframes would get new IDs and were handled above, so we |
| 687 | // know this is auto. Since the current page was found in the navigation |
| 688 | // entry list, we're guaranteed to have a last committed entry. |
| 689 | DCHECK(GetLastCommittedEntry()); |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 690 | return NavigationType::AUTO_SUBFRAME; |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 691 | } |
| 692 | |
[email protected] | fc60f22 | 2008-12-18 17:36:54 | [diff] [blame] | 693 | // Any toplevel navigations with the same base (minus the reference fragment) |
| 694 | // are in-page navigations. We weeded out subframe navigations above. Most of |
| 695 | // the time this doesn't matter since WebKit doesn't tell us about subframe |
| 696 | // navigations that don't actually navigate, but it can happen when there is |
| 697 | // an encoding override (it always sends a navigation request). |
| 698 | if (AreURLsInPageNavigation(existing_entry->url(), params.url)) |
| 699 | return NavigationType::IN_PAGE; |
| 700 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 701 | // Since we weeded out "new" navigations above, we know this is an existing |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 702 | // (back/forward) navigation. |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 703 | return NavigationType::EXISTING_PAGE; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | void NavigationController::RendererDidNavigateToNewPage( |
| 707 | const ViewHostMsg_FrameNavigate_Params& params) { |
| 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(NavigationEntry::NORMAL_PAGE); |
| 721 | } else { |
| 722 | new_entry = new NavigationEntry(active_contents_->type()); |
| 723 | } |
| 724 | |
| 725 | new_entry->set_url(params.url); |
[email protected] | 740fbda | 2009-02-18 21:38:08 | [diff] [blame] | 726 | new_entry->set_referrer(params.referrer); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 727 | new_entry->set_page_id(params.page_id); |
| 728 | new_entry->set_transition_type(params.transition); |
| 729 | new_entry->set_site_instance(active_contents_->GetSiteInstance()); |
| 730 | new_entry->set_has_post_data(params.is_post); |
| 731 | |
| 732 | InsertEntry(new_entry); |
| 733 | } |
| 734 | |
| 735 | void NavigationController::RendererDidNavigateToExistingPage( |
| 736 | const ViewHostMsg_FrameNavigate_Params& params) { |
| 737 | // We should only get here for main frame navigations. |
| 738 | DCHECK(PageTransition::IsMainFrame(params.transition)); |
| 739 | |
| 740 | // This is a back/forward navigation. The existing page for the ID is |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 741 | // guaranteed to exist by ClassifyNavigation, and we just need to update it |
| 742 | // with new information from the renderer. |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 743 | int entry_index = GetEntryIndexWithPageID( |
| 744 | active_contents_->type(), |
| 745 | active_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 | DCHECK(entry->site_instance() == NULL || |
| 756 | entry->site_instance() == active_contents_->GetSiteInstance()); |
| 757 | entry->set_site_instance(active_contents_->GetSiteInstance()); |
| 758 | |
| 759 | // The entry we found in the list might be pending if the user hit |
| 760 | // back/forward/reload. This load should commit it (since it's already in the |
| 761 | // list, we can just discard the pending pointer). |
| 762 | // |
| 763 | // Note that we need to use the "internal" version since we don't want to |
| 764 | // actually change any other state, just kill the pointer. |
| 765 | if (entry == pending_entry_) |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 766 | DiscardNonCommittedEntriesInternal(); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 767 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 768 | last_committed_entry_index_ = entry_index; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | void NavigationController::RendererDidNavigateToSamePage( |
| 772 | const ViewHostMsg_FrameNavigate_Params& params) { |
| 773 | // 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] | 774 | // entry for this page ID. This entry is guaranteed to exist by |
| 775 | // ClassifyNavigation. All we need to do is update the existing entry. |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 776 | NavigationEntry* existing_entry = GetEntryWithPageID( |
| 777 | active_contents_->type(), |
| 778 | active_contents_->GetSiteInstance(), |
| 779 | params.page_id); |
| 780 | |
| 781 | // We assign the entry's unique ID to be that of the new one. Since this is |
| 782 | // always the result of a user action, we want to dismiss infobars, etc. like |
| 783 | // a regular user-initiated navigation. |
| 784 | existing_entry->set_unique_id(pending_entry_->unique_id()); |
| 785 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 786 | DiscardNonCommittedEntries(); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | void NavigationController::RendererDidNavigateInPage( |
| 790 | const ViewHostMsg_FrameNavigate_Params& params) { |
| 791 | DCHECK(PageTransition::IsMainFrame(params.transition)) << |
| 792 | "WebKit should only tell us about in-page navs for the main frame."; |
| 793 | // We're guaranteed to have an entry for this one. |
| 794 | NavigationEntry* existing_entry = GetEntryWithPageID( |
| 795 | active_contents_->type(), |
| 796 | active_contents_->GetSiteInstance(), |
| 797 | params.page_id); |
| 798 | |
| 799 | // Reference fragment navigation. We're guaranteed to have the last_committed |
| 800 | // entry and it will be the same page as the new navigation (minus the |
| 801 | // reference fragments, of course). |
| 802 | NavigationEntry* new_entry = new NavigationEntry(*existing_entry); |
| 803 | new_entry->set_page_id(params.page_id); |
| 804 | new_entry->set_url(params.url); |
| 805 | InsertEntry(new_entry); |
| 806 | } |
| 807 | |
| 808 | void NavigationController::RendererDidNavigateNewSubframe( |
| 809 | const ViewHostMsg_FrameNavigate_Params& params) { |
| 810 | // Manual subframe navigations just get the current entry cloned so the user |
| 811 | // can go back or forward to it. The actual subframe information will be |
| 812 | // stored in the page state for each of those entries. This happens out of |
| 813 | // band with the actual navigations. |
[email protected] | 4c27ba8 | 2008-09-24 16:49:09 | [diff] [blame] | 814 | DCHECK(GetLastCommittedEntry()) << "ClassifyNavigation should guarantee " |
| 815 | << "that a last committed entry exists."; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 816 | NavigationEntry* new_entry = new NavigationEntry(*GetLastCommittedEntry()); |
| 817 | new_entry->set_page_id(params.page_id); |
| 818 | InsertEntry(new_entry); |
| 819 | } |
| 820 | |
| 821 | bool NavigationController::RendererDidNavigateAutoSubframe( |
| 822 | const ViewHostMsg_FrameNavigate_Params& params) { |
| 823 | // We're guaranteed to have a previously committed entry, and we now need to |
| 824 | // handle navigation inside of a subframe in it without creating a new entry. |
| 825 | DCHECK(GetLastCommittedEntry()); |
| 826 | |
| 827 | // Handle the case where we're navigating back/forward to a previous subframe |
| 828 | // navigation entry. This is case "2." in NAV_AUTO_SUBFRAME comment in the |
| 829 | // header file. In case "1." this will be a NOP. |
| 830 | int entry_index = GetEntryIndexWithPageID( |
| 831 | active_contents_->type(), |
| 832 | active_contents_->GetSiteInstance(), |
| 833 | params.page_id); |
| 834 | if (entry_index < 0 || |
| 835 | entry_index >= static_cast<int>(entries_.size())) { |
| 836 | NOTREACHED(); |
| 837 | return false; |
| 838 | } |
| 839 | |
| 840 | // Update the current navigation entry in case we're going back/forward. |
| 841 | if (entry_index != last_committed_entry_index_) { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 842 | last_committed_entry_index_ = entry_index; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 843 | return true; |
| 844 | } |
| 845 | return false; |
| 846 | } |
| 847 | |
| 848 | void NavigationController::CommitPendingEntry() { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 849 | DiscardTransientEntry(); |
| 850 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 851 | if (!GetPendingEntry()) |
| 852 | return; // Nothing to do. |
| 853 | |
| 854 | // Need to save the previous URL for the notification. |
| 855 | LoadCommittedDetails details; |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 856 | if (GetLastCommittedEntry()) { |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 857 | details.previous_url = GetLastCommittedEntry()->url(); |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 858 | details.previous_entry_index = GetLastCommittedEntryIndex(); |
| 859 | } else { |
| 860 | details.previous_entry_index = -1; |
| 861 | } |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 862 | |
| 863 | if (pending_entry_index_ >= 0) { |
| 864 | // This is a previous navigation (back/forward) that we're just now |
| 865 | // committing. Just mark it as committed. |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 866 | details.type = NavigationType::EXISTING_PAGE; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 867 | int new_entry_index = pending_entry_index_; |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 868 | DiscardNonCommittedEntriesInternal(); |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 869 | |
| 870 | // Mark that entry as committed. |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 871 | last_committed_entry_index_ = new_entry_index; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 872 | } else { |
| 873 | // This is a new navigation. It's easiest to just copy the entry and insert |
| 874 | // it new again, since InsertEntry expects to take ownership and also |
| 875 | // discard the pending entry. We also need to synthesize a page ID. We can |
| 876 | // only do this because this function will only be called by our custom |
| 877 | // TabContents types. For WebContents, the IDs are generated by the |
| 878 | // renderer, so we can't do this. |
[email protected] | 0e8db94 | 2008-09-24 21:21:48 | [diff] [blame] | 879 | details.type = NavigationType::NEW_PAGE; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 880 | pending_entry_->set_page_id(active_contents_->GetMaxPageID() + 1); |
| 881 | active_contents_->UpdateMaxPageID(pending_entry_->page_id()); |
| 882 | InsertEntry(new NavigationEntry(*pending_entry_)); |
| 883 | } |
| 884 | |
| 885 | // Broadcast the notification of the navigation. |
| 886 | details.entry = GetActiveEntry(); |
| 887 | details.is_auto = false; |
| 888 | details.is_in_page = AreURLsInPageNavigation(details.previous_url, |
| 889 | details.entry->url()); |
| 890 | details.is_main_frame = true; |
| 891 | NotifyNavigationEntryCommitted(&details); |
| 892 | } |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 893 | |
| 894 | int NavigationController::GetIndexOfEntry( |
| 895 | const NavigationEntry* entry) const { |
| 896 | const NavigationEntries::const_iterator i(std::find( |
| 897 | entries_.begin(), |
| 898 | entries_.end(), |
| 899 | entry)); |
| 900 | return (i == entries_.end()) ? -1 : static_cast<int>(i - entries_.begin()); |
| 901 | } |
| 902 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 903 | bool NavigationController::IsURLInPageNavigation(const GURL& url) const { |
| 904 | NavigationEntry* last_committed = GetLastCommittedEntry(); |
| 905 | if (!last_committed) |
| 906 | return false; |
| 907 | return AreURLsInPageNavigation(last_committed->url(), url); |
| 908 | } |
| 909 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 910 | void NavigationController::DiscardNonCommittedEntries() { |
| 911 | bool transient = transient_entry_index_ != -1; |
| 912 | DiscardNonCommittedEntriesInternal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 913 | |
| 914 | // Synchronize the active_contents_ to the last committed entry. |
| 915 | NavigationEntry* last_entry = GetLastCommittedEntry(); |
[email protected] | 1e5645ff | 2008-08-27 18:09:07 | [diff] [blame] | 916 | if (last_entry && last_entry->tab_type() != active_contents_->type()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 917 | TabContents* from_contents = active_contents_; |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 918 | from_contents->set_is_active(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 919 | |
| 920 | // Switch back to the previous tab contents. |
[email protected] | 1e5645ff | 2008-08-27 18:09:07 | [diff] [blame] | 921 | active_contents_ = GetTabContents(last_entry->tab_type()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 922 | DCHECK(active_contents_); |
| 923 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 924 | active_contents_->set_is_active(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 925 | |
| 926 | // If we are transitioning from two types of WebContents, we need to migrate |
| 927 | // the download shelf if it is visible. The download shelf may have been |
| 928 | // created before the error that caused us to discard the entry. |
[email protected] | faa138b | 2009-02-05 18:33:15 | [diff] [blame] | 929 | TabContents::MigrateShelfView(from_contents, active_contents_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 930 | |
| 931 | if (from_contents->delegate()) { |
| 932 | from_contents->delegate()->ReplaceContents(from_contents, |
| 933 | active_contents_); |
| 934 | } |
| 935 | |
| 936 | // The entry we just discarded needed a different TabContents type. We no |
| 937 | // longer need it but we can't destroy it just yet because the TabContents |
| 938 | // is very likely involved in the current stack. |
| 939 | DCHECK(from_contents != active_contents_); |
| 940 | ScheduleTabContentsCollection(from_contents->type()); |
| 941 | } |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 942 | |
| 943 | // If there was a transient entry, invalidate everything so the new active |
| 944 | // entry state is shown. |
| 945 | if (transient) { |
| 946 | active_contents_->NotifyNavigationStateChanged( |
| 947 | TabContents::INVALIDATE_EVERYTHING); |
| 948 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | void NavigationController::InsertEntry(NavigationEntry* entry) { |
[email protected] | 1e5645ff | 2008-08-27 18:09:07 | [diff] [blame] | 952 | DCHECK(entry->transition_type() != PageTransition::AUTO_SUBFRAME); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 953 | |
| 954 | // Copy the pending entry's unique ID to the committed entry. |
| 955 | // I don't know if pending_entry_index_ can be other than -1 here. |
| 956 | const NavigationEntry* const pending_entry = (pending_entry_index_ == -1) ? |
| 957 | pending_entry_ : entries_[pending_entry_index_].get(); |
| 958 | if (pending_entry) |
| 959 | entry->set_unique_id(pending_entry->unique_id()); |
| 960 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 961 | DiscardNonCommittedEntriesInternal(); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 962 | |
| 963 | int current_size = static_cast<int>(entries_.size()); |
| 964 | |
| 965 | // Prune any entries which are in front of the current entry. |
| 966 | if (current_size > 0) { |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 967 | int num_pruned = 0; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 968 | while (last_committed_entry_index_ < (current_size - 1)) { |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 969 | num_pruned++; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 970 | entries_.pop_back(); |
| 971 | current_size--; |
| 972 | } |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 973 | if (num_pruned > 0) // Only notify if we did prune something. |
| 974 | NotifyPrunedEntries(this, false, num_pruned); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 975 | } |
| 976 | |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 977 | if (entries_.size() >= max_entry_count_) { |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 978 | RemoveEntryAtIndex(0, GURL()); |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 979 | NotifyPrunedEntries(this, true, 1); |
| 980 | } |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 981 | |
| 982 | entries_.push_back(linked_ptr<NavigationEntry>(entry)); |
| 983 | last_committed_entry_index_ = static_cast<int>(entries_.size()) - 1; |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 984 | |
| 985 | // This is a new page ID, so we need everybody to know about it. |
| 986 | active_contents_->UpdateMaxPageID(entry->page_id()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | void NavigationController::SetWindowID(const SessionID& id) { |
| 990 | window_id_ = id; |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 991 | NotificationService::current()->Notify(NotificationType::TAB_PARENTED, |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 992 | Source<NavigationController>(this), |
| 993 | NotificationService::NoDetails()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | void NavigationController::NavigateToPendingEntry(bool reload) { |
| 997 | TabContents* from_contents = active_contents_; |
| 998 | |
| 999 | // For session history navigations only the pending_entry_index_ is set. |
| 1000 | if (!pending_entry_) { |
| 1001 | DCHECK(pending_entry_index_ != -1); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1002 | pending_entry_ = entries_[pending_entry_index_].get(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | // Reset the security states as any SSL error may have been resolved since we |
| 1006 | // last visited that page. |
[email protected] | eb34392b | 2008-08-19 15:42:20 | [diff] [blame] | 1007 | pending_entry_->ssl() = NavigationEntry::SSLStatus(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1008 | |
[email protected] | 1e5645ff | 2008-08-27 18:09:07 | [diff] [blame] | 1009 | if (from_contents && from_contents->type() != pending_entry_->tab_type()) |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 1010 | from_contents->set_is_active(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1011 | |
[email protected] | ec322ff | 2008-11-19 22:53:30 | [diff] [blame] | 1012 | TabContents* contents = GetTabContentsCreateIfNecessary(*pending_entry_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1013 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 1014 | contents->set_is_active(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1015 | active_contents_ = contents; |
| 1016 | |
[email protected] | 58567187 | 2009-01-21 01:08:44 | [diff] [blame] | 1017 | if (from_contents && from_contents != contents && from_contents->delegate()) |
| 1018 | from_contents->delegate()->ReplaceContents(from_contents, contents); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1019 | |
[email protected] | e9ba447 | 2008-09-14 15:42:43 | [diff] [blame] | 1020 | NavigationEntry temp_entry(*pending_entry_); |
| 1021 | if (!contents->NavigateToPendingEntry(reload)) |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 1022 | DiscardNonCommittedEntries(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1023 | } |
| 1024 | |
[email protected] | ecd9d870 | 2008-08-28 22:10:17 | [diff] [blame] | 1025 | void NavigationController::NotifyNavigationEntryCommitted( |
| 1026 | LoadCommittedDetails* details) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1027 | // TODO(pkasting): http://b/1113079 Probably these explicit notification paths |
| 1028 | // should be removed, and interested parties should just listen for the |
| 1029 | // notification below instead. |
| 1030 | ssl_manager_.NavigationStateChanged(); |
| 1031 | active_contents_->NotifyNavigationStateChanged( |
| 1032 | TabContents::INVALIDATE_EVERYTHING); |
| 1033 | |
[email protected] | ecd9d870 | 2008-08-28 22:10:17 | [diff] [blame] | 1034 | details->entry = GetActiveEntry(); |
| 1035 | NotificationService::current()->Notify( |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 1036 | NotificationType::NAV_ENTRY_COMMITTED, |
[email protected] | ecd9d870 | 2008-08-28 22:10:17 | [diff] [blame] | 1037 | Source<NavigationController>(this), |
| 1038 | Details<LoadCommittedDetails>(details)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1039 | } |
| 1040 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1041 | TabContents* NavigationController::GetTabContentsCreateIfNecessary( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1042 | const NavigationEntry& entry) { |
[email protected] | 1e5645ff | 2008-08-27 18:09:07 | [diff] [blame] | 1043 | TabContents* contents = GetTabContents(entry.tab_type()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1044 | if (!contents) { |
[email protected] | ec322ff | 2008-11-19 22:53:30 | [diff] [blame] | 1045 | contents = TabContents::CreateWithType(entry.tab_type(), profile_, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1046 | entry.site_instance()); |
| 1047 | if (!contents->AsWebContents()) { |
| 1048 | // Update the max page id, otherwise the newly created TabContents may |
| 1049 | // have reset its max page id resulting in all new navigations. We only |
| 1050 | // do this for non-WebContents as WebContents takes care of this via its |
| 1051 | // SiteInstance. If this creation is the result of a restore, WebContents |
| 1052 | // handles invoking ReservePageIDRange to make sure the renderer's |
| 1053 | // max_page_id is updated to reflect the restored range of page ids. |
| 1054 | int32 max_page_id = contents->GetMaxPageID(); |
| 1055 | for (size_t i = 0; i < entries_.size(); ++i) { |
[email protected] | 1e5645ff | 2008-08-27 18:09:07 | [diff] [blame] | 1056 | if (entries_[i]->tab_type() == entry.tab_type()) |
| 1057 | max_page_id = std::max(max_page_id, entries_[i]->page_id()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1058 | } |
| 1059 | contents->UpdateMaxPageID(max_page_id); |
| 1060 | } |
| 1061 | RegisterTabContents(contents); |
| 1062 | } |
| 1063 | |
| 1064 | // We should not be trying to collect this tab contents. |
| 1065 | DCHECK(tab_contents_collector_map_.find(contents->type()) == |
| 1066 | tab_contents_collector_map_.end()); |
| 1067 | |
| 1068 | return contents; |
| 1069 | } |
| 1070 | |
| 1071 | void NavigationController::RegisterTabContents(TabContents* some_contents) { |
| 1072 | DCHECK(some_contents); |
| 1073 | TabContentsType t = some_contents->type(); |
| 1074 | TabContents* tc; |
| 1075 | if ((tc = tab_contents_map_[t]) != some_contents) { |
| 1076 | if (tc) { |
| 1077 | NOTREACHED() << "Should not happen. Multiple contents for one type"; |
| 1078 | } else { |
| 1079 | tab_contents_map_[t] = some_contents; |
| 1080 | some_contents->set_controller(this); |
| 1081 | } |
| 1082 | } |
| 1083 | if (some_contents->AsDOMUIHost()) |
| 1084 | some_contents->AsDOMUIHost()->AttachMessageHandlers(); |
| 1085 | } |
| 1086 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1087 | // static |
| 1088 | void NavigationController::DisablePromptOnRepost() { |
| 1089 | check_for_repost_ = false; |
| 1090 | } |
| 1091 | |
| 1092 | void NavigationController::SetActive(bool is_active) { |
| 1093 | if (is_active) { |
| 1094 | if (needs_reload_) { |
| 1095 | LoadIfNecessary(); |
| 1096 | } else if (load_pending_entry_when_active_) { |
| 1097 | NavigateToPendingEntry(false); |
| 1098 | load_pending_entry_when_active_ = false; |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | void NavigationController::LoadIfNecessary() { |
| 1104 | if (!needs_reload_) |
| 1105 | return; |
| 1106 | |
| 1107 | needs_reload_ = false; |
| 1108 | // Calling Reload() results in ignoring state, and not loading. |
| 1109 | // Explicitly use NavigateToPendingEntry so that the renderer uses the |
| 1110 | // cached state. |
| 1111 | pending_entry_index_ = last_committed_entry_index_; |
| 1112 | NavigateToPendingEntry(false); |
| 1113 | } |
| 1114 | |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1115 | void NavigationController::NotifyEntryChanged(const NavigationEntry* entry, |
| 1116 | int index) { |
| 1117 | EntryChangedDetails det; |
| 1118 | det.changed_entry = entry; |
| 1119 | det.index = index; |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 1120 | NotificationService::current()->Notify(NotificationType::NAV_ENTRY_CHANGED, |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1121 | Source<NavigationController>(this), |
| 1122 | Details<EntryChangedDetails>(&det)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1123 | } |
| 1124 | |
[email protected] | ec322ff | 2008-11-19 22:53:30 | [diff] [blame] | 1125 | NavigationController* NavigationController::Clone() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1126 | NavigationController* nc = new NavigationController(NULL, profile_); |
| 1127 | |
| 1128 | if (GetEntryCount() == 0) |
| 1129 | return nc; |
| 1130 | |
| 1131 | nc->needs_reload_ = true; |
| 1132 | |
| 1133 | nc->entries_.reserve(entries_.size()); |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1134 | for (int i = 0, c = GetEntryCount(); i < c; ++i) { |
| 1135 | nc->entries_.push_back(linked_ptr<NavigationEntry>( |
| 1136 | new NavigationEntry(*GetEntryAtIndex(i)))); |
| 1137 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1138 | |
[email protected] | ec322ff | 2008-11-19 22:53:30 | [diff] [blame] | 1139 | nc->FinishRestore(last_committed_entry_index_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1140 | |
| 1141 | return nc; |
| 1142 | } |
| 1143 | |
[email protected] | 50664fd | 2008-08-28 16:10:30 | [diff] [blame] | 1144 | void NavigationController::ScheduleTabContentsCollectionForInactiveTabs() { |
| 1145 | int index = GetCurrentEntryIndex(); |
| 1146 | if (index < 0 || GetPendingEntryIndex() != -1) |
| 1147 | return; |
| 1148 | |
| 1149 | TabContentsType active_type = GetEntryAtIndex(index)->tab_type(); |
| 1150 | for (TabContentsMap::iterator i = tab_contents_map_.begin(); |
| 1151 | i != tab_contents_map_.end(); ++i) { |
| 1152 | if (i->first != active_type) |
| 1153 | ScheduleTabContentsCollection(i->first); |
| 1154 | } |
| 1155 | } |
| 1156 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1157 | void NavigationController::ScheduleTabContentsCollection(TabContentsType t) { |
| 1158 | TabContentsCollectorMap::const_iterator i = |
| 1159 | tab_contents_collector_map_.find(t); |
| 1160 | |
| 1161 | // The tab contents is already scheduled for collection. |
| 1162 | if (i != tab_contents_collector_map_.end()) |
| 1163 | return; |
| 1164 | |
| 1165 | // If we currently don't have a TabContents for t, skip. |
| 1166 | if (tab_contents_map_.find(t) == tab_contents_map_.end()) |
| 1167 | return; |
| 1168 | |
| 1169 | // Create a collector and schedule it. |
| 1170 | TabContentsCollector* tcc = new TabContentsCollector(this, t); |
| 1171 | tab_contents_collector_map_[t] = tcc; |
| 1172 | MessageLoop::current()->PostTask(FROM_HERE, tcc); |
| 1173 | } |
| 1174 | |
| 1175 | void NavigationController::CancelTabContentsCollection(TabContentsType t) { |
| 1176 | TabContentsCollectorMap::iterator i = tab_contents_collector_map_.find(t); |
| 1177 | |
| 1178 | if (i != tab_contents_collector_map_.end()) { |
| 1179 | DCHECK(i->second); |
| 1180 | i->second->Cancel(); |
| 1181 | tab_contents_collector_map_.erase(i); |
| 1182 | } |
| 1183 | } |
| 1184 | |
[email protected] | ec322ff | 2008-11-19 22:53:30 | [diff] [blame] | 1185 | void NavigationController::FinishRestore(int selected_index) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1186 | DCHECK(selected_index >= 0 && selected_index < GetEntryCount()); |
| 1187 | ConfigureEntriesForRestore(&entries_); |
| 1188 | |
| 1189 | set_max_restored_page_id(GetEntryCount()); |
| 1190 | |
| 1191 | last_committed_entry_index_ = selected_index; |
| 1192 | |
| 1193 | // Callers assume we have an active_contents after restoring, so set it now. |
[email protected] | ec322ff | 2008-11-19 22:53:30 | [diff] [blame] | 1194 | active_contents_ = GetTabContentsCreateIfNecessary(*entries_[selected_index]); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1195 | } |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1196 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 1197 | void NavigationController::DiscardNonCommittedEntriesInternal() { |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1198 | if (pending_entry_index_ == -1) |
| 1199 | delete pending_entry_; |
| 1200 | pending_entry_ = NULL; |
| 1201 | pending_entry_index_ = -1; |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 1202 | |
| 1203 | DiscardTransientEntry(); |
| 1204 | } |
| 1205 | |
| 1206 | void NavigationController::DiscardTransientEntry() { |
| 1207 | if (transient_entry_index_ == -1) |
| 1208 | return; |
| 1209 | entries_.erase(entries_.begin() + transient_entry_index_ ); |
| 1210 | transient_entry_index_ = -1; |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | int NavigationController::GetEntryIndexWithPageID( |
| 1214 | TabContentsType type, SiteInstance* instance, int32 page_id) const { |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1215 | for (int i = static_cast<int>(entries_.size()) - 1; i >= 0; --i) { |
[email protected] | 1e5645ff | 2008-08-27 18:09:07 | [diff] [blame] | 1216 | if ((entries_[i]->tab_type() == type) && |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1217 | (entries_[i]->site_instance() == instance) && |
[email protected] | 1e5645ff | 2008-08-27 18:09:07 | [diff] [blame] | 1218 | (entries_[i]->page_id() == page_id)) |
[email protected] | 765b3550 | 2008-08-21 00:51:20 | [diff] [blame] | 1219 | return i; |
| 1220 | } |
| 1221 | return -1; |
| 1222 | } |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 1223 | |
| 1224 | NavigationEntry* NavigationController::GetTransientEntry() const { |
| 1225 | if (transient_entry_index_ == -1) |
| 1226 | return NULL; |
| 1227 | return entries_[transient_entry_index_].get(); |
| 1228 | } |