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

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

Issue 2076313002: Mojo C++ bindings: expose serialization public API for mojo structs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 months 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 | « no previous file | mojo/public/cpp/bindings/struct_ptr.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_
7 7
8 #include <string.h>
9
8 #include "mojo/public/cpp/bindings/array_traits_carray.h" 10 #include "mojo/public/cpp/bindings/array_traits_carray.h"
9 #include "mojo/public/cpp/bindings/array_traits_standard.h" 11 #include "mojo/public/cpp/bindings/array_traits_standard.h"
10 #include "mojo/public/cpp/bindings/array_traits_stl.h" 12 #include "mojo/public/cpp/bindings/array_traits_stl.h"
11 #include "mojo/public/cpp/bindings/lib/array_serialization.h" 13 #include "mojo/public/cpp/bindings/lib/array_serialization.h"
14 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
12 #include "mojo/public/cpp/bindings/lib/map_serialization.h" 15 #include "mojo/public/cpp/bindings/lib/map_serialization.h"
13 #include "mojo/public/cpp/bindings/lib/native_enum_serialization.h" 16 #include "mojo/public/cpp/bindings/lib/native_enum_serialization.h"
14 #include "mojo/public/cpp/bindings/lib/native_struct_serialization.h" 17 #include "mojo/public/cpp/bindings/lib/native_struct_serialization.h"
15 #include "mojo/public/cpp/bindings/lib/string_serialization.h" 18 #include "mojo/public/cpp/bindings/lib/string_serialization.h"
19 #include "mojo/public/cpp/bindings/lib/template_util.h"
16 #include "mojo/public/cpp/bindings/map_traits_standard.h" 20 #include "mojo/public/cpp/bindings/map_traits_standard.h"
17 #include "mojo/public/cpp/bindings/map_traits_stl.h" 21 #include "mojo/public/cpp/bindings/map_traits_stl.h"
18 #include "mojo/public/cpp/bindings/string_traits_standard.h" 22 #include "mojo/public/cpp/bindings/string_traits_standard.h"
19 #include "mojo/public/cpp/bindings/string_traits_stl.h" 23 #include "mojo/public/cpp/bindings/string_traits_stl.h"
20 #include "mojo/public/cpp/bindings/string_traits_string16.h" 24 #include "mojo/public/cpp/bindings/string_traits_string16.h"
21 #include "mojo/public/cpp/bindings/string_traits_string_piece.h" 25 #include "mojo/public/cpp/bindings/string_traits_string_piece.h"
22 26
27 namespace mojo {
28 namespace internal {
29
30 template <typename MojomType, typename DataArrayType, typename UserType>
31 DataArrayType StructSerializeImpl(UserType* input) {
32 static_assert(IsSpecializationOf<StructPtr, MojomType>::value ||
33 IsSpecializationOf<InlinedStructPtr, MojomType>::value,
34 "Unexpected type.");
35
36 SerializationContext context;
37 size_t size = PrepareToSerialize<MojomType>(*input, &context);
38 DCHECK_EQ(size, Align(size));
39
40 DataArrayType result(size);
41 if (size == 0)
42 return result;
43
44 void* result_buffer = &result.front();
45 // The serialization logic requires that the buffer is 8-byte aligned. If the
46 // result buffer is not properly aligned, we have to do an extra copy. In
47 // practice, this should never happen for mojo::Array (backed by std::vector).
48 bool need_copy = !IsAligned(result_buffer);
49
50 if (need_copy) {
51 // calloc sets the memory to all zero.
52 result_buffer = calloc(size, 1);
53 DCHECK(IsAligned(result_buffer));
54 }
55
56 FixedBuffer buffer;
57 buffer.Initialize(result_buffer, size);
58 typename MojomType::Struct::Data_* data = nullptr;
59 Serialize<MojomType>(*input, &buffer, &data, &context);
60
61 data->EncodePointers();
62
63 if (need_copy) {
64 memcpy(&result.front(), result_buffer, size);
65 free(result_buffer);
66 }
67
68 return result;
69 }
70
71 template <typename MojomType, typename DataArrayType, typename UserType>
72 bool StructDeserializeImpl(DataArrayType input, UserType* output) {
73 static_assert(IsSpecializationOf<StructPtr, MojomType>::value ||
74 IsSpecializationOf<InlinedStructPtr, MojomType>::value,
75 "Unexpected type.");
76 using DataType = typename MojomType::Struct::Data_;
77
78 if (input.is_null())
79 return false;
80
81 void* input_buffer = input.empty() ? nullptr : &input.front();
82
83 // Please see comments in StructSerializeImpl.
84 bool need_copy = !IsAligned(input_buffer);
85
86 if (need_copy) {
87 input_buffer = malloc(input.size());
88 DCHECK(IsAligned(input_buffer));
89 memcpy(input_buffer, &input.front(), input.size());
90 }
91
92 ValidationContext validation_context(input_buffer, input.size(), 0);
93 bool result = false;
94 if (DataType::Validate(input_buffer, &validation_context)) {
95 auto data = reinterpret_cast<DataType*>(input_buffer);
96 if (data)
97 data->DecodePointers();
98
99 SerializationContext context;
100 result = Deserialize<MojomType>(data, output, &context);
101 }
102
103 if (need_copy)
104 free(input_buffer);
105
106 return result;
107 }
108
109 } // namespace internal
110 } // namespace mojo
111
23 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_ 112 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_
OLDNEW
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/struct_ptr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698