| 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/bindings_serialization.h" | 18 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" |
| 19 #include "mojo/public/cpp/bindings/lib/native_struct_data.h" | 19 #include "mojo/public/cpp/bindings/lib/native_struct_data.h" |
| 20 #include "mojo/public/cpp/bindings/lib/pickle_buffer.h" | |
| 21 #include "mojo/public/cpp/bindings/lib/serialization_forward.h" | 20 #include "mojo/public/cpp/bindings/lib/serialization_forward.h" |
| 22 | 21 |
| 23 namespace mojo { | 22 namespace mojo { |
| 24 namespace internal { | 23 namespace internal { |
| 25 | 24 |
| 26 // Generated bindings for native-only types will specialize this to |true|. | 25 // Generated bindings for native-only types will specialize this to |true|. |
| 27 // It can be used as a signal (by e.g. the Array serializer) for when to use | 26 // It can be used as a signal (by e.g. the Array serializer) for when to use |
| 28 // SerializeNative_ with a type. | 27 // SerializeNative_ with a type. |
| 29 template <typename E> | 28 template <typename E> |
| 30 struct ShouldUseNativeSerializer { | 29 struct ShouldUseNativeSerializer { |
| 31 static const bool value = false; | 30 static const bool value = false; |
| 32 }; | 31 }; |
| 33 | 32 |
| 34 template <typename T> | 33 template <typename T> |
| 35 size_t GetSerializedSizeNative_(const T& value, SerializationContext* context) { | 34 size_t GetSerializedSizeNative_(const T& value, SerializationContext* context) { |
| 36 base::PickleSizer sizer; | 35 base::PickleSizer sizer; |
| 37 IPC::ParamTraits<T>::GetSize(&sizer, value); | 36 IPC::ParamTraits<T>::GetSize(&sizer, value); |
| 38 return Align(sizer.payload_size() + sizeof(ArrayHeader)); | 37 return Align(sizer.payload_size() + sizeof(ArrayHeader)); |
| 39 } | 38 } |
| 40 | 39 |
| 41 template <typename T> | 40 template <typename T> |
| 42 void SerializeNative_(const T& value, | 41 void SerializeNative_(const T& value, |
| 43 Buffer* buffer, | 42 Buffer* buffer, |
| 44 NativeStruct_Data** out, | 43 NativeStruct_Data** out, |
| 45 SerializationContext* context) { | 44 SerializationContext* context) { |
| 46 PickleBuffer* pickler = buffer->AsPickleBuffer(); | 45 base::Pickle pickle; |
| 47 DCHECK(pickler) << "Native types can only be used with PickleBuffers."; | 46 IPC::ParamTraits<T>::Write(&pickle, value); |
| 48 | 47 |
| 48 size_t total_size = pickle.payload_size() + sizeof(ArrayHeader); |
| 49 DCHECK_LT(total_size, std::numeric_limits<uint32_t>::max()); |
| 50 DCHECK_EQ(Align(total_size), GetSerializedSizeNative_(value, context)); |
| 51 |
| 52 // Allocate a uint8 array, initialize its header, and copy the Pickle in. |
| 49 ArrayHeader* header = | 53 ArrayHeader* header = |
| 50 reinterpret_cast<ArrayHeader*>(buffer->Allocate(sizeof(ArrayHeader))); | 54 reinterpret_cast<ArrayHeader*>(buffer->Allocate(total_size)); |
| 51 | |
| 52 // Remember where the Pickle started before writing. | |
| 53 base::Pickle* pickle = pickler->pickle(); | |
| 54 const char* data_start = pickle->end_of_payload(); | |
| 55 | |
| 56 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | |
| 57 const char* payload_base = pickle->payload(); | |
| 58 size_t size_before_write = pickle->payload_size(); | |
| 59 #endif | |
| 60 | |
| 61 IPC::ParamTraits<T>::Write(pickle, value); | |
| 62 | |
| 63 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | |
| 64 // Ensure the pickle buffer hasn't moved. | |
| 65 DCHECK_EQ(payload_base, pickle->payload()); | |
| 66 // Explicitly validate that the value returned by GetSize() always equals the | |
| 67 // number of bytes actually written by Write(). | |
| 68 DCHECK_GE(pickle->payload_size(), size_before_write); | |
| 69 size_t bytes_written = pickle->payload_size() - size_before_write; | |
| 70 DCHECK_EQ(Align(bytes_written + sizeof(ArrayHeader)), | |
| 71 GetSerializedSizeNative_(value, context)); | |
| 72 #endif | |
| 73 | |
| 74 // Fix up the ArrayHeader so that num_elements contains the length of the | |
| 75 // pickled data. | |
| 76 size_t pickled_size = pickle->end_of_payload() - data_start; | |
| 77 size_t total_size = pickled_size + sizeof(ArrayHeader); | |
| 78 DCHECK_LT(total_size, std::numeric_limits<uint32_t>::max()); | |
| 79 header->num_bytes = static_cast<uint32_t>(total_size); | 55 header->num_bytes = static_cast<uint32_t>(total_size); |
| 80 header->num_elements = static_cast<uint32_t>(pickled_size); | 56 header->num_elements = static_cast<uint32_t>(pickle.payload_size()); |
| 57 memcpy(reinterpret_cast<char*>(header) + sizeof(ArrayHeader), |
| 58 pickle.payload(), pickle.payload_size()); |
| 81 | 59 |
| 82 *out = reinterpret_cast<NativeStruct_Data*>(header); | 60 *out = reinterpret_cast<NativeStruct_Data*>(header); |
| 83 } | 61 } |
| 84 | 62 |
| 85 template <typename T> | 63 template <typename T> |
| 86 bool DeserializeNative_(NativeStruct_Data* data, | 64 bool DeserializeNative_(NativeStruct_Data* data, |
| 87 T* out, | 65 T* out, |
| 88 SerializationContext* context) { | 66 SerializationContext* context) { |
| 89 if (!data) | 67 if (!data) |
| 90 return true; | 68 return true; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 119 // Return the header to its original state. | 97 // Return the header to its original state. |
| 120 header->num_bytes += sizeof(ArrayHeader); | 98 header->num_bytes += sizeof(ArrayHeader); |
| 121 | 99 |
| 122 return true; | 100 return true; |
| 123 } | 101 } |
| 124 | 102 |
| 125 } // namespace internal | 103 } // namespace internal |
| 126 } // namespace mojo | 104 } // namespace mojo |
| 127 | 105 |
| 128 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_STRUCT_SERIALIZATION_H_ | 106 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_STRUCT_SERIALIZATION_H_ |
| OLD | NEW |