[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ipc/ipc_message_utils.h" |
| 6 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 7 | #include "base/file_path.h" |
[email protected] | 93d49d7 | 2009-10-23 20:00:20 | [diff] [blame] | 8 | #include "base/json/json_writer.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 9 | #include "base/memory/scoped_ptr.h" |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 10 | #include "base/nullable_string16.h" |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 11 | #include "base/string_number_conversions.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 12 | #include "base/time.h" |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 13 | #include "base/utf_string_conversions.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 14 | #include "base/values.h" |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 15 | #if defined(OS_POSIX) |
| 16 | #include "ipc/file_descriptor_set_posix.h" |
| 17 | #endif |
| 18 | #include "ipc/ipc_channel_handle.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 19 | |
| 20 | namespace IPC { |
| 21 | |
| 22 | const int kMaxRecursionDepth = 100; |
| 23 | |
| 24 | // Value serialization |
| 25 | |
| 26 | static bool ReadValue(const Message* m, void** iter, Value** value, |
| 27 | int recursion); |
| 28 | |
| 29 | static void WriteValue(Message* m, const Value* value, int recursion) { |
| 30 | if (recursion > kMaxRecursionDepth) { |
| 31 | LOG(WARNING) << "Max recursion depth hit in WriteValue."; |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | m->WriteInt(value->GetType()); |
| 36 | |
| 37 | switch (value->GetType()) { |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 38 | case Value::TYPE_NULL: |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 39 | break; |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 40 | case Value::TYPE_BOOLEAN: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 41 | bool val; |
| 42 | value->GetAsBoolean(&val); |
| 43 | WriteParam(m, val); |
| 44 | break; |
| 45 | } |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 46 | case Value::TYPE_INTEGER: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 47 | int val; |
| 48 | value->GetAsInteger(&val); |
| 49 | WriteParam(m, val); |
| 50 | break; |
| 51 | } |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 52 | case Value::TYPE_DOUBLE: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 53 | double val; |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 54 | value->GetAsDouble(&val); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 55 | WriteParam(m, val); |
| 56 | break; |
| 57 | } |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 58 | case Value::TYPE_STRING: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 59 | std::string val; |
| 60 | value->GetAsString(&val); |
| 61 | WriteParam(m, val); |
| 62 | break; |
| 63 | } |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 64 | case Value::TYPE_BINARY: { |
[email protected] | b59ea31 | 2011-08-05 18:20:05 | [diff] [blame] | 65 | const base::BinaryValue* binary = |
| 66 | static_cast<const base::BinaryValue*>(value); |
[email protected] | 7ee1a44c | 2010-07-23 14:18:59 | [diff] [blame] | 67 | m->WriteData(binary->GetBuffer(), static_cast<int>(binary->GetSize())); |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 68 | break; |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 69 | } |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 70 | case Value::TYPE_DICTIONARY: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 71 | const DictionaryValue* dict = static_cast<const DictionaryValue*>(value); |
| 72 | |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 73 | WriteParam(m, static_cast<int>(dict->size())); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 74 | |
| 75 | for (DictionaryValue::key_iterator it = dict->begin_keys(); |
| 76 | it != dict->end_keys(); ++it) { |
| 77 | Value* subval; |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 78 | if (dict->GetWithoutPathExpansion(*it, &subval)) { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 79 | WriteParam(m, *it); |
| 80 | WriteValue(m, subval, recursion + 1); |
| 81 | } else { |
| 82 | NOTREACHED() << "DictionaryValue iterators are filthy liars."; |
| 83 | } |
| 84 | } |
| 85 | break; |
| 86 | } |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 87 | case Value::TYPE_LIST: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 88 | const ListValue* list = static_cast<const ListValue*>(value); |
| 89 | WriteParam(m, static_cast<int>(list->GetSize())); |
| 90 | for (size_t i = 0; i < list->GetSize(); ++i) { |
| 91 | Value* subval; |
| 92 | if (list->Get(i, &subval)) { |
| 93 | WriteValue(m, subval, recursion + 1); |
| 94 | } else { |
| 95 | NOTREACHED() << "ListValue::GetSize is a filthy liar."; |
| 96 | } |
| 97 | } |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // Helper for ReadValue that reads a DictionaryValue into a pre-allocated |
| 104 | // object. |
| 105 | static bool ReadDictionaryValue(const Message* m, void** iter, |
| 106 | DictionaryValue* value, int recursion) { |
| 107 | int size; |
| 108 | if (!ReadParam(m, iter, &size)) |
| 109 | return false; |
| 110 | |
| 111 | for (int i = 0; i < size; ++i) { |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 112 | std::string key; |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 113 | Value* subval; |
| 114 | if (!ReadParam(m, iter, &key) || |
| 115 | !ReadValue(m, iter, &subval, recursion + 1)) |
| 116 | return false; |
[email protected] | d8b4aa4 | 2011-08-19 05:59:57 | [diff] [blame] | 117 | value->SetWithoutPathExpansion(key, subval); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | // Helper for ReadValue that reads a ReadListValue into a pre-allocated |
| 124 | // object. |
| 125 | static bool ReadListValue(const Message* m, void** iter, |
| 126 | ListValue* value, int recursion) { |
| 127 | int size; |
| 128 | if (!ReadParam(m, iter, &size)) |
| 129 | return false; |
| 130 | |
| 131 | for (int i = 0; i < size; ++i) { |
| 132 | Value* subval; |
| 133 | if (!ReadValue(m, iter, &subval, recursion + 1)) |
| 134 | return false; |
| 135 | value->Set(i, subval); |
| 136 | } |
| 137 | |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | static bool ReadValue(const Message* m, void** iter, Value** value, |
| 142 | int recursion) { |
| 143 | if (recursion > kMaxRecursionDepth) { |
| 144 | LOG(WARNING) << "Max recursion depth hit in ReadValue."; |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | int type; |
| 149 | if (!ReadParam(m, iter, &type)) |
| 150 | return false; |
| 151 | |
| 152 | switch (type) { |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 153 | case Value::TYPE_NULL: |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 154 | *value = Value::CreateNullValue(); |
| 155 | break; |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 156 | case Value::TYPE_BOOLEAN: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 157 | bool val; |
| 158 | if (!ReadParam(m, iter, &val)) |
| 159 | return false; |
| 160 | *value = Value::CreateBooleanValue(val); |
| 161 | break; |
| 162 | } |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 163 | case Value::TYPE_INTEGER: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 164 | int val; |
| 165 | if (!ReadParam(m, iter, &val)) |
| 166 | return false; |
| 167 | *value = Value::CreateIntegerValue(val); |
| 168 | break; |
| 169 | } |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 170 | case Value::TYPE_DOUBLE: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 171 | double val; |
| 172 | if (!ReadParam(m, iter, &val)) |
| 173 | return false; |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 174 | *value = Value::CreateDoubleValue(val); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 175 | break; |
| 176 | } |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 177 | case Value::TYPE_STRING: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 178 | std::string val; |
| 179 | if (!ReadParam(m, iter, &val)) |
| 180 | return false; |
| 181 | *value = Value::CreateStringValue(val); |
| 182 | break; |
| 183 | } |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 184 | case Value::TYPE_BINARY: { |
| 185 | const char* data; |
| 186 | int length; |
| 187 | if (!m->ReadData(iter, &data, &length)) |
| 188 | return false; |
[email protected] | b59ea31 | 2011-08-05 18:20:05 | [diff] [blame] | 189 | *value = base::BinaryValue::CreateWithCopiedBuffer(data, length); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 190 | break; |
| 191 | } |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 192 | case Value::TYPE_DICTIONARY: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 193 | scoped_ptr<DictionaryValue> val(new DictionaryValue()); |
| 194 | if (!ReadDictionaryValue(m, iter, val.get(), recursion)) |
| 195 | return false; |
| 196 | *value = val.release(); |
| 197 | break; |
| 198 | } |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 199 | case Value::TYPE_LIST: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 200 | scoped_ptr<ListValue> val(new ListValue()); |
| 201 | if (!ReadListValue(m, iter, val.get(), recursion)) |
| 202 | return false; |
| 203 | *value = val.release(); |
| 204 | break; |
| 205 | } |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 206 | default: |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 207 | return false; |
| 208 | } |
| 209 | |
| 210 | return true; |
| 211 | } |
| 212 | |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 213 | void ParamTraits<int>::Log(const param_type& p, std::string* l) { |
| 214 | l->append(base::IntToString(p)); |
| 215 | } |
| 216 | |
| 217 | void ParamTraits<unsigned int>::Log(const param_type& p, std::string* l) { |
| 218 | l->append(base::UintToString(p)); |
| 219 | } |
| 220 | |
| 221 | void ParamTraits<long>::Log(const param_type& p, std::string* l) { |
| 222 | l->append(base::Int64ToString(static_cast<int64>(p))); |
| 223 | } |
| 224 | |
| 225 | void ParamTraits<unsigned long>::Log(const param_type& p, std::string* l) { |
| 226 | l->append(base::Uint64ToString(static_cast<uint64>(p))); |
| 227 | } |
| 228 | |
| 229 | void ParamTraits<long long>::Log(const param_type& p, std::string* l) { |
| 230 | l->append(base::Int64ToString(static_cast<int64>(p))); |
| 231 | } |
| 232 | |
| 233 | void ParamTraits<unsigned long long>::Log(const param_type& p, std::string* l) { |
| 234 | l->append(base::Uint64ToString(p)); |
| 235 | } |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 236 | |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 237 | void ParamTraits<unsigned short>::Write(Message* m, const param_type& p) { |
| 238 | m->WriteBytes(&p, sizeof(param_type)); |
| 239 | } |
| 240 | |
| 241 | bool ParamTraits<unsigned short>::Read(const Message* m, void** iter, |
| 242 | param_type* r) { |
| 243 | const char* data; |
| 244 | if (!m->ReadBytes(iter, &data, sizeof(param_type))) |
| 245 | return false; |
| 246 | memcpy(r, data, sizeof(param_type)); |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | void ParamTraits<unsigned short>::Log(const param_type& p, std::string* l) { |
| 251 | l->append(base::UintToString(p)); |
| 252 | } |
| 253 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 254 | void ParamTraits<base::Time>::Write(Message* m, const param_type& p) { |
| 255 | ParamTraits<int64>::Write(m, p.ToInternalValue()); |
| 256 | } |
| 257 | |
| 258 | bool ParamTraits<base::Time>::Read(const Message* m, void** iter, |
| 259 | param_type* r) { |
| 260 | int64 value; |
| 261 | if (!ParamTraits<int64>::Read(m, iter, &value)) |
| 262 | return false; |
| 263 | *r = base::Time::FromInternalValue(value); |
| 264 | return true; |
| 265 | } |
| 266 | |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 267 | void ParamTraits<base::Time>::Log(const param_type& p, std::string* l) { |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 268 | ParamTraits<int64>::Log(p.ToInternalValue(), l); |
| 269 | } |
| 270 | |
[email protected] | d84e48b | 2010-10-21 22:04:52 | [diff] [blame] | 271 | void ParamTraits<base::TimeDelta> ::Write(Message* m, const param_type& p) { |
[email protected] | 1d14f58 | 2011-09-02 20:42:04 | [diff] [blame] | 272 | ParamTraits<int64> ::Write(m, p.ToInternalValue()); |
[email protected] | d84e48b | 2010-10-21 22:04:52 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | bool ParamTraits<base::TimeDelta> ::Read(const Message* m, |
| 276 | void** iter, |
| 277 | param_type* r) { |
| 278 | int64 value; |
| 279 | bool ret = ParamTraits<int64> ::Read(m, iter, &value); |
| 280 | if (ret) |
[email protected] | 1d14f58 | 2011-09-02 20:42:04 | [diff] [blame] | 281 | *r = base::TimeDelta::FromInternalValue(value); |
[email protected] | d84e48b | 2010-10-21 22:04:52 | [diff] [blame] | 282 | |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | void ParamTraits<base::TimeDelta> ::Log(const param_type& p, std::string* l) { |
[email protected] | 1d14f58 | 2011-09-02 20:42:04 | [diff] [blame] | 287 | ParamTraits<int64> ::Log(p.ToInternalValue(), l); |
| 288 | } |
| 289 | |
| 290 | void ParamTraits<base::TimeTicks> ::Write(Message* m, const param_type& p) { |
| 291 | ParamTraits<int64> ::Write(m, p.ToInternalValue()); |
| 292 | } |
| 293 | |
| 294 | bool ParamTraits<base::TimeTicks> ::Read(const Message* m, |
| 295 | void** iter, |
| 296 | param_type* r) { |
| 297 | int64 value; |
| 298 | bool ret = ParamTraits<int64> ::Read(m, iter, &value); |
| 299 | if (ret) |
| 300 | *r = base::TimeTicks::FromInternalValue(value); |
| 301 | |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | void ParamTraits<base::TimeTicks> ::Log(const param_type& p, std::string* l) { |
| 306 | ParamTraits<int64> ::Log(p.ToInternalValue(), l); |
[email protected] | d84e48b | 2010-10-21 22:04:52 | [diff] [blame] | 307 | } |
| 308 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 309 | void ParamTraits<DictionaryValue>::Write(Message* m, const param_type& p) { |
| 310 | WriteValue(m, &p, 0); |
| 311 | } |
| 312 | |
| 313 | bool ParamTraits<DictionaryValue>::Read( |
| 314 | const Message* m, void** iter, param_type* r) { |
| 315 | int type; |
| 316 | if (!ReadParam(m, iter, &type) || type != Value::TYPE_DICTIONARY) |
| 317 | return false; |
| 318 | |
| 319 | return ReadDictionaryValue(m, iter, r, 0); |
| 320 | } |
| 321 | |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 322 | void ParamTraits<DictionaryValue>::Log(const param_type& p, std::string* l) { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 323 | std::string json; |
[email protected] | 93d49d7 | 2009-10-23 20:00:20 | [diff] [blame] | 324 | base::JSONWriter::Write(&p, false, &json); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 325 | l->append(json); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | void ParamTraits<ListValue>::Write(Message* m, const param_type& p) { |
| 329 | WriteValue(m, &p, 0); |
| 330 | } |
| 331 | |
| 332 | bool ParamTraits<ListValue>::Read( |
| 333 | const Message* m, void** iter, param_type* r) { |
| 334 | int type; |
| 335 | if (!ReadParam(m, iter, &type) || type != Value::TYPE_LIST) |
| 336 | return false; |
| 337 | |
| 338 | return ReadListValue(m, iter, r, 0); |
| 339 | } |
| 340 | |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 341 | void ParamTraits<ListValue>::Log(const param_type& p, std::string* l) { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 342 | std::string json; |
[email protected] | 93d49d7 | 2009-10-23 20:00:20 | [diff] [blame] | 343 | base::JSONWriter::Write(&p, false, &json); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 344 | l->append(json); |
| 345 | } |
| 346 | |
| 347 | void ParamTraits<std::wstring>::Log(const param_type& p, std::string* l) { |
| 348 | l->append(WideToUTF8(p)); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 349 | } |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 350 | |
| 351 | void ParamTraits<NullableString16>::Write(Message* m, const param_type& p) { |
| 352 | WriteParam(m, p.string()); |
| 353 | WriteParam(m, p.is_null()); |
| 354 | } |
| 355 | |
| 356 | bool ParamTraits<NullableString16>::Read(const Message* m, void** iter, |
| 357 | param_type* r) { |
| 358 | string16 string; |
| 359 | if (!ReadParam(m, iter, &string)) |
| 360 | return false; |
| 361 | bool is_null; |
| 362 | if (!ReadParam(m, iter, &is_null)) |
| 363 | return false; |
| 364 | *r = NullableString16(string, is_null); |
| 365 | return true; |
| 366 | } |
| 367 | |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 368 | void ParamTraits<NullableString16>::Log(const param_type& p, std::string* l) { |
| 369 | l->append("("); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 370 | LogParam(p.string(), l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 371 | l->append(", "); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 372 | LogParam(p.is_null(), l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 373 | l->append(")"); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 374 | } |
| 375 | |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 376 | #if !defined(WCHAR_T_IS_UTF16) |
| 377 | void ParamTraits<string16>::Log(const param_type& p, std::string* l) { |
| 378 | l->append(UTF16ToUTF8(p)); |
| 379 | } |
| 380 | #endif |
| 381 | |
| 382 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 383 | void ParamTraits<FilePath>::Write(Message* m, const param_type& p) { |
| 384 | ParamTraits<FilePath::StringType>::Write(m, p.value()); |
| 385 | } |
| 386 | |
| 387 | bool ParamTraits<FilePath>::Read(const Message* m, void** iter, param_type* r) { |
| 388 | FilePath::StringType value; |
| 389 | if (!ParamTraits<FilePath::StringType>::Read(m, iter, &value)) |
| 390 | return false; |
| 391 | *r = FilePath(value); |
| 392 | return true; |
| 393 | } |
| 394 | |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 395 | void ParamTraits<FilePath>::Log(const param_type& p, std::string* l) { |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 396 | ParamTraits<FilePath::StringType>::Log(p.value(), l); |
| 397 | } |
| 398 | |
| 399 | #if defined(OS_POSIX) |
| 400 | void ParamTraits<base::FileDescriptor>::Write(Message* m, const param_type& p) { |
| 401 | const bool valid = p.fd >= 0; |
| 402 | WriteParam(m, valid); |
| 403 | |
| 404 | if (valid) { |
| 405 | if (!m->WriteFileDescriptor(p)) |
| 406 | NOTREACHED(); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | bool ParamTraits<base::FileDescriptor>::Read(const Message* m, void** iter, |
| 411 | param_type* r) { |
| 412 | bool valid; |
| 413 | if (!ReadParam(m, iter, &valid)) |
| 414 | return false; |
| 415 | |
| 416 | if (!valid) { |
| 417 | r->fd = -1; |
| 418 | r->auto_close = false; |
| 419 | return true; |
| 420 | } |
| 421 | |
| 422 | return m->ReadFileDescriptor(iter, r); |
| 423 | } |
| 424 | |
| 425 | void ParamTraits<base::FileDescriptor>::Log(const param_type& p, |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 426 | std::string* l) { |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 427 | if (p.auto_close) { |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 428 | l->append(StringPrintf("FD(%d auto-close)", p.fd)); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 429 | } else { |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 430 | l->append(StringPrintf("FD(%d)", p.fd)); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | #endif // defined(OS_POSIX) |
| 434 | |
| 435 | void ParamTraits<IPC::ChannelHandle>::Write(Message* m, const param_type& p) { |
| 436 | WriteParam(m, p.name); |
| 437 | #if defined(OS_POSIX) |
| 438 | WriteParam(m, p.socket); |
| 439 | #endif |
| 440 | } |
| 441 | |
| 442 | bool ParamTraits<IPC::ChannelHandle>::Read(const Message* m, void** iter, |
| 443 | param_type* r) { |
| 444 | return ReadParam(m, iter, &r->name) |
| 445 | #if defined(OS_POSIX) |
| 446 | && ReadParam(m, iter, &r->socket) |
| 447 | #endif |
| 448 | ; |
| 449 | } |
| 450 | |
| 451 | void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p, |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 452 | std::string* l) { |
| 453 | l->append(StringPrintf("ChannelHandle(%s", p.name.c_str())); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 454 | #if defined(OS_POSIX) |
[email protected] | 3cd3bce | 2011-09-23 10:32:19 | [diff] [blame] | 455 | l->append(", "); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 456 | ParamTraits<base::FileDescriptor>::Log(p.socket, l); |
| 457 | #endif |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 458 | l->append(")"); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 459 | } |
| 460 | |
[email protected] | 5c622af | 2011-03-30 10:19:53 | [diff] [blame] | 461 | LogData::LogData() |
| 462 | : routing_id(0), |
| 463 | type(0), |
| 464 | sent(0), |
| 465 | receive(0), |
| 466 | dispatch(0) { |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | LogData::~LogData() { |
| 470 | } |
| 471 | |
| 472 | void ParamTraits<LogData>::Write(Message* m, const param_type& p) { |
| 473 | WriteParam(m, p.channel); |
| 474 | WriteParam(m, p.routing_id); |
[email protected] | 8bf55ca | 2011-10-17 22:15:27 | [diff] [blame] | 475 | WriteParam(m, p.type); |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 476 | WriteParam(m, p.flags); |
| 477 | WriteParam(m, p.sent); |
| 478 | WriteParam(m, p.receive); |
| 479 | WriteParam(m, p.dispatch); |
| 480 | WriteParam(m, p.params); |
| 481 | } |
| 482 | |
| 483 | bool ParamTraits<LogData>::Read(const Message* m, void** iter, param_type* r) { |
[email protected] | 8bf55ca | 2011-10-17 22:15:27 | [diff] [blame] | 484 | return |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 485 | ReadParam(m, iter, &r->channel) && |
| 486 | ReadParam(m, iter, &r->routing_id) && |
[email protected] | 8bf55ca | 2011-10-17 22:15:27 | [diff] [blame] | 487 | ReadParam(m, iter, &r->type) && |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 488 | ReadParam(m, iter, &r->flags) && |
| 489 | ReadParam(m, iter, &r->sent) && |
| 490 | ReadParam(m, iter, &r->receive) && |
| 491 | ReadParam(m, iter, &r->dispatch) && |
| 492 | ReadParam(m, iter, &r->params); |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 493 | } |
| 494 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 495 | } // namespace IPC |