ClientDOMUtil.h (1655B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 #ifndef _mozilla_dom_ClientDOMUtil_h 7 #define _mozilla_dom_ClientDOMUtil_h 8 9 #include "mozilla/dom/ClientIPCTypes.h" 10 #include "mozilla/dom/ClientOpPromise.h" 11 #include "mozilla/dom/DOMMozPromiseRequestHolder.h" 12 #include "mozilla/dom/WorkerPrivate.h" 13 14 class nsIGlobalObject; 15 16 namespace mozilla::dom { 17 18 // Utility method to properly execute a ClientManager operation. It 19 // will properly hold a worker thread alive and avoid executing callbacks 20 // if the thread is shutting down. 21 template <typename Func, typename Arg, typename Resolve, typename Reject> 22 void StartClientManagerOp(Func aFunc, const Arg& aArg, nsIGlobalObject* aGlobal, 23 Resolve aResolve, Reject aReject) { 24 MOZ_DIAGNOSTIC_ASSERT(aGlobal); 25 26 nsCOMPtr<nsISerialEventTarget> target = aGlobal->SerialEventTarget(); 27 auto holder = 28 MakeRefPtr<DOMMozPromiseRequestHolder<ClientOpPromise>>(aGlobal); 29 30 aFunc(aArg, target) 31 ->Then( 32 target, __func__, 33 [aResolve, holder](const ClientOpResult& aResult) { 34 holder->Complete(); 35 aResolve(aResult); 36 }, 37 [aReject, holder](const CopyableErrorResult& aResult) { 38 holder->Complete(); 39 aReject(aResult); 40 }) 41 ->Track(*holder); 42 } 43 44 } // namespace mozilla::dom 45 46 #endif // _mozilla_dom_ClientDOMUtil_h