| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/sessions/core/session_service_commands.h" | 5 #include "components/sessions/core/session_service_commands.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 tabs->clear(); | 316 tabs->clear(); |
| 317 } | 317 } |
| 318 | 318 |
| 319 // Creates tabs and windows from the commands specified in |data|. The created | 319 // Creates tabs and windows from the commands specified in |data|. The created |
| 320 // tabs and windows are added to |tabs| and |windows| respectively, with the | 320 // tabs and windows are added to |tabs| and |windows| respectively, with the |
| 321 // id of the active window set in |active_window_id|. It is up to the caller | 321 // id of the active window set in |active_window_id|. It is up to the caller |
| 322 // to delete the tabs and windows added to |tabs| and |windows|. | 322 // to delete the tabs and windows added to |tabs| and |windows|. |
| 323 // | 323 // |
| 324 // This does NOT add any created SessionTabs to SessionWindow.tabs, that is | 324 // This does NOT add any created SessionTabs to SessionWindow.tabs, that is |
| 325 // done by AddTabsToWindows. | 325 // done by AddTabsToWindows. |
| 326 bool CreateTabsAndWindows(const ScopedVector<SessionCommand>& data, | 326 bool CreateTabsAndWindows( |
| 327 IdToSessionTab* tabs, | 327 const std::vector<std::unique_ptr<SessionCommand>>& data, |
| 328 IdToSessionWindow* windows, | 328 IdToSessionTab* tabs, |
| 329 SessionID::id_type* active_window_id) { | 329 IdToSessionWindow* windows, |
| 330 SessionID::id_type* active_window_id) { |
| 330 // If the file is corrupt (command with wrong size, or unknown command), we | 331 // If the file is corrupt (command with wrong size, or unknown command), we |
| 331 // still return true and attempt to restore what we we can. | 332 // still return true and attempt to restore what we we can. |
| 332 DVLOG(1) << "CreateTabsAndWindows"; | 333 DVLOG(1) << "CreateTabsAndWindows"; |
| 333 | 334 |
| 334 for (auto i = data.begin(); i != data.end(); ++i) { | 335 for (const auto& command_ptr : data) { |
| 335 const SessionCommand::id_type kCommandSetWindowBounds2 = 10; | 336 const SessionCommand::id_type kCommandSetWindowBounds2 = 10; |
| 336 const SessionCommand* command = *i; | 337 const SessionCommand* command = command_ptr.get(); |
| 337 | 338 |
| 338 DVLOG(1) << "Read command " << (int) command->id(); | 339 DVLOG(1) << "Read command " << (int) command->id(); |
| 339 switch (command->id()) { | 340 switch (command->id()) { |
| 340 case kCommandSetTabWindow: { | 341 case kCommandSetTabWindow: { |
| 341 SessionID::id_type payload[2]; | 342 SessionID::id_type payload[2]; |
| 342 if (!command->GetPayload(payload, sizeof(payload))) { | 343 if (!command->GetPayload(payload, sizeof(payload))) { |
| 343 DVLOG(1) << "Failed reading command " << command->id(); | 344 DVLOG(1) << "Failed reading command " << command->id(); |
| 344 return true; | 345 return true; |
| 345 } | 346 } |
| 346 GetTab(payload[1], tabs)->window_id.set_id(payload[0]); | 347 GetTab(payload[1], tabs)->window_id.set_id(payload[0]); |
| (...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 818 } | 819 } |
| 819 | 820 |
| 820 bool ReplacePendingCommand(BaseSessionService* base_session_service, | 821 bool ReplacePendingCommand(BaseSessionService* base_session_service, |
| 821 std::unique_ptr<SessionCommand>* command) { | 822 std::unique_ptr<SessionCommand>* command) { |
| 822 // We optimize page navigations, which can happen quite frequently and | 823 // We optimize page navigations, which can happen quite frequently and |
| 823 // is expensive. And activation is like Highlander, there can only be one! | 824 // is expensive. And activation is like Highlander, there can only be one! |
| 824 if ((*command)->id() != kCommandUpdateTabNavigation && | 825 if ((*command)->id() != kCommandUpdateTabNavigation && |
| 825 (*command)->id() != kCommandSetActiveWindow) { | 826 (*command)->id() != kCommandSetActiveWindow) { |
| 826 return false; | 827 return false; |
| 827 } | 828 } |
| 828 for (ScopedVector<SessionCommand>::const_reverse_iterator i = | 829 for (auto i = base_session_service->pending_commands().rbegin(); |
| 829 base_session_service->pending_commands().rbegin(); | |
| 830 i != base_session_service->pending_commands().rend(); ++i) { | 830 i != base_session_service->pending_commands().rend(); ++i) { |
| 831 SessionCommand* existing_command = *i; | 831 SessionCommand* existing_command = i->get(); |
| 832 if ((*command)->id() == kCommandUpdateTabNavigation && | 832 if ((*command)->id() == kCommandUpdateTabNavigation && |
| 833 existing_command->id() == kCommandUpdateTabNavigation) { | 833 existing_command->id() == kCommandUpdateTabNavigation) { |
| 834 std::unique_ptr<base::Pickle> command_pickle( | 834 std::unique_ptr<base::Pickle> command_pickle( |
| 835 (*command)->PayloadAsPickle()); | 835 (*command)->PayloadAsPickle()); |
| 836 base::PickleIterator iterator(*command_pickle); | 836 base::PickleIterator iterator(*command_pickle); |
| 837 SessionID::id_type command_tab_id; | 837 SessionID::id_type command_tab_id; |
| 838 int command_nav_index; | 838 int command_nav_index; |
| 839 if (!iterator.ReadInt(&command_tab_id) || | 839 if (!iterator.ReadInt(&command_tab_id) || |
| 840 !iterator.ReadInt(&command_nav_index)) { | 840 !iterator.ReadInt(&command_nav_index)) { |
| 841 return false; | 841 return false; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 852 if (!iterator.ReadInt(&existing_tab_id) || | 852 if (!iterator.ReadInt(&existing_tab_id) || |
| 853 !iterator.ReadInt(&existing_nav_index)) { | 853 !iterator.ReadInt(&existing_nav_index)) { |
| 854 return false; | 854 return false; |
| 855 } | 855 } |
| 856 } | 856 } |
| 857 if (existing_tab_id == command_tab_id && | 857 if (existing_tab_id == command_tab_id && |
| 858 existing_nav_index == command_nav_index) { | 858 existing_nav_index == command_nav_index) { |
| 859 // existing_command is an update for the same tab/index pair. Replace | 859 // existing_command is an update for the same tab/index pair. Replace |
| 860 // it with the new one. We need to add to the end of the list just in | 860 // it with the new one. We need to add to the end of the list just in |
| 861 // case there is a prune command after the update command. | 861 // case there is a prune command after the update command. |
| 862 base_session_service->EraseCommand(*(i.base() - 1)); | 862 base_session_service->EraseCommand((i.base() - 1)->get()); |
| 863 base_session_service->AppendRebuildCommand((std::move(*command))); | 863 base_session_service->AppendRebuildCommand(std::move(*command)); |
| 864 return true; | 864 return true; |
| 865 } | 865 } |
| 866 return false; | 866 return false; |
| 867 } | 867 } |
| 868 if ((*command)->id() == kCommandSetActiveWindow && | 868 if ((*command)->id() == kCommandSetActiveWindow && |
| 869 existing_command->id() == kCommandSetActiveWindow) { | 869 existing_command->id() == kCommandSetActiveWindow) { |
| 870 base_session_service->SwapCommand(existing_command, | 870 base_session_service->SwapCommand(existing_command, |
| 871 (std::move(*command))); | 871 (std::move(*command))); |
| 872 return true; | 872 return true; |
| 873 } | 873 } |
| 874 } | 874 } |
| 875 return false; | 875 return false; |
| 876 } | 876 } |
| 877 | 877 |
| 878 bool IsClosingCommand(SessionCommand* command) { | 878 bool IsClosingCommand(SessionCommand* command) { |
| 879 return command->id() == kCommandTabClosed || | 879 return command->id() == kCommandTabClosed || |
| 880 command->id() == kCommandWindowClosed; | 880 command->id() == kCommandWindowClosed; |
| 881 } | 881 } |
| 882 | 882 |
| 883 void RestoreSessionFromCommands( | 883 void RestoreSessionFromCommands( |
| 884 const ScopedVector<SessionCommand>& commands, | 884 const std::vector<std::unique_ptr<SessionCommand>>& commands, |
| 885 std::vector<std::unique_ptr<SessionWindow>>* valid_windows, | 885 std::vector<std::unique_ptr<SessionWindow>>* valid_windows, |
| 886 SessionID::id_type* active_window_id) { | 886 SessionID::id_type* active_window_id) { |
| 887 IdToSessionTab tabs; | 887 IdToSessionTab tabs; |
| 888 IdToSessionWindow windows; | 888 IdToSessionWindow windows; |
| 889 | 889 |
| 890 DVLOG(1) << "RestoreSessionFromCommands " << commands.size(); | 890 DVLOG(1) << "RestoreSessionFromCommands " << commands.size(); |
| 891 if (CreateTabsAndWindows(commands, &tabs, &windows, active_window_id)) { | 891 if (CreateTabsAndWindows(commands, &tabs, &windows, active_window_id)) { |
| 892 AddTabsToWindows(&tabs, &windows); | 892 AddTabsToWindows(&tabs, &windows); |
| 893 SortTabsBasedOnVisualOrderAndClear(&windows, valid_windows); | 893 SortTabsBasedOnVisualOrderAndClear(&windows, valid_windows); |
| 894 UpdateSelectedTabIndex(valid_windows); | 894 UpdateSelectedTabIndex(valid_windows); |
| 895 } | 895 } |
| 896 // AddTabsToWindows should have processed all the tabs. | 896 // AddTabsToWindows should have processed all the tabs. |
| 897 DCHECK_EQ(0u, tabs.size()); | 897 DCHECK_EQ(0u, tabs.size()); |
| 898 // SortTabsBasedOnVisualOrderAndClear should have processed all the windows. | 898 // SortTabsBasedOnVisualOrderAndClear should have processed all the windows. |
| 899 DCHECK_EQ(0u, windows.size()); | 899 DCHECK_EQ(0u, windows.size()); |
| 900 } | 900 } |
| 901 | 901 |
| 902 } // namespace sessions | 902 } // namespace sessions |
| OLD | NEW |