OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_ | |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <limits> | |
11 | |
12 #include "base/logging.h" | |
13 #include "base/pickle.h" | |
14 #include "ipc/ipc_param_traits.h" | |
15 #include "mojo/public/cpp/bindings/lib/array_internal.h" | |
16 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | |
17 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" | |
18 #include "mojo/public/cpp/bindings/lib/pickle_buffer.h" | |
19 | |
20 namespace mojo { | |
21 namespace internal { | |
22 | |
23 template <typename T> | |
24 size_t GetSerializedSizeNative_(const T& value) { | |
25 return IPC::ParamTraits<T>::GetSize(value); | |
26 } | |
27 | |
28 template <typename T> | |
29 void SerializeNative_(const T& value, | |
30 Buffer* buffer, | |
31 Array_Data<uint8_t>** out) { | |
32 PickleBuffer* pickler = buffer->AsPickleBuffer(); | |
33 DCHECK(pickler) << "Native types can only be used with PickleBuffers."; | |
34 | |
35 ArrayHeader* header = | |
36 reinterpret_cast<ArrayHeader*>(buffer->Allocate(sizeof(ArrayHeader))); | |
37 header->num_bytes = sizeof(ArrayHeader); | |
yzshen1
2015/12/15 21:20:26
header->num_bytes should be the total size of the
Ken Rockot(use gerrit already)
2015/12/15 23:31:07
Oops, I was confused by this and at some point con
| |
38 | |
39 // Remember where the Pickle started before writing. | |
40 const char* data_start = pickler->end_of_payload(); | |
41 | |
42 IPC::ParamTraits<T>::Write(pickler, value); | |
43 | |
44 // Fix up the ArrayHeader so that num_elements contains the length of the | |
45 // pickled data. | |
46 size_t pickled_size = pickler->end_of_payload() - data_start; | |
47 DCHECK_LT(pickled_size, std::numeric_limits<uint32_t>::max()); | |
48 header->num_elements = static_cast<uint32_t>(pickled_size); | |
49 | |
50 *out = reinterpret_cast<Array_Data<uint8_t>*>(header); | |
51 } | |
52 | |
53 template <typename T> | |
54 bool DeserializeNative_(Array_Data<uint8_t>* data, | |
yzshen1
2015/12/15 21:20:26
Please handle the case |data| is nullptr, in that
Ken Rockot(use gerrit already)
2015/12/15 23:31:07
Done, sort of. I'd rather not force native types t
yzshen1
2015/12/15 23:55:29
Okay. The other deserialization routines don't mak
| |
55 T* out, | |
56 SerializationContext* context) { | |
57 // Construct a temporary base::Pickle view over the array data. Note that | |
58 // the Array_Data is laid out like this: | |
59 // | |
60 // [header_size (4 bytes)] [element_count (4 bytes)] [elements...] | |
61 // | |
62 // and base::Pickle expects to view data like this: | |
63 // | |
64 // [payload_size (4 bytes)] [header bytes ...] [payload...] | |
65 // | |
66 // Conveniently we can get Pickle to do what we want by hacking up the first | |
67 // 4 bytes so the element count is ignored as part of the header, and the | |
68 // Pickle payload consists of only the ParamTraits-serialized bytes. | |
69 ArrayHeader* header = reinterpret_cast<ArrayHeader*>(data); | |
70 DCHECK_EQ(header->num_bytes, 8u); | |
yzshen1
2015/12/15 21:20:26
Please see my comment on line 37.
Ken Rockot(use gerrit already)
2015/12/15 23:31:07
Fixed
| |
71 | |
72 // Write the number of elements to the Pickle header location. It will treat | |
73 // this as the bytes size. | |
74 header->num_bytes = header->num_elements; | |
75 | |
76 { | |
77 // Construct a view over the full Array_Data, including our hacked up | |
78 // header. Pickle will infer from this that the header is 8 bytes long, | |
79 // and the payload will contain all of the pickled bytes. | |
80 base::Pickle pickle_view(reinterpret_cast<const char*>(header), | |
81 header->num_elements + 8); | |
yzshen1
2015/12/15 21:20:26
Please use sizeof(ArrayHeader) instead of 8.
Ken Rockot(use gerrit already)
2015/12/15 23:31:07
Done
| |
82 base::PickleIterator iter(pickle_view); | |
83 if (!IPC::ParamTraits<T>::Read(&pickle_view, &iter, out)) { | |
84 return false; | |
85 } | |
86 } | |
87 | |
88 // Just to be nice, we return the message buffer to its original state. | |
89 header->num_bytes = sizeof(ArrayHeader); | |
90 return true; | |
91 } | |
92 | |
93 } // namespace internal | |
94 } // namespace mojo | |
95 | |
96 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_ | |
OLD | NEW |