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" | |
8 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
9 #include "base/nullable_string16.h" | |
10 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
11 #include "base/time.h" | 9 #include "base/time.h" |
12 #include "base/values.h" | 10 #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" | |
17 | 11 |
18 namespace IPC { | 12 namespace IPC { |
19 | 13 |
20 const int kMaxRecursionDepth = 100; | 14 const int kMaxRecursionDepth = 100; |
21 | 15 |
22 // Value serialization | 16 // Value serialization |
23 | 17 |
24 static bool ReadValue(const Message* m, void** iter, Value** value, | 18 static bool ReadValue(const Message* m, void** iter, Value** value, |
25 int recursion); | 19 int recursion); |
26 | 20 |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 *value = val.release(); | 194 *value = val.release(); |
201 break; | 195 break; |
202 } | 196 } |
203 default: | 197 default: |
204 return false; | 198 return false; |
205 } | 199 } |
206 | 200 |
207 return true; | 201 return true; |
208 } | 202 } |
209 | 203 |
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 | |
228 void ParamTraits<DictionaryValue>::Write(Message* m, const param_type& p) { | 204 void ParamTraits<DictionaryValue>::Write(Message* m, const param_type& p) { |
229 WriteValue(m, &p, 0); | 205 WriteValue(m, &p, 0); |
230 } | 206 } |
231 | 207 |
232 bool ParamTraits<DictionaryValue>::Read( | 208 bool ParamTraits<DictionaryValue>::Read( |
233 const Message* m, void** iter, param_type* r) { | 209 const Message* m, void** iter, param_type* r) { |
234 int type; | 210 int type; |
235 if (!ReadParam(m, iter, &type) || type != Value::TYPE_DICTIONARY) | 211 if (!ReadParam(m, iter, &type) || type != Value::TYPE_DICTIONARY) |
236 return false; | 212 return false; |
237 | 213 |
(...skipping 17 matching lines...) Expand all Loading... |
255 return false; | 231 return false; |
256 | 232 |
257 return ReadListValue(m, iter, r, 0); | 233 return ReadListValue(m, iter, r, 0); |
258 } | 234 } |
259 | 235 |
260 void ParamTraits<ListValue>::Log(const param_type& p, std::wstring* l) { | 236 void ParamTraits<ListValue>::Log(const param_type& p, std::wstring* l) { |
261 std::string json; | 237 std::string json; |
262 base::JSONWriter::Write(&p, false, &json); | 238 base::JSONWriter::Write(&p, false, &json); |
263 l->append(UTF8ToWide(json)); | 239 l->append(UTF8ToWide(json)); |
264 } | 240 } |
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 | |
368 } // namespace IPC | 241 } // namespace IPC |
OLD | NEW |