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