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