From 24a39442febd166d03ce2a5fe284425e736e1022 Mon Sep 17 00:00:00 2001 From: Ben Kelly Date: Wed, 2 May 2018 06:29:27 -0700 Subject: [PATCH] Bug 1457157 P3 Replace ClientHandleOpChild MozPromise direct std::function callbacks. r=baku --- dom/clients/manager/ClientHandle.cpp | 39 ++++++++------------- dom/clients/manager/ClientHandle.h | 6 ++-- dom/clients/manager/ClientHandleOpChild.cpp | 25 +++++-------- dom/clients/manager/ClientHandleOpChild.h | 9 ++--- dom/clients/manager/ClientOpPromise.h | 2 ++ 5 files changed, 34 insertions(+), 47 deletions(-) diff --git a/dom/clients/manager/ClientHandle.cpp b/dom/clients/manager/ClientHandle.cpp index 4daf07c87edb..555f878cbdd5 100644 --- a/dom/clients/manager/ClientHandle.cpp +++ b/dom/clients/manager/ClientHandle.cpp @@ -37,32 +37,27 @@ ClientHandle::Shutdown() mManager = nullptr; } -already_AddRefed -ClientHandle::StartOp(const ClientOpConstructorArgs& aArgs) +void +ClientHandle::StartOp(const ClientOpConstructorArgs& aArgs, + const ClientOpCallback&& aResolveCallback, + const ClientOpCallback&& aRejectCallback) { - RefPtr promise = - new ClientOpPromise::Private(__func__); - // Hold a ref to the client until the remote operation completes. Otherwise // the ClientHandle might get de-refed and teardown the actor before we // get an answer. RefPtr kungFuGrip = this; - promise->Then(mSerialEventTarget, __func__, - [kungFuGrip] (const ClientOpResult &) { }, - [kungFuGrip] (nsresult) { }); - MaybeExecute([aArgs, promise] (ClientHandleChild* aActor) { - ClientHandleOpChild* actor = new ClientHandleOpChild(aArgs, promise); + MaybeExecute([aArgs, kungFuGrip, aRejectCallback, + resolve = Move(aResolveCallback)] (ClientHandleChild* aActor) { + ClientHandleOpChild* actor = + new ClientHandleOpChild(aArgs, Move(resolve), Move(aRejectCallback)); if (!aActor->SendPClientHandleOpConstructor(actor, aArgs)) { - // Constructor failure will reject promise via ActorDestroy() + // Constructor failure will call reject callback via ActorDestroy() return; } - }, [promise] { - promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__); + }, [aRejectCallback, kungFuGrip] { + aRejectCallback(NS_ERROR_DOM_INVALID_STATE_ERR); }); - - RefPtr ref = promise.get(); - return ref.forget(); } void @@ -124,10 +119,7 @@ ClientHandle::Control(const ServiceWorkerDescriptor& aServiceWorker) RefPtr outerPromise = new GenericPromise::Private(__func__); - RefPtr innerPromise = - StartOp(ClientControlledArgs(aServiceWorker.ToIPC())); - - innerPromise->Then(mSerialEventTarget, __func__, + StartOp(ClientControlledArgs(aServiceWorker.ToIPC()), [outerPromise](const ClientOpResult& aResult) { outerPromise->Resolve(true, __func__); }, @@ -144,9 +136,7 @@ ClientHandle::Focus() RefPtr outerPromise = new ClientStatePromise::Private(__func__); - RefPtr innerPromise = StartOp(ClientFocusArgs()); - - innerPromise->Then(mSerialEventTarget, __func__, + StartOp(ClientFocusArgs(), [outerPromise](const ClientOpResult& aResult) { outerPromise->Resolve(ClientState::FromIPC(aResult.get_IPCClientState()), __func__); }, [outerPromise](const ClientOpResult& aResult) { @@ -180,8 +170,7 @@ ClientHandle::PostMessage(StructuredCloneData& aData, RefPtr outerPromise = new GenericPromise::Private(__func__); - RefPtr innerPromise = StartOp(args); - innerPromise->Then(mSerialEventTarget, __func__, + StartOp(args, [outerPromise](const ClientOpResult& aResult) { outerPromise->Resolve(true, __func__); }, [outerPromise](const ClientOpResult& aResult) { diff --git a/dom/clients/manager/ClientHandle.h b/dom/clients/manager/ClientHandle.h index 28cf7f1992be..a9d830725e58 100644 --- a/dom/clients/manager/ClientHandle.h +++ b/dom/clients/manager/ClientHandle.h @@ -50,8 +50,10 @@ class ClientHandle final : public ClientThing void Shutdown(); - already_AddRefed - StartOp(const ClientOpConstructorArgs& aArgs); + void + StartOp(const ClientOpConstructorArgs& aArgs, + const ClientOpCallback&& aResolveCallback, + const ClientOpCallback&& aRejectCallback); // ClientThing interface void diff --git a/dom/clients/manager/ClientHandleOpChild.cpp b/dom/clients/manager/ClientHandleOpChild.cpp index 2e909babaecd..1bb90266058f 100644 --- a/dom/clients/manager/ClientHandleOpChild.cpp +++ b/dom/clients/manager/ClientHandleOpChild.cpp @@ -12,10 +12,7 @@ namespace dom { void ClientHandleOpChild::ActorDestroy(ActorDestroyReason aReason) { - if (mPromise) { - mPromise->Reject(NS_ERROR_ABORT, __func__); - mPromise = nullptr; - } + mRejectCallback(NS_ERROR_DOM_ABORT_ERR); } mozilla::ipc::IPCResult @@ -23,25 +20,21 @@ ClientHandleOpChild::Recv__delete__(const ClientOpResult& aResult) { if (aResult.type() == ClientOpResult::Tnsresult && NS_FAILED(aResult.get_nsresult())) { - mPromise->Reject(aResult.get_nsresult(), __func__); - mPromise = nullptr; + mRejectCallback(aResult.get_nsresult()); return IPC_OK(); } - mPromise->Resolve(aResult, __func__); - mPromise = nullptr; + mResolveCallback(aResult); return IPC_OK(); } ClientHandleOpChild::ClientHandleOpChild(const ClientOpConstructorArgs& aArgs, - ClientOpPromise::Private* aPromise) - : mPromise(aPromise) + const ClientOpCallback&& aResolveCallback, + const ClientOpCallback&& aRejectCallback) + : mResolveCallback(Move(aResolveCallback)) + , mRejectCallback(Move(aRejectCallback)) { - MOZ_DIAGNOSTIC_ASSERT(mPromise); -} - -ClientHandleOpChild::~ClientHandleOpChild() -{ - MOZ_DIAGNOSTIC_ASSERT(!mPromise); + MOZ_DIAGNOSTIC_ASSERT(mResolveCallback); + MOZ_DIAGNOSTIC_ASSERT(mRejectCallback); } } // namespace dom diff --git a/dom/clients/manager/ClientHandleOpChild.h b/dom/clients/manager/ClientHandleOpChild.h index 3bd125c0fbea..e793a1cbf774 100644 --- a/dom/clients/manager/ClientHandleOpChild.h +++ b/dom/clients/manager/ClientHandleOpChild.h @@ -8,14 +8,14 @@ #include "mozilla/dom/ClientOpPromise.h" #include "mozilla/dom/PClientHandleOpChild.h" -#include "mozilla/MozPromise.h" namespace mozilla { namespace dom { class ClientHandleOpChild final : public PClientHandleOpChild { - RefPtr mPromise; + const ClientOpCallback mResolveCallback; + const ClientOpCallback mRejectCallback; // PClientHandleOpChild interface void @@ -26,9 +26,10 @@ class ClientHandleOpChild final : public PClientHandleOpChild public: ClientHandleOpChild(const ClientOpConstructorArgs& aArgs, - ClientOpPromise::Private* aPromise); + const ClientOpCallback&& aResolveCallback, + const ClientOpCallback&& aRejectCallback); - ~ClientHandleOpChild(); + ~ClientHandleOpChild() = default; }; } // namespace dom diff --git a/dom/clients/manager/ClientOpPromise.h b/dom/clients/manager/ClientOpPromise.h index 6e9dc083c847..4667079812b2 100644 --- a/dom/clients/manager/ClientOpPromise.h +++ b/dom/clients/manager/ClientOpPromise.h @@ -18,6 +18,8 @@ typedef MozPromise ClientOpPromise; typedef MozPromise ClientStatePromise; +typedef std::function ClientOpCallback; + } // namespace dom } // namespace mozilla