OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/session_restore.h" | 5 #include "chrome/browser/sessions/session_restore.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <list> | 8 #include <list> |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 delete this; | 293 delete this; |
294 return; | 294 return; |
295 } | 295 } |
296 | 296 |
297 if (browser_) { | 297 if (browser_) { |
298 registrar_.Add(this, NotificationType::BROWSER_CLOSED, | 298 registrar_.Add(this, NotificationType::BROWSER_CLOSED, |
299 Source<Browser>(browser_)); | 299 Source<Browser>(browser_)); |
300 } | 300 } |
301 } | 301 } |
302 | 302 |
303 void RestoreForeignSession(std::vector<SessionWindow*>* windows) { | 303 // Restore window(s) from a foreign session. |
304 tab_loader_.reset(new TabLoader()); | 304 void RestoreForeignSession( |
| 305 std::vector<SessionWindow*>::const_iterator begin, |
| 306 std::vector<SessionWindow*>::const_iterator end) { |
| 307 StartTabCreation(); |
305 // Create a browser instance to put the restored tabs in. | 308 // Create a browser instance to put the restored tabs in. |
306 bool has_tabbed_browser = false; | 309 for (std::vector<SessionWindow*>::const_iterator i = begin; |
307 for (std::vector<SessionWindow*>::iterator i = (*windows).begin(); | 310 i != end; ++i) { |
308 i != (*windows).end(); ++i) { | 311 Browser* browser = CreateRestoredBrowser( |
309 Browser* browser = NULL; | 312 static_cast<Browser::Type>((*i)->type), |
310 if (!has_tabbed_browser && (*i)->type == Browser::TYPE_NORMAL) | 313 (*i)->bounds, |
311 has_tabbed_browser = true; | 314 (*i)->is_maximized); |
312 browser = new Browser(static_cast<Browser::Type>((*i)->type), | |
313 profile_); | |
314 browser->set_override_bounds((*i)->bounds); | |
315 browser->set_maximized_state((*i)->is_maximized ? | |
316 Browser::MAXIMIZED_STATE_MAXIMIZED : | |
317 Browser::MAXIMIZED_STATE_UNMAXIMIZED); | |
318 browser->CreateBrowserWindow(); | |
319 | 315 |
320 // Restore and show the browser. | 316 // Restore and show the browser. |
321 const int initial_tab_count = browser->tab_count(); | 317 const int initial_tab_count = browser->tab_count(); |
322 RestoreTabsToBrowser(*(*i), browser); | 318 RestoreTabsToBrowser(*(*i), browser); |
323 ShowBrowser(browser, initial_tab_count, | 319 ShowBrowser(browser, initial_tab_count, (*i)->selected_tab_index); |
324 (*i)->selected_tab_index); | |
325 NotifySessionServiceOfRestoredTabs(browser, initial_tab_count); | 320 NotifySessionServiceOfRestoredTabs(browser, initial_tab_count); |
326 } | 321 } |
327 FinishedTabCreation(true, has_tabbed_browser); | 322 |
| 323 // Always create in a new window |
| 324 FinishedTabCreation(true, true); |
| 325 } |
| 326 |
| 327 // Restore a single tab from a foreign session. |
| 328 // Note: we currently restore the tab to the last active browser. |
| 329 void RestoreForeignTab(const SessionTab& tab) { |
| 330 StartTabCreation(); |
| 331 Browser* current_browser = |
| 332 browser_ ? browser_ : BrowserList::GetLastActive(); |
| 333 RestoreTab(tab, current_browser->tab_count(), current_browser); |
| 334 NotifySessionServiceOfRestoredTabs(current_browser, |
| 335 current_browser->tab_count()); |
| 336 FinishedTabCreation(true, true); |
328 } | 337 } |
329 | 338 |
330 ~SessionRestoreImpl() { | 339 ~SessionRestoreImpl() { |
331 STLDeleteElements(&windows_); | 340 STLDeleteElements(&windows_); |
332 restoring = false; | 341 restoring = false; |
333 } | 342 } |
334 | 343 |
335 virtual void Observe(NotificationType type, | 344 virtual void Observe(NotificationType type, |
336 const NotificationSource& source, | 345 const NotificationSource& source, |
337 const NotificationDetails& details) { | 346 const NotificationDetails& details) { |
338 switch (type.value) { | 347 switch (type.value) { |
339 case NotificationType::BROWSER_CLOSED: | 348 case NotificationType::BROWSER_CLOSED: |
340 delete this; | 349 delete this; |
341 return; | 350 return; |
342 | 351 |
343 default: | 352 default: |
344 NOTREACHED(); | 353 NOTREACHED(); |
345 break; | 354 break; |
346 } | 355 } |
347 } | 356 } |
348 | 357 |
349 private: | 358 private: |
| 359 // Invoked when beginning to create new tabs. Resets the tab_loader_. |
| 360 void StartTabCreation() { |
| 361 tab_loader_.reset(new TabLoader()); |
| 362 } |
| 363 |
350 // Invoked when done with creating all the tabs/browsers. | 364 // Invoked when done with creating all the tabs/browsers. |
351 // | 365 // |
352 // |created_tabbed_browser| indicates whether a tabbed browser was created, | 366 // |created_tabbed_browser| indicates whether a tabbed browser was created, |
353 // or we used an existing tabbed browser. | 367 // or we used an existing tabbed browser. |
354 // | 368 // |
355 // If successful, this begins loading tabs and deletes itself when all tabs | 369 // If successful, this begins loading tabs and deletes itself when all tabs |
356 // have been loaded. | 370 // have been loaded. |
357 void FinishedTabCreation(bool succeeded, bool created_tabbed_browser) { | 371 void FinishedTabCreation(bool succeeded, bool created_tabbed_browser) { |
358 if (!created_tabbed_browser && always_create_tabbed_browser_) { | 372 if (!created_tabbed_browser && always_create_tabbed_browser_) { |
359 Browser* browser = Browser::Create(profile_); | 373 Browser* browser = Browser::Create(profile_); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 ProcessSessionWindows(windows); | 408 ProcessSessionWindows(windows); |
395 } | 409 } |
396 | 410 |
397 void ProcessSessionWindows(std::vector<SessionWindow*>* windows) { | 411 void ProcessSessionWindows(std::vector<SessionWindow*>* windows) { |
398 if (windows->empty()) { | 412 if (windows->empty()) { |
399 // Restore was unsuccessful. | 413 // Restore was unsuccessful. |
400 FinishedTabCreation(false, false); | 414 FinishedTabCreation(false, false); |
401 return; | 415 return; |
402 } | 416 } |
403 | 417 |
404 tab_loader_.reset(new TabLoader()); | 418 StartTabCreation(); |
405 | 419 |
406 Browser* current_browser = | 420 Browser* current_browser = |
407 browser_ ? browser_ : BrowserList::GetLastActive(); | 421 browser_ ? browser_ : BrowserList::GetLastActive(); |
408 // After the for loop this contains the last TABBED_BROWSER. Is null if no | 422 // After the for loop this contains the last TABBED_BROWSER. Is null if no |
409 // tabbed browsers exist. | 423 // tabbed browsers exist. |
410 Browser* last_browser = NULL; | 424 Browser* last_browser = NULL; |
411 bool has_tabbed_browser = false; | 425 bool has_tabbed_browser = false; |
412 for (std::vector<SessionWindow*>::iterator i = windows->begin(); | 426 for (std::vector<SessionWindow*>::iterator i = windows->begin(); |
413 i != windows->end(); ++i) { | 427 i != windows->end(); ++i) { |
414 Browser* browser = NULL; | 428 Browser* browser = NULL; |
415 if (!has_tabbed_browser && (*i)->type == Browser::TYPE_NORMAL) | 429 if (!has_tabbed_browser && (*i)->type == Browser::TYPE_NORMAL) |
416 has_tabbed_browser = true; | 430 has_tabbed_browser = true; |
417 if (i == windows->begin() && (*i)->type == Browser::TYPE_NORMAL && | 431 if (i == windows->begin() && (*i)->type == Browser::TYPE_NORMAL && |
418 !clobber_existing_window_) { | 432 !clobber_existing_window_) { |
419 // If there is an open tabbed browser window, use it. Otherwise fall | 433 // If there is an open tabbed browser window, use it. Otherwise fall |
420 // through and create a new one. | 434 // through and create a new one. |
421 browser = current_browser; | 435 browser = current_browser; |
422 if (browser && (browser->type() != Browser::TYPE_NORMAL || | 436 if (browser && (browser->type() != Browser::TYPE_NORMAL || |
423 browser->profile()->IsOffTheRecord())) { | 437 browser->profile()->IsOffTheRecord())) { |
424 browser = NULL; | 438 browser = NULL; |
425 } | 439 } |
426 } | 440 } |
427 if (!browser) { | 441 if (!browser) { |
428 browser = new Browser(static_cast<Browser::Type>((*i)->type), profile_); | 442 browser = CreateRestoredBrowser( |
429 browser->set_override_bounds((*i)->bounds); | 443 static_cast<Browser::Type>((*i)->type), |
430 browser->set_maximized_state((*i)->is_maximized ? | 444 (*i)->bounds, |
431 Browser::MAXIMIZED_STATE_MAXIMIZED : | 445 (*i)->is_maximized); |
432 Browser::MAXIMIZED_STATE_UNMAXIMIZED); | |
433 browser->CreateBrowserWindow(); | |
434 } | 446 } |
435 if ((*i)->type == Browser::TYPE_NORMAL) | 447 if ((*i)->type == Browser::TYPE_NORMAL) |
436 last_browser = browser; | 448 last_browser = browser; |
437 const int initial_tab_count = browser->tab_count(); | 449 const int initial_tab_count = browser->tab_count(); |
438 RestoreTabsToBrowser(*(*i), browser); | 450 RestoreTabsToBrowser(*(*i), browser); |
439 ShowBrowser(browser, initial_tab_count, (*i)->selected_tab_index); | 451 ShowBrowser(browser, initial_tab_count, (*i)->selected_tab_index); |
440 NotifySessionServiceOfRestoredTabs(browser, initial_tab_count); | 452 NotifySessionServiceOfRestoredTabs(browser, initial_tab_count); |
441 } | 453 } |
442 | 454 |
443 // If we're restoring a session as the result of a crash and the session | 455 // If we're restoring a session as the result of a crash and the session |
444 // included at least one tabbed browser, then close the browser window | 456 // included at least one tabbed browser, then close the browser window |
445 // that was opened when the user clicked to restore the session. | 457 // that was opened when the user clicked to restore the session. |
446 if (clobber_existing_window_ && current_browser && has_tabbed_browser && | 458 if (clobber_existing_window_ && current_browser && has_tabbed_browser && |
447 current_browser->type() == Browser::TYPE_NORMAL) { | 459 current_browser->type() == Browser::TYPE_NORMAL) { |
448 current_browser->CloseAllTabs(); | 460 current_browser->CloseAllTabs(); |
449 } | 461 } |
450 if (last_browser && !urls_to_open_.empty()) | 462 if (last_browser && !urls_to_open_.empty()) |
451 AppendURLsToBrowser(last_browser, urls_to_open_); | 463 AppendURLsToBrowser(last_browser, urls_to_open_); |
452 // If last_browser is NULL and urls_to_open_ is non-empty, | 464 // If last_browser is NULL and urls_to_open_ is non-empty, |
453 // FinishedTabCreation will create a new TabbedBrowser and add the urls to | 465 // FinishedTabCreation will create a new TabbedBrowser and add the urls to |
454 // it. | 466 // it. |
455 FinishedTabCreation(true, has_tabbed_browser); | 467 FinishedTabCreation(true, has_tabbed_browser); |
456 } | 468 } |
457 | 469 |
458 void RestoreTabsToBrowser(const SessionWindow& window, Browser* browser) { | 470 void RestoreTabsToBrowser(const SessionWindow& window, Browser* browser) { |
459 DCHECK(!window.tabs.empty()); | 471 DCHECK(!window.tabs.empty()); |
460 for (std::vector<SessionTab*>::const_iterator i = window.tabs.begin(); | 472 for (std::vector<SessionTab*>::const_iterator i = window.tabs.begin(); |
461 i != window.tabs.end(); ++i) { | 473 i != window.tabs.end(); ++i) { |
462 const SessionTab& tab = *(*i); | 474 const SessionTab& tab = *(*i); |
463 DCHECK(!tab.navigations.empty()); | 475 RestoreTab(tab, static_cast<int>(i - window.tabs.begin()), browser); |
464 int selected_index = tab.current_navigation_index; | |
465 selected_index = std::max( | |
466 0, | |
467 std::min(selected_index, | |
468 static_cast<int>(tab.navigations.size() - 1))); | |
469 tab_loader_->ScheduleLoad( | |
470 &browser->AddRestoredTab(tab.navigations, | |
471 static_cast<int>(i - window.tabs.begin()), | |
472 selected_index, | |
473 tab.extension_app_id, | |
474 false, | |
475 tab.pinned, | |
476 true, | |
477 NULL)->controller()); | |
478 } | 476 } |
479 } | 477 } |
480 | 478 |
| 479 void RestoreTab(const SessionTab& tab, |
| 480 const int tab_index, |
| 481 Browser* browser) { |
| 482 DCHECK(!tab.navigations.empty()); |
| 483 int selected_index = tab.current_navigation_index; |
| 484 selected_index = std::max( |
| 485 0, |
| 486 std::min(selected_index, |
| 487 static_cast<int>(tab.navigations.size() - 1))); |
| 488 tab_loader_->ScheduleLoad( |
| 489 &browser->AddRestoredTab(tab.navigations, |
| 490 tab_index, |
| 491 selected_index, |
| 492 tab.extension_app_id, |
| 493 false, |
| 494 tab.pinned, |
| 495 true, |
| 496 NULL)->controller()); |
| 497 } |
| 498 |
| 499 Browser* CreateRestoredBrowser(Browser::Type type, |
| 500 gfx::Rect bounds, |
| 501 bool is_maximized) { |
| 502 Browser* browser = new Browser(type, profile_); |
| 503 browser->set_override_bounds(bounds); |
| 504 browser->set_maximized_state(is_maximized ? |
| 505 Browser::MAXIMIZED_STATE_MAXIMIZED : |
| 506 Browser::MAXIMIZED_STATE_UNMAXIMIZED); |
| 507 browser->CreateBrowserWindow(); |
| 508 return browser; |
| 509 } |
| 510 |
481 void ShowBrowser(Browser* browser, | 511 void ShowBrowser(Browser* browser, |
482 int initial_tab_count, | 512 int initial_tab_count, |
483 int selected_session_index) { | 513 int selected_session_index) { |
484 if (browser_ == browser) { | 514 if (browser_ == browser) { |
485 browser->SelectTabContentsAt(browser->tab_count() - 1, true); | 515 browser->SelectTabContentsAt(browser->tab_count() - 1, true); |
486 return; | 516 return; |
487 } | 517 } |
488 | 518 |
489 DCHECK(browser); | 519 DCHECK(browser); |
490 DCHECK(browser->tab_count()); | 520 DCHECK(browser->tab_count()); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
594 void SessionRestore::RestoreSession(Profile* profile, | 624 void SessionRestore::RestoreSession(Profile* profile, |
595 Browser* browser, | 625 Browser* browser, |
596 bool clobber_existing_window, | 626 bool clobber_existing_window, |
597 bool always_create_tabbed_browser, | 627 bool always_create_tabbed_browser, |
598 const std::vector<GURL>& urls_to_open) { | 628 const std::vector<GURL>& urls_to_open) { |
599 Restore(profile, browser, false, clobber_existing_window, | 629 Restore(profile, browser, false, clobber_existing_window, |
600 always_create_tabbed_browser, urls_to_open); | 630 always_create_tabbed_browser, urls_to_open); |
601 } | 631 } |
602 | 632 |
603 // static | 633 // static |
604 void SessionRestore::RestoreForeignSessionWindows(Profile* profile, | 634 void SessionRestore::RestoreForeignSessionWindows( |
605 std::vector<SessionWindow*>* windows) { | 635 Profile* profile, |
| 636 std::vector<SessionWindow*>::const_iterator begin, |
| 637 std::vector<SessionWindow*>::const_iterator end) { |
606 // Create a SessionRestore object to eventually restore the tabs. | 638 // Create a SessionRestore object to eventually restore the tabs. |
607 std::vector<GURL> gurls; | 639 std::vector<GURL> gurls; |
608 SessionRestoreImpl restorer(profile, | 640 SessionRestoreImpl restorer(profile, |
609 static_cast<Browser*>(NULL), true, false, true, gurls); | 641 static_cast<Browser*>(NULL), true, false, true, gurls); |
610 restorer.RestoreForeignSession(windows); | 642 restorer.RestoreForeignSession(begin, end); |
| 643 } |
| 644 |
| 645 // static |
| 646 void SessionRestore::RestoreForeignSessionTab(Profile* profile, |
| 647 const SessionTab& tab) { |
| 648 // Create a SessionRestore object to eventually restore the tabs. |
| 649 std::vector<GURL> gurls; |
| 650 SessionRestoreImpl restorer(profile, |
| 651 static_cast<Browser*>(NULL), true, false, true, gurls); |
| 652 restorer.RestoreForeignTab(tab); |
611 } | 653 } |
612 | 654 |
613 // static | 655 // static |
614 void SessionRestore::RestoreSessionSynchronously( | 656 void SessionRestore::RestoreSessionSynchronously( |
615 Profile* profile, | 657 Profile* profile, |
616 const std::vector<GURL>& urls_to_open) { | 658 const std::vector<GURL>& urls_to_open) { |
617 Restore(profile, NULL, true, false, true, urls_to_open); | 659 Restore(profile, NULL, true, false, true, urls_to_open); |
618 } | 660 } |
619 | 661 |
620 // static | 662 // static |
621 bool SessionRestore::IsRestoring() { | 663 bool SessionRestore::IsRestoring() { |
622 return restoring; | 664 return restoring; |
623 } | 665 } |
OLD | NEW |