| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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_NATIVE_STRUCT_SERIALIZATION_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_STRUCT_SERIALIZATION_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_STRUCT_SERIALIZATION_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_STRUCT_SERIALIZATION_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <limits> | 11 #include <limits> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/pickle.h" | 14 #include "base/pickle.h" |
| 15 #include "ipc/ipc_param_traits.h" | 15 #include "ipc/ipc_param_traits.h" |
| 16 #include "mojo/public/cpp/bindings/lib/array_internal.h" | 16 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
| 17 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | 17 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
| 18 #include "mojo/public/cpp/bindings/lib/native_struct_data.h" | 18 #include "mojo/public/cpp/bindings/lib/native_struct_data.h" |
| 19 #include "mojo/public/cpp/bindings/lib/serialization_forward.h" | 19 #include "mojo/public/cpp/bindings/lib/serialization_forward.h" |
| 20 #include "mojo/public/cpp/bindings/lib/serialization_util.h" | 20 #include "mojo/public/cpp/bindings/lib/serialization_util.h" |
| 21 #include "mojo/public/cpp/bindings/native_struct.h" |
| 21 | 22 |
| 22 namespace mojo { | 23 namespace mojo { |
| 23 namespace internal { | 24 namespace internal { |
| 24 | 25 |
| 25 template <typename MaybeConstUserType, | |
| 26 bool unmapped = std::is_same< | |
| 27 NativeStructPtr, | |
| 28 typename std::remove_const<MaybeConstUserType>::type>::value> | |
| 29 struct NativeStructSerializerImpl; | |
| 30 | |
| 31 template <typename MaybeConstUserType> | 26 template <typename MaybeConstUserType> |
| 32 struct NativeStructSerializerImpl<MaybeConstUserType, true> { | 27 struct NativeStructSerializerImpl { |
| 33 static size_t PrepareToSerialize(const NativeStructPtr& input, | |
| 34 SerializationContext* context) { | |
| 35 if (!input) | |
| 36 return 0; | |
| 37 return internal::PrepareToSerialize<Array<uint8_t>>(input->data, context); | |
| 38 } | |
| 39 | |
| 40 static void Serialize(NativeStructPtr& input, | |
| 41 Buffer* buffer, | |
| 42 NativeStruct_Data** output, | |
| 43 SerializationContext* context) { | |
| 44 if (!input) { | |
| 45 *output = nullptr; | |
| 46 return; | |
| 47 } | |
| 48 | |
| 49 Array_Data<uint8_t>* data = nullptr; | |
| 50 const ArrayValidateParams params(0, false, nullptr); | |
| 51 internal::Serialize<Array<uint8_t>>(input->data, buffer, &data, ¶ms, | |
| 52 context); | |
| 53 *output = reinterpret_cast<NativeStruct_Data*>(data); | |
| 54 } | |
| 55 | |
| 56 static bool Deserialize(NativeStruct_Data* input, | |
| 57 NativeStructPtr* output, | |
| 58 SerializationContext* context) { | |
| 59 Array_Data<uint8_t>* data = reinterpret_cast<Array_Data<uint8_t>*>(input); | |
| 60 | |
| 61 NativeStructPtr result(NativeStruct::New()); | |
| 62 if (!internal::Deserialize<Array<uint8_t>>(data, &result->data, context)) { | |
| 63 output = nullptr; | |
| 64 return false; | |
| 65 } | |
| 66 if (!result->data) | |
| 67 *output = nullptr; | |
| 68 else | |
| 69 result.Swap(output); | |
| 70 return true; | |
| 71 } | |
| 72 }; | |
| 73 | |
| 74 template <typename MaybeConstUserType> | |
| 75 struct NativeStructSerializerImpl<MaybeConstUserType, false> { | |
| 76 using UserType = typename std::remove_const<MaybeConstUserType>::type; | 28 using UserType = typename std::remove_const<MaybeConstUserType>::type; |
| 77 using Traits = IPC::ParamTraits<UserType>; | 29 using Traits = IPC::ParamTraits<UserType>; |
| 78 | 30 |
| 79 static size_t PrepareToSerialize(MaybeConstUserType& value, | 31 static size_t PrepareToSerialize(MaybeConstUserType& value, |
| 80 SerializationContext* context) { | 32 SerializationContext* context) { |
| 81 base::PickleSizer sizer; | 33 base::PickleSizer sizer; |
| 82 Traits::GetSize(&sizer, value); | 34 Traits::GetSize(&sizer, value); |
| 83 return Align(sizer.payload_size() + sizeof(ArrayHeader)); | 35 return Align(sizer.payload_size() + sizeof(ArrayHeader)); |
| 84 } | 36 } |
| 85 | 37 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 return false; | 89 return false; |
| 138 } | 90 } |
| 139 | 91 |
| 140 // Return the header to its original state. | 92 // Return the header to its original state. |
| 141 header->num_bytes += sizeof(ArrayHeader); | 93 header->num_bytes += sizeof(ArrayHeader); |
| 142 | 94 |
| 143 return true; | 95 return true; |
| 144 } | 96 } |
| 145 }; | 97 }; |
| 146 | 98 |
| 99 struct UnmappedNativeStructSerializerImpl { |
| 100 static size_t PrepareToSerialize(const NativeStructPtr& input, |
| 101 SerializationContext* context); |
| 102 static void Serialize(const NativeStructPtr& input, |
| 103 Buffer* buffer, |
| 104 NativeStruct_Data** output, |
| 105 SerializationContext* context); |
| 106 static bool Deserialize(NativeStruct_Data* input, |
| 107 NativeStructPtr* output, |
| 108 SerializationContext* context); |
| 109 }; |
| 110 |
| 111 template <> |
| 112 struct NativeStructSerializerImpl<NativeStructPtr> |
| 113 : public UnmappedNativeStructSerializerImpl {}; |
| 114 |
| 115 template <> |
| 116 struct NativeStructSerializerImpl<const NativeStructPtr> |
| 117 : public UnmappedNativeStructSerializerImpl {}; |
| 118 |
| 147 template <typename MaybeConstUserType> | 119 template <typename MaybeConstUserType> |
| 148 struct Serializer<NativeStructPtr, MaybeConstUserType> | 120 struct Serializer<NativeStructPtr, MaybeConstUserType> |
| 149 : public NativeStructSerializerImpl<MaybeConstUserType> {}; | 121 : public NativeStructSerializerImpl<MaybeConstUserType> {}; |
| 150 | 122 |
| 151 } // namespace internal | 123 } // namespace internal |
| 152 } // namespace mojo | 124 } // namespace mojo |
| 153 | 125 |
| 154 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_STRUCT_SERIALIZATION_H_ | 126 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_STRUCT_SERIALIZATION_H_ |
| OLD | NEW |