1 // Copyright 2018 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_INTERFACE_SERIALIZATION_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_SERIALIZATION_H_ 7 8 #include <type_traits> 9 10 #include "mojo/public/cpp/bindings/associated_group_controller.h" 11 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" 12 #include "mojo/public/cpp/bindings/associated_interface_request.h" 13 #include "mojo/public/cpp/bindings/interface_data_view.h" 14 #include "mojo/public/cpp/bindings/interface_ptr.h" 15 #include "mojo/public/cpp/bindings/interface_request.h" 16 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" 17 #include "mojo/public/cpp/bindings/lib/serialization_context.h" 18 #include "mojo/public/cpp/bindings/lib/serialization_forward.h" 19 #include "mojo/public/cpp/system/handle.h" 20 #include "mojo/public/cpp/system/message_pipe.h" 21 22 namespace mojo { 23 namespace internal { 24 25 template <typename Base, typename T> 26 struct Serializer<AssociatedInterfacePtrInfoDataView<Base>, 27 AssociatedInterfacePtrInfo<T>> { 28 static_assert(std::is_base_of<Base, T>::value, "Interface type mismatch."); 29 30 static void Serialize(AssociatedInterfacePtrInfo<T>& input, 31 AssociatedInterface_Data* output, 32 SerializationContext* context) { 33 DCHECK(!input.handle().is_valid() || input.handle().pending_association()); 34 context->AddAssociatedInterfaceInfo(input.PassHandle(), input.version(), 35 output); 36 } 37 38 static bool Deserialize(AssociatedInterface_Data* input, 39 AssociatedInterfacePtrInfo<T>* output, 40 SerializationContext* context) { 41 auto handle = context->TakeAssociatedEndpointHandle(input->handle); 42 if (!handle.is_valid()) { 43 *output = AssociatedInterfacePtrInfo<T>(); 44 } else { 45 output->set_handle(std::move(handle)); 46 output->set_version(input->version); 47 } 48 return true; 49 } 50 }; 51 52 template <typename Base, typename T> 53 struct Serializer<AssociatedInterfaceRequestDataView<Base>, 54 AssociatedInterfaceRequest<T>> { 55 static_assert(std::is_base_of<Base, T>::value, "Interface type mismatch."); 56 57 static void Serialize(AssociatedInterfaceRequest<T>& input, 58 AssociatedEndpointHandle_Data* output, 59 SerializationContext* context) { 60 DCHECK(!input.handle().is_valid() || input.handle().pending_association()); 61 context->AddAssociatedEndpoint(input.PassHandle(), output); 62 } 63 64 static bool Deserialize(AssociatedEndpointHandle_Data* input, 65 AssociatedInterfaceRequest<T>* output, 66 SerializationContext* context) { 67 auto handle = context->TakeAssociatedEndpointHandle(*input); 68 if (!handle.is_valid()) 69 *output = AssociatedInterfaceRequest<T>(); 70 else 71 *output = AssociatedInterfaceRequest<T>(std::move(handle)); 72 return true; 73 } 74 }; 75 76 template <typename Base, typename T> 77 struct Serializer<InterfacePtrDataView<Base>, InterfacePtr<T>> { 78 static_assert(std::is_base_of<Base, T>::value, "Interface type mismatch."); 79 80 static void Serialize(InterfacePtr<T>& input, 81 Interface_Data* output, 82 SerializationContext* context) { 83 InterfacePtrInfo<T> info = input.PassInterface(); 84 context->AddInterfaceInfo(info.PassHandle(), info.version(), output); 85 } 86 87 static bool Deserialize(Interface_Data* input, 88 InterfacePtr<T>* output, 89 SerializationContext* context) { 90 output->Bind(InterfacePtrInfo<T>( 91 context->TakeHandleAs<mojo::MessagePipeHandle>(input->handle), 92 input->version)); 93 return true; 94 } 95 }; 96 97 template <typename Base, typename T> 98 struct Serializer<InterfacePtrDataView<Base>, InterfacePtrInfo<T>> { 99 static_assert(std::is_base_of<Base, T>::value, "Interface type mismatch."); 100 101 static void Serialize(InterfacePtrInfo<T>& input, 102 Interface_Data* output, 103 SerializationContext* context) { 104 context->AddInterfaceInfo(input.PassHandle(), input.version(), output); 105 } 106 107 static bool Deserialize(Interface_Data* input, 108 InterfacePtrInfo<T>* output, 109 SerializationContext* context) { 110 *output = InterfacePtrInfo<T>( 111 context->TakeHandleAs<mojo::MessagePipeHandle>(input->handle), 112 input->version); 113 return true; 114 } 115 }; 116 117 template <typename Base, typename T> 118 struct Serializer<InterfaceRequestDataView<Base>, InterfaceRequest<T>> { 119 static_assert(std::is_base_of<Base, T>::value, "Interface type mismatch."); 120 121 static void Serialize(InterfaceRequest<T>& input, 122 Handle_Data* output, 123 SerializationContext* context) { 124 context->AddHandle(ScopedHandle::From(input.PassMessagePipe()), output); 125 } 126 127 static bool Deserialize(Handle_Data* input, 128 InterfaceRequest<T>* output, 129 SerializationContext* context) { 130 *output = 131 InterfaceRequest<T>(context->TakeHandleAs<MessagePipeHandle>(*input)); 132 return true; 133 } 134 }; 135 136 } // namespace internal 137 } // namespace mojo 138 139 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_SERIALIZATION_H_ 140