| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/sessions/core/tab_restore_service_helper.h" | 5 #include "components/sessions/core/tab_restore_service_helper.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <iterator> | 10 #include <iterator> |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 observer_->OnClearEntries(); | 128 observer_->OnClearEntries(); |
| 129 STLDeleteElements(&entries_); | 129 STLDeleteElements(&entries_); |
| 130 NotifyTabsChanged(); | 130 NotifyTabsChanged(); |
| 131 } | 131 } |
| 132 | 132 |
| 133 const TabRestoreService::Entries& TabRestoreServiceHelper::entries() const { | 133 const TabRestoreService::Entries& TabRestoreServiceHelper::entries() const { |
| 134 return entries_; | 134 return entries_; |
| 135 } | 135 } |
| 136 | 136 |
| 137 std::vector<LiveTab*> TabRestoreServiceHelper::RestoreMostRecentEntry( | 137 std::vector<LiveTab*> TabRestoreServiceHelper::RestoreMostRecentEntry( |
| 138 LiveTabContext* context, | 138 LiveTabContext* context) { |
| 139 int host_desktop_type) { | |
| 140 if (entries_.empty()) | 139 if (entries_.empty()) |
| 141 return std::vector<LiveTab*>(); | 140 return std::vector<LiveTab*>(); |
| 142 | 141 |
| 143 return RestoreEntryById(context, entries_.front()->id, host_desktop_type, | 142 return RestoreEntryById(context, entries_.front()->id, UNKNOWN); |
| 144 UNKNOWN); | |
| 145 } | 143 } |
| 146 | 144 |
| 147 TabRestoreService::Tab* TabRestoreServiceHelper::RemoveTabEntryById( | 145 TabRestoreService::Tab* TabRestoreServiceHelper::RemoveTabEntryById( |
| 148 SessionID::id_type id) { | 146 SessionID::id_type id) { |
| 149 Entries::iterator i = GetEntryIteratorById(id); | 147 Entries::iterator i = GetEntryIteratorById(id); |
| 150 if (i == entries_.end()) | 148 if (i == entries_.end()) |
| 151 return NULL; | 149 return NULL; |
| 152 | 150 |
| 153 Entry* entry = *i; | 151 Entry* entry = *i; |
| 154 if (entry->type != TabRestoreService::TAB) | 152 if (entry->type != TabRestoreService::TAB) |
| 155 return NULL; | 153 return NULL; |
| 156 | 154 |
| 157 Tab* tab = static_cast<Tab*>(entry); | 155 Tab* tab = static_cast<Tab*>(entry); |
| 158 entries_.erase(i); | 156 entries_.erase(i); |
| 159 return tab; | 157 return tab; |
| 160 } | 158 } |
| 161 | 159 |
| 162 std::vector<LiveTab*> TabRestoreServiceHelper::RestoreEntryById( | 160 std::vector<LiveTab*> TabRestoreServiceHelper::RestoreEntryById( |
| 163 LiveTabContext* context, | 161 LiveTabContext* context, |
| 164 SessionID::id_type id, | 162 SessionID::id_type id, |
| 165 int host_desktop_type, | |
| 166 WindowOpenDisposition disposition) { | 163 WindowOpenDisposition disposition) { |
| 167 Entries::iterator entry_iterator = GetEntryIteratorById(id); | 164 Entries::iterator entry_iterator = GetEntryIteratorById(id); |
| 168 if (entry_iterator == entries_.end()) | 165 if (entry_iterator == entries_.end()) |
| 169 // Don't hoark here, we allow an invalid id. | 166 // Don't hoark here, we allow an invalid id. |
| 170 return std::vector<LiveTab*>(); | 167 return std::vector<LiveTab*>(); |
| 171 | 168 |
| 172 if (observer_) | 169 if (observer_) |
| 173 observer_->OnRestoreEntryById(id, entry_iterator); | 170 observer_->OnRestoreEntryById(id, entry_iterator); |
| 174 restoring_ = true; | 171 restoring_ = true; |
| 175 Entry* entry = *entry_iterator; | 172 Entry* entry = *entry_iterator; |
| 176 | 173 |
| 177 // If the entry's ID does not match the ID that is being restored, then the | 174 // If the entry's ID does not match the ID that is being restored, then the |
| 178 // entry is a window from which a single tab will be restored. | 175 // entry is a window from which a single tab will be restored. |
| 179 bool restoring_tab_in_window = entry->id != id; | 176 bool restoring_tab_in_window = entry->id != id; |
| 180 | 177 |
| 181 if (!restoring_tab_in_window) { | 178 if (!restoring_tab_in_window) { |
| 182 entries_.erase(entry_iterator); | 179 entries_.erase(entry_iterator); |
| 183 entry_iterator = entries_.end(); | 180 entry_iterator = entries_.end(); |
| 184 } | 181 } |
| 185 | 182 |
| 186 // |context| will be NULL in cases where one isn't already available (eg, | 183 // |context| will be NULL in cases where one isn't already available (eg, |
| 187 // when invoked on Mac OS X with no windows open). In this case, create a | 184 // when invoked on Mac OS X with no windows open). In this case, create a |
| 188 // new browser into which we restore the tabs. | 185 // new browser into which we restore the tabs. |
| 189 std::vector<LiveTab*> live_tabs; | 186 std::vector<LiveTab*> live_tabs; |
| 190 if (entry->type == TabRestoreService::TAB) { | 187 if (entry->type == TabRestoreService::TAB) { |
| 191 Tab* tab = static_cast<Tab*>(entry); | 188 Tab* tab = static_cast<Tab*>(entry); |
| 192 LiveTab* restored_tab = NULL; | 189 LiveTab* restored_tab = NULL; |
| 193 context = RestoreTab(*tab, context, host_desktop_type, disposition, | 190 context = RestoreTab(*tab, context, disposition, &restored_tab); |
| 194 &restored_tab); | |
| 195 live_tabs.push_back(restored_tab); | 191 live_tabs.push_back(restored_tab); |
| 196 context->ShowBrowserWindow(); | 192 context->ShowBrowserWindow(); |
| 197 } else if (entry->type == TabRestoreService::WINDOW) { | 193 } else if (entry->type == TabRestoreService::WINDOW) { |
| 198 LiveTabContext* current_context = context; | 194 LiveTabContext* current_context = context; |
| 199 Window* window = static_cast<Window*>(entry); | 195 Window* window = static_cast<Window*>(entry); |
| 200 | 196 |
| 201 // When restoring a window, either the entire window can be restored, or a | 197 // When restoring a window, either the entire window can be restored, or a |
| 202 // single tab within it. If the entry's ID matches the one to restore, then | 198 // single tab within it. If the entry's ID matches the one to restore, then |
| 203 // the entire window will be restored. | 199 // the entire window will be restored. |
| 204 if (!restoring_tab_in_window) { | 200 if (!restoring_tab_in_window) { |
| 205 context = | 201 context = client_->CreateLiveTabContext(window->app_name); |
| 206 client_->CreateLiveTabContext(host_desktop_type, window->app_name); | |
| 207 for (size_t tab_i = 0; tab_i < window->tabs.size(); ++tab_i) { | 202 for (size_t tab_i = 0; tab_i < window->tabs.size(); ++tab_i) { |
| 208 const Tab& tab = window->tabs[tab_i]; | 203 const Tab& tab = window->tabs[tab_i]; |
| 209 LiveTab* restored_tab = context->AddRestoredTab( | 204 LiveTab* restored_tab = context->AddRestoredTab( |
| 210 tab.navigations, context->GetTabCount(), | 205 tab.navigations, context->GetTabCount(), |
| 211 tab.current_navigation_index, tab.extension_app_id, | 206 tab.current_navigation_index, tab.extension_app_id, |
| 212 static_cast<int>(tab_i) == window->selected_tab_index, tab.pinned, | 207 static_cast<int>(tab_i) == window->selected_tab_index, tab.pinned, |
| 213 tab.from_last_session, tab.platform_data.get(), | 208 tab.from_last_session, tab.platform_data.get(), |
| 214 tab.user_agent_override); | 209 tab.user_agent_override); |
| 215 if (restored_tab) { | 210 if (restored_tab) { |
| 216 restored_tab->LoadIfNecessary(); | 211 restored_tab->LoadIfNecessary(); |
| 217 client_->OnTabRestored( | 212 client_->OnTabRestored( |
| 218 tab.navigations.at(tab.current_navigation_index).virtual_url()); | 213 tab.navigations.at(tab.current_navigation_index).virtual_url()); |
| 219 live_tabs.push_back(restored_tab); | 214 live_tabs.push_back(restored_tab); |
| 220 } | 215 } |
| 221 } | 216 } |
| 222 // All the window's tabs had the same former browser_id. | 217 // All the window's tabs had the same former browser_id. |
| 223 if (window->tabs[0].has_browser()) { | 218 if (window->tabs[0].has_browser()) { |
| 224 UpdateTabBrowserIDs(window->tabs[0].browser_id, | 219 UpdateTabBrowserIDs(window->tabs[0].browser_id, |
| 225 context->GetSessionID().id()); | 220 context->GetSessionID().id()); |
| 226 } | 221 } |
| 227 } else { | 222 } else { |
| 228 // Restore a single tab from the window. Find the tab that matches the ID | 223 // Restore a single tab from the window. Find the tab that matches the ID |
| 229 // in the window and restore it. | 224 // in the window and restore it. |
| 230 for (std::vector<Tab>::iterator tab_i = window->tabs.begin(); | 225 for (std::vector<Tab>::iterator tab_i = window->tabs.begin(); |
| 231 tab_i != window->tabs.end(); ++tab_i) { | 226 tab_i != window->tabs.end(); ++tab_i) { |
| 232 const Tab& tab = *tab_i; | 227 const Tab& tab = *tab_i; |
| 233 if (tab.id == id) { | 228 if (tab.id == id) { |
| 234 LiveTab* restored_tab = NULL; | 229 LiveTab* restored_tab = NULL; |
| 235 context = RestoreTab(tab, context, host_desktop_type, disposition, | 230 context = RestoreTab(tab, context, disposition, &restored_tab); |
| 236 &restored_tab); | |
| 237 live_tabs.push_back(restored_tab); | 231 live_tabs.push_back(restored_tab); |
| 238 window->tabs.erase(tab_i); | 232 window->tabs.erase(tab_i); |
| 239 // If restoring the tab leaves the window with nothing else, delete it | 233 // If restoring the tab leaves the window with nothing else, delete it |
| 240 // as well. | 234 // as well. |
| 241 if (!window->tabs.size()) { | 235 if (!window->tabs.size()) { |
| 242 entries_.erase(entry_iterator); | 236 entries_.erase(entry_iterator); |
| 243 delete entry; | 237 delete entry; |
| 244 } else { | 238 } else { |
| 245 // Update the browser ID of the rest of the tabs in the window so if | 239 // Update the browser ID of the rest of the tabs in the window so if |
| 246 // any one is restored, it goes into the same window as the tab | 240 // any one is restored, it goes into the same window as the tab |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 // Delegate may be NULL during unit tests. | 382 // Delegate may be NULL during unit tests. |
| 389 if (context) { | 383 if (context) { |
| 390 tab->browser_id = context->GetSessionID().id(); | 384 tab->browser_id = context->GetSessionID().id(); |
| 391 tab->pinned = context->IsTabPinned(tab->tabstrip_index); | 385 tab->pinned = context->IsTabPinned(tab->tabstrip_index); |
| 392 } | 386 } |
| 393 } | 387 } |
| 394 | 388 |
| 395 LiveTabContext* TabRestoreServiceHelper::RestoreTab( | 389 LiveTabContext* TabRestoreServiceHelper::RestoreTab( |
| 396 const Tab& tab, | 390 const Tab& tab, |
| 397 LiveTabContext* context, | 391 LiveTabContext* context, |
| 398 int host_desktop_type, | |
| 399 WindowOpenDisposition disposition, | 392 WindowOpenDisposition disposition, |
| 400 LiveTab** live_tab) { | 393 LiveTab** live_tab) { |
| 401 LiveTab* restored_tab; | 394 LiveTab* restored_tab; |
| 402 if (disposition == CURRENT_TAB && context) { | 395 if (disposition == CURRENT_TAB && context) { |
| 403 restored_tab = context->ReplaceRestoredTab( | 396 restored_tab = context->ReplaceRestoredTab( |
| 404 tab.navigations, tab.current_navigation_index, tab.from_last_session, | 397 tab.navigations, tab.current_navigation_index, tab.from_last_session, |
| 405 tab.extension_app_id, tab.platform_data.get(), tab.user_agent_override); | 398 tab.extension_app_id, tab.platform_data.get(), tab.user_agent_override); |
| 406 } else { | 399 } else { |
| 407 // We only respsect the tab's original browser if there's no disposition. | 400 // We only respsect the tab's original browser if there's no disposition. |
| 408 if (disposition == UNKNOWN && tab.has_browser()) { | 401 if (disposition == UNKNOWN && tab.has_browser()) { |
| 409 context = | 402 context = client_->FindLiveTabContextWithID(tab.browser_id); |
| 410 client_->FindLiveTabContextWithID(tab.browser_id, host_desktop_type); | |
| 411 } | 403 } |
| 412 | 404 |
| 413 int tab_index = -1; | 405 int tab_index = -1; |
| 414 | 406 |
| 415 // |context| will be NULL in cases where one isn't already available (eg, | 407 // |context| will be NULL in cases where one isn't already available (eg, |
| 416 // when invoked on Mac OS X with no windows open). In this case, create a | 408 // when invoked on Mac OS X with no windows open). In this case, create a |
| 417 // new browser into which we restore the tabs. | 409 // new browser into which we restore the tabs. |
| 418 if (context && disposition != NEW_WINDOW) { | 410 if (context && disposition != NEW_WINDOW) { |
| 419 tab_index = tab.tabstrip_index; | 411 tab_index = tab.tabstrip_index; |
| 420 } else { | 412 } else { |
| 421 context = client_->CreateLiveTabContext(host_desktop_type, std::string()); | 413 context = client_->CreateLiveTabContext(std::string()); |
| 422 if (tab.has_browser()) | 414 if (tab.has_browser()) |
| 423 UpdateTabBrowserIDs(tab.browser_id, context->GetSessionID().id()); | 415 UpdateTabBrowserIDs(tab.browser_id, context->GetSessionID().id()); |
| 424 } | 416 } |
| 425 | 417 |
| 426 // Place the tab at the end if the tab index is no longer valid or | 418 // Place the tab at the end if the tab index is no longer valid or |
| 427 // we were passed a specific disposition. | 419 // we were passed a specific disposition. |
| 428 if (tab_index < 0 || tab_index > context->GetTabCount() || | 420 if (tab_index < 0 || tab_index > context->GetTabCount() || |
| 429 disposition != UNKNOWN) { | 421 disposition != UNKNOWN) { |
| 430 tab_index = context->GetTabCount(); | 422 tab_index = context->GetTabCount(); |
| 431 } | 423 } |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 tab->browser_id = new_id; | 519 tab->browser_id = new_id; |
| 528 } | 520 } |
| 529 } | 521 } |
| 530 } | 522 } |
| 531 | 523 |
| 532 base::Time TabRestoreServiceHelper::TimeNow() const { | 524 base::Time TabRestoreServiceHelper::TimeNow() const { |
| 533 return time_factory_ ? time_factory_->TimeNow() : base::Time::Now(); | 525 return time_factory_ ? time_factory_->TimeNow() : base::Time::Now(); |
| 534 } | 526 } |
| 535 | 527 |
| 536 } // namespace sessions | 528 } // namespace sessions |
| OLD | NEW |