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

Side by Side Diff: mojo/public/bindings/lib/bindings_serialization.h

Issue 131033002: Mojo: Simplify object serialization (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix indentation error Created 6 years, 11 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_ 5 #ifndef MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_
6 #define MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_ 6 #define MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_
7 7
8 #include <string.h>
9
10 #include <vector> 8 #include <vector>
11 9
12 #include "mojo/public/bindings/lib/bindings.h" 10 #include "mojo/public/bindings/lib/buffer.h"
13 #include "mojo/public/bindings/lib/message.h" 11 #include "mojo/public/bindings/lib/message.h"
14 12
15 namespace mojo { 13 namespace mojo {
16 namespace internal { 14 namespace internal {
17 15
18 size_t Align(size_t size); 16 size_t Align(size_t size);
19 17
20 // Pointers are encoded as relative offsets. The offsets are relative to the 18 // Pointers are encoded as relative offsets. The offsets are relative to the
21 // address of where the offset value is stored, such that the pointer may be 19 // address of where the offset value is stored, such that the pointer may be
22 // recovered with the expression: 20 // recovered with the expression:
(...skipping 11 matching lines...) Expand all
34 } 32 }
35 33
36 // Check that the given pointer references memory contained within the message. 34 // Check that the given pointer references memory contained within the message.
37 bool ValidatePointer(const void* ptr, const Message& message); 35 bool ValidatePointer(const void* ptr, const Message& message);
38 36
39 // Handles are encoded as indices into a vector of handles. These functions 37 // Handles are encoded as indices into a vector of handles. These functions
40 // manipulate the value of |handle|, mapping it to and from an index. 38 // manipulate the value of |handle|, mapping it to and from an index.
41 void EncodeHandle(Handle* handle, std::vector<Handle>* handles); 39 void EncodeHandle(Handle* handle, std::vector<Handle>* handles);
42 bool DecodeHandle(Handle* handle, std::vector<Handle>* handles); 40 bool DecodeHandle(Handle* handle, std::vector<Handle>* handles);
43 41
44 // All objects (structs and arrays) support the following operations:
45 // - computing size
46 // - cloning
47 // - encoding pointers and handles
48 // - decoding pointers and handles
49 //
50 // The following functions are used to select the proper ObjectTraits<>
51 // specialization.
52
53 template <typename T>
54 inline size_t ComputeSizeOf(const T* obj) {
55 return obj ? ObjectTraits<T>::ComputeSizeOf(obj) : 0;
56 }
57
58 template <typename T>
59 inline T* Clone(const T* obj, Buffer* buf) {
60 return obj ? ObjectTraits<T>::Clone(obj, buf) : NULL;
61 }
62
63 template <typename T>
64 inline void CloseHandles(T* obj) {
65 if (obj)
66 ObjectTraits<T>::CloseHandles(obj);
67 }
68
69 template <typename T>
70 inline void EncodePointersAndHandles(T* obj,
71 std::vector<Handle>* handles) {
72 ObjectTraits<T>::EncodePointersAndHandles(obj, handles);
73 }
74
75 template <typename T>
76 inline bool DecodePointersAndHandles(T* obj, Message* message) {
77 return ObjectTraits<T>::DecodePointersAndHandles(obj, message);
78 }
79
80 // The following 2 functions are used to encode/decode all objects (structs and 42 // The following 2 functions are used to encode/decode all objects (structs and
81 // arrays) in a consistent manner. 43 // arrays) in a consistent manner.
82 44
83 template <typename T> 45 template <typename T>
84 inline void Encode(T* obj, std::vector<Handle>* handles) { 46 inline void Encode(T* obj, std::vector<Handle>* handles) {
85 if (obj->ptr) 47 if (obj->ptr)
86 EncodePointersAndHandles(obj->ptr, handles); 48 obj->ptr->EncodePointersAndHandles(handles);
87 EncodePointer(obj->ptr, &obj->offset); 49 EncodePointer(obj->ptr, &obj->offset);
88 } 50 }
89 51
90 template <typename T> 52 template <typename T>
91 inline bool Decode(T* obj, Message* message) { 53 inline bool Decode(T* obj, Message* message) {
92 DecodePointer(&obj->offset, &obj->ptr); 54 DecodePointer(&obj->offset, &obj->ptr);
93 if (obj->ptr) { 55 if (obj->ptr) {
94 if (!ValidatePointer(obj->ptr, *message)) 56 if (!ValidatePointer(obj->ptr, *message))
95 return false; 57 return false;
96 if (!DecodePointersAndHandles(obj->ptr, message)) 58 if (!obj->ptr->DecodePointersAndHandles(message))
97 return false; 59 return false;
98 } 60 }
99 return true; 61 return true;
100 } 62 }
101 63
102 // What follows is code to support the ObjectTraits<> specialization of
103 // Array_Data<T>. There are two interesting cases: arrays of primitives and
104 // arrays of objects. Arrays of objects are represented as arrays of pointers
105 // to objects.
106
107 template <typename T>
108 struct ArrayHelper {
109 typedef T ElementType;
110
111 static size_t ComputeSizeOfElements(const ArrayHeader* header,
112 const ElementType* elements) {
113 return 0;
114 }
115
116 static void CloneElements(const ArrayHeader* header,
117 ElementType* elements,
118 Buffer* buf) {
119 }
120
121 static void EncodePointersAndHandles(const ArrayHeader* header,
122 ElementType* elements,
123 std::vector<Handle>* handles) {
124 }
125 static bool DecodePointersAndHandles(const ArrayHeader* header,
126 ElementType* elements,
127 Message* message) {
128 return true;
129 }
130 };
131
132 template <>
133 struct ArrayHelper<Handle> {
134 typedef Handle ElementType;
135
136 static size_t ComputeSizeOfElements(const ArrayHeader* header,
137 const ElementType* elements) {
138 return 0;
139 }
140
141 static void CloneElements(const ArrayHeader* header,
142 ElementType* elements,
143 Buffer* buf) {
144 }
145
146 static void EncodePointersAndHandles(const ArrayHeader* header,
147 ElementType* elements,
148 std::vector<Handle>* handles);
149 static bool DecodePointersAndHandles(const ArrayHeader* header,
150 ElementType* elements,
151 Message* message);
152 };
153
154 template <typename P>
155 struct ArrayHelper<P*> {
156 typedef StructPointer<P> ElementType;
157
158 static size_t ComputeSizeOfElements(const ArrayHeader* header,
159 const ElementType* elements) {
160 size_t result = 0;
161 for (uint32_t i = 0; i < header->num_elements; ++i)
162 result += ComputeSizeOf(elements[i].ptr);
163 return result;
164 }
165
166 static void CloneElements(const ArrayHeader* header,
167 ElementType* elements,
168 Buffer* buf) {
169 for (uint32_t i = 0; i < header->num_elements; ++i)
170 elements[i].ptr = Clone(elements[i].ptr, buf);
171 }
172
173 static void EncodePointersAndHandles(const ArrayHeader* header,
174 ElementType* elements,
175 std::vector<Handle>* handles) {
176 for (uint32_t i = 0; i < header->num_elements; ++i)
177 Encode(&elements[i], handles);
178 }
179 static bool DecodePointersAndHandles(const ArrayHeader* header,
180 ElementType* elements,
181 Message* message) {
182 for (uint32_t i = 0; i < header->num_elements; ++i) {
183 if (!Decode(&elements[i], message))
184 return false;
185 }
186 return true;
187 }
188 };
189
190 template <typename T>
191 class ObjectTraits<Array_Data<T> > {
192 public:
193 static size_t ComputeSizeOf(const Array_Data<T>* array) {
194 return Align(array->header_.num_bytes) +
195 ArrayHelper<T>::ComputeSizeOfElements(&array->header_,
196 array->storage());
197 }
198
199 static Array_Data<T>* Clone(const Array_Data<T>* array, Buffer* buf) {
200 Array_Data<T>* clone = Array_Data<T>::New(array->header_.num_elements, buf);
201 memcpy(clone->storage(),
202 array->storage(),
203 array->header_.num_bytes - sizeof(Array_Data<T>));
204
205 ArrayHelper<T>::CloneElements(&clone->header_, clone->storage(), buf);
206 return clone;
207 }
208
209 static void CloseHandles(Array_Data<T>* array) {
210 // TODO(darin): Implement!
211 }
212
213 static void EncodePointersAndHandles(Array_Data<T>* array,
214 std::vector<Handle>* handles) {
215 ArrayHelper<T>::EncodePointersAndHandles(&array->header_, array->storage(),
216 handles);
217 }
218
219 static bool DecodePointersAndHandles(Array_Data<T>* array,
220 Message* message) {
221 return ArrayHelper<T>::DecodePointersAndHandles(&array->header_,
222 array->storage(),
223 message);
224 }
225 };
226
227 } // namespace internal 64 } // namespace internal
228 } // namespace mojo 65 } // namespace mojo
229 66
230 #endif // MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_ 67 #endif // MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_
OLDNEW
« no previous file with comments | « mojo/public/bindings/lib/array_internal.cc ('k') | mojo/public/bindings/lib/bindings_serialization.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698