1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/sessions/core/base_session_service_commands.h"
#include <stddef.h>
#include <memory>
#include "base/pickle.h"
#include "components/sessions/core/command_storage_backend.h"
#include "components/sessions/core/session_types.h"
namespace sessions {
namespace {
// Helper used by CreateUpdateTabNavigationCommand(). It writes |str| to
// |pickle|, if and only if |str| fits within (|max_bytes| - |*bytes_written|).
// |bytes_written| is incremented to reflect the data written.
void WriteStringToPickle(base::Pickle& pickle,
int* bytes_written,
int max_bytes,
const std::string& str) {
int num_bytes = str.size() * sizeof(char);
if (*bytes_written + num_bytes < max_bytes) {
*bytes_written += num_bytes;
pickle.WriteString(str);
} else {
pickle.WriteString(std::string());
}
}
bool ReadSessionIdFromPickle(base::PickleIterator* iterator, SessionID* id) {
SessionID::id_type value;
if (!iterator->ReadInt(&value)) {
return false;
}
*id = SessionID::FromSerializedValue(value);
return true;
}
} // namespace
std::unique_ptr<SessionCommand> CreateUpdateTabNavigationCommand(
SessionCommand::id_type command_id,
SessionID tab_id,
const sessions::SerializedNavigationEntry& navigation) {
// Use pickle to handle marshalling.
base::Pickle pickle;
pickle.WriteInt(tab_id.id());
// We only allow navigations up to 63k (which should be completely
// reasonable).
static const size_t max_state_size =
std::numeric_limits<SessionCommand::size_type>::max() - 1024;
navigation.WriteToPickle(max_state_size, &pickle);
return std::make_unique<SessionCommand>(command_id, pickle);
}
std::unique_ptr<SessionCommand> CreateSetTabExtensionAppIDCommand(
SessionCommand::id_type command_id,
SessionID tab_id,
const std::string& extension_id) {
// Use pickle to handle marshalling.
base::Pickle pickle;
pickle.WriteInt(tab_id.id());
// Enforce a max for ids. They should never be anywhere near this size.
static const SessionCommand::size_type max_id_size =
std::numeric_limits<SessionCommand::size_type>::max() - 1024;
int bytes_written = 0;
WriteStringToPickle(pickle, &bytes_written, max_id_size, extension_id);
return std::make_unique<SessionCommand>(command_id, pickle);
}
std::unique_ptr<SessionCommand> CreateSetTabUserAgentOverrideCommand(
SessionCommand::id_type command_id,
SessionID tab_id,
const SerializedUserAgentOverride& user_agent_override) {
// Use pickle to handle marshalling.
base::Pickle pickle;
pickle.WriteInt(tab_id.id());
// Enforce a max for the user agent length. They should never be anywhere
// near this size.
static const SessionCommand::size_type max_user_agent_size =
(std::numeric_limits<SessionCommand::size_type>::max() - 1024) / 2;
int bytes_written = 0;
WriteStringToPickle(pickle, &bytes_written, max_user_agent_size,
user_agent_override.ua_string_override);
bool have_usable_ua_metadata_override =
user_agent_override.opaque_ua_metadata_override.has_value() &&
user_agent_override.opaque_ua_metadata_override->size() <
max_user_agent_size;
pickle.WriteBool(have_usable_ua_metadata_override);
if (have_usable_ua_metadata_override) {
bytes_written = 0;
WriteStringToPickle(
pickle, &bytes_written, max_user_agent_size,
user_agent_override.opaque_ua_metadata_override.value());
}
return std::make_unique<SessionCommand>(command_id, pickle);
}
std::unique_ptr<SessionCommand> CreateSetWindowAppNameCommand(
SessionCommand::id_type command_id,
SessionID window_id,
const std::string& app_name) {
// Use pickle to handle marshalling.
base::Pickle pickle;
pickle.WriteInt(window_id.id());
// Enforce a max for ids. They should never be anywhere near this size.
static const SessionCommand::size_type max_id_size =
std::numeric_limits<SessionCommand::size_type>::max() - 1024;
int bytes_written = 0;
WriteStringToPickle(pickle, &bytes_written, max_id_size, app_name);
return std::make_unique<SessionCommand>(command_id, pickle);
}
std::unique_ptr<SessionCommand> CreateSetWindowUserTitleCommand(
SessionCommand::id_type command_id,
SessionID window_id,
const std::string& user_title) {
// Use pickle to handle marshalling.
base::Pickle pickle;
pickle.WriteInt(window_id.id());
// Enforce a max for ids. They should never be anywhere near this size.
static const SessionCommand::size_type max_id_size =
std::numeric_limits<SessionCommand::size_type>::max() - 1024;
int bytes_written = 0;
WriteStringToPickle(pickle, &bytes_written, max_id_size, user_title);
return std::make_unique<SessionCommand>(command_id, pickle);
}
std::unique_ptr<SessionCommand> CreateAddExtraDataCommand(
SessionCommand::id_type command,
SessionID session_id,
const std::string& key,
const std::string& data) {
base::Pickle pickle;
pickle.WriteInt(session_id.id());
// Enforce a max for ids. They should never be anywhere near this size.
static const SessionCommand::size_type max_id_size =
std::numeric_limits<SessionCommand::size_type>::max() - 1024;
int total_bytes_written = 0;
WriteStringToPickle(pickle, &total_bytes_written, max_id_size, key);
WriteStringToPickle(pickle, &total_bytes_written, max_id_size, data);
return std::make_unique<SessionCommand>(command, pickle);
}
std::unique_ptr<SessionCommand> CreateSetPlatformSessionIdCommand(
SessionCommand::id_type command_id,
const std::string& platform_session_id) {
base::Pickle pickle;
static const SessionCommand::size_type max_id_size =
std::numeric_limits<SessionCommand::size_type>::max() - 1024;
int bytes_written = 0;
WriteStringToPickle(pickle, &bytes_written, max_id_size, platform_session_id);
return std::make_unique<SessionCommand>(command_id, pickle);
}
bool RestoreUpdateTabNavigationCommand(
const SessionCommand& command,
sessions::SerializedNavigationEntry* navigation,
SessionID* tab_id) {
base::Pickle pickle = command.PayloadAsPickle();
base::PickleIterator iterator(pickle);
return ReadSessionIdFromPickle(&iterator, tab_id) &&
navigation->ReadFromPickle(&iterator);
}
bool RestoreSetTabExtensionAppIDCommand(const SessionCommand& command,
SessionID* tab_id,
std::string* extension_app_id) {
base::Pickle pickle = command.PayloadAsPickle();
base::PickleIterator iterator(pickle);
return ReadSessionIdFromPickle(&iterator, tab_id) &&
iterator.ReadString(extension_app_id);
}
bool RestoreSetTabUserAgentOverrideCommand(const SessionCommand& command,
SessionID* tab_id,
std::string* user_agent_override) {
base::Pickle pickle = command.PayloadAsPickle();
base::PickleIterator iterator(pickle);
return ReadSessionIdFromPickle(&iterator, tab_id) &&
iterator.ReadString(user_agent_override);
}
bool RestoreSetTabUserAgentOverrideCommand2(
const SessionCommand& command,
SessionID* tab_id,
std::string* user_agent_override,
std::optional<std::string>* opaque_ua_metadata_override) {
base::Pickle pickle = command.PayloadAsPickle();
base::PickleIterator iterator(pickle);
if (!ReadSessionIdFromPickle(&iterator, tab_id)) {
return false;
}
if (!iterator.ReadString(user_agent_override)) {
return false;
}
// See if there is UA metadata override.
bool has_ua_metadata_override;
if (!iterator.ReadBool(&has_ua_metadata_override)) {
return false;
}
if (!has_ua_metadata_override) {
*opaque_ua_metadata_override = std::nullopt;
return true;
}
std::string ua_metadata_override_value;
if (!iterator.ReadString(&ua_metadata_override_value)) {
return false;
}
*opaque_ua_metadata_override = std::move(ua_metadata_override_value);
return true;
}
bool RestoreSetWindowAppNameCommand(const SessionCommand& command,
SessionID* window_id,
std::string* app_name) {
base::Pickle pickle = command.PayloadAsPickle();
base::PickleIterator iterator(pickle);
return ReadSessionIdFromPickle(&iterator, window_id) &&
iterator.ReadString(app_name);
}
bool RestoreSetWindowUserTitleCommand(const SessionCommand& command,
SessionID* window_id,
std::string* user_title) {
base::Pickle pickle = command.PayloadAsPickle();
base::PickleIterator iterator(pickle);
return ReadSessionIdFromPickle(&iterator, window_id) &&
iterator.ReadString(user_title);
}
bool RestoreAddExtraDataCommand(const SessionCommand& command,
SessionID* session_id,
std::string* key,
std::string* data) {
base::Pickle pickle = command.PayloadAsPickle();
base::PickleIterator it(pickle);
return ReadSessionIdFromPickle(&it, session_id) && it.ReadString(key) &&
it.ReadString(data);
}
bool RestoreSetPlatformSessionIdCommand(const SessionCommand& command,
std::string* platform_session_id) {
base::Pickle pickle = command.PayloadAsPickle();
base::PickleIterator iterator(pickle);
return iterator.ReadString(platform_session_id);
}
} // namespace sessions
|