OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/nullable_string16.h" |
8 #include "base/scoped_ptr.h" | 10 #include "base/scoped_ptr.h" |
9 #include "base/time.h" | 11 #include "base/time.h" |
10 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #if defined(OS_POSIX) |
| 14 #include "ipc/file_descriptor_set_posix.h" |
| 15 #endif |
| 16 #include "ipc/ipc_channel_handle.h" |
11 | 17 |
12 namespace IPC { | 18 namespace IPC { |
13 | 19 |
14 const int kMaxRecursionDepth = 100; | 20 const int kMaxRecursionDepth = 100; |
15 | 21 |
16 // Value serialization | 22 // Value serialization |
17 | 23 |
18 static bool ReadValue(const Message* m, void** iter, Value** value, | 24 static bool ReadValue(const Message* m, void** iter, Value** value, |
19 int recursion); | 25 int recursion); |
20 | 26 |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 *value = val.release(); | 200 *value = val.release(); |
195 break; | 201 break; |
196 } | 202 } |
197 default: | 203 default: |
198 return false; | 204 return false; |
199 } | 205 } |
200 | 206 |
201 return true; | 207 return true; |
202 } | 208 } |
203 | 209 |
| 210 |
| 211 void ParamTraits<base::Time>::Write(Message* m, const param_type& p) { |
| 212 ParamTraits<int64>::Write(m, p.ToInternalValue()); |
| 213 } |
| 214 |
| 215 bool ParamTraits<base::Time>::Read(const Message* m, void** iter, |
| 216 param_type* r) { |
| 217 int64 value; |
| 218 if (!ParamTraits<int64>::Read(m, iter, &value)) |
| 219 return false; |
| 220 *r = base::Time::FromInternalValue(value); |
| 221 return true; |
| 222 } |
| 223 |
| 224 void ParamTraits<base::Time>::Log(const param_type& p, std::wstring* l) { |
| 225 ParamTraits<int64>::Log(p.ToInternalValue(), l); |
| 226 } |
| 227 |
204 void ParamTraits<DictionaryValue>::Write(Message* m, const param_type& p) { | 228 void ParamTraits<DictionaryValue>::Write(Message* m, const param_type& p) { |
205 WriteValue(m, &p, 0); | 229 WriteValue(m, &p, 0); |
206 } | 230 } |
207 | 231 |
208 bool ParamTraits<DictionaryValue>::Read( | 232 bool ParamTraits<DictionaryValue>::Read( |
209 const Message* m, void** iter, param_type* r) { | 233 const Message* m, void** iter, param_type* r) { |
210 int type; | 234 int type; |
211 if (!ReadParam(m, iter, &type) || type != Value::TYPE_DICTIONARY) | 235 if (!ReadParam(m, iter, &type) || type != Value::TYPE_DICTIONARY) |
212 return false; | 236 return false; |
213 | 237 |
(...skipping 17 matching lines...) Expand all Loading... |
231 return false; | 255 return false; |
232 | 256 |
233 return ReadListValue(m, iter, r, 0); | 257 return ReadListValue(m, iter, r, 0); |
234 } | 258 } |
235 | 259 |
236 void ParamTraits<ListValue>::Log(const param_type& p, std::wstring* l) { | 260 void ParamTraits<ListValue>::Log(const param_type& p, std::wstring* l) { |
237 std::string json; | 261 std::string json; |
238 base::JSONWriter::Write(&p, false, &json); | 262 base::JSONWriter::Write(&p, false, &json); |
239 l->append(UTF8ToWide(json)); | 263 l->append(UTF8ToWide(json)); |
240 } | 264 } |
| 265 |
| 266 void ParamTraits<NullableString16>::Write(Message* m, const param_type& p) { |
| 267 WriteParam(m, p.string()); |
| 268 WriteParam(m, p.is_null()); |
| 269 } |
| 270 |
| 271 bool ParamTraits<NullableString16>::Read(const Message* m, void** iter, |
| 272 param_type* r) { |
| 273 string16 string; |
| 274 if (!ReadParam(m, iter, &string)) |
| 275 return false; |
| 276 bool is_null; |
| 277 if (!ReadParam(m, iter, &is_null)) |
| 278 return false; |
| 279 *r = NullableString16(string, is_null); |
| 280 return true; |
| 281 } |
| 282 |
| 283 void ParamTraits<NullableString16>::Log(const param_type& p, std::wstring* l) { |
| 284 l->append(L"("); |
| 285 LogParam(p.string(), l); |
| 286 l->append(L", "); |
| 287 LogParam(p.is_null(), l); |
| 288 l->append(L")"); |
| 289 } |
| 290 |
| 291 void ParamTraits<FilePath>::Write(Message* m, const param_type& p) { |
| 292 ParamTraits<FilePath::StringType>::Write(m, p.value()); |
| 293 } |
| 294 |
| 295 bool ParamTraits<FilePath>::Read(const Message* m, void** iter, param_type* r) { |
| 296 FilePath::StringType value; |
| 297 if (!ParamTraits<FilePath::StringType>::Read(m, iter, &value)) |
| 298 return false; |
| 299 *r = FilePath(value); |
| 300 return true; |
| 301 } |
| 302 |
| 303 void ParamTraits<FilePath>::Log(const param_type& p, std::wstring* l) { |
| 304 ParamTraits<FilePath::StringType>::Log(p.value(), l); |
| 305 } |
| 306 |
| 307 #if defined(OS_POSIX) |
| 308 void ParamTraits<base::FileDescriptor>::Write(Message* m, const param_type& p) { |
| 309 const bool valid = p.fd >= 0; |
| 310 WriteParam(m, valid); |
| 311 |
| 312 if (valid) { |
| 313 if (!m->WriteFileDescriptor(p)) |
| 314 NOTREACHED(); |
| 315 } |
| 316 } |
| 317 |
| 318 bool ParamTraits<base::FileDescriptor>::Read(const Message* m, void** iter, |
| 319 param_type* r) { |
| 320 bool valid; |
| 321 if (!ReadParam(m, iter, &valid)) |
| 322 return false; |
| 323 |
| 324 if (!valid) { |
| 325 r->fd = -1; |
| 326 r->auto_close = false; |
| 327 return true; |
| 328 } |
| 329 |
| 330 return m->ReadFileDescriptor(iter, r); |
| 331 } |
| 332 |
| 333 void ParamTraits<base::FileDescriptor>::Log(const param_type& p, |
| 334 std::wstring* l) { |
| 335 if (p.auto_close) { |
| 336 l->append(StringPrintf(L"FD(%d auto-close)", p.fd)); |
| 337 } else { |
| 338 l->append(StringPrintf(L"FD(%d)", p.fd)); |
| 339 } |
| 340 } |
| 341 #endif // defined(OS_POSIX) |
| 342 |
| 343 void ParamTraits<IPC::ChannelHandle>::Write(Message* m, const param_type& p) { |
| 344 WriteParam(m, p.name); |
| 345 #if defined(OS_POSIX) |
| 346 WriteParam(m, p.socket); |
| 347 #endif |
| 348 } |
| 349 |
| 350 bool ParamTraits<IPC::ChannelHandle>::Read(const Message* m, void** iter, |
| 351 param_type* r) { |
| 352 return ReadParam(m, iter, &r->name) |
| 353 #if defined(OS_POSIX) |
| 354 && ReadParam(m, iter, &r->socket) |
| 355 #endif |
| 356 ; |
| 357 } |
| 358 |
| 359 void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p, |
| 360 std::wstring* l) { |
| 361 l->append(ASCIIToWide(StringPrintf("ChannelHandle(%s", p.name.c_str()))); |
| 362 #if defined(OS_POSIX) |
| 363 ParamTraits<base::FileDescriptor>::Log(p.socket, l); |
| 364 #endif |
| 365 l->append(L")"); |
| 366 } |
| 367 |
241 } // namespace IPC | 368 } // namespace IPC |
OLD | NEW |