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

Side by Side Diff: mojo/public/cpp/bindings/lib/native_serialization.h

Issue 1526533002: [mojo] Add pickling support for native-only structs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bindings-4-bool-deserialize
Patch Set: merge Created 5 years 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
« no previous file with comments | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/tests/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
38 // Remember where the Pickle started before writing.
39 base::Pickle* pickle = pickler->pickle();
40 const char* data_start = pickle->end_of_payload();
41
42 IPC::ParamTraits<T>::Write(pickle, value);
43
44 // Fix up the ArrayHeader so that num_elements contains the length of the
45 // pickled data.
46 size_t pickled_size = pickle->end_of_payload() - data_start;
47 size_t total_size = pickled_size + sizeof(ArrayHeader);
48 DCHECK_LT(total_size, std::numeric_limits<uint32_t>::max());
49 header->num_bytes = static_cast<uint32_t>(total_size);
50 header->num_elements = static_cast<uint32_t>(pickled_size);
51
52 *out = reinterpret_cast<Array_Data<uint8_t>*>(header);
53 }
54
55 template <typename T>
56 bool DeserializeNative_(Array_Data<uint8_t>* data,
57 T* out,
58 SerializationContext* context) {
59 if (!data)
60 return true;
61
62 // Construct a temporary base::Pickle view over the array data. Note that
63 // the Array_Data is laid out like this:
64 //
65 // [num_bytes (4 bytes)] [num_elements (4 bytes)] [elements...]
66 //
67 // and base::Pickle expects to view data like this:
68 //
69 // [payload_size (4 bytes)] [header bytes ...] [payload...]
70 //
71 // Because ArrayHeader's num_bytes includes the length of the header and
72 // Pickle's payload_size does not, we need to adjust the stored value
73 // momentarily so Pickle can view the data.
74 ArrayHeader* header = reinterpret_cast<ArrayHeader*>(data);
75 DCHECK_GE(header->num_bytes, sizeof(ArrayHeader));
76 header->num_bytes -= sizeof(ArrayHeader);
77
78 {
79 // Construct a view over the full Array_Data, including our hacked up
80 // header. Pickle will infer from this that the header is 8 bytes long,
81 // and the payload will contain all of the pickled bytes.
82 base::Pickle pickle_view(reinterpret_cast<const char*>(header),
83 header->num_bytes + sizeof(ArrayHeader));
84 base::PickleIterator iter(pickle_view);
85 if (!IPC::ParamTraits<T>::Read(&pickle_view, &iter, out))
86 return false;
87 }
88
89 // Return the header to its original state.
90 header->num_bytes += sizeof(ArrayHeader);
91
92 return true;
93 }
94
95 } // namespace internal
96 } // namespace mojo
97
98 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/tests/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698