| 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 "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 28 matching lines...) Expand all Loading... |
| 39 Entries::const_iterator entry_iterator) { | 39 Entries::const_iterator entry_iterator) { |
| 40 } | 40 } |
| 41 | 41 |
| 42 void TabRestoreServiceHelper::Observer::OnAddEntry() {} | 42 void TabRestoreServiceHelper::Observer::OnAddEntry() {} |
| 43 | 43 |
| 44 // TabRestoreServiceHelper ----------------------------------------------------- | 44 // TabRestoreServiceHelper ----------------------------------------------------- |
| 45 | 45 |
| 46 TabRestoreServiceHelper::TabRestoreServiceHelper( | 46 TabRestoreServiceHelper::TabRestoreServiceHelper( |
| 47 TabRestoreService* tab_restore_service, | 47 TabRestoreService* tab_restore_service, |
| 48 Observer* observer, | 48 Observer* observer, |
| 49 Profile* profile, | |
| 50 sessions::TabRestoreServiceClient* client, | 49 sessions::TabRestoreServiceClient* client, |
| 51 TabRestoreService::TimeFactory* time_factory) | 50 TabRestoreService::TimeFactory* time_factory) |
| 52 : tab_restore_service_(tab_restore_service), | 51 : tab_restore_service_(tab_restore_service), |
| 53 observer_(observer), | 52 observer_(observer), |
| 54 profile_(profile), | |
| 55 client_(client), | 53 client_(client), |
| 56 restoring_(false), | 54 restoring_(false), |
| 57 time_factory_(time_factory) { | 55 time_factory_(time_factory) { |
| 58 DCHECK(tab_restore_service_); | 56 DCHECK(tab_restore_service_); |
| 59 } | 57 } |
| 60 | 58 |
| 61 TabRestoreServiceHelper::~TabRestoreServiceHelper() { | 59 TabRestoreServiceHelper::~TabRestoreServiceHelper() { |
| 62 FOR_EACH_OBSERVER(TabRestoreServiceObserver, observer_list_, | 60 FOR_EACH_OBSERVER(TabRestoreServiceObserver, observer_list_, |
| 63 TabRestoreServiceDestroyed(tab_restore_service_)); | 61 TabRestoreServiceDestroyed(tab_restore_service_)); |
| 64 STLDeleteElements(&entries_); | 62 STLDeleteElements(&entries_); |
| 65 } | 63 } |
| 66 | 64 |
| 67 void TabRestoreServiceHelper::AddObserver( | 65 void TabRestoreServiceHelper::AddObserver( |
| 68 TabRestoreServiceObserver* observer) { | 66 TabRestoreServiceObserver* observer) { |
| 69 observer_list_.AddObserver(observer); | 67 observer_list_.AddObserver(observer); |
| 70 } | 68 } |
| 71 | 69 |
| 72 void TabRestoreServiceHelper::RemoveObserver( | 70 void TabRestoreServiceHelper::RemoveObserver( |
| 73 TabRestoreServiceObserver* observer) { | 71 TabRestoreServiceObserver* observer) { |
| 74 observer_list_.RemoveObserver(observer); | 72 observer_list_.RemoveObserver(observer); |
| 75 } | 73 } |
| 76 | 74 |
| 77 void TabRestoreServiceHelper::CreateHistoricalTab( | 75 void TabRestoreServiceHelper::CreateHistoricalTab( |
| 78 content::WebContents* contents, | 76 content::WebContents* contents, |
| 79 int index) { | 77 int index) { |
| 80 if (restoring_) | 78 if (restoring_) |
| 81 return; | 79 return; |
| 82 | 80 |
| 83 TabRestoreServiceDelegate* delegate = | 81 TabRestoreServiceDelegate* delegate = |
| 84 TabRestoreServiceDelegate::FindDelegateForWebContents(contents); | 82 client_->FindTabRestoreServiceDelegateForWebContents(contents); |
| 85 if (closing_delegates_.find(delegate) != closing_delegates_.end()) | 83 if (closing_delegates_.find(delegate) != closing_delegates_.end()) |
| 86 return; | 84 return; |
| 87 | 85 |
| 88 scoped_ptr<Tab> local_tab(new Tab()); | 86 scoped_ptr<Tab> local_tab(new Tab()); |
| 89 PopulateTab(local_tab.get(), index, delegate, &contents->GetController()); | 87 PopulateTab(local_tab.get(), index, delegate, &contents->GetController()); |
| 90 if (local_tab->navigations.empty()) | 88 if (local_tab->navigations.empty()) |
| 91 return; | 89 return; |
| 92 | 90 |
| 93 AddEntry(local_tab.release(), true, true); | 91 AddEntry(local_tab.release(), true, true); |
| 94 } | 92 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 NotifyTabsChanged(); | 143 NotifyTabsChanged(); |
| 146 } | 144 } |
| 147 | 145 |
| 148 const TabRestoreService::Entries& TabRestoreServiceHelper::entries() const { | 146 const TabRestoreService::Entries& TabRestoreServiceHelper::entries() const { |
| 149 return entries_; | 147 return entries_; |
| 150 } | 148 } |
| 151 | 149 |
| 152 std::vector<content::WebContents*> | 150 std::vector<content::WebContents*> |
| 153 TabRestoreServiceHelper::RestoreMostRecentEntry( | 151 TabRestoreServiceHelper::RestoreMostRecentEntry( |
| 154 TabRestoreServiceDelegate* delegate, | 152 TabRestoreServiceDelegate* delegate, |
| 155 chrome::HostDesktopType host_desktop_type) { | 153 int host_desktop_type) { |
| 156 if (entries_.empty()) | 154 if (entries_.empty()) |
| 157 return std::vector<WebContents*>(); | 155 return std::vector<WebContents*>(); |
| 158 | 156 |
| 159 return RestoreEntryById(delegate, entries_.front()->id, host_desktop_type, | 157 return RestoreEntryById(delegate, entries_.front()->id, host_desktop_type, |
| 160 UNKNOWN); | 158 UNKNOWN); |
| 161 } | 159 } |
| 162 | 160 |
| 163 TabRestoreService::Tab* TabRestoreServiceHelper::RemoveTabEntryById( | 161 TabRestoreService::Tab* TabRestoreServiceHelper::RemoveTabEntryById( |
| 164 SessionID::id_type id) { | 162 SessionID::id_type id) { |
| 165 Entries::iterator i = GetEntryIteratorById(id); | 163 Entries::iterator i = GetEntryIteratorById(id); |
| 166 if (i == entries_.end()) | 164 if (i == entries_.end()) |
| 167 return NULL; | 165 return NULL; |
| 168 | 166 |
| 169 Entry* entry = *i; | 167 Entry* entry = *i; |
| 170 if (entry->type != TabRestoreService::TAB) | 168 if (entry->type != TabRestoreService::TAB) |
| 171 return NULL; | 169 return NULL; |
| 172 | 170 |
| 173 Tab* tab = static_cast<Tab*>(entry); | 171 Tab* tab = static_cast<Tab*>(entry); |
| 174 entries_.erase(i); | 172 entries_.erase(i); |
| 175 return tab; | 173 return tab; |
| 176 } | 174 } |
| 177 | 175 |
| 178 std::vector<content::WebContents*> TabRestoreServiceHelper::RestoreEntryById( | 176 std::vector<content::WebContents*> TabRestoreServiceHelper::RestoreEntryById( |
| 179 TabRestoreServiceDelegate* delegate, | 177 TabRestoreServiceDelegate* delegate, |
| 180 SessionID::id_type id, | 178 SessionID::id_type id, |
| 181 chrome::HostDesktopType host_desktop_type, | 179 int host_desktop_type, |
| 182 WindowOpenDisposition disposition) { | 180 WindowOpenDisposition disposition) { |
| 183 Entries::iterator entry_iterator = GetEntryIteratorById(id); | 181 Entries::iterator entry_iterator = GetEntryIteratorById(id); |
| 184 if (entry_iterator == entries_.end()) | 182 if (entry_iterator == entries_.end()) |
| 185 // Don't hoark here, we allow an invalid id. | 183 // Don't hoark here, we allow an invalid id. |
| 186 return std::vector<WebContents*>(); | 184 return std::vector<WebContents*>(); |
| 187 | 185 |
| 188 if (observer_) | 186 if (observer_) |
| 189 observer_->OnRestoreEntryById(id, entry_iterator); | 187 observer_->OnRestoreEntryById(id, entry_iterator); |
| 190 restoring_ = true; | 188 restoring_ = true; |
| 191 Entry* entry = *entry_iterator; | 189 Entry* entry = *entry_iterator; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 211 web_contents.push_back(restored_tab); | 209 web_contents.push_back(restored_tab); |
| 212 delegate->ShowBrowserWindow(); | 210 delegate->ShowBrowserWindow(); |
| 213 } else if (entry->type == TabRestoreService::WINDOW) { | 211 } else if (entry->type == TabRestoreService::WINDOW) { |
| 214 TabRestoreServiceDelegate* current_delegate = delegate; | 212 TabRestoreServiceDelegate* current_delegate = delegate; |
| 215 Window* window = static_cast<Window*>(entry); | 213 Window* window = static_cast<Window*>(entry); |
| 216 | 214 |
| 217 // When restoring a window, either the entire window can be restored, or a | 215 // When restoring a window, either the entire window can be restored, or a |
| 218 // single tab within it. If the entry's ID matches the one to restore, then | 216 // single tab within it. If the entry's ID matches the one to restore, then |
| 219 // the entire window will be restored. | 217 // the entire window will be restored. |
| 220 if (!restoring_tab_in_window) { | 218 if (!restoring_tab_in_window) { |
| 221 delegate = TabRestoreServiceDelegate::Create(profile_, host_desktop_type, | 219 delegate = client_->CreateTabRestoreServiceDelegate(host_desktop_type, |
| 222 window->app_name); | 220 window->app_name); |
| 223 for (size_t tab_i = 0; tab_i < window->tabs.size(); ++tab_i) { | 221 for (size_t tab_i = 0; tab_i < window->tabs.size(); ++tab_i) { |
| 224 const Tab& tab = window->tabs[tab_i]; | 222 const Tab& tab = window->tabs[tab_i]; |
| 225 WebContents* restored_tab = delegate->AddRestoredTab( | 223 WebContents* restored_tab = delegate->AddRestoredTab( |
| 226 tab.navigations, | 224 tab.navigations, |
| 227 delegate->GetTabCount(), | 225 delegate->GetTabCount(), |
| 228 tab.current_navigation_index, | 226 tab.current_navigation_index, |
| 229 tab.extension_app_id, | 227 tab.extension_app_id, |
| 230 static_cast<int>(tab_i) == window->selected_tab_index, | 228 static_cast<int>(tab_i) == window->selected_tab_index, |
| 231 tab.pinned, | 229 tab.pinned, |
| 232 tab.from_last_session, | 230 tab.from_last_session, |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 // Delegate may be NULL during unit tests. | 421 // Delegate may be NULL during unit tests. |
| 424 if (delegate) { | 422 if (delegate) { |
| 425 tab->browser_id = delegate->GetSessionID().id(); | 423 tab->browser_id = delegate->GetSessionID().id(); |
| 426 tab->pinned = delegate->IsTabPinned(tab->tabstrip_index); | 424 tab->pinned = delegate->IsTabPinned(tab->tabstrip_index); |
| 427 } | 425 } |
| 428 } | 426 } |
| 429 | 427 |
| 430 TabRestoreServiceDelegate* TabRestoreServiceHelper::RestoreTab( | 428 TabRestoreServiceDelegate* TabRestoreServiceHelper::RestoreTab( |
| 431 const Tab& tab, | 429 const Tab& tab, |
| 432 TabRestoreServiceDelegate* delegate, | 430 TabRestoreServiceDelegate* delegate, |
| 433 chrome::HostDesktopType host_desktop_type, | 431 int host_desktop_type, |
| 434 WindowOpenDisposition disposition, | 432 WindowOpenDisposition disposition, |
| 435 WebContents** contents) { | 433 WebContents** contents) { |
| 436 WebContents* web_contents; | 434 WebContents* web_contents; |
| 437 if (disposition == CURRENT_TAB && delegate) { | 435 if (disposition == CURRENT_TAB && delegate) { |
| 438 web_contents = delegate->ReplaceRestoredTab( | 436 web_contents = delegate->ReplaceRestoredTab( |
| 439 tab.navigations, | 437 tab.navigations, |
| 440 tab.current_navigation_index, | 438 tab.current_navigation_index, |
| 441 tab.from_last_session, | 439 tab.from_last_session, |
| 442 tab.extension_app_id, | 440 tab.extension_app_id, |
| 443 tab.session_storage_namespace.get(), | 441 tab.session_storage_namespace.get(), |
| 444 tab.user_agent_override); | 442 tab.user_agent_override); |
| 445 } else { | 443 } else { |
| 446 // We only respsect the tab's original browser if there's no disposition. | 444 // We only respsect the tab's original browser if there's no disposition. |
| 447 if (disposition == UNKNOWN && tab.has_browser()) { | 445 if (disposition == UNKNOWN && tab.has_browser()) { |
| 448 delegate = TabRestoreServiceDelegate::FindDelegateWithID( | 446 delegate = client_->FindTabRestoreServiceDelegateWithID( |
| 449 tab.browser_id, host_desktop_type); | 447 tab.browser_id, host_desktop_type); |
| 450 } | 448 } |
| 451 | 449 |
| 452 int tab_index = -1; | 450 int tab_index = -1; |
| 453 | 451 |
| 454 // |delegate| will be NULL in cases where one isn't already available (eg, | 452 // |delegate| will be NULL in cases where one isn't already available (eg, |
| 455 // when invoked on Mac OS X with no windows open). In this case, create a | 453 // when invoked on Mac OS X with no windows open). In this case, create a |
| 456 // new browser into which we restore the tabs. | 454 // new browser into which we restore the tabs. |
| 457 if (delegate && disposition != NEW_WINDOW) { | 455 if (delegate && disposition != NEW_WINDOW) { |
| 458 tab_index = tab.tabstrip_index; | 456 tab_index = tab.tabstrip_index; |
| 459 } else { | 457 } else { |
| 460 delegate = TabRestoreServiceDelegate::Create(profile_, host_desktop_type, | 458 delegate = client_->CreateTabRestoreServiceDelegate(host_desktop_type, |
| 461 std::string()); | 459 std::string()); |
| 462 if (tab.has_browser()) | 460 if (tab.has_browser()) |
| 463 UpdateTabBrowserIDs(tab.browser_id, delegate->GetSessionID().id()); | 461 UpdateTabBrowserIDs(tab.browser_id, delegate->GetSessionID().id()); |
| 464 } | 462 } |
| 465 | 463 |
| 466 // Place the tab at the end if the tab index is no longer valid or | 464 // Place the tab at the end if the tab index is no longer valid or |
| 467 // we were passed a specific disposition. | 465 // we were passed a specific disposition. |
| 468 if (tab_index < 0 || tab_index > delegate->GetTabCount() || | 466 if (tab_index < 0 || tab_index > delegate->GetTabCount() || |
| 469 disposition != UNKNOWN) { | 467 disposition != UNKNOWN) { |
| 470 tab_index = delegate->GetTabCount(); | 468 tab_index = delegate->GetTabCount(); |
| 471 } | 469 } |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 569 Tab* tab = static_cast<Tab*>(entry); | 567 Tab* tab = static_cast<Tab*>(entry); |
| 570 if (tab->browser_id == old_id) | 568 if (tab->browser_id == old_id) |
| 571 tab->browser_id = new_id; | 569 tab->browser_id = new_id; |
| 572 } | 570 } |
| 573 } | 571 } |
| 574 } | 572 } |
| 575 | 573 |
| 576 base::Time TabRestoreServiceHelper::TimeNow() const { | 574 base::Time TabRestoreServiceHelper::TimeNow() const { |
| 577 return time_factory_ ? time_factory_->TimeNow() : base::Time::Now(); | 575 return time_factory_ ? time_factory_->TimeNow() : base::Time::Now(); |
| 578 } | 576 } |
| OLD | NEW |