// Copyright 2018 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_SERIALIZATION_H_ #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_SERIALIZATION_H_ #include #include "mojo/public/cpp/bindings/associated_group_controller.h" #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" #include "mojo/public/cpp/bindings/associated_interface_request.h" #include "mojo/public/cpp/bindings/interface_data_view.h" #include "mojo/public/cpp/bindings/interface_ptr.h" #include "mojo/public/cpp/bindings/interface_request.h" #include "mojo/public/cpp/bindings/lib/bindings_internal.h" #include "mojo/public/cpp/bindings/lib/serialization_context.h" #include "mojo/public/cpp/bindings/lib/serialization_forward.h" #include "mojo/public/cpp/system/handle.h" #include "mojo/public/cpp/system/message_pipe.h" namespace mojo { namespace internal { template struct Serializer, AssociatedInterfacePtrInfo> { static_assert(std::is_base_of::value, "Interface type mismatch."); static void Serialize(AssociatedInterfacePtrInfo& input, AssociatedInterface_Data* output, SerializationContext* context) { DCHECK(!input.handle().is_valid() || input.handle().pending_association()); context->AddAssociatedInterfaceInfo(input.PassHandle(), input.version(), output); } static bool Deserialize(AssociatedInterface_Data* input, AssociatedInterfacePtrInfo* output, SerializationContext* context) { auto handle = context->TakeAssociatedEndpointHandle(input->handle); if (!handle.is_valid()) { *output = AssociatedInterfacePtrInfo(); } else { output->set_handle(std::move(handle)); output->set_version(input->version); } return true; } }; template struct Serializer, AssociatedInterfaceRequest> { static_assert(std::is_base_of::value, "Interface type mismatch."); static void Serialize(AssociatedInterfaceRequest& input, AssociatedEndpointHandle_Data* output, SerializationContext* context) { DCHECK(!input.handle().is_valid() || input.handle().pending_association()); context->AddAssociatedEndpoint(input.PassHandle(), output); } static bool Deserialize(AssociatedEndpointHandle_Data* input, AssociatedInterfaceRequest* output, SerializationContext* context) { auto handle = context->TakeAssociatedEndpointHandle(*input); if (!handle.is_valid()) *output = AssociatedInterfaceRequest(); else *output = AssociatedInterfaceRequest(std::move(handle)); return true; } }; template struct Serializer, InterfacePtr> { static_assert(std::is_base_of::value, "Interface type mismatch."); static void Serialize(InterfacePtr& input, Interface_Data* output, SerializationContext* context) { InterfacePtrInfo info = input.PassInterface(); context->AddInterfaceInfo(info.PassHandle(), info.version(), output); } static bool Deserialize(Interface_Data* input, InterfacePtr* output, SerializationContext* context) { output->Bind(InterfacePtrInfo( context->TakeHandleAs(input->handle), input->version)); return true; } }; template struct Serializer, InterfacePtrInfo> { static_assert(std::is_base_of::value, "Interface type mismatch."); static void Serialize(InterfacePtrInfo& input, Interface_Data* output, SerializationContext* context) { context->AddInterfaceInfo(input.PassHandle(), input.version(), output); } static bool Deserialize(Interface_Data* input, InterfacePtrInfo* output, SerializationContext* context) { *output = InterfacePtrInfo( context->TakeHandleAs(input->handle), input->version); return true; } }; template struct Serializer, InterfaceRequest> { static_assert(std::is_base_of::value, "Interface type mismatch."); static void Serialize(InterfaceRequest& input, Handle_Data* output, SerializationContext* context) { context->AddHandle(ScopedHandle::From(input.PassMessagePipe()), output); } static bool Deserialize(Handle_Data* input, InterfaceRequest* output, SerializationContext* context) { *output = InterfaceRequest(context->TakeHandleAs(*input)); return true; } }; } // namespace internal } // namespace mojo #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_SERIALIZATION_H_