| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_HANDLE_INTERFACE_SERIALIZATION_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_HANDLE_INTERFACE_SERIALIZATION_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_HANDLE_INTERFACE_SERIALIZATION_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_HANDLE_INTERFACE_SERIALIZATION_H_ |
| 7 | 7 |
| 8 #include <type_traits> | 8 #include <type_traits> |
| 9 | 9 |
| 10 #include "mojo/public/cpp/bindings/associated_group_controller.h" | 10 #include "mojo/public/cpp/bindings/associated_group_controller.h" |
| 11 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" | 11 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" |
| 12 #include "mojo/public/cpp/bindings/associated_interface_request.h" | 12 #include "mojo/public/cpp/bindings/associated_interface_request.h" |
| 13 #include "mojo/public/cpp/bindings/interface_data_view.h" | 13 #include "mojo/public/cpp/bindings/interface_data_view.h" |
| 14 #include "mojo/public/cpp/bindings/interface_ptr.h" | 14 #include "mojo/public/cpp/bindings/interface_ptr.h" |
| 15 #include "mojo/public/cpp/bindings/interface_request.h" | 15 #include "mojo/public/cpp/bindings/interface_request.h" |
| 16 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | 16 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
| 17 #include "mojo/public/cpp/bindings/lib/serialization_context.h" | 17 #include "mojo/public/cpp/bindings/lib/serialization_context.h" |
| 18 #include "mojo/public/cpp/bindings/lib/serialization_forward.h" | 18 #include "mojo/public/cpp/bindings/lib/serialization_forward.h" |
| 19 #include "mojo/public/cpp/system/handle.h" | 19 #include "mojo/public/cpp/system/handle.h" |
| 20 | 20 |
| 21 namespace mojo { | 21 namespace mojo { |
| 22 namespace internal { | 22 namespace internal { |
| 23 | 23 |
| 24 template <typename Base, typename T> | 24 template <typename Base, typename T> |
| 25 struct Serializer<AssociatedInterfacePtrInfoDataView<Base>, | 25 struct Serializer<AssociatedInterfacePtrInfoDataView<Base>, |
| 26 AssociatedInterfacePtrInfo<T>> { | 26 AssociatedInterfacePtrInfo<T>> { |
| 27 static_assert(std::is_base_of<Base, T>::value, "Interface type mismatch."); | 27 static_assert(std::is_base_of<Base, T>::value, "Interface type mismatch."); |
| 28 | 28 |
| 29 static size_t PrepareToSerialize(const AssociatedInterfacePtrInfo<T>& input, |
| 30 SerializationContext* context) { |
| 31 if (input.handle().is_valid()) |
| 32 context->associated_endpoint_count++; |
| 33 return 0; |
| 34 } |
| 35 |
| 29 static void Serialize(AssociatedInterfacePtrInfo<T>& input, | 36 static void Serialize(AssociatedInterfacePtrInfo<T>& input, |
| 30 AssociatedInterface_Data* output, | 37 AssociatedInterface_Data* output, |
| 31 SerializationContext* context) { | 38 SerializationContext* context) { |
| 32 DCHECK(!input.handle().is_valid() || !input.handle().is_local()); | 39 DCHECK(!input.handle().is_valid() || !input.handle().is_local()); |
| 33 if (input.handle().is_valid()) { | 40 if (input.handle().is_valid()) { |
| 34 DCHECK_EQ(input.handle().group_controller(), | 41 // Set to the index of the element pushed to the back of the vector. |
| 35 context->group_controller.get()); | 42 output->handle.value = |
| 43 static_cast<uint32_t>(context->associated_endpoint_handles.size()); |
| 44 context->associated_endpoint_handles.push_back(input.PassHandle()); |
| 45 } else { |
| 46 output->handle.value = kEncodedInvalidHandleValue; |
| 36 } | 47 } |
| 37 output->version = input.version(); | 48 output->version = input.version(); |
| 38 output->interface_id = input.PassHandle().release(); | |
| 39 } | 49 } |
| 40 | 50 |
| 41 static bool Deserialize(AssociatedInterface_Data* input, | 51 static bool Deserialize(AssociatedInterface_Data* input, |
| 42 AssociatedInterfacePtrInfo<T>* output, | 52 AssociatedInterfacePtrInfo<T>* output, |
| 43 SerializationContext* context) { | 53 SerializationContext* context) { |
| 44 output->set_handle(context->group_controller->CreateLocalEndpointHandle( | 54 if (input->handle.is_valid()) { |
| 45 FetchAndReset(&input->interface_id))); | 55 DCHECK_LT(input->handle.value, |
| 56 context->associated_endpoint_handles.size()); |
| 57 output->set_handle( |
| 58 std::move(context->associated_endpoint_handles[input->handle.value])); |
| 59 } else { |
| 60 output->set_handle(ScopedInterfaceEndpointHandle()); |
| 61 } |
| 46 output->set_version(input->version); | 62 output->set_version(input->version); |
| 47 return true; | 63 return true; |
| 48 } | 64 } |
| 49 }; | 65 }; |
| 50 | 66 |
| 51 template <typename Base, typename T> | 67 template <typename Base, typename T> |
| 52 struct Serializer<AssociatedInterfaceRequestDataView<Base>, | 68 struct Serializer<AssociatedInterfaceRequestDataView<Base>, |
| 53 AssociatedInterfaceRequest<T>> { | 69 AssociatedInterfaceRequest<T>> { |
| 54 static_assert(std::is_base_of<Base, T>::value, "Interface type mismatch."); | 70 static_assert(std::is_base_of<Base, T>::value, "Interface type mismatch."); |
| 55 | 71 |
| 72 static size_t PrepareToSerialize(const AssociatedInterfaceRequest<T>& input, |
| 73 SerializationContext* context) { |
| 74 if (input.handle().is_valid()) |
| 75 context->associated_endpoint_count++; |
| 76 return 0; |
| 77 } |
| 78 |
| 56 static void Serialize(AssociatedInterfaceRequest<T>& input, | 79 static void Serialize(AssociatedInterfaceRequest<T>& input, |
| 57 AssociatedInterfaceRequest_Data* output, | 80 AssociatedEndpointHandle_Data* output, |
| 58 SerializationContext* context) { | 81 SerializationContext* context) { |
| 59 DCHECK(!input.handle().is_valid() || !input.handle().is_local()); | 82 DCHECK(!input.handle().is_valid() || !input.handle().is_local()); |
| 60 if (input.handle().is_valid()) { | 83 if (input.handle().is_valid()) { |
| 61 DCHECK_EQ(input.handle().group_controller(), | 84 // Set to the index of the element pushed to the back of the vector. |
| 62 context->group_controller.get()); | 85 output->value = |
| 86 static_cast<uint32_t>(context->associated_endpoint_handles.size()); |
| 87 context->associated_endpoint_handles.push_back(input.PassHandle()); |
| 88 } else { |
| 89 output->value = kEncodedInvalidHandleValue; |
| 63 } | 90 } |
| 64 output->interface_id = input.PassHandle().release(); | |
| 65 } | 91 } |
| 66 | 92 |
| 67 static bool Deserialize(AssociatedInterfaceRequest_Data* input, | 93 static bool Deserialize(AssociatedEndpointHandle_Data* input, |
| 68 AssociatedInterfaceRequest<T>* output, | 94 AssociatedInterfaceRequest<T>* output, |
| 69 SerializationContext* context) { | 95 SerializationContext* context) { |
| 70 output->Bind(context->group_controller->CreateLocalEndpointHandle( | 96 if (input->is_valid()) { |
| 71 FetchAndReset(&input->interface_id))); | 97 DCHECK_LT(input->value, context->associated_endpoint_handles.size()); |
| 98 output->Bind( |
| 99 std::move(context->associated_endpoint_handles[input->value])); |
| 100 } else { |
| 101 output->Bind(ScopedInterfaceEndpointHandle()); |
| 102 } |
| 72 return true; | 103 return true; |
| 73 } | 104 } |
| 74 }; | 105 }; |
| 75 | 106 |
| 76 template <typename Base, typename T> | 107 template <typename Base, typename T> |
| 77 struct Serializer<InterfacePtrDataView<Base>, InterfacePtr<T>> { | 108 struct Serializer<InterfacePtrDataView<Base>, InterfacePtr<T>> { |
| 78 static_assert(std::is_base_of<Base, T>::value, "Interface type mismatch."); | 109 static_assert(std::is_base_of<Base, T>::value, "Interface type mismatch."); |
| 79 | 110 |
| 111 static size_t PrepareToSerialize(const InterfacePtr<T>& input, |
| 112 SerializationContext* context) { |
| 113 return 0; |
| 114 } |
| 115 |
| 80 static void Serialize(InterfacePtr<T>& input, | 116 static void Serialize(InterfacePtr<T>& input, |
| 81 Interface_Data* output, | 117 Interface_Data* output, |
| 82 SerializationContext* context) { | 118 SerializationContext* context) { |
| 83 InterfacePtrInfo<T> info = input.PassInterface(); | 119 InterfacePtrInfo<T> info = input.PassInterface(); |
| 84 output->handle = context->handles.AddHandle(info.PassHandle().release()); | 120 output->handle = context->handles.AddHandle(info.PassHandle().release()); |
| 85 output->version = info.version(); | 121 output->version = info.version(); |
| 86 } | 122 } |
| 87 | 123 |
| 88 static bool Deserialize(Interface_Data* input, | 124 static bool Deserialize(Interface_Data* input, |
| 89 InterfacePtr<T>* output, | 125 InterfacePtr<T>* output, |
| 90 SerializationContext* context) { | 126 SerializationContext* context) { |
| 91 output->Bind(InterfacePtrInfo<T>( | 127 output->Bind(InterfacePtrInfo<T>( |
| 92 context->handles.TakeHandleAs<mojo::MessagePipeHandle>(input->handle), | 128 context->handles.TakeHandleAs<mojo::MessagePipeHandle>(input->handle), |
| 93 input->version)); | 129 input->version)); |
| 94 return true; | 130 return true; |
| 95 } | 131 } |
| 96 }; | 132 }; |
| 97 | 133 |
| 98 template <typename Base, typename T> | 134 template <typename Base, typename T> |
| 99 struct Serializer<InterfaceRequestDataView<Base>, InterfaceRequest<T>> { | 135 struct Serializer<InterfaceRequestDataView<Base>, InterfaceRequest<T>> { |
| 100 static_assert(std::is_base_of<Base, T>::value, "Interface type mismatch."); | 136 static_assert(std::is_base_of<Base, T>::value, "Interface type mismatch."); |
| 101 | 137 |
| 138 static size_t PrepareToSerialize(const InterfaceRequest<T>& input, |
| 139 SerializationContext* context) { |
| 140 return 0; |
| 141 } |
| 142 |
| 102 static void Serialize(InterfaceRequest<T>& input, | 143 static void Serialize(InterfaceRequest<T>& input, |
| 103 Handle_Data* output, | 144 Handle_Data* output, |
| 104 SerializationContext* context) { | 145 SerializationContext* context) { |
| 105 *output = context->handles.AddHandle(input.PassMessagePipe().release()); | 146 *output = context->handles.AddHandle(input.PassMessagePipe().release()); |
| 106 } | 147 } |
| 107 | 148 |
| 108 static bool Deserialize(Handle_Data* input, | 149 static bool Deserialize(Handle_Data* input, |
| 109 InterfaceRequest<T>* output, | 150 InterfaceRequest<T>* output, |
| 110 SerializationContext* context) { | 151 SerializationContext* context) { |
| 111 output->Bind(context->handles.TakeHandleAs<MessagePipeHandle>(*input)); | 152 output->Bind(context->handles.TakeHandleAs<MessagePipeHandle>(*input)); |
| 112 return true; | 153 return true; |
| 113 } | 154 } |
| 114 }; | 155 }; |
| 115 | 156 |
| 116 template <typename T> | 157 template <typename T> |
| 117 struct Serializer<ScopedHandleBase<T>, ScopedHandleBase<T>> { | 158 struct Serializer<ScopedHandleBase<T>, ScopedHandleBase<T>> { |
| 159 static size_t PrepareToSerialize(const ScopedHandleBase<T>& input, |
| 160 SerializationContext* context) { |
| 161 return 0; |
| 162 } |
| 163 |
| 118 static void Serialize(ScopedHandleBase<T>& input, | 164 static void Serialize(ScopedHandleBase<T>& input, |
| 119 Handle_Data* output, | 165 Handle_Data* output, |
| 120 SerializationContext* context) { | 166 SerializationContext* context) { |
| 121 *output = context->handles.AddHandle(input.release()); | 167 *output = context->handles.AddHandle(input.release()); |
| 122 } | 168 } |
| 123 | 169 |
| 124 static bool Deserialize(Handle_Data* input, | 170 static bool Deserialize(Handle_Data* input, |
| 125 ScopedHandleBase<T>* output, | 171 ScopedHandleBase<T>* output, |
| 126 SerializationContext* context) { | 172 SerializationContext* context) { |
| 127 *output = context->handles.TakeHandleAs<T>(*input); | 173 *output = context->handles.TakeHandleAs<T>(*input); |
| 128 return true; | 174 return true; |
| 129 } | 175 } |
| 130 }; | 176 }; |
| 131 | 177 |
| 132 } // namespace internal | 178 } // namespace internal |
| 133 } // namespace mojo | 179 } // namespace mojo |
| 134 | 180 |
| 135 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_HANDLE_INTERFACE_SERIALIZATION_H_ | 181 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_HANDLE_INTERFACE_SERIALIZATION_H_ |
| OLD | NEW |