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/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 <string> | 10 #include <string> |
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
560 } | 560 } |
561 | 561 |
562 if (browser_) { | 562 if (browser_) { |
563 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSED, | 563 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSED, |
564 content::Source<Browser>(browser_)); | 564 content::Source<Browser>(browser_)); |
565 } | 565 } |
566 | 566 |
567 return browser_; | 567 return browser_; |
568 } | 568 } |
569 | 569 |
570 // Restore window(s) from a foreign session. | 570 // Restore window(s) from a foreign session. Return corresponding created |
sky
2013/08/01 00:05:43
Returns newly created Browsers.
Kristen Dwan
2013/08/02 20:54:19
Done.
| |
571 void RestoreForeignSession( | 571 // Browsers to identify restored Windows. |
572 std::vector<Browser*> RestoreForeignSession( | |
572 std::vector<const SessionWindow*>::const_iterator begin, | 573 std::vector<const SessionWindow*>::const_iterator begin, |
573 std::vector<const SessionWindow*>::const_iterator end) { | 574 std::vector<const SessionWindow*>::const_iterator end) { |
574 StartTabCreation(); | 575 StartTabCreation(); |
576 std::vector<Browser*> browsers; | |
575 // Create a browser instance to put the restored tabs in. | 577 // Create a browser instance to put the restored tabs in. |
576 for (std::vector<const SessionWindow*>::const_iterator i = begin; | 578 for (std::vector<const SessionWindow*>::const_iterator i = begin; |
577 i != end; ++i) { | 579 i != end; ++i) { |
578 Browser* browser = CreateRestoredBrowser( | 580 Browser* browser = CreateRestoredBrowser( |
579 static_cast<Browser::Type>((*i)->type), | 581 static_cast<Browser::Type>((*i)->type), |
580 (*i)->bounds, | 582 (*i)->bounds, |
581 (*i)->show_state, | 583 (*i)->show_state, |
582 (*i)->app_name); | 584 (*i)->app_name); |
585 browsers.push_back(browser); | |
583 | 586 |
584 // Restore and show the browser. | 587 // Restore and show the browser. |
585 const int initial_tab_count = 0; | 588 const int initial_tab_count = 0; |
586 int selected_tab_index = std::max( | 589 int selected_tab_index = std::max( |
587 0, | 590 0, |
588 std::min((*i)->selected_tab_index, | 591 std::min((*i)->selected_tab_index, |
589 static_cast<int>((*i)->tabs.size()) - 1)); | 592 static_cast<int>((*i)->tabs.size()) - 1)); |
590 RestoreTabsToBrowser(*(*i), browser, initial_tab_count, | 593 RestoreTabsToBrowser(*(*i), browser, initial_tab_count, |
591 selected_tab_index); | 594 selected_tab_index); |
592 NotifySessionServiceOfRestoredTabs(browser, initial_tab_count); | 595 NotifySessionServiceOfRestoredTabs(browser, initial_tab_count); |
593 } | 596 } |
594 | 597 |
595 // Always create in a new window | 598 // Always create in a new window |
596 FinishedTabCreation(true, true); | 599 FinishedTabCreation(true, true); |
600 return browsers; | |
597 } | 601 } |
598 | 602 |
599 // Restore a single tab from a foreign session. | 603 // Restore a single tab from a foreign session. |
600 // Opens in the tab in the last active browser, unless disposition is | 604 // Opens in the tab in the last active browser, unless disposition is |
601 // NEW_WINDOW, in which case the tab will be opened in a new browser. | 605 // NEW_WINDOW, in which case the tab will be opened in a new browser. Returns |
602 void RestoreForeignTab(const SessionTab& tab, | 606 // the WebContents of the restored tab. |
607 WebContents* RestoreForeignTab(const SessionTab& tab, | |
603 WindowOpenDisposition disposition) { | 608 WindowOpenDisposition disposition) { |
sky
2013/08/01 00:05:43
nit: fix indentation.
Kristen Dwan
2013/08/02 20:54:19
Done.
| |
604 DCHECK(!tab.navigations.empty()); | 609 DCHECK(!tab.navigations.empty()); |
605 int selected_index = tab.current_navigation_index; | 610 int selected_index = tab.current_navigation_index; |
606 selected_index = std::max( | 611 selected_index = std::max( |
607 0, | 612 0, |
608 std::min(selected_index, | 613 std::min(selected_index, |
609 static_cast<int>(tab.navigations.size() - 1))); | 614 static_cast<int>(tab.navigations.size() - 1))); |
610 | 615 |
611 bool use_new_window = disposition == NEW_WINDOW; | 616 bool use_new_window = disposition == NEW_WINDOW; |
612 | 617 |
613 Browser* browser = use_new_window ? | 618 Browser* browser = use_new_window ? |
614 new Browser(Browser::CreateParams(profile_, host_desktop_type_)) : | 619 new Browser(Browser::CreateParams(profile_, host_desktop_type_)) : |
615 browser_; | 620 browser_; |
616 | 621 |
617 RecordAppLaunchForTab(browser, tab, selected_index); | 622 RecordAppLaunchForTab(browser, tab, selected_index); |
618 | 623 |
624 WebContents* web_contents; | |
619 if (disposition == CURRENT_TAB) { | 625 if (disposition == CURRENT_TAB) { |
620 DCHECK(!use_new_window); | 626 DCHECK(!use_new_window); |
621 chrome::ReplaceRestoredTab(browser, | 627 web_contents = chrome::ReplaceRestoredTab(browser, |
622 tab.navigations, | 628 tab.navigations, |
623 selected_index, | 629 selected_index, |
624 true, | 630 true, |
625 tab.extension_app_id, | 631 tab.extension_app_id, |
626 NULL, | 632 NULL, |
627 tab.user_agent_override); | 633 tab.user_agent_override); |
628 } else { | 634 } else { |
629 int tab_index = | 635 int tab_index = |
630 use_new_window ? 0 : browser->tab_strip_model()->active_index() + 1; | 636 use_new_window ? 0 : browser->tab_strip_model()->active_index() + 1; |
631 WebContents* web_contents = chrome::AddRestoredTab( | 637 web_contents = chrome::AddRestoredTab( |
632 browser, | 638 browser, |
633 tab.navigations, | 639 tab.navigations, |
634 tab_index, | 640 tab_index, |
635 selected_index, | 641 selected_index, |
636 tab.extension_app_id, | 642 tab.extension_app_id, |
637 disposition == NEW_FOREGROUND_TAB, // selected | 643 disposition == NEW_FOREGROUND_TAB, // selected |
638 tab.pinned, | 644 tab.pinned, |
639 true, | 645 true, |
640 NULL, | 646 NULL, |
641 tab.user_agent_override); | 647 tab.user_agent_override); |
642 // Start loading the tab immediately. | 648 // Start loading the tab immediately. |
643 web_contents->GetController().LoadIfNecessary(); | 649 web_contents->GetController().LoadIfNecessary(); |
644 } | 650 } |
645 | 651 |
646 if (use_new_window) { | 652 if (use_new_window) { |
647 browser->tab_strip_model()->ActivateTabAt(0, true); | 653 browser->tab_strip_model()->ActivateTabAt(0, true); |
648 browser->window()->Show(); | 654 browser->window()->Show(); |
649 } | 655 } |
650 NotifySessionServiceOfRestoredTabs(browser, | 656 NotifySessionServiceOfRestoredTabs(browser, |
651 browser->tab_strip_model()->count()); | 657 browser->tab_strip_model()->count()); |
652 | 658 |
653 // Since FinishedTabCreation() is not called here, |this| will leak if we | 659 // Since FinishedTabCreation() is not called here, |this| will leak if we |
654 // are not in sychronous mode. | 660 // are not in sychronous mode. |
655 DCHECK(synchronous_); | 661 DCHECK(synchronous_); |
662 return web_contents; | |
656 } | 663 } |
657 | 664 |
658 virtual ~SessionRestoreImpl() { | 665 virtual ~SessionRestoreImpl() { |
659 STLDeleteElements(&windows_); | 666 STLDeleteElements(&windows_); |
660 | 667 |
661 active_session_restorers->erase(this); | 668 active_session_restorers->erase(this); |
662 if (active_session_restorers->empty()) { | 669 if (active_session_restorers->empty()) { |
663 delete active_session_restorers; | 670 delete active_session_restorers; |
664 active_session_restorers = NULL; | 671 active_session_restorers = NULL; |
665 } | 672 } |
(...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1212 // SessionRestoreImpl takes care of deleting itself when done. | 1219 // SessionRestoreImpl takes care of deleting itself when done. |
1213 SessionRestoreImpl* restorer = new SessionRestoreImpl( | 1220 SessionRestoreImpl* restorer = new SessionRestoreImpl( |
1214 profile, browser, host_desktop_type, (behavior & SYNCHRONOUS) != 0, | 1221 profile, browser, host_desktop_type, (behavior & SYNCHRONOUS) != 0, |
1215 (behavior & CLOBBER_CURRENT_TAB) != 0, | 1222 (behavior & CLOBBER_CURRENT_TAB) != 0, |
1216 (behavior & ALWAYS_CREATE_TABBED_BROWSER) != 0, | 1223 (behavior & ALWAYS_CREATE_TABBED_BROWSER) != 0, |
1217 urls_to_open); | 1224 urls_to_open); |
1218 return restorer->Restore(); | 1225 return restorer->Restore(); |
1219 } | 1226 } |
1220 | 1227 |
1221 // static | 1228 // static |
1222 void SessionRestore::RestoreForeignSessionWindows( | 1229 std::vector<Browser*> SessionRestore::RestoreForeignSessionWindows( |
1223 Profile* profile, | 1230 Profile* profile, |
1224 chrome::HostDesktopType host_desktop_type, | 1231 chrome::HostDesktopType host_desktop_type, |
1225 std::vector<const SessionWindow*>::const_iterator begin, | 1232 std::vector<const SessionWindow*>::const_iterator begin, |
1226 std::vector<const SessionWindow*>::const_iterator end) { | 1233 std::vector<const SessionWindow*>::const_iterator end) { |
1227 std::vector<GURL> gurls; | 1234 std::vector<GURL> gurls; |
1228 SessionRestoreImpl restorer(profile, | 1235 SessionRestoreImpl restorer(profile, |
1229 static_cast<Browser*>(NULL), host_desktop_type, true, false, true, gurls); | 1236 static_cast<Browser*>(NULL), host_desktop_type, true, false, true, gurls); |
1230 restorer.RestoreForeignSession(begin, end); | 1237 return restorer.RestoreForeignSession(begin, end); |
1231 } | 1238 } |
1232 | 1239 |
1233 // static | 1240 // static |
1234 void SessionRestore::RestoreForeignSessionTab( | 1241 WebContents* SessionRestore::RestoreForeignSessionTab( |
1235 content::WebContents* source_web_contents, | 1242 content::WebContents* source_web_contents, |
1236 const SessionTab& tab, | 1243 const SessionTab& tab, |
1237 WindowOpenDisposition disposition) { | 1244 WindowOpenDisposition disposition) { |
1238 Browser* browser = chrome::FindBrowserWithWebContents(source_web_contents); | 1245 Browser* browser = chrome::FindBrowserWithWebContents(source_web_contents); |
1239 Profile* profile = browser->profile(); | 1246 Profile* profile = browser->profile(); |
1240 std::vector<GURL> gurls; | 1247 std::vector<GURL> gurls; |
1241 SessionRestoreImpl restorer(profile, browser, browser->host_desktop_type(), | 1248 SessionRestoreImpl restorer(profile, browser, browser->host_desktop_type(), |
1242 true, false, false, gurls); | 1249 true, false, false, gurls); |
1243 restorer.RestoreForeignTab(tab, disposition); | 1250 return restorer.RestoreForeignTab(tab, disposition); |
1244 } | 1251 } |
1245 | 1252 |
1246 // static | 1253 // static |
1247 bool SessionRestore::IsRestoring(const Profile* profile) { | 1254 bool SessionRestore::IsRestoring(const Profile* profile) { |
1248 if (active_session_restorers == NULL) | 1255 if (active_session_restorers == NULL) |
1249 return false; | 1256 return false; |
1250 for (std::set<SessionRestoreImpl*>::const_iterator it = | 1257 for (std::set<SessionRestoreImpl*>::const_iterator it = |
1251 active_session_restorers->begin(); | 1258 active_session_restorers->begin(); |
1252 it != active_session_restorers->end(); ++it) { | 1259 it != active_session_restorers->end(); ++it) { |
1253 if ((*it)->profile() == profile) | 1260 if ((*it)->profile() == profile) |
1254 return true; | 1261 return true; |
1255 } | 1262 } |
1256 return false; | 1263 return false; |
1257 } | 1264 } |
1258 | 1265 |
1259 // static | 1266 // static |
1260 bool SessionRestore::IsRestoringSynchronously() { | 1267 bool SessionRestore::IsRestoringSynchronously() { |
1261 if (!active_session_restorers) | 1268 if (!active_session_restorers) |
1262 return false; | 1269 return false; |
1263 for (std::set<SessionRestoreImpl*>::const_iterator it = | 1270 for (std::set<SessionRestoreImpl*>::const_iterator it = |
1264 active_session_restorers->begin(); | 1271 active_session_restorers->begin(); |
1265 it != active_session_restorers->end(); ++it) { | 1272 it != active_session_restorers->end(); ++it) { |
1266 if ((*it)->synchronous()) | 1273 if ((*it)->synchronous()) |
1267 return true; | 1274 return true; |
1268 } | 1275 } |
1269 return false; | 1276 return false; |
1270 } | 1277 } |
OLD | NEW |