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

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

Issue 1518293002: [mojo] Add Mojo bindings support for IPC::ParamTraits (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@pickle4
Patch Set: 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
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 void SerializeNative_(const T& value,
25 Buffer* buffer,
26 Array_Data<uint8_t>** out) {
27 PickleBuffer* pickler = buffer->AsPickleBuffer();
28 DCHECK(pickler) << "Native types can only be used with PickleBuffers.";
29
30 ArrayHeader* header =
31 reinterpret_cast<ArrayHeader*>(buffer->Allocate(sizeof(ArrayHeader)));
32 header->num_bytes = sizeof(ArrayHeader);
33
34 // Remember where the Pickle started before writing.
35 const char* data_start = pickler->end_of_payload();
36
37 IPC::ParamTraits<T>::Write(pickler, value);
38
39 // Fix up the ArrayHeader so that num_elements contains the length of the
40 // pickled data.
41 size_t pickled_size = pickler->end_of_payload() - data_start;
42 DCHECK_LT(pickled_size, std::numeric_limits<uint32_t>::max());
43 header->num_elements = static_cast<uint32_t>(pickled_size);
44
45 *out = reinterpret_cast<Array_Data<uint8_t>*>(header);
46 }
47
48 template <typename T>
49 bool DeserializeNative_(Array_Data<uint8_t>* data,
50 T* out,
51 SerializationContext* context) {
52 // Construct a temporary base::Pickle view over the array data. Note that
53 // the Array_Data is laid out like this:
54 //
55 // [header_size (4 bytes)] [element_count (4 bytes)] [elements...]
56 //
57 // and base::Pickle expects to view data like this:
58 //
59 // [payload_size (4 bytes)] [header bytes ...] [payload...]
60 //
61 // Conveniently we can get Pickle to do what we want by hacking up the first
62 // 4 bytes so the element count is ignored as part of the header, and the
63 // Pickle payload consists of only the ParamTraits-serialized bytes.
64 ArrayHeader* header = reinterpret_cast<ArrayHeader*>(data);
65 DCHECK_EQ(header->num_bytes, 8u);
66
67 // Write the number of elements to the Pickle header location. It will treat
68 // this as the bytes size.
69 header->num_bytes = header->num_elements;
70
71 {
72 // Construct a view over the full Array_Data, including our hacked up
73 // header. Pickle will infer from this that the header is 8 bytes long,
74 // and the payload will contain all of the pickled bytes.
75 base::Pickle pickle_view(reinterpret_cast<const char*>(header),
76 header->num_elements + 8);
77 base::PickleIterator iter(pickle_view);
78 if (!IPC::ParamTraits<T>::Read(&pickle_view, &iter, out)) {
79 return false;
80 }
81 }
82
83 // Just to be nice, we return the message buffer to its original state.
84 header->num_bytes = sizeof(ArrayHeader);
85 return true;
86 }
87
88 } // namespace internal
89 } // namespace mojo
90
91 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/map_serialization.h ('k') | mojo/public/cpp/bindings/lib/pickle_buffer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698