| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // TODO(vardhan): Currently, the logic for serializing a mojom type exists in | 5 // TODO(vardhan): Currently, the logic for serializing a mojom type exists in |
| 6 // two places: the C++ code generator template, and here. However, most types | 6 // two places: the C++ code generator template, and here. However, most types |
| 7 // are serialized the same way within Arrays or outside, with the exception of | 7 // are serialized the same way within Arrays or outside, with the exception of |
| 8 // |bool|. Consider defining serialization/deserialization traits for each | 8 // |bool|. Consider defining serialization/deserialization traits for each |
| 9 // serializable type and call those traits from here. This should help us | 9 // serializable type and call those traits from here. This should help us |
| 10 // remove most of the ArraySerializer<> specializations here. | 10 // remove most of the ArraySerializer<> specializations here. |
| 11 | 11 |
| 12 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 12 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
| 13 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 13 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
| 14 | 14 |
| 15 #include <string.h> // For |memcpy()|. | 15 #include <string.h> // For |memcpy()|. |
| 16 #include <type_traits> | 16 #include <type_traits> |
| 17 #include <vector> | 17 #include <vector> |
| 18 | 18 |
| 19 #include "mojo/public/c/system/macros.h" | 19 #include "mojo/public/c/system/macros.h" |
| 20 #include "mojo/public/cpp/bindings/lib/array_internal.h" | 20 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
| 21 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | 21 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
| 22 #include "mojo/public/cpp/bindings/lib/iterator_util.h" | 22 #include "mojo/public/cpp/bindings/lib/iterator_util.h" |
| 23 #include "mojo/public/cpp/bindings/lib/map_data_internal.h" | 23 #include "mojo/public/cpp/bindings/lib/map_data_internal.h" |
| 24 #include "mojo/public/cpp/bindings/lib/map_serialization_forward.h" | 24 #include "mojo/public/cpp/bindings/lib/map_serialization_forward.h" |
| 25 #include "mojo/public/cpp/bindings/lib/string_serialization.h" | 25 #include "mojo/public/cpp/bindings/lib/string_serialization.h" |
| 26 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | 26 #include "mojo/public/cpp/bindings/lib/validation_errors.h" |
| 27 | 27 |
| 28 namespace mojo { | 28 namespace mojo { |
| 29 | 29 |
| 30 template <typename Interface> | |
| 31 InterfaceRequest<Interface> MakeRequest(ScopedMessagePipeHandle handle); | |
| 32 | |
| 33 namespace internal { | 30 namespace internal { |
| 34 | 31 |
| 35 // The ArraySerializer template contains static methods for serializing |Array|s | 32 // The ArraySerializer template contains static methods for serializing |Array|s |
| 36 // of various types. These methods include: | 33 // of various types. These methods include: |
| 37 // * size_t GetSerializedSize(..) | 34 // * size_t GetSerializedSize(..) |
| 38 // Computes the size of the serialized version of the |Array|. | 35 // Computes the size of the serialized version of the |Array|. |
| 39 // * void SerializeElements(..) | 36 // * void SerializeElements(..) |
| 40 // Takes an |Iterator| and a size and serializes it. | 37 // Takes an |Iterator| and a size and serializes it. |
| 41 // * void DeserializeElements(..) | 38 // * void DeserializeElements(..) |
| 42 // Takes a pointer to an |Array_Data| and deserializes it into a given | 39 // Takes a pointer to an |Array_Data| and deserializes it into a given |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 } | 225 } |
| 229 | 226 |
| 230 return ValidationError::NONE; | 227 return ValidationError::NONE; |
| 231 } | 228 } |
| 232 | 229 |
| 233 static void DeserializeElements(Array_Data<MessagePipeHandle>* input, | 230 static void DeserializeElements(Array_Data<MessagePipeHandle>* input, |
| 234 Array<InterfaceRequest<I>>* output) { | 231 Array<InterfaceRequest<I>>* output) { |
| 235 auto result = Array<InterfaceRequest<I>>::New(input->size()); | 232 auto result = Array<InterfaceRequest<I>>::New(input->size()); |
| 236 for (size_t i = 0; i < input->size(); ++i) | 233 for (size_t i = 0; i < input->size(); ++i) |
| 237 result.at(i) = | 234 result.at(i) = |
| 238 MakeRequest<I>(MakeScopedHandle(FetchAndReset(&input->at(i)))).Pass(); | 235 InterfaceRequest<I>(MakeScopedHandle(FetchAndReset(&input->at(i)))) |
| 236 .Pass(); |
| 239 output->Swap(&result); | 237 output->Swap(&result); |
| 240 } | 238 } |
| 241 }; | 239 }; |
| 242 | 240 |
| 243 // Serializes and deserializes arrays of interfaces (interface handles). | 241 // Serializes and deserializes arrays of interfaces (interface handles). |
| 244 template <typename Interface> | 242 template <typename Interface> |
| 245 struct ArraySerializer<InterfaceHandle<Interface>, Interface_Data, false> { | 243 struct ArraySerializer<InterfaceHandle<Interface>, Interface_Data, false> { |
| 246 static size_t GetSerializedSize( | 244 static size_t GetSerializedSize( |
| 247 const Array<InterfaceHandle<Interface>>& input) { | 245 const Array<InterfaceHandle<Interface>>& input) { |
| 248 return sizeof(Array_Data<Interface_Data>) + | 246 return sizeof(Array_Data<Interface_Data>) + |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 532 if (input) { | 530 if (input) { |
| 533 internal::ArraySerializer<E, F>::DeserializeElements(input, output); | 531 internal::ArraySerializer<E, F>::DeserializeElements(input, output); |
| 534 } else { | 532 } else { |
| 535 output->reset(); | 533 output->reset(); |
| 536 } | 534 } |
| 537 } | 535 } |
| 538 | 536 |
| 539 } // namespace mojo | 537 } // namespace mojo |
| 540 | 538 |
| 541 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 539 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
| OLD | NEW |