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 3 // found in the LICENSE file. 4 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_HANDLE_INTERFACE_SERIALIZATION_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_HANDLE_INTERFACE_SERIALIZATION_H_ 7 8 #include "mojo/public/cpp/bindings/associated_group_controller.h" 9 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" 10 #include "mojo/public/cpp/bindings/associated_interface_request.h" 11 #include "mojo/public/cpp/bindings/interface_ptr.h" 12 #include "mojo/public/cpp/bindings/interface_request.h" 13 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" 14 #include "mojo/public/cpp/bindings/lib/serialization_context.h" 15 #include "mojo/public/cpp/bindings/lib/serialization_forward.h" 16 #include "mojo/public/cpp/system/handle.h" 17 18 namespace mojo { 19 namespace internal { 20 21 template <typename T> 22 struct Serializer<AssociatedInterfacePtrInfo<T>, 23 AssociatedInterfacePtrInfo<T>> { 24 static void Serialize(AssociatedInterfacePtrInfo<T>& input, 25 AssociatedInterface_Data* output, 26 SerializationContext* context) { 27 DCHECK(!input.handle().is_valid() || !input.handle().is_local()); 28 DCHECK_EQ(input.handle().group_controller(), 29 context->group_controller.get()); 30 output->version = input.version(); 31 output->interface_id = input.PassHandle().release(); 32 } 33 34 static bool Deserialize(AssociatedInterface_Data* input, 35 AssociatedInterfacePtrInfo<T>* output, 36 SerializationContext* context) { 37 output->set_handle(context->group_controller->CreateLocalEndpointHandle( 38 FetchAndReset(&input->interface_id))); 39 output->set_version(input->version); 40 return true; 41 } 42 }; 43 44 template <typename T> 45 struct Serializer<AssociatedInterfaceRequest<T>, 46 AssociatedInterfaceRequest<T>> { 47 static void Serialize(AssociatedInterfaceRequest<T>& input, 48 AssociatedInterfaceRequest_Data* output, 49 SerializationContext* context) { 50 DCHECK(!input.handle().is_valid() || !input.handle().is_local()); 51 DCHECK_EQ(input.handle().group_controller(), 52 context->group_controller.get()); 53 output->interface_id = input.PassHandle().release(); 54 } 55 56 static bool Deserialize(AssociatedInterfaceRequest_Data* input, 57 AssociatedInterfaceRequest<T>* output, 58 SerializationContext* context) { 59 output->Bind(context->group_controller->CreateLocalEndpointHandle( 60 FetchAndReset(&input->interface_id))); 61 return true; 62 } 63 }; 64 65 template <typename T> 66 struct Serializer<InterfacePtr<T>, InterfacePtr<T>> { 67 static void Serialize(InterfacePtr<T>& input, 68 Interface_Data* output, 69 SerializationContext* context) { 70 InterfacePtrInfo<T> info = input.PassInterface(); 71 output->handle = context->handles.AddHandle(info.PassHandle().release()); 72 output->version = info.version(); 73 } 74 75 static bool Deserialize(Interface_Data* input, 76 InterfacePtr<T>* output, 77 SerializationContext* context) { 78 output->Bind(InterfacePtrInfo<T>( 79 context->handles.TakeHandleAs<mojo::MessagePipeHandle>(input->handle), 80 input->version)); 81 return true; 82 } 83 }; 84 85 template <typename T> 86 struct Serializer<InterfaceRequest<T>, InterfaceRequest<T>> { 87 static void Serialize(InterfaceRequest<T>& input, 88 Handle_Data* output, 89 SerializationContext* context) { 90 *output = context->handles.AddHandle(input.PassMessagePipe().release()); 91 } 92 93 static bool Deserialize(Handle_Data* input, 94 InterfaceRequest<T>* output, 95 SerializationContext* context) { 96 output->Bind(context->handles.TakeHandleAs<MessagePipeHandle>(*input)); 97 return true; 98 } 99 }; 100 101 template <typename T> 102 struct Serializer<ScopedHandleBase<T>, ScopedHandleBase<T>> { 103 static void Serialize(ScopedHandleBase<T>& input, 104 Handle_Data* output, 105 SerializationContext* context) { 106 *output = context->handles.AddHandle(input.release()); 107 } 108 109 static bool Deserialize(Handle_Data* input, 110 ScopedHandleBase<T>* output, 111 SerializationContext* context) { 112 *output = context->handles.TakeHandleAs<T>(*input); 113 return true; 114 } 115 }; 116 117 } // namespace internal 118 } // namespace mojo 119 120 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_HANDLE_INTERFACE_SERIALIZATION_H_ 121