| 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_SERIALIZATION_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 namespace internal { | 22 namespace internal { |
| 23 | 23 |
| 24 // Generated bindings for native-only types will specialize this to |true|. | 24 // Generated bindings for native-only types will specialize this to |true|. |
| 25 // It can be used as a signal (by e.g. the Array serializer) for when to use | 25 // It can be used as a signal (by e.g. the Array serializer) for when to use |
| 26 // SerializeNative_ with a type. | 26 // SerializeNative_ with a type. |
| 27 template <typename E> | 27 template <typename E> |
| 28 struct ShouldUseNativeSerializer { static const bool value = false; }; | 28 struct ShouldUseNativeSerializer { static const bool value = false; }; |
| 29 | 29 |
| 30 template <typename T> | 30 template <typename T> |
| 31 size_t GetSerializedSizeNative_(const T& value) { | 31 size_t GetSerializedSizeNative_(const T& value) { |
| 32 return IPC::ParamTraits<T>::GetSize(value); | 32 base::PickleSizer sizer; |
| 33 IPC::ParamTraits<T>::GetSize(&sizer, value); |
| 34 return sizer.payload_size() + sizeof(ArrayHeader); |
| 33 } | 35 } |
| 34 | 36 |
| 35 template <typename T> | 37 template <typename T> |
| 36 void SerializeNative_(const T& value, | 38 void SerializeNative_(const T& value, |
| 37 Buffer* buffer, | 39 Buffer* buffer, |
| 38 Array_Data<uint8_t>** out) { | 40 Array_Data<uint8_t>** out) { |
| 39 PickleBuffer* pickler = buffer->AsPickleBuffer(); | 41 PickleBuffer* pickler = buffer->AsPickleBuffer(); |
| 40 DCHECK(pickler) << "Native types can only be used with PickleBuffers."; | 42 DCHECK(pickler) << "Native types can only be used with PickleBuffers."; |
| 41 | 43 |
| 42 ArrayHeader* header = | 44 ArrayHeader* header = |
| 43 reinterpret_cast<ArrayHeader*>(buffer->Allocate(sizeof(ArrayHeader))); | 45 reinterpret_cast<ArrayHeader*>(buffer->Allocate(sizeof(ArrayHeader))); |
| 44 | 46 |
| 45 // Remember where the Pickle started before writing. | 47 // Remember where the Pickle started before writing. |
| 46 base::Pickle* pickle = pickler->pickle(); | 48 base::Pickle* pickle = pickler->pickle(); |
| 47 const char* data_start = pickle->end_of_payload(); | 49 const char* data_start = pickle->end_of_payload(); |
| 48 | 50 |
| 51 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 52 const char* payload_base = pickle->payload(); |
| 53 size_t size_before_write = pickle->payload_size(); |
| 54 #endif |
| 55 |
| 49 IPC::ParamTraits<T>::Write(pickle, value); | 56 IPC::ParamTraits<T>::Write(pickle, value); |
| 50 | 57 |
| 58 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 59 // Ensure the pickle buffer hasn't moved. |
| 60 DCHECK_EQ(payload_base, pickle->payload()); |
| 61 // Explicitly validate that the value returned by GetSize() always equals the |
| 62 // number of bytes actually written by Write(). |
| 63 DCHECK_GE(pickle->payload_size(), size_before_write); |
| 64 size_t bytes_written = pickle->payload_size() - size_before_write; |
| 65 DCHECK_EQ(bytes_written + sizeof(ArrayHeader), |
| 66 GetSerializedSizeNative_(value)); |
| 67 #endif |
| 68 |
| 51 // Fix up the ArrayHeader so that num_elements contains the length of the | 69 // Fix up the ArrayHeader so that num_elements contains the length of the |
| 52 // pickled data. | 70 // pickled data. |
| 53 size_t pickled_size = pickle->end_of_payload() - data_start; | 71 size_t pickled_size = pickle->end_of_payload() - data_start; |
| 54 size_t total_size = pickled_size + sizeof(ArrayHeader); | 72 size_t total_size = pickled_size + sizeof(ArrayHeader); |
| 55 DCHECK_LT(total_size, std::numeric_limits<uint32_t>::max()); | 73 DCHECK_LT(total_size, std::numeric_limits<uint32_t>::max()); |
| 56 header->num_bytes = static_cast<uint32_t>(total_size); | 74 header->num_bytes = static_cast<uint32_t>(total_size); |
| 57 header->num_elements = static_cast<uint32_t>(pickled_size); | 75 header->num_elements = static_cast<uint32_t>(pickled_size); |
| 58 | 76 |
| 59 *out = reinterpret_cast<Array_Data<uint8_t>*>(header); | 77 *out = reinterpret_cast<Array_Data<uint8_t>*>(header); |
| 60 } | 78 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 // Return the header to its original state. | 114 // Return the header to its original state. |
| 97 header->num_bytes += sizeof(ArrayHeader); | 115 header->num_bytes += sizeof(ArrayHeader); |
| 98 | 116 |
| 99 return true; | 117 return true; |
| 100 } | 118 } |
| 101 | 119 |
| 102 } // namespace internal | 120 } // namespace internal |
| 103 } // namespace mojo | 121 } // namespace mojo |
| 104 | 122 |
| 105 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_ | 123 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_ |
| OLD | NEW |