| 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_service.h" | 5 #include "chrome/browser/sessions/session_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 #include "chrome/browser/ui/browser_window.h" | 30 #include "chrome/browser/ui/browser_window.h" |
| 31 #include "chrome/browser/ui/startup/startup_browser_creator.h" | 31 #include "chrome/browser/ui/startup/startup_browser_creator.h" |
| 32 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 32 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 33 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 33 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 34 #include "chrome/common/chrome_notification_types.h" | 34 #include "chrome/common/chrome_notification_types.h" |
| 35 #include "chrome/common/extensions/extension.h" | 35 #include "chrome/common/extensions/extension.h" |
| 36 #include "content/public/browser/navigation_details.h" | 36 #include "content/public/browser/navigation_details.h" |
| 37 #include "content/public/browser/navigation_entry.h" | 37 #include "content/public/browser/navigation_entry.h" |
| 38 #include "content/public/browser/notification_details.h" | 38 #include "content/public/browser/notification_details.h" |
| 39 #include "content/public/browser/notification_service.h" | 39 #include "content/public/browser/notification_service.h" |
| 40 #include "content/public/browser/session_storage_namespace.h" |
| 40 #include "content/public/browser/web_contents.h" | 41 #include "content/public/browser/web_contents.h" |
| 41 | 42 |
| 42 #if defined(OS_MACOSX) | 43 #if defined(OS_MACOSX) |
| 43 #include "chrome/browser/app_controller_mac.h" | 44 #include "chrome/browser/app_controller_mac.h" |
| 44 #endif | 45 #endif |
| 45 | 46 |
| 46 using base::Time; | 47 using base::Time; |
| 47 using content::NavigationEntry; | 48 using content::NavigationEntry; |
| 48 | 49 |
| 49 // Identifier for commands written to file. | 50 // Identifier for commands written to file. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 65 // static const SessionCommand::id_type kCommandSetWindowBounds2 = 10; | 66 // static const SessionCommand::id_type kCommandSetWindowBounds2 = 10; |
| 66 static const SessionCommand::id_type | 67 static const SessionCommand::id_type |
| 67 kCommandTabNavigationPathPrunedFromFront = 11; | 68 kCommandTabNavigationPathPrunedFromFront = 11; |
| 68 static const SessionCommand::id_type kCommandSetPinnedState = 12; | 69 static const SessionCommand::id_type kCommandSetPinnedState = 12; |
| 69 static const SessionCommand::id_type kCommandSetExtensionAppID = 13; | 70 static const SessionCommand::id_type kCommandSetExtensionAppID = 13; |
| 70 static const SessionCommand::id_type kCommandSetWindowBounds3 = 14; | 71 static const SessionCommand::id_type kCommandSetWindowBounds3 = 14; |
| 71 static const SessionCommand::id_type kCommandSetWindowAppName = 15; | 72 static const SessionCommand::id_type kCommandSetWindowAppName = 15; |
| 72 static const SessionCommand::id_type kCommandTabClosed = 16; | 73 static const SessionCommand::id_type kCommandTabClosed = 16; |
| 73 static const SessionCommand::id_type kCommandWindowClosed = 17; | 74 static const SessionCommand::id_type kCommandWindowClosed = 17; |
| 74 static const SessionCommand::id_type kCommandSetTabUserAgentOverride = 18; | 75 static const SessionCommand::id_type kCommandSetTabUserAgentOverride = 18; |
| 76 static const SessionCommand::id_type kCommandSessionStorageAssociated = 19; |
| 75 | 77 |
| 76 // Every kWritesPerReset commands triggers recreating the file. | 78 // Every kWritesPerReset commands triggers recreating the file. |
| 77 static const int kWritesPerReset = 250; | 79 static const int kWritesPerReset = 250; |
| 78 | 80 |
| 79 namespace { | 81 namespace { |
| 80 | 82 |
| 81 // The callback from GetLastSession is internally routed to SessionService | 83 // The callback from GetLastSession is internally routed to SessionService |
| 82 // first and then the caller. This is done so that the SessionWindows can be | 84 // first and then the caller. This is done so that the SessionWindows can be |
| 83 // recreated from the SessionCommands and the SessionWindows passed to the | 85 // recreated from the SessionCommands and the SessionWindows passed to the |
| 84 // caller. The following class is used for this. | 86 // caller. The following class is used for this. |
| (...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 592 if (tab->profile() != profile()) | 594 if (tab->profile() != profile()) |
| 593 return; | 595 return; |
| 594 SetTabWindow(tab->restore_tab_helper()->window_id(), | 596 SetTabWindow(tab->restore_tab_helper()->window_id(), |
| 595 tab->restore_tab_helper()->session_id()); | 597 tab->restore_tab_helper()->session_id()); |
| 596 if (tab->extension_tab_helper()->extension_app()) { | 598 if (tab->extension_tab_helper()->extension_app()) { |
| 597 SetTabExtensionAppID( | 599 SetTabExtensionAppID( |
| 598 tab->restore_tab_helper()->window_id(), | 600 tab->restore_tab_helper()->window_id(), |
| 599 tab->restore_tab_helper()->session_id(), | 601 tab->restore_tab_helper()->session_id(), |
| 600 tab->extension_tab_helper()->extension_app()->id()); | 602 tab->extension_tab_helper()->extension_app()->id()); |
| 601 } | 603 } |
| 604 |
| 605 // Record the association between the SessionStorageNamespace and the |
| 606 // tab. |
| 607 content::SessionStorageNamespace* session_storage_namespace = |
| 608 tab->web_contents()->GetController().GetSessionStorageNamespace(); |
| 609 ScheduleCommand(CreateSessionStorageAssociatedCommand( |
| 610 tab->restore_tab_helper()->session_id(), |
| 611 session_storage_namespace->persistent_id())); |
| 612 session_storage_namespace->SetShouldPersist(true); |
| 602 break; | 613 break; |
| 603 } | 614 } |
| 604 | 615 |
| 605 case chrome::NOTIFICATION_TAB_CONTENTS_DESTROYED: { | 616 case chrome::NOTIFICATION_TAB_CONTENTS_DESTROYED: { |
| 606 TabContents* tab = content::Source<TabContents>(source).ptr(); | 617 TabContents* tab = content::Source<TabContents>(source).ptr(); |
| 607 if (!tab || tab->profile() != profile()) | 618 if (!tab || tab->profile() != profile()) |
| 608 return; | 619 return; |
| 620 // Allow the associated sessionStorage to get deleted; it won't be needed |
| 621 // in the session restore. |
| 622 content::SessionStorageNamespace* session_storage_namespace = |
| 623 tab->web_contents()->GetController().GetSessionStorageNamespace(); |
| 624 session_storage_namespace->SetShouldPersist(false); |
| 609 TabClosed(tab->restore_tab_helper()->window_id(), | 625 TabClosed(tab->restore_tab_helper()->window_id(), |
| 610 tab->restore_tab_helper()->session_id(), | 626 tab->restore_tab_helper()->session_id(), |
| 611 tab->web_contents()->GetClosedByUserGesture()); | 627 tab->web_contents()->GetClosedByUserGesture()); |
| 612 RecordSessionUpdateHistogramData(type, &last_updated_tab_closed_time_); | 628 RecordSessionUpdateHistogramData(type, &last_updated_tab_closed_time_); |
| 613 break; | 629 break; |
| 614 } | 630 } |
| 615 | 631 |
| 616 case content::NOTIFICATION_NAV_LIST_PRUNED: { | 632 case content::NOTIFICATION_NAV_LIST_PRUNED: { |
| 617 TabContents* tab = TabContents::FromWebContents( | 633 TabContents* tab = TabContents::FromWebContents( |
| 618 content::Source<content::NavigationController>(source).ptr()-> | 634 content::Source<content::NavigationController>(source).ptr()-> |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 831 bool is_pinned) { | 847 bool is_pinned) { |
| 832 PinnedStatePayload payload = { 0 }; | 848 PinnedStatePayload payload = { 0 }; |
| 833 payload.tab_id = tab_id.id(); | 849 payload.tab_id = tab_id.id(); |
| 834 payload.pinned_state = is_pinned; | 850 payload.pinned_state = is_pinned; |
| 835 SessionCommand* command = | 851 SessionCommand* command = |
| 836 new SessionCommand(kCommandSetPinnedState, sizeof(payload)); | 852 new SessionCommand(kCommandSetPinnedState, sizeof(payload)); |
| 837 memcpy(command->contents(), &payload, sizeof(payload)); | 853 memcpy(command->contents(), &payload, sizeof(payload)); |
| 838 return command; | 854 return command; |
| 839 } | 855 } |
| 840 | 856 |
| 857 SessionCommand* SessionService::CreateSessionStorageAssociatedCommand( |
| 858 const SessionID& tab_id, |
| 859 const std::string& session_storage_persistent_id) { |
| 860 Pickle pickle; |
| 861 pickle.WriteInt(tab_id.id()); |
| 862 pickle.WriteString(session_storage_persistent_id); |
| 863 return new SessionCommand(kCommandSessionStorageAssociated, pickle); |
| 864 } |
| 865 |
| 841 void SessionService::OnGotSessionCommands( | 866 void SessionService::OnGotSessionCommands( |
| 842 Handle handle, | 867 Handle handle, |
| 843 scoped_refptr<InternalGetCommandsRequest> request) { | 868 scoped_refptr<InternalGetCommandsRequest> request) { |
| 844 if (request->canceled()) | 869 if (request->canceled()) |
| 845 return; | 870 return; |
| 846 | 871 |
| 847 ScopedVector<SessionWindow> valid_windows; | 872 ScopedVector<SessionWindow> valid_windows; |
| 848 RestoreSessionFromCommands( | 873 RestoreSessionFromCommands( |
| 849 request->commands, &(valid_windows.get())); | 874 request->commands, &(valid_windows.get())); |
| 850 static_cast<InternalSessionRequest*>(request.get())-> | 875 static_cast<InternalSessionRequest*>(request.get())-> |
| (...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1227 std::string user_agent_override; | 1252 std::string user_agent_override; |
| 1228 if (!RestoreSetTabUserAgentOverrideCommand( | 1253 if (!RestoreSetTabUserAgentOverrideCommand( |
| 1229 *command, &tab_id, &user_agent_override)) { | 1254 *command, &tab_id, &user_agent_override)) { |
| 1230 return true; | 1255 return true; |
| 1231 } | 1256 } |
| 1232 | 1257 |
| 1233 GetTab(tab_id, tabs)->user_agent_override.swap(user_agent_override); | 1258 GetTab(tab_id, tabs)->user_agent_override.swap(user_agent_override); |
| 1234 break; | 1259 break; |
| 1235 } | 1260 } |
| 1236 | 1261 |
| 1262 case kCommandSessionStorageAssociated: { |
| 1263 scoped_ptr<Pickle> command_pickle(command->PayloadAsPickle()); |
| 1264 SessionID::id_type command_tab_id; |
| 1265 std::string session_storage_persistent_id; |
| 1266 PickleIterator iter(*command_pickle.get()); |
| 1267 if (!command_pickle->ReadInt(&iter, &command_tab_id) || |
| 1268 !command_pickle->ReadString(&iter, &session_storage_persistent_id)) |
| 1269 return false; |
| 1270 // Associate the session storage back. |
| 1271 GetTab(command_tab_id, tabs)->session_storage_persistent_id = |
| 1272 session_storage_persistent_id; |
| 1273 break; |
| 1274 } |
| 1275 |
| 1237 default: | 1276 default: |
| 1238 VLOG(1) << "Failed reading an unknown command " << command->id(); | 1277 VLOG(1) << "Failed reading an unknown command " << command->id(); |
| 1239 return true; | 1278 return true; |
| 1240 } | 1279 } |
| 1241 } | 1280 } |
| 1242 return true; | 1281 return true; |
| 1243 } | 1282 } |
| 1244 | 1283 |
| 1245 void SessionService::BuildCommandsForTab( | 1284 void SessionService::BuildCommandsForTab( |
| 1246 const SessionID& window_id, | 1285 const SessionID& window_id, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1296 kCommandUpdateTabNavigation, session_id.id(), i, *entry)); | 1335 kCommandUpdateTabNavigation, session_id.id(), i, *entry)); |
| 1297 } | 1336 } |
| 1298 } | 1337 } |
| 1299 commands->push_back( | 1338 commands->push_back( |
| 1300 CreateSetSelectedNavigationIndexCommand(session_id, current_index)); | 1339 CreateSetSelectedNavigationIndexCommand(session_id, current_index)); |
| 1301 | 1340 |
| 1302 if (index_in_window != -1) { | 1341 if (index_in_window != -1) { |
| 1303 commands->push_back( | 1342 commands->push_back( |
| 1304 CreateSetTabIndexInWindowCommand(session_id, index_in_window)); | 1343 CreateSetTabIndexInWindowCommand(session_id, index_in_window)); |
| 1305 } | 1344 } |
| 1345 |
| 1346 // Record the association between the sessionStorage namespace and the tab. |
| 1347 content::SessionStorageNamespace* session_storage_namespace = |
| 1348 tab->web_contents()->GetController().GetSessionStorageNamespace(); |
| 1349 ScheduleCommand(CreateSessionStorageAssociatedCommand( |
| 1350 tab->restore_tab_helper()->session_id(), |
| 1351 session_storage_namespace->persistent_id())); |
| 1306 } | 1352 } |
| 1307 | 1353 |
| 1308 void SessionService::BuildCommandsForBrowser( | 1354 void SessionService::BuildCommandsForBrowser( |
| 1309 Browser* browser, | 1355 Browser* browser, |
| 1310 std::vector<SessionCommand*>* commands, | 1356 std::vector<SessionCommand*>* commands, |
| 1311 IdToRange* tab_to_available_range, | 1357 IdToRange* tab_to_available_range, |
| 1312 std::set<SessionID::id_type>* windows_to_track) { | 1358 std::set<SessionID::id_type>* windows_to_track) { |
| 1313 DCHECK(browser && commands); | 1359 DCHECK(browser && commands); |
| 1314 DCHECK(browser->session_id().id()); | 1360 DCHECK(browser->session_id().id()); |
| 1315 | 1361 |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1678 50); | 1724 50); |
| 1679 if (use_long_period) { | 1725 if (use_long_period) { |
| 1680 std::string long_name_("SessionRestore.SaveLongPeriod"); | 1726 std::string long_name_("SessionRestore.SaveLongPeriod"); |
| 1681 UMA_HISTOGRAM_CUSTOM_TIMES(long_name_, | 1727 UMA_HISTOGRAM_CUSTOM_TIMES(long_name_, |
| 1682 delta, | 1728 delta, |
| 1683 save_delay_in_mins_, | 1729 save_delay_in_mins_, |
| 1684 save_delay_in_hrs_, | 1730 save_delay_in_hrs_, |
| 1685 50); | 1731 50); |
| 1686 } | 1732 } |
| 1687 } | 1733 } |
| OLD | NEW |