tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

StreamUtils.h (1809B)


      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/. */
      4 
      5 #ifndef mozilla_dom_StreamUtils_h
      6 #define mozilla_dom_StreamUtils_h
      7 
      8 #include "mozilla/ErrorResult.h"
      9 #include "mozilla/dom/Promise.h"
     10 
     11 class nsIGlobalObject;
     12 
     13 namespace mozilla::dom {
     14 
     15 struct QueuingStrategy;
     16 
     17 double ExtractHighWaterMark(const QueuingStrategy& aStrategy,
     18                            double aDefaultHWM, ErrorResult& aRv);
     19 
     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);
     36  }
     37 
     38  if (aRv.IsUncatchableException()) {
     39    return nullptr;
     40  }
     41 
     42  if (aRv.Failed()) {
     43    return Promise::CreateRejectedWithErrorResult(aGlobal, aRv);
     44  }
     45 
     46  // Step 2. If result is a Promise, then return result.
     47  if (result) {
     48    return result.forget();
     49  }
     50 
     51  // Step 3. Return a promise resolved with undefined.
     52  return Promise::CreateResolvedWithUndefined(aGlobal, aRv);
     53 }
     54 
     55 }  // namespace mozilla::dom
     56 
     57 #endif  // mozilla_dom_StreamUtils_h