OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <limits> |
| 11 |
| 12 #include "base/logging.h" |
| 13 #include "base/pickle.h" |
| 14 #include "ipc/ipc_param_traits.h" |
| 15 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
| 16 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
| 17 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" |
| 18 #include "mojo/public/cpp/bindings/lib/pickle_buffer.h" |
| 19 |
| 20 namespace mojo { |
| 21 namespace internal { |
| 22 |
| 23 template <typename T> |
| 24 size_t GetSerializedSizeNative_(const T& value) { |
| 25 return IPC::ParamTraits<T>::GetSize(value); |
| 26 } |
| 27 |
| 28 template <typename T> |
| 29 void SerializeNative_(const T& value, |
| 30 Buffer* buffer, |
| 31 Array_Data<uint8_t>** out) { |
| 32 PickleBuffer* pickler = buffer->AsPickleBuffer(); |
| 33 DCHECK(pickler) << "Native types can only be used with PickleBuffers."; |
| 34 |
| 35 ArrayHeader* header = |
| 36 reinterpret_cast<ArrayHeader*>(buffer->Allocate(sizeof(ArrayHeader))); |
| 37 |
| 38 // Remember where the Pickle started before writing. |
| 39 base::Pickle* pickle = pickler->pickle(); |
| 40 const char* data_start = pickle->end_of_payload(); |
| 41 |
| 42 IPC::ParamTraits<T>::Write(pickle, value); |
| 43 |
| 44 // Fix up the ArrayHeader so that num_elements contains the length of the |
| 45 // pickled data. |
| 46 size_t pickled_size = pickle->end_of_payload() - data_start; |
| 47 size_t total_size = pickled_size + sizeof(ArrayHeader); |
| 48 DCHECK_LT(total_size, std::numeric_limits<uint32_t>::max()); |
| 49 header->num_bytes = static_cast<uint32_t>(total_size); |
| 50 header->num_elements = static_cast<uint32_t>(pickled_size); |
| 51 |
| 52 *out = reinterpret_cast<Array_Data<uint8_t>*>(header); |
| 53 } |
| 54 |
| 55 template <typename T> |
| 56 bool DeserializeNative_(Array_Data<uint8_t>* data, |
| 57 T* out, |
| 58 SerializationContext* context) { |
| 59 if (!data) |
| 60 return true; |
| 61 |
| 62 // Construct a temporary base::Pickle view over the array data. Note that |
| 63 // the Array_Data is laid out like this: |
| 64 // |
| 65 // [num_bytes (4 bytes)] [num_elements (4 bytes)] [elements...] |
| 66 // |
| 67 // and base::Pickle expects to view data like this: |
| 68 // |
| 69 // [payload_size (4 bytes)] [header bytes ...] [payload...] |
| 70 // |
| 71 // Because ArrayHeader's num_bytes includes the length of the header and |
| 72 // Pickle's payload_size does not, we need to adjust the stored value |
| 73 // momentarily so Pickle can view the data. |
| 74 ArrayHeader* header = reinterpret_cast<ArrayHeader*>(data); |
| 75 DCHECK_GE(header->num_bytes, sizeof(ArrayHeader)); |
| 76 header->num_bytes -= sizeof(ArrayHeader); |
| 77 |
| 78 { |
| 79 // Construct a view over the full Array_Data, including our hacked up |
| 80 // header. Pickle will infer from this that the header is 8 bytes long, |
| 81 // and the payload will contain all of the pickled bytes. |
| 82 base::Pickle pickle_view(reinterpret_cast<const char*>(header), |
| 83 header->num_bytes + sizeof(ArrayHeader)); |
| 84 base::PickleIterator iter(pickle_view); |
| 85 if (!IPC::ParamTraits<T>::Read(&pickle_view, &iter, out)) |
| 86 return false; |
| 87 } |
| 88 |
| 89 // Return the header to its original state. |
| 90 header->num_bytes += sizeof(ArrayHeader); |
| 91 |
| 92 return true; |
| 93 } |
| 94 |
| 95 } // namespace internal |
| 96 } // namespace mojo |
| 97 |
| 98 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_ |
OLD | NEW |