| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "chrome/browser/sessions/tab_restore_service_helper.h" | 5 #include "chrome/browser/sessions/tab_restore_service_helper.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 if (observer_) | 163 if (observer_) |
| 164 observer_->OnClearEntries(); | 164 observer_->OnClearEntries(); |
| 165 STLDeleteElements(&entries_); | 165 STLDeleteElements(&entries_); |
| 166 NotifyTabsChanged(); | 166 NotifyTabsChanged(); |
| 167 } | 167 } |
| 168 | 168 |
| 169 const TabRestoreService::Entries& TabRestoreServiceHelper::entries() const { | 169 const TabRestoreService::Entries& TabRestoreServiceHelper::entries() const { |
| 170 return entries_; | 170 return entries_; |
| 171 } | 171 } |
| 172 | 172 |
| 173 void TabRestoreServiceHelper::RestoreMostRecentEntry( | 173 content::WebContents* TabRestoreServiceHelper::RestoreMostRecentEntry( |
| 174 TabRestoreServiceDelegate* delegate, | 174 TabRestoreServiceDelegate* delegate, |
| 175 chrome::HostDesktopType host_desktop_type) { | 175 chrome::HostDesktopType host_desktop_type) { |
| 176 if (entries_.empty()) | 176 if (entries_.empty()) |
| 177 return; | 177 return NULL; |
| 178 | 178 |
| 179 RestoreEntryById(delegate, entries_.front()->id, host_desktop_type, UNKNOWN); | 179 return RestoreEntryById(delegate, entries_.front()->id, host_desktop_type, |
| 180 UNKNOWN); |
| 180 } | 181 } |
| 181 | 182 |
| 182 TabRestoreService::Tab* TabRestoreServiceHelper::RemoveTabEntryById( | 183 TabRestoreService::Tab* TabRestoreServiceHelper::RemoveTabEntryById( |
| 183 SessionID::id_type id) { | 184 SessionID::id_type id) { |
| 184 Entries::iterator i = GetEntryIteratorById(id); | 185 Entries::iterator i = GetEntryIteratorById(id); |
| 185 if (i == entries_.end()) | 186 if (i == entries_.end()) |
| 186 return NULL; | 187 return NULL; |
| 187 | 188 |
| 188 Entry* entry = *i; | 189 Entry* entry = *i; |
| 189 if (entry->type != TabRestoreService::TAB) | 190 if (entry->type != TabRestoreService::TAB) |
| 190 return NULL; | 191 return NULL; |
| 191 | 192 |
| 192 Tab* tab = static_cast<Tab*>(entry); | 193 Tab* tab = static_cast<Tab*>(entry); |
| 193 entries_.erase(i); | 194 entries_.erase(i); |
| 194 return tab; | 195 return tab; |
| 195 } | 196 } |
| 196 | 197 |
| 197 void TabRestoreServiceHelper::RestoreEntryById( | 198 content::WebContents* TabRestoreServiceHelper::RestoreEntryById( |
| 198 TabRestoreServiceDelegate* delegate, | 199 TabRestoreServiceDelegate* delegate, |
| 199 SessionID::id_type id, | 200 SessionID::id_type id, |
| 200 chrome::HostDesktopType host_desktop_type, | 201 chrome::HostDesktopType host_desktop_type, |
| 201 WindowOpenDisposition disposition) { | 202 WindowOpenDisposition disposition) { |
| 202 Entries::iterator entry_iterator = GetEntryIteratorById(id); | 203 Entries::iterator entry_iterator = GetEntryIteratorById(id); |
| 203 if (entry_iterator == entries_.end()) | 204 if (entry_iterator == entries_.end()) |
| 204 // Don't hoark here, we allow an invalid id. | 205 // Don't hoark here, we allow an invalid id. |
| 205 return; | 206 return NULL; |
| 206 | 207 |
| 207 if (observer_) | 208 if (observer_) |
| 208 observer_->OnRestoreEntryById(id, entry_iterator); | 209 observer_->OnRestoreEntryById(id, entry_iterator); |
| 209 restoring_ = true; | 210 restoring_ = true; |
| 210 Entry* entry = *entry_iterator; | 211 Entry* entry = *entry_iterator; |
| 211 | 212 |
| 212 // If the entry's ID does not match the ID that is being restored, then the | 213 // If the entry's ID does not match the ID that is being restored, then the |
| 213 // entry is a window from which a single tab will be restored. | 214 // entry is a window from which a single tab will be restored. |
| 214 bool restoring_tab_in_window = entry->id != id; | 215 bool restoring_tab_in_window = entry->id != id; |
| 215 | 216 |
| 216 if (!restoring_tab_in_window) { | 217 if (!restoring_tab_in_window) { |
| 217 entries_.erase(entry_iterator); | 218 entries_.erase(entry_iterator); |
| 218 entry_iterator = entries_.end(); | 219 entry_iterator = entries_.end(); |
| 219 } | 220 } |
| 220 | 221 |
| 221 // |delegate| will be NULL in cases where one isn't already available (eg, | 222 // |delegate| will be NULL in cases where one isn't already available (eg, |
| 222 // when invoked on Mac OS X with no windows open). In this case, create a | 223 // when invoked on Mac OS X with no windows open). In this case, create a |
| 223 // new browser into which we restore the tabs. | 224 // new browser into which we restore the tabs. |
| 225 WebContents* web_contents = NULL; |
| 224 if (entry->type == TabRestoreService::TAB) { | 226 if (entry->type == TabRestoreService::TAB) { |
| 225 Tab* tab = static_cast<Tab*>(entry); | 227 Tab* tab = static_cast<Tab*>(entry); |
| 226 delegate = RestoreTab(*tab, delegate, host_desktop_type, disposition); | 228 delegate = RestoreTab(*tab, delegate, host_desktop_type, disposition, |
| 229 &web_contents); |
| 227 delegate->ShowBrowserWindow(); | 230 delegate->ShowBrowserWindow(); |
| 228 } else if (entry->type == TabRestoreService::WINDOW) { | 231 } else if (entry->type == TabRestoreService::WINDOW) { |
| 229 TabRestoreServiceDelegate* current_delegate = delegate; | 232 TabRestoreServiceDelegate* current_delegate = delegate; |
| 230 Window* window = static_cast<Window*>(entry); | 233 Window* window = static_cast<Window*>(entry); |
| 231 | 234 |
| 232 // When restoring a window, either the entire window can be restored, or a | 235 // When restoring a window, either the entire window can be restored, or a |
| 233 // single tab within it. If the entry's ID matches the one to restore, then | 236 // single tab within it. If the entry's ID matches the one to restore, then |
| 234 // the entire window will be restored. | 237 // the entire window will be restored. |
| 235 if (!restoring_tab_in_window) { | 238 if (!restoring_tab_in_window) { |
| 236 delegate = TabRestoreServiceDelegate::Create(profile_, host_desktop_type, | 239 delegate = TabRestoreServiceDelegate::Create(profile_, host_desktop_type, |
| 237 window->app_name); | 240 window->app_name); |
| 238 for (size_t tab_i = 0; tab_i < window->tabs.size(); ++tab_i) { | 241 for (size_t tab_i = 0; tab_i < window->tabs.size(); ++tab_i) { |
| 239 const Tab& tab = window->tabs[tab_i]; | 242 const Tab& tab = window->tabs[tab_i]; |
| 240 WebContents* restored_tab = delegate->AddRestoredTab( | 243 WebContents* restored_tab = delegate->AddRestoredTab( |
| 241 tab.navigations, | 244 tab.navigations, |
| 242 delegate->GetTabCount(), | 245 delegate->GetTabCount(), |
| 243 tab.current_navigation_index, | 246 tab.current_navigation_index, |
| 244 tab.extension_app_id, | 247 tab.extension_app_id, |
| 245 static_cast<int>(tab_i) == window->selected_tab_index, | 248 static_cast<int>(tab_i) == window->selected_tab_index, |
| 246 tab.pinned, | 249 tab.pinned, |
| 247 tab.from_last_session, | 250 tab.from_last_session, |
| 248 tab.session_storage_namespace.get(), | 251 tab.session_storage_namespace.get(), |
| 249 tab.user_agent_override); | 252 tab.user_agent_override); |
| 250 if (restored_tab) { | 253 if (restored_tab) { |
| 251 restored_tab->GetController().LoadIfNecessary(); | 254 restored_tab->GetController().LoadIfNecessary(); |
| 252 RecordAppLaunch(profile_, tab); | 255 RecordAppLaunch(profile_, tab); |
| 256 // Return the last successfully restored tab for window entries. |
| 257 web_contents = restored_tab; |
| 253 } | 258 } |
| 254 } | 259 } |
| 255 // All the window's tabs had the same former browser_id. | 260 // All the window's tabs had the same former browser_id. |
| 256 if (window->tabs[0].has_browser()) { | 261 if (window->tabs[0].has_browser()) { |
| 257 UpdateTabBrowserIDs(window->tabs[0].browser_id, | 262 UpdateTabBrowserIDs(window->tabs[0].browser_id, |
| 258 delegate->GetSessionID().id()); | 263 delegate->GetSessionID().id()); |
| 259 } | 264 } |
| 260 } else { | 265 } else { |
| 261 // Restore a single tab from the window. Find the tab that matches the ID | 266 // Restore a single tab from the window. Find the tab that matches the ID |
| 262 // in the window and restore it. | 267 // in the window and restore it. |
| 263 for (std::vector<Tab>::iterator tab_i = window->tabs.begin(); | 268 for (std::vector<Tab>::iterator tab_i = window->tabs.begin(); |
| 264 tab_i != window->tabs.end(); ++tab_i) { | 269 tab_i != window->tabs.end(); ++tab_i) { |
| 265 const Tab& tab = *tab_i; | 270 const Tab& tab = *tab_i; |
| 266 if (tab.id == id) { | 271 if (tab.id == id) { |
| 267 delegate = RestoreTab(tab, delegate, host_desktop_type, disposition); | 272 delegate = RestoreTab(tab, delegate, host_desktop_type, disposition, |
| 273 &web_contents); |
| 268 window->tabs.erase(tab_i); | 274 window->tabs.erase(tab_i); |
| 269 // If restoring the tab leaves the window with nothing else, delete it | 275 // If restoring the tab leaves the window with nothing else, delete it |
| 270 // as well. | 276 // as well. |
| 271 if (!window->tabs.size()) { | 277 if (!window->tabs.size()) { |
| 272 entries_.erase(entry_iterator); | 278 entries_.erase(entry_iterator); |
| 273 delete entry; | 279 delete entry; |
| 274 } else { | 280 } else { |
| 275 // Update the browser ID of the rest of the tabs in the window so if | 281 // Update the browser ID of the rest of the tabs in the window so if |
| 276 // any one is restored, it goes into the same window as the tab | 282 // any one is restored, it goes into the same window as the tab |
| 277 // being restored now. | 283 // being restored now. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 295 } else { | 301 } else { |
| 296 NOTREACHED(); | 302 NOTREACHED(); |
| 297 } | 303 } |
| 298 | 304 |
| 299 if (!restoring_tab_in_window) { | 305 if (!restoring_tab_in_window) { |
| 300 delete entry; | 306 delete entry; |
| 301 } | 307 } |
| 302 | 308 |
| 303 restoring_ = false; | 309 restoring_ = false; |
| 304 NotifyTabsChanged(); | 310 NotifyTabsChanged(); |
| 311 // Returns the WebContents of the restored tab if entry is a Tab. In the case |
| 312 // of restoring type Window, returns the WebContents of the last successfully |
| 313 // restored tab in the window. |
| 314 return web_contents; |
| 305 } | 315 } |
| 306 | 316 |
| 307 void TabRestoreServiceHelper::NotifyTabsChanged() { | 317 void TabRestoreServiceHelper::NotifyTabsChanged() { |
| 308 FOR_EACH_OBSERVER(TabRestoreServiceObserver, observer_list_, | 318 FOR_EACH_OBSERVER(TabRestoreServiceObserver, observer_list_, |
| 309 TabRestoreServiceChanged(tab_restore_service_)); | 319 TabRestoreServiceChanged(tab_restore_service_)); |
| 310 } | 320 } |
| 311 | 321 |
| 312 void TabRestoreServiceHelper::NotifyLoaded() { | 322 void TabRestoreServiceHelper::NotifyLoaded() { |
| 313 FOR_EACH_OBSERVER(TabRestoreServiceObserver, observer_list_, | 323 FOR_EACH_OBSERVER(TabRestoreServiceObserver, observer_list_, |
| 314 TabRestoreServiceLoaded(tab_restore_service_)); | 324 TabRestoreServiceLoaded(tab_restore_service_)); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 if (delegate) { | 440 if (delegate) { |
| 431 tab->browser_id = delegate->GetSessionID().id(); | 441 tab->browser_id = delegate->GetSessionID().id(); |
| 432 tab->pinned = delegate->IsTabPinned(tab->tabstrip_index); | 442 tab->pinned = delegate->IsTabPinned(tab->tabstrip_index); |
| 433 } | 443 } |
| 434 } | 444 } |
| 435 | 445 |
| 436 TabRestoreServiceDelegate* TabRestoreServiceHelper::RestoreTab( | 446 TabRestoreServiceDelegate* TabRestoreServiceHelper::RestoreTab( |
| 437 const Tab& tab, | 447 const Tab& tab, |
| 438 TabRestoreServiceDelegate* delegate, | 448 TabRestoreServiceDelegate* delegate, |
| 439 chrome::HostDesktopType host_desktop_type, | 449 chrome::HostDesktopType host_desktop_type, |
| 440 WindowOpenDisposition disposition) { | 450 WindowOpenDisposition disposition, |
| 451 WebContents** contents) { |
| 452 WebContents* web_contents; |
| 441 if (disposition == CURRENT_TAB && delegate) { | 453 if (disposition == CURRENT_TAB && delegate) { |
| 442 delegate->ReplaceRestoredTab(tab.navigations, | 454 web_contents = delegate->ReplaceRestoredTab( |
| 443 tab.current_navigation_index, | 455 tab.navigations, |
| 444 tab.from_last_session, | 456 tab.current_navigation_index, |
| 445 tab.extension_app_id, | 457 tab.from_last_session, |
| 446 tab.session_storage_namespace.get(), | 458 tab.extension_app_id, |
| 447 tab.user_agent_override); | 459 tab.session_storage_namespace.get(), |
| 460 tab.user_agent_override); |
| 448 } else { | 461 } else { |
| 449 // We only respsect the tab's original browser if there's no disposition. | 462 // We only respsect the tab's original browser if there's no disposition. |
| 450 if (disposition == UNKNOWN && tab.has_browser()) { | 463 if (disposition == UNKNOWN && tab.has_browser()) { |
| 451 delegate = TabRestoreServiceDelegate::FindDelegateWithID( | 464 delegate = TabRestoreServiceDelegate::FindDelegateWithID( |
| 452 tab.browser_id, host_desktop_type); | 465 tab.browser_id, host_desktop_type); |
| 453 } | 466 } |
| 454 | 467 |
| 455 int tab_index = -1; | 468 int tab_index = -1; |
| 456 | 469 |
| 457 // |delegate| will be NULL in cases where one isn't already available (eg, | 470 // |delegate| will be NULL in cases where one isn't already available (eg, |
| 458 // when invoked on Mac OS X with no windows open). In this case, create a | 471 // when invoked on Mac OS X with no windows open). In this case, create a |
| 459 // new browser into which we restore the tabs. | 472 // new browser into which we restore the tabs. |
| 460 if (delegate && disposition != NEW_WINDOW) { | 473 if (delegate && disposition != NEW_WINDOW) { |
| 461 tab_index = tab.tabstrip_index; | 474 tab_index = tab.tabstrip_index; |
| 462 } else { | 475 } else { |
| 463 delegate = TabRestoreServiceDelegate::Create(profile_, host_desktop_type, | 476 delegate = TabRestoreServiceDelegate::Create(profile_, host_desktop_type, |
| 464 std::string()); | 477 std::string()); |
| 465 if (tab.has_browser()) | 478 if (tab.has_browser()) |
| 466 UpdateTabBrowserIDs(tab.browser_id, delegate->GetSessionID().id()); | 479 UpdateTabBrowserIDs(tab.browser_id, delegate->GetSessionID().id()); |
| 467 } | 480 } |
| 468 | 481 |
| 469 // Place the tab at the end if the tab index is no longer valid or | 482 // Place the tab at the end if the tab index is no longer valid or |
| 470 // we were passed a specific disposition. | 483 // we were passed a specific disposition. |
| 471 if (tab_index < 0 || tab_index > delegate->GetTabCount() || | 484 if (tab_index < 0 || tab_index > delegate->GetTabCount() || |
| 472 disposition != UNKNOWN) { | 485 disposition != UNKNOWN) { |
| 473 tab_index = delegate->GetTabCount(); | 486 tab_index = delegate->GetTabCount(); |
| 474 } | 487 } |
| 475 | 488 |
| 476 WebContents* web_contents = | 489 web_contents = delegate->AddRestoredTab(tab.navigations, |
| 477 delegate->AddRestoredTab(tab.navigations, | 490 tab_index, |
| 478 tab_index, | 491 tab.current_navigation_index, |
| 479 tab.current_navigation_index, | 492 tab.extension_app_id, |
| 480 tab.extension_app_id, | 493 disposition != NEW_BACKGROUND_TAB, |
| 481 disposition != NEW_BACKGROUND_TAB, | 494 tab.pinned, |
| 482 tab.pinned, | 495 tab.from_last_session, |
| 483 tab.from_last_session, | 496 tab.session_storage_namespace.get(), |
| 484 tab.session_storage_namespace.get(), | 497 tab.user_agent_override); |
| 485 tab.user_agent_override); | |
| 486 web_contents->GetController().LoadIfNecessary(); | 498 web_contents->GetController().LoadIfNecessary(); |
| 487 } | 499 } |
| 488 RecordAppLaunch(profile_, tab); | 500 RecordAppLaunch(profile_, tab); |
| 501 if (contents) |
| 502 *contents = web_contents; |
| 503 |
| 489 return delegate; | 504 return delegate; |
| 490 } | 505 } |
| 491 | 506 |
| 492 | 507 |
| 493 bool TabRestoreServiceHelper::ValidateTab(Tab* tab) { | 508 bool TabRestoreServiceHelper::ValidateTab(Tab* tab) { |
| 494 if (tab->navigations.empty()) | 509 if (tab->navigations.empty()) |
| 495 return false; | 510 return false; |
| 496 | 511 |
| 497 tab->current_navigation_index = | 512 tab->current_navigation_index = |
| 498 std::max(0, std::min(tab->current_navigation_index, | 513 std::max(0, std::min(tab->current_navigation_index, |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 570 Tab* tab = static_cast<Tab*>(entry); | 585 Tab* tab = static_cast<Tab*>(entry); |
| 571 if (tab->browser_id == old_id) | 586 if (tab->browser_id == old_id) |
| 572 tab->browser_id = new_id; | 587 tab->browser_id = new_id; |
| 573 } | 588 } |
| 574 } | 589 } |
| 575 } | 590 } |
| 576 | 591 |
| 577 base::Time TabRestoreServiceHelper::TimeNow() const { | 592 base::Time TabRestoreServiceHelper::TimeNow() const { |
| 578 return time_factory_ ? time_factory_->TimeNow() : base::Time::Now(); | 593 return time_factory_ ? time_factory_->TimeNow() : base::Time::Now(); |
| 579 } | 594 } |
| OLD | NEW |