#ifndef MOJO_PUBLIC_CPP_BINDINGS_PENDING_ASSOCIATED_RECEIVER_H_
#define MOJO_PUBLIC_CPP_BINDINGS_PENDING_ASSOCIATED_RECEIVER_H_
#include <stdint.h>
#include <type_traits>
#include <utility>
#include "base/compiler_specific.h"
#include "base/task/sequenced_task_runner.h"
#include "build/build_config.h"
#include "mojo/public/cpp/bindings/lib/multiplex_router.h"
#include "mojo/public/cpp/bindings/runtime_features.h"
#include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
#include "mojo/public/cpp/system/message_pipe.h"
namespace mojo {
template <typename T>
class PendingAssociatedRemote;
template <typename T>
struct PendingAssociatedReceiverConverter;
template <typename Interface>
class PendingAssociatedReceiver {
public:
PendingAssociatedReceiver() = default;
PendingAssociatedReceiver(PendingAssociatedReceiver&& other)
: handle_(std::move(other.handle_)) {}
explicit PendingAssociatedReceiver(ScopedInterfaceEndpointHandle handle)
: handle_(std::move(handle)) {}
template <
typename T,
std::enable_if_t<std::is_same<
PendingAssociatedReceiver<Interface>,
std::invoke_result_t<decltype(&PendingAssociatedReceiverConverter<
T>::template To<Interface>),
T&&>>::value>* = nullptr>
PendingAssociatedReceiver(T&& other)
: PendingAssociatedReceiver(
PendingAssociatedReceiverConverter<T>::template To<Interface>(
std::move(other))) {}
PendingAssociatedReceiver(const PendingAssociatedReceiver&) = delete;
PendingAssociatedReceiver& operator=(const PendingAssociatedReceiver&) =
delete;
~PendingAssociatedReceiver() = default;
PendingAssociatedReceiver& operator=(PendingAssociatedReceiver&& other) {
handle_ = std::move(other.handle_);
return *this;
}
bool is_valid() const { return handle_.is_valid(); }
explicit operator bool() const { return is_valid(); }
ScopedInterfaceEndpointHandle PassHandle() { return std::move(handle_); }
const ScopedInterfaceEndpointHandle& handle() const { return handle_; }
void set_handle(ScopedInterfaceEndpointHandle handle) {
handle_ = std::move(handle);
}
void reset() { handle_.reset(); }
void ResetWithReason(uint32_t custom_reason, const std::string& description) {
handle_.ResetWithReason(custom_reason, description);
}
[[nodiscard]] REINITIALIZES_AFTER_MOVE PendingAssociatedRemote<Interface>
InitWithNewEndpointAndPassRemote();
void EnableUnassociatedUsage() {
DCHECK(is_valid());
MessagePipe pipe;
scoped_refptr<internal::MultiplexRouter> router0 =
internal::MultiplexRouter::CreateAndStartReceiving(
std::move(pipe.handle0), internal::MultiplexRouter::MULTI_INTERFACE,
false, base::SequencedTaskRunner::GetCurrentDefault());
scoped_refptr<internal::MultiplexRouter> router1 =
internal::MultiplexRouter::CreateAndStartReceiving(
std::move(pipe.handle1), internal::MultiplexRouter::MULTI_INTERFACE,
true, base::SequencedTaskRunner::GetCurrentDefault());
InterfaceId id = router1->AssociateInterface(PassHandle());
set_handle(router0->CreateLocalEndpointHandle(id));
}
private:
ScopedInterfaceEndpointHandle handle_;
};
class COMPONENT_EXPORT(MOJO_CPP_BINDINGS) NullAssociatedReceiver {
public:
template <typename Interface>
operator PendingAssociatedReceiver<Interface>() const {
return PendingAssociatedReceiver<Interface>();
}
};
}
#include "mojo/public/cpp/bindings/pending_associated_remote.h"
namespace mojo {
template <typename Interface>
PendingAssociatedRemote<Interface>
PendingAssociatedReceiver<Interface>::InitWithNewEndpointAndPassRemote() {
if (!internal::GetRuntimeFeature_ExpectEnabled<Interface>()) {
return PendingAssociatedRemote<Interface>();
}
ScopedInterfaceEndpointHandle remote_handle;
ScopedInterfaceEndpointHandle::CreatePairPendingAssociation(&handle_,
&remote_handle);
return PendingAssociatedRemote<Interface>(std::move(remote_handle), 0u);
}
}
#endif