| 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 "chrome/browser/sessions/session_service_commands.h" | 5 #include "chrome/browser/sessions/session_service_commands.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/pickle.h" | 9 #include "base/pickle.h" |
| 10 #include "chrome/browser/sessions/base_session_service_commands.h" | 10 #include "chrome/browser/sessions/base_session_service_commands.h" |
| (...skipping 737 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 748 } | 748 } |
| 749 | 749 |
| 750 scoped_ptr<SessionCommand> CreateSetWindowAppNameCommand( | 750 scoped_ptr<SessionCommand> CreateSetWindowAppNameCommand( |
| 751 const SessionID& window_id, | 751 const SessionID& window_id, |
| 752 const std::string& app_name) { | 752 const std::string& app_name) { |
| 753 return CreateSetWindowAppNameCommand(kCommandSetWindowAppName, | 753 return CreateSetWindowAppNameCommand(kCommandSetWindowAppName, |
| 754 window_id.id(), | 754 window_id.id(), |
| 755 app_name); | 755 app_name); |
| 756 } | 756 } |
| 757 | 757 |
| 758 bool ReplacePendingCommand(ScopedVector<SessionCommand>& pending_commands, | 758 bool ReplacePendingCommand(BaseSessionService* base_session_service, |
| 759 scoped_ptr<SessionCommand>* command) { | 759 scoped_ptr<SessionCommand>* command) { |
| 760 // We optimize page navigations, which can happen quite frequently and | 760 // We optimize page navigations, which can happen quite frequently and |
| 761 // is expensive. And activation is like Highlander, there can only be one! | 761 // is expensive. And activation is like Highlander, there can only be one! |
| 762 if ((*command)->id() != kCommandUpdateTabNavigation && | 762 if ((*command)->id() != kCommandUpdateTabNavigation && |
| 763 (*command)->id() != kCommandSetActiveWindow) { | 763 (*command)->id() != kCommandSetActiveWindow) { |
| 764 return false; | 764 return false; |
| 765 } | 765 } |
| 766 for (ScopedVector<SessionCommand>::reverse_iterator i = | 766 for (ScopedVector<SessionCommand>::const_reverse_iterator i = |
| 767 pending_commands.rbegin(); i != pending_commands.rend(); ++i) { | 767 base_session_service->pending_commands().rbegin(); |
| 768 i != base_session_service->pending_commands().rend(); ++i) { |
| 768 SessionCommand* existing_command = *i; | 769 SessionCommand* existing_command = *i; |
| 769 if ((*command)->id() == kCommandUpdateTabNavigation && | 770 if ((*command)->id() == kCommandUpdateTabNavigation && |
| 770 existing_command->id() == kCommandUpdateTabNavigation) { | 771 existing_command->id() == kCommandUpdateTabNavigation) { |
| 771 scoped_ptr<Pickle> command_pickle((*command)->PayloadAsPickle()); | 772 scoped_ptr<Pickle> command_pickle((*command)->PayloadAsPickle()); |
| 772 PickleIterator iterator(*command_pickle); | 773 PickleIterator iterator(*command_pickle); |
| 773 SessionID::id_type command_tab_id; | 774 SessionID::id_type command_tab_id; |
| 774 int command_nav_index; | 775 int command_nav_index; |
| 775 if (!command_pickle->ReadInt(&iterator, &command_tab_id) || | 776 if (!command_pickle->ReadInt(&iterator, &command_tab_id) || |
| 776 !command_pickle->ReadInt(&iterator, &command_nav_index)) { | 777 !command_pickle->ReadInt(&iterator, &command_nav_index)) { |
| 777 return false; | 778 return false; |
| 778 } | 779 } |
| 779 SessionID::id_type existing_tab_id; | 780 SessionID::id_type existing_tab_id; |
| 780 int existing_nav_index; | 781 int existing_nav_index; |
| 781 { | 782 { |
| 782 // Creating a pickle like this means the Pickle references the data from | 783 // Creating a pickle like this means the Pickle references the data from |
| 783 // the command. Make sure we delete the pickle before the command, else | 784 // the command. Make sure we delete the pickle before the command, else |
| 784 // the pickle references deleted memory. | 785 // the pickle references deleted memory. |
| 785 scoped_ptr<Pickle> existing_pickle(existing_command->PayloadAsPickle()); | 786 scoped_ptr<Pickle> existing_pickle(existing_command->PayloadAsPickle()); |
| 786 iterator = PickleIterator(*existing_pickle); | 787 iterator = PickleIterator(*existing_pickle); |
| 787 if (!existing_pickle->ReadInt(&iterator, &existing_tab_id) || | 788 if (!existing_pickle->ReadInt(&iterator, &existing_tab_id) || |
| 788 !existing_pickle->ReadInt(&iterator, &existing_nav_index)) { | 789 !existing_pickle->ReadInt(&iterator, &existing_nav_index)) { |
| 789 return false; | 790 return false; |
| 790 } | 791 } |
| 791 } | 792 } |
| 792 if (existing_tab_id == command_tab_id && | 793 if (existing_tab_id == command_tab_id && |
| 793 existing_nav_index == command_nav_index) { | 794 existing_nav_index == command_nav_index) { |
| 794 // existing_command is an update for the same tab/index pair. Replace | 795 // existing_command is an update for the same tab/index pair. Replace |
| 795 // it with the new one. We need to add to the end of the list just in | 796 // it with the new one. We need to add to the end of the list just in |
| 796 // case there is a prune command after the update command. | 797 // case there is a prune command after the update command. |
| 797 // Note: ScopedVector::erase will also delete the element. | 798 base_session_service->EraseCommand( |
| 798 pending_commands.erase(i.base() - 1); | 799 scoped_ptr<SessionCommand>(*(i.base() - 1)).Pass()); |
| 799 pending_commands.push_back((*command).release()); | 800 base_session_service->AppendRebuildCommand((*command).Pass()); |
| 800 return true; | 801 return true; |
| 801 } | 802 } |
| 802 return false; | 803 return false; |
| 803 } | 804 } |
| 804 if ((*command)->id() == kCommandSetActiveWindow && | 805 if ((*command)->id() == kCommandSetActiveWindow && |
| 805 existing_command->id() == kCommandSetActiveWindow) { | 806 existing_command->id() == kCommandSetActiveWindow) { |
| 806 *i = (*command).release(); | 807 base_session_service->SwapCommand( |
| 807 delete existing_command; | 808 scoped_ptr<SessionCommand>(existing_command).Pass(), |
| 809 (*command).Pass()); |
| 808 return true; | 810 return true; |
| 809 } | 811 } |
| 810 } | 812 } |
| 811 return false; | 813 return false; |
| 812 } | 814 } |
| 813 | 815 |
| 814 bool IsClosingCommand(SessionCommand* command) { | 816 bool IsClosingCommand(SessionCommand* command) { |
| 815 return command->id() == kCommandTabClosed || | 817 return command->id() == kCommandTabClosed || |
| 816 command->id() == kCommandWindowClosed; | 818 command->id() == kCommandWindowClosed; |
| 817 } | 819 } |
| 818 | 820 |
| 819 void RestoreSessionFromCommands(const ScopedVector<SessionCommand>& commands, | 821 void RestoreSessionFromCommands(const ScopedVector<SessionCommand>& commands, |
| 820 std::vector<SessionWindow*>* valid_windows, | 822 std::vector<SessionWindow*>* valid_windows, |
| 821 SessionID::id_type* active_window_id) { | 823 SessionID::id_type* active_window_id) { |
| 822 std::map<int, SessionTab*> tabs; | 824 std::map<int, SessionTab*> tabs; |
| 823 std::map<int, SessionWindow*> windows; | 825 std::map<int, SessionWindow*> windows; |
| 824 | 826 |
| 825 VLOG(1) << "RestoreSessionFromCommands " << commands.size(); | 827 VLOG(1) << "RestoreSessionFromCommands " << commands.size(); |
| 826 if (CreateTabsAndWindows(commands, &tabs, &windows, active_window_id)) { | 828 if (CreateTabsAndWindows(commands, &tabs, &windows, active_window_id)) { |
| 827 AddTabsToWindows(&tabs, &windows); | 829 AddTabsToWindows(&tabs, &windows); |
| 828 SortTabsBasedOnVisualOrderAndPrune(&windows, valid_windows); | 830 SortTabsBasedOnVisualOrderAndPrune(&windows, valid_windows); |
| 829 UpdateSelectedTabIndex(valid_windows); | 831 UpdateSelectedTabIndex(valid_windows); |
| 830 } | 832 } |
| 831 STLDeleteValues(&tabs); | 833 STLDeleteValues(&tabs); |
| 832 // Don't delete contents of windows, that is done by the caller as all | 834 // Don't delete contents of windows, that is done by the caller as all |
| 833 // valid windows are added to valid_windows. | 835 // valid windows are added to valid_windows. |
| 834 } | 836 } |
| OLD | NEW |