Bug 1457157 P3 Replace ClientHandleOpChild MozPromise direct std::function callbacks. r=baku
authorBen Kelly <ben@wanderview.com>
Wed, 02 May 2018 06:29:27 -0700
changeset 470232 9184164d3c6c714b148f6ba36df3b85f6a9b5f1b
parent 470231 c86679a9bf8360dd95790c63768a70913467c46a
child 470233 0bac87ce995f538f0274770d03795f0d274d86bb
push id9179
push userarchaeopteryx@coole-files.de
push dateThu, 03 May 2018 15:28:18 +0000
treeherdermozilla-beta@e6f9ade8bca7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1457157
milestone61.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1457157 P3 Replace ClientHandleOpChild MozPromise direct std::function callbacks. r=baku
dom/clients/manager/ClientHandle.cpp
dom/clients/manager/ClientHandle.h
dom/clients/manager/ClientHandleOpChild.cpp
dom/clients/manager/ClientHandleOpChild.h
dom/clients/manager/ClientOpPromise.h
--- a/dom/clients/manager/ClientHandle.cpp
+++ b/dom/clients/manager/ClientHandle.cpp
@@ -32,42 +32,37 @@ ClientHandle::Shutdown()
     return;
   }
 
   ShutdownThing();
 
   mManager = nullptr;
 }
 
-already_AddRefed<ClientOpPromise>
-ClientHandle::StartOp(const ClientOpConstructorArgs& aArgs)
+void
+ClientHandle::StartOp(const ClientOpConstructorArgs& aArgs,
+                      const ClientOpCallback&& aResolveCallback,
+                      const ClientOpCallback&& aRejectCallback)
 {
-  RefPtr<ClientOpPromise::Private> 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<ClientHandle> 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<ClientOpPromise> ref = promise.get();
-  return ref.forget();
 }
 
 void
 ClientHandle::OnShutdownThing()
 {
   NS_ASSERT_OWNINGTHREAD(ClientHandle);
   if (!mDetachPromise) {
     return;
@@ -119,39 +114,34 @@ ClientHandle::Info() const
 }
 
 RefPtr<GenericPromise>
 ClientHandle::Control(const ServiceWorkerDescriptor& aServiceWorker)
 {
   RefPtr<GenericPromise::Private> outerPromise =
     new GenericPromise::Private(__func__);
 
-  RefPtr<ClientOpPromise> innerPromise =
-    StartOp(ClientControlledArgs(aServiceWorker.ToIPC()));
-
-  innerPromise->Then(mSerialEventTarget, __func__,
+  StartOp(ClientControlledArgs(aServiceWorker.ToIPC()),
     [outerPromise](const ClientOpResult& aResult) {
       outerPromise->Resolve(true, __func__);
     },
     [outerPromise](const ClientOpResult& aResult) {
       outerPromise->Reject(aResult.get_nsresult(), __func__);
     });
 
   return outerPromise.forget();
 }
 
 RefPtr<ClientStatePromise>
 ClientHandle::Focus()
 {
   RefPtr<ClientStatePromise::Private> outerPromise =
     new ClientStatePromise::Private(__func__);
 
-  RefPtr<ClientOpPromise> 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) {
       outerPromise->Reject(aResult.get_nsresult(), __func__);
     });
 
   RefPtr<ClientStatePromise> ref = outerPromise.get();
   return ref.forget();
@@ -175,18 +165,17 @@ ClientHandle::PostMessage(StructuredClon
                                                       args.clonedData())) {
     ref = GenericPromise::CreateAndReject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__);
     return ref.forget();
   }
 
   RefPtr<GenericPromise::Private> outerPromise =
     new GenericPromise::Private(__func__);
 
-  RefPtr<ClientOpPromise> innerPromise = StartOp(args);
-  innerPromise->Then(mSerialEventTarget, __func__,
+  StartOp(args,
     [outerPromise](const ClientOpResult& aResult) {
       outerPromise->Resolve(true, __func__);
     }, [outerPromise](const ClientOpResult& aResult) {
       outerPromise->Reject(aResult.get_nsresult(), __func__);
     });
 
   ref = outerPromise.get();
   return ref.forget();
--- a/dom/clients/manager/ClientHandle.h
+++ b/dom/clients/manager/ClientHandle.h
@@ -45,18 +45,20 @@ class ClientHandle final : public Client
   RefPtr<GenericPromise::Private> mDetachPromise;
   ClientInfo mClientInfo;
 
   ~ClientHandle();
 
   void
   Shutdown();
 
-  already_AddRefed<ClientOpPromise>
-  StartOp(const ClientOpConstructorArgs& aArgs);
+  void
+  StartOp(const ClientOpConstructorArgs& aArgs,
+          const ClientOpCallback&& aResolveCallback,
+          const ClientOpCallback&& aRejectCallback);
 
   // ClientThing interface
   void
   OnShutdownThing() override;
 
   // Private methods called by ClientHandleChild
   void
   ExecutionReady(const ClientInfo& aClientInfo);
--- a/dom/clients/manager/ClientHandleOpChild.cpp
+++ b/dom/clients/manager/ClientHandleOpChild.cpp
@@ -7,42 +7,35 @@
 #include "ClientHandleOpChild.h"
 
 namespace mozilla {
 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
 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
 } // namespace mozilla
--- a/dom/clients/manager/ClientHandleOpChild.h
+++ b/dom/clients/manager/ClientHandleOpChild.h
@@ -3,35 +3,36 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 #ifndef _mozilla_dom_ClientHandleOpChild_h
 #define _mozilla_dom_ClientHandleOpChild_h
 
 #include "mozilla/dom/ClientOpPromise.h"
 #include "mozilla/dom/PClientHandleOpChild.h"
-#include "mozilla/MozPromise.h"
 
 namespace mozilla {
 namespace dom {
 
 class ClientHandleOpChild final : public PClientHandleOpChild
 {
-  RefPtr<ClientOpPromise::Private> mPromise;
+  const ClientOpCallback mResolveCallback;
+  const ClientOpCallback mRejectCallback;
 
   // PClientHandleOpChild interface
   void
   ActorDestroy(ActorDestroyReason aReason) override;
 
   mozilla::ipc::IPCResult
   Recv__delete__(const ClientOpResult& aResult) override;
 
 public:
   ClientHandleOpChild(const ClientOpConstructorArgs& aArgs,
-                      ClientOpPromise::Private* aPromise);
+                      const ClientOpCallback&& aResolveCallback,
+                      const ClientOpCallback&& aRejectCallback);
 
-  ~ClientHandleOpChild();
+  ~ClientHandleOpChild() = default;
 };
 
 } // namespace dom
 } // namespace mozilla
 
 #endif // _mozilla_dom_ClientHandleOpChild_h
--- a/dom/clients/manager/ClientOpPromise.h
+++ b/dom/clients/manager/ClientOpPromise.h
@@ -13,12 +13,14 @@ namespace dom {
 
 class ClientOpResult;
 class ClientState;
 
 typedef MozPromise<ClientOpResult, nsresult, false> ClientOpPromise;
 
 typedef MozPromise<ClientState, nsresult, false> ClientStatePromise;
 
+typedef std::function<void(const ClientOpResult&)> ClientOpCallback;
+
 } // namespace dom
 } // namespace mozilla
 
 #endif // _mozilla_dom_ClientOpPromise_h