| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_BINDINGS_SERIALIZATION_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <queue> | 11 #include <queue> |
| 12 #include <vector> | |
| 13 | 12 |
| 13 #include "base/logging.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | |
| 16 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" | 15 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" |
| 17 #include "mojo/public/cpp/bindings/interface_ptr.h" | 16 #include "mojo/public/cpp/bindings/interface_ptr.h" |
| 18 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | 17 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
| 19 #include "mojo/public/cpp/system/core.h" | 18 #include "mojo/public/cpp/bindings/lib/serialization_context.h" |
| 20 #include "mojo/public/cpp/system/handle.h" | 19 #include "mojo/public/cpp/system/handle.h" |
| 21 | 20 |
| 22 namespace mojo { | 21 namespace mojo { |
| 23 namespace internal { | 22 namespace internal { |
| 24 | 23 |
| 25 struct SerializationContext; | |
| 26 class MultiplexRouter; | 24 class MultiplexRouter; |
| 27 | 25 |
| 28 size_t Align(size_t size); | 26 size_t Align(size_t size); |
| 29 char* AlignPointer(char* ptr); | 27 char* AlignPointer(char* ptr); |
| 30 | 28 |
| 31 bool IsAligned(const void* ptr); | 29 bool IsAligned(const void* ptr); |
| 32 | 30 |
| 33 // Pointers are encoded as relative offsets. The offsets are relative to the | 31 // Pointers are encoded as relative offsets. The offsets are relative to the |
| 34 // address of where the offset value is stored, such that the pointer may be | 32 // address of where the offset value is stored, such that the pointer may be |
| 35 // recovered with the expression: | 33 // recovered with the expression: |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 inline void AssociatedInterfaceDataToPtrInfo( | 77 inline void AssociatedInterfaceDataToPtrInfo( |
| 80 AssociatedInterface_Data* input, | 78 AssociatedInterface_Data* input, |
| 81 AssociatedInterfacePtrInfo<T>* output, | 79 AssociatedInterfacePtrInfo<T>* output, |
| 82 MultiplexRouter* router) { | 80 MultiplexRouter* router) { |
| 83 AssociatedInterfacePtrInfoHelper::SetHandle( | 81 AssociatedInterfacePtrInfoHelper::SetHandle( |
| 84 output, | 82 output, |
| 85 router->CreateLocalEndpointHandle(FetchAndReset(&input->interface_id))); | 83 router->CreateLocalEndpointHandle(FetchAndReset(&input->interface_id))); |
| 86 output->set_version(input->version); | 84 output->set_version(input->version); |
| 87 } | 85 } |
| 88 | 86 |
| 89 // A container for handles during serialization/deserialization. | |
| 90 class SerializedHandleVector { | |
| 91 public: | |
| 92 SerializedHandleVector(); | |
| 93 ~SerializedHandleVector(); | |
| 94 | |
| 95 size_t size() const { return handles_.size(); } | |
| 96 | |
| 97 // Adds a handle to the handle list and returns its index for encoding. | |
| 98 Handle_Data AddHandle(mojo::Handle handle); | |
| 99 | |
| 100 // Takes a handle from the list of serialized handle data. | |
| 101 mojo::Handle TakeHandle(const Handle_Data& encoded_handle); | |
| 102 | |
| 103 // Takes a handle from the list of serialized handle data and returns it in | |
| 104 // |*out_handle| as a specific scoped handle type. | |
| 105 template <typename T> | |
| 106 ScopedHandleBase<T> TakeHandleAs(const Handle_Data& encoded_handle) { | |
| 107 return MakeScopedHandle(T(TakeHandle(encoded_handle).value())); | |
| 108 } | |
| 109 | |
| 110 // Swaps all owned handles out with another Handle vector. | |
| 111 void Swap(std::vector<mojo::Handle>* other); | |
| 112 | |
| 113 private: | |
| 114 // Handles are owned by this object. | |
| 115 std::vector<mojo::Handle> handles_; | |
| 116 | |
| 117 DISALLOW_COPY_AND_ASSIGN(SerializedHandleVector); | |
| 118 }; | |
| 119 | |
| 120 // Context information for serialization/deserialization routines. | |
| 121 struct SerializationContext { | |
| 122 SerializationContext(); | |
| 123 explicit SerializationContext(scoped_refptr<MultiplexRouter> in_router); | |
| 124 | |
| 125 ~SerializationContext(); | |
| 126 | |
| 127 // Used to serialize/deserialize associated interface pointers and requests. | |
| 128 scoped_refptr<MultiplexRouter> router; | |
| 129 | |
| 130 // Opaque context pointers returned by StringTraits::SetUpContext(). | |
| 131 std::unique_ptr<std::queue<void*>> custom_contexts; | |
| 132 | |
| 133 // Stashes handles encoded in a message by index. | |
| 134 SerializedHandleVector handles; | |
| 135 }; | |
| 136 | |
| 137 template <typename T> | 87 template <typename T> |
| 138 inline void InterfacePointerToData(InterfacePtr<T> input, | 88 inline void InterfacePointerToData(InterfacePtr<T> input, |
| 139 Interface_Data* output, | 89 Interface_Data* output, |
| 140 SerializationContext* context) { | 90 SerializationContext* context) { |
| 141 InterfacePtrInfo<T> info = input.PassInterface(); | 91 InterfacePtrInfo<T> info = input.PassInterface(); |
| 142 output->handle = context->handles.AddHandle(info.PassHandle().release()); | 92 output->handle = context->handles.AddHandle(info.PassHandle().release()); |
| 143 output->version = info.version(); | 93 output->version = info.version(); |
| 144 } | 94 } |
| 145 | 95 |
| 146 template <typename T> | 96 template <typename T> |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 template <typename ReturnType, typename ParamType, typename MaybeConstUserType> | 206 template <typename ReturnType, typename ParamType, typename MaybeConstUserType> |
| 257 ReturnType CallWithContext(ReturnType (*f)(ParamType), | 207 ReturnType CallWithContext(ReturnType (*f)(ParamType), |
| 258 MaybeConstUserType& input, | 208 MaybeConstUserType& input, |
| 259 void* context) { | 209 void* context) { |
| 260 return f(input); | 210 return f(input); |
| 261 } | 211 } |
| 262 | 212 |
| 263 } // namespace internal | 213 } // namespace internal |
| 264 } // namespace mojo | 214 } // namespace mojo |
| 265 | 215 |
| 266 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_ | 216 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_ |
| OLD | NEW |