OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_ARRAY_SERIALIZATION_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <string.h> // For |memcpy()|. | 9 #include <string.h> // For |memcpy()|. |
10 | 10 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 memcpy(output->storage(), data, size * sizeof(DataElement)); | 130 memcpy(output->storage(), data, size * sizeof(DataElement)); |
131 } else { | 131 } else { |
132 for (size_t i = 0; i < size; ++i) | 132 for (size_t i = 0; i < size; ++i) |
133 output->at(i) = static_cast<DataElement>(input->GetNext()); | 133 output->at(i) = static_cast<DataElement>(input->GetNext()); |
134 } | 134 } |
135 } | 135 } |
136 | 136 |
137 static bool DeserializeElements(Data* input, | 137 static bool DeserializeElements(Data* input, |
138 UserType* output, | 138 UserType* output, |
139 SerializationContext* context) { | 139 SerializationContext* context) { |
140 Traits::Resize(*output, input->size()); | 140 if (!Traits::Resize(*output, input->size())) |
| 141 return false; |
141 if (input->size()) { | 142 if (input->size()) { |
142 auto data = CallGetDataIfExists<Traits>(*output); | 143 auto data = CallGetDataIfExists<Traits>(*output); |
143 if (data) { | 144 if (data) { |
144 memcpy(data, input->storage(), input->size() * sizeof(DataElement)); | 145 memcpy(data, input->storage(), input->size() * sizeof(DataElement)); |
145 } else { | 146 } else { |
146 for (size_t i = 0; i < input->size(); ++i) | 147 for (size_t i = 0; i < input->size(); ++i) |
147 Traits::GetAt(*output, i) = static_cast<Element>(input->at(i)); | 148 Traits::GetAt(*output, i) = static_cast<Element>(input->at(i)); |
148 } | 149 } |
149 } | 150 } |
150 return true; | 151 return true; |
(...skipping 30 matching lines...) Expand all Loading... |
181 DCHECK(!validate_params->element_validate_params) | 182 DCHECK(!validate_params->element_validate_params) |
182 << "Primitive type should not have array validate params"; | 183 << "Primitive type should not have array validate params"; |
183 | 184 |
184 size_t size = input->GetSize(); | 185 size_t size = input->GetSize(); |
185 for (size_t i = 0; i < size; ++i) | 186 for (size_t i = 0; i < size; ++i) |
186 output->at(i) = input->GetNext(); | 187 output->at(i) = input->GetNext(); |
187 } | 188 } |
188 static bool DeserializeElements(Data* input, | 189 static bool DeserializeElements(Data* input, |
189 UserType* output, | 190 UserType* output, |
190 SerializationContext* context) { | 191 SerializationContext* context) { |
191 Traits::Resize(*output, input->size()); | 192 if (!Traits::Resize(*output, input->size())) |
| 193 return false; |
192 for (size_t i = 0; i < input->size(); ++i) | 194 for (size_t i = 0; i < input->size(); ++i) |
193 Traits::GetAt(*output, i) = input->at(i); | 195 Traits::GetAt(*output, i) = input->at(i); |
194 return true; | 196 return true; |
195 } | 197 } |
196 }; | 198 }; |
197 | 199 |
198 // Serializes and deserializes arrays of handles. | 200 // Serializes and deserializes arrays of handles. |
199 template <typename MojomType, | 201 template <typename MojomType, |
200 typename MaybeConstUserType, | 202 typename MaybeConstUserType, |
201 typename UserTypeReader> | 203 typename UserTypeReader> |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 !validate_params->element_is_nullable && !output->at(i).is_valid(), | 235 !validate_params->element_is_nullable && !output->at(i).is_valid(), |
234 VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, | 236 VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, |
235 MakeMessageWithArrayIndex( | 237 MakeMessageWithArrayIndex( |
236 "invalid handle in array expecting valid handles", size, i)); | 238 "invalid handle in array expecting valid handles", size, i)); |
237 } | 239 } |
238 } | 240 } |
239 static bool DeserializeElements(Data* input, | 241 static bool DeserializeElements(Data* input, |
240 UserType* output, | 242 UserType* output, |
241 SerializationContext* context) { | 243 SerializationContext* context) { |
242 using HandleType = typename Element::RawHandleType; | 244 using HandleType = typename Element::RawHandleType; |
243 Traits::Resize(*output, input->size()); | 245 if (!Traits::Resize(*output, input->size())) |
| 246 return false; |
244 for (size_t i = 0; i < input->size(); ++i) { | 247 for (size_t i = 0; i < input->size(); ++i) { |
245 Traits::GetAt(*output, i) = MakeScopedHandle( | 248 Traits::GetAt(*output, i) = MakeScopedHandle( |
246 HandleType(context->handles.TakeHandle(input->at(i)).value())); | 249 HandleType(context->handles.TakeHandle(input->at(i)).value())); |
247 } | 250 } |
248 return true; | 251 return true; |
249 } | 252 } |
250 }; | 253 }; |
251 | 254 |
252 // This template must only apply to pointer mojo entity (strings, structs, | 255 // This template must only apply to pointer mojo entity (strings, structs, |
253 // arrays and maps). | 256 // arrays and maps). |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 !validate_params->element_is_nullable && !element, | 295 !validate_params->element_is_nullable && !element, |
293 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 296 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
294 MakeMessageWithArrayIndex("null in array expecting valid pointers", | 297 MakeMessageWithArrayIndex("null in array expecting valid pointers", |
295 size, i)); | 298 size, i)); |
296 } | 299 } |
297 } | 300 } |
298 static bool DeserializeElements(Data* input, | 301 static bool DeserializeElements(Data* input, |
299 UserType* output, | 302 UserType* output, |
300 SerializationContext* context) { | 303 SerializationContext* context) { |
301 bool success = true; | 304 bool success = true; |
302 Traits::Resize(*output, input->size()); | 305 if (!Traits::Resize(*output, input->size())) |
| 306 return false; |
303 for (size_t i = 0; i < input->size(); ++i) { | 307 for (size_t i = 0; i < input->size(); ++i) { |
304 // Note that we rely on complete deserialization taking place in order to | 308 // Note that we rely on complete deserialization taking place in order to |
305 // transfer ownership of all encoded handles. Therefore we don't | 309 // transfer ownership of all encoded handles. Therefore we don't |
306 // short-circuit on failure here. | 310 // short-circuit on failure here. |
307 if (!Deserialize<Element>(input->at(i), &Traits::GetAt(*output, i), | 311 if (!Deserialize<Element>(input->at(i), &Traits::GetAt(*output, i), |
308 context)) { | 312 context)) { |
309 success = false; | 313 success = false; |
310 } | 314 } |
311 } | 315 } |
312 return success; | 316 return success; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 388 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
385 MakeMessageWithArrayIndex("null in array expecting valid unions", | 389 MakeMessageWithArrayIndex("null in array expecting valid unions", |
386 size, i)); | 390 size, i)); |
387 } | 391 } |
388 } | 392 } |
389 | 393 |
390 static bool DeserializeElements(Data* input, | 394 static bool DeserializeElements(Data* input, |
391 UserType* output, | 395 UserType* output, |
392 SerializationContext* context) { | 396 SerializationContext* context) { |
393 bool success = true; | 397 bool success = true; |
394 Traits::Resize(*output, input->size()); | 398 if (!Traits::Resize(*output, input->size())) |
| 399 return false; |
395 for (size_t i = 0; i < input->size(); ++i) { | 400 for (size_t i = 0; i < input->size(); ++i) { |
396 // Note that we rely on complete deserialization taking place in order to | 401 // Note that we rely on complete deserialization taking place in order to |
397 // transfer ownership of all encoded handles. Therefore we don't | 402 // transfer ownership of all encoded handles. Therefore we don't |
398 // short-circuit on failure here. | 403 // short-circuit on failure here. |
399 if (!Deserialize<Element>(&input->at(i), &Traits::GetAt(*output, i), | 404 if (!Deserialize<Element>(&input->at(i), &Traits::GetAt(*output, i), |
400 context)) { | 405 context)) { |
401 success = false; | 406 success = false; |
402 } | 407 } |
403 } | 408 } |
404 return success; | 409 return success; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 if (!input) | 457 if (!input) |
453 return CallSetToNullIfExists<Traits>(output); | 458 return CallSetToNullIfExists<Traits>(output); |
454 return Impl::DeserializeElements(input, output, context); | 459 return Impl::DeserializeElements(input, output, context); |
455 } | 460 } |
456 }; | 461 }; |
457 | 462 |
458 } // namespace internal | 463 } // namespace internal |
459 } // namespace mojo | 464 } // namespace mojo |
460 | 465 |
461 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 466 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
OLD | NEW |