blob: adb14d1d56c5ef64cbf56e93553989cbe614ee9d [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:
estadea68b0442015-05-12 18:11:50182 *value = base::Value::CreateNullValue().release();
[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]476dafb2013-12-03 00:39:26346void ParamTraits<base::string16>::Log(const param_type& p, std::string* l) {
[email protected]ad65a3e2013-12-25 18:18:01347 l->append(base::UTF16ToUTF8(p));
[email protected]bf5aedf02012-06-04 21:18:25348}
[email protected]bf5aedf02012-06-04 21:18:25349
350void ParamTraits<std::vector<char> >::Write(Message* m, const param_type& p) {
351 if (p.empty()) {
352 m->WriteData(NULL, 0);
353 } else {
354 m->WriteData(&p.front(), static_cast<int>(p.size()));
355 }
356}
357
358bool ParamTraits<std::vector<char> >::Read(const Message* m,
359 PickleIterator* iter,
360 param_type* r) {
361 const char *data;
362 int data_size = 0;
avi48fc13b2014-12-28 23:31:48363 if (!iter->ReadData(&data, &data_size) || data_size < 0)
[email protected]bf5aedf02012-06-04 21:18:25364 return false;
365 r->resize(data_size);
366 if (data_size)
367 memcpy(&r->front(), data, data_size);
368 return true;
369}
370
371void ParamTraits<std::vector<char> >::Log(const param_type& p, std::string* l) {
372 LogBytes(p, l);
373}
374
375void ParamTraits<std::vector<unsigned char> >::Write(Message* m,
376 const param_type& p) {
377 if (p.empty()) {
378 m->WriteData(NULL, 0);
379 } else {
380 m->WriteData(reinterpret_cast<const char*>(&p.front()),
381 static_cast<int>(p.size()));
382 }
383}
384
385bool ParamTraits<std::vector<unsigned char> >::Read(const Message* m,
386 PickleIterator* iter,
387 param_type* r) {
388 const char *data;
389 int data_size = 0;
avi48fc13b2014-12-28 23:31:48390 if (!iter->ReadData(&data, &data_size) || data_size < 0)
[email protected]bf5aedf02012-06-04 21:18:25391 return false;
392 r->resize(data_size);
393 if (data_size)
394 memcpy(&r->front(), data, data_size);
395 return true;
396}
397
398void ParamTraits<std::vector<unsigned char> >::Log(const param_type& p,
399 std::string* l) {
400 LogBytes(p, l);
401}
402
403void ParamTraits<std::vector<bool> >::Write(Message* m, const param_type& p) {
404 WriteParam(m, static_cast<int>(p.size()));
[email protected]d4124852013-03-20 20:25:00405 // Cast to bool below is required because libc++'s
406 // vector<bool>::const_reference is different from bool, and we want to avoid
407 // writing an extra specialization of ParamTraits for it.
[email protected]bf5aedf02012-06-04 21:18:25408 for (size_t i = 0; i < p.size(); i++)
[email protected]d4124852013-03-20 20:25:00409 WriteParam(m, static_cast<bool>(p[i]));
[email protected]bf5aedf02012-06-04 21:18:25410}
411
412bool ParamTraits<std::vector<bool> >::Read(const Message* m,
413 PickleIterator* iter,
414 param_type* r) {
415 int size;
416 // ReadLength() checks for < 0 itself.
avi48fc13b2014-12-28 23:31:48417 if (!iter->ReadLength(&size))
[email protected]bf5aedf02012-06-04 21:18:25418 return false;
419 r->resize(size);
420 for (int i = 0; i < size; i++) {
421 bool value;
422 if (!ReadParam(m, iter, &value))
423 return false;
424 (*r)[i] = value;
425 }
426 return true;
427}
428
429void ParamTraits<std::vector<bool> >::Log(const param_type& p, std::string* l) {
430 for (size_t i = 0; i < p.size(); ++i) {
431 if (i != 0)
432 l->push_back(' ');
[email protected]d4124852013-03-20 20:25:00433 LogParam(static_cast<bool>(p[i]), l);
[email protected]bf5aedf02012-06-04 21:18:25434 }
[email protected]d84e48b2010-10-21 22:04:52435}
436
[email protected]ea5ef4c2013-06-13 22:50:27437void ParamTraits<base::DictionaryValue>::Write(Message* m,
438 const param_type& p) {
[email protected]946d1b22009-07-22 23:57:21439 WriteValue(m, &p, 0);
440}
441
[email protected]ea5ef4c2013-06-13 22:50:27442bool ParamTraits<base::DictionaryValue>::Read(
[email protected]ce208f872012-03-07 20:42:56443 const Message* m, PickleIterator* iter, param_type* r) {
[email protected]946d1b22009-07-22 23:57:21444 int type;
[email protected]0c6c1e42013-06-21 19:42:19445 if (!ReadParam(m, iter, &type) || type != base::Value::TYPE_DICTIONARY)
[email protected]946d1b22009-07-22 23:57:21446 return false;
447
448 return ReadDictionaryValue(m, iter, r, 0);
449}
450
[email protected]ea5ef4c2013-06-13 22:50:27451void ParamTraits<base::DictionaryValue>::Log(const param_type& p,
452 std::string* l) {
[email protected]946d1b22009-07-22 23:57:21453 std::string json;
[email protected]4abb4602012-03-16 01:59:55454 base::JSONWriter::Write(&p, &json);
[email protected]252cad62010-08-18 18:33:57455 l->append(json);
[email protected]946d1b22009-07-22 23:57:21456}
457
[email protected]7a4de7a62010-08-17 18:38:24458#if defined(OS_POSIX)
459void ParamTraits<base::FileDescriptor>::Write(Message* m, const param_type& p) {
460 const bool valid = p.fd >= 0;
461 WriteParam(m, valid);
462
morrita96693852014-09-24 20:11:45463 if (!valid)
464 return;
465
466 if (p.auto_close) {
morrita1aa788c2015-01-31 05:45:42467 if (!m->WriteAttachment(
468 new internal::PlatformFileAttachment(base::ScopedFD(p.fd))))
morrita96693852014-09-24 20:11:45469 NOTREACHED();
470 } else {
morrita1aa788c2015-01-31 05:45:42471 if (!m->WriteAttachment(new internal::PlatformFileAttachment(p.fd)))
[email protected]7a4de7a62010-08-17 18:38:24472 NOTREACHED();
473 }
474}
475
[email protected]ce208f872012-03-07 20:42:56476bool ParamTraits<base::FileDescriptor>::Read(const Message* m,
477 PickleIterator* iter,
[email protected]7a4de7a62010-08-17 18:38:24478 param_type* r) {
morrita96693852014-09-24 20:11:45479 *r = base::FileDescriptor();
480
[email protected]7a4de7a62010-08-17 18:38:24481 bool valid;
482 if (!ReadParam(m, iter, &valid))
483 return false;
484
morrita96693852014-09-24 20:11:45485 // TODO(morrita): Seems like this should return false.
486 if (!valid)
[email protected]7a4de7a62010-08-17 18:38:24487 return true;
[email protected]7a4de7a62010-08-17 18:38:24488
morrita1aa788c2015-01-31 05:45:42489 scoped_refptr<MessageAttachment> attachment;
490 if (!m->ReadAttachment(iter, &attachment))
morrita96693852014-09-24 20:11:45491 return false;
492
morrita1aa788c2015-01-31 05:45:42493 *r = base::FileDescriptor(attachment->TakePlatformFile(), true);
morrita96693852014-09-24 20:11:45494 return true;
[email protected]7a4de7a62010-08-17 18:38:24495}
496
497void ParamTraits<base::FileDescriptor>::Log(const param_type& p,
[email protected]252cad62010-08-18 18:33:57498 std::string* l) {
[email protected]7a4de7a62010-08-17 18:38:24499 if (p.auto_close) {
[email protected]7d3cbc92013-03-18 22:33:04500 l->append(base::StringPrintf("FD(%d auto-close)", p.fd));
[email protected]7a4de7a62010-08-17 18:38:24501 } else {
[email protected]7d3cbc92013-03-18 22:33:04502 l->append(base::StringPrintf("FD(%d)", p.fd));
[email protected]7a4de7a62010-08-17 18:38:24503 }
504}
505#endif // defined(OS_POSIX)
506
[email protected]6d4b67a2013-02-10 04:49:30507void ParamTraits<base::FilePath>::Write(Message* m, const param_type& p) {
[email protected]aeae59f2013-01-28 13:47:55508 p.WriteToPickle(m);
[email protected]bf5aedf02012-06-04 21:18:25509}
510
[email protected]6d4b67a2013-02-10 04:49:30511bool ParamTraits<base::FilePath>::Read(const Message* m,
512 PickleIterator* iter,
513 param_type* r) {
[email protected]aeae59f2013-01-28 13:47:55514 return r->ReadFromPickle(iter);
[email protected]bf5aedf02012-06-04 21:18:25515}
516
[email protected]6d4b67a2013-02-10 04:49:30517void ParamTraits<base::FilePath>::Log(const param_type& p, std::string* l) {
518 ParamTraits<base::FilePath::StringType>::Log(p.value(), l);
[email protected]bf5aedf02012-06-04 21:18:25519}
520
[email protected]ea5ef4c2013-06-13 22:50:27521void ParamTraits<base::ListValue>::Write(Message* m, const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:25522 WriteValue(m, &p, 0);
523}
524
[email protected]ea5ef4c2013-06-13 22:50:27525bool ParamTraits<base::ListValue>::Read(
[email protected]bf5aedf02012-06-04 21:18:25526 const Message* m, PickleIterator* iter, param_type* r) {
527 int type;
[email protected]0c6c1e42013-06-21 19:42:19528 if (!ReadParam(m, iter, &type) || type != base::Value::TYPE_LIST)
[email protected]bf5aedf02012-06-04 21:18:25529 return false;
530
531 return ReadListValue(m, iter, r, 0);
532}
533
[email protected]ea5ef4c2013-06-13 22:50:27534void ParamTraits<base::ListValue>::Log(const param_type& p, std::string* l) {
[email protected]bf5aedf02012-06-04 21:18:25535 std::string json;
536 base::JSONWriter::Write(&p, &json);
537 l->append(json);
538}
539
[email protected]0238a162013-06-13 13:47:46540void ParamTraits<base::NullableString16>::Write(Message* m,
541 const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:25542 WriteParam(m, p.string());
543 WriteParam(m, p.is_null());
544}
545
[email protected]0238a162013-06-13 13:47:46546bool ParamTraits<base::NullableString16>::Read(const Message* m,
547 PickleIterator* iter,
548 param_type* r) {
[email protected]476dafb2013-12-03 00:39:26549 base::string16 string;
[email protected]bf5aedf02012-06-04 21:18:25550 if (!ReadParam(m, iter, &string))
551 return false;
552 bool is_null;
553 if (!ReadParam(m, iter, &is_null))
554 return false;
[email protected]0238a162013-06-13 13:47:46555 *r = base::NullableString16(string, is_null);
[email protected]bf5aedf02012-06-04 21:18:25556 return true;
557}
558
[email protected]0238a162013-06-13 13:47:46559void ParamTraits<base::NullableString16>::Log(const param_type& p,
560 std::string* l) {
[email protected]bf5aedf02012-06-04 21:18:25561 l->append("(");
562 LogParam(p.string(), l);
563 l->append(", ");
564 LogParam(p.is_null(), l);
565 l->append(")");
566}
567
[email protected]141bcc52014-01-27 21:36:00568void ParamTraits<base::File::Info>::Write(Message* m,
569 const param_type& p) {
[email protected]bf5aedf02012-06-04 21:18:25570 WriteParam(m, p.size);
571 WriteParam(m, p.is_directory);
572 WriteParam(m, p.last_modified.ToDoubleT());
573 WriteParam(m, p.last_accessed.ToDoubleT());
574 WriteParam(m, p.creation_time.ToDoubleT());
575}
576
[email protected]141bcc52014-01-27 21:36:00577bool ParamTraits<base::File::Info>::Read(const Message* m,
578 PickleIterator* iter,
579 param_type* p) {
[email protected]481c3e82014-07-18 01:40:47580 double last_modified, last_accessed, creation_time;
581 if (!ReadParam(m, iter, &p->size) ||
582 !ReadParam(m, iter, &p->is_directory) ||
583 !ReadParam(m, iter, &last_modified) ||
584 !ReadParam(m, iter, &last_accessed) ||
585 !ReadParam(m, iter, &creation_time))
586 return false;
587 p->last_modified = base::Time::FromDoubleT(last_modified);
588 p->last_accessed = base::Time::FromDoubleT(last_accessed);
589 p->creation_time = base::Time::FromDoubleT(creation_time);
590 return true;
[email protected]bf5aedf02012-06-04 21:18:25591}
592
[email protected]141bcc52014-01-27 21:36:00593void ParamTraits<base::File::Info>::Log(const param_type& p,
594 std::string* l) {
[email protected]bf5aedf02012-06-04 21:18:25595 l->append("(");
596 LogParam(p.size, l);
597 l->append(",");
598 LogParam(p.is_directory, l);
599 l->append(",");
600 LogParam(p.last_modified.ToDoubleT(), l);
601 l->append(",");
602 LogParam(p.last_accessed.ToDoubleT(), l);
603 l->append(",");
604 LogParam(p.creation_time.ToDoubleT(), l);
605 l->append(")");
606}
607
608void ParamTraits<base::Time>::Write(Message* m, const param_type& p) {
609 ParamTraits<int64>::Write(m, p.ToInternalValue());
610}
611
612bool ParamTraits<base::Time>::Read(const Message* m, PickleIterator* iter,
613 param_type* r) {
614 int64 value;
615 if (!ParamTraits<int64>::Read(m, iter, &value))
616 return false;
617 *r = base::Time::FromInternalValue(value);
618 return true;
619}
620
621void ParamTraits<base::Time>::Log(const param_type& p, std::string* l) {
622 ParamTraits<int64>::Log(p.ToInternalValue(), l);
623}
624
625void ParamTraits<base::TimeDelta>::Write(Message* m, const param_type& p) {
626 ParamTraits<int64>::Write(m, p.ToInternalValue());
627}
628
629bool ParamTraits<base::TimeDelta>::Read(const Message* m,
630 PickleIterator* iter,
631 param_type* r) {
632 int64 value;
633 bool ret = ParamTraits<int64>::Read(m, iter, &value);
634 if (ret)
635 *r = base::TimeDelta::FromInternalValue(value);
636
637 return ret;
638}
639
640void ParamTraits<base::TimeDelta>::Log(const param_type& p, std::string* l) {
641 ParamTraits<int64>::Log(p.ToInternalValue(), l);
642}
643
644void ParamTraits<base::TimeTicks>::Write(Message* m, const param_type& p) {
645 ParamTraits<int64>::Write(m, p.ToInternalValue());
646}
647
648bool ParamTraits<base::TimeTicks>::Read(const Message* m,
649 PickleIterator* iter,
650 param_type* r) {
651 int64 value;
652 bool ret = ParamTraits<int64>::Read(m, iter, &value);
653 if (ret)
654 *r = base::TimeTicks::FromInternalValue(value);
655
656 return ret;
657}
658
659void ParamTraits<base::TimeTicks>::Log(const param_type& p, std::string* l) {
660 ParamTraits<int64>::Log(p.ToInternalValue(), l);
661}
662
[email protected]7a4de7a62010-08-17 18:38:24663void ParamTraits<IPC::ChannelHandle>::Write(Message* m, const param_type& p) {
[email protected]a7c03d4f32012-01-24 02:36:05664#if defined(OS_WIN)
665 // On Windows marshalling pipe handle is not supported.
666 DCHECK(p.pipe.handle == NULL);
667#endif // defined (OS_WIN)
[email protected]7a4de7a62010-08-17 18:38:24668 WriteParam(m, p.name);
669#if defined(OS_POSIX)
670 WriteParam(m, p.socket);
671#endif
672}
673
[email protected]ce208f872012-03-07 20:42:56674bool ParamTraits<IPC::ChannelHandle>::Read(const Message* m,
675 PickleIterator* iter,
[email protected]7a4de7a62010-08-17 18:38:24676 param_type* r) {
677 return ReadParam(m, iter, &r->name)
678#if defined(OS_POSIX)
679 && ReadParam(m, iter, &r->socket)
680#endif
681 ;
682}
683
684void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p,
[email protected]252cad62010-08-18 18:33:57685 std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:04686 l->append(base::StringPrintf("ChannelHandle(%s", p.name.c_str()));
[email protected]7a4de7a62010-08-17 18:38:24687#if defined(OS_POSIX)
[email protected]3cd3bce2011-09-23 10:32:19688 l->append(", ");
[email protected]7a4de7a62010-08-17 18:38:24689 ParamTraits<base::FileDescriptor>::Log(p.socket, l);
690#endif
[email protected]252cad62010-08-18 18:33:57691 l->append(")");
[email protected]7a4de7a62010-08-17 18:38:24692}
693
[email protected]20f0487a2010-09-30 20:06:30694void ParamTraits<LogData>::Write(Message* m, const param_type& p) {
695 WriteParam(m, p.channel);
696 WriteParam(m, p.routing_id);
[email protected]8bf55ca2011-10-17 22:15:27697 WriteParam(m, p.type);
[email protected]20f0487a2010-09-30 20:06:30698 WriteParam(m, p.flags);
699 WriteParam(m, p.sent);
700 WriteParam(m, p.receive);
701 WriteParam(m, p.dispatch);
[email protected]bae578e92012-11-15 03:17:45702 WriteParam(m, p.message_name);
[email protected]20f0487a2010-09-30 20:06:30703 WriteParam(m, p.params);
704}
705
[email protected]ce208f872012-03-07 20:42:56706bool ParamTraits<LogData>::Read(const Message* m,
707 PickleIterator* iter,
708 param_type* r) {
[email protected]8bf55ca2011-10-17 22:15:27709 return
[email protected]20f0487a2010-09-30 20:06:30710 ReadParam(m, iter, &r->channel) &&
711 ReadParam(m, iter, &r->routing_id) &&
[email protected]8bf55ca2011-10-17 22:15:27712 ReadParam(m, iter, &r->type) &&
[email protected]20f0487a2010-09-30 20:06:30713 ReadParam(m, iter, &r->flags) &&
714 ReadParam(m, iter, &r->sent) &&
715 ReadParam(m, iter, &r->receive) &&
716 ReadParam(m, iter, &r->dispatch) &&
[email protected]bae578e92012-11-15 03:17:45717 ReadParam(m, iter, &r->message_name) &&
[email protected]20f0487a2010-09-30 20:06:30718 ReadParam(m, iter, &r->params);
[email protected]20f0487a2010-09-30 20:06:30719}
720
[email protected]bf5aedf02012-06-04 21:18:25721void ParamTraits<LogData>::Log(const param_type& p, std::string* l) {
722 // Doesn't make sense to implement this!
723}
724
725void ParamTraits<Message>::Write(Message* m, const Message& p) {
[email protected]34d48612012-06-29 00:05:04726#if defined(OS_POSIX)
727 // We don't serialize the file descriptors in the nested message, so there
728 // better not be any.
morrita1aa788c2015-01-31 05:45:42729 DCHECK(!p.HasAttachments());
[email protected]34d48612012-06-29 00:05:04730#endif
731
732 // Don't just write out the message. This is used to send messages between
733 // NaCl (Posix environment) and the browser (could be on Windows). The message
734 // header formats differ between these systems (so does handle sharing, but
735 // we already asserted we don't have any handles). So just write out the
736 // parts of the header we use.
737 //
738 // Be careful also to use only explicitly-sized types. The NaCl environment
739 // could be 64-bit and the host browser could be 32-bits. The nested message
740 // may or may not be safe to send between 32-bit and 64-bit systems, but we
741 // leave that up to the code sending the message to ensure.
742 m->WriteUInt32(static_cast<uint32>(p.routing_id()));
743 m->WriteUInt32(p.type());
744 m->WriteUInt32(p.flags());
745 m->WriteData(p.payload(), static_cast<uint32>(p.payload_size()));
[email protected]bf5aedf02012-06-04 21:18:25746}
747
748bool ParamTraits<Message>::Read(const Message* m, PickleIterator* iter,
749 Message* r) {
[email protected]34d48612012-06-29 00:05:04750 uint32 routing_id, type, flags;
avi48fc13b2014-12-28 23:31:48751 if (!iter->ReadUInt32(&routing_id) ||
752 !iter->ReadUInt32(&type) ||
753 !iter->ReadUInt32(&flags))
[email protected]bf5aedf02012-06-04 21:18:25754 return false;
[email protected]34d48612012-06-29 00:05:04755
756 int payload_size;
757 const char* payload;
avi48fc13b2014-12-28 23:31:48758 if (!iter->ReadData(&payload, &payload_size))
[email protected]bf5aedf02012-06-04 21:18:25759 return false;
[email protected]34d48612012-06-29 00:05:04760
761 r->SetHeaderValues(static_cast<int32>(routing_id), type, flags);
762 return r->WriteBytes(payload, payload_size);
[email protected]bf5aedf02012-06-04 21:18:25763}
764
765void ParamTraits<Message>::Log(const Message& p, std::string* l) {
766 l->append("<IPC::Message>");
767}
768
769#if defined(OS_WIN)
770// Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64
[email protected]4a635b72013-03-04 02:29:03771// bit systems. That's why we use the Windows macros to convert to 32 bits.
[email protected]bf5aedf02012-06-04 21:18:25772void ParamTraits<HANDLE>::Write(Message* m, const param_type& p) {
[email protected]4a635b72013-03-04 02:29:03773 m->WriteInt(HandleToLong(p));
[email protected]bf5aedf02012-06-04 21:18:25774}
775
776bool ParamTraits<HANDLE>::Read(const Message* m, PickleIterator* iter,
777 param_type* r) {
[email protected]4a635b72013-03-04 02:29:03778 int32 temp;
avi48fc13b2014-12-28 23:31:48779 if (!iter->ReadInt(&temp))
[email protected]bf5aedf02012-06-04 21:18:25780 return false;
[email protected]4a635b72013-03-04 02:29:03781 *r = LongToHandle(temp);
[email protected]bf5aedf02012-06-04 21:18:25782 return true;
783}
784
785void ParamTraits<HANDLE>::Log(const param_type& p, std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:04786 l->append(base::StringPrintf("0x%X", p));
[email protected]bf5aedf02012-06-04 21:18:25787}
788
789void ParamTraits<LOGFONT>::Write(Message* m, const param_type& p) {
790 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT));
791}
792
793bool ParamTraits<LOGFONT>::Read(const Message* m, PickleIterator* iter,
794 param_type* r) {
795 const char *data;
796 int data_size = 0;
avi48fc13b2014-12-28 23:31:48797 if (iter->ReadData(&data, &data_size) && data_size == sizeof(LOGFONT)) {
[email protected]2e02cfe82012-11-21 00:58:00798 const LOGFONT *font = reinterpret_cast<LOGFONT*>(const_cast<char*>(data));
799 if (_tcsnlen(font->lfFaceName, LF_FACESIZE) < LF_FACESIZE) {
800 memcpy(r, data, sizeof(LOGFONT));
801 return true;
802 }
[email protected]bf5aedf02012-06-04 21:18:25803 }
804
[email protected]2e02cfe82012-11-21 00:58:00805 NOTREACHED();
806 return false;
[email protected]bf5aedf02012-06-04 21:18:25807}
808
809void ParamTraits<LOGFONT>::Log(const param_type& p, std::string* l) {
[email protected]7d3cbc92013-03-18 22:33:04810 l->append(base::StringPrintf("<LOGFONT>"));
[email protected]bf5aedf02012-06-04 21:18:25811}
812
813void ParamTraits<MSG>::Write(Message* m, const param_type& p) {
814 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG));
815}
816
817bool ParamTraits<MSG>::Read(const Message* m, PickleIterator* iter,
818 param_type* r) {
819 const char *data;
820 int data_size = 0;
avi48fc13b2014-12-28 23:31:48821 bool result = iter->ReadData(&data, &data_size);
[email protected]bf5aedf02012-06-04 21:18:25822 if (result && data_size == sizeof(MSG)) {
823 memcpy(r, data, sizeof(MSG));
824 } else {
825 result = false;
826 NOTREACHED();
827 }
828
829 return result;
830}
831
832void ParamTraits<MSG>::Log(const param_type& p, std::string* l) {
833 l->append("<MSG>");
834}
835
836#endif // OS_WIN
837
[email protected]946d1b22009-07-22 23:57:21838} // namespace IPC