| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/sessions/base_session_service_commands.h" |
| 6 |
| 7 #include "base/pickle.h" |
| 8 #include "chrome/browser/sessions/session_backend.h" |
| 9 #include "chrome/browser/sessions/session_types.h" |
| 10 |
| 11 // BaseSessionService --------------------------------------------------------- |
| 12 |
| 13 namespace { |
| 14 |
| 15 // Helper used by CreateUpdateTabNavigationCommand(). It writes |str| to |
| 16 // |pickle|, if and only if |str| fits within (|max_bytes| - |*bytes_written|). |
| 17 // |bytes_written| is incremented to reflect the data written. |
| 18 void WriteStringToPickle(Pickle& pickle, int* bytes_written, int max_bytes, |
| 19 const std::string& str) { |
| 20 int num_bytes = str.size() * sizeof(char); |
| 21 if (*bytes_written + num_bytes < max_bytes) { |
| 22 *bytes_written += num_bytes; |
| 23 pickle.WriteString(str); |
| 24 } else { |
| 25 pickle.WriteString(std::string()); |
| 26 } |
| 27 } |
| 28 |
| 29 } // namespace |
| 30 |
| 31 BaseSessionServiceCommands::BaseSessionServiceCommands() {} |
| 32 |
| 33 BaseSessionServiceCommands::~BaseSessionServiceCommands() { |
| 34 } |
| 35 |
| 36 SessionCommand* BaseSessionServiceCommands::CreateUpdateTabNavigationCommand( |
| 37 SessionID::id_type command_id, |
| 38 SessionID::id_type tab_id, |
| 39 const sessions::SerializedNavigationEntry& navigation) { |
| 40 // Use pickle to handle marshalling. |
| 41 Pickle pickle; |
| 42 pickle.WriteInt(tab_id); |
| 43 // We only allow navigations up to 63k (which should be completely |
| 44 // reasonable). |
| 45 static const size_t max_state_size = |
| 46 std::numeric_limits<SessionCommand::size_type>::max() - 1024; |
| 47 navigation.WriteToPickle(max_state_size, &pickle); |
| 48 return new SessionCommand(command_id, pickle); |
| 49 } |
| 50 |
| 51 SessionCommand* BaseSessionServiceCommands::CreateSetTabExtensionAppIDCommand( |
| 52 SessionID::id_type command_id, |
| 53 SessionID::id_type tab_id, |
| 54 const std::string& extension_id) { |
| 55 // Use pickle to handle marshalling. |
| 56 Pickle pickle; |
| 57 pickle.WriteInt(tab_id); |
| 58 |
| 59 // Enforce a max for ids. They should never be anywhere near this size. |
| 60 static const SessionCommand::size_type max_id_size = |
| 61 std::numeric_limits<SessionCommand::size_type>::max() - 1024; |
| 62 |
| 63 int bytes_written = 0; |
| 64 |
| 65 WriteStringToPickle(pickle, &bytes_written, max_id_size, extension_id); |
| 66 |
| 67 return new SessionCommand(command_id, pickle); |
| 68 } |
| 69 |
| 70 SessionCommand* |
| 71 BaseSessionServiceCommands::CreateSetTabUserAgentOverrideCommand( |
| 72 SessionID::id_type command_id, |
| 73 SessionID::id_type tab_id, |
| 74 const std::string& user_agent_override) { |
| 75 // Use pickle to handle marshalling. |
| 76 Pickle pickle; |
| 77 pickle.WriteInt(tab_id); |
| 78 |
| 79 // Enforce a max for the user agent length. They should never be anywhere |
| 80 // near this size. |
| 81 static const SessionCommand::size_type max_user_agent_size = |
| 82 std::numeric_limits<SessionCommand::size_type>::max() - 1024; |
| 83 |
| 84 int bytes_written = 0; |
| 85 |
| 86 WriteStringToPickle(pickle, &bytes_written, max_user_agent_size, |
| 87 user_agent_override); |
| 88 |
| 89 return new SessionCommand(command_id, pickle); |
| 90 } |
| 91 |
| 92 SessionCommand* BaseSessionServiceCommands::CreateSetWindowAppNameCommand( |
| 93 SessionID::id_type command_id, |
| 94 SessionID::id_type window_id, |
| 95 const std::string& app_name) { |
| 96 // Use pickle to handle marshalling. |
| 97 Pickle pickle; |
| 98 pickle.WriteInt(window_id); |
| 99 |
| 100 // Enforce a max for ids. They should never be anywhere near this size. |
| 101 static const SessionCommand::size_type max_id_size = |
| 102 std::numeric_limits<SessionCommand::size_type>::max() - 1024; |
| 103 |
| 104 int bytes_written = 0; |
| 105 |
| 106 WriteStringToPickle(pickle, &bytes_written, max_id_size, app_name); |
| 107 |
| 108 return new SessionCommand(command_id, pickle); |
| 109 } |
| 110 |
| 111 bool BaseSessionServiceCommands::RestoreUpdateTabNavigationCommand( |
| 112 const SessionCommand& command, |
| 113 sessions::SerializedNavigationEntry* navigation, |
| 114 SessionID::id_type* tab_id) { |
| 115 scoped_ptr<Pickle> pickle(command.PayloadAsPickle()); |
| 116 if (!pickle.get()) |
| 117 return false; |
| 118 PickleIterator iterator(*pickle); |
| 119 return |
| 120 pickle->ReadInt(&iterator, tab_id) && |
| 121 navigation->ReadFromPickle(&iterator); |
| 122 } |
| 123 |
| 124 bool BaseSessionServiceCommands::RestoreSetTabExtensionAppIDCommand( |
| 125 const SessionCommand& command, |
| 126 SessionID::id_type* tab_id, |
| 127 std::string* extension_app_id) { |
| 128 scoped_ptr<Pickle> pickle(command.PayloadAsPickle()); |
| 129 if (!pickle.get()) |
| 130 return false; |
| 131 |
| 132 PickleIterator iterator(*pickle); |
| 133 return pickle->ReadInt(&iterator, tab_id) && |
| 134 pickle->ReadString(&iterator, extension_app_id); |
| 135 } |
| 136 |
| 137 bool BaseSessionServiceCommands::RestoreSetTabUserAgentOverrideCommand( |
| 138 const SessionCommand& command, |
| 139 SessionID::id_type* tab_id, |
| 140 std::string* user_agent_override) { |
| 141 scoped_ptr<Pickle> pickle(command.PayloadAsPickle()); |
| 142 if (!pickle.get()) |
| 143 return false; |
| 144 |
| 145 PickleIterator iterator(*pickle); |
| 146 return pickle->ReadInt(&iterator, tab_id) && |
| 147 pickle->ReadString(&iterator, user_agent_override); |
| 148 } |
| 149 |
| 150 bool BaseSessionServiceCommands::RestoreSetWindowAppNameCommand( |
| 151 const SessionCommand& command, |
| 152 SessionID::id_type* window_id, |
| 153 std::string* app_name) { |
| 154 scoped_ptr<Pickle> pickle(command.PayloadAsPickle()); |
| 155 if (!pickle.get()) |
| 156 return false; |
| 157 |
| 158 PickleIterator iterator(*pickle); |
| 159 return pickle->ReadInt(&iterator, window_id) && |
| 160 pickle->ReadString(&iterator, app_name); |
| 161 } |
| OLD | NEW |