OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ipc/ipc_message_utils.h" | 5 #include "ipc/ipc_message_utils.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/nullable_string16.h" | 10 #include "base/nullable_string16.h" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "base/time.h" | 12 #include "base/time.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #if defined(OS_POSIX) | 15 #if defined(OS_POSIX) |
16 #include "ipc/file_descriptor_set_posix.h" | 16 #include "ipc/file_descriptor_set_posix.h" |
17 #endif | 17 #endif |
18 #include "ipc/ipc_channel_handle.h" | 18 #include "ipc/ipc_channel_handle.h" |
19 | 19 |
20 namespace IPC { | 20 namespace IPC { |
21 | 21 |
22 const int kMaxRecursionDepth = 100; | 22 const int kMaxRecursionDepth = 100; |
23 | 23 |
24 // Value serialization | 24 // Value serialization |
25 | 25 |
26 static bool ReadValue(const Message* m, void** iter, Value** value, | 26 static bool ReadValue(const Message* m, PickleIterator* iter, Value** value, |
27 int recursion); | 27 int recursion); |
28 | 28 |
29 static void WriteValue(Message* m, const Value* value, int recursion) { | 29 static void WriteValue(Message* m, const Value* value, int recursion) { |
30 if (recursion > kMaxRecursionDepth) { | 30 if (recursion > kMaxRecursionDepth) { |
31 LOG(WARNING) << "Max recursion depth hit in WriteValue."; | 31 LOG(WARNING) << "Max recursion depth hit in WriteValue."; |
32 return; | 32 return; |
33 } | 33 } |
34 | 34 |
35 m->WriteInt(value->GetType()); | 35 m->WriteInt(value->GetType()); |
36 | 36 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 NOTREACHED() << "ListValue::GetSize is a filthy liar."; | 95 NOTREACHED() << "ListValue::GetSize is a filthy liar."; |
96 } | 96 } |
97 } | 97 } |
98 break; | 98 break; |
99 } | 99 } |
100 } | 100 } |
101 } | 101 } |
102 | 102 |
103 // Helper for ReadValue that reads a DictionaryValue into a pre-allocated | 103 // Helper for ReadValue that reads a DictionaryValue into a pre-allocated |
104 // object. | 104 // object. |
105 static bool ReadDictionaryValue(const Message* m, void** iter, | 105 static bool ReadDictionaryValue(const Message* m, PickleIterator* iter, |
106 DictionaryValue* value, int recursion) { | 106 DictionaryValue* value, int recursion) { |
107 int size; | 107 int size; |
108 if (!ReadParam(m, iter, &size)) | 108 if (!ReadParam(m, iter, &size)) |
109 return false; | 109 return false; |
110 | 110 |
111 for (int i = 0; i < size; ++i) { | 111 for (int i = 0; i < size; ++i) { |
112 std::string key; | 112 std::string key; |
113 Value* subval; | 113 Value* subval; |
114 if (!ReadParam(m, iter, &key) || | 114 if (!ReadParam(m, iter, &key) || |
115 !ReadValue(m, iter, &subval, recursion + 1)) | 115 !ReadValue(m, iter, &subval, recursion + 1)) |
116 return false; | 116 return false; |
117 value->SetWithoutPathExpansion(key, subval); | 117 value->SetWithoutPathExpansion(key, subval); |
118 } | 118 } |
119 | 119 |
120 return true; | 120 return true; |
121 } | 121 } |
122 | 122 |
123 // Helper for ReadValue that reads a ReadListValue into a pre-allocated | 123 // Helper for ReadValue that reads a ReadListValue into a pre-allocated |
124 // object. | 124 // object. |
125 static bool ReadListValue(const Message* m, void** iter, | 125 static bool ReadListValue(const Message* m, PickleIterator* iter, |
126 ListValue* value, int recursion) { | 126 ListValue* value, int recursion) { |
127 int size; | 127 int size; |
128 if (!ReadParam(m, iter, &size)) | 128 if (!ReadParam(m, iter, &size)) |
129 return false; | 129 return false; |
130 | 130 |
131 for (int i = 0; i < size; ++i) { | 131 for (int i = 0; i < size; ++i) { |
132 Value* subval; | 132 Value* subval; |
133 if (!ReadValue(m, iter, &subval, recursion + 1)) | 133 if (!ReadValue(m, iter, &subval, recursion + 1)) |
134 return false; | 134 return false; |
135 value->Set(i, subval); | 135 value->Set(i, subval); |
136 } | 136 } |
137 | 137 |
138 return true; | 138 return true; |
139 } | 139 } |
140 | 140 |
141 static bool ReadValue(const Message* m, void** iter, Value** value, | 141 static bool ReadValue(const Message* m, PickleIterator* iter, Value** value, |
142 int recursion) { | 142 int recursion) { |
143 if (recursion > kMaxRecursionDepth) { | 143 if (recursion > kMaxRecursionDepth) { |
144 LOG(WARNING) << "Max recursion depth hit in ReadValue."; | 144 LOG(WARNING) << "Max recursion depth hit in ReadValue."; |
145 return false; | 145 return false; |
146 } | 146 } |
147 | 147 |
148 int type; | 148 int type; |
149 if (!ReadParam(m, iter, &type)) | 149 if (!ReadParam(m, iter, &type)) |
150 return false; | 150 return false; |
151 | 151 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 } | 231 } |
232 | 232 |
233 void ParamTraits<unsigned long long>::Log(const param_type& p, std::string* l) { | 233 void ParamTraits<unsigned long long>::Log(const param_type& p, std::string* l) { |
234 l->append(base::Uint64ToString(p)); | 234 l->append(base::Uint64ToString(p)); |
235 } | 235 } |
236 | 236 |
237 void ParamTraits<unsigned short>::Write(Message* m, const param_type& p) { | 237 void ParamTraits<unsigned short>::Write(Message* m, const param_type& p) { |
238 m->WriteBytes(&p, sizeof(param_type)); | 238 m->WriteBytes(&p, sizeof(param_type)); |
239 } | 239 } |
240 | 240 |
241 bool ParamTraits<unsigned short>::Read(const Message* m, void** iter, | 241 bool ParamTraits<unsigned short>::Read(const Message* m, PickleIterator* iter, |
242 param_type* r) { | 242 param_type* r) { |
243 const char* data; | 243 const char* data; |
244 if (!m->ReadBytes(iter, &data, sizeof(param_type))) | 244 if (!m->ReadBytes(iter, &data, sizeof(param_type))) |
245 return false; | 245 return false; |
246 memcpy(r, data, sizeof(param_type)); | 246 memcpy(r, data, sizeof(param_type)); |
247 return true; | 247 return true; |
248 } | 248 } |
249 | 249 |
250 void ParamTraits<unsigned short>::Log(const param_type& p, std::string* l) { | 250 void ParamTraits<unsigned short>::Log(const param_type& p, std::string* l) { |
251 l->append(base::UintToString(p)); | 251 l->append(base::UintToString(p)); |
252 } | 252 } |
253 | 253 |
254 void ParamTraits<base::Time>::Write(Message* m, const param_type& p) { | 254 void ParamTraits<base::Time>::Write(Message* m, const param_type& p) { |
255 ParamTraits<int64>::Write(m, p.ToInternalValue()); | 255 ParamTraits<int64>::Write(m, p.ToInternalValue()); |
256 } | 256 } |
257 | 257 |
258 bool ParamTraits<base::Time>::Read(const Message* m, void** iter, | 258 bool ParamTraits<base::Time>::Read(const Message* m, PickleIterator* iter, |
259 param_type* r) { | 259 param_type* r) { |
260 int64 value; | 260 int64 value; |
261 if (!ParamTraits<int64>::Read(m, iter, &value)) | 261 if (!ParamTraits<int64>::Read(m, iter, &value)) |
262 return false; | 262 return false; |
263 *r = base::Time::FromInternalValue(value); | 263 *r = base::Time::FromInternalValue(value); |
264 return true; | 264 return true; |
265 } | 265 } |
266 | 266 |
267 void ParamTraits<base::Time>::Log(const param_type& p, std::string* l) { | 267 void ParamTraits<base::Time>::Log(const param_type& p, std::string* l) { |
268 ParamTraits<int64>::Log(p.ToInternalValue(), l); | 268 ParamTraits<int64>::Log(p.ToInternalValue(), l); |
269 } | 269 } |
270 | 270 |
271 void ParamTraits<base::TimeDelta> ::Write(Message* m, const param_type& p) { | 271 void ParamTraits<base::TimeDelta> ::Write(Message* m, const param_type& p) { |
272 ParamTraits<int64> ::Write(m, p.ToInternalValue()); | 272 ParamTraits<int64> ::Write(m, p.ToInternalValue()); |
273 } | 273 } |
274 | 274 |
275 bool ParamTraits<base::TimeDelta> ::Read(const Message* m, | 275 bool ParamTraits<base::TimeDelta> ::Read(const Message* m, |
276 void** iter, | 276 PickleIterator* iter, |
277 param_type* r) { | 277 param_type* r) { |
278 int64 value; | 278 int64 value; |
279 bool ret = ParamTraits<int64> ::Read(m, iter, &value); | 279 bool ret = ParamTraits<int64> ::Read(m, iter, &value); |
280 if (ret) | 280 if (ret) |
281 *r = base::TimeDelta::FromInternalValue(value); | 281 *r = base::TimeDelta::FromInternalValue(value); |
282 | 282 |
283 return ret; | 283 return ret; |
284 } | 284 } |
285 | 285 |
286 void ParamTraits<base::TimeDelta> ::Log(const param_type& p, std::string* l) { | 286 void ParamTraits<base::TimeDelta> ::Log(const param_type& p, std::string* l) { |
287 ParamTraits<int64> ::Log(p.ToInternalValue(), l); | 287 ParamTraits<int64> ::Log(p.ToInternalValue(), l); |
288 } | 288 } |
289 | 289 |
290 void ParamTraits<base::TimeTicks> ::Write(Message* m, const param_type& p) { | 290 void ParamTraits<base::TimeTicks> ::Write(Message* m, const param_type& p) { |
291 ParamTraits<int64> ::Write(m, p.ToInternalValue()); | 291 ParamTraits<int64> ::Write(m, p.ToInternalValue()); |
292 } | 292 } |
293 | 293 |
294 bool ParamTraits<base::TimeTicks> ::Read(const Message* m, | 294 bool ParamTraits<base::TimeTicks> ::Read(const Message* m, |
295 void** iter, | 295 PickleIterator* iter, |
296 param_type* r) { | 296 param_type* r) { |
297 int64 value; | 297 int64 value; |
298 bool ret = ParamTraits<int64> ::Read(m, iter, &value); | 298 bool ret = ParamTraits<int64> ::Read(m, iter, &value); |
299 if (ret) | 299 if (ret) |
300 *r = base::TimeTicks::FromInternalValue(value); | 300 *r = base::TimeTicks::FromInternalValue(value); |
301 | 301 |
302 return ret; | 302 return ret; |
303 } | 303 } |
304 | 304 |
305 void ParamTraits<base::TimeTicks> ::Log(const param_type& p, std::string* l) { | 305 void ParamTraits<base::TimeTicks> ::Log(const param_type& p, std::string* l) { |
306 ParamTraits<int64> ::Log(p.ToInternalValue(), l); | 306 ParamTraits<int64> ::Log(p.ToInternalValue(), l); |
307 } | 307 } |
308 | 308 |
309 void ParamTraits<DictionaryValue>::Write(Message* m, const param_type& p) { | 309 void ParamTraits<DictionaryValue>::Write(Message* m, const param_type& p) { |
310 WriteValue(m, &p, 0); | 310 WriteValue(m, &p, 0); |
311 } | 311 } |
312 | 312 |
313 bool ParamTraits<DictionaryValue>::Read( | 313 bool ParamTraits<DictionaryValue>::Read( |
314 const Message* m, void** iter, param_type* r) { | 314 const Message* m, PickleIterator* iter, param_type* r) { |
315 int type; | 315 int type; |
316 if (!ReadParam(m, iter, &type) || type != Value::TYPE_DICTIONARY) | 316 if (!ReadParam(m, iter, &type) || type != Value::TYPE_DICTIONARY) |
317 return false; | 317 return false; |
318 | 318 |
319 return ReadDictionaryValue(m, iter, r, 0); | 319 return ReadDictionaryValue(m, iter, r, 0); |
320 } | 320 } |
321 | 321 |
322 void ParamTraits<DictionaryValue>::Log(const param_type& p, std::string* l) { | 322 void ParamTraits<DictionaryValue>::Log(const param_type& p, std::string* l) { |
323 std::string json; | 323 std::string json; |
324 base::JSONWriter::Write(&p, false, &json); | 324 base::JSONWriter::Write(&p, false, &json); |
325 l->append(json); | 325 l->append(json); |
326 } | 326 } |
327 | 327 |
328 void ParamTraits<ListValue>::Write(Message* m, const param_type& p) { | 328 void ParamTraits<ListValue>::Write(Message* m, const param_type& p) { |
329 WriteValue(m, &p, 0); | 329 WriteValue(m, &p, 0); |
330 } | 330 } |
331 | 331 |
332 bool ParamTraits<ListValue>::Read( | 332 bool ParamTraits<ListValue>::Read( |
333 const Message* m, void** iter, param_type* r) { | 333 const Message* m, PickleIterator* iter, param_type* r) { |
334 int type; | 334 int type; |
335 if (!ReadParam(m, iter, &type) || type != Value::TYPE_LIST) | 335 if (!ReadParam(m, iter, &type) || type != Value::TYPE_LIST) |
336 return false; | 336 return false; |
337 | 337 |
338 return ReadListValue(m, iter, r, 0); | 338 return ReadListValue(m, iter, r, 0); |
339 } | 339 } |
340 | 340 |
341 void ParamTraits<ListValue>::Log(const param_type& p, std::string* l) { | 341 void ParamTraits<ListValue>::Log(const param_type& p, std::string* l) { |
342 std::string json; | 342 std::string json; |
343 base::JSONWriter::Write(&p, false, &json); | 343 base::JSONWriter::Write(&p, false, &json); |
344 l->append(json); | 344 l->append(json); |
345 } | 345 } |
346 | 346 |
347 void ParamTraits<std::wstring>::Log(const param_type& p, std::string* l) { | 347 void ParamTraits<std::wstring>::Log(const param_type& p, std::string* l) { |
348 l->append(WideToUTF8(p)); | 348 l->append(WideToUTF8(p)); |
349 } | 349 } |
350 | 350 |
351 void ParamTraits<NullableString16>::Write(Message* m, const param_type& p) { | 351 void ParamTraits<NullableString16>::Write(Message* m, const param_type& p) { |
352 WriteParam(m, p.string()); | 352 WriteParam(m, p.string()); |
353 WriteParam(m, p.is_null()); | 353 WriteParam(m, p.is_null()); |
354 } | 354 } |
355 | 355 |
356 bool ParamTraits<NullableString16>::Read(const Message* m, void** iter, | 356 bool ParamTraits<NullableString16>::Read(const Message* m, PickleIterator* iter, |
357 param_type* r) { | 357 param_type* r) { |
358 string16 string; | 358 string16 string; |
359 if (!ReadParam(m, iter, &string)) | 359 if (!ReadParam(m, iter, &string)) |
360 return false; | 360 return false; |
361 bool is_null; | 361 bool is_null; |
362 if (!ReadParam(m, iter, &is_null)) | 362 if (!ReadParam(m, iter, &is_null)) |
363 return false; | 363 return false; |
364 *r = NullableString16(string, is_null); | 364 *r = NullableString16(string, is_null); |
365 return true; | 365 return true; |
366 } | 366 } |
(...skipping 10 matching lines...) Expand all Loading... |
377 void ParamTraits<string16>::Log(const param_type& p, std::string* l) { | 377 void ParamTraits<string16>::Log(const param_type& p, std::string* l) { |
378 l->append(UTF16ToUTF8(p)); | 378 l->append(UTF16ToUTF8(p)); |
379 } | 379 } |
380 #endif | 380 #endif |
381 | 381 |
382 | 382 |
383 void ParamTraits<FilePath>::Write(Message* m, const param_type& p) { | 383 void ParamTraits<FilePath>::Write(Message* m, const param_type& p) { |
384 ParamTraits<FilePath::StringType>::Write(m, p.value()); | 384 ParamTraits<FilePath::StringType>::Write(m, p.value()); |
385 } | 385 } |
386 | 386 |
387 bool ParamTraits<FilePath>::Read(const Message* m, void** iter, param_type* r) { | 387 bool ParamTraits<FilePath>::Read(const Message* m, |
| 388 PickleIterator* iter, |
| 389 param_type* r) { |
388 FilePath::StringType value; | 390 FilePath::StringType value; |
389 if (!ParamTraits<FilePath::StringType>::Read(m, iter, &value)) | 391 if (!ParamTraits<FilePath::StringType>::Read(m, iter, &value)) |
390 return false; | 392 return false; |
391 *r = FilePath(value); | 393 *r = FilePath(value); |
392 return true; | 394 return true; |
393 } | 395 } |
394 | 396 |
395 void ParamTraits<FilePath>::Log(const param_type& p, std::string* l) { | 397 void ParamTraits<FilePath>::Log(const param_type& p, std::string* l) { |
396 ParamTraits<FilePath::StringType>::Log(p.value(), l); | 398 ParamTraits<FilePath::StringType>::Log(p.value(), l); |
397 } | 399 } |
398 | 400 |
399 #if defined(OS_POSIX) | 401 #if defined(OS_POSIX) |
400 void ParamTraits<base::FileDescriptor>::Write(Message* m, const param_type& p) { | 402 void ParamTraits<base::FileDescriptor>::Write(Message* m, const param_type& p) { |
401 const bool valid = p.fd >= 0; | 403 const bool valid = p.fd >= 0; |
402 WriteParam(m, valid); | 404 WriteParam(m, valid); |
403 | 405 |
404 if (valid) { | 406 if (valid) { |
405 if (!m->WriteFileDescriptor(p)) | 407 if (!m->WriteFileDescriptor(p)) |
406 NOTREACHED(); | 408 NOTREACHED(); |
407 } | 409 } |
408 } | 410 } |
409 | 411 |
410 bool ParamTraits<base::FileDescriptor>::Read(const Message* m, void** iter, | 412 bool ParamTraits<base::FileDescriptor>::Read(const Message* m, |
| 413 PickleIterator* iter, |
411 param_type* r) { | 414 param_type* r) { |
412 bool valid; | 415 bool valid; |
413 if (!ReadParam(m, iter, &valid)) | 416 if (!ReadParam(m, iter, &valid)) |
414 return false; | 417 return false; |
415 | 418 |
416 if (!valid) { | 419 if (!valid) { |
417 r->fd = -1; | 420 r->fd = -1; |
418 r->auto_close = false; | 421 r->auto_close = false; |
419 return true; | 422 return true; |
420 } | 423 } |
(...skipping 15 matching lines...) Expand all Loading... |
436 #if defined(OS_WIN) | 439 #if defined(OS_WIN) |
437 // On Windows marshalling pipe handle is not supported. | 440 // On Windows marshalling pipe handle is not supported. |
438 DCHECK(p.pipe.handle == NULL); | 441 DCHECK(p.pipe.handle == NULL); |
439 #endif // defined (OS_WIN) | 442 #endif // defined (OS_WIN) |
440 WriteParam(m, p.name); | 443 WriteParam(m, p.name); |
441 #if defined(OS_POSIX) | 444 #if defined(OS_POSIX) |
442 WriteParam(m, p.socket); | 445 WriteParam(m, p.socket); |
443 #endif | 446 #endif |
444 } | 447 } |
445 | 448 |
446 bool ParamTraits<IPC::ChannelHandle>::Read(const Message* m, void** iter, | 449 bool ParamTraits<IPC::ChannelHandle>::Read(const Message* m, |
| 450 PickleIterator* iter, |
447 param_type* r) { | 451 param_type* r) { |
448 return ReadParam(m, iter, &r->name) | 452 return ReadParam(m, iter, &r->name) |
449 #if defined(OS_POSIX) | 453 #if defined(OS_POSIX) |
450 && ReadParam(m, iter, &r->socket) | 454 && ReadParam(m, iter, &r->socket) |
451 #endif | 455 #endif |
452 ; | 456 ; |
453 } | 457 } |
454 | 458 |
455 void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p, | 459 void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p, |
456 std::string* l) { | 460 std::string* l) { |
(...skipping 20 matching lines...) Expand all Loading... |
477 WriteParam(m, p.channel); | 481 WriteParam(m, p.channel); |
478 WriteParam(m, p.routing_id); | 482 WriteParam(m, p.routing_id); |
479 WriteParam(m, p.type); | 483 WriteParam(m, p.type); |
480 WriteParam(m, p.flags); | 484 WriteParam(m, p.flags); |
481 WriteParam(m, p.sent); | 485 WriteParam(m, p.sent); |
482 WriteParam(m, p.receive); | 486 WriteParam(m, p.receive); |
483 WriteParam(m, p.dispatch); | 487 WriteParam(m, p.dispatch); |
484 WriteParam(m, p.params); | 488 WriteParam(m, p.params); |
485 } | 489 } |
486 | 490 |
487 bool ParamTraits<LogData>::Read(const Message* m, void** iter, param_type* r) { | 491 bool ParamTraits<LogData>::Read(const Message* m, |
| 492 PickleIterator* iter, |
| 493 param_type* r) { |
488 return | 494 return |
489 ReadParam(m, iter, &r->channel) && | 495 ReadParam(m, iter, &r->channel) && |
490 ReadParam(m, iter, &r->routing_id) && | 496 ReadParam(m, iter, &r->routing_id) && |
491 ReadParam(m, iter, &r->type) && | 497 ReadParam(m, iter, &r->type) && |
492 ReadParam(m, iter, &r->flags) && | 498 ReadParam(m, iter, &r->flags) && |
493 ReadParam(m, iter, &r->sent) && | 499 ReadParam(m, iter, &r->sent) && |
494 ReadParam(m, iter, &r->receive) && | 500 ReadParam(m, iter, &r->receive) && |
495 ReadParam(m, iter, &r->dispatch) && | 501 ReadParam(m, iter, &r->dispatch) && |
496 ReadParam(m, iter, &r->params); | 502 ReadParam(m, iter, &r->params); |
497 } | 503 } |
498 | 504 |
499 } // namespace IPC | 505 } // namespace IPC |
OLD | NEW |