blob: 73f51c90ad684b78851acdf646cfb2b8b89e3caf [file] [log] [blame]
[email protected]a7c03d4f32012-01-24 02:36:051// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]946d1b22009-07-22 23:57:212// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ipc/ipc_message_utils.h"
6
avi246998d82015-12-22 02:39:047#include <stddef.h>
8#include <stdint.h>
9
[email protected]57999812013-02-24 05:40:5210#include "base/files/file_path.h"
[email protected]93d49d72009-10-23 20:00:2011#include "base/json/json_writer.h"
Hans Wennborg24397592020-06-17 16:58:5012#include "base/logging.h"
jdoerrief1e72e32017-04-26 16:23:5513#include "base/memory/ptr_util.h"
Hans Wennborg5f62968b2021-04-22 18:44:0514#include "base/notreached.h"
[email protected]4aa794a12013-06-11 06:32:1815#include "base/strings/string_number_conversions.h"
Lei Zhange02299a2021-04-26 23:12:2416#include "base/strings/stringprintf.h"
[email protected]906265872013-06-07 22:40:4517#include "base/strings/utf_string_conversions.h"
[email protected]b43e5562013-06-28 15:20:0218#include "base/time/time.h"
tguilbert4a5ac602016-09-19 21:11:2519#include "base/unguessable_token.h"
[email protected]946d1b22009-07-22 23:57:2120#include "base/values.h"
avi246998d82015-12-22 02:39:0421#include "build/build_config.h"
[email protected]bf5aedf02012-06-04 21:18:2522#include "ipc/ipc_channel_handle.h"
Lei Zhang5226db72022-01-25 03:38:5523#include "ipc/ipc_message.h"
morrita1aa788c2015-01-31 05:45:4224#include "ipc/ipc_message_attachment.h"
morrita4b5c28e22015-01-14 21:17:0625#include "ipc/ipc_message_attachment_set.h"
amistry36182522016-06-27 06:34:4226#include "ipc/ipc_mojo_param_traits.h"
[email protected]bf5aedf02012-06-04 21:18:2527
Xiaohan Wangab909b32022-01-12 17:57:3928#if BUILDFLAG(IS_MAC)
erikchenaf8299d2015-10-09 19:12:0629#include "ipc/mach_port_mac.h"
30#endif
31
Xiaohan Wangab909b32022-01-12 17:57:3932#if BUILDFLAG(IS_WIN)
[email protected]2e02cfe82012-11-21 00:58:0033#include <tchar.h>
erikchen5ea2ab72015-09-25 22:34:3134#include "ipc/handle_win.h"
erikchend804e1052017-04-29 02:24:3635#include "ipc/ipc_platform_file.h"
Xiaohan Wangab909b32022-01-12 17:57:3936#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
Fabrice de Gans-Ribericbce4342018-05-07 20:02:0937#include "base/file_descriptor_posix.h"
38#include "ipc/ipc_platform_file_attachment_posix.h"
[email protected]7a4de7a62010-08-17 18:38:2439#endif
[email protected]946d1b22009-07-22 23:57:2140
Xiaohan Wangab909b32022-01-12 17:57:3941#if BUILDFLAG(IS_FUCHSIA)
Sergey Ulanov04367c72019-06-21 15:51:4342#include "base/fuchsia/fuchsia_logging.h"
Sergey Ulanova97f3282019-04-17 01:27:3443#include "ipc/handle_attachment_fuchsia.h"
Scott Graham3eebff02017-06-30 01:07:1044#endif
45
Xiaohan Wangab909b32022-01-12 17:57:3946#if BUILDFLAG(IS_ANDROID)
Ken Rockot097248f02018-04-23 16:23:3447#include "base/android/scoped_hardware_buffer_handle.h"
48#include "ipc/ipc_mojo_handle_attachment.h"
49#include "mojo/public/cpp/system/message_pipe.h"
50#include "mojo/public/cpp/system/scope_to_message_pipe.h"
51#endif
52
[email protected]946d1b22009-07-22 23:57:2153namespace IPC {
54
[email protected]bf5aedf02012-06-04 21:18:2555namespace {
56
joaodasilva383b174a2017-01-10 09:55:3657const int kMaxRecursionDepth = 200;
[email protected]946d1b22009-07-22 23:57:2158
[email protected]bf5aedf02012-06-04 21:18:2559template<typename CharType>
60void LogBytes(const std::vector<CharType>& data, std::string* out) {
Xiaohan Wangab909b32022-01-12 17:57:3961#if BUILDFLAG(IS_WIN)
[email protected]bf5aedf02012-06-04 21:18:2562 // Windows has a GUI for logging, which can handle arbitrary binary data.
63 for (size_t i = 0; i < data.size(); ++i)
64 out->push_back(data[i]);
Xiaohan Wangab909b32022-01-12 17:57:3965#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
[email protected]bf5aedf02012-06-04 21:18:2566 // On POSIX, we log to stdout, which we assume can display ASCII.
67 static const size_t kMaxBytesToLog = 100;
68 for (size_t i = 0; i < std::min(data.size(), kMaxBytesToLog); ++i) {
69 if (isprint(data[i]))
70 out->push_back(data[i]);
71 else
[email protected]7d3cbc92013-03-18 22:33:0472 out->append(
73 base::StringPrintf("[%02X]", static_cast<unsigned char>(data[i])));
[email protected]bf5aedf02012-06-04 21:18:2574 }
75 if (data.size() > kMaxBytesToLog) {
[email protected]f8660f82013-03-30 17:29:2876 out->append(base::StringPrintf(
77 " and %u more bytes",
78 static_cast<unsigned>(data.size() - kMaxBytesToLog)));
[email protected]bf5aedf02012-06-04 21:18:2579 }
80#endif
81}
[email protected]946d1b22009-07-22 23:57:2182
rockot502c94f2016-02-03 20:20:1683bool ReadValue(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:2584 base::PickleIterator* iter,
Jeremy Roman2d8d7802020-06-11 22:08:2085 base::Value* value,
[email protected]bf5aedf02012-06-04 21:18:2586 int recursion);
[email protected]946d1b22009-07-22 23:57:2187
rockot502c94f2016-02-03 20:20:1688void WriteValue(base::Pickle* m, const base::Value* value, int recursion) {
[email protected]dbc761a2012-07-26 01:29:2189 bool result;
[email protected]946d1b22009-07-22 23:57:2190 if (recursion > kMaxRecursionDepth) {
joaodasilva383b174a2017-01-10 09:55:3691 LOG(ERROR) << "Max recursion depth hit in WriteValue.";
[email protected]946d1b22009-07-22 23:57:2192 return;
93 }
94
jdoerrie76cee9c2017-10-06 22:42:4295 m->WriteInt(static_cast<int>(value->type()));
[email protected]946d1b22009-07-22 23:57:2196
jdoerrie76cee9c2017-10-06 22:42:4297 switch (value->type()) {
jdoerriedc72ee942016-12-07 15:43:2898 case base::Value::Type::NONE:
jdoerriee1b1f3a2019-03-16 04:08:0199 break;
jdoerriedc72ee942016-12-07 15:43:28100 case base::Value::Type::BOOLEAN: {
Ayu Ishii68ff2262021-04-21 01:17:40101 WriteParam(m, value->GetBool());
[email protected]946d1b22009-07-22 23:57:21102 break;
103 }
jdoerriedc72ee942016-12-07 15:43:28104 case base::Value::Type::INTEGER: {
Minoru Chikamune330d3372021-04-17 02:28:19105 DCHECK(value->is_int());
106 WriteParam(m, value->GetInt());
[email protected]946d1b22009-07-22 23:57:21107 break;
108 }
jdoerriedc72ee942016-12-07 15:43:28109 case base::Value::Type::DOUBLE: {
Maksim Sadymb2681942021-07-07 16:55:24110 DCHECK(value->is_int() || value->is_double());
111 WriteParam(m, value->GetDouble());
[email protected]946d1b22009-07-22 23:57:21112 break;
113 }
jdoerriedc72ee942016-12-07 15:43:28114 case base::Value::Type::STRING: {
Sammie Quondab84eba2021-08-18 02:34:48115 const std::string* val = value->GetIfString();
116 result = !!val;
[email protected]dbc761a2012-07-26 01:29:21117 DCHECK(result);
Sammie Quondab84eba2021-08-18 02:34:48118 WriteParam(m, *val);
[email protected]946d1b22009-07-22 23:57:21119 break;
120 }
jdoerriedc72ee942016-12-07 15:43:28121 case base::Value::Type::BINARY: {
jdoerrie9970f20e2018-07-20 21:41:18122 m->WriteData(reinterpret_cast<const char*>(value->GetBlob().data()),
Chris Palmerc5ea9b92017-09-25 22:53:22123 base::checked_cast<int>(value->GetBlob().size()));
[email protected]e4dad9fb2009-10-06 18:15:58124 break;
[email protected]946d1b22009-07-22 23:57:21125 }
jdoerriedc72ee942016-12-07 15:43:28126 case base::Value::Type::DICTIONARY: {
Weiliang Chen7395b2c2022-01-15 01:17:29127 DCHECK(value->is_dict());
128 WriteParam(m, base::checked_cast<int>(value->DictSize()));
[email protected]946d1b22009-07-22 23:57:21129
Weiliang Chen7395b2c2022-01-15 01:17:29130 for (auto it : value->DictItems()) {
131 WriteParam(m, it.first);
132 WriteValue(m, &it.second, recursion + 1);
[email protected]946d1b22009-07-22 23:57:21133 }
134 break;
135 }
jdoerriedc72ee942016-12-07 15:43:28136 case base::Value::Type::LIST: {
[email protected]ea5ef4c2013-06-13 22:50:27137 const base::ListValue* list = static_cast<const base::ListValue*>(value);
Clark DuValle5638f002021-09-02 00:57:26138 WriteParam(m, base::checked_cast<int>(list->GetList().size()));
David Bertoni7018c4002021-05-12 21:40:19139 for (const auto& entry : list->GetList()) {
jdoerriea5676c62017-04-11 18:09:14140 WriteValue(m, &entry, recursion + 1);
[email protected]946d1b22009-07-22 23:57:21141 }
142 break;
143 }
144 }
145}
146
147// Helper for ReadValue that reads a DictionaryValue into a pre-allocated
148// object.
rockot502c94f2016-02-03 20:20:16149bool ReadDictionaryValue(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25150 base::PickleIterator* iter,
151 base::DictionaryValue* value,
152 int recursion) {
[email protected]946d1b22009-07-22 23:57:21153 int size;
154 if (!ReadParam(m, iter, &size))
155 return false;
156
Jan Wilken Dörrief961a372020-11-02 20:30:34157 std::vector<base::Value::LegacyDictStorage::value_type> entries;
Jeremy Roman2d8d7802020-06-11 22:08:20158 entries.resize(size);
159 for (auto& entry : entries) {
160 entry.second = std::make_unique<base::Value>();
161 if (!ReadParam(m, iter, &entry.first) ||
162 !ReadValue(m, iter, entry.second.get(), recursion + 1))
[email protected]946d1b22009-07-22 23:57:21163 return false;
[email protected]946d1b22009-07-22 23:57:21164 }
165
Jan Wilken Dörrief961a372020-11-02 20:30:34166 *value =
167 base::DictionaryValue(base::Value::LegacyDictStorage(std::move(entries)));
[email protected]946d1b22009-07-22 23:57:21168 return true;
169}
170
171// Helper for ReadValue that reads a ReadListValue into a pre-allocated
172// object.
rockot502c94f2016-02-03 20:20:16173bool ReadListValue(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25174 base::PickleIterator* iter,
175 base::ListValue* value,
176 int recursion) {
[email protected]946d1b22009-07-22 23:57:21177 int size;
178 if (!ReadParam(m, iter, &size))
179 return false;
180
Jeremy Roman2d8d7802020-06-11 22:08:20181 base::Value::ListStorage list_storage;
182 list_storage.resize(size);
183 for (base::Value& subval : list_storage) {
[email protected]946d1b22009-07-22 23:57:21184 if (!ReadValue(m, iter, &subval, recursion + 1))
185 return false;
[email protected]946d1b22009-07-22 23:57:21186 }
Jeremy Roman2d8d7802020-06-11 22:08:20187 *value = base::ListValue(std::move(list_storage));
[email protected]946d1b22009-07-22 23:57:21188 return true;
189}
190
rockot502c94f2016-02-03 20:20:16191bool ReadValue(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25192 base::PickleIterator* iter,
Jeremy Roman2d8d7802020-06-11 22:08:20193 base::Value* value,
[email protected]bf5aedf02012-06-04 21:18:25194 int recursion) {
[email protected]946d1b22009-07-22 23:57:21195 if (recursion > kMaxRecursionDepth) {
joaodasilva383b174a2017-01-10 09:55:36196 LOG(ERROR) << "Max recursion depth hit in ReadValue.";
[email protected]946d1b22009-07-22 23:57:21197 return false;
198 }
199
200 int type;
201 if (!ReadParam(m, iter, &type))
202 return false;
203
Devlin Cronin7178a5bd2021-02-02 02:56:47204 constexpr int kMinValueType = static_cast<int>(base::Value::Type::NONE);
205 constexpr int kMaxValueType = static_cast<int>(base::Value::Type::LIST);
206 if (type > kMaxValueType || type < kMinValueType)
207 return false;
208
jdoerriedc72ee942016-12-07 15:43:28209 switch (static_cast<base::Value::Type>(type)) {
210 case base::Value::Type::NONE:
Jeremy Roman2d8d7802020-06-11 22:08:20211 *value = base::Value();
jdoerriee067999a2017-04-07 06:39:00212 break;
jdoerriedc72ee942016-12-07 15:43:28213 case base::Value::Type::BOOLEAN: {
[email protected]946d1b22009-07-22 23:57:21214 bool val;
215 if (!ReadParam(m, iter, &val))
216 return false;
Jeremy Roman2d8d7802020-06-11 22:08:20217 *value = base::Value(val);
[email protected]946d1b22009-07-22 23:57:21218 break;
219 }
jdoerriedc72ee942016-12-07 15:43:28220 case base::Value::Type::INTEGER: {
[email protected]946d1b22009-07-22 23:57:21221 int val;
222 if (!ReadParam(m, iter, &val))
223 return false;
Jeremy Roman2d8d7802020-06-11 22:08:20224 *value = base::Value(val);
[email protected]946d1b22009-07-22 23:57:21225 break;
226 }
jdoerriedc72ee942016-12-07 15:43:28227 case base::Value::Type::DOUBLE: {
[email protected]946d1b22009-07-22 23:57:21228 double val;
229 if (!ReadParam(m, iter, &val))
230 return false;
Jeremy Roman2d8d7802020-06-11 22:08:20231 *value = base::Value(val);
[email protected]946d1b22009-07-22 23:57:21232 break;
233 }
jdoerriedc72ee942016-12-07 15:43:28234 case base::Value::Type::STRING: {
[email protected]946d1b22009-07-22 23:57:21235 std::string val;
236 if (!ReadParam(m, iter, &val))
237 return false;
Jeremy Roman2d8d7802020-06-11 22:08:20238 *value = base::Value(std::move(val));
[email protected]946d1b22009-07-22 23:57:21239 break;
240 }
jdoerriedc72ee942016-12-07 15:43:28241 case base::Value::Type::BINARY: {
Jeremy Roman2d8d7802020-06-11 22:08:20242 base::span<const uint8_t> data;
243 if (!iter->ReadData(&data))
[email protected]e4dad9fb2009-10-06 18:15:58244 return false;
Jeremy Roman2d8d7802020-06-11 22:08:20245 *value = base::Value(data);
[email protected]946d1b22009-07-22 23:57:21246 break;
247 }
jdoerriedc72ee942016-12-07 15:43:28248 case base::Value::Type::DICTIONARY: {
jdoerrief1e72e32017-04-26 16:23:55249 base::DictionaryValue val;
250 if (!ReadDictionaryValue(m, iter, &val, recursion))
[email protected]946d1b22009-07-22 23:57:21251 return false;
Jeremy Roman2d8d7802020-06-11 22:08:20252 *value = std::move(val);
[email protected]946d1b22009-07-22 23:57:21253 break;
254 }
jdoerriedc72ee942016-12-07 15:43:28255 case base::Value::Type::LIST: {
jdoerrief1e72e32017-04-26 16:23:55256 base::ListValue val;
257 if (!ReadListValue(m, iter, &val, recursion))
[email protected]946d1b22009-07-22 23:57:21258 return false;
Jeremy Roman2d8d7802020-06-11 22:08:20259 *value = std::move(val);
[email protected]946d1b22009-07-22 23:57:21260 break;
261 }
[email protected]e4dad9fb2009-10-06 18:15:58262 default:
Jan Wilken Dörrie271086b42019-06-13 20:03:41263 NOTREACHED();
jdoerriee1b1f3a2019-03-16 04:08:01264 return false;
[email protected]946d1b22009-07-22 23:57:21265 }
266
267 return true;
268}
269
[email protected]bf5aedf02012-06-04 21:18:25270} // namespace
271
272// -----------------------------------------------------------------------------
273
274LogData::LogData()
275 : routing_id(0),
276 type(0),
277 sent(0),
278 receive(0),
279 dispatch(0) {
280}
281
vmpstrbf0d713a2016-03-24 20:22:54282LogData::LogData(const LogData& other) = default;
283
Chris Watkins2d879af2017-11-30 02:11:59284LogData::~LogData() = default;
[email protected]bf5aedf02012-06-04 21:18:25285
[email protected]bf5aedf02012-06-04 21:18:25286void ParamTraits<bool>::Log(const param_type& p, std::string* l) {
287 l->append(p ? "true" : "false");
288}
289
rockot502c94f2016-02-03 20:20:16290void ParamTraits<signed char>::Write(base::Pickle* m, const param_type& p) {
ortuno19ecf1842015-10-30 00:46:20291 m->WriteBytes(&p, sizeof(param_type));
292}
293
rockot502c94f2016-02-03 20:20:16294bool ParamTraits<signed char>::Read(const base::Pickle* m,
295 base::PickleIterator* iter,
296 param_type* r) {
ortuno19ecf1842015-10-30 00:46:20297 const char* data;
298 if (!iter->ReadBytes(&data, sizeof(param_type)))
299 return false;
300 memcpy(r, data, sizeof(param_type));
301 return true;
302}
303
304void ParamTraits<signed char>::Log(const param_type& p, std::string* l) {
Raul Tambref89a5102019-02-08 23:01:38305 l->append(base::NumberToString(p));
ortuno19ecf1842015-10-30 00:46:20306}
307
rockot502c94f2016-02-03 20:20:16308void ParamTraits<unsigned char>::Write(base::Pickle* m, const param_type& p) {
[email protected]c1ee48d2013-07-12 23:12:28309 m->WriteBytes(&p, sizeof(param_type));
310}
311
rockot502c94f2016-02-03 20:20:16312bool ParamTraits<unsigned char>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25313 base::PickleIterator* iter,
314 param_type* r) {
[email protected]c1ee48d2013-07-12 23:12:28315 const char* data;
avi48fc13b2014-12-28 23:31:48316 if (!iter->ReadBytes(&data, sizeof(param_type)))
[email protected]c1ee48d2013-07-12 23:12:28317 return false;
318 memcpy(r, data, sizeof(param_type));
319 return true;
320}
321
322void ParamTraits<unsigned char>::Log(const param_type& p, std::string* l) {
Raul Tambref89a5102019-02-08 23:01:38323 l->append(base::NumberToString(p));
[email protected]c1ee48d2013-07-12 23:12:28324}
325
rockot502c94f2016-02-03 20:20:16326void ParamTraits<unsigned short>::Write(base::Pickle* m, const param_type& p) {
[email protected]c1ee48d2013-07-12 23:12:28327 m->WriteBytes(&p, sizeof(param_type));
328}
329
rockot502c94f2016-02-03 20:20:16330bool ParamTraits<unsigned short>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25331 base::PickleIterator* iter,
[email protected]c1ee48d2013-07-12 23:12:28332 param_type* r) {
333 const char* data;
avi48fc13b2014-12-28 23:31:48334 if (!iter->ReadBytes(&data, sizeof(param_type)))
[email protected]c1ee48d2013-07-12 23:12:28335 return false;
336 memcpy(r, data, sizeof(param_type));
337 return true;
338}
339
340void ParamTraits<unsigned short>::Log(const param_type& p, std::string* l) {
Daniel Cheng3d199b12017-12-12 03:51:09341 l->append(base::NumberToString(p));
[email protected]c1ee48d2013-07-12 23:12:28342}
343
[email protected]252cad62010-08-18 18:33:57344void ParamTraits<int>::Log(const param_type& p, std::string* l) {
Daniel Cheng3d199b12017-12-12 03:51:09345 l->append(base::NumberToString(p));
[email protected]252cad62010-08-18 18:33:57346}
347
348void ParamTraits<unsigned int>::Log(const param_type& p, std::string* l) {
Daniel Cheng3d199b12017-12-12 03:51:09349 l->append(base::NumberToString(p));
[email protected]252cad62010-08-18 18:33:57350}
351
Xiaohan Wangab909b32022-01-12 17:57:39352#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
353 BUILDFLAG(IS_FUCHSIA) || \
354 (BUILDFLAG(IS_ANDROID) && defined(ARCH_CPU_64_BITS))
[email protected]252cad62010-08-18 18:33:57355void ParamTraits<long>::Log(const param_type& p, std::string* l) {
Daniel Cheng3d199b12017-12-12 03:51:09356 l->append(base::NumberToString(p));
[email protected]252cad62010-08-18 18:33:57357}
358
359void ParamTraits<unsigned long>::Log(const param_type& p, std::string* l) {
Daniel Cheng3d199b12017-12-12 03:51:09360 l->append(base::NumberToString(p));
[email protected]252cad62010-08-18 18:33:57361}
jam03d8a782016-02-10 20:13:39362#endif
[email protected]252cad62010-08-18 18:33:57363
364void ParamTraits<long long>::Log(const param_type& p, std::string* l) {
Daniel Cheng3d199b12017-12-12 03:51:09365 l->append(base::NumberToString(p));
[email protected]252cad62010-08-18 18:33:57366}
367
368void ParamTraits<unsigned long long>::Log(const param_type& p, std::string* l) {
Daniel Cheng3d199b12017-12-12 03:51:09369 l->append(base::NumberToString(p));
[email protected]252cad62010-08-18 18:33:57370}
[email protected]7a4de7a62010-08-17 18:38:24371
[email protected]bf5aedf02012-06-04 21:18:25372void ParamTraits<float>::Log(const param_type& p, std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:04373 l->append(base::StringPrintf("%e", p));
[email protected]7a4de7a62010-08-17 18:38:24374}
375
rockot502c94f2016-02-03 20:20:16376void ParamTraits<double>::Write(base::Pickle* m, const param_type& p) {
[email protected]48328ff2013-10-31 09:27:31377 m->WriteBytes(reinterpret_cast<const char*>(&p), sizeof(param_type));
[email protected]d84e48b2010-10-21 22:04:52378}
379
rockot502c94f2016-02-03 20:20:16380bool ParamTraits<double>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25381 base::PickleIterator* iter,
[email protected]bf5aedf02012-06-04 21:18:25382 param_type* r) {
383 const char *data;
avi48fc13b2014-12-28 23:31:48384 if (!iter->ReadBytes(&data, sizeof(*r))) {
[email protected]bf5aedf02012-06-04 21:18:25385 NOTREACHED();
386 return false;
387 }
388 memcpy(r, data, sizeof(param_type));
389 return true;
[email protected]d84e48b2010-10-21 22:04:52390}
391
[email protected]bf5aedf02012-06-04 21:18:25392void ParamTraits<double>::Log(const param_type& p, std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:04393 l->append(base::StringPrintf("%e", p));
[email protected]1d14f582011-09-02 20:42:04394}
395
[email protected]bf5aedf02012-06-04 21:18:25396
397void ParamTraits<std::string>::Log(const param_type& p, std::string* l) {
398 l->append(p);
[email protected]1d14f582011-09-02 20:42:04399}
400
Jan Wilken Dörrie739ccc212021-03-11 18:13:05401void ParamTraits<std::u16string>::Log(const param_type& p, std::string* l) {
[email protected]ad65a3e2013-12-25 18:18:01402 l->append(base::UTF16ToUTF8(p));
[email protected]bf5aedf02012-06-04 21:18:25403}
[email protected]bf5aedf02012-06-04 21:18:25404
Xiaohan Wangab909b32022-01-12 17:57:39405#if BUILDFLAG(IS_WIN)
Jan Wilken Dörriecf5a66902020-12-07 20:51:31406bool ParamTraits<std::wstring>::Read(const base::Pickle* m,
407 base::PickleIterator* iter,
408 param_type* r) {
409 base::StringPiece16 piece16;
410 if (!iter->ReadStringPiece16(&piece16))
411 return false;
412
413 *r = base::AsWString(piece16);
414 return true;
415}
416
417void ParamTraits<std::wstring>::Log(const param_type& p, std::string* l) {
418 l->append(base::WideToUTF8(p));
419}
420#endif
421
rockot502c94f2016-02-03 20:20:16422void ParamTraits<std::vector<char>>::Write(base::Pickle* m,
423 const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:25424 if (p.empty()) {
425 m->WriteData(NULL, 0);
426 } else {
Chris Palmerc5ea9b92017-09-25 22:53:22427 m->WriteData(&p.front(), base::checked_cast<int>(p.size()));
[email protected]bf5aedf02012-06-04 21:18:25428 }
429}
430
rockot502c94f2016-02-03 20:20:16431bool ParamTraits<std::vector<char>>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25432 base::PickleIterator* iter,
433 param_type* r) {
[email protected]bf5aedf02012-06-04 21:18:25434 const char *data;
435 int data_size = 0;
avi48fc13b2014-12-28 23:31:48436 if (!iter->ReadData(&data, &data_size) || data_size < 0)
[email protected]bf5aedf02012-06-04 21:18:25437 return false;
438 r->resize(data_size);
439 if (data_size)
440 memcpy(&r->front(), data, data_size);
441 return true;
442}
443
444void ParamTraits<std::vector<char> >::Log(const param_type& p, std::string* l) {
445 LogBytes(p, l);
446}
447
rockot502c94f2016-02-03 20:20:16448void ParamTraits<std::vector<unsigned char>>::Write(base::Pickle* m,
449 const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:25450 if (p.empty()) {
451 m->WriteData(NULL, 0);
452 } else {
453 m->WriteData(reinterpret_cast<const char*>(&p.front()),
Chris Palmerc5ea9b92017-09-25 22:53:22454 base::checked_cast<int>(p.size()));
[email protected]bf5aedf02012-06-04 21:18:25455 }
456}
457
rockot502c94f2016-02-03 20:20:16458bool ParamTraits<std::vector<unsigned char>>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25459 base::PickleIterator* iter,
460 param_type* r) {
[email protected]bf5aedf02012-06-04 21:18:25461 const char *data;
462 int data_size = 0;
avi48fc13b2014-12-28 23:31:48463 if (!iter->ReadData(&data, &data_size) || data_size < 0)
[email protected]bf5aedf02012-06-04 21:18:25464 return false;
465 r->resize(data_size);
466 if (data_size)
467 memcpy(&r->front(), data, data_size);
468 return true;
469}
470
471void ParamTraits<std::vector<unsigned char> >::Log(const param_type& p,
472 std::string* l) {
473 LogBytes(p, l);
474}
475
rockot502c94f2016-02-03 20:20:16476void ParamTraits<std::vector<bool>>::Write(base::Pickle* m,
477 const param_type& p) {
Chris Palmerc5ea9b92017-09-25 22:53:22478 WriteParam(m, base::checked_cast<int>(p.size()));
[email protected]d4124852013-03-20 20:25:00479 // Cast to bool below is required because libc++'s
480 // vector<bool>::const_reference is different from bool, and we want to avoid
481 // writing an extra specialization of ParamTraits for it.
[email protected]bf5aedf02012-06-04 21:18:25482 for (size_t i = 0; i < p.size(); i++)
[email protected]d4124852013-03-20 20:25:00483 WriteParam(m, static_cast<bool>(p[i]));
[email protected]bf5aedf02012-06-04 21:18:25484}
485
rockot502c94f2016-02-03 20:20:16486bool ParamTraits<std::vector<bool>>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25487 base::PickleIterator* iter,
488 param_type* r) {
[email protected]bf5aedf02012-06-04 21:18:25489 int size;
490 // ReadLength() checks for < 0 itself.
avi48fc13b2014-12-28 23:31:48491 if (!iter->ReadLength(&size))
[email protected]bf5aedf02012-06-04 21:18:25492 return false;
493 r->resize(size);
494 for (int i = 0; i < size; i++) {
495 bool value;
496 if (!ReadParam(m, iter, &value))
497 return false;
498 (*r)[i] = value;
499 }
500 return true;
501}
502
503void ParamTraits<std::vector<bool> >::Log(const param_type& p, std::string* l) {
504 for (size_t i = 0; i < p.size(); ++i) {
505 if (i != 0)
506 l->push_back(' ');
[email protected]d4124852013-03-20 20:25:00507 LogParam(static_cast<bool>(p[i]), l);
[email protected]bf5aedf02012-06-04 21:18:25508 }
[email protected]d84e48b2010-10-21 22:04:52509}
510
rockot502c94f2016-02-03 20:20:16511void ParamTraits<base::DictionaryValue>::Write(base::Pickle* m,
[email protected]ea5ef4c2013-06-13 22:50:27512 const param_type& p) {
[email protected]946d1b22009-07-22 23:57:21513 WriteValue(m, &p, 0);
514}
515
rockot502c94f2016-02-03 20:20:16516bool ParamTraits<base::DictionaryValue>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25517 base::PickleIterator* iter,
518 param_type* r) {
[email protected]946d1b22009-07-22 23:57:21519 int type;
jdoerriedc72ee942016-12-07 15:43:28520 if (!ReadParam(m, iter, &type) ||
521 type != static_cast<int>(base::Value::Type::DICTIONARY))
[email protected]946d1b22009-07-22 23:57:21522 return false;
523
524 return ReadDictionaryValue(m, iter, r, 0);
525}
526
[email protected]ea5ef4c2013-06-13 22:50:27527void ParamTraits<base::DictionaryValue>::Log(const param_type& p,
528 std::string* l) {
[email protected]946d1b22009-07-22 23:57:21529 std::string json;
estade8d046462015-05-16 01:02:34530 base::JSONWriter::Write(p, &json);
[email protected]252cad62010-08-18 18:33:57531 l->append(json);
[email protected]946d1b22009-07-22 23:57:21532}
533
Xiaohan Wangab909b32022-01-12 17:57:39534#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
rockot502c94f2016-02-03 20:20:16535void ParamTraits<base::FileDescriptor>::Write(base::Pickle* m,
536 const param_type& p) {
erikchen14525202017-05-06 19:16:51537 // This serialization must be kept in sync with
erikchen9d6afd712017-05-18 17:49:06538 // nacl_message_scanner.cc:WriteHandle().
[email protected]7a4de7a62010-08-17 18:38:24539 const bool valid = p.fd >= 0;
540 WriteParam(m, valid);
541
morrita96693852014-09-24 20:11:45542 if (!valid)
543 return;
544
545 if (p.auto_close) {
morrita1aa788c2015-01-31 05:45:42546 if (!m->WriteAttachment(
547 new internal::PlatformFileAttachment(base::ScopedFD(p.fd))))
morrita96693852014-09-24 20:11:45548 NOTREACHED();
549 } else {
morrita1aa788c2015-01-31 05:45:42550 if (!m->WriteAttachment(new internal::PlatformFileAttachment(p.fd)))
[email protected]7a4de7a62010-08-17 18:38:24551 NOTREACHED();
552 }
553}
554
rockot502c94f2016-02-03 20:20:16555bool ParamTraits<base::FileDescriptor>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25556 base::PickleIterator* iter,
[email protected]7a4de7a62010-08-17 18:38:24557 param_type* r) {
morrita96693852014-09-24 20:11:45558 *r = base::FileDescriptor();
559
[email protected]7a4de7a62010-08-17 18:38:24560 bool valid;
561 if (!ReadParam(m, iter, &valid))
562 return false;
563
morrita96693852014-09-24 20:11:45564 if (!valid)
[email protected]7a4de7a62010-08-17 18:38:24565 return true;
[email protected]7a4de7a62010-08-17 18:38:24566
rockot502c94f2016-02-03 20:20:16567 scoped_refptr<base::Pickle::Attachment> attachment;
morrita1aa788c2015-01-31 05:45:42568 if (!m->ReadAttachment(iter, &attachment))
morrita96693852014-09-24 20:11:45569 return false;
570
sammc6ed3efb2016-11-23 03:17:35571 if (static_cast<MessageAttachment*>(attachment.get())->GetType() !=
572 MessageAttachment::Type::PLATFORM_FILE) {
573 return false;
574 }
575
rockot502c94f2016-02-03 20:20:16576 *r = base::FileDescriptor(
sammc6ed3efb2016-11-23 03:17:35577 static_cast<internal::PlatformFileAttachment*>(attachment.get())
578 ->TakePlatformFile(),
rockot502c94f2016-02-03 20:20:16579 true);
morrita96693852014-09-24 20:11:45580 return true;
[email protected]7a4de7a62010-08-17 18:38:24581}
582
583void ParamTraits<base::FileDescriptor>::Log(const param_type& p,
[email protected]252cad62010-08-18 18:33:57584 std::string* l) {
[email protected]7a4de7a62010-08-17 18:38:24585 if (p.auto_close) {
[email protected]7d3cbc92013-03-18 22:33:04586 l->append(base::StringPrintf("FD(%d auto-close)", p.fd));
[email protected]7a4de7a62010-08-17 18:38:24587 } else {
[email protected]7d3cbc92013-03-18 22:33:04588 l->append(base::StringPrintf("FD(%d)", p.fd));
[email protected]7a4de7a62010-08-17 18:38:24589 }
590}
Sergey Ulanovac89bb92019-03-22 18:46:49591
592void ParamTraits<base::ScopedFD>::Write(base::Pickle* m, const param_type& p) {
593 // This serialization must be kept in sync with
594 // nacl_message_scanner.cc:WriteHandle().
595 const bool valid = p.is_valid();
596 WriteParam(m, valid);
597
598 if (!valid)
599 return;
600
601 if (!m->WriteAttachment(new internal::PlatformFileAttachment(
602 std::move(const_cast<param_type&>(p))))) {
603 NOTREACHED();
604 }
605}
606
607bool ParamTraits<base::ScopedFD>::Read(const base::Pickle* m,
608 base::PickleIterator* iter,
609 param_type* r) {
610 r->reset();
611
612 bool valid;
613 if (!ReadParam(m, iter, &valid))
614 return false;
615
616 if (!valid)
617 return true;
618
619 scoped_refptr<base::Pickle::Attachment> attachment;
620 if (!m->ReadAttachment(iter, &attachment))
621 return false;
622
623 if (static_cast<MessageAttachment*>(attachment.get())->GetType() !=
624 MessageAttachment::Type::PLATFORM_FILE) {
625 return false;
626 }
627
628 *r = base::ScopedFD(
629 static_cast<internal::PlatformFileAttachment*>(attachment.get())
630 ->TakePlatformFile());
631 return true;
632}
633
634void ParamTraits<base::ScopedFD>::Log(const param_type& p, std::string* l) {
635 l->append(base::StringPrintf("ScopedFD(%d)", p.get()));
636}
Xiaohan Wangab909b32022-01-12 17:57:39637#endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
[email protected]7a4de7a62010-08-17 18:38:24638
Xiaohan Wangab909b32022-01-12 17:57:39639#if BUILDFLAG(IS_WIN)
Robert Sesek02910662020-02-28 20:15:51640void ParamTraits<base::win::ScopedHandle>::Write(base::Pickle* m,
641 const param_type& p) {
642 const bool valid = p.IsValid();
643 WriteParam(m, valid);
644 if (!valid)
645 return;
646
647 HandleWin handle(p.Get());
648 WriteParam(m, handle);
649}
650
651bool ParamTraits<base::win::ScopedHandle>::Read(const base::Pickle* m,
652 base::PickleIterator* iter,
653 param_type* r) {
654 r->Close();
655
656 bool valid;
657 if (!ReadParam(m, iter, &valid))
658 return false;
659 if (!valid)
660 return true;
661
662 HandleWin handle;
663 if (!ReadParam(m, iter, &handle))
664 return false;
665
666 r->Set(handle.get_handle());
667 return true;
668}
669
670void ParamTraits<base::win::ScopedHandle>::Log(const param_type& p,
671 std::string* l) {
672 l->append(base::StringPrintf("ScopedHandle(%p)", p.Get()));
673}
Xiaohan Wangab909b32022-01-12 17:57:39674#endif // BUILDFLAG(IS_WIN)
Robert Sesek02910662020-02-28 20:15:51675
Xiaohan Wangab909b32022-01-12 17:57:39676#if BUILDFLAG(IS_FUCHSIA)
Sergey Ulanova97f3282019-04-17 01:27:34677void ParamTraits<zx::vmo>::Write(base::Pickle* m, const param_type& p) {
678 // This serialization must be kept in sync with
679 // nacl_message_scanner.cc:WriteHandle().
680 const bool valid = p.is_valid();
681 WriteParam(m, valid);
682
683 if (!valid)
684 return;
685
686 if (!m->WriteAttachment(new internal::HandleAttachmentFuchsia(
Sergey Ulanov04367c72019-06-21 15:51:43687 std::move(const_cast<param_type&>(p))))) {
Sergey Ulanova97f3282019-04-17 01:27:34688 NOTREACHED();
689 }
690}
691
692bool ParamTraits<zx::vmo>::Read(const base::Pickle* m,
693 base::PickleIterator* iter,
694 param_type* r) {
695 r->reset();
696
697 bool valid;
698 if (!ReadParam(m, iter, &valid))
699 return false;
700
701 if (!valid)
702 return true;
703
704 scoped_refptr<base::Pickle::Attachment> attachment;
705 if (!m->ReadAttachment(iter, &attachment))
706 return false;
707
708 if (static_cast<MessageAttachment*>(attachment.get())->GetType() !=
709 MessageAttachment::Type::FUCHSIA_HANDLE) {
710 return false;
711 }
712
713 *r = zx::vmo(static_cast<internal::HandleAttachmentFuchsia*>(attachment.get())
714 ->Take());
715 return true;
716}
717
718void ParamTraits<zx::vmo>::Log(const param_type& p, std::string* l) {
719 l->append("ZirconVMO");
720}
Sergey Ulanov7b343ff2019-08-15 07:52:37721
722void ParamTraits<zx::channel>::Write(base::Pickle* m, const param_type& p) {
723 // This serialization must be kept in sync with
724 // nacl_message_scanner.cc:WriteHandle().
725 const bool valid = p.is_valid();
726 WriteParam(m, valid);
727
728 if (!valid)
729 return;
730
731 if (!m->WriteAttachment(new internal::HandleAttachmentFuchsia(
732 std::move(const_cast<param_type&>(p))))) {
733 NOTREACHED();
734 }
735}
736
737bool ParamTraits<zx::channel>::Read(const base::Pickle* m,
738 base::PickleIterator* iter,
739 param_type* r) {
740 r->reset();
741
742 bool valid;
743 if (!ReadParam(m, iter, &valid))
744 return false;
745
746 if (!valid)
747 return true;
748
749 scoped_refptr<base::Pickle::Attachment> attachment;
750 if (!m->ReadAttachment(iter, &attachment))
751 return false;
752
753 if (static_cast<MessageAttachment*>(attachment.get())->GetType() !=
754 MessageAttachment::Type::FUCHSIA_HANDLE) {
755 return false;
756 }
757
758 *r = zx::channel(
759 static_cast<internal::HandleAttachmentFuchsia*>(attachment.get())
760 ->Take());
761 return true;
762}
763
764void ParamTraits<zx::channel>::Log(const param_type& p, std::string* l) {
765 l->append("ZirconChannel");
766}
Xiaohan Wangab909b32022-01-12 17:57:39767#endif // BUILDFLAG(IS_FUCHSIA)
Sergey Ulanova97f3282019-04-17 01:27:34768
Xiaohan Wangab909b32022-01-12 17:57:39769#if BUILDFLAG(IS_ANDROID)
Alexandr Ilin0443a8f2018-07-20 20:14:50770void ParamTraits<base::android::ScopedHardwareBufferHandle>::Write(
771 base::Pickle* m,
772 const param_type& p) {
773 const bool is_valid = p.is_valid();
Ken Rockot097248f02018-04-23 16:23:34774 WriteParam(m, is_valid);
775 if (!is_valid)
776 return;
777
Ken Rockot097248f02018-04-23 16:23:34778 // We must keep a ref to the AHardwareBuffer alive until the receiver has
779 // acquired its own reference. We do this by sending a message pipe handle
780 // along with the buffer. When the receiver deserializes (or even if they
781 // die without ever reading the message) their end of the pipe will be
782 // closed. We will eventually detect this and release the AHB reference.
783 mojo::MessagePipe tracking_pipe;
784 m->WriteAttachment(new internal::MojoHandleAttachment(
785 mojo::ScopedHandle::From(std::move(tracking_pipe.handle0))));
Alexandr Ilin0443a8f2018-07-20 20:14:50786 WriteParam(m, base::FileDescriptor(p.SerializeAsFileDescriptor().release(),
787 true /* auto_close */));
Ken Rockot097248f02018-04-23 16:23:34788
789 // Pass ownership of the input handle to our tracking pipe to keep the AHB
790 // alive long enough to be deserialized by the receiver.
Alexandr Ilin0443a8f2018-07-20 20:14:50791 mojo::ScopeToMessagePipe(std::move(const_cast<param_type&>(p)),
792 std::move(tracking_pipe.handle1));
Klaus Weidner3824a8882017-11-03 06:24:57793}
794
Alexandr Ilin0443a8f2018-07-20 20:14:50795bool ParamTraits<base::android::ScopedHardwareBufferHandle>::Read(
796 const base::Pickle* m,
797 base::PickleIterator* iter,
798 param_type* r) {
799 *r = base::android::ScopedHardwareBufferHandle();
Ken Rockot097248f02018-04-23 16:23:34800
801 bool is_valid;
802 if (!ReadParam(m, iter, &is_valid))
Klaus Weidner3824a8882017-11-03 06:24:57803 return false;
Ken Rockot097248f02018-04-23 16:23:34804 if (!is_valid)
805 return true;
806
807 scoped_refptr<base::Pickle::Attachment> tracking_pipe_attachment;
808 if (!m->ReadAttachment(iter, &tracking_pipe_attachment))
Klaus Weidner3824a8882017-11-03 06:24:57809 return false;
Ken Rockot097248f02018-04-23 16:23:34810
811 // We keep this alive until the AHB is safely deserialized below. When this
812 // goes out of scope, the sender holding the other end of this pipe will treat
813 // this handle closure as a signal that it's safe to release their AHB
814 // keepalive ref.
815 mojo::ScopedHandle tracking_pipe =
816 static_cast<MessageAttachment*>(tracking_pipe_attachment.get())
817 ->TakeMojoHandle();
818
819 base::FileDescriptor descriptor;
820 if (!ReadParam(m, iter, &descriptor))
821 return false;
822
823 // NOTE: It is valid to deserialize an invalid FileDescriptor, so the success
824 // of |ReadParam()| above does not imply that |descriptor| is valid.
825 base::ScopedFD scoped_fd(descriptor.fd);
826 if (!scoped_fd.is_valid())
827 return false;
828
829 *r = base::android::ScopedHardwareBufferHandle::DeserializeFromFileDescriptor(
Alexandr Ilin0443a8f2018-07-20 20:14:50830 std::move(scoped_fd));
Klaus Weidner3824a8882017-11-03 06:24:57831 return true;
832}
833
Alexandr Ilin0443a8f2018-07-20 20:14:50834void ParamTraits<base::android::ScopedHardwareBufferHandle>::Log(
835 const param_type& p,
836 std::string* l) {
837 l->append(base::StringPrintf("base::android::ScopedHardwareBufferHandle(%p)",
838 p.get()));
Klaus Weidner3824a8882017-11-03 06:24:57839}
Xiaohan Wangab909b32022-01-12 17:57:39840#endif // BUILDFLAG(IS_ANDROID)
Klaus Weidner3824a8882017-11-03 06:24:57841
Alexandr Ilind497eee2018-04-19 22:50:54842void ParamTraits<base::ReadOnlySharedMemoryRegion>::Write(base::Pickle* m,
843 const param_type& p) {
844 base::subtle::PlatformSharedMemoryRegion handle =
845 base::ReadOnlySharedMemoryRegion::TakeHandleForSerialization(
846 std::move(const_cast<param_type&>(p)));
847 WriteParam(m, std::move(handle));
848}
849
850bool ParamTraits<base::ReadOnlySharedMemoryRegion>::Read(
851 const base::Pickle* m,
852 base::PickleIterator* iter,
853 param_type* r) {
854 base::subtle::PlatformSharedMemoryRegion handle;
855 if (!ReadParam(m, iter, &handle))
856 return false;
857
858 *r = base::ReadOnlySharedMemoryRegion::Deserialize(std::move(handle));
859 return true;
860}
861
862void ParamTraits<base::ReadOnlySharedMemoryRegion>::Log(const param_type& p,
863 std::string* l) {
864 *l = "<base::ReadOnlySharedMemoryRegion>";
865 // TODO(alexilin): currently there is no way to access underlying handle
866 // without destructing a ReadOnlySharedMemoryRegion instance.
867}
868
869void ParamTraits<base::WritableSharedMemoryRegion>::Write(base::Pickle* m,
870 const param_type& p) {
871 base::subtle::PlatformSharedMemoryRegion handle =
872 base::WritableSharedMemoryRegion::TakeHandleForSerialization(
873 std::move(const_cast<param_type&>(p)));
874 WriteParam(m, std::move(handle));
875}
876
877bool ParamTraits<base::WritableSharedMemoryRegion>::Read(
878 const base::Pickle* m,
879 base::PickleIterator* iter,
880 param_type* r) {
881 base::subtle::PlatformSharedMemoryRegion handle;
882 if (!ReadParam(m, iter, &handle))
883 return false;
884
885 *r = base::WritableSharedMemoryRegion::Deserialize(std::move(handle));
886 return true;
887}
888
889void ParamTraits<base::WritableSharedMemoryRegion>::Log(const param_type& p,
890 std::string* l) {
891 *l = "<base::WritableSharedMemoryRegion>";
892 // TODO(alexilin): currently there is no way to access underlying handle
893 // without destructing a ReadOnlySharedMemoryRegion instance.
894}
895
896void ParamTraits<base::UnsafeSharedMemoryRegion>::Write(base::Pickle* m,
897 const param_type& p) {
898 base::subtle::PlatformSharedMemoryRegion handle =
899 base::UnsafeSharedMemoryRegion::TakeHandleForSerialization(
900 std::move(const_cast<param_type&>(p)));
901 WriteParam(m, std::move(handle));
902}
903
904bool ParamTraits<base::UnsafeSharedMemoryRegion>::Read(
905 const base::Pickle* m,
906 base::PickleIterator* iter,
907 param_type* r) {
908 base::subtle::PlatformSharedMemoryRegion handle;
909 if (!ReadParam(m, iter, &handle))
910 return false;
911
912 *r = base::UnsafeSharedMemoryRegion::Deserialize(std::move(handle));
913 return true;
914}
915
916void ParamTraits<base::UnsafeSharedMemoryRegion>::Log(const param_type& p,
917 std::string* l) {
918 *l = "<base::UnsafeSharedMemoryRegion>";
919 // TODO(alexilin): currently there is no way to access underlying handle
920 // without destructing a ReadOnlySharedMemoryRegion instance.
921}
922
923void ParamTraits<base::subtle::PlatformSharedMemoryRegion>::Write(
924 base::Pickle* m,
925 const param_type& p) {
Alexandr Ilinebab9da2018-06-01 02:37:47926 // This serialization must be kept in sync with
927 // nacl_message_scanner.cc::WriteHandle().
Alexandr Ilind497eee2018-04-19 22:50:54928 const bool valid = p.IsValid();
929 WriteParam(m, valid);
930
931 if (!valid)
932 return;
933
934 WriteParam(m, p.GetMode());
935 WriteParam(m, static_cast<uint64_t>(p.GetSize()));
936 WriteParam(m, p.GetGUID());
937
Xiaohan Wangab909b32022-01-12 17:57:39938#if BUILDFLAG(IS_WIN)
Fabrice de Gans-Ribericbce4342018-05-07 20:02:09939 base::win::ScopedHandle h = const_cast<param_type&>(p).PassPlatformHandle();
Alexandr Ilin6544b8a2018-08-15 23:08:54940 HandleWin handle_win(h.Get());
Fabrice de Gans-Ribericbce4342018-05-07 20:02:09941 WriteParam(m, handle_win);
Xiaohan Wangab909b32022-01-12 17:57:39942#elif BUILDFLAG(IS_FUCHSIA)
Sergey Ulanov04367c72019-06-21 15:51:43943 zx::vmo vmo = const_cast<param_type&>(p).PassPlatformHandle();
944 WriteParam(m, vmo);
Xiaohan Wangab909b32022-01-12 17:57:39945#elif BUILDFLAG(IS_MAC)
Fabrice de Gans-Ribericbce4342018-05-07 20:02:09946 base::mac::ScopedMachSendRight h =
947 const_cast<param_type&>(p).PassPlatformHandle();
Alexandr Ilin6544b8a2018-08-15 23:08:54948 MachPortMac mach_port_mac(h.get());
Fabrice de Gans-Ribericbce4342018-05-07 20:02:09949 WriteParam(m, mach_port_mac);
Xiaohan Wangab909b32022-01-12 17:57:39950#elif BUILDFLAG(IS_ANDROID)
Alexandr Ilind497eee2018-04-19 22:50:54951 m->WriteAttachment(new internal::PlatformFileAttachment(
952 base::ScopedFD(const_cast<param_type&>(p).PassPlatformHandle())));
Xiaohan Wangab909b32022-01-12 17:57:39953#elif BUILDFLAG(IS_POSIX)
Alexandr Ilind497eee2018-04-19 22:50:54954 base::subtle::ScopedFDPair h =
955 const_cast<param_type&>(p).PassPlatformHandle();
956 m->WriteAttachment(new internal::PlatformFileAttachment(std::move(h.fd)));
957 if (p.GetMode() ==
958 base::subtle::PlatformSharedMemoryRegion::Mode::kWritable) {
959 m->WriteAttachment(
960 new internal::PlatformFileAttachment(std::move(h.readonly_fd)));
961 }
962#endif
963}
964
965bool ParamTraits<base::subtle::PlatformSharedMemoryRegion>::Read(
966 const base::Pickle* m,
967 base::PickleIterator* iter,
968 param_type* r) {
969 bool valid;
970 if (!ReadParam(m, iter, &valid))
971 return false;
972 if (!valid) {
973 *r = base::subtle::PlatformSharedMemoryRegion();
974 return true;
975 }
976
977 base::subtle::PlatformSharedMemoryRegion::Mode mode;
978 uint64_t shm_size;
979 base::UnguessableToken guid;
980 if (!ReadParam(m, iter, &mode) || !ReadParam(m, iter, &shm_size) ||
981 !base::IsValueInRangeForNumericType<size_t>(shm_size) ||
982 !ReadParam(m, iter, &guid)) {
983 return false;
984 }
985 size_t size = static_cast<size_t>(shm_size);
986
Xiaohan Wangab909b32022-01-12 17:57:39987#if BUILDFLAG(IS_WIN)
Fabrice de Gans-Ribericbce4342018-05-07 20:02:09988 HandleWin handle_win;
989 if (!ReadParam(m, iter, &handle_win))
Alexandr Ilind497eee2018-04-19 22:50:54990 return false;
991 *r = base::subtle::PlatformSharedMemoryRegion::Take(
Fabrice de Gans-Ribericbce4342018-05-07 20:02:09992 base::win::ScopedHandle(handle_win.get_handle()), mode, size, guid);
Xiaohan Wangab909b32022-01-12 17:57:39993#elif BUILDFLAG(IS_FUCHSIA)
Sergey Ulanov04367c72019-06-21 15:51:43994 zx::vmo vmo;
995 if (!ReadParam(m, iter, &vmo))
Alexandr Ilind497eee2018-04-19 22:50:54996 return false;
Sergey Ulanov04367c72019-06-21 15:51:43997 *r = base::subtle::PlatformSharedMemoryRegion::Take(std::move(vmo), mode,
998 size, guid);
Xiaohan Wangab909b32022-01-12 17:57:39999#elif BUILDFLAG(IS_MAC)
Fabrice de Gans-Ribericbce4342018-05-07 20:02:091000 MachPortMac mach_port_mac;
1001 if (!ReadParam(m, iter, &mach_port_mac))
Alexandr Ilind497eee2018-04-19 22:50:541002 return false;
1003 *r = base::subtle::PlatformSharedMemoryRegion::Take(
Fabrice de Gans-Ribericbce4342018-05-07 20:02:091004 base::mac::ScopedMachSendRight(mach_port_mac.get_mach_port()), mode, size,
1005 guid);
Xiaohan Wangab909b32022-01-12 17:57:391006#elif BUILDFLAG(IS_POSIX)
Alexandr Ilind497eee2018-04-19 22:50:541007 scoped_refptr<base::Pickle::Attachment> attachment;
1008 if (!m->ReadAttachment(iter, &attachment))
1009 return false;
1010 if (static_cast<MessageAttachment*>(attachment.get())->GetType() !=
1011 MessageAttachment::Type::PLATFORM_FILE) {
1012 return false;
1013 }
1014
Xiaohan Wangab909b32022-01-12 17:57:391015#if BUILDFLAG(IS_ANDROID)
Alexandr Ilind497eee2018-04-19 22:50:541016 *r = base::subtle::PlatformSharedMemoryRegion::Take(
1017 base::ScopedFD(
1018 static_cast<internal::PlatformFileAttachment*>(attachment.get())
1019 ->TakePlatformFile()),
1020 mode, size, guid);
1021#else
1022 scoped_refptr<base::Pickle::Attachment> readonly_attachment;
1023 if (mode == base::subtle::PlatformSharedMemoryRegion::Mode::kWritable) {
1024 if (!m->ReadAttachment(iter, &readonly_attachment))
1025 return false;
1026
1027 if (static_cast<MessageAttachment*>(readonly_attachment.get())->GetType() !=
1028 MessageAttachment::Type::PLATFORM_FILE) {
1029 return false;
1030 }
1031 }
1032 *r = base::subtle::PlatformSharedMemoryRegion::Take(
1033 base::subtle::ScopedFDPair(
1034 base::ScopedFD(
1035 static_cast<internal::PlatformFileAttachment*>(attachment.get())
1036 ->TakePlatformFile()),
1037 readonly_attachment
1038 ? base::ScopedFD(static_cast<internal::PlatformFileAttachment*>(
1039 readonly_attachment.get())
1040 ->TakePlatformFile())
1041 : base::ScopedFD()),
1042 mode, size, guid);
Xiaohan Wangab909b32022-01-12 17:57:391043#endif // BUILDFLAG(IS_ANDROID)
Alexandr Ilind497eee2018-04-19 22:50:541044
1045#endif
1046
1047 return true;
1048}
1049
1050void ParamTraits<base::subtle::PlatformSharedMemoryRegion>::Log(
1051 const param_type& p,
1052 std::string* l) {
Xiaohan Wangab909b32022-01-12 17:57:391053#if BUILDFLAG(IS_FUCHSIA)
Wezb908e432018-09-05 17:35:221054 l->append("Handle: ");
1055 LogParam(p.GetPlatformHandle()->get(), l);
Xiaohan Wangab909b32022-01-12 17:57:391056#elif BUILDFLAG(IS_WIN)
Alexandr Ilind497eee2018-04-19 22:50:541057 l->append("Handle: ");
1058 LogParam(p.GetPlatformHandle(), l);
Xiaohan Wangab909b32022-01-12 17:57:391059#elif BUILDFLAG(IS_MAC)
Fabrice de Gans-Ribericbce4342018-05-07 20:02:091060 l->append("Mach port: ");
1061 LogParam(p.GetPlatformHandle(), l);
Xiaohan Wangab909b32022-01-12 17:57:391062#elif BUILDFLAG(IS_ANDROID)
Alexandr Ilind497eee2018-04-19 22:50:541063 l->append("FD: ");
1064 LogParam(p.GetPlatformHandle(), l);
Xiaohan Wangab909b32022-01-12 17:57:391065#elif BUILDFLAG(IS_POSIX)
Alexandr Ilind497eee2018-04-19 22:50:541066 base::subtle::FDPair h = p.GetPlatformHandle();
1067 l->append("FD: ");
1068 LogParam(h.fd, l);
1069 l->append("Read-only FD: ");
1070 LogParam(h.readonly_fd, l);
1071#endif
1072
1073 l->append("Mode: ");
1074 LogParam(p.GetMode(), l);
1075 l->append("size: ");
1076 LogParam(static_cast<uint64_t>(p.GetSize()), l);
1077 l->append("GUID: ");
1078 LogParam(p.GetGUID(), l);
1079}
1080
1081void ParamTraits<base::subtle::PlatformSharedMemoryRegion::Mode>::Write(
1082 base::Pickle* m,
1083 const param_type& value) {
1084 DCHECK(static_cast<int>(value) >= 0 &&
1085 static_cast<int>(value) <= static_cast<int>(param_type::kMaxValue));
1086 m->WriteInt(static_cast<int>(value));
1087}
1088
1089bool ParamTraits<base::subtle::PlatformSharedMemoryRegion::Mode>::Read(
1090 const base::Pickle* m,
1091 base::PickleIterator* iter,
1092 param_type* p) {
1093 int value;
1094 if (!iter->ReadInt(&value))
1095 return false;
1096 if (!(static_cast<int>(value) >= 0 &&
1097 static_cast<int>(value) <= static_cast<int>(param_type::kMaxValue))) {
1098 return false;
1099 }
1100 *p = static_cast<param_type>(value);
1101 return true;
1102}
1103
1104void ParamTraits<base::subtle::PlatformSharedMemoryRegion::Mode>::Log(
1105 const param_type& p,
1106 std::string* l) {
1107 LogParam(static_cast<int>(p), l);
1108}
1109
Xiaohan Wangab909b32022-01-12 17:57:391110#if BUILDFLAG(IS_WIN)
erikchend804e1052017-04-29 02:24:361111void ParamTraits<PlatformFileForTransit>::Write(base::Pickle* m,
1112 const param_type& p) {
1113 m->WriteBool(p.IsValid());
1114 if (p.IsValid()) {
Wez51eaaad2017-08-09 05:51:381115 HandleWin handle_win(p.GetHandle());
erikchend804e1052017-04-29 02:24:361116 ParamTraits<HandleWin>::Write(m, handle_win);
1117 ::CloseHandle(p.GetHandle());
1118 }
1119}
1120
1121bool ParamTraits<PlatformFileForTransit>::Read(const base::Pickle* m,
1122 base::PickleIterator* iter,
1123 param_type* r) {
1124 bool is_valid;
1125 if (!iter->ReadBool(&is_valid))
1126 return false;
1127 if (!is_valid) {
1128 *r = PlatformFileForTransit();
1129 return true;
1130 }
1131
1132 HandleWin handle_win;
1133 if (!ParamTraits<HandleWin>::Read(m, iter, &handle_win))
1134 return false;
1135 *r = PlatformFileForTransit(handle_win.get_handle());
1136 return true;
1137}
1138
1139void ParamTraits<PlatformFileForTransit>::Log(const param_type& p,
1140 std::string* l) {
1141 LogParam(p.GetHandle(), l);
1142}
Xiaohan Wangab909b32022-01-12 17:57:391143#endif // BUILDFLAG(IS_WIN)
erikchend804e1052017-04-29 02:24:361144
rockot502c94f2016-02-03 20:20:161145void ParamTraits<base::FilePath>::Write(base::Pickle* m, const param_type& p) {
[email protected]aeae59f2013-01-28 13:47:551146 p.WriteToPickle(m);
[email protected]bf5aedf02012-06-04 21:18:251147}
1148
rockot502c94f2016-02-03 20:20:161149bool ParamTraits<base::FilePath>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:251150 base::PickleIterator* iter,
[email protected]6d4b67a2013-02-10 04:49:301151 param_type* r) {
[email protected]aeae59f2013-01-28 13:47:551152 return r->ReadFromPickle(iter);
[email protected]bf5aedf02012-06-04 21:18:251153}
1154
[email protected]6d4b67a2013-02-10 04:49:301155void ParamTraits<base::FilePath>::Log(const param_type& p, std::string* l) {
1156 ParamTraits<base::FilePath::StringType>::Log(p.value(), l);
[email protected]bf5aedf02012-06-04 21:18:251157}
1158
rockot502c94f2016-02-03 20:20:161159void ParamTraits<base::ListValue>::Write(base::Pickle* m, const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:251160 WriteValue(m, &p, 0);
1161}
1162
rockot502c94f2016-02-03 20:20:161163bool ParamTraits<base::ListValue>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:251164 base::PickleIterator* iter,
1165 param_type* r) {
[email protected]bf5aedf02012-06-04 21:18:251166 int type;
jdoerriedc72ee942016-12-07 15:43:281167 if (!ReadParam(m, iter, &type) ||
1168 type != static_cast<int>(base::Value::Type::LIST))
[email protected]bf5aedf02012-06-04 21:18:251169 return false;
1170
1171 return ReadListValue(m, iter, r, 0);
1172}
1173
[email protected]ea5ef4c2013-06-13 22:50:271174void ParamTraits<base::ListValue>::Log(const param_type& p, std::string* l) {
[email protected]bf5aedf02012-06-04 21:18:251175 std::string json;
estade8d046462015-05-16 01:02:341176 base::JSONWriter::Write(p, &json);
[email protected]bf5aedf02012-06-04 21:18:251177 l->append(json);
1178}
1179
Devlin Cronin7178a5bd2021-02-02 02:56:471180void ParamTraits<base::Value>::Write(base::Pickle* m, const param_type& p) {
1181 WriteValue(m, &p, 0);
1182}
1183
1184bool ParamTraits<base::Value>::Read(const base::Pickle* m,
1185 base::PickleIterator* iter,
1186 param_type* r) {
1187 return ReadValue(m, iter, r, 0);
1188}
1189
1190void ParamTraits<base::Value>::Log(const param_type& p, std::string* l) {
1191 std::string json;
1192 base::JSONWriter::Write(p, &json);
1193 l->append(json);
1194}
1195
rockot502c94f2016-02-03 20:20:161196void ParamTraits<base::File::Info>::Write(base::Pickle* m,
[email protected]141bcc52014-01-27 21:36:001197 const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:251198 WriteParam(m, p.size);
1199 WriteParam(m, p.is_directory);
1200 WriteParam(m, p.last_modified.ToDoubleT());
1201 WriteParam(m, p.last_accessed.ToDoubleT());
1202 WriteParam(m, p.creation_time.ToDoubleT());
1203}
1204
rockot502c94f2016-02-03 20:20:161205bool ParamTraits<base::File::Info>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:251206 base::PickleIterator* iter,
[email protected]141bcc52014-01-27 21:36:001207 param_type* p) {
[email protected]481c3e82014-07-18 01:40:471208 double last_modified, last_accessed, creation_time;
1209 if (!ReadParam(m, iter, &p->size) ||
1210 !ReadParam(m, iter, &p->is_directory) ||
1211 !ReadParam(m, iter, &last_modified) ||
1212 !ReadParam(m, iter, &last_accessed) ||
1213 !ReadParam(m, iter, &creation_time))
1214 return false;
1215 p->last_modified = base::Time::FromDoubleT(last_modified);
1216 p->last_accessed = base::Time::FromDoubleT(last_accessed);
1217 p->creation_time = base::Time::FromDoubleT(creation_time);
1218 return true;
[email protected]bf5aedf02012-06-04 21:18:251219}
1220
[email protected]141bcc52014-01-27 21:36:001221void ParamTraits<base::File::Info>::Log(const param_type& p,
1222 std::string* l) {
[email protected]bf5aedf02012-06-04 21:18:251223 l->append("(");
1224 LogParam(p.size, l);
1225 l->append(",");
1226 LogParam(p.is_directory, l);
1227 l->append(",");
1228 LogParam(p.last_modified.ToDoubleT(), l);
1229 l->append(",");
1230 LogParam(p.last_accessed.ToDoubleT(), l);
1231 l->append(",");
1232 LogParam(p.creation_time.ToDoubleT(), l);
1233 l->append(")");
1234}
1235
rockot502c94f2016-02-03 20:20:161236void ParamTraits<base::Time>::Write(base::Pickle* m, const param_type& p) {
tfarina10a5c062015-09-04 18:47:571237 ParamTraits<int64_t>::Write(m, p.ToInternalValue());
[email protected]bf5aedf02012-06-04 21:18:251238}
1239
rockot502c94f2016-02-03 20:20:161240bool ParamTraits<base::Time>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:251241 base::PickleIterator* iter,
[email protected]bf5aedf02012-06-04 21:18:251242 param_type* r) {
tfarina10a5c062015-09-04 18:47:571243 int64_t value;
1244 if (!ParamTraits<int64_t>::Read(m, iter, &value))
[email protected]bf5aedf02012-06-04 21:18:251245 return false;
1246 *r = base::Time::FromInternalValue(value);
1247 return true;
1248}
1249
1250void ParamTraits<base::Time>::Log(const param_type& p, std::string* l) {
tfarina10a5c062015-09-04 18:47:571251 ParamTraits<int64_t>::Log(p.ToInternalValue(), l);
[email protected]bf5aedf02012-06-04 21:18:251252}
1253
rockot502c94f2016-02-03 20:20:161254void ParamTraits<base::TimeDelta>::Write(base::Pickle* m, const param_type& p) {
tfarina10a5c062015-09-04 18:47:571255 ParamTraits<int64_t>::Write(m, p.ToInternalValue());
[email protected]bf5aedf02012-06-04 21:18:251256}
1257
rockot502c94f2016-02-03 20:20:161258bool ParamTraits<base::TimeDelta>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:251259 base::PickleIterator* iter,
[email protected]bf5aedf02012-06-04 21:18:251260 param_type* r) {
tfarina10a5c062015-09-04 18:47:571261 int64_t value;
1262 bool ret = ParamTraits<int64_t>::Read(m, iter, &value);
[email protected]bf5aedf02012-06-04 21:18:251263 if (ret)
1264 *r = base::TimeDelta::FromInternalValue(value);
1265
1266 return ret;
1267}
1268
1269void ParamTraits<base::TimeDelta>::Log(const param_type& p, std::string* l) {
tfarina10a5c062015-09-04 18:47:571270 ParamTraits<int64_t>::Log(p.ToInternalValue(), l);
[email protected]bf5aedf02012-06-04 21:18:251271}
1272
rockot502c94f2016-02-03 20:20:161273void ParamTraits<base::TimeTicks>::Write(base::Pickle* m, const param_type& p) {
tfarina10a5c062015-09-04 18:47:571274 ParamTraits<int64_t>::Write(m, p.ToInternalValue());
[email protected]bf5aedf02012-06-04 21:18:251275}
1276
rockot502c94f2016-02-03 20:20:161277bool ParamTraits<base::TimeTicks>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:251278 base::PickleIterator* iter,
[email protected]bf5aedf02012-06-04 21:18:251279 param_type* r) {
tfarina10a5c062015-09-04 18:47:571280 int64_t value;
1281 bool ret = ParamTraits<int64_t>::Read(m, iter, &value);
[email protected]bf5aedf02012-06-04 21:18:251282 if (ret)
1283 *r = base::TimeTicks::FromInternalValue(value);
1284
1285 return ret;
1286}
1287
1288void ParamTraits<base::TimeTicks>::Log(const param_type& p, std::string* l) {
tfarina10a5c062015-09-04 18:47:571289 ParamTraits<int64_t>::Log(p.ToInternalValue(), l);
[email protected]bf5aedf02012-06-04 21:18:251290}
1291
tguilbert4a5ac602016-09-19 21:11:251292// If base::UnguessableToken is no longer 128 bits, the IPC serialization logic
1293// below should be updated.
1294static_assert(sizeof(base::UnguessableToken) == 2 * sizeof(uint64_t),
1295 "base::UnguessableToken should be of size 2 * sizeof(uint64_t).");
1296
tguilbert4a5ac602016-09-19 21:11:251297void ParamTraits<base::UnguessableToken>::Write(base::Pickle* m,
1298 const param_type& p) {
1299 DCHECK(!p.is_empty());
1300
1301 ParamTraits<uint64_t>::Write(m, p.GetHighForSerialization());
1302 ParamTraits<uint64_t>::Write(m, p.GetLowForSerialization());
1303}
1304
1305bool ParamTraits<base::UnguessableToken>::Read(const base::Pickle* m,
1306 base::PickleIterator* iter,
1307 param_type* r) {
1308 uint64_t high, low;
1309 if (!ParamTraits<uint64_t>::Read(m, iter, &high) ||
1310 !ParamTraits<uint64_t>::Read(m, iter, &low))
1311 return false;
1312
1313 // Receiving a zeroed UnguessableToken is a security issue.
1314 if (high == 0 && low == 0)
1315 return false;
1316
1317 *r = base::UnguessableToken::Deserialize(high, low);
1318 return true;
1319}
1320
1321void ParamTraits<base::UnguessableToken>::Log(const param_type& p,
1322 std::string* l) {
1323 l->append(p.ToString());
1324}
1325
rockot502c94f2016-02-03 20:20:161326void ParamTraits<IPC::ChannelHandle>::Write(base::Pickle* m,
1327 const param_type& p) {
Xiaohan Wang53a511632022-01-21 18:14:581328#if BUILDFLAG(IS_NACL)
[email protected]7a4de7a62010-08-17 18:38:241329 WriteParam(m, p.socket);
sammc9bf370c2016-11-14 03:29:081330#else
amistry36182522016-06-27 06:34:421331 WriteParam(m, p.mojo_handle);
sammc9bf370c2016-11-14 03:29:081332#endif
[email protected]7a4de7a62010-08-17 18:38:241333}
1334
rockot502c94f2016-02-03 20:20:161335bool ParamTraits<IPC::ChannelHandle>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:251336 base::PickleIterator* iter,
[email protected]7a4de7a62010-08-17 18:38:241337 param_type* r) {
Xiaohan Wang53a511632022-01-21 18:14:581338#if BUILDFLAG(IS_NACL)
sammc9bf370c2016-11-14 03:29:081339 return ReadParam(m, iter, &r->socket);
1340#else
1341 return ReadParam(m, iter, &r->mojo_handle);
[email protected]7a4de7a62010-08-17 18:38:241342#endif
[email protected]7a4de7a62010-08-17 18:38:241343}
1344
1345void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p,
[email protected]252cad62010-08-18 18:33:571346 std::string* l) {
sammc9bf370c2016-11-14 03:29:081347 l->append("ChannelHandle(");
Xiaohan Wang53a511632022-01-21 18:14:581348#if BUILDFLAG(IS_NACL)
[email protected]7a4de7a62010-08-17 18:38:241349 ParamTraits<base::FileDescriptor>::Log(p.socket, l);
sammc9bf370c2016-11-14 03:29:081350#else
amistry36182522016-06-27 06:34:421351 LogParam(p.mojo_handle, l);
sammc9bf370c2016-11-14 03:29:081352#endif
[email protected]252cad62010-08-18 18:33:571353 l->append(")");
[email protected]7a4de7a62010-08-17 18:38:241354}
1355
rockot502c94f2016-02-03 20:20:161356void ParamTraits<LogData>::Write(base::Pickle* m, const param_type& p) {
[email protected]20f0487a2010-09-30 20:06:301357 WriteParam(m, p.channel);
1358 WriteParam(m, p.routing_id);
[email protected]8bf55ca2011-10-17 22:15:271359 WriteParam(m, p.type);
[email protected]20f0487a2010-09-30 20:06:301360 WriteParam(m, p.flags);
1361 WriteParam(m, p.sent);
1362 WriteParam(m, p.receive);
1363 WriteParam(m, p.dispatch);
[email protected]bae578e92012-11-15 03:17:451364 WriteParam(m, p.message_name);
[email protected]20f0487a2010-09-30 20:06:301365 WriteParam(m, p.params);
1366}
1367
rockot502c94f2016-02-03 20:20:161368bool ParamTraits<LogData>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:251369 base::PickleIterator* iter,
[email protected]ce208f872012-03-07 20:42:561370 param_type* r) {
[email protected]8bf55ca2011-10-17 22:15:271371 return
[email protected]20f0487a2010-09-30 20:06:301372 ReadParam(m, iter, &r->channel) &&
1373 ReadParam(m, iter, &r->routing_id) &&
[email protected]8bf55ca2011-10-17 22:15:271374 ReadParam(m, iter, &r->type) &&
[email protected]20f0487a2010-09-30 20:06:301375 ReadParam(m, iter, &r->flags) &&
1376 ReadParam(m, iter, &r->sent) &&
1377 ReadParam(m, iter, &r->receive) &&
1378 ReadParam(m, iter, &r->dispatch) &&
[email protected]bae578e92012-11-15 03:17:451379 ReadParam(m, iter, &r->message_name) &&
[email protected]20f0487a2010-09-30 20:06:301380 ReadParam(m, iter, &r->params);
[email protected]20f0487a2010-09-30 20:06:301381}
1382
[email protected]bf5aedf02012-06-04 21:18:251383void ParamTraits<LogData>::Log(const param_type& p, std::string* l) {
1384 // Doesn't make sense to implement this!
1385}
1386
rockot502c94f2016-02-03 20:20:161387void ParamTraits<Message>::Write(base::Pickle* m, const Message& p) {
Xiaohan Wangab909b32022-01-12 17:57:391388#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
[email protected]34d48612012-06-29 00:05:041389 // We don't serialize the file descriptors in the nested message, so there
1390 // better not be any.
morrita1aa788c2015-01-31 05:45:421391 DCHECK(!p.HasAttachments());
[email protected]34d48612012-06-29 00:05:041392#endif
1393
1394 // Don't just write out the message. This is used to send messages between
1395 // NaCl (Posix environment) and the browser (could be on Windows). The message
1396 // header formats differ between these systems (so does handle sharing, but
1397 // we already asserted we don't have any handles). So just write out the
1398 // parts of the header we use.
1399 //
1400 // Be careful also to use only explicitly-sized types. The NaCl environment
1401 // could be 64-bit and the host browser could be 32-bits. The nested message
1402 // may or may not be safe to send between 32-bit and 64-bit systems, but we
1403 // leave that up to the code sending the message to ensure.
tfarina10a5c062015-09-04 18:47:571404 m->WriteUInt32(static_cast<uint32_t>(p.routing_id()));
[email protected]34d48612012-06-29 00:05:041405 m->WriteUInt32(p.type());
1406 m->WriteUInt32(p.flags());
tfarina10a5c062015-09-04 18:47:571407 m->WriteData(p.payload(), static_cast<uint32_t>(p.payload_size()));
[email protected]bf5aedf02012-06-04 21:18:251408}
1409
rockot502c94f2016-02-03 20:20:161410bool ParamTraits<Message>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:251411 base::PickleIterator* iter,
[email protected]bf5aedf02012-06-04 21:18:251412 Message* r) {
tfarina10a5c062015-09-04 18:47:571413 uint32_t routing_id, type, flags;
avi48fc13b2014-12-28 23:31:481414 if (!iter->ReadUInt32(&routing_id) ||
1415 !iter->ReadUInt32(&type) ||
1416 !iter->ReadUInt32(&flags))
[email protected]bf5aedf02012-06-04 21:18:251417 return false;
[email protected]34d48612012-06-29 00:05:041418
1419 int payload_size;
1420 const char* payload;
avi48fc13b2014-12-28 23:31:481421 if (!iter->ReadData(&payload, &payload_size))
[email protected]bf5aedf02012-06-04 21:18:251422 return false;
[email protected]34d48612012-06-29 00:05:041423
tfarina10a5c062015-09-04 18:47:571424 r->SetHeaderValues(static_cast<int32_t>(routing_id), type, flags);
Daniel Cheng0d89f9222017-09-22 05:05:071425 r->WriteBytes(payload, payload_size);
1426 return true;
[email protected]bf5aedf02012-06-04 21:18:251427}
1428
1429void ParamTraits<Message>::Log(const Message& p, std::string* l) {
1430 l->append("<IPC::Message>");
1431}
1432
Xiaohan Wangab909b32022-01-12 17:57:391433#if BUILDFLAG(IS_WIN)
[email protected]bf5aedf02012-06-04 21:18:251434// Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64
[email protected]4a635b72013-03-04 02:29:031435// bit systems. That's why we use the Windows macros to convert to 32 bits.
rockot502c94f2016-02-03 20:20:161436void ParamTraits<HANDLE>::Write(base::Pickle* m, const param_type& p) {
[email protected]4a635b72013-03-04 02:29:031437 m->WriteInt(HandleToLong(p));
[email protected]bf5aedf02012-06-04 21:18:251438}
1439
rockot502c94f2016-02-03 20:20:161440bool ParamTraits<HANDLE>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:251441 base::PickleIterator* iter,
[email protected]bf5aedf02012-06-04 21:18:251442 param_type* r) {
tfarina10a5c062015-09-04 18:47:571443 int32_t temp;
avi48fc13b2014-12-28 23:31:481444 if (!iter->ReadInt(&temp))
[email protected]bf5aedf02012-06-04 21:18:251445 return false;
[email protected]4a635b72013-03-04 02:29:031446 *r = LongToHandle(temp);
[email protected]bf5aedf02012-06-04 21:18:251447 return true;
1448}
1449
1450void ParamTraits<HANDLE>::Log(const param_type& p, std::string* l) {
brucedawson5604a11d2015-10-06 19:22:001451 l->append(base::StringPrintf("0x%p", p));
[email protected]bf5aedf02012-06-04 21:18:251452}
1453
rockot502c94f2016-02-03 20:20:161454void ParamTraits<MSG>::Write(base::Pickle* m, const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:251455 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG));
1456}
1457
rockot502c94f2016-02-03 20:20:161458bool ParamTraits<MSG>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:251459 base::PickleIterator* iter,
[email protected]bf5aedf02012-06-04 21:18:251460 param_type* r) {
1461 const char *data;
1462 int data_size = 0;
avi48fc13b2014-12-28 23:31:481463 bool result = iter->ReadData(&data, &data_size);
[email protected]bf5aedf02012-06-04 21:18:251464 if (result && data_size == sizeof(MSG)) {
1465 memcpy(r, data, sizeof(MSG));
1466 } else {
1467 result = false;
1468 NOTREACHED();
1469 }
1470
1471 return result;
1472}
1473
1474void ParamTraits<MSG>::Log(const param_type& p, std::string* l) {
1475 l->append("<MSG>");
1476}
1477
Xiaohan Wang53a511632022-01-21 18:14:581478#endif // BUILDFLAG(IS_WIN)
[email protected]bf5aedf02012-06-04 21:18:251479
[email protected]946d1b22009-07-22 23:57:211480} // namespace IPC