blob: 302778e495eccab005e2ec54e2c5a258b25f1f17 [file] [log] [blame]
amit@chromium.org37290742012-01-24 11:36:05 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09002// 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
avi42ebda42015-12-22 11:39:04 +09007#include <stddef.h>
8#include <stdint.h>
9
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090010#include "base/files/file_path.h"
brettw@chromium.org7cd41eb2009-10-24 05:00:20 +090011#include "base/json/json_writer.h"
avi@chromium.orgd8179652013-06-13 22:47:46 +090012#include "base/strings/nullable_string16.h"
avi@chromium.orge7eaf392013-06-11 15:32:18 +090013#include "base/strings/string_number_conversions.h"
avi@chromium.org64535542013-06-08 07:40:45 +090014#include "base/strings/utf_string_conversions.h"
avi@chromium.org78a7e7b2013-06-29 00:20:02 +090015#include "base/time/time.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090016#include "base/values.h"
avi42ebda42015-12-22 11:39:04 +090017#include "build/build_config.h"
brettw@chromium.orgce352e52012-06-05 06:18:25 +090018#include "ipc/ipc_channel_handle.h"
morrita7d1bfcc2015-01-31 14:45:42 +090019#include "ipc/ipc_message_attachment.h"
morrita33a35902015-01-15 06:17:06 +090020#include "ipc/ipc_message_attachment_set.h"
brettw@chromium.orgce352e52012-06-05 06:18:25 +090021
morrita7d1bfcc2015-01-31 14:45:42 +090022#if defined(OS_POSIX)
23#include "ipc/ipc_platform_file_attachment_posix.h"
24#endif
25
erikchen18430e52015-09-26 07:34:31 +090026#if (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN)
scottmg81491272015-06-20 07:51:00 +090027#include "base/memory/shared_memory_handle.h"
erikchen18430e52015-09-26 07:34:31 +090028#endif // (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN)
scottmg81491272015-06-20 07:51:00 +090029
erikchenae16e0c2015-10-10 04:12:06 +090030#if defined(OS_MACOSX) && !defined(OS_IOS)
31#include "ipc/mach_port_mac.h"
32#endif
33
morrita33a35902015-01-15 06:17:06 +090034#if defined(OS_WIN)
jschuh@chromium.orgb9ee7d62012-11-21 09:58:00 +090035#include <tchar.h>
erikchen18430e52015-09-26 07:34:31 +090036#include "ipc/handle_win.h"
erg@google.come6ffcb52010-08-18 03:38:24 +090037#endif
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090038
39namespace IPC {
40
brettw@chromium.orgce352e52012-06-05 06:18:25 +090041namespace {
42
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090043const int kMaxRecursionDepth = 100;
44
brettw@chromium.orgce352e52012-06-05 06:18:25 +090045template<typename CharType>
46void LogBytes(const std::vector<CharType>& data, std::string* out) {
47#if defined(OS_WIN)
48 // Windows has a GUI for logging, which can handle arbitrary binary data.
49 for (size_t i = 0; i < data.size(); ++i)
50 out->push_back(data[i]);
51#else
52 // On POSIX, we log to stdout, which we assume can display ASCII.
53 static const size_t kMaxBytesToLog = 100;
54 for (size_t i = 0; i < std::min(data.size(), kMaxBytesToLog); ++i) {
55 if (isprint(data[i]))
56 out->push_back(data[i]);
57 else
groby@chromium.org6b4dc5e2013-03-19 07:33:04 +090058 out->append(
59 base::StringPrintf("[%02X]", static_cast<unsigned char>(data[i])));
brettw@chromium.orgce352e52012-06-05 06:18:25 +090060 }
61 if (data.size() > kMaxBytesToLog) {
groby@chromium.org22e93982013-03-31 02:29:28 +090062 out->append(base::StringPrintf(
63 " and %u more bytes",
64 static_cast<unsigned>(data.size() - kMaxBytesToLog)));
brettw@chromium.orgce352e52012-06-05 06:18:25 +090065 }
66#endif
67}
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090068
rockot6dbfea52016-02-04 05:20:16 +090069bool ReadValue(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +090070 base::PickleIterator* iter,
71 base::Value** value,
brettw@chromium.orgce352e52012-06-05 06:18:25 +090072 int recursion);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090073
rockot15c8ac42016-02-05 11:12:32 +090074void GetValueSize(base::PickleSizer* sizer,
75 const base::Value* value,
76 int recursion) {
77 if (recursion > kMaxRecursionDepth) {
78 LOG(WARNING) << "Max recursion depth hit in GetValueSize.";
79 return;
80 }
81
82 sizer->AddInt();
83 switch (value->GetType()) {
84 case base::Value::TYPE_NULL:
85 break;
86 case base::Value::TYPE_BOOLEAN:
87 sizer->AddBool();
88 break;
89 case base::Value::TYPE_INTEGER:
90 sizer->AddInt();
91 break;
92 case base::Value::TYPE_DOUBLE:
93 sizer->AddDouble();
94 break;
95 case base::Value::TYPE_STRING: {
96 const base::StringValue* result;
97 value->GetAsString(&result);
amistry0bab7322016-04-08 13:21:54 +090098 if (value->GetAsString(&result)) {
99 DCHECK(result);
100 GetParamSize(sizer, result->GetString());
101 } else {
102 std::string str;
103 bool as_string_result = value->GetAsString(&str);
104 DCHECK(as_string_result);
105 GetParamSize(sizer, str);
106 }
rockot15c8ac42016-02-05 11:12:32 +0900107 break;
108 }
109 case base::Value::TYPE_BINARY: {
110 const base::BinaryValue* binary =
111 static_cast<const base::BinaryValue*>(value);
112 sizer->AddData(static_cast<int>(binary->GetSize()));
113 break;
114 }
115 case base::Value::TYPE_DICTIONARY: {
116 sizer->AddInt();
117 const base::DictionaryValue* dict =
118 static_cast<const base::DictionaryValue*>(value);
119 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd();
120 it.Advance()) {
121 GetParamSize(sizer, it.key());
122 GetValueSize(sizer, &it.value(), recursion + 1);
123 }
124 break;
125 }
126 case base::Value::TYPE_LIST: {
127 sizer->AddInt();
128 const base::ListValue* list = static_cast<const base::ListValue*>(value);
dcheng1fa44fb2016-05-26 03:30:47 +0900129 for (const auto& entry : *list) {
130 GetValueSize(sizer, entry.get(), recursion + 1);
rockot15c8ac42016-02-05 11:12:32 +0900131 }
132 break;
133 }
134 default:
135 NOTREACHED() << "Invalid base::Value type.";
136 }
137}
138
rockot6dbfea52016-02-04 05:20:16 +0900139void WriteValue(base::Pickle* m, const base::Value* value, int recursion) {
orenb@chromium.org88de3872012-07-26 10:29:21 +0900140 bool result;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900141 if (recursion > kMaxRecursionDepth) {
142 LOG(WARNING) << "Max recursion depth hit in WriteValue.";
143 return;
144 }
145
146 m->WriteInt(value->GetType());
147
148 switch (value->GetType()) {
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900149 case base::Value::TYPE_NULL:
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900150 break;
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900151 case base::Value::TYPE_BOOLEAN: {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900152 bool val;
orenb@chromium.org88de3872012-07-26 10:29:21 +0900153 result = value->GetAsBoolean(&val);
154 DCHECK(result);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900155 WriteParam(m, val);
156 break;
157 }
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900158 case base::Value::TYPE_INTEGER: {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900159 int val;
orenb@chromium.org88de3872012-07-26 10:29:21 +0900160 result = value->GetAsInteger(&val);
161 DCHECK(result);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900162 WriteParam(m, val);
163 break;
164 }
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900165 case base::Value::TYPE_DOUBLE: {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900166 double val;
orenb@chromium.org88de3872012-07-26 10:29:21 +0900167 result = value->GetAsDouble(&val);
168 DCHECK(result);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900169 WriteParam(m, val);
170 break;
171 }
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900172 case base::Value::TYPE_STRING: {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900173 std::string val;
orenb@chromium.org88de3872012-07-26 10:29:21 +0900174 result = value->GetAsString(&val);
175 DCHECK(result);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900176 WriteParam(m, val);
177 break;
178 }
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900179 case base::Value::TYPE_BINARY: {
tfarina@chromium.org2e2a9022011-08-06 03:20:05 +0900180 const base::BinaryValue* binary =
181 static_cast<const base::BinaryValue*>(value);
thomasvl@google.com9a242072010-07-23 23:18:59 +0900182 m->WriteData(binary->GetBuffer(), static_cast<int>(binary->GetSize()));
mpcomplete@chromium.org554d4312009-10-07 03:15:58 +0900183 break;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900184 }
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900185 case base::Value::TYPE_DICTIONARY: {
186 const base::DictionaryValue* dict =
187 static_cast<const base::DictionaryValue*>(value);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900188
pkasting@chromium.org36515db2009-11-26 05:47:52 +0900189 WriteParam(m, static_cast<int>(dict->size()));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900190
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900191 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd();
192 it.Advance()) {
pneubeck@chromium.orga2fbefc2013-01-18 23:43:27 +0900193 WriteParam(m, it.key());
194 WriteValue(m, &it.value(), recursion + 1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900195 }
196 break;
197 }
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900198 case base::Value::TYPE_LIST: {
199 const base::ListValue* list = static_cast<const base::ListValue*>(value);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900200 WriteParam(m, static_cast<int>(list->GetSize()));
dcheng1fa44fb2016-05-26 03:30:47 +0900201 for (const auto& entry : *list) {
202 WriteValue(m, entry.get(), recursion + 1);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900203 }
204 break;
205 }
206 }
207}
208
209// Helper for ReadValue that reads a DictionaryValue into a pre-allocated
210// object.
rockot6dbfea52016-02-04 05:20:16 +0900211bool ReadDictionaryValue(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +0900212 base::PickleIterator* iter,
213 base::DictionaryValue* value,
214 int recursion) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900215 int size;
216 if (!ReadParam(m, iter, &size))
217 return false;
218
219 for (int i = 0; i < size; ++i) {
viettrungluu@chromium.org178423d2010-07-31 04:47:47 +0900220 std::string key;
brettw@chromium.org32285192013-06-22 04:42:19 +0900221 base::Value* subval;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900222 if (!ReadParam(m, iter, &key) ||
223 !ReadValue(m, iter, &subval, recursion + 1))
224 return false;
kalman@chromium.org9c8b5732011-08-19 14:59:57 +0900225 value->SetWithoutPathExpansion(key, subval);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900226 }
227
228 return true;
229}
230
231// Helper for ReadValue that reads a ReadListValue into a pre-allocated
232// object.
rockot6dbfea52016-02-04 05:20:16 +0900233bool ReadListValue(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +0900234 base::PickleIterator* iter,
235 base::ListValue* value,
236 int recursion) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900237 int size;
238 if (!ReadParam(m, iter, &size))
239 return false;
240
241 for (int i = 0; i < size; ++i) {
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900242 base::Value* subval;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900243 if (!ReadValue(m, iter, &subval, recursion + 1))
244 return false;
245 value->Set(i, subval);
246 }
247
248 return true;
249}
250
rockot6dbfea52016-02-04 05:20:16 +0900251bool ReadValue(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +0900252 base::PickleIterator* iter,
253 base::Value** value,
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900254 int recursion) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900255 if (recursion > kMaxRecursionDepth) {
256 LOG(WARNING) << "Max recursion depth hit in ReadValue.";
257 return false;
258 }
259
260 int type;
261 if (!ReadParam(m, iter, &type))
262 return false;
263
264 switch (type) {
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900265 case base::Value::TYPE_NULL:
estade033e61e2015-05-13 03:11:50 +0900266 *value = base::Value::CreateNullValue().release();
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900267 break;
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900268 case base::Value::TYPE_BOOLEAN: {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900269 bool val;
270 if (!ReadParam(m, iter, &val))
271 return false;
tfarina@chromium.org4ef57d92013-01-30 14:24:07 +0900272 *value = new base::FundamentalValue(val);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900273 break;
274 }
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900275 case base::Value::TYPE_INTEGER: {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900276 int val;
277 if (!ReadParam(m, iter, &val))
278 return false;
tfarina@chromium.org4ef57d92013-01-30 14:24:07 +0900279 *value = new base::FundamentalValue(val);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900280 break;
281 }
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900282 case base::Value::TYPE_DOUBLE: {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900283 double val;
284 if (!ReadParam(m, iter, &val))
285 return false;
tfarina@chromium.org4ef57d92013-01-30 14:24:07 +0900286 *value = new base::FundamentalValue(val);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900287 break;
288 }
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900289 case base::Value::TYPE_STRING: {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900290 std::string val;
291 if (!ReadParam(m, iter, &val))
292 return false;
tfarina@chromium.org4ef57d92013-01-30 14:24:07 +0900293 *value = new base::StringValue(val);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900294 break;
295 }
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900296 case base::Value::TYPE_BINARY: {
mpcomplete@chromium.org554d4312009-10-07 03:15:58 +0900297 const char* data;
298 int length;
avic9f0ad02014-12-29 08:31:48 +0900299 if (!iter->ReadData(&data, &length))
mpcomplete@chromium.org554d4312009-10-07 03:15:58 +0900300 return false;
dcheng03c8f322016-06-16 19:48:42 +0900301 std::unique_ptr<base::BinaryValue> val =
302 base::BinaryValue::CreateWithCopiedBuffer(data, length);
303 *value = val.release();
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900304 break;
305 }
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900306 case base::Value::TYPE_DICTIONARY: {
danakjc3fb6c52016-04-23 13:21:09 +0900307 std::unique_ptr<base::DictionaryValue> val(new base::DictionaryValue());
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900308 if (!ReadDictionaryValue(m, iter, val.get(), recursion))
309 return false;
310 *value = val.release();
311 break;
312 }
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900313 case base::Value::TYPE_LIST: {
danakjc3fb6c52016-04-23 13:21:09 +0900314 std::unique_ptr<base::ListValue> val(new base::ListValue());
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900315 if (!ReadListValue(m, iter, val.get(), recursion))
316 return false;
317 *value = val.release();
318 break;
319 }
mpcomplete@chromium.org554d4312009-10-07 03:15:58 +0900320 default:
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900321 return false;
322 }
323
324 return true;
325}
326
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900327} // namespace
328
329// -----------------------------------------------------------------------------
330
331LogData::LogData()
332 : routing_id(0),
333 type(0),
334 sent(0),
335 receive(0),
336 dispatch(0) {
337}
338
vmpstrd661bf72016-03-25 05:22:54 +0900339LogData::LogData(const LogData& other) = default;
340
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900341LogData::~LogData() {
342}
343
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900344void ParamTraits<bool>::Log(const param_type& p, std::string* l) {
345 l->append(p ? "true" : "false");
346}
347
rockot15c8ac42016-02-05 11:12:32 +0900348void ParamTraits<signed char>::GetSize(base::PickleSizer* sizer,
349 const param_type& p) {
350 sizer->AddBytes(sizeof(param_type));
351}
352
rockot6dbfea52016-02-04 05:20:16 +0900353void ParamTraits<signed char>::Write(base::Pickle* m, const param_type& p) {
ortuno6e3f7b32015-10-30 09:46:20 +0900354 m->WriteBytes(&p, sizeof(param_type));
355}
356
rockot6dbfea52016-02-04 05:20:16 +0900357bool ParamTraits<signed char>::Read(const base::Pickle* m,
358 base::PickleIterator* iter,
359 param_type* r) {
ortuno6e3f7b32015-10-30 09:46:20 +0900360 const char* data;
361 if (!iter->ReadBytes(&data, sizeof(param_type)))
362 return false;
363 memcpy(r, data, sizeof(param_type));
364 return true;
365}
366
367void ParamTraits<signed char>::Log(const param_type& p, std::string* l) {
368 l->append(base::IntToString(p));
369}
370
rockot15c8ac42016-02-05 11:12:32 +0900371void ParamTraits<unsigned char>::GetSize(base::PickleSizer* sizer,
372 const param_type& p) {
373 sizer->AddBytes(sizeof(param_type));
374}
375
rockot6dbfea52016-02-04 05:20:16 +0900376void ParamTraits<unsigned char>::Write(base::Pickle* m, const param_type& p) {
tsepez@chromium.org09eb95f2013-07-13 08:12:28 +0900377 m->WriteBytes(&p, sizeof(param_type));
378}
379
rockot6dbfea52016-02-04 05:20:16 +0900380bool ParamTraits<unsigned char>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +0900381 base::PickleIterator* iter,
382 param_type* r) {
tsepez@chromium.org09eb95f2013-07-13 08:12:28 +0900383 const char* data;
avic9f0ad02014-12-29 08:31:48 +0900384 if (!iter->ReadBytes(&data, sizeof(param_type)))
tsepez@chromium.org09eb95f2013-07-13 08:12:28 +0900385 return false;
386 memcpy(r, data, sizeof(param_type));
387 return true;
388}
389
390void ParamTraits<unsigned char>::Log(const param_type& p, std::string* l) {
391 l->append(base::UintToString(p));
392}
393
rockot15c8ac42016-02-05 11:12:32 +0900394void ParamTraits<unsigned short>::GetSize(base::PickleSizer* sizer,
395 const param_type& p) {
396 sizer->AddBytes(sizeof(param_type));
397}
398
rockot6dbfea52016-02-04 05:20:16 +0900399void ParamTraits<unsigned short>::Write(base::Pickle* m, const param_type& p) {
tsepez@chromium.org09eb95f2013-07-13 08:12:28 +0900400 m->WriteBytes(&p, sizeof(param_type));
401}
402
rockot6dbfea52016-02-04 05:20:16 +0900403bool ParamTraits<unsigned short>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +0900404 base::PickleIterator* iter,
tsepez@chromium.org09eb95f2013-07-13 08:12:28 +0900405 param_type* r) {
406 const char* data;
avic9f0ad02014-12-29 08:31:48 +0900407 if (!iter->ReadBytes(&data, sizeof(param_type)))
tsepez@chromium.org09eb95f2013-07-13 08:12:28 +0900408 return false;
409 memcpy(r, data, sizeof(param_type));
410 return true;
411}
412
413void ParamTraits<unsigned short>::Log(const param_type& p, std::string* l) {
414 l->append(base::UintToString(p));
415}
416
erg@google.com8aca7272010-08-19 03:33:57 +0900417void ParamTraits<int>::Log(const param_type& p, std::string* l) {
418 l->append(base::IntToString(p));
419}
420
421void ParamTraits<unsigned int>::Log(const param_type& p, std::string* l) {
422 l->append(base::UintToString(p));
423}
424
jam739d8d12016-02-11 09:50:28 +0900425#if defined(OS_WIN) || defined(OS_LINUX) || \
426 (defined(OS_ANDROID) && defined(ARCH_CPU_64_BITS))
erg@google.com8aca7272010-08-19 03:33:57 +0900427void ParamTraits<long>::Log(const param_type& p, std::string* l) {
tfarina1cbfa082015-09-05 03:47:57 +0900428 l->append(base::Int64ToString(static_cast<int64_t>(p)));
erg@google.com8aca7272010-08-19 03:33:57 +0900429}
430
431void ParamTraits<unsigned long>::Log(const param_type& p, std::string* l) {
tfarina1cbfa082015-09-05 03:47:57 +0900432 l->append(base::Uint64ToString(static_cast<uint64_t>(p)));
erg@google.com8aca7272010-08-19 03:33:57 +0900433}
jam923e5462016-02-11 05:13:39 +0900434#endif
erg@google.com8aca7272010-08-19 03:33:57 +0900435
436void ParamTraits<long long>::Log(const param_type& p, std::string* l) {
tfarina1cbfa082015-09-05 03:47:57 +0900437 l->append(base::Int64ToString(static_cast<int64_t>(p)));
erg@google.com8aca7272010-08-19 03:33:57 +0900438}
439
440void ParamTraits<unsigned long long>::Log(const param_type& p, std::string* l) {
441 l->append(base::Uint64ToString(p));
442}
erg@google.come6ffcb52010-08-18 03:38:24 +0900443
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900444void ParamTraits<float>::Log(const param_type& p, std::string* l) {
groby@chromium.org6b4dc5e2013-03-19 07:33:04 +0900445 l->append(base::StringPrintf("%e", p));
erg@google.come6ffcb52010-08-18 03:38:24 +0900446}
447
rockot15c8ac42016-02-05 11:12:32 +0900448void ParamTraits<double>::GetSize(base::PickleSizer* sizer,
449 const param_type& p) {
450 sizer->AddBytes(sizeof(param_type));
451}
452
rockot6dbfea52016-02-04 05:20:16 +0900453void ParamTraits<double>::Write(base::Pickle* m, const param_type& p) {
piman@chromium.orgdd413b42013-10-31 18:27:31 +0900454 m->WriteBytes(reinterpret_cast<const char*>(&p), sizeof(param_type));
apatrick@chromium.org519c1c62010-10-22 07:04:52 +0900455}
456
rockot6dbfea52016-02-04 05:20:16 +0900457bool ParamTraits<double>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +0900458 base::PickleIterator* iter,
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900459 param_type* r) {
460 const char *data;
avic9f0ad02014-12-29 08:31:48 +0900461 if (!iter->ReadBytes(&data, sizeof(*r))) {
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900462 NOTREACHED();
463 return false;
464 }
465 memcpy(r, data, sizeof(param_type));
466 return true;
apatrick@chromium.org519c1c62010-10-22 07:04:52 +0900467}
468
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900469void ParamTraits<double>::Log(const param_type& p, std::string* l) {
groby@chromium.org6b4dc5e2013-03-19 07:33:04 +0900470 l->append(base::StringPrintf("%e", p));
isherman@chromium.org9952aaf2011-09-03 05:42:04 +0900471}
472
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900473
474void ParamTraits<std::string>::Log(const param_type& p, std::string* l) {
475 l->append(p);
isherman@chromium.org9952aaf2011-09-03 05:42:04 +0900476}
477
brettw@chromium.org5b040852013-12-03 09:39:26 +0900478void ParamTraits<base::string16>::Log(const param_type& p, std::string* l) {
avi@chromium.org4f87d8f2013-12-26 03:18:01 +0900479 l->append(base::UTF16ToUTF8(p));
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900480}
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900481
rockot15c8ac42016-02-05 11:12:32 +0900482void ParamTraits<std::vector<char>>::GetSize(base::PickleSizer* sizer,
483 const param_type& p) {
484 sizer->AddData(static_cast<int>(p.size()));
485}
486
rockot6dbfea52016-02-04 05:20:16 +0900487void ParamTraits<std::vector<char>>::Write(base::Pickle* m,
488 const param_type& p) {
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900489 if (p.empty()) {
490 m->WriteData(NULL, 0);
491 } else {
492 m->WriteData(&p.front(), static_cast<int>(p.size()));
493 }
494}
495
rockot6dbfea52016-02-04 05:20:16 +0900496bool ParamTraits<std::vector<char>>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +0900497 base::PickleIterator* iter,
498 param_type* r) {
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900499 const char *data;
500 int data_size = 0;
avic9f0ad02014-12-29 08:31:48 +0900501 if (!iter->ReadData(&data, &data_size) || data_size < 0)
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900502 return false;
503 r->resize(data_size);
504 if (data_size)
505 memcpy(&r->front(), data, data_size);
506 return true;
507}
508
509void ParamTraits<std::vector<char> >::Log(const param_type& p, std::string* l) {
510 LogBytes(p, l);
511}
512
rockot15c8ac42016-02-05 11:12:32 +0900513void ParamTraits<std::vector<unsigned char>>::GetSize(base::PickleSizer* sizer,
514 const param_type& p) {
515 sizer->AddData(static_cast<int>(p.size()));
516}
517
rockot6dbfea52016-02-04 05:20:16 +0900518void ParamTraits<std::vector<unsigned char>>::Write(base::Pickle* m,
519 const param_type& p) {
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900520 if (p.empty()) {
521 m->WriteData(NULL, 0);
522 } else {
523 m->WriteData(reinterpret_cast<const char*>(&p.front()),
524 static_cast<int>(p.size()));
525 }
526}
527
rockot6dbfea52016-02-04 05:20:16 +0900528bool ParamTraits<std::vector<unsigned char>>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +0900529 base::PickleIterator* iter,
530 param_type* r) {
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900531 const char *data;
532 int data_size = 0;
avic9f0ad02014-12-29 08:31:48 +0900533 if (!iter->ReadData(&data, &data_size) || data_size < 0)
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900534 return false;
535 r->resize(data_size);
536 if (data_size)
537 memcpy(&r->front(), data, data_size);
538 return true;
539}
540
541void ParamTraits<std::vector<unsigned char> >::Log(const param_type& p,
542 std::string* l) {
543 LogBytes(p, l);
544}
545
rockot15c8ac42016-02-05 11:12:32 +0900546void ParamTraits<std::vector<bool>>::GetSize(base::PickleSizer* sizer,
547 const param_type& p) {
548 GetParamSize(sizer, static_cast<int>(p.size()));
549 for (size_t i = 0; i < p.size(); ++i)
550 GetParamSize(sizer, static_cast<bool>(p[i]));
551}
552
rockot6dbfea52016-02-04 05:20:16 +0900553void ParamTraits<std::vector<bool>>::Write(base::Pickle* m,
554 const param_type& p) {
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900555 WriteParam(m, static_cast<int>(p.size()));
eugenis@chromium.org17085812013-03-21 05:25:00 +0900556 // Cast to bool below is required because libc++'s
557 // vector<bool>::const_reference is different from bool, and we want to avoid
558 // writing an extra specialization of ParamTraits for it.
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900559 for (size_t i = 0; i < p.size(); i++)
eugenis@chromium.org17085812013-03-21 05:25:00 +0900560 WriteParam(m, static_cast<bool>(p[i]));
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900561}
562
rockot6dbfea52016-02-04 05:20:16 +0900563bool ParamTraits<std::vector<bool>>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +0900564 base::PickleIterator* iter,
565 param_type* r) {
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900566 int size;
567 // ReadLength() checks for < 0 itself.
avic9f0ad02014-12-29 08:31:48 +0900568 if (!iter->ReadLength(&size))
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900569 return false;
570 r->resize(size);
571 for (int i = 0; i < size; i++) {
572 bool value;
573 if (!ReadParam(m, iter, &value))
574 return false;
575 (*r)[i] = value;
576 }
577 return true;
578}
579
580void ParamTraits<std::vector<bool> >::Log(const param_type& p, std::string* l) {
581 for (size_t i = 0; i < p.size(); ++i) {
582 if (i != 0)
583 l->push_back(' ');
eugenis@chromium.org17085812013-03-21 05:25:00 +0900584 LogParam(static_cast<bool>(p[i]), l);
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900585 }
apatrick@chromium.org519c1c62010-10-22 07:04:52 +0900586}
587
erikchenfdd43fe2015-07-08 07:13:11 +0900588void ParamTraits<BrokerableAttachment::AttachmentId>::Write(
rockot6dbfea52016-02-04 05:20:16 +0900589 base::Pickle* m,
erikchenfdd43fe2015-07-08 07:13:11 +0900590 const param_type& p) {
erikchenc5778442015-08-28 04:49:58 +0900591 m->WriteBytes(p.nonce, BrokerableAttachment::kNonceSize);
erikchenfdd43fe2015-07-08 07:13:11 +0900592}
593
594bool ParamTraits<BrokerableAttachment::AttachmentId>::Read(
rockot6dbfea52016-02-04 05:20:16 +0900595 const base::Pickle* m,
erikchenfdd43fe2015-07-08 07:13:11 +0900596 base::PickleIterator* iter,
597 param_type* r) {
598 const char* data;
erikchenc5778442015-08-28 04:49:58 +0900599 if (!iter->ReadBytes(&data, BrokerableAttachment::kNonceSize))
erikchenfdd43fe2015-07-08 07:13:11 +0900600 return false;
601 memcpy(r->nonce, data, BrokerableAttachment::kNonceSize);
602 return true;
603}
604
605void ParamTraits<BrokerableAttachment::AttachmentId>::Log(const param_type& p,
606 std::string* l) {
607 l->append(base::HexEncode(p.nonce, BrokerableAttachment::kNonceSize));
608}
609
rockot15c8ac42016-02-05 11:12:32 +0900610void ParamTraits<base::DictionaryValue>::GetSize(base::PickleSizer* sizer,
611 const param_type& p) {
612 GetValueSize(sizer, &p, 0);
613}
614
rockot6dbfea52016-02-04 05:20:16 +0900615void ParamTraits<base::DictionaryValue>::Write(base::Pickle* m,
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900616 const param_type& p) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900617 WriteValue(m, &p, 0);
618}
619
rockot6dbfea52016-02-04 05:20:16 +0900620bool ParamTraits<base::DictionaryValue>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +0900621 base::PickleIterator* iter,
622 param_type* r) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900623 int type;
brettw@chromium.org32285192013-06-22 04:42:19 +0900624 if (!ReadParam(m, iter, &type) || type != base::Value::TYPE_DICTIONARY)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900625 return false;
626
627 return ReadDictionaryValue(m, iter, r, 0);
628}
629
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900630void ParamTraits<base::DictionaryValue>::Log(const param_type& p,
631 std::string* l) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900632 std::string json;
estadeb5f30dd2015-05-16 10:02:34 +0900633 base::JSONWriter::Write(p, &json);
erg@google.com8aca7272010-08-19 03:33:57 +0900634 l->append(json);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900635}
636
erg@google.come6ffcb52010-08-18 03:38:24 +0900637#if defined(OS_POSIX)
jam7f5c5742016-05-12 06:05:05 +0900638void ParamTraits<base::FileDescriptor>::GetSize(base::PickleSizer* sizer,
639 const param_type& p) {
640 GetParamSize(sizer, p.fd >= 0);
641 sizer->AddAttachment();
642}
643
rockot6dbfea52016-02-04 05:20:16 +0900644void ParamTraits<base::FileDescriptor>::Write(base::Pickle* m,
645 const param_type& p) {
erg@google.come6ffcb52010-08-18 03:38:24 +0900646 const bool valid = p.fd >= 0;
647 WriteParam(m, valid);
648
morritaab207252014-09-25 05:11:45 +0900649 if (!valid)
650 return;
651
652 if (p.auto_close) {
morrita7d1bfcc2015-01-31 14:45:42 +0900653 if (!m->WriteAttachment(
654 new internal::PlatformFileAttachment(base::ScopedFD(p.fd))))
morritaab207252014-09-25 05:11:45 +0900655 NOTREACHED();
656 } else {
morrita7d1bfcc2015-01-31 14:45:42 +0900657 if (!m->WriteAttachment(new internal::PlatformFileAttachment(p.fd)))
erg@google.come6ffcb52010-08-18 03:38:24 +0900658 NOTREACHED();
659 }
660}
661
rockot6dbfea52016-02-04 05:20:16 +0900662bool ParamTraits<base::FileDescriptor>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +0900663 base::PickleIterator* iter,
erg@google.come6ffcb52010-08-18 03:38:24 +0900664 param_type* r) {
morritaab207252014-09-25 05:11:45 +0900665 *r = base::FileDescriptor();
666
erg@google.come6ffcb52010-08-18 03:38:24 +0900667 bool valid;
668 if (!ReadParam(m, iter, &valid))
669 return false;
670
morritaab207252014-09-25 05:11:45 +0900671 // TODO(morrita): Seems like this should return false.
672 if (!valid)
erg@google.come6ffcb52010-08-18 03:38:24 +0900673 return true;
erg@google.come6ffcb52010-08-18 03:38:24 +0900674
rockot6dbfea52016-02-04 05:20:16 +0900675 scoped_refptr<base::Pickle::Attachment> attachment;
morrita7d1bfcc2015-01-31 14:45:42 +0900676 if (!m->ReadAttachment(iter, &attachment))
morritaab207252014-09-25 05:11:45 +0900677 return false;
678
rockot6dbfea52016-02-04 05:20:16 +0900679 *r = base::FileDescriptor(
680 static_cast<MessageAttachment*>(attachment.get())->TakePlatformFile(),
681 true);
morritaab207252014-09-25 05:11:45 +0900682 return true;
erg@google.come6ffcb52010-08-18 03:38:24 +0900683}
684
685void ParamTraits<base::FileDescriptor>::Log(const param_type& p,
erg@google.com8aca7272010-08-19 03:33:57 +0900686 std::string* l) {
erg@google.come6ffcb52010-08-18 03:38:24 +0900687 if (p.auto_close) {
groby@chromium.org6b4dc5e2013-03-19 07:33:04 +0900688 l->append(base::StringPrintf("FD(%d auto-close)", p.fd));
erg@google.come6ffcb52010-08-18 03:38:24 +0900689 } else {
groby@chromium.org6b4dc5e2013-03-19 07:33:04 +0900690 l->append(base::StringPrintf("FD(%d)", p.fd));
erg@google.come6ffcb52010-08-18 03:38:24 +0900691 }
692}
693#endif // defined(OS_POSIX)
694
scottmg81491272015-06-20 07:51:00 +0900695#if defined(OS_MACOSX) && !defined(OS_IOS)
jam7f5c5742016-05-12 06:05:05 +0900696void ParamTraits<base::SharedMemoryHandle>::GetSize(base::PickleSizer* sizer,
697 const param_type& p) {
698 GetParamSize(sizer, p.GetMemoryObject());
699 uint32_t dummy = 0;
700 GetParamSize(sizer, dummy);
701}
702
rockot6dbfea52016-02-04 05:20:16 +0900703void ParamTraits<base::SharedMemoryHandle>::Write(base::Pickle* m,
scottmg81491272015-06-20 07:51:00 +0900704 const param_type& p) {
erikchena355c822016-04-19 03:39:15 +0900705 MachPortMac mach_port_mac(p.GetMemoryObject());
706 ParamTraits<MachPortMac>::Write(m, mach_port_mac);
707 size_t size = 0;
708 bool result = p.GetSize(&size);
709 DCHECK(result);
710 ParamTraits<uint32_t>::Write(m, static_cast<uint32_t>(size));
scottmg81491272015-06-20 07:51:00 +0900711
erikchena355c822016-04-19 03:39:15 +0900712 // If the caller intended to pass ownership to the IPC stack, release a
713 // reference.
714 if (p.OwnershipPassesToIPC())
715 p.Close();
scottmg81491272015-06-20 07:51:00 +0900716}
717
rockot6dbfea52016-02-04 05:20:16 +0900718bool ParamTraits<base::SharedMemoryHandle>::Read(const base::Pickle* m,
scottmg81491272015-06-20 07:51:00 +0900719 base::PickleIterator* iter,
720 param_type* r) {
erikchena355c822016-04-19 03:39:15 +0900721 MachPortMac mach_port_mac;
722 if (!ParamTraits<MachPortMac>::Read(m, iter, &mach_port_mac))
scottmg81491272015-06-20 07:51:00 +0900723 return false;
724
erikchena355c822016-04-19 03:39:15 +0900725 uint32_t size;
726 if (!ParamTraits<uint32_t>::Read(m, iter, &size))
727 return false;
scottmg81491272015-06-20 07:51:00 +0900728
erikchena355c822016-04-19 03:39:15 +0900729 *r = base::SharedMemoryHandle(mach_port_mac.get_mach_port(),
730 static_cast<size_t>(size),
731 base::GetCurrentProcId());
732 return true;
scottmg81491272015-06-20 07:51:00 +0900733}
734
735void ParamTraits<base::SharedMemoryHandle>::Log(const param_type& p,
736 std::string* l) {
erikchena355c822016-04-19 03:39:15 +0900737 l->append("Mach port: ");
738 LogParam(p.GetMemoryObject(), l);
scottmg81491272015-06-20 07:51:00 +0900739}
erikchen347f5cb2015-10-03 08:49:26 +0900740
erikchen18430e52015-09-26 07:34:31 +0900741#elif defined(OS_WIN)
jam7f5c5742016-05-12 06:05:05 +0900742void ParamTraits<base::SharedMemoryHandle>::GetSize(base::PickleSizer* s,
743 const param_type& p) {
744 GetParamSize(s, p.NeedsBrokering());
745 if (p.NeedsBrokering()) {
746 GetParamSize(s, p.GetHandle());
747 } else {
748 GetParamSize(s, HandleToLong(p.GetHandle()));
749 }
750}
751
rockot6dbfea52016-02-04 05:20:16 +0900752void ParamTraits<base::SharedMemoryHandle>::Write(base::Pickle* m,
erikchen18430e52015-09-26 07:34:31 +0900753 const param_type& p) {
erikchen18430e52015-09-26 07:34:31 +0900754 m->WriteBool(p.NeedsBrokering());
755
756 if (p.NeedsBrokering()) {
757 HandleWin handle_win(p.GetHandle(), HandleWin::DUPLICATE);
758 ParamTraits<HandleWin>::Write(m, handle_win);
erikcheneeaf5f92016-02-02 07:14:28 +0900759
760 // If the caller intended to pass ownership to the IPC stack, release a
761 // reference.
erikchenb23e3a42016-03-30 07:26:42 +0900762 if (p.OwnershipPassesToIPC() && p.BelongsToCurrentProcess())
erikcheneeaf5f92016-02-02 07:14:28 +0900763 p.Close();
erikchen18430e52015-09-26 07:34:31 +0900764 } else {
765 m->WriteInt(HandleToLong(p.GetHandle()));
766 }
767}
768
rockot6dbfea52016-02-04 05:20:16 +0900769bool ParamTraits<base::SharedMemoryHandle>::Read(const base::Pickle* m,
erikchen18430e52015-09-26 07:34:31 +0900770 base::PickleIterator* iter,
771 param_type* r) {
erikchen18430e52015-09-26 07:34:31 +0900772 bool needs_brokering;
773 if (!iter->ReadBool(&needs_brokering))
774 return false;
775
776 if (needs_brokering) {
777 HandleWin handle_win;
778 if (!ParamTraits<HandleWin>::Read(m, iter, &handle_win))
779 return false;
erikchen97211e12016-01-08 11:17:04 +0900780 *r = base::SharedMemoryHandle(handle_win.get_handle(),
781 base::GetCurrentProcId());
erikchen18430e52015-09-26 07:34:31 +0900782 return true;
783 }
784
785 int handle_int;
786 if (!iter->ReadInt(&handle_int))
787 return false;
788 HANDLE handle = LongToHandle(handle_int);
erikchen97211e12016-01-08 11:17:04 +0900789 *r = base::SharedMemoryHandle(handle, base::GetCurrentProcId());
erikchen18430e52015-09-26 07:34:31 +0900790 return true;
791}
792
793void ParamTraits<base::SharedMemoryHandle>::Log(const param_type& p,
794 std::string* l) {
erikchen18430e52015-09-26 07:34:31 +0900795 LogParam(p.GetHandle(), l);
796 l->append(" needs brokering: ");
797 LogParam(p.NeedsBrokering(), l);
798}
scottmg81491272015-06-20 07:51:00 +0900799#endif // defined(OS_MACOSX) && !defined(OS_IOS)
800
rockot15c8ac42016-02-05 11:12:32 +0900801void ParamTraits<base::FilePath>::GetSize(base::PickleSizer* sizer,
802 const param_type& p) {
803 p.GetSizeForPickle(sizer);
804}
805
rockot6dbfea52016-02-04 05:20:16 +0900806void ParamTraits<base::FilePath>::Write(base::Pickle* m, const param_type& p) {
aedla@chromium.org51127b92013-01-28 22:47:55 +0900807 p.WriteToPickle(m);
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900808}
809
rockot6dbfea52016-02-04 05:20:16 +0900810bool ParamTraits<base::FilePath>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +0900811 base::PickleIterator* iter,
brettw@chromium.org22b3fda2013-02-10 13:49:30 +0900812 param_type* r) {
aedla@chromium.org51127b92013-01-28 22:47:55 +0900813 return r->ReadFromPickle(iter);
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900814}
815
brettw@chromium.org22b3fda2013-02-10 13:49:30 +0900816void ParamTraits<base::FilePath>::Log(const param_type& p, std::string* l) {
817 ParamTraits<base::FilePath::StringType>::Log(p.value(), l);
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900818}
819
rockot15c8ac42016-02-05 11:12:32 +0900820void ParamTraits<base::ListValue>::GetSize(base::PickleSizer* sizer,
821 const param_type& p) {
822 GetValueSize(sizer, &p, 0);
823}
824
rockot6dbfea52016-02-04 05:20:16 +0900825void ParamTraits<base::ListValue>::Write(base::Pickle* m, const param_type& p) {
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900826 WriteValue(m, &p, 0);
827}
828
rockot6dbfea52016-02-04 05:20:16 +0900829bool ParamTraits<base::ListValue>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +0900830 base::PickleIterator* iter,
831 param_type* r) {
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900832 int type;
brettw@chromium.org32285192013-06-22 04:42:19 +0900833 if (!ReadParam(m, iter, &type) || type != base::Value::TYPE_LIST)
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900834 return false;
835
836 return ReadListValue(m, iter, r, 0);
837}
838
brettw@chromium.orgccffb7d2013-06-14 07:50:27 +0900839void ParamTraits<base::ListValue>::Log(const param_type& p, std::string* l) {
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900840 std::string json;
estadeb5f30dd2015-05-16 10:02:34 +0900841 base::JSONWriter::Write(p, &json);
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900842 l->append(json);
843}
844
rockot15c8ac42016-02-05 11:12:32 +0900845void ParamTraits<base::NullableString16>::GetSize(base::PickleSizer* sizer,
846 const param_type& p) {
847 GetParamSize(sizer, p.string());
848 GetParamSize(sizer, p.is_null());
849}
850
rockot6dbfea52016-02-04 05:20:16 +0900851void ParamTraits<base::NullableString16>::Write(base::Pickle* m,
avi@chromium.orgd8179652013-06-13 22:47:46 +0900852 const param_type& p) {
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900853 WriteParam(m, p.string());
854 WriteParam(m, p.is_null());
855}
856
rockot6dbfea52016-02-04 05:20:16 +0900857bool ParamTraits<base::NullableString16>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +0900858 base::PickleIterator* iter,
avi@chromium.orgd8179652013-06-13 22:47:46 +0900859 param_type* r) {
brettw@chromium.org5b040852013-12-03 09:39:26 +0900860 base::string16 string;
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900861 if (!ReadParam(m, iter, &string))
862 return false;
863 bool is_null;
864 if (!ReadParam(m, iter, &is_null))
865 return false;
avi@chromium.orgd8179652013-06-13 22:47:46 +0900866 *r = base::NullableString16(string, is_null);
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900867 return true;
868}
869
avi@chromium.orgd8179652013-06-13 22:47:46 +0900870void ParamTraits<base::NullableString16>::Log(const param_type& p,
871 std::string* l) {
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900872 l->append("(");
873 LogParam(p.string(), l);
874 l->append(", ");
875 LogParam(p.is_null(), l);
876 l->append(")");
877}
878
rockot15c8ac42016-02-05 11:12:32 +0900879void ParamTraits<base::File::Info>::GetSize(base::PickleSizer* sizer,
880 const param_type& p) {
881 GetParamSize(sizer, p.size);
882 GetParamSize(sizer, p.is_directory);
883 GetParamSize(sizer, p.last_modified.ToDoubleT());
884 GetParamSize(sizer, p.last_accessed.ToDoubleT());
885 GetParamSize(sizer, p.creation_time.ToDoubleT());
886}
887
rockot6dbfea52016-02-04 05:20:16 +0900888void ParamTraits<base::File::Info>::Write(base::Pickle* m,
rvargas@chromium.org9e469f62014-01-28 06:36:00 +0900889 const param_type& p) {
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900890 WriteParam(m, p.size);
891 WriteParam(m, p.is_directory);
892 WriteParam(m, p.last_modified.ToDoubleT());
893 WriteParam(m, p.last_accessed.ToDoubleT());
894 WriteParam(m, p.creation_time.ToDoubleT());
895}
896
rockot6dbfea52016-02-04 05:20:16 +0900897bool ParamTraits<base::File::Info>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +0900898 base::PickleIterator* iter,
rvargas@chromium.org9e469f62014-01-28 06:36:00 +0900899 param_type* p) {
pkasting@chromium.org150a8492014-07-18 10:40:47 +0900900 double last_modified, last_accessed, creation_time;
901 if (!ReadParam(m, iter, &p->size) ||
902 !ReadParam(m, iter, &p->is_directory) ||
903 !ReadParam(m, iter, &last_modified) ||
904 !ReadParam(m, iter, &last_accessed) ||
905 !ReadParam(m, iter, &creation_time))
906 return false;
907 p->last_modified = base::Time::FromDoubleT(last_modified);
908 p->last_accessed = base::Time::FromDoubleT(last_accessed);
909 p->creation_time = base::Time::FromDoubleT(creation_time);
910 return true;
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900911}
912
rvargas@chromium.org9e469f62014-01-28 06:36:00 +0900913void ParamTraits<base::File::Info>::Log(const param_type& p,
914 std::string* l) {
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900915 l->append("(");
916 LogParam(p.size, l);
917 l->append(",");
918 LogParam(p.is_directory, l);
919 l->append(",");
920 LogParam(p.last_modified.ToDoubleT(), l);
921 l->append(",");
922 LogParam(p.last_accessed.ToDoubleT(), l);
923 l->append(",");
924 LogParam(p.creation_time.ToDoubleT(), l);
925 l->append(")");
926}
927
rockot15c8ac42016-02-05 11:12:32 +0900928void ParamTraits<base::Time>::GetSize(base::PickleSizer* sizer,
929 const param_type& p) {
930 sizer->AddInt64();
931}
932
rockot6dbfea52016-02-04 05:20:16 +0900933void ParamTraits<base::Time>::Write(base::Pickle* m, const param_type& p) {
tfarina1cbfa082015-09-05 03:47:57 +0900934 ParamTraits<int64_t>::Write(m, p.ToInternalValue());
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900935}
936
rockot6dbfea52016-02-04 05:20:16 +0900937bool ParamTraits<base::Time>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +0900938 base::PickleIterator* iter,
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900939 param_type* r) {
tfarina1cbfa082015-09-05 03:47:57 +0900940 int64_t value;
941 if (!ParamTraits<int64_t>::Read(m, iter, &value))
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900942 return false;
943 *r = base::Time::FromInternalValue(value);
944 return true;
945}
946
947void ParamTraits<base::Time>::Log(const param_type& p, std::string* l) {
tfarina1cbfa082015-09-05 03:47:57 +0900948 ParamTraits<int64_t>::Log(p.ToInternalValue(), l);
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900949}
950
rockot15c8ac42016-02-05 11:12:32 +0900951void ParamTraits<base::TimeDelta>::GetSize(base::PickleSizer* sizer,
952 const param_type& p) {
953 sizer->AddInt64();
954}
955
rockot6dbfea52016-02-04 05:20:16 +0900956void ParamTraits<base::TimeDelta>::Write(base::Pickle* m, const param_type& p) {
tfarina1cbfa082015-09-05 03:47:57 +0900957 ParamTraits<int64_t>::Write(m, p.ToInternalValue());
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900958}
959
rockot6dbfea52016-02-04 05:20:16 +0900960bool ParamTraits<base::TimeDelta>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +0900961 base::PickleIterator* iter,
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900962 param_type* r) {
tfarina1cbfa082015-09-05 03:47:57 +0900963 int64_t value;
964 bool ret = ParamTraits<int64_t>::Read(m, iter, &value);
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900965 if (ret)
966 *r = base::TimeDelta::FromInternalValue(value);
967
968 return ret;
969}
970
971void ParamTraits<base::TimeDelta>::Log(const param_type& p, std::string* l) {
tfarina1cbfa082015-09-05 03:47:57 +0900972 ParamTraits<int64_t>::Log(p.ToInternalValue(), l);
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900973}
974
rockot15c8ac42016-02-05 11:12:32 +0900975void ParamTraits<base::TimeTicks>::GetSize(base::PickleSizer* sizer,
976 const param_type& p) {
977 sizer->AddInt64();
978}
979
rockot6dbfea52016-02-04 05:20:16 +0900980void ParamTraits<base::TimeTicks>::Write(base::Pickle* m, const param_type& p) {
tfarina1cbfa082015-09-05 03:47:57 +0900981 ParamTraits<int64_t>::Write(m, p.ToInternalValue());
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900982}
983
rockot6dbfea52016-02-04 05:20:16 +0900984bool ParamTraits<base::TimeTicks>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +0900985 base::PickleIterator* iter,
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900986 param_type* r) {
tfarina1cbfa082015-09-05 03:47:57 +0900987 int64_t value;
988 bool ret = ParamTraits<int64_t>::Read(m, iter, &value);
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900989 if (ret)
990 *r = base::TimeTicks::FromInternalValue(value);
991
992 return ret;
993}
994
995void ParamTraits<base::TimeTicks>::Log(const param_type& p, std::string* l) {
tfarina1cbfa082015-09-05 03:47:57 +0900996 ParamTraits<int64_t>::Log(p.ToInternalValue(), l);
brettw@chromium.orgce352e52012-06-05 06:18:25 +0900997}
998
jam1ea1e0d2016-05-14 00:09:58 +0900999void ParamTraits<IPC::ChannelHandle>::GetSize(base::PickleSizer* sizer,
1000 const param_type& p) {
1001 GetParamSize(sizer, p.name);
1002#if defined(OS_POSIX)
1003 GetParamSize(sizer, p.socket);
1004#endif
1005}
1006
rockot6dbfea52016-02-04 05:20:16 +09001007void ParamTraits<IPC::ChannelHandle>::Write(base::Pickle* m,
1008 const param_type& p) {
amit@chromium.org37290742012-01-24 11:36:05 +09001009#if defined(OS_WIN)
1010 // On Windows marshalling pipe handle is not supported.
1011 DCHECK(p.pipe.handle == NULL);
1012#endif // defined (OS_WIN)
erg@google.come6ffcb52010-08-18 03:38:24 +09001013 WriteParam(m, p.name);
1014#if defined(OS_POSIX)
1015 WriteParam(m, p.socket);
1016#endif
1017}
1018
rockot6dbfea52016-02-04 05:20:16 +09001019bool ParamTraits<IPC::ChannelHandle>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +09001020 base::PickleIterator* iter,
erg@google.come6ffcb52010-08-18 03:38:24 +09001021 param_type* r) {
1022 return ReadParam(m, iter, &r->name)
1023#if defined(OS_POSIX)
1024 && ReadParam(m, iter, &r->socket)
1025#endif
1026 ;
1027}
1028
1029void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p,
erg@google.com8aca7272010-08-19 03:33:57 +09001030 std::string* l) {
groby@chromium.org6b4dc5e2013-03-19 07:33:04 +09001031 l->append(base::StringPrintf("ChannelHandle(%s", p.name.c_str()));
erg@google.come6ffcb52010-08-18 03:38:24 +09001032#if defined(OS_POSIX)
steveblock@chromium.org7a470312011-09-23 19:32:19 +09001033 l->append(", ");
erg@google.come6ffcb52010-08-18 03:38:24 +09001034 ParamTraits<base::FileDescriptor>::Log(p.socket, l);
1035#endif
erg@google.com8aca7272010-08-19 03:33:57 +09001036 l->append(")");
erg@google.come6ffcb52010-08-18 03:38:24 +09001037}
1038
rockot15c8ac42016-02-05 11:12:32 +09001039void ParamTraits<LogData>::GetSize(base::PickleSizer* sizer,
1040 const param_type& p) {
1041 GetParamSize(sizer, p.channel);
1042 GetParamSize(sizer, p.routing_id);
1043 GetParamSize(sizer, p.type);
1044 GetParamSize(sizer, p.flags);
1045 GetParamSize(sizer, p.sent);
1046 GetParamSize(sizer, p.receive);
1047 GetParamSize(sizer, p.dispatch);
1048 GetParamSize(sizer, p.message_name);
1049 GetParamSize(sizer, p.params);
1050}
1051
rockot6dbfea52016-02-04 05:20:16 +09001052void ParamTraits<LogData>::Write(base::Pickle* m, const param_type& p) {
erg@google.com20b66e32010-10-01 05:06:30 +09001053 WriteParam(m, p.channel);
1054 WriteParam(m, p.routing_id);
jam@chromium.org1d8d4d12011-10-18 07:15:27 +09001055 WriteParam(m, p.type);
erg@google.com20b66e32010-10-01 05:06:30 +09001056 WriteParam(m, p.flags);
1057 WriteParam(m, p.sent);
1058 WriteParam(m, p.receive);
1059 WriteParam(m, p.dispatch);
tsepez@chromium.org6639c182012-11-15 12:17:45 +09001060 WriteParam(m, p.message_name);
erg@google.com20b66e32010-10-01 05:06:30 +09001061 WriteParam(m, p.params);
1062}
1063
rockot6dbfea52016-02-04 05:20:16 +09001064bool ParamTraits<LogData>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +09001065 base::PickleIterator* iter,
jbates@chromium.org0fc87362012-03-08 05:42:56 +09001066 param_type* r) {
jam@chromium.org1d8d4d12011-10-18 07:15:27 +09001067 return
erg@google.com20b66e32010-10-01 05:06:30 +09001068 ReadParam(m, iter, &r->channel) &&
1069 ReadParam(m, iter, &r->routing_id) &&
jam@chromium.org1d8d4d12011-10-18 07:15:27 +09001070 ReadParam(m, iter, &r->type) &&
erg@google.com20b66e32010-10-01 05:06:30 +09001071 ReadParam(m, iter, &r->flags) &&
1072 ReadParam(m, iter, &r->sent) &&
1073 ReadParam(m, iter, &r->receive) &&
1074 ReadParam(m, iter, &r->dispatch) &&
tsepez@chromium.org6639c182012-11-15 12:17:45 +09001075 ReadParam(m, iter, &r->message_name) &&
erg@google.com20b66e32010-10-01 05:06:30 +09001076 ReadParam(m, iter, &r->params);
erg@google.com20b66e32010-10-01 05:06:30 +09001077}
1078
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001079void ParamTraits<LogData>::Log(const param_type& p, std::string* l) {
1080 // Doesn't make sense to implement this!
1081}
1082
rockot6dbfea52016-02-04 05:20:16 +09001083void ParamTraits<Message>::Write(base::Pickle* m, const Message& p) {
brettw@chromium.orgf48910a2012-06-29 09:05:04 +09001084#if defined(OS_POSIX)
1085 // We don't serialize the file descriptors in the nested message, so there
1086 // better not be any.
morrita7d1bfcc2015-01-31 14:45:42 +09001087 DCHECK(!p.HasAttachments());
brettw@chromium.orgf48910a2012-06-29 09:05:04 +09001088#endif
1089
1090 // Don't just write out the message. This is used to send messages between
1091 // NaCl (Posix environment) and the browser (could be on Windows). The message
1092 // header formats differ between these systems (so does handle sharing, but
1093 // we already asserted we don't have any handles). So just write out the
1094 // parts of the header we use.
1095 //
1096 // Be careful also to use only explicitly-sized types. The NaCl environment
1097 // could be 64-bit and the host browser could be 32-bits. The nested message
1098 // may or may not be safe to send between 32-bit and 64-bit systems, but we
1099 // leave that up to the code sending the message to ensure.
tfarina1cbfa082015-09-05 03:47:57 +09001100 m->WriteUInt32(static_cast<uint32_t>(p.routing_id()));
brettw@chromium.orgf48910a2012-06-29 09:05:04 +09001101 m->WriteUInt32(p.type());
1102 m->WriteUInt32(p.flags());
tfarina1cbfa082015-09-05 03:47:57 +09001103 m->WriteData(p.payload(), static_cast<uint32_t>(p.payload_size()));
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001104}
1105
rockot6dbfea52016-02-04 05:20:16 +09001106bool ParamTraits<Message>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +09001107 base::PickleIterator* iter,
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001108 Message* r) {
tfarina1cbfa082015-09-05 03:47:57 +09001109 uint32_t routing_id, type, flags;
avic9f0ad02014-12-29 08:31:48 +09001110 if (!iter->ReadUInt32(&routing_id) ||
1111 !iter->ReadUInt32(&type) ||
1112 !iter->ReadUInt32(&flags))
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001113 return false;
brettw@chromium.orgf48910a2012-06-29 09:05:04 +09001114
1115 int payload_size;
1116 const char* payload;
avic9f0ad02014-12-29 08:31:48 +09001117 if (!iter->ReadData(&payload, &payload_size))
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001118 return false;
brettw@chromium.orgf48910a2012-06-29 09:05:04 +09001119
tfarina1cbfa082015-09-05 03:47:57 +09001120 r->SetHeaderValues(static_cast<int32_t>(routing_id), type, flags);
brettw@chromium.orgf48910a2012-06-29 09:05:04 +09001121 return r->WriteBytes(payload, payload_size);
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001122}
1123
1124void ParamTraits<Message>::Log(const Message& p, std::string* l) {
1125 l->append("<IPC::Message>");
1126}
1127
1128#if defined(OS_WIN)
rockot15c8ac42016-02-05 11:12:32 +09001129void ParamTraits<HANDLE>::GetSize(base::PickleSizer* sizer,
1130 const param_type& p) {
1131 sizer->AddInt();
1132}
1133
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001134// Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64
jschuh@chromium.orgfb87c342013-03-04 11:29:03 +09001135// bit systems. That's why we use the Windows macros to convert to 32 bits.
rockot6dbfea52016-02-04 05:20:16 +09001136void ParamTraits<HANDLE>::Write(base::Pickle* m, const param_type& p) {
jschuh@chromium.orgfb87c342013-03-04 11:29:03 +09001137 m->WriteInt(HandleToLong(p));
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001138}
1139
rockot6dbfea52016-02-04 05:20:16 +09001140bool ParamTraits<HANDLE>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +09001141 base::PickleIterator* iter,
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001142 param_type* r) {
tfarina1cbfa082015-09-05 03:47:57 +09001143 int32_t temp;
avic9f0ad02014-12-29 08:31:48 +09001144 if (!iter->ReadInt(&temp))
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001145 return false;
jschuh@chromium.orgfb87c342013-03-04 11:29:03 +09001146 *r = LongToHandle(temp);
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001147 return true;
1148}
1149
1150void ParamTraits<HANDLE>::Log(const param_type& p, std::string* l) {
brucedawsond7e5dfd2015-10-07 04:22:00 +09001151 l->append(base::StringPrintf("0x%p", p));
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001152}
1153
rockot15c8ac42016-02-05 11:12:32 +09001154void ParamTraits<LOGFONT>::GetSize(base::PickleSizer* sizer,
1155 const param_type& p) {
1156 sizer->AddData(sizeof(LOGFONT));
1157}
1158
rockot6dbfea52016-02-04 05:20:16 +09001159void ParamTraits<LOGFONT>::Write(base::Pickle* m, const param_type& p) {
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001160 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT));
1161}
1162
rockot6dbfea52016-02-04 05:20:16 +09001163bool ParamTraits<LOGFONT>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +09001164 base::PickleIterator* iter,
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001165 param_type* r) {
1166 const char *data;
1167 int data_size = 0;
avic9f0ad02014-12-29 08:31:48 +09001168 if (iter->ReadData(&data, &data_size) && data_size == sizeof(LOGFONT)) {
jschuh@chromium.orgb9ee7d62012-11-21 09:58:00 +09001169 const LOGFONT *font = reinterpret_cast<LOGFONT*>(const_cast<char*>(data));
1170 if (_tcsnlen(font->lfFaceName, LF_FACESIZE) < LF_FACESIZE) {
1171 memcpy(r, data, sizeof(LOGFONT));
1172 return true;
1173 }
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001174 }
1175
jschuh@chromium.orgb9ee7d62012-11-21 09:58:00 +09001176 NOTREACHED();
1177 return false;
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001178}
1179
1180void ParamTraits<LOGFONT>::Log(const param_type& p, std::string* l) {
groby@chromium.org6b4dc5e2013-03-19 07:33:04 +09001181 l->append(base::StringPrintf("<LOGFONT>"));
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001182}
1183
rockot15c8ac42016-02-05 11:12:32 +09001184void ParamTraits<MSG>::GetSize(base::PickleSizer* sizer, const param_type& p) {
1185 sizer->AddData(sizeof(MSG));
1186}
1187
rockot6dbfea52016-02-04 05:20:16 +09001188void ParamTraits<MSG>::Write(base::Pickle* m, const param_type& p) {
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001189 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG));
1190}
1191
rockot6dbfea52016-02-04 05:20:16 +09001192bool ParamTraits<MSG>::Read(const base::Pickle* m,
brettwf3146202015-06-03 13:29:25 +09001193 base::PickleIterator* iter,
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001194 param_type* r) {
1195 const char *data;
1196 int data_size = 0;
avic9f0ad02014-12-29 08:31:48 +09001197 bool result = iter->ReadData(&data, &data_size);
brettw@chromium.orgce352e52012-06-05 06:18:25 +09001198 if (result && data_size == sizeof(MSG)) {
1199 memcpy(r, data, sizeof(MSG));
1200 } else {
1201 result = false;
1202 NOTREACHED();
1203 }
1204
1205 return result;
1206}
1207
1208void ParamTraits<MSG>::Log(const param_type& p, std::string* l) {
1209 l->append("<MSG>");
1210}
1211
1212#endif // OS_WIN
1213
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09001214} // namespace IPC