Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Side by Side Diff: ipc/ipc_message_utils.cc

Issue 10498002: This groups the ParamTraits into different categories and orders the .cc file the same. It makes al… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ipc/ipc_message_utils.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "ipc/ipc_channel_handle.h"
16
15 #if defined(OS_POSIX) 17 #if defined(OS_POSIX)
16 #include "ipc/file_descriptor_set_posix.h" 18 #include "ipc/file_descriptor_set_posix.h"
17 #endif 19 #endif
18 #include "ipc/ipc_channel_handle.h"
19 20
20 namespace IPC { 21 namespace IPC {
21 22
23 namespace {
24
22 const int kMaxRecursionDepth = 100; 25 const int kMaxRecursionDepth = 100;
23 26
24 // Value serialization 27 template<typename CharType>
28 void LogBytes(const std::vector<CharType>& data, std::string* out) {
29 #if defined(OS_WIN)
30 // Windows has a GUI for logging, which can handle arbitrary binary data.
31 for (size_t i = 0; i < data.size(); ++i)
32 out->push_back(data[i]);
33 #else
34 // On POSIX, we log to stdout, which we assume can display ASCII.
35 static const size_t kMaxBytesToLog = 100;
36 for (size_t i = 0; i < std::min(data.size(), kMaxBytesToLog); ++i) {
37 if (isprint(data[i]))
38 out->push_back(data[i]);
39 else
40 out->append(StringPrintf("[%02X]", static_cast<unsigned char>(data[i])));
41 }
42 if (data.size() > kMaxBytesToLog) {
43 out->append(
44 StringPrintf(" and %u more bytes",
45 static_cast<unsigned>(data.size() - kMaxBytesToLog)));
46 }
47 #endif
48 }
25 49
26 static bool ReadValue(const Message* m, PickleIterator* iter, Value** value, 50 bool ReadValue(const Message* m, PickleIterator* iter, Value** value,
27 int recursion); 51 int recursion);
28 52
29 static void WriteValue(Message* m, const Value* value, int recursion) { 53 void WriteValue(Message* m, const Value* value, int recursion) {
30 if (recursion > kMaxRecursionDepth) { 54 if (recursion > kMaxRecursionDepth) {
31 LOG(WARNING) << "Max recursion depth hit in WriteValue."; 55 LOG(WARNING) << "Max recursion depth hit in WriteValue.";
32 return; 56 return;
33 } 57 }
34 58
35 m->WriteInt(value->GetType()); 59 m->WriteInt(value->GetType());
36 60
37 switch (value->GetType()) { 61 switch (value->GetType()) {
38 case Value::TYPE_NULL: 62 case Value::TYPE_NULL:
39 break; 63 break;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 NOTREACHED() << "ListValue::GetSize is a filthy liar."; 119 NOTREACHED() << "ListValue::GetSize is a filthy liar.";
96 } 120 }
97 } 121 }
98 break; 122 break;
99 } 123 }
100 } 124 }
101 } 125 }
102 126
103 // Helper for ReadValue that reads a DictionaryValue into a pre-allocated 127 // Helper for ReadValue that reads a DictionaryValue into a pre-allocated
104 // object. 128 // object.
105 static bool ReadDictionaryValue(const Message* m, PickleIterator* iter, 129 bool ReadDictionaryValue(const Message* m, PickleIterator* iter,
106 DictionaryValue* value, int recursion) { 130 DictionaryValue* value, int recursion) {
107 int size; 131 int size;
108 if (!ReadParam(m, iter, &size)) 132 if (!ReadParam(m, iter, &size))
109 return false; 133 return false;
110 134
111 for (int i = 0; i < size; ++i) { 135 for (int i = 0; i < size; ++i) {
112 std::string key; 136 std::string key;
113 Value* subval; 137 Value* subval;
114 if (!ReadParam(m, iter, &key) || 138 if (!ReadParam(m, iter, &key) ||
115 !ReadValue(m, iter, &subval, recursion + 1)) 139 !ReadValue(m, iter, &subval, recursion + 1))
116 return false; 140 return false;
117 value->SetWithoutPathExpansion(key, subval); 141 value->SetWithoutPathExpansion(key, subval);
118 } 142 }
119 143
120 return true; 144 return true;
121 } 145 }
122 146
123 // Helper for ReadValue that reads a ReadListValue into a pre-allocated 147 // Helper for ReadValue that reads a ReadListValue into a pre-allocated
124 // object. 148 // object.
125 static bool ReadListValue(const Message* m, PickleIterator* iter, 149 bool ReadListValue(const Message* m, PickleIterator* iter,
126 ListValue* value, int recursion) { 150 ListValue* value, int recursion) {
127 int size; 151 int size;
128 if (!ReadParam(m, iter, &size)) 152 if (!ReadParam(m, iter, &size))
129 return false; 153 return false;
130 154
131 for (int i = 0; i < size; ++i) { 155 for (int i = 0; i < size; ++i) {
132 Value* subval; 156 Value* subval;
133 if (!ReadValue(m, iter, &subval, recursion + 1)) 157 if (!ReadValue(m, iter, &subval, recursion + 1))
134 return false; 158 return false;
135 value->Set(i, subval); 159 value->Set(i, subval);
136 } 160 }
137 161
138 return true; 162 return true;
139 } 163 }
140 164
141 static bool ReadValue(const Message* m, PickleIterator* iter, Value** value, 165 bool ReadValue(const Message* m, PickleIterator* iter, Value** value,
142 int recursion) { 166 int recursion) {
143 if (recursion > kMaxRecursionDepth) { 167 if (recursion > kMaxRecursionDepth) {
144 LOG(WARNING) << "Max recursion depth hit in ReadValue."; 168 LOG(WARNING) << "Max recursion depth hit in ReadValue.";
145 return false; 169 return false;
146 } 170 }
147 171
148 int type; 172 int type;
149 if (!ReadParam(m, iter, &type)) 173 if (!ReadParam(m, iter, &type))
150 return false; 174 return false;
151 175
152 switch (type) { 176 switch (type) {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 *value = val.release(); 227 *value = val.release();
204 break; 228 break;
205 } 229 }
206 default: 230 default:
207 return false; 231 return false;
208 } 232 }
209 233
210 return true; 234 return true;
211 } 235 }
212 236
237 } // namespace
238
239 // -----------------------------------------------------------------------------
240
241 LogData::LogData()
242 : routing_id(0),
243 type(0),
244 sent(0),
245 receive(0),
246 dispatch(0) {
247 }
248
249 LogData::~LogData() {
250 }
251
252 MessageIterator::MessageIterator(const Message& m) : iter_(m) {
253 }
254
255 int MessageIterator::NextInt() const {
256 int val = -1;
257 if (!iter_.ReadInt(&val))
258 NOTREACHED();
259 return val;
260 }
261
262 const std::string MessageIterator::NextString() const {
263 std::string val;
264 if (!iter_.ReadString(&val))
265 NOTREACHED();
266 return val;
267 }
268
269 void ParamTraits<bool>::Log(const param_type& p, std::string* l) {
270 l->append(p ? "true" : "false");
271 }
272
213 void ParamTraits<int>::Log(const param_type& p, std::string* l) { 273 void ParamTraits<int>::Log(const param_type& p, std::string* l) {
214 l->append(base::IntToString(p)); 274 l->append(base::IntToString(p));
215 } 275 }
216 276
217 void ParamTraits<unsigned int>::Log(const param_type& p, std::string* l) { 277 void ParamTraits<unsigned int>::Log(const param_type& p, std::string* l) {
218 l->append(base::UintToString(p)); 278 l->append(base::UintToString(p));
219 } 279 }
220 280
221 void ParamTraits<long>::Log(const param_type& p, std::string* l) { 281 void ParamTraits<long>::Log(const param_type& p, std::string* l) {
222 l->append(base::Int64ToString(static_cast<int64>(p))); 282 l->append(base::Int64ToString(static_cast<int64>(p)));
(...skipping 21 matching lines...) Expand all
244 if (!m->ReadBytes(iter, &data, sizeof(param_type))) 304 if (!m->ReadBytes(iter, &data, sizeof(param_type)))
245 return false; 305 return false;
246 memcpy(r, data, sizeof(param_type)); 306 memcpy(r, data, sizeof(param_type));
247 return true; 307 return true;
248 } 308 }
249 309
250 void ParamTraits<unsigned short>::Log(const param_type& p, std::string* l) { 310 void ParamTraits<unsigned short>::Log(const param_type& p, std::string* l) {
251 l->append(base::UintToString(p)); 311 l->append(base::UintToString(p));
252 } 312 }
253 313
254 void ParamTraits<base::PlatformFileInfo>::Write( 314 void ParamTraits<float>::Write(Message* m, const param_type& p) {
255 Message* m, const param_type& p) { 315 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type));
256 WriteParam(m, p.size);
257 WriteParam(m, p.is_directory);
258 WriteParam(m, p.last_modified.ToDoubleT());
259 WriteParam(m, p.last_accessed.ToDoubleT());
260 WriteParam(m, p.creation_time.ToDoubleT());
261 } 316 }
262 317
263 bool ParamTraits<base::PlatformFileInfo>::Read( 318 bool ParamTraits<float>::Read(const Message* m, PickleIterator* iter,
264 const Message* m, PickleIterator* iter, param_type* p) { 319 param_type* r) {
265 double last_modified; 320 const char *data;
266 double last_accessed; 321 int data_size;
267 double creation_time; 322 if (!m->ReadData(iter, &data, &data_size) ||
268 bool result = 323 data_size != sizeof(param_type)) {
269 ReadParam(m, iter, &p->size) && 324 NOTREACHED();
270 ReadParam(m, iter, &p->is_directory) && 325 return false;
271 ReadParam(m, iter, &last_modified) &&
272 ReadParam(m, iter, &last_accessed) &&
273 ReadParam(m, iter, &creation_time);
274 if (result) {
275 p->last_modified = base::Time::FromDoubleT(last_modified);
276 p->last_accessed = base::Time::FromDoubleT(last_accessed);
277 p->creation_time = base::Time::FromDoubleT(creation_time);
278 } 326 }
279 return result; 327 memcpy(r, data, sizeof(param_type));
280 }
281
282 void ParamTraits<base::PlatformFileInfo>::Log(
283 const param_type& p, std::string* l) {
284 l->append("(");
285 LogParam(p.size, l);
286 l->append(",");
287 LogParam(p.is_directory, l);
288 l->append(",");
289 LogParam(p.last_modified.ToDoubleT(), l);
290 l->append(",");
291 LogParam(p.last_accessed.ToDoubleT(), l);
292 l->append(",");
293 LogParam(p.creation_time.ToDoubleT(), l);
294 l->append(")");
295 }
296
297 void ParamTraits<base::Time>::Write(Message* m, const param_type& p) {
298 ParamTraits<int64>::Write(m, p.ToInternalValue());
299 }
300
301 bool ParamTraits<base::Time>::Read(const Message* m, PickleIterator* iter,
302 param_type* r) {
303 int64 value;
304 if (!ParamTraits<int64>::Read(m, iter, &value))
305 return false;
306 *r = base::Time::FromInternalValue(value);
307 return true; 328 return true;
308 } 329 }
309 330
310 void ParamTraits<base::Time>::Log(const param_type& p, std::string* l) { 331 void ParamTraits<float>::Log(const param_type& p, std::string* l) {
311 ParamTraits<int64>::Log(p.ToInternalValue(), l); 332 l->append(StringPrintf("%e", p));
312 } 333 }
313 334
314 void ParamTraits<base::TimeDelta> ::Write(Message* m, const param_type& p) { 335 void ParamTraits<double>::Write(Message* m, const param_type& p) {
315 ParamTraits<int64> ::Write(m, p.ToInternalValue()); 336 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type));
316 } 337 }
317 338
318 bool ParamTraits<base::TimeDelta> ::Read(const Message* m, 339 bool ParamTraits<double>::Read(const Message* m, PickleIterator* iter,
319 PickleIterator* iter, 340 param_type* r) {
320 param_type* r) { 341 const char *data;
321 int64 value; 342 int data_size;
322 bool ret = ParamTraits<int64> ::Read(m, iter, &value); 343 if (!m->ReadData(iter, &data, &data_size) ||
323 if (ret) 344 data_size != sizeof(param_type)) {
324 *r = base::TimeDelta::FromInternalValue(value); 345 NOTREACHED();
325 346 return false;
326 return ret; 347 }
348 memcpy(r, data, sizeof(param_type));
349 return true;
327 } 350 }
328 351
329 void ParamTraits<base::TimeDelta> ::Log(const param_type& p, std::string* l) { 352 void ParamTraits<double>::Log(const param_type& p, std::string* l) {
330 ParamTraits<int64> ::Log(p.ToInternalValue(), l); 353 l->append(StringPrintf("%e", p));
331 } 354 }
332 355
333 void ParamTraits<base::TimeTicks> ::Write(Message* m, const param_type& p) { 356
334 ParamTraits<int64> ::Write(m, p.ToInternalValue()); 357 void ParamTraits<std::string>::Log(const param_type& p, std::string* l) {
358 l->append(p);
335 } 359 }
336 360
337 bool ParamTraits<base::TimeTicks> ::Read(const Message* m, 361 void ParamTraits<std::wstring>::Log(const param_type& p, std::string* l) {
338 PickleIterator* iter, 362 l->append(WideToUTF8(p));
339 param_type* r) {
340 int64 value;
341 bool ret = ParamTraits<int64> ::Read(m, iter, &value);
342 if (ret)
343 *r = base::TimeTicks::FromInternalValue(value);
344
345 return ret;
346 } 363 }
347 364
348 void ParamTraits<base::TimeTicks> ::Log(const param_type& p, std::string* l) { 365 #if !defined(WCHAR_T_IS_UTF16)
349 ParamTraits<int64> ::Log(p.ToInternalValue(), l); 366 void ParamTraits<string16>::Log(const param_type& p, std::string* l) {
367 l->append(UTF16ToUTF8(p));
368 }
369 #endif
370
371 void ParamTraits<std::vector<char> >::Write(Message* m, const param_type& p) {
372 if (p.empty()) {
373 m->WriteData(NULL, 0);
374 } else {
375 m->WriteData(&p.front(), static_cast<int>(p.size()));
376 }
377 }
378
379 bool ParamTraits<std::vector<char> >::Read(const Message* m,
380 PickleIterator* iter,
381 param_type* r) {
382 const char *data;
383 int data_size = 0;
384 if (!m->ReadData(iter, &data, &data_size) || data_size < 0)
385 return false;
386 r->resize(data_size);
387 if (data_size)
388 memcpy(&r->front(), data, data_size);
389 return true;
390 }
391
392 void ParamTraits<std::vector<char> >::Log(const param_type& p, std::string* l) {
393 LogBytes(p, l);
394 }
395
396 void ParamTraits<std::vector<unsigned char> >::Write(Message* m,
397 const param_type& p) {
398 if (p.empty()) {
399 m->WriteData(NULL, 0);
400 } else {
401 m->WriteData(reinterpret_cast<const char*>(&p.front()),
402 static_cast<int>(p.size()));
403 }
404 }
405
406 bool ParamTraits<std::vector<unsigned char> >::Read(const Message* m,
407 PickleIterator* iter,
408 param_type* r) {
409 const char *data;
410 int data_size = 0;
411 if (!m->ReadData(iter, &data, &data_size) || data_size < 0)
412 return false;
413 r->resize(data_size);
414 if (data_size)
415 memcpy(&r->front(), data, data_size);
416 return true;
417 }
418
419 void ParamTraits<std::vector<unsigned char> >::Log(const param_type& p,
420 std::string* l) {
421 LogBytes(p, l);
422 }
423
424 void ParamTraits<std::vector<bool> >::Write(Message* m, const param_type& p) {
425 WriteParam(m, static_cast<int>(p.size()));
426 for (size_t i = 0; i < p.size(); i++)
427 WriteParam(m, p[i]);
428 }
429
430 bool ParamTraits<std::vector<bool> >::Read(const Message* m,
431 PickleIterator* iter,
432 param_type* r) {
433 int size;
434 // ReadLength() checks for < 0 itself.
435 if (!m->ReadLength(iter, &size))
436 return false;
437 r->resize(size);
438 for (int i = 0; i < size; i++) {
439 bool value;
440 if (!ReadParam(m, iter, &value))
441 return false;
442 (*r)[i] = value;
443 }
444 return true;
445 }
446
447 void ParamTraits<std::vector<bool> >::Log(const param_type& p, std::string* l) {
448 for (size_t i = 0; i < p.size(); ++i) {
449 if (i != 0)
450 l->push_back(' ');
451 LogParam((p[i]), l);
452 }
350 } 453 }
351 454
352 void ParamTraits<DictionaryValue>::Write(Message* m, const param_type& p) { 455 void ParamTraits<DictionaryValue>::Write(Message* m, const param_type& p) {
353 WriteValue(m, &p, 0); 456 WriteValue(m, &p, 0);
354 } 457 }
355 458
356 bool ParamTraits<DictionaryValue>::Read( 459 bool ParamTraits<DictionaryValue>::Read(
357 const Message* m, PickleIterator* iter, param_type* r) { 460 const Message* m, PickleIterator* iter, param_type* r) {
358 int type; 461 int type;
359 if (!ReadParam(m, iter, &type) || type != Value::TYPE_DICTIONARY) 462 if (!ReadParam(m, iter, &type) || type != Value::TYPE_DICTIONARY)
360 return false; 463 return false;
361 464
362 return ReadDictionaryValue(m, iter, r, 0); 465 return ReadDictionaryValue(m, iter, r, 0);
363 } 466 }
364 467
365 void ParamTraits<DictionaryValue>::Log(const param_type& p, std::string* l) { 468 void ParamTraits<DictionaryValue>::Log(const param_type& p, std::string* l) {
366 std::string json; 469 std::string json;
367 base::JSONWriter::Write(&p, &json); 470 base::JSONWriter::Write(&p, &json);
368 l->append(json); 471 l->append(json);
369 } 472 }
370 473
474 #if defined(OS_POSIX)
475 void ParamTraits<base::FileDescriptor>::Write(Message* m, const param_type& p) {
476 const bool valid = p.fd >= 0;
477 WriteParam(m, valid);
478
479 if (valid) {
480 if (!m->WriteFileDescriptor(p))
481 NOTREACHED();
482 }
483 }
484
485 bool ParamTraits<base::FileDescriptor>::Read(const Message* m,
486 PickleIterator* iter,
487 param_type* r) {
488 bool valid;
489 if (!ReadParam(m, iter, &valid))
490 return false;
491
492 if (!valid) {
493 r->fd = -1;
494 r->auto_close = false;
495 return true;
496 }
497
498 return m->ReadFileDescriptor(iter, r);
499 }
500
501 void ParamTraits<base::FileDescriptor>::Log(const param_type& p,
502 std::string* l) {
503 if (p.auto_close) {
504 l->append(StringPrintf("FD(%d auto-close)", p.fd));
505 } else {
506 l->append(StringPrintf("FD(%d)", p.fd));
507 }
508 }
509 #endif // defined(OS_POSIX)
510
511 void ParamTraits<FilePath>::Write(Message* m, const param_type& p) {
512 ParamTraits<FilePath::StringType>::Write(m, p.value());
513 }
514
515 bool ParamTraits<FilePath>::Read(const Message* m,
516 PickleIterator* iter,
517 param_type* r) {
518 FilePath::StringType value;
519 if (!ParamTraits<FilePath::StringType>::Read(m, iter, &value))
520 return false;
521 *r = FilePath(value);
522 return true;
523 }
524
525 void ParamTraits<FilePath>::Log(const param_type& p, std::string* l) {
526 ParamTraits<FilePath::StringType>::Log(p.value(), l);
527 }
528
371 void ParamTraits<ListValue>::Write(Message* m, const param_type& p) { 529 void ParamTraits<ListValue>::Write(Message* m, const param_type& p) {
372 WriteValue(m, &p, 0); 530 WriteValue(m, &p, 0);
373 } 531 }
374 532
375 bool ParamTraits<ListValue>::Read( 533 bool ParamTraits<ListValue>::Read(
376 const Message* m, PickleIterator* iter, param_type* r) { 534 const Message* m, PickleIterator* iter, param_type* r) {
377 int type; 535 int type;
378 if (!ReadParam(m, iter, &type) || type != Value::TYPE_LIST) 536 if (!ReadParam(m, iter, &type) || type != Value::TYPE_LIST)
379 return false; 537 return false;
380 538
381 return ReadListValue(m, iter, r, 0); 539 return ReadListValue(m, iter, r, 0);
382 } 540 }
383 541
384 void ParamTraits<ListValue>::Log(const param_type& p, std::string* l) { 542 void ParamTraits<ListValue>::Log(const param_type& p, std::string* l) {
385 std::string json; 543 std::string json;
386 base::JSONWriter::Write(&p, &json); 544 base::JSONWriter::Write(&p, &json);
387 l->append(json); 545 l->append(json);
388 } 546 }
389 547
390 void ParamTraits<std::wstring>::Log(const param_type& p, std::string* l) {
391 l->append(WideToUTF8(p));
392 }
393
394 void ParamTraits<NullableString16>::Write(Message* m, const param_type& p) { 548 void ParamTraits<NullableString16>::Write(Message* m, const param_type& p) {
395 WriteParam(m, p.string()); 549 WriteParam(m, p.string());
396 WriteParam(m, p.is_null()); 550 WriteParam(m, p.is_null());
397 } 551 }
398 552
399 bool ParamTraits<NullableString16>::Read(const Message* m, PickleIterator* iter, 553 bool ParamTraits<NullableString16>::Read(const Message* m, PickleIterator* iter,
400 param_type* r) { 554 param_type* r) {
401 string16 string; 555 string16 string;
402 if (!ReadParam(m, iter, &string)) 556 if (!ReadParam(m, iter, &string))
403 return false; 557 return false;
404 bool is_null; 558 bool is_null;
405 if (!ReadParam(m, iter, &is_null)) 559 if (!ReadParam(m, iter, &is_null))
406 return false; 560 return false;
407 *r = NullableString16(string, is_null); 561 *r = NullableString16(string, is_null);
408 return true; 562 return true;
409 } 563 }
410 564
411 void ParamTraits<NullableString16>::Log(const param_type& p, std::string* l) { 565 void ParamTraits<NullableString16>::Log(const param_type& p, std::string* l) {
412 l->append("("); 566 l->append("(");
413 LogParam(p.string(), l); 567 LogParam(p.string(), l);
414 l->append(", "); 568 l->append(", ");
415 LogParam(p.is_null(), l); 569 LogParam(p.is_null(), l);
416 l->append(")"); 570 l->append(")");
417 } 571 }
418 572
419 #if !defined(WCHAR_T_IS_UTF16) 573 void ParamTraits<base::PlatformFileInfo>::Write(Message* m,
420 void ParamTraits<string16>::Log(const param_type& p, std::string* l) { 574 const param_type& p) {
421 l->append(UTF16ToUTF8(p)); 575 WriteParam(m, p.size);
422 } 576 WriteParam(m, p.is_directory);
423 #endif 577 WriteParam(m, p.last_modified.ToDoubleT());
424 578 WriteParam(m, p.last_accessed.ToDoubleT());
425 579 WriteParam(m, p.creation_time.ToDoubleT());
426 void ParamTraits<FilePath>::Write(Message* m, const param_type& p) {
427 ParamTraits<FilePath::StringType>::Write(m, p.value());
428 } 580 }
429 581
430 bool ParamTraits<FilePath>::Read(const Message* m, 582 bool ParamTraits<base::PlatformFileInfo>::Read(const Message* m,
431 PickleIterator* iter, 583 PickleIterator* iter,
432 param_type* r) { 584 param_type* p) {
433 FilePath::StringType value; 585 double last_modified;
434 if (!ParamTraits<FilePath::StringType>::Read(m, iter, &value)) 586 double last_accessed;
587 double creation_time;
588 bool result =
589 ReadParam(m, iter, &p->size) &&
590 ReadParam(m, iter, &p->is_directory) &&
591 ReadParam(m, iter, &last_modified) &&
592 ReadParam(m, iter, &last_accessed) &&
593 ReadParam(m, iter, &creation_time);
594 if (result) {
595 p->last_modified = base::Time::FromDoubleT(last_modified);
596 p->last_accessed = base::Time::FromDoubleT(last_accessed);
597 p->creation_time = base::Time::FromDoubleT(creation_time);
598 }
599 return result;
600 }
601
602 void ParamTraits<base::PlatformFileInfo>::Log(const param_type& p,
603 std::string* l) {
604 l->append("(");
605 LogParam(p.size, l);
606 l->append(",");
607 LogParam(p.is_directory, l);
608 l->append(",");
609 LogParam(p.last_modified.ToDoubleT(), l);
610 l->append(",");
611 LogParam(p.last_accessed.ToDoubleT(), l);
612 l->append(",");
613 LogParam(p.creation_time.ToDoubleT(), l);
614 l->append(")");
615 }
616
617 void ParamTraits<base::Time>::Write(Message* m, const param_type& p) {
618 ParamTraits<int64>::Write(m, p.ToInternalValue());
619 }
620
621 bool ParamTraits<base::Time>::Read(const Message* m, PickleIterator* iter,
622 param_type* r) {
623 int64 value;
624 if (!ParamTraits<int64>::Read(m, iter, &value))
435 return false; 625 return false;
436 *r = FilePath(value); 626 *r = base::Time::FromInternalValue(value);
437 return true; 627 return true;
438 } 628 }
439 629
440 void ParamTraits<FilePath>::Log(const param_type& p, std::string* l) { 630 void ParamTraits<base::Time>::Log(const param_type& p, std::string* l) {
441 ParamTraits<FilePath::StringType>::Log(p.value(), l); 631 ParamTraits<int64>::Log(p.ToInternalValue(), l);
442 } 632 }
443 633
444 #if defined(OS_POSIX) 634 void ParamTraits<base::TimeDelta>::Write(Message* m, const param_type& p) {
445 void ParamTraits<base::FileDescriptor>::Write(Message* m, const param_type& p) { 635 ParamTraits<int64>::Write(m, p.ToInternalValue());
446 const bool valid = p.fd >= 0;
447 WriteParam(m, valid);
448
449 if (valid) {
450 if (!m->WriteFileDescriptor(p))
451 NOTREACHED();
452 }
453 } 636 }
454 637
455 bool ParamTraits<base::FileDescriptor>::Read(const Message* m, 638 bool ParamTraits<base::TimeDelta>::Read(const Message* m,
456 PickleIterator* iter, 639 PickleIterator* iter,
457 param_type* r) { 640 param_type* r) {
458 bool valid; 641 int64 value;
459 if (!ReadParam(m, iter, &valid)) 642 bool ret = ParamTraits<int64>::Read(m, iter, &value);
460 return false; 643 if (ret)
644 *r = base::TimeDelta::FromInternalValue(value);
461 645
462 if (!valid) { 646 return ret;
463 r->fd = -1;
464 r->auto_close = false;
465 return true;
466 }
467
468 return m->ReadFileDescriptor(iter, r);
469 } 647 }
470 648
471 void ParamTraits<base::FileDescriptor>::Log(const param_type& p, 649 void ParamTraits<base::TimeDelta>::Log(const param_type& p, std::string* l) {
472 std::string* l) { 650 ParamTraits<int64>::Log(p.ToInternalValue(), l);
473 if (p.auto_close) {
474 l->append(StringPrintf("FD(%d auto-close)", p.fd));
475 } else {
476 l->append(StringPrintf("FD(%d)", p.fd));
477 }
478 } 651 }
479 #endif // defined(OS_POSIX) 652
653 void ParamTraits<base::TimeTicks>::Write(Message* m, const param_type& p) {
654 ParamTraits<int64>::Write(m, p.ToInternalValue());
655 }
656
657 bool ParamTraits<base::TimeTicks>::Read(const Message* m,
658 PickleIterator* iter,
659 param_type* r) {
660 int64 value;
661 bool ret = ParamTraits<int64>::Read(m, iter, &value);
662 if (ret)
663 *r = base::TimeTicks::FromInternalValue(value);
664
665 return ret;
666 }
667
668 void ParamTraits<base::TimeTicks>::Log(const param_type& p, std::string* l) {
669 ParamTraits<int64>::Log(p.ToInternalValue(), l);
670 }
480 671
481 void ParamTraits<IPC::ChannelHandle>::Write(Message* m, const param_type& p) { 672 void ParamTraits<IPC::ChannelHandle>::Write(Message* m, const param_type& p) {
482 #if defined(OS_WIN) 673 #if defined(OS_WIN)
483 // On Windows marshalling pipe handle is not supported. 674 // On Windows marshalling pipe handle is not supported.
484 DCHECK(p.pipe.handle == NULL); 675 DCHECK(p.pipe.handle == NULL);
485 #endif // defined (OS_WIN) 676 #endif // defined (OS_WIN)
486 WriteParam(m, p.name); 677 WriteParam(m, p.name);
487 #if defined(OS_POSIX) 678 #if defined(OS_POSIX)
488 WriteParam(m, p.socket); 679 WriteParam(m, p.socket);
489 #endif 680 #endif
(...skipping 12 matching lines...) Expand all
502 void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p, 693 void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p,
503 std::string* l) { 694 std::string* l) {
504 l->append(StringPrintf("ChannelHandle(%s", p.name.c_str())); 695 l->append(StringPrintf("ChannelHandle(%s", p.name.c_str()));
505 #if defined(OS_POSIX) 696 #if defined(OS_POSIX)
506 l->append(", "); 697 l->append(", ");
507 ParamTraits<base::FileDescriptor>::Log(p.socket, l); 698 ParamTraits<base::FileDescriptor>::Log(p.socket, l);
508 #endif 699 #endif
509 l->append(")"); 700 l->append(")");
510 } 701 }
511 702
512 LogData::LogData()
513 : routing_id(0),
514 type(0),
515 sent(0),
516 receive(0),
517 dispatch(0) {
518 }
519
520 LogData::~LogData() {
521 }
522
523 void ParamTraits<LogData>::Write(Message* m, const param_type& p) { 703 void ParamTraits<LogData>::Write(Message* m, const param_type& p) {
524 WriteParam(m, p.channel); 704 WriteParam(m, p.channel);
525 WriteParam(m, p.routing_id); 705 WriteParam(m, p.routing_id);
526 WriteParam(m, p.type); 706 WriteParam(m, p.type);
527 WriteParam(m, p.flags); 707 WriteParam(m, p.flags);
528 WriteParam(m, p.sent); 708 WriteParam(m, p.sent);
529 WriteParam(m, p.receive); 709 WriteParam(m, p.receive);
530 WriteParam(m, p.dispatch); 710 WriteParam(m, p.dispatch);
531 WriteParam(m, p.params); 711 WriteParam(m, p.params);
532 } 712 }
533 713
534 bool ParamTraits<LogData>::Read(const Message* m, 714 bool ParamTraits<LogData>::Read(const Message* m,
535 PickleIterator* iter, 715 PickleIterator* iter,
536 param_type* r) { 716 param_type* r) {
537 return 717 return
538 ReadParam(m, iter, &r->channel) && 718 ReadParam(m, iter, &r->channel) &&
539 ReadParam(m, iter, &r->routing_id) && 719 ReadParam(m, iter, &r->routing_id) &&
540 ReadParam(m, iter, &r->type) && 720 ReadParam(m, iter, &r->type) &&
541 ReadParam(m, iter, &r->flags) && 721 ReadParam(m, iter, &r->flags) &&
542 ReadParam(m, iter, &r->sent) && 722 ReadParam(m, iter, &r->sent) &&
543 ReadParam(m, iter, &r->receive) && 723 ReadParam(m, iter, &r->receive) &&
544 ReadParam(m, iter, &r->dispatch) && 724 ReadParam(m, iter, &r->dispatch) &&
545 ReadParam(m, iter, &r->params); 725 ReadParam(m, iter, &r->params);
546 } 726 }
547 727
728 void ParamTraits<LogData>::Log(const param_type& p, std::string* l) {
729 // Doesn't make sense to implement this!
730 }
731
732 void ParamTraits<Message>::Write(Message* m, const Message& p) {
733 DCHECK(p.size() <= INT_MAX);
734 int message_size = static_cast<int>(p.size());
735 m->WriteInt(message_size);
736 m->WriteData(reinterpret_cast<const char*>(p.data()), message_size);
737 }
738
739 bool ParamTraits<Message>::Read(const Message* m, PickleIterator* iter,
740 Message* r) {
741 int size;
742 if (!m->ReadInt(iter, &size))
743 return false;
744 const char* data;
745 if (!m->ReadData(iter, &data, &size))
746 return false;
747 *r = Message(data, size);
748 return true;
749 }
750
751 void ParamTraits<Message>::Log(const Message& p, std::string* l) {
752 l->append("<IPC::Message>");
753 }
754
755 #if defined(OS_WIN)
756 // Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64
757 // bit systems.
758 void ParamTraits<HANDLE>::Write(Message* m, const param_type& p) {
759 m->WriteUInt32(reinterpret_cast<uint32>(p));
760 }
761
762 bool ParamTraits<HANDLE>::Read(const Message* m, PickleIterator* iter,
763 param_type* r) {
764 uint32 temp;
765 if (!m->ReadUInt32(iter, &temp))
766 return false;
767 *r = reinterpret_cast<HANDLE>(temp);
768 return true;
769 }
770
771 void ParamTraits<HANDLE>::Log(const param_type& p, std::string* l) {
772 l->append(StringPrintf("0x%X", p));
773 }
774
775 void ParamTraits<LOGFONT>::Write(Message* m, const param_type& p) {
776 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT));
777 }
778
779 bool ParamTraits<LOGFONT>::Read(const Message* m, PickleIterator* iter,
780 param_type* r) {
781 const char *data;
782 int data_size = 0;
783 bool result = m->ReadData(iter, &data, &data_size);
784 if (result && data_size == sizeof(LOGFONT)) {
785 memcpy(r, data, sizeof(LOGFONT));
786 } else {
787 result = false;
788 NOTREACHED();
789 }
790
791 return result;
792 }
793
794 void ParamTraits<LOGFONT>::Log(const param_type& p, std::string* l) {
795 l->append(StringPrintf("<LOGFONT>"));
796 }
797
798 void ParamTraits<MSG>::Write(Message* m, const param_type& p) {
799 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG));
800 }
801
802 bool ParamTraits<MSG>::Read(const Message* m, PickleIterator* iter,
803 param_type* r) {
804 const char *data;
805 int data_size = 0;
806 bool result = m->ReadData(iter, &data, &data_size);
807 if (result && data_size == sizeof(MSG)) {
808 memcpy(r, data, sizeof(MSG));
809 } else {
810 result = false;
811 NOTREACHED();
812 }
813
814 return result;
815 }
816
817 void ParamTraits<MSG>::Log(const param_type& p, std::string* l) {
818 l->append("<MSG>");
819 }
820
821 #endif // OS_WIN
822
548 } // namespace IPC 823 } // namespace IPC
OLDNEW
« no previous file with comments | « ipc/ipc_message_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698