blob: 7368a0db22101222f067a6ef5350c27fe6238caf [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
[email protected]57999812013-02-24 05:40:527#include "base/files/file_path.h"
[email protected]93d49d72009-10-23 20:00:208#include "base/json/json_writer.h"
[email protected]3b63f8f42011-03-28 01:54:159#include "base/memory/scoped_ptr.h"
[email protected]0238a162013-06-13 13:47:4610#include "base/strings/nullable_string16.h"
[email protected]4aa794a12013-06-11 06:32:1811#include "base/strings/string_number_conversions.h"
[email protected]906265872013-06-07 22:40:4512#include "base/strings/utf_string_conversions.h"
[email protected]b43e5562013-06-28 15:20:0213#include "base/time/time.h"
[email protected]946d1b22009-07-22 23:57:2114#include "base/values.h"
[email protected]bf5aedf02012-06-04 21:18:2515#include "ipc/ipc_channel_handle.h"
morrita1aa788c2015-01-31 05:45:4216#include "ipc/ipc_message_attachment.h"
morrita4b5c28e22015-01-14 21:17:0617#include "ipc/ipc_message_attachment_set.h"
[email protected]bf5aedf02012-06-04 21:18:2518
morrita1aa788c2015-01-31 05:45:4219#if defined(OS_POSIX)
20#include "ipc/ipc_platform_file_attachment_posix.h"
21#endif
22
morrita4b5c28e22015-01-14 21:17:0623#if defined(OS_WIN)
[email protected]2e02cfe82012-11-21 00:58:0024#include <tchar.h>
[email protected]7a4de7a62010-08-17 18:38:2425#endif
[email protected]946d1b22009-07-22 23:57:2126
27namespace IPC {
28
[email protected]bf5aedf02012-06-04 21:18:2529namespace {
30
[email protected]946d1b22009-07-22 23:57:2131const int kMaxRecursionDepth = 100;
32
[email protected]bf5aedf02012-06-04 21:18:2533template<typename CharType>
34void LogBytes(const std::vector<CharType>& data, std::string* out) {
35#if defined(OS_WIN)
36 // Windows has a GUI for logging, which can handle arbitrary binary data.
37 for (size_t i = 0; i < data.size(); ++i)
38 out->push_back(data[i]);
39#else
40 // On POSIX, we log to stdout, which we assume can display ASCII.
41 static const size_t kMaxBytesToLog = 100;
42 for (size_t i = 0; i < std::min(data.size(), kMaxBytesToLog); ++i) {
43 if (isprint(data[i]))
44 out->push_back(data[i]);
45 else
[email protected]7d3cbc92013-03-18 22:33:0446 out->append(
47 base::StringPrintf("[%02X]", static_cast<unsigned char>(data[i])));
[email protected]bf5aedf02012-06-04 21:18:2548 }
49 if (data.size() > kMaxBytesToLog) {
[email protected]f8660f82013-03-30 17:29:2850 out->append(base::StringPrintf(
51 " and %u more bytes",
52 static_cast<unsigned>(data.size() - kMaxBytesToLog)));
[email protected]bf5aedf02012-06-04 21:18:2553 }
54#endif
55}
[email protected]946d1b22009-07-22 23:57:2156
[email protected]ea5ef4c2013-06-13 22:50:2757bool ReadValue(const Message* m, PickleIterator* iter, base::Value** value,
[email protected]bf5aedf02012-06-04 21:18:2558 int recursion);
[email protected]946d1b22009-07-22 23:57:2159
[email protected]ea5ef4c2013-06-13 22:50:2760void WriteValue(Message* m, const base::Value* value, int recursion) {
[email protected]dbc761a2012-07-26 01:29:2161 bool result;
[email protected]946d1b22009-07-22 23:57:2162 if (recursion > kMaxRecursionDepth) {
63 LOG(WARNING) << "Max recursion depth hit in WriteValue.";
64 return;
65 }
66
67 m->WriteInt(value->GetType());
68
69 switch (value->GetType()) {
[email protected]ea5ef4c2013-06-13 22:50:2770 case base::Value::TYPE_NULL:
[email protected]946d1b22009-07-22 23:57:2171 break;
[email protected]ea5ef4c2013-06-13 22:50:2772 case base::Value::TYPE_BOOLEAN: {
[email protected]946d1b22009-07-22 23:57:2173 bool val;
[email protected]dbc761a2012-07-26 01:29:2174 result = value->GetAsBoolean(&val);
75 DCHECK(result);
[email protected]946d1b22009-07-22 23:57:2176 WriteParam(m, val);
77 break;
78 }
[email protected]ea5ef4c2013-06-13 22:50:2779 case base::Value::TYPE_INTEGER: {
[email protected]946d1b22009-07-22 23:57:2180 int val;
[email protected]dbc761a2012-07-26 01:29:2181 result = value->GetAsInteger(&val);
82 DCHECK(result);
[email protected]946d1b22009-07-22 23:57:2183 WriteParam(m, val);
84 break;
85 }
[email protected]ea5ef4c2013-06-13 22:50:2786 case base::Value::TYPE_DOUBLE: {
[email protected]946d1b22009-07-22 23:57:2187 double val;
[email protected]dbc761a2012-07-26 01:29:2188 result = value->GetAsDouble(&val);
89 DCHECK(result);
[email protected]946d1b22009-07-22 23:57:2190 WriteParam(m, val);
91 break;
92 }
[email protected]ea5ef4c2013-06-13 22:50:2793 case base::Value::TYPE_STRING: {
[email protected]946d1b22009-07-22 23:57:2194 std::string val;
[email protected]dbc761a2012-07-26 01:29:2195 result = value->GetAsString(&val);
96 DCHECK(result);
[email protected]946d1b22009-07-22 23:57:2197 WriteParam(m, val);
98 break;
99 }
[email protected]ea5ef4c2013-06-13 22:50:27100 case base::Value::TYPE_BINARY: {
[email protected]b59ea312011-08-05 18:20:05101 const base::BinaryValue* binary =
102 static_cast<const base::BinaryValue*>(value);
[email protected]7ee1a44c2010-07-23 14:18:59103 m->WriteData(binary->GetBuffer(), static_cast<int>(binary->GetSize()));
[email protected]e4dad9fb2009-10-06 18:15:58104 break;
[email protected]946d1b22009-07-22 23:57:21105 }
[email protected]ea5ef4c2013-06-13 22:50:27106 case base::Value::TYPE_DICTIONARY: {
107 const base::DictionaryValue* dict =
108 static_cast<const base::DictionaryValue*>(value);
[email protected]946d1b22009-07-22 23:57:21109
[email protected]4dad9ad82009-11-25 20:47:52110 WriteParam(m, static_cast<int>(dict->size()));
[email protected]946d1b22009-07-22 23:57:21111
[email protected]ea5ef4c2013-06-13 22:50:27112 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd();
113 it.Advance()) {
[email protected]a899c0b02013-01-18 14:43:27114 WriteParam(m, it.key());
115 WriteValue(m, &it.value(), recursion + 1);
[email protected]946d1b22009-07-22 23:57:21116 }
117 break;
118 }
[email protected]ea5ef4c2013-06-13 22:50:27119 case base::Value::TYPE_LIST: {
120 const base::ListValue* list = static_cast<const base::ListValue*>(value);
[email protected]946d1b22009-07-22 23:57:21121 WriteParam(m, static_cast<int>(list->GetSize()));
[email protected]ea5ef4c2013-06-13 22:50:27122 for (base::ListValue::const_iterator it = list->begin();
123 it != list->end(); ++it) {
[email protected]a899c0b02013-01-18 14:43:27124 WriteValue(m, *it, recursion + 1);
[email protected]946d1b22009-07-22 23:57:21125 }
126 break;
127 }
128 }
129}
130
131// Helper for ReadValue that reads a DictionaryValue into a pre-allocated
132// object.
[email protected]bf5aedf02012-06-04 21:18:25133bool ReadDictionaryValue(const Message* m, PickleIterator* iter,
[email protected]ea5ef4c2013-06-13 22:50:27134 base::DictionaryValue* value, int recursion) {
[email protected]946d1b22009-07-22 23:57:21135 int size;
136 if (!ReadParam(m, iter, &size))
137 return false;
138
139 for (int i = 0; i < size; ++i) {
[email protected]e7b418b2010-07-30 19:47:47140 std::string key;
[email protected]0c6c1e42013-06-21 19:42:19141 base::Value* subval;
[email protected]946d1b22009-07-22 23:57:21142 if (!ReadParam(m, iter, &key) ||
143 !ReadValue(m, iter, &subval, recursion + 1))
144 return false;
[email protected]d8b4aa42011-08-19 05:59:57145 value->SetWithoutPathExpansion(key, subval);
[email protected]946d1b22009-07-22 23:57:21146 }
147
148 return true;
149}
150
151// Helper for ReadValue that reads a ReadListValue into a pre-allocated
152// object.
[email protected]bf5aedf02012-06-04 21:18:25153bool ReadListValue(const Message* m, PickleIterator* iter,
[email protected]ea5ef4c2013-06-13 22:50:27154 base::ListValue* value, int recursion) {
[email protected]946d1b22009-07-22 23:57:21155 int size;
156 if (!ReadParam(m, iter, &size))
157 return false;
158
159 for (int i = 0; i < size; ++i) {
[email protected]ea5ef4c2013-06-13 22:50:27160 base::Value* subval;
[email protected]946d1b22009-07-22 23:57:21161 if (!ReadValue(m, iter, &subval, recursion + 1))
162 return false;
163 value->Set(i, subval);
164 }
165
166 return true;
167}
168
[email protected]ea5ef4c2013-06-13 22:50:27169bool ReadValue(const Message* m, PickleIterator* iter, base::Value** value,
[email protected]bf5aedf02012-06-04 21:18:25170 int recursion) {
[email protected]946d1b22009-07-22 23:57:21171 if (recursion > kMaxRecursionDepth) {
172 LOG(WARNING) << "Max recursion depth hit in ReadValue.";
173 return false;
174 }
175
176 int type;
177 if (!ReadParam(m, iter, &type))
178 return false;
179
180 switch (type) {
[email protected]ea5ef4c2013-06-13 22:50:27181 case base::Value::TYPE_NULL:
182 *value = base::Value::CreateNullValue();
[email protected]946d1b22009-07-22 23:57:21183 break;
[email protected]ea5ef4c2013-06-13 22:50:27184 case base::Value::TYPE_BOOLEAN: {
[email protected]946d1b22009-07-22 23:57:21185 bool val;
186 if (!ReadParam(m, iter, &val))
187 return false;
[email protected]4038a132013-01-30 05:24:07188 *value = new base::FundamentalValue(val);
[email protected]946d1b22009-07-22 23:57:21189 break;
190 }
[email protected]ea5ef4c2013-06-13 22:50:27191 case base::Value::TYPE_INTEGER: {
[email protected]946d1b22009-07-22 23:57:21192 int val;
193 if (!ReadParam(m, iter, &val))
194 return false;
[email protected]4038a132013-01-30 05:24:07195 *value = new base::FundamentalValue(val);
[email protected]946d1b22009-07-22 23:57:21196 break;
197 }
[email protected]ea5ef4c2013-06-13 22:50:27198 case base::Value::TYPE_DOUBLE: {
[email protected]946d1b22009-07-22 23:57:21199 double val;
200 if (!ReadParam(m, iter, &val))
201 return false;
[email protected]4038a132013-01-30 05:24:07202 *value = new base::FundamentalValue(val);
[email protected]946d1b22009-07-22 23:57:21203 break;
204 }
[email protected]ea5ef4c2013-06-13 22:50:27205 case base::Value::TYPE_STRING: {
[email protected]946d1b22009-07-22 23:57:21206 std::string val;
207 if (!ReadParam(m, iter, &val))
208 return false;
[email protected]4038a132013-01-30 05:24:07209 *value = new base::StringValue(val);
[email protected]946d1b22009-07-22 23:57:21210 break;
211 }
[email protected]ea5ef4c2013-06-13 22:50:27212 case base::Value::TYPE_BINARY: {
[email protected]e4dad9fb2009-10-06 18:15:58213 const char* data;
214 int length;
avi48fc13b2014-12-28 23:31:48215 if (!iter->ReadData(&data, &length))
[email protected]e4dad9fb2009-10-06 18:15:58216 return false;
[email protected]b59ea312011-08-05 18:20:05217 *value = base::BinaryValue::CreateWithCopiedBuffer(data, length);
[email protected]946d1b22009-07-22 23:57:21218 break;
219 }
[email protected]ea5ef4c2013-06-13 22:50:27220 case base::Value::TYPE_DICTIONARY: {
221 scoped_ptr<base::DictionaryValue> val(new base::DictionaryValue());
[email protected]946d1b22009-07-22 23:57:21222 if (!ReadDictionaryValue(m, iter, val.get(), recursion))
223 return false;
224 *value = val.release();
225 break;
226 }
[email protected]ea5ef4c2013-06-13 22:50:27227 case base::Value::TYPE_LIST: {
228 scoped_ptr<base::ListValue> val(new base::ListValue());
[email protected]946d1b22009-07-22 23:57:21229 if (!ReadListValue(m, iter, val.get(), recursion))
230 return false;
231 *value = val.release();
232 break;
233 }
[email protected]e4dad9fb2009-10-06 18:15:58234 default:
[email protected]946d1b22009-07-22 23:57:21235 return false;
236 }
237
238 return true;
239}
240
[email protected]bf5aedf02012-06-04 21:18:25241} // namespace
242
243// -----------------------------------------------------------------------------
244
245LogData::LogData()
246 : routing_id(0),
247 type(0),
248 sent(0),
249 receive(0),
250 dispatch(0) {
251}
252
253LogData::~LogData() {
254}
255
[email protected]bf5aedf02012-06-04 21:18:25256void ParamTraits<bool>::Log(const param_type& p, std::string* l) {
257 l->append(p ? "true" : "false");
258}
259
[email protected]c1ee48d2013-07-12 23:12:28260void ParamTraits<unsigned char>::Write(Message* m, const param_type& p) {
261 m->WriteBytes(&p, sizeof(param_type));
262}
263
264bool ParamTraits<unsigned char>::Read(const Message* m, PickleIterator* iter,
265 param_type* r) {
266 const char* data;
avi48fc13b2014-12-28 23:31:48267 if (!iter->ReadBytes(&data, sizeof(param_type)))
[email protected]c1ee48d2013-07-12 23:12:28268 return false;
269 memcpy(r, data, sizeof(param_type));
270 return true;
271}
272
273void ParamTraits<unsigned char>::Log(const param_type& p, std::string* l) {
274 l->append(base::UintToString(p));
275}
276
277void ParamTraits<unsigned short>::Write(Message* m, const param_type& p) {
278 m->WriteBytes(&p, sizeof(param_type));
279}
280
281bool ParamTraits<unsigned short>::Read(const Message* m, PickleIterator* iter,
282 param_type* r) {
283 const char* data;
avi48fc13b2014-12-28 23:31:48284 if (!iter->ReadBytes(&data, sizeof(param_type)))
[email protected]c1ee48d2013-07-12 23:12:28285 return false;
286 memcpy(r, data, sizeof(param_type));
287 return true;
288}
289
290void ParamTraits<unsigned short>::Log(const param_type& p, std::string* l) {
291 l->append(base::UintToString(p));
292}
293
[email protected]252cad62010-08-18 18:33:57294void ParamTraits<int>::Log(const param_type& p, std::string* l) {
295 l->append(base::IntToString(p));
296}
297
298void ParamTraits<unsigned int>::Log(const param_type& p, std::string* l) {
299 l->append(base::UintToString(p));
300}
301
302void ParamTraits<long>::Log(const param_type& p, std::string* l) {
303 l->append(base::Int64ToString(static_cast<int64>(p)));
304}
305
306void ParamTraits<unsigned long>::Log(const param_type& p, std::string* l) {
307 l->append(base::Uint64ToString(static_cast<uint64>(p)));
308}
309
310void ParamTraits<long long>::Log(const param_type& p, std::string* l) {
311 l->append(base::Int64ToString(static_cast<int64>(p)));
312}
313
314void ParamTraits<unsigned long long>::Log(const param_type& p, std::string* l) {
315 l->append(base::Uint64ToString(p));
316}
[email protected]7a4de7a62010-08-17 18:38:24317
[email protected]bf5aedf02012-06-04 21:18:25318void ParamTraits<float>::Log(const param_type& p, std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:04319 l->append(base::StringPrintf("%e", p));
[email protected]7a4de7a62010-08-17 18:38:24320}
321
[email protected]bf5aedf02012-06-04 21:18:25322void ParamTraits<double>::Write(Message* m, const param_type& p) {
[email protected]48328ff2013-10-31 09:27:31323 m->WriteBytes(reinterpret_cast<const char*>(&p), sizeof(param_type));
[email protected]d84e48b2010-10-21 22:04:52324}
325
[email protected]bf5aedf02012-06-04 21:18:25326bool ParamTraits<double>::Read(const Message* m, PickleIterator* iter,
327 param_type* r) {
328 const char *data;
avi48fc13b2014-12-28 23:31:48329 if (!iter->ReadBytes(&data, sizeof(*r))) {
[email protected]bf5aedf02012-06-04 21:18:25330 NOTREACHED();
331 return false;
332 }
333 memcpy(r, data, sizeof(param_type));
334 return true;
[email protected]d84e48b2010-10-21 22:04:52335}
336
[email protected]bf5aedf02012-06-04 21:18:25337void ParamTraits<double>::Log(const param_type& p, std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:04338 l->append(base::StringPrintf("%e", p));
[email protected]1d14f582011-09-02 20:42:04339}
340
[email protected]bf5aedf02012-06-04 21:18:25341
342void ParamTraits<std::string>::Log(const param_type& p, std::string* l) {
343 l->append(p);
[email protected]1d14f582011-09-02 20:42:04344}
345
[email protected]bf5aedf02012-06-04 21:18:25346void ParamTraits<std::wstring>::Log(const param_type& p, std::string* l) {
[email protected]ad65a3e2013-12-25 18:18:01347 l->append(base::WideToUTF8(p));
[email protected]1d14f582011-09-02 20:42:04348}
349
[email protected]bf5aedf02012-06-04 21:18:25350#if !defined(WCHAR_T_IS_UTF16)
[email protected]476dafb2013-12-03 00:39:26351void ParamTraits<base::string16>::Log(const param_type& p, std::string* l) {
[email protected]ad65a3e2013-12-25 18:18:01352 l->append(base::UTF16ToUTF8(p));
[email protected]bf5aedf02012-06-04 21:18:25353}
354#endif
355
356void ParamTraits<std::vector<char> >::Write(Message* m, const param_type& p) {
357 if (p.empty()) {
358 m->WriteData(NULL, 0);
359 } else {
360 m->WriteData(&p.front(), static_cast<int>(p.size()));
361 }
362}
363
364bool ParamTraits<std::vector<char> >::Read(const Message* m,
365 PickleIterator* iter,
366 param_type* r) {
367 const char *data;
368 int data_size = 0;
avi48fc13b2014-12-28 23:31:48369 if (!iter->ReadData(&data, &data_size) || data_size < 0)
[email protected]bf5aedf02012-06-04 21:18:25370 return false;
371 r->resize(data_size);
372 if (data_size)
373 memcpy(&r->front(), data, data_size);
374 return true;
375}
376
377void ParamTraits<std::vector<char> >::Log(const param_type& p, std::string* l) {
378 LogBytes(p, l);
379}
380
381void ParamTraits<std::vector<unsigned char> >::Write(Message* m,
382 const param_type& p) {
383 if (p.empty()) {
384 m->WriteData(NULL, 0);
385 } else {
386 m->WriteData(reinterpret_cast<const char*>(&p.front()),
387 static_cast<int>(p.size()));
388 }
389}
390
391bool ParamTraits<std::vector<unsigned char> >::Read(const Message* m,
392 PickleIterator* iter,
393 param_type* r) {
394 const char *data;
395 int data_size = 0;
avi48fc13b2014-12-28 23:31:48396 if (!iter->ReadData(&data, &data_size) || data_size < 0)
[email protected]bf5aedf02012-06-04 21:18:25397 return false;
398 r->resize(data_size);
399 if (data_size)
400 memcpy(&r->front(), data, data_size);
401 return true;
402}
403
404void ParamTraits<std::vector<unsigned char> >::Log(const param_type& p,
405 std::string* l) {
406 LogBytes(p, l);
407}
408
409void ParamTraits<std::vector<bool> >::Write(Message* m, const param_type& p) {
410 WriteParam(m, static_cast<int>(p.size()));
[email protected]d4124852013-03-20 20:25:00411 // Cast to bool below is required because libc++'s
412 // vector<bool>::const_reference is different from bool, and we want to avoid
413 // writing an extra specialization of ParamTraits for it.
[email protected]bf5aedf02012-06-04 21:18:25414 for (size_t i = 0; i < p.size(); i++)
[email protected]d4124852013-03-20 20:25:00415 WriteParam(m, static_cast<bool>(p[i]));
[email protected]bf5aedf02012-06-04 21:18:25416}
417
418bool ParamTraits<std::vector<bool> >::Read(const Message* m,
419 PickleIterator* iter,
420 param_type* r) {
421 int size;
422 // ReadLength() checks for < 0 itself.
avi48fc13b2014-12-28 23:31:48423 if (!iter->ReadLength(&size))
[email protected]bf5aedf02012-06-04 21:18:25424 return false;
425 r->resize(size);
426 for (int i = 0; i < size; i++) {
427 bool value;
428 if (!ReadParam(m, iter, &value))
429 return false;
430 (*r)[i] = value;
431 }
432 return true;
433}
434
435void ParamTraits<std::vector<bool> >::Log(const param_type& p, std::string* l) {
436 for (size_t i = 0; i < p.size(); ++i) {
437 if (i != 0)
438 l->push_back(' ');
[email protected]d4124852013-03-20 20:25:00439 LogParam(static_cast<bool>(p[i]), l);
[email protected]bf5aedf02012-06-04 21:18:25440 }
[email protected]d84e48b2010-10-21 22:04:52441}
442
[email protected]ea5ef4c2013-06-13 22:50:27443void ParamTraits<base::DictionaryValue>::Write(Message* m,
444 const param_type& p) {
[email protected]946d1b22009-07-22 23:57:21445 WriteValue(m, &p, 0);
446}
447
[email protected]ea5ef4c2013-06-13 22:50:27448bool ParamTraits<base::DictionaryValue>::Read(
[email protected]ce208f872012-03-07 20:42:56449 const Message* m, PickleIterator* iter, param_type* r) {
[email protected]946d1b22009-07-22 23:57:21450 int type;
[email protected]0c6c1e42013-06-21 19:42:19451 if (!ReadParam(m, iter, &type) || type != base::Value::TYPE_DICTIONARY)
[email protected]946d1b22009-07-22 23:57:21452 return false;
453
454 return ReadDictionaryValue(m, iter, r, 0);
455}
456
[email protected]ea5ef4c2013-06-13 22:50:27457void ParamTraits<base::DictionaryValue>::Log(const param_type& p,
458 std::string* l) {
[email protected]946d1b22009-07-22 23:57:21459 std::string json;
[email protected]4abb4602012-03-16 01:59:55460 base::JSONWriter::Write(&p, &json);
[email protected]252cad62010-08-18 18:33:57461 l->append(json);
[email protected]946d1b22009-07-22 23:57:21462}
463
[email protected]7a4de7a62010-08-17 18:38:24464#if defined(OS_POSIX)
465void ParamTraits<base::FileDescriptor>::Write(Message* m, const param_type& p) {
466 const bool valid = p.fd >= 0;
467 WriteParam(m, valid);
468
morrita96693852014-09-24 20:11:45469 if (!valid)
470 return;
471
472 if (p.auto_close) {
morrita1aa788c2015-01-31 05:45:42473 if (!m->WriteAttachment(
474 new internal::PlatformFileAttachment(base::ScopedFD(p.fd))))
morrita96693852014-09-24 20:11:45475 NOTREACHED();
476 } else {
morrita1aa788c2015-01-31 05:45:42477 if (!m->WriteAttachment(new internal::PlatformFileAttachment(p.fd)))
[email protected]7a4de7a62010-08-17 18:38:24478 NOTREACHED();
479 }
480}
481
[email protected]ce208f872012-03-07 20:42:56482bool ParamTraits<base::FileDescriptor>::Read(const Message* m,
483 PickleIterator* iter,
[email protected]7a4de7a62010-08-17 18:38:24484 param_type* r) {
morrita96693852014-09-24 20:11:45485 *r = base::FileDescriptor();
486
[email protected]7a4de7a62010-08-17 18:38:24487 bool valid;
488 if (!ReadParam(m, iter, &valid))
489 return false;
490
morrita96693852014-09-24 20:11:45491 // TODO(morrita): Seems like this should return false.
492 if (!valid)
[email protected]7a4de7a62010-08-17 18:38:24493 return true;
[email protected]7a4de7a62010-08-17 18:38:24494
morrita1aa788c2015-01-31 05:45:42495 scoped_refptr<MessageAttachment> attachment;
496 if (!m->ReadAttachment(iter, &attachment))
morrita96693852014-09-24 20:11:45497 return false;
498
morrita1aa788c2015-01-31 05:45:42499 *r = base::FileDescriptor(attachment->TakePlatformFile(), true);
morrita96693852014-09-24 20:11:45500 return true;
[email protected]7a4de7a62010-08-17 18:38:24501}
502
503void ParamTraits<base::FileDescriptor>::Log(const param_type& p,
[email protected]252cad62010-08-18 18:33:57504 std::string* l) {
[email protected]7a4de7a62010-08-17 18:38:24505 if (p.auto_close) {
[email protected]7d3cbc92013-03-18 22:33:04506 l->append(base::StringPrintf("FD(%d auto-close)", p.fd));
[email protected]7a4de7a62010-08-17 18:38:24507 } else {
[email protected]7d3cbc92013-03-18 22:33:04508 l->append(base::StringPrintf("FD(%d)", p.fd));
[email protected]7a4de7a62010-08-17 18:38:24509 }
510}
511#endif // defined(OS_POSIX)
512
[email protected]6d4b67a2013-02-10 04:49:30513void ParamTraits<base::FilePath>::Write(Message* m, const param_type& p) {
[email protected]aeae59f2013-01-28 13:47:55514 p.WriteToPickle(m);
[email protected]bf5aedf02012-06-04 21:18:25515}
516
[email protected]6d4b67a2013-02-10 04:49:30517bool ParamTraits<base::FilePath>::Read(const Message* m,
518 PickleIterator* iter,
519 param_type* r) {
[email protected]aeae59f2013-01-28 13:47:55520 return r->ReadFromPickle(iter);
[email protected]bf5aedf02012-06-04 21:18:25521}
522
[email protected]6d4b67a2013-02-10 04:49:30523void ParamTraits<base::FilePath>::Log(const param_type& p, std::string* l) {
524 ParamTraits<base::FilePath::StringType>::Log(p.value(), l);
[email protected]bf5aedf02012-06-04 21:18:25525}
526
[email protected]ea5ef4c2013-06-13 22:50:27527void ParamTraits<base::ListValue>::Write(Message* m, const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:25528 WriteValue(m, &p, 0);
529}
530
[email protected]ea5ef4c2013-06-13 22:50:27531bool ParamTraits<base::ListValue>::Read(
[email protected]bf5aedf02012-06-04 21:18:25532 const Message* m, PickleIterator* iter, param_type* r) {
533 int type;
[email protected]0c6c1e42013-06-21 19:42:19534 if (!ReadParam(m, iter, &type) || type != base::Value::TYPE_LIST)
[email protected]bf5aedf02012-06-04 21:18:25535 return false;
536
537 return ReadListValue(m, iter, r, 0);
538}
539
[email protected]ea5ef4c2013-06-13 22:50:27540void ParamTraits<base::ListValue>::Log(const param_type& p, std::string* l) {
[email protected]bf5aedf02012-06-04 21:18:25541 std::string json;
542 base::JSONWriter::Write(&p, &json);
543 l->append(json);
544}
545
[email protected]0238a162013-06-13 13:47:46546void ParamTraits<base::NullableString16>::Write(Message* m,
547 const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:25548 WriteParam(m, p.string());
549 WriteParam(m, p.is_null());
550}
551
[email protected]0238a162013-06-13 13:47:46552bool ParamTraits<base::NullableString16>::Read(const Message* m,
553 PickleIterator* iter,
554 param_type* r) {
[email protected]476dafb2013-12-03 00:39:26555 base::string16 string;
[email protected]bf5aedf02012-06-04 21:18:25556 if (!ReadParam(m, iter, &string))
557 return false;
558 bool is_null;
559 if (!ReadParam(m, iter, &is_null))
560 return false;
[email protected]0238a162013-06-13 13:47:46561 *r = base::NullableString16(string, is_null);
[email protected]bf5aedf02012-06-04 21:18:25562 return true;
563}
564
[email protected]0238a162013-06-13 13:47:46565void ParamTraits<base::NullableString16>::Log(const param_type& p,
566 std::string* l) {
[email protected]bf5aedf02012-06-04 21:18:25567 l->append("(");
568 LogParam(p.string(), l);
569 l->append(", ");
570 LogParam(p.is_null(), l);
571 l->append(")");
572}
573
[email protected]141bcc52014-01-27 21:36:00574void ParamTraits<base::File::Info>::Write(Message* m,
575 const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:25576 WriteParam(m, p.size);
577 WriteParam(m, p.is_directory);
578 WriteParam(m, p.last_modified.ToDoubleT());
579 WriteParam(m, p.last_accessed.ToDoubleT());
580 WriteParam(m, p.creation_time.ToDoubleT());
581}
582
[email protected]141bcc52014-01-27 21:36:00583bool ParamTraits<base::File::Info>::Read(const Message* m,
584 PickleIterator* iter,
585 param_type* p) {
[email protected]481c3e82014-07-18 01:40:47586 double last_modified, last_accessed, creation_time;
587 if (!ReadParam(m, iter, &p->size) ||
588 !ReadParam(m, iter, &p->is_directory) ||
589 !ReadParam(m, iter, &last_modified) ||
590 !ReadParam(m, iter, &last_accessed) ||
591 !ReadParam(m, iter, &creation_time))
592 return false;
593 p->last_modified = base::Time::FromDoubleT(last_modified);
594 p->last_accessed = base::Time::FromDoubleT(last_accessed);
595 p->creation_time = base::Time::FromDoubleT(creation_time);
596 return true;
[email protected]bf5aedf02012-06-04 21:18:25597}
598
[email protected]141bcc52014-01-27 21:36:00599void ParamTraits<base::File::Info>::Log(const param_type& p,
600 std::string* l) {
[email protected]bf5aedf02012-06-04 21:18:25601 l->append("(");
602 LogParam(p.size, l);
603 l->append(",");
604 LogParam(p.is_directory, l);
605 l->append(",");
606 LogParam(p.last_modified.ToDoubleT(), l);
607 l->append(",");
608 LogParam(p.last_accessed.ToDoubleT(), l);
609 l->append(",");
610 LogParam(p.creation_time.ToDoubleT(), l);
611 l->append(")");
612}
613
614void ParamTraits<base::Time>::Write(Message* m, const param_type& p) {
615 ParamTraits<int64>::Write(m, p.ToInternalValue());
616}
617
618bool ParamTraits<base::Time>::Read(const Message* m, PickleIterator* iter,
619 param_type* r) {
620 int64 value;
621 if (!ParamTraits<int64>::Read(m, iter, &value))
622 return false;
623 *r = base::Time::FromInternalValue(value);
624 return true;
625}
626
627void ParamTraits<base::Time>::Log(const param_type& p, std::string* l) {
628 ParamTraits<int64>::Log(p.ToInternalValue(), l);
629}
630
631void ParamTraits<base::TimeDelta>::Write(Message* m, const param_type& p) {
632 ParamTraits<int64>::Write(m, p.ToInternalValue());
633}
634
635bool ParamTraits<base::TimeDelta>::Read(const Message* m,
636 PickleIterator* iter,
637 param_type* r) {
638 int64 value;
639 bool ret = ParamTraits<int64>::Read(m, iter, &value);
640 if (ret)
641 *r = base::TimeDelta::FromInternalValue(value);
642
643 return ret;
644}
645
646void ParamTraits<base::TimeDelta>::Log(const param_type& p, std::string* l) {
647 ParamTraits<int64>::Log(p.ToInternalValue(), l);
648}
649
650void ParamTraits<base::TimeTicks>::Write(Message* m, const param_type& p) {
651 ParamTraits<int64>::Write(m, p.ToInternalValue());
652}
653
654bool ParamTraits<base::TimeTicks>::Read(const Message* m,
655 PickleIterator* iter,
656 param_type* r) {
657 int64 value;
658 bool ret = ParamTraits<int64>::Read(m, iter, &value);
659 if (ret)
660 *r = base::TimeTicks::FromInternalValue(value);
661
662 return ret;
663}
664
665void ParamTraits<base::TimeTicks>::Log(const param_type& p, std::string* l) {
666 ParamTraits<int64>::Log(p.ToInternalValue(), l);
667}
668
[email protected]7a4de7a62010-08-17 18:38:24669void ParamTraits<IPC::ChannelHandle>::Write(Message* m, const param_type& p) {
[email protected]a7c03d4f32012-01-24 02:36:05670#if defined(OS_WIN)
671 // On Windows marshalling pipe handle is not supported.
672 DCHECK(p.pipe.handle == NULL);
673#endif // defined (OS_WIN)
[email protected]7a4de7a62010-08-17 18:38:24674 WriteParam(m, p.name);
675#if defined(OS_POSIX)
676 WriteParam(m, p.socket);
677#endif
678}
679
[email protected]ce208f872012-03-07 20:42:56680bool ParamTraits<IPC::ChannelHandle>::Read(const Message* m,
681 PickleIterator* iter,
[email protected]7a4de7a62010-08-17 18:38:24682 param_type* r) {
683 return ReadParam(m, iter, &r->name)
684#if defined(OS_POSIX)
685 && ReadParam(m, iter, &r->socket)
686#endif
687 ;
688}
689
690void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p,
[email protected]252cad62010-08-18 18:33:57691 std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:04692 l->append(base::StringPrintf("ChannelHandle(%s", p.name.c_str()));
[email protected]7a4de7a62010-08-17 18:38:24693#if defined(OS_POSIX)
[email protected]3cd3bce2011-09-23 10:32:19694 l->append(", ");
[email protected]7a4de7a62010-08-17 18:38:24695 ParamTraits<base::FileDescriptor>::Log(p.socket, l);
696#endif
[email protected]252cad62010-08-18 18:33:57697 l->append(")");
[email protected]7a4de7a62010-08-17 18:38:24698}
699
[email protected]20f0487a2010-09-30 20:06:30700void ParamTraits<LogData>::Write(Message* m, const param_type& p) {
701 WriteParam(m, p.channel);
702 WriteParam(m, p.routing_id);
[email protected]8bf55ca2011-10-17 22:15:27703 WriteParam(m, p.type);
[email protected]20f0487a2010-09-30 20:06:30704 WriteParam(m, p.flags);
705 WriteParam(m, p.sent);
706 WriteParam(m, p.receive);
707 WriteParam(m, p.dispatch);
[email protected]bae578e92012-11-15 03:17:45708 WriteParam(m, p.message_name);
[email protected]20f0487a2010-09-30 20:06:30709 WriteParam(m, p.params);
710}
711
[email protected]ce208f872012-03-07 20:42:56712bool ParamTraits<LogData>::Read(const Message* m,
713 PickleIterator* iter,
714 param_type* r) {
[email protected]8bf55ca2011-10-17 22:15:27715 return
[email protected]20f0487a2010-09-30 20:06:30716 ReadParam(m, iter, &r->channel) &&
717 ReadParam(m, iter, &r->routing_id) &&
[email protected]8bf55ca2011-10-17 22:15:27718 ReadParam(m, iter, &r->type) &&
[email protected]20f0487a2010-09-30 20:06:30719 ReadParam(m, iter, &r->flags) &&
720 ReadParam(m, iter, &r->sent) &&
721 ReadParam(m, iter, &r->receive) &&
722 ReadParam(m, iter, &r->dispatch) &&
[email protected]bae578e92012-11-15 03:17:45723 ReadParam(m, iter, &r->message_name) &&
[email protected]20f0487a2010-09-30 20:06:30724 ReadParam(m, iter, &r->params);
[email protected]20f0487a2010-09-30 20:06:30725}
726
[email protected]bf5aedf02012-06-04 21:18:25727void ParamTraits<LogData>::Log(const param_type& p, std::string* l) {
728 // Doesn't make sense to implement this!
729}
730
731void ParamTraits<Message>::Write(Message* m, const Message& p) {
[email protected]34d48612012-06-29 00:05:04732#if defined(OS_POSIX)
733 // We don't serialize the file descriptors in the nested message, so there
734 // better not be any.
morrita1aa788c2015-01-31 05:45:42735 DCHECK(!p.HasAttachments());
[email protected]34d48612012-06-29 00:05:04736#endif
737
738 // Don't just write out the message. This is used to send messages between
739 // NaCl (Posix environment) and the browser (could be on Windows). The message
740 // header formats differ between these systems (so does handle sharing, but
741 // we already asserted we don't have any handles). So just write out the
742 // parts of the header we use.
743 //
744 // Be careful also to use only explicitly-sized types. The NaCl environment
745 // could be 64-bit and the host browser could be 32-bits. The nested message
746 // may or may not be safe to send between 32-bit and 64-bit systems, but we
747 // leave that up to the code sending the message to ensure.
748 m->WriteUInt32(static_cast<uint32>(p.routing_id()));
749 m->WriteUInt32(p.type());
750 m->WriteUInt32(p.flags());
751 m->WriteData(p.payload(), static_cast<uint32>(p.payload_size()));
[email protected]bf5aedf02012-06-04 21:18:25752}
753
754bool ParamTraits<Message>::Read(const Message* m, PickleIterator* iter,
755 Message* r) {
[email protected]34d48612012-06-29 00:05:04756 uint32 routing_id, type, flags;
avi48fc13b2014-12-28 23:31:48757 if (!iter->ReadUInt32(&routing_id) ||
758 !iter->ReadUInt32(&type) ||
759 !iter->ReadUInt32(&flags))
[email protected]bf5aedf02012-06-04 21:18:25760 return false;
[email protected]34d48612012-06-29 00:05:04761
762 int payload_size;
763 const char* payload;
avi48fc13b2014-12-28 23:31:48764 if (!iter->ReadData(&payload, &payload_size))
[email protected]bf5aedf02012-06-04 21:18:25765 return false;
[email protected]34d48612012-06-29 00:05:04766
767 r->SetHeaderValues(static_cast<int32>(routing_id), type, flags);
768 return r->WriteBytes(payload, payload_size);
[email protected]bf5aedf02012-06-04 21:18:25769}
770
771void ParamTraits<Message>::Log(const Message& p, std::string* l) {
772 l->append("<IPC::Message>");
773}
774
775#if defined(OS_WIN)
776// Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64
[email protected]4a635b72013-03-04 02:29:03777// bit systems. That's why we use the Windows macros to convert to 32 bits.
[email protected]bf5aedf02012-06-04 21:18:25778void ParamTraits<HANDLE>::Write(Message* m, const param_type& p) {
[email protected]4a635b72013-03-04 02:29:03779 m->WriteInt(HandleToLong(p));
[email protected]bf5aedf02012-06-04 21:18:25780}
781
782bool ParamTraits<HANDLE>::Read(const Message* m, PickleIterator* iter,
783 param_type* r) {
[email protected]4a635b72013-03-04 02:29:03784 int32 temp;
avi48fc13b2014-12-28 23:31:48785 if (!iter->ReadInt(&temp))
[email protected]bf5aedf02012-06-04 21:18:25786 return false;
[email protected]4a635b72013-03-04 02:29:03787 *r = LongToHandle(temp);
[email protected]bf5aedf02012-06-04 21:18:25788 return true;
789}
790
791void ParamTraits<HANDLE>::Log(const param_type& p, std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:04792 l->append(base::StringPrintf("0x%X", p));
[email protected]bf5aedf02012-06-04 21:18:25793}
794
795void ParamTraits<LOGFONT>::Write(Message* m, const param_type& p) {
796 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT));
797}
798
799bool ParamTraits<LOGFONT>::Read(const Message* m, PickleIterator* iter,
800 param_type* r) {
801 const char *data;
802 int data_size = 0;
avi48fc13b2014-12-28 23:31:48803 if (iter->ReadData(&data, &data_size) && data_size == sizeof(LOGFONT)) {
[email protected]2e02cfe82012-11-21 00:58:00804 const LOGFONT *font = reinterpret_cast<LOGFONT*>(const_cast<char*>(data));
805 if (_tcsnlen(font->lfFaceName, LF_FACESIZE) < LF_FACESIZE) {
806 memcpy(r, data, sizeof(LOGFONT));
807 return true;
808 }
[email protected]bf5aedf02012-06-04 21:18:25809 }
810
[email protected]2e02cfe82012-11-21 00:58:00811 NOTREACHED();
812 return false;
[email protected]bf5aedf02012-06-04 21:18:25813}
814
815void ParamTraits<LOGFONT>::Log(const param_type& p, std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:04816 l->append(base::StringPrintf("<LOGFONT>"));
[email protected]bf5aedf02012-06-04 21:18:25817}
818
819void ParamTraits<MSG>::Write(Message* m, const param_type& p) {
820 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG));
821}
822
823bool ParamTraits<MSG>::Read(const Message* m, PickleIterator* iter,
824 param_type* r) {
825 const char *data;
826 int data_size = 0;
avi48fc13b2014-12-28 23:31:48827 bool result = iter->ReadData(&data, &data_size);
[email protected]bf5aedf02012-06-04 21:18:25828 if (result && data_size == sizeof(MSG)) {
829 memcpy(r, data, sizeof(MSG));
830 } else {
831 result = false;
832 NOTREACHED();
833 }
834
835 return result;
836}
837
838void ParamTraits<MSG>::Log(const param_type& p, std::string* l) {
839 l->append("<MSG>");
840}
841
842#endif // OS_WIN
843
[email protected]946d1b22009-07-22 23:57:21844} // namespace IPC