Bug 1890277: part 2) Add `require-trusted-types-for` directive to CSP parser, guarded...
[gecko.git] / dom / streams / StreamUtils.h
blobf0b09c63b825f70cd337c6ece1738756e5542eea
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef mozilla_dom_StreamUtils_h
6 #define mozilla_dom_StreamUtils_h
8 #include "mozilla/ErrorResult.h"
9 #include "mozilla/dom/Promise.h"
11 class nsIGlobalObject;
13 namespace mozilla::dom {
15 struct QueuingStrategy;
17 double ExtractHighWaterMark(const QueuingStrategy& aStrategy,
18 double aDefaultHWM, ErrorResult& aRv);
20 // Promisification algorithm, shared among:
21 // Step 2 and 3 of https://streams.spec.whatwg.org/#readablestream-set-up
22 // Step 2 and 3 of
23 // https://streams.spec.whatwg.org/#readablestream-set-up-with-byte-reading-support
24 // Step 2 and 3 of https://streams.spec.whatwg.org/#writablestream-set-up
25 // Step 5 and 6 of https://streams.spec.whatwg.org/#transformstream-set-up
26 template <typename T>
27 MOZ_CAN_RUN_SCRIPT static already_AddRefed<Promise> PromisifyAlgorithm(
28 nsIGlobalObject* aGlobal, T aFunc, mozilla::ErrorResult& aRv) {
29 // Step 1. Let result be the result of running (algorithm). If this throws an
30 // exception e, return a promise rejected with e.
31 RefPtr<Promise> result;
32 if constexpr (!std::is_same<decltype(aFunc(aRv)), void>::value) {
33 result = aFunc(aRv);
34 } else {
35 aFunc(aRv);
38 if (aRv.IsUncatchableException()) {
39 return nullptr;
42 if (aRv.Failed()) {
43 return Promise::CreateRejectedWithErrorResult(aGlobal, aRv);
46 // Step 2. If result is a Promise, then return result.
47 if (result) {
48 return result.forget();
51 // Step 3. Return a promise resolved with undefined.
52 return Promise::CreateResolvedWithUndefined(aGlobal, aRv);
55 } // namespace mozilla::dom
57 #endif // mozilla_dom_StreamUtils_h