blob: 29fa0382546d0210158a3b9cfaae9ca6c73c4edb [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"
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/scoped_ptr.h"
[email protected]0238a162013-06-13 13:47:4613#include "base/strings/nullable_string16.h"
[email protected]4aa794a12013-06-11 06:32:1814#include "base/strings/string_number_conversions.h"
[email protected]906265872013-06-07 22:40:4515#include "base/strings/utf_string_conversions.h"
[email protected]b43e5562013-06-28 15:20:0216#include "base/time/time.h"
[email protected]946d1b22009-07-22 23:57:2117#include "base/values.h"
avi246998d82015-12-22 02:39:0418#include "build/build_config.h"
[email protected]bf5aedf02012-06-04 21:18:2519#include "ipc/ipc_channel_handle.h"
morrita1aa788c2015-01-31 05:45:4220#include "ipc/ipc_message_attachment.h"
morrita4b5c28e22015-01-14 21:17:0621#include "ipc/ipc_message_attachment_set.h"
[email protected]bf5aedf02012-06-04 21:18:2522
morrita1aa788c2015-01-31 05:45:4223#if defined(OS_POSIX)
24#include "ipc/ipc_platform_file_attachment_posix.h"
25#endif
26
erikchen5ea2ab72015-09-25 22:34:3127#if (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN)
scottmgd19b4f72015-06-19 22:51:0028#include "base/memory/shared_memory_handle.h"
erikchen5ea2ab72015-09-25 22:34:3129#endif // (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN)
scottmgd19b4f72015-06-19 22:51:0030
erikchenaf8299d2015-10-09 19:12:0631#if defined(OS_MACOSX) && !defined(OS_IOS)
32#include "ipc/mach_port_mac.h"
33#endif
34
morrita4b5c28e22015-01-14 21:17:0635#if defined(OS_WIN)
[email protected]2e02cfe82012-11-21 00:58:0036#include <tchar.h>
erikchen5ea2ab72015-09-25 22:34:3137#include "ipc/handle_win.h"
[email protected]7a4de7a62010-08-17 18:38:2438#endif
[email protected]946d1b22009-07-22 23:57:2139
40namespace IPC {
41
[email protected]bf5aedf02012-06-04 21:18:2542namespace {
43
[email protected]946d1b22009-07-22 23:57:2144const int kMaxRecursionDepth = 100;
45
[email protected]bf5aedf02012-06-04 21:18:2546template<typename CharType>
47void LogBytes(const std::vector<CharType>& data, std::string* out) {
48#if defined(OS_WIN)
49 // Windows has a GUI for logging, which can handle arbitrary binary data.
50 for (size_t i = 0; i < data.size(); ++i)
51 out->push_back(data[i]);
52#else
53 // On POSIX, we log to stdout, which we assume can display ASCII.
54 static const size_t kMaxBytesToLog = 100;
55 for (size_t i = 0; i < std::min(data.size(), kMaxBytesToLog); ++i) {
56 if (isprint(data[i]))
57 out->push_back(data[i]);
58 else
[email protected]7d3cbc92013-03-18 22:33:0459 out->append(
60 base::StringPrintf("[%02X]", static_cast<unsigned char>(data[i])));
[email protected]bf5aedf02012-06-04 21:18:2561 }
62 if (data.size() > kMaxBytesToLog) {
[email protected]f8660f82013-03-30 17:29:2863 out->append(base::StringPrintf(
64 " and %u more bytes",
65 static_cast<unsigned>(data.size() - kMaxBytesToLog)));
[email protected]bf5aedf02012-06-04 21:18:2566 }
67#endif
68}
[email protected]946d1b22009-07-22 23:57:2169
rockot502c94f2016-02-03 20:20:1670bool ReadValue(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:2571 base::PickleIterator* iter,
72 base::Value** value,
[email protected]bf5aedf02012-06-04 21:18:2573 int recursion);
[email protected]946d1b22009-07-22 23:57:2174
rockot0457af102016-02-05 02:12:3275void GetValueSize(base::PickleSizer* sizer,
76 const base::Value* value,
77 int recursion) {
78 if (recursion > kMaxRecursionDepth) {
79 LOG(WARNING) << "Max recursion depth hit in GetValueSize.";
80 return;
81 }
82
83 sizer->AddInt();
84 switch (value->GetType()) {
85 case base::Value::TYPE_NULL:
86 break;
87 case base::Value::TYPE_BOOLEAN:
88 sizer->AddBool();
89 break;
90 case base::Value::TYPE_INTEGER:
91 sizer->AddInt();
92 break;
93 case base::Value::TYPE_DOUBLE:
94 sizer->AddDouble();
95 break;
96 case base::Value::TYPE_STRING: {
97 const base::StringValue* result;
98 value->GetAsString(&result);
99 DCHECK(result);
100 GetParamSize(sizer, result->GetString());
101 break;
102 }
103 case base::Value::TYPE_BINARY: {
104 const base::BinaryValue* binary =
105 static_cast<const base::BinaryValue*>(value);
106 sizer->AddData(static_cast<int>(binary->GetSize()));
107 break;
108 }
109 case base::Value::TYPE_DICTIONARY: {
110 sizer->AddInt();
111 const base::DictionaryValue* dict =
112 static_cast<const base::DictionaryValue*>(value);
113 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd();
114 it.Advance()) {
115 GetParamSize(sizer, it.key());
116 GetValueSize(sizer, &it.value(), recursion + 1);
117 }
118 break;
119 }
120 case base::Value::TYPE_LIST: {
121 sizer->AddInt();
122 const base::ListValue* list = static_cast<const base::ListValue*>(value);
123 for (base::ListValue::const_iterator it = list->begin();
124 it != list->end(); ++it) {
125 GetValueSize(sizer, *it, recursion + 1);
126 }
127 break;
128 }
129 default:
130 NOTREACHED() << "Invalid base::Value type.";
131 }
132}
133
rockot502c94f2016-02-03 20:20:16134void WriteValue(base::Pickle* m, const base::Value* value, int recursion) {
[email protected]dbc761a2012-07-26 01:29:21135 bool result;
[email protected]946d1b22009-07-22 23:57:21136 if (recursion > kMaxRecursionDepth) {
137 LOG(WARNING) << "Max recursion depth hit in WriteValue.";
138 return;
139 }
140
141 m->WriteInt(value->GetType());
142
143 switch (value->GetType()) {
[email protected]ea5ef4c2013-06-13 22:50:27144 case base::Value::TYPE_NULL:
[email protected]946d1b22009-07-22 23:57:21145 break;
[email protected]ea5ef4c2013-06-13 22:50:27146 case base::Value::TYPE_BOOLEAN: {
[email protected]946d1b22009-07-22 23:57:21147 bool val;
[email protected]dbc761a2012-07-26 01:29:21148 result = value->GetAsBoolean(&val);
149 DCHECK(result);
[email protected]946d1b22009-07-22 23:57:21150 WriteParam(m, val);
151 break;
152 }
[email protected]ea5ef4c2013-06-13 22:50:27153 case base::Value::TYPE_INTEGER: {
[email protected]946d1b22009-07-22 23:57:21154 int val;
[email protected]dbc761a2012-07-26 01:29:21155 result = value->GetAsInteger(&val);
156 DCHECK(result);
[email protected]946d1b22009-07-22 23:57:21157 WriteParam(m, val);
158 break;
159 }
[email protected]ea5ef4c2013-06-13 22:50:27160 case base::Value::TYPE_DOUBLE: {
[email protected]946d1b22009-07-22 23:57:21161 double val;
[email protected]dbc761a2012-07-26 01:29:21162 result = value->GetAsDouble(&val);
163 DCHECK(result);
[email protected]946d1b22009-07-22 23:57:21164 WriteParam(m, val);
165 break;
166 }
[email protected]ea5ef4c2013-06-13 22:50:27167 case base::Value::TYPE_STRING: {
[email protected]946d1b22009-07-22 23:57:21168 std::string val;
[email protected]dbc761a2012-07-26 01:29:21169 result = value->GetAsString(&val);
170 DCHECK(result);
[email protected]946d1b22009-07-22 23:57:21171 WriteParam(m, val);
172 break;
173 }
[email protected]ea5ef4c2013-06-13 22:50:27174 case base::Value::TYPE_BINARY: {
[email protected]b59ea312011-08-05 18:20:05175 const base::BinaryValue* binary =
176 static_cast<const base::BinaryValue*>(value);
[email protected]7ee1a44c2010-07-23 14:18:59177 m->WriteData(binary->GetBuffer(), static_cast<int>(binary->GetSize()));
[email protected]e4dad9fb2009-10-06 18:15:58178 break;
[email protected]946d1b22009-07-22 23:57:21179 }
[email protected]ea5ef4c2013-06-13 22:50:27180 case base::Value::TYPE_DICTIONARY: {
181 const base::DictionaryValue* dict =
182 static_cast<const base::DictionaryValue*>(value);
[email protected]946d1b22009-07-22 23:57:21183
[email protected]4dad9ad82009-11-25 20:47:52184 WriteParam(m, static_cast<int>(dict->size()));
[email protected]946d1b22009-07-22 23:57:21185
[email protected]ea5ef4c2013-06-13 22:50:27186 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd();
187 it.Advance()) {
[email protected]a899c0b02013-01-18 14:43:27188 WriteParam(m, it.key());
189 WriteValue(m, &it.value(), recursion + 1);
[email protected]946d1b22009-07-22 23:57:21190 }
191 break;
192 }
[email protected]ea5ef4c2013-06-13 22:50:27193 case base::Value::TYPE_LIST: {
194 const base::ListValue* list = static_cast<const base::ListValue*>(value);
[email protected]946d1b22009-07-22 23:57:21195 WriteParam(m, static_cast<int>(list->GetSize()));
[email protected]ea5ef4c2013-06-13 22:50:27196 for (base::ListValue::const_iterator it = list->begin();
197 it != list->end(); ++it) {
[email protected]a899c0b02013-01-18 14:43:27198 WriteValue(m, *it, recursion + 1);
[email protected]946d1b22009-07-22 23:57:21199 }
200 break;
201 }
202 }
203}
204
205// Helper for ReadValue that reads a DictionaryValue into a pre-allocated
206// object.
rockot502c94f2016-02-03 20:20:16207bool ReadDictionaryValue(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25208 base::PickleIterator* iter,
209 base::DictionaryValue* value,
210 int recursion) {
[email protected]946d1b22009-07-22 23:57:21211 int size;
212 if (!ReadParam(m, iter, &size))
213 return false;
214
215 for (int i = 0; i < size; ++i) {
[email protected]e7b418b2010-07-30 19:47:47216 std::string key;
[email protected]0c6c1e42013-06-21 19:42:19217 base::Value* subval;
[email protected]946d1b22009-07-22 23:57:21218 if (!ReadParam(m, iter, &key) ||
219 !ReadValue(m, iter, &subval, recursion + 1))
220 return false;
[email protected]d8b4aa42011-08-19 05:59:57221 value->SetWithoutPathExpansion(key, subval);
[email protected]946d1b22009-07-22 23:57:21222 }
223
224 return true;
225}
226
227// Helper for ReadValue that reads a ReadListValue into a pre-allocated
228// object.
rockot502c94f2016-02-03 20:20:16229bool ReadListValue(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25230 base::PickleIterator* iter,
231 base::ListValue* value,
232 int recursion) {
[email protected]946d1b22009-07-22 23:57:21233 int size;
234 if (!ReadParam(m, iter, &size))
235 return false;
236
237 for (int i = 0; i < size; ++i) {
[email protected]ea5ef4c2013-06-13 22:50:27238 base::Value* subval;
[email protected]946d1b22009-07-22 23:57:21239 if (!ReadValue(m, iter, &subval, recursion + 1))
240 return false;
241 value->Set(i, subval);
242 }
243
244 return true;
245}
246
rockot502c94f2016-02-03 20:20:16247bool ReadValue(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25248 base::PickleIterator* iter,
249 base::Value** value,
[email protected]bf5aedf02012-06-04 21:18:25250 int recursion) {
[email protected]946d1b22009-07-22 23:57:21251 if (recursion > kMaxRecursionDepth) {
252 LOG(WARNING) << "Max recursion depth hit in ReadValue.";
253 return false;
254 }
255
256 int type;
257 if (!ReadParam(m, iter, &type))
258 return false;
259
260 switch (type) {
[email protected]ea5ef4c2013-06-13 22:50:27261 case base::Value::TYPE_NULL:
estadea68b0442015-05-12 18:11:50262 *value = base::Value::CreateNullValue().release();
[email protected]946d1b22009-07-22 23:57:21263 break;
[email protected]ea5ef4c2013-06-13 22:50:27264 case base::Value::TYPE_BOOLEAN: {
[email protected]946d1b22009-07-22 23:57:21265 bool val;
266 if (!ReadParam(m, iter, &val))
267 return false;
[email protected]4038a132013-01-30 05:24:07268 *value = new base::FundamentalValue(val);
[email protected]946d1b22009-07-22 23:57:21269 break;
270 }
[email protected]ea5ef4c2013-06-13 22:50:27271 case base::Value::TYPE_INTEGER: {
[email protected]946d1b22009-07-22 23:57:21272 int val;
273 if (!ReadParam(m, iter, &val))
274 return false;
[email protected]4038a132013-01-30 05:24:07275 *value = new base::FundamentalValue(val);
[email protected]946d1b22009-07-22 23:57:21276 break;
277 }
[email protected]ea5ef4c2013-06-13 22:50:27278 case base::Value::TYPE_DOUBLE: {
[email protected]946d1b22009-07-22 23:57:21279 double val;
280 if (!ReadParam(m, iter, &val))
281 return false;
[email protected]4038a132013-01-30 05:24:07282 *value = new base::FundamentalValue(val);
[email protected]946d1b22009-07-22 23:57:21283 break;
284 }
[email protected]ea5ef4c2013-06-13 22:50:27285 case base::Value::TYPE_STRING: {
[email protected]946d1b22009-07-22 23:57:21286 std::string val;
287 if (!ReadParam(m, iter, &val))
288 return false;
[email protected]4038a132013-01-30 05:24:07289 *value = new base::StringValue(val);
[email protected]946d1b22009-07-22 23:57:21290 break;
291 }
[email protected]ea5ef4c2013-06-13 22:50:27292 case base::Value::TYPE_BINARY: {
[email protected]e4dad9fb2009-10-06 18:15:58293 const char* data;
294 int length;
avi48fc13b2014-12-28 23:31:48295 if (!iter->ReadData(&data, &length))
[email protected]e4dad9fb2009-10-06 18:15:58296 return false;
[email protected]b59ea312011-08-05 18:20:05297 *value = base::BinaryValue::CreateWithCopiedBuffer(data, length);
[email protected]946d1b22009-07-22 23:57:21298 break;
299 }
[email protected]ea5ef4c2013-06-13 22:50:27300 case base::Value::TYPE_DICTIONARY: {
301 scoped_ptr<base::DictionaryValue> val(new base::DictionaryValue());
[email protected]946d1b22009-07-22 23:57:21302 if (!ReadDictionaryValue(m, iter, val.get(), recursion))
303 return false;
304 *value = val.release();
305 break;
306 }
[email protected]ea5ef4c2013-06-13 22:50:27307 case base::Value::TYPE_LIST: {
308 scoped_ptr<base::ListValue> val(new base::ListValue());
[email protected]946d1b22009-07-22 23:57:21309 if (!ReadListValue(m, iter, val.get(), recursion))
310 return false;
311 *value = val.release();
312 break;
313 }
[email protected]e4dad9fb2009-10-06 18:15:58314 default:
[email protected]946d1b22009-07-22 23:57:21315 return false;
316 }
317
318 return true;
319}
320
[email protected]bf5aedf02012-06-04 21:18:25321} // namespace
322
323// -----------------------------------------------------------------------------
324
325LogData::LogData()
326 : routing_id(0),
327 type(0),
328 sent(0),
329 receive(0),
330 dispatch(0) {
331}
332
vmpstrbf0d713a2016-03-24 20:22:54333LogData::LogData(const LogData& other) = default;
334
[email protected]bf5aedf02012-06-04 21:18:25335LogData::~LogData() {
336}
337
[email protected]bf5aedf02012-06-04 21:18:25338void ParamTraits<bool>::Log(const param_type& p, std::string* l) {
339 l->append(p ? "true" : "false");
340}
341
rockot0457af102016-02-05 02:12:32342void ParamTraits<signed char>::GetSize(base::PickleSizer* sizer,
343 const param_type& p) {
344 sizer->AddBytes(sizeof(param_type));
345}
346
rockot502c94f2016-02-03 20:20:16347void ParamTraits<signed char>::Write(base::Pickle* m, const param_type& p) {
ortuno19ecf1842015-10-30 00:46:20348 m->WriteBytes(&p, sizeof(param_type));
349}
350
rockot502c94f2016-02-03 20:20:16351bool ParamTraits<signed char>::Read(const base::Pickle* m,
352 base::PickleIterator* iter,
353 param_type* r) {
ortuno19ecf1842015-10-30 00:46:20354 const char* data;
355 if (!iter->ReadBytes(&data, sizeof(param_type)))
356 return false;
357 memcpy(r, data, sizeof(param_type));
358 return true;
359}
360
361void ParamTraits<signed char>::Log(const param_type& p, std::string* l) {
362 l->append(base::IntToString(p));
363}
364
rockot0457af102016-02-05 02:12:32365void ParamTraits<unsigned char>::GetSize(base::PickleSizer* sizer,
366 const param_type& p) {
367 sizer->AddBytes(sizeof(param_type));
368}
369
rockot502c94f2016-02-03 20:20:16370void ParamTraits<unsigned char>::Write(base::Pickle* m, const param_type& p) {
[email protected]c1ee48d2013-07-12 23:12:28371 m->WriteBytes(&p, sizeof(param_type));
372}
373
rockot502c94f2016-02-03 20:20:16374bool ParamTraits<unsigned char>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25375 base::PickleIterator* iter,
376 param_type* r) {
[email protected]c1ee48d2013-07-12 23:12:28377 const char* data;
avi48fc13b2014-12-28 23:31:48378 if (!iter->ReadBytes(&data, sizeof(param_type)))
[email protected]c1ee48d2013-07-12 23:12:28379 return false;
380 memcpy(r, data, sizeof(param_type));
381 return true;
382}
383
384void ParamTraits<unsigned char>::Log(const param_type& p, std::string* l) {
385 l->append(base::UintToString(p));
386}
387
rockot0457af102016-02-05 02:12:32388void ParamTraits<unsigned short>::GetSize(base::PickleSizer* sizer,
389 const param_type& p) {
390 sizer->AddBytes(sizeof(param_type));
391}
392
rockot502c94f2016-02-03 20:20:16393void ParamTraits<unsigned short>::Write(base::Pickle* m, const param_type& p) {
[email protected]c1ee48d2013-07-12 23:12:28394 m->WriteBytes(&p, sizeof(param_type));
395}
396
rockot502c94f2016-02-03 20:20:16397bool ParamTraits<unsigned short>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25398 base::PickleIterator* iter,
[email protected]c1ee48d2013-07-12 23:12:28399 param_type* r) {
400 const char* data;
avi48fc13b2014-12-28 23:31:48401 if (!iter->ReadBytes(&data, sizeof(param_type)))
[email protected]c1ee48d2013-07-12 23:12:28402 return false;
403 memcpy(r, data, sizeof(param_type));
404 return true;
405}
406
407void ParamTraits<unsigned short>::Log(const param_type& p, std::string* l) {
408 l->append(base::UintToString(p));
409}
410
[email protected]252cad62010-08-18 18:33:57411void ParamTraits<int>::Log(const param_type& p, std::string* l) {
412 l->append(base::IntToString(p));
413}
414
415void ParamTraits<unsigned int>::Log(const param_type& p, std::string* l) {
416 l->append(base::UintToString(p));
417}
418
jamac78d7d82016-02-11 00:50:28419#if defined(OS_WIN) || defined(OS_LINUX) || \
420 (defined(OS_ANDROID) && defined(ARCH_CPU_64_BITS))
[email protected]252cad62010-08-18 18:33:57421void ParamTraits<long>::Log(const param_type& p, std::string* l) {
tfarina10a5c062015-09-04 18:47:57422 l->append(base::Int64ToString(static_cast<int64_t>(p)));
[email protected]252cad62010-08-18 18:33:57423}
424
425void ParamTraits<unsigned long>::Log(const param_type& p, std::string* l) {
tfarina10a5c062015-09-04 18:47:57426 l->append(base::Uint64ToString(static_cast<uint64_t>(p)));
[email protected]252cad62010-08-18 18:33:57427}
jam03d8a782016-02-10 20:13:39428#endif
[email protected]252cad62010-08-18 18:33:57429
430void ParamTraits<long long>::Log(const param_type& p, std::string* l) {
tfarina10a5c062015-09-04 18:47:57431 l->append(base::Int64ToString(static_cast<int64_t>(p)));
[email protected]252cad62010-08-18 18:33:57432}
433
434void ParamTraits<unsigned long long>::Log(const param_type& p, std::string* l) {
435 l->append(base::Uint64ToString(p));
436}
[email protected]7a4de7a62010-08-17 18:38:24437
[email protected]bf5aedf02012-06-04 21:18:25438void ParamTraits<float>::Log(const param_type& p, std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:04439 l->append(base::StringPrintf("%e", p));
[email protected]7a4de7a62010-08-17 18:38:24440}
441
rockot0457af102016-02-05 02:12:32442void ParamTraits<double>::GetSize(base::PickleSizer* sizer,
443 const param_type& p) {
444 sizer->AddBytes(sizeof(param_type));
445}
446
rockot502c94f2016-02-03 20:20:16447void ParamTraits<double>::Write(base::Pickle* m, const param_type& p) {
[email protected]48328ff2013-10-31 09:27:31448 m->WriteBytes(reinterpret_cast<const char*>(&p), sizeof(param_type));
[email protected]d84e48b2010-10-21 22:04:52449}
450
rockot502c94f2016-02-03 20:20:16451bool ParamTraits<double>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25452 base::PickleIterator* iter,
[email protected]bf5aedf02012-06-04 21:18:25453 param_type* r) {
454 const char *data;
avi48fc13b2014-12-28 23:31:48455 if (!iter->ReadBytes(&data, sizeof(*r))) {
[email protected]bf5aedf02012-06-04 21:18:25456 NOTREACHED();
457 return false;
458 }
459 memcpy(r, data, sizeof(param_type));
460 return true;
[email protected]d84e48b2010-10-21 22:04:52461}
462
[email protected]bf5aedf02012-06-04 21:18:25463void ParamTraits<double>::Log(const param_type& p, std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:04464 l->append(base::StringPrintf("%e", p));
[email protected]1d14f582011-09-02 20:42:04465}
466
[email protected]bf5aedf02012-06-04 21:18:25467
468void ParamTraits<std::string>::Log(const param_type& p, std::string* l) {
469 l->append(p);
[email protected]1d14f582011-09-02 20:42:04470}
471
[email protected]476dafb2013-12-03 00:39:26472void ParamTraits<base::string16>::Log(const param_type& p, std::string* l) {
[email protected]ad65a3e2013-12-25 18:18:01473 l->append(base::UTF16ToUTF8(p));
[email protected]bf5aedf02012-06-04 21:18:25474}
[email protected]bf5aedf02012-06-04 21:18:25475
rockot0457af102016-02-05 02:12:32476void ParamTraits<std::vector<char>>::GetSize(base::PickleSizer* sizer,
477 const param_type& p) {
478 sizer->AddData(static_cast<int>(p.size()));
479}
480
rockot502c94f2016-02-03 20:20:16481void ParamTraits<std::vector<char>>::Write(base::Pickle* m,
482 const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:25483 if (p.empty()) {
484 m->WriteData(NULL, 0);
485 } else {
486 m->WriteData(&p.front(), static_cast<int>(p.size()));
487 }
488}
489
rockot502c94f2016-02-03 20:20:16490bool ParamTraits<std::vector<char>>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25491 base::PickleIterator* iter,
492 param_type* r) {
[email protected]bf5aedf02012-06-04 21:18:25493 const char *data;
494 int data_size = 0;
avi48fc13b2014-12-28 23:31:48495 if (!iter->ReadData(&data, &data_size) || data_size < 0)
[email protected]bf5aedf02012-06-04 21:18:25496 return false;
497 r->resize(data_size);
498 if (data_size)
499 memcpy(&r->front(), data, data_size);
500 return true;
501}
502
503void ParamTraits<std::vector<char> >::Log(const param_type& p, std::string* l) {
504 LogBytes(p, l);
505}
506
rockot0457af102016-02-05 02:12:32507void ParamTraits<std::vector<unsigned char>>::GetSize(base::PickleSizer* sizer,
508 const param_type& p) {
509 sizer->AddData(static_cast<int>(p.size()));
510}
511
rockot502c94f2016-02-03 20:20:16512void ParamTraits<std::vector<unsigned char>>::Write(base::Pickle* m,
513 const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:25514 if (p.empty()) {
515 m->WriteData(NULL, 0);
516 } else {
517 m->WriteData(reinterpret_cast<const char*>(&p.front()),
518 static_cast<int>(p.size()));
519 }
520}
521
rockot502c94f2016-02-03 20:20:16522bool ParamTraits<std::vector<unsigned char>>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25523 base::PickleIterator* iter,
524 param_type* r) {
[email protected]bf5aedf02012-06-04 21:18:25525 const char *data;
526 int data_size = 0;
avi48fc13b2014-12-28 23:31:48527 if (!iter->ReadData(&data, &data_size) || data_size < 0)
[email protected]bf5aedf02012-06-04 21:18:25528 return false;
529 r->resize(data_size);
530 if (data_size)
531 memcpy(&r->front(), data, data_size);
532 return true;
533}
534
535void ParamTraits<std::vector<unsigned char> >::Log(const param_type& p,
536 std::string* l) {
537 LogBytes(p, l);
538}
539
rockot0457af102016-02-05 02:12:32540void ParamTraits<std::vector<bool>>::GetSize(base::PickleSizer* sizer,
541 const param_type& p) {
542 GetParamSize(sizer, static_cast<int>(p.size()));
543 for (size_t i = 0; i < p.size(); ++i)
544 GetParamSize(sizer, static_cast<bool>(p[i]));
545}
546
rockot502c94f2016-02-03 20:20:16547void ParamTraits<std::vector<bool>>::Write(base::Pickle* m,
548 const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:25549 WriteParam(m, static_cast<int>(p.size()));
[email protected]d4124852013-03-20 20:25:00550 // Cast to bool below is required because libc++'s
551 // vector<bool>::const_reference is different from bool, and we want to avoid
552 // writing an extra specialization of ParamTraits for it.
[email protected]bf5aedf02012-06-04 21:18:25553 for (size_t i = 0; i < p.size(); i++)
[email protected]d4124852013-03-20 20:25:00554 WriteParam(m, static_cast<bool>(p[i]));
[email protected]bf5aedf02012-06-04 21:18:25555}
556
rockot502c94f2016-02-03 20:20:16557bool ParamTraits<std::vector<bool>>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25558 base::PickleIterator* iter,
559 param_type* r) {
[email protected]bf5aedf02012-06-04 21:18:25560 int size;
561 // ReadLength() checks for < 0 itself.
avi48fc13b2014-12-28 23:31:48562 if (!iter->ReadLength(&size))
[email protected]bf5aedf02012-06-04 21:18:25563 return false;
564 r->resize(size);
565 for (int i = 0; i < size; i++) {
566 bool value;
567 if (!ReadParam(m, iter, &value))
568 return false;
569 (*r)[i] = value;
570 }
571 return true;
572}
573
574void ParamTraits<std::vector<bool> >::Log(const param_type& p, std::string* l) {
575 for (size_t i = 0; i < p.size(); ++i) {
576 if (i != 0)
577 l->push_back(' ');
[email protected]d4124852013-03-20 20:25:00578 LogParam(static_cast<bool>(p[i]), l);
[email protected]bf5aedf02012-06-04 21:18:25579 }
[email protected]d84e48b2010-10-21 22:04:52580}
581
erikcheneece6c32015-07-07 22:13:11582void ParamTraits<BrokerableAttachment::AttachmentId>::Write(
rockot502c94f2016-02-03 20:20:16583 base::Pickle* m,
erikcheneece6c32015-07-07 22:13:11584 const param_type& p) {
erikchen06faf0c2015-08-27 19:49:58585 m->WriteBytes(p.nonce, BrokerableAttachment::kNonceSize);
erikcheneece6c32015-07-07 22:13:11586}
587
588bool ParamTraits<BrokerableAttachment::AttachmentId>::Read(
rockot502c94f2016-02-03 20:20:16589 const base::Pickle* m,
erikcheneece6c32015-07-07 22:13:11590 base::PickleIterator* iter,
591 param_type* r) {
592 const char* data;
erikchen06faf0c2015-08-27 19:49:58593 if (!iter->ReadBytes(&data, BrokerableAttachment::kNonceSize))
erikcheneece6c32015-07-07 22:13:11594 return false;
595 memcpy(r->nonce, data, BrokerableAttachment::kNonceSize);
596 return true;
597}
598
599void ParamTraits<BrokerableAttachment::AttachmentId>::Log(const param_type& p,
600 std::string* l) {
601 l->append(base::HexEncode(p.nonce, BrokerableAttachment::kNonceSize));
602}
603
rockot0457af102016-02-05 02:12:32604void ParamTraits<base::DictionaryValue>::GetSize(base::PickleSizer* sizer,
605 const param_type& p) {
606 GetValueSize(sizer, &p, 0);
607}
608
rockot502c94f2016-02-03 20:20:16609void ParamTraits<base::DictionaryValue>::Write(base::Pickle* m,
[email protected]ea5ef4c2013-06-13 22:50:27610 const param_type& p) {
[email protected]946d1b22009-07-22 23:57:21611 WriteValue(m, &p, 0);
612}
613
rockot502c94f2016-02-03 20:20:16614bool ParamTraits<base::DictionaryValue>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25615 base::PickleIterator* iter,
616 param_type* r) {
[email protected]946d1b22009-07-22 23:57:21617 int type;
[email protected]0c6c1e42013-06-21 19:42:19618 if (!ReadParam(m, iter, &type) || type != base::Value::TYPE_DICTIONARY)
[email protected]946d1b22009-07-22 23:57:21619 return false;
620
621 return ReadDictionaryValue(m, iter, r, 0);
622}
623
[email protected]ea5ef4c2013-06-13 22:50:27624void ParamTraits<base::DictionaryValue>::Log(const param_type& p,
625 std::string* l) {
[email protected]946d1b22009-07-22 23:57:21626 std::string json;
estade8d046462015-05-16 01:02:34627 base::JSONWriter::Write(p, &json);
[email protected]252cad62010-08-18 18:33:57628 l->append(json);
[email protected]946d1b22009-07-22 23:57:21629}
630
[email protected]7a4de7a62010-08-17 18:38:24631#if defined(OS_POSIX)
rockot502c94f2016-02-03 20:20:16632void ParamTraits<base::FileDescriptor>::Write(base::Pickle* m,
633 const param_type& p) {
[email protected]7a4de7a62010-08-17 18:38:24634 const bool valid = p.fd >= 0;
635 WriteParam(m, valid);
636
morrita96693852014-09-24 20:11:45637 if (!valid)
638 return;
639
640 if (p.auto_close) {
morrita1aa788c2015-01-31 05:45:42641 if (!m->WriteAttachment(
642 new internal::PlatformFileAttachment(base::ScopedFD(p.fd))))
morrita96693852014-09-24 20:11:45643 NOTREACHED();
644 } else {
morrita1aa788c2015-01-31 05:45:42645 if (!m->WriteAttachment(new internal::PlatformFileAttachment(p.fd)))
[email protected]7a4de7a62010-08-17 18:38:24646 NOTREACHED();
647 }
648}
649
rockot502c94f2016-02-03 20:20:16650bool ParamTraits<base::FileDescriptor>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25651 base::PickleIterator* iter,
[email protected]7a4de7a62010-08-17 18:38:24652 param_type* r) {
morrita96693852014-09-24 20:11:45653 *r = base::FileDescriptor();
654
[email protected]7a4de7a62010-08-17 18:38:24655 bool valid;
656 if (!ReadParam(m, iter, &valid))
657 return false;
658
morrita96693852014-09-24 20:11:45659 // TODO(morrita): Seems like this should return false.
660 if (!valid)
[email protected]7a4de7a62010-08-17 18:38:24661 return true;
[email protected]7a4de7a62010-08-17 18:38:24662
rockot502c94f2016-02-03 20:20:16663 scoped_refptr<base::Pickle::Attachment> attachment;
morrita1aa788c2015-01-31 05:45:42664 if (!m->ReadAttachment(iter, &attachment))
morrita96693852014-09-24 20:11:45665 return false;
666
rockot502c94f2016-02-03 20:20:16667 *r = base::FileDescriptor(
668 static_cast<MessageAttachment*>(attachment.get())->TakePlatformFile(),
669 true);
morrita96693852014-09-24 20:11:45670 return true;
[email protected]7a4de7a62010-08-17 18:38:24671}
672
673void ParamTraits<base::FileDescriptor>::Log(const param_type& p,
[email protected]252cad62010-08-18 18:33:57674 std::string* l) {
[email protected]7a4de7a62010-08-17 18:38:24675 if (p.auto_close) {
[email protected]7d3cbc92013-03-18 22:33:04676 l->append(base::StringPrintf("FD(%d auto-close)", p.fd));
[email protected]7a4de7a62010-08-17 18:38:24677 } else {
[email protected]7d3cbc92013-03-18 22:33:04678 l->append(base::StringPrintf("FD(%d)", p.fd));
[email protected]7a4de7a62010-08-17 18:38:24679 }
680}
681#endif // defined(OS_POSIX)
682
scottmgd19b4f72015-06-19 22:51:00683#if defined(OS_MACOSX) && !defined(OS_IOS)
rockot502c94f2016-02-03 20:20:16684void ParamTraits<base::SharedMemoryHandle>::Write(base::Pickle* m,
scottmgd19b4f72015-06-19 22:51:00685 const param_type& p) {
686 m->WriteInt(p.GetType());
687
erikchen0d779702015-10-02 23:49:26688 switch (p.GetType()) {
689 case base::SharedMemoryHandle::POSIX:
690 ParamTraits<base::FileDescriptor>::Write(m, p.GetFileDescriptor());
691 break;
692 case base::SharedMemoryHandle::MACH:
erikchenaf8299d2015-10-09 19:12:06693 MachPortMac mach_port_mac(p.GetMemoryObject());
694 ParamTraits<MachPortMac>::Write(m, mach_port_mac);
695 size_t size = 0;
696 bool result = p.GetSize(&size);
697 DCHECK(result);
jam03d8a782016-02-10 20:13:39698 ParamTraits<uint32_t>::Write(m, static_cast<uint32_t>(size));
erikchen328bf3f2015-10-24 00:46:54699
700 // If the caller intended to pass ownership to the IPC stack, release a
701 // reference.
702 if (p.OwnershipPassesToIPC())
703 p.Close();
704
erikchen0d779702015-10-02 23:49:26705 break;
706 }
scottmgd19b4f72015-06-19 22:51:00707}
708
rockot502c94f2016-02-03 20:20:16709bool ParamTraits<base::SharedMemoryHandle>::Read(const base::Pickle* m,
scottmgd19b4f72015-06-19 22:51:00710 base::PickleIterator* iter,
711 param_type* r) {
712 base::SharedMemoryHandle::TypeWireFormat type;
713 if (!iter->ReadInt(&type))
714 return false;
715
716 base::SharedMemoryHandle::Type shm_type = base::SharedMemoryHandle::POSIX;
717 switch (type) {
718 case base::SharedMemoryHandle::POSIX:
719 case base::SharedMemoryHandle::MACH: {
720 shm_type = static_cast<base::SharedMemoryHandle::Type>(type);
721 break;
722 }
erikchen0d779702015-10-02 23:49:26723 default: {
scottmgd19b4f72015-06-19 22:51:00724 return false;
erikchen0d779702015-10-02 23:49:26725 }
scottmgd19b4f72015-06-19 22:51:00726 }
727
erikchen0d779702015-10-02 23:49:26728 switch (shm_type) {
729 case base::SharedMemoryHandle::POSIX: {
730 base::FileDescriptor file_descriptor;
scottmgd19b4f72015-06-19 22:51:00731
erikchen0d779702015-10-02 23:49:26732 bool success =
733 ParamTraits<base::FileDescriptor>::Read(m, iter, &file_descriptor);
734 if (!success)
735 return false;
scottmgd19b4f72015-06-19 22:51:00736
erikchen0d779702015-10-02 23:49:26737 *r = base::SharedMemoryHandle(file_descriptor.fd,
738 file_descriptor.auto_close);
739 return true;
740 }
741 case base::SharedMemoryHandle::MACH: {
erikchenaf8299d2015-10-09 19:12:06742 MachPortMac mach_port_mac;
743 if (!ParamTraits<MachPortMac>::Read(m, iter, &mach_port_mac))
744 return false;
745
jam03d8a782016-02-10 20:13:39746 uint32_t size;
747 if (!ParamTraits<uint32_t>::Read(m, iter, &size))
erikchenaf8299d2015-10-09 19:12:06748 return false;
749
jam03d8a782016-02-10 20:13:39750 *r = base::SharedMemoryHandle(mach_port_mac.get_mach_port(),
751 static_cast<size_t>(size),
erikchenaf8299d2015-10-09 19:12:06752 base::GetCurrentProcId());
erikchen0d779702015-10-02 23:49:26753 return true;
754 }
scottmgd19b4f72015-06-19 22:51:00755 }
scottmgd19b4f72015-06-19 22:51:00756}
757
758void ParamTraits<base::SharedMemoryHandle>::Log(const param_type& p,
759 std::string* l) {
erikchen0d779702015-10-02 23:49:26760 switch (p.GetType()) {
761 case base::SharedMemoryHandle::POSIX:
762 l->append("POSIX Fd: ");
763 ParamTraits<base::FileDescriptor>::Log(p.GetFileDescriptor(), l);
764 break;
765 case base::SharedMemoryHandle::MACH:
erikchenaf8299d2015-10-09 19:12:06766 l->append("Mach port: ");
767 LogParam(p.GetMemoryObject(), l);
erikchen0d779702015-10-02 23:49:26768 break;
scottmgd19b4f72015-06-19 22:51:00769 }
770}
erikchen0d779702015-10-02 23:49:26771
erikchen5ea2ab72015-09-25 22:34:31772#elif defined(OS_WIN)
rockot502c94f2016-02-03 20:20:16773void ParamTraits<base::SharedMemoryHandle>::Write(base::Pickle* m,
erikchen5ea2ab72015-09-25 22:34:31774 const param_type& p) {
erikchen5ea2ab72015-09-25 22:34:31775 m->WriteBool(p.NeedsBrokering());
776
777 if (p.NeedsBrokering()) {
778 HandleWin handle_win(p.GetHandle(), HandleWin::DUPLICATE);
779 ParamTraits<HandleWin>::Write(m, handle_win);
erikchen71bc3b22016-02-01 22:14:28780
781 // If the caller intended to pass ownership to the IPC stack, release a
782 // reference.
783 if (p.OwnershipPassesToIPC())
784 p.Close();
erikchen5ea2ab72015-09-25 22:34:31785 } else {
786 m->WriteInt(HandleToLong(p.GetHandle()));
787 }
788}
789
rockot502c94f2016-02-03 20:20:16790bool ParamTraits<base::SharedMemoryHandle>::Read(const base::Pickle* m,
erikchen5ea2ab72015-09-25 22:34:31791 base::PickleIterator* iter,
792 param_type* r) {
erikchen5ea2ab72015-09-25 22:34:31793 bool needs_brokering;
794 if (!iter->ReadBool(&needs_brokering))
795 return false;
796
797 if (needs_brokering) {
798 HandleWin handle_win;
799 if (!ParamTraits<HandleWin>::Read(m, iter, &handle_win))
800 return false;
erikchen3d87ecf72016-01-08 02:17:04801 *r = base::SharedMemoryHandle(handle_win.get_handle(),
802 base::GetCurrentProcId());
erikchen5ea2ab72015-09-25 22:34:31803 return true;
804 }
805
806 int handle_int;
807 if (!iter->ReadInt(&handle_int))
808 return false;
809 HANDLE handle = LongToHandle(handle_int);
erikchen3d87ecf72016-01-08 02:17:04810 *r = base::SharedMemoryHandle(handle, base::GetCurrentProcId());
erikchen5ea2ab72015-09-25 22:34:31811 return true;
812}
813
814void ParamTraits<base::SharedMemoryHandle>::Log(const param_type& p,
815 std::string* l) {
erikchen5ea2ab72015-09-25 22:34:31816 LogParam(p.GetHandle(), l);
817 l->append(" needs brokering: ");
818 LogParam(p.NeedsBrokering(), l);
819}
scottmgd19b4f72015-06-19 22:51:00820#endif // defined(OS_MACOSX) && !defined(OS_IOS)
821
rockot0457af102016-02-05 02:12:32822void ParamTraits<base::FilePath>::GetSize(base::PickleSizer* sizer,
823 const param_type& p) {
824 p.GetSizeForPickle(sizer);
825}
826
rockot502c94f2016-02-03 20:20:16827void ParamTraits<base::FilePath>::Write(base::Pickle* m, const param_type& p) {
[email protected]aeae59f2013-01-28 13:47:55828 p.WriteToPickle(m);
[email protected]bf5aedf02012-06-04 21:18:25829}
830
rockot502c94f2016-02-03 20:20:16831bool ParamTraits<base::FilePath>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25832 base::PickleIterator* iter,
[email protected]6d4b67a2013-02-10 04:49:30833 param_type* r) {
[email protected]aeae59f2013-01-28 13:47:55834 return r->ReadFromPickle(iter);
[email protected]bf5aedf02012-06-04 21:18:25835}
836
[email protected]6d4b67a2013-02-10 04:49:30837void ParamTraits<base::FilePath>::Log(const param_type& p, std::string* l) {
838 ParamTraits<base::FilePath::StringType>::Log(p.value(), l);
[email protected]bf5aedf02012-06-04 21:18:25839}
840
rockot0457af102016-02-05 02:12:32841void ParamTraits<base::ListValue>::GetSize(base::PickleSizer* sizer,
842 const param_type& p) {
843 GetValueSize(sizer, &p, 0);
844}
845
rockot502c94f2016-02-03 20:20:16846void ParamTraits<base::ListValue>::Write(base::Pickle* m, const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:25847 WriteValue(m, &p, 0);
848}
849
rockot502c94f2016-02-03 20:20:16850bool ParamTraits<base::ListValue>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25851 base::PickleIterator* iter,
852 param_type* r) {
[email protected]bf5aedf02012-06-04 21:18:25853 int type;
[email protected]0c6c1e42013-06-21 19:42:19854 if (!ReadParam(m, iter, &type) || type != base::Value::TYPE_LIST)
[email protected]bf5aedf02012-06-04 21:18:25855 return false;
856
857 return ReadListValue(m, iter, r, 0);
858}
859
[email protected]ea5ef4c2013-06-13 22:50:27860void ParamTraits<base::ListValue>::Log(const param_type& p, std::string* l) {
[email protected]bf5aedf02012-06-04 21:18:25861 std::string json;
estade8d046462015-05-16 01:02:34862 base::JSONWriter::Write(p, &json);
[email protected]bf5aedf02012-06-04 21:18:25863 l->append(json);
864}
865
rockot0457af102016-02-05 02:12:32866void ParamTraits<base::NullableString16>::GetSize(base::PickleSizer* sizer,
867 const param_type& p) {
868 GetParamSize(sizer, p.string());
869 GetParamSize(sizer, p.is_null());
870}
871
rockot502c94f2016-02-03 20:20:16872void ParamTraits<base::NullableString16>::Write(base::Pickle* m,
[email protected]0238a162013-06-13 13:47:46873 const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:25874 WriteParam(m, p.string());
875 WriteParam(m, p.is_null());
876}
877
rockot502c94f2016-02-03 20:20:16878bool ParamTraits<base::NullableString16>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25879 base::PickleIterator* iter,
[email protected]0238a162013-06-13 13:47:46880 param_type* r) {
[email protected]476dafb2013-12-03 00:39:26881 base::string16 string;
[email protected]bf5aedf02012-06-04 21:18:25882 if (!ReadParam(m, iter, &string))
883 return false;
884 bool is_null;
885 if (!ReadParam(m, iter, &is_null))
886 return false;
[email protected]0238a162013-06-13 13:47:46887 *r = base::NullableString16(string, is_null);
[email protected]bf5aedf02012-06-04 21:18:25888 return true;
889}
890
[email protected]0238a162013-06-13 13:47:46891void ParamTraits<base::NullableString16>::Log(const param_type& p,
892 std::string* l) {
[email protected]bf5aedf02012-06-04 21:18:25893 l->append("(");
894 LogParam(p.string(), l);
895 l->append(", ");
896 LogParam(p.is_null(), l);
897 l->append(")");
898}
899
rockot0457af102016-02-05 02:12:32900void ParamTraits<base::File::Info>::GetSize(base::PickleSizer* sizer,
901 const param_type& p) {
902 GetParamSize(sizer, p.size);
903 GetParamSize(sizer, p.is_directory);
904 GetParamSize(sizer, p.last_modified.ToDoubleT());
905 GetParamSize(sizer, p.last_accessed.ToDoubleT());
906 GetParamSize(sizer, p.creation_time.ToDoubleT());
907}
908
rockot502c94f2016-02-03 20:20:16909void ParamTraits<base::File::Info>::Write(base::Pickle* m,
[email protected]141bcc52014-01-27 21:36:00910 const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:25911 WriteParam(m, p.size);
912 WriteParam(m, p.is_directory);
913 WriteParam(m, p.last_modified.ToDoubleT());
914 WriteParam(m, p.last_accessed.ToDoubleT());
915 WriteParam(m, p.creation_time.ToDoubleT());
916}
917
rockot502c94f2016-02-03 20:20:16918bool ParamTraits<base::File::Info>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25919 base::PickleIterator* iter,
[email protected]141bcc52014-01-27 21:36:00920 param_type* p) {
[email protected]481c3e82014-07-18 01:40:47921 double last_modified, last_accessed, creation_time;
922 if (!ReadParam(m, iter, &p->size) ||
923 !ReadParam(m, iter, &p->is_directory) ||
924 !ReadParam(m, iter, &last_modified) ||
925 !ReadParam(m, iter, &last_accessed) ||
926 !ReadParam(m, iter, &creation_time))
927 return false;
928 p->last_modified = base::Time::FromDoubleT(last_modified);
929 p->last_accessed = base::Time::FromDoubleT(last_accessed);
930 p->creation_time = base::Time::FromDoubleT(creation_time);
931 return true;
[email protected]bf5aedf02012-06-04 21:18:25932}
933
[email protected]141bcc52014-01-27 21:36:00934void ParamTraits<base::File::Info>::Log(const param_type& p,
935 std::string* l) {
[email protected]bf5aedf02012-06-04 21:18:25936 l->append("(");
937 LogParam(p.size, l);
938 l->append(",");
939 LogParam(p.is_directory, l);
940 l->append(",");
941 LogParam(p.last_modified.ToDoubleT(), l);
942 l->append(",");
943 LogParam(p.last_accessed.ToDoubleT(), l);
944 l->append(",");
945 LogParam(p.creation_time.ToDoubleT(), l);
946 l->append(")");
947}
948
rockot0457af102016-02-05 02:12:32949void ParamTraits<base::Time>::GetSize(base::PickleSizer* sizer,
950 const param_type& p) {
951 sizer->AddInt64();
952}
953
rockot502c94f2016-02-03 20:20:16954void ParamTraits<base::Time>::Write(base::Pickle* m, const param_type& p) {
tfarina10a5c062015-09-04 18:47:57955 ParamTraits<int64_t>::Write(m, p.ToInternalValue());
[email protected]bf5aedf02012-06-04 21:18:25956}
957
rockot502c94f2016-02-03 20:20:16958bool ParamTraits<base::Time>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25959 base::PickleIterator* iter,
[email protected]bf5aedf02012-06-04 21:18:25960 param_type* r) {
tfarina10a5c062015-09-04 18:47:57961 int64_t value;
962 if (!ParamTraits<int64_t>::Read(m, iter, &value))
[email protected]bf5aedf02012-06-04 21:18:25963 return false;
964 *r = base::Time::FromInternalValue(value);
965 return true;
966}
967
968void ParamTraits<base::Time>::Log(const param_type& p, std::string* l) {
tfarina10a5c062015-09-04 18:47:57969 ParamTraits<int64_t>::Log(p.ToInternalValue(), l);
[email protected]bf5aedf02012-06-04 21:18:25970}
971
rockot0457af102016-02-05 02:12:32972void ParamTraits<base::TimeDelta>::GetSize(base::PickleSizer* sizer,
973 const param_type& p) {
974 sizer->AddInt64();
975}
976
rockot502c94f2016-02-03 20:20:16977void ParamTraits<base::TimeDelta>::Write(base::Pickle* m, const param_type& p) {
tfarina10a5c062015-09-04 18:47:57978 ParamTraits<int64_t>::Write(m, p.ToInternalValue());
[email protected]bf5aedf02012-06-04 21:18:25979}
980
rockot502c94f2016-02-03 20:20:16981bool ParamTraits<base::TimeDelta>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:25982 base::PickleIterator* iter,
[email protected]bf5aedf02012-06-04 21:18:25983 param_type* r) {
tfarina10a5c062015-09-04 18:47:57984 int64_t value;
985 bool ret = ParamTraits<int64_t>::Read(m, iter, &value);
[email protected]bf5aedf02012-06-04 21:18:25986 if (ret)
987 *r = base::TimeDelta::FromInternalValue(value);
988
989 return ret;
990}
991
992void ParamTraits<base::TimeDelta>::Log(const param_type& p, std::string* l) {
tfarina10a5c062015-09-04 18:47:57993 ParamTraits<int64_t>::Log(p.ToInternalValue(), l);
[email protected]bf5aedf02012-06-04 21:18:25994}
995
rockot0457af102016-02-05 02:12:32996void ParamTraits<base::TimeTicks>::GetSize(base::PickleSizer* sizer,
997 const param_type& p) {
998 sizer->AddInt64();
999}
1000
rockot502c94f2016-02-03 20:20:161001void ParamTraits<base::TimeTicks>::Write(base::Pickle* m, const param_type& p) {
tfarina10a5c062015-09-04 18:47:571002 ParamTraits<int64_t>::Write(m, p.ToInternalValue());
[email protected]bf5aedf02012-06-04 21:18:251003}
1004
rockot502c94f2016-02-03 20:20:161005bool ParamTraits<base::TimeTicks>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:251006 base::PickleIterator* iter,
[email protected]bf5aedf02012-06-04 21:18:251007 param_type* r) {
tfarina10a5c062015-09-04 18:47:571008 int64_t value;
1009 bool ret = ParamTraits<int64_t>::Read(m, iter, &value);
[email protected]bf5aedf02012-06-04 21:18:251010 if (ret)
1011 *r = base::TimeTicks::FromInternalValue(value);
1012
1013 return ret;
1014}
1015
1016void ParamTraits<base::TimeTicks>::Log(const param_type& p, std::string* l) {
tfarina10a5c062015-09-04 18:47:571017 ParamTraits<int64_t>::Log(p.ToInternalValue(), l);
[email protected]bf5aedf02012-06-04 21:18:251018}
1019
rockot502c94f2016-02-03 20:20:161020void ParamTraits<IPC::ChannelHandle>::Write(base::Pickle* m,
1021 const param_type& p) {
[email protected]a7c03d4f32012-01-24 02:36:051022#if defined(OS_WIN)
1023 // On Windows marshalling pipe handle is not supported.
1024 DCHECK(p.pipe.handle == NULL);
1025#endif // defined (OS_WIN)
[email protected]7a4de7a62010-08-17 18:38:241026 WriteParam(m, p.name);
1027#if defined(OS_POSIX)
1028 WriteParam(m, p.socket);
1029#endif
1030}
1031
rockot502c94f2016-02-03 20:20:161032bool ParamTraits<IPC::ChannelHandle>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:251033 base::PickleIterator* iter,
[email protected]7a4de7a62010-08-17 18:38:241034 param_type* r) {
1035 return ReadParam(m, iter, &r->name)
1036#if defined(OS_POSIX)
1037 && ReadParam(m, iter, &r->socket)
1038#endif
1039 ;
1040}
1041
1042void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p,
[email protected]252cad62010-08-18 18:33:571043 std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:041044 l->append(base::StringPrintf("ChannelHandle(%s", p.name.c_str()));
[email protected]7a4de7a62010-08-17 18:38:241045#if defined(OS_POSIX)
[email protected]3cd3bce2011-09-23 10:32:191046 l->append(", ");
[email protected]7a4de7a62010-08-17 18:38:241047 ParamTraits<base::FileDescriptor>::Log(p.socket, l);
1048#endif
[email protected]252cad62010-08-18 18:33:571049 l->append(")");
[email protected]7a4de7a62010-08-17 18:38:241050}
1051
rockot0457af102016-02-05 02:12:321052void ParamTraits<LogData>::GetSize(base::PickleSizer* sizer,
1053 const param_type& p) {
1054 GetParamSize(sizer, p.channel);
1055 GetParamSize(sizer, p.routing_id);
1056 GetParamSize(sizer, p.type);
1057 GetParamSize(sizer, p.flags);
1058 GetParamSize(sizer, p.sent);
1059 GetParamSize(sizer, p.receive);
1060 GetParamSize(sizer, p.dispatch);
1061 GetParamSize(sizer, p.message_name);
1062 GetParamSize(sizer, p.params);
1063}
1064
rockot502c94f2016-02-03 20:20:161065void ParamTraits<LogData>::Write(base::Pickle* m, const param_type& p) {
[email protected]20f0487a2010-09-30 20:06:301066 WriteParam(m, p.channel);
1067 WriteParam(m, p.routing_id);
[email protected]8bf55ca2011-10-17 22:15:271068 WriteParam(m, p.type);
[email protected]20f0487a2010-09-30 20:06:301069 WriteParam(m, p.flags);
1070 WriteParam(m, p.sent);
1071 WriteParam(m, p.receive);
1072 WriteParam(m, p.dispatch);
[email protected]bae578e92012-11-15 03:17:451073 WriteParam(m, p.message_name);
[email protected]20f0487a2010-09-30 20:06:301074 WriteParam(m, p.params);
1075}
1076
rockot502c94f2016-02-03 20:20:161077bool ParamTraits<LogData>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:251078 base::PickleIterator* iter,
[email protected]ce208f872012-03-07 20:42:561079 param_type* r) {
[email protected]8bf55ca2011-10-17 22:15:271080 return
[email protected]20f0487a2010-09-30 20:06:301081 ReadParam(m, iter, &r->channel) &&
1082 ReadParam(m, iter, &r->routing_id) &&
[email protected]8bf55ca2011-10-17 22:15:271083 ReadParam(m, iter, &r->type) &&
[email protected]20f0487a2010-09-30 20:06:301084 ReadParam(m, iter, &r->flags) &&
1085 ReadParam(m, iter, &r->sent) &&
1086 ReadParam(m, iter, &r->receive) &&
1087 ReadParam(m, iter, &r->dispatch) &&
[email protected]bae578e92012-11-15 03:17:451088 ReadParam(m, iter, &r->message_name) &&
[email protected]20f0487a2010-09-30 20:06:301089 ReadParam(m, iter, &r->params);
[email protected]20f0487a2010-09-30 20:06:301090}
1091
[email protected]bf5aedf02012-06-04 21:18:251092void ParamTraits<LogData>::Log(const param_type& p, std::string* l) {
1093 // Doesn't make sense to implement this!
1094}
1095
rockot502c94f2016-02-03 20:20:161096void ParamTraits<Message>::Write(base::Pickle* m, const Message& p) {
[email protected]34d48612012-06-29 00:05:041097#if defined(OS_POSIX)
1098 // We don't serialize the file descriptors in the nested message, so there
1099 // better not be any.
morrita1aa788c2015-01-31 05:45:421100 DCHECK(!p.HasAttachments());
[email protected]34d48612012-06-29 00:05:041101#endif
1102
1103 // Don't just write out the message. This is used to send messages between
1104 // NaCl (Posix environment) and the browser (could be on Windows). The message
1105 // header formats differ between these systems (so does handle sharing, but
1106 // we already asserted we don't have any handles). So just write out the
1107 // parts of the header we use.
1108 //
1109 // Be careful also to use only explicitly-sized types. The NaCl environment
1110 // could be 64-bit and the host browser could be 32-bits. The nested message
1111 // may or may not be safe to send between 32-bit and 64-bit systems, but we
1112 // leave that up to the code sending the message to ensure.
tfarina10a5c062015-09-04 18:47:571113 m->WriteUInt32(static_cast<uint32_t>(p.routing_id()));
[email protected]34d48612012-06-29 00:05:041114 m->WriteUInt32(p.type());
1115 m->WriteUInt32(p.flags());
tfarina10a5c062015-09-04 18:47:571116 m->WriteData(p.payload(), static_cast<uint32_t>(p.payload_size()));
[email protected]bf5aedf02012-06-04 21:18:251117}
1118
rockot502c94f2016-02-03 20:20:161119bool ParamTraits<Message>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:251120 base::PickleIterator* iter,
[email protected]bf5aedf02012-06-04 21:18:251121 Message* r) {
tfarina10a5c062015-09-04 18:47:571122 uint32_t routing_id, type, flags;
avi48fc13b2014-12-28 23:31:481123 if (!iter->ReadUInt32(&routing_id) ||
1124 !iter->ReadUInt32(&type) ||
1125 !iter->ReadUInt32(&flags))
[email protected]bf5aedf02012-06-04 21:18:251126 return false;
[email protected]34d48612012-06-29 00:05:041127
1128 int payload_size;
1129 const char* payload;
avi48fc13b2014-12-28 23:31:481130 if (!iter->ReadData(&payload, &payload_size))
[email protected]bf5aedf02012-06-04 21:18:251131 return false;
[email protected]34d48612012-06-29 00:05:041132
tfarina10a5c062015-09-04 18:47:571133 r->SetHeaderValues(static_cast<int32_t>(routing_id), type, flags);
[email protected]34d48612012-06-29 00:05:041134 return r->WriteBytes(payload, payload_size);
[email protected]bf5aedf02012-06-04 21:18:251135}
1136
1137void ParamTraits<Message>::Log(const Message& p, std::string* l) {
1138 l->append("<IPC::Message>");
1139}
1140
1141#if defined(OS_WIN)
rockot0457af102016-02-05 02:12:321142void ParamTraits<HANDLE>::GetSize(base::PickleSizer* sizer,
1143 const param_type& p) {
1144 sizer->AddInt();
1145}
1146
[email protected]bf5aedf02012-06-04 21:18:251147// Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64
[email protected]4a635b72013-03-04 02:29:031148// bit systems. That's why we use the Windows macros to convert to 32 bits.
rockot502c94f2016-02-03 20:20:161149void ParamTraits<HANDLE>::Write(base::Pickle* m, const param_type& p) {
[email protected]4a635b72013-03-04 02:29:031150 m->WriteInt(HandleToLong(p));
[email protected]bf5aedf02012-06-04 21:18:251151}
1152
rockot502c94f2016-02-03 20:20:161153bool ParamTraits<HANDLE>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:251154 base::PickleIterator* iter,
[email protected]bf5aedf02012-06-04 21:18:251155 param_type* r) {
tfarina10a5c062015-09-04 18:47:571156 int32_t temp;
avi48fc13b2014-12-28 23:31:481157 if (!iter->ReadInt(&temp))
[email protected]bf5aedf02012-06-04 21:18:251158 return false;
[email protected]4a635b72013-03-04 02:29:031159 *r = LongToHandle(temp);
[email protected]bf5aedf02012-06-04 21:18:251160 return true;
1161}
1162
1163void ParamTraits<HANDLE>::Log(const param_type& p, std::string* l) {
brucedawson5604a11d2015-10-06 19:22:001164 l->append(base::StringPrintf("0x%p", p));
[email protected]bf5aedf02012-06-04 21:18:251165}
1166
rockot0457af102016-02-05 02:12:321167void ParamTraits<LOGFONT>::GetSize(base::PickleSizer* sizer,
1168 const param_type& p) {
1169 sizer->AddData(sizeof(LOGFONT));
1170}
1171
rockot502c94f2016-02-03 20:20:161172void ParamTraits<LOGFONT>::Write(base::Pickle* m, const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:251173 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT));
1174}
1175
rockot502c94f2016-02-03 20:20:161176bool ParamTraits<LOGFONT>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:251177 base::PickleIterator* iter,
[email protected]bf5aedf02012-06-04 21:18:251178 param_type* r) {
1179 const char *data;
1180 int data_size = 0;
avi48fc13b2014-12-28 23:31:481181 if (iter->ReadData(&data, &data_size) && data_size == sizeof(LOGFONT)) {
[email protected]2e02cfe82012-11-21 00:58:001182 const LOGFONT *font = reinterpret_cast<LOGFONT*>(const_cast<char*>(data));
1183 if (_tcsnlen(font->lfFaceName, LF_FACESIZE) < LF_FACESIZE) {
1184 memcpy(r, data, sizeof(LOGFONT));
1185 return true;
1186 }
[email protected]bf5aedf02012-06-04 21:18:251187 }
1188
[email protected]2e02cfe82012-11-21 00:58:001189 NOTREACHED();
1190 return false;
[email protected]bf5aedf02012-06-04 21:18:251191}
1192
1193void ParamTraits<LOGFONT>::Log(const param_type& p, std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:041194 l->append(base::StringPrintf("<LOGFONT>"));
[email protected]bf5aedf02012-06-04 21:18:251195}
1196
rockot0457af102016-02-05 02:12:321197void ParamTraits<MSG>::GetSize(base::PickleSizer* sizer, const param_type& p) {
1198 sizer->AddData(sizeof(MSG));
1199}
1200
rockot502c94f2016-02-03 20:20:161201void ParamTraits<MSG>::Write(base::Pickle* m, const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:251202 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG));
1203}
1204
rockot502c94f2016-02-03 20:20:161205bool ParamTraits<MSG>::Read(const base::Pickle* m,
brettwbd4d7112015-06-03 04:29:251206 base::PickleIterator* iter,
[email protected]bf5aedf02012-06-04 21:18:251207 param_type* r) {
1208 const char *data;
1209 int data_size = 0;
avi48fc13b2014-12-28 23:31:481210 bool result = iter->ReadData(&data, &data_size);
[email protected]bf5aedf02012-06-04 21:18:251211 if (result && data_size == sizeof(MSG)) {
1212 memcpy(r, data, sizeof(MSG));
1213 } else {
1214 result = false;
1215 NOTREACHED();
1216 }
1217
1218 return result;
1219}
1220
1221void ParamTraits<MSG>::Log(const param_type& p, std::string* l) {
1222 l->append("<MSG>");
1223}
1224
1225#endif // OS_WIN
1226
[email protected]946d1b22009-07-22 23:57:211227} // namespace IPC