| OLD | NEW |
| 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> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "mojo/public/cpp/bindings/array_traits_carray.h" | 10 #include "mojo/public/cpp/bindings/array_traits_carray.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 | 57 |
| 58 if (need_copy) { | 58 if (need_copy) { |
| 59 memcpy(&result.front(), result_buffer, size); | 59 memcpy(&result.front(), result_buffer, size); |
| 60 free(result_buffer); | 60 free(result_buffer); |
| 61 } | 61 } |
| 62 | 62 |
| 63 return result; | 63 return result; |
| 64 } | 64 } |
| 65 | 65 |
| 66 template <typename MojomType, typename DataArrayType, typename UserType> | 66 template <typename MojomType, typename DataArrayType, typename UserType> |
| 67 bool StructDeserializeImpl(const DataArrayType& input, UserType* output) { | 67 bool StructDeserializeImpl(const DataArrayType& input, |
| 68 UserType* output, |
| 69 bool (*validate_func)(const void*, |
| 70 ValidationContext*)) { |
| 68 static_assert(BelongsTo<MojomType, MojomTypeCategory::STRUCT>::value, | 71 static_assert(BelongsTo<MojomType, MojomTypeCategory::STRUCT>::value, |
| 69 "Unexpected type."); | 72 "Unexpected type."); |
| 70 using DataType = typename MojomTypeTraits<MojomType>::Data; | 73 using DataType = typename MojomTypeTraits<MojomType>::Data; |
| 71 | 74 |
| 72 // TODO(sammc): Use DataArrayType::empty() once WTF::Vector::empty() exists. | 75 // TODO(sammc): Use DataArrayType::empty() once WTF::Vector::empty() exists. |
| 73 void* input_buffer = | 76 void* input_buffer = |
| 74 input.size() == 0 | 77 input.size() == 0 |
| 75 ? nullptr | 78 ? nullptr |
| 76 : const_cast<void*>(reinterpret_cast<const void*>(&input.front())); | 79 : const_cast<void*>(reinterpret_cast<const void*>(&input.front())); |
| 77 | 80 |
| 78 // Please see comments in StructSerializeImpl. | 81 // Please see comments in StructSerializeImpl. |
| 79 bool need_copy = !IsAligned(input_buffer); | 82 bool need_copy = !IsAligned(input_buffer); |
| 80 | 83 |
| 81 if (need_copy) { | 84 if (need_copy) { |
| 82 input_buffer = malloc(input.size()); | 85 input_buffer = malloc(input.size()); |
| 83 DCHECK(IsAligned(input_buffer)); | 86 DCHECK(IsAligned(input_buffer)); |
| 84 memcpy(input_buffer, &input.front(), input.size()); | 87 memcpy(input_buffer, &input.front(), input.size()); |
| 85 } | 88 } |
| 86 | 89 |
| 87 ValidationContext validation_context(input_buffer, input.size(), 0, 0); | 90 ValidationContext validation_context(input_buffer, input.size(), 0, 0); |
| 88 bool result = false; | 91 bool result = false; |
| 89 if (DataType::Validate(input_buffer, &validation_context)) { | 92 if (validate_func(input_buffer, &validation_context)) { |
| 90 auto data = reinterpret_cast<DataType*>(input_buffer); | 93 auto data = reinterpret_cast<DataType*>(input_buffer); |
| 91 SerializationContext context; | 94 SerializationContext context; |
| 92 result = Deserialize<MojomType>(data, output, &context); | 95 result = Deserialize<MojomType>(data, output, &context); |
| 93 } | 96 } |
| 94 | 97 |
| 95 if (need_copy) | 98 if (need_copy) |
| 96 free(input_buffer); | 99 free(input_buffer); |
| 97 | 100 |
| 98 return result; | 101 return result; |
| 99 } | 102 } |
| 100 | 103 |
| 101 } // namespace internal | 104 } // namespace internal |
| 102 } // namespace mojo | 105 } // namespace mojo |
| 103 | 106 |
| 104 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_ | 107 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_ |
| OLD | NEW |