| 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 955 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 966 int initial_tab_count, | 966 int initial_tab_count, |
| 967 int selected_tab_index) { | 967 int selected_tab_index) { |
| 968 VLOG(1) << "RestoreTabsToBrowser " << window.tabs.size(); | 968 VLOG(1) << "RestoreTabsToBrowser " << window.tabs.size(); |
| 969 DCHECK(!window.tabs.empty()); | 969 DCHECK(!window.tabs.empty()); |
| 970 if (initial_tab_count == 0) { | 970 if (initial_tab_count == 0) { |
| 971 for (int i = 0; i < static_cast<int>(window.tabs.size()); ++i) { | 971 for (int i = 0; i < static_cast<int>(window.tabs.size()); ++i) { |
| 972 const SessionTab& tab = *(window.tabs[i]); | 972 const SessionTab& tab = *(window.tabs[i]); |
| 973 | 973 |
| 974 // Loads are scheduled for each restored tab unless the tab is going to | 974 // Loads are scheduled for each restored tab unless the tab is going to |
| 975 // be selected as ShowBrowser() will load the selected tab. | 975 // be selected as ShowBrowser() will load the selected tab. |
| 976 bool is_not_selected_tab = (i != selected_tab_index); | 976 bool is_selected_tab = (i == selected_tab_index); |
| 977 WebContents* restored_tab = | 977 WebContents* restored_tab = |
| 978 RestoreTab(tab, i, browser, is_not_selected_tab); | 978 RestoreTab(tab, i, browser, is_selected_tab); |
| 979 | 979 |
| 980 // RestoreTab can return NULL if |tab| doesn't have valid data. | 980 // RestoreTab can return NULL if |tab| doesn't have valid data. |
| 981 if (!restored_tab) | 981 if (!restored_tab) |
| 982 continue; | 982 continue; |
| 983 | 983 |
| 984 // If this isn't the selected tab, there's nothing else to do. | 984 // If this isn't the selected tab, there's nothing else to do. |
| 985 if (is_not_selected_tab) | 985 if (!is_selected_tab) |
| 986 continue; | 986 continue; |
| 987 | 987 |
| 988 ShowBrowser( | 988 ShowBrowser( |
| 989 browser, | 989 browser, |
| 990 browser->tab_strip_model()->GetIndexOfWebContents(restored_tab)); | 990 browser->tab_strip_model()->GetIndexOfWebContents(restored_tab)); |
| 991 // TODO(sky): remove. For debugging 368236. | 991 // TODO(sky): remove. For debugging 368236. |
| 992 CHECK_EQ(browser->tab_strip_model()->GetActiveWebContents(), | 992 CHECK_EQ(browser->tab_strip_model()->GetActiveWebContents(), |
| 993 restored_tab); | 993 restored_tab); |
| 994 tab_loader_->TabIsLoading(&browser->tab_strip_model() | 994 tab_loader_->TabIsLoading(&browser->tab_strip_model() |
| 995 ->GetActiveWebContents() | 995 ->GetActiveWebContents() |
| 996 ->GetController()); | 996 ->GetController()); |
| 997 } | 997 } |
| 998 } else { | 998 } else { |
| 999 // If the browser already has tabs, we want to restore the new ones after | 999 // If the browser already has tabs, we want to restore the new ones after |
| 1000 // the existing ones. E.g. this happens in Win8 Metro where we merge | 1000 // the existing ones. E.g. this happens in Win8 Metro where we merge |
| 1001 // windows or when launching a hosted app from the app launcher. | 1001 // windows or when launching a hosted app from the app launcher. |
| 1002 int tab_index_offset = initial_tab_count; | 1002 int tab_index_offset = initial_tab_count; |
| 1003 for (int i = 0; i < static_cast<int>(window.tabs.size()); ++i) { | 1003 for (int i = 0; i < static_cast<int>(window.tabs.size()); ++i) { |
| 1004 const SessionTab& tab = *(window.tabs[i]); | 1004 const SessionTab& tab = *(window.tabs[i]); |
| 1005 // Always schedule loads as we will not be calling ShowBrowser(). | 1005 // Always schedule loads as we will not be calling ShowBrowser(). |
| 1006 RestoreTab(tab, tab_index_offset + i, browser, true); | 1006 RestoreTab(tab, tab_index_offset + i, browser, false); |
| 1007 } | 1007 } |
| 1008 } | 1008 } |
| 1009 } | 1009 } |
| 1010 | 1010 |
| 1011 // |tab_index| is ignored for pinned tabs which will always be pushed behind | 1011 // |tab_index| is ignored for pinned tabs which will always be pushed behind |
| 1012 // the last existing pinned tab. | 1012 // the last existing pinned tab. |
| 1013 // |schedule_load| will let |tab_loader_| know that it should schedule this | 1013 // |tab_loader_| will schedule this tab for loading if |is_selected_tab| is |
| 1014 // tab for loading. | 1014 // false. |
| 1015 WebContents* RestoreTab(const SessionTab& tab, | 1015 WebContents* RestoreTab(const SessionTab& tab, |
| 1016 const int tab_index, | 1016 const int tab_index, |
| 1017 Browser* browser, | 1017 Browser* browser, |
| 1018 bool schedule_load) { | 1018 bool is_selected_tab) { |
| 1019 // It's possible (particularly for foreign sessions) to receive a tab | 1019 // It's possible (particularly for foreign sessions) to receive a tab |
| 1020 // without valid navigations. In that case, just skip it. | 1020 // without valid navigations. In that case, just skip it. |
| 1021 // See crbug.com/154129. | 1021 // See crbug.com/154129. |
| 1022 if (tab.navigations.empty()) | 1022 if (tab.navigations.empty()) |
| 1023 return NULL; | 1023 return NULL; |
| 1024 int selected_index = tab.current_navigation_index; | 1024 int selected_index = tab.current_navigation_index; |
| 1025 selected_index = std::max( | 1025 selected_index = std::max( |
| 1026 0, | 1026 0, |
| 1027 std::min(selected_index, | 1027 std::min(selected_index, |
| 1028 static_cast<int>(tab.navigations.size() - 1))); | 1028 static_cast<int>(tab.navigations.size() - 1))); |
| 1029 | 1029 |
| 1030 RecordAppLaunchForTab(browser, tab, selected_index); | 1030 RecordAppLaunchForTab(browser, tab, selected_index); |
| 1031 | 1031 |
| 1032 // Associate sessionStorage (if any) to the restored tab. | 1032 // Associate sessionStorage (if any) to the restored tab. |
| 1033 scoped_refptr<content::SessionStorageNamespace> session_storage_namespace; | 1033 scoped_refptr<content::SessionStorageNamespace> session_storage_namespace; |
| 1034 if (!tab.session_storage_persistent_id.empty()) { | 1034 if (!tab.session_storage_persistent_id.empty()) { |
| 1035 session_storage_namespace = | 1035 session_storage_namespace = |
| 1036 content::BrowserContext::GetDefaultStoragePartition(profile_)-> | 1036 content::BrowserContext::GetDefaultStoragePartition(profile_)-> |
| 1037 GetDOMStorageContext()->RecreateSessionStorage( | 1037 GetDOMStorageContext()->RecreateSessionStorage( |
| 1038 tab.session_storage_persistent_id); | 1038 tab.session_storage_persistent_id); |
| 1039 } | 1039 } |
| 1040 | 1040 |
| 1041 WebContents* web_contents = | 1041 WebContents* web_contents = |
| 1042 chrome::AddRestoredTab(browser, | 1042 chrome::AddRestoredTab(browser, |
| 1043 tab.navigations, | 1043 tab.navigations, |
| 1044 tab_index, | 1044 tab_index, |
| 1045 selected_index, | 1045 selected_index, |
| 1046 tab.extension_app_id, | 1046 tab.extension_app_id, |
| 1047 !schedule_load, // select | 1047 is_selected_tab, // select |
| 1048 tab.pinned, | 1048 tab.pinned, |
| 1049 true, | 1049 true, |
| 1050 session_storage_namespace.get(), | 1050 session_storage_namespace.get(), |
| 1051 tab.user_agent_override); | 1051 tab.user_agent_override); |
| 1052 // Regression check: check that the tab didn't start loading right away. The | 1052 // Regression check: check that the tab didn't start loading right away. The |
| 1053 // focused tab will be loaded by Browser, and TabLoader will load the rest. | 1053 // focused tab will be loaded by Browser, and TabLoader will load the rest. |
| 1054 DCHECK(!schedule_load || web_contents->GetController().NeedsReload()); | 1054 DCHECK(is_selected_tab || web_contents->GetController().NeedsReload()); |
| 1055 | 1055 |
| 1056 // Set up the file access rights for the selected navigation entry. | 1056 // Set up the file access rights for the selected navigation entry. |
| 1057 const int id = web_contents->GetRenderProcessHost()->GetID(); | 1057 const int id = web_contents->GetRenderProcessHost()->GetID(); |
| 1058 const content::PageState& page_state = | 1058 const content::PageState& page_state = |
| 1059 tab.navigations.at(selected_index).page_state(); | 1059 tab.navigations.at(selected_index).page_state(); |
| 1060 const std::vector<base::FilePath>& file_paths = | 1060 const std::vector<base::FilePath>& file_paths = |
| 1061 page_state.GetReferencedFiles(); | 1061 page_state.GetReferencedFiles(); |
| 1062 for (std::vector<base::FilePath>::const_iterator file = file_paths.begin(); | 1062 for (std::vector<base::FilePath>::const_iterator file = file_paths.begin(); |
| 1063 file != file_paths.end(); ++file) { | 1063 file != file_paths.end(); ++file) { |
| 1064 content::ChildProcessSecurityPolicy::GetInstance()->GrantReadFile(id, | 1064 content::ChildProcessSecurityPolicy::GetInstance()->GrantReadFile(id, |
| 1065 *file); | 1065 *file); |
| 1066 } | 1066 } |
| 1067 | 1067 |
| 1068 if (schedule_load) | 1068 if (!is_selected_tab) |
| 1069 tab_loader_->ScheduleLoad(&web_contents->GetController()); | 1069 tab_loader_->ScheduleLoad(&web_contents->GetController()); |
| 1070 return web_contents; | 1070 return web_contents; |
| 1071 } | 1071 } |
| 1072 | 1072 |
| 1073 Browser* CreateRestoredBrowser(Browser::Type type, | 1073 Browser* CreateRestoredBrowser(Browser::Type type, |
| 1074 gfx::Rect bounds, | 1074 gfx::Rect bounds, |
| 1075 ui::WindowShowState show_state, | 1075 ui::WindowShowState show_state, |
| 1076 const std::string& app_name) { | 1076 const std::string& app_name) { |
| 1077 Browser::CreateParams params(type, profile_, host_desktop_type_); | 1077 Browser::CreateParams params(type, profile_, host_desktop_type_); |
| 1078 if (!app_name.empty()) { | 1078 if (!app_name.empty()) { |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1292 if (!active_session_restorers) | 1292 if (!active_session_restorers) |
| 1293 return false; | 1293 return false; |
| 1294 for (std::set<SessionRestoreImpl*>::const_iterator it = | 1294 for (std::set<SessionRestoreImpl*>::const_iterator it = |
| 1295 active_session_restorers->begin(); | 1295 active_session_restorers->begin(); |
| 1296 it != active_session_restorers->end(); ++it) { | 1296 it != active_session_restorers->end(); ++it) { |
| 1297 if ((*it)->synchronous()) | 1297 if ((*it)->synchronous()) |
| 1298 return true; | 1298 return true; |
| 1299 } | 1299 } |
| 1300 return false; | 1300 return false; |
| 1301 } | 1301 } |
| OLD | NEW |