Bug 1945643 - Update to mozilla-nimbus-schemas 2025.1.1 r=chumphreys
[gecko.git] / dom / clients / api / ClientDOMUtil.h
bloba1ad6a49ed97d75bfcc34745880cdb10bcb2f91e
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
9 #include "mozilla/dom/ClientIPCTypes.h"
10 #include "mozilla/dom/ClientOpPromise.h"
11 #include "mozilla/dom/DOMMozPromiseRequestHolder.h"
12 #include "mozilla/dom/WorkerPrivate.h"
14 class nsIGlobalObject;
16 namespace mozilla::dom {
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);
26 nsCOMPtr<nsISerialEventTarget> target = aGlobal->SerialEventTarget();
27 auto holder =
28 MakeRefPtr<DOMMozPromiseRequestHolder<ClientOpPromise>>(aGlobal);
30 aFunc(aArg, target)
31 ->Then(
32 target, __func__,
33 [aResolve, holder](const ClientOpResult& aResult) {
34 holder->Complete();
35 aResolve(aResult);
37 [aReject, holder](const CopyableErrorResult& aResult) {
38 holder->Complete();
39 aReject(aResult);
41 ->Track(*holder);
44 } // namespace mozilla::dom
46 #endif // _mozilla_dom_ClientDOMUtil_h