#ifndef MOJO_PUBLIC_CPP_BINDINGS_SELF_OWNED_ASSOCIATED_RECEIVER_H_
#define MOJO_PUBLIC_CPP_BINDINGS_SELF_OWNED_ASSOCIATED_RECEIVER_H_
#include <memory>
#include <utility>
#include "base/memory/scoped_refptr.h"
#include "base/task/sequenced_task_runner.h"
#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "mojo/public/cpp/bindings/pending_associated_receiver.h"
#include "mojo/public/cpp/bindings/runtime_features.h"
namespace mojo {
namespace internal {
template <typename Interface>
class SelfOwnedAssociatedReceiver;
}
template <typename Interface>
using SelfOwnedAssociatedReceiverRef =
base::WeakPtr<internal::SelfOwnedAssociatedReceiver<Interface>>;
namespace internal {
template <typename Interface>
class SelfOwnedAssociatedReceiver {
public:
static SelfOwnedAssociatedReceiverRef<Interface> Create(
std::unique_ptr<Interface> impl,
PendingAssociatedReceiver<Interface> receiver,
scoped_refptr<base::SequencedTaskRunner> task_runner = nullptr) {
if (!internal::GetRuntimeFeature_ExpectEnabled<Interface>()) {
return nullptr;
}
SelfOwnedAssociatedReceiver* self_owned = new SelfOwnedAssociatedReceiver(
std::move(impl), std::move(receiver), std::move(task_runner));
return self_owned->weak_factory_.GetWeakPtr();
}
SelfOwnedAssociatedReceiver(const SelfOwnedAssociatedReceiver&) = delete;
SelfOwnedAssociatedReceiver& operator=(const SelfOwnedAssociatedReceiver&) =
delete;
void set_connection_error_handler(base::OnceClosure error_handler) {
DCHECK(receiver_.is_bound());
connection_error_handler_ = std::move(error_handler);
connection_error_with_reason_handler_.Reset();
}
void set_connection_error_with_reason_handler(
ConnectionErrorWithReasonCallback error_handler) {
DCHECK(receiver_.is_bound());
connection_error_with_reason_handler_ = std::move(error_handler);
connection_error_handler_.Reset();
}
void Close() { delete this; }
Interface* impl() { return impl_.get(); }
void FlushForTesting() { receiver_.FlushForTesting(); }
[[nodiscard]] std::unique_ptr<Interface> SwapImplForTesting(
std::unique_ptr<Interface> new_impl) {
std::ignore = receiver_.SwapImplForTesting(new_impl.get());
impl_.swap(new_impl);
return new_impl;
}
void ReportBadMessage(std::string_view error) {
GetBadMessageCallback().Run(error);
}
ReportBadMessageCallback GetBadMessageCallback() {
return base::BindOnce(
[](ReportBadMessageCallback inner_callback,
base::WeakPtr<SelfOwnedAssociatedReceiver> self_owner,
std::string_view error) {
std::move(inner_callback).Run(error);
if (self_owner) {
self_owner->Close();
}
},
receiver_.GetBadMessageCallback(), weak_factory_.GetWeakPtr());
}
private:
SelfOwnedAssociatedReceiver(
std::unique_ptr<Interface> impl,
PendingAssociatedReceiver<Interface> receiver,
scoped_refptr<base::SequencedTaskRunner> task_runner)
: impl_(std::move(impl)),
receiver_(impl_.get(), std::move(receiver), std::move(task_runner)) {
receiver_.set_disconnect_with_reason_handler(base::BindOnce(
&SelfOwnedAssociatedReceiver::OnDisconnect, base::Unretained(this)));
}
~SelfOwnedAssociatedReceiver() = default;
void OnDisconnect(uint32_t custom_reason, const std::string& description) {
if (connection_error_handler_) {
std::move(connection_error_handler_).Run();
} else if (connection_error_with_reason_handler_) {
std::move(connection_error_with_reason_handler_)
.Run(custom_reason, description);
}
Close();
}
std::unique_ptr<Interface> impl_;
base::OnceClosure connection_error_handler_;
ConnectionErrorWithReasonCallback connection_error_with_reason_handler_;
AssociatedReceiver<Interface> receiver_;
base::WeakPtrFactory<SelfOwnedAssociatedReceiver> weak_factory_{this};
};
}
template <typename Interface, typename Impl>
SelfOwnedAssociatedReceiverRef<Interface> MakeSelfOwnedAssociatedReceiver(
std::unique_ptr<Impl> impl,
PendingAssociatedReceiver<Interface> receiver,
scoped_refptr<base::SequencedTaskRunner> task_runner = nullptr) {
return internal::SelfOwnedAssociatedReceiver<Interface>::Create(
std::move(impl), std::move(receiver), std::move(task_runner));
}
}
#endif